From 7768fa5625274c03c2a62bd284d696951c5bcd19 Mon Sep 17 00:00:00 2001 From: Kameron Niksefat Date: Thu, 19 May 2022 09:12:47 -0400 Subject: [PATCH] add willPaste event --- README.md | 2 ++ src/js/editor/editor.ts | 9 ++++++++ src/js/editor/event-manager.ts | 2 ++ tests/acceptance/editor-copy-paste-test.js | 24 ++++++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/README.md b/README.md index 3e50f8b4b..c727454e1 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,8 @@ The available lifecycle hooks are: * `editor.willCopy(({html, text, mobiledoc}) => {...})` - Called before the serialized versions of the selected markup is copied to the system pasteboard. + `editor.willPaste(({html, text, mobiledoc}) => {...})` - Called before the + serialized versions of the system pasteboard is pasted into the mobiledoc. For more details on the lifecycle hooks, see the [Editor documentation](https://bustle.github.io/mobiledoc-kit/demo/docs/Editor.html). diff --git a/src/js/editor/editor.ts b/src/js/editor/editor.ts index 22608c5f2..8d9471e93 100644 --- a/src/js/editor/editor.ts +++ b/src/js/editor/editor.ts @@ -109,6 +109,7 @@ const CALLBACK_QUEUES = { POST_DID_CHANGE: 'postDidChange', INPUT_MODE_DID_CHANGE: 'inputModeDidChange', WILL_COPY: 'willCopy', + WILL_PASTE: 'willPaster', } export enum Format { @@ -961,6 +962,14 @@ export default class Editor implements EditorOptions { this.addCallback(CALLBACK_QUEUES.WILL_COPY, callback) } + /** + * @param {Function} callback This callback will be called before pasting. + * @public + */ + willPaste(callback: LifecycleCallback) { + this.addCallback(CALLBACK_QUEUES.WILL_PASTE, callback) + } + /** * @param {Function} callback This callback will be called before deleting. * @public diff --git a/src/js/editor/event-manager.ts b/src/js/editor/event-manager.ts index 70c28c01b..3ee3518ab 100644 --- a/src/js/editor/event-manager.ts +++ b/src/js/editor/event-manager.ts @@ -369,6 +369,8 @@ export default class EventManager { let targetFormat = this.modifierKeys.shift ? 'text' : 'html' let pastedPost = parsePostFromPaste(event, editor, { targetFormat }) + editor.runCallbacks('willPaste', [pastedPost]) + editor.run(postEditor => { let nextPosition = postEditor.insertPost(position, pastedPost!) postEditor.setRange(nextPosition) diff --git a/tests/acceptance/editor-copy-paste-test.js b/tests/acceptance/editor-copy-paste-test.js index 2562e00ca..7a9be0430 100644 --- a/tests/acceptance/editor-copy-paste-test.js +++ b/tests/acceptance/editor-copy-paste-test.js @@ -141,6 +141,30 @@ test('willCopy callback called before copy', (assert) => { Helpers.dom.triggerCopyEvent(editor); }); +test('willPaste callback called before paste', assert => { + const mobiledoc = Helpers.mobiledoc.build(({ post, markupSection, marker, markup }) => { + return post([markupSection('p', [marker('a', [markup('strong')]), marker('bc')])]) + }) + editor = new Editor({ mobiledoc }) + editor.addCallback('willPaste', post => { + assert.equal(post.sections.head.text, 'bc') + }) + editor.render(editorElement) + + Helpers.dom.selectText(editor, 'a', editorElement, 'b', editorElement) + Helpers.dom.triggerCopyEvent(editor) + + let textNode = $('#editor p')[0].childNodes[1] + assert.equal(textNode.textContent, 'bc') //precond + Helpers.dom.moveCursorTo(editor, textNode, textNode.length) + + Helpers.dom.triggerPasteEvent(editor) + + assert.hasElement('#editor p:contains(abcab)', 'pastes the text') + assert.equal($('#editor p strong:contains(a)').length, 2, 'two bold As') +}) + + test('can cut and then paste content', (assert) => { const mobiledoc = Helpers.mobiledoc.build( ({post, markupSection, marker}) => {