From 059943db1750629eb58dab019b668a23f7da76b8 Mon Sep 17 00:00:00 2001 From: rcmerci Date: Fri, 18 Jun 2021 17:59:55 +0800 Subject: [PATCH] feat(util): add thingatpt.cljs (thing-at-point) --- src/main/frontend/util/thingatpt.cljs | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/frontend/util/thingatpt.cljs diff --git a/src/main/frontend/util/thingatpt.cljs b/src/main/frontend/util/thingatpt.cljs new file mode 100644 index 000000000..cdeee4623 --- /dev/null +++ b/src/main/frontend/util/thingatpt.cljs @@ -0,0 +1,36 @@ +(ns frontend.util.thingatpt + (:require [clojure.string :as string] + [frontend.util :as util] + [frontend.state :as state] + [goog.object :as gobj] + [frontend.util.cursor :as cursor] + )) + +(defn- get-content&pos-at-point [] + (when-let [input (state/get-input)] + [(gobj/get input "value") (cursor/pos input)])) + + +(defn block-ref-at-point [] + (when-let [[content pos] (get-content&pos-at-point)] + (let [start (string/last-index-of content "((" pos) + end (string/index-of content "))" (- pos 2)) + end* (+ 2 end)] + (when (and start end) + (let [block-ref-id (subs content (+ start 2) end)] + (when (every? false? (mapv #(string/includes? block-ref-id %) ["((" "))" " "])) + {:content (subs content start end*) + :start start + :end end*})))))) + +(defn embed-macro-at-point [] + (when-let [[content pos] (get-content&pos-at-point)] + (let [start (string/last-index-of content "{{embed" pos) + end (string/index-of content "}}" (- pos 2)) + end* (+ 2 end)] + (when (and start end) + (let [macro-content (subs content (+ start 2) end)] + (when (every? false? (mapv #(string/includes? macro-content %) ["{{embed" "}}"])) + {:content (subs content start end*) + :start start + :end end*}))))))