2020-10-23 05:21:52 +08:00
|
|
|
|
import {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
DOMOutputSpec, Node as ProseMirrorNode, NodeSpec, NodeType,
|
|
|
|
|
} from '@tiptap/pm/model'
|
|
|
|
|
import { Plugin, Transaction } from '@tiptap/pm/state'
|
2022-06-08 20:10:25 +08:00
|
|
|
|
|
2023-07-01 03:03:49 +08:00
|
|
|
|
import { Editor } from './Editor.js'
|
|
|
|
|
import { getExtensionField } from './helpers/getExtensionField.js'
|
|
|
|
|
import { NodeConfig } from './index.js'
|
|
|
|
|
import { InputRule } from './InputRule.js'
|
|
|
|
|
import { PasteRule } from './PasteRule.js'
|
2021-02-10 16:59:35 +08:00
|
|
|
|
import {
|
2021-10-22 14:52:54 +08:00
|
|
|
|
AnyConfig,
|
2021-02-19 17:09:25 +08:00
|
|
|
|
Attributes,
|
2022-06-08 20:10:25 +08:00
|
|
|
|
Extensions,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
GlobalAttributes,
|
2021-04-21 04:58:09 +08:00
|
|
|
|
KeyboardShortcutCommand,
|
2022-06-08 20:10:25 +08:00
|
|
|
|
NodeViewRenderer,
|
|
|
|
|
ParentConfig,
|
|
|
|
|
RawCommands,
|
2023-07-01 03:03:49 +08:00
|
|
|
|
} from './types.js'
|
|
|
|
|
import { callOrReturn } from './utilities/callOrReturn.js'
|
|
|
|
|
import { mergeDeep } from './utilities/mergeDeep.js'
|
2020-10-21 21:17:05 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
declare module '@tiptap/core' {
|
2021-10-22 14:52:54 +08:00
|
|
|
|
interface NodeConfig<Options = any, Storage = any> {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
[key: string]: any
|
2021-02-20 00:47:22 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Name
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
2021-04-08 00:29:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* Priority
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
priority?: number
|
2021-04-08 00:29:16 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Default options
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
defaultOptions?: Options
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
2021-10-27 00:31:13 +08:00
|
|
|
|
/**
|
|
|
|
|
* Default Options
|
|
|
|
|
*/
|
|
|
|
|
addOptions?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
parent: Exclude<ParentConfig<NodeConfig<Options, Storage>>['addOptions'], undefined>
|
|
|
|
|
}) => Options
|
2021-10-27 00:31:13 +08:00
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* Default Storage
|
|
|
|
|
*/
|
|
|
|
|
addStorage?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
parent: Exclude<ParentConfig<NodeConfig<Options, Storage>>['addStorage'], undefined>
|
|
|
|
|
}) => Storage
|
2021-10-22 14:52:54 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Global attributes
|
|
|
|
|
*/
|
|
|
|
|
addGlobalAttributes?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addGlobalAttributes']
|
|
|
|
|
}) => GlobalAttributes | {}
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Raw
|
|
|
|
|
*/
|
|
|
|
|
addCommands?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addCommands']
|
|
|
|
|
}) => Partial<RawCommands>
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Keyboard shortcuts
|
|
|
|
|
*/
|
|
|
|
|
addKeyboardShortcuts?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addKeyboardShortcuts']
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
[key: string]: KeyboardShortcutCommand
|
|
|
|
|
}
|
2021-01-20 03:27:51 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Input rules
|
|
|
|
|
*/
|
|
|
|
|
addInputRules?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addInputRules']
|
|
|
|
|
}) => InputRule[]
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Paste rules
|
|
|
|
|
*/
|
|
|
|
|
addPasteRules?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addPasteRules']
|
|
|
|
|
}) => PasteRule[]
|
2020-11-30 21:12:36 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* ProseMirror plugins
|
|
|
|
|
*/
|
|
|
|
|
addProseMirrorPlugins?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addProseMirrorPlugins']
|
|
|
|
|
}) => Plugin[]
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
2021-05-07 00:39:47 +08:00
|
|
|
|
/**
|
|
|
|
|
* Extensions
|
|
|
|
|
*/
|
|
|
|
|
addExtensions?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addExtensions']
|
|
|
|
|
}) => Extensions
|
2021-05-07 00:39:47 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Extend Node Schema
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
extendNodeSchema?:
|
|
|
|
|
| ((
|
|
|
|
|
this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['extendNodeSchema']
|
|
|
|
|
},
|
|
|
|
|
extension: Node,
|
|
|
|
|
) => Record<string, any>)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extend Mark Schema
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
extendMarkSchema?:
|
|
|
|
|
| ((
|
|
|
|
|
this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['extendMarkSchema']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
},
|
|
|
|
|
extension: Node,
|
|
|
|
|
) => Record<string, any>)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
2021-04-02 06:07:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* The editor is not ready yet.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
onBeforeCreate?:
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onBeforeCreate']
|
|
|
|
|
}) => void)
|
|
|
|
|
| null
|
2021-04-02 06:07:40 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* The editor is ready.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
onCreate?:
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onCreate']
|
|
|
|
|
}) => void)
|
|
|
|
|
| null
|
2020-11-30 21:12:36 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* The content has changed.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
onUpdate?:
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onUpdate']
|
|
|
|
|
}) => void)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The selection has changed.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
onSelectionUpdate?:
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onSelectionUpdate']
|
|
|
|
|
}) => void)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor state has changed.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
onTransaction?:
|
|
|
|
|
| ((
|
|
|
|
|
this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onTransaction']
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
transaction: Transaction
|
|
|
|
|
},
|
|
|
|
|
) => void)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is focused.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
onFocus?:
|
|
|
|
|
| ((
|
|
|
|
|
this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onFocus']
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent
|
|
|
|
|
},
|
|
|
|
|
) => void)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor isn’t focused anymore.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
onBlur?:
|
|
|
|
|
| ((
|
|
|
|
|
this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onBlur']
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent
|
|
|
|
|
},
|
|
|
|
|
) => void)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is destroyed.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
onDestroy?:
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onDestroy']
|
|
|
|
|
}) => void)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Node View
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
addNodeView?:
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
editor: Editor
|
|
|
|
|
type: NodeType
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addNodeView']
|
|
|
|
|
}) => NodeViewRenderer)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TopNode
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
topNode?: boolean
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Content
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
content?:
|
|
|
|
|
| NodeSpec['content']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['content']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['content'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Marks
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
marks?:
|
|
|
|
|
| NodeSpec['marks']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['marks']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['marks'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Group
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
group?:
|
|
|
|
|
| NodeSpec['group']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['group']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['group'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Inline
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
inline?:
|
|
|
|
|
| NodeSpec['inline']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['inline']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['inline'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Atom
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
atom?:
|
|
|
|
|
| NodeSpec['atom']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['atom']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['atom'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Selectable
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
selectable?:
|
|
|
|
|
| NodeSpec['selectable']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['selectable']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['selectable'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Draggable
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
draggable?:
|
|
|
|
|
| NodeSpec['draggable']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['draggable']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['draggable'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Code
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
code?:
|
|
|
|
|
| NodeSpec['code']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['code']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['code'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
2022-01-04 17:02:24 +08:00
|
|
|
|
/**
|
|
|
|
|
* Whitespace
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
whitespace?:
|
|
|
|
|
| NodeSpec['whitespace']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['whitespace']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['whitespace'])
|
2022-01-04 17:02:24 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Defining
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
defining?:
|
|
|
|
|
| NodeSpec['defining']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['defining']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['defining'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Isolating
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
isolating?:
|
|
|
|
|
| NodeSpec['isolating']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['isolating']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['isolating'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse HTML
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
parseHTML?: (this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['parseHTML']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => NodeSpec['parseDOM']
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render HTML
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
renderHTML?:
|
|
|
|
|
| ((
|
|
|
|
|
this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['renderHTML']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
node: ProseMirrorNode
|
|
|
|
|
HTMLAttributes: Record<string, any>
|
|
|
|
|
},
|
|
|
|
|
) => DOMOutputSpec)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render Text
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
renderText?:
|
|
|
|
|
| ((
|
|
|
|
|
this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['renderText']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
node: ProseMirrorNode
|
|
|
|
|
pos: number
|
|
|
|
|
parent: ProseMirrorNode
|
|
|
|
|
index: number
|
|
|
|
|
},
|
|
|
|
|
) => string)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add Attributes
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
addAttributes?: (this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addAttributes']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => Attributes | {}
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-21 21:17:05 +08:00
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
export class Node<Options = any, Storage = any> {
|
2020-11-20 04:08:25 +08:00
|
|
|
|
type = 'node'
|
|
|
|
|
|
2021-04-16 04:03:45 +08:00
|
|
|
|
name = 'node'
|
2021-04-12 17:11:02 +08:00
|
|
|
|
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: Node | null = null
|
2020-11-16 06:25:25 +08:00
|
|
|
|
|
2021-04-15 20:40:28 +08:00
|
|
|
|
child: Node | null = null
|
|
|
|
|
|
2021-04-16 04:03:45 +08:00
|
|
|
|
options: Options
|
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage
|
|
|
|
|
|
2021-04-16 04:03:45 +08:00
|
|
|
|
config: NodeConfig = {
|
|
|
|
|
name: this.name,
|
2023-12-01 00:54:06 +08:00
|
|
|
|
defaultOptions: {},
|
2021-04-16 04:03:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
constructor(config: Partial<NodeConfig<Options, Storage>> = {}) {
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 04:03:45 +08:00
|
|
|
|
this.name = this.config.name
|
2021-10-27 00:31:13 +08:00
|
|
|
|
|
2023-12-01 00:54:06 +08:00
|
|
|
|
if (config.defaultOptions && Object.keys(config.defaultOptions).length > 0) {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
console.warn(
|
|
|
|
|
`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${this.name}".`,
|
|
|
|
|
)
|
2021-10-27 00:31:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: remove `addOptions` fallback
|
2020-11-16 06:25:25 +08:00
|
|
|
|
this.options = this.config.defaultOptions
|
2021-10-27 00:31:13 +08:00
|
|
|
|
|
|
|
|
|
if (this.config.addOptions) {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
this.options = callOrReturn(
|
|
|
|
|
getExtensionField<AnyConfig['addOptions']>(this, 'addOptions', {
|
2021-10-27 00:31:13 +08:00
|
|
|
|
name: this.name,
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}),
|
|
|
|
|
)
|
2021-10-27 00:31:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
|
this.storage = callOrReturn(
|
|
|
|
|
getExtensionField<AnyConfig['addStorage']>(this, 'addStorage', {
|
2021-10-22 14:52:54 +08:00
|
|
|
|
name: this.name,
|
|
|
|
|
options: this.options,
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}),
|
|
|
|
|
) || {}
|
2020-09-09 05:44:45 +08:00
|
|
|
|
}
|
2020-10-10 04:59:25 +08:00
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
static create<O = any, S = any>(config: Partial<NodeConfig<O, S>> = {}) {
|
|
|
|
|
return new Node<O, S>(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> = {}) {
|
2021-05-11 23:03:34 +08:00
|
|
|
|
// return a new instance so we can use the same extension
|
|
|
|
|
// with different calls of `configure`
|
2021-06-03 19:13:43 +08:00
|
|
|
|
const extension = this.extend()
|
|
|
|
|
|
2023-01-25 17:19:51 +08:00
|
|
|
|
extension.options = mergeDeep(this.options as Record<string, any>, options) as Options
|
2021-06-03 19:13:43 +08:00
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
|
extension.storage = callOrReturn(
|
|
|
|
|
getExtensionField<AnyConfig['addStorage']>(extension, 'addStorage', {
|
2021-10-25 06:27:24 +08:00
|
|
|
|
name: extension.name,
|
|
|
|
|
options: extension.options,
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}),
|
|
|
|
|
)
|
2021-10-25 06:27:24 +08:00
|
|
|
|
|
2021-06-03 19:13:43 +08:00
|
|
|
|
return extension
|
2020-10-10 04:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
|
extend<ExtendedOptions = Options, ExtendedStorage = Storage>(
|
|
|
|
|
extendedConfig: Partial<NodeConfig<ExtendedOptions, ExtendedStorage>> = {},
|
|
|
|
|
) {
|
2023-10-10 10:24:27 +08:00
|
|
|
|
const extension = new Node<ExtendedOptions, ExtendedStorage>({ ...this.config, ...extendedConfig })
|
2021-04-12 17:11:02 +08:00
|
|
|
|
|
2021-04-14 15:48:38 +08:00
|
|
|
|
extension.parent = this
|
2021-04-15 20:40:28 +08:00
|
|
|
|
|
|
|
|
|
this.child = extension
|
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
|
extension.name = extendedConfig.name ? extendedConfig.name : extension.parent.name
|
2021-04-16 04:03:45 +08:00
|
|
|
|
|
2021-10-27 00:31:13 +08:00
|
|
|
|
if (extendedConfig.defaultOptions) {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
console.warn(
|
|
|
|
|
`[tiptap warn]: BREAKING CHANGE: "defaultOptions" is deprecated. Please use "addOptions" instead. Found in extension: "${extension.name}".`,
|
|
|
|
|
)
|
2021-10-27 00:31:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
|
extension.options = callOrReturn(
|
|
|
|
|
getExtensionField<AnyConfig['addOptions']>(extension, 'addOptions', {
|
2021-12-03 03:04:45 +08:00
|
|
|
|
name: extension.name,
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}),
|
|
|
|
|
)
|
2021-10-27 00:31:13 +08:00
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
|
extension.storage = callOrReturn(
|
|
|
|
|
getExtensionField<AnyConfig['addStorage']>(extension, 'addStorage', {
|
2021-10-22 14:52:54 +08:00
|
|
|
|
name: extension.name,
|
|
|
|
|
options: extension.options,
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}),
|
|
|
|
|
)
|
2021-10-22 14:52:54 +08:00
|
|
|
|
|
2021-04-12 17:11:02 +08:00
|
|
|
|
return extension
|
2020-11-16 06:25:25 +08:00
|
|
|
|
}
|
2019-12-17 06:20:05 +08:00
|
|
|
|
}
|