Brian McKenna committed fed29c6

Fix cursor position and container node calculations

Comments (0)

Files changed (1)


       (doall (map #(highlight-token sel %1 %2) wo-eof ends)))))
 (defn get-current-pos [node pos]
-  (let [nodes (.childNodes iframe-body)
-        under-nodes (goog.array.slice nodes 0 (goog.array.indexOf nodes node))
+  (let [node (if (= (.nodeType node) (.parentNode node) node)
+        nodes (.childNodes iframe-body)
+        under-nodes (goog.array.slice nodes 0 (goog.array.indexOf (goog.array.slice nodes) node))
         under-length ( under-nodes #(count (dom/getTextContent %)))]
     (+ (apply + under-length) pos)))
 (defn get-character-node [pos]
   (let [nodes (.childNodes iframe-body)
-        nodes-inners ( nodes #(count (dom/getTextContent %)))
         sum-lengths #(apply + %)
-        under-nodes (reduce #(if (< (+ (sum-lengths %1) %2) pos) (cons %2 %1) %1) [] (js->clj nodes-inners))
+        under-nodes (loop [accum [] lengths ( nodes #(count (dom/getTextContent %)))]
+                      (let [total (+ (sum-lengths accum) (first lengths))]
+                        (if (and (count lengths) (< total pos))
+                          (recur (cons (first lengths) accum) (rest lengths))
+                          accum)))
         node (aget nodes (count under-nodes))]
     (if (and node (> (.. node childNodes length) 0))
       {:pos (- pos (sum-lengths under-nodes))
        :node (aget (.childNodes node) 0)}
       (fn [e]
         (let [sel (get-selection)
               range (. sel (getRangeAt 0))
-              prev-start-node (.startContainer range)
-              prev-end-node (.endContainer range)
-              prev-start-pos (.startOffset range)
-              prev-end-pos (.endOffset range)]
+              start-node (.startContainer range)
+              end-node (.endContainer range)
+              start-pos (.startOffset range)
+              end-pos (.endOffset range)
+              start (get-current-pos start-node start-pos)
+              end (get-current-pos end-node end-pos)]
           (. field (setHtml false (get-content)))
            (catch e))
-          (let [start (get-current-pos prev-start-node prev-start-pos)
-                end (get-current-pos prev-end-node prev-end-pos)
-                nodes-pos (get-range-nodes start end)
+          (let [nodes-pos (get-range-nodes start end)
                 start-node-pos (first nodes-pos)
                 end-node-pos (second nodes-pos)
                 range (. iframe-doc (createRange))]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.