2020-10-22 03:13:38 +08:00
|
|
|
import { NodeSpec, MarkSpec, Schema } from 'prosemirror-model'
|
2020-09-04 17:59:09 +08:00
|
|
|
import { Extensions } from '../types'
|
2020-10-22 03:01:39 +08:00
|
|
|
// import getTopNodeFromExtensions from './getTopNodeFromExtensions'
|
|
|
|
// import getNodesFromExtensions from './getNodesFromExtensions'
|
|
|
|
// import getMarksFromExtensions from './getMarksFromExtensions'
|
|
|
|
// import resolveExtensionConfig from './resolveExtensionConfig'
|
|
|
|
import splitExtensions from './splitExtensions'
|
2020-10-22 05:32:28 +08:00
|
|
|
import getAttributesFromExtensions from './getAttributesFromExtensions'
|
|
|
|
import getRenderedAttributes from './getRenderedAttributes'
|
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-10-10 04:59:25 +08:00
|
|
|
|
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 => {
|
2020-10-22 03:01:39 +08:00
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
}
|
|
|
|
|
2020-10-22 05:32:28 +08:00
|
|
|
const attributes = allAttributes.filter(attribute => attribute.type === extension.name)
|
2020-10-22 03:01:39 +08:00
|
|
|
|
2020-10-12 16:32:54 +08:00
|
|
|
const schema: NodeSpec = {
|
|
|
|
content: extension.content,
|
2020-10-22 03:01:39 +08:00
|
|
|
marks: extension.marks,
|
2020-10-12 16:32:54 +08:00
|
|
|
group: extension.group,
|
2020-10-22 03:01:39 +08:00
|
|
|
inline: extension.inline,
|
|
|
|
atom: extension.atom,
|
|
|
|
selectable: extension.selectable,
|
|
|
|
draggable: extension.draggable,
|
|
|
|
code: extension.code,
|
|
|
|
defining: extension.defining,
|
|
|
|
isolating: extension.isolating,
|
|
|
|
parseDOM: extension.parseHTML.bind(context)(),
|
2020-10-22 05:32:28 +08:00
|
|
|
toDOM: node => {
|
|
|
|
return extension.renderHTML.bind(context)({
|
|
|
|
node,
|
|
|
|
attributes: getRenderedAttributes(node, attributes),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
attrs: Object.fromEntries(attributes.map(attribute => {
|
|
|
|
return [attribute.name, { default: attribute?.attribute?.default }]
|
|
|
|
})),
|
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 05:32:28 +08:00
|
|
|
// console.log({ nodes })
|
|
|
|
|
2020-10-22 03:13:38 +08:00
|
|
|
const marks = Object.fromEntries(markExtensions.map(extension => {
|
|
|
|
const context = {
|
|
|
|
options: extension.options,
|
|
|
|
}
|
|
|
|
|
2020-10-22 05:32:28 +08:00
|
|
|
// const attributes = {
|
|
|
|
// class: 'test',
|
|
|
|
// }
|
2020-10-22 03:13:38 +08:00
|
|
|
|
|
|
|
const schema: MarkSpec = {
|
|
|
|
inclusive: extension.inclusive,
|
|
|
|
excludes: extension.excludes,
|
|
|
|
group: extension.group,
|
|
|
|
spanning: extension.spanning,
|
|
|
|
parseDOM: extension.parseHTML.bind(context)(),
|
2020-10-22 05:32:28 +08:00
|
|
|
toDOM: node => extension.renderHTML.bind(context)({ node, attributes: {} }),
|
2020-10-22 03:13:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return [extension.name, schema]
|
|
|
|
}))
|
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|