Fix autosave on unload

This commit is contained in:
Paulo Gustavo Veiga
2022-02-13 18:06:50 -08:00
parent e331ba77b9
commit c4516a7ac1
6 changed files with 119 additions and 68 deletions

View File

@ -30,7 +30,8 @@ class LocalStorageManager extends PersistenceManager {
this.forceLoad = forceLoad;
}
saveMapXml(mapId: string, mapXml: string) {
saveMapXml(mapId: string, mapDoc: Document, pref = null, events = null): void {
const mapXml = new XMLSerializer().serializeToString(mapDoc);
localStorage.setItem(`${mapId}-xml`, mapXml);
}

View File

@ -24,7 +24,7 @@ abstract class PersistenceManager {
// eslint-disable-next-line no-use-before-define
static _instance: PersistenceManager;
save(mindmap: Mindmap, editorProperties, saveHistory: boolean, events, sync: boolean) {
save(mindmap: Mindmap, editorProperties, saveHistory: boolean, events?) {
$assert(mindmap, 'mindmap can not be null');
$assert(editorProperties, 'editorProperties can not be null');
@ -33,11 +33,9 @@ abstract class PersistenceManager {
const serializer = XMLSerializerFactory.createInstanceFromMindmap(mindmap);
const domMap = serializer.toXML(mindmap);
const mapXml = new XMLSerializer().serializeToString(domMap);
const pref = JSON.stringify(editorProperties);
try {
this.saveMapXml(mapId, mapXml, pref, saveHistory, events, sync);
this.saveMapXml(mapId, domMap, pref, saveHistory, events);
} catch (e) {
console.error(e);
events.onError(e);
@ -54,7 +52,7 @@ abstract class PersistenceManager {
abstract loadMapDom(mapId: string): Document;
abstract saveMapXml(mapId: string, mapXml, pref, saveHistory, events, sync);
abstract saveMapXml(mapId: string, mapXml: Document, pref?, saveHistory?: boolean, events?);
abstract unlockMap(mindmap: Mindmap): void;

View File

@ -51,10 +51,10 @@ class RESTPersistenceManager extends PersistenceManager {
this.session = options.session;
}
saveMapXml(mapId: string, mapXml: Document, pref: string, saveHistory: boolean, events, sync: boolean): void {
saveMapXml(mapId: string, mapXml: Document, pref: string, saveHistory: boolean, events): void {
const data = {
id: mapId,
xml: mapXml,
xml: new XMLSerializer().serializeToString(mapXml),
properties: pref,
};
@ -71,73 +71,77 @@ class RESTPersistenceManager extends PersistenceManager {
}, 10000);
const persistence = this;
$.ajax({
type: 'put',
url: `${this.documentUrl.replace('{id}', mapId)}?${query}`,
dataType: 'json',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
async: !sync,
success(successData) {
persistence.timestamp = successData;
fetch(
`${this.documentUrl.replace('{id}', mapId)}?${query}`,
{
method: 'PUT',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json; charset=utf-8', Accept: 'application/json' },
keepalive: true,
},
).then(async (response: Response) => {
if (response.ok) {
persistence.timestamp = await response.text();
events.onSuccess();
},
complete() {
// Clear event timeout ...
if (persistence.clearTimeout) {
clearTimeout(persistence.clearTimeout);
}
persistence.onSave = false;
},
error(xhr) {
const { responseText } = xhr;
let userMsg = { severity: 'SEVERE', message: $msg('SAVE_COULD_NOT_BE_COMPLETED') };
const contentType = xhr.getResponseHeader('Content-Type');
if (contentType != null && contentType.indexOf('application/json') !== -1) {
let serverMsg = null;
try {
serverMsg = $.parseJSON(responseText);
serverMsg = serverMsg.globalSeverity ? serverMsg : null;
} catch (e) {
// Message could not be decoded ...
}
userMsg = persistence._buildError(serverMsg);
} else if (this.status === 405) {
} else {
console.log(`Saving error: ${response.status}`);
let userMsg;
if (response.status === 405) {
userMsg = { severity: 'SEVERE', message: $msg('SESSION_EXPIRED') };
} else {
const responseText = await response.text();
const contentType = response.headers['Content-Type'];
if (contentType != null && contentType.indexOf('application/json') !== -1) {
let serverMsg = null;
try {
serverMsg = JSON.parse(responseText);
serverMsg = serverMsg.globalSeverity ? serverMsg : null;
} catch (e) {
// Message could not be decoded ...
}
userMsg = persistence._buildError(serverMsg);
}
}
events.onError(userMsg);
persistence.onSave = false;
},
}
// Clear event timeout ...
if (persistence.clearTimeout) {
clearTimeout(persistence.clearTimeout);
}
persistence.onSave = false;
}).catch(() => {
const userMsg = { severity: 'SEVERE', message: $msg('SAVE_COULD_NOT_BE_COMPLETED') };
events.onError(userMsg);
// Clear event timeout ...
if (persistence.clearTimeout) {
clearTimeout(persistence.clearTimeout);
}
persistence.onSave = false;
});
}
}
discardChanges(mapId: string) {
$.ajax({
url: this.revertUrl.replace('{id}', mapId),
async: false,
method: 'post',
headers: { 'Content-Type': 'application/json; charset=utf-8', Accept: 'application/json' },
error(xhr, ajaxOptions, thrownError) {
console.error(`Request error => status:${xhr.status} ,thrownError: ${thrownError}`);
},
});
fetch(this.revertUrl.replace('{id}', mapId),
{
method: 'POST',
headers: { 'Content-Type': 'application/json; charset=utf-8', Accept: 'application/json' },
keepalive: true,
});
}
unlockMap(mindmap: Mindmap) {
const mapId = mindmap.getId();
$.ajax({
url: this.lockUrl.replace('{id}', mapId),
async: false,
method: 'put',
headers: { 'Content-Type': 'text/plain' },
data: 'false',
error(xhr, ajaxOptions, thrownError) {
console.error(`Request error => status:${xhr.status} ,thrownError: ${thrownError}`);
fetch(
this.lockUrl.replace('{id}', mapId),
{
method: 'POST',
headers: { 'Content-Type': 'text/plain' },
body: 'false',
},
});
);
}
private _buildError(jsonSeverResponse) {

View File

@ -73,7 +73,7 @@ class IMenu {
persistenceManager.unlockMap(mindmap);
}
save(saveElem: JQuery, designer: Designer, saveHistory: boolean, sync?: boolean) {
save(saveElem: JQuery, designer: Designer, saveHistory: boolean) {
// Load map content ...
const mindmap = designer.getMindmap();
const mindmapProp = designer.getMindmapProperties();
@ -106,7 +106,7 @@ class IMenu {
}
}
},
}, sync);
});
}
isSaveRequired(): boolean {

View File

@ -297,9 +297,9 @@ class Menu extends IMenu {
Menu._registerTooltip('save', $msg('SAVE'), 'meta+S');
if (!readOnly) {
$(window).bind('beforeunload', () => {
window.addEventListener('beforeunload', () => {
if (this.isSaveRequired()) {
this.save(saveElem, designer, false, true);
this.save(saveElem, designer, false);
}
this.unlockMap(designer);
});
@ -310,7 +310,7 @@ class Menu extends IMenu {
if (this.isSaveRequired()) {
this.save(saveElem, designer, false);
}
}, 30000,
}, 10000,
);
}
}