2023-02-03 00:37:33 +08:00
|
|
|
import { MarkSpec, NodeSpec, Schema } from '@tiptap/pm/model'
|
2022-06-08 20:10:25 +08:00
|
|
|
|
2023-07-01 03:03:49 +08:00
|
|
|
import { Editor, MarkConfig, NodeConfig } from '../index.js'
|
|
|
|
import { AnyConfig, Extensions } from '../types.js'
|
|
|
|
import { callOrReturn } from '../utilities/callOrReturn.js'
|
|
|
|
import { isEmptyObject } from '../utilities/isEmptyObject.js'
|
|
|
|
import { getAttributesFromExtensions } from './getAttributesFromExtensions.js'
|
|
|
|
import { getExtensionField } from './getExtensionField.js'
|
|
|
|
import { getRenderedAttributes } from './getRenderedAttributes.js'
|
|
|
|
import { injectExtensionAttributesToParseRule } from './injectExtensionAttributesToParseRule.js'
|
|
|
|
import { splitExtensions } from './splitExtensions.js'
|
2021-05-07 16:25:55 +08:00
|
|
|
|
|
|
|
function cleanUpSchemaItem<T>(data: T) {
|
2023-02-03 00:37:33 +08:00
|
|
|
return Object.fromEntries(
|
2023-03-29 21:16:43 +08:00
|
|
|
// @ts-ignore
|
2023-02-03 00:37:33 +08:00
|
|
|
Object.entries(data).filter(([key, value]) => {
|
2023-03-29 21:16:43 +08:00
|
|
|
if (key === 'attrs' && isEmptyObject(value as {} | undefined)) {
|
2023-02-03 00:37:33 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-05-07 16:25:55 +08:00
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
return value !== null && value !== undefined
|
|
|
|
}),
|
|
|
|
) as T
|
2021-05-07 16:25:55 +08:00
|
|
|
}
|
|
|
|
|
2023-03-29 21:16:43 +08:00
|
|
|
export function getSchemaByResolvedExtensions(extensions: Extensions, editor?: Editor): Schema {
|
2021-05-07 16:25:55 +08:00
|
|
|
const allAttributes = getAttributesFromExtensions(extensions)
|
|
|
|
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
|
|
|
const topNode = nodeExtensions.find(extension => getExtensionField(extension, 'topNode'))?.name
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
const nodes = Object.fromEntries(
|
|
|
|
nodeExtensions.map(extension => {
|
|
|
|
const extensionAttributes = allAttributes.filter(
|
|
|
|
attribute => attribute.type === extension.name,
|
2021-05-07 16:25:55 +08:00
|
|
|
)
|
2023-02-03 00:37:33 +08:00
|
|
|
const context = {
|
|
|
|
name: extension.name,
|
|
|
|
options: extension.options,
|
|
|
|
storage: extension.storage,
|
2023-03-29 21:16:43 +08:00
|
|
|
editor,
|
2021-05-07 16:25:55 +08:00
|
|
|
}
|
2023-02-03 00:37:33 +08:00
|
|
|
|
|
|
|
const extraNodeFields = extensions.reduce((fields, e) => {
|
|
|
|
const extendNodeSchema = getExtensionField<AnyConfig['extendNodeSchema']>(
|
|
|
|
e,
|
|
|
|
'extendNodeSchema',
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
...fields,
|
|
|
|
...(extendNodeSchema ? extendNodeSchema(extension) : {}),
|
|
|
|
}
|
|
|
|
}, {})
|
|
|
|
|
|
|
|
const schema: NodeSpec = cleanUpSchemaItem({
|
|
|
|
...extraNodeFields,
|
|
|
|
content: callOrReturn(
|
|
|
|
getExtensionField<NodeConfig['content']>(extension, 'content', context),
|
|
|
|
),
|
|
|
|
marks: callOrReturn(getExtensionField<NodeConfig['marks']>(extension, 'marks', context)),
|
|
|
|
group: callOrReturn(getExtensionField<NodeConfig['group']>(extension, 'group', context)),
|
|
|
|
inline: callOrReturn(getExtensionField<NodeConfig['inline']>(extension, 'inline', context)),
|
|
|
|
atom: callOrReturn(getExtensionField<NodeConfig['atom']>(extension, 'atom', context)),
|
|
|
|
selectable: callOrReturn(
|
|
|
|
getExtensionField<NodeConfig['selectable']>(extension, 'selectable', context),
|
|
|
|
),
|
|
|
|
draggable: callOrReturn(
|
|
|
|
getExtensionField<NodeConfig['draggable']>(extension, 'draggable', context),
|
|
|
|
),
|
|
|
|
code: callOrReturn(getExtensionField<NodeConfig['code']>(extension, 'code', context)),
|
|
|
|
defining: callOrReturn(
|
|
|
|
getExtensionField<NodeConfig['defining']>(extension, 'defining', context),
|
|
|
|
),
|
|
|
|
isolating: callOrReturn(
|
|
|
|
getExtensionField<NodeConfig['isolating']>(extension, 'isolating', context),
|
|
|
|
),
|
|
|
|
attrs: Object.fromEntries(
|
|
|
|
extensionAttributes.map(extensionAttribute => {
|
|
|
|
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
|
|
|
|
}),
|
|
|
|
),
|
2021-05-07 16:25:55 +08:00
|
|
|
})
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
const parseHTML = callOrReturn(
|
|
|
|
getExtensionField<NodeConfig['parseHTML']>(extension, 'parseHTML', context),
|
|
|
|
)
|
2021-09-10 05:51:05 +08:00
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
if (parseHTML) {
|
|
|
|
schema.parseDOM = parseHTML.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes))
|
|
|
|
}
|
2021-09-10 05:51:05 +08:00
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
const renderHTML = getExtensionField<NodeConfig['renderHTML']>(
|
|
|
|
extension,
|
|
|
|
'renderHTML',
|
|
|
|
context,
|
|
|
|
)
|
2021-05-07 16:25:55 +08:00
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
if (renderHTML) {
|
|
|
|
schema.toDOM = node => renderHTML({
|
|
|
|
node,
|
|
|
|
HTMLAttributes: getRenderedAttributes(node, extensionAttributes),
|
|
|
|
})
|
|
|
|
}
|
2021-05-07 16:25:55 +08:00
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
const renderText = getExtensionField<NodeConfig['renderText']>(
|
|
|
|
extension,
|
|
|
|
'renderText',
|
2021-05-07 16:25:55 +08:00
|
|
|
context,
|
|
|
|
)
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
if (renderText) {
|
|
|
|
schema.toText = renderText
|
2021-05-07 16:25:55 +08:00
|
|
|
}
|
2023-02-03 00:37:33 +08:00
|
|
|
|
|
|
|
return [extension.name, schema]
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
const marks = Object.fromEntries(
|
|
|
|
markExtensions.map(extension => {
|
|
|
|
const extensionAttributes = allAttributes.filter(
|
|
|
|
attribute => attribute.type === extension.name,
|
|
|
|
)
|
|
|
|
const context = {
|
|
|
|
name: extension.name,
|
|
|
|
options: extension.options,
|
|
|
|
storage: extension.storage,
|
2023-03-29 21:16:43 +08:00
|
|
|
editor,
|
2023-02-03 00:37:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const extraMarkFields = extensions.reduce((fields, e) => {
|
|
|
|
const extendMarkSchema = getExtensionField<AnyConfig['extendMarkSchema']>(
|
|
|
|
e,
|
|
|
|
'extendMarkSchema',
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
...fields,
|
|
|
|
...(extendMarkSchema ? extendMarkSchema(extension) : {}),
|
|
|
|
}
|
|
|
|
}, {})
|
|
|
|
|
|
|
|
const schema: MarkSpec = cleanUpSchemaItem({
|
|
|
|
...extraMarkFields,
|
|
|
|
inclusive: callOrReturn(
|
|
|
|
getExtensionField<MarkConfig['inclusive']>(extension, 'inclusive', context),
|
|
|
|
),
|
|
|
|
excludes: callOrReturn(
|
|
|
|
getExtensionField<MarkConfig['excludes']>(extension, 'excludes', context),
|
|
|
|
),
|
|
|
|
group: callOrReturn(getExtensionField<MarkConfig['group']>(extension, 'group', context)),
|
|
|
|
spanning: callOrReturn(
|
|
|
|
getExtensionField<MarkConfig['spanning']>(extension, 'spanning', context),
|
|
|
|
),
|
|
|
|
code: callOrReturn(getExtensionField<MarkConfig['code']>(extension, 'code', context)),
|
|
|
|
attrs: Object.fromEntries(
|
|
|
|
extensionAttributes.map(extensionAttribute => {
|
|
|
|
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
|
|
|
|
}),
|
|
|
|
),
|
2021-05-07 16:25:55 +08:00
|
|
|
})
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
const parseHTML = callOrReturn(
|
|
|
|
getExtensionField<MarkConfig['parseHTML']>(extension, 'parseHTML', context),
|
|
|
|
)
|
|
|
|
|
|
|
|
if (parseHTML) {
|
|
|
|
schema.parseDOM = parseHTML.map(parseRule => injectExtensionAttributesToParseRule(parseRule, extensionAttributes))
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderHTML = getExtensionField<MarkConfig['renderHTML']>(
|
|
|
|
extension,
|
|
|
|
'renderHTML',
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
|
|
|
|
if (renderHTML) {
|
|
|
|
schema.toDOM = mark => renderHTML({
|
|
|
|
mark,
|
|
|
|
HTMLAttributes: getRenderedAttributes(mark, extensionAttributes),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return [extension.name, schema]
|
|
|
|
}),
|
|
|
|
)
|
2021-05-07 16:25:55 +08:00
|
|
|
|
|
|
|
return new Schema({
|
|
|
|
topNode,
|
|
|
|
nodes,
|
|
|
|
marks,
|
|
|
|
})
|
|
|
|
}
|