forked from mem9-ai/mem9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui-dialog-pagination.ts
More file actions
82 lines (67 loc) · 1.85 KB
/
ui-dialog-pagination.ts
File metadata and controls
82 lines (67 loc) · 1.85 KB
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
export interface PixelFarmDialogPaginationInput {
text: string;
maxLines: number;
measureLineCount: (value: string) => number;
}
function normalizeDialogText(text: string): string {
return text.replace(/\s+/g, " ").trim();
}
function splitIntoSentenceUnits(text: string): string[] {
const sentenceUnits = text.split(/(?<=[.!?。!?])\s+/).map((unit) => unit.trim()).filter(Boolean);
return sentenceUnits.length > 0 ? sentenceUnits : [text];
}
function pushWordGroup(
pages: string[],
words: string[],
maxLines: number,
measureLineCount: (value: string) => number,
): void {
let current = "";
for (const word of words) {
const next = current ? `${current} ${word}` : word;
if (current && measureLineCount(next) > maxLines) {
pages.push(current);
current = word;
continue;
}
current = next;
}
if (current) {
pages.push(current);
}
}
export function paginatePixelFarmDialogText(
input: PixelFarmDialogPaginationInput,
): string[] {
const normalized = normalizeDialogText(input.text);
if (!normalized) {
return [""];
}
if (input.measureLineCount(normalized) <= input.maxLines) {
return [normalized];
}
const pages: string[] = [];
const sentenceUnits = splitIntoSentenceUnits(normalized);
let current = "";
for (const unit of sentenceUnits) {
const next = current ? `${current} ${unit}` : unit;
if (current && input.measureLineCount(next) > input.maxLines) {
pages.push(current);
current = unit;
continue;
}
if (input.measureLineCount(unit) > input.maxLines) {
if (current) {
pages.push(current);
current = "";
}
pushWordGroup(pages, unit.split(" ").filter(Boolean), input.maxLines, input.measureLineCount);
continue;
}
current = next;
}
if (current) {
pages.push(current);
}
return pages;
}