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'
|
|
|
|
import { Plugin } from 'prosemirror-state'
|
2020-11-16 06:25:25 +08:00
|
|
|
import { ExtensionSpec } from './Extension'
|
2020-10-23 17:24:27 +08:00
|
|
|
import { Attributes, Overwrite } from './types'
|
2020-10-23 05:21:52 +08:00
|
|
|
import { Editor } from './Editor'
|
2020-10-22 03:13:38 +08:00
|
|
|
|
2020-11-14 16:23:47 +08:00
|
|
|
export interface MarkExtensionSpec<Options = any, Commands = {}> extends Overwrite<ExtensionSpec<Options, Commands>, {
|
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,
|
|
|
|
},
|
|
|
|
) => Attributes,
|
2020-10-23 05:21:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Commands
|
|
|
|
*/
|
|
|
|
addCommands?: (this: {
|
|
|
|
options: Options,
|
|
|
|
editor: Editor,
|
|
|
|
type: MarkType,
|
|
|
|
}) => Commands,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keyboard shortcuts
|
|
|
|
*/
|
|
|
|
addKeyboardShortcuts?: (this: {
|
|
|
|
options: Options,
|
|
|
|
editor: Editor,
|
|
|
|
type: MarkType,
|
|
|
|
}) => {
|
|
|
|
[key: string]: any
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Input rules
|
|
|
|
*/
|
|
|
|
addInputRules?: (this: {
|
|
|
|
options: Options,
|
|
|
|
editor: Editor,
|
|
|
|
type: MarkType,
|
|
|
|
}) => any[],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Paste rules
|
|
|
|
*/
|
|
|
|
addPasteRules?: (this: {
|
|
|
|
options: Options,
|
|
|
|
editor: Editor,
|
|
|
|
type: MarkType,
|
|
|
|
}) => any[],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ProseMirror plugins
|
|
|
|
*/
|
|
|
|
addProseMirrorPlugins?: (this: {
|
|
|
|
options: Options,
|
|
|
|
editor: Editor,
|
|
|
|
type: MarkType,
|
|
|
|
}) => Plugin[],
|
2020-10-23 17:24:27 +08:00
|
|
|
}> {}
|
2020-10-22 03:13:38 +08:00
|
|
|
|
2020-11-16 18:19:43 +08:00
|
|
|
export class Mark<Options = any, Commands = {}> {
|
2020-11-16 06:25:25 +08:00
|
|
|
config: Required<MarkExtensionSpec> = {
|
|
|
|
name: 'mark',
|
|
|
|
defaultOptions: {},
|
|
|
|
addGlobalAttributes: () => [],
|
|
|
|
addCommands: () => ({}),
|
|
|
|
addKeyboardShortcuts: () => ({}),
|
|
|
|
addInputRules: () => [],
|
|
|
|
addPasteRules: () => [],
|
|
|
|
addProseMirrorPlugins: () => [],
|
|
|
|
inclusive: null,
|
|
|
|
excludes: null,
|
|
|
|
group: null,
|
|
|
|
spanning: null,
|
|
|
|
parseHTML: () => null,
|
|
|
|
renderHTML: null,
|
|
|
|
addAttributes: () => ({}),
|
|
|
|
}
|
2020-10-22 03:13:38 +08:00
|
|
|
|
2020-11-16 06:25:25 +08:00
|
|
|
options!: Options
|
|
|
|
|
|
|
|
constructor(config: MarkExtensionSpec<Options, Commands>) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-16 06:25:25 +08:00
|
|
|
static create<O, C>(config: MarkExtensionSpec<O, C>) {
|
2020-11-16 18:19:43 +08:00
|
|
|
return new Mark<O, C>(config)
|
2020-11-16 06:25:25 +08:00
|
|
|
}
|
2020-10-22 03:13:38 +08:00
|
|
|
|
2020-11-16 18:07:06 +08:00
|
|
|
configure(options: Partial<Options>) {
|
2020-11-16 18:19:43 +08:00
|
|
|
return Mark
|
2020-11-16 17:22:12 +08:00
|
|
|
.create<Options, Commands>(this.config as MarkExtensionSpec<Options, Commands>)
|
2020-11-16 18:07:06 +08:00
|
|
|
.#configure({
|
2020-11-16 17:22:12 +08:00
|
|
|
...this.config.defaultOptions,
|
|
|
|
...options,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-16 18:07:06 +08:00
|
|
|
#configure = (options: Partial<Options>) => {
|
2020-11-16 06:25:25 +08:00
|
|
|
this.options = {
|
|
|
|
...this.config.defaultOptions,
|
|
|
|
...options,
|
2020-10-22 03:13:38 +08:00
|
|
|
}
|
2020-11-16 16:43:17 +08:00
|
|
|
|
|
|
|
return this
|
2020-10-22 03:13:38 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 06:25:25 +08:00
|
|
|
extend<ExtendedOptions = Options, ExtendedCommands = Commands>(extendedConfig: Partial<MarkExtensionSpec<ExtendedOptions, ExtendedCommands>>) {
|
2020-11-16 18:19:43 +08:00
|
|
|
return new Mark<ExtendedOptions, ExtendedCommands>({
|
2020-11-16 06:25:25 +08:00
|
|
|
...this.config,
|
|
|
|
...extendedConfig,
|
|
|
|
} as MarkExtensionSpec<ExtendedOptions, ExtendedCommands>)
|
|
|
|
}
|
2020-10-22 03:13:38 +08:00
|
|
|
}
|