Skip to content

Commit

Permalink
add willPaste event
Browse files Browse the repository at this point in the history
  • Loading branch information
nikse committed May 19, 2022
1 parent 3b6438d commit 7768fa5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
9 changes: 9 additions & 0 deletions src/js/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/js/editor/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions tests/acceptance/editor-copy-paste-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {
Expand Down

0 comments on commit 7768fa5

Please sign in to comment.