@filipc77/cowrite 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/ui/client.js +35 -0
package/package.json
CHANGED
package/ui/client.js
CHANGED
|
@@ -327,6 +327,41 @@ function computeOffset(selection, text) {
|
|
|
327
327
|
const idx = currentContent.indexOf(text);
|
|
328
328
|
if (idx !== -1) return idx;
|
|
329
329
|
|
|
330
|
+
// Exact match failed — likely a cross-element selection (e.g. multiple list
|
|
331
|
+
// items) where the browser's selection.toString() strips markdown syntax.
|
|
332
|
+
// Use the DOM block structure to scope the search.
|
|
333
|
+
const markdownBody = fileContentEl.querySelector(".markdown-body");
|
|
334
|
+
if (markdownBody && currentBlocks.length) {
|
|
335
|
+
let el = startNode.nodeType === Node.TEXT_NODE ? startNode.parentElement : startNode;
|
|
336
|
+
while (el && el.parentElement !== markdownBody) {
|
|
337
|
+
el = el.parentElement;
|
|
338
|
+
}
|
|
339
|
+
if (el && el.parentElement === markdownBody) {
|
|
340
|
+
let blockIndex = 0;
|
|
341
|
+
let sib = el.previousElementSibling;
|
|
342
|
+
while (sib) {
|
|
343
|
+
blockIndex++;
|
|
344
|
+
sib = sib.previousElementSibling;
|
|
345
|
+
}
|
|
346
|
+
if (blockIndex < currentBlocks.length) {
|
|
347
|
+
const block = currentBlocks[blockIndex];
|
|
348
|
+
const blockSource = currentContent.slice(block.sourceStart, block.sourceEnd);
|
|
349
|
+
const firstLine = text.split("\n")[0].trim();
|
|
350
|
+
if (firstLine) {
|
|
351
|
+
const lineIdx = blockSource.indexOf(firstLine);
|
|
352
|
+
if (lineIdx !== -1) return block.sourceStart + lineIdx;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Last resort: match the first line of the selection anywhere in the content
|
|
359
|
+
const firstLine = text.split("\n")[0].trim();
|
|
360
|
+
if (firstLine && firstLine !== text) {
|
|
361
|
+
const flIdx = currentContent.indexOf(firstLine);
|
|
362
|
+
if (flIdx !== -1) return flIdx;
|
|
363
|
+
}
|
|
364
|
+
|
|
330
365
|
return -1;
|
|
331
366
|
}
|
|
332
367
|
|