@@ -3,9 +3,11 @@ import {
33 asyncMapStrict ,
44 clearStorage ,
55 get_ipa_no_cache ,
6+ memoizeLocalStorage ,
67 wait ,
78} from "./utils.js" ;
89import { tts } from "./tts.js" ;
10+ import { toPdf } from "./pdf_export.js" ;
911
1012document . 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-
7222const 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 */
9444async 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-
284237document
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+
448403document . getElementById ( "lang" ) . addEventListener ( "change" , giveSelection ) ;
404+ document
405+ . getElementById ( "export_pdf" )
406+ . addEventListener ( "click" , ( ) =>
407+ toPdf ( globalThis . transcription_mode , isDarkMode ( ) ) ,
408+ ) ;
0 commit comments