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'
|
2024-06-03 23:49:07 +08:00
|
|
|
|
import { Mark } from './Mark.js'
|
2023-07-01 03:03:49 +08:00
|
|
|
|
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> {
|
2024-06-11 20:24:10 +08:00
|
|
|
|
// @ts-ignore - this is a dynamic key
|
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
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* The extension name - this must be unique.
|
|
|
|
|
* It will be used to identify the extension.
|
|
|
|
|
*
|
|
|
|
|
* @example 'myExtension'
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
/**
|
2024-08-12 17:14:41 +08:00
|
|
|
|
* The priority of your extension. The higher, the earlier it will be called
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* and will take precedence over other extensions with a lower priority.
|
2024-08-07 21:39:49 +08:00
|
|
|
|
* @default 100
|
|
|
|
|
* @example 101
|
2021-04-08 00:29:16 +08:00
|
|
|
|
*/
|
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
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* The default options for this extension.
|
|
|
|
|
* @example
|
|
|
|
|
* defaultOptions: {
|
|
|
|
|
* myOption: 'foo',
|
|
|
|
|
* myOtherOption: 10,
|
|
|
|
|
* }
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This method will add options to this extension
|
|
|
|
|
* @see https://tiptap.dev/guide/custom-extensions#settings
|
|
|
|
|
* @example
|
|
|
|
|
* addOptions() {
|
|
|
|
|
* return {
|
|
|
|
|
* myOption: 'foo',
|
|
|
|
|
* myOtherOption: 10,
|
|
|
|
|
* }
|
2021-10-27 00:31:13 +08:00
|
|
|
|
*/
|
|
|
|
|
addOptions?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
2024-10-29 04:44:47 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addOptions']
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => Options
|
2021-10-27 00:31:13 +08:00
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* The default storage this extension can save data to.
|
|
|
|
|
* @see https://tiptap.dev/guide/custom-extensions#storage
|
|
|
|
|
* @example
|
|
|
|
|
* defaultStorage: {
|
|
|
|
|
* prefetchedUsers: [],
|
|
|
|
|
* loading: false,
|
|
|
|
|
* }
|
2021-10-22 14:52:54 +08:00
|
|
|
|
*/
|
|
|
|
|
addStorage?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
2024-10-29 04:44:47 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addStorage']
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => Storage
|
2021-10-22 14:52:54 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This function adds globalAttributes to specific nodes.
|
|
|
|
|
* @see https://tiptap.dev/guide/custom-extensions#global-attributes
|
|
|
|
|
* @example
|
|
|
|
|
* addGlobalAttributes() {
|
|
|
|
|
* return [
|
|
|
|
|
* {
|
|
|
|
|
// Extend the following extensions
|
|
|
|
|
* types: [
|
|
|
|
|
* 'heading',
|
|
|
|
|
* 'paragraph',
|
|
|
|
|
* ],
|
|
|
|
|
* // … with those attributes
|
|
|
|
|
* attributes: {
|
|
|
|
|
* textAlign: {
|
|
|
|
|
* default: 'left',
|
|
|
|
|
* renderHTML: attributes => ({
|
|
|
|
|
* style: `text-align: ${attributes.textAlign}`,
|
|
|
|
|
* }),
|
|
|
|
|
* parseHTML: element => element.style.textAlign || 'left',
|
|
|
|
|
* },
|
|
|
|
|
* },
|
|
|
|
|
* },
|
|
|
|
|
* ]
|
|
|
|
|
* }
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
|
|
|
|
addGlobalAttributes?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
2024-06-03 23:49:07 +08:00
|
|
|
|
extensions: (Node | Mark)[]
|
2023-02-03 00:37:33 +08:00
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['addGlobalAttributes']
|
2024-06-03 23:49:07 +08:00
|
|
|
|
}) => GlobalAttributes
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This function adds commands to the editor
|
|
|
|
|
* @see https://tiptap.dev/guide/custom-extensions#keyboard-shortcuts
|
|
|
|
|
* @example
|
|
|
|
|
* addCommands() {
|
|
|
|
|
* return {
|
|
|
|
|
* myCommand: () => ({ chain }) => chain().setMark('type', 'foo').run(),
|
|
|
|
|
* }
|
|
|
|
|
* }
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This function registers keyboard shortcuts.
|
|
|
|
|
* @see https://tiptap.dev/guide/custom-extensions#keyboard-shortcuts
|
|
|
|
|
* @example
|
|
|
|
|
* addKeyboardShortcuts() {
|
|
|
|
|
* return {
|
|
|
|
|
* 'Mod-l': () => this.editor.commands.toggleBulletList(),
|
|
|
|
|
* }
|
|
|
|
|
* },
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This function adds input rules to the editor.
|
|
|
|
|
* @see https://tiptap.dev/guide/custom-extensions#input-rules
|
|
|
|
|
* @example
|
|
|
|
|
* addInputRules() {
|
|
|
|
|
* return [
|
|
|
|
|
* markInputRule({
|
|
|
|
|
* find: inputRegex,
|
|
|
|
|
* type: this.type,
|
|
|
|
|
* }),
|
|
|
|
|
* ]
|
|
|
|
|
* },
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This function adds paste rules to the editor.
|
|
|
|
|
* @see https://tiptap.dev/guide/custom-extensions#paste-rules
|
|
|
|
|
* @example
|
|
|
|
|
* addPasteRules() {
|
|
|
|
|
* return [
|
|
|
|
|
* markPasteRule({
|
|
|
|
|
* find: pasteRegex,
|
|
|
|
|
* type: this.type,
|
|
|
|
|
* }),
|
|
|
|
|
* ]
|
|
|
|
|
* },
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This function adds Prosemirror plugins to the editor
|
|
|
|
|
* @see https://tiptap.dev/guide/custom-extensions#prosemirror-plugins
|
|
|
|
|
* @example
|
|
|
|
|
* addProseMirrorPlugins() {
|
|
|
|
|
* return [
|
|
|
|
|
* customPlugin(),
|
|
|
|
|
* ]
|
|
|
|
|
* }
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This function adds additional extensions to the editor. This is useful for
|
|
|
|
|
* building extension kits.
|
|
|
|
|
* @example
|
|
|
|
|
* addExtensions() {
|
|
|
|
|
* return [
|
|
|
|
|
* BulletList,
|
|
|
|
|
* OrderedList,
|
|
|
|
|
* ListItem
|
|
|
|
|
* ]
|
|
|
|
|
* }
|
2021-05-07 00:39:47 +08:00
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This function extends the schema of the node.
|
|
|
|
|
* @example
|
|
|
|
|
* extendNodeSchema() {
|
|
|
|
|
* return {
|
|
|
|
|
* group: 'inline',
|
|
|
|
|
* selectable: false,
|
|
|
|
|
* }
|
|
|
|
|
* }
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* This function extends the schema of the mark.
|
|
|
|
|
* @example
|
|
|
|
|
* extendMarkSchema() {
|
|
|
|
|
* return {
|
|
|
|
|
* group: 'inline',
|
|
|
|
|
* selectable: false,
|
|
|
|
|
* }
|
|
|
|
|
* }
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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: {
|
2024-08-07 23:50:04 +08:00
|
|
|
|
editor: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* Defines if this node should be a top level node (doc)
|
|
|
|
|
* @default false
|
|
|
|
|
* @example true
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
topNode?: boolean
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* The content expression for this node, as described in the [schema
|
|
|
|
|
* guide](/docs/guide/#schema.content_expressions). When not given,
|
|
|
|
|
* the node does not allow any content.
|
|
|
|
|
*
|
|
|
|
|
* You can read more about it on the Prosemirror documentation here
|
|
|
|
|
* @see https://prosemirror.net/docs/guide/#schema.content_expressions
|
|
|
|
|
* @default undefined
|
|
|
|
|
* @example content: 'block+'
|
|
|
|
|
* @example content: 'headline paragraph block*'
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* The marks that are allowed inside of this node. May be a
|
|
|
|
|
* space-separated string referring to mark names or groups, `"_"`
|
|
|
|
|
* to explicitly allow all marks, or `""` to disallow marks. When
|
|
|
|
|
* not given, nodes with inline content default to allowing all
|
|
|
|
|
* marks, other nodes default to not allowing marks.
|
|
|
|
|
*
|
|
|
|
|
* @example marks: 'strong em'
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* The group or space-separated groups to which this node belongs,
|
|
|
|
|
* which can be referred to in the content expressions for the
|
|
|
|
|
* schema.
|
|
|
|
|
*
|
|
|
|
|
* By default Tiptap uses the groups 'block' and 'inline' for nodes. You
|
|
|
|
|
* can also use custom groups if you want to group specific nodes together
|
|
|
|
|
* and handle them in your schema.
|
|
|
|
|
* @example group: 'block'
|
|
|
|
|
* @example group: 'inline'
|
|
|
|
|
* @example group: 'customBlock' // this uses a custom group
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* Should be set to true for inline nodes. (Implied for text nodes.)
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* Can be set to true to indicate that, though this isn't a [leaf
|
|
|
|
|
* node](https://prosemirror.net/docs/ref/#model.NodeType.isLeaf), it doesn't have directly editable
|
|
|
|
|
* content and should be treated as a single unit in the view.
|
|
|
|
|
*
|
|
|
|
|
* @example atom: true
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* Controls whether nodes of this type can be selected as a [node
|
|
|
|
|
* selection](https://prosemirror.net/docs/ref/#state.NodeSelection). Defaults to true for non-text
|
|
|
|
|
* nodes.
|
|
|
|
|
*
|
|
|
|
|
* @default true
|
|
|
|
|
* @example selectable: false
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* Determines whether nodes of this type can be dragged without
|
|
|
|
|
* being selected. Defaults to false.
|
|
|
|
|
*
|
|
|
|
|
* @default: false
|
|
|
|
|
* @example: draggable: true
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* Can be used to indicate that this node contains code, which
|
|
|
|
|
* causes some commands to behave differently.
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* Controls way whitespace in this a node is parsed. The default is
|
|
|
|
|
* `"normal"`, which causes the [DOM parser](https://prosemirror.net/docs/ref/#model.DOMParser) to
|
|
|
|
|
* collapse whitespace in normal mode, and normalize it (replacing
|
|
|
|
|
* newlines and such with spaces) otherwise. `"pre"` causes the
|
|
|
|
|
* parser to preserve spaces inside the node. When this option isn't
|
|
|
|
|
* given, but [`code`](https://prosemirror.net/docs/ref/#model.NodeSpec.code) is true, `whitespace`
|
|
|
|
|
* will default to `"pre"`. Note that this option doesn't influence
|
|
|
|
|
* the way the node is rendered—that should be handled by `toDOM`
|
|
|
|
|
* and/or styling.
|
2022-01-04 17:02:24 +08:00
|
|
|
|
*/
|
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
|
|
|
|
/**
|
2024-11-11 16:05:32 +08:00
|
|
|
|
* Allows a **single** node to be set as linebreak equivalent (e.g. hardBreak).
|
|
|
|
|
* When converting between block types that have whitespace set to "pre"
|
|
|
|
|
* and don't support the linebreak node (e.g. codeBlock) and other block types
|
|
|
|
|
* that do support the linebreak node (e.g. paragraphs) - this node will be used
|
|
|
|
|
* as the linebreak instead of stripping the newline.
|
|
|
|
|
*
|
|
|
|
|
* See [linebreakReplacement](https://prosemirror.net/docs/ref/#model.NodeSpec.linebreakReplacement).
|
|
|
|
|
*/
|
|
|
|
|
linebreakReplacement?:
|
|
|
|
|
| NodeSpec['linebreakReplacement']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<NodeConfig<Options, Storage>>['linebreakReplacement']
|
|
|
|
|
editor?: Editor
|
|
|
|
|
}) => NodeSpec['linebreakReplacement'])
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* When enabled, enables both
|
|
|
|
|
* [`definingAsContext`](https://prosemirror.net/docs/ref/#model.NodeSpec.definingAsContext) and
|
|
|
|
|
* [`definingForContent`](https://prosemirror.net/docs/ref/#model.NodeSpec.definingForContent).
|
|
|
|
|
*
|
|
|
|
|
* @default false
|
|
|
|
|
* @example isolating: true
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* When enabled (default is false), the sides of nodes of this type
|
|
|
|
|
* count as boundaries that regular editing operations, like
|
|
|
|
|
* backspacing or lifting, won't cross. An example of a node that
|
|
|
|
|
* should probably have this enabled is a table cell.
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* Associates DOM parser information with this node, which can be
|
|
|
|
|
* used by [`DOMParser.fromSchema`](https://prosemirror.net/docs/ref/#model.DOMParser^fromSchema) to
|
|
|
|
|
* automatically derive a parser. The `node` field in the rules is
|
|
|
|
|
* implied (the name of this node will be filled in automatically).
|
|
|
|
|
* If you supply your own parser, you do not need to also specify
|
|
|
|
|
* parsing rules in your schema.
|
|
|
|
|
*
|
|
|
|
|
* @example parseHTML: [{ tag: 'div', attrs: { 'data-id': 'my-block' } }]
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* A description of a DOM structure. Can be either a string, which is
|
|
|
|
|
* interpreted as a text node, a DOM node, which is interpreted as
|
|
|
|
|
* itself, a `{dom, contentDOM}` object, or an array.
|
|
|
|
|
*
|
|
|
|
|
* An array describes a DOM element. The first value in the array
|
|
|
|
|
* should be a string—the name of the DOM element, optionally prefixed
|
|
|
|
|
* by a namespace URL and a space. If the second element is plain
|
|
|
|
|
* object, it is interpreted as a set of attributes for the element.
|
|
|
|
|
* Any elements after that (including the 2nd if it's not an attribute
|
|
|
|
|
* object) are interpreted as children of the DOM elements, and must
|
|
|
|
|
* either be valid `DOMOutputSpec` values, or the number zero.
|
|
|
|
|
*
|
|
|
|
|
* The number zero (pronounced “hole”) is used to indicate the place
|
|
|
|
|
* where a node's child nodes should be inserted. If it occurs in an
|
|
|
|
|
* output spec, it should be the only child element in its parent
|
|
|
|
|
* node.
|
|
|
|
|
*
|
|
|
|
|
* @example toDOM: ['div[data-id="my-block"]', { class: 'my-block' }, 0]
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* renders the node as text
|
|
|
|
|
* @example renderText: () => 'foo
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
|
/**
|
2024-05-11 20:30:44 +08:00
|
|
|
|
* Add attributes to the node
|
|
|
|
|
* @example addAttributes: () => ({ class: 'foo' })
|
2021-02-20 00:35:50 +08:00
|
|
|
|
*/
|
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
|
|
|
|
|
2024-05-11 20:30:44 +08:00
|
|
|
|
/**
|
|
|
|
|
* The Node class is used to create custom node extensions.
|
|
|
|
|
* @see https://tiptap.dev/api/extensions#create-a-new-extension
|
|
|
|
|
*/
|
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`
|
2024-08-01 06:28:51 +08:00
|
|
|
|
const extension = this.extend<Options, Storage>({
|
2024-06-18 14:00:28 +08:00
|
|
|
|
...this.config,
|
2024-07-17 15:06:41 +08:00
|
|
|
|
addOptions: () => {
|
|
|
|
|
return mergeDeep(this.options as Record<string, any>, options) as Options
|
2024-06-18 14:00:28 +08:00
|
|
|
|
},
|
|
|
|
|
})
|
2021-06-03 19:13:43 +08:00
|
|
|
|
|
2024-06-18 14:00:28 +08:00
|
|
|
|
// Always preserve the current name
|
|
|
|
|
extension.name = this.name
|
|
|
|
|
// Set the parent to be our parent
|
|
|
|
|
extension.parent = this.parent
|
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>> = {},
|
|
|
|
|
) {
|
2024-06-18 14:00:28 +08:00
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
|
2024-06-18 14:00:28 +08:00
|
|
|
|
if (extendedConfig.defaultOptions && Object.keys(extendedConfig.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: "${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
|
|
|
|
}
|