From 735ceb42a7aee5412fbcb2bb1214d3d91ef44cee Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Thu, 30 Apr 2020 08:30:18 +0800 Subject: [PATCH] Same rendering for agenda headings --- web/src/main/frontend/components/agenda.cljs | 98 ++------------------ web/src/main/frontend/components/hiccup.cljs | 70 +++++++++----- web/src/main/frontend/format/org_mode.cljs | 10 -- web/src/main/frontend/util.cljs | 6 ++ 4 files changed, 63 insertions(+), 121 deletions(-) diff --git a/web/src/main/frontend/components/agenda.cljs b/web/src/main/frontend/components/agenda.cljs index 94014f78f..fa702ed6f 100644 --- a/web/src/main/frontend/components/agenda.cljs +++ b/web/src/main/frontend/components/agenda.cljs @@ -1,104 +1,22 @@ (ns frontend.components.agenda (:require [rum.core :as rum] - [frontend.util :as util] - [frontend.handler :as handler] [frontend.format.org.block :as block] - [frontend.state :as state] [clojure.string :as string] - [frontend.format.org-mode :as org] [frontend.components.sidebar :as sidebar] - [frontend.db :as db] - [frontend.ui :as ui])) - -(rum/defc timestamps-cp - [timestamps] - [:ul - (for [[type {:keys [date time]}] timestamps] - (let [{:keys [year month day]} date - {:keys [hour min]} time] - [:li {:key type} - [:span {:style {:margin-right 6}} type] - [:span (if time - (str year "-" month "-" day " " hour ":" min) - (str year "-" month "-" day))]]))]) - -(rum/defc title-cp - [title] - (let [title-json (js/JSON.stringify (clj->js title)) - html (org/inline-list->html title-json)] - (util/raw-html html))) - -(rum/defc children-cp - [children] - (let [children-json (js/JSON.stringify (clj->js children)) - html (org/json->html children-json)] - (util/raw-html html))) - -(rum/defc marker-cp - [marker] - (if marker - [:span {:class (str "marker-" (string/lower-case marker)) - :style {:margin-left 8}} - (if (contains? #{"DOING" "IN-PROGRESS"} marker) - (str " (" marker ")"))])) - -(rum/defc tags-cp - [tags] - [:span - (for [{:keys [tag/name]} tags] - [:span.tag {:key name} - [:span - name]])]) + [frontend.components.content :as content] + [frontend.components.hiccup :as hiccup] + [frontend.db :as db])) (rum/defc agenda [] (let [tasks (db/get-agenda)] (sidebar/sidebar [:div#agenda - [:h2.mb-3 "Agenda"] + [:h1.title "Agenda"] (if (seq tasks) [:div.ml-1 - (let [tasks (block/sort-tasks tasks)] - (for [{:heading/keys [uuid marker title priority level tags children timestamps meta repo file] :as task} tasks] - [:div.mb-2 - {:key (str "task-" uuid) - :style {:padding-left 8 - :padding-right 8}} - [:div - [:div.flex.flex-row {:style {:align-items "center"}} - (case marker - (list "DOING" "IN-PROGRESS" "TODO") - (ui/checkbox {:on-change (fn [_] - ;; FIXME: Log timestamp - (handler/check task))}) - - "WAIT" - [:span {:style {:font-weight "bold"}} - "WAIT"] - - "DONE" - (ui/checkbox {:checked true - :on-change (fn [_] - ;; FIXME: Log timestamp - (handler/uncheck task) - )}) - - nil) - [:div.flex.flex-row.ml-2 - (if priority - [:span.priority.mr-1 - (str "#[" priority "]")]) - (title-cp title) - (marker-cp marker) - (when (seq tags) - (tags-cp tags))]] - (when (seq timestamps) - (timestamps-cp timestamps)) - - ;; FIXME: parse error - ;; (when (seq children) - ;; (children-cp children)) - - ]] - ))] + (let [tasks (block/sort-tasks tasks) + id "agenda"] + (content/content id :org + {:hiccup (hiccup/->hiccup tasks {:id id})}))] "Empty")]))) diff --git a/web/src/main/frontend/components/hiccup.cljs b/web/src/main/frontend/components/hiccup.cljs index c388aeb3f..15b87b620 100644 --- a/web/src/main/frontend/components/hiccup.cljs +++ b/web/src/main/frontend/components/hiccup.cljs @@ -12,6 +12,7 @@ [frontend.expand :as expand] [frontend.components.editor :as editor] [frontend.components.svg :as svg] + [frontend.ui :as ui] [frontend.handler :as handler] [goog.object :as gobj])) @@ -278,7 +279,8 @@ heading-id (str "ls-heading-parent-" uuid) collapsed-atom? (get state ::collapsed?) toggle-collapsed? (state/sub [:ui/collapsed-headings heading-id]) - collapsed? (or toggle-collapsed? @collapsed-atom?)] + collapsed? (or toggle-collapsed? @collapsed-atom?) + agenda? (= (:id config) "agenda")] (let [edit-input-id (str "edit-box-" uuid)] (when-not lock? [:div.ls-heading-parent.flex-1 {:key (str uuid) @@ -299,25 +301,26 @@ :on-mouse-out (fn [] (when (has-children? heading-id level) (reset! control-show? false)))} - [:a.heading-control.pt-1 - {:id (str "control-" uuid) - :class "block no-underline text-gray-700 hover:text-gray-700 transition ease-in-out duration-150 mr-1" - :on-click (fn [] - (let [id (str "ls-heading-parent-" uuid)] - (if collapsed? - (expand/expand! (:id config) id) - (expand/collapse! (:id config) id)) - (reset! collapsed-atom? (not collapsed?))))} - (cond - collapsed? - (svg/caret-right) + (when-not agenda? + [:a.heading-control.pt-1 + {:id (str "control-" uuid) + :class "block no-underline text-gray-700 hover:text-gray-700 transition ease-in-out duration-150 mr-1" + :on-click (fn [] + (let [id (str "ls-heading-parent-" uuid)] + (if collapsed? + (expand/expand! (:id config) id) + (expand/collapse! (:id config) id)) + (reset! collapsed-atom? (not collapsed?))))} + (cond + collapsed? + (svg/caret-right) - (and @control-show? - (has-children? heading-id level)) - (svg/caret-down) + (and @control-show? + (has-children? heading-id level)) + (svg/caret-down) - :else - nil)] + :else + nil)]) (if @edit? (editor/box content {:on-hide (fn [value] @@ -333,7 +336,8 @@ edit-input-id) [:div.flex-1.heading-body {:on-click (fn [e] - (when-not (util/link? (gobj/get e "target")) + (when-not (or (util/link? (gobj/get e "target")) + (util/input? (gobj/get e "target"))) (reset! edit? true) (handler/reset-cursor-range! (gdom/getElement heading-id)) (swap! state/state assoc @@ -348,10 +352,33 @@ (rum/with-key (heading-child block) (cljs.core/random-uuid)))))])]])))) +(rum/defc heading-checkbox + [heading class] + (case (:heading/marker heading) + (list "DOING" "IN-PROGRESS" "TODO" "WAIT") + (ui/checkbox {:class class + :style {:margin-top -2} + :on-change (fn [_e] + ;; FIXME: Log timestamp + (handler/check heading))}) + + "DONE" + (ui/checkbox {:checked true + :class class + :style {:margin-top -2} + :on-change (fn [_e] + ;; FIXME: Log timestamp + (handler/uncheck heading))}) + + nil)) + (defn heading [config {:heading/keys [uuid title tags marker level priority anchor meta numbering children] :as t}] - (let [marker (if marker + (let [agenda? (= (:id config) "agenda") + checkbox (heading-checkbox t + (str "mr-1 cursor" (when-not agenda? " ml-1"))) + marker (if (contains? #{"DOING" "IN-PROGRESS" "WAIT"} marker) [:span {:class (str "task-status " (string/lower-case marker)) :style {:margin-right 3.5}} (string/upper-case marker)]) @@ -376,7 +403,8 @@ :uuid (str uuid)} (remove-nils (concat - [level-str + [(when-not agenda? level-str) + checkbox marker priority] (map-inline title) diff --git a/web/src/main/frontend/format/org_mode.cljs b/web/src/main/frontend/format/org_mode.cljs index 0d1e296cc..3fb28f7a6 100644 --- a/web/src/main/frontend/format/org_mode.cljs +++ b/web/src/main/frontend/format/org_mode.cljs @@ -37,16 +37,6 @@ (parse-json config) (util/json->clj))))) -(defn inline-list->html - [json] - (when (loaded?) - (.inlineListToHtmlStr js/window.MldocOrg json))) - -(defn json->html - [json] - (when (loaded?) - (.jsonToHtmlStr js/window.MldocOrg json default-config))) - (defrecord OrgMode [] protocol/Format (toHtml [this content config] diff --git a/web/src/main/frontend/util.cljs b/web/src/main/frontend/util.cljs index f52b6e652..6a4eb71cc 100644 --- a/web/src/main/frontend/util.cljs +++ b/web/src/main/frontend/util.cljs @@ -315,6 +315,12 @@ #{"A" "BUTTON"} (gobj/get node "tagName"))) +(defn input? + [node] + (contains? + #{"INPUT"} + (gobj/get node "tagName"))) + (defn journal? [path] (string/starts-with? path "journals/"))