From 290442829d1ffbb84365407954d4f49ba64440f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 14 Nov 2018 10:45:13 +0100 Subject: [PATCH] refactoring --- .../tiptap/src/Components/EditorContent.js | 2 +- packages/tiptap/src/index.js | 10 ++- packages/tiptap/src/utils/ComponentView.js | 2 + packages/tiptap/src/utils/Editor.js | 78 ++++++++++--------- packages/tiptap/src/utils/builtInKeymap.js | 3 +- packages/tiptap/src/utils/plugin.js | 3 - packages/tiptap/test/Editor.spec.js | 2 +- 7 files changed, 52 insertions(+), 48 deletions(-) delete mode 100644 packages/tiptap/src/utils/plugin.js diff --git a/packages/tiptap/src/Components/EditorContent.js b/packages/tiptap/src/Components/EditorContent.js index 1ae5a5b9..527bebab 100644 --- a/packages/tiptap/src/Components/EditorContent.js +++ b/packages/tiptap/src/Components/EditorContent.js @@ -14,7 +14,7 @@ export default { if (editor.element) { this.$nextTick(() => { this.$el.append(editor.element.firstChild) - editor.setParent(this) + editor.setParentComponent(this) }) } }, diff --git a/packages/tiptap/src/index.js b/packages/tiptap/src/index.js index cea5e362..60305026 100644 --- a/packages/tiptap/src/index.js +++ b/packages/tiptap/src/index.js @@ -1,10 +1,12 @@ export { default as Editor } from './Utils/Editor' +export { default as Extension } from './Utils/Extension' +export { default as Node } from './Utils/Node' +export { default as Mark } from './Utils/Mark' + export { default as EditorContent } from './Components/EditorContent' export { default as EditorMenuBar } from './Components/EditorMenuBar' export { default as EditorMenuBubble } from './Components/EditorMenuBubble' export { default as EditorFloatingMenu } from './Components/EditorFloatingMenu' -export { default as Extension } from './Utils/Extension' -export { default as Node } from './Utils/Node' -export { default as Mark } from './Utils/Mark' -export { default as Plugin } from './Utils/Plugin' +export { Plugin } from 'prosemirror-state' +export { PluginKey } from 'prosemirror-state' diff --git a/packages/tiptap/src/utils/ComponentView.js b/packages/tiptap/src/utils/ComponentView.js index cc9b27fe..d0bffca2 100644 --- a/packages/tiptap/src/utils/ComponentView.js +++ b/packages/tiptap/src/utils/ComponentView.js @@ -1,6 +1,7 @@ import Vue from 'vue' export default class ComponentView { + constructor(component, { extension, parent, @@ -94,4 +95,5 @@ export default class ComponentView { destroy() { this.vm.$destroy() } + } diff --git a/packages/tiptap/src/utils/Editor.js b/packages/tiptap/src/utils/Editor.js index a26e8c75..11b2652a 100644 --- a/packages/tiptap/src/utils/Editor.js +++ b/packages/tiptap/src/utils/Editor.js @@ -18,8 +18,27 @@ import builtInNodes from '../Nodes' export default class Editor { constructor(options = {}) { + this.init(options) + } + + init(options = {}) { this.setOptions(options) - this.init() + this.element = document.createElement('div') + this.extensions = this.createExtensions() + this.nodes = this.createNodes() + this.marks = this.createMarks() + this.schema = this.createSchema() + this.plugins = this.createPlugins() + this.keymaps = this.createKeymaps() + this.inputRules = this.createInputRules() + this.state = this.createState() + this.view = this.createView() + this.commands = this.createCommands() + this.setActiveNodesAndMarks() + this.options.onInit({ + view: this.view, + state: this.state, + }) } setOptions(options) { @@ -39,25 +58,6 @@ export default class Editor { } } - init() { - this.element = document.createElement('div') - this.extensions = this.createExtensions() - this.nodes = this.createNodes() - this.marks = this.createMarks() - this.schema = this.createSchema() - this.plugins = this.createPlugins() - this.keymaps = this.createKeymaps() - this.inputRules = this.createInputRules() - this.state = this.createState() - this.view = this.createView() - this.commands = this.createCommands() - this.setActiveNodesAndMarks() - this.options.onInit({ - view: this.view, - state: this.state, - }) - } - createExtensions() { return new ExtensionManager([ ...builtInNodes, @@ -169,7 +169,7 @@ export default class Editor { return view } - setParent(component = null) { + setParentComponent(component = null) { if (!component) { return } @@ -206,6 +206,10 @@ export default class Editor { }) } + focus() { + this.view.focus() + } + getHTML() { const div = document.createElement('div') const fragment = DOMSerializer @@ -267,19 +271,6 @@ export default class Editor { }), {}) } - focus() { - this.view.focus() - } - - registerPlugin(plugin = null) { - if (plugin) { - this.state = this.state.reconfigure({ - plugins: this.state.plugins.concat([plugin]), - }) - this.view.updateState(this.state) - } - } - getMarkAttrs(type = null) { return this.activeMarkAttrs[type] } @@ -296,10 +287,23 @@ export default class Editor { }), {}) } - destroy() { - if (this.view) { - this.view.destroy() + registerPlugin(plugin = null) { + if (!plugin) { + return } + + this.state = this.state.reconfigure({ + plugins: this.state.plugins.concat([plugin]), + }) + this.view.updateState(this.state) + } + + destroy() { + if (!this.view) { + return + } + + this.view.destroy() } } diff --git a/packages/tiptap/src/utils/builtInKeymap.js b/packages/tiptap/src/utils/builtInKeymap.js index 9a323e6e..23919b10 100644 --- a/packages/tiptap/src/utils/builtInKeymap.js +++ b/packages/tiptap/src/utils/builtInKeymap.js @@ -1,8 +1,7 @@ -import { lift, selectParentNode } from 'prosemirror-commands' +import { selectParentNode } from 'prosemirror-commands' import { undoInputRule } from 'prosemirror-inputrules' const keymap = { - 'Mod-BracketLeft': lift, Backspace: undoInputRule, Escape: selectParentNode, } diff --git a/packages/tiptap/src/utils/plugin.js b/packages/tiptap/src/utils/plugin.js deleted file mode 100644 index 61f42341..00000000 --- a/packages/tiptap/src/utils/plugin.js +++ /dev/null @@ -1,3 +0,0 @@ -import { Plugin } from 'prosemirror-state' - -export default Plugin diff --git a/packages/tiptap/test/Editor.spec.js b/packages/tiptap/test/Editor.spec.js index fef31a0b..ae5c44d3 100644 --- a/packages/tiptap/test/Editor.spec.js +++ b/packages/tiptap/test/Editor.spec.js @@ -19,7 +19,7 @@ import { History, } from '../../tiptap-extensions' -test('can create editor', () => { +test('create editor', () => { const editor = new Editor() expect(editor).toBeDefined()