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

37 lines
1.1 KiB
TypeScript
Raw Normal View History

import Extension from '../Extension'
import Node from '../Node'
import Mark from '../Mark'
import { Schema } from 'prosemirror-model'
import collect from 'collect.js'
2020-09-04 17:59:09 +08:00
import { Extensions } from '../types'
2020-09-04 17:59:09 +08:00
export default function getSchema(extensions: Extensions): Schema {
return new Schema({
topNode: getTopNodeFromExtensions(extensions),
nodes: getNodesFromExtensions(extensions),
marks: getMarksFromExtensions(extensions),
})
}
function getNodesFromExtensions(extensions: (Extension | Node | Mark)[]): any {
return collect(extensions)
.where('extensionType', 'node')
.mapWithKeys((extension: Node) => [extension.name, extension.schema()])
.all()
}
function getTopNodeFromExtensions(extensions: (Extension | Node | Mark)[]): any {
const topNode = collect(extensions).firstWhere('topNode', true)
if (topNode) {
return topNode.name
}
}
function getMarksFromExtensions(extensions: (Extension | Node | Mark)[]): any {
return collect(extensions)
.where('extensionType', 'mark')
.mapWithKeys((extension: Mark) => [extension.name, extension.schema()])
.all()
}