file.cljs 4.6 KB

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