tiptap/packages/core/src/ExtensionManager.ts

132 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-03-06 04:35:30 +08:00
import collect from 'collect.js'
2020-08-22 06:12:34 +08:00
import { Plugin } from 'prosemirror-state'
2020-04-02 15:42:26 +08:00
import { keymap } from 'prosemirror-keymap'
import { inputRules } from 'prosemirror-inputrules'
2020-04-26 04:40:38 +08:00
import { EditorView, Decoration } from 'prosemirror-view'
import { Node as ProsemirrorNode } from 'prosemirror-model'
2020-08-22 06:12:34 +08:00
import { Editor } from './Editor'
2020-03-08 06:37:58 +08:00
import Extension from './Extension'
import Node from './Node'
2020-08-22 06:05:00 +08:00
import Mark from './Mark'
2020-04-26 04:40:38 +08:00
import capitalize from './utils/capitalize'
2020-03-06 04:05:01 +08:00
2020-09-09 05:49:58 +08:00
// type Extensions = (Extension | Node | Mark)[]
type Extensions = Function[]
2020-08-22 06:05:00 +08:00
2020-03-06 04:05:01 +08:00
export default class ExtensionManager {
2020-04-01 04:17:54 +08:00
editor: Editor
2020-08-22 06:05:00 +08:00
extensions: Extensions
2020-03-06 04:05:01 +08:00
2020-08-22 06:05:00 +08:00
constructor(extensions: Extensions, editor: Editor) {
2020-04-01 04:17:54 +08:00
this.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 => {
2020-09-09 05:49:58 +08:00
console.log({extension})
// extension.bindEditor(editor)
// editor.on('schemaCreated', () => {
// this.editor.registerCommands(extension.commands())
// extension.created()
// })
2020-03-06 05:40:02 +08:00
})
2020-03-06 04:05:01 +08:00
}
2020-04-02 20:34:07 +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 {
2020-09-09 05:49:58 +08:00
// return collect(this.extensions)
// .where('extensionType', 'node')
// .mapWithKeys((extension: Node) => [extension.name, extension.schema()])
// .all()
return []
2020-03-06 04:05:01 +08:00
}
2020-03-06 04:35:30 +08:00
get marks(): any {
2020-09-09 05:49:58 +08:00
// return collect(this.extensions)
// .where('extensionType', 'mark')
// .mapWithKeys((extension: Mark) => [extension.name, extension.schema()])
// .all()
return []
2020-03-06 04:05:01 +08:00
}
2020-08-22 06:12:34 +08:00
get plugins(): Plugin[] {
2020-09-09 05:49:58 +08:00
// const plugins = collect(this.extensions)
// .flatMap(extension => extension.plugins())
// .toArray()
// return [
// ...plugins,
// ...this.keymaps,
// ...this.pasteRules,
// inputRules({ rules: this.inputRules }),
// ]
return []
2020-03-06 05:15:17 +08:00
}
2020-04-02 14:53:59 +08:00
get inputRules(): any {
2020-09-09 05:49:58 +08:00
// return collect(this.extensions)
// .flatMap(extension => extension.inputRules())
// .toArray()
return {}
2020-04-02 14:53:59 +08:00
}
get pasteRules(): any {
2020-09-09 05:49:58 +08:00
// return collect(this.extensions)
// .flatMap(extension => extension.pasteRules())
// .toArray()
return {}
2020-04-02 14:53:59 +08:00
}
2020-04-01 04:17:54 +08:00
get keymaps() {
2020-09-09 05:49:58 +08:00
// return collect(this.extensions)
// .map(extension => extension.keys())
// .filter(keys => !!Object.keys(keys).length)
// // @ts-ignore
// .map(keys => keymap(keys))
// .toArray()
return []
2020-04-01 04:17:54 +08:00
}
2020-04-24 15:32:37 +08:00
get nodeViews() {
2020-09-09 05:49:58 +08:00
// const { renderer: Renderer } = this.editor
// if (!Renderer || !Renderer.type) {
// return {}
// }
// const prop = `to${capitalize(Renderer.type)}`
// return collect(this.extensions)
// .where('extensionType', 'node')
// .filter((extension: any) => extension.schema()[prop])
// .map((extension: any) => {
// return (
// node: ProsemirrorNode,
// view: EditorView,
// getPos: (() => number) | boolean,
// decorations: Decoration[],
// ) => {
// return new Renderer(extension.schema()[prop], {
// extension,
// editor: this.editor,
// node,
// getPos,
// decorations,
// })
// }
// })
// .all()
return {}
2020-04-24 15:32:37 +08:00
}
2020-03-06 04:05:01 +08:00
}