Skip to content

Commit

Permalink
fix: get-selection-text not working on Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Nov 25, 2020
1 parent 320ac8a commit a98239e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/main/frontend/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[clojure.string :as string]
["/frontend/caret_pos" :as caret-pos]
["/frontend/selection" :as selection]
["/frontend/utils" :as utils]
[goog.string :as gstring]
[goog.string.format]
[dommy.core :as d]
Expand Down Expand Up @@ -324,8 +325,8 @@
[pos]
(when-let [main-content (gdom/getElement "main-content")]
(.scroll main-content
#js {:top pos
:behavior "smooth"})))
#js {:top pos
:behavior "smooth"})))

(defn scroll-to-top
[]
Expand Down Expand Up @@ -622,15 +623,9 @@
[input]
(and input (.-selectionStart input)))

(defn get-selection
[]
(when (gobj/get js/window "getSelection")
(js/window.getSelection)))

(defn get-selected-text
[]
(some-> (get-selection)
(.toString)))
(utils/getSelectionText))

(def clear-selection! selection/clearSelection)

Expand Down Expand Up @@ -969,5 +964,4 @@
"./2020_11_19.org")

(= (get-relative-path "a/b/c/d/g.org" "a/b/c/e/f.org")
"../e/f.org")
)
"../e/f.org"))
18 changes: 18 additions & 0 deletions src/main/frontend/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,21 @@ export var timeConversion = function (millisec) {
return days + "d"
}
}

export var getSelectionText = function() {
const selection = (window.getSelection() || '').toString().trim();
if (selection) {
return selection;
}

// Firefox fix
const activeElement = window.document.activeElement;
if (activeElement) {
if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') {
const el = activeElement;
return el.value.slice(el.selectionStart || 0, el.selectionEnd || 0);
}
}

return '';
}

0 comments on commit a98239e

Please sign in to comment.