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

65 lines
1.6 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) {
const allAttributes: 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: () => null,
}
2020-10-22 05:55:14 +08:00
extensions.forEach(extension => {
const context = {
options: extension.options,
}
const globalAttributes = extension.createGlobalAttributes.bind(context)() as GlobalAttributes
globalAttributes.forEach(globalAttribute => {
globalAttribute.types.forEach(type => {
Object.entries(globalAttribute.attributes).forEach(([name, attribute]) => {
allAttributes.push({
type,
name,
attribute: {
...defaultAttribute,
...attribute,
},
})
})
})
})
})
nodeAndMarkExtensions.forEach(extension => {
const context = {
options: extension.options,
}
const attributes = extension.createAttributes.bind(context)() as Attributes
Object.entries(attributes).forEach(([name, attribute]) => {
allAttributes.push({
type: extension.name,
name,
attribute: {
...defaultAttribute,
...attribute,
},
})
})
})
return allAttributes
}