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-19 06:20:34 +08:00
|
|
|
export interface ExtensionProps<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-19 06:20:34 +08:00
|
|
|
export interface ExtensionMethods<Props, Options> {
|
2020-09-09 05:44:45 +08:00
|
|
|
name: string
|
2020-09-09 23:10:29 +08:00
|
|
|
options: Options
|
2020-09-19 06:20:34 +08:00
|
|
|
commands: (params: Props) => CommandSpec
|
|
|
|
inputRules: (params: Props) => any[]
|
|
|
|
pasteRules: (params: Props) => any[]
|
|
|
|
keys: (params: Props) => {
|
2020-09-09 05:44:45 +08:00
|
|
|
[key: string]: Function
|
2019-12-17 06:20:05 +08:00
|
|
|
}
|
2020-09-19 06:20:34 +08:00
|
|
|
plugins: (params: Props) => Plugin[]
|
2020-09-09 05:44:45 +08:00
|
|
|
}
|
|
|
|
|
2020-09-09 23:10:29 +08:00
|
|
|
export default class Extension<
|
|
|
|
Options = {},
|
2020-09-19 06:20:34 +08:00
|
|
|
Props = ExtensionProps<Options>,
|
|
|
|
Methods extends ExtensionMethods<Props, Options> = ExtensionMethods<Props, Options>
|
2020-09-09 23:10:29 +08:00
|
|
|
> {
|
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-19 06:20:34 +08:00
|
|
|
public name(value: Methods['name']) {
|
2020-09-09 05:44:45 +08:00
|
|
|
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-19 06:20:34 +08:00
|
|
|
public commands(value: Methods['commands']) {
|
2020-09-09 05:44:45 +08:00
|
|
|
this.storeConfig('commands', value, 'overwrite')
|
|
|
|
return this
|
2019-12-17 06:20:05 +08:00
|
|
|
}
|
|
|
|
|
2020-09-19 06:20:34 +08:00
|
|
|
public keys(value: Methods['keys']) {
|
2020-09-09 05:44:45 +08:00
|
|
|
this.storeConfig('keys', value, 'overwrite')
|
|
|
|
return this
|
2019-12-17 06:20:05 +08:00
|
|
|
}
|
|
|
|
|
2020-09-19 06:20:34 +08:00
|
|
|
public inputRules(value: Methods['inputRules']) {
|
2020-09-09 05:44:45 +08:00
|
|
|
this.storeConfig('inputRules', value, 'overwrite')
|
|
|
|
return this
|
2020-04-02 20:34:07 +08:00
|
|
|
}
|
|
|
|
|
2020-09-19 06:20:34 +08:00
|
|
|
public pasteRules(value: Methods['pasteRules']) {
|
2020-09-09 05:44:45 +08:00
|
|
|
this.storeConfig('pasteRules', value, 'overwrite')
|
|
|
|
return this
|
|
|
|
}
|
2019-12-17 06:20:05 +08:00
|
|
|
|
2020-09-19 06:20:34 +08:00
|
|
|
public plugins(value: Methods['plugins']) {
|
2020-09-09 05:44:45 +08:00
|
|
|
this.storeConfig('plugins', value, 'overwrite')
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2020-09-19 06:20:34 +08:00
|
|
|
public extend<T extends Extract<keyof Methods, string>>(key: T, value: Methods[T]) {
|
2020-09-09 05:44:45 +08:00
|
|
|
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
|
|
|
}
|