tiptap/packages/core/src/Extension.ts

248 lines
5.4 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-02-19 16:54:39 +08:00
import { Node } from './Node'
2021-01-20 16:18:49 +08:00
import mergeDeep from './utilities/mergeDeep'
import { GlobalAttributes, RawCommands, ParentConfig } from './types'
2021-03-31 19:44:56 +08:00
import { ExtensionConfig } from '.'
2021-02-20 00:35:50 +08:00
declare module '@tiptap/core' {
interface ExtensionConfig<Options = any> {
[key: string]: any;
2021-02-20 00:35:50 +08:00
/**
* Name
*/
name: string,
/**
* Priority
*/
priority?: number,
2021-02-20 00:35:50 +08:00
/**
* Default options
*/
defaultOptions?: Options,
/**
* Global attributes
*/
addGlobalAttributes?: (this: {
2021-02-19 16:54:39 +08:00
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['addGlobalAttributes'],
2021-02-20 00:35:50 +08:00
}) => GlobalAttributes | {},
/**
* Raw
*/
addCommands?: (this: {
2021-02-19 16:54:39 +08:00
options: Options,
2021-02-20 00:35:50 +08:00
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addCommands'],
2021-02-20 00:35:50 +08:00
}) => Partial<RawCommands>,
/**
* Keyboard shortcuts
*/
addKeyboardShortcuts?: (this: {
2020-11-30 21:12:36 +08:00
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addKeyboardShortcuts'],
2021-02-20 00:35:50 +08:00
}) => {
[key: string]: ProseMirrorCommand,
2020-11-30 21:12:36 +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,
parent: ParentConfig<ExtensionConfig<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,
parent: ParentConfig<ExtensionConfig<Options>>['addPasteRules'],
2021-02-20 00:35:50 +08:00
}) => Plugin[],
/**
* ProseMirror plugins
*/
addProseMirrorPlugins?: (this: {
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['addProseMirrorPlugins'],
2021-02-20 00:35:50 +08:00
}) => Plugin[],
/**
* Extend Node Schema
*/
extendNodeSchema?: ((
this: {
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['extendNodeSchema'],
2021-02-20 00:35:50 +08:00
},
extension: Node,
) => {
[key: string]: any,
}) | null,
/**
* Extend Mark Schema
*/
extendMarkSchema?: ((
this: {
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['extendMarkSchema'],
2021-02-20 00:35:50 +08:00
},
extension: Node,
) => {
[key: string]: any,
}) | null,
2021-04-02 06:07:40 +08:00
/**
* The editor is not ready yet.
*/
onBeforeCreate?: ((this: {
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<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,
parent: ParentConfig<ExtensionConfig<Options>>['onCreate'],
2021-02-20 00:35:50 +08:00
}) => void) | null,
/**
* The content has changed.
*/
onUpdate?: ((this: {
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onUpdate'],
2021-02-20 00:35:50 +08:00
}) => void) | null,
/**
* The selection has changed.
*/
onSelectionUpdate?: ((this: {
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<Options>>['onSelectionUpdate'],
}) => void) | null,
2021-02-20 00:35:50 +08:00
/**
* The editor state has changed.
*/
onTransaction?: ((
this: {
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<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,
parent: ParentConfig<ExtensionConfig<Options>>['onFocus'],
2021-02-20 00:35:50 +08:00
},
props: {
event: FocusEvent,
},
) => void) | null,
/**
* The editor isnt focused anymore.
*/
onBlur?: ((
this: {
options: Options,
editor: Editor,
parent: ParentConfig<ExtensionConfig<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,
parent: ParentConfig<ExtensionConfig<Options>>['onDestroy'],
2021-02-20 00:35:50 +08:00
}) => 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'
2021-02-19 17:54:47 +08:00
config: ExtensionConfig = {
2020-11-16 06:25:25 +08:00
name: 'extension',
priority: 100,
2020-11-16 06:25:25 +08:00
defaultOptions: {},
}
2020-09-09 05:44:45 +08:00
options: Options
parent: Extension | null = null
2020-11-16 06:25:25 +08:00
child: Extension | null = null
constructor(config: Partial<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
}
static create<O>(config: Partial<ExtensionConfig<O>> = {}) {
2021-02-10 16:59:35 +08:00
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> = {}) {
this.options = mergeDeep(this.options, options) as Options
2020-11-16 16:43:17 +08:00
return this
2019-12-17 06:20:05 +08:00
}
2021-04-14 15:48:38 +08:00
extend<ExtendedOptions = Options>(extendedConfig: Partial<ExtensionConfig<ExtendedOptions>> = {}) {
const extension = new Extension<ExtendedOptions>(extendedConfig)
2021-04-14 15:48:38 +08:00
extension.parent = this
this.child = extension
2021-04-14 15:48:38 +08:00
extension.options = {
...extension.parent.options,
...extension.options,
2021-04-14 15:48:38 +08:00
}
return extension
2020-11-16 06:25:25 +08:00
}
2019-12-17 06:20:05 +08:00
}