2020-04-11 03:43:23 +08:00
|
|
|
import { EditorState, Plugin } from 'prosemirror-state'
|
2020-03-06 06:59:48 +08:00
|
|
|
import { EditorView} from 'prosemirror-view'
|
|
|
|
import { Schema, DOMParser, DOMSerializer } from 'prosemirror-model'
|
2020-04-02 15:42:26 +08:00
|
|
|
import { undoInputRule } from 'prosemirror-inputrules'
|
2020-03-06 05:15:17 +08:00
|
|
|
import { keymap } from 'prosemirror-keymap'
|
|
|
|
import { baseKeymap } from 'prosemirror-commands'
|
|
|
|
import { dropCursor } from 'prosemirror-dropcursor'
|
|
|
|
import { gapCursor } from 'prosemirror-gapcursor'
|
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-30 02:36:57 +08:00
|
|
|
import getAllMethodNames from './utils/getAllMethodNames'
|
2020-03-30 17:25:07 +08:00
|
|
|
import nodeIsActive from './utils/nodeIsActive'
|
2020-03-30 20:42:10 +08:00
|
|
|
import markIsActive from './utils/markIsActive'
|
2020-04-11 17:45:41 +08:00
|
|
|
import getNodeAttrs from './utils/getNodeAttrs'
|
2020-03-30 20:42:10 +08:00
|
|
|
import getMarkAttrs from './utils/getMarkAttrs'
|
2020-03-31 19:06:34 +08:00
|
|
|
import removeElement from './utils/removeElement'
|
2020-03-31 18:53:52 +08:00
|
|
|
import getSchemaTypeByName from './utils/getSchemaTypeByName'
|
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-04-11 04:59:09 +08:00
|
|
|
import Mark from './Mark'
|
2020-04-02 03:15:23 +08:00
|
|
|
import EventEmitter from './EventEmitter'
|
2020-08-18 16:06:08 +08:00
|
|
|
import ComponentRenderer from './ComponentRenderer'
|
2020-03-06 04:05:01 +08:00
|
|
|
|
2020-08-21 05:25:55 +08:00
|
|
|
import clearContent from './commands/clearContent'
|
|
|
|
import deleteSelection from './commands/deleteSelection'
|
|
|
|
import focus from './commands/focus'
|
|
|
|
import insertHTML from './commands/insertHTML'
|
|
|
|
import insertText from './commands/insertText'
|
|
|
|
import removeMark from './commands/removeMark'
|
|
|
|
import removeMarks from './commands/removeMarks'
|
|
|
|
import replaceWithNode from './commands/replaceWithNode'
|
|
|
|
import selectAll from './commands/selectAll'
|
|
|
|
import selectParentNode from './commands/selectParentNode'
|
|
|
|
import setContent from './commands/setContent'
|
|
|
|
import toggleMark from './commands/toggleMark'
|
|
|
|
import toggleNode from './commands/toggleNode'
|
|
|
|
import updateMark from './commands/updateMark'
|
|
|
|
|
2020-04-22 05:22:27 +08:00
|
|
|
export type Command = (next: Function, editor: Editor) => (...args: any) => any
|
2020-04-11 04:59:09 +08:00
|
|
|
|
2020-04-02 20:34:07 +08:00
|
|
|
export interface CommandSpec {
|
|
|
|
[key: string]: Command
|
|
|
|
}
|
2019-12-17 06:20:05 +08:00
|
|
|
|
2020-04-11 04:59:09 +08:00
|
|
|
type EditorContent = string | JSON | null
|
|
|
|
|
2020-04-14 16:13:27 +08:00
|
|
|
interface EditorOptions {
|
2020-04-27 05:21:27 +08:00
|
|
|
element: Element,
|
2020-04-14 16:13:27 +08:00
|
|
|
content: EditorContent
|
|
|
|
extensions: (Extension | Node | Mark)[]
|
|
|
|
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-08-18 16:06:08 +08:00
|
|
|
public renderer!: any
|
|
|
|
private proxy!: Editor
|
2020-08-18 15:36:37 +08:00
|
|
|
private extensionManager!: ExtensionManager
|
|
|
|
private commands: { [key: string]: any } = {}
|
|
|
|
private css!: HTMLStyleElement
|
|
|
|
private lastCommand = Promise.resolve()
|
|
|
|
public schema!: Schema
|
|
|
|
public view!: EditorView
|
|
|
|
public selection = { from: 0, to: 0 }
|
2020-08-18 15:41:31 +08:00
|
|
|
public options: EditorOptions = {
|
2020-04-11 20:33:58 +08:00
|
|
|
element: document.createElement('div'),
|
2020-03-05 05:40:08 +08:00
|
|
|
content: '',
|
|
|
|
injectCSS: true,
|
2020-03-06 03:30:58 +08:00
|
|
|
extensions: [],
|
2020-03-05 05:40:08 +08:00
|
|
|
}
|
2020-08-11 22:57:11 +08:00
|
|
|
|
2020-04-14 16:13:27 +08:00
|
|
|
constructor(options: Partial<EditorOptions> = {}) {
|
2020-03-06 06:59:48 +08:00
|
|
|
super()
|
2020-03-05 05:40:08 +08:00
|
|
|
this.options = { ...this.options, ...options }
|
2020-04-01 04:17:54 +08:00
|
|
|
}
|
2020-08-11 22:57:11 +08:00
|
|
|
|
2020-04-01 04:17:54 +08:00
|
|
|
private init() {
|
2020-03-06 04:49:53 +08:00
|
|
|
this.createExtensionManager()
|
|
|
|
this.createSchema()
|
|
|
|
this.createView()
|
2020-08-21 05:25:55 +08:00
|
|
|
this.registerCommand('clearContent', clearContent)
|
|
|
|
this.registerCommand('deleteSelection', deleteSelection)
|
|
|
|
this.registerCommand('focus', focus)
|
|
|
|
this.registerCommand('insertHTML', insertHTML)
|
|
|
|
this.registerCommand('insertText', insertText)
|
|
|
|
this.registerCommand('removeMark', removeMark)
|
|
|
|
this.registerCommand('removeMarks', removeMarks)
|
|
|
|
this.registerCommand('replaceWithNode', replaceWithNode)
|
|
|
|
this.registerCommand('selectAll', selectAll)
|
|
|
|
this.registerCommand('selectParentNode', selectParentNode)
|
|
|
|
this.registerCommand('setContent', setContent)
|
|
|
|
this.registerCommand('toggleMark', toggleMark)
|
|
|
|
this.registerCommand('toggleNode', toggleNode)
|
|
|
|
this.registerCommand('updateMark', updateMark)
|
2020-08-11 22:57:11 +08:00
|
|
|
|
2020-03-05 05:40:08 +08:00
|
|
|
if (this.options.injectCSS) {
|
2020-04-11 05:09:31 +08:00
|
|
|
require('./style.css')
|
2020-03-05 05:40:08 +08:00
|
|
|
}
|
2019-12-08 07:16:44 +08:00
|
|
|
}
|
|
|
|
|
2020-08-18 15:36:37 +08:00
|
|
|
private __get(name: string) {
|
2020-03-11 17:02:47 +08:00
|
|
|
const command = this.commands[name]
|
|
|
|
|
|
|
|
if (!command) {
|
2020-04-02 03:15:23 +08:00
|
|
|
// TODO: prevent vue devtools to throw error
|
|
|
|
// throw new Error(`tiptap: command '${name}' not found.`)
|
|
|
|
return
|
2020-03-11 17:02:47 +08:00
|
|
|
}
|
2020-08-11 22:57:11 +08:00
|
|
|
|
2020-03-11 17:02:47 +08:00
|
|
|
return (...args: any) => command(...args)
|
|
|
|
}
|
|
|
|
|
2020-03-29 07:21:28 +08:00
|
|
|
public get state() {
|
|
|
|
return this.view.state
|
|
|
|
}
|
|
|
|
|
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-08-11 22:57:11 +08:00
|
|
|
|
2020-03-30 02:36:57 +08:00
|
|
|
if (getAllMethodNames(this).includes(name)) {
|
|
|
|
throw new Error(`tiptap: '${name}' is a protected name.`)
|
|
|
|
}
|
2020-08-11 22:57:11 +08:00
|
|
|
|
2020-03-11 17:02:47 +08:00
|
|
|
this.commands[name] = this.chainCommand((...args: any) => {
|
2020-04-22 05:22:27 +08:00
|
|
|
return new Promise(resolve => callback(resolve, this.proxy)(...args))
|
2020-03-11 06:19:41 +08:00
|
|
|
})
|
|
|
|
|
2020-03-11 17:02:47 +08:00
|
|
|
return this.proxy
|
2020-03-11 06:19:41 +08:00
|
|
|
}
|
|
|
|
|
2020-04-13 02:17:56 +08:00
|
|
|
public registerPlugin(plugin: Plugin, handlePlugins?: (plugin: Plugin, plugins: Plugin[]) => Plugin[]) {
|
|
|
|
const plugins = typeof handlePlugins === 'function'
|
|
|
|
? handlePlugins(plugin, this.state.plugins)
|
|
|
|
: [plugin, ...this.state.plugins]
|
2020-04-11 03:43:23 +08:00
|
|
|
|
2020-04-13 02:17:56 +08:00
|
|
|
const state = this.state.reconfigure({ plugins })
|
2020-04-11 03:43:23 +08:00
|
|
|
|
|
|
|
this.view.updateState(state)
|
|
|
|
}
|
|
|
|
|
2020-04-11 04:55:14 +08:00
|
|
|
public unregisterPlugin(name: string) {
|
|
|
|
const state = this.state.reconfigure({
|
|
|
|
// @ts-ignore
|
|
|
|
plugins: this.state.plugins.filter(plugin => !plugin.key.startsWith(`${name}$`)),
|
|
|
|
})
|
|
|
|
|
|
|
|
this.view.updateState(state)
|
|
|
|
}
|
|
|
|
|
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() {
|
2020-04-01 04:17:54 +08:00
|
|
|
this.extensionManager = new ExtensionManager(this.options.extensions, this.proxy)
|
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-30 18:40:25 +08:00
|
|
|
this.emit('schemaCreated')
|
2020-03-06 04:05:01 +08:00
|
|
|
}
|
|
|
|
|
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-04-11 20:33:58 +08:00
|
|
|
this.view = new EditorView(this.options.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),
|
2020-04-24 15:32:37 +08:00
|
|
|
nodeViews: this.extensionManager.nodeViews,
|
2019-12-08 07:16:44 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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-30 04:24:51 +08:00
|
|
|
public createDocument = (content: EditorContent, parseOptions: any = {}): any => {
|
2020-03-29 07:21:28 +08:00
|
|
|
if (content && typeof content === 'object') {
|
2020-03-29 07:11:10 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-29 07:21:28 +08:00
|
|
|
return this.createDocument('')
|
|
|
|
}
|
|
|
|
|
|
|
|
private storeSelection() {
|
|
|
|
const { from, to } = this.state.selection
|
|
|
|
this.selection = { from, to }
|
2019-12-08 07:16:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private dispatchTransaction(transaction: any): void {
|
2020-03-05 05:40:08 +08:00
|
|
|
const state = this.state.apply(transaction)
|
|
|
|
this.view.updateState(state)
|
2020-03-29 07:21:28 +08:00
|
|
|
this.storeSelection()
|
2020-03-30 03:04:56 +08:00
|
|
|
this.emit('transaction', { transaction })
|
2020-08-11 22:57:11 +08:00
|
|
|
|
2019-12-08 07:16:44 +08:00
|
|
|
if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-30 03:04:56 +08:00
|
|
|
this.emit('update', { transaction })
|
2019-12-08 07:16:44 +08:00
|
|
|
}
|
2019-12-17 06:51:18 +08:00
|
|
|
|
2020-04-11 17:45:41 +08:00
|
|
|
public getNodeAttrs(name: string) {
|
|
|
|
return getNodeAttrs(this.state, this.schema.nodes[name])
|
|
|
|
}
|
|
|
|
|
2020-04-02 03:41:53 +08:00
|
|
|
public getMarkAttrs(name: string) {
|
2020-03-31 18:53:52 +08:00
|
|
|
return getMarkAttrs(this.state, this.schema.marks[name])
|
|
|
|
}
|
2020-03-30 20:49:48 +08:00
|
|
|
|
2020-04-02 03:41:53 +08:00
|
|
|
public isActive(name: string, attrs = {}) {
|
2020-03-31 18:53:52 +08:00
|
|
|
const schemaType = getSchemaTypeByName(name, this.schema)
|
2020-03-30 20:49:48 +08:00
|
|
|
|
2020-03-31 18:53:52 +08:00
|
|
|
if (schemaType === 'node') {
|
|
|
|
return nodeIsActive(this.state, this.schema.nodes[name], attrs)
|
|
|
|
} else if (schemaType === 'mark') {
|
|
|
|
return markIsActive(this.state, this.schema.marks[name])
|
|
|
|
}
|
2020-03-30 20:42:10 +08:00
|
|
|
|
2020-03-31 18:53:52 +08:00
|
|
|
return false
|
2020-03-06 04:49:53 +08:00
|
|
|
}
|
|
|
|
|
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() {
|
2020-03-31 19:07:57 +08:00
|
|
|
if (this.view) {
|
|
|
|
this.view.destroy()
|
2020-03-05 04:45:49 +08:00
|
|
|
}
|
|
|
|
|
2020-03-06 06:59:48 +08:00
|
|
|
this.removeAllListeners()
|
2020-03-31 19:06:34 +08:00
|
|
|
removeElement(this.css)
|
2020-03-05 04:45:49 +08:00
|
|
|
}
|
2020-08-11 22:57:11 +08:00
|
|
|
|
2019-12-08 07:16:44 +08:00
|
|
|
}
|