Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added delete hooks in lifecycle #454

Merged
merged 1 commit into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const CALLBACK_QUEUES = {
DID_UPDATE: 'didUpdate',
WILL_RENDER: 'willRender',
DID_RENDER: 'didRender',
WILL_DELETE: 'willDelete',
DID_DELETE: 'didDelete',
CURSOR_DID_CHANGE: 'cursorDidChange',
DID_REPARSE: 'didReparse',
POST_DID_CHANGE: 'postDidChange',
Expand Down Expand Up @@ -311,11 +313,13 @@ class Editor {
performDelete({direction, unit}={direction: DIRECTION.BACKWARD, unit: 'char'}) {
let { range } = this;

this.runCallbacks(CALLBACK_QUEUES.WILL_DELETE);
if (range.isCollapsed) {
this.deleteAtPosition(range.head, direction, {unit});
} else {
this.deleteRange(range);
}
this.runCallbacks(CALLBACK_QUEUES.DID_DELETE);
}

handleNewline(event) {
Expand Down Expand Up @@ -748,6 +752,22 @@ class Editor {
this.addCallback(CALLBACK_QUEUES.DID_RENDER, callback);
}

/**
* @param {Function} callback This callback will be called before deleting.
* @public
*/
willDelete(callback) {
this.addCallback(CALLBACK_QUEUES.WILL_DELETE, callback);
}

/**
* @param {Function} callback This callback will be called after deleting.
* @public
*/
didDelete(callback) {
this.addCallback(CALLBACK_QUEUES.DID_DELETE, callback);
}

/**
* @param {Function} callback This callback will be called every time the cursor
* position (or selection) changes.
Expand Down
34 changes: 34 additions & 0 deletions tests/acceptance/editor-sections-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,40 @@ test('deleting across 1 section removes it, joins the 2 boundary sections', (ass
'remaining paragraph has correct text');
});

test('failing to delete will not trigger deleting hooks', (assert) => {
assert.expect(0);
editor = new Editor({mobiledoc: mobileDocWith2Sections});
editor.willDelete(() => {
assert.ok(false, 'willDelete should not be triggered');
});
editor.didDelete(() => {
assert.ok(false, 'didDelete should not be triggered');
});

editor.render(editorElement);
editor.disableEditing();
Helpers.dom.triggerDelete(editor);
});

test('deleting chracter triggers deleting hooks', (assert) => {
assert.expect(3);
let lifeCycles = [];

editor = new Editor({mobiledoc: mobileDocWith2Sections});
editor.willDelete(() => {
assert.ok(true, 'willDelete is triggered');
lifeCycles.push('willDelete');
});
editor.didDelete(() => {
assert.ok(true, 'didDelete is triggered');
lifeCycles.push('didDelete');
});
editor.render(editorElement);

Helpers.dom.triggerDelete(editor);
assert.deepEqual(lifeCycles, ['willDelete', 'didDelete'], 'hooks are triggered in order');
});

test('keystroke of delete removes that character', (assert) => {
editor = new Editor({mobiledoc: mobileDocWith3Sections});
editor.render(editorElement);
Expand Down