2020-09-03 22:22:08 +08:00
|
|
|
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-03 22:22:08 +08:00
|
|
|
|
2020-09-04 17:59:09 +08:00
|
|
|
export default function getSchema(extensions: Extensions): Schema {
|
2020-09-03 22:22:08 +08:00
|
|
|
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()
|
|
|
|
}
|