wip: fix extendNodeSchema and extendMarkSchema

This commit is contained in:
Philipp Kühn 2021-04-15 21:43:41 +02:00
parent 07bc40ce75
commit 016bda4010
3 changed files with 23 additions and 30 deletions

View File

@ -3,6 +3,7 @@ import { Command as ProseMirrorCommand } from 'prosemirror-commands'
import { InputRule } from 'prosemirror-inputrules'
import { Editor } from './Editor'
import { Node } from './Node'
import { Mark } from './Mark'
import mergeDeep from './utilities/mergeDeep'
import { GlobalAttributes, RawCommands, ParentConfig } from './types'
import { ExtensionConfig } from '.'
@ -102,7 +103,7 @@ declare module '@tiptap/core' {
options: Options,
parent: ParentConfig<ExtensionConfig<Options>>['extendMarkSchema'],
},
extension: Node,
extension: Mark,
) => {
[key: string]: any,
}) | null,

View File

@ -14,6 +14,7 @@ import {
GlobalAttributes,
ParentConfig,
} from './types'
import { Node } from './Node'
import { MarkConfig } from '.'
import { Editor } from './Editor'
@ -117,7 +118,7 @@ declare module '@tiptap/core' {
options: Options,
parent: ParentConfig<MarkConfig<Options>>['extendMarkSchema'],
},
extension: Node,
extension: Mark,
) => {
[key: string]: any,
}) | null,

View File

@ -1,5 +1,5 @@
import { NodeSpec, MarkSpec, Schema } from 'prosemirror-model'
import { Extensions } from '../types'
import { AnyConfig, Extensions } from '../types'
import { ExtensionConfig, NodeConfig, MarkConfig } from '..'
import splitExtensions from './splitExtensions'
import getAttributesFromExtensions from './getAttributesFromExtensions'
@ -22,27 +22,10 @@ function cleanUpSchemaItem<T>(data: T) {
export default function getSchema(extensions: Extensions): Schema {
const allAttributes = getAttributesFromExtensions(extensions)
const { nodeExtensions, markExtensions } = splitExtensions(extensions)
const topNode = nodeExtensions.find(extension => extension.config.topNode)?.config.name
const nodeSchemaExtenders: (
| ExtensionConfig['extendNodeSchema']
| NodeConfig['extendNodeSchema']
| MarkConfig['extendNodeSchema']
)[] = []
const markSchemaExtenders: (
| ExtensionConfig['extendNodeSchema']
| NodeConfig['extendNodeSchema']
| MarkConfig['extendNodeSchema']
)[] = []
extensions.forEach(extension => {
if (typeof extension.config.extendNodeSchema === 'function') {
nodeSchemaExtenders.push(extension.config.extendNodeSchema)
}
if (typeof extension.config.extendMarkSchema === 'function') {
markSchemaExtenders.push(extension.config.extendMarkSchema)
}
})
const topNodeExtension = nodeExtensions.find(extension => getExtensionField(extension, 'topNode'))
const topNode = topNodeExtension
? getExtensionField<NodeConfig['name']>(topNodeExtension, 'name')
: null
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
const extensionAttributes = allAttributes.filter(attribute => attribute.type === extension.config.name)
@ -50,12 +33,16 @@ export default function getSchema(extensions: Extensions): Schema {
options: extension.options,
}
const extraNodeFields = nodeSchemaExtenders.reduce((fields, nodeSchemaExtender) => {
const extraFields = callOrReturn(nodeSchemaExtender, context, extension)
const extraNodeFields = extensions.reduce((fields, e) => {
const extendNodeSchema = getExtensionField<AnyConfig['extendNodeSchema']>(
e,
'extendNodeSchema',
context,
)
return {
...fields,
...extraFields,
...(extendNodeSchema ? extendNodeSchema(extension) : {}),
}
}, {})
@ -101,12 +88,16 @@ export default function getSchema(extensions: Extensions): Schema {
options: extension.options,
}
const extraMarkFields = markSchemaExtenders.reduce((fields, markSchemaExtender) => {
const extraFields = callOrReturn(markSchemaExtender, context, extension)
const extraMarkFields = extensions.reduce((fields, e) => {
const extendMarkSchema = getExtensionField<AnyConfig['extendMarkSchema']>(
e,
'extendMarkSchema',
context,
)
return {
...fields,
...extraFields,
...(extendMarkSchema ? extendMarkSchema(extension) : {}),
}
}, {})