-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
130 lines (107 loc) · 3.35 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Add this to the top of your main.js file
const { Artboard, Rectangle, Ellipse, Text, Color } = require("scenegraph");
const { alert, error } = require('./lib/dialogs.js');
var shouldShowDialog = true;
function createStyledTextHandlerFunction(selection, documentRoot) {
shouldShowDialog = true;
recursiveStyling(documentRoot);
if (shouldShowDialog) {
showError();
}
}
function recursiveStyling(node) {
node.children.forEach(children => {
if (children instanceof Text) {
changeStyle(children);
}
recursiveStyling(children);
});
}
async function showError() {
await error("Styling failed",
"HTML Style Applicator did not find any text inside HTML tags.");
}
const commands = ["<b>", "<i>", "<u>"];
function changeStyle(text) {
var data = [];
var structure = prepareStructure(text.text, data, true);
if (data.length == 0) {
return;
}
text.text = structure.map(item => item.text).join("");
text.styleRanges = structure.map(item => ({
length: item.text.length,
fontStyle: getFontStyleFromItem(item),
underline: item.underline
}));
}
function getFontStyleFromItem(item) {
var style = ""
if (item.bold) {
style += "Bold";
}
if (item.italic) {
if (style.length != 0) {
style += " ";
}
style += "Italic";
}
if (item.length == 0) {
style += "Regular ";
}
return style;
}
function prepareStructure(text, structure, start) {
if (text.length == 0) {
return structure;
}
var startLess = text.indexOf("<")
var command = text.substring(startLess, startLess + 3)
if (start && !commands.includes(command)) {
return structure
}
shouldShowDialog = false;
if (commands.includes(command)) {
var beforeText = text.substring(0, startLess);
structure.push( { text: beforeText, bold: false, italic: false, underline: false } );
} else {
structure.push( { text: text, bold: false, italic: false, underline: false } );
return structure;
}
var newStart = -1;
switch (command) {
case "<b>":
var endLess = text.indexOf("</b>")
var sub = text.substring(startLess + 3, endLess);
var obj = { text: sub, bold: true, italic: false, underline: false }
newStart = endLess + 4;
processInnerTags(obj);
structure.push(obj);
break;
case "<i>":
var endLess = text.indexOf("</i>")
var sub = text.substring(startLess + 3, endLess);
var obj = { text: sub, bold: false, italic: true, underline: false }
newStart = endLess + 4;
processInnerTags(obj);
structure.push(obj);
break;
case "<u>":
var endLess = text.indexOf("</u>")
var sub = text.substring(startLess + 3, endLess);
var obj = { text: sub, bold: false, italic: false, underline: true }
newStart = endLess + 4;
processInnerTags(obj);
structure.push(obj);
break;
}
text = text.substring(newStart);
return prepareStructure(text, structure, false);
}
function processInnerTags(obj) {
}
module.exports = {
commands: {
"createStyledTextCommand": createStyledTextHandlerFunction
}
};