Extract storage implementation from Interface [#60]

This commit is contained in:
David Evans 2020-01-19 19:35:48 +00:00
parent f663f0426b
commit f2e8c35a2a
6 changed files with 72 additions and 40 deletions

View File

@ -1123,34 +1123,6 @@
} }
} }
class LocalStorage {
constructor(id) {
this.id = id;
}
set(value) {
if(!this.id) {
return;
}
try {
window.localStorage.setItem(this.id, value);
} catch(ignore) {
// Ignore
}
}
get() {
if(!this.id) {
return '';
}
try {
return window.localStorage.getItem(this.id) || '';
} catch(e) {
return '';
}
}
}
function toCappedFixed(v, cap) { function toCappedFixed(v, cap) {
const s = v.toString(); const s = v.toString();
const p = s.indexOf('.'); const p = s.indexOf('.');
@ -1219,6 +1191,20 @@
} }
} }
class VoidStorage {
constructor() {
this.value = '';
}
set(value) {
this.value = value;
}
get() {
return this.value;
}
}
const DELAY_AGENTCHANGE = 500; const DELAY_AGENTCHANGE = 500;
const DELAY_STAGECHANGE = 250; const DELAY_STAGECHANGE = 250;
const PNG_RESOLUTION = 4; const PNG_RESOLUTION = 4;
@ -1252,15 +1238,15 @@
constructor({ constructor({
sequenceDiagram, sequenceDiagram,
defaultCode = '', defaultCode = '',
localStorage = '',
library = [], library = [],
links = [], links = [],
require = null, require = null,
storage = new VoidStorage(),
touchUI = false, touchUI = false,
}) { }) {
this.diagram = sequenceDiagram; this.diagram = sequenceDiagram;
this.defaultCode = defaultCode; this.defaultCode = defaultCode;
this.localStorage = new LocalStorage(localStorage); this.storage = storage;
this.library = library; this.library = library;
this.links = links; this.links = links;
this.minScale = 1.5; this.minScale = 1.5;
@ -1558,7 +1544,7 @@
this.code = new CodeEditor(this.dom, container, { this.code = new CodeEditor(this.dom, container, {
mode: 'sequence', mode: 'sequence',
require: this.require, require: this.require,
value: this.localStorage.get() || this.defaultCode, value: this.storage.get() || this.defaultCode,
}); });
this.code this.code
@ -1751,7 +1737,7 @@
update(immediate = true) { update(immediate = true) {
this._hideURLBuilder(); this._hideURLBuilder();
const src = this.code.value(); const src = this.code.value();
this.localStorage.set(src); this.storage.set(src);
let sequence = null; let sequence = null;
try { try {
sequence = this.diagram.process(src); sequence = this.diagram.process(src);
@ -1837,6 +1823,34 @@
} }
} }
class LocalStorage {
constructor(id) {
this.id = id;
}
set(value) {
if(!this.id) {
return;
}
try {
window.localStorage.setItem(this.id, value);
} catch(ignore) {
// Ignore
}
}
get() {
if(!this.id) {
return '';
}
try {
return window.localStorage.getItem(this.id) || '';
} catch(e) {
return '';
}
}
}
var SequenceDiagram = window.SequenceDiagram; var SequenceDiagram = window.SequenceDiagram;
const require = window.requirejs; const require = window.requirejs;
@ -1909,13 +1923,15 @@
}); });
} }
const storage = new LocalStorage('src');
const ui = new Interface({ const ui = new Interface({
defaultCode, defaultCode,
library: ComponentsLibrary, library: ComponentsLibrary,
links, links,
localStorage: 'src',
require, require,
sequenceDiagram: new SequenceDiagram(), sequenceDiagram: new SequenceDiagram(),
storage,
touchUI: ('ontouchstart' in window), touchUI: ('ontouchstart' in window),
}); });
loader.parentNode.removeChild(loader); loader.parentNode.removeChild(loader);

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,6 @@
import ComponentsLibrary from './interface/ComponentsLibrary.mjs'; import ComponentsLibrary from './interface/ComponentsLibrary.mjs';
import Interface from './interface/Interface.mjs'; import Interface from './interface/Interface.mjs';
import LocalStorage from './storage/LocalStorage.mjs';
import SequenceDiagram from '../../scripts/sequence/SequenceDiagram.mjs'; import SequenceDiagram from '../../scripts/sequence/SequenceDiagram.mjs';
import {require} from './requireCDN.mjs'; import {require} from './requireCDN.mjs';
@ -36,13 +37,15 @@ window.addEventListener('load', () => {
}); });
} }
const storage = new LocalStorage('src');
const ui = new Interface({ const ui = new Interface({
defaultCode, defaultCode,
library: ComponentsLibrary, library: ComponentsLibrary,
links, links,
localStorage: 'src',
require, require,
sequenceDiagram: new SequenceDiagram(), sequenceDiagram: new SequenceDiagram(),
storage,
touchUI: ('ontouchstart' in window), touchUI: ('ontouchstart' in window),
}); });
loader.parentNode.removeChild(loader); loader.parentNode.removeChild(loader);

View File

@ -7,8 +7,8 @@ import {
} from './fileHelpers.mjs'; } from './fileHelpers.mjs';
import CodeEditor from './CodeEditor.mjs'; import CodeEditor from './CodeEditor.mjs';
import DOMWrapper from '../../../scripts/core/DOMWrapper.mjs'; import DOMWrapper from '../../../scripts/core/DOMWrapper.mjs';
import LocalStorage from './LocalStorage.mjs';
import URLExporter from './URLExporter.mjs'; import URLExporter from './URLExporter.mjs';
import VoidStorage from '../storage/VoidStorage.mjs';
const DELAY_AGENTCHANGE = 500; const DELAY_AGENTCHANGE = 500;
const DELAY_STAGECHANGE = 250; const DELAY_STAGECHANGE = 250;
@ -43,15 +43,15 @@ export default class Interface {
constructor({ constructor({
sequenceDiagram, sequenceDiagram,
defaultCode = '', defaultCode = '',
localStorage = '',
library = [], library = [],
links = [], links = [],
require = null, require = null,
storage = new VoidStorage(),
touchUI = false, touchUI = false,
}) { }) {
this.diagram = sequenceDiagram; this.diagram = sequenceDiagram;
this.defaultCode = defaultCode; this.defaultCode = defaultCode;
this.localStorage = new LocalStorage(localStorage); this.storage = storage;
this.library = library; this.library = library;
this.links = links; this.links = links;
this.minScale = 1.5; this.minScale = 1.5;
@ -349,7 +349,7 @@ export default class Interface {
this.code = new CodeEditor(this.dom, container, { this.code = new CodeEditor(this.dom, container, {
mode: 'sequence', mode: 'sequence',
require: this.require, require: this.require,
value: this.localStorage.get() || this.defaultCode, value: this.storage.get() || this.defaultCode,
}); });
this.code this.code
@ -542,7 +542,7 @@ export default class Interface {
update(immediate = true) { update(immediate = true) {
this._hideURLBuilder(); this._hideURLBuilder();
const src = this.code.value(); const src = this.code.value();
this.localStorage.set(src); this.storage.set(src);
let sequence = null; let sequence = null;
try { try {
sequence = this.diagram.process(src); sequence = this.diagram.process(src);

View File

@ -0,0 +1,13 @@
export default class VoidStorage {
constructor() {
this.value = '';
}
set(value) {
this.value = value;
}
get() {
return this.value;
}
}