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

58 lines
1.9 KiB
TypeScript
Raw Normal View History

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-21 21:30:34 +08:00
import { NodeExtension } from '../Node'
2020-10-10 04:59:25 +08:00
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-21 21:17:05 +08:00
// const baseExtensions = extensions.filter(extension => extension.type === 'extension') as Extension[]
2020-10-21 21:30:34 +08:00
const nodeExtensions = extensions.filter(extension => extension.type === 'node') as NodeExtension[]
2020-10-21 21:17:05 +08:00
// const markExtensions = extensions.filter(extension => extension.type === 'mark') as Mark[]
2020-10-10 04:59:25 +08:00
2020-10-21 21:17:05 +08:00
// console.log({ extensions })
2020-10-12 16:32:54 +08:00
2020-10-21 21:17:05 +08:00
const nodes = Object.fromEntries(nodeExtensions.map(extension => {
2020-10-12 16:32:54 +08:00
const schema: NodeSpec = {
content: extension.content,
group: extension.group,
parseDOM: extension.parseHTML(),
2020-10-21 21:17:05 +08:00
toDOM: node => extension.renderHTML({ node, attributes: { class: 'test' } }),
2020-10-12 16:32:54 +08:00
}
2020-10-21 21:17:05 +08:00
return [extension.name, schema]
2020-10-10 04:59:25 +08:00
}))
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: {},
})
}