-
Wordwrap expression, count the number of lines, use the variable elsewhere
Consider the following expression:
var str = text.sourceText;
var lines = str.split(‘\n’);
var longestLineLength = 0;
for (var i = 0; i < lines.length; i++) {
if (lines[i].length > longestLineLength) {
longestLineLength = lines[i].length;
}
}
var maxW = longestLineLength <= 20 ? 20 : 40;
str = wordWrap(str, maxW);
var lineCount = str.split(‘\n’).length;
function wordWrap(str, maxW) {
var newLineStr = “\n”;
done = false;
res = ”;
while (str.length > maxW) {
found = false;
for (i = maxW – 1; i >= 0; i–) {
if (testWhite(str.charAt(i))) {
res = res + [str.slice(0, i), newLineStr].join(”);
str = str.slice(i + 1);
found = true;
break;
}
}
if (!found) {
res += [str.slice(0, maxW), newLineStr].join(”);
str = str.slice(maxW);
}
}
return res + str;
}
function testWhite(x) {
var white = new RegExp(/^\s$/);
return white.test(x.charAt(0));
};
text.sourceText = str;
————————————————————————————————-
It wraps text exactly as it should, but I would like to reference the variable “lineCount” in another property and use it to drive other expressions. I cannot attach it to a slider as the lineCount will change constantly based on user input. It does output the correct lineCount, so I know it’s working. I just need to use the variable to drive other expressions while still allowing it to change dynamically. Is this possible?