tiptap/packages/core/src/utils/getSchema.ts

65 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-09-10 03:18:04 +08:00
import deepmerge from 'deepmerge'
2020-10-10 04:59:25 +08:00
import { NodeSpec, Schema } from 'prosemirror-model'
2020-09-04 17:59:09 +08:00
import { Extensions } from '../types'
import getTopNodeFromExtensions from './getTopNodeFromExtensions'
import getNodesFromExtensions from './getNodesFromExtensions'
import getMarksFromExtensions from './getMarksFromExtensions'
2020-09-10 03:18:04 +08:00
import resolveExtensionConfig from './resolveExtensionConfig'
2020-10-10 04:59:25 +08:00
import Node from '../Node'
import Mark from '../Mark'
import Extension from '../Extension'
2020-09-04 17:59:09 +08:00
export default function getSchema(extensions: Extensions): Schema {
2020-10-10 04:59:25 +08:00
const baseExtensions = extensions.filter(extension => extension.type === 'extension') as Extension[]
const nodeExtensions = extensions.filter(extension => extension.type === 'node') as Node[]
const markExtensions = extensions.filter(extension => extension.type === 'mark') as Mark[]
2020-10-12 16:32:54 +08:00
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
const schema: NodeSpec = {
content: extension.content,
group: extension.group,
parseDOM: extension.parseHTML(),
toDOM: node => extension.renderHTML({ node, attributes: { class: 'hee' } }),
}
2020-10-10 04:59:25 +08:00
return [
2020-10-12 16:32:54 +08:00
extension.name,
schema,
2020-10-10 04:59:25 +08:00
]
}))
console.log({ nodes })
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),
// })
2020-09-10 03:18:04 +08:00
return new Schema({
2020-10-10 04:59:25 +08:00
topNode,
nodes,
marks: {},
// topNode: getTopNodeFromExtensions(extensions),
// nodes: getNodesFromExtensions(extensions),
// marks: getMarksFromExtensions(extensions),
})
}