Skip to content

Commit

Permalink
fix: new file state
Browse files Browse the repository at this point in the history
viewers: interpose-comma only for vectors/maps
  • Loading branch information
mhuebert committed Jul 28, 2023
1 parent 8fe9764 commit 60da87e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
22 changes: 11 additions & 11 deletions editor2/src/main/maria/cloud/persistence.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@

(def state-source (comp doc/doc->clj (j/get :doc)))

(defn new-local-file! [& [{:as file
(defn new-blank-file! [& [{:as file
:file/keys [source name]}]]
(some-> js/window.event (j/call :preventDefault))
(let [id (str (random-uuid))]
(reset! (local-ratom id)
{:file/source (or source "")
:file/name (or name (extract-filename source))})
(routes/navigate! 'maria.cloud.views/local {:local/id id})))
(routes/navigate! 'maria.cloud.views/local {:local/id id})
true))

(defn current-file [id]
(merge @(persisted-ratom id)
Expand Down Expand Up @@ -179,15 +181,13 @@
(not= :file.provider/curriculum (:file/provider (current-file id))))

(keymaps/register-commands!
{:file/new {:bindings [(if keymaps/mac?
:Ctrl-n
:Meta-n)]
:f (fn [_] (new-local-file!))}
{:file/new {:bindings [:Shift-Mod-b]
:f (fn [_] (new-blank-file!))}
:file/duplicate {:when (every-pred :ProseView :file/id)
;; create a new gist with contents of current doc.
:f (fn [{:keys [ProseView file/id]}]
(let [source (state-source (j/get ProseView :state))]
(new-local-file! {:file/source source
(new-blank-file! {:file/source source
:file/name (some-> (:file/name (current-file id))
(swap-name (partial str "copy_of_")))})))}
:file/revert {:when (comp seq changes :file/id)
Expand All @@ -208,8 +208,8 @@

(defn use-persisted-file
"Syncs persisted file to re-db"
[id v]
[{:as file :file/keys [id source provider]}]
(h/use-effect (fn []
(when (and id v (not= :file.provider/local (:file/provider v)))
(reset! (persisted-ratom id) v)))
[id v]))
(when (not= :file.provider/local provider)
(reset! (persisted-ratom id) file)))
[id source]))
11 changes: 7 additions & 4 deletions editor2/src/main/maria/editor/code/show_values.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@
(defn punctuate ^v/el [s]
(v/x [:div.inline-block.font-bold.opacity-60 s]))

(defview show-brackets [left right more children !wrapper !parent]
(defview show-brackets [left right more children !wrapper !parent interpose-comma?]
(let [bracket-classes "flex flex-none"]
[:div.inline-flex.max-w-full.gap-list.whitespace-nowrap.text-brackets {:ref !wrapper}
[:div.items-start {:class bracket-classes} (punctuate left)]

(-> [:div.inline-flex.flex-wrap.items-end.gap-list.overflow-hidden.interpose-comma {:ref !parent}]
(-> [:div.inline-flex.flex-wrap.items-end.gap-list.overflow-hidden
{:ref !parent
:class (when interpose-comma? "interpose-comma")}]
(into (map (fn [x] x)) children))

[:div.items-end {:class bracket-classes}
Expand Down Expand Up @@ -203,7 +205,8 @@
(comp (map #(do [:span.inline-flex.align-center (show opts %)])))
coll)
!wrapper
!parent]))
!parent
(or (map? coll) (vector? coll))]))

(defview show-map-entry [opts this]
[:div.inline-flex.gap-list
Expand Down Expand Up @@ -303,7 +306,7 @@
"Evaluates reagent form within namespace of current cell"
[opts form]
(commands/code:eval-form-in-show opts
`(~'reagent.core/as-element [(fn [] ~form)])))
`(~'reagent.core/as-element [(fn [] ~form)])))

(defn handles-keys
"Specify keywords indicating what kind of values a viewer handles.
Expand Down
50 changes: 26 additions & 24 deletions editor2/src/main/maria/editor/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,15 @@
(let [!content (ui/use-context ::menu/!content)]
(h/use-effect
(fn []
(when id
(let [content (v/x [menu/doc-menu id])]
(reset! !content content)
#(swap! !content (fn [x]
(if (identical? x content)
nil
x))))))
(let [content (v/x [menu/doc-menu id])]
(reset! !content content)
#(swap! !content (fn [x]
(if (identical? x content)
nil
x)))))
[id])))

(defn use-prose-view [{:keys [default-value on-change-state]}]
(defn use-prose-view [{:keys [default-value on-change-state]} deps]
(let [!ref (h/use-state nil)
ref-fn (h/use-callback #(when % (reset! !ref %)))
!prose-view (h/use-state nil)
Expand All @@ -124,30 +123,29 @@
(reset! !prose-view ProseView)
#(j/call ProseView :destroy))
(reset! !prose-view nil)))
[default-value @!ref])
(conj deps @!ref))
[@!prose-view ref-fn]))

(ui/defview editor [params {:as file :keys [file/id]}]
(ui/defview editor* [params {:as file :keys [file/id]}]
"Returns a ref for the element where the editor is to be mounted."

(persist/use-persisted-file id file)
(persist/use-persisted-file file)
(use-menubar-title-dropdown! id)
(persist/use-recents! (::routes/path params) file)

(let [autosave! (h/use-memo persist/autosave-local-fn [id])
default-value (h/use-memo #(when file
(or (:file/source @(persist/local-ratom id))
(:file/source file)
""))
[id])
[ProseView ref-fn] (use-prose-view {:default-value default-value
(let [autosave! (h/use-memo persist/autosave-local-fn)
[ProseView ref-fn] (use-prose-view {:default-value (or (:file/source @(persist/local-ratom id))
(:file/source file)
"")
:on-change-state (fn [prev-state next-state]
(autosave! id prev-state next-state))})]
(autosave! id prev-state next-state))}
[])]

;; initialize new editors
(h/use-effect
(fn []
(when (some-> ProseView (u/guard (complement (j/get :isDestroyed))))

(keymaps/add-context :ProseView ProseView)
(commands/prose:eval-prose-view! ProseView)
(j/call ProseView :focus)))
Expand All @@ -156,8 +154,12 @@
;; set file/id context
(h/use-effect (fn []
(keymaps/add-context :file/id id)
#(keymaps/remove-context :file/id id))
[id])
(if file
[:div.relative.notebook.my-4 {:ref ref-fn}]
"Loading...")))
#(keymaps/remove-context :file/id id)))
[:div.relative.notebook.my-4 {:ref ref-fn}]))

(ui/defview editor
{:key (fn [params file] (:file/id file))}
[params file]
(if file
[editor* params file]
"Loading..."))

0 comments on commit 60da87e

Please sign in to comment.