Skip to content

Commit

Permalink
Fix for Sketch 66 APIs
Browse files Browse the repository at this point in the history
- Bug fix for Sketch 66 APIs
- Fixed an issue where having email as a text layer name would cause parsing error.
  • Loading branch information
pratikjshah committed May 21, 2020
1 parent e605ef2 commit 610f94c
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 104 deletions.
13 changes: 13 additions & 0 deletions appcast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
<link>https://raw.githubusercontent.com/pratikjshah/symbol-browser/master/appcast.xml</link>
<description>Better way to browse your Design System and UI Sticker sheet</description>
<language>en</language>
<item>
<title>v3.5.2</title>
<description>
<![CDATA[
<h2>Fix for Sketch 66</h2>
]]>
</description>
<pubDate>Thu, 21 May 2020 2:54:24 GMT</pubDate>
<enclosure
url="https://github.com/pratikjshah/symbol-browser/archive/v3.5.2.zip"
sparkle:version="3.5.2"
type="application/octet-stream"/>
</item>
<item>
<title>v3.5.1</title>
<description>
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"email": "[email protected]",
"appcast": "https://raw.githubusercontent.com/pratikjshah/symbol-browser/master/appcast.xml",
"homepage": "http://symbol-browser.pratikshah.website/",
"version": "3.5.1",
"version": "3.5.2",
"identifier" : "website.pratikshah.symbol-browser",
"compatibleVersion": "52",
"bundleVersion": "1",
Expand Down
41 changes: 36 additions & 5 deletions src/sticker-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as util from './util';
import * as colorUtil from './color-util';
import {ProgressReporter} from './util-progress-reporter';

const INDEX_FORMAT_VERSION = 6;
const INDEX_FORMAT_VERSION = 7;
const FORCE_REBULD = false;
const SHOW_DEFAULT_STICKERS = true;
let _defaultSection = 'DefaultSymbols';
Expand Down Expand Up @@ -213,14 +213,20 @@ async function buildStickerIndexForLibrary(libraryId, defaultLibName, document,
NSPredicate.predicateWithFormat('className == %@', 'MSTextLayer'));
allTextLayers.reverse(); // layer list order, not stacking order

log("allTextLayers: " + allTextLayers.length);
// log("allTextLayers: " + allTextLayers.length);
let combinedText = "";
for (let textLayer of allTextLayers) {
let text = textLayer.stringValue();
if (text.indexOf('!Sticker') < 0) {
continue;
}

var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(re.test(String(text).toLowerCase())) {
log("Skip Email textLayer: " + text);
continue;
}

combinedText = combinedText + " \r\n\r\n\r\n" + text;

// let tempParsedMetadata = parseStickerMetadata(text);
Expand Down Expand Up @@ -320,6 +326,12 @@ async function buildStickerIndexForLibrary(libraryId, defaultLibName, document,
continue;
}

var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(layer instanceof MSTextLayer && re.test(String(layer.name()).toLowerCase())) {
// log("Skip Email text layer Name: " + layer.name());
continue;
}

// let parsedName;
// if(layer instanceof MSSymbolMaster && !String(layer.name()).startsWith('@')) {
// parsedName = parseLayerName(layer.name(), sectionId => sectionId in sectionsById, true);
Expand Down Expand Up @@ -501,8 +513,20 @@ const SPECIAL_INSTRUCTIONS = new Set(['icon']);
// return parsed;
// }

function sanatizedEmailName(name) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(re.test(String(name).toLowerCase())) {
name = name.replace("@","");
name = name.replace(".","");
}
return name;
}

function getSymbolNameWithSectionFn(name) {
var init, last, newName;

name = sanatizedEmailName(name);

name = name.replace(/[^a-zA-Z0-9 \/_]/g, '');
if(name.lastIndexOf("/") > 0) {
init = (name.substring(0, name.lastIndexOf("/") + 0)).replace(/\s\s+/g, ' ').replace(/\//g,'_').replace(/\s/g,'');
Expand All @@ -512,7 +536,7 @@ function getSymbolNameWithSectionFn(name) {
last = name;
}

newName = name + "@" + _defaultSection + "." + init;
newName = name + " @" + _defaultSection + "." + init;

return newName;
}
Expand All @@ -526,13 +550,20 @@ function parseLayerName(name, isValidSectionIdFn) {
};

name = String(name || '');
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

if(!name.includes("@")) {
name = getSymbolNameWithSectionFn(name);
// log("Ideal symbol Name: " + name);
} else if(re.test(String(name).toLowerCase())) {
name = getSymbolNameWithSectionFn(sanatizedEmailName(name));
// log("sanatizedEmailName Name: " + name + " | new: " + name);
} else {
log(name);
log("Exception/tagged Name: " + name);
}

// log(name);

let unspecialParts = name
.split(/(@+[\w\.]+)/)
.filter(part => {
Expand Down Expand Up @@ -571,7 +602,7 @@ function getStickersMetadata(stickersMetaData) {
var sectionPrefix = '!StickerSection ';
var title = 'title: ';
var hideNames = 'hideNames: false';
var description = 'description: All of your symbols show up here. Symbol Browser will arrange all of your symbols in a groups based on the names. <br/><br/><br/><b>Want to define your own groupings?</b><br/> Follow below link to see how to define new sections. <br/><br/> <a class="sticker-root-section__link" href="https://github.com/pratikjshah/symbol-browser/wiki/Create-custom-sections-for-Symbols" style="text-alignn:center;">View Documentation</a>';
var description = 'description: All of your symbols show up here. Symbol Browser will arrange all of your symbols in a groups based on the names. <br/><br/><br/><b>Want to define your own groupings?</b><br/> Follow below link to see how to define new sections. <br/><br/> <a class="sticker-root-section__link" href="https://github.com/pratikjshah/symbol-browser/wiki/Create-custom-sections-for-Symbols">View Documentation</a>';
var backgroundEach = "backgroundEach: '#ffffff'";
var layout = 'layout: row';
var yamlStickersMetadata = '';
Expand Down
18 changes: 12 additions & 6 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,18 @@ export function getLayerImage(document, layer) {
export function captureLayerImage(document, layer, destPath) {
let air = layer.absoluteInfluenceRect();
let rect = NSMakeRect(air.origin.x, air.origin.y, air.size.width, air.size.height);
let exportRequest = MSExportRequest.exportRequestsFromLayerAncestry_inRect_(
MSImmutableLayerAncestry.ancestryWithMSLayer_(layer),
rect // we pass this to avoid trimming
).firstObject();
exportRequest.format = 'png';
exportRequest.scale = 2;
// let exportRequest = MSExportRequest.exportRequestsFromLayerAncestry_inRect_(
// MSImmutableLayerAncestry.ancestryWithMSLayer_(layer),
// rect // we pass this to avoid trimming
// ).firstObject();
// exportRequest.format = 'png';
// exportRequest.scale = 2;
let exportRequest = MSExportRequest.exportRequestFromExportFormat_layer_inRect_useIDForName_(
MSExportFormat.formatWithScale_name_fileFormat_(2.0, 'temp.png', 'png'),
layer,
rect, // avoid trimming
true);

if (!(layer instanceof MSArtboardGroup || layer instanceof MSSymbolMaster)) {
exportRequest.includeArtboardBackground = false;
}
Expand Down
Loading

0 comments on commit 610f94c

Please sign in to comment.