2020-03-06 06:59:48 +08:00
|
|
|
import { EventEmitter } from 'events'
|
|
|
|
import { EditorState, TextSelection } from 'prosemirror-state'
|
|
|
|
import { EditorView} from 'prosemirror-view'
|
|
|
|
import { Schema, DOMParser, DOMSerializer } from 'prosemirror-model'
|
2020-03-06 05:15:17 +08:00
|
|
|
import { inputRules, undoInputRule } from 'prosemirror-inputrules'
|
|
|
|
import { keymap } from 'prosemirror-keymap'
|
|
|
|
import { baseKeymap } from 'prosemirror-commands'
|
|
|
|
import { dropCursor } from 'prosemirror-dropcursor'
|
|
|
|
import { gapCursor } from 'prosemirror-gapcursor'
|
2019-12-08 07:16:44 +08:00
|
|
|
|
2020-03-11 17:23:28 +08:00
|
|
|
import magicMethods from './utils/magicMethods'
|
2019-12-17 06:51:18 +08:00
|
|
|
import elementFromString from './utils/elementFromString'
|
2020-03-05 04:45:49 +08:00
|
|
|
import injectCSS from './utils/injectCSS'
|
2020-03-06 04:05:01 +08:00
|
|
|
import ExtensionManager from './ExtensionManager'
|
2020-03-08 06:37:58 +08:00
|
|
|
import Extension from './Extension'
|
|
|
|
import Node from './Node'
|
2020-03-06 04:05:01 +08:00
|
|
|
|
2020-03-29 07:11:10 +08:00
|
|
|
type EditorContent = string | JSON | null
|
2020-03-06 18:24:58 +08:00
|
|
|
type Command = (next: Function, editor: Editor, ...args: any) => any
|
2019-12-17 06:20:05 +08:00
|
|
|
|
2020-03-05 05:40:08 +08:00
|
|
|
interface Options {
|
2019-12-17 06:20:05 +08:00
|
|
|
content: EditorContent
|
2020-03-08 06:37:58 +08:00
|
|
|
extensions: (Extension | Node)[]
|
2020-03-05 05:40:08 +08:00
|
|
|
injectCSS: Boolean
|
2019-12-08 07:16:44 +08:00
|
|
|
}
|
2019-12-17 06:20:05 +08:00
|
|
|
|
2020-03-11 17:02:47 +08:00
|
|
|
@magicMethods
|
2020-03-06 06:59:48 +08:00
|
|
|
export class Editor extends EventEmitter {
|
2019-12-08 07:16:44 +08:00
|
|
|
|
2020-03-11 17:23:28 +08:00
|
|
|
proxy!: any
|
2020-03-09 06:25:48 +08:00
|
|
|
element = document.createElement('div')
|
2020-03-06 04:49:53 +08:00
|
|
|
extensionManager!: ExtensionManager
|
|
|
|
schema!: Schema
|
|
|
|
view!: EditorView
|
2020-03-05 05:40:08 +08:00
|
|
|
options: Options = {
|
|
|
|
content: '',
|
|
|
|
injectCSS: true,
|
2020-03-06 03:30:58 +08:00
|
|
|
extensions: [],
|
2020-03-05 05:40:08 +08:00
|
|
|
}
|
2020-03-11 06:19:41 +08:00
|
|
|
commands: { [key: string]: any } = {}
|
2020-03-06 04:49:53 +08:00
|
|
|
|
|
|
|
private lastCommand = Promise.resolve()
|
|
|
|
|
|
|
|
public selection = { from: 0, to: 0 }
|
2020-03-06 04:05:01 +08:00
|
|
|
|
2020-03-05 05:40:08 +08:00
|
|
|
constructor(options: Options) {
|
2020-03-06 06:59:48 +08:00
|
|
|
super()
|
2020-03-05 05:40:08 +08:00
|
|
|
this.options = { ...this.options, ...options }
|
2020-03-06 04:49:53 +08:00
|
|
|
this.createExtensionManager()
|
|
|
|
this.createSchema()
|
|
|
|
this.createView()
|
2020-03-05 04:23:18 +08:00
|
|
|
this.registerCommand('focus', require('./commands/focus').default)
|
|
|
|
this.registerCommand('insertText', require('./commands/insertText').default)
|
|
|
|
this.registerCommand('insertHTML', require('./commands/insertHTML').default)
|
2020-03-05 04:45:49 +08:00
|
|
|
|
2020-03-05 05:40:08 +08:00
|
|
|
if (this.options.injectCSS) {
|
|
|
|
injectCSS(require('./style.css'))
|
|
|
|
}
|
2019-12-08 07:16:44 +08:00
|
|
|
}
|
|
|
|
|
2020-03-11 17:02:47 +08:00
|
|
|
__get(name: string) {
|
|
|
|
const command = this.commands[name]
|
|
|
|
|
|
|
|
if (!command) {
|
|
|
|
throw new Error(`tiptap: command '${name}' not found.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (...args: any) => command(...args)
|
|
|
|
}
|
|
|
|
|
2020-03-11 06:19:41 +08:00
|
|
|
public registerCommand(name: string, callback: Command): Editor {
|
|
|
|
if (this.commands[name]) {
|
|
|
|
throw new Error(`tiptap: command '${name}' is already defined.`)
|
|
|
|
}
|
2020-03-11 17:02:47 +08:00
|
|
|
|
|
|
|
this.commands[name] = this.chainCommand((...args: any) => {
|
2020-03-11 06:19:41 +08:00
|
|
|
return new Promise(resolve => callback(resolve, this, ...args))
|
|
|
|
})
|
|
|
|
|
2020-03-11 17:02:47 +08:00
|
|
|
return this.proxy
|
2020-03-11 06:19:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public command(name: string, ...args: any) {
|
2020-03-11 17:02:47 +08:00
|
|
|
return this.commands[name](...args)
|
2020-03-11 06:19:41 +08:00
|
|
|
}
|
|
|
|
|
2020-03-06 04:49:53 +08:00
|
|
|
private createExtensionManager() {
|
|
|
|
this.extensionManager = new ExtensionManager(this.options.extensions, this)
|
2019-12-08 07:16:44 +08:00
|
|
|
}
|
|
|
|
|
2020-03-06 04:05:01 +08:00
|
|
|
private createSchema() {
|
2020-03-06 04:49:53 +08:00
|
|
|
this.schema = new Schema({
|
2020-03-06 07:15:36 +08:00
|
|
|
topNode: this.extensionManager.topNode,
|
2020-03-06 04:05:01 +08:00
|
|
|
nodes: this.extensionManager.nodes,
|
|
|
|
marks: this.extensionManager.marks,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-06 05:15:17 +08:00
|
|
|
private get plugins() {
|
|
|
|
return [
|
|
|
|
...this.extensionManager.plugins,
|
|
|
|
keymap({ Backspace: undoInputRule }),
|
|
|
|
keymap(baseKeymap),
|
|
|
|
dropCursor(),
|
|
|
|
gapCursor(),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2019-12-08 07:16:44 +08:00
|
|
|
private createView() {
|
2020-03-09 06:25:48 +08:00
|
|
|
this.view = new EditorView(this.element, {
|
2020-03-06 04:49:53 +08:00
|
|
|
state: EditorState.create({
|
|
|
|
doc: this.createDocument(this.options.content),
|
2020-03-06 05:15:17 +08:00
|
|
|
plugins: this.plugins,
|
2020-03-06 04:49:53 +08:00
|
|
|
}),
|
2019-12-08 07:16:44 +08:00
|
|
|
dispatchTransaction: this.dispatchTransaction.bind(this),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private chainCommand = (method: Function) => (...args: any) => {
|
|
|
|
this.lastCommand = this.lastCommand
|
|
|
|
.then(() => method.apply(this, args))
|
|
|
|
.catch(console.error)
|
|
|
|
|
2020-03-11 17:02:47 +08:00
|
|
|
return this.proxy
|
2019-12-08 07:16:44 +08:00
|
|
|
}
|
|
|
|
|
2020-03-05 05:47:50 +08:00
|
|
|
private createDocument(content: EditorContent, parseOptions: any = {}): any {
|
2020-03-29 07:11:10 +08:00
|
|
|
if (content === null) {
|
|
|
|
// this.options.emptyDocument
|
|
|
|
return this.createDocument('')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof content === 'object') {
|
|
|
|
try {
|
|
|
|
return this.schema.nodeFromJSON(content)
|
|
|
|
} catch (error) {
|
|
|
|
console.warn('[tiptap warn]: Invalid content.', 'Passed value:', content, 'Error:', error)
|
|
|
|
return this.createDocument('')
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 07:16:44 +08:00
|
|
|
|
|
|
|
if (typeof content === 'string') {
|
2019-12-17 06:51:18 +08:00
|
|
|
return DOMParser
|
|
|
|
.fromSchema(this.schema)
|
2020-03-05 05:47:50 +08:00
|
|
|
.parse(elementFromString(content), parseOptions)
|
2019-12-08 07:16:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
private dispatchTransaction(transaction: any): void {
|
2020-03-05 05:40:08 +08:00
|
|
|
const state = this.state.apply(transaction)
|
|
|
|
this.view.updateState(state)
|
|
|
|
|
|
|
|
const { from, to } = this.state.selection
|
|
|
|
this.selection = { from, to }
|
2020-03-05 05:47:50 +08:00
|
|
|
|
2019-12-08 07:16:44 +08:00
|
|
|
// this.setActiveNodesAndMarks()
|
|
|
|
|
|
|
|
// this.emit('transaction', {
|
|
|
|
// getHTML: this.getHTML.bind(this),
|
|
|
|
// getJSON: this.getJSON.bind(this),
|
|
|
|
// state: this.state,
|
|
|
|
// transaction,
|
|
|
|
// })
|
|
|
|
|
|
|
|
if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// this.emitUpdate(transaction)
|
|
|
|
}
|
2019-12-17 06:51:18 +08:00
|
|
|
|
2020-03-06 04:49:53 +08:00
|
|
|
public get state() {
|
|
|
|
return this.view.state
|
|
|
|
}
|
|
|
|
|
|
|
|
public setContent(content: EditorContent = '', emitUpdate: Boolean = false, parseOptions: any = {}) {
|
|
|
|
const { doc, tr } = this.state
|
|
|
|
const document = this.createDocument(content, parseOptions)
|
|
|
|
const selection = TextSelection.create(doc, 0, doc.content.size)
|
|
|
|
const transaction = tr
|
|
|
|
.setSelection(selection)
|
|
|
|
.replaceSelectionWith(document, false)
|
|
|
|
.setMeta('preventUpdate', !emitUpdate)
|
|
|
|
|
|
|
|
this.view.dispatch(transaction)
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2020-03-09 06:25:48 +08:00
|
|
|
// public setParentComponent(component = null) {
|
|
|
|
// if (!component) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// this.view.setProps({
|
|
|
|
// nodeViews: this.initNodeViews({
|
|
|
|
// parent: component,
|
|
|
|
// extensions: [
|
|
|
|
// ...this.builtInExtensions,
|
|
|
|
// ...this.options.extensions,
|
|
|
|
// ],
|
|
|
|
// }),
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
2020-03-04 17:21:48 +08:00
|
|
|
public json() {
|
|
|
|
return this.state.doc.toJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
public html() {
|
|
|
|
const div = document.createElement('div')
|
|
|
|
const fragment = DOMSerializer
|
|
|
|
.fromSchema(this.schema)
|
|
|
|
.serializeFragment(this.state.doc.content)
|
|
|
|
|
|
|
|
div.appendChild(fragment)
|
|
|
|
|
|
|
|
return div.innerHTML
|
|
|
|
}
|
2020-03-05 04:45:49 +08:00
|
|
|
|
|
|
|
public destroy() {
|
|
|
|
if (!this.view) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.view.destroy()
|
2020-03-06 06:59:48 +08:00
|
|
|
this.removeAllListeners()
|
2020-03-05 04:45:49 +08:00
|
|
|
}
|
2019-12-08 07:16:44 +08:00
|
|
|
|
|
|
|
}
|