Skip to content

Commit f5f0cb8

Browse files
committed
Adds a willCopy hook to the editor πŸ§›β€β™€οΈ
1 parent e1d066c commit f5f0cb8

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

β€Žsrc/js/editor/editor.jsβ€Ž

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ const CALLBACK_QUEUES = {
7070
CURSOR_DID_CHANGE: 'cursorDidChange',
7171
DID_REPARSE: 'didReparse',
7272
POST_DID_CHANGE: 'postDidChange',
73-
INPUT_MODE_DID_CHANGE: 'inputModeDidChange'
73+
INPUT_MODE_DID_CHANGE: 'inputModeDidChange',
74+
WILL_COPY: 'willCopy'
7475
};
7576

7677
/**
@@ -812,6 +813,10 @@ class Editor {
812813
this.addCallback(CALLBACK_QUEUES.DID_RENDER, callback);
813814
}
814815

816+
willCopy(callback) {
817+
this.addCallback(CALLBACK_QUEUES.WILL_COPY, callback);
818+
}
819+
815820
/**
816821
* @param {Function} callback This callback will be called before deleting.
817822
* @public

β€Žsrc/js/editor/event-manager.jsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ export default class EventManager {
229229
mobiledoc: editor.serializePost(post, 'mobiledoc')
230230
};
231231

232+
editor.runCallbacks('willCopy', [data]);
233+
232234
setClipboardData(event, data, window);
233235
}
234236

β€Žtests/acceptance/editor-copy-paste-test.jsβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ test('paste plain text into an empty Mobiledoc', (assert) => {
123123
assert.hasElement('#editor p:contains(abc)', 'pastes the text');
124124
});
125125

126+
test('willCopy callback called before copy', (assert) => {
127+
const mobiledoc = Helpers.mobiledoc.build(
128+
({post, markupSection, marker}) => {
129+
return post([markupSection('p', [marker('abc')])]);
130+
});
131+
editor = new Editor({mobiledoc});
132+
editor.addCallback('willCopy', data => {
133+
assert.deepEqual(data.mobiledoc, mobiledoc);
134+
data.mobiledoc.sections[0][1] = 'blockquote';
135+
console.log({ data })
136+
});
137+
editor.render(editorElement);
138+
139+
assert.hasElement('#editor p:contains(abc)', 'precond - has p');
140+
141+
Helpers.dom.selectText(editor, 'abc', editorElement);
142+
Helpers.dom.triggerCopyEvent(editor);
143+
});
144+
126145
test('can cut and then paste content', (assert) => {
127146
const mobiledoc = Helpers.mobiledoc.build(
128147
({post, markupSection, marker}) => {

0 commit comments

Comments
Β (0)