mirror of
https://github.com/penpot/penpot.git
synced 2025-02-04 13:29:14 -05:00
✨ Update text name on edit
This commit is contained in:
parent
abb3a33021
commit
800d35a42c
1 changed files with 28 additions and 18 deletions
|
@ -52,6 +52,14 @@
|
||||||
(when-let [editor (:workspace-editor state)]
|
(when-let [editor (:workspace-editor state)]
|
||||||
(ts/schedule #(.focus ^js editor))))))
|
(ts/schedule #(.focus ^js editor))))))
|
||||||
|
|
||||||
|
(defn gen-name
|
||||||
|
[editor]
|
||||||
|
(when (some? editor)
|
||||||
|
(let [result
|
||||||
|
(-> (ted/get-editor-current-plain-text editor)
|
||||||
|
(txt/generate-shape-name))]
|
||||||
|
(when (not= result "") result))))
|
||||||
|
|
||||||
(defn update-editor-state
|
(defn update-editor-state
|
||||||
[{:keys [id] :as shape} editor-state]
|
[{:keys [id] :as shape} editor-state]
|
||||||
(ptk/reify ::update-editor-state
|
(ptk/reify ::update-editor-state
|
||||||
|
@ -62,7 +70,7 @@
|
||||||
(update state :workspace-editor-state dissoc id)))))
|
(update state :workspace-editor-state dissoc id)))))
|
||||||
|
|
||||||
(defn finalize-editor-state
|
(defn finalize-editor-state
|
||||||
[id]
|
[id update-name?]
|
||||||
(ptk/reify ::finalize-editor-state
|
(ptk/reify ::finalize-editor-state
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [_ state _]
|
(watch [_ state _]
|
||||||
|
@ -72,8 +80,8 @@
|
||||||
editor-state (get-in state [:workspace-editor-state id])
|
editor-state (get-in state [:workspace-editor-state id])
|
||||||
content (-> editor-state
|
content (-> editor-state
|
||||||
(ted/get-editor-current-content))
|
(ted/get-editor-current-content))
|
||||||
text (-> (ted/get-editor-current-plain-text editor-state)
|
name (gen-name editor-state)
|
||||||
(txt/generate-shape-name))
|
|
||||||
new-shape? (nil? (:content shape))]
|
new-shape? (nil? (:content shape))]
|
||||||
(if (ted/content-has-text? content)
|
(if (ted/content-has-text? content)
|
||||||
(let [content (d/merge (ted/export-content content)
|
(let [content (d/merge (ted/export-content content)
|
||||||
|
@ -93,8 +101,8 @@
|
||||||
(assoc :content content)
|
(assoc :content content)
|
||||||
(cond-> position-data
|
(cond-> position-data
|
||||||
(assoc :position-data position-data))
|
(assoc :position-data position-data))
|
||||||
(cond-> new-shape?
|
(cond-> (and update-name? (some? name))
|
||||||
(assoc :name text))
|
(assoc :name name))
|
||||||
(cond-> (or (some? width) (some? height))
|
(cond-> (or (some? width) (some? height))
|
||||||
(gsh/transform-shape (ctm/change-size shape width height))))))
|
(gsh/transform-shape (ctm/change-size shape width height))))))
|
||||||
{:undo-group (when new-shape? id)})))))
|
{:undo-group (when new-shape? id)})))))
|
||||||
|
@ -104,7 +112,7 @@
|
||||||
(dwsh/delete-shapes #{id})))))))))
|
(dwsh/delete-shapes #{id})))))))))
|
||||||
|
|
||||||
(defn initialize-editor-state
|
(defn initialize-editor-state
|
||||||
[{:keys [id content] :as shape} decorator]
|
[{:keys [id name content] :as shape} decorator]
|
||||||
(ptk/reify ::initialize-editor-state
|
(ptk/reify ::initialize-editor-state
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
|
@ -118,15 +126,17 @@
|
||||||
(assoc-in [:workspace-editor-state id] editor))))
|
(assoc-in [:workspace-editor-state id] editor))))
|
||||||
|
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [_ _ stream]
|
(watch [_ state stream]
|
||||||
;; We need to finalize editor on two main events: (1) when user
|
;; We need to finalize editor on two main events: (1) when user
|
||||||
;; explicitly navigates to other section or page; (2) when user
|
;; explicitly navigates to other section or page; (2) when user
|
||||||
;; leaves the editor.
|
;; leaves the editor.
|
||||||
|
(let [editor (dm/get-in state [:workspace-editor-state id])
|
||||||
|
update-name? (or (nil? content) (= name (gen-name editor)))]
|
||||||
(->> (rx/merge
|
(->> (rx/merge
|
||||||
(rx/filter (ptk/type? ::rt/navigate) stream)
|
(rx/filter (ptk/type? ::rt/navigate) stream)
|
||||||
(rx/filter #(= ::finalize-editor-state %) stream))
|
(rx/filter #(= ::finalize-editor-state %) stream))
|
||||||
(rx/take 1)
|
(rx/take 1)
|
||||||
(rx/map #(finalize-editor-state id))))))
|
(rx/map #(finalize-editor-state id update-name?)))))))
|
||||||
|
|
||||||
(defn select-all
|
(defn select-all
|
||||||
"Select all content of the current editor. When not editor found this
|
"Select all content of the current editor. When not editor found this
|
||||||
|
|
Loading…
Add table
Reference in a new issue