mirror of
https://github.com/cryptpad/cryptpad.git
synced 2026-09-14 11:05:41 +05:00
Send changed state to outer application #1034
This commit is contained in:
parent
5e6b042f28
commit
e0daae115e
@ -638,8 +638,12 @@ define([
|
||||
upload(syncBlob);
|
||||
}
|
||||
};
|
||||
const integrationHasUnsavedChanges = function(unsavedChanges, cb) {
|
||||
sframeChan.query('Q_INTEGRATION_HAS_UNSAVED_CHANGES', unsavedChanges, cb);
|
||||
};
|
||||
var inte = common.createIntegration(onLocal, cpNfInner.chainpad,
|
||||
integrationSave, toolbar);
|
||||
integrationSave, integrationHasUnsavedChanges,
|
||||
toolbar);
|
||||
if (inte) {
|
||||
integration = true;
|
||||
evIntegrationSave.reg(function () {
|
||||
|
||||
@ -3,7 +3,14 @@ define([
|
||||
], function (Util) {
|
||||
var module = {};
|
||||
|
||||
module.create = function (Common, onLocal, chainpad, saveHandler, toolbar) {
|
||||
module.create = function (
|
||||
Common,
|
||||
onLocal,
|
||||
chainpad,
|
||||
saveHandler,
|
||||
unsavedChangesHandler,
|
||||
toolbar) {
|
||||
|
||||
var exp = {};
|
||||
var metadataMgr = Common.getMetadataMgr();
|
||||
var privateData = metadataMgr.getPrivateData();
|
||||
@ -13,6 +20,9 @@ define([
|
||||
if (typeof(saveHandler) !== "function") {
|
||||
throw new Error("Incorrect save handler");
|
||||
}
|
||||
if (typeof(unsavedChangesHandler) !== "function") {
|
||||
throw new Error("Incorrect unsaves changes handler");
|
||||
}
|
||||
|
||||
var debug = console.warn;
|
||||
//debug = function () {};
|
||||
@ -37,6 +47,13 @@ define([
|
||||
var SETTLE_TO = 3000;
|
||||
var SAVE_TO = Math.min((BASE_TIMER / 2), 10000);
|
||||
|
||||
const setStateChanged = function(newValue) {
|
||||
if (state.changed == newValue) {
|
||||
return;
|
||||
}
|
||||
state.changed = newValue;
|
||||
unsavedChangesHandler(state.changed, function() {});
|
||||
}
|
||||
var requestSave = function () {}; // placeholder
|
||||
var saved = function () {}; // placeholder;
|
||||
var save = function (id) {
|
||||
@ -84,7 +101,7 @@ define([
|
||||
clearTimeout(saveTo);
|
||||
clearTimeout(onSettleTo);
|
||||
|
||||
state.changed = false;
|
||||
setStateChanged(false);
|
||||
saveTo = setTimeout(function () {
|
||||
// They weren't able to save in time, try ourselves
|
||||
var id = state.other;
|
||||
@ -149,7 +166,7 @@ define([
|
||||
}
|
||||
state.me = true;
|
||||
state.lastTmp = +new Date();
|
||||
state.changed = false;
|
||||
setStateChanged(false);
|
||||
cb(true);
|
||||
});
|
||||
};
|
||||
@ -177,7 +194,7 @@ define([
|
||||
};
|
||||
|
||||
exp.changed = function () {
|
||||
state.changed = true;
|
||||
setStateChanged(true);
|
||||
|
||||
var timeSinceLastSave = +new Date() - state.last; // in ms
|
||||
var to = autosaveTimer - timeSinceLastSave; // negative if we can save
|
||||
|
||||
@ -1946,6 +1946,11 @@ define([
|
||||
cfg.integrationUtils.save(obj, cb);
|
||||
}
|
||||
});
|
||||
sframeChan.on('Q_INTEGRATION_HAS_UNSAVED_CHANGES', function (obj, cb) {
|
||||
if (cfg.integrationUtils && cfg.integrationUtils.onHasUnsavedChanges) {
|
||||
cfg.integrationUtils.onHasUnsavedChanges(obj, cb);
|
||||
}
|
||||
});
|
||||
integrationSave = function (cb) {
|
||||
sframeChan.query('Q_INTEGRATION_NEEDSAVE', null, cb);
|
||||
};
|
||||
|
||||
@ -158,6 +158,10 @@
|
||||
document.getElementById('cryptpad-editor').remove();
|
||||
makeIframe(config);
|
||||
});
|
||||
chan.on('HAS_UNSAVED_CHANGES', function(unsavedChanges, cb) {
|
||||
config.events.onHasUnsavedChanges(unsavedChanges);
|
||||
cb();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
@ -197,7 +201,8 @@
|
||||
|
||||
if (!config) { return reject('Missing args: no data provided'); }
|
||||
if(['document.url', 'document.fileType', 'documentType',
|
||||
'events.onSave', 'events.onNewKey'].some(function (k) {
|
||||
'events.onSave', 'events.onHasUnsavedChanges',
|
||||
'events.onNewKey'].some(function (k) {
|
||||
var s = k.split('.');
|
||||
var c = config;
|
||||
return s.some(function (key) {
|
||||
|
||||
@ -117,6 +117,9 @@ define([
|
||||
var reload = function (data) {
|
||||
chan.send('RELOAD', data);
|
||||
};
|
||||
var onHasUnsavedChanges = function (unsavedChanges, cb) {
|
||||
chan.send('HAS_UNSAVED_CHANGES', unsavedChanges, cb);
|
||||
};
|
||||
|
||||
chan.on('START', function (data) {
|
||||
console.warn('INNER START', data);
|
||||
@ -133,7 +136,8 @@ define([
|
||||
},
|
||||
utils: {
|
||||
save: save,
|
||||
reload: reload
|
||||
reload: reload,
|
||||
onHasUnsavedChanges: onHasUnsavedChanges
|
||||
}
|
||||
};
|
||||
require(['/common/sframe-app-outer.js'], function () {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user