tiptap/packages/core/src/Node.ts

146 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-10-10 04:59:25 +08:00
// import { NodeSpec, NodeType } from 'prosemirror-model'
// import Extension, { ExtensionMethods } from './Extension'
// import { Editor } from './Editor'
2019-12-17 06:20:05 +08:00
2020-10-10 04:59:25 +08:00
// export interface NodeProps<Options> {
// name: string
// editor: Editor
// options: Options
// type: NodeType
// }
// export interface NodeMethods<Props, Options> extends ExtensionMethods<Props, Options> {
// topNode: boolean
// schema: (params: Omit<Props, 'type' | 'editor'>) => NodeSpec
// }
// export default class Node<
// Options = {},
// Props = NodeProps<Options>,
// Methods extends NodeMethods<Props, Options> = NodeMethods<Props, Options>,
// > extends Extension<Options, Props, Methods> {
// type = 'node'
// public topNode(value: Methods['topNode'] = true) {
// this.storeConfig('topNode', value, 'overwrite')
// return this
// }
// public schema(value: Methods['schema']) {
// this.storeConfig('schema', value, 'overwrite')
// return this
// }
// }
2020-10-21 21:17:05 +08:00
// import { DOMOutputSpec, DOMOutputSpecArray } from 'prosemirror-model'
// import Extension from './Extension'
2020-10-12 16:32:54 +08:00
2020-10-21 21:17:05 +08:00
// export interface INode {
// type: string
// topNode: boolean
// group: string
// content: string
// createAttributes(): any
// parseHTML(): any
// renderHTML(props: number): DOMOutputSpec
// }
2020-09-09 05:44:45 +08:00
2020-10-21 21:17:05 +08:00
// export default class Node<Options = {}> extends Extension<Options> implements INode {
2020-09-09 05:44:45 +08:00
2020-10-21 21:17:05 +08:00
// type = 'node'
2020-10-10 04:59:25 +08:00
2020-10-21 21:17:05 +08:00
// topNode = false
2020-10-10 04:59:25 +08:00
2020-10-21 21:17:05 +08:00
// group = ''
2020-10-10 04:59:25 +08:00
2020-10-21 21:17:05 +08:00
// content = ''
// createAttributes() {
// return {}
// }
// parseHTML() {
// return []
// }
// renderHTML() {
// return null
// }
// }
import { DOMOutputSpec, NodeSpec, Node } from 'prosemirror-model'
2020-10-21 21:30:34 +08:00
import { ExtensionSpec } from './Extension'
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,
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-21 21:17:05 +08:00
parseHTML?: () => NodeSpec['parseDOM'],
renderHTML?: (props: {
node: Node,
attributes: {
[key: string]: any,
},
}) => DOMOutputSpec,
}
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 = {
type: 'node',
name: 'node',
options: {},
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
createCommands: () => ({}),
parseHTML: () => null,
renderHTML: () => null,
}
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
}