Skip to content

Commit

Permalink
Merge pull request #351 from evolus/development
Browse files Browse the repository at this point in the history
PR for 3.0.4
  • Loading branch information
mbrainiac committed Jun 27, 2017
2 parents 8e0f210 + fc16c86 commit 592faba
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 100 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Expand Up @@ -2,7 +2,7 @@
"name": "Pencil",
"productName": "Pencil",
"description": "An open-source GUI prototyping tool that is available for ALL platforms.",
"version": "3.0.3",
"version": "3.0.4",
"author": {
"name": "Evolus",
"url": "http://evolus.vn",
Expand Down
6 changes: 5 additions & 1 deletion app/pencil-core/common/EpgzHandler.js
Expand Up @@ -15,7 +15,11 @@ EpgzHandler.prototype.loadDocument = function(filePath) {
var thiz = this;
return new Promise(function (resolve, reject) {

var wrappedRejectCalled = false;
var wrappedReject = function (error) {
if (wrappedRejectCalled) return;
wrappedRejectCalled = true;
console.log(error);
var recoverable = fs.existsSync(path.join(Pencil.documentHandler.tempDir.name, "content.xml"));
if (!recoverable) {
reject(error);
Expand All @@ -38,7 +42,7 @@ EpgzHandler.prototype.loadDocument = function(filePath) {
fs.createReadStream(filePath)
.pipe(zlib.Gunzip())
.on("error", wrappedReject)
.pipe(tarfs.extract(Pencil.documentHandler.tempDir.name)
.pipe(tarfs.extract(Pencil.documentHandler.tempDir.name, {readable: true, writable: true})
.on("error", wrappedReject)
.on("finish", function() {
console.log("Successfully extracted.");
Expand Down
42 changes: 27 additions & 15 deletions app/pencil-core/common/FileHandler.js
Expand Up @@ -25,20 +25,21 @@ FileHandler.prototype.parseDocument = function (filePath, callback) {
thiz.controller.doc.properties[propNode.getAttribute("name")] = value;
});
Dom.workOn("./p:Pages/p:Page", dom.documentElement, function (pageNode) {
var page = new Page(thiz.controller.doc);
if (!pageNode) return;
var pageFileName = pageNode.getAttribute("href");
if (pageFileName == null) return;
var pageFile = path.join(targetDir, pageFileName);
if (!fs.existsSync(pageFile)) {
return;
}
var page = new Page(thiz.controller.doc);
page.pageFileName = pageFileName;
page.tempFilePath = path.join(targetDir, pageFileName);
page.tempFilePath = pageFile;
thiz.controller.doc.pages.push(page);
});

thiz.controller.doc.pages.forEach(function (page) {
var pageFile = path.join(targetDir, page.pageFileName);
if (!fs.existsSync(pageFile)) {
if (callback) callback(new Error(Util.getMessage("page.specification.is.not.found.in.the.archive")));
return;
}

var pageFile = page.tempFilePath;
var dom = Controller.parser.parseFromString(fs.readFileSync(pageFile, "utf8"), "text/xml");
Dom.workOn("./p:Properties/p:Property", dom.documentElement, function (propNode) {
var propName = propNode.getAttribute("name");
Expand Down Expand Up @@ -77,6 +78,8 @@ FileHandler.prototype.parseDocument = function (filePath, callback) {
if (fs.existsSync(thumbPath)) {
page.thumbPath = thumbPath;
page.thumbCreated = new Date();
} else {
thiz.controller.invalidateBitmapFilePath(page);
}
page.canvas = null;
}, thiz);
Expand All @@ -86,11 +89,15 @@ FileHandler.prototype.parseDocument = function (filePath, callback) {
page.backgroundPage = this.controller.findPageById(page.backgroundPageId);
thiz.controller.invalidateBitmapFilePath(page);
}

if (page.parentPageId) {
var parentPage = this.controller.findPageById(page.parentPageId);
page.parentPage = parentPage;
parentPage.children.push(page);
if (parentPage){
page.parentPage = parentPage;
parentPage.children.push(page);
} else {
console.log("Remove parent page due to does not exist --> " + page.parentPageId);
delete page.parentPageId;
}
}
}, thiz);

Expand All @@ -102,10 +109,15 @@ FileHandler.prototype.parseDocument = function (filePath, callback) {
FontLoader.instance.setDocumentRepoDir(path.join(targetDir, "fonts"));
FontLoader.instance.loadFonts(function () {
thiz.controller.sayControllerStatusChanged();
var activePage = null;
if (thiz.controller.doc.properties.activeId) {
thiz.controller.activatePage(thiz.controller.findPageById(thiz.controller.doc.properties.activeId));
} else {
if (thiz.controller.doc.pages.length > 0) thiz.controller.activatePage(thiz.controller.doc.pages[0]);
activePage = thiz.controller.findPageById(thiz.controller.doc.properties.activeId);
}
if (activePage == null && thiz.controller.doc.pages.length > 0) {
activePage = thiz.controller.doc.pages[0];
}
if (activePage != null) {
thiz.controller.activatePage(activePage);
}
thiz.controller.applicationPane.onDocumentChanged();
thiz.modified = false;
Expand All @@ -114,7 +126,7 @@ FileHandler.prototype.parseDocument = function (filePath, callback) {
Pencil.documentHandler.preDocument = filePath;
} catch (e) {
// Pencil.documentHandler.newDocument()
console.error(e);
console.log(e);
Dialog.alert("Unexpected error while accessing file: " + path.basename(filePath), null, function() {
(oldPencilDocument != null) ? Pencil.documentHandler.loadDocument(oldPencilDocument) : function() {
Pencil.controller.confirmAndclose(function () {
Expand Down
9 changes: 9 additions & 0 deletions app/pencil-core/common/FontLoader.js
Expand Up @@ -262,6 +262,15 @@ FontRepository.SUPPORTED_VARIANTS["italic"] = {
displayName: "Regular Italic"
};

FontRepository.findVariantName = function (weight, style) {
for (var variantName in FontRepository.SUPPORTED_VARIANTS) {
var spec = FontRepository.SUPPORTED_VARIANTS[variantName];
if (spec.weight == weight && spec.style == style) return variantName;
}

return null;
};

FontRepository.prototype.addFont = function (data) {
if (!this.loaded) this.load();
var font = {
Expand Down
45 changes: 31 additions & 14 deletions app/pencil-core/definition/collectionManager.js
Expand Up @@ -288,23 +288,37 @@ CollectionManager.extractCollection = function(file, callback) {
var targetDir = path.join(CollectionManager.getUserStencilDirectory(), fileName);
console.log("extracting to", targetDir);

var extractor = unzip.Extract({ path: targetDir });
extractor.on("close", function () {
if (callback) {
callback(err);
var admZip = require('adm-zip');

var zip = new admZip(filePath);
zip.extractAllToAsync(targetDir, true, function (err) {
if (err) {
error(err);
setTimeout(function() {
CollectionManager.removeCollectionDir(targetDir);
}, 10);
} else {
resolve(targetDir);
}
resolve(targetDir);
});
extractor.on("error", (err) => {
console.log("extract error", err);
error(err);

setTimeout(function() {
CollectionManager.removeCollectionDir(targetDir);
}, 10);
});

fs.createReadStream(filePath).pipe(extractor);
// var extractor = unzip.Extract({ path: targetDir });
// extractor.on("close", function () {
// if (callback) {
// callback(err);
// }
// resolve(targetDir);
// });
// extractor.on("error", (err) => {
// console.log("extract error", err);
// error(err);

// setTimeout(function() {
// CollectionManager.removeCollectionDir(targetDir);
// }, 10);
// });

// fs.createReadStream(filePath).pipe(extractor);
});
};
CollectionManager.installCollectionFonts = function (collection) {
Expand All @@ -319,6 +333,7 @@ CollectionManager.installCollectionFonts = function (collection) {
if (existingFont.source == collection.id) {
FontLoader.instance.userRepo.removeFont(existingFont);
} else {
console.log("Skip installing: " + font.name);
continue; //skip installing this font
}
}
Expand All @@ -344,6 +359,8 @@ CollectionManager.installCollectionFonts = function (collection) {
fontData[variantName + "FilePath"] = filePath;
}

console.log("Fontdata to install", fontData);

FontLoader.instance.installNewFont(fontData);
installedFonts.push(fontData.fontName);
}
Expand Down
13 changes: 8 additions & 5 deletions app/pencil-core/definition/shapeDefCollectionParser.js
Expand Up @@ -210,12 +210,15 @@ ShapeDefCollectionParser.prototype.loadCustomLayout = function (installDirPath)
});
Dom.workOn("./p:Fonts/p:Font", shapeDefsNode, function (fontNode) {
var font = {
name: fontNode.getAttribute("name"),
regular: fontNode.getAttribute("regular"),
bold: fontNode.getAttribute("bold"),
italic: fontNode.getAttribute("italic"),
boldItalic: fontNode.getAttribute("boldItalic")
name: fontNode.getAttribute("name")
};

for (var variantName in FontRepository.SUPPORTED_VARIANTS) {
var filePath = fontNode.getAttribute(variantName);
if (filePath) font[variantName] = filePath;
}

console.log("Font:", font);
collection.fonts.push(font);
});

Expand Down
153 changes: 106 additions & 47 deletions app/pencil-core/privateCollection/privateCollectionManager.js
Expand Up @@ -221,63 +221,122 @@ PrivateCollectionManager.installCollectionFromFile = function (file) {
var targetDir = path.join(tempDir.name, fileName);
console.log("targetPath:", targetDir);

var extractor = unzip.Extract({ path: targetDir });
extractor.on("close", function () {
var admZip = require('adm-zip');

//try loading the collection
try {
var definitionFile = path.join(targetDir, "Definition.xml");
if (!fs.existsSync(definitionFile)) throw Util.getMessage("collection.specification.is.not.found.in.the.archive");
var zip = new admZip(filePath);
zip.extractAllToAsync(targetDir, true, function (err) {
if (err) {
ApplicationPane._instance.unbusy();
Dialog.error("Error installing collection.");
tempDir.removeCallback();
} else {
//try loading the collection
try {
var definitionFile = path.join(targetDir, "Definition.xml");
if (!fs.existsSync(definitionFile)) throw Util.getMessage("collection.specification.is.not.found.in.the.archive");

var fileContents = fs.readFileSync(definitionFile, ShapeDefCollectionParser.CHARSET);
var fileContents = fs.readFileSync(definitionFile, ShapeDefCollectionParser.CHARSET);

var domParser = new DOMParser();
var domParser = new DOMParser();

var collection = null;
var dom = domParser.parseFromString(fileContents, "text/xml");
if (dom != null) {
var dom = dom.documentElement;
var parser = new PrivateShapeDefParser();
Dom.workOn("./p:Collection", dom, function (node) {
collection = parser.parseNode(node);
});
};

if (collection && collection.id) {
//check for duplicate of name
for (i in PrivateCollectionManager.privateShapeDef.collections) {
var existingCollection = PrivateCollectionManager.privateShapeDef.collections[i];
if (existingCollection.id == collection.id) {
throw Util.getMessage("collection.named.already.installed", collection.id);
}
}
var collection = null;
var dom = domParser.parseFromString(fileContents, "text/xml");
if (dom != null) {
var dom = dom.documentElement;
var parser = new PrivateShapeDefParser();
Dom.workOn("./p:Collection", dom, function (node) {
collection = parser.parseNode(node);
});
};

Dialog.confirm("Are you sure you want to install the unsigned collection: " + collection.displayName + "?",
"Since a collection may contain execution code that could harm your machine. It is hightly recommanded that you should only install collections from authors whom you trust.",
"Install", function () {
// CollectionManager.setCollectionCollapsed(collection, false);
PrivateCollectionManager.addShapeCollection(collection);
tempDir.removeCallback();
}, "Cancel", function () {
tempDir.removeCallback();
if (collection && collection.id) {
//check for duplicate of name
for (i in PrivateCollectionManager.privateShapeDef.collections) {
var existingCollection = PrivateCollectionManager.privateShapeDef.collections[i];
if (existingCollection.id == collection.id) {
throw Util.getMessage("collection.named.already.installed", collection.id);
}
}
);
} else {
throw Util.getMessage("collection.specification.is.not.found.in.the.archive");

Dialog.confirm("Are you sure you want to install the unsigned collection: " + collection.displayName + "?",
"Since a collection may contain execution code that could harm your machine. It is hightly recommanded that you should only install collections from authors whom you trust.",
"Install", function () {
// CollectionManager.setCollectionCollapsed(collection, false);
PrivateCollectionManager.addShapeCollection(collection);
tempDir.removeCallback();
}, "Cancel", function () {
tempDir.removeCallback();
}
);
} else {
throw Util.getMessage("collection.specification.is.not.found.in.the.archive");
}
} catch (e) {
Dialog.error("Error installing collection.");
} finally {
ApplicationPane._instance.unbusy();
tempDir.removeCallback();
}
} catch (e) {
Dialog.error("Error installing collection.");
} finally {
ApplicationPane._instance.unbusy();
tempDir.removeCallback();
}
}).on("error", function (error) {
ApplicationPane._instance.unbusy();
Dialog.error("Error installing collection.");
tempDir.removeCallback();
});

fs.createReadStream(filePath).pipe(extractor);
// var extractor = unzip.Extract({ path: targetDir });
// extractor.on("close", function () {

// //try loading the collection
// try {
// var definitionFile = path.join(targetDir, "Definition.xml");
// if (!fs.existsSync(definitionFile)) throw Util.getMessage("collection.specification.is.not.found.in.the.archive");

// var fileContents = fs.readFileSync(definitionFile, ShapeDefCollectionParser.CHARSET);

// var domParser = new DOMParser();

// var collection = null;
// var dom = domParser.parseFromString(fileContents, "text/xml");
// if (dom != null) {
// var dom = dom.documentElement;
// var parser = new PrivateShapeDefParser();
// Dom.workOn("./p:Collection", dom, function (node) {
// collection = parser.parseNode(node);
// });
// };

// if (collection && collection.id) {
// //check for duplicate of name
// for (i in PrivateCollectionManager.privateShapeDef.collections) {
// var existingCollection = PrivateCollectionManager.privateShapeDef.collections[i];
// if (existingCollection.id == collection.id) {
// throw Util.getMessage("collection.named.already.installed", collection.id);
// }
// }

// Dialog.confirm("Are you sure you want to install the unsigned collection: " + collection.displayName + "?",
// "Since a collection may contain execution code that could harm your machine. It is hightly recommanded that you should only install collections from authors whom you trust.",
// "Install", function () {
// // CollectionManager.setCollectionCollapsed(collection, false);
// PrivateCollectionManager.addShapeCollection(collection);
// tempDir.removeCallback();
// }, "Cancel", function () {
// tempDir.removeCallback();
// }
// );
// } else {
// throw Util.getMessage("collection.specification.is.not.found.in.the.archive");
// }
// } catch (e) {
// Dialog.error("Error installing collection.");
// } finally {
// ApplicationPane._instance.unbusy();
// tempDir.removeCallback();
// }
// }).on("error", function (error) {
// ApplicationPane._instance.unbusy();
// Dialog.error("Error installing collection.");
// tempDir.removeCallback();
// });

// fs.createReadStream(filePath).pipe(extractor);
};
PrivateCollectionManager.setLastUsedCollection = function (collection) {
Config.set("PrivateCollection.lastUsedCollection.id", collection.id);
Expand Down

0 comments on commit 592faba

Please sign in to comment.