replace extensions
This commit is contained in:
@@ -1,54 +1,151 @@
|
||||
import cloneDeep from 'clone-deep'
|
||||
import { Plugin } from 'prosemirror-state'
|
||||
import { Editor, Command } from './Editor'
|
||||
import { Editor, CommandSpec } from './Editor'
|
||||
|
||||
export default abstract class Extension {
|
||||
// export default abstract class Extension {
|
||||
|
||||
constructor(options = {}) {
|
||||
this.options = {
|
||||
...this.defaultOptions(),
|
||||
...options,
|
||||
// constructor(options = {}) {
|
||||
// this.options = {
|
||||
// ...this.defaultOptions(),
|
||||
// ...options,
|
||||
// }
|
||||
// }
|
||||
|
||||
// editor!: Editor
|
||||
// options: { [key: string]: any } = {}
|
||||
|
||||
// public abstract name: string
|
||||
|
||||
// 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 []
|
||||
// }
|
||||
|
||||
// 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]
|
||||
|
||||
export interface ExtensionCallback {
|
||||
name: string
|
||||
editor: Editor
|
||||
options: any
|
||||
}
|
||||
|
||||
export interface ExtensionExtends<Callback = ExtensionCallback> {
|
||||
name: string
|
||||
options: AnyObject
|
||||
commands: (params: Callback) => CommandSpec
|
||||
inputRules: (params: Callback) => any[]
|
||||
pasteRules: (params: Callback) => any[]
|
||||
keys: (params: Callback) => {
|
||||
[key: string]: Function
|
||||
}
|
||||
plugins: (params: Callback) => Plugin[]
|
||||
}
|
||||
|
||||
export default class Extension<Options, Extends extends ExtensionExtends = ExtensionExtends> {
|
||||
type = 'extension'
|
||||
configs: any = {}
|
||||
usedOptions: Partial<Options> = {}
|
||||
|
||||
protected storeConfig(key: string, value: any, stategy: 'extend' | 'overwrite') {
|
||||
const item = {
|
||||
stategy,
|
||||
value,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
public name(value: Extends['name']) {
|
||||
this.storeConfig('name', value, 'overwrite')
|
||||
return this
|
||||
}
|
||||
|
||||
public defaults(value: Options) {
|
||||
this.storeConfig('defaults', value, 'overwrite')
|
||||
return this
|
||||
}
|
||||
|
||||
public commands(value: Extends['commands']) {
|
||||
this.storeConfig('commands', value, 'overwrite')
|
||||
return this
|
||||
}
|
||||
|
||||
public keys(value: Extends['keys']) {
|
||||
this.storeConfig('keys', value, 'overwrite')
|
||||
return this
|
||||
}
|
||||
|
||||
public inputRules(value: Extends['inputRules']) {
|
||||
this.storeConfig('inputRules', value, 'overwrite')
|
||||
return this
|
||||
}
|
||||
|
||||
public pasteRules(value: Extends['pasteRules']) {
|
||||
this.storeConfig('pasteRules', value, 'overwrite')
|
||||
return this
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
editor!: Editor
|
||||
options: { [key: string]: any } = {}
|
||||
|
||||
public abstract name: string
|
||||
|
||||
public extensionType = 'extension'
|
||||
public create() {
|
||||
type ParentOptions = Options
|
||||
|
||||
public created() {}
|
||||
|
||||
public bindEditor(editor: Editor): void {
|
||||
this.editor = editor
|
||||
return <Options = ParentOptions>(options?: Partial<NoInfer<Options>>) => {
|
||||
return cloneDeep(this, true).configure(options as Options)
|
||||
}
|
||||
}
|
||||
|
||||
defaultOptions(): { [key: string]: any } {
|
||||
return {}
|
||||
}
|
||||
|
||||
update(): any {
|
||||
return () => {}
|
||||
}
|
||||
|
||||
plugins(): Plugin[] {
|
||||
return []
|
||||
}
|
||||
|
||||
inputRules(): any {
|
||||
return []
|
||||
}
|
||||
|
||||
pasteRules(): any {
|
||||
return []
|
||||
}
|
||||
|
||||
keys(): { [key: string]: Function } {
|
||||
return {}
|
||||
}
|
||||
|
||||
commands(): { [key: string]: Command } {
|
||||
return {}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user