Skip to content

Commit

Permalink
frontend: always calculate the snippet indexes from the beginning
Browse files Browse the repository at this point in the history
We have been collecting incorrect indexes. They aren't calculated from
the end of the log but rather from the end of the previous node.

I am currently working on fixing our collected data.
  • Loading branch information
FrostyX committed Mar 19, 2024
1 parent 74680be commit f93049c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 20 additions & 1 deletion frontend/src/app/contribute_events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[lambdaisland.fetch :as fetch]
[app.helpers :refer
[current-path
remove-trailing-slash]]
remove-trailing-slash
previous-siblings]]
[app.editor.core :refer [active-file]]
[app.contribute-logic :refer
[file-id
Expand Down Expand Up @@ -88,8 +89,26 @@

(let [selection (.getSelection js/window)
content (.toString selection)

;; The position is calculated from the end of the last node
;; This can be be either a previous snippet span or if the text
;; longer than 65536 characters than it is implictily split into
;; multiple sibling text nodes
start (.-anchorOffset selection)

;; Calculate the real starting index from the beginning of the log
offset (->> selection
.-anchorNode
previous-siblings
(map #(.-textContent %))
(map #(count %))
(reduce +))
start (+ start offset)

;; Index of the last snippet character. When parsing in python, don't
;; forget to do text[start:end+1]
end (+ start (count content) -1)

snippet
{:text content
:start-index start
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/app/helpers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@

(defn remove-trailing-slash [text]
(clojure.string/replace text #"/$" ""))

(defn previous-siblings [node]
(let [sibling (.-previousSibling node)]
(if-not sibling
[]
(conj (previous-siblings sibling) sibling))))

0 comments on commit f93049c

Please sign in to comment.