feat!: Replace defaultOptions with addOptions (#2088)

* add new addOptions option

* replace defaultOptions with addOptions for all extensions

* replace defaultOptions with addOptions for all demos

* replace defaultOptions with addOptions in docs

* refactoring

* refactoring

* drop object support for addOptions

* fix optional options

* fix tests
This commit is contained in:
Philipp Kühn
2021-10-26 18:31:13 +02:00
committed by GitHub
parent 2fff9c264b
commit 9afadeb7fe
57 changed files with 622 additions and 233 deletions

View File

@@ -3,7 +3,7 @@
import { Extension } from '@tiptap/core/src/Extension'
describe('extension options', () => {
it('should set options', () => {
it('should set options (deprecated)', () => {
const extension = Extension.create({
defaultOptions: {
foo: 1,
@@ -17,7 +17,23 @@ describe('extension options', () => {
})
})
it('should pass through', () => {
it('should set options', () => {
const extension = Extension.create({
addOptions() {
return {
foo: 1,
bar: 1,
}
},
})
expect(extension.options).to.deep.eq({
foo: 1,
bar: 1,
})
})
it('should pass through (deprecated)', () => {
const extension = Extension
.create({
defaultOptions: {
@@ -34,7 +50,26 @@ describe('extension options', () => {
})
})
it('should be configurable', () => {
it('should pass through', () => {
const extension = Extension
.create({
addOptions() {
return {
foo: 1,
bar: 1,
}
},
})
.extend()
.configure()
expect(extension.options).to.deep.eq({
foo: 1,
bar: 1,
})
})
it('should be configurable (deprecated)', () => {
const extension = Extension
.create({
defaultOptions: {
@@ -52,7 +87,27 @@ describe('extension options', () => {
})
})
it('should be extendable', () => {
it('should be configurable', () => {
const extension = Extension
.create({
addOptions() {
return {
foo: 1,
bar: 1,
}
},
})
.configure({
bar: 2,
})
expect(extension.options).to.deep.eq({
foo: 1,
bar: 2,
})
})
it('should be extendable (deprecated)', () => {
const extension = Extension.create({
defaultOptions: {
foo: 1,
@@ -74,7 +129,33 @@ describe('extension options', () => {
})
})
it('should be overwritable', () => {
it('should be extendable', () => {
const extension = Extension.create({
addOptions() {
return {
foo: 1,
bar: 1,
}
},
})
const newExtension = extension.extend({
addOptions() {
return {
...this.parent?.(),
baz: 1,
}
},
})
expect(newExtension.options).to.deep.eq({
foo: 1,
bar: 1,
baz: 1,
})
})
it('should be overwritable (deprecated)', () => {
const extension = Extension
.create({
defaultOptions: {
@@ -93,7 +174,30 @@ describe('extension options', () => {
})
})
it('should configure nested objects', () => {
it('should be overwritable', () => {
const extension = Extension
.create({
addOptions() {
return {
foo: 1,
bar: 1,
}
},
})
.extend({
addOptions() {
return {
baz: 1,
}
},
})
expect(extension.options).to.deep.eq({
baz: 1,
})
})
it('should configure nested objects (deprecated)', () => {
const extension = Extension
.create<{
foo: number[],
@@ -122,7 +226,38 @@ describe('extension options', () => {
})
})
it('should create its own instance on configure', () => {
it('should configure nested objects', () => {
const extension = Extension
.create<{
foo: number[],
HTMLAttributes: Record<string, any>,
}>({
addOptions() {
return {
foo: [1, 2, 3],
HTMLAttributes: {
class: 'foo',
},
}
},
})
.configure({
foo: [1],
HTMLAttributes: {
id: 'bar',
},
})
expect(extension.options).to.deep.eq({
foo: [1],
HTMLAttributes: {
class: 'foo',
id: 'bar',
},
})
})
it('should create its own instance on configure (deprecated)', () => {
const extension = Extension
.create({
defaultOptions: {
@@ -150,4 +285,35 @@ describe('extension options', () => {
bar: 2,
})
})
it('should create its own instance on configure', () => {
const extension = Extension
.create({
addOptions() {
return {
foo: 1,
bar: 2,
}
},
})
const extension1 = extension.configure({
foo: 2,
bar: 4,
})
const extension2 = extension.configure({
foo: 3,
})
expect(extension1.options).to.deep.eq({
foo: 2,
bar: 4,
})
expect(extension2.options).to.deep.eq({
foo: 3,
bar: 2,
})
})
})