tiptap/packages/core/src/ExtensionManager.ts

48 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-03-06 04:05:01 +08:00
import { keymap } from 'prosemirror-keymap'
2020-03-06 04:35:30 +08:00
import collect from 'collect.js'
2020-03-06 04:05:01 +08:00
import { Editor } from './Editor'
2020-03-08 06:37:58 +08:00
import Extension from './Extension'
import Node from './Node'
2020-03-06 04:05:01 +08:00
export default class ExtensionManager {
2020-03-08 06:37:58 +08:00
extensions: (Extension | Node)[]
2020-03-06 04:05:01 +08:00
2020-03-08 06:37:58 +08:00
constructor(extensions: (Extension | Node)[], editor: Editor) {
2020-03-06 04:05:01 +08:00
this.extensions = extensions
2020-03-06 05:40:02 +08:00
this.extensions.forEach(extension => {
extension.bindEditor(editor)
2020-03-08 05:54:45 +08:00
extension.created()
2020-03-06 05:40:02 +08:00
})
2020-03-06 04:05:01 +08:00
}
2020-03-06 07:15:36 +08:00
get topNode() {
2020-03-08 06:37:58 +08:00
const topNode = collect(this.extensions).firstWhere('topNode', true)
2020-03-06 07:15:36 +08:00
if (topNode) {
return topNode.name
}
}
2020-03-06 04:35:30 +08:00
get nodes(): any {
return collect(this.extensions)
.where('type', 'node')
2020-03-29 06:54:49 +08:00
.mapWithKeys((extension: any) => [extension.name, extension.schema()])
2020-03-06 04:35:30 +08:00
.all()
2020-03-06 04:05:01 +08:00
}
2020-03-06 04:35:30 +08:00
get marks(): any {
return collect(this.extensions)
.where('type', 'mark')
2020-03-29 06:54:49 +08:00
.mapWithKeys((extension: any) => [extension.name, extension.schema()])
2020-03-06 04:35:30 +08:00
.all()
2020-03-06 04:05:01 +08:00
}
2020-03-06 07:02:48 +08:00
get plugins(): any {
2020-03-06 05:15:17 +08:00
return collect(this.extensions)
2020-03-29 06:58:48 +08:00
.flatMap(extension => extension.plugins())
2020-03-06 05:15:17 +08:00
.toArray()
}
2020-03-06 04:05:01 +08:00
}