// localStorage.setItem("lpe-cache",`{}`) const FileManager = (() => { // Binding the browse holder change event to file loading const browseHolder = document.getElementById('open-image-browse-holder'); const browsePaletteHolder = document.getElementById('load-palette-browse-holder'); Events.on('change', browseHolder, loadFile); Events.on('change', browsePaletteHolder, loadPalette); Events.on("click", "save-project-confirm", saveProject); function openSaveProjectWindow() { //create name let selectedPalette = Util.getText('palette-button'); if (selectedPalette != 'Choose a palette...'){ var paletteAbbreviation = palettes[selectedPalette].abbreviation; var fileName = 'pixel-'+paletteAbbreviation+'-'+currFile.canvasSize[0]+'x'+currFile.canvasSize[1]; } else { var fileName = 'pixel-'+currFile.canvasSize[0]+'x'+currFile.canvasSize[1]; selectedPalette = 'none'; } Util.setValue('lpe-file-name', fileName); Dialogue.showDialogue('save-project', false); } function openPixelExportWindow() { let selectedPalette = Util.getText('palette-button'); if (selectedPalette != 'Choose a palette...'){ var paletteAbbreviation = palettes[selectedPalette].name; var fileName = 'pixel-'+paletteAbbreviation+'-'+canvasSize[0]+'x'+canvasSize[1]+'.png'; } else { var fileName = 'pixel-'+currFile.canvasSize[0]+'x'+currFile.canvasSize[1]+'.png'; selectedPalette = 'none'; } Util.setValue('export-file-name', fileName); Events.on("click", "export-confirm", exportProject); Dialogue.showDialogue('export', false); } function saveProject() { // Get name // debugger; let fileName = Util.getValue("lpe-file-name") + ".lpe"; let selectedPalette = Util.getText('palette-button'); //set download link const linkHolder = document.getElementById('save-project-link-holder'); // create file content const content = getProjectData(); linkHolder.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(content); linkHolder.download = fileName; linkHolder.click(); if (typeof ga !== 'undefined') ga('send', 'event', 'Pixel Editor Save', selectedPalette, currFile.canvasSize[0]+'/'+currFile.canvasSize[1]); /*global ga*/ LayerList.closeOptionsMenu(); // is this the right place for this? } function exportProject() { if (EditorState.documentCreated()) { //create name let fileName = Util.getValue("export-file-name"); //set download link let linkHolder = document.getElementById('save-image-link-holder'); // Creating a tmp canvas to flatten everything let exportCanvas = document.createElement("canvas"); let emptyCanvas = document.createElement("canvas"); let layersCopy = currFile.layers.slice(); exportCanvas.width = currFile.canvasSize[0]; exportCanvas.height = currFile.canvasSize[1]; emptyCanvas.width = currFile.canvasSize[0]; emptyCanvas.height = currFile.canvasSize[1]; // Sorting the layers by z index layersCopy.sort((a, b) => (a.canvas.style.zIndex > b.canvas.style.zIndex) ? 1 : -1); // Merging every layer on the export canvas for (let i=0; i{ // let layerImage = layerData.src; // if (layerData != null) { // // Setting id // let createdLayer = LayerList.addLayer(layerData.id, false, layerData.name); // // Setting name // createdLayer.menuEntry.getElementsByTagName("p")[0].innerHTML = layerData.name; // // Adding the image (I can do that because they're sorted by increasing z-index) // let img = new Image(); // img.onload = function() { // createdLayer.context.drawImage(img, 0, 0); // createdLayer.updateLayerPreview(); // }; // img.src = layerImage; // // Setting visibility and lock options // if (!layerData.isVisible) { // createdLayer.hide(); // } // if (layerData.isLocked) { // createdLayer.lock(); // } // } // }) if(dictionary.colors)ColorModule.createColorPalette(dictionary.colors); } function getProjectData() { // use a dictionary let dictionary = {}; // sorting layers by increasing z-index let layersCopy = currFile.layers.filter(n=>!!n.menuEntry).slice(); dictionary['canvasWidth'] = currFile.canvasSize[0]; dictionary['canvasHeight'] = currFile.canvasSize[1]; dictionary['editorMode'] = EditorState.getCurrentMode(); dictionary.colors = [ ...ColorModule.getCurrentPalette() ]; dictionary.layers = layersCopy .map((n,i)=>{ //console.log('n.name === ',n.name); if(n.isSelected)dictionary.selectedLayer = i; return { ...n, src: n.canvas.toDataURL(), }; }); return JSON.stringify(dictionary); } function loadPalette() { if (browsePaletteHolder.files && browsePaletteHolder.files[0]) { //make sure file is allowed filetype var fileContentType = browsePaletteHolder.files[0].type; if (fileContentType == 'image/png' || fileContentType == 'image/gif') { //load file var fileReader = new FileReader(); fileReader.onload = function(e) { var img = new Image(); img.onload = function() { //draw image onto the temporary canvas var loadPaletteCanvas = document.getElementById('load-palette-canvas-holder'); var loadPaletteContext = loadPaletteCanvas.getContext('2d'); loadPaletteCanvas.width = img.width; loadPaletteCanvas.height = img.height; loadPaletteContext.drawImage(img, 0, 0); //create array to hold found colors var colorPalette = []; var imagePixelData = loadPaletteContext.getImageData(0,0,this.width, this.height).data; //loop through pixels looking for colors to add to palette for (var i = 0; i < imagePixelData.length; i += 4) { const newColor = {r:imagePixelData[i],g:imagePixelData[i + 1],b:imagePixelData[i + 2]}; var color = '#' + Color.rgbToHex(newColor); if (colorPalette.indexOf(color) == -1) { colorPalette.push(color); } } //add to palettes so that it can be loaded when they click okay palettes['Loaded palette'] = {}; palettes['Loaded palette'].colors = colorPalette; Util.setText('palette-button', 'Loaded palette'); Util.setText('palette-button-splash', 'Loaded palette'); Util.toggle('palette-menu-splash'); }; img.src = e.target.result; }; fileReader.readAsDataURL(browsePaletteHolder.files[0]); } else alert('Only PNG and GIF files are supported at this time.'); } browsePaletteHolder.value = null; } function upgradeLPE(dictionary) { ////console.log('dictionary === ',dictionary); if(dictionary.color0 && !dictionary.colors) { dictionary.colors = []; let colorIdx = 0; while(dictionary[`color${colorIdx}`]) { dictionary.colors.push(dictionary[`color${colorIdx}`]); delete dictionary[`color${colorIdx}`]; colorIdx++; } dictionary.layers = Object.keys(dictionary).reduce((r,k,i)=>{ if(k.slice(0,5) === "layer"){ if(dictionary[k].isSelected){ dictionary.selectedLayer = r.length; } r.push({ ...dictionary[k], src: dictionary[`${k}ImageData`] }); delete dictionary[k]; delete dictionary[`${k}ImageData`]; } return r; },[]); } return dictionary; } function toggleCache(elm){ console.log('elm === ',elm); FileManager.cacheEnabled = !FileManager.cacheEnabled; localStorage.setItem("lpe-cache-enabled",FileManager.cacheEnabled ? "1" : "0"); elm.textContent = cacheBtnText(FileManager.cacheEnabled); } function cacheBtnText(cacheEnabled) { return `${cacheEnabled ? "Disable" : "Enable"} auto-cache`; } const cacheEnabled = !!Number(localStorage.getItem("lpe-cache-enabled")); document.getElementById("auto-cache-button").textContent = cacheBtnText(cacheEnabled); return { cacheEnabled, loadFromLPE, toggleCache, getProjectData, localStorageReset, localStorageCheck, localStorageSave, localStorageLoad, upgradeLPE, defaultLPE, saveProject, openProject, exportProject, openPixelExportWindow, openSaveProjectWindow, open }; })();