tiptap/packages/core/src/Extension.ts

195 lines
3.6 KiB
TypeScript
Raw Normal View History

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'
2020-10-22 15:14:24 +08:00
import { Editor } from './Editor'
2021-01-20 16:18:49 +08:00
import mergeDeep from './utilities/mergeDeep'
2021-02-17 01:36:37 +08:00
import { GlobalAttributes, RawCommands } from './types'
2020-10-22 05:55:14 +08:00
2021-02-10 16:59:35 +08:00
export interface ExtensionConfig<Options = any> {
2020-10-23 16:59:26 +08:00
/**
* Name
*/
2020-12-02 16:44:46 +08:00
name: string,
2020-10-23 16:59:26 +08:00
2020-10-22 18:34:49 +08:00
/**
* Default options
*/
2020-10-21 21:17:05 +08:00
defaultOptions?: Options,
2020-10-22 18:34:49 +08:00
/**
* Global attributes
*/
2020-10-23 05:21:52 +08:00
addGlobalAttributes?: (this: {
options: Options,
2020-12-04 06:32:11 +08:00
}) => GlobalAttributes | {},
2020-10-22 18:34:49 +08:00
/**
2021-02-17 01:36:37 +08:00
* Raw
2020-10-22 18:34:49 +08:00
*/
2020-10-22 17:14:44 +08:00
addCommands?: (this: {
2020-10-21 21:17:05 +08:00
options: Options,
2020-10-22 15:14:24 +08:00
editor: Editor,
2021-02-17 01:36:37 +08:00
}) => Partial<RawCommands>,
2020-10-22 18:34:49 +08:00
/**
* Keyboard shortcuts
*/
2020-10-22 17:14:44 +08:00
addKeyboardShortcuts?: (this: {
2020-10-22 15:42:28 +08:00
options: Options,
editor: Editor,
}) => {
2021-02-17 05:59:45 +08:00
[key: string]: ProseMirrorCommand,
2020-10-22 15:42:28 +08:00
},
2020-10-22 18:34:49 +08:00
/**
* Input rules
*/
addInputRules?: (this: {
options: Options,
editor: Editor,
2020-11-17 04:54:40 +08:00
}) => InputRule[],
2020-10-22 18:34:49 +08:00
/**
* Paste rules
*/
addPasteRules?: (this: {
options: Options,
editor: Editor,
2020-11-17 04:54:40 +08:00
}) => Plugin[],
2020-10-22 18:34:49 +08:00
/**
* ProseMirror plugins
*/
addProseMirrorPlugins?: (this: {
options: Options,
editor: Editor,
}) => 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,
}) => void) | null,
/**
* The content has changed.
*/
onUpdate?: ((this: {
options: Options,
editor: Editor,
}) => void) | null,
/**
* The selection has changed.
*/
onSelection?: ((this: {
options: Options,
editor: Editor,
}) => void) | null,
/**
* The editor state has changed.
*/
onTransaction?: ((
this: {
options: Options,
editor: Editor,
},
props: {
transaction: Transaction,
},
) => void) | null,
/**
* The editor is focused.
*/
onFocus?: ((
this: {
options: Options,
editor: Editor,
},
props: {
event: FocusEvent,
},
) => void) | null,
/**
* The editor isnt focused anymore.
*/
onBlur?: ((
this: {
options: Options,
editor: Editor,
},
props: {
event: FocusEvent,
},
) => void) | null,
/**
* The editor is destroyed.
*/
2020-11-30 20:50:06 +08:00
onDestroy?: ((this: {
options: Options,
editor: Editor,
}) => void) | null,
2020-10-21 21:17:05 +08:00
}
2020-09-24 06:29:05 +08:00
2021-02-10 16:59:35 +08:00
export class Extension<Options = any> {
2020-11-20 04:08:25 +08:00
type = 'extension'
2020-11-16 18:21:54 +08:00
config: Required<ExtensionConfig> = {
2020-11-16 06:25:25 +08:00
name: 'extension',
defaultOptions: {},
addGlobalAttributes: () => [],
addCommands: () => ({}),
addKeyboardShortcuts: () => ({}),
addInputRules: () => [],
addPasteRules: () => [],
addProseMirrorPlugins: () => [],
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-09-09 05:44:45 +08:00
2020-11-16 06:25:25 +08:00
options!: Options
2021-02-10 16:59:35 +08:00
constructor(config: ExtensionConfig<Options>) {
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
}
this.options = this.config.defaultOptions
2020-09-09 05:44:45 +08:00
}
2021-02-10 16:59:35 +08:00
static create<O>(config: ExtensionConfig<O>) {
return new Extension<O>(config)
2020-11-16 06:25:25 +08:00
}
2020-04-13 20:03:39 +08:00
2021-01-20 05:29:46 +08:00
configure(options: Partial<Options> = {}) {
return Extension
2021-02-10 16:59:35 +08:00
.create<Options>(this.config as ExtensionConfig<Options>)
2021-01-20 05:29:46 +08:00
.#configure(options)
}
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
2019-12-17 06:20:05 +08:00
}
2021-02-10 16:59:35 +08:00
extend<ExtendedOptions = Options>(extendedConfig: Partial<ExtensionConfig<ExtendedOptions>>) {
return new Extension<ExtendedOptions>({
2020-11-16 06:25:25 +08:00
...this.config,
...extendedConfig,
2021-02-10 16:59:35 +08:00
} as ExtensionConfig<ExtendedOptions>)
2020-11-16 06:25:25 +08:00
}
2019-12-17 06:20:05 +08:00
}