Skip to content

Commit 2effba1

Browse files
authored
🧛‍♀️Merge pull request #724 from bustle/zeejab/willcopy
Adds `willCopy` hook to the editor 🧛‍♀️
2 parents e1d066c + be4324e commit 2effba1

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,14 @@ The available lifecycle hooks are:
225225
movement or clicking in the document.
226226
* `editor.onTextInput()` - When the user adds text to the document (see [example](https://github.com/bustlelabs/mobiledoc-kit#responding-to-text-input))
227227
* `editor.inputModeDidChange()` - The active section(s) or markup(s) at the current cursor position or selection have changed. This hook can be used with `Editor#activeMarkups`, `Editor#activeSections`, and `Editor#activeSectionAttributes` to implement a custom toolbar.
228-
* `editor.beforeToggleMarkup(({markup, range, willAdd} => {...})` - Register a
228+
* `editor.beforeToggleMarkup(({markup, range, willAdd}) => {...})` - Register a
229229
callback that will be called before `editor#toggleMarkup` is applied. If any
230230
callback returns literal `false`, the toggling of markup will be canceled.
231231
(Toggling markup done via the postEditor, e.g. `editor.run(postEditor =>
232232
postEditor.toggleMarkup(...))` will skip this callback.
233+
* `editor.willCopy(({html, text, mobiledoc}) => {...})` - Called before the
234+
serialized versions of the selected markup is copied to the system
235+
pasteboard.
233236

234237
For more details on the lifecycle hooks, see the [Editor documentation](https://bustle.github.io/mobiledoc-kit/demo/docs/Editor.html).
235238

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)