@milkdown/utils
Advanced tools
Comparing version 5.0.1 to 5.1.0
@@ -5,20 +5,17 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
import { addMetadata, applyMethods, getUtils, withExtend } from './common'; | ||
export const createNode = (factory) => { | ||
const origin = addMetadata((options) => () => (ctx) => __awaiter(void 0, void 0, void 0, function* () { | ||
const utils = getUtils(ctx, options); | ||
const plugin = factory(utils, options); | ||
yield applyMethods(ctx, plugin, () => __awaiter(void 0, void 0, void 0, function* () { | ||
const node = plugin.schema(ctx); | ||
ctx.update(nodesCtx, (ns) => [...ns, [plugin.id, node]]); | ||
yield ctx.wait(SchemaReady); | ||
const schema = ctx.get(schemaCtx); | ||
return schema.nodes[plugin.id]; | ||
}), options); | ||
if (plugin.view) { | ||
const view = plugin.view(ctx); | ||
ctx.update(viewCtx, (v) => [...v, [plugin.id, view]]); | ||
} | ||
})); | ||
return withExtend(factory, origin, createNode); | ||
}; | ||
export const createNode = (factory) => withExtend(factory, addMetadata((options) => () => (ctx) => __awaiter(void 0, void 0, void 0, function* () { | ||
const utils = getUtils(ctx, options); | ||
const plugin = factory(utils, options); | ||
yield applyMethods(ctx, plugin, () => __awaiter(void 0, void 0, void 0, function* () { | ||
const node = plugin.schema(ctx); | ||
ctx.update(nodesCtx, (ns) => [...ns, [plugin.id, node]]); | ||
yield ctx.wait(SchemaReady); | ||
const schema = ctx.get(schemaCtx); | ||
return schema.nodes[plugin.id]; | ||
}), options); | ||
if (plugin.view) { | ||
const view = plugin.view(ctx); | ||
ctx.update(viewCtx, (v) => [...v, [plugin.id, view]]); | ||
} | ||
})), createNode); | ||
//# sourceMappingURL=create-node.js.map |
@@ -5,39 +5,39 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
import { addMetadata, applyMethods, getUtils, withExtend } from './common'; | ||
export const createPlugin = (factory) => { | ||
const origin = addMetadata((options) => () => (ctx) => __awaiter(void 0, void 0, void 0, function* () { | ||
const utils = getUtils(ctx, options); | ||
const plugin = factory(utils, options); | ||
yield applyMethods(ctx, plugin, () => __awaiter(void 0, void 0, void 0, function* () { | ||
let node = {}; | ||
let mark = {}; | ||
if (plugin.schema) { | ||
const schemas = plugin.schema(ctx); | ||
if (schemas.node) { | ||
node = schemas.node; | ||
const nodes = Object.entries(schemas.node); | ||
ctx.update(nodesCtx, (ns) => [...ns, ...nodes]); | ||
} | ||
if (schemas.mark) { | ||
mark = schemas.mark; | ||
const marks = Object.entries(schemas.mark); | ||
ctx.update(marksCtx, (ms) => [...ms, ...marks]); | ||
} | ||
export const createPlugin = (factory) => withExtend(factory, addMetadata((options) => () => (ctx) => __awaiter(void 0, void 0, void 0, function* () { | ||
const utils = getUtils(ctx, options); | ||
const plugin = factory(utils, options); | ||
yield applyMethods(ctx, plugin, () => __awaiter(void 0, void 0, void 0, function* () { | ||
let node = {}; | ||
let mark = {}; | ||
if (plugin.schema) { | ||
const schemas = plugin.schema(ctx); | ||
if (schemas.node) { | ||
node = schemas.node; | ||
const nodes = Object.entries(schemas.node); | ||
ctx.update(nodesCtx, (ns) => [...ns, ...nodes]); | ||
} | ||
yield ctx.wait(SchemaReady); | ||
const schema = ctx.get(schemaCtx); | ||
const nodeTypes = Object.keys(node).map((id) => [id, schema.nodes[id]]); | ||
const markTypes = Object.keys(mark).map((id) => [id, schema.marks[id]]); | ||
const type = Object.fromEntries([...nodeTypes, ...markTypes]); | ||
return type; | ||
}), options); | ||
if (plugin.view) { | ||
const view = plugin.view(ctx); | ||
ctx.update(viewCtx, (v) => [ | ||
...v, | ||
...Object.entries(view), | ||
]); | ||
if (schemas.mark) { | ||
mark = schemas.mark; | ||
const marks = Object.entries(schemas.mark); | ||
ctx.update(marksCtx, (ms) => [...ms, ...marks]); | ||
} | ||
} | ||
})); | ||
return withExtend(factory, origin, createPlugin); | ||
}; | ||
yield ctx.wait(SchemaReady); | ||
const schema = ctx.get(schemaCtx); | ||
const nodeTypes = Object.keys(node).map((id) => [id, schema.nodes[id]]); | ||
const markTypes = Object.keys(mark).map((id) => [id, schema.marks[id]]); | ||
const type = Object.fromEntries([ | ||
...nodeTypes, | ||
...markTypes, | ||
]); | ||
return type; | ||
}), options); | ||
if (plugin.view) { | ||
const view = plugin.view(ctx); | ||
ctx.update(viewCtx, (v) => [ | ||
...v, | ||
...Object.entries(view), | ||
]); | ||
} | ||
})), createPlugin); | ||
//# sourceMappingURL=create-plugin.js.map |
export * from './atom'; | ||
export * from './composable'; | ||
export * from './factory'; | ||
export { CommonOptions, Methods, Utils } from './types'; | ||
//# sourceMappingURL=index.d.ts.map |
/* Copyright 2021, Milkdown by Mirone. */ | ||
export * from './atom'; | ||
export * from './composable'; | ||
export * from './factory'; | ||
//# sourceMappingURL=index.js.map |
@@ -24,5 +24,4 @@ import type { Attrs, CmdKey, MilkdownPlugin, ThemeTool } from '@milkdown/core'; | ||
export declare type GetPlugin<SupportedKeys extends string = string, Options extends UnknownRecord = UnknownRecord> = (options?: Partial<CommonOptions<SupportedKeys, Options>>) => MilkdownPlugin; | ||
export declare type AnyFn = (...args: any[]) => any; | ||
export declare type Metadata<T = unknown> = { | ||
origin: T; | ||
export declare type Metadata<Origin = unknown> = { | ||
origin: Origin; | ||
}; | ||
@@ -29,0 +28,0 @@ export declare type AddMetadata<SupportedKeys extends string = string, Options extends UnknownRecord = UnknownRecord> = (options?: Partial<CommonOptions<SupportedKeys, Options>>) => Metadata<GetPlugin<SupportedKeys, Options>> & MilkdownPlugin; |
{ | ||
"name": "@milkdown/utils", | ||
"version": "5.0.1", | ||
"version": "5.1.0", | ||
"main": "lib/index.js", | ||
@@ -17,3 +17,3 @@ "module": "lib/index.js", | ||
"dependencies": { | ||
"@milkdown/exception": "5.0.1", | ||
"@milkdown/exception": "5.1.0", | ||
"tslib": "^2.3.1" | ||
@@ -20,0 +20,0 @@ }, |
@@ -24,33 +24,35 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
factory: NodeFactory<SupportedKeys, Options>, | ||
): WithExtend<SupportedKeys, Options, NodeType, NodeRest> => { | ||
const origin = addMetadata<SupportedKeys, Options>( | ||
(options): MilkdownPlugin => | ||
() => | ||
async (ctx) => { | ||
const utils = getUtils(ctx, options); | ||
): WithExtend<SupportedKeys, Options, NodeType, NodeRest> => | ||
withExtend( | ||
factory, | ||
addMetadata( | ||
(options): MilkdownPlugin => | ||
() => | ||
async (ctx) => { | ||
const utils = getUtils(ctx, options); | ||
const plugin = factory(utils, options); | ||
const plugin = factory(utils, options); | ||
await applyMethods( | ||
ctx, | ||
plugin, | ||
async () => { | ||
const node = plugin.schema(ctx); | ||
ctx.update(nodesCtx, (ns) => [...ns, [plugin.id, node] as [string, NodeSchema]]); | ||
await applyMethods( | ||
ctx, | ||
plugin, | ||
async () => { | ||
const node = plugin.schema(ctx); | ||
ctx.update(nodesCtx, (ns) => [...ns, [plugin.id, node] as [string, NodeSchema]]); | ||
await ctx.wait(SchemaReady); | ||
await ctx.wait(SchemaReady); | ||
const schema = ctx.get(schemaCtx); | ||
return schema.nodes[plugin.id]; | ||
}, | ||
options, | ||
); | ||
const schema = ctx.get(schemaCtx); | ||
return schema.nodes[plugin.id]; | ||
}, | ||
options, | ||
); | ||
if (plugin.view) { | ||
const view = plugin.view(ctx); | ||
ctx.update(viewCtx, (v) => [...v, [plugin.id, view] as [string, ViewFactory]]); | ||
} | ||
}, | ||
if (plugin.view) { | ||
const view = plugin.view(ctx); | ||
ctx.update(viewCtx, (v) => [...v, [plugin.id, view] as [string, ViewFactory]]); | ||
} | ||
}, | ||
), | ||
createNode, | ||
); | ||
return withExtend(factory, origin, createNode); | ||
}; |
@@ -52,54 +52,58 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
factory: PluginFactory<SupportedKeys, Options, NodeKeys, MarkKeys>, | ||
): WithExtend<SupportedKeys, Options, TypeMapping<NodeKeys, MarkKeys>, PluginRest<NodeKeys, MarkKeys>> => { | ||
const origin = addMetadata<SupportedKeys, Options>( | ||
(options): MilkdownPlugin => | ||
() => | ||
async (ctx) => { | ||
const utils = getUtils(ctx, options); | ||
): WithExtend<SupportedKeys, Options, TypeMapping<NodeKeys, MarkKeys>, PluginRest<NodeKeys, MarkKeys>> => | ||
withExtend( | ||
factory, | ||
addMetadata( | ||
(options): MilkdownPlugin => | ||
() => | ||
async (ctx) => { | ||
const utils = getUtils(ctx, options); | ||
const plugin = factory(utils, options); | ||
const plugin = factory(utils, options); | ||
await applyMethods( | ||
ctx, | ||
plugin, | ||
async () => { | ||
let node: Record<NodeKeys, NodeSchema> = {} as Record<NodeKeys, NodeSchema>; | ||
let mark: Record<MarkKeys, MarkSchema> = {} as Record<MarkKeys, MarkSchema>; | ||
if (plugin.schema) { | ||
const schemas = plugin.schema(ctx); | ||
if (schemas.node) { | ||
node = schemas.node; | ||
const nodes = Object.entries<NodeSchema>(schemas.node); | ||
ctx.update(nodesCtx, (ns) => [...ns, ...nodes]); | ||
} | ||
await applyMethods( | ||
ctx, | ||
plugin, | ||
async () => { | ||
let node: Record<NodeKeys, NodeSchema> = {} as Record<NodeKeys, NodeSchema>; | ||
let mark: Record<MarkKeys, MarkSchema> = {} as Record<MarkKeys, MarkSchema>; | ||
if (plugin.schema) { | ||
const schemas = plugin.schema(ctx); | ||
if (schemas.node) { | ||
node = schemas.node; | ||
const nodes = Object.entries<NodeSchema>(schemas.node); | ||
ctx.update(nodesCtx, (ns) => [...ns, ...nodes]); | ||
} | ||
if (schemas.mark) { | ||
mark = schemas.mark; | ||
const marks = Object.entries<MarkSchema>(schemas.mark); | ||
ctx.update(marksCtx, (ms) => [...ms, ...marks]); | ||
if (schemas.mark) { | ||
mark = schemas.mark; | ||
const marks = Object.entries<MarkSchema>(schemas.mark); | ||
ctx.update(marksCtx, (ms) => [...ms, ...marks]); | ||
} | ||
} | ||
} | ||
await ctx.wait(SchemaReady); | ||
await ctx.wait(SchemaReady); | ||
const schema = ctx.get(schemaCtx); | ||
const nodeTypes = Object.keys(node).map((id) => [id, schema.nodes[id]] as const); | ||
const markTypes = Object.keys(mark).map((id) => [id, schema.marks[id]] as const); | ||
const type: TypeMapping<NodeKeys, MarkKeys> = Object.fromEntries([...nodeTypes, ...markTypes]); | ||
return type; | ||
}, | ||
options, | ||
); | ||
const schema = ctx.get(schemaCtx); | ||
const nodeTypes = Object.keys(node).map((id) => [id, schema.nodes[id]] as const); | ||
const markTypes = Object.keys(mark).map((id) => [id, schema.marks[id]] as const); | ||
const type: TypeMapping<NodeKeys, MarkKeys> = Object.fromEntries([ | ||
...nodeTypes, | ||
...markTypes, | ||
]); | ||
return type; | ||
}, | ||
options, | ||
); | ||
if (plugin.view) { | ||
const view = plugin.view(ctx); | ||
ctx.update(viewCtx, (v) => [ | ||
...v, | ||
...Object.entries<ViewFactory>(view as Record<string, ViewFactory>), | ||
]); | ||
} | ||
}, | ||
if (plugin.view) { | ||
const view = plugin.view(ctx); | ||
ctx.update(viewCtx, (v) => [ | ||
...v, | ||
...Object.entries<ViewFactory>(view as Record<string, ViewFactory>), | ||
]); | ||
} | ||
}, | ||
), | ||
createPlugin, | ||
); | ||
return withExtend(factory, origin, createPlugin); | ||
}; |
/* Copyright 2021, Milkdown by Mirone. */ | ||
export * from './atom'; | ||
export * from './composable'; | ||
export * from './factory'; | ||
export { CommonOptions, Methods, Utils } from './types'; |
@@ -30,3 +30,2 @@ /* Copyright 2021, Milkdown by Mirone. */ | ||
// get plugin | ||
export type GetPlugin<SupportedKeys extends string = string, Options extends UnknownRecord = UnknownRecord> = ( | ||
@@ -36,8 +35,5 @@ options?: Partial<CommonOptions<SupportedKeys, Options>>, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type AnyFn = (...args: any[]) => any; | ||
export type Metadata<T = unknown> = { | ||
origin: T; | ||
export type Metadata<Origin = unknown> = { | ||
origin: Origin; | ||
}; | ||
// export type AddMetadata<T extends AnyFn = AnyFn> = (...args: Parameters<T>) => Metadata<T> & ReturnType<T>; | ||
export type AddMetadata<SupportedKeys extends string = string, Options extends UnknownRecord = UnknownRecord> = ( | ||
@@ -44,0 +40,0 @@ options?: Partial<CommonOptions<SupportedKeys, Options>>, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75777
93
1045
+ Added@milkdown/exception@5.1.0(transitive)
- Removed@milkdown/exception@5.0.1(transitive)
Updated@milkdown/exception@5.1.0