diff --git a/packages/core/src/Extension.ts b/packages/core/src/Extension.ts index 4b2f95ed..1eef45a0 100644 --- a/packages/core/src/Extension.ts +++ b/packages/core/src/Extension.ts @@ -31,7 +31,7 @@ declare module '@tiptap/core' { */ addGlobalAttributes?: (this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['addGlobalAttributes'], }) => GlobalAttributes | {}, /** @@ -40,7 +40,7 @@ declare module '@tiptap/core' { addCommands?: (this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['addCommands'], }) => Partial, /** @@ -49,7 +49,7 @@ declare module '@tiptap/core' { addKeyboardShortcuts?: (this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['addKeyboardShortcuts'], }) => { [key: string]: ProseMirrorCommand, }, @@ -60,7 +60,7 @@ declare module '@tiptap/core' { addInputRules?: (this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['addInputRules'], }) => InputRule[], /** @@ -69,7 +69,7 @@ declare module '@tiptap/core' { addPasteRules?: (this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['addPasteRules'], }) => Plugin[], /** @@ -78,7 +78,7 @@ declare module '@tiptap/core' { addProseMirrorPlugins?: (this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['addProseMirrorPlugins'], }) => Plugin[], /** @@ -87,7 +87,7 @@ declare module '@tiptap/core' { extendNodeSchema?: (( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['extendNodeSchema'], }, extension: Node, ) => { @@ -100,7 +100,7 @@ declare module '@tiptap/core' { extendMarkSchema?: (( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['extendMarkSchema'], }, extension: Node, ) => { @@ -113,7 +113,7 @@ declare module '@tiptap/core' { onBeforeCreate?: ((this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['onBeforeCreate'], }) => void) | null, /** @@ -122,7 +122,7 @@ declare module '@tiptap/core' { onCreate?: ((this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['onCreate'], }) => void) | null, /** @@ -131,7 +131,7 @@ declare module '@tiptap/core' { onUpdate?: ((this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['onUpdate'], }) => void) | null, /** @@ -140,7 +140,7 @@ declare module '@tiptap/core' { onSelectionUpdate?: ((this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['onSelectionUpdate'], }) => void) | null, /** @@ -150,7 +150,7 @@ declare module '@tiptap/core' { this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['onTransaction'], }, props: { transaction: Transaction, @@ -164,7 +164,7 @@ declare module '@tiptap/core' { this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['onFocus'], }, props: { event: FocusEvent, @@ -178,7 +178,7 @@ declare module '@tiptap/core' { this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['onBlur'], }, props: { event: FocusEvent, @@ -191,7 +191,7 @@ declare module '@tiptap/core' { onDestroy?: ((this: { options: Options, editor: Editor, - parentConfig: ParentConfig>, + parent: ParentConfig>['onDestroy'], }) => void) | null, } } @@ -205,12 +205,13 @@ export class Extension { defaultOptions: {}, } - // parentConfig: Partial = {} - parent: any + options: Options - options!: Options + parent: Extension | null = null - constructor(config: ExtensionConfig) { + child: Extension | null = null + + constructor(config: Partial> = {}) { this.config = { ...this.config, ...config, @@ -219,33 +220,26 @@ export class Extension { this.options = this.config.defaultOptions } - static create(config: ExtensionConfig) { + static create(config: Partial> = {}) { return new Extension(config) } configure(options: Partial = {}) { - return Extension - .create(this.config as ExtensionConfig) - .#configure(options) - } - - #configure = (options: Partial) => { - this.options = mergeDeep(this.config.defaultOptions, options) as Options + this.options = mergeDeep(this.options, options) as Options return this } extend(extendedConfig: Partial> = {}) { - const extension = new Extension({ - // ...this.config, - ...extendedConfig, - } as ExtensionConfig) + const extension = new Extension(extendedConfig) - // extension.parentConfig = this.config extension.parent = this + + this.child = extension + extension.options = { - ...(this.config.defaultOptions ?? {}), - ...(extendedConfig.defaultOptions ?? {}), + ...extension.parent.options, + ...extension.options, } return extension diff --git a/packages/core/src/Mark.ts b/packages/core/src/Mark.ts index 94d3709b..767a70eb 100644 --- a/packages/core/src/Mark.ts +++ b/packages/core/src/Mark.ts @@ -41,7 +41,7 @@ declare module '@tiptap/core' { */ addGlobalAttributes?: (this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['addGlobalAttributes'], }) => GlobalAttributes | {}, /** @@ -51,7 +51,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addCommands'], }) => Partial, /** @@ -61,7 +61,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addKeyboardShortcuts'], }) => { [key: string]: ProseMirrorCommand, }, @@ -73,7 +73,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addInputRules'], }) => InputRule[], /** @@ -83,7 +83,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addPasteRules'], }) => Plugin[], /** @@ -93,7 +93,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addProseMirrorPlugins'], }) => Plugin[], /** @@ -102,7 +102,7 @@ declare module '@tiptap/core' { extendNodeSchema?: (( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['extendNodeSchema'], }, extension: Node, ) => { @@ -115,7 +115,7 @@ declare module '@tiptap/core' { extendMarkSchema?: (( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['extendMarkSchema'], }, extension: Node, ) => { @@ -125,11 +125,11 @@ declare module '@tiptap/core' { /** * The editor is not ready yet. */ - onBeforeCreate?: ((this: { + onBeforeCreate?: ((this: { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onBeforeCreate'], }) => void) | null, /** @@ -139,7 +139,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onCreate'], }) => void) | null, /** @@ -149,7 +149,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onUpdate'], }) => void) | null, /** @@ -159,7 +159,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onSelectionUpdate'], }) => void) | null, /** @@ -170,7 +170,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onTransaction'], }, props: { transaction: Transaction, @@ -185,7 +185,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onFocus'], }, props: { event: FocusEvent, @@ -200,7 +200,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onBlur'], }, props: { event: FocusEvent, @@ -214,7 +214,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: MarkType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onDestroy'], }) => void) | null, /** @@ -227,7 +227,7 @@ declare module '@tiptap/core' { */ inclusive?: MarkSpec['inclusive'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['inclusive'], }) => MarkSpec['inclusive']), /** @@ -235,7 +235,7 @@ declare module '@tiptap/core' { */ excludes?: MarkSpec['excludes'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['excludes'], }) => MarkSpec['excludes']), /** @@ -243,7 +243,7 @@ declare module '@tiptap/core' { */ group?: MarkSpec['group'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['group'], }) => MarkSpec['group']), /** @@ -251,7 +251,7 @@ declare module '@tiptap/core' { */ spanning?: MarkSpec['spanning'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['spanning'], }) => MarkSpec['spanning']), /** @@ -260,7 +260,7 @@ declare module '@tiptap/core' { parseHTML?: ( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['parseHTML'], }, ) => MarkSpec['parseDOM'], @@ -270,12 +270,12 @@ declare module '@tiptap/core' { renderHTML?: (( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['renderHTML'], }, props: { mark: ProseMirrorMark, HTMLAttributes: { [key: string]: any }, - } + }, ) => DOMOutputSpec) | null, /** @@ -284,27 +284,28 @@ declare module '@tiptap/core' { addAttributes?: ( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['addAttributes'], }, ) => Attributes | {}, } } export class Mark { - type = 'mark' + type = 'node' config: MarkConfig = { - name: 'mark', + name: 'node', priority: 100, defaultOptions: {}, } - // parentConfig: Partial = {} - parent: any + options: Options - options!: Options + parent: Mark | null = null - constructor(config: MarkConfig) { + child: Mark | null = null + + constructor(config: Partial> = {}) { this.config = { ...this.config, ...config, @@ -313,31 +314,27 @@ export class Mark { this.options = this.config.defaultOptions } - static create(config: MarkConfig) { + static create(config: Partial> = {}) { return new Mark(config) } configure(options: Partial = {}) { - return Mark - .create(this.config as MarkConfig) - .#configure(options) - } - - #configure = (options: Partial) => { - this.options = mergeDeep(this.config.defaultOptions, options) as Options + this.options = mergeDeep(this.options, options) as Options return this } - extend(extendedConfig: Partial>) { - const extension = new Mark({ - ...this.config, - ...extendedConfig, - } as MarkConfig) + extend(extendedConfig: Partial> = {}) { + const extension = new Mark(extendedConfig) extension.parent = this - // extension.parentConfig = this.config + this.child = extension + + extension.options = { + ...extension.parent.options, + ...extension.options, + } return extension } diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index 54c4a8d9..de72159e 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -42,7 +42,7 @@ declare module '@tiptap/core' { */ addGlobalAttributes?: (this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['addGlobalAttributes'], }) => GlobalAttributes | {}, /** @@ -52,7 +52,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addCommands'], }) => Partial, /** @@ -62,7 +62,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addKeyboardShortcuts'], }) => { [key: string]: ProseMirrorCommand, }, @@ -74,7 +74,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addInputRules'], }) => InputRule[], /** @@ -84,7 +84,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addPasteRules'], }) => Plugin[], /** @@ -94,7 +94,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addProseMirrorPlugins'], }) => Plugin[], /** @@ -103,7 +103,7 @@ declare module '@tiptap/core' { extendNodeSchema?: (( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['extendNodeSchema'], }, extension: Node, ) => { @@ -116,7 +116,7 @@ declare module '@tiptap/core' { extendMarkSchema?: (( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['extendMarkSchema'], }, extension: Node, ) => { @@ -130,7 +130,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onBeforeCreate'], }) => void) | null, /** @@ -140,7 +140,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onCreate'], }) => void) | null, /** @@ -150,7 +150,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onUpdate'], }) => void) | null, /** @@ -160,7 +160,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onSelectionUpdate'], }) => void) | null, /** @@ -171,7 +171,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onTransaction'], }, props: { transaction: Transaction, @@ -186,7 +186,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onFocus'], }, props: { event: FocusEvent, @@ -201,7 +201,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onBlur'], }, props: { event: FocusEvent, @@ -215,7 +215,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['onDestroy'], }) => void) | null, /** @@ -225,7 +225,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['addNodeView'], }) => NodeViewRenderer) | null, /** @@ -238,7 +238,7 @@ declare module '@tiptap/core' { */ content?: NodeSpec['content'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['content'], }) => NodeSpec['content']), /** @@ -246,7 +246,7 @@ declare module '@tiptap/core' { */ marks?: NodeSpec['marks'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['marks'], }) => NodeSpec['marks']), /** @@ -254,7 +254,7 @@ declare module '@tiptap/core' { */ group?: NodeSpec['group'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['group'], }) => NodeSpec['group']), /** @@ -262,7 +262,7 @@ declare module '@tiptap/core' { */ inline?: NodeSpec['inline'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['inline'], }) => NodeSpec['inline']), /** @@ -270,7 +270,7 @@ declare module '@tiptap/core' { */ atom?: NodeSpec['atom'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['atom'], }) => NodeSpec['atom']), /** @@ -278,7 +278,7 @@ declare module '@tiptap/core' { */ selectable?: NodeSpec['selectable'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['selectable'], }) => NodeSpec['selectable']), /** @@ -286,7 +286,7 @@ declare module '@tiptap/core' { */ draggable?: NodeSpec['draggable'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['draggable'], }) => NodeSpec['draggable']), /** @@ -294,7 +294,7 @@ declare module '@tiptap/core' { */ code?: NodeSpec['code'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['code'], }) => NodeSpec['code']), /** @@ -302,7 +302,7 @@ declare module '@tiptap/core' { */ defining?: NodeSpec['defining'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['defining'], }) => NodeSpec['defining']), /** @@ -310,7 +310,7 @@ declare module '@tiptap/core' { */ isolating?: NodeSpec['isolating'] | ((this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['isolating'], }) => NodeSpec['isolating']), /** @@ -319,7 +319,7 @@ declare module '@tiptap/core' { parseHTML?: ( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['parseHTML'], }, ) => NodeSpec['parseDOM'], @@ -329,7 +329,7 @@ declare module '@tiptap/core' { renderHTML?: (( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['renderHTML'], }, props: { node: ProseMirrorNode, @@ -345,7 +345,7 @@ declare module '@tiptap/core' { options: Options, editor: Editor, type: NodeType, - parentConfig: ParentConfig>, + parent: ParentConfig>['renderText'], }, props: { node: ProseMirrorNode, @@ -358,7 +358,7 @@ declare module '@tiptap/core' { addAttributes?: ( this: { options: Options, - parentConfig: ParentConfig>, + parent: ParentConfig>['addAttributes'], }, ) => Attributes | {}, } @@ -373,12 +373,13 @@ export class Node { defaultOptions: {}, } - // parentConfig: Partial = {} - parent: any + options: Options - options!: Options + parent: Node | null = null - constructor(config: NodeConfig) { + child: Node | null = null + + constructor(config: Partial> = {}) { this.config = { ...this.config, ...config, @@ -387,30 +388,27 @@ export class Node { this.options = this.config.defaultOptions } - static create(config: NodeConfig) { + static create(config: Partial> = {}) { return new Node(config) } configure(options: Partial = {}) { - return Node - .create(this.config as NodeConfig) - .#configure(options) - } - - #configure = (options: Partial) => { - this.options = mergeDeep(this.config.defaultOptions, options) as Options + this.options = mergeDeep(this.options, options) as Options return this } - extend(extendedConfig: Partial>) { - const extension = new Node({ - ...this.config, - ...extendedConfig, - } as NodeConfig) + extend(extendedConfig: Partial> = {}) { + const extension = new Node(extendedConfig) extension.parent = this - // extension.parentConfig = this.config + + this.child = extension + + extension.options = { + ...extension.parent.options, + ...extension.options, + } return extension }