Fix firefox selection

pull/645/head
Tienson Qin 2020-06-07 09:44:42 +08:00
parent 608fd6c7a5
commit 42a2eab530
5 changed files with 50 additions and 29 deletions

View File

@ -46,16 +46,18 @@
(fn [state]
(mixins/listen state js/window "mouseup"
(fn [e]
(when-let [headings (seq (util/get-selected-nodes "ls-heading-parent"))]
(let [headings (remove nil? headings)
headings (remove #(d/has-class? % "dummy") headings)]
(when (seq headings)
(doseq [heading headings]
(d/add-class! heading "selected noselect"))
;; TODO: We delay this so the following "click" event won't clear the selections.
;; Needs more thinking.
(js/setTimeout #(state/set-selection-headings! headings)
200))))))
(when-not (state/in-selection-mode?)
(when-not false
(when-let [headings (seq (util/get-selected-nodes "ls-heading-parent"))]
(let [headings (remove nil? headings)
headings (remove #(d/has-class? % "dummy") headings)]
(when (seq headings)
(doseq [heading headings]
(d/add-class! heading "selected noselect"))
;; TODO: We delay this so the following "click" event won't clear the selections.
;; Needs more thinking.
(js/setTimeout #(state/set-selection-headings! headings)
200))))))))
(mixins/listen state js/window "click"
(fn [e]

View File

@ -516,6 +516,7 @@
(util/input? target)
(and (util/sup? target)
(d/has-class? target "fn")))
(handler/clear-selection! nil)
(handler/unhighlight-heading!)
(util/stop e)
(handler/reset-cursor-range! (gdom/getElement heading-id))
@ -817,13 +818,14 @@
(rum/defc headings-cp
[headings config]
(for [item headings]
(let [item (if (:heading/dummy? item)
item
(dissoc item :heading/meta))]
(rum/with-key
(heading config item)
(:heading/uuid item)))))
[:div.headings-container
(for [item headings]
(let [item (if (:heading/dummy? item)
item
(dissoc item :heading/meta))]
(rum/with-key
(heading config item)
(:heading/uuid item))))])
(defn ->hiccup
([headings config]

View File

@ -1123,8 +1123,9 @@
(dom/remove-class! heading "selected")
(dom/remove-class! heading "noselect"))
(state/clear-selection!))
(when-not (util/input? (gobj/get e "target"))
(util/clear-selection!)))
(when e
(when-not (util/input? (gobj/get e "target"))
(util/clear-selection!))))
(defn copy-selection-headings
[]

View File

@ -39,15 +39,22 @@ function getNodesBetween(rootNode, node1, node2) {
}
}
export var getSelectedNodes = function (selectionAncestor) {
export var getSelectedNodes = function (selectionAncestor, startNode) {
// from https://developer.mozilla.org/en-US/docs/Web/API/Selection
var selection = window.getSelection();
if (selection.isCollapsed) {
return [];
};
var node1 = selection.anchorNode;
var node2 = selection.focusNode;
return getNodesBetween(selectionAncestor, node1, node2);
var selection = null;
if(window.getSelection){
selection = window.getSelection();
} else if(document.selection){
selection = document.selection;
}
if(selection) {
if (selection.isCollapsed) {
return [];
};
var node2 = selection.focusNode;
return getNodesBetween(selectionAncestor, startNode, node2);
}
};
export var clearSelection = function () {

View File

@ -505,6 +505,13 @@
(and node
(rec-get-heading-node (gobj/get node "parentNode")))))
(defn- rec-get-headings-container
[node]
(if (and node (d/has-class? node "headings-container"))
node
(and node
(rec-get-headings-container (gobj/get node "parentNode")))))
;; Take the idea from https://stackoverflow.com/questions/4220478/get-all-dom-block-elements-for-selected-texts.
;; FIXME: Note that it might not works for IE.
(defn get-selected-nodes
@ -513,8 +520,10 @@
(when (gobj/get js/window "getSelection")
(let [selection (js/window.getSelection)
range (.getRangeAt selection 0)
container (gobj/get range "commonAncestorContainer")
container-nodes (array-seq (selection/getSelectedNodes container))]
container (-> (gobj/get range "commonAncestorContainer")
(rec-get-headings-container))
start-node (gobj/get range "startContainer")
container-nodes (array-seq (selection/getSelectedNodes container start-node))]
(map
(fn [node]
(if (or (= 3 (gobj/get node "nodeType"))