From 10e16208f94e53b5e20138fb0c37b158d7a1b027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 21 Oct 2020 21:01:39 +0200 Subject: [PATCH] refactoring --- packages/core/src/Node.ts | 10 ++++ packages/core/src/utils/getSchema.ts | 61 +++++++++------------- packages/core/src/utils/splitExtensions.ts | 16 ++++++ 3 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 packages/core/src/utils/splitExtensions.ts diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index cafc6ddfe..73b239b72 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -79,6 +79,11 @@ export interface NodeExtensionSpec extends Extensio group?: NodeSpec['group'], inline?: NodeSpec['inline'], atom?: NodeSpec['atom'], + selectable?: NodeSpec['selectable'], + draggable?: NodeSpec['draggable'], + code?: NodeSpec['code'], + defining?: NodeSpec['defining'], + isolating?: NodeSpec['isolating'], parseHTML?: () => NodeSpec['parseDOM'], renderHTML?: (props: { node: Node, @@ -105,6 +110,11 @@ const defaultNode: NodeExtension = { group: null, inline: null, atom: null, + selectable: null, + draggable: null, + code: null, + defining: null, + isolating: null, createCommands: () => ({}), parseHTML: () => null, renderHTML: () => null, diff --git a/packages/core/src/utils/getSchema.ts b/packages/core/src/utils/getSchema.ts index 49af44a05..6f2f75ca1 100644 --- a/packages/core/src/utils/getSchema.ts +++ b/packages/core/src/utils/getSchema.ts @@ -1,54 +1,43 @@ import { NodeSpec, Schema } from 'prosemirror-model' - import { Extensions } from '../types' -import getTopNodeFromExtensions from './getTopNodeFromExtensions' -import getNodesFromExtensions from './getNodesFromExtensions' -import getMarksFromExtensions from './getMarksFromExtensions' -import resolveExtensionConfig from './resolveExtensionConfig' -import { NodeExtension } from '../Node' -import Mark from '../Mark' -import Extension from '../Extension' +// import getTopNodeFromExtensions from './getTopNodeFromExtensions' +// import getNodesFromExtensions from './getNodesFromExtensions' +// import getMarksFromExtensions from './getMarksFromExtensions' +// import resolveExtensionConfig from './resolveExtensionConfig' +import splitExtensions from './splitExtensions' export default function getSchema(extensions: Extensions): Schema { - // 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 Mark[] + const { nodeExtensions } = splitExtensions(extensions) - // console.log({ extensions }) + const topNode = nodeExtensions.find(extension => extension.topNode)?.name const nodes = Object.fromEntries(nodeExtensions.map(extension => { + const context = { + options: extension.options, + } + + const attributes = { + class: 'test', + } + const schema: NodeSpec = { content: extension.content, + marks: extension.marks, group: extension.group, - parseDOM: extension.parseHTML(), - toDOM: node => extension.renderHTML({ node, attributes: { class: 'test' } }), + 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)(), + toDOM: node => extension.renderHTML.bind(context)({ node, attributes }), } 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({ topNode, nodes, diff --git a/packages/core/src/utils/splitExtensions.ts b/packages/core/src/utils/splitExtensions.ts new file mode 100644 index 000000000..f52004217 --- /dev/null +++ b/packages/core/src/utils/splitExtensions.ts @@ -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, + } +}