Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt at JSON output from script #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions ai2html.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ var defaultSettings = {
"output": "one-file", // Options: one-file, multiple-files
"project_name": "", // Defaults to the name of the AI file
"project_type": "",
"generate_html": true, // allow html output to be enabled/disabled
"generate_json": true, // allow json output to be enabled/disabled
"html_output_path": "/ai2html-output/",
"html_output_extension": ".html",
"json_output_path": "/ai2html-output/",
"json_output_extension": ".json",
"image_output_path": "",
"image_source_path": null,
"image_alt_text": "",
Expand Down Expand Up @@ -539,10 +543,26 @@ function render(settings, customBlocks) {
var abSettings = getArtboardSettings(activeArtboard);
var docArtboardName = getDocumentArtboardName(activeArtboard);
var textFrames, textData, imageData;
var artboardContent = {html: '', css: '', js: ''};

doc.artboards.setActiveArtboardIndex(abIndex);

// TODO :: this work is duplicated in generateArtboardDiv but is the only
// place where we read width / height / aspectRatio etc. so ideally this would
// be passed into that function
abSettings.id = nameSpace + getArtboardFullName(activeArtboard, settings);
abSettings.classname = nameSpace + 'artboard';
abSettings.widthRange = getArtboardWidthRange(activeArtboard, settings);
abSettings.visibleRange = getArtboardVisibilityRange(activeArtboard, settings);
abSettings.abBox = convertAiBounds(activeArtboard.artboardRect);
abSettings.aspectRatio = abSettings.abBox.width / abSettings.abBox.height;
var artboardContent = {
html: '',
css: '',
js: '',
settings: abSettings,
name: docArtboardName
};

// ========================
// Convert text objects
// ========================
Expand Down Expand Up @@ -597,8 +617,18 @@ function render(settings, customBlocks) {
//=====================================

forEach(fileContentArr, function(fileContent) {
addCustomContent(fileContent, customBlocks);
generateOutputHtml(fileContent, fileContent.name, settings);
if (isTrue(settings.generate_html)) {
addCustomContent(fileContent, customBlocks);
generateOutputHtml(fileContent, fileContent.name, settings);
}
if (isTrue(settings.generate_json)) {
var serializedObject = {
name: fileContent.name,
artboards: fileContent.artboards,
customBlocks: customBlocks
}
generateOutputJson(serializedObject, settings);
}
});

//=====================================
Expand Down Expand Up @@ -3867,12 +3897,13 @@ function injectCSSinSVG(content, css) {
function assignArtboardContentToFile(name, abData, outputArr) {
var obj = find(outputArr, function(o) {return o.name == name;});
if (!obj) {
obj = {name: name, html: '', js: '', css: ''};
obj = {name: name, html: '', js: '', css: '', artboards: []};
outputArr.push(obj);
}
obj.html += abData.html;
obj.js += abData.js;
obj.css += abData.css;
obj.artboards.push(abData);
}

function generateArtboardDiv(ab, settings) {
Expand Down Expand Up @@ -4174,6 +4205,27 @@ function addCustomContent(content, customBlocks) {
}
}

// Wrap content HTML in a <div>, add styles and resizer script, write to a file
function generateOutputJson(content, settings) {
var pageName = content.name;
var textForFile;

progressBar.setTitle('Writing JSON output...');

textForFile = JSON.stringify(content);
jsonFileDestinationFolder = docPath + settings.json_output_path;
checkForOutputFolder(jsonFileDestinationFolder, 'json_output_path');
jsonFileDestination = jsonFileDestinationFolder + pageName + settings.json_output_extension;

if (settings.output == 'one-file' && settings.project_type == 'ai2html') {
jsonFileDestination = jsonFileDestinationFolder + 'index' + settings.json_output_extension;
}

// write file
saveTextFile(jsonFileDestination, textForFile);
feedback.push("Your JSON was saved to `" + pathResolve(jsonFileDestination) + "`");
}

// Wrap content HTML in a <div>, add styles and resizer script, write to a file
function generateOutputHtml(content, pageName, settings) {
var linkSrc = settings.clickable_link || '';
Expand Down