refactoring
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import { NodeSpec, MarkSpec, Schema } from 'prosemirror-model'
|
import {
|
||||||
import { Extensions } from '../types'
|
NodeSpec, MarkSpec, Schema, ParseRule,
|
||||||
|
} from 'prosemirror-model'
|
||||||
|
import { ExtensionAttribute, Extensions } from '../types'
|
||||||
import splitExtensions from './splitExtensions'
|
import splitExtensions from './splitExtensions'
|
||||||
import getAttributesFromExtensions from './getAttributesFromExtensions'
|
import getAttributesFromExtensions from './getAttributesFromExtensions'
|
||||||
import getRenderedAttributes from './getRenderedAttributes'
|
import getRenderedAttributes from './getRenderedAttributes'
|
||||||
@@ -15,33 +17,7 @@ function cleanUpSchemaItem<T>(data: T) {
|
|||||||
})) as T
|
})) as T
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function getSchema(extensions: Extensions): Schema {
|
function injectExtensionAttributes(parseRule: ParseRule, extensionAttributes: ExtensionAttribute[]): ParseRule {
|
||||||
const allAttributes = getAttributesFromExtensions(extensions)
|
|
||||||
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
|
||||||
const topNode = nodeExtensions.find(extension => extension.topNode)?.name
|
|
||||||
|
|
||||||
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
|
|
||||||
const context = {
|
|
||||||
options: extension.options,
|
|
||||||
}
|
|
||||||
|
|
||||||
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name)
|
|
||||||
|
|
||||||
const schema: NodeSpec = cleanUpSchemaItem({
|
|
||||||
content: extension.content,
|
|
||||||
marks: extension.marks,
|
|
||||||
group: extension.group,
|
|
||||||
inline: extension.inline,
|
|
||||||
atom: extension.atom,
|
|
||||||
selectable: extension.selectable,
|
|
||||||
draggable: extension.draggable,
|
|
||||||
code: extension.code,
|
|
||||||
defining: extension.defining,
|
|
||||||
isolating: extension.isolating,
|
|
||||||
attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
|
|
||||||
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
|
|
||||||
})),
|
|
||||||
parseDOM: extension.parseHTML.bind(context)()?.map(parseRule => {
|
|
||||||
if (parseRule.style) {
|
if (parseRule.style) {
|
||||||
return parseRule
|
return parseRule
|
||||||
}
|
}
|
||||||
@@ -60,27 +36,51 @@ export default function getSchema(extensions: Extensions): Schema {
|
|||||||
return { ...oldAttributes, ...newAttributes }
|
return { ...oldAttributes, ...newAttributes }
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}),
|
}
|
||||||
...(typeof extension.renderHTML === 'function') && {
|
|
||||||
toDOM: node => {
|
export default function getSchema(extensions: Extensions): Schema {
|
||||||
return (extension.renderHTML as Function).bind(context)({
|
const allAttributes = getAttributesFromExtensions(extensions)
|
||||||
|
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
|
||||||
|
const topNode = nodeExtensions.find(extension => extension.topNode)?.name
|
||||||
|
|
||||||
|
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
|
||||||
|
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name)
|
||||||
|
const context = { options: extension.options }
|
||||||
|
const schema: NodeSpec = cleanUpSchemaItem({
|
||||||
|
content: extension.content,
|
||||||
|
marks: extension.marks,
|
||||||
|
group: extension.group,
|
||||||
|
inline: extension.inline,
|
||||||
|
atom: extension.atom,
|
||||||
|
selectable: extension.selectable,
|
||||||
|
draggable: extension.draggable,
|
||||||
|
code: extension.code,
|
||||||
|
defining: extension.defining,
|
||||||
|
isolating: extension.isolating,
|
||||||
|
attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
|
||||||
|
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
|
||||||
|
})),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (extension.parseHTML) {
|
||||||
|
schema.parseDOM = extension.parseHTML
|
||||||
|
.bind(context)()
|
||||||
|
?.map(parseRule => injectExtensionAttributes(parseRule, extensionAttributes))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension.renderHTML) {
|
||||||
|
schema.toDOM = node => (extension.renderHTML as Function)?.bind(context)({
|
||||||
node,
|
node,
|
||||||
attributes: getRenderedAttributes(node, extensionAttributes),
|
attributes: getRenderedAttributes(node, extensionAttributes),
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return [extension.name, schema]
|
return [extension.name, schema]
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const marks = Object.fromEntries(markExtensions.map(extension => {
|
const marks = Object.fromEntries(markExtensions.map(extension => {
|
||||||
const context = {
|
|
||||||
options: extension.options,
|
|
||||||
}
|
|
||||||
|
|
||||||
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name)
|
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.name)
|
||||||
|
const context = { options: extension.options }
|
||||||
const schema: MarkSpec = cleanUpSchemaItem({
|
const schema: MarkSpec = cleanUpSchemaItem({
|
||||||
inclusive: extension.inclusive,
|
inclusive: extension.inclusive,
|
||||||
excludes: extension.excludes,
|
excludes: extension.excludes,
|
||||||
@@ -89,16 +89,20 @@ export default function getSchema(extensions: Extensions): Schema {
|
|||||||
attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
|
attrs: Object.fromEntries(extensionAttributes.map(extensionAttribute => {
|
||||||
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
|
return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }]
|
||||||
})),
|
})),
|
||||||
parseDOM: extension.parseHTML.bind(context)(),
|
})
|
||||||
...(typeof extension.renderHTML === 'function') && {
|
|
||||||
toDOM: mark => {
|
if (extension.parseHTML) {
|
||||||
return (extension.renderHTML as Function).bind(context)({
|
schema.parseDOM = extension.parseHTML
|
||||||
|
.bind(context)()
|
||||||
|
?.map(parseRule => injectExtensionAttributes(parseRule, extensionAttributes))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension.renderHTML) {
|
||||||
|
schema.toDOM = mark => (extension.renderHTML as Function)?.bind(context)({
|
||||||
mark,
|
mark,
|
||||||
attributes: getRenderedAttributes(mark, extensionAttributes),
|
attributes: getRenderedAttributes(mark, extensionAttributes),
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return [extension.name, schema]
|
return [extension.name, schema]
|
||||||
}))
|
}))
|
||||||
|
|||||||
Reference in New Issue
Block a user