file.cljs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. (ns frontend.components.file
  2. (:require [cljs-time.coerce :as tc]
  3. [cljs-time.core :as t]
  4. [clojure.string :as string]
  5. [datascript.core :as d]
  6. [frontend.components.lazy-editor :as lazy-editor]
  7. [frontend.components.svg :as svg]
  8. [frontend.context.i18n :refer [t]]
  9. [frontend.date :as date]
  10. [frontend.db :as db]
  11. [frontend.handler.export :as export-handler]
  12. [frontend.state :as state]
  13. [frontend.util :as util]
  14. [logseq.graph-parser.config :as gp-config]
  15. [logseq.graph-parser.util :as gp-util]
  16. [goog.object :as gobj]
  17. [reitit.frontend.easy :as rfe]
  18. [rum.core :as rum]))
  19. (defn- get-path
  20. [state]
  21. (let [route-match (first (:rum/args state))]
  22. (get-in route-match [:parameters :path :path])))
  23. (rum/defc files-all < rum/reactive
  24. []
  25. (when-let [current-repo (state/sub :git/current-repo)]
  26. (let [files (db/get-files current-repo)
  27. mobile? (util/mobile?)]
  28. [:table.table-auto
  29. [:thead
  30. [:tr
  31. [:th (t :file/name)]
  32. (when-not mobile?
  33. [:th (t :file/last-modified-at)])
  34. (when-not mobile?
  35. [:th ""])]]
  36. [:tbody
  37. (for [[file modified-at] files]
  38. (let [file-id file]
  39. [:tr {:key file-id}
  40. [:td
  41. (let [href (if (gp-config/draw? file)
  42. (rfe/href :draw nil {:file (string/replace file (str gp-config/default-draw-directory "/") "")})
  43. (rfe/href :file {:path file-id}))]
  44. [:a {:href href}
  45. file])]
  46. (when-not mobile?
  47. [:td [:span.text-gray-500.text-sm
  48. (if (zero? modified-at)
  49. (t :file/no-data)
  50. (date/get-date-time-string
  51. (t/to-default-time-zone (tc/to-date-time modified-at))))]])
  52. (when-not mobile?
  53. [:td [:a.text-sm
  54. {:on-click (fn [_e]
  55. (export-handler/download-file! file))}
  56. [:span (t :download)]]])]))]])))
  57. (rum/defc files
  58. []
  59. [:div.flex-1.overflow-hidden
  60. [:h1.title
  61. (t :all-files)]
  62. (files-all)
  63. ])
  64. (rum/defcs file < rum/reactive
  65. {:did-mount (fn [state]
  66. (state/set-file-component! (:rum/react-component state))
  67. state)
  68. :will-unmount (fn [state]
  69. (state/clear-file-component!)
  70. state)}
  71. [state]
  72. (let [path (get-path state)
  73. format (gp-util/get-format path)
  74. original-name (db/get-file-page path)
  75. random-id (str (d/squuid))]
  76. [:div.file {:id (str "file-edit-wrapper-" random-id)
  77. :key path}
  78. [:h1.title
  79. [:bdi (js/decodeURI path)]]
  80. (when original-name
  81. [:div.text-sm.mb-4.ml-1 "Page: "
  82. [:a.bg-base-2.p-1.ml-1 {:style {:border-radius 4}
  83. :href (rfe/href :page {:name original-name})
  84. :on-click (fn [e]
  85. (when (gobj/get e "shiftKey")
  86. (when-let [page (db/entity [:block/name (util/page-name-sanity-lc original-name)])]
  87. (state/sidebar-add-block!
  88. (state/get-current-repo)
  89. (:db/id page)
  90. :page))
  91. (util/stop e)))}
  92. original-name]])
  93. (when (and original-name (not (string/starts-with? original-name "logseq/")))
  94. [:p.text-sm.ml-1.mb-4
  95. (svg/warning {:style {:width "1em"
  96. :display "inline-block"}})
  97. [:span.ml-1 "Please don't remove the page's title property (you can still modify it)."]])
  98. (cond
  99. ;; image type
  100. (and format (contains? (gp-config/img-formats) format))
  101. [:img {:src (util/node-path.join "file://" path)}]
  102. (and format (contains? (gp-config/text-formats) format))
  103. (when-let [file-content (or (db/get-file path) "")]
  104. (let [content (string/trim file-content)
  105. mode (util/get-file-ext path)]
  106. (lazy-editor/editor {:file? true
  107. :file-path path}
  108. (str "file-edit-" random-id)
  109. {:data-lang mode}
  110. content
  111. {})))
  112. :else
  113. [:div (t :file/format-not-supported (name format))])]))