wip: add some hierarchy tests

This commit is contained in:
Philipp Kühn
2021-04-14 09:48:38 +02:00
parent daa5dc0fb1
commit c40ce34eec
9 changed files with 364 additions and 28 deletions

View File

@@ -205,7 +205,8 @@ export class Extension<Options = any> {
defaultOptions: {},
}
parentConfig: Partial<ExtensionConfig> = {}
// parentConfig: Partial<ExtensionConfig> = {}
parent: any
options!: Options
@@ -234,13 +235,18 @@ export class Extension<Options = any> {
return this
}
extend<ExtendedOptions = Options>(extendedConfig: Partial<ExtensionConfig<ExtendedOptions>>) {
extend<ExtendedOptions = Options>(extendedConfig: Partial<ExtensionConfig<ExtendedOptions>> = {}) {
const extension = new Extension<ExtendedOptions>({
...this.config,
// ...this.config,
...extendedConfig,
} as ExtensionConfig<ExtendedOptions>)
extension.parentConfig = this.config
// extension.parentConfig = this.config
extension.parent = this
extension.options = {
...(this.config.defaultOptions ?? {}),
...(extendedConfig.defaultOptions ?? {}),
}
return extension
}

View File

@@ -146,12 +146,40 @@ export default class ExtensionManager {
plugins.push(...pasteRulePlugins)
}
if (extension.config.addProseMirrorPlugins) {
const proseMirrorPlugins = extension.config.addProseMirrorPlugins.bind(context)()
// console.log('has pm', extension.config.addProseMirrorPlugins, extension)
plugins.push(...proseMirrorPlugins)
const getItem = (rootext: any, ext: any, field: string): any => {
const realctx = createExtensionContext(ext, {
options: rootext.options,
// options: getItem(ext, 'defaultOptions'),
editor: this.editor,
type: getSchemaTypeByName(ext.config.name, this.schema),
})
if (ext.config[field]) {
if (typeof ext.config[field] === 'function') {
return ext.config[field].bind(realctx)()
}
return ext.config[field]
}
if (ext.parent) {
return getItem(rootext, ext.parent, field)
}
return undefined
}
// console.log('get PM', getItem(extension, 'addProseMirrorPlugins', context))
const realPMP = getItem(extension, extension, 'addProseMirrorPlugins')
// if (extension.config.addProseMirrorPlugins) {
// const proseMirrorPlugins = extension.config.addProseMirrorPlugins.bind(context)()
// plugins.push(...proseMirrorPlugins)
// }
return plugins
})
.flat()

View File

@@ -299,7 +299,8 @@ export class Mark<Options = any> {
defaultOptions: {},
}
parentConfig: Partial<MarkConfig> = {}
// parentConfig: Partial<MarkConfig> = {}
parent: any
options!: Options
@@ -334,7 +335,9 @@ export class Mark<Options = any> {
...extendedConfig,
} as MarkConfig<ExtendedOptions>)
extension.parentConfig = this.config
extension.parent = this
// extension.parentConfig = this.config
return extension
}

View File

@@ -373,7 +373,8 @@ export class Node<Options = any> {
defaultOptions: {},
}
parentConfig: Partial<NodeConfig> = {}
// parentConfig: Partial<NodeConfig> = {}
parent: any
options!: Options
@@ -408,7 +409,8 @@ export class Node<Options = any> {
...extendedConfig,
} as NodeConfig<ExtendedOptions>)
extension.parentConfig = this.config
extension.parent = this
// extension.parentConfig = this.config
return extension
}

View File

@@ -1,13 +1,93 @@
import { AnyExtension, AnyObject } from '../types'
// export default function createExtensionContext<T>(
// extension: AnyExtension,
// data: T,
// ): T & { parentConfig: AnyObject } {
// const context: any = {
// ...data,
// // get parentConfig() {
// // return Object.fromEntries(Object.entries(extension.parentConfig).map(([key, value]) => {
// // if (typeof value !== 'function') {
// // return [key, value]
// // }
// // console.log('call', key)
// // return [key, value.bind(context)]
// // }))
// // },
// parentConfig: Object.fromEntries(Object.entries(extension.parent.config).map(([key, value]) => {
// if (typeof value !== 'function') {
// return [key, value]
// }
// // console.log('call', key)
// return [key, value.bind(data)]
// })),
// // get parentConfig() {
// // console.log('parent', extension.parent)
// // console.log('parent parent', extension.parent?.parent)
// // return Object.fromEntries(Object.entries(extension.parent.config).map(([key, value]) => {
// // if (typeof value !== 'function') {
// // return [key, value]
// // }
// // // console.log('call', key)
// // return [key, value.bind(context)]
// // }))
// // },
// // parentConfig: null,
// }
// return context
// }
// export default function createExtensionContext<T>(
// extension: AnyExtension,
// data: T,
// // @ts-ignore
// ): T & { parentConfig: AnyObject } {
// const context: any = data
// if (!extension.parent) {
// context.parentConfig = {}
// return context
// }
// // const bla = {
// // ...(extension.parent.parent ? extension.parent.parent.config : {}),
// // ...extension.parent.config,
// // }
// context.parentConfig = Object.fromEntries(Object.entries(extension.parent.config).map(([key, value]) => {
// if (typeof value !== 'function') {
// return [key, value]
// }
// // console.log('call', key)
// return [key, value.bind(createExtensionContext(extension.parent, data))]
// }))
// return context
// }
export default function createExtensionContext<T>(
extension: AnyExtension,
data: T,
): T & { parentConfig: AnyObject } {
const context = {
const context: any = {
...data,
get parentConfig() {
return Object.fromEntries(Object.entries(extension.parentConfig).map(([key, value]) => {
return Object.fromEntries(Object.entries(extension.parent.config).map(([key, value]) => {
if (typeof value !== 'function') {
return [key, value]
}

View File

@@ -14,7 +14,7 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
addProseMirrorPlugins() {
return [
...this.parentConfig.addProseMirrorPlugins?.() || [],
// ...this.parentConfig.addProseMirrorPlugins?.() || [],
LowlightPlugin({
name: 'codeBlock',
lowlight: this.options.lowlight,