Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit 086bb15

Browse files
committed
Merge pull request #40 from lemonde/fix-empty-snapshot
Amends the top of the undo stack when model sync
2 parents 8b91af1 + 8f9c589 commit 086bb15

File tree

5 files changed

+72
-35
lines changed

5 files changed

+72
-35
lines changed

angular-ckeditor.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,16 @@
6262
// Set editor data when view data change.
6363
ngModelController.$render = function syncEditor() {
6464
controller.ready().then(function () {
65-
controller.instance.setData(ngModelController.$viewValue || '');
65+
// "noSnapshot" prevent recording an undo snapshot
66+
controller.instance.setData(ngModelController.$viewValue || '', {
67+
noSnapshot: true,
68+
callback: function () {
69+
// Amends the top of the undo stack with the current DOM changes
70+
// ie: merge snapshot with the first empty one
71+
// http://docs.ckeditor.com/#!/api/CKEDITOR.editor-event-updateSnapshot
72+
controller.instance.fire('updateSnapshot');
73+
}
74+
});
6675
});
6776
};
6877
}

angular-ckeditor.min.js

Lines changed: 30 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

angular-ckeditor.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
],
2525
"devDependencies": {
2626
"angular-mocks": "~1.3.21",
27-
"ckeditor": "#full/4.4.2",
27+
"ckeditor": "#full/4.4.6",
2828
"jquery": "~2.1.4",
2929
"lodash": "~3.10.1"
3030
}

test/angular-ckeditor.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
'use strict';
2+
13
var expect = chai.expect;
24
_.str = s; // clearer exposition of underscore.string
35

46
describe('CKEditor directive', function () {
5-
var $compile, $document, $rootScope, scope;
7+
var $compile, $document, $rootScope, scope, createElement, element;
68

79
beforeEach(module('ckeditor'));
810

@@ -11,6 +13,12 @@ describe('CKEditor directive', function () {
1113
$rootScope = $injector.get('$rootScope');
1214
$document = $injector.get('$document');
1315
scope = $rootScope.$new();
16+
17+
createElement = function () {
18+
element = $compile(
19+
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
20+
)(scope);
21+
};
1422
}));
1523

1624
afterEach(function cleanup (done) {
@@ -30,31 +38,23 @@ describe('CKEditor directive', function () {
3038
done();
3139
};
3240

33-
var element = $compile(
34-
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
35-
)(scope);
41+
createElement();
3642
});
3743

3844
it('should put editor out of readonly mode when ready', function (done) {
3945
scope.onReady = function () {
4046
expect(_.find(CKEDITOR.instances).readOnly).to.be.false;
4147
done();
4248
};
43-
44-
var element = $compile(
45-
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
46-
)(scope);
49+
createElement();
4750
});
4851

4952
it('should destroy instance on scope destroy', function (done) {
5053
scope.onReady = function () {
5154
done();
5255
};
5356

54-
var element = $compile(
55-
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
56-
)(scope);
57-
57+
createElement();
5858
// this is tested in the afterEach "cleanup" above
5959
});
6060
});
@@ -63,9 +63,7 @@ describe('CKEditor directive', function () {
6363
scope.content = 'Hello';
6464
scope.onReady = done;
6565

66-
var element = $compile(
67-
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
68-
)(scope);
66+
createElement();
6967
});
7068

7169
describe('model sync', function() {
@@ -84,9 +82,7 @@ describe('CKEditor directive', function () {
8482
}, 0);
8583
};
8684

87-
var element = $compile(
88-
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
89-
)(scope);
85+
createElement();
9086
});
9187

9288
it('should synchronize editor to the view', function (done) {
@@ -101,9 +97,7 @@ describe('CKEditor directive', function () {
10197
}, 5);
10298
};
10399

104-
var element = $compile(
105-
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
106-
)(scope);
100+
createElement();
107101
});
108102

109103
it('should synchronize editor to the view at start', function (done) {
@@ -117,9 +111,19 @@ describe('CKEditor directive', function () {
117111
};
118112

119113
scope.content = 'at start !';
120-
var element = $compile(
121-
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
122-
)(scope);
114+
createElement();
115+
});
116+
117+
it('should contain only one snapshot at start', function (done) {
118+
scope.onReady = function () {
119+
setTimeout(function () {
120+
expect(_.find(CKEDITOR.instances).undoManager.snapshots.length).to.equal(1);
121+
done();
122+
}, 5);
123+
};
124+
125+
scope.content = 'at start !';
126+
createElement();
123127
});
124128

125129
it('should update model in a watchable way', function (done) {
@@ -134,10 +138,7 @@ describe('CKEditor directive', function () {
134138
_.find(CKEDITOR.instances).setData('<p>Hey</p>');
135139
};
136140

137-
var element =
138-
$compile(
139-
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
140-
)(scope);
141+
createElement();
141142
});
142143
});
143144

@@ -150,9 +151,7 @@ describe('CKEditor directive', function () {
150151
done();
151152
};
152153

153-
var element = $compile(
154-
'<div contenteditable="true" ckeditor ng-model="content" ready="onReady()"></div>'
155-
)(scope);
154+
createElement();
156155
});
157156
});
158157

0 commit comments

Comments
 (0)