diff --git a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/combobox.cljs b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/combobox.cljs index 239f8ae000..d53b8d0d60 100644 --- a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/combobox.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/combobox.cljs @@ -137,7 +137,8 @@ (mf/deps value resolve-stream name) (fn [id] (let [input-node (mf/ref-val ref) - {:keys [value cursor]} (tp/select-option-by-id id options-ref input-node value)] + input-value (dom/get-input-value input-node) + {:keys [value cursor]} (tp/select-option-by-id id options-ref input-node input-value)] (when value (fm/on-input-change form name value true) (rx/push! resolve-stream value) @@ -184,7 +185,9 @@ (let [input-node (mf/ref-val ref) node (dom/get-current-target event) id (dom/get-data node "id") - {:keys [value cursor]} (tp/select-option-by-id id options-ref input-node value)] + input-value (dom/get-input-value input-node) + + {:keys [value cursor]} (tp/select-option-by-id id options-ref input-node input-value)] (reset! filter-term* "") (dom/focus! input-node) diff --git a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/token_parsing.cljs b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/token_parsing.cljs index e4561152c8..d4c8057008 100644 --- a/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/token_parsing.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/management/forms/controls/token_parsing.cljs @@ -26,7 +26,7 @@ A token starts with '{', contains no spaces, and may be incomplete. Returns nil if no valid token is active." [value cursor] - (let [start (.lastIndexOf value "{" cursor)] + (let [start (.lastIndexOf value "{" (dec cursor))] (when (>= start 0) (let [between (subs value (inc start) cursor)] (when-not (re-find #"\s" between) @@ -37,43 +37,34 @@ space-index (.indexOf after " ") space-pos (when (>= space-index 0) (+ (inc start) space-index)) - - end (cond - (and close-pos - (or (nil? space-pos) - (< close-pos space-pos))) - (inc close-pos) - - space-pos - space-pos - - :else - cursor)] + open-index (.indexOf after "{") + open-pos (when (>= open-index 0) + (+ (inc start) open-index)) + candidates (->> [space-pos open-pos close-pos] + (remove nil?) + (sort)) + end (or (first candidates) cursor) + inside-token? (and (>= cursor start) (< cursor end))] {:start start - :end end})))))) + :end (if inside-token? + (inc end) + end)})))))) (defn replace-active-token "Replaces the token at the cursor with `{new-name}`. Returns {:value :cursor} with the updated value and new cursor position." [value cursor new-name] (let [new-token (str "{" new-name "}")] - (if-let [{:keys [start end]} - (find-active-token-range value cursor)] - - (let [new-value (str (subs value 0 start) - new-token - (subs value end)) - new-cursor (+ start (count new-token))] - {:value new-value - :cursor new-cursor}) - - (let [new-value (str (subs value 0 cursor) - new-token - (subs value cursor)) - new-cursor (+ cursor (count new-token))] - {:value new-value - :cursor new-cursor})))) + (if-let [{:keys [start end]} (find-active-token-range value cursor)] + {:value (str (subs value 0 start) + new-token + (subs value end)) + :cursor (+ start (count new-token))} + {:value (str (subs value 0 cursor) + new-token + (subs value cursor)) + :cursor (+ cursor (count new-token))}))) (defn active-token [value input-node] (let [cursor (dom/selection-start input-node)]