tiptap/packages/core/src/utils/getRenderedAttributes.ts

24 lines
787 B
TypeScript
Raw Normal View History

2020-10-22 15:14:24 +08:00
import { Node, Mark } from 'prosemirror-model'
import { ExtensionAttribute } from '../types'
2020-10-23 21:02:52 +08:00
import mergeAttributes from './mergeAttributes'
2020-10-24 19:48:41 +08:00
import isEmptyObject from './isEmptyObject'
2020-10-22 15:42:28 +08:00
export default function getRenderedAttributes(nodeOrMark: Node | Mark, extensionAttributes: ExtensionAttribute[]): { [key: string]: any } {
return extensionAttributes
2020-10-22 05:35:12 +08:00
.filter(item => item.attribute.rendered)
.map(item => {
2020-10-24 19:48:41 +08:00
const renderedAttributes = item.attribute.renderHTML(nodeOrMark.attrs)
if (isEmptyObject(renderedAttributes)) {
return {
[`data-${item.name}`]: nodeOrMark.attrs[item.name],
}
}
return renderedAttributes
})
2020-10-22 15:42:28 +08:00
.reduce((attributes, attribute) => {
2020-10-23 21:02:52 +08:00
return mergeAttributes(attributes, attribute)
}, {})
}