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

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-10-22 05:55:14 +08:00
import splitExtensions from './splitExtensions'
import {
2020-10-22 05:55:14 +08:00
Extensions,
GlobalAttributes,
Attributes,
Attribute,
ExtensionAttribute,
} from '../types'
export default function getAttributesFromExtensions(extensions: Extensions) {
2020-10-22 15:42:28 +08:00
const extensionAttributes: ExtensionAttribute[] = []
2020-10-22 05:55:14 +08:00
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions]
const defaultAttribute: Required<Attribute> = {
default: null,
rendered: true,
renderHTML: () => ({}),
parseHTML: () => ({}),
}
2020-10-22 05:55:14 +08:00
extensions.forEach(extension => {
const context = {
options: extension.options,
}
2020-10-22 17:14:44 +08:00
const globalAttributes = extension.addGlobalAttributes.bind(context)() as GlobalAttributes
2020-10-22 05:55:14 +08:00
globalAttributes.forEach(globalAttribute => {
globalAttribute.types.forEach(type => {
2020-10-22 15:42:28 +08:00
Object
.entries(globalAttribute.attributes)
.forEach(([name, attribute]) => {
extensionAttributes.push({
type,
name,
attribute: {
...defaultAttribute,
...attribute,
},
})
2020-10-22 05:55:14 +08:00
})
})
})
})
nodeAndMarkExtensions.forEach(extension => {
const context = {
options: extension.options,
}
2020-10-22 17:14:44 +08:00
const attributes = extension.addAttributes.bind(context)() as Attributes
2020-10-22 15:42:28 +08:00
Object
.entries(attributes)
.forEach(([name, attribute]) => {
extensionAttributes.push({
type: extension.name,
name,
attribute: {
...defaultAttribute,
...attribute,
},
})
})
})
2020-10-22 15:42:28 +08:00
return extensionAttributes
}