2020-10-21 21:17:05 +08:00
|
|
|
import { DOMOutputSpec, NodeSpec, Node } from 'prosemirror-model'
|
2020-10-22 15:14:24 +08:00
|
|
|
import { ExtensionSpec, defaultExtension } from './Extension'
|
2020-10-22 05:32:28 +08:00
|
|
|
import { Attributes } from './types'
|
2020-10-21 21:17:05 +08:00
|
|
|
|
2020-10-21 21:30:34 +08:00
|
|
|
export interface NodeExtensionSpec<Options = {}, Commands = {}> extends ExtensionSpec<Options, Commands> {
|
2020-10-21 21:17:05 +08:00
|
|
|
topNode?: boolean,
|
2020-10-22 17:14:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* content
|
|
|
|
*/
|
2020-10-21 21:17:05 +08:00
|
|
|
content?: NodeSpec['content'],
|
|
|
|
marks?: NodeSpec['marks'],
|
|
|
|
group?: NodeSpec['group'],
|
|
|
|
inline?: NodeSpec['inline'],
|
|
|
|
atom?: NodeSpec['atom'],
|
2020-10-22 03:01:39 +08:00
|
|
|
selectable?: NodeSpec['selectable'],
|
|
|
|
draggable?: NodeSpec['draggable'],
|
|
|
|
code?: NodeSpec['code'],
|
|
|
|
defining?: NodeSpec['defining'],
|
|
|
|
isolating?: NodeSpec['isolating'],
|
2020-10-22 03:13:38 +08:00
|
|
|
parseHTML?: (
|
|
|
|
this: {
|
|
|
|
options: Options,
|
2020-10-21 21:17:05 +08:00
|
|
|
},
|
2020-10-22 03:13:38 +08:00
|
|
|
) => NodeSpec['parseDOM'],
|
|
|
|
renderHTML?: (
|
|
|
|
this: {
|
|
|
|
options: Options,
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
node: Node,
|
2020-10-22 15:14:24 +08:00
|
|
|
attributes: { [key: string]: any },
|
2020-10-22 03:13:38 +08:00
|
|
|
}
|
|
|
|
) => DOMOutputSpec,
|
2020-10-22 17:14:44 +08:00
|
|
|
addAttributes?: (
|
2020-10-22 05:32:28 +08:00
|
|
|
this: {
|
|
|
|
options: Options,
|
|
|
|
},
|
|
|
|
) => Attributes,
|
2020-10-21 21:17:05 +08:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:30:34 +08:00
|
|
|
export type NodeExtension = Required<Omit<NodeExtensionSpec, 'defaultOptions'> & {
|
|
|
|
type: string,
|
|
|
|
options: {
|
|
|
|
[key: string]: any
|
|
|
|
},
|
|
|
|
}>
|
|
|
|
|
2020-10-21 21:17:05 +08:00
|
|
|
const defaultNode: NodeExtension = {
|
2020-10-22 15:14:24 +08:00
|
|
|
...defaultExtension,
|
2020-10-21 21:17:05 +08:00
|
|
|
type: 'node',
|
|
|
|
name: 'node',
|
|
|
|
topNode: false,
|
|
|
|
content: null,
|
|
|
|
marks: null,
|
|
|
|
group: null,
|
|
|
|
inline: null,
|
|
|
|
atom: null,
|
2020-10-22 03:01:39 +08:00
|
|
|
selectable: null,
|
|
|
|
draggable: null,
|
|
|
|
code: null,
|
|
|
|
defining: null,
|
|
|
|
isolating: null,
|
2020-10-21 21:17:05 +08:00
|
|
|
parseHTML: () => null,
|
|
|
|
renderHTML: () => null,
|
2020-10-22 17:14:44 +08:00
|
|
|
addAttributes: () => ({}),
|
2020-10-21 21:17:05 +08:00
|
|
|
}
|
2020-03-30 18:40:25 +08:00
|
|
|
|
2020-10-21 21:17:05 +08:00
|
|
|
export function createNode<Options extends {}, Commands extends {}>(config: NodeExtensionSpec<Options, Commands>) {
|
|
|
|
const extend = <ExtendedOptions = Options, ExtendedCommands = Commands>(extendedConfig: Partial<NodeExtensionSpec<ExtendedOptions, ExtendedCommands>>) => {
|
|
|
|
return createNode({
|
|
|
|
...config,
|
|
|
|
...extendedConfig,
|
|
|
|
} as NodeExtensionSpec<ExtendedOptions, ExtendedCommands>)
|
2020-09-09 05:44:45 +08:00
|
|
|
}
|
2020-10-10 04:59:25 +08:00
|
|
|
|
2020-10-21 21:17:05 +08:00
|
|
|
const setOptions = (options?: Partial<Options>) => {
|
|
|
|
const { defaultOptions, ...rest } = config
|
|
|
|
|
|
|
|
return {
|
|
|
|
...defaultNode,
|
|
|
|
...rest,
|
|
|
|
options: {
|
|
|
|
...defaultOptions,
|
|
|
|
...options,
|
|
|
|
} as Options,
|
|
|
|
}
|
2020-10-10 04:59:25 +08:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:17:05 +08:00
|
|
|
return Object.assign(setOptions, { config, extend })
|
2019-12-17 06:20:05 +08:00
|
|
|
}
|