Browse Source

Merge branch 'master' into enhance/graph-parser-part-trois

Gabriel Horner 3 years ago
parent
commit
c541b096f4

+ 3 - 0
externs.js

@@ -127,6 +127,9 @@ dummy.getNodesObjects = function() {};
 dummy.getEdgesObjects = function() {};
 dummy.alphaTarget = function() {};
 dummy.restart = function() {};
+dummy.observe = function() {};
+dummy.contentRect = function() {};
+dummy.height = function() {};
 
 /**
  * @typedef {{

+ 5 - 0
resources/css/common.css

@@ -715,6 +715,11 @@ li p:last-child,
   background-color: var(--ls-primary-background-color, #fff);
 }
 
+.bg-base-4 {
+  background-color: var(--ls-tertiary-background-color);
+}
+
+
 .pre-white-space {
   white-space: pre;
 }

+ 41 - 38
src/main/frontend/components/block.cljs

@@ -266,9 +266,15 @@
         href (config/get-local-asset-absolute-path href)]
     (when (or granted? (util/electron?) (mobile-util/is-native-platform?))
       (p/then (editor-handler/make-asset-url href) #(reset! src %)))
-
+    
     (when @src
-      (let [ext (keyword (util/get-file-ext @src))]
+      (let [ext (keyword (util/get-file-ext @src))
+            share-fn (fn [event]
+                       (util/stop event)
+                       (when (mobile-util/is-native-platform?)
+                         (p/let [url (str (config/get-repo-dir (state/get-current-repo)) href)]
+                           (.share Share #js {:url url
+                                              :title "Open file with your favorite app"}))))]
         (cond
           (contains? config/audio-formats ext)
           (audio-cp @src)
@@ -277,19 +283,14 @@
           (resizable-image config title @src metadata full_text true)
 
           (= ext :pdf)
-          [:a.asset-ref.is-pdf
-           {:href @src
-            :on-click
-            (fn [event]
-              (util/stop event)
-              (when (mobile-util/is-native-platform?)
-                (p/let [url (str (config/get-repo-dir (state/get-current-repo)) href)]
-                  (.share Share #js {:url url
-                                     :title "Open PDF fils with your favorite app"}))))}
+          [:a.asset-ref.is-pdf {:href @src
+                                :on-click share-fn}
            title]
-          
+
           :else
-          [:a.asset-ref {:ref @src} title])))))
+          [:a.asset-ref.is-doc {:ref @src
+                                :on-click share-fn}
+           title])))))
 
 (defn ar-url->http-url
   [href]
@@ -660,7 +661,7 @@
   (and (= 1 (count label))
        (let [label (first label)]
          (string? (last label))
-         (last label))))
+         (js/decodeURIComponent (last label)))))
 
 (defn- get-page
   [label]
@@ -842,28 +843,31 @@
 
 (defn- media-link
   [config url s label metadata full_text]
-  (let [ext (util/get-file-ext s)]
+  (let [ext (keyword (util/get-file-ext s))
+        label-text (get-label-text label)]
     (cond
-      (contains? (set (map name config/audio-formats)) ext)
+      (contains? config/audio-formats ext)
       (audio-link config url s label metadata full_text)
 
-      (not (contains? #{"pdf" "mp4" "webm" "mov"} ext))
+      (contains? (config/doc-formats) ext)
+      (asset-link config label-text s metadata full_text)
+
+      (= ext :pdf)
+      (cond
+        (util/electron?)
+        [:a.asset-ref.is-pdf
+         {:href "javascript:void(0);"
+          :on-mouse-down (fn [_event]
+                           (when-let [current (pdf-assets/inflate-asset s)]
+                             (state/set-state! :pdf/current current)))}
+         label-text]
+
+        (mobile-util/is-native-platform?)
+        (asset-link config label-text s metadata full_text))
+
+      (not (contains? #{:mp4 :webm :mov} ext))
       (image-link config url s label metadata full_text)
 
-      (= (util/get-file-ext s) "pdf")
-      (let [label-text (get-label-text label)]
-        (cond
-          (util/electron?)
-          [:a.asset-ref.is-pdf
-           {:href "javascript:void(0);"
-            :on-mouse-down (fn [_event]
-                             (when-let [current (pdf-assets/inflate-asset s)]
-                               (state/set-state! :pdf/current current)))}
-           label-text]
-
-          (mobile-util/is-native-platform?)
-          (asset-link config label-text s metadata full_text)))
-      
       :else
       (asset-reference config label s))))
 
@@ -2403,14 +2407,13 @@
   [state config block]
   (let [repo (state/get-current-repo)
         ref? (:ref? config)
-        custom-query? (boolean (:custom-query? config))
-        ref-or-custom-query? (or ref? custom-query?)]
-    (if (and ref-or-custom-query? (not (:ref-query-child? config)))
+        custom-query? (boolean (:custom-query? config))]
+    (if (and ref? (not custom-query?) (not (:ref-query-child? config)))
       (ui/lazy-visible
-       nil
        (fn []
          (block-container-inner state repo config block))
-       nil)
+       nil
+       false)
       (block-container-inner state repo config block))))
 
 (defn divide-lists
@@ -2738,9 +2741,9 @@
   (ui/catch-error
    (ui/block-error "Query Error:" {:content (:query q)})
    (ui/lazy-visible
-    "loading ..."
     (fn [] (custom-query* config q))
-    nil)))
+    nil
+    true)))
 
 (defn admonition
   [config type result]

+ 13 - 0
src/main/frontend/components/block.css

@@ -240,6 +240,19 @@ html.is-native-android {
       align-items: center;
     }
   }
+
+  &.is-doc {
+      &:before {
+          content: "[[📜";
+          opacity: .7;
+          margin-right: 4px;
+      }
+
+      &:after {
+          content: "]]";
+          opacity: .7;
+      }
+  }
 }
 
 .embed-page {

+ 2 - 2
src/main/frontend/components/journal.cljs

@@ -61,12 +61,12 @@
      (page/today-queries repo today? false)
 
      (rum/with-key
-       (reference/references title false)
+       (reference/references title)
        (str title "-refs"))]))
 
 (rum/defc journal-cp
   [journal]
-  (ui/lazy-visible nil (fn [] (journal-cp-inner journal)) nil))
+  (ui/lazy-visible (fn [] (journal-cp-inner journal)) nil true))
 
 (rum/defc journals < rum/reactive
   [latest-journals]

+ 1 - 1
src/main/frontend/components/page.cljs

@@ -394,7 +394,7 @@
        ;; referenced blocks
        [:div {:key "page-references"}
         (rum/with-key
-          (reference/references route-page-name sidebar?)
+          (reference/references route-page-name)
           (str route-page-name "-refs"))]
 
        (when-not block?

+ 3 - 5
src/main/frontend/components/reference.cljs

@@ -165,16 +165,14 @@
               :title-trigger? true}))]]))))
 
 (rum/defc references
-  [page-name sidebar?]
+  [page-name]
   (ui/catch-error
    (ui/component-error "Linked References: Unexpected error")
    (ui/lazy-visible
-    (if (or sidebar? (util/uuid-string? page-name))
-      nil
-      "loading references...")
     (fn []
       (references* page-name))
-    nil)))
+    nil
+    false)))
 
 (rum/defcs unlinked-references-aux
   < rum/reactive db-mixins/query

+ 9 - 0
src/main/frontend/config.cljs

@@ -85,6 +85,15 @@
      config-formats
      #{:gif :svg :jpeg :ico :png :jpg :bmp :webp})))
 
+(defn doc-formats
+  []
+  (let [config-formats (some->> (get-in @state/state [:config :document-formats])
+                                (map :keyword)
+                                (set))]
+    (set/union
+     config-formats
+     #{:doc :docx :xls :xlsx :ppt :pptx :one :epub})))
+
 (def audio-formats #{:mp3 :ogg :mpeg :wav :m4a :flac :wma :aac})
 
 (def media-formats (set/union (img-formats) audio-formats))

+ 20 - 5
src/main/frontend/fs/sync.cljs

@@ -52,7 +52,12 @@
 ;;       and re-produce a new same-file-delete diff.
 
 ;;; ### specs
-(s/def ::state #{::idle
+(s/def ::state #{;; do following jobs when ::starting:
+                 ;; - wait seconds for file-change-events from file-watcher
+                 ;; - drop redundant file-change-events
+                 ;; - setup states in `frontend.state`
+                 ::starting
+                 ::idle
                  ;; sync local-changed files
                  ::local->remote
                  ;; sync remote latest-transactions
@@ -847,7 +852,7 @@
   "create a new sync-state"
   []
   {:post [(s/valid? ::sync-state %)]}
-  {:state ::idle
+  {:state ::starting
    :current-local->remote-files #{}
    :current-remote->local-files #{}
    :queued-local->remote-files #{}
@@ -1417,24 +1422,34 @@
       ;; 1. if remote graph has been deleted, clear graphs-txid.edn
       ;; 2. if graphs-txid.edn's content isn't [user-uuid graph-uuid txid], clear it
       (if (not= 3 (count @graphs-txid))
-        (clear-graphs-txid! repo)
+        (do (clear-graphs-txid! repo)
+            (state/set-file-sync-state nil))
         (when (check-graph-belong-to-current-user current-user-uuid user-uuid)
           (if-not (<! (check-remote-graph-exists graph-uuid))
             (clear-graphs-txid! repo)
             (do
               ;; set-env
               (set-env rsapi config/FILE-SYNC-PROD?)
-
+              (state/set-file-sync-state @*sync-state)
+              (state/set-file-sync-manager sm)
+              ;; wait seconds to receive all file change events,
+              ;; and then drop all of them.
+              ;; WHY: when opening a graph(or switching to another graph),
+              ;;      file-watcher will send a lot of file-change-events,
+              ;;      actually, each file corresponds to a file-change-event,
+              ;;      we need to ignore all of them.
+              (<! (timeout 5000))
               (drain-chan local-changes-chan)
               (poll! stop-sync-chan)
               (poll! remote->local-sync-chan)
+
               ;; update global state when *sync-state changes
               (add-watch *sync-state ::update-global-state
                          (fn [_ _ _ n]
                            (state/set-file-sync-state n)))
               (.start sm)
 
-              (state/set-file-sync-manager sm)
+
               (offer! remote->local-sync-chan true)
               (offer! full-sync-chan true)
 

+ 2 - 3
src/main/frontend/handler/events.cljs

@@ -59,8 +59,8 @@
 (defn- file-sync-stop-when-switch-graph []
   (p/do! (persist-var/load-vars)
          (sync/sync-stop)
-         ;; trigger rerender file-sync-header
-         (state/set-file-sync-state nil)))
+         (sync/sync-start)
+))
 
 (defn- graph-switch [graph]
   (state/set-current-repo! graph)
@@ -98,7 +98,6 @@
      (graph-switch graph))))
 
 (defmethod handle :graph/switch [[_ graph]]
-  (file-sync-stop-when-switch-graph)
   (if (outliner-file/writes-finished?)
     (graph-switch-on-persisted graph)
     (notification/show!

+ 2 - 1
src/main/frontend/mobile/intent.cljs

@@ -113,7 +113,8 @@
                     (gp-config/mldoc-support? application-type)
                     (embed-text-file url title)
 
-                    (contains? (set/union #{:pdf} config/media-formats) (keyword application-type))
+                    (contains? (set/union (config/doc-formats) config/media-formats)
+                               (keyword application-type))
                     (embed-asset-file url format)
 
                     :else

+ 1 - 0
src/main/frontend/state.cljs

@@ -1667,6 +1667,7 @@
 
 (defn get-file-sync-manager []
   (:file-sync/sync-manager @state))
+
 (defn get-file-sync-state []
   (:file-sync/sync-state @state))
 

+ 43 - 15
src/main/frontend/ui.cljs

@@ -911,23 +911,51 @@
      label-right]]
    (progress-bar width)])
 
-(rum/defc lazy-visible-inner
-  [visible? content-fn loading-label]
-  [:div.lazy-visibility
+(rum/defcs lazy-visible-inner <
+  {:init (fn [state]
+           (assoc state
+                  ::ref (atom nil)
+                  ::height (atom 26)))
+   :did-mount (fn [state]
+                (when (last (:rum/args state))
+                  (let [observer (js/ResizeObserver. (fn [entries]
+                                                      (let [entry (first entries)
+                                                            *height (::height state)
+                                                            height' (.-height (.-contentRect entry))]
+                                                        (when (and (> height' @*height)
+                                                                   (not= height' 64))
+                                                          (reset! *height height')))))]
+                   (.observe observer @(::ref state))))
+                state)}
+  [state visible? content-fn _reset-height?]
+  [:div.lazy-visibility {:ref #(reset! (::ref state) %)
+                         :style {:min-height @(::height state)}}
+
    (if visible?
      (when (fn? content-fn) (content-fn))
-     (when loading-label [:span.text-sm.font-medium
-                          loading-label]))])
+     [:div.shadow.rounded-md.p-4.w-full.mx-auto {:style {:height 64}}
+      [:div.animate-pulse.flex.space-x-4
+       [:div.flex-1.space-y-3.py-1
+        [:div.h-2.bg-base-4.rounded]
+        [:div.space-y-3
+         [:div.grid.grid-cols-3.gap-4
+          [:div.h-2.bg-base-4.rounded.col-span-2]
+          [:div.h-2.bg-base-4.rounded.col-span-1]]
+         [:div.h-2.bg-base-4.rounded]]]]])])
 
 (rum/defcs lazy-visible <
   (rum/local false ::visible?)
-  [state loading-label content-fn sensor-opts]
-  (let [*visible? (::visible? state)]
-    (visibility-sensor
-     (merge
-      {:on-change #(reset! *visible? %)
-       :partialVisibility true
-       :offset {:top -300
-                :bottom -300}}
-      sensor-opts)
-     (lazy-visible-inner @*visible? content-fn loading-label))))
+  [state content-fn sensor-opts reset-height?]
+  (if (or (util/mobile?) (mobile-util/is-native-platform?))
+    (content-fn)
+    (let [*visible? (::visible? state)]
+      (visibility-sensor
+       (merge
+        {:on-change #(reset! *visible? %)
+         :partialVisibility true
+         :offset {:top -300
+                  :bottom -300}
+         :scrollCheck true
+         :scrollThrottle 1}
+        sensor-opts)
+       (lazy-visible-inner @*visible? content-fn reset-height?)))))