Simplify contentEditable check for frontend routing

This commit is contained in:
Juho Teperi 2019-02-08 13:57:03 +02:00
parent f5eb1d17f5
commit 6123230f98
2 changed files with 12 additions and 5 deletions

View file

@ -27,7 +27,13 @@
[:ul [:ul
[:li [:a {:href "http://google.com"} "external link"]] [:li [:a {:href "http://google.com"} "external link"]]
[:li [:a {:href (rfe/href ::foobar)} "Missing route"]] [:li [:a {:href (rfe/href ::foobar)} "Missing route"]]
[:li [:a {:href (rfe/href ::item)} "Missing route params"]]]]) [:li [:a {:href (rfe/href ::item)} "Missing route params"]]]
[:div
{:content-editable true
:suppressContentEditableWarning true}
[:p "Link inside contentEditable element is ignored."]
[:a {:href (rfe/href ::frontpage)} "Link"]]])
(defn item-page [match] (defn item-page [match]
(let [{:keys [path query]} (:parameters match) (let [{:keys [path query]} (:parameters match)

View file

@ -4,8 +4,7 @@
(:require [reitit.core :as reitit] (:require [reitit.core :as reitit]
[reitit.core :as r] [reitit.core :as r]
[reitit.frontend :as rf] [reitit.frontend :as rf]
[goog.events :as gevents] [goog.events :as gevents])
[goog.dom :as gdom])
(:import goog.Uri)) (:import goog.Uri))
(defprotocol History (defprotocol History
@ -78,8 +77,10 @@
(not (contains? #{"_blank" "self"} (.getAttribute el "target"))) (not (contains? #{"_blank" "self"} (.getAttribute el "target")))
;; Left button ;; Left button
(= 0 (.-button e)) (= 0 (.-button e))
(reitit/match-by-path router (.getPath uri)) ;; isContentEditable property is inherited from parents,
(not (gdom/getAncestor el (fn [node] (.isContentEditable node))))) ;; so if the anchor is inside contenteditable div, the property will be true.
(not (.-isContentEditable (.-target e)))
(reitit/match-by-path router (.getPath uri)))
(.preventDefault e) (.preventDefault e)
(let [path (str (.getPath uri) (let [path (str (.getPath uri)
(if (seq (.getQuery uri)) (if (seq (.getQuery uri))