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

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

* fix: order of tests
This commit is contained in:
Nick Perez 2024-07-17 09:06:41 +02:00 committed by GitHub
parent a21a122759
commit 07f4c03315
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 35 additions and 9 deletions

View File

@ -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.

View File

@ -459,8 +459,8 @@ export class Extension<Options = any, Storage = any> {
// with different calls of `configure` // with different calls of `configure`
const extension = this.extend({ const extension = this.extend({
...this.config, ...this.config,
addOptions() { addOptions: () => {
return mergeDeep(this.parent?.() || {}, options) as Options return mergeDeep(this.options as Record<string, any>, options) as Options
}, },
}) })

View File

@ -591,8 +591,8 @@ export class Mark<Options = any, Storage = any> {
// with different calls of `configure` // with different calls of `configure`
const extension = this.extend({ const extension = this.extend({
...this.config, ...this.config,
addOptions() { addOptions: () => {
return mergeDeep(this.parent?.() || {}, options) as Options return mergeDeep(this.options as Record<string, any>, options) as Options
}, },
}) })

View File

@ -782,8 +782,8 @@ export class Node<Options = any, Storage = any> {
// with different calls of `configure` // with different calls of `configure`
const extension = this.extend({ const extension = this.extend({
...this.config, ...this.config,
addOptions() { addOptions: () => {
return mergeDeep(this.parent?.() || {}, options) as Options return mergeDeep(this.options as Record<string, any>, options) as Options
}, },
}) })

View File

@ -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', () => { it('should deeply merge options when extending a configured extension', () => {
const parentExtension = Extendable const parentExtension = Extendable
.create({ .create({
name: 'parentExtension', name: 'parentExtension',
addOptions() { addOptions() {
return { defaultOptions: 'is-overwritten' } return { defaultOptions: 'exists', overwrite: 'parent' }
}, },
}) })
const childExtension = parentExtension const childExtension = parentExtension
.configure({ configuredOptions: 'exists-too' }).extend({ .configure({ configuredOptions: 'exists-too', overwrite: 'configure' }).extend({
name: 'childExtension', name: 'childExtension',
addOptions() { addOptions() {
return { ...this.parent?.(), additionalOptions: 'exist-too' } return { ...this.parent?.(), additionalOptions: 'exist-too', overwrite: 'child' }
}, },
}) })
expect(childExtension.options).to.deep.eq({ expect(childExtension.options).to.deep.eq({
defaultOptions: 'exists',
configuredOptions: 'exists-too', configuredOptions: 'exists-too',
additionalOptions: 'exist-too', additionalOptions: 'exist-too',
overwrite: 'child',
}) })
}) })
}) })