diff --git a/plugins/draw-tools.user.js b/plugins/draw-tools.user.js index 702a6181..64a1aa1c 100644 --- a/plugins/draw-tools.user.js +++ b/plugins/draw-tools.user.js @@ -281,8 +281,12 @@ window.plugin.drawTools.manualOpt = function() { //TODO: add line style choosers: thickness, maybe dash styles? + '' + '
' - + 'Copy/Export Drawn Items' - + 'Paste/Import Drawn Items' + + 'Copy Drawn Items' + + 'Paste Drawn Items' + + (window.requestFile != undefined + ? 'Import Drawn Items' : '') + + ((typeof android !== 'undefined' && android && android.saveFile) // saveFile only exists on Kitkat+! + ? 'Export Drawn Items' : '') + 'Reset Drawn Items' + '
'; @@ -327,6 +331,12 @@ window.plugin.drawTools.optCopy = function() { } } +window.plugin.drawTools.optExport = function() { + if(typeof android !== 'undefined' && android && android.saveFile) { // saveFile only exists on Kitkat+! + android.saveFile('IITC-drawn-items.json', 'application/json', localStorage['plugin-draw-tools-layer']); + } +} + window.plugin.drawTools.optPaste = function() { var promptAction = prompt('Press CTRL+V to paste it.', ''); if(promptAction !== null && promptAction !== '') { @@ -343,10 +353,28 @@ window.plugin.drawTools.optPaste = function() { console.warn('DRAWTOOLS: failed to import data: '+e); window.plugin.drawTools.optAlert('Import failed'); } - } } +window.plugin.drawTools.optImport = function() { + if (window.requestFile === undefined) return; + window.requestFile(function(filename, content) { + try { + var data = JSON.parse(content); + window.plugin.drawTools.drawnItems.clearLayers(); + window.plugin.drawTools.import(data); + console.log('DRAWTOOLS: reset and imported drawn tiems'); + window.plugin.drawTools.optAlert('Import Successful.'); + + // to write back the data to localStorage + window.plugin.drawTools.save(); + } catch(e) { + console.warn('DRAWTOOLS: failed to import data: '+e); + window.plugin.drawTools.optAlert('Import failed'); + } + }); +} + window.plugin.drawTools.optReset = function() { var promptAction = confirm('All drawn items will be deleted. Are you sure?', ''); if(promptAction) {