Skip to content

Commit

Permalink
- added support for hiding the ui header; server refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
daslu committed Sep 13, 2024
1 parent eb9968a commit 2fc4c6d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file. This change
- applying `:kindly/options` in all hiccup-based pathways
- bugfix: using the appropriate Quarto target format in Markdown generation
- removed the `:fontsize` setting in the default config (as it interferred with revealjs slides)
- added support for hiding the ui header; server refactoring

## [2-beta15] - 2024-07-27
- introducing code-and-value and horizontal layout - WIP (PR #127)
Expand Down
76 changes: 39 additions & 37 deletions src/scicloj/clay/v2/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
[scicloj.clay.v2.util.time :as time]
[scicloj.clay.v2.item :as item]
[clojure.string :as str]
[hiccup.core :as hiccup])
[hiccup.core :as hiccup]
[tablecloth.api :as tc])
(:import (java.net ServerSocket)))

(def default-port 1971)
Expand All @@ -29,7 +30,6 @@
(catch Exception e nil))
(recur (inc port)))))


(defn communication-script [{:keys [port counter]}]
(format "
<script type=\"text/javascript\">
Expand Down Expand Up @@ -67,49 +67,48 @@

(defn header [state]
(hiccup.core/html
[:div
[:div
[:img
{:style {:display "inline-block"
:zoom 1
:width "40px"
:margin-left "20px"}
;; { zoom: 1; vertical-align: top; font-size: 12px;}
:src "/Clay.svg.png"
:alt "Clay logo"}]
#_[:big [:big "(Clay)"]]
[:div {:style {:display "inline-block"
:margin "20px"}}
[:pre {:style {:margin 0}}
(some->> state
:full-target-path)]
[:pre {:style {:margin 0}}
(time/now)]]]
#_(:hiccup item/separator)]))
[:div
[:div
[:img
{:style {:display "inline-block"
:zoom 1
:width "40px"
:margin-left "20px"}
;; { zoom: 1; vertical-align: top; font-size: 12px;}
:src "/Clay.svg.png"
:alt "Clay logo"}]
#_[:big [:big "(Clay)"]]
[:div {:style {:display "inline-block"
:margin "20px"}}
[:pre {:style {:margin 0}}
(some->> state
:last-rendered-spec
:full-target-path)]
[:pre {:style {:margin 0}}
(time/now)]]]
#_(:hiccup item/separator)]))

(defn page
([]
(page @server.state/*state))
([state]
(let [relative-path (some-> state
:full-target-path
(string/replace (re-pattern (str "^"
(:base-target-path state)
"/"))
""))]
(some-> state
:full-target-path
slurp))))
(some-> state
:last-rendered-spec
:full-target-path
slurp)))

(defn wrap-html [html state]
(-> html
(str/replace #"(<\s*body[^>]*>)"
(str "$1"
(hiccup/html
#_[:style "* {margin: 0; padding: 0; top: 0;}"]
[:div {:style {:height "70px"
:background-color "#eee"}}
(header state)])
(when-not (-> state
:last-rendered-spec
:hide-ui-header)
(hiccup/html
#_[:style "* {margin: 0; padding: 0; top: 0;}"]
[:div {:style {:height "70px"
:background-color "#eee"}}
(header state)]))
(communication-script state)))))

(defn routes [{:keys [:body :request-method :uri]
Expand Down Expand Up @@ -182,7 +181,8 @@
(println "serving Clay at" (port->url port))
(browse!)))))

(defn update-page! [{:keys [show
(defn update-page! [{:as spec
:keys [show
base-target-path
page
full-target-path]
Expand All @@ -195,7 +195,9 @@
(io/make-parents full-target-path)
(when page
(spit full-target-path page))
(server.state/reset-full-target-path! full-target-path)
(-> spec
(assoc :full-target-path full-target-path)
(server.state/reset-last-rendered-spec!))
(when show
(broadcast! "refresh"))
[:ok])
Expand Down
8 changes: 4 additions & 4 deletions src/scicloj/clay/v2/server/state.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
(defonce *state
(atom {:port nil
:counter 0
:full-target-path nil
:base-target-path nil}))
:base-target-path nil
:last-rendered-spec nil}))

(defn swap-state! [f & args]
(-> *state
Expand All @@ -20,8 +20,8 @@
(update :counter inc)
(#(apply f % args))))))

(defn reset-full-target-path! [path]
(swap-state-and-increment! assoc :full-target-path path))
(defn reset-last-rendered-spec! [spec]
(swap-state-and-increment! assoc :last-rendered-spec spec))

(defn set-port! [port]
(swap-state! assoc :port port))
Expand Down

0 comments on commit 2fc4c6d

Please sign in to comment.