2020-10-10 04:59:25 +08:00
|
|
|
// import cloneDeep from 'clone-deep'
|
|
|
|
// import { Plugin } from 'prosemirror-state'
|
|
|
|
// import { Editor, CommandsSpec } from './Editor'
|
|
|
|
|
|
|
|
// type AnyObject = {
|
|
|
|
// [key: string]: any
|
|
|
|
// }
|
|
|
|
|
|
|
|
// type NoInfer<T> = [T][T extends any ? 0 : never]
|
|
|
|
|
|
|
|
// type MergeStrategy = 'extend' | 'overwrite'
|
|
|
|
|
|
|
|
// type Configs = {
|
|
|
|
// [key: string]: {
|
|
|
|
// stategy: MergeStrategy
|
|
|
|
// value: any
|
|
|
|
// }[]
|
|
|
|
// }
|
|
|
|
|
|
|
|
// export interface ExtensionProps<Options> {
|
|
|
|
// name: string
|
|
|
|
// editor: Editor
|
|
|
|
// options: Options
|
|
|
|
// }
|
|
|
|
|
|
|
|
// export interface ExtensionMethods<Props, Options> {
|
|
|
|
// name: string
|
|
|
|
// options: Options
|
|
|
|
// commands: (params: Props) => CommandsSpec
|
|
|
|
// inputRules: (params: Props) => any[]
|
|
|
|
// pasteRules: (params: Props) => any[]
|
|
|
|
// keys: (params: Props) => {
|
|
|
|
// [key: string]: Function
|
|
|
|
// }
|
|
|
|
// plugins: (params: Props) => Plugin[]
|
|
|
|
// }
|
|
|
|
|
|
|
|
// export default class Extension<
|
|
|
|
// Options = {},
|
|
|
|
// Props = ExtensionProps<Options>,
|
|
|
|
// Methods extends ExtensionMethods<Props, Options> = ExtensionMethods<Props, Options>,
|
|
|
|
// > {
|
|
|
|
// type = 'extension'
|
|
|
|
|
|
|
|
// config: AnyObject = {}
|
|
|
|
|
|
|
|
// configs: Configs = {}
|
|
|
|
|
|
|
|
// options: Partial<Options> = {}
|
|
|
|
|
|
|
|
// protected storeConfig(key: string, value: any, stategy: MergeStrategy) {
|
|
|
|
// const item = {
|
|
|
|
// stategy,
|
|
|
|
// value,
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (this.configs[key]) {
|
|
|
|
// this.configs[key].push(item)
|
|
|
|
// } else {
|
|
|
|
// this.configs[key] = [item]
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public configure(options: Partial<Options>) {
|
|
|
|
// this.options = { ...this.options, ...options }
|
|
|
|
// return this
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public name(value: Methods['name']) {
|
|
|
|
// this.storeConfig('name', value, 'overwrite')
|
|
|
|
// return this
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public defaults(value: Options) {
|
|
|
|
// this.storeConfig('defaults', value, 'overwrite')
|
|
|
|
// return this
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public commands(value: Methods['commands']) {
|
|
|
|
// this.storeConfig('commands', value, 'overwrite')
|
|
|
|
// return this
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public keys(value: Methods['keys']) {
|
|
|
|
// this.storeConfig('keys', value, 'overwrite')
|
|
|
|
// return this
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public inputRules(value: Methods['inputRules']) {
|
|
|
|
// this.storeConfig('inputRules', value, 'overwrite')
|
|
|
|
// return this
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public pasteRules(value: Methods['pasteRules']) {
|
|
|
|
// this.storeConfig('pasteRules', value, 'overwrite')
|
|
|
|
// return this
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public plugins(value: Methods['plugins']) {
|
|
|
|
// this.storeConfig('plugins', value, 'overwrite')
|
|
|
|
// return this
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public extend<T extends Extract<keyof Methods, string>>(key: T, value: Methods[T]) {
|
|
|
|
// this.storeConfig(key, value, 'extend')
|
|
|
|
// return this
|
|
|
|
// }
|
|
|
|
|
|
|
|
// public create() {
|
|
|
|
// return <NewOptions = Options>(options?: Partial<NoInfer<NewOptions>>) => {
|
|
|
|
// return cloneDeep(this, true).configure(options as NewOptions)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2020-10-22 15:14:24 +08:00
|
|
|
import { Editor } from './Editor'
|
2020-10-22 05:55:14 +08:00
|
|
|
import { GlobalAttributes } from './types'
|
|
|
|
|
2020-10-21 21:30:34 +08:00
|
|
|
export interface ExtensionSpec<Options = {}, Commands = {}> {
|
2020-10-21 21:17:05 +08:00
|
|
|
name: string,
|
|
|
|
defaultOptions?: Options,
|
2020-10-22 05:55:14 +08:00
|
|
|
createGlobalAttributes?: (
|
|
|
|
this: {
|
|
|
|
options: Options,
|
|
|
|
},
|
|
|
|
) => GlobalAttributes,
|
2020-10-22 15:14:24 +08:00
|
|
|
createCommands?: (this: {
|
2020-10-21 21:17:05 +08:00
|
|
|
options: Options,
|
2020-10-22 15:14:24 +08:00
|
|
|
editor: Editor,
|
|
|
|
}) => Commands,
|
2020-10-22 15:42:28 +08:00
|
|
|
createShortcuts?: (this: {
|
|
|
|
options: Options,
|
|
|
|
editor: Editor,
|
|
|
|
}) => {
|
|
|
|
[key: string]: any
|
|
|
|
},
|
2020-10-21 21:17:05 +08:00
|
|
|
}
|
2020-09-24 06:29:05 +08:00
|
|
|
|
2020-10-21 21:30:34 +08:00
|
|
|
export type Extension = Required<Omit<ExtensionSpec, 'defaultOptions'> & {
|
|
|
|
type: string,
|
|
|
|
options: {
|
|
|
|
[key: string]: any
|
|
|
|
},
|
|
|
|
}>
|
|
|
|
|
2020-10-22 15:14:24 +08:00
|
|
|
export const defaultExtension: Extension = {
|
2020-10-21 21:17:05 +08:00
|
|
|
type: 'extension',
|
|
|
|
name: 'extension',
|
|
|
|
options: {},
|
2020-10-22 05:55:14 +08:00
|
|
|
createGlobalAttributes: () => [],
|
2020-10-21 21:30:34 +08:00
|
|
|
createCommands: () => ({}),
|
2020-10-21 21:17:05 +08:00
|
|
|
}
|
2020-09-09 05:44:45 +08:00
|
|
|
|
2020-10-21 21:17:05 +08:00
|
|
|
export function createExtension<Options extends {}, Commands extends {}>(config: ExtensionSpec<Options, Commands>) {
|
|
|
|
const extend = <ExtendedOptions = Options, ExtendedCommands = Commands>(extendedConfig: Partial<ExtensionSpec<ExtendedOptions, ExtendedCommands>>) => {
|
|
|
|
return createExtension({
|
|
|
|
...config,
|
|
|
|
...extendedConfig,
|
|
|
|
} as ExtensionSpec<ExtendedOptions, ExtendedCommands>)
|
2020-09-09 05:44:45 +08:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:17:05 +08:00
|
|
|
const setOptions = (options?: Partial<Options>) => {
|
|
|
|
const { defaultOptions, ...rest } = config
|
2020-04-13 20:03:39 +08:00
|
|
|
|
2020-10-21 21:17:05 +08:00
|
|
|
return {
|
|
|
|
...defaultExtension,
|
|
|
|
...rest,
|
|
|
|
options: {
|
|
|
|
...defaultOptions,
|
|
|
|
...options,
|
|
|
|
} as Options,
|
|
|
|
}
|
2019-12-17 06:20:05 +08:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:17:05 +08:00
|
|
|
return Object.assign(setOptions, { config, extend })
|
2019-12-17 06:20:05 +08:00
|
|
|
}
|