Skip to content

Commit cbd5baf

Browse files
committed
implement pagination
1 parent 774c32b commit cbd5baf

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

dist/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ <h1 lang="tok">ilo Token</h1>
6363
<ul id="output"></ul>
6464
<p id="error"></p>
6565
<ul id="error-list"></ul>
66+
<p>
67+
<button id="load-more-button" style="display: none">Load more</button>
68+
</p>
6669
</main>
6770
<footer>
6871
<details>

src/main.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import BrowserDetector from "browser-dtector";
1212
import { dictionary } from "../dictionary/dictionary.ts";
1313
import { dictionaryParser } from "../dictionary/parser.ts";
1414
import PROJECT_DATA from "../project_data.json" with { type: "json" };
15+
import { Result, ResultError } from "./compound.ts";
1516
import { loadCustomDictionary } from "./dictionary.ts";
1617
import { checkLocalStorage, setIgnoreError } from "./local_storage.ts";
1718
import { PositionedError } from "./parser/parser_lib.ts";
@@ -23,9 +24,9 @@ import {
2324
resetElementsToDefault,
2425
} from "./settings_frontend.ts";
2526
import { translate } from "./translator/translator.ts";
26-
import { ResultError } from "./compound.ts";
2727

2828
const DICTIONARY_AUTO_PARSE_THRESHOLD = 9000;
29+
const PAGE_SIZE = 100;
2930

3031
// never change this
3132
const DICTIONARY_KEY = "dictionary";
@@ -76,6 +77,9 @@ function main() {
7677
const errorList = document.getElementById(
7778
"error-list",
7879
) as HTMLParagraphElement;
80+
const loadMoreButton = document.getElementById(
81+
"load-more-button",
82+
) as HTMLButtonElement;
7983

8084
const translateButton = document.getElementById(
8185
"translate-button",
@@ -184,6 +188,9 @@ function main() {
184188
// this variable also holds error messages
185189
let currentDictionary = lastSavedDictionary;
186190

191+
// state for output
192+
let output: null | Generator<Result<string>> = null;
193+
187194
// load custom dictionary
188195
if (!currentDictionary.isError()) {
189196
loadCustomDictionary(currentDictionary.unwrap()[0]);
@@ -240,25 +247,39 @@ function main() {
240247
updateOutput();
241248
}
242249
});
250+
loadMoreButton.addEventListener("click", moreOutput);
243251
function updateOutput() {
244252
outputList.innerHTML = "";
245253
errorList.innerHTML = "";
246254
errorDisplay.innerText = "";
247-
const iterableResult = translate(inputTextBox.value);
255+
loadMoreButton.style.display = "";
256+
output = translate(inputTextBox.value).iterable();
257+
moreOutput();
258+
}
259+
function moreOutput() {
248260
const errors: Array<ResultError> = [];
249261
let yielded = false;
250-
for (const result of iterableResult.iterable()) {
251-
switch (result.type) {
252-
case "value": {
253-
yielded = true;
254-
const list = document.createElement("li");
255-
list.innerHTML = result.value;
256-
outputList.appendChild(list);
257-
break;
262+
let i = 0;
263+
while (i < PAGE_SIZE) {
264+
const next = output!.next();
265+
if (!next.done) {
266+
const result = next.value;
267+
switch (result.type) {
268+
case "value": {
269+
yielded = true;
270+
const list = document.createElement("li");
271+
list.innerHTML = result.value;
272+
outputList.appendChild(list);
273+
i++;
274+
break;
275+
}
276+
case "error":
277+
errors.push(result.error);
278+
break;
258279
}
259-
case "error":
260-
errors.push(result.error);
261-
break;
280+
} else {
281+
loadMoreButton.style.display = "none";
282+
break;
262283
}
263284
}
264285
if (!yielded) {

0 commit comments

Comments
 (0)