file.cljs 5.2 KB

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