Skip to content

Commit

Permalink
Open up support for customizing separator characters. Fixes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
romannurik committed Mar 20, 2019
1 parent 4eb278d commit e8e4525
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 37 deletions.
Binary file modified art/icons.sketch
Binary file not shown.
Binary file added resources/icon_plugin.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 7 additions & 25 deletions src/command-numbers-add.js
Expand Up @@ -14,14 +14,13 @@
* limitations under the License.
*/

import * as util from './sketch-util';
import * as common from './common';
import * as prefs from './prefs';


export default function(context) {
let page = context.document.currentPage();
let currentPrefs = prefs.resolvePagePrefs(context, page);
let resolvedPrefs = prefs.resolvePagePrefs(context, page);
let artboardMetas = common.generateArtboardMetas(
common.collectTargetArtboards(context));

Expand All @@ -45,9 +44,7 @@ export default function(context) {
artboardMetas.forEach(meta => {
// strip off current digits and dots
let fullName = meta.artboard.name();
let currentNamePath = fullName.substring(0, fullName.lastIndexOf('/') + 1);
let currentName = fullName.slice(currentNamePath.length);
let [_, currentNumber, baseName] = currentName.match(/^([\d.]*)[_-]?(.*)$/);
let nameParts = common.parseCurrentArtboardName(fullName, resolvedPrefs);

if (lastMetaT === null || lastMetaT != meta.t) {
lastMetaT = meta.t;
Expand All @@ -56,7 +53,7 @@ export default function(context) {
col = -1;
}

if (currentNumber.indexOf('.') >= 0) {
if (nameParts.subCol) {
// in a subcol
++subCol;
col = Math.max(0, col);
Expand All @@ -70,27 +67,12 @@ export default function(context) {
++col;
}

// create prefix (e.g. "301" and "415.4" with subflows)
let prefix = zeropad(row, numRows >= 10 ? 2 : 1)
+ currentPrefs.rowColSeparator
+ zeropad(col, 2)
+ (subCol > 0 ? '.' + (subCol) : '');
nameParts.row = row;
nameParts.col = col;
nameParts.subCol = subCol;

// add prefix to the name
meta.artboard.setName(`${currentNamePath}${prefix}${currentPrefs.numberTitleSeparator}${baseName}`);
meta.artboard.setName(common.composeArtboardName(nameParts, resolvedPrefs, {numRows}));
});
}


/**
* Left-pads the given string with zeros to the given length.
*/
function zeropad(s, length = 1) {
s = String(s);
s = s || '';
while (s.length < length) {
s = '0' + s;
}
return s;
}

12 changes: 6 additions & 6 deletions src/command-numbers-remove.js
Expand Up @@ -14,22 +14,22 @@
* limitations under the License.
*/

import * as util from './sketch-util';
import * as common from './common';
import * as prefs from './prefs';


export default function(context) {
let page = context.document.currentPage();
let resolvedPrefs = prefs.resolvePagePrefs(context, page);
let artboards = common.collectTargetArtboards(context);

// update artboard names
artboards.forEach(artboard => {
// strip off current digits and dots
let fullName = artboard.name();
let currentNamePath = fullName.substring(0, fullName.lastIndexOf('/') + 1);
let currentName = fullName.slice(currentNamePath.length);
currentName = currentName.replace(/^[\d.]*[-_]?/, '');
let nameParts = common.parseCurrentArtboardName(artboard.name(), resolvedPrefs);
nameParts.row = 0;

// reset the name
artboard.setName(currentNamePath + currentName);
artboard.setName(common.composeArtboardName(nameParts, resolvedPrefs, {}));
});
}
12 changes: 6 additions & 6 deletions src/command-preferences.js
Expand Up @@ -40,12 +40,12 @@ const WIDGET_CONFIGS = [
// onDisplayValueForPlaceholder: displayValueForChar,
// onParse: onParseString,
// },
// {
// label: 'Numbering: Number/title separator',
// key: 'numberTitleSeparator',
// onDisplayValueForPlaceholder: displayValueForChar,
// onParse: onParseString,
// }
{
label: 'Numbering: Number/title separator',
key: 'numberTitleSeparator',
onDisplayValueForPlaceholder: displayValueForChar,
onParse: onParseString,
}
];


Expand Down
73 changes: 73 additions & 0 deletions src/common.js
Expand Up @@ -3,6 +3,9 @@
import * as util from './sketch-util';


const COL_LENGTH = 2; // # of digits for the column


/**
* Returns an array of all the artboards to target for grid operations
* (e.g. re-number, rearrange, etc)
Expand Down Expand Up @@ -39,3 +42,73 @@ export function generateArtboardMetas(artboards) {
};
});
}


/**
* Parses the given artboard name based on the given resolved preferences.
*/
export function parseCurrentArtboardName(fullName, resolvedPrefs) {
let {numberTitleSeparator} = resolvedPrefs;
let path = fullName.substring(0, fullName.lastIndexOf('/') + 1);
let currentName = fullName.slice(path.length);
let [_, rowCol, subCol, baseName] = currentName.match(new RegExp(
'^(?:(\\d+)(?:\.(\\d+))?)?' + // row/col/subcol
'(?:' + escapeRegExp(numberTitleSeparator) + ')?' + // separator
'(.+?)?$')); // basename

subCol = Number(subCol || 0);
let row = 0;
let col = -1;
if (rowCol) {
row = Number(rowCol.slice(0, -COL_LENGTH));
col = Number(rowCol.slice(-COL_LENGTH));
}

return {path, row, col, subCol, baseName};
}

/**
* Creates a new artboard name based on the given pieces and resolved preferences.
*/
export function composeArtboardName(parts, resolvedPrefs, {numRows = 1} = {}) {
let {numberTitleSeparator} = resolvedPrefs;
let {path, row, col, subCol, baseName} = parts;

let name = path || '';

// create prefix (e.g. "301" and "415.4" with subflows)
let hasNumber = row && (col >= 0);
if (hasNumber) {
name += zeropad(row, numRows >= 10 ? 2 : 1)
// + String(rowColSeparator)
+ zeropad(col, COL_LENGTH)
+ (subCol ? ('.' + String(subCol)) : '');
}

if (baseName) {
name += (hasNumber ? numberTitleSeparator : '') + baseName;
}

return name;
}


/**
* Left-pads the given string with zeros to the given length.
*/
function zeropad(s, length = 1) {
s = String(s) || '';
while (s.length < length) {
s = '0' + s;
}
return s;
}


/**
* Escape a string for use in a regex
* From https://stackoverflow.com/a/6969486/102703
*/
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
1 change: 1 addition & 0 deletions src/manifest.json
Expand Up @@ -5,6 +5,7 @@
"compatibleVersion": 41,
"bundleVersion": 1,
"name": "Artboard Tricks",
"icon": "icon_plugin.png",
"commands" : [
{
"script" : "command-select-artboards.js",
Expand Down

0 comments on commit e8e4525

Please sign in to comment.