Page command enhancements

Three file commands will work in journal page when they have been
edited. These commands will also explicitly warn if no action is taken.
Also developer page commands recognize navigation now
pull/8504/head
Gabriel Horner 2023-02-01 17:44:05 -05:00 committed by Andelf
parent 917a8aa098
commit 04f1e0e076
5 changed files with 44 additions and 26 deletions

View File

@ -83,6 +83,7 @@
frontend.ui ui frontend.ui ui
frontend.util util frontend.util util
frontend.util.clock clock frontend.util.clock clock
frontend.util.page page-util
frontend.util.property property frontend.util.property property
frontend.util.persist-var persist-var frontend.util.persist-var persist-var
frontend.util.text text-util frontend.util.text text-util

View File

@ -11,6 +11,7 @@
[frontend.ui :as ui] [frontend.ui :as ui]
[frontend.util :as util] [frontend.util :as util]
[frontend.util.url :as url-util] [frontend.util.url :as url-util]
[frontend.util.page :as page-util]
[frontend.handler.shell :as shell] [frontend.handler.shell :as shell]
[frontend.mobile.util :as mobile-util] [frontend.mobile.util :as mobile-util]
[electron.ipc :as ipc] [electron.ipc :as ipc]
@ -72,7 +73,7 @@
favorited? (contains? (set (map util/page-name-sanity-lc favorites)) favorited? (contains? (set (map util/page-name-sanity-lc favorites))
page-name) page-name)
developer-mode? (state/sub [:ui/developer-mode?]) developer-mode? (state/sub [:ui/developer-mode?])
file-path (when (util/electron?) (page-handler/get-page-file-path)) file-path (when (util/electron?) (page-util/get-page-file-path page-name))
_ (state/sub :auth/id-token) _ (state/sub :auth/id-token)
file-sync-graph-uuid (and (user-handler/logged-in?) file-sync-graph-uuid (and (user-handler/logged-in?)
(file-sync-handler/enable-sync?) (file-sync-handler/enable-sync?)

View File

@ -5,6 +5,7 @@
[frontend.state :as state] [frontend.state :as state]
[frontend.handler.notification :as notification] [frontend.handler.notification :as notification]
[frontend.ui :as ui] [frontend.ui :as ui]
[frontend.util.page :as page-util]
[logseq.graph-parser.mldoc :as gp-mldoc])) [logseq.graph-parser.mldoc :as gp-mldoc]))
;; Fns used between menus and commands ;; Fns used between menus and commands
@ -42,25 +43,21 @@
;; Use editor state to locate most recent block ;; Use editor state to locate most recent block
(if-let [block-uuid (:block-id (first (state/get-editor-args)))] (if-let [block-uuid (:block-id (first (state/get-editor-args)))]
(show-entity-data [:block/uuid block-uuid]) (show-entity-data [:block/uuid block-uuid])
(notification/show! "No block found" :error))) (notification/show! "No block found" :warning)))
(defn ^:export show-block-ast [] (defn ^:export show-block-ast []
(if-let [{:block/keys [content format]} (:block (first (state/get-editor-args)))] (if-let [{:block/keys [content format]} (:block (first (state/get-editor-args)))]
(show-content-ast content format) (show-content-ast content format)
(notification/show! "No block found" :error))) (notification/show! "No block found" :warning)))
(defn ^:export show-page-data [] (defn ^:export show-page-data []
;; Use editor state to locate most recent page. (if-let [page-id (page-util/get-current-page-id)]
;; Consider replacing with navigation history if it's more useful
(if-let [page-id (get-in (first (state/get-editor-args))
[:block :block/page :db/id])]
(show-entity-data page-id) (show-entity-data page-id)
(notification/show! "No page found" :error))) (notification/show! "No page found" :warning)))
(defn ^:export show-page-ast [] (defn ^:export show-page-ast []
(let [page-data (db/pull '[:block/format {:block/file [:file/content]}] (let [page-data (db/pull '[:block/format {:block/file [:file/content]}]
(get-in (first (state/get-editor-args)) (page-util/get-current-page-id))]
[:block :block/page :db/id]))]
(if (get-in page-data [:block/file :file/content]) (if (get-in page-data [:block/file :file/content])
(show-content-ast (get-in page-data [:block/file :file/content]) (:block/format page-data)) (show-content-ast (get-in page-data [:block/file :file/content]) (:block/format page-data))
(notification/show! "No page found" :error)))) (notification/show! "No page found" :warning))))

View File

@ -31,6 +31,7 @@
[frontend.util.property :as property] [frontend.util.property :as property]
[frontend.util.fs :as fs-util] [frontend.util.fs :as fs-util]
[frontend.util.page-property :as page-property] [frontend.util.page-property :as page-property]
[frontend.util.page :as page-util]
[goog.object :as gobj] [goog.object :as gobj]
[lambdaisland.glogi :as log] [lambdaisland.glogi :as log]
[promesa.core :as p] [promesa.core :as p]
@ -60,15 +61,6 @@
;; Win10 file path has a length limit of 260 chars ;; Win10 file path has a length limit of 260 chars
(gp-util/safe-subs s 0 200))) (gp-util/safe-subs s 0 200)))
(defn get-page-file-path
([] (get-page-file-path (or (state/get-current-page)
(state/get-current-whiteboard))))
([page-name]
(when page-name
(let [page-name (util/page-name-sanity-lc page-name)]
(when-let [page (db/entity [:block/name page-name])]
(:file/path (:block/file page)))))))
(defn- build-title [page] (defn- build-title [page]
;; Don't wrap `\"` anymore, as tiitle property is not effected by `,` now ;; Don't wrap `\"` anymore, as tiitle property is not effected by `,` now
;; The previous extract behavior isn't unwrapping the `'"` either. So no need ;; The previous extract behavior isn't unwrapping the `'"` either. So no need
@ -870,13 +862,16 @@
:page))) :page)))
(defn open-file-in-default-app [] (defn open-file-in-default-app []
(when-let [file-path (and (util/electron?) (get-page-file-path))] (if-let [file-path (and (util/electron?) (page-util/get-page-file-path))]
(js/window.apis.openPath file-path))) (js/window.apis.openPath file-path)
(notification/show! "No file found" :warning)))
(defn copy-current-file [] (defn copy-current-file []
(when-let [file-path (and (util/electron?) (get-page-file-path))] (if-let [file-path (and (util/electron?) (page-util/get-page-file-path))]
(util/copy-to-clipboard! file-path))) (util/copy-to-clipboard! file-path)
(notification/show! "No file found" :warning)))
(defn open-file-in-directory [] (defn open-file-in-directory []
(when-let [file-path (and (util/electron?) (get-page-file-path))] (if-let [file-path (and (util/electron?) (page-util/get-page-file-path))]
(js/window.apis.showItemInFolder file-path))) (js/window.apis.showItemInFolder file-path)
(notification/show! "No file found" :warning)))

View File

@ -0,0 +1,24 @@
(ns frontend.util.page
"Provides util fns for page blocks"
(:require [frontend.state :as state]
[frontend.util :as util]
[frontend.db :as db]))
(defn get-current-page-id
"Fetches the current page id. Looks up page based on latest route and if
nothing is found, gets page of last edited block"
[]
(let [page-name (some-> (or (state/get-current-page) (state/get-current-whiteboard))
util/page-name-sanity-lc)]
(or (and page-name (:db/id (db/entity [:block/name page-name])))
(get-in (first (state/get-editor-args)) [:block :block/page :db/id]))))
(defn get-page-file-path
"Gets the file path of a page. If no page is given, detects the current page.
Returns nil if no file path is found or no page is detected or given"
([]
(when-let [page-id (get-current-page-id)]
(get-in (db/entity page-id) [:block/file :file/path])))
([page-name]
(when-let [page-name' (some-> page-name util/page-name-sanity-lc)]
(get-in (db/entity [:block/name page-name']) [:block/file :file/path]))))