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

102 lines
3.8 KiB
TypeScript
Raw Normal View History

import { NodeSpec, MarkSpec, Schema } from 'prosemirror-model'
import { Extensions } from '../types'
2020-10-22 03:01:39 +08:00
import splitExtensions from './splitExtensions'
import getAttributesFromExtensions from './getAttributesFromExtensions'
import getRenderedAttributes from './getRenderedAttributes'
2020-10-23 15:20:26 +08:00
import isEmptyObject from './isEmptyObject'
import injectExtensionAttributesToParseRule from './injectExtensionAttributesToParseRule'
import callOrReturn from './callOrReturn'
2020-11-15 00:27:59 +08:00
import mergeAttributes from './mergeAttributes'
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-04 17:59:09 +08:00
export default function getSchema(extensions: Extensions): Schema {
const allAttributes = getAttributesFromExtensions(extensions)
2020-10-22 03:13:38 +08:00
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
2020-10-22 03:01:39 +08:00
const topNode = nodeExtensions.find(extension => extension.topNode)?.name
2020-10-12 16:32:54 +08:00
2020-10-21 21:17:05 +08:00
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.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({
content: callOrReturn(extension.content, context),
marks: callOrReturn(extension.marks, context),
group: callOrReturn(extension.group, context),
inline: callOrReturn(extension.inline, context),
atom: callOrReturn(extension.atom, context),
selectable: callOrReturn(extension.selectable, context),
draggable: callOrReturn(extension.draggable, context),
code: callOrReturn(extension.code, context),
defining: callOrReturn(extension.defining, context),
isolating: callOrReturn(extension.isolating, context),
attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
})),
2020-10-24 05:16:54 +08:00
})
2020-10-24 05:16:54 +08:00
if (extension.parseHTML) {
schema.parseDOM = extension.parseHTML
.bind(context)()
?.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes))
2020-10-24 05:16:54 +08:00
}
2020-10-24 05:16:54 +08:00
if (extension.renderHTML) {
schema.toDOM = node => (extension.renderHTML as Function)?.bind(context)({
node,
2020-11-15 00:27:59 +08:00
HTMLAttributes: mergeAttributes(
extension.options.HTMLAttributes,
getRenderedAttributes(node, extensionAttributes),
),
2020-10-24 05:16:54 +08:00
})
}
2020-10-12 16:32:54 +08:00
2020-10-21 21:17:05 +08:00
return [extension.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 => {
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.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({
inclusive: callOrReturn(extension.inclusive, context),
excludes: callOrReturn(extension.excludes, context),
group: callOrReturn(extension.group, context),
spanning: callOrReturn(extension.spanning, context),
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-10-24 05:16:54 +08:00
if (extension.parseHTML) {
schema.parseDOM = extension.parseHTML
.bind(context)()
?.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes))
2020-10-24 05:16:54 +08:00
}
if (extension.renderHTML) {
schema.toDOM = mark => (extension.renderHTML as Function)?.bind(context)({
mark,
2020-11-15 00:27:59 +08:00
HTMLAttributes: mergeAttributes(
extension.options.HTMLAttributes,
getRenderedAttributes(mark, extensionAttributes),
),
2020-10-24 05:16:54 +08:00
})
}
2020-10-22 03:13:38 +08:00
return [extension.name, schema]
}))
return new Schema({
2020-10-10 04:59:25 +08:00
topNode,
nodes,
2020-10-22 03:13:38 +08:00
marks,
})
}