2020-10-23 05:21:52 +08:00
|
|
|
|
import {
|
2020-11-16 18:19:43 +08:00
|
|
|
|
DOMOutputSpec,
|
|
|
|
|
NodeSpec,
|
|
|
|
|
Node as ProseMirrorNode,
|
|
|
|
|
NodeType,
|
2020-10-23 05:21:52 +08:00
|
|
|
|
} from 'prosemirror-model'
|
2021-02-17 05:59:45 +08:00
|
|
|
|
import { Command as ProseMirrorCommand } from 'prosemirror-commands'
|
2020-11-30 21:12:36 +08:00
|
|
|
|
import { Plugin, Transaction } from 'prosemirror-state'
|
2020-11-17 04:54:40 +08:00
|
|
|
|
import { InputRule } from 'prosemirror-inputrules'
|
2021-01-20 16:18:49 +08:00
|
|
|
|
import mergeDeep from './utilities/mergeDeep'
|
2021-02-10 16:59:35 +08:00
|
|
|
|
import {
|
2021-02-19 17:09:25 +08:00
|
|
|
|
Attributes,
|
|
|
|
|
NodeViewRenderer,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
GlobalAttributes,
|
2021-02-19 17:09:25 +08:00
|
|
|
|
RawCommands,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
NodeConfig,
|
2021-02-10 16:59:35 +08:00
|
|
|
|
} from './types'
|
2020-10-23 05:21:52 +08:00
|
|
|
|
import { Editor } from './Editor'
|
2020-10-21 21:17:05 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
declare module '@tiptap/core' {
|
|
|
|
|
interface NodeConfig<Options = any> {
|
2021-02-20 00:47:22 +08:00
|
|
|
|
[key: string]: any;
|
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Name
|
|
|
|
|
*/
|
|
|
|
|
name: string,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default options
|
|
|
|
|
*/
|
|
|
|
|
defaultOptions?: Options,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Global attributes
|
|
|
|
|
*/
|
|
|
|
|
addGlobalAttributes?: (this: {
|
2020-10-22 03:13:38 +08:00
|
|
|
|
options: Options,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => GlobalAttributes | {},
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Raw
|
|
|
|
|
*/
|
|
|
|
|
addCommands?: (this: {
|
2020-10-22 03:13:38 +08:00
|
|
|
|
options: Options,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
}) => Partial<RawCommands>,
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Keyboard shortcuts
|
|
|
|
|
*/
|
|
|
|
|
addKeyboardShortcuts?: (this: {
|
2021-01-20 03:27:51 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => {
|
|
|
|
|
[key: string]: ProseMirrorCommand,
|
2021-01-20 03:27:51 +08:00
|
|
|
|
},
|
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Input rules
|
|
|
|
|
*/
|
|
|
|
|
addInputRules?: (this: {
|
2020-10-22 05:32:28 +08:00
|
|
|
|
options: Options,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
}) => InputRule[],
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Paste rules
|
|
|
|
|
*/
|
|
|
|
|
addPasteRules?: (this: {
|
2020-11-30 21:12:36 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => Plugin[],
|
2020-11-30 21:12:36 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* ProseMirror plugins
|
|
|
|
|
*/
|
|
|
|
|
addProseMirrorPlugins?: (this: {
|
2020-11-30 21:12:36 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => Plugin[],
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extend Node Schema
|
|
|
|
|
*/
|
|
|
|
|
extendNodeSchema?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
},
|
|
|
|
|
extension: Node,
|
|
|
|
|
) => {
|
|
|
|
|
[key: string]: any,
|
|
|
|
|
}) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extend Mark Schema
|
|
|
|
|
*/
|
|
|
|
|
extendMarkSchema?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
},
|
|
|
|
|
extension: Node,
|
|
|
|
|
) => {
|
|
|
|
|
[key: string]: any,
|
|
|
|
|
}) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is ready.
|
|
|
|
|
*/
|
|
|
|
|
onCreate?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
}) => void) | null,
|
2020-11-30 21:12:36 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* The content has changed.
|
|
|
|
|
*/
|
|
|
|
|
onUpdate?: ((this: {
|
2020-11-30 21:12:36 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The selection has changed.
|
|
|
|
|
*/
|
2021-03-09 16:50:03 +08:00
|
|
|
|
onSelectionUpdate?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The view has changed.
|
|
|
|
|
*/
|
|
|
|
|
onViewUpdate?: ((this: {
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor state has changed.
|
|
|
|
|
*/
|
|
|
|
|
onTransaction?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
transaction: Transaction,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is focused.
|
|
|
|
|
*/
|
|
|
|
|
onFocus?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor isn’t focused anymore.
|
|
|
|
|
*/
|
|
|
|
|
onBlur?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is destroyed.
|
|
|
|
|
*/
|
|
|
|
|
onDestroy?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Node View
|
|
|
|
|
*/
|
|
|
|
|
addNodeView?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
}) => NodeViewRenderer) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TopNode
|
|
|
|
|
*/
|
|
|
|
|
topNode?: boolean,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Content
|
|
|
|
|
*/
|
|
|
|
|
content?: NodeSpec['content'] | ((this: { options: Options }) => NodeSpec['content']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Marks
|
|
|
|
|
*/
|
|
|
|
|
marks?: NodeSpec['marks'] | ((this: { options: Options }) => NodeSpec['marks']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Group
|
|
|
|
|
*/
|
|
|
|
|
group?: NodeSpec['group'] | ((this: { options: Options }) => NodeSpec['group']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Inline
|
|
|
|
|
*/
|
|
|
|
|
inline?: NodeSpec['inline'] | ((this: { options: Options }) => NodeSpec['inline']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Atom
|
|
|
|
|
*/
|
|
|
|
|
atom?: NodeSpec['atom'] | ((this: { options: Options }) => NodeSpec['atom']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Selectable
|
|
|
|
|
*/
|
|
|
|
|
selectable?: NodeSpec['selectable'] | ((this: { options: Options }) => NodeSpec['selectable']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Draggable
|
|
|
|
|
*/
|
|
|
|
|
draggable?: NodeSpec['draggable'] | ((this: { options: Options }) => NodeSpec['draggable']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Code
|
|
|
|
|
*/
|
|
|
|
|
code?: NodeSpec['code'] | ((this: { options: Options }) => NodeSpec['code']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defining
|
|
|
|
|
*/
|
|
|
|
|
defining?: NodeSpec['defining'] | ((this: { options: Options }) => NodeSpec['defining']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Isolating
|
|
|
|
|
*/
|
|
|
|
|
isolating?: NodeSpec['isolating'] | ((this: { options: Options }) => NodeSpec['isolating']),
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse HTML
|
|
|
|
|
*/
|
|
|
|
|
parseHTML?: (
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
},
|
|
|
|
|
) => NodeSpec['parseDOM'],
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render HTML
|
|
|
|
|
*/
|
|
|
|
|
renderHTML?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
node: ProseMirrorNode,
|
|
|
|
|
HTMLAttributes: { [key: string]: any },
|
|
|
|
|
}
|
|
|
|
|
) => DOMOutputSpec) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render Text
|
|
|
|
|
*/
|
|
|
|
|
renderText?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
node: ProseMirrorNode,
|
|
|
|
|
}
|
|
|
|
|
) => string) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add Attributes
|
|
|
|
|
*/
|
|
|
|
|
addAttributes?: (
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
},
|
|
|
|
|
) => Attributes | {},
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-21 21:17:05 +08:00
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
|
export class Node<Options = any> {
|
2020-11-20 04:08:25 +08:00
|
|
|
|
type = 'node'
|
|
|
|
|
|
2021-02-19 17:54:47 +08:00
|
|
|
|
config: NodeConfig = {
|
2020-11-16 06:25:25 +08:00
|
|
|
|
name: 'node',
|
|
|
|
|
defaultOptions: {},
|
|
|
|
|
}
|
2020-03-30 18:40:25 +08:00
|
|
|
|
|
2020-11-16 06:25:25 +08:00
|
|
|
|
options!: Options
|
|
|
|
|
|
2021-02-19 17:54:47 +08:00
|
|
|
|
constructor(config: NodeConfig<Options>) {
|
2020-11-16 06:25:25 +08:00
|
|
|
|
this.config = {
|
|
|
|
|
...this.config,
|
2020-10-21 21:17:05 +08:00
|
|
|
|
...config,
|
2020-11-16 06:25:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.options = this.config.defaultOptions
|
2020-09-09 05:44:45 +08:00
|
|
|
|
}
|
2020-10-10 04:59:25 +08:00
|
|
|
|
|
2021-02-19 17:54:47 +08:00
|
|
|
|
static create<O>(config: NodeConfig<O>) {
|
2021-02-10 16:59:35 +08:00
|
|
|
|
return new Node<O>(config)
|
2020-11-16 06:25:25 +08:00
|
|
|
|
}
|
2020-10-21 21:17:05 +08:00
|
|
|
|
|
2021-01-20 05:29:46 +08:00
|
|
|
|
configure(options: Partial<Options> = {}) {
|
2020-11-16 18:19:43 +08:00
|
|
|
|
return Node
|
2021-02-19 17:54:47 +08:00
|
|
|
|
.create<Options>(this.config as NodeConfig<Options>)
|
2021-01-20 05:29:46 +08:00
|
|
|
|
.#configure(options)
|
2020-11-16 17:22:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 18:07:06 +08:00
|
|
|
|
#configure = (options: Partial<Options>) => {
|
2021-01-20 16:18:49 +08:00
|
|
|
|
this.options = mergeDeep(this.config.defaultOptions, options) as Options
|
2020-11-16 16:43:17 +08:00
|
|
|
|
|
|
|
|
|
return this
|
2020-10-10 04:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 17:54:47 +08:00
|
|
|
|
extend<ExtendedOptions = Options>(extendedConfig: Partial<NodeConfig<ExtendedOptions>>) {
|
2021-02-10 16:59:35 +08:00
|
|
|
|
return new Node<ExtendedOptions>({
|
2020-11-16 06:25:25 +08:00
|
|
|
|
...this.config,
|
|
|
|
|
...extendedConfig,
|
2021-02-19 17:54:47 +08:00
|
|
|
|
} as NodeConfig<ExtendedOptions>)
|
2020-11-16 06:25:25 +08:00
|
|
|
|
}
|
2019-12-17 06:20:05 +08:00
|
|
|
|
}
|