tiptap/packages/core/src/Node.ts

327 lines
6.2 KiB
TypeScript
Raw Normal View History

2021-01-20 16:18:49 +08:00
// @ts-nocheck
2020-10-23 05:21:52 +08:00
import {
DOMOutputSpec,
NodeSpec,
Node as ProseMirrorNode,
NodeType,
2020-10-23 05:21:52 +08:00
} from 'prosemirror-model'
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'
2020-11-16 18:21:54 +08:00
import { ExtensionConfig } from './Extension'
2021-01-20 16:18:49 +08:00
import mergeDeep from './utilities/mergeDeep'
2020-11-17 04:54:40 +08:00
import { Attributes, NodeViewRenderer, Overwrite } from './types'
2020-10-23 05:21:52 +08:00
import { Editor } from './Editor'
2020-10-21 21:17:05 +08:00
2020-11-16 18:21:54 +08:00
export interface NodeConfig<Options = any, Commands = {}> extends Overwrite<ExtensionConfig<Options, Commands>, {
2020-10-23 05:21:52 +08:00
/**
* TopNode
*/
2020-10-21 21:17:05 +08:00
topNode?: boolean,
2020-10-22 17:14:44 +08:00
/**
2020-10-23 05:21:52 +08:00
* Content
2020-10-22 17:14:44 +08:00
*/
content?: NodeSpec['content'] | ((this: { options: Options }) => NodeSpec['content']),
2020-10-23 05:21:52 +08:00
/**
* Marks
*/
marks?: NodeSpec['marks'] | ((this: { options: Options }) => NodeSpec['marks']),
2020-10-23 05:21:52 +08:00
/**
* Group
*/
group?: NodeSpec['group'] | ((this: { options: Options }) => NodeSpec['group']),
2020-10-23 05:21:52 +08:00
/**
* Inline
*/
inline?: NodeSpec['inline'] | ((this: { options: Options }) => NodeSpec['inline']),
2020-10-23 05:21:52 +08:00
/**
* Atom
*/
atom?: NodeSpec['atom'] | ((this: { options: Options }) => NodeSpec['atom']),
2020-10-23 05:21:52 +08:00
/**
* Selectable
*/
selectable?: NodeSpec['selectable'] | ((this: { options: Options }) => NodeSpec['selectable']),
2020-10-23 05:21:52 +08:00
/**
* Draggable
*/
draggable?: NodeSpec['draggable'] | ((this: { options: Options }) => NodeSpec['draggable']),
2020-10-23 05:21:52 +08:00
/**
* Code
*/
code?: NodeSpec['code'] | ((this: { options: Options }) => NodeSpec['code']),
2020-10-23 05:21:52 +08:00
/**
* Defining
*/
defining?: NodeSpec['defining'] | ((this: { options: Options }) => NodeSpec['defining']),
2020-10-23 05:21:52 +08:00
/**
* Isolating
*/
isolating?: NodeSpec['isolating'] | ((this: { options: Options }) => NodeSpec['isolating']),
2020-10-23 05:21:52 +08:00
/**
* Table Role
*/
tableRole?: NodeSpec['tableRole'] | ((this: { options: Options }) => NodeSpec['tableRole']),
2020-10-23 05:21:52 +08:00
/**
* Parse HTML
*/
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'],
2020-10-23 05:21:52 +08:00
/**
* Render HTML
*/
2020-10-23 20:24:19 +08:00
renderHTML?: ((
2020-10-22 03:13:38 +08:00
this: {
options: Options,
},
props: {
node: ProseMirrorNode,
2020-11-13 23:07:20 +08:00
HTMLAttributes: { [key: string]: any },
2020-10-22 03:13:38 +08:00
}
2020-10-23 20:24:19 +08:00
) => DOMOutputSpec) | null,
2020-10-23 05:21:52 +08:00
2021-01-20 03:27:51 +08:00
/**
* Render Text
*/
renderText?: ((
this: {
options: Options,
editor: Editor,
type: NodeType,
},
props: {
node: ProseMirrorNode,
}
) => string) | null,
2020-10-23 05:21:52 +08:00
/**
* Add Attributes
*/
2020-10-22 17:14:44 +08:00
addAttributes?: (
this: {
options: Options,
},
2020-12-04 06:32:11 +08:00
) => Attributes | {},
2020-10-23 05:21:52 +08:00
/**
* Commands
*/
addCommands?: (this: {
options: Options,
editor: Editor,
type: NodeType,
}) => Commands,
/**
* Keyboard shortcuts
*/
addKeyboardShortcuts?: (this: {
options: Options,
editor: Editor,
type: NodeType,
}) => {
[key: string]: any
},
/**
* Input rules
*/
addInputRules?: (this: {
options: Options,
editor: Editor,
type: NodeType,
2020-11-17 04:54:40 +08:00
}) => InputRule[],
2020-10-23 05:21:52 +08:00
/**
* Paste rules
*/
addPasteRules?: (this: {
options: Options,
editor: Editor,
type: NodeType,
2020-11-17 04:54:40 +08:00
}) => Plugin[],
2020-10-23 05:21:52 +08:00
/**
* ProseMirror plugins
*/
addProseMirrorPlugins?: (this: {
options: Options,
editor: Editor,
type: NodeType,
}) => Plugin[],
2020-10-29 16:26:24 +08:00
/**
* Node View
*/
2020-10-31 00:43:59 +08:00
addNodeView?: ((this: {
options: Options,
editor: Editor,
type: NodeType,
}) => NodeViewRenderer) | null,
2020-11-30 20:50:06 +08:00
2020-11-30 21:12:36 +08:00
/**
* The editor is ready.
*/
onCreate?: ((this: {
options: Options,
editor: Editor,
type: NodeType,
}) => void) | null,
/**
* The content has changed.
*/
onUpdate?: ((this: {
options: Options,
editor: Editor,
type: NodeType,
}) => void) | null,
/**
* The selection has changed.
*/
onSelection?: ((this: {
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 isnt focused anymore.
*/
onBlur?: ((
this: {
options: Options,
editor: Editor,
type: NodeType,
},
props: {
event: FocusEvent,
},
) => void) | null,
/**
* The editor is destroyed.
*/
2020-11-30 20:50:06 +08:00
onDestroy?: ((this: {
options: Options,
editor: Editor,
type: NodeType,
}) => void) | null,
2020-10-23 17:24:27 +08:00
}> {}
2020-10-21 21:17:05 +08:00
export class Node<Options = any, Commands = {}> {
2020-11-20 04:08:25 +08:00
type = 'node'
2020-11-16 18:21:54 +08:00
config: Required<NodeConfig> = {
2020-11-16 06:25:25 +08:00
name: 'node',
defaultOptions: {},
addGlobalAttributes: () => [],
addCommands: () => ({}),
addKeyboardShortcuts: () => ({}),
addInputRules: () => [],
addPasteRules: () => [],
addProseMirrorPlugins: () => [],
topNode: false,
content: null,
marks: null,
group: null,
inline: null,
atom: null,
selectable: null,
draggable: null,
code: null,
defining: null,
isolating: null,
parseHTML: () => null,
renderHTML: null,
2021-01-20 03:27:51 +08:00
renderText: null,
2020-11-16 06:25:25 +08:00
addAttributes: () => ({}),
addNodeView: null,
2020-11-30 21:12:36 +08:00
onCreate: null,
onUpdate: null,
onSelection: null,
onTransaction: null,
onFocus: null,
onBlur: null,
2020-11-30 20:50:06 +08:00
onDestroy: null,
2020-11-16 06:25:25 +08:00
}
2020-03-30 18:40:25 +08:00
2020-11-16 06:25:25 +08:00
options!: Options
2020-11-16 18:21:54 +08:00
constructor(config: NodeConfig<Options, Commands>) {
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
2020-11-16 18:21:54 +08:00
static create<O, C>(config: NodeConfig<O, C>) {
return new Node<O, C>(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> = {}) {
return Node
2020-11-16 18:21:54 +08:00
.create<Options, Commands>(this.config as NodeConfig<Options, Commands>)
2021-01-20 05:29:46 +08:00
.#configure(options)
}
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
}
2020-11-16 18:21:54 +08:00
extend<ExtendedOptions = Options, ExtendedCommands = Commands>(extendedConfig: Partial<NodeConfig<ExtendedOptions, ExtendedCommands>>) {
return new Node<ExtendedOptions, ExtendedCommands>({
2020-11-16 06:25:25 +08:00
...this.config,
...extendedConfig,
2020-11-16 18:21:54 +08:00
} as NodeConfig<ExtendedOptions, ExtendedCommands>)
2020-11-16 06:25:25 +08:00
}
2019-12-17 06:20:05 +08:00
}