From 367f2889419a52c035e9567b18a1fedec2728994 Mon Sep 17 00:00:00 2001 From: Yukun Guo Date: Thu, 3 Dec 2020 21:37:30 +0800 Subject: [PATCH 1/9] fix: only periodically push current repo --- src/main/frontend/components/repo.cljs | 1 + src/main/frontend/config.cljs | 4 +-- src/main/frontend/handler/repo.cljs | 34 +++++++++++--------------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main/frontend/components/repo.cljs b/src/main/frontend/components/repo.cljs index c64335e62..40bc7e444 100644 --- a/src/main/frontend/components/repo.cljs +++ b/src/main/frontend/components/repo.cljs @@ -161,6 +161,7 @@ (fn [{:keys [id url]}] {:title (get-repo-name url) :options {:on-click (fn [] + (repo-handler/push-if-auto-enabled! (state/get-current-repo)) (state/set-current-repo! url) (when-not (= :draw (state/get-current-route)) (route-handler/redirect-to-home!)) diff --git a/src/main/frontend/config.cljs b/src/main/frontend/config.cljs index 33ad14846..1746ade37 100644 --- a/src/main/frontend/config.cljs +++ b/src/main/frontend/config.cljs @@ -39,9 +39,7 @@ (defn git-pull-secs [] - (if dev? - (* 60 5) - (or 60 (get-in @state/state [:config :git-pull-secs])))) + (or 60 (get-in @state/state [:config :git-pull-secs]))) (defn git-push-secs [] diff --git a/src/main/frontend/handler/repo.cljs b/src/main/frontend/handler/repo.cljs index 768dc8d4f..3eb52256a 100644 --- a/src/main/frontend/handler/repo.cljs +++ b/src/main/frontend/handler/repo.cljs @@ -474,7 +474,8 @@ (js/setTimeout setup-local-repo-if-not-exists! 100))) (defn periodically-pull - [repo-url pull-now?] + [repo-url {:keys [pull-now?] + :or {pull-now? true}}] (spec/validate :repos/url repo-url) (p/let [token (helper/get-github-token repo-url)] (when token @@ -482,18 +483,11 @@ (js/setInterval #(pull repo-url nil) (* (config/git-pull-secs) 1000))))) -(defn periodically-push-tasks - [repo-url] - (js/setInterval #(push-if-auto-enabled! repo-url) +(defn periodically-push-current-repo + [] + (js/setInterval #(push-if-auto-enabled! (state/get-current-repo)) (* (config/git-push-secs) 1000))) -(defn periodically-pull-and-push - [repo-url {:keys [pull-now?] - :or {pull-now? true}}] - (spec/validate :repos/url repo-url) - (periodically-pull repo-url pull-now?) - (periodically-push-tasks repo-url)) - (defn create-repo! [repo-url branch] (spec/validate :repos/url repo-url) @@ -509,16 +503,13 @@ (println "Something wrong!") (js/console.dir error)))) -(defn- clone-and-pull +(defn- clone-and-load-db [repo-url] (spec/validate :repos/url repo-url) (-> (p/let [_ (clone repo-url) _ (git-handler/git-set-username-email! repo-url (state/get-me))] - (load-db-and-journals! repo-url nil true) - (periodically-pull-and-push repo-url {:pull-now? false}) - ;; (periodically-persist-app-metadata repo-url) -) + (load-db-and-journals! repo-url nil true)) (p/catch (fn [error] (js/console.error error))))) @@ -535,10 +526,13 @@ (db/cloned? repo)) (do (git-handler/git-set-username-email! repo me) - (periodically-pull-and-push repo {:pull-now? true}) + (periodically-pull repo {:pull-now? true}) ;; (periodically-persist-app-metadata repo) -) - (clone-and-pull repo))))) + ) + (do + (clone-and-load-db repo) + (periodically-pull repo {:pull-now? false}))) + (periodically-push-current-repo)))) (js/setTimeout (fn [] (clone-and-pull-repos me)) 500))) @@ -551,7 +545,7 @@ (-> (p/do! (db/remove-db! url) (db/remove-files-db! url) (fs/rmdir (util/get-repo-dir url)) - (clone-and-pull url)) + (clone-and-load-db url)) (p/catch (fn [error] (prn "Delete repo failed, error: " error))))) From 2d50185379499f35cf8def08d9072ac476c78db4 Mon Sep 17 00:00:00 2001 From: Yukun Guo Date: Fri, 4 Dec 2020 15:13:55 +0800 Subject: [PATCH 2/9] fix: only periodically pull current repo --- src/main/frontend/handler/repo.cljs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/frontend/handler/repo.cljs b/src/main/frontend/handler/repo.cljs index 3eb52256a..a7bd7d83b 100644 --- a/src/main/frontend/handler/repo.cljs +++ b/src/main/frontend/handler/repo.cljs @@ -473,15 +473,15 @@ (state/set-db-restoring! false))) (js/setTimeout setup-local-repo-if-not-exists! 100))) -(defn periodically-pull - [repo-url {:keys [pull-now?] - :or {pull-now? true}}] - (spec/validate :repos/url repo-url) - (p/let [token (helper/get-github-token repo-url)] - (when token - (when pull-now? (pull repo-url nil)) - (js/setInterval #(pull repo-url nil) - (* (config/git-pull-secs) 1000))))) +(defn periodically-pull-current-repo + [] + (js/setInterval + (fn [] + (p/let [repo-url (state/get-current-repo) + token (helper/get-github-token repo-url)] + (when token + (pull repo-url nil)))) + (* (config/git-pull-secs) 1000))) (defn periodically-push-current-repo [] @@ -526,12 +526,12 @@ (db/cloned? repo)) (do (git-handler/git-set-username-email! repo me) - (periodically-pull repo {:pull-now? true}) + (pull repo nil) ;; (periodically-persist-app-metadata repo) ) (do - (clone-and-load-db repo) - (periodically-pull repo {:pull-now? false}))) + (clone-and-load-db repo))) + (periodically-pull-current-repo) (periodically-push-current-repo)))) (js/setTimeout (fn [] (clone-and-pull-repos me)) From 1b4b22013386492ad70d2e6d4ab53346b06528b4 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Sat, 5 Dec 2020 00:33:09 +0800 Subject: [PATCH 3/9] fix(git): allow users to push at any time --- src/main/frontend/handler/repo.cljs | 51 +++++++++++++++-------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/main/frontend/handler/repo.cljs b/src/main/frontend/handler/repo.cljs index a7bd7d83b..3732a05e2 100644 --- a/src/main/frontend/handler/repo.cljs +++ b/src/main/frontend/handler/repo.cljs @@ -60,8 +60,7 @@ (db/reset-file! repo-url path content) (db/reset-config! repo-url content) (when-not (= content old-content) - (git-handler/git-add repo-url path)))) -)))) + (git-handler/git-add repo-url path)))))))) (defn create-contents-file [repo-url] @@ -324,35 +323,37 @@ (push repo-url {:merge-push-no-diff? true :commit-message "Merge push without diffed files"})))))))) (p/catch - (fn [error] - (cond - (string/includes? (str error) "404") - (do (log/error :git/pull-error error) - (show-install-error! repo-url (util/format "Failed to fetch %s." repo-url))) + (fn [error] + (cond + (string/includes? (str error) "404") + (do (log/error :git/pull-error error) + (show-install-error! repo-url (util/format "Failed to fetch %s." repo-url))) - (string/includes? (str error) "401") - (let [remain-times (dec try-times)] - (if (> remain-times 0) - (let [new-opts (merge opts {:try-times remain-times})] - (pull repo-url new-opts)) - (let [error-msg - (util/format "Failed to fetch %s. It may be caused by token expiration or missing." repo-url)] - (log/error :git/pull-error error) - (notification/show! error-msg :error false)))) + (string/includes? (str error) "401") + (let [remain-times (dec try-times)] + (if (> remain-times 0) + (let [new-opts (merge opts {:try-times remain-times})] + (pull repo-url new-opts)) + (let [error-msg + (util/format "Failed to fetch %s. It may be caused by token expiration or missing." repo-url)] + (log/error :git/pull-error error) + (notification/show! error-msg :error false)))) - :else - (log/error :git/pull-error error))))))))))))) + :else + (log/error :git/pull-error error))))))))))))) (defn push - [repo-url {:keys [commit-message merge-push-no-diff?] - :or {commit-message "Logseq auto save" + [repo-url {:keys [commit-message merge-push-no-diff? custom-commit?] + :or {custom-commit false + commit-message "Logseq auto save" merge-push-no-diff? false}}] (spec/validate :repos/url repo-url) (let [status (db/get-key-value repo-url :git/status)] (if (and (db/cloned? repo-url) (not (state/get-edit-input-id)) - (not= status :pushing)) + (or (not= status :pushing) + custom-commit?)) (-> (p/let [files (js/window.workerThread.getChangedFiles (util/get-repo-dir (state/get-current-repo)))] (when (or (seq files) merge-push-no-diff?) ;; auto commit if there are any un-committed changes @@ -362,7 +363,8 @@ (p/let [commit-oid (git/commit repo-url commit-message) token (helper/get-github-token repo-url) status (db/get-key-value repo-url :git/status)] - (when (and token (not= status :pushing)) + (when (and token (or (not= status :pushing) + custom-commit?)) (git-handler/set-git-status! repo-url :pushing) (util/p-handle (git/push repo-url token merge-push-no-diff?) @@ -528,7 +530,7 @@ (git-handler/git-set-username-email! repo me) (pull repo nil) ;; (periodically-persist-app-metadata repo) - ) +) (do (clone-and-load-db repo))) (periodically-pull-current-repo) @@ -552,4 +554,5 @@ (defn git-commit-and-push! [commit-message] (when-let [repo (state/get-current-repo)] - (push repo {:commit-message commit-message}))) + (push repo {:commit-message commit-message + :custom-commit? true}))) From d563512b3814c39d7f8e95ef9be710d94fcec86d Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Sat, 5 Dec 2020 00:33:43 +0800 Subject: [PATCH 4/9] chore: bump minor version --- src/main/frontend/version.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/frontend/version.cljs b/src/main/frontend/version.cljs index f95e20c1f..2ddf5eb9f 100644 --- a/src/main/frontend/version.cljs +++ b/src/main/frontend/version.cljs @@ -1,3 +1,3 @@ (ns frontend.version) -(defonce version "0.0.4.7-1") +(defonce version "0.0.4.7-2") From f9c7da14f932f0ffc28efde3a3dddc63bb3ce94f Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Sat, 5 Dec 2020 00:40:16 +0800 Subject: [PATCH 5/9] fix(git): only call periodically-pull-current-repo once --- src/main/frontend/handler/repo.cljs | 32 ++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/frontend/handler/repo.cljs b/src/main/frontend/handler/repo.cljs index 3732a05e2..5793b8caa 100644 --- a/src/main/frontend/handler/repo.cljs +++ b/src/main/frontend/handler/repo.cljs @@ -519,22 +519,22 @@ [me] (spec/validate :state/me me) (if (and js/window.git js/window.pfs) - (doseq [{:keys [id url]} (:repos me)] - (let [repo url] - (p/let [config-exists? (fs/file-exists? - (util/get-repo-dir url) - ".git/config")] - (if (and config-exists? - (db/cloned? repo)) - (do - (git-handler/git-set-username-email! repo me) - (pull repo nil) - ;; (periodically-persist-app-metadata repo) -) - (do - (clone-and-load-db repo))) - (periodically-pull-current-repo) - (periodically-push-current-repo)))) + (do + (doseq [{:keys [id url]} (:repos me)] + (let [repo url] + (p/let [config-exists? (fs/file-exists? + (util/get-repo-dir url) + ".git/config")] + (if (and config-exists? + (db/cloned? repo)) + (do + (git-handler/git-set-username-email! repo me) + (pull repo nil)) + (do + (clone-and-load-db repo)))))) + + (periodically-pull-current-repo) + (periodically-push-current-repo)) (js/setTimeout (fn [] (clone-and-pull-repos me)) 500))) From df36d977ee00594dead474debe978681c2b920e7 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Sat, 5 Dec 2020 11:13:34 +0800 Subject: [PATCH 6/9] feat: macro render support both inline and block elements --- resources/css/common.css | 7 +++ src/main/frontend/components/block.cljs | 57 ++++++++++++++++--------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/resources/css/common.css b/resources/css/common.css index 5562ba1aa..2178c7240 100644 --- a/resources/css/common.css +++ b/resources/css/common.css @@ -1465,3 +1465,10 @@ a.tag:hover { .hide-scrollbar::-webkit-scrollbar { display: none; } + +.left { + float: left; +} +.right { + float: right; +} diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 9e6f18792..942253539 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -382,6 +382,28 @@ [:span.warning.mr-1 {:title "Block ref invalid"} (util/format "((%s))" id)])))) +(defn inline-text + [format v] + (when (string? v) + (let [inline-list (mldoc/inline->edn v (mldoc/default-config format))] + [:div.inline.mr-1 (map-inline {} inline-list)]))) + +(defn- render-macro + [config name arguments macro-content format] + (if macro-content + (let [ast (->> (mldoc/->edn macro-content (mldoc/default-config format)) + (map first)) + block? (contains? #{"Paragraph" + "Raw_Html" + "Hiccup"} + (ffirst ast))] + (if block? + [:div + (markup-elements-cp (assoc config :block/format format) ast)] + (inline-text format macro-content))) + [:span.warning {:title (str "Unsupported macro name: " name)} + (macro->text name arguments)])) + (defn inline [{:keys [html-export?] :as config} item] (match item @@ -650,7 +672,8 @@ :else (if-let [block-uuid (:block/uuid config)] - (let [macro-content (or + (let [format (get-in config [:block :block/format] :markdown) + macro-content (or (-> (db/entity [:block/uuid block-uuid]) (:block/page) (:db/id) @@ -659,17 +682,15 @@ :macros (get name)) (get-in (state/get-config) [:macros name]) - (get-in (state/get-config) [:macros (keyword name)]))] - [:span - (if (and (seq arguments) macro-content) - (block/macro-subs macro-content arguments) - (or - macro-content - [:span.warning {:title (str "Unsupported macro name: " name)} - (macro->text name arguments)]))]) + (get-in (state/get-config) [:macros (keyword name)])) + macro-content (if (and (seq arguments) macro-content) + (block/macro-subs macro-content arguments) + macro-content)] + (render-macro config name arguments macro-content format)) - [:span - (macro->text name arguments)]))) + (when-let [macro-txt (macro->text name arguments)] + (let [format (get-in config [:block :block/format] :markdown)] + (render-macro config name arguments macro-txt format)))))) :else "")) @@ -978,12 +999,6 @@ [:div.pre-block.bg-base-2.p-2 (markup-elements-cp (assoc config :block/format format) ast)])) -(defn property-value - [format v] - (when (string? v) - (let [inline-list (mldoc/inline->edn v (mldoc/default-config format))] - [:div.inline.mr-1 (map-inline {} inline-list)]))) - (rum/defc properties-cp [block] (let [properties (apply dissoc (:block/properties block) text/hidden-properties)] @@ -993,7 +1008,7 @@ [:div.my-1 [:b k] [:span.mr-1 ":"] - (property-value (:block/format block) v)])]))) + (inline-text (:block/format block) v)])]))) (rum/defcs timestamp-cp < rum/reactive (rum/local false ::show?) @@ -1579,14 +1594,14 @@ (if (or (= k :tags) (= k :alias)) (if (string/includes? item "[[") - (property-value format item) + (inline-text format item) (let [tag (-> item (string/replace "[" "") (string/replace "]" ""))] [:a.tag.mr-1 {:href (rfe/href :page {:name tag})} tag])) - (property-value format item))) - (property-value format v))])))] + (inline-text format item))) + (inline-text format v))])))] ["Paragraph" l] ;; TODO: speedup From 83e888d765012a3e59cebd2de776bc2144eeec0d Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Sat, 5 Dec 2020 11:30:04 +0800 Subject: [PATCH 7/9] feat: add built-in img macro --- src/main/frontend/components/block.cljs | 4 ++-- src/main/frontend/state.cljs | 19 ++++++++++++++----- src/main/frontend/version.cljs | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 942253539..320539dfb 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -681,8 +681,8 @@ :page/properties :macros (get name)) - (get-in (state/get-config) [:macros name]) - (get-in (state/get-config) [:macros (keyword name)])) + (get (state/get-macros) name) + (get (state/get-macros) (keyword name))) macro-content (if (and (seq arguments) macro-content) (block/macro-subs macro-content arguments) macro-content)] diff --git a/src/main/frontend/state.cljs b/src/main/frontend/state.cljs index 067774e16..9de1696db 100644 --- a/src/main/frontend/state.cljs +++ b/src/main/frontend/state.cljs @@ -142,6 +142,15 @@ ([repo-url] (get-in @state [:config repo-url]))) +(defonce built-in-macros + {"img" "[:img.$4 {:src \"$1\" :style {:width $2 :height $3}}]"}) + +(defn get-macros + [] + (merge + built-in-macros + (:macros (get-config)))) + (defn sub-config [] (sub :config)) @@ -501,11 +510,11 @@ (when token-result (let [{:keys [token expires_at]} token-result] (swap! state update-in [:me :repos] - (fn [repos] - (map (fn [r] - (if (= repo (:url r)) - (merge r {:token token :expires_at expires_at}) - repo)) repos)))))) + (fn [repos] + (map (fn [r] + (if (= repo (:url r)) + (merge r {:token token :expires_at expires_at}) + repo)) repos)))))) (defn set-github-installation-tokens! [tokens] diff --git a/src/main/frontend/version.cljs b/src/main/frontend/version.cljs index 2ddf5eb9f..2e51ebdbb 100644 --- a/src/main/frontend/version.cljs +++ b/src/main/frontend/version.cljs @@ -1,3 +1,3 @@ (ns frontend.version) -(defonce version "0.0.4.7-2") +(defonce version "0.0.4.7-3") From de33233ec2033c04d8298b024be3674b9d2eeb1f Mon Sep 17 00:00:00 2001 From: charlie Date: Sat, 5 Dec 2020 12:01:51 +0800 Subject: [PATCH 8/9] fix: try to fix #811 --- src/main/frontend/ui.cljs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/frontend/ui.cljs b/src/main/frontend/ui.cljs index 5ff20dd85..6db746124 100644 --- a/src/main/frontend/ui.cljs +++ b/src/main/frontend/ui.cljs @@ -18,13 +18,20 @@ (defonce css-transition (r/adapt-class CSSTransition)) (defonce textarea (r/adapt-class (gobj/get TextareaAutosize "default"))) -(rum/defc ls-textarea [{:keys [on-change] :as -props}] - (let [composition? (atom false) +(rum/defc ls-textarea < rum/reactive + [{:keys [on-change] :as -props}] + (let [skip-composition? (or + (state/sub :editor/show-page-search?) + (state/sub :editor/show-block-search?) + (state/sub :editor/show-template-search?)) + composition? (atom false) set-composition? #(reset! composition? %) on-composition (fn [e] - (case e.type - "compositionend" (do (set-composition? false) (on-change e)) - (set-composition? true))) + (if skip-composition? + (on-change e) + (case e.type + "compositionend" (do (set-composition? false) (on-change e)) + (set-composition? true)))) props (assoc -props :on-change (fn [e] (when-not @composition? (on-change e))) From 2bb969609e0ec0ee32cc11a74cbb3aa20872ebb9 Mon Sep 17 00:00:00 2001 From: charlie Date: Sat, 5 Dec 2020 12:13:21 +0800 Subject: [PATCH 9/9] fix: remove redundant bindings --- src/main/frontend/ui.cljs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/frontend/ui.cljs b/src/main/frontend/ui.cljs index 6db746124..51c850eae 100644 --- a/src/main/frontend/ui.cljs +++ b/src/main/frontend/ui.cljs @@ -19,7 +19,7 @@ (defonce textarea (r/adapt-class (gobj/get TextareaAutosize "default"))) (rum/defc ls-textarea < rum/reactive - [{:keys [on-change] :as -props}] + [{:keys [on-change] :as props}] (let [skip-composition? (or (state/sub :editor/show-page-search?) (state/sub :editor/show-block-search?) @@ -32,7 +32,7 @@ (case e.type "compositionend" (do (set-composition? false) (on-change e)) (set-composition? true)))) - props (assoc -props + props (assoc props :on-change (fn [e] (when-not @composition? (on-change e))) :on-composition-start on-composition