add basic extension manager
This commit is contained in:
@@ -11,6 +11,8 @@ import {exampleSetup} from "prosemirror-example-setup"
|
||||
import elementFromString from './utils/elementFromString'
|
||||
import injectCSS from './utils/injectCSS'
|
||||
|
||||
import ExtensionManager from './ExtensionManager'
|
||||
|
||||
type EditorContent = string | JSON
|
||||
|
||||
interface Options {
|
||||
@@ -24,10 +26,12 @@ export class Editor {
|
||||
|
||||
private lastCommand = Promise.resolve()
|
||||
|
||||
private schema: Schema = new Schema({
|
||||
nodes: addListNodes(schema.spec.nodes, "paragraph block*", "block"),
|
||||
marks: schema.spec.marks
|
||||
})
|
||||
schema: Schema
|
||||
|
||||
// private schema: Schema = new Schema({
|
||||
// nodes: addListNodes(schema.spec.nodes, "paragraph block*", "block"),
|
||||
// marks: schema.spec.marks
|
||||
// })
|
||||
|
||||
selection = { from: 0, to: 0 }
|
||||
|
||||
@@ -39,8 +43,12 @@ export class Editor {
|
||||
extensions: [],
|
||||
}
|
||||
|
||||
extensionManager: ExtensionManager
|
||||
|
||||
constructor(options: Options) {
|
||||
this.options = { ...this.options, ...options }
|
||||
this.extensionManager = new ExtensionManager(this.options.extensions, this)
|
||||
this.schema = this.createSchema()
|
||||
this.view = this.createView()
|
||||
this.registerCommand('focus', require('./commands/focus').default)
|
||||
this.registerCommand('insertText', require('./commands/insertText').default)
|
||||
@@ -55,6 +63,14 @@ export class Editor {
|
||||
return this.view.state
|
||||
}
|
||||
|
||||
private createSchema() {
|
||||
return new Schema({
|
||||
// topNode: this.options.topNode,
|
||||
nodes: this.extensionManager.nodes,
|
||||
marks: this.extensionManager.marks,
|
||||
})
|
||||
}
|
||||
|
||||
private createState() {
|
||||
return EditorState.create({
|
||||
doc: this.createDocument(this.options.content),
|
||||
|
||||
@@ -23,9 +23,7 @@ export default abstract class Extension {
|
||||
this.editor = editor
|
||||
}
|
||||
|
||||
get type(): any {
|
||||
return 'extension'
|
||||
}
|
||||
public type = 'extension'
|
||||
|
||||
get update(): any {
|
||||
return () => {}
|
||||
|
||||
34
packages/tiptap-core/src/ExtensionManager.ts
Normal file
34
packages/tiptap-core/src/ExtensionManager.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { keymap } from 'prosemirror-keymap'
|
||||
import { Editor } from './Editor'
|
||||
|
||||
export default class ExtensionManager {
|
||||
|
||||
extensions: [any?]
|
||||
|
||||
constructor(extensions: any = [], editor: Editor) {
|
||||
// extensions.forEach(extension => {
|
||||
// extension.bindEditor(editor)
|
||||
// extension.init()
|
||||
// })
|
||||
this.extensions = extensions
|
||||
}
|
||||
|
||||
get nodes() {
|
||||
return this.extensions
|
||||
.filter(extension => extension.type === 'node')
|
||||
.reduce((nodes, { name, schema }) => ({
|
||||
...nodes,
|
||||
[name]: schema,
|
||||
}), {})
|
||||
}
|
||||
|
||||
get marks() {
|
||||
return this.extensions
|
||||
.filter(extension => extension.type === 'mark')
|
||||
.reduce((marks, { name, schema }) => ({
|
||||
...marks,
|
||||
[name]: schema,
|
||||
}), {})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ export default abstract class Node extends Extension {
|
||||
super(options)
|
||||
}
|
||||
|
||||
// protected type = 'node'
|
||||
public type = 'node'
|
||||
|
||||
// get type() {
|
||||
// return 'node'
|
||||
|
||||
Reference in New Issue
Block a user