mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-18 06:03:22 +08:00
add deep merge
This commit is contained in:
parent
b1d3b4ce8d
commit
1c424f4db1
@ -1,6 +1,7 @@
|
||||
import { Plugin, Transaction } from 'prosemirror-state'
|
||||
import { InputRule } from 'prosemirror-inputrules'
|
||||
import { Editor } from './Editor'
|
||||
import mergeDeep from './utilities/mergeDeep'
|
||||
import { GlobalAttributes } from './types'
|
||||
|
||||
export interface ExtensionConfig<Options = any, Commands = {}> {
|
||||
@ -178,10 +179,7 @@ export class Extension<Options = any, Commands = any> {
|
||||
}
|
||||
|
||||
#configure = (options: Partial<Options>) => {
|
||||
this.options = {
|
||||
...this.config.defaultOptions,
|
||||
...options,
|
||||
}
|
||||
this.options = mergeDeep(this.config.defaultOptions, options) as Options
|
||||
|
||||
return this
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import {
|
||||
import { Plugin, Transaction } from 'prosemirror-state'
|
||||
import { InputRule } from 'prosemirror-inputrules'
|
||||
import { ExtensionConfig } from './Extension'
|
||||
import mergeDeep from './utilities/mergeDeep'
|
||||
import { Attributes, Overwrite } from './types'
|
||||
import { Editor } from './Editor'
|
||||
|
||||
@ -238,10 +239,7 @@ export class Mark<Options = any, Commands = {}> {
|
||||
}
|
||||
|
||||
#configure = (options: Partial<Options>) => {
|
||||
this.options = {
|
||||
...this.config.defaultOptions,
|
||||
...options,
|
||||
}
|
||||
this.options = mergeDeep(this.config.defaultOptions, options) as Options
|
||||
|
||||
return this
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
// @ts-nocheck
|
||||
import {
|
||||
DOMOutputSpec,
|
||||
NodeSpec,
|
||||
@ -7,6 +8,7 @@ import {
|
||||
import { Plugin, Transaction } from 'prosemirror-state'
|
||||
import { InputRule } from 'prosemirror-inputrules'
|
||||
import { ExtensionConfig } from './Extension'
|
||||
import mergeDeep from './utilities/mergeDeep'
|
||||
import { Attributes, NodeViewRenderer, Overwrite } from './types'
|
||||
import { Editor } from './Editor'
|
||||
|
||||
@ -305,10 +307,7 @@ export class Node<Options = any, Commands = {}> {
|
||||
}
|
||||
|
||||
#configure = (options: Partial<Options>) => {
|
||||
this.options = {
|
||||
...this.config.defaultOptions,
|
||||
...options,
|
||||
}
|
||||
this.options = mergeDeep(this.config.defaultOptions, options) as Options
|
||||
|
||||
return this
|
||||
}
|
||||
|
3
packages/core/src/utilities/isObject.ts
Normal file
3
packages/core/src/utilities/isObject.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export default function isObject(item: any): boolean {
|
||||
return (item && typeof item === 'object' && !Array.isArray(item))
|
||||
}
|
22
packages/core/src/utilities/mergeDeep.ts
Normal file
22
packages/core/src/utilities/mergeDeep.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { AnyObject } from '../types'
|
||||
import isObject from './isObject'
|
||||
|
||||
export default function mergeDeep(target: AnyObject, source: AnyObject) {
|
||||
const output = { ...target }
|
||||
|
||||
if (isObject(target) && isObject(source)) {
|
||||
Object.keys(source).forEach(key => {
|
||||
if (isObject(source[key])) {
|
||||
if (!(key in target)) {
|
||||
Object.assign(output, { [key]: source[key] })
|
||||
} else {
|
||||
output[key] = mergeDeep(target[key], source[key])
|
||||
}
|
||||
} else {
|
||||
Object.assign(output, { [key]: source[key] })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
68
tests/cypress/integration/core/mergeDeep.spec.ts
Normal file
68
tests/cypress/integration/core/mergeDeep.spec.ts
Normal file
@ -0,0 +1,68 @@
|
||||
/// <reference types="cypress" />
|
||||
|
||||
import mergeDeep from '@tiptap/core/src/utilities/mergeDeep'
|
||||
|
||||
describe('mergeDeep', () => {
|
||||
it('should merge', () => {
|
||||
const one = {
|
||||
a: 1,
|
||||
}
|
||||
const two = {
|
||||
b: 1,
|
||||
}
|
||||
const result = {
|
||||
a: 1,
|
||||
b: 1,
|
||||
}
|
||||
const merged = mergeDeep(one, two)
|
||||
|
||||
expect(merged).to.deep.eq(result)
|
||||
})
|
||||
|
||||
it('should not merge array', () => {
|
||||
const one = {
|
||||
a: [1],
|
||||
}
|
||||
const two = {
|
||||
a: [2],
|
||||
}
|
||||
const result = {
|
||||
a: [2],
|
||||
}
|
||||
const merged = mergeDeep(one, two)
|
||||
|
||||
expect(merged).to.deep.eq(result)
|
||||
})
|
||||
|
||||
it('should merge deep', () => {
|
||||
const one = {
|
||||
a: 1,
|
||||
b: {
|
||||
c: true,
|
||||
},
|
||||
d: {
|
||||
e: true,
|
||||
f: [1],
|
||||
},
|
||||
}
|
||||
const two = {
|
||||
b: 1,
|
||||
d: {
|
||||
f: [2],
|
||||
g: 1,
|
||||
},
|
||||
}
|
||||
const result = {
|
||||
a: 1,
|
||||
b: 1,
|
||||
d: {
|
||||
e: true,
|
||||
f: [2],
|
||||
g: 1,
|
||||
},
|
||||
}
|
||||
const merged = mergeDeep(one, two)
|
||||
|
||||
expect(merged).to.deep.eq(result)
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user