Commit ·
7abe807
1
Parent(s): d2ac012
fix: robust apply using TreeWalker text search instead of fragile Range
Browse files- src/index.html +44 -11
src/index.html
CHANGED
|
@@ -1303,27 +1303,60 @@
|
|
| 1303 |
}
|
| 1304 |
|
| 1305 |
function applyQuranText() {
|
| 1306 |
-
if (!_quranVerseClean || !
|
| 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 |
-
|
| 1316 |
-
// Focus editor and restore selection
|
| 1317 |
editor.focus();
|
| 1318 |
-
const sel = window.getSelection();
|
| 1319 |
-
sel.removeAllRanges();
|
| 1320 |
-
sel.addRange(_quranSavedRange);
|
| 1321 |
|
| 1322 |
-
//
|
| 1323 |
-
document.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1324 |
|
| 1325 |
-
|
| 1326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1327 |
}
|
| 1328 |
|
| 1329 |
|
|
|
|
| 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 |
+
}
|
| 1360 |
}
|
| 1361 |
|
| 1362 |
|