Skip to content

Commit

Permalink
#54
Browse files Browse the repository at this point in the history
  • Loading branch information
odrick committed Apr 28, 2021
1 parent 6f7ae7a commit df08bff
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#

## IMPORTANT: I don't have time to imporove this app. Only critical bugs will be fixed.
## IMPORTANT: I don't have time to imporove this app anymore. Only critical bugs will be fixed.

#

Expand Down
125 changes: 64 additions & 61 deletions src/client/platform/electron/FileSystem.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const fs = require('fs');
const chokidar = require('chokidar');
const {dialog} = require('electron').remote;
const { dialog } = require('electron').remote;

import Controller from 'platform/Controller';
import I18 from '../../utils/I18';
import Base64ImagesLoader from '../../utils/Base64ImagesLoader';
import {Observer, GLOBAL_EVENT} from '../../Observer';
import { Observer, GLOBAL_EVENT } from '../../Observer';

const IMAGES_EXT = ['jpg', 'png', 'gif'];

Expand All @@ -15,21 +15,21 @@ class FileSystem {
static fixPath(path) {
return path.split("\\").join("/");
}

static getExtFromPath(path) {
return path.split(".").pop().toLowerCase();
}

static selectFolder() {
let dir = dialog.showOpenDialog({
properties: ['openDirectory']
});
return dir;
}
static getFolderFilesList(dir, base="", list=[]) {

static getFolderFilesList(dir, base = "", list = []) {
let files = fs.readdirSync(dir);
for(let file of files) {
for (let file of files) {
if (fs.statSync(dir + file).isDirectory() && (dir + file).toUpperCase().indexOf("__MACOSX") < 0) {
list = FileSystem.getFolderFilesList(dir + file + '/', base + file + "/", list);
}
Expand All @@ -43,16 +43,16 @@ class FileSystem {

return list;
}

static addImages(cb) {
let list = dialog.showOpenDialog({
filters: [{name: I18.f("IMAGES"), extensions: IMAGES_EXT}],
filters: [{ name: I18.f("IMAGES"), extensions: IMAGES_EXT }],
properties: ['openFile', 'multiSelections']
});
if(list) {

if (list) {
let files = [];
for(let path of list) {
for (let path of list) {
path = FileSystem.fixPath(path);
let name = path.split("/").pop();

Expand All @@ -66,7 +66,7 @@ class FileSystem {
FileSystem.loadImages(files, cb);
}
else {
if(cb) cb();
if (cb) cb();
}
}

Expand All @@ -75,71 +75,74 @@ class FileSystem {
properties: ['openDirectory']
});

if(dir && dir.length) {
if (dir && dir.length) {
let path = FileSystem.fixPath(dir[0]);
FileSystem.loadFolder(path, cb);
}
else {
if(cb) cb();
if (cb) cb();
}
}

static startWatch(path) {
if(!watcher) {
watcher = chokidar.watch(path, {ignoreInitial: true});
watcher.on('all', FileSystem.onWatchEvent);
}
else {
watcher.add(path);
try {
if (!watcher) {
watcher = chokidar.watch(path, { ignoreInitial: true });
watcher.on('all', FileSystem.onWatchEvent);
}
else {
watcher.add(path);
}
}
catch (e) { }
}

static stopWatch(path) {
if(watcher) {
if (watcher) {
watcher.unwatch(path);
}
}

static terminateWatch() {
if(watcher) {
if (watcher) {
watcher.close();
watcher = null;
}
}

static onWatchEvent(event, path) {
Observer.emit(GLOBAL_EVENT.FS_CHANGES, {event: event, path: FileSystem.fixPath(path)});
Observer.emit(GLOBAL_EVENT.FS_CHANGES, { event: event, path: FileSystem.fixPath(path) });
}

static loadImages(list, cb) {
let files = [];

for(let item of list) {
for (let item of list) {
let path = item.path;
let ext = FileSystem.getExtFromPath(path);
if(IMAGES_EXT.indexOf(ext) >= 0) {
if(!item.folder) FileSystem.startWatch(path);

if (IMAGES_EXT.indexOf(ext) >= 0) {
if (!item.folder) FileSystem.startWatch(path);

try {
let content = fs.readFileSync(path, 'base64');
content = "data:image/" + ext + ";base64," + content;
files.push({name: item.name, url: content, fsPath: item});
files.push({ name: item.name, url: content, fsPath: item });
}
catch(e){}
catch (e) { }
}
}

let loader = new Base64ImagesLoader();
loader.load(files, null, (res) => {
if(cb) cb(res);
if (cb) cb(res);
});
}

static loadFolder(path, cb) {
if(fs.existsSync(path)) {
if (fs.existsSync(path)) {
FileSystem.startWatch(path);

let parts = path.split("/");
let name = "";
while (parts.length && !name) name = parts.pop();
Expand All @@ -156,60 +159,60 @@ class FileSystem {
cb({});
}
}
static saveProject(data, path="") {

static saveProject(data, path = "") {
let options = {
filters: [{name: "Free texture packer", extensions: ['ftpp']}]
filters: [{ name: "Free texture packer", extensions: ['ftpp'] }]
};
if(!path) {

if (!path) {
path = dialog.showSaveDialog(options);
}
if(path) {

if (path) {
path = FileSystem.fixPath(path);

try {
fs.writeFileSync(path, JSON.stringify(data, null, 2));
Controller.updateProject(path);
}
catch(e) {
catch (e) {

}
}

return path;
}
static loadProject(pathToLoad="") {

static loadProject(pathToLoad = "") {
let path;
if(pathToLoad) {

if (pathToLoad) {
path = FileSystem.fixPath(pathToLoad);
}
else {
path = dialog.showOpenDialog({
filters: [{name: "Free texture packer", extensions: ['ftpp']}],
filters: [{ name: "Free texture packer", extensions: ['ftpp'] }],
properties: ['openFile']
});

if(path && path[0]) {
if (path && path[0]) {
path = FileSystem.fixPath(path[0]);
}
}

let data = null;
if(path) {

if (path) {
try {
data = fs.readFileSync(path);
data = JSON.parse(data);
Controller.updateProject(path);
}
catch(e) {data = null}
catch (e) { data = null }
}
return {path, data};

return { path, data };
}
}

Expand Down

0 comments on commit df08bff

Please sign in to comment.