Skip to content

Commit 2884ebf

Browse files
eguitarzbantic
authored andcommitted
feat(delete): add range, direction and unit to delete hook (#455)
1 parent 1898cf5 commit 2884ebf

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ The available lifecycle hooks are:
130130
the DOM is updated.
131131
* `editor.didRender()` - After the DOM has been updated to match the
132132
edited post.
133+
* `editor.willDelete((range, direction, unit))` - Provides `range`, `direction` and `unit` to identify the coming deletion.
134+
* `editor.didDelete((range, direction, unit))` - Provides `range`, `direction` and `unit` to identify the completed deletion.
133135
* `editor.cursorDidChange()` - When the cursor (or selection) changes as a result of arrow-key
134136
movement or clicking in the document.
135137
* `editor.onTextInput()` - When the user adds text to the document (see [example](https://github.com/bustlelabs/mobiledoc-kit#responding-to-text-input))

src/js/editor/editor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,13 @@ class Editor {
313313
performDelete({direction, unit}={direction: DIRECTION.BACKWARD, unit: 'char'}) {
314314
let { range } = this;
315315

316-
this.runCallbacks(CALLBACK_QUEUES.WILL_DELETE);
316+
this.runCallbacks(CALLBACK_QUEUES.WILL_DELETE, [range, direction, unit]);
317317
if (range.isCollapsed) {
318318
this.deleteAtPosition(range.head, direction, {unit});
319319
} else {
320320
this.deleteRange(range);
321321
}
322-
this.runCallbacks(CALLBACK_QUEUES.DID_DELETE);
322+
this.runCallbacks(CALLBACK_QUEUES.DID_DELETE, [range, direction, unit]);
323323
}
324324

325325
handleNewline(event) {

tests/acceptance/editor-sections-test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,21 @@ test('failing to delete will not trigger deleting hooks', (assert) => {
265265
});
266266

267267
test('deleting chracter triggers deleting hooks', (assert) => {
268-
assert.expect(3);
268+
assert.expect(9);
269269
let lifeCycles = [];
270270

271271
editor = new Editor({mobiledoc: mobileDocWith2Sections});
272-
editor.willDelete(() => {
272+
editor.willDelete((range, direction, unit) => {
273+
assert.ok(range, 'range is not empty');
274+
assert.equal(direction, -1, 'direction defaults to -1');
275+
assert.equal(unit, 'char', 'unit defaults to char');
273276
assert.ok(true, 'willDelete is triggered');
274277
lifeCycles.push('willDelete');
275278
});
276-
editor.didDelete(() => {
279+
editor.didDelete((range, direction, unit) => {
280+
assert.ok(range, 'range is not empty');
281+
assert.equal(direction, -1, 'direction defaults to -1');
282+
assert.equal(unit, 'char', 'unit defaults to char');
277283
assert.ok(true, 'didDelete is triggered');
278284
lifeCycles.push('didDelete');
279285
});

0 commit comments

Comments
 (0)