From ba69a0d8f905c26fa69c0d6b2988416c9df4a591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 19 Feb 2021 10:54:47 +0100 Subject: [PATCH] improve extending nodes and marks --- packages/core/src/Extension.ts | 17 +------ packages/core/src/ExtensionManager.ts | 48 +++++++++++++------ packages/core/src/Mark.ts | 24 +--------- packages/core/src/Node.ts | 44 +++-------------- .../helpers/getAttributesFromExtensions.ts | 8 ++++ packages/core/src/index.ts | 3 -- packages/core/src/types.ts | 4 +- packages/extension-table/src/table.ts | 18 +++---- 8 files changed, 60 insertions(+), 106 deletions(-) diff --git a/packages/core/src/Extension.ts b/packages/core/src/Extension.ts index 7ad45967..65cd4885 100644 --- a/packages/core/src/Extension.ts +++ b/packages/core/src/Extension.ts @@ -165,24 +165,9 @@ export interface ExtensionConfig { export class Extension { type = 'extension' - config: Required = { + config: ExtensionConfig = { name: 'extension', defaultOptions: {}, - addGlobalAttributes: () => [], - addCommands: () => ({}), - addKeyboardShortcuts: () => ({}), - addInputRules: () => [], - addPasteRules: () => [], - addProseMirrorPlugins: () => [], - extendNodeSchema: null, - extendMarkSchema: null, - onCreate: null, - onUpdate: null, - onSelection: null, - onTransaction: null, - onFocus: null, - onBlur: null, - onDestroy: null, } options!: Options diff --git a/packages/core/src/ExtensionManager.ts b/packages/core/src/ExtensionManager.ts index 0e4d301f..3309053a 100644 --- a/packages/core/src/ExtensionManager.ts +++ b/packages/core/src/ExtensionManager.ts @@ -70,6 +70,10 @@ export default class ExtensionManager { type: getSchemaTypeByName(extension.config.name, this.schema), } + if (!extension.config.addCommands) { + return commands + } + return { ...commands, ...extension.config.addCommands.bind(context)(), @@ -87,22 +91,36 @@ export default class ExtensionManager { type: getSchemaTypeByName(extension.config.name, this.schema), } - const keymapPlugin = keymap(extension.config.addKeyboardShortcuts.bind(context)()) - const inputRules = extension.config.addInputRules.bind(context)() - const inputRulePlugins = this.editor.options.enableInputRules && inputRules.length - ? [inputRulesPlugin({ rules: inputRules })] - : [] - const pasteRulePlugins = this.editor.options.enablePasteRules - ? extension.config.addPasteRules.bind(context)() - : [] - const plugins = extension.config.addProseMirrorPlugins.bind(context)() + const plugins: Plugin[] = [] - return [ - keymapPlugin, - ...inputRulePlugins, - ...pasteRulePlugins, - ...plugins, - ] + if (extension.config.addKeyboardShortcuts) { + const keyMapPlugin = keymap(extension.config.addKeyboardShortcuts.bind(context)()) + + plugins.push(keyMapPlugin) + } + + if (this.editor.options.enableInputRules && extension.config.addInputRules) { + const inputRules = extension.config.addInputRules.bind(context)() + const inputRulePlugins = inputRules.length + ? [inputRulesPlugin({ rules: inputRules })] + : [] + + plugins.push(...inputRulePlugins) + } + + if (this.editor.options.enablePasteRules && extension.config.addPasteRules) { + const pasteRulePlugins = extension.config.addPasteRules.bind(context)() + + plugins.push(...pasteRulePlugins) + } + + if (extension.config.addProseMirrorPlugins) { + const proseMirrorPlugins = extension.config.addProseMirrorPlugins.bind(context)() + + proseMirrorPlugins.push(...proseMirrorPlugins) + } + + return plugins }) .flat() } diff --git a/packages/core/src/Mark.ts b/packages/core/src/Mark.ts index 83a4ac0f..138ea5b1 100644 --- a/packages/core/src/Mark.ts +++ b/packages/core/src/Mark.ts @@ -193,31 +193,9 @@ export interface MarkConfig extends Overwrite { type = 'mark' - config: Required = { + config: MarkConfig = { name: 'mark', defaultOptions: {}, - addGlobalAttributes: () => [], - addCommands: () => ({}), - addKeyboardShortcuts: () => ({}), - addInputRules: () => [], - addPasteRules: () => [], - addProseMirrorPlugins: () => [], - inclusive: null, - excludes: null, - group: null, - spanning: null, - parseHTML: () => null, - renderHTML: null, - addAttributes: () => ({}), - extendNodeSchema: null, - extendMarkSchema: null, - onCreate: null, - onUpdate: null, - onSelection: null, - onTransaction: null, - onFocus: null, - onBlur: null, - onDestroy: null, } options!: Options diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index edf0e94f..464fb8ff 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -14,7 +14,6 @@ import { NodeViewRenderer, Overwrite, RawCommands, - NodeSchemaFields, } from './types' import { Editor } from './Editor' @@ -257,45 +256,14 @@ export interface NodeConfig extends Overwrite { type = 'node' - config: Required & NodeSchemaFields = { + config: NodeConfig = { name: 'node', defaultOptions: {}, - addGlobalAttributes: () => [], - addCommands: () => ({}), - addKeyboardShortcuts: () => ({}), - addInputRules: () => [], - addPasteRules: () => [], - addProseMirrorPlugins: () => [], - topNode: false, - content: null, - marks: null, - group: null, - inline: null, - atom: null, - selectable: null, - draggable: null, - code: null, - defining: null, - isolating: null, - parseHTML: () => null, - renderHTML: null, - renderText: null, - addAttributes: () => ({}), - addNodeView: null, - extendNodeSchema: null, - extendMarkSchema: null, - onCreate: null, - onUpdate: null, - onSelection: null, - onTransaction: null, - onFocus: null, - onBlur: null, - onDestroy: null, } options!: Options - constructor(config: NodeConfig & NodeSchemaFields) { + constructor(config: NodeConfig) { this.config = { ...this.config, ...config, @@ -304,13 +272,13 @@ export class Node { this.options = this.config.defaultOptions } - static create(config: NodeConfig & NodeSchemaFields) { + static create(config: NodeConfig) { return new Node(config) } configure(options: Partial = {}) { return Node - .create(this.config as (NodeConfig & NodeSchemaFields)) + .create(this.config as NodeConfig) .#configure(options) } @@ -320,10 +288,10 @@ export class Node { return this } - extend(extendedConfig: Partial & NodeSchemaFields>) { + extend(extendedConfig: Partial>) { return new Node({ ...this.config, ...extendedConfig, - } as NodeConfig & NodeSchemaFields) + } as NodeConfig) } } diff --git a/packages/core/src/helpers/getAttributesFromExtensions.ts b/packages/core/src/helpers/getAttributesFromExtensions.ts index b7a64b18..4bbf2056 100644 --- a/packages/core/src/helpers/getAttributesFromExtensions.ts +++ b/packages/core/src/helpers/getAttributesFromExtensions.ts @@ -28,6 +28,10 @@ export default function getAttributesFromExtensions(extensions: Extensions): Ext options: extension.options, } + if (!extension.config.addGlobalAttributes) { + return + } + const globalAttributes = extension.config.addGlobalAttributes.bind(context)() as GlobalAttributes globalAttributes.forEach(globalAttribute => { @@ -53,6 +57,10 @@ export default function getAttributesFromExtensions(extensions: Extensions): Ext options: extension.options, } + if (!extension.config.addAttributes) { + return + } + const attributes = extension.config.addAttributes.bind(context)() as Attributes Object diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 7f9ae906..ba953275 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -23,6 +23,3 @@ export { default as isCellSelection } from './helpers/isCellSelection' export { default as findParentNodeClosestToPos } from './helpers/findParentNodeClosestToPos' export interface Commands {} - -// eslint-disable-next-line -export interface NodeSchemaFields {} diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index b156f487..54bd8b0d 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -14,9 +14,9 @@ import { Extension } from './Extension' import { Node } from './Node' import { Mark } from './Mark' import { Editor } from './Editor' -import { Commands, NodeSchemaFields } from '.' +import { Commands } from '.' -export { Commands, NodeSchemaFields } +export { Commands } export type Extensions = (Extension | Node | Mark)[] diff --git a/packages/extension-table/src/table.ts b/packages/extension-table/src/table.ts index 07245972..6c32c40a 100644 --- a/packages/extension-table/src/table.ts +++ b/packages/extension-table/src/table.ts @@ -66,7 +66,7 @@ declare module '@tiptap/core' { } } - interface NodeSchemaFields { + interface NodeConfig { /** * Table Role */ @@ -89,14 +89,6 @@ export const Table = Node.create({ allowTableNodeSelection: false, }, - extendNodeSchema(extension) { - const context = { options: extension.options } - - return { - tableRole: callOrReturn(extension.config.tableRole, context), - } - }, - content: 'tableRow+', tableRole: 'table', @@ -265,4 +257,12 @@ export const Table = Node.create({ }), ] }, + + extendNodeSchema(extension) { + const context = { options: extension.options } + + return { + tableRole: callOrReturn(extension.config.tableRole, context), + } + }, })