2020-10-23 05:21:52 +08:00
|
|
|
|
import {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
DOMOutputSpec, Mark as ProseMirrorMark, MarkSpec, MarkType,
|
|
|
|
|
} 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 { MarkConfig } from './index.js'
|
|
|
|
|
import { InputRule } from './InputRule.js'
|
|
|
|
|
import { Node } from './Node.js'
|
|
|
|
|
import { PasteRule } from './PasteRule.js'
|
2021-04-12 17:11:02 +08:00
|
|
|
|
import {
|
2021-10-22 14:52:54 +08:00
|
|
|
|
AnyConfig,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
Attributes,
|
2022-06-08 20:10:25 +08:00
|
|
|
|
Extensions,
|
2021-04-12 17:11:02 +08:00
|
|
|
|
GlobalAttributes,
|
2021-04-21 04:58:09 +08:00
|
|
|
|
KeyboardShortcutCommand,
|
2022-06-08 20:10:25 +08:00
|
|
|
|
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-22 03:13:38 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
declare module '@tiptap/core' {
|
2021-10-22 14:52:54 +08:00
|
|
|
|
export interface MarkConfig<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<MarkConfig<Options, Storage>>['addOptions'], undefined>
|
|
|
|
|
}) => Options
|
2021-10-27 00:31:13 +08:00
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* Default Storage
|
|
|
|
|
*/
|
2021-10-27 00:31:13 +08:00
|
|
|
|
addStorage?: (this: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
parent: Exclude<ParentConfig<MarkConfig<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<MarkConfig<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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['addKeyboardShortcuts']
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
[key: string]: KeyboardShortcutCommand
|
|
|
|
|
}
|
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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['addInputRules']
|
|
|
|
|
}) => InputRule[]
|
2020-11-30 21:12:36 +08:00
|
|
|
|
|
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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<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<MarkConfig<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<MarkConfig<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<MarkConfig<Options, Storage>>['extendMarkSchema']
|
|
|
|
|
},
|
|
|
|
|
extension: Mark,
|
|
|
|
|
) => 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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['onCreate']
|
|
|
|
|
}) => void)
|
|
|
|
|
| null
|
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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['onSelectionUpdate']
|
|
|
|
|
}) => void)
|
|
|
|
|
| null
|
2021-03-09 16:50:03 +08:00
|
|
|
|
|
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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<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: MarkType
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['onDestroy']
|
|
|
|
|
}) => void)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
2021-04-02 01:06:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* Keep mark after split node
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
keepOnSplit?: boolean | (() => boolean)
|
2021-04-02 01:06:40 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Inclusive
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
inclusive?:
|
|
|
|
|
| MarkSpec['inclusive']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['inclusive']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => MarkSpec['inclusive'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Excludes
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
excludes?:
|
|
|
|
|
| MarkSpec['excludes']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['excludes']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => MarkSpec['excludes'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
2022-08-22 21:23:44 +08:00
|
|
|
|
/**
|
|
|
|
|
* Marks this Mark as exitable
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
exitable?: boolean | (() => boolean)
|
2022-08-22 21:23:44 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Group
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
group?:
|
|
|
|
|
| MarkSpec['group']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['group']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => MarkSpec['group'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spanning
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
spanning?:
|
|
|
|
|
| MarkSpec['spanning']
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['spanning']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => MarkSpec['spanning'])
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2021-10-08 21:02:09 +08:00
|
|
|
|
* Code
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
code?:
|
|
|
|
|
| boolean
|
|
|
|
|
| ((this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<MarkConfig<Options, Storage>>['code']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => boolean)
|
2021-10-08 21:02:09 +08:00
|
|
|
|
|
|
|
|
|
/**
|
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<MarkConfig<Options, Storage>>['parseHTML']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}) => MarkSpec['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<MarkConfig<Options, Storage>>['renderHTML']
|
2023-03-29 21:16:43 +08:00
|
|
|
|
editor?: Editor
|
2023-02-03 00:37:33 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
mark: ProseMirrorMark
|
|
|
|
|
HTMLAttributes: Record<string, any>
|
|
|
|
|
},
|
|
|
|
|
) => DOMOutputSpec)
|
|
|
|
|
| null
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attributes
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
addAttributes?: (this: {
|
|
|
|
|
name: string
|
|
|
|
|
options: Options
|
|
|
|
|
storage: Storage
|
|
|
|
|
parent: ParentConfig<MarkConfig<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-22 03:13:38 +08:00
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
export class Mark<Options = any, Storage = any> {
|
2021-04-16 04:03:45 +08:00
|
|
|
|
type = 'mark'
|
2020-11-20 04:08:25 +08:00
|
|
|
|
|
2021-04-16 04:03:45 +08:00
|
|
|
|
name = 'mark'
|
2021-04-15 20:40:28 +08:00
|
|
|
|
|
|
|
|
|
parent: Mark | null = null
|
2021-04-12 17:11:02 +08:00
|
|
|
|
|
2021-04-15 20:40:28 +08:00
|
|
|
|
child: Mark | null = null
|
2020-11-16 06:25:25 +08:00
|
|
|
|
|
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: MarkConfig = {
|
|
|
|
|
name: this.name,
|
|
|
|
|
defaultOptions: {},
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
constructor(config: Partial<MarkConfig<Options, Storage>> = {}) {
|
2020-11-16 06:25:25 +08:00
|
|
|
|
this.config = {
|
|
|
|
|
...this.config,
|
2020-10-22 03:13:38 +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
|
|
|
|
|
|
|
|
|
if (config.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: "${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-10-22 03:13:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 14:52:54 +08:00
|
|
|
|
static create<O = any, S = any>(config: Partial<MarkConfig<O, S>> = {}) {
|
|
|
|
|
return new Mark<O, S>(config)
|
2020-11-16 06:25:25 +08:00
|
|
|
|
}
|
2020-10-22 03:13:38 +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-22 03:13:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
|
extend<ExtendedOptions = Options, ExtendedStorage = Storage>(
|
|
|
|
|
extendedConfig: Partial<MarkConfig<ExtendedOptions, ExtendedStorage>> = {},
|
|
|
|
|
) {
|
2021-10-22 14:52:54 +08:00
|
|
|
|
const extension = new Mark<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
|
|
|
|
|
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
|
|
|
|
}
|
2022-08-22 21:23:44 +08:00
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
|
static handleExit({ editor, mark }: { editor: Editor; mark: Mark }) {
|
2022-08-22 21:23:44 +08:00
|
|
|
|
const { tr } = editor.state
|
|
|
|
|
const currentPos = editor.state.selection.$from
|
|
|
|
|
const isAtEnd = currentPos.pos === currentPos.end()
|
|
|
|
|
|
|
|
|
|
if (isAtEnd) {
|
|
|
|
|
const currentMarks = currentPos.marks()
|
|
|
|
|
const isInMark = !!currentMarks.find(m => m?.type.name === mark.name)
|
|
|
|
|
|
|
|
|
|
if (!isInMark) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const removeMark = currentMarks.find(m => m?.type.name === mark.name)
|
|
|
|
|
|
|
|
|
|
if (removeMark) {
|
|
|
|
|
tr.removeStoredMark(removeMark)
|
|
|
|
|
}
|
|
|
|
|
tr.insertText(' ', currentPos.pos)
|
|
|
|
|
|
|
|
|
|
editor.view.dispatch(tr)
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
2020-10-22 03:13:38 +08:00
|
|
|
|
}
|