tiptap/packages/core/src/Extension.ts

114 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-09-09 05:44:45 +08:00
import cloneDeep from 'clone-deep'
2020-08-22 06:12:34 +08:00
import { Plugin } from 'prosemirror-state'
2020-09-09 05:44:45 +08:00
import { Editor, CommandSpec } from './Editor'
2019-12-17 06:20:05 +08:00
2020-09-09 05:44:45 +08:00
type AnyObject = {
[key: string]: any
}
type NoInfer<T> = [T][T extends any ? 0 : never]
2020-09-09 23:29:19 +08:00
type MergeStrategy = 'extend' | 'overwrite'
type Configs = {
[key: string]: {
stategy: MergeStrategy
value: any
}[]
}
2020-09-09 23:10:29 +08:00
export interface ExtensionCallback<Options> {
2020-09-09 05:44:45 +08:00
name: string
editor: Editor
2020-09-09 23:10:29 +08:00
options: Options
2020-09-09 05:44:45 +08:00
}
2019-12-17 06:20:05 +08:00
2020-09-09 23:10:29 +08:00
export interface ExtensionExtends<Callback, Options> {
2020-09-09 05:44:45 +08:00
name: string
2020-09-09 23:10:29 +08:00
options: Options
2020-09-09 05:44:45 +08:00
commands: (params: Callback) => CommandSpec
inputRules: (params: Callback) => any[]
pasteRules: (params: Callback) => any[]
keys: (params: Callback) => {
[key: string]: Function
2019-12-17 06:20:05 +08:00
}
2020-09-09 05:44:45 +08:00
plugins: (params: Callback) => Plugin[]
}
2020-09-09 23:10:29 +08:00
export default class Extension<
Options = {},
Callback = ExtensionCallback<Options>,
Extends extends ExtensionExtends<Callback, Options> = ExtensionExtends<Callback, Options>
> {
2020-09-09 05:44:45 +08:00
type = 'extension'
2020-09-09 23:29:19 +08:00
config: AnyObject = {}
configs: Configs = {}
options: Partial<Options> = {}
2020-09-09 05:44:45 +08:00
2020-09-09 23:29:19 +08:00
protected storeConfig(key: string, value: any, stategy: MergeStrategy) {
2020-09-09 05:44:45 +08:00
const item = {
stategy,
value,
}
2019-12-17 06:20:05 +08:00
2020-09-09 05:44:45 +08:00
if (this.configs[key]) {
this.configs[key].push(item)
} else {
this.configs[key] = [item]
}
}
public configure(options: Partial<Options>) {
2020-09-09 23:29:19 +08:00
this.options = { ...this.options, ...options }
2020-09-09 05:44:45 +08:00
return this
2020-04-13 20:03:39 +08:00
}
2020-09-09 05:44:45 +08:00
public name(value: Extends['name']) {
this.storeConfig('name', value, 'overwrite')
return this
2019-12-17 06:20:05 +08:00
}
2020-09-09 05:44:45 +08:00
public defaults(value: Options) {
this.storeConfig('defaults', value, 'overwrite')
return this
2019-12-17 06:20:05 +08:00
}
2020-09-09 05:44:45 +08:00
public commands(value: Extends['commands']) {
this.storeConfig('commands', value, 'overwrite')
return this
2019-12-17 06:20:05 +08:00
}
2020-09-09 05:44:45 +08:00
public keys(value: Extends['keys']) {
this.storeConfig('keys', value, 'overwrite')
return this
2019-12-17 06:20:05 +08:00
}
2020-09-09 05:44:45 +08:00
public inputRules(value: Extends['inputRules']) {
this.storeConfig('inputRules', value, 'overwrite')
return this
2020-04-02 20:34:07 +08:00
}
2020-09-09 05:44:45 +08:00
public pasteRules(value: Extends['pasteRules']) {
this.storeConfig('pasteRules', value, 'overwrite')
return this
}
2019-12-17 06:20:05 +08:00
2020-09-09 05:44:45 +08:00
public plugins(value: Extends['plugins']) {
this.storeConfig('plugins', value, 'overwrite')
return this
}
public extend<T extends Extract<keyof Extends, string>>(key: T, value: Extends[T]) {
this.storeConfig(key, value, 'extend')
return this
}
public create() {
type ParentOptions = Options
return <Options = ParentOptions>(options?: Partial<NoInfer<Options>>) => {
return cloneDeep(this, true).configure(options as Options)
}
}
2019-12-17 06:20:05 +08:00
}