Skip to content

Commit

Permalink
fix slab#746 and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen authored and Tim McClure committed Dec 12, 2016
1 parent 059bcd7 commit 97d3f22
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
9 changes: 6 additions & 3 deletions core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import extend from 'extend';


class Editor {
constructor(scroll, emitter) {
constructor(scroll, emitter, selection) {
this.scroll = scroll;
this.selection = selection;
this.emitter = emitter;
this.emitter.on(Emitter.events.SCROLL_UPDATE, this.update.bind(this, null));
this.delta = this.getDelta();
Expand Down Expand Up @@ -194,6 +195,8 @@ class Editor {

update(change, source = Emitter.sources.USER, mutations = []) {
let oldDelta = this.delta;
let range = this.selection.lastRange;
let index = range && range.length === 0 ? range.index : undefined;
if (mutations.length === 1 &&
mutations[0].type === 'characterData' &&
Parchment.find(mutations[0].target)) {
Expand All @@ -204,7 +207,7 @@ class Editor {
let oldValue = mutations[0].oldValue.replace(CursorBlot.CONTENTS, '');
let oldText = new Delta().insert(oldValue);
let newText = new Delta().insert(textBlot.value());
let diffDelta = new Delta().retain(index).concat(oldText.diff(newText));
let diffDelta = new Delta().retain(index).concat(oldText.diff(newText, index));
change = diffDelta.reduce(function(delta, op) {
if (op.insert) {
return delta.insert(op.insert, formats);
Expand All @@ -216,7 +219,7 @@ class Editor {
} else {
this.delta = this.getDelta();
if (!change || !equal(oldDelta.compose(change), this.delta)) {
change = oldDelta.diff(this.delta);
change = oldDelta.diff(this.delta, index);
}
}
if (change.length() > 0) {
Expand Down
2 changes: 1 addition & 1 deletion core/quill.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class Quill {
emitter: this.emitter,
whitelist: this.options.formats
});
this.editor = new Editor(this.scroll, this.emitter);
this.selection = new Selection(this.scroll, this.emitter);
this.editor = new Editor(this.scroll, this.emitter, this.selection);
this.theme = new this.options.theme(this, this.options);
this.keyboard = this.theme.addModule('keyboard');
this.clipboard = this.theme.addModule('clipboard');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"eventemitter3": "~2.0.1",
"extend": "~3.0.0",
"parchment": "1.0.2",
"quill-delta": "3.3.0"
"quill-delta": "3.4.0"
},
"devDependencies": {
"babel-core": "^6.16.0",
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ function initialize(klass, html, container = this.container) {
let emitter = new Emitter();
let scroll = new Scroll(container, { emitter: emitter });
if (klass === Scroll) return scroll;
let editor = new Editor(scroll, emitter);
if (klass === Editor) return editor;
let selection = new Selection(scroll, emitter);
let editor = new Editor(scroll, emitter, selection);
if (klass === Selection) return selection;
if (klass === Editor) return editor;
if (klass[0] === Editor && klass[1] === Selection) return [editor, selection];
}
39 changes: 28 additions & 11 deletions test/unit/core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,25 +444,42 @@ describe('Editor', function() {
});

describe('events', function() {
beforeEach(function() {
this.editor = this.initialize(Editor, '<p>0123</p>');
this.editor.update();
spyOn(this.editor.emitter, 'emit').and.callThrough();
});

it('api text insert', function() {
let old = this.editor.getDelta();
this.editor.insertText(2, '!');
let editor = this.initialize(Editor, '<p>0123</p>');
editor.update();
spyOn(editor.emitter, 'emit').and.callThrough();
let old = editor.getDelta();
editor.insertText(2, '!');
let delta = new Delta().retain(2).insert('!');
expect(this.editor.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, delta, old, Emitter.sources.API);
expect(editor.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, delta, old, Emitter.sources.API);
});

it('user text insert', function(done) {
let old = this.editor.getDelta();
let editor = this.initialize(Editor, '<p>0123</p>');
editor.update();
spyOn(editor.emitter, 'emit').and.callThrough();
let old = editor.getDelta();
this.container.firstChild.firstChild.data = '01!23';
let delta = new Delta().retain(2).insert('!');
setTimeout(() => {
expect(this.editor.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, delta, old, Emitter.sources.USER);
expect(editor.emitter.emit).toHaveBeenCalledWith(Emitter.events.TEXT_CHANGE, delta, old, Emitter.sources.USER);
done();
}, 1);
});

it('insert same character', function(done) {
let [editor, selection] = this.initialize([Editor, Selection], '<p>aaaa</p>');
selection.setRange(new Range(2, 0));
editor.update();
spyOn(editor.emitter, 'emit').and.callThrough();
let old = editor.getDelta();
let textNode = this.container.firstChild.firstChild
textNode.data = 'aaaaa';
selection.setNativeRange(textNode.data, 3);
let delta = new Delta().retain(2).insert('a');
setTimeout(() => {
let args = editor.emitter.emit.calls.mostRecent().args;
expect(args).toEqual([Emitter.events.TEXT_CHANGE, delta, old, Emitter.sources.USER]);
done();
}, 1);
});
Expand Down

0 comments on commit 97d3f22

Please sign in to comment.