Skip to content

Commit

Permalink
Adds a willCopy hook to the editor 🧛‍♀️
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeeJab committed May 22, 2020
1 parent e1d066c commit f5f0cb8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const CALLBACK_QUEUES = {
CURSOR_DID_CHANGE: 'cursorDidChange',
DID_REPARSE: 'didReparse',
POST_DID_CHANGE: 'postDidChange',
INPUT_MODE_DID_CHANGE: 'inputModeDidChange'
INPUT_MODE_DID_CHANGE: 'inputModeDidChange',
WILL_COPY: 'willCopy'
};

/**
Expand Down Expand Up @@ -812,6 +813,10 @@ class Editor {
this.addCallback(CALLBACK_QUEUES.DID_RENDER, callback);
}

willCopy(callback) {
this.addCallback(CALLBACK_QUEUES.WILL_COPY, callback);
}

/**
* @param {Function} callback This callback will be called before deleting.
* @public
Expand Down
2 changes: 2 additions & 0 deletions src/js/editor/event-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ export default class EventManager {
mobiledoc: editor.serializePost(post, 'mobiledoc')
};

editor.runCallbacks('willCopy', [data]);

setClipboardData(event, data, window);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/acceptance/editor-copy-paste-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ test('paste plain text into an empty Mobiledoc', (assert) => {
assert.hasElement('#editor p:contains(abc)', 'pastes the text');
});

test('willCopy callback called before copy', (assert) => {
const mobiledoc = Helpers.mobiledoc.build(
({post, markupSection, marker}) => {
return post([markupSection('p', [marker('abc')])]);
});
editor = new Editor({mobiledoc});
editor.addCallback('willCopy', data => {
assert.deepEqual(data.mobiledoc, mobiledoc);
data.mobiledoc.sections[0][1] = 'blockquote';
console.log({ data })
});
editor.render(editorElement);

assert.hasElement('#editor p:contains(abc)', 'precond - has p');

Helpers.dom.selectText(editor, 'abc', editorElement);
Helpers.dom.triggerCopyEvent(editor);
});

test('can cut and then paste content', (assert) => {
const mobiledoc = Helpers.mobiledoc.build(
({post, markupSection, marker}) => {
Expand Down

0 comments on commit f5f0cb8

Please sign in to comment.