@filipc77/cowrite 0.4.1 → 0.4.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/ui/client.js +23 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@filipc77/cowrite",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Live commenting plugin for coding agent sessions",
5
5
  "type": "module",
6
6
  "bin": {
package/ui/client.js CHANGED
@@ -156,22 +156,31 @@ document.addEventListener("mouseup", (e) => {
156
156
  });
157
157
 
158
158
  function computeOffset(selection, text) {
159
- // For plain text: use data-offset on span.line elements
160
- const anchor = selection.anchorNode;
161
- if (!anchor) return -1;
162
-
163
- // Walk up to find a [data-offset] element
164
- let node = anchor.nodeType === Node.TEXT_NODE ? anchor.parentElement : anchor;
165
- while (node && !node.dataset?.offset && node !== fileContentEl) {
166
- node = node.parentElement;
159
+ const range = selection.getRangeAt(0);
160
+ const startNode = range.startContainer;
161
+ const startCharOffset = range.startOffset;
162
+
163
+ // Walk up from the range start to find a [data-offset] element
164
+ let lineEl = startNode.nodeType === Node.TEXT_NODE ? startNode.parentElement : startNode;
165
+ while (lineEl && !lineEl.dataset?.offset && lineEl !== fileContentEl) {
166
+ lineEl = lineEl.parentElement;
167
167
  }
168
168
 
169
- if (node?.dataset?.offset !== undefined) {
170
- // Plain text mode: compute from data-offset + text offset within the line
171
- const lineOffset = parseInt(node.dataset.offset, 10);
172
- const nodeText = node.textContent || "";
173
- const idx = nodeText.indexOf(text);
174
- if (idx !== -1) return lineOffset + idx;
169
+ if (lineEl?.dataset?.offset !== undefined) {
170
+ // Compute exact character offset within the line using the range start
171
+ const lineOffset = parseInt(lineEl.dataset.offset, 10);
172
+ // Walk text nodes inside this line element to find the position of startNode
173
+ const walker = document.createTreeWalker(lineEl, NodeFilter.SHOW_TEXT);
174
+ let charsBefore = 0;
175
+ while (walker.nextNode()) {
176
+ if (walker.currentNode === startNode) {
177
+ return lineOffset + charsBefore + startCharOffset;
178
+ }
179
+ charsBefore += walker.currentNode.textContent.length;
180
+ }
181
+ // If the start node wasn't found in this line, the selection might start
182
+ // at the line element boundary itself
183
+ return lineOffset;
175
184
  }
176
185
 
177
186
  // Fallback: search for the text in the raw content