add extension class

This commit is contained in:
Philipp Kühn 2019-12-16 23:20:05 +01:00
parent 6f1aedb2b2
commit 3b291c3a3c
7 changed files with 129 additions and 9 deletions

View File

@ -1,3 +1,5 @@
import { Editor } from './src/Editor'
export default Editor
export default Editor
export { default as Extension } from './src/Extension'
export { default as Node } from './src/Node'

View File

@ -12,11 +12,13 @@ import insertText from './commands/insertText'
import insertHTML from './commands/insertHTML'
import focus from './commands/focus'
type EditorContent = string | JSON
interface EditorOptions {
element: Node
content: string
content: EditorContent
}
export class Editor {
private lastCommand = Promise.resolve()
@ -84,7 +86,7 @@ export class Editor {
return this[name](...args)
}
private createDocument(content: any): any {
private createDocument(content: EditorContent): any {
// if (content === null) {
// return this.schema.nodeFromJSON(this.options.emptyDocument)
// }

View File

@ -0,0 +1,52 @@
import { Editor } from './Editor'
export default class Extension {
editor: any
options: { [key: string]: any } = {}
defaultOptions: { [key: string]: any } = {}
constructor(options = {}) {
this.options = {
...this.defaultOptions,
...options,
}
}
init(): any {
return null
}
bindEditor(editor: Editor): void {
this.editor = editor
}
get name(): any {
return null
}
get type(): any {
return 'extension'
}
get update(): any {
return () => {}
}
get plugins(): any {
return []
}
inputRules(): any {
return []
}
pasteRules(): any {
return []
}
keys(): any {
return {}
}
}

View File

@ -0,0 +1,27 @@
import Extension from './Extension'
export default class Node extends Extension {
constructor(options = {}) {
super(options)
}
// protected type = 'node'
get type() {
return 'node'
}
get view(): any {
return null
}
get schema(): any {
return null
}
command() {
return () => {}
}
}

View File

@ -5,7 +5,7 @@ import minMax from '../utils/minMax'
declare module '../Editor' {
interface Editor {
focus(position: any): Editor
focus(position: Position): Editor
}
}

View File

@ -0,0 +1,23 @@
import { Node } from '@tiptap/core'
export default class Document extends Node {
// get name() {
// return 'document'
// }
// get schema() {
// return {
// content: 'block+',
// }
// }
// type = 'nope'
name = 'document'
schema = {
content: 'block+',
}
}

View File

@ -41,10 +41,24 @@ export default {
// console.log(html)
// next()
// })
.focus('start')
.insertText('<p>start</p>')
.focus('end')
.insertHTML('<p>end</p>')
// .registerCommand('insertHello', async (next, editor) => {
// await editor.insertHTML('<strong>HELLO</strong>')
// next()
// })
// .registerCommand('insertHello', (next, editor) => {
// editor
// .focus('start')
// .insertHTML('<strong>HELLO</strong>')
// next()
// })
// .focus('start')
// .insertHello()
// .insertText('eins')
// .insertText('zwei')
// .insertText('drei')
// .insertHello()
// .focus('end')
// .insertHTML('<p>end</p>')
}
}
</script>