-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
43 lines (35 loc) · 973 Bytes
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { Text } = require("scenegraph");
const { alert, error } = require("./lib/dialogs");
/**
* Entry point for the plugin
*
* @param {!Selection} selection
*/
async function countText(selection) {
let wc = 0, cc = 0, textLayersSelected = false;
selection.items.forEach(node => {
if (node instanceof Text) {
let text = node.text.trim();
textLayersSelected = true;
wc += countWords(text);
cc += text.length;
}
});
// Handle error cases
if (textLayersSelected === false) {
await error("Count Text - Error", "No text layers selected");
return ;
}
await alert("Count Text", `Word Count: ${wc}`, `Character Count: ${cc}`);
}
/**
* Counts the number of words in the string
* @param {!string} str
* @returns {Number} no of words in the string
*/
function countWords(str) {
return str.split(/\s+/).length;
}
module.exports.commands = {
countText
};