From cd46b163d059260a5643bdd253bb1a3d82fdc020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Sat, 4 May 2019 00:05:39 +0200 Subject: [PATCH] add emitter, move some collab logic to extension --- babel.config.js | 1 + .../Components/Routes/Collaboration/Collab.js | 21 ++++++- .../Components/Routes/Collaboration/index.vue | 32 +++++----- package.json | 1 + packages/tiptap/src/Editor.js | 42 ++++++++----- packages/tiptap/src/Utils/Emitter.js | 59 +++++++++++++++++++ packages/tiptap/src/Utils/Extension.js | 8 +++ packages/tiptap/src/Utils/ExtensionManager.js | 6 +- packages/tiptap/src/Utils/camelCase.js | 3 + packages/tiptap/src/Utils/index.js | 2 + yarn.lock | 51 ++++++++++------ 11 files changed, 173 insertions(+), 53 deletions(-) create mode 100644 packages/tiptap/src/Utils/Emitter.js create mode 100644 packages/tiptap/src/Utils/camelCase.js diff --git a/babel.config.js b/babel.config.js index 9f8a0a192..ebc1a2539 100644 --- a/babel.config.js +++ b/babel.config.js @@ -4,5 +4,6 @@ module.exports = { ], plugins: [ '@babel/plugin-syntax-dynamic-import', + '@babel/plugin-proposal-class-properties', ], } diff --git a/examples/Components/Routes/Collaboration/Collab.js b/examples/Components/Routes/Collaboration/Collab.js index 21dc7255f..a59328b25 100644 --- a/examples/Components/Routes/Collaboration/Collab.js +++ b/examples/Components/Routes/Collaboration/Collab.js @@ -1,15 +1,25 @@ import { Extension } from 'tiptap' -import { collab } from 'prosemirror-collab' +import { collab, sendableSteps } from 'prosemirror-collab' +import { debounce } from 'lodash-es' export default class CollabExtension extends Extension { + get name() { return 'collab' } + init() { + this.editor.on('update', ({ state }) => { + this.getSendableSteps(state) + }) + } + get defaultOptions() { return { version: 0, clientID: Math.floor(Math.random() * 0xFFFFFFFF), + debounce: 250, + onSend: () => {}, } } @@ -21,4 +31,13 @@ export default class CollabExtension extends Extension { }), ] } + + getSendableSteps = debounce(state => { + const sendable = sendableSteps(state) + + if (sendable) { + this.options.onSend(sendable) + } + }, this.options.debounce) + } diff --git a/examples/Components/Routes/Collaboration/index.vue b/examples/Components/Routes/Collaboration/index.vue index 6ac6073d2..f7adc7cb4 100644 --- a/examples/Components/Routes/Collaboration/index.vue +++ b/examples/Components/Routes/Collaboration/index.vue @@ -9,10 +9,9 @@