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'
|
2020-11-17 04:54:40 +08:00
|
|
|
|
import { InputRule } from 'prosemirror-inputrules'
|
2020-11-16 18:21:54 +08:00
|
|
|
|
import { ExtensionConfig } from './Extension'
|
2021-01-20 16:18:49 +08:00
|
|
|
|
import mergeDeep from './utilities/mergeDeep'
|
2021-02-17 01:36:37 +08:00
|
|
|
|
import { Attributes, Overwrite, RawCommands } from './types'
|
2020-10-23 05:21:52 +08:00
|
|
|
|
import { Editor } from './Editor'
|
2020-10-22 03:13:38 +08:00
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
|
export interface MarkConfig<Options = any> extends Overwrite<ExtensionConfig<Options>, {
|
2020-10-23 05:21:52 +08:00
|
|
|
|
/**
|
|
|
|
|
* Inclusive
|
|
|
|
|
*/
|
2020-10-30 22:20:10 +08:00
|
|
|
|
inclusive?: MarkSpec['inclusive'] | ((this: { options: Options }) => MarkSpec['inclusive']),
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Excludes
|
|
|
|
|
*/
|
2020-10-30 22:20:10 +08:00
|
|
|
|
excludes?: MarkSpec['excludes'] | ((this: { options: Options }) => MarkSpec['excludes']),
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Group
|
|
|
|
|
*/
|
2020-10-30 22:20:10 +08:00
|
|
|
|
group?: MarkSpec['group'] | ((this: { options: Options }) => MarkSpec['group']),
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spanning
|
|
|
|
|
*/
|
2020-10-30 22:20:10 +08:00
|
|
|
|
spanning?: MarkSpec['spanning'] | ((this: { options: Options }) => MarkSpec['spanning']),
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse HTML
|
|
|
|
|
*/
|
2020-10-22 03:13:38 +08:00
|
|
|
|
parseHTML?: (
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
},
|
|
|
|
|
) => MarkSpec['parseDOM'],
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render HTML
|
|
|
|
|
*/
|
2020-10-23 20:24:19 +08:00
|
|
|
|
renderHTML?: ((
|
2020-10-22 03:13:38 +08:00
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
2020-11-16 18:19:43 +08:00
|
|
|
|
mark: ProseMirrorMark,
|
2020-11-13 23:07:20 +08:00
|
|
|
|
HTMLAttributes: { [key: string]: any },
|
2020-10-22 03:13:38 +08:00
|
|
|
|
}
|
2020-10-23 20:24:19 +08:00
|
|
|
|
) => DOMOutputSpec) | null,
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attributes
|
|
|
|
|
*/
|
2020-10-22 17:14:44 +08:00
|
|
|
|
addAttributes?: (
|
2020-10-22 05:55:14 +08:00
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
},
|
2020-12-04 06:32:11 +08:00
|
|
|
|
) => Attributes | {},
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Commands
|
|
|
|
|
*/
|
|
|
|
|
addCommands?: (this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2021-02-17 01:36:37 +08:00
|
|
|
|
}) => Partial<RawCommands>,
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Keyboard shortcuts
|
|
|
|
|
*/
|
|
|
|
|
addKeyboardShortcuts?: (this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
|
|
|
|
}) => {
|
|
|
|
|
[key: string]: any
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Input rules
|
|
|
|
|
*/
|
|
|
|
|
addInputRules?: (this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2020-11-17 04:54:40 +08:00
|
|
|
|
}) => InputRule[],
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Paste rules
|
|
|
|
|
*/
|
|
|
|
|
addPasteRules?: (this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
2020-11-17 04:54:40 +08:00
|
|
|
|
}) => Plugin[],
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ProseMirror plugins
|
|
|
|
|
*/
|
|
|
|
|
addProseMirrorPlugins?: (this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
|
|
|
|
}) => Plugin[],
|
2020-11-30 20:50:06 +08:00
|
|
|
|
|
2020-11-30 21:12:36 +08:00
|
|
|
|
/**
|
|
|
|
|
* The editor is ready.
|
|
|
|
|
*/
|
|
|
|
|
onCreate?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The content has changed.
|
|
|
|
|
*/
|
|
|
|
|
onUpdate?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The selection has changed.
|
|
|
|
|
*/
|
|
|
|
|
onSelection?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
|
|
|
|
}) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor state has changed.
|
|
|
|
|
*/
|
|
|
|
|
onTransaction?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
transaction: Transaction,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is focused.
|
|
|
|
|
*/
|
|
|
|
|
onFocus?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor isn’t focused anymore.
|
|
|
|
|
*/
|
|
|
|
|
onBlur?: ((
|
|
|
|
|
this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
|
|
|
|
},
|
|
|
|
|
props: {
|
|
|
|
|
event: FocusEvent,
|
|
|
|
|
},
|
|
|
|
|
) => void) | null,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor is destroyed.
|
|
|
|
|
*/
|
2020-11-30 20:50:06 +08:00
|
|
|
|
onDestroy?: ((this: {
|
|
|
|
|
options: Options,
|
|
|
|
|
editor: Editor,
|
|
|
|
|
type: MarkType,
|
|
|
|
|
}) => void) | null,
|
2020-10-23 17:24:27 +08:00
|
|
|
|
}> {}
|
2020-10-22 03:13:38 +08:00
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
|
export class Mark<Options = any> {
|
2020-11-20 04:08:25 +08:00
|
|
|
|
type = 'mark'
|
|
|
|
|
|
2020-11-16 18:21:54 +08:00
|
|
|
|
config: Required<MarkConfig> = {
|
2020-11-16 06:25:25 +08:00
|
|
|
|
name: 'mark',
|
|
|
|
|
defaultOptions: {},
|
|
|
|
|
addGlobalAttributes: () => [],
|
|
|
|
|
addCommands: () => ({}),
|
|
|
|
|
addKeyboardShortcuts: () => ({}),
|
|
|
|
|
addInputRules: () => [],
|
|
|
|
|
addPasteRules: () => [],
|
|
|
|
|
addProseMirrorPlugins: () => [],
|
|
|
|
|
inclusive: null,
|
|
|
|
|
excludes: null,
|
|
|
|
|
group: null,
|
|
|
|
|
spanning: null,
|
|
|
|
|
parseHTML: () => null,
|
|
|
|
|
renderHTML: null,
|
|
|
|
|
addAttributes: () => ({}),
|
2020-11-30 21:12:36 +08:00
|
|
|
|
onCreate: null,
|
|
|
|
|
onUpdate: null,
|
|
|
|
|
onSelection: null,
|
|
|
|
|
onTransaction: null,
|
|
|
|
|
onFocus: null,
|
|
|
|
|
onBlur: null,
|
2020-11-30 20:50:06 +08:00
|
|
|
|
onDestroy: null,
|
2020-11-16 06:25:25 +08:00
|
|
|
|
}
|
2020-10-22 03:13:38 +08:00
|
|
|
|
|
2020-11-16 06:25:25 +08:00
|
|
|
|
options!: Options
|
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
|
constructor(config: 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.options = this.config.defaultOptions
|
2020-10-22 03:13:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
|
static create<O>(config: MarkConfig<O>) {
|
|
|
|
|
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> = {}) {
|
2020-11-16 18:19:43 +08:00
|
|
|
|
return Mark
|
2021-02-10 16:59:35 +08:00
|
|
|
|
.create<Options>(this.config as MarkConfig<Options>)
|
2021-01-20 05:29:46 +08:00
|
|
|
|
.#configure(options)
|
2020-11-16 17:22:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 18:07:06 +08:00
|
|
|
|
#configure = (options: Partial<Options>) => {
|
2021-01-20 16:18:49 +08:00
|
|
|
|
this.options = mergeDeep(this.config.defaultOptions, options) as Options
|
2020-11-16 16:43:17 +08:00
|
|
|
|
|
|
|
|
|
return this
|
2020-10-22 03:13:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
|
extend<ExtendedOptions = Options>(extendedConfig: Partial<MarkConfig<ExtendedOptions>>) {
|
|
|
|
|
return new Mark<ExtendedOptions>({
|
2020-11-16 06:25:25 +08:00
|
|
|
|
...this.config,
|
|
|
|
|
...extendedConfig,
|
2021-02-10 16:59:35 +08:00
|
|
|
|
} as MarkConfig<ExtendedOptions>)
|
2020-11-16 06:25:25 +08:00
|
|
|
|
}
|
2020-10-22 03:13:38 +08:00
|
|
|
|
}
|