Improve saving indicator
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-11-01 21:21:10 +01:00
parent b94611b160
commit 86d1a25d4e
3 changed files with 35 additions and 22 deletions

View File

@ -37,16 +37,17 @@ function isMultiline(input) {
function addSaver(editor, saveUrl, page, beforeSave) { function addSaver(editor, saveUrl, page, beforeSave) {
return { return {
save() { save() {
return editor.save().then(outputData => { return editor.save()
beforeSave() .then(outputData => {
let data = { beforeSave()
'json': 1, let data = {
'p': page, 'json': 1,
'summary': "", 'p': page,
'content': JSON.stringify(outputData), 'summary': "",
}; 'content': JSON.stringify(outputData),
return axios.post(saveUrl, qs.encode(data)) };
}) return axios.post(saveUrl, qs.encode(data))
})
} }
} }
} }
@ -58,16 +59,22 @@ function Indicator(element, timeout) {
done() { done() {
timeoutId = setTimeout(() => { timeoutId = setTimeout(() => {
element.classList.add('hide') element.classList.add('hide')
element.classList.remove('error')
}, timeout * 1000); }, timeout * 1000);
}, },
addError(message) {
element.classList.add('error')
this.setText(message)
},
setText(text) { setText(text) {
if (timeoutId) { if (timeoutId) {
clearTimeout(timeoutId); clearTimeout(timeoutId);
timeoutId = null; timeoutId = null;
} }
element.innerText = text;
element.classList.remove('hide') element.classList.remove('hide')
element.innerText = text;
}, },
} }
} }
@ -75,13 +82,18 @@ function Indicator(element, timeout) {
function addIndicator(editor, indicator) { function addIndicator(editor, indicator) {
return { return {
save() { save() {
editor.save().then(() => { return editor.save()
indicator.setText('saved!'); .then(() => {
indicator.done(); console.log('success while saving')
}).catch(() => { indicator.setText('saved!');
console.log('error while saving') indicator.done();
indicator.done(); return true
}) })
.catch(reason => {
console.warn('error while saving: '+ reason)
indicator.addError('error!')
indicator.done();
})
} }
} }
} }
@ -288,11 +300,10 @@ function Editor(holder, input) {
let saveUrl = element.dataset.saveurl; let saveUrl = element.dataset.saveurl;
let page = element.dataset.page; let page = element.dataset.page;
indicator.setText('has changes...');
addIndicator( addIndicator(
addSaver(editor, saveUrl, page, () => indicator.setText('saving...')), addSaver(editor, saveUrl, page, () => indicator.setText('saving...')),
indicator indicator
).save() ).save().then(() => indicator.done())
}) })
// editor.on('rendered', function () { // editor.on('rendered', function () {

View File

@ -82,6 +82,10 @@ body {
color: white; color: white;
padding: 3px; padding: 3px;
z-index: 50; z-index: 50;
&.error {
background: red;
}
} }
#autocomplete.hidden { #autocomplete.hidden {

View File

@ -58,8 +58,6 @@ function editor(root, inputData, options) {
if (store.hasChanged()) { if (store.hasChanged()) {
resolve(store.debug().result) resolve(store.debug().result)
store.clearChanged() store.clearChanged()
} else {
reject()
} }
}); });
} }