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'
|
2020-11-30 21:12:36 +08:00
|
|
|
|
import { Plugin, Transaction } from 'prosemirror-state'
|
2021-10-08 21:02:09 +08:00
|
|
|
|
import { InputRule } from './InputRule'
|
|
|
|
|
import { PasteRule } from './PasteRule'
|
2021-01-20 16:18:49 +08:00
|
|
|
|
import mergeDeep from './utilities/mergeDeep'
|
2021-10-22 14:52:54 +08:00
|
|
|
|
import callOrReturn from './utilities/callOrReturn'
|
|
|
|
|
import getExtensionField from './helpers/getExtensionField'
|
2021-02-10 16:59:35 +08:00
|
|
|
|
import {
|
2021-10-22 14:52:54 +08:00
|
|
|
|
AnyConfig,
|
2021-05-07 16:08:30 +08:00
|
|
|
|
Extensions,
|
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-04-12 17:11:02 +08:00
|
|
|
|
ParentConfig,
|
2021-04-21 04:58:09 +08:00
|
|
|
|
KeyboardShortcutCommand,
|
2021-02-10 16:59:35 +08:00
|
|
|
|
} from './types'
|
2021-03-31 19:44:56 +08:00
|
|
|
|
import { NodeConfig } from '.'
|
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' {
|
2021-10-22 14:52:54 +08:00
|
|
|
|
interface NodeConfig<Options = any, Storage = any> {
|
2021-02-20 00:47:22 +08:00
|
|
|
|
[key: string]: any;
|
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Name
|
|
|
|
|
*/
|
|
|
|
|
name: string,
|
|
|
|
|
|
2021-04-08 00:29:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* Priority
|
|
|
|
|
*/
|
|
|
|
|
priority?: number,
|
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Default options
|
|
|
|
|
*/
|
|
|
|
|
defaultOptions?: Options,
|
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* Default Storage
|
|
|
|
|
*/
|
|
|
|
|
addStorage?: (this: {
|
|
|
|
|
name: string,
|
|
|
|
|
options: Options,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addGlobalAttributes'],
|
|
|
|
|
}) => Storage,
|
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Global attributes
|
|
|
|
|
*/
|
|
|
|
|
addGlobalAttributes?: (this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2020-10-22 03:13:38 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addGlobalAttributes'],
|
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: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2020-10-22 03:13:38 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addCommands'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => Partial<RawCommands>,
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Keyboard shortcuts
|
|
|
|
|
*/
|
|
|
|
|
addKeyboardShortcuts?: (this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-01-20 03:27:51 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-01-20 03:27:51 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addKeyboardShortcuts'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => {
|
2021-04-21 04:58:09 +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: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2020-10-22 05:32:28 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addInputRules'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => InputRule[],
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Paste rules
|
|
|
|
|
*/
|
|
|
|
|
addPasteRules?: (this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2020-11-30 21:12:36 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2020-11-30 21:12:36 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addPasteRules'],
|
2021-10-08 21:02:09 +08:00
|
|
|
|
}) => PasteRule[],
|
2020-11-30 21:12:36 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* ProseMirror plugins
|
|
|
|
|
*/
|
|
|
|
|
addProseMirrorPlugins?: (this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2020-11-30 21:12:36 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2020-11-30 21:12:36 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addProseMirrorPlugins'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => Plugin[],
|
|
|
|
|
|
2021-05-07 00:39:47 +08:00
|
|
|
|
/**
|
|
|
|
|
* Extensions
|
|
|
|
|
*/
|
|
|
|
|
addExtensions?: (this: {
|
|
|
|
|
name: string,
|
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addExtensions'],
|
2021-05-07 16:08:30 +08:00
|
|
|
|
}) => Extensions,
|
2021-05-07 00:39:47 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Extend Node Schema
|
|
|
|
|
*/
|
|
|
|
|
extendNodeSchema?: ((
|
|
|
|
|
this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['extendNodeSchema'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
extension: Node,
|
2021-04-21 15:43:31 +08:00
|
|
|
|
) => Record<string, any>) | null,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extend Mark Schema
|
|
|
|
|
*/
|
|
|
|
|
extendMarkSchema?: ((
|
|
|
|
|
this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['extendMarkSchema'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
extension: Node,
|
2021-04-21 15:43:31 +08:00
|
|
|
|
) => 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.
|
|
|
|
|
*/
|
2021-04-21 05:11:35 +08:00
|
|
|
|
onBeforeCreate?: ((this: {
|
|
|
|
|
name: string,
|
2021-04-02 06:07:40 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-04-02 06:07:40 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onBeforeCreate'],
|
2021-04-02 06:07:40 +08:00
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* The editor is ready.
|
|
|
|
|
*/
|
|
|
|
|
onCreate?: ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onCreate'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => void) | null,
|
2020-11-30 21:12:36 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* The content has changed.
|
|
|
|
|
*/
|
|
|
|
|
onUpdate?: ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2020-11-30 21:12:36 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2020-11-30 21:12:36 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onUpdate'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The selection has changed.
|
|
|
|
|
*/
|
2021-04-08 04:07:36 +08:00
|
|
|
|
onSelectionUpdate?: ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onSelectionUpdate'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor state has changed.
|
|
|
|
|
*/
|
|
|
|
|
onTransaction?: ((
|
|
|
|
|
this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onTransaction'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
transaction: Transaction,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is focused.
|
|
|
|
|
*/
|
|
|
|
|
onFocus?: ((
|
|
|
|
|
this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onFocus'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor isn’t focused anymore.
|
|
|
|
|
*/
|
|
|
|
|
onBlur?: ((
|
|
|
|
|
this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onBlur'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is destroyed.
|
|
|
|
|
*/
|
|
|
|
|
onDestroy?: ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onDestroy'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Node View
|
|
|
|
|
*/
|
|
|
|
|
addNodeView?: ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: NodeType,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addNodeView'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => NodeViewRenderer) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TopNode
|
|
|
|
|
*/
|
|
|
|
|
topNode?: boolean,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Content
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
content?: NodeSpec['content'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['content'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['content']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Marks
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
marks?: NodeSpec['marks'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['marks'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['marks']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Group
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
group?: NodeSpec['group'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['group'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['group']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Inline
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
inline?: NodeSpec['inline'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['inline'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['inline']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Atom
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
atom?: NodeSpec['atom'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['atom'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['atom']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Selectable
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
selectable?: NodeSpec['selectable'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['selectable'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['selectable']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Draggable
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
draggable?: NodeSpec['draggable'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['draggable'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['draggable']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Code
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
code?: NodeSpec['code'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['code'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['code']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defining
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
defining?: NodeSpec['defining'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['defining'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['defining']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Isolating
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
isolating?: NodeSpec['isolating'] | ((this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['isolating'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => NodeSpec['isolating']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse HTML
|
|
|
|
|
*/
|
|
|
|
|
parseHTML?: (
|
|
|
|
|
this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['parseHTML'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
) => NodeSpec['parseDOM'],
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render HTML
|
|
|
|
|
*/
|
|
|
|
|
renderHTML?: ((
|
|
|
|
|
this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['renderHTML'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
node: ProseMirrorNode,
|
2021-04-21 15:43:31 +08:00
|
|
|
|
HTMLAttributes: Record<string, any>,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}
|
|
|
|
|
) => DOMOutputSpec) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render Text
|
|
|
|
|
*/
|
|
|
|
|
renderText?: ((
|
|
|
|
|
this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['renderText'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
node: ProseMirrorNode,
|
2021-09-10 05:51:05 +08:00
|
|
|
|
pos: number,
|
|
|
|
|
parent: ProseMirrorNode,
|
|
|
|
|
index: number,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}
|
|
|
|
|
) => string) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add Attributes
|
|
|
|
|
*/
|
|
|
|
|
addAttributes?: (
|
|
|
|
|
this: {
|
2021-04-21 05:11:35 +08:00
|
|
|
|
name: string,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
options: Options,
|
2021-10-22 14:52:54 +08:00
|
|
|
|
storage: Storage,
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addAttributes'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
) => Attributes | {},
|
|
|
|
|
}
|
|
|
|
|
}
|
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,
|
|
|
|
|
defaultOptions: {},
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2020-11-16 06:25:25 +08:00
|
|
|
|
this.options = this.config.defaultOptions
|
2021-10-22 14:52:54 +08:00
|
|
|
|
this.storage = callOrReturn(getExtensionField<AnyConfig['addStorage']>(
|
|
|
|
|
this,
|
|
|
|
|
'addStorage',
|
|
|
|
|
{
|
|
|
|
|
name: this.name,
|
|
|
|
|
options: this.options,
|
|
|
|
|
},
|
|
|
|
|
))
|
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()
|
|
|
|
|
|
|
|
|
|
extension.options = mergeDeep(this.options, options) as Options
|
|
|
|
|
|
|
|
|
|
return extension
|
2020-10-10 04:59:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
extend<ExtendedOptions = Options, ExtendedStorage = Storage>(extendedConfig: Partial<NodeConfig<ExtendedOptions, ExtendedStorage>> = {}) {
|
|
|
|
|
const extension = new Node<ExtendedOptions, ExtendedStorage>(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
|
|
|
|
|
|
2021-04-16 04:03:45 +08:00
|
|
|
|
extension.name = extendedConfig.name
|
|
|
|
|
? extendedConfig.name
|
2021-04-18 03:25:29 +08:00
|
|
|
|
: extension.parent.name
|
2021-04-16 04:03:45 +08:00
|
|
|
|
|
2021-04-18 03:25:29 +08:00
|
|
|
|
extension.options = extendedConfig.defaultOptions
|
|
|
|
|
? extendedConfig.defaultOptions
|
|
|
|
|
: extension.parent.options
|
2021-04-12 17:11:02 +08:00
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
extension.storage = callOrReturn(getExtensionField<AnyConfig['addStorage']>(
|
|
|
|
|
extension,
|
|
|
|
|
'addStorage',
|
|
|
|
|
{
|
|
|
|
|
name: extension.name,
|
|
|
|
|
options: extension.options,
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
|
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
|
|
|
|
}
|