diff --git a/notes.md b/notes.md index 86c2cb4f..23597b04 100644 --- a/notes.md +++ b/notes.md @@ -10,7 +10,6 @@ * `P` client/worker interface normalization * `P` explore widget vertex reloading / replacing (Onshape primarily) -* `F` add profile only download / restore like devices * `F` bundle current profile into exported device * `F` show slider range values in workspace units (on hover?) * `F` allow select of a range by typing in values in slices or workspace units diff --git a/src/kiri/init.js b/src/kiri/init.js index f45a19a0..5685e91a 100644 --- a/src/kiri/init.js +++ b/src/kiri/init.js @@ -549,7 +549,7 @@ if (reply) { let res = JSON.parse(reply); if (res && res.ver && res.rec) { - let set = JSON.parse(atob(res.rec)); + let set = API.util.b64dec(res.rec); set.id = res.space; set.ver = res.ver; API.conf.put(set); @@ -563,7 +563,7 @@ } function storeSettingsToServer(display) { - let set = btoa(JSON.stringify(settings())); + let set = API.util.b64enc(settings()); new moto.Ajax(function(reply) { if (reply) { let res = JSON.parse(reply); @@ -586,10 +586,7 @@ .replace(/\./g,'_'); UC.prompt("Export Device Filename", name).then(name => { if (name) { - let blob = new Blob([exp], {type: "octet/stream"}), - url = WIN.URL.createObjectURL(blob); - $('mod-any').innerHTML = `x`; - $('sexport').click(); + API.util.download(exp, `${name}.km`); } }); } @@ -601,13 +598,7 @@ if (name.toLowerCase().indexOf(".stl") < 0) { name = `${name}.stl`; } - let xprt = API.selection.export(), - blob = new Blob([xprt], {type: "octet/stream"}), - url = WIN.URL.createObjectURL(blob); - $('mod-any').innerHTML = [ - `x` - ].join(''); - $('sexport').click(); + API.util.download(API.selection.export(), name); }); } @@ -628,13 +619,8 @@ UC.confirm("Export Filename", {ok:true, cancel: false}, "workspace", opt).then(name => { if (name) { let work = $('incwork').checked, - json = API.conf.export({work}), - blob = new Blob([json], {type: "octet/stream"}), - url = WIN.URL.createObjectURL(blob); - $('mod-any').innerHTML = [ - `x` - ].join(''); - $('sexport').click(); + json = API.conf.export({work}); + API.util.download(json, `${name}.km`); } }); } @@ -994,13 +980,13 @@ showDevices(); }; UI.deviceExport.onclick = function() { - let exp = btoa(JSON.stringify({ + let exp = API.util.b64enc({ version: KIRI.version, device: selected, process: API.process.code(), code: devs[selected], time: Date.now() - })); + }); deviceExport(exp, selected); }; diff --git a/src/kiri/main.js b/src/kiri/main.js index 5bde4966..5b3ccc08 100644 --- a/src/kiri/main.js +++ b/src/kiri/main.js @@ -340,7 +340,10 @@ util: { isSecure, ui2rec: updateSettingsFromFields, - rec2ui: updateFieldsFromSettings + rec2ui: updateFieldsFromSettings, + download: downloadBlob, + b64enc: obj => { return base64js.fromByteArray(new TextEncoder().encode(JSON.stringify(obj))) }, + b64dec: obj => { return JSON.parse(new TextDecoder().decode(base64js.toByteArray(obj))) } }, view: { get: function() { return viewMode }, @@ -2221,18 +2224,19 @@ function settingsImport(data, ask) { if (typeof(data) === 'string') { try { - data = JSON.parse(new TextDecoder().decode(base64js.toByteArray(data))); + data = API.util.b64dec(data); } catch (e) { - UC.alert('invalid settings format'); + UC.alert('invalid import format'); console.log('data',data); return; } } if (LOCAL) console.log('import',data); let isSettings = (data.settings && data.version && data.time); + let isProcess = (data.process && data.version && data.time && data.mode && data.name); let isDevice = (data.device && data.version && data.time); let isWork = (data.work); - if (!isSettings && !isDevice) { + if (!isSettings && !isDevice && !isProcess) { UC.alert('invalid settings or device format'); console.log('data',data); return; @@ -2251,6 +2255,19 @@ API.show.devices(); } } + if (isProcess) { + if (settings.sproc[data.mode][data.name]) { + UC.confirm(`Replace process ${data.name}?`).then(yes => { + if (yes) { + settings.sproc[data.mode][data.name] = data.process; + API.conf.show(); + } + }); + } else { + settings.sproc[data.mode][data.name] = data.process; + API.conf.show(); + } + } if (isSettings) { settings = CONF.normalize(data.settings); SDB.setItem('ws-settings', JSON.stringify(settings)); @@ -2273,7 +2290,8 @@ if (ask) { let opt = {}; let prompt = isDevice ? - `Import device "${data.device}"?` : + `Import device "${data.device}"?` : isProcess ? + `Import process "${data.name}"?` : `Import settings made in Kiri:Moto version ${data.version} on
${new Date(data.time)}?`; if (data.screen) { opt.pre = [ @@ -2296,7 +2314,7 @@ const shot = opts.work || opts.screen ? SPACE.screenshot() : undefined; const work = opts.work ? KIRI.codec.encode(WIDGETS,{_json_:true}) : undefined; const view = opts.work ? SPACE.view.save() : undefined; - return base64js.fromByteArray(new TextEncoder().encode(JSON.stringify({ + return API.util.b64enc({ settings: settings, version: KIRI.version, screen: shot, @@ -2307,7 +2325,7 @@ moto: MOTO.id, init: SDB.getItem('kiri-init'), time: Date.now() - }))); + }); } function platformLoadFiles(files,group) { @@ -2559,6 +2577,24 @@ } } + function exportSettings(e) { + let mode = getMode(), + name = e.target.getAttribute("name"), + data = API.util.b64enc({ + process: settings.sproc[mode][name], + version: KIRI.version, + moto: MOTO.id, + time: Date.now(), + mode, + name + }); + UC.prompt("Export Process Filename", name).then(name => { + if (name) { + API.util.download(data, `${name}.km`); + } + }); + } + function loadSettings(e, named) { let mode = getMode(), name = e ? e.target.getAttribute("load") : named || currentProcessName() || "default", @@ -2605,6 +2641,7 @@ let row = DOC.createElement('div'), load = DOC.createElement('button'), edit = DOC.createElement('button'), + xprt = DOC.createElement('button'), del = DOC.createElement('button'), name = sk; @@ -2629,9 +2666,15 @@ edit.innerHTML = ''; edit.onclick = editSettings; + xprt.setAttribute('name', sk); + xprt.setAttribute('title', 'export'); + xprt.innerHTML = ''; + xprt.onclick = exportSettings; + row.setAttribute("class", "flow-row"); row.appendChild(edit); row.appendChild(load); + row.appendChild(xprt); row.appendChild(del); table.appendChild(row); }); @@ -2843,6 +2886,12 @@ $('mid-rcol').style.display = show ? 'flex' : 'none'; } + function downloadBlob(data, filename) { + let url = WIN.URL.createObjectURL(new Blob([data], {type: "octet/stream"})); + $('mod-any').innerHTML = `x`; + $('_dexport_').click(); + } + // prevent safari from exiting full screen mode DOC.onkeydown = function (evt) { if (evt.keyCode == 27) evt.preventDefault() }