Skip to content

Commit

Permalink
feat: 🎸 move slice inject to part of factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Sep 25, 2022
1 parent 35df9a1 commit 59b94b8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 38 deletions.
12 changes: 5 additions & 7 deletions packages/preset-commonmark/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import { getInlineSyncPlugin, inlineSyncConfigCtx } from './inline-sync';

export { inlineSyncConfigCtx };
export const commonmarkPlugins = [
createPlugin(
() => ({
prosePlugins: (_, ctx) => [getInlineNodesCursorPlugin(), getInlineSyncPlugin(ctx)],
remarkPlugins: () => [links, filterHTMLPlugin, addOrderInList],
}),
[inlineSyncConfigCtx],
)(),
createPlugin(() => ({
injectSlices: [inlineSyncConfigCtx],
prosePlugins: (_, ctx) => [getInlineNodesCursorPlugin(), getInlineSyncPlugin(ctx)],
remarkPlugins: () => [links, filterHTMLPlugin, addOrderInList],
}))(),
];
9 changes: 3 additions & 6 deletions packages/utils/src/factory/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Copyright 2021, Milkdown by Mirone. */
import { Attrs, CmdKey, Ctx, emotionCtx, Slice, themeManagerCtx } from '@milkdown/core';
import { Attrs, CmdKey, Ctx, emotionCtx, themeManagerCtx } from '@milkdown/core';
import { themeMustInstalled } from '@milkdown/exception';

import {
Expand Down Expand Up @@ -62,16 +62,13 @@ export const withExtend =
factory: Factory<SupportedKeys, Options, Type, Rest>,
creator: (
factory: Factory<SupportedKeys, Options, Type, Rest>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inject?: Slice<any>[],
) => WithExtend<SupportedKeys, Options, Type, Rest>,
) =>
(origin: AddMetadata<SupportedKeys, Options>): WithExtend<SupportedKeys, Options, Type, Rest> => {
type Ext = WithExtend<SupportedKeys, Options, Type, Rest>;
const next = origin as Ext;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const extend = (extendFactory: Parameters<Ext['extend']>[0], inject?: Slice<any>[]) =>
creator((...args) => extendFactory(factory(...args), ...args), inject);
const extend = (extendFactory: Parameters<Ext['extend']>[0]) =>
creator((...args) => extendFactory(factory(...args), ...args));

next.extend = extend as Ext['extend'];

Expand Down
20 changes: 16 additions & 4 deletions packages/utils/src/factory/create-mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MarkType } from '@milkdown/prose/model';
import { MarkViewConstructor } from '@milkdown/prose/view';

import { pipe } from '../pipe';
import { AnySlice, CommonOptions, Factory, UnknownRecord, WithExtend } from '../types';
import { CommonOptions, Factory, UnknownRecord, WithExtend } from '../types';
import { addMetadata, getThemeUtils, withExtend } from './common';
import {
applyProsePlugins,
Expand All @@ -24,6 +24,7 @@ import {
idPipeCtx,
injectPipeEnv,
injectSlices,
injectSlicesPipeCtx,
optionsPipeCtx,
shortcutsPipeCtx,
waitThemeReady,
Expand Down Expand Up @@ -52,7 +53,6 @@ export type MarkCreator<SupportedKeys extends string, Options extends UnknownRec

export const createMark = <SupportedKeys extends string = string, Options extends UnknownRecord = UnknownRecord>(
factory: MarkFactory<SupportedKeys, Options>,
inject?: AnySlice[],
): MarkCreator<string, Options> =>
pipe(
addMetadata,
Expand All @@ -65,15 +65,27 @@ export const createMark = <SupportedKeys extends string = string, Options extend
const utils = getThemeUtils(ctx, options);
const plugin = factory(utils, options);

const { id, commands, remarkPlugins, schema, inputRules, shortcuts, prosePlugins, view } = plugin;
const {
id,
commands,
remarkPlugins,
schema,
inputRules,
shortcuts,
prosePlugins,
view,
injectSlices,
} = plugin;

const viewOption = options?.view;
const injectOptions = injectSlices ?? [];

const pluginOptions = {
...(options || {}),
view: viewOption ? (ctx: Ctx) => ({ [id]: viewOption(ctx) }) : undefined,
};

pipelineCtx.set(injectSlicesPipeCtx, injectOptions);
pipelineCtx.set(idPipeCtx, id);
pipelineCtx.set(optionsPipeCtx, pluginOptions);
pipelineCtx.set(getRemarkPluginsPipeCtx, remarkPlugins);
Expand All @@ -98,9 +110,9 @@ export const createMark = <SupportedKeys extends string = string, Options extend

const runner = run([
injectPipeEnv,
injectSlices(inject),
waitThemeReady,
setPipelineEnv,
injectSlices,
applyRemarkPlugins,
applySchema,
createCommands,
Expand Down
20 changes: 16 additions & 4 deletions packages/utils/src/factory/create-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NodeType } from '@milkdown/prose/model';
import { NodeViewConstructor } from '@milkdown/prose/view';

import { pipe } from '../pipe';
import { AnySlice, CommonOptions, Factory, UnknownRecord, WithExtend } from '../types';
import { CommonOptions, Factory, UnknownRecord, WithExtend } from '../types';
import { addMetadata, getThemeUtils, withExtend } from './common';
import {
applyProsePlugins,
Expand All @@ -24,6 +24,7 @@ import {
idPipeCtx,
injectPipeEnv,
injectSlices,
injectSlicesPipeCtx,
optionsPipeCtx,
shortcutsPipeCtx,
waitThemeReady,
Expand All @@ -50,7 +51,6 @@ export type NodeCreator<

export const createNode = <SupportedKeys extends string = string, Options extends UnknownRecord = UnknownRecord>(
factory: NodeFactory<SupportedKeys, Options>,
inject?: AnySlice[],
): NodeCreator<SupportedKeys, Options> =>
pipe(
addMetadata,
Expand All @@ -63,15 +63,27 @@ export const createNode = <SupportedKeys extends string = string, Options extend
const utils = getThemeUtils(ctx, options);
const plugin = factory(utils, options);

const { id, commands, remarkPlugins, schema, inputRules, shortcuts, prosePlugins, view } = plugin;
const {
id,
commands,
remarkPlugins,
schema,
inputRules,
shortcuts,
prosePlugins,
view,
injectSlices,
} = plugin;

const viewOption = options?.view;
const injectOptions = injectSlices ?? [];

const pluginOptions = {
...(options || {}),
view: viewOption ? (ctx: Ctx) => ({ [id]: viewOption(ctx) }) : undefined,
};

pipelineCtx.set(injectSlicesPipeCtx, injectOptions);
pipelineCtx.set(idPipeCtx, id);
pipelineCtx.set(optionsPipeCtx, pluginOptions);
pipelineCtx.set(getRemarkPluginsPipeCtx, remarkPlugins);
Expand All @@ -96,9 +108,9 @@ export const createNode = <SupportedKeys extends string = string, Options extend

const runner = run([
injectPipeEnv,
injectSlices(inject),
waitThemeReady,
setPipelineEnv,
injectSlices,
applyRemarkPlugins,
applySchema,
createCommands,
Expand Down
12 changes: 8 additions & 4 deletions packages/utils/src/factory/create-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MarkType, NodeType } from '@milkdown/prose/model';
import { MarkViewConstructor, NodeViewConstructor } from '@milkdown/prose/view';

import { pipe } from '../pipe';
import { AnySlice, CommonOptions, Factory, UnknownRecord, WithExtend } from '../types';
import { CommonOptions, Factory, UnknownRecord, WithExtend } from '../types';
import { addMetadata, getThemeUtils, withExtend } from './common';
import {
applyProsePlugins,
Expand All @@ -23,6 +23,7 @@ import {
getViewPipeCtx,
injectPipeEnv,
injectSlices,
injectSlicesPipeCtx,
optionsPipeCtx,
shortcutsPipeCtx,
waitThemeReady,
Expand Down Expand Up @@ -62,7 +63,6 @@ export const createPlugin = <
MarkKeys extends string = string,
>(
factory: PluginFactory<SupportedKeys, Options, NodeKeys, MarkKeys>,
inject?: AnySlice[],
): WithExtend<SupportedKeys, Options, TypeMapping<NodeKeys, MarkKeys>, PluginRest<NodeKeys, MarkKeys>> =>
pipe(
addMetadata,
Expand All @@ -75,8 +75,12 @@ export const createPlugin = <
const utils = getThemeUtils(ctx, options);
const plugin = factory(utils, options);

const { commands, remarkPlugins, schema, inputRules, shortcuts, prosePlugins, view } = plugin;
const { commands, remarkPlugins, schema, inputRules, shortcuts, prosePlugins, view, injectSlices } =
plugin;

const injectOptions = injectSlices ?? [];

pipelineCtx.set(injectSlicesPipeCtx, injectOptions);
pipelineCtx.set(optionsPipeCtx, (options || {}) as Options);
pipelineCtx.set(getRemarkPluginsPipeCtx, remarkPlugins);
if (schema) {
Expand All @@ -102,9 +106,9 @@ export const createPlugin = <

const runner = run([
injectPipeEnv,
injectSlices(inject),
waitThemeReady,
setPipelineEnv,
injectSlices,
applyRemarkPlugins,
applySchema,
createCommands,
Expand Down
24 changes: 13 additions & 11 deletions packages/utils/src/factory/pieces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@ export type PluginView = Record<string, NodeViewConstructor | MarkViewConstructo

type Maybe<T> = T | undefined;

export const injectSlices =
(inject?: AnySlice[]): Pipeline =>
async (env, next) => {
if (inject) {
inject.forEach((slice) => env.pre.inject(slice));
env.onCleanup((post) => {
inject.forEach((slice) => post.remove(slice));
});
}
await next();
};
export const injectSlicesPipeCtx = createSlice<AnySlice[]>([], 'injectPipeCtx');
export const injectSlices: Pipeline = async (env, next) => {
const { pipelineCtx, onCleanup, pre } = env;
const inject = pipelineCtx.get(injectSlicesPipeCtx);
if (inject) {
inject.forEach((slice) => pre.inject(slice));
onCleanup((post) => {
inject.forEach((slice) => post.remove(slice));
});
}
await next();
};

export const waitThemeReady: Pipeline = async (env, next) => {
const { ctx } = env;
Expand Down Expand Up @@ -256,6 +257,7 @@ export const injectPipeEnv: Pipeline = async (env, next) => {
pipelineCtx
.inject(idPipeCtx)
.inject(optionsPipeCtx)
.inject(injectSlicesPipeCtx)
.inject(getRemarkPluginsPipeCtx)
.inject(getSchemaPipeCtx)
.inject(typePipeCtx)
Expand Down
3 changes: 1 addition & 2 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type CommonOptions<SupportedKeys extends string = string, Obj = UnknownRe
};

export type Methods<Keys extends string, Type> = {
injectSlices?: AnySlice[];
remarkPlugins?: (ctx: Ctx) => RemarkPlugin[];
inputRules?: (types: Type, ctx: Ctx) => InputRule[];
prosePlugins?: (types: Type, ctx: Ctx) => Plugin[];
Expand Down Expand Up @@ -73,8 +74,6 @@ export type Extendable<SupportedKeys extends string, Options extends UnknownReco
...rest: Parameters<Factory<ExtendedSupportedKeys, ExtendedOptions, ExtendedType, ExtendedRest>>,
]
) => Spec<ExtendedSupportedKeys, ExtendedType, ExtendedRest>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inject?: Slice<any>[],
) => WithExtend<ExtendedSupportedKeys, ExtendedOptions, Type, Rest>;
};
export type WithExtend<SupportedKeys extends string, Options extends UnknownRecord, Type, Rest> = AddMetadata<
Expand Down

0 comments on commit 59b94b8

Please sign in to comment.