|
| 1 | +<script> |
| 2 | + import { base } from '$app/paths'; |
| 3 | + import config from '$lib/data/config'; |
| 4 | + import { convertStyle } from '$lib/data/stores'; |
| 5 | + import initSqlJs from 'sql.js'; |
| 6 | + import { afterUpdate, onMount } from 'svelte'; |
| 7 | +
|
| 8 | + export let selectedWord; |
| 9 | + export let vernacularWordsList; |
| 10 | + export let vernacularLanguage; |
| 11 | + export let onSelectWord; |
| 12 | + export let onSwitchLanguage; |
| 13 | +
|
| 14 | + let xmlData = ''; |
| 15 | +
|
| 16 | + let singleEntryStyles = config.singleEntryStyles; |
| 17 | +
|
| 18 | + async function queryXmlByWordId(wordId) { |
| 19 | + try { |
| 20 | + const SQL = await initSqlJs({ |
| 21 | + locateFile: (file) => `${base}/wasm/sql-wasm.wasm` |
| 22 | + }); |
| 23 | +
|
| 24 | + const response = await fetch(`${base}/data.sqlite`); |
| 25 | + if (!response.ok) { |
| 26 | + throw new Error( |
| 27 | + `Failed to fetch database: ${response.status} ${response.statusText}` |
| 28 | + ); |
| 29 | + } |
| 30 | + const buffer = await response.arrayBuffer(); |
| 31 | + const db = new SQL.Database(new Uint8Array(buffer)); |
| 32 | + if (!db) { |
| 33 | + console.error('Database not initialized'); |
| 34 | + return null; |
| 35 | + } |
| 36 | +
|
| 37 | + const stmt = db.prepare('SELECT xml FROM entries WHERE id = ?'); |
| 38 | + stmt.bind([wordId]); |
| 39 | +
|
| 40 | + let result = null; |
| 41 | + if (stmt.step()) { |
| 42 | + result = stmt.getAsObject().xml; |
| 43 | + } |
| 44 | + stmt.free(); |
| 45 | + db.close(); |
| 46 | +
|
| 47 | + return result; |
| 48 | + } catch (error) { |
| 49 | + console.error(`Error querying XML for word ID ${wordId}:`, error); |
| 50 | + return null; |
| 51 | + } |
| 52 | + } |
| 53 | +
|
| 54 | + function formatXmlByClass(xmlString) { |
| 55 | + if (!xmlString) return ''; |
| 56 | +
|
| 57 | + const parser = new DOMParser(); |
| 58 | + const xmlDoc = parser.parseFromString(xmlString, 'text/xml'); |
| 59 | +
|
| 60 | + // Check if parsing failed |
| 61 | + const parseError = xmlDoc.querySelector('parsererror'); |
| 62 | + if (parseError) { |
| 63 | + console.error('XML parsing error:', parseError.textContent); |
| 64 | + return `<span class="text-error">Error parsing XML: Invalid format</span>`; |
| 65 | + } |
| 66 | +
|
| 67 | + function processNode(node, parentHasSenseNumber = false) { |
| 68 | + let output = ''; |
| 69 | +
|
| 70 | + if (node.nodeType === Node.TEXT_NODE) { |
| 71 | + return node.nodeValue.trim() ? node.nodeValue + ' ' : ''; |
| 72 | + } |
| 73 | +
|
| 74 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 75 | + let className = node.getAttribute('class') || ''; |
| 76 | + let isSenseNumber = className.includes('sensenumber'); |
| 77 | +
|
| 78 | + let parentContainsSenseNumber = |
| 79 | + parentHasSenseNumber || |
| 80 | + [...node.parentNode.children].some( |
| 81 | + (child) => |
| 82 | + child.getAttribute && |
| 83 | + (child.getAttribute('class') || '').includes('sensenumber') |
| 84 | + ); |
| 85 | +
|
| 86 | + if (node.tagName === 'a' && node.hasAttribute('href')) { |
| 87 | + const href = node.getAttribute('href'); |
| 88 | + const match = href.match(/E-(\d+)/); // Extract index number |
| 89 | + if (match) { |
| 90 | + const index = parseInt(match[1], 10); // Extracted number as integer |
| 91 | + const wordObject = vernacularWordsList.find((item) => item.id === index); |
| 92 | + const word = wordObject ? wordObject.name : 'Unknown'; // Fallback if not found |
| 93 | + const homonymIndex = wordObject ? wordObject.homonym_index : 1; // Default to 1 if not found |
| 94 | +
|
| 95 | + let linkText = node.textContent.trim(); |
| 96 | +
|
| 97 | + // If the text inside the link matches the homonym index, use the homonym index as the text |
| 98 | + if (linkText === String(homonymIndex)) { |
| 99 | + linkText = homonymIndex.toString(); |
| 100 | + } |
| 101 | +
|
| 102 | + output += `<span class="clickable cursor-pointer" data-word="${word}" data-index="${index}" data-homonym="${homonymIndex}">${linkText}</span>`; |
| 103 | + return output; |
| 104 | + } |
| 105 | + } else { |
| 106 | + output += '<' + node.tagName; |
| 107 | + for (let attr of node.attributes) { |
| 108 | + output += ` ${attr.name}="${attr.value}"`; |
| 109 | + } |
| 110 | + output += '>'; |
| 111 | +
|
| 112 | + for (let child of node.childNodes) { |
| 113 | + output += processNode(child, parentContainsSenseNumber || isSenseNumber); |
| 114 | + } |
| 115 | +
|
| 116 | + output += `</${node.tagName}>`; |
| 117 | + } |
| 118 | + } |
| 119 | +
|
| 120 | + return output; |
| 121 | + } |
| 122 | +
|
| 123 | + return processNode(xmlDoc.documentElement); |
| 124 | + } |
| 125 | +
|
| 126 | + async function updateXmlData() { |
| 127 | + if ( |
| 128 | + !selectedWord || |
| 129 | + (!selectedWord.index && (!selectedWord.indexes || selectedWord.indexes.length === 0)) |
| 130 | + ) { |
| 131 | + xmlData = ''; |
| 132 | + return; |
| 133 | + } |
| 134 | +
|
| 135 | + let wordIds = selectedWord.indexes ? selectedWord.indexes : [selectedWord.index]; |
| 136 | + let xmlResults = await Promise.all(wordIds.map(queryXmlByWordId)); |
| 137 | +
|
| 138 | + // Insert an `<hr>` tag or a visible separator between entries |
| 139 | + xmlData = |
| 140 | + xmlResults |
| 141 | + .filter((xml) => xml) // Ensure no null values are included |
| 142 | + .map(formatXmlByClass) |
| 143 | + .join('\n<hr>\n') + '\n<hr>\n'; // `<hr>` adds a visible line between entries |
| 144 | + } |
| 145 | +
|
| 146 | + function attachEventListeners() { |
| 147 | + const spans = document.querySelectorAll('.clickable'); |
| 148 | +
|
| 149 | + spans.forEach((span) => { |
| 150 | + const oldSpan = span.cloneNode(true); |
| 151 | + span.parentNode.replaceChild(oldSpan, span); |
| 152 | + }); |
| 153 | +
|
| 154 | + const freshSpans = document.querySelectorAll('.clickable'); |
| 155 | + freshSpans.forEach((span) => { |
| 156 | + span.addEventListener('click', () => { |
| 157 | + onSwitchLanguage(vernacularLanguage); |
| 158 | + const word = span.getAttribute('data-word'); |
| 159 | + const index = parseInt(span.getAttribute('data-index'), 10); |
| 160 | + const homonym_index = parseInt(span.getAttribute('data-homonym'), 10); |
| 161 | +
|
| 162 | + onSelectWord({ |
| 163 | + word, |
| 164 | + index, |
| 165 | + homonym_index |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | + } |
| 170 | +
|
| 171 | + function applyStyles() { |
| 172 | + for (let stl of singleEntryStyles) { |
| 173 | + for (let elm of document.querySelectorAll(stl.name)) { |
| 174 | + elm.style = convertStyle(stl.properties); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | +
|
| 179 | + onMount(updateXmlData); |
| 180 | +
|
| 181 | + afterUpdate(() => { |
| 182 | + updateXmlData(); |
| 183 | + applyStyles(); |
| 184 | + attachEventListeners(); |
| 185 | + }); |
| 186 | +</script> |
| 187 | + |
| 188 | +<pre class="p-4 whitespace-pre-wrap break-words min-w-[100vw]">{@html xmlData}</pre> |
0 commit comments