Skip to content

Commit

Permalink
fix(core): configuring extensions should add to the parent's options …
Browse files Browse the repository at this point in the history
…not replace them (#5357)

* fix(core): configuring extensions should add to the parent's options not replace them

* fix: order of tests
  • Loading branch information
nperez0111 authored Jul 17, 2024
1 parent a21a122 commit 07f4c03
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-bugs-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---

There was a bug where doing a `.configure` on an extension, node or mark would overwrite the extensions options instead of being merged with the default options.
4 changes: 2 additions & 2 deletions packages/core/src/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ export class Extension<Options = any, Storage = any> {
// with different calls of `configure`
const extension = this.extend({
...this.config,
addOptions() {
return mergeDeep(this.parent?.() || {}, options) as Options
addOptions: () => {
return mergeDeep(this.options as Record<string, any>, options) as Options
},
})

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ export class Mark<Options = any, Storage = any> {
// with different calls of `configure`
const extension = this.extend({
...this.config,
addOptions() {
return mergeDeep(this.parent?.() || {}, options) as Options
addOptions: () => {
return mergeDeep(this.options as Record<string, any>, options) as Options
},
})

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,8 @@ export class Node<Options = any, Storage = any> {
// with different calls of `configure`
const extension = this.extend({
...this.config,
addOptions() {
return mergeDeep(this.parent?.() || {}, options) as Options
addOptions: () => {
return mergeDeep(this.options as Record<string, any>, options) as Options
},
})

Expand Down
27 changes: 24 additions & 3 deletions tests/cypress/integration/core/extendExtensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,26 +393,47 @@ describe('extend extensions', () => {
})
})

it('should configure to be in addition to the parent options', () => {
const parentExtension = Extendable
.create({
name: 'parentExtension',
addOptions() {
return { parent: 'exists', overwrite: 'parent' }
},
})

const childExtension = parentExtension
.configure({ child: 'exists-too', overwrite: 'child' })

expect(childExtension.options).to.deep.eq({
parent: 'exists',
child: 'exists-too',
overwrite: 'child',
})
})

it('should deeply merge options when extending a configured extension', () => {
const parentExtension = Extendable
.create({
name: 'parentExtension',
addOptions() {
return { defaultOptions: 'is-overwritten' }
return { defaultOptions: 'exists', overwrite: 'parent' }
},
})

const childExtension = parentExtension
.configure({ configuredOptions: 'exists-too' }).extend({
.configure({ configuredOptions: 'exists-too', overwrite: 'configure' }).extend({
name: 'childExtension',
addOptions() {
return { ...this.parent?.(), additionalOptions: 'exist-too' }
return { ...this.parent?.(), additionalOptions: 'exist-too', overwrite: 'child' }
},
})

expect(childExtension.options).to.deep.eq({
defaultOptions: 'exists',
configuredOptions: 'exists-too',
additionalOptions: 'exist-too',
overwrite: 'child',
})
})
})
Expand Down

0 comments on commit 07f4c03

Please sign in to comment.