2021-01-24 06:24:14 +08:00
|
|
|
const LastVersion = localStorage.getItem('last_version') || localStorage.getItem('welcomed_version') || appVersion;
|
|
|
|
|
2019-07-18 00:02:07 +08:00
|
|
|
const Blockbench = {
|
|
|
|
isWeb: !isApp,
|
2020-10-22 00:31:26 +08:00
|
|
|
isMobile: !isApp && window.innerWidth <= 640,
|
2021-01-30 22:18:45 +08:00
|
|
|
isTouch: 'ontouchend' in document,
|
2021-02-21 23:46:06 +08:00
|
|
|
get isPWA() {
|
|
|
|
return navigator.standalone || window.matchMedia('(display-mode: standalone)').matches;
|
|
|
|
},
|
2019-07-18 00:02:07 +08:00
|
|
|
version: appVersion,
|
|
|
|
platform: 'web',
|
|
|
|
flags: [],
|
|
|
|
drag_handlers: {},
|
|
|
|
events: {},
|
|
|
|
openTime: new Date(),
|
|
|
|
get elements() {
|
|
|
|
console.warn('Blockbench.elements is deprecated. Please use Outliner.elements instead.')
|
|
|
|
return Outliner.elements
|
|
|
|
},
|
|
|
|
get selection() {
|
|
|
|
console.warn('Blockbench.selection is deprecated. Please use Cube.selected or Outliner.selected instead.')
|
|
|
|
return Cube.selected
|
|
|
|
},
|
|
|
|
get textures() {
|
2021-03-04 00:50:30 +08:00
|
|
|
console.warn('Blockbench.textures is deprecated. Please use Texture.all instead.')
|
2019-07-18 00:02:07 +08:00
|
|
|
return textures;
|
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
edit(aspects, cb) {
|
|
|
|
Undo.initEdit(aspects)
|
|
|
|
cb()
|
|
|
|
Undo.finishEdit()
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
reload() {
|
2019-04-08 00:53:33 +08:00
|
|
|
localStorage.removeItem('backup_model')
|
2018-10-18 01:50:25 +08:00
|
|
|
if (isApp) {
|
2019-07-18 00:02:07 +08:00
|
|
|
Blockbench.addFlag('allow_closing')
|
2020-07-16 15:32:59 +08:00
|
|
|
Blockbench.addFlag('allow_reload')
|
2018-10-18 01:50:25 +08:00
|
|
|
currentwindow.reload()
|
|
|
|
} else {
|
|
|
|
location.reload()
|
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2020-10-02 00:24:20 +08:00
|
|
|
isNewerThan(version) {
|
|
|
|
return compareVersions(Blockbench.version, version);
|
|
|
|
},
|
2020-10-12 01:53:22 +08:00
|
|
|
isOlderThan(version) {
|
|
|
|
return compareVersions(version, Blockbench.version);
|
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
registerEdit() {
|
2019-02-04 04:09:35 +08:00
|
|
|
console.warn('Blockbench.registerEdit is outdated. Please use Undo.initEdit and Undo.finishEdit')
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
//Interface
|
|
|
|
getIconNode(icon, color) {
|
2020-01-24 01:53:36 +08:00
|
|
|
let node;
|
2018-10-18 01:50:25 +08:00
|
|
|
if (typeof icon === 'function') {
|
|
|
|
icon = icon()
|
|
|
|
}
|
2020-01-24 01:53:36 +08:00
|
|
|
if (icon === undefined) {
|
|
|
|
//Missing
|
|
|
|
node = document.createElement('i');
|
|
|
|
node.classList.add('material-icons', 'icon');
|
|
|
|
node.innerText = 'help_outline';
|
|
|
|
} else if (icon instanceof HTMLElement) {
|
|
|
|
//Node
|
|
|
|
node = icon
|
2020-03-05 03:56:17 +08:00
|
|
|
} else if (icon === null) {
|
|
|
|
//Node
|
|
|
|
node = document.createElement('i');
|
|
|
|
node.classList.add('fa_big', 'icon');
|
|
|
|
|
2020-01-24 01:53:36 +08:00
|
|
|
} else if (icon.substr(0, 2) === 'fa') {
|
|
|
|
//Font Awesome
|
|
|
|
node = document.createElement('i');
|
|
|
|
node.classList.add('fa_big', 'icon');
|
|
|
|
if (icon.substr(3, 1) === '.') {
|
|
|
|
node.classList.add(icon.substr(0, 3), icon.substr(4));
|
|
|
|
} else {
|
|
|
|
node.classList.add('fa', icon);
|
|
|
|
}
|
|
|
|
} else if (icon.substr(0, 5) === 'icon-') {
|
|
|
|
//Icomoon
|
|
|
|
node = document.createElement('i');
|
|
|
|
node.classList.add(icon, 'icon');
|
|
|
|
} else if (icon.substr(0, 14) === 'data:image/png') {
|
|
|
|
//Data URL
|
|
|
|
node = document.createElement('img');
|
|
|
|
node.classList.add('icon');
|
|
|
|
node.src = icon;
|
|
|
|
} else {
|
|
|
|
//Material Icon
|
|
|
|
node = document.createElement('i');
|
|
|
|
node.classList.add('material-icons', 'icon');
|
|
|
|
node.innerText = icon;
|
|
|
|
}
|
|
|
|
if (color) {
|
|
|
|
if (color === 'x') {
|
|
|
|
node.classList.add('color_x');
|
|
|
|
} else if (color === 'y') {
|
|
|
|
node.classList.add('color_y');
|
|
|
|
} else if (color === 'z') {
|
|
|
|
node.classList.add('color_z');
|
|
|
|
} else if (typeof color === 'string') {
|
|
|
|
node.style.color = color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
showQuickMessage(message, time) {
|
2018-12-27 21:03:04 +08:00
|
|
|
$('#quick_message_box').remove()
|
2018-10-18 01:50:25 +08:00
|
|
|
var quick_message_box = $('<div id="quick_message_box" class="hidden"></div>')
|
|
|
|
$('body').append(quick_message_box)
|
|
|
|
|
|
|
|
quick_message_box.text(tl(message))
|
2019-07-18 00:02:07 +08:00
|
|
|
quick_message_box.fadeIn(0)
|
2018-10-18 01:50:25 +08:00
|
|
|
setTimeout(function() {
|
2019-07-18 00:02:07 +08:00
|
|
|
quick_message_box.fadeOut(0)
|
2018-10-18 01:50:25 +08:00
|
|
|
setTimeout(function() {
|
|
|
|
quick_message_box.remove()
|
2019-07-18 00:02:07 +08:00
|
|
|
}, 1)
|
2018-10-18 01:50:25 +08:00
|
|
|
}, time ? time : 1000)
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2021-02-28 19:51:15 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {object} options Options
|
|
|
|
* @param {string} options.text Text Message
|
|
|
|
* @param {string} [options.icon] Blockbench icon string
|
|
|
|
* @param {number} [options.expire] Expire time in miliseconds
|
|
|
|
* @param {string} [options.color] Background color, accepts any CSS color string
|
2021-05-06 03:04:16 +08:00
|
|
|
* @param {function} [options.click] Method to run on click. Return `true` to close toast
|
2021-02-28 19:51:15 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
showToastNotification(options) {
|
|
|
|
let notification = document.createElement('li');
|
|
|
|
notification.className = 'toast_notification';
|
|
|
|
if (options.icon) {
|
|
|
|
let icon = Blockbench.getIconNode(options.icon);
|
|
|
|
notification.append(icon);
|
|
|
|
}
|
|
|
|
let text = document.createElement('span');
|
|
|
|
text.innerText = tl(options.text);
|
|
|
|
notification.append(text);
|
|
|
|
|
|
|
|
let close_button = document.createElement('div');
|
|
|
|
close_button.innerHTML = '<i class="material-icons">clear</i>';
|
|
|
|
close_button.className = 'toast_close_button';
|
|
|
|
close_button.addEventListener('click', (event) => {
|
|
|
|
notification.remove();
|
|
|
|
})
|
|
|
|
notification.append(close_button);
|
|
|
|
|
|
|
|
if (options.color) {
|
|
|
|
notification.style.backgroundColor = options.color;
|
|
|
|
}
|
|
|
|
if (typeof options.click == 'function') {
|
|
|
|
notification.addEventListener('click', (event) => {
|
|
|
|
if (event.target == close_button || event.target.parentElement == close_button) return;
|
|
|
|
let result = options.click(event);
|
|
|
|
if (result == true) {
|
|
|
|
notification.remove();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
notification.style.cursor = 'pointer';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.expire) {
|
|
|
|
setTimeout(() => {
|
|
|
|
notification.remove();
|
|
|
|
}, options.expire);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('toast_notification_list').append(notification);
|
|
|
|
|
|
|
|
function deletableToast(node) {
|
|
|
|
this.delete = function() {
|
|
|
|
node.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new deletableToast(notification);
|
2019-12-16 03:04:31 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
showStatusMessage(message, time) {
|
2018-11-12 04:19:08 +08:00
|
|
|
Blockbench.setStatusBarText(tl(message))
|
|
|
|
setTimeout(function() {
|
|
|
|
Blockbench.setStatusBarText()
|
2019-07-18 00:02:07 +08:00
|
|
|
}, time ? time : 800)
|
|
|
|
},
|
2018-11-12 04:19:08 +08:00
|
|
|
setStatusBarText(text) {
|
2020-01-24 01:53:36 +08:00
|
|
|
if (text !== undefined) {
|
2018-11-12 04:19:08 +08:00
|
|
|
Prop.file_name = text
|
|
|
|
} else {
|
|
|
|
Prop.file_name = Prop.file_name_alt||''
|
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-12-27 21:03:04 +08:00
|
|
|
setProgress(progress, time, bar) {
|
|
|
|
setProgressBar(bar, progress||0, time)
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2017-10-27 01:00:52 +08:00
|
|
|
showMessage(message, location) {
|
|
|
|
if (location === 'status_bar') {
|
2018-10-18 01:50:25 +08:00
|
|
|
Blockbench.showStatusMessage(message)
|
2017-10-27 01:00:52 +08:00
|
|
|
} else if (location === 'center') {
|
2018-10-18 01:50:25 +08:00
|
|
|
Blockbench.showQuickMessage(message)
|
2017-10-27 01:00:52 +08:00
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2020-10-27 02:56:00 +08:00
|
|
|
showMessageBox(options = 0, cb) {
|
2017-11-17 05:23:41 +08:00
|
|
|
|
|
|
|
if (options.confirm === undefined) options.confirm = 0
|
2020-10-27 02:56:00 +08:00
|
|
|
if (options.cancel === undefined) options.cancel = (options.buttons && options.buttons.length) ? options.buttons.length-1 : 0;
|
2018-10-18 01:50:25 +08:00
|
|
|
if (!options.buttons) options.buttons = [tl('dialog.ok')]
|
|
|
|
|
|
|
|
if (options.translateKey) {
|
|
|
|
if (!options.title) options.title = tl('message.'+options.translateKey+'.title')
|
|
|
|
if (!options.message) options.message = tl('message.'+options.translateKey+'.message')
|
|
|
|
}
|
2017-11-17 05:23:41 +08:00
|
|
|
|
2021-01-27 06:27:11 +08:00
|
|
|
var jq_dialog = $('<dialog class="dialog paddinged" style="width: auto;" id="message_box"><div class="dialog_handle">'+tl(options.title)+'</div></dialog>')
|
2017-11-17 05:23:41 +08:00
|
|
|
|
|
|
|
jq_dialog.append('<div class="dialog_bar" style="height: auto; min-height: 56px; margin-bottom: 16px;">'+
|
2019-07-18 00:02:07 +08:00
|
|
|
marked(tl(options.message))+'</div>'
|
2017-11-17 05:23:41 +08:00
|
|
|
)
|
2018-10-18 01:50:25 +08:00
|
|
|
if (options.icon) {
|
|
|
|
jq_dialog.find('.dialog_bar').prepend($(Blockbench.getIconNode(options.icon)).addClass('message_box_icon'))
|
|
|
|
}
|
2017-11-17 05:23:41 +08:00
|
|
|
|
2021-06-09 05:33:40 +08:00
|
|
|
function close(button) {
|
|
|
|
hideDialog();
|
|
|
|
setTimeout(function() {
|
|
|
|
jq_dialog.remove();
|
|
|
|
},200)
|
|
|
|
if (cb) {
|
|
|
|
cb(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 05:23:41 +08:00
|
|
|
var buttons = []
|
|
|
|
|
|
|
|
options.buttons.forEach(function(b, i) {
|
2019-07-18 00:02:07 +08:00
|
|
|
var btn = $('<button type="button">'+tl(b)+'</button>')
|
2017-11-17 05:23:41 +08:00
|
|
|
btn.click(function(e) {
|
2021-06-09 05:33:40 +08:00
|
|
|
close(i);
|
2017-11-17 05:23:41 +08:00
|
|
|
})
|
2021-06-09 05:33:40 +08:00
|
|
|
buttons.push(btn);
|
2017-11-17 05:23:41 +08:00
|
|
|
})
|
2018-10-18 01:50:25 +08:00
|
|
|
jq_dialog.hide = function() {
|
|
|
|
$(jq_dialog.find('button').get(options.cancel)).click()
|
|
|
|
}
|
2017-11-17 05:23:41 +08:00
|
|
|
buttons[options.confirm].addClass('confirm_btn')
|
|
|
|
buttons[options.cancel].addClass('cancel_btn')
|
2018-12-03 02:37:06 +08:00
|
|
|
jq_dialog.append($('<div class="dialog_bar button_bar"></div>').append(buttons))
|
2019-08-18 00:26:14 +08:00
|
|
|
buttons.forEach(b => {
|
|
|
|
b.after(' ')
|
|
|
|
})
|
2017-11-17 05:23:41 +08:00
|
|
|
|
|
|
|
|
2018-10-18 01:50:25 +08:00
|
|
|
jq_dialog.addClass('draggable')
|
|
|
|
jq_dialog.draggable({
|
2019-09-06 06:16:54 +08:00
|
|
|
handle: ".dialog_handle",
|
|
|
|
containment: '#page_wrapper'
|
2018-10-18 01:50:25 +08:00
|
|
|
})
|
2021-01-31 07:12:53 +08:00
|
|
|
var x = (window.innerWidth-540)/2
|
2018-10-18 01:50:25 +08:00
|
|
|
jq_dialog.css('left', x+'px')
|
|
|
|
jq_dialog.css('position', 'absolute')
|
2017-11-17 05:23:41 +08:00
|
|
|
|
2021-02-20 20:21:11 +08:00
|
|
|
$('#dialog_wrapper').append(jq_dialog)
|
2019-07-18 00:02:07 +08:00
|
|
|
$('.dialog').hide()
|
|
|
|
$('#blackout').show()
|
|
|
|
jq_dialog.show()
|
2018-10-18 01:50:25 +08:00
|
|
|
|
2021-01-31 07:12:53 +08:00
|
|
|
jq_dialog.css('top', limitNumber(window.innerHeight/2-jq_dialog.height()/2 - 140, 0, 2000)+'px')
|
2018-10-18 01:50:25 +08:00
|
|
|
if (options.width) {
|
|
|
|
jq_dialog.css('width', options.width+'px')
|
|
|
|
} else {
|
|
|
|
jq_dialog.css('width', limitNumber(options.buttons.length*170+44, 380, 894)+'px')
|
|
|
|
}
|
|
|
|
open_dialog = 'message_box'
|
2021-06-09 05:33:40 +08:00
|
|
|
open_interface = {
|
|
|
|
confirm() {
|
|
|
|
close(options.confirm);
|
|
|
|
},
|
|
|
|
cancel() {
|
|
|
|
close(options.cancel);
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 01:50:25 +08:00
|
|
|
return jq_dialog
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
textPrompt(title, value, callback) {
|
|
|
|
showDialog('text_input')
|
2018-11-12 04:19:08 +08:00
|
|
|
$('#text_input h2').text(tl(title))
|
2018-10-18 01:50:25 +08:00
|
|
|
$('#text_input input#text_input_field').val(value).select()
|
|
|
|
$('#text_input button.confirm_btn').off()
|
|
|
|
$('#text_input button.confirm_btn').click(function() {
|
|
|
|
var s = $('#text_input input#text_input_field').val()
|
|
|
|
if (callback !== undefined) {
|
|
|
|
callback(s)
|
|
|
|
}
|
|
|
|
})
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
addMenuEntry(name, icon, click) {
|
2021-03-04 00:50:30 +08:00
|
|
|
console.warn('Blockbench.addMenuEntry is deprecated. Please use Actions instead.')
|
|
|
|
let id = name.replace(/\s/g, '').toLowerCase();
|
|
|
|
var action = new Action(id, {icon: icon, name: name, click: click})
|
2019-02-04 04:09:35 +08:00
|
|
|
MenuBar.addAction(action, 'filter')
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
removeMenuEntry(name) {
|
2021-03-04 00:50:30 +08:00
|
|
|
let id = name.replace(/\s/g, '').toLowerCase();
|
|
|
|
MenuBar.removeAction('filter.'+id);
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
openLink(link) {
|
|
|
|
if (isApp) {
|
|
|
|
shell.openExternal(link)
|
|
|
|
} else {
|
|
|
|
window.open(link)
|
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
|
|
|
notification(title, text, icon) {
|
|
|
|
Notification.requestPermission().then(status => {
|
|
|
|
if (status == 'granted') {
|
|
|
|
let n = new Notification(title, {body: text, icon: icon||'favicon.png'})
|
|
|
|
n.onclick = function() {
|
|
|
|
if (isApp) {
|
|
|
|
currentwindow.focus();
|
|
|
|
} else {
|
|
|
|
window.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2020-11-28 05:59:52 +08:00
|
|
|
//CSS
|
|
|
|
addCSS(css) {
|
|
|
|
let style_node = document.createElement('style');
|
|
|
|
style_node.type ='text/css';
|
|
|
|
style_node.appendChild(document.createTextNode(css));
|
|
|
|
document.getElementsByTagName('head')[0].appendChild(style_node);
|
|
|
|
function deletableStyle(node) {
|
|
|
|
this.delete = function() {
|
|
|
|
node.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new deletableStyle(style_node);
|
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
//Flags
|
|
|
|
addFlag(flag) {
|
2018-12-03 02:37:06 +08:00
|
|
|
this.flags[flag] = true
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
removeFlag(flag) {
|
2018-12-03 02:37:06 +08:00
|
|
|
delete this.flags[flag]
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
hasFlag(flag) {
|
2018-12-03 02:37:06 +08:00
|
|
|
return this.flags[flag]
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2018-10-18 01:50:25 +08:00
|
|
|
//Events
|
2019-04-08 00:53:33 +08:00
|
|
|
dispatchEvent(event_name, data) {
|
|
|
|
var list = this.events[event_name]
|
|
|
|
if (!list) return;
|
|
|
|
for (var i = 0; i < list.length; i++) {
|
|
|
|
if (typeof list[i] === 'function') {
|
|
|
|
list[i](data)
|
2017-10-27 01:00:52 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2020-10-22 00:31:26 +08:00
|
|
|
addListener(event_names, cb) {
|
|
|
|
event_names.split(' ').forEach(event_name => {
|
|
|
|
if (!this.events[event_name]) {
|
|
|
|
this.events[event_name] = [];
|
|
|
|
}
|
|
|
|
this.events[event_name].safePush(cb);
|
|
|
|
})
|
2020-05-31 21:54:04 +08:00
|
|
|
return Blockbench;
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2019-04-08 00:53:33 +08:00
|
|
|
on(event_name, cb) {
|
|
|
|
return Blockbench.addListener(event_name, cb)
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2017-10-27 01:00:52 +08:00
|
|
|
removeListener(event_name, cb) {
|
2019-04-08 00:53:33 +08:00
|
|
|
if (!this.events[event_name]) return;
|
|
|
|
this.events[event_name].remove(cb);
|
2019-07-18 00:02:07 +08:00
|
|
|
},
|
2021-01-24 06:24:14 +08:00
|
|
|
onUpdateTo(version, callback) {
|
2021-01-24 07:09:44 +08:00
|
|
|
if (LastVersion && compareVersions(version, LastVersion) && !Blockbench.isOlderThan(version)) {
|
2021-01-24 06:24:14 +08:00
|
|
|
callback(LastVersion);
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 07:56:13 +08:00
|
|
|
};
|
2019-09-06 07:47:49 +08:00
|
|
|
|
|
|
|
(function() {
|
2021-01-24 06:24:14 +08:00
|
|
|
if (!LastVersion || LastVersion.replace(/.\d+$/, '') != appVersion.replace(/.\d+$/, '')) {
|
2019-09-06 07:47:49 +08:00
|
|
|
Blockbench.addFlag('after_update');
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2019-07-18 00:02:07 +08:00
|
|
|
if (isApp) {
|
|
|
|
Blockbench.platform = process.platform;
|
|
|
|
switch (Blockbench.platform) {
|
|
|
|
case 'win32': Blockbench.operating_system = 'Windows'; break;
|
|
|
|
case 'darwin': Blockbench.operating_system = 'macOS'; break;
|
|
|
|
default: Blockbench.operating_system = 'Linux'; break;
|
|
|
|
}
|
|
|
|
if (Blockbench.platform.includes('win32') === true) osfs = '\\';
|
2017-10-27 01:00:52 +08:00
|
|
|
}
|
|
|
|
|
2020-04-26 02:25:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
const StateMemory = {
|
|
|
|
init(key, type) {
|
|
|
|
let saved = localStorage.getItem(`StateMemory.${key}`)
|
|
|
|
if (typeof saved == 'string') {
|
|
|
|
try {
|
|
|
|
saved = JSON.parse(saved)
|
|
|
|
} catch (err) {
|
|
|
|
localStorage.clearItem(`StateMemory.${key}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( saved !== null && (typeof saved == type || (type == 'array' && saved instanceof Array)) ) {
|
|
|
|
StateMemory[key] = saved;
|
|
|
|
} else {
|
|
|
|
StateMemory[key] = (() => {switch (type) {
|
|
|
|
case 'string': return ''; break;
|
|
|
|
case 'number': return 0; break;
|
|
|
|
case 'boolean': return false; break;
|
|
|
|
case 'object': return {}; break;
|
|
|
|
case 'array': return []; break;
|
|
|
|
}})();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
save(key) {
|
|
|
|
let serialized = JSON.stringify(StateMemory[key])
|
|
|
|
localStorage.setItem(`StateMemory.${key}`, serialized)
|
|
|
|
}
|
|
|
|
}
|