changes
This commit is contained in:
@@ -76,3 +76,59 @@ export function allQuestionsAnswered(questions, answers) {
|
||||
export function nextTabAfterAnswer(currentTab, questionCount) {
|
||||
return currentTab < questionCount - 1 ? currentTab + 1 : questionCount;
|
||||
}
|
||||
|
||||
function takeWrappedSegment(text, maxWidth) {
|
||||
if (text.length <= maxWidth) {
|
||||
return { line: text, rest: "" };
|
||||
}
|
||||
|
||||
let breakpoint = -1;
|
||||
for (let index = 0; index < maxWidth; index += 1) {
|
||||
if (/\s/.test(text[index])) {
|
||||
breakpoint = index;
|
||||
}
|
||||
}
|
||||
|
||||
if (breakpoint > 0) {
|
||||
return {
|
||||
line: text.slice(0, breakpoint).trimEnd(),
|
||||
rest: text.slice(breakpoint + 1).trimStart(),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
line: text.slice(0, maxWidth),
|
||||
rest: text.slice(maxWidth),
|
||||
};
|
||||
}
|
||||
|
||||
export function wrapPrefixedText(text, width, firstPrefix = "", continuationPrefix = firstPrefix) {
|
||||
const source = String(text ?? "");
|
||||
if (source.length === 0) {
|
||||
return [firstPrefix];
|
||||
}
|
||||
|
||||
const lines = [];
|
||||
const blocks = source.split(/\r?\n/);
|
||||
let isFirstLine = true;
|
||||
|
||||
for (const block of blocks) {
|
||||
let remaining = block.trim();
|
||||
if (remaining.length === 0) {
|
||||
lines.push(isFirstLine ? firstPrefix : continuationPrefix);
|
||||
isFirstLine = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
while (remaining.length > 0) {
|
||||
const prefix = isFirstLine ? firstPrefix : continuationPrefix;
|
||||
const maxTextWidth = Math.max(1, width - prefix.length);
|
||||
const { line, rest } = takeWrappedSegment(remaining, maxTextWidth);
|
||||
lines.push(prefix + line);
|
||||
remaining = rest;
|
||||
isFirstLine = false;
|
||||
}
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user