Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add context to extension builder #22

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/core/ExtensionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,24 @@ enum PluginPriority {

const DEFAULT_PRIORITY = PluginPriority.Medium;

type BuilderContext<T extends object> = {
has(key: keyof T): boolean;
get<K extends keyof T>(key: K): T[K] | undefined;
set<K extends keyof T>(key: K, value: T[K]): BuilderContext<T>;
};

declare global {
namespace YfmEditor {
interface Context {}
}
}

export class ExtensionBuilder {
static createContext(): BuilderContext<YfmEditor.Context> {
return new Map();
}

// eslint-disable-next-line @typescript-eslint/member-ordering
static readonly PluginPriority = PluginPriority;
readonly PluginPriority = ExtensionBuilder.PluginPriority;

Expand All @@ -48,6 +65,12 @@ export class ExtensionBuilder {
#plugins: {cb: AddPmPluginCallback; priority: number}[] = [];
#actions: [string, AddActionCallback][] = [];

readonly context: BuilderContext<YfmEditor.Context>;

constructor(context?: BuilderContext<YfmEditor.Context>) {
this.context = context ?? ExtensionBuilder.createContext();
}

use(extension: Extension): this;
use<T>(extension: ExtensionWithOptions<T>, options: T): this;
use(extension: ExtensionWithParams, ...params: any[]): this {
Expand Down
31 changes: 28 additions & 3 deletions src/extensions/markdown/Breaks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {NodeType} from 'prosemirror-model';
import {chainCommands, exitCode} from 'prosemirror-commands';
import {logger} from '../../../logger';
import type {ExtensionAuto, Keymap} from '../../../core';
import {isMac} from '../../../utils/platform';
import {nodeTypeFactory} from '../../../utils/schema';
Expand All @@ -10,12 +11,25 @@ export const hbType = nodeTypeFactory(hardBreak);
export const sbType = nodeTypeFactory(softBreak);

export type BreaksOptions = {
// TODO: [builder context] get this parameter from builder context
/** @default 'hard' */
/**
* This option is used if the 'breaks' parameter is not specified via the context
* @default 'hard'
*/
// TODO: [context] make this deprecated
preferredBreak?: 'hard' | 'soft';
};

export const Breaks: ExtensionAuto<BreaksOptions> = (builder, opts) => {
let preferredBreak: 'hard' | 'soft';
if (builder.context.has('breaks')) {
preferredBreak = builder.context.get('breaks') ? 'soft' : 'hard';
} else {
preferredBreak = opts.preferredBreak ?? 'hard';
logger.info(
"[Breaks extension]: Parameter 'breaks' is not defined in context; value from options is used",
);
}

builder.addNode(hardBreak, () => ({
spec: {
inline: true,
Expand Down Expand Up @@ -62,7 +76,7 @@ export const Breaks: ExtensionAuto<BreaksOptions> = (builder, opts) => {
}));

builder.addKeymap(({schema}) => {
const cmd = addBr((opts?.preferredBreak === 'soft' ? sbType : hbType)(schema));
const cmd = addBr((preferredBreak === 'soft' ? sbType : hbType)(schema));
const keys: Keymap = {
'Shift-Enter': cmd,
};
Expand All @@ -80,3 +94,14 @@ const addBr = (br: NodeType) =>
dispatch?.(state.tr.replaceSelectionWith(br.create()).scrollIntoView());
return true;
});

declare global {
namespace YfmEditor {
interface Context {
/**
* Same as @type {MarkdownIt.Options.breaks}
*/
breaks: boolean;
}
}
}
17 changes: 17 additions & 0 deletions src/extensions/markdown/Html/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {logger} from '../../../logger';
import {createExtension, ExtensionAuto} from '../../../core';
import {HtmlNode} from './const';
import {fromYfm} from './fromYfm';
import {spec} from './spec';
import {toYfm} from './toYfm';

export const Html: ExtensionAuto = (builder) => {
if (builder.context.has('html') && builder.context.get('html') === false) {
logger.info('[HTML extension]: Skip extension, because HTML disabled via context');
return;
}

builder.addNode(HtmlNode.Block, () => ({
spec: spec[HtmlNode.Block],
fromYfm: {tokenSpec: fromYfm[HtmlNode.Block]},
Expand All @@ -24,3 +30,14 @@ export const Html: ExtensionAuto = (builder) => {
* Remove after WIKI-16660
*/
export const HtmlE = createExtension((b, o = {}) => b.use(Html, o));

declare global {
namespace YfmEditor {
interface Context {
/**
* Same as @type {MarkdownIt.Options.html}
*/
html: boolean;
}
}
}