youssefreda9 commited on
Commit
b8fa1e0
·
1 Parent(s): 7abe807

fix: apply uses window.find() + add translation apply button

Browse files
Files changed (1) hide show
  1. src/index.html +34 -44
src/index.html CHANGED
@@ -1200,7 +1200,8 @@
1200
  ═══════════════════════════════════════════ */
1201
  let _quranCurrentQuery = '';
1202
  let _quranSavedRange = null;
1203
- let _quranVerseClean = ''; // the verified uthmani text (for apply)
 
1204
 
1205
  async function verifyQuranText() {
1206
  const sel = window.getSelection();
@@ -1285,12 +1286,15 @@
1285
  const transRef = refMatch ? refMatch[1] : '';
1286
 
1287
  textEl.textContent = transText;
 
1288
  // Show reference in translated language
1289
  const refEl = document.getElementById('quran-translation-ref');
1290
  if (refEl) {
1291
  refEl.textContent = transRef ? '[' + transRef + ']' : '';
1292
  refEl.style.display = transRef ? '' : 'none';
1293
  }
 
 
1294
 
1295
  } catch (err) {
1296
  textEl.innerHTML = '<span class="text-secondary">حدث خطأ في الترجمة</span>';
@@ -1302,58 +1306,41 @@
1302
  document.body.style.overflow = '';
1303
  }
1304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1305
  function applyQuranText() {
1306
  if (!_quranVerseClean || !_quranCurrentQuery) {
1307
  if (typeof showToast === 'function') showToast('لا يوجد نص للتطبيق');
1308
  return;
1309
  }
1310
- const editor = document.getElementById('editor');
1311
- if (!editor) return;
1312
-
1313
- // Close modal first
1314
- closeQuranModal();
1315
- editor.focus();
1316
-
1317
- // Walk text nodes to find the original query text
1318
- const walker = document.createTreeWalker(editor, NodeFilter.SHOW_TEXT, null, false);
1319
- const plainText = editor.textContent || '';
1320
- const queryIdx = plainText.indexOf(_quranCurrentQuery);
1321
-
1322
- if (queryIdx === -1) {
1323
  if (typeof showToast === 'function') showToast('لم يتم العثور على النص الأصلي');
1324
- return;
1325
  }
 
1326
 
1327
- let charCount = 0;
1328
- let startNode = null, startOffset = 0;
1329
- let endNode = null, endOffset = 0;
1330
- const endIdx = queryIdx + _quranCurrentQuery.length;
1331
-
1332
- while (walker.nextNode()) {
1333
- const node = walker.currentNode;
1334
- const nodeLen = node.textContent.length;
1335
- if (!startNode && charCount + nodeLen > queryIdx) {
1336
- startNode = node;
1337
- startOffset = queryIdx - charCount;
1338
- }
1339
- if (!endNode && charCount + nodeLen >= endIdx) {
1340
- endNode = node;
1341
- endOffset = endIdx - charCount;
1342
- break;
1343
- }
1344
- charCount += nodeLen;
1345
  }
1346
-
1347
- if (startNode && endNode) {
1348
- const range = document.createRange();
1349
- range.setStart(startNode, startOffset);
1350
- range.setEnd(endNode, endOffset);
1351
- const sel = window.getSelection();
1352
- sel.removeAllRanges();
1353
- sel.addRange(range);
1354
- document.execCommand('insertText', false, _quranVerseClean);
1355
- if (typeof showToast === 'function') showToast('✓ تم تطبيق النص القرآني المدقق');
1356
- editor.dispatchEvent(new Event('input', { bubbles: true }));
1357
  } else {
1358
  if (typeof showToast === 'function') showToast('لم يتم العثور على النص الأصلي');
1359
  }
@@ -1769,6 +1756,9 @@
1769
  <div id="quran-translation-result" class="is-hidden p-4 rounded-xl" style="background:rgba(6,182,212,0.06); border:1px solid rgba(6,182,212,0.15);">
1770
  <p id="quran-translation-text" style="font-size:20px; line-height:2; color:var(--color-text-primary); text-align:center;"></p>
1771
  <p id="quran-translation-ref" class="quran-reference" style="display:none;"></p>
 
 
 
1772
  </div>
1773
  </div>
1774
  </div>
 
1200
  ═══════════════════════════════════════════ */
1201
  let _quranCurrentQuery = '';
1202
  let _quranSavedRange = null;
1203
+ let _quranVerseClean = '';
1204
+ let _quranTransClean = '';
1205
 
1206
  async function verifyQuranText() {
1207
  const sel = window.getSelection();
 
1286
  const transRef = refMatch ? refMatch[1] : '';
1287
 
1288
  textEl.textContent = transText;
1289
+ _quranTransClean = transText;
1290
  // Show reference in translated language
1291
  const refEl = document.getElementById('quran-translation-ref');
1292
  if (refEl) {
1293
  refEl.textContent = transRef ? '[' + transRef + ']' : '';
1294
  refEl.style.display = transRef ? '' : 'none';
1295
  }
1296
+ // Show apply translation button
1297
+ document.getElementById('quran-apply-trans-btn').classList.remove('is-hidden');
1298
 
1299
  } catch (err) {
1300
  textEl.innerHTML = '<span class="text-secondary">حدث خطأ في الترجمة</span>';
 
1306
  document.body.style.overflow = '';
1307
  }
1308
 
1309
+ function _replaceInEditor(newText) {
1310
+ var editor = document.getElementById('editor');
1311
+ if (!editor || !_quranCurrentQuery) return false;
1312
+ // Simple: get all text content, find the query, build selection via window.find
1313
+ closeQuranModal();
1314
+ editor.focus();
1315
+ // Try window.find to select the text
1316
+ var found = window.find(_quranCurrentQuery, false, false, false, false, false, false);
1317
+ if (found) {
1318
+ document.execCommand('insertText', false, newText);
1319
+ editor.dispatchEvent(new Event('input', { bubbles: true }));
1320
+ return true;
1321
+ }
1322
+ return false;
1323
+ }
1324
+
1325
  function applyQuranText() {
1326
  if (!_quranVerseClean || !_quranCurrentQuery) {
1327
  if (typeof showToast === 'function') showToast('لا يوجد نص للتطبيق');
1328
  return;
1329
  }
1330
+ if (_replaceInEditor(_quranVerseClean)) {
1331
+ if (typeof showToast === 'function') showToast('✓ تم تطبيق النص القرآني المدقق');
1332
+ } else {
 
 
 
 
 
 
 
 
 
 
1333
  if (typeof showToast === 'function') showToast('لم يتم العثور على النص الأصلي');
 
1334
  }
1335
+ }
1336
 
1337
+ function applyQuranTranslation() {
1338
+ if (!_quranTransClean || !_quranCurrentQuery) {
1339
+ if (typeof showToast === 'function') showToast('لا يوجد ترجمة للتطبيق');
1340
+ return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1341
  }
1342
+ if (_replaceInEditor(_quranTransClean)) {
1343
+ if (typeof showToast === 'function') showToast('✓ تم تطبيق الترجمة');
 
 
 
 
 
 
 
 
 
1344
  } else {
1345
  if (typeof showToast === 'function') showToast('لم يتم العثور على النص الأصلي');
1346
  }
 
1756
  <div id="quran-translation-result" class="is-hidden p-4 rounded-xl" style="background:rgba(6,182,212,0.06); border:1px solid rgba(6,182,212,0.15);">
1757
  <p id="quran-translation-text" style="font-size:20px; line-height:2; color:var(--color-text-primary); text-align:center;"></p>
1758
  <p id="quran-translation-ref" class="quran-reference" style="display:none;"></p>
1759
+ <div class="flex items-center justify-center mt-3">
1760
+ <button id="quran-apply-trans-btn" onclick="applyQuranTranslation()" class="quran-apply-btn is-hidden" type="button">تطبيق الترجمة ✓</button>
1761
+ </div>
1762
  </div>
1763
  </div>
1764
  </div>