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'
|
|
|
|
|
2020-10-27 20:19:23 +08:00
|
|
|
/**
|
|
|
|
* Get a list of all extension attributes defined in `addAttribute` and `addGlobalAttribute`.
|
|
|
|
* @param extensions List of extensions
|
|
|
|
*/
|
2021-01-28 16:50:17 +08:00
|
|
|
export default function getAttributesFromExtensions(extensions: Extensions): ExtensionAttribute[] {
|
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]
|
2020-10-22 05:32:28 +08:00
|
|
|
const defaultAttribute: Required<Attribute> = {
|
|
|
|
default: null,
|
|
|
|
rendered: true,
|
2020-10-25 05:53:56 +08:00
|
|
|
renderHTML: null,
|
|
|
|
parseHTML: null,
|
2020-10-22 05:32:28 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 05:55:14 +08:00
|
|
|
extensions.forEach(extension => {
|
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:43:17 +08:00
|
|
|
const globalAttributes = extension.config.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 => {
|
2020-10-22 05:32:28 +08:00
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:43:17 +08:00
|
|
|
const attributes = extension.config.addAttributes.bind(context)() as Attributes
|
2020-10-22 05:32:28 +08:00
|
|
|
|
2020-10-22 15:42:28 +08:00
|
|
|
Object
|
|
|
|
.entries(attributes)
|
|
|
|
.forEach(([name, attribute]) => {
|
|
|
|
extensionAttributes.push({
|
2020-11-16 16:43:17 +08:00
|
|
|
type: extension.config.name,
|
2020-10-22 15:42:28 +08:00
|
|
|
name,
|
|
|
|
attribute: {
|
|
|
|
...defaultAttribute,
|
|
|
|
...attribute,
|
|
|
|
},
|
|
|
|
})
|
2020-10-22 05:32:28 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-10-22 15:42:28 +08:00
|
|
|
return extensionAttributes
|
2020-10-22 05:32:28 +08:00
|
|
|
}
|