Skip to content

Commit b947012

Browse files
committed
table editor uses mutations
1 parent 64e0694 commit b947012

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

indigo_app/static/javascript/indigo/views/document_editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
* Discard the content of all editors.
160160
*/
161161
discardChanges: function() {
162-
this.tableEditor.discardChanges(null, true);
162+
this.tableEditor.discardChanges(true);
163163
this.aknTextEditor.discardChanges();
164164
if (this.editing) {
165165
this.editActivityCancelled();

indigo_app/static/javascript/indigo/views/document_xml_editor.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,7 @@ class XMLEditor {
330330

331331
setXmlElement (element) {
332332
this.xmlElement = element;
333-
if (this.visible) {
334-
this.render();
335-
}
333+
this.render();
336334
}
337335

338336
show() {
@@ -347,17 +345,19 @@ class XMLEditor {
347345

348346
render() {
349347
// pretty-print the xml
350-
const xml = this.xmlElement ? prettyPrintXml(Indigo.toXml(this.xmlElement)) : '';
351-
if (this.editor.getValue() !== xml) {
352-
const posn = this.editor.getPosition();
348+
if (this.visible) {
349+
const xml = this.xmlElement ? prettyPrintXml(Indigo.toXml(this.xmlElement)) : '';
350+
if (this.editor.getValue() !== xml) {
351+
const posn = this.editor.getPosition();
353352

354-
// ignore the onDidChangeModelContent event triggered by setValue
355-
this.updating = true;
356-
this.editor.setValue(xml);
357-
this.updating = false;
353+
// ignore the onDidChangeModelContent event triggered by setValue
354+
this.updating = true;
355+
this.editor.setValue(xml);
356+
this.updating = false;
358357

359-
this.editor.setPosition(posn);
360-
this.editor.layout();
358+
this.editor.setPosition(posn);
359+
this.editor.layout();
360+
}
361361
}
362362
}
363363

@@ -397,19 +397,18 @@ class XMLEditor {
397397
switch (model.getMutationImpact(mutation, this.xmlElement)) {
398398
case 'replaced':
399399
this.xmlElement = mutation.addedNodes[0];
400+
this.render();
400401
break;
401402
case 'changed':
403+
this.render();
402404
break;
403405
case 'removed':
404406
// the change removed xmlElement from the tree
405407
console.log('Mutation removes SourceEditor.xmlElement from the tree');
406408
this.xmlElement = null;
409+
this.render();
407410
break;
408411
}
409-
410-
if (this.visible) {
411-
this.render();
412-
}
413412
}
414413

415414
onNavTabChanged(e) {

indigo_app/static/javascript/indigo/views/table_editor.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
},
3131

3232
initialize: function(options) {
33-
var self = this;
34-
3533
this.parent = options.parent;
3634
this.documentContent = options.documentContent;
35+
this.documentContent.on('mutation', this.onDomMutated.bind(this));
3736
this.tableWrapper = this.$('.table-editor-wrapper').removeClass('d-none').remove()[0];
3837
this.editing = false;
3938

@@ -77,6 +76,30 @@
7776
return true;
7877
},
7978

79+
/**
80+
* The XML document has changed, re-render if it impacts our table element.
81+
*
82+
* @param model documentContent model
83+
* @param mutation a MutationRecord object
84+
*/
85+
onDomMutated (model, mutation) {
86+
if (!this.editing) return;
87+
88+
switch (model.getMutationImpact(mutation, this.table)) {
89+
case 'replaced':
90+
this.discardChanges(true);
91+
break;
92+
case 'changed':
93+
this.discardChanges(true);
94+
break;
95+
case 'removed':
96+
// the change removed xmlElement from the tree
97+
console.log('Mutation removes TableEditor.table from the tree');
98+
this.discardChanges(true);
99+
break;
100+
}
101+
},
102+
80103
saveChanges: function(e) {
81104
if (!this.editing || !this.table) return;
82105

@@ -111,7 +134,7 @@
111134
this.trigger('save');
112135
},
113136

114-
discardChanges: function(e, force) {
137+
discardChanges: function(force) {
115138
if (!this.editing || !this.table) return;
116139
if (!force && !confirm($t("You'll lose your changes, are you sure?"))) return;
117140

0 commit comments

Comments
 (0)