2020-10-23 05:21:52 +08:00
|
|
|
|
import {
|
2020-11-16 18:19:43 +08:00
|
|
|
|
DOMOutputSpec,
|
|
|
|
|
MarkSpec,
|
|
|
|
|
Mark as ProseMirrorMark,
|
|
|
|
|
MarkType,
|
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-02-17 05:59:45 +08:00
|
|
|
|
import { Command as ProseMirrorCommand } from 'prosemirror-commands'
|
2020-11-17 04:54:40 +08:00
|
|
|
|
import { InputRule } from 'prosemirror-inputrules'
|
2021-01-20 16:18:49 +08:00
|
|
|
|
import mergeDeep from './utilities/mergeDeep'
|
2021-04-12 17:11:02 +08:00
|
|
|
|
import {
|
|
|
|
|
Attributes,
|
|
|
|
|
RawCommands,
|
|
|
|
|
GlobalAttributes,
|
|
|
|
|
ParentConfig,
|
|
|
|
|
} from './types'
|
2021-04-16 03:43:41 +08:00
|
|
|
|
import { Node } from './Node'
|
2021-03-31 19:44:56 +08:00
|
|
|
|
import { MarkConfig } from '.'
|
2020-10-23 05:21:52 +08:00
|
|
|
|
import { Editor } from './Editor'
|
2020-10-22 03:13:38 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
declare module '@tiptap/core' {
|
|
|
|
|
export interface MarkConfig<Options = 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,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Global attributes
|
|
|
|
|
*/
|
|
|
|
|
addGlobalAttributes?: (this: {
|
2020-10-22 03:13:38 +08:00
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['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: {
|
2020-10-22 03:13:38 +08:00
|
|
|
|
options: Options,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['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: {
|
2020-10-22 05:55:14 +08:00
|
|
|
|
options: Options,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['addKeyboardShortcuts'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => {
|
|
|
|
|
[key: string]: ProseMirrorCommand,
|
2020-10-22 05:55:14 +08:00
|
|
|
|
},
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Input rules
|
|
|
|
|
*/
|
|
|
|
|
addInputRules?: (this: {
|
2020-11-30 21:12:36 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['addInputRules'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => InputRule[],
|
2020-11-30 21:12:36 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Paste rules
|
|
|
|
|
*/
|
|
|
|
|
addPasteRules?: (this: {
|
2020-11-30 21:12:36 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['addPasteRules'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => Plugin[],
|
2020-11-30 21:12:36 +08:00
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* ProseMirror plugins
|
|
|
|
|
*/
|
|
|
|
|
addProseMirrorPlugins?: (this: {
|
2020-11-30 21:12:36 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['addProseMirrorPlugins'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => Plugin[],
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extend Node Schema
|
|
|
|
|
*/
|
|
|
|
|
extendNodeSchema?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['extendNodeSchema'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
extension: Node,
|
|
|
|
|
) => {
|
|
|
|
|
[key: string]: any,
|
|
|
|
|
}) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extend Mark Schema
|
|
|
|
|
*/
|
|
|
|
|
extendMarkSchema?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['extendMarkSchema'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
2021-04-16 03:43:41 +08:00
|
|
|
|
extension: Mark,
|
2021-02-20 00:35:50 +08:00
|
|
|
|
) => {
|
|
|
|
|
[key: string]: any,
|
|
|
|
|
}) | null,
|
|
|
|
|
|
2021-04-02 06:07:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* The editor is not ready yet.
|
|
|
|
|
*/
|
2021-04-15 20:40:28 +08:00
|
|
|
|
onBeforeCreate?: ((this: {
|
2021-04-02 06:07:40 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['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: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['onCreate'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The content has changed.
|
|
|
|
|
*/
|
|
|
|
|
onUpdate?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['onUpdate'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The selection has changed.
|
|
|
|
|
*/
|
2021-04-02 00:42:31 +08:00
|
|
|
|
onSelectionUpdate?: ((this: {
|
2021-03-09 16:50:03 +08:00
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['onSelectionUpdate'],
|
2021-03-09 16:50:03 +08:00
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* The editor state has changed.
|
|
|
|
|
*/
|
|
|
|
|
onTransaction?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['onTransaction'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
transaction: Transaction,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is focused.
|
|
|
|
|
*/
|
|
|
|
|
onFocus?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['onFocus'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor isn’t focused anymore.
|
|
|
|
|
*/
|
|
|
|
|
onBlur?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['onBlur'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is destroyed.
|
|
|
|
|
*/
|
|
|
|
|
onDestroy?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['onDestroy'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
2021-04-02 01:06:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* Keep mark after split node
|
|
|
|
|
*/
|
|
|
|
|
keepOnSplit?: boolean | (() => boolean),
|
|
|
|
|
|
2021-02-20 00:35:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* Inclusive
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
inclusive?: MarkSpec['inclusive'] | ((this: {
|
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['inclusive'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => MarkSpec['inclusive']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Excludes
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
excludes?: MarkSpec['excludes'] | ((this: {
|
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['excludes'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => MarkSpec['excludes']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Group
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
group?: MarkSpec['group'] | ((this: {
|
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['group'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => MarkSpec['group']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spanning
|
|
|
|
|
*/
|
2021-04-12 17:11:02 +08:00
|
|
|
|
spanning?: MarkSpec['spanning'] | ((this: {
|
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['spanning'],
|
2021-04-12 17:11:02 +08:00
|
|
|
|
}) => MarkSpec['spanning']),
|
2021-02-20 00:35:50 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse HTML
|
|
|
|
|
*/
|
|
|
|
|
parseHTML?: (
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['parseHTML'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
) => MarkSpec['parseDOM'],
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render HTML
|
|
|
|
|
*/
|
|
|
|
|
renderHTML?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['renderHTML'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
mark: ProseMirrorMark,
|
|
|
|
|
HTMLAttributes: { [key: string]: any },
|
2021-04-15 20:40:28 +08:00
|
|
|
|
},
|
2021-02-20 00:35:50 +08:00
|
|
|
|
) => DOMOutputSpec) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attributes
|
|
|
|
|
*/
|
|
|
|
|
addAttributes?: (
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
2021-04-15 20:40:28 +08:00
|
|
|
|
parent: ParentConfig<MarkConfig<Options>>['addAttributes'],
|
2021-02-20 00:35:50 +08:00
|
|
|
|
},
|
|
|
|
|
) => Attributes | {},
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-22 03:13:38 +08:00
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
|
export class Mark<Options = 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
|
|
|
|
|
|
|
|
|
|
config: MarkConfig = {
|
|
|
|
|
name: this.name,
|
|
|
|
|
priority: 100,
|
|
|
|
|
defaultOptions: {},
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 20:40:28 +08:00
|
|
|
|
constructor(config: Partial<MarkConfig<Options>> = {}) {
|
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
|
2020-11-16 06:25:25 +08:00
|
|
|
|
this.options = this.config.defaultOptions
|
2020-10-22 03:13:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 20:40:28 +08:00
|
|
|
|
static create<O>(config: Partial<MarkConfig<O>> = {}) {
|
2021-02-10 16:59:35 +08:00
|
|
|
|
return new Mark<O>(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-04-15 20:40:28 +08:00
|
|
|
|
this.options = mergeDeep(this.options, options) as Options
|
2020-11-16 16:43:17 +08:00
|
|
|
|
|
|
|
|
|
return this
|
2020-10-22 03:13:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-15 20:40:28 +08:00
|
|
|
|
extend<ExtendedOptions = Options>(extendedConfig: Partial<MarkConfig<ExtendedOptions>> = {}) {
|
|
|
|
|
const extension = new Mark<ExtendedOptions>(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
|
|
|
|
|
: this.name
|
|
|
|
|
|
2021-04-15 20:40:28 +08:00
|
|
|
|
extension.options = {
|
|
|
|
|
...extension.parent.options,
|
|
|
|
|
...extension.options,
|
|
|
|
|
}
|
2021-04-12 17:11:02 +08:00
|
|
|
|
|
|
|
|
|
return extension
|
2020-11-16 06:25:25 +08:00
|
|
|
|
}
|
2020-10-22 03:13:38 +08:00
|
|
|
|
}
|