From 392faf552607ecde52be264ebcd6749dab9544d1 Mon Sep 17 00:00:00 2001 From: Siarhei Siniak <siarheisiniak@gmail.com> Date: Sat, 24 Aug 2024 12:40:15 +0300 Subject: [PATCH] [~] Refactor --- d2/book1/book.js | 7 ++++-- d2/book1/script.js | 55 +++++++++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/d2/book1/book.js b/d2/book1/book.js index 6612937..7baeeeb 100644 --- a/d2/book1/book.js +++ b/d2/book1/book.js @@ -1,7 +1,9 @@ (async function() { await context.loading.script.promise; - window.context.books.push(` + window.context.books.push({ + url: null, + text: ` DEATH of a HERO @@ -4748,6 +4750,7 @@ And I too walked away In an agony of helpless grief and pity. - `); + ` + }); window.context.update_books(); })(); diff --git a/d2/book1/script.js b/d2/book1/script.js index 7841f6d..3179a36 100644 --- a/d2/book1/script.js +++ b/d2/book1/script.js @@ -50,7 +50,11 @@ $(window).on('load', () => { context.update_books = () => { context.ui.books_select.empty(); window.context.books.map( - (o, i) => $('<option>').attr('value', '' + i).text(o.slice(0, 10)) + (o, i) => + $('<option>') + .attr('value', '' + i) + .attr('url', o.url || '') + .text(o.text.slice(0, 10)) ).forEach((o) => context.ui.books_select.append(o)) } @@ -163,7 +167,7 @@ $(window).on('load', () => { while ( context.callbacks.get_cookie('sentence_id') < context.sentences.length && - !context.pending_stop + !context.pending_stop ) { let sentence = @@ -236,11 +240,19 @@ $(window).on('load', () => { let book_id = parseInt(context.ui.books_select.val()); if (context.current_book != book_id) { + if (context.books[book_id].url) + { + context.callbacks.set_cookie( + 'book_url', + context.books[book_id].url, + ); + } + context.current_book = book_id; context.sentences = context.books[ context.current_book - ].replaceAll(/([\.\?\!])\s+/g,'$1\n') + ].text.replaceAll(/([\.\?\!])\s+/g,'$1\n') .split('\n'); context.ui.total_sentences_input.val( context.sentences.length, @@ -249,6 +261,7 @@ $(window).on('load', () => { let state = context.callbacks.get_state(); } } + if ( context.ui.current_sentence_input.val() != '' ) @@ -281,6 +294,18 @@ $(window).on('load', () => { context.callbacks.continuous_reading(); } }, + book_add: async (url) => { + let book = await ( + (await fetch(url)).text() + ); + //let book = prompt('enter text', ''); + //let title = prompt('enter title', ''); + //window.context.books.push(title + '\n' + book); + window.context.books.push({ + text: book, url + }); + window.context.update_books(); + }, populateVoiceList: () => { voices = synth.getVoices().sort(function (a, b) { const aname = a.name.toUpperCase(), bname = b.name.toUpperCase(); @@ -293,7 +318,7 @@ $(window).on('load', () => { for(i = 0; i < voices.length ; i++) { var option = document.createElement('option'); option.textContent = voices[i].name + ' (' + voices[i].lang + ')'; - + if(voices[i].default) { option.textContent += ' -- DEFAULT'; } @@ -331,12 +356,19 @@ $(window).on('load', () => { 0, ); } + if (state.book_url) + { + context.callbacks.book_add(state.book_url); + state.book_id = context.books.length - 1; + } + if (state.book_id) { context.ui.books_select.find( '>option', ).eq(state.book_id).attr('selected', 'selected'); } + if (state.sentence_id) { context.ui.current_sentence_input.val( @@ -355,17 +387,10 @@ $(window).on('load', () => { context.ui.add_book.on( 'click', async () => { - alert('fuck'); - let book = await ( - (await fetch( - 'books/' + prompt('enter book file', '1.txt') - )).text() - ); - //let book = prompt('enter text', ''); - //let title = prompt('enter title', ''); - //window.context.books.push(title + '\n' + book); - window.context.books.push(book); - window.context.update_books(); + // alert('fuck'); + let url = 'books/' + prompt('enter book file', '1.txt'); + + await context.callbacks.book_add(url); }, ); context.ui.read_aloud.on(