Skip to content

Commit 242e8ec

Browse files
committed
move text editor update/cancel buttons into editor toolbar
this makes the text editor responsible for handling accept/discard changes, and visually links the buttons to the contents of the editor also discards changes when editing a table while text editor is open
1 parent ab1c2ae commit 242e8ec

File tree

5 files changed

+37
-44
lines changed

5 files changed

+37
-44
lines changed

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
Indigo.SourceEditorView = Backbone.View.extend({
1818
el: 'body',
1919
events: {
20-
'click .text-editor-buttons .btn.save': 'acceptChanges',
21-
'click .text-editor-buttons .btn.cancel': 'discardChanges',
2220
'click .btn.edit-text': 'fullEdit',
2321
'click .btn.edit-table': 'editTable',
2422
'click .quick-edit': 'quickEdit',
@@ -38,9 +36,11 @@
3836
this.toolbar = document.querySelector('.document-toolbar-wrapper');
3937

4038
this.aknTextEditor = new Indigo.AknTextEditor(
41-
this.el,
39+
this.el.querySelector('.document-primary-pane-editor-pane'),
4240
this.document,
4341
true,
42+
this.onTextEditSaved.bind(this),
43+
this.onTextEditCancelled.bind(this),
4444
);
4545

4646
const xmlEditorBox = document.querySelector('.document-xml-editor');
@@ -138,36 +138,10 @@
138138
return true;
139139
},
140140

141-
/**
142-
* End the edit activity and save the changes made (in text or table edit modes).
143-
*/
144-
acceptChanges: async function() {
145-
// save table edits, if any
146-
this.tableEditor.saveChanges();
147-
148-
if (this.aknTextEditor.editing) {
149-
const btn = this.toolbar.querySelector('.text-editor-buttons .btn.save');
150-
btn.setAttribute('disabled', 'true');
151-
152-
try {
153-
if (await this.aknTextEditor.acceptChanges()) {
154-
this.editActivityEnded();
155-
this.closeTextEditor();
156-
}
157-
} finally {
158-
btn.removeAttribute('disabled');
159-
}
160-
}
161-
},
162-
163141
/**
164142
* Discard the content of all editors.
165143
*/
166144
discardChanges: function() {
167-
if (this.aknTextEditor.editing) {
168-
this.editActivityCancelled();
169-
this.closeTextEditor();
170-
}
171145
this.tableEditor.discardChanges(true);
172146
this.aknTextEditor.discardChanges();
173147
},
@@ -234,6 +208,16 @@
234208
}
235209
},
236210

211+
onTextEditSaved: function() {
212+
this.editActivityEnded();
213+
this.closeTextEditor();
214+
},
215+
216+
onTextEditCancelled: function() {
217+
this.editActivityCancelled();
218+
this.closeTextEditor();
219+
},
220+
237221
editActivityStarted: function(mode) {
238222
this.trigger('edit-activity-started', mode);
239223
},
@@ -400,6 +384,9 @@
400384
const tableId = e.currentTarget.dataset.tableId;
401385

402386
if (this.confirmAndDiscardChanges()) {
387+
// ensure the text editor is closed, even if there were no changes
388+
this.aknTextEditor.discardChanges();
389+
403390
// we queue this up to run after the click event has finished, because we need the HTML to be re-rendered
404391
// after changes are potentially discarded
405392
setTimeout(() => {

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* text into XML, and handling text-based editor actions (like bolding, etc.).
44
*/
55
class AknTextEditor {
6-
constructor (root, document, liveUpdates) {
6+
constructor (root, document, liveUpdates, onSave, onDiscard) {
77
this.root = root;
88
this.document = document;
9+
this.onSave = onSave;
10+
this.onDiscard = onDiscard
911
this.editing = false;
1012
this.previousText = null;
1113
this.xmlElement = null;
@@ -71,6 +73,9 @@ class AknTextEditor {
7173
this.liveUpdates = e.currentTarget.checked;
7274
this.onTextChanged();
7375
});
76+
77+
this.root.querySelector('.btn.save').addEventListener('click', this.acceptChanges.bind(this));
78+
this.root.querySelector('.btn.cancel').addEventListener('click', this.discardChanges.bind(this));
7479
}
7580

7681
setXmlElement (element) {
@@ -196,15 +201,21 @@ class AknTextEditor {
196201
if (!this.editing) return false;
197202

198203
if (!this.liveUpdates) {
204+
let elements;
199205
// use a nonce to check if we're still the current save when the parse completes
200206
const nonce = this.nonce = Math.random();
201-
let elements;
207+
const btn = this.root.querySelector('.btn.save');
208+
202209
try {
210+
btn.setAttribute('disabled', 'true');
203211
elements = await this.parse();
204212
} catch(err) {
205213
Indigo.errorView.show(err);
206214
return false;
215+
} finally {
216+
btn.removeAttribute('disabled');
207217
}
218+
208219
// check if we're still the current save
209220
if (nonce !== this.nonce) return false;
210221
this.replaceElement(elements);
@@ -214,6 +225,8 @@ class AknTextEditor {
214225
this.dirty = false;
215226
this.xmlElement = this.xmlElementOriginal = null;
216227

228+
this.onSave();
229+
217230
return true;
218231
}
219232

@@ -228,6 +241,7 @@ class AknTextEditor {
228241
}
229242
this.editing = false;
230243
this.dirty = false;
244+
this.onDiscard();
231245
}
232246
}
233247

indigo_app/static/stylesheets/_documents.scss

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,10 @@
7979
.document-toolbar-wrapper {
8080
padding-bottom: 5px;
8181

82-
.text-editor-buttons {
83-
display: none;
84-
}
85-
8682
&.is-editing.edit-mode-text {
8783
.editor-start-buttons {
8884
display: none;
8985
}
90-
91-
.text-editor-buttons {
92-
display: flex;
93-
}
9486
}
9587

9688
&.is-editing.edit-mode-table {

indigo_app/templates/indigo_api/document/_text_editor.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@
4949
<div class="btn-group btn-group-sm me-2">
5050
<button type="button" class="btn btn-outline-secondary toggle-word-wrap active">{% trans "Wrap lines" %}</button>
5151
</div>
52-
<div class="ms-auto">
52+
<div class="ms-auto me-2">
5353
<label>
5454
<input class="form-check-input me-1" type="checkbox" id="live-updates-chk" checked>
5555
<label class="fw-normal" for="live-updates-chk">{% trans "Preview changes" %}</label>
5656
</label>
5757
</div>
58+
<div class="btn-group btn-group-sm">
59+
<button class="btn btn-success save"><i class="fas fa-check"></i> {% trans "Update" %}</button>
60+
<button class="btn btn-secondary cancel"><i class="fas fa-times"></i> {% trans "Cancel" %}</button>
61+
</div>
5862
</div>
5963
<div class="document-text-editor">
6064
<div class="monaco-editor-box"></div>

indigo_app/templates/indigo_api/document/_toolbar.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@
5858
<div class="btn-group btn-group-sm me-2 editor-start-buttons">
5959
<button type="button" class="btn btn-primary edit-text"><i class="fas fa-pencil-alt"></i> {% trans "Edit" %}</button>
6060
</div>
61-
<div class="btn-group btn-group-sm me-2 text-editor-buttons">
62-
<button class="btn btn-success save"><i class="fas fa-check"></i> {% trans "Update" %}</button>
63-
<button class="btn btn-secondary cancel"><i class="fas fa-times"></i> {% trans "Cancel" %}</button>
64-
</div>
6561

6662
<div class="btn-group btn-group-sm ms-auto me-2">
6763
<a class="btn btn-link" href="{% url 'work_tasks' frbr_uri=work.frbr_uri %}"><i class="fas fa-thumbtack me-1"></i>{% trans "Tasks" %}</a>

0 commit comments

Comments
 (0)