diff options
Diffstat (limited to 'src/lib/Tools/Hayai.svelte')
| -rw-r--r-- | src/lib/Tools/Hayai.svelte | 118 |
1 files changed, 59 insertions, 59 deletions
diff --git a/src/lib/Tools/Hayai.svelte b/src/lib/Tools/Hayai.svelte index 1ba99d86..aa1112da 100644 --- a/src/lib/Tools/Hayai.svelte +++ b/src/lib/Tools/Hayai.svelte @@ -1,83 +1,83 @@ <script lang="ts"> - import Spacer from '$lib/Layout/Spacer.svelte'; - import { onMount } from 'svelte'; - import JSZip from 'jszip'; +import Spacer from '$lib/Layout/Spacer.svelte'; +import { onMount } from 'svelte'; +import JSZip from 'jszip'; - let fileInput: HTMLInputElement | null = null; +let fileInput: HTMLInputElement | null = null; - const handleFileUpload = async () => { - if (!fileInput || !fileInput.files || fileInput.files.length === 0) return; +const handleFileUpload = async () => { + if (!fileInput || !fileInput.files || fileInput.files.length === 0) return; - const file = fileInput.files[0]; - const reader = new FileReader(); + const file = fileInput.files[0]; + const reader = new FileReader(); - reader.onload = async (event) => { - const zip = await JSZip.loadAsync((event.target as FileReader).result as ArrayBuffer); - const newZip = new JSZip(); + reader.onload = async (event) => { + const zip = await JSZip.loadAsync((event.target as FileReader).result as ArrayBuffer); + const newZip = new JSZip(); - for (const relativePath in zip.files) { - const zipEntry = zip.files[relativePath]; + for (const relativePath in zip.files) { + const zipEntry = zip.files[relativePath]; - if (zipEntry.dir) { - newZip.folder(relativePath); - } else if (relativePath.endsWith('.xhtml') || relativePath.endsWith('.html')) { - newZip.file(relativePath, applyBionicReadingToHTML(await zipEntry.async('text'))); - } else { - newZip.file(relativePath, await zipEntry.async('arraybuffer')); - } + if (zipEntry.dir) { + newZip.folder(relativePath); + } else if (relativePath.endsWith('.xhtml') || relativePath.endsWith('.html')) { + newZip.file(relativePath, applyBionicReadingToHTML(await zipEntry.async('text'))); + } else { + newZip.file(relativePath, await zipEntry.async('arraybuffer')); } + } - downloadEPUB( - await newZip.generateAsync({ type: 'blob' }), - `${file.name.split('.epub')[0]}_hayai.epub` - ); - }; - - reader.readAsArrayBuffer(file); + downloadEPUB( + await newZip.generateAsync({ type: 'blob' }), + `${file.name.split('.epub')[0]}_hayai.epub` + ); }; - const applyBionicReadingToHTML = (content: string) => { - const contentParser = new DOMParser().parseFromString(content, 'text/html'); + reader.readAsArrayBuffer(file); +}; - for (const paragraph of contentParser.getElementsByTagName('p')) - paragraph.innerHTML = applyBionicReadingToString(paragraph.textContent ?? ''); +const applyBionicReadingToHTML = (content: string) => { + const contentParser = new DOMParser().parseFromString(content, 'text/html'); - return contentParser.documentElement.outerHTML; - }; + for (const paragraph of contentParser.getElementsByTagName('p')) + paragraph.innerHTML = applyBionicReadingToString(paragraph.textContent ?? ''); - const applyBionicReadingToString = (text: string) => - text - .split(/\s+/) - .map((word) => { - if (/^\W+$/.test(word) || word.length <= 2) return word; + return contentParser.documentElement.outerHTML; +}; - let boldLength; +const applyBionicReadingToString = (text: string) => + text + .split(/\s+/) + .map((word) => { + if (/^\W+$/.test(word) || word.length <= 2) return word; - if (word.length <= 4) { - boldLength = 2; - } else if (word.length <= 7) { - boldLength = 3; - } else if (word.length <= 10) { - boldLength = 4; - } else { - boldLength = Math.ceil(word.length * 0.5); - } + let boldLength; - return `<strong>${word.slice(0, boldLength)}</strong>${word.slice(boldLength)}`; - }) - .join(' '); + if (word.length <= 4) { + boldLength = 2; + } else if (word.length <= 7) { + boldLength = 3; + } else if (word.length <= 10) { + boldLength = 4; + } else { + boldLength = Math.ceil(word.length * 0.5); + } - const downloadEPUB = (blob: Blob | MediaSource, fileName: string) => { - const link = document.createElement('a'); + return `<strong>${word.slice(0, boldLength)}</strong>${word.slice(boldLength)}`; + }) + .join(' '); - link.href = URL.createObjectURL(blob); - link.download = fileName; +const downloadEPUB = (blob: Blob | MediaSource, fileName: string) => { + const link = document.createElement('a'); - link.click(); - URL.revokeObjectURL(link.href); - }; + link.href = URL.createObjectURL(blob); + link.download = fileName; + + link.click(); + URL.revokeObjectURL(link.href); +}; - onMount(() => (fileInput = document.getElementById('epub-file') as HTMLInputElement)); +onMount(() => (fileInput = document.getElementById('epub-file') as HTMLInputElement)); </script> <div class="card"> |