tiptap/packages/core/src/Extension.ts

162 lines
3.4 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
// export default abstract class Extension {
2020-03-06 03:30:58 +08:00
2020-09-09 05:44:45 +08:00
// constructor(options = {}) {
// this.options = {
// ...this.defaultOptions(),
// ...options,
// }
// }
2020-03-06 05:40:02 +08:00
2020-09-09 05:44:45 +08:00
// editor!: Editor
// options: { [key: string]: any } = {}
2020-03-06 05:40:02 +08:00
2020-09-09 05:44:45 +08:00
// public abstract name: string
2020-03-06 05:40:02 +08:00
2020-09-09 05:44:45 +08:00
// public extensionType = 'extension'
// public created() {}
// public bindEditor(editor: Editor): void {
// this.editor = editor
// }
// defaultOptions(): { [key: string]: any } {
// return {}
// }
// update(): any {
// return () => {}
// }
// plugins(): Plugin[] {
// return []
// }
// inputRules(): any {
// return []
// }
2019-12-17 06:20:05 +08:00
2020-09-09 05:44:45 +08:00
// pasteRules(): any {
// return []
// }
// keys(): { [key: string]: Function } {
// return {}
// }
// commands(): { [key: string]: Command } {
// return {}
// }
// }
type AnyObject = {
[key: string]: any
}
type NoInfer<T> = [T][T extends any ? 0 : never]
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 16:58:10 +08:00
config: any = {}
configs: {
[key: string]: {
stategy: 'extend' | 'overwrite'
value: any
}[]
} = {}
2020-09-09 05:44:45 +08:00
usedOptions: Partial<Options> = {}
protected storeConfig(key: string, value: any, stategy: 'extend' | 'overwrite') {
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>) {
this.usedOptions = { ...this.usedOptions, ...options }
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
}