refactoring

This commit is contained in:
Philipp Kühn 2020-10-21 21:01:39 +02:00
parent 2ebf4bf790
commit 10e16208f9
3 changed files with 51 additions and 36 deletions

View File

@ -79,6 +79,11 @@ export interface NodeExtensionSpec<Options = {}, Commands = {}> extends Extensio
group?: NodeSpec['group'], group?: NodeSpec['group'],
inline?: NodeSpec['inline'], inline?: NodeSpec['inline'],
atom?: NodeSpec['atom'], atom?: NodeSpec['atom'],
selectable?: NodeSpec['selectable'],
draggable?: NodeSpec['draggable'],
code?: NodeSpec['code'],
defining?: NodeSpec['defining'],
isolating?: NodeSpec['isolating'],
parseHTML?: () => NodeSpec['parseDOM'], parseHTML?: () => NodeSpec['parseDOM'],
renderHTML?: (props: { renderHTML?: (props: {
node: Node, node: Node,
@ -105,6 +110,11 @@ const defaultNode: NodeExtension = {
group: null, group: null,
inline: null, inline: null,
atom: null, atom: null,
selectable: null,
draggable: null,
code: null,
defining: null,
isolating: null,
createCommands: () => ({}), createCommands: () => ({}),
parseHTML: () => null, parseHTML: () => null,
renderHTML: () => null, renderHTML: () => null,

View File

@ -1,54 +1,43 @@
import { NodeSpec, Schema } from 'prosemirror-model' import { NodeSpec, Schema } from 'prosemirror-model'
import { Extensions } from '../types' import { Extensions } from '../types'
import getTopNodeFromExtensions from './getTopNodeFromExtensions' // import getTopNodeFromExtensions from './getTopNodeFromExtensions'
import getNodesFromExtensions from './getNodesFromExtensions' // import getNodesFromExtensions from './getNodesFromExtensions'
import getMarksFromExtensions from './getMarksFromExtensions' // import getMarksFromExtensions from './getMarksFromExtensions'
import resolveExtensionConfig from './resolveExtensionConfig' // import resolveExtensionConfig from './resolveExtensionConfig'
import { NodeExtension } from '../Node' import splitExtensions from './splitExtensions'
import Mark from '../Mark'
import Extension from '../Extension'
export default function getSchema(extensions: Extensions): Schema { export default function getSchema(extensions: Extensions): Schema {
// const baseExtensions = extensions.filter(extension => extension.type === 'extension') as Extension[] const { nodeExtensions } = splitExtensions(extensions)
const nodeExtensions = extensions.filter(extension => extension.type === 'node') as NodeExtension[]
// const markExtensions = extensions.filter(extension => extension.type === 'mark') as Mark[]
// console.log({ extensions }) const topNode = nodeExtensions.find(extension => extension.topNode)?.name
const nodes = Object.fromEntries(nodeExtensions.map(extension => { const nodes = Object.fromEntries(nodeExtensions.map(extension => {
const context = {
options: extension.options,
}
const attributes = {
class: 'test',
}
const schema: NodeSpec = { const schema: NodeSpec = {
content: extension.content, content: extension.content,
marks: extension.marks,
group: extension.group, group: extension.group,
parseDOM: extension.parseHTML(), inline: extension.inline,
toDOM: node => extension.renderHTML({ node, attributes: { class: 'test' } }), atom: extension.atom,
selectable: extension.selectable,
draggable: extension.draggable,
code: extension.code,
defining: extension.defining,
isolating: extension.isolating,
parseDOM: extension.parseHTML.bind(context)(),
toDOM: node => extension.renderHTML.bind(context)({ node, attributes }),
} }
return [extension.name, schema] return [extension.name, schema]
})) }))
const topNode = nodeExtensions.find(extension => extension.topNode)?.name
// extensions.forEach(extension => {
// resolveExtensionConfig(extension, 'name')
// resolveExtensionConfig(extension, 'defaults')
// resolveExtensionConfig(extension, 'topNode')
// const { name } = extension.config
// const options = {
// ...extension.config.defaults,
// ...extension.options,
// }
// resolveExtensionConfig(extension, 'schema', { name, options })
// })
// return new Schema({
// topNode: getTopNodeFromExtensions(extensions),
// nodes: getNodesFromExtensions(extensions),
// marks: getMarksFromExtensions(extensions),
// })
return new Schema({ return new Schema({
topNode, topNode,
nodes, nodes,

View File

@ -0,0 +1,16 @@
import { Extensions } from '../types'
import { Extension } from '../Extension'
import { NodeExtension } from '../Node'
// import { MarkExtension } from '../Node'
export default function splitExtensions(extensions: Extensions) {
const baseExtensions = extensions.filter(extension => extension.type === 'extension') as Extension[]
const nodeExtensions = extensions.filter(extension => extension.type === 'node') as NodeExtension[]
// const markExtensions = extensions.filter(extension => extension.type === 'mark') as MarkExtension[]
return {
baseExtensions,
nodeExtensions,
// markExtensions,
}
}