Update ConfigObj.js

This commit is contained in:
JFH
2021-01-01 11:23:19 +01:00
parent 031812c7ae
commit 528c69edbb

View File

@@ -150,8 +150,7 @@ export default class ConfigObj {
// EXTENSION (CLIENT VS. SERVER SAVING/OPENING) // EXTENSION (CLIENT VS. SERVER SAVING/OPENING)
avoidClientSide: false, // Deprecated in favor of `avoidClientSideDownload` avoidClientSide: false, // Deprecated in favor of `avoidClientSideDownload`
avoidClientSideDownload: false, avoidClientSideDownload: false,
avoidClientSideOpen: false, avoidClientSideOpen: false
userExtensions: []
}; };
this.curPrefs = {}; this.curPrefs = {};
@@ -176,6 +175,27 @@ export default class ConfigObj {
'ext-star', 'ext-star',
'ext-storage' 'ext-storage'
]; ];
this.curConfig = {
// We do not put on defaultConfig to simplify object copying
// procedures (we obtain instead from defaultExtensions)
extensions: [],
userExtensions: [],
/**
* Can use `location.origin` to indicate the current
* origin. Can contain a '*' to allow all domains or 'null' (as
* a string) to support all `file:///` URLs. Cannot be set by
* URL for security reasons (not safe, at least for
* privacy or data integrity of SVG content).
* Might have been fairly safe to allow
* `new URL(location.href).origin` by default but
* avoiding it ensures some more security that even third
* party apps on the same domain also cannot communicate
* with this app by default.
* For use with `ext-xdomain-messaging.js`
* @todo We might instead make as a user-facing preference.
*/
allowedOrigins: []
};
this.editor = editor; this.editor = editor;
} }
/** /**
@@ -192,7 +212,7 @@ export default class ConfigObj {
* @returns {void} * @returns {void}
*/ */
setupCurConfig () { setupCurConfig () {
const curConfig = {...this.defaultConfig, ...this.editor.configObj.curConfig}; // Now safe to merge with priority for curConfig in the event any are already set const curConfig = {...this.defaultConfig, ...this.curConfig}; // Now safe to merge with priority for curConfig in the event any are already set
// Now deal with extensions and other array config // Now deal with extensions and other array config
if (!curConfig.noDefaultExtensions) { if (!curConfig.noDefaultExtensions) {
@@ -207,7 +227,7 @@ export default class ConfigObj {
}); });
*/ */
// Export updated config // Export updated config
this.editor.configObj.curConfig = curConfig; this.curConfig = curConfig;
} }
/** /**
* @function loadFromURL Load config/data from URL if given * @function loadFromURL Load config/data from URL if given
@@ -259,7 +279,7 @@ export default class ConfigObj {
this.setConfig(this.urldata, {overwrite: false}); this.setConfig(this.urldata, {overwrite: false});
this.setupCurConfig(); this.setupCurConfig();
if (!this.editor.configObj.curConfig.preventURLContentLoading) { if (!this.curConfig.preventURLContentLoading) {
let {source} = this.urldata; let {source} = this.urldata;
if (!source) { // urldata.source may have been null if it ended with '=' if (!source) { // urldata.source may have been null if it ended with '='
const src = searchParams.get('source'); const src = searchParams.get('source');
@@ -280,7 +300,7 @@ export default class ConfigObj {
return; return;
} }
} }
if (!this.urldata.noStorageOnLoad || this.editor.configObj.curConfig.forceStorage) { if (!this.urldata.noStorageOnLoad || this.curConfig.forceStorage) {
this.loadContentAndPrefs(); this.loadContentAndPrefs();
} }
} else { } else {
@@ -303,8 +323,8 @@ export default class ConfigObj {
* @returns {void} * @returns {void}
*/ */
loadContentAndPrefs () { loadContentAndPrefs () {
if (!this.editor.configObj.curConfig.forceStorage && if (!this.curConfig.forceStorage &&
(this.editor.configObj.curConfig.noStorageOnLoad || (this.curConfig.noStorageOnLoad ||
!document.cookie.match(/(?:^|;\s*)svgeditstore=(?:prefsAndContent|prefsOnly)/) !document.cookie.match(/(?:^|;\s*)svgeditstore=(?:prefsAndContent|prefsOnly)/)
) )
) { ) {
@@ -313,12 +333,12 @@ export default class ConfigObj {
// LOAD CONTENT // LOAD CONTENT
if (this.editor.storage && // Cookies do not have enough available memory to hold large documents if (this.editor.storage && // Cookies do not have enough available memory to hold large documents
(this.editor.configObj.curConfig.forceStorage || (this.curConfig.forceStorage ||
(!this.editor.configObj.curConfig.noStorageOnLoad && (!this.curConfig.noStorageOnLoad &&
document.cookie.match(/(?:^|;\s*)svgeditstore=prefsAndContent/)) document.cookie.match(/(?:^|;\s*)svgeditstore=prefsAndContent/))
) )
) { ) {
const name = 'svgedit-' + this.editor.configObj.curConfig.canvasName; const name = 'svgedit-' + this.curConfig.canvasName;
const cached = this.editor.storage.getItem(name); const cached = this.editor.storage.getItem(name);
if (cached) { if (cached) {
this.editor.loadFromString(cached); this.editor.loadFromString(cached);
@@ -386,7 +406,7 @@ export default class ConfigObj {
// Only allow prefs defined in configObj.defaultPrefs or... // Only allow prefs defined in configObj.defaultPrefs or...
if (this.defaultPrefs[key]) { if (this.defaultPrefs[key]) {
if (cfgCfg.overwrite === false && ( if (cfgCfg.overwrite === false && (
this.editor.configObj.curConfig.preventAllURLConfig || this.curConfig.preventAllURLConfig ||
this.curPrefs[key]) this.curPrefs[key])
) { ) {
return; return;
@@ -399,35 +419,35 @@ export default class ConfigObj {
} else if (['extensions', 'userExtensions', 'allowedOrigins'].includes(key)) { } else if (['extensions', 'userExtensions', 'allowedOrigins'].includes(key)) {
if (cfgCfg.overwrite === false && if (cfgCfg.overwrite === false &&
( (
this.editor.configObj.curConfig.preventAllURLConfig || this.curConfig.preventAllURLConfig ||
['allowedOrigins'].includes(key) || ['allowedOrigins'].includes(key) ||
(key === 'extensions' && this.editor.configObj.curConfig.lockExtensions) (key === 'extensions' && this.curConfig.lockExtensions)
) )
) { ) {
return; return;
} }
this.editor.configObj.curConfig[key] = this.editor.configObj.curConfig[key].concat(val); // We will handle any dupes later this.curConfig[key] = this.curConfig[key].concat(val); // We will handle any dupes later
// Only allow other configObj.curConfig if defined in configObj.defaultConfig // Only allow other configObj.curConfig if defined in configObj.defaultConfig
} else if ({}.hasOwnProperty.call(this.defaultConfig, key)) { } else if ({}.hasOwnProperty.call(this.defaultConfig, key)) {
if (cfgCfg.overwrite === false && ( if (cfgCfg.overwrite === false && (
this.editor.configObj.curConfig.preventAllURLConfig || this.curConfig.preventAllURLConfig ||
{}.hasOwnProperty.call(this.editor.configObj.curConfig, key) {}.hasOwnProperty.call(this.curConfig, key)
)) { )) {
return; return;
} }
// Potentially overwriting of previously set config // Potentially overwriting of previously set config
if ({}.hasOwnProperty.call(this.editor.configObj.curConfig, key)) { if ({}.hasOwnProperty.call(this.curConfig, key)) {
if (cfgCfg.overwrite === false) { if (cfgCfg.overwrite === false) {
return; return;
} }
extendOrAdd(this.editor.configObj.curConfig, key, val); extendOrAdd(this.curConfig, key, val);
} else if (cfgCfg.allowInitialUserOverride === true) { } else if (cfgCfg.allowInitialUserOverride === true) {
extendOrAdd(this.defaultConfig, key, val); extendOrAdd(this.defaultConfig, key, val);
} else if (this.defaultConfig[key] && typeof this.defaultConfig[key] === 'object') { } else if (this.defaultConfig[key] && typeof this.defaultConfig[key] === 'object') {
this.editor.configObj.curConfig[key] = Array.isArray(this.defaultConfig[key]) ? [] : {}; this.curConfig[key] = Array.isArray(this.defaultConfig[key]) ? [] : {};
$.extend(true, this.editor.configObj.curConfig[key], val); // Merge properties recursively, e.g., on initFill, initStroke objects $.extend(true, this.curConfig[key], val); // Merge properties recursively, e.g., on initFill, initStroke objects
} else { } else {
this.editor.configObj.curConfig[key] = val; this.curConfig[key] = val;
} }
} }
}); });