2020-10-25 05:53:56 +08:00
|
|
|
import { NodeSpec, MarkSpec, Schema } from 'prosemirror-model'
|
|
|
|
import { Extensions } from '../types'
|
2020-10-22 03:01:39 +08:00
|
|
|
import splitExtensions from './splitExtensions'
|
2020-10-22 05:32:28 +08:00
|
|
|
import getAttributesFromExtensions from './getAttributesFromExtensions'
|
|
|
|
import getRenderedAttributes from './getRenderedAttributes'
|
2020-11-30 16:42:53 +08:00
|
|
|
import isEmptyObject from '../utilities/isEmptyObject'
|
2020-10-25 05:53:56 +08:00
|
|
|
import injectExtensionAttributesToParseRule from './injectExtensionAttributesToParseRule'
|
2020-11-30 16:42:53 +08:00
|
|
|
import callOrReturn from '../utilities/callOrReturn'
|
2020-10-23 15:20:26 +08:00
|
|
|
|
2020-10-23 16:59:26 +08:00
|
|
|
function cleanUpSchemaItem<T>(data: T) {
|
2020-10-23 15:20:26 +08:00
|
|
|
return Object.fromEntries(Object.entries(data).filter(([key, value]) => {
|
|
|
|
if (key === 'attrs' && isEmptyObject(value)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return value !== null && value !== undefined
|
2020-10-23 16:59:26 +08:00
|
|
|
})) as T
|
2020-10-23 15:20:26 +08:00
|
|
|
}
|
2020-09-03 22:22:08 +08:00
|
|
|
|
2020-09-04 17:59:09 +08:00
|
|
|
export default function getSchema(extensions: Extensions): Schema {
|
2020-10-22 05:32:28 +08:00
|
|
|
const allAttributes = getAttributesFromExtensions(extensions)
|
2020-10-22 03:13:38 +08:00
|
|
|
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
2020-11-16 16:43:17 +08:00
|
|
|
const topNode = nodeExtensions.find(extension => extension.config.topNode)?.config.name
|
2020-10-12 16:32:54 +08:00
|
|
|
|
2020-10-21 21:17:05 +08:00
|
|
|
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
|
2020-11-16 16:43:17 +08:00
|
|
|
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.config.name)
|
2020-10-24 05:16:54 +08:00
|
|
|
const context = { options: extension.options }
|
2020-10-23 15:20:26 +08:00
|
|
|
const schema: NodeSpec = cleanUpSchemaItem({
|
2020-11-16 16:43:17 +08:00
|
|
|
content: callOrReturn(extension.config.content, context),
|
|
|
|
marks: callOrReturn(extension.config.marks, context),
|
|
|
|
group: callOrReturn(extension.config.group, context),
|
|
|
|
inline: callOrReturn(extension.config.inline, context),
|
|
|
|
atom: callOrReturn(extension.config.atom, context),
|
|
|
|
selectable: callOrReturn(extension.config.selectable, context),
|
|
|
|
draggable: callOrReturn(extension.config.draggable, context),
|
|
|
|
code: callOrReturn(extension.config.code, context),
|
|
|
|
defining: callOrReturn(extension.config.defining, context),
|
|
|
|
isolating: callOrReturn(extension.config.isolating, context),
|
2020-10-24 04:55:48 +08:00
|
|
|
attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
|
|
|
|
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
|
2020-10-22 05:32:28 +08:00
|
|
|
})),
|
2020-10-24 05:16:54 +08:00
|
|
|
})
|
2020-10-24 04:55:48 +08:00
|
|
|
|
2020-11-16 16:43:17 +08:00
|
|
|
if (extension.config.parseHTML) {
|
|
|
|
schema.parseDOM = extension.config.parseHTML
|
2020-10-24 05:16:54 +08:00
|
|
|
.bind(context)()
|
2020-10-25 05:53:56 +08:00
|
|
|
?.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes))
|
2020-10-24 05:16:54 +08:00
|
|
|
}
|
2020-10-24 04:55:48 +08:00
|
|
|
|
2020-11-16 16:43:17 +08:00
|
|
|
if (extension.config.renderHTML) {
|
|
|
|
schema.toDOM = node => (extension.config.renderHTML as Function)?.bind(context)({
|
2020-10-24 05:16:54 +08:00
|
|
|
node,
|
2020-11-25 16:50:54 +08:00
|
|
|
HTMLAttributes: getRenderedAttributes(node, extensionAttributes),
|
2020-10-24 05:16:54 +08:00
|
|
|
})
|
|
|
|
}
|
2020-10-12 16:32:54 +08:00
|
|
|
|
2020-11-16 16:43:17 +08:00
|
|
|
return [extension.config.name, schema]
|
2020-10-10 04:59:25 +08:00
|
|
|
}))
|
|
|
|
|
2020-10-22 03:13:38 +08:00
|
|
|
const marks = Object.fromEntries(markExtensions.map(extension => {
|
2020-11-16 16:43:17 +08:00
|
|
|
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.config.name)
|
2020-10-24 05:16:54 +08:00
|
|
|
const context = { options: extension.options }
|
2020-10-23 15:20:26 +08:00
|
|
|
const schema: MarkSpec = cleanUpSchemaItem({
|
2020-11-16 16:43:17 +08:00
|
|
|
inclusive: callOrReturn(extension.config.inclusive, context),
|
|
|
|
excludes: callOrReturn(extension.config.excludes, context),
|
|
|
|
group: callOrReturn(extension.config.group, context),
|
|
|
|
spanning: callOrReturn(extension.config.spanning, context),
|
2020-10-24 04:55:48 +08:00
|
|
|
attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
|
|
|
|
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
|
2020-10-22 15:14:24 +08:00
|
|
|
})),
|
2020-10-23 15:20:26 +08:00
|
|
|
})
|
2020-10-22 03:13:38 +08:00
|
|
|
|
2020-11-16 16:43:17 +08:00
|
|
|
if (extension.config.parseHTML) {
|
|
|
|
schema.parseDOM = extension.config.parseHTML
|
2020-10-24 05:16:54 +08:00
|
|
|
.bind(context)()
|
2020-10-25 05:53:56 +08:00
|
|
|
?.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes))
|
2020-10-24 05:16:54 +08:00
|
|
|
}
|
|
|
|
|
2020-11-16 16:43:17 +08:00
|
|
|
if (extension.config.renderHTML) {
|
|
|
|
schema.toDOM = mark => (extension.config.renderHTML as Function)?.bind(context)({
|
2020-10-24 05:16:54 +08:00
|
|
|
mark,
|
2020-11-25 16:50:54 +08:00
|
|
|
HTMLAttributes: getRenderedAttributes(mark, extensionAttributes),
|
2020-10-24 05:16:54 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:43:17 +08:00
|
|
|
return [extension.config.name, schema]
|
2020-10-22 03:13:38 +08:00
|
|
|
}))
|
|
|
|
|
2020-09-03 22:22:08 +08:00
|
|
|
return new Schema({
|
2020-10-10 04:59:25 +08:00
|
|
|
topNode,
|
|
|
|
nodes,
|
2020-10-22 03:13:38 +08:00
|
|
|
marks,
|
2020-09-03 22:22:08 +08:00
|
|
|
})
|
|
|
|
}
|