merge HTMLAttributes
This commit is contained in:
@@ -6,6 +6,7 @@ import getRenderedAttributes from './getRenderedAttributes'
|
||||
import isEmptyObject from './isEmptyObject'
|
||||
import injectExtensionAttributesToParseRule from './injectExtensionAttributesToParseRule'
|
||||
import callOrReturn from './callOrReturn'
|
||||
import mergeAttributes from './mergeAttributes'
|
||||
|
||||
function cleanUpSchemaItem<T>(data: T) {
|
||||
return Object.fromEntries(Object.entries(data).filter(([key, value]) => {
|
||||
@@ -50,7 +51,10 @@ export default function getSchema(extensions: Extensions): Schema {
|
||||
if (extension.renderHTML) {
|
||||
schema.toDOM = node => (extension.renderHTML as Function)?.bind(context)({
|
||||
node,
|
||||
HTMLAttributes: getRenderedAttributes(node, extensionAttributes),
|
||||
HTMLAttributes: mergeAttributes(
|
||||
extension.options.HTMLAttributes,
|
||||
getRenderedAttributes(node, extensionAttributes),
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -79,7 +83,10 @@ export default function getSchema(extensions: Extensions): Schema {
|
||||
if (extension.renderHTML) {
|
||||
schema.toDOM = mark => (extension.renderHTML as Function)?.bind(context)({
|
||||
mark,
|
||||
HTMLAttributes: getRenderedAttributes(mark, extensionAttributes),
|
||||
HTMLAttributes: mergeAttributes(
|
||||
extension.options.HTMLAttributes,
|
||||
getRenderedAttributes(mark, extensionAttributes),
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,26 +1,28 @@
|
||||
import { AnyObject } from '../types'
|
||||
|
||||
export default function mergeAttributes(...object: AnyObject[]) {
|
||||
return object.reduce((items, item) => {
|
||||
const mergedAttributes = { ...items }
|
||||
export default function mergeAttributes(...objects: AnyObject[]) {
|
||||
return objects
|
||||
.filter(item => !!item)
|
||||
.reduce((items, item) => {
|
||||
const mergedAttributes = { ...items }
|
||||
|
||||
Object.entries(item).forEach(([key, value]) => {
|
||||
const exists = mergedAttributes[key]
|
||||
Object.entries(item).forEach(([key, value]) => {
|
||||
const exists = mergedAttributes[key]
|
||||
|
||||
if (!exists) {
|
||||
mergedAttributes[key] = value
|
||||
return
|
||||
}
|
||||
if (!exists) {
|
||||
mergedAttributes[key] = value
|
||||
return
|
||||
}
|
||||
|
||||
if (key === 'class') {
|
||||
mergedAttributes[key] = [mergedAttributes[key], value].join(' ')
|
||||
} else if (key === 'style') {
|
||||
mergedAttributes[key] = [mergedAttributes[key], value].join('; ')
|
||||
} else {
|
||||
mergedAttributes[key] = value
|
||||
}
|
||||
})
|
||||
if (key === 'class') {
|
||||
mergedAttributes[key] = [mergedAttributes[key], value].join(' ')
|
||||
} else if (key === 'style') {
|
||||
mergedAttributes[key] = [mergedAttributes[key], value].join('; ')
|
||||
} else {
|
||||
mergedAttributes[key] = value
|
||||
}
|
||||
})
|
||||
|
||||
return mergedAttributes
|
||||
}, {})
|
||||
return mergedAttributes
|
||||
}, {})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user