Skip to content

Commit 4ae9c44

Browse files
committed
Adding export to PDF functionality
1 parent b831394 commit 4ae9c44

File tree

7 files changed

+411
-69
lines changed

7 files changed

+411
-69
lines changed
64.7 KB
Binary file not shown.

wiktionary_pron/index.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@
9292
align-items: center;
9393
}
9494

95+
#lang_style{
96+
max-width: 100px;
97+
98+
}
9599
h1 {
96100
margin-top: 0px;
97101
}
@@ -256,10 +260,11 @@
256260
padding: 0px;
257261
}
258262

259-
#tts{
263+
#tts {
260264
max-width: 350px;
261265
}
262266

267+
263268
</style>
264269

265270

@@ -378,8 +383,19 @@ <h1 style="flex: 1;">Online IPA Converter</h1>
378383
<hr>
379384
<table>
380385
<tbody id="result"></tbody>
381-
</table>
382386

387+
</table>
388+
<button
389+
autocomplete="off"
390+
class="btn btn-primary btn-block"
391+
disabled="disabled"
392+
id="export_pdf"
393+
name="submit"
394+
type="button"
395+
value=""
396+
>Export as PDF
397+
<i class="fa fa-file-pdf-o" style="font-size: 30px;color: #c11313;"></i>
398+
</button>
383399
</div>
384400
<hr>
385401
<div id="footer">

wiktionary_pron/scripts/lua_init.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ await lua.doString(`
4242
4343
4444
--local resp = fetch(string.format('https://cdn.statically.io/gh/hellpanderrr/hellpanderrr.github.io/master/wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
45-
--local resp = fetch(string.format('https://cdn.jsdelivr.net/gh/hellpanderrr/hellpanderrr.github.io/wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
45+
--local resp = fetch(string.format('https://cdn.jsdelivr.net/gh/hellpanderrr/hellpanderrr.github.io@0.1.0/wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
4646
updateLoadingText(path, extension)
47-
local resp = fetch(string.format('../wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
47+
local resp = fetch(string.format('https://cdn.jsdelivr.net/gh/hellpanderrr/[email protected]/wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
48+
49+
--local resp = fetch(string.format('../wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
4850
updateLoadingText("", "")
4951
local text = resp:text():await()
5052
local module = load(text)()

wiktionary_pron/scripts/main.js

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import {
33
asyncMapStrict,
44
clearStorage,
55
get_ipa_no_cache,
6+
memoizeLocalStorage,
67
wait,
78
} from "./utils.js";
89
import { tts } from "./tts.js";
10+
import { toPdf } from "./pdf_export.js";
911

1012
document.querySelector("#lang").disabled = false;
1113

@@ -17,58 +19,6 @@ function prepareTranscribe() {
1719
return [resultDiv, textLines];
1820
}
1921

20-
/**
21-
* Memoizes the given function in local storage.
22-
* @param {Function} fn - The function to memoize.
23-
* @param {Object} options - Memoization options.
24-
* @param {number} options.ttl - Time to live for cached results in milliseconds.
25-
* @param {boolean} options.backgroundRefresh - Whether to refresh the cache in the background.
26-
* @throws {Error} Throws an error if the function is anonymous.
27-
* @returns {Function} Returns the memoized function.
28-
*/
29-
function memoizeLocalStorage(
30-
fn,
31-
options = { ttl: 100, backgroundRefresh: false },
32-
) {
33-
if (!fn.name)
34-
throw new Error("memoizeLocalStorage only accepts non-anonymous functions");
35-
// Fetch localstorage or init new object
36-
let cache = JSON.parse(localStorage.getItem(fn.name) || "{}");
37-
38-
//executes and caches result
39-
function executeAndCacheFn(fn, args, argsKey) {
40-
const result = fn(...args);
41-
// reset the cache value
42-
cache[fn.name] = {
43-
...cache[fn.name],
44-
[argsKey]: { expiration: Date.now() + options.ttl, result },
45-
};
46-
localStorage.setItem(fn.name, JSON.stringify(cache));
47-
}
48-
49-
return function () {
50-
// Note: JSON.stringify is non-deterministic,
51-
// consider something like json-stable-stringify to avoid extra cache misses
52-
53-
const argsKey = JSON.stringify(arguments);
54-
55-
if (
56-
!cache[fn.name] ||
57-
!cache[fn.name][argsKey] ||
58-
cache[fn.name][argsKey].expiration >= Date.now()
59-
) {
60-
executeAndCacheFn(fn, arguments, argsKey);
61-
return cache[fn.name][argsKey].result;
62-
} else if (options.backgroundRefresh) {
63-
executeAndCacheFn(fn, arguments, argsKey);
64-
return cache[fn.name][argsKey].result;
65-
}
66-
console.log("Using cached", argsKey);
67-
68-
return cache[fn.name][argsKey].result;
69-
};
70-
}
71-
7222
const get_ipa_cache = memoizeLocalStorage(get_ipa_no_cache);
7323

7424
/**
@@ -92,7 +42,7 @@ function getIpa(text, lang, lang_style, lang_form) {
9242
* @param {string} mode - The mode for transcribing the text (default, line, column).
9343
*/
9444
async function transcribe(mode) {
95-
disableAll();
45+
disableAll([document.querySelector("#export_pdf")]);
9646
try {
9747
const [resultDiv, textLines] = prepareTranscribe();
9848
const { lang, langStyle, langForm } = getLangStyleForm();
@@ -248,39 +198,42 @@ async function transcribe(mode) {
248198
console.log(err);
249199
} finally {
250200
console.log("finally");
251-
enableAll();
201+
globalThis.transcription_mode = mode;
202+
enableAll([document.querySelector("#export_pdf")]);
252203
tts();
253204
}
254205
}
255206

256207
/**
257208
* Disables all form elements on the page
258209
*/
259-
function disableAll() {
210+
function disableAll(include_elements = []) {
260211
// Select all the forms on the page
261212
const forms = Array.from(document.querySelectorAll("form"));
262213

263214
// Iterate through each form and disable all its elements
264215
forms.forEach((form) => {
265-
Array.from(form.elements).forEach((element) => {
266-
element.disabled = true;
267-
});
216+
Array.from(form.elements)
217+
.concat(include_elements)
218+
.forEach((element) => {
219+
element.disabled = true;
220+
});
268221
});
269222
}
270223

271-
function enableAll() {
224+
function enableAll(include_elements = []) {
272225
// Get all the form elements on the page
273226
const forms = Array.from(document.querySelectorAll("form"));
274227
forms.forEach((form) => {
275228
// Enable all elements in the form
276-
Array.from(form.elements).forEach((element) => {
277-
element.disabled = false;
278-
});
229+
Array.from(form.elements)
230+
.concat(include_elements)
231+
.forEach((element) => {
232+
element.disabled = false;
233+
});
279234
});
280235
}
281236

282-
var form = document.getElementById("frm1");
283-
284237
document
285238
.getElementById("submit")
286239
.addEventListener("click", () => transcribe("default"));
@@ -445,4 +398,11 @@ function selectTTS(language) {
445398
}
446399
}
447400

401+
const isDarkMode = () => document.body.classList.contains("dark_mode");
402+
448403
document.getElementById("lang").addEventListener("change", giveSelection);
404+
document
405+
.getElementById("export_pdf")
406+
.addEventListener("click", () =>
407+
toPdf(globalThis.transcription_mode, isDarkMode()),
408+
);

wiktionary_pron/scripts/pdf-lib.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)