From 05434afc4795116d58c451ba4a956722d91fde1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Thu, 15 Apr 2021 22:03:45 +0200 Subject: [PATCH] wip: add extension.name --- .../Examples/CollaborativeEditing/index.vue | 2 +- docs/src/docPages/guide/configuration.md | 2 +- packages/core/src/Extension.ts | 21 +++++++++++------ packages/core/src/ExtensionManager.ts | 16 ++++++------- packages/core/src/Mark.ts | 23 ++++++++++++------- packages/core/src/Node.ts | 21 +++++++++++------ packages/core/src/helpers/getSchema.ts | 13 ++++------- packages/core/src/helpers/isList.ts | 2 +- packages/react/src/ReactNodeViewRenderer.tsx | 2 +- 9 files changed, 60 insertions(+), 42 deletions(-) diff --git a/docs/src/demos/Examples/CollaborativeEditing/index.vue b/docs/src/demos/Examples/CollaborativeEditing/index.vue index ef1fd0f8..25d1e0e3 100644 --- a/docs/src/demos/Examples/CollaborativeEditing/index.vue +++ b/docs/src/demos/Examples/CollaborativeEditing/index.vue @@ -70,7 +70,7 @@ export default { this.editor = new Editor({ extensions: [ - ...defaultExtensions().filter(extension => extension.config.name !== 'history'), + ...defaultExtensions().filter(extension => extension.name !== 'history'), Highlight, TaskList, TaskItem, diff --git a/docs/src/docPages/guide/configuration.md b/docs/src/docPages/guide/configuration.md index de6e638a..c9660ed8 100644 --- a/docs/src/docPages/guide/configuration.md +++ b/docs/src/docPages/guide/configuration.md @@ -127,7 +127,7 @@ import { Editor, defaultExtensions } from '@tiptap/starter-kit' new Editor({ extensions: [ - ...defaultExtensions().filter(extension => extension.config.name !== 'history'), + ...defaultExtensions().filter(extension => extension.name !== 'history'), ], }) ``` diff --git a/packages/core/src/Extension.ts b/packages/core/src/Extension.ts index 73c56a6d..7d847801 100644 --- a/packages/core/src/Extension.ts +++ b/packages/core/src/Extension.ts @@ -200,24 +200,27 @@ declare module '@tiptap/core' { export class Extension { type = 'extension' - config: ExtensionConfig = { - name: 'extension', - priority: 100, - defaultOptions: {}, - } - - options: Options + name = 'extension' parent: Extension | null = null child: Extension | null = null + options: Options + + config: ExtensionConfig = { + name: this.name, + priority: 100, + defaultOptions: {}, + } + constructor(config: Partial> = {}) { this.config = { ...this.config, ...config, } + this.name = this.config.name this.options = this.config.defaultOptions } @@ -238,6 +241,10 @@ export class Extension { this.child = extension + extension.name = extendedConfig.name + ? extendedConfig.name + : this.name + extension.options = { ...extension.parent.options, ...extension.options, diff --git a/packages/core/src/ExtensionManager.ts b/packages/core/src/ExtensionManager.ts index 0b8cef7d..5c6d2706 100644 --- a/packages/core/src/ExtensionManager.ts +++ b/packages/core/src/ExtensionManager.ts @@ -34,14 +34,14 @@ export default class ExtensionManager { const context = { options: extension.options, editor: this.editor, - type: getSchemaTypeByName(extension.config.name, this.schema), + type: getSchemaTypeByName(extension.name, this.schema), } if (extension.type === 'mark') { const keepOnSplit = callOrReturn(getExtensionField(extension, 'keepOnSplit', context)) ?? true if (keepOnSplit) { - this.splittableMarks.push(extension.config.name) + this.splittableMarks.push(extension.name) } } @@ -100,7 +100,7 @@ export default class ExtensionManager { const context = { options: extension.options, editor: this.editor, - type: getSchemaTypeByName(extension.config.name, this.schema), + type: getSchemaTypeByName(extension.name, this.schema), } if (!extension.config.addCommands) { @@ -121,7 +121,7 @@ export default class ExtensionManager { const context = { options: extension.options, editor: this.editor, - type: getSchemaTypeByName(extension.config.name, this.schema), + type: getSchemaTypeByName(extension.name, this.schema), } const plugins: Plugin[] = [] @@ -198,7 +198,7 @@ export default class ExtensionManager { const context = { options: extension.options, editor, - type: getNodeType(extension.config.name, this.schema), + type: getNodeType(extension.name, this.schema), } const addNodeView = getExtensionField( extension, @@ -228,7 +228,7 @@ export default class ExtensionManager { }) } - return [extension.config.name, nodeview] + return [extension.name, nodeview] })) } @@ -242,7 +242,7 @@ export default class ExtensionManager { const context = { options: extension.options, editor, - type: getNodeType(extension.config.name, this.schema), + type: getNodeType(extension.name, this.schema), } const renderText = getExtensionField(extension, 'renderText', context) @@ -253,7 +253,7 @@ export default class ExtensionManager { const textSerializer = (props: { node: ProsemirrorNode }) => renderText(props) - return [extension.config.name, textSerializer] + return [extension.name, textSerializer] })) } diff --git a/packages/core/src/Mark.ts b/packages/core/src/Mark.ts index 2c5fe04f..9be3a5a3 100644 --- a/packages/core/src/Mark.ts +++ b/packages/core/src/Mark.ts @@ -292,26 +292,29 @@ declare module '@tiptap/core' { } export class Mark { - type = 'node' + type = 'mark' - config: MarkConfig = { - name: 'node', - priority: 100, - defaultOptions: {}, - } - - options: Options + name = 'mark' parent: Mark | null = null child: Mark | null = null + options: Options + + config: MarkConfig = { + name: this.name, + priority: 100, + defaultOptions: {}, + } + constructor(config: Partial> = {}) { this.config = { ...this.config, ...config, } + this.name = this.config.name this.options = this.config.defaultOptions } @@ -332,6 +335,10 @@ export class Mark { this.child = extension + extension.name = extendedConfig.name + ? extendedConfig.name + : this.name + extension.options = { ...extension.parent.options, ...extension.options, diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index de72159e..960d4afb 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -367,24 +367,27 @@ declare module '@tiptap/core' { export class Node { type = 'node' - config: NodeConfig = { - name: 'node', - priority: 100, - defaultOptions: {}, - } - - options: Options + name = 'node' parent: Node | null = null child: Node | null = null + options: Options + + config: NodeConfig = { + name: this.name, + priority: 100, + defaultOptions: {}, + } + constructor(config: Partial> = {}) { this.config = { ...this.config, ...config, } + this.name = this.config.name this.options = this.config.defaultOptions } @@ -405,6 +408,10 @@ export class Node { this.child = extension + extension.name = extendedConfig.name + ? extendedConfig.name + : this.name + extension.options = { ...extension.parent.options, ...extension.options, diff --git a/packages/core/src/helpers/getSchema.ts b/packages/core/src/helpers/getSchema.ts index 92fabbf5..e4c40322 100644 --- a/packages/core/src/helpers/getSchema.ts +++ b/packages/core/src/helpers/getSchema.ts @@ -22,13 +22,10 @@ function cleanUpSchemaItem(data: T) { export default function getSchema(extensions: Extensions): Schema { const allAttributes = getAttributesFromExtensions(extensions) const { nodeExtensions, markExtensions } = splitExtensions(extensions) - const topNodeExtension = nodeExtensions.find(extension => getExtensionField(extension, 'topNode')) - const topNode = topNodeExtension - ? getExtensionField(topNodeExtension, 'name') - : null + const topNode = nodeExtensions.find(extension => getExtensionField(extension, 'topNode'))?.name const nodes = Object.fromEntries(nodeExtensions.map(extension => { - const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.config.name) + const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name) const context = { options: extension.options, } @@ -79,11 +76,11 @@ export default function getSchema(extensions: Extensions): Schema { }) } - return [extension.config.name, schema] + return [extension.name, schema] })) const marks = Object.fromEntries(markExtensions.map(extension => { - const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.config.name) + const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name) const context = { options: extension.options, } @@ -128,7 +125,7 @@ export default function getSchema(extensions: Extensions): Schema { }) } - return [extension.config.name, schema] + return [extension.name, schema] })) return new Schema({ diff --git a/packages/core/src/helpers/isList.ts b/packages/core/src/helpers/isList.ts index 6c1cd644..2e1a5c0b 100644 --- a/packages/core/src/helpers/isList.ts +++ b/packages/core/src/helpers/isList.ts @@ -4,7 +4,7 @@ import callOrReturn from '../utilities/callOrReturn' export default function isList(name: string, extensions: Extensions): boolean { const { nodeExtensions } = splitExtensions(extensions) - const extension = nodeExtensions.find(item => item.config.name === name) + const extension = nodeExtensions.find(item => item.name === name) if (!extension) { return false diff --git a/packages/react/src/ReactNodeViewRenderer.tsx b/packages/react/src/ReactNodeViewRenderer.tsx index e4022211..27deb0dd 100644 --- a/packages/react/src/ReactNodeViewRenderer.tsx +++ b/packages/react/src/ReactNodeViewRenderer.tsx @@ -38,7 +38,7 @@ class ReactNodeView extends NodeView { return string.charAt(0).toUpperCase() + string.substring(1) } - this.component.displayName = capitalizeFirstChar(this.extension.config.name) + this.component.displayName = capitalizeFirstChar(this.extension.name) } const ReactNodeViewProvider: React.FunctionComponent = componentProps => {