2020-10-22 05:55:14 +08:00
|
|
|
import splitExtensions from './splitExtensions'
|
2020-10-22 05:32:28 +08:00
|
|
|
import {
|
2020-10-22 05:55:14 +08:00
|
|
|
Extensions,
|
|
|
|
GlobalAttributes,
|
|
|
|
Attributes,
|
|
|
|
Attribute,
|
|
|
|
ExtensionAttribute,
|
2020-10-22 05:32:28 +08:00
|
|
|
} 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]
|
2020-10-22 05:32:28 +08:00
|
|
|
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 => {
|
2020-10-22 05:32:28 +08:00
|
|
|
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
|
|
|
|
}
|