Browse Source

Merge branch 'master' into feat/integration-plugins-core

charlie 4 years ago
parent
commit
6a1ebafdac

+ 1 - 1
package.json

@@ -75,7 +75,7 @@
         "ignore": "^5.1.8",
         "is-svg": "4.2.2",
         "jszip": "^3.5.0",
-        "mldoc": "0.7.2",
+        "mldoc": "0.7.4",
         "path": "^0.12.7",
         "posthog-js": "^1.10.2",
         "react": "^17.0.2",

+ 25 - 7
src/main/frontend/components/block.cljs

@@ -377,7 +377,7 @@
                                (:block/alias? config)
                                page
 
-                               (db/page-empty? (state/get-current-repo) (:db/id page-entity))
+                               (db/page-empty-or-dummy? (state/get-current-repo) (:db/id page-entity))
                                (let [source-page (model/get-alias-source-page (state/get-current-repo)
                                                                               (string/lower-case page-name))]
                                  (or (when source-page (:block/name source-page))
@@ -396,10 +396,14 @@
                                           :font-weight    500
                                           :max-height     600
                                           :padding-bottom 64}}
-                                 [:h2.font-bold.text-lg page-name]
-                                 (let [page (db/entity [:block/name (string/lower-case page-name)])]
+                                 [:h2.font-bold.text-lg (if (= page redirect-page-name)
+                                                          page
+                                                          [:span
+                                                           [:span.text-sm.mr-2 "Alias:" ]
+                                                           redirect-page-name])]
+                                 (let [page (db/entity [:block/name (string/lower-case redirect-page-name)])]
                                    (when-let [f (state/get-page-blocks-cp)]
-                                     (f (state/get-current-repo) page {:sidebar? sidebar? :preview? true})))]
+                                     (f (state/get-current-repo) redirect-page-name {:sidebar? sidebar? :preview? true})))]
                    :interactive true
                    :delay       1000}
                   inner)
@@ -1357,7 +1361,16 @@
         properties (apply dissoc properties property/built-in-properties)
         pre-block? (:block/pre-block? block)
         properties (if pre-block?
-                     (dissoc properties :title :filters)
+                     (let [repo (state/get-current-repo)
+                           properties (dissoc properties :title :filters)
+                           aliases (db/get-page-alias-names repo
+                                                            (:block/name (db/pull (:db/id (:block/page block)))))]
+                       (if (seq aliases)
+                         (if (:alias properties)
+                           (update properties :alias (fn [c]
+                                                       (distinct (concat c aliases))))
+                           (assoc properties :alias aliases))
+                         properties))
                      properties)
         properties (sort properties)]
     (cond
@@ -1372,7 +1385,8 @@
            (str (:block/uuid block) "-" k)))]
 
       (and pre-block? properties)
-      [:span.opacity-50 "Properties"]
+      (ui/tippy {:title "Click to edit page properties"}
+                [:span "🍵"])
 
       :else
       nil)))
@@ -1430,7 +1444,11 @@
         (let [block (or (db/pull [:block/uuid (:block/uuid block)]) block)
               f #(let [cursor-range (util/caret-range (gdom/getElement block-id))
                        content (property/remove-built-in-properties (:block/format block)
-                                                                 content)]
+                                                                    content)]
+                   ;; save current editing block
+                   (let [{:keys [value] :as state} (editor-handler/get-state)]
+                     (editor-handler/save-block! state value))
+
                    (state/set-editing!
                     edit-input-id
                     content

+ 1 - 7
src/main/frontend/components/export.cljs

@@ -27,9 +27,6 @@
         [:li.mb-4
          [:a.font-medium {:on-click #(export/export-repo-as-opml! current-repo)}
           (t :export-opml)]]
-        [:li.mb-4
-         [:a.font-medium {:on-click #(export/export-repo-as-edn! current-repo)}
-          (t :export-datascript-edn)]]
         [:li.mb-4
          [:a.font-medium {:on-click #(export/export-repo-as-edn-v2! current-repo)}
           (t :export-edn)]]
@@ -60,10 +57,7 @@
             (t :export-markdown)]]
           [:li.mb-4
            [:a.font-medium {:on-click #(export/export-page-as-opml! page)}
-            (t :export-opml)]]
-          [:li.mb-4
-           [:a.font-medium {:on-click #(export/convert-page-markdown-unordered-list-or-heading! page)}
-            (t :convert-markdown)]]]
+            (t :export-opml)]]]
          [:a#export-page-as-markdown.hidden]
          [:a#export-page-as-opml.hidden]
          [:a#convert-markdown-to-unordered-list-or-heading.hidden]]))))

+ 1 - 1
src/main/frontend/db.cljs

@@ -50,7 +50,7 @@
   get-page-referenced-blocks get-page-referenced-pages get-page-unlinked-references get-page-referenced-blocks-no-cache
   get-pages get-pages-relation get-pages-that-mentioned-page get-public-pages get-tag-pages
   journal-page? local-native-fs? mark-repo-as-cloned! page-alias-set page-blocks-transform pull-block
-  set-file-last-modified-at! transact-files-db! with-block-refs-count get-modified-pages page-empty? get-alias-source-page
+  set-file-last-modified-at! transact-files-db! with-block-refs-count get-modified-pages page-empty? page-empty-or-dummy? get-alias-source-page
   set-file-content! has-children?]
 
  [frontend.db.react

+ 20 - 3
src/main/frontend/db/model.cljs

@@ -357,6 +357,9 @@
              [?e2 :block/alias ?e1]]
             [(alias ?e2 ?e1)
              [?e1 :block/alias ?e2]]
+            [(alias ?e1 ?e3)
+             [?e1 :block/alias ?e2]
+             [?e2 :block/alias ?e3]]
             [(alias ?e3 ?e1)
              [?e1 :block/alias ?e2]
              [?e2 :block/alias ?e3]]])
@@ -386,9 +389,14 @@
   [repo page-name]
   (let [alias-ids (page-alias-set repo page-name)]
     (when (seq alias-ids)
-      (->> (get-page-names-by-ids repo alias-ids)
-           distinct
-           (remove #(= (string/lower-case %) (string/lower-case page-name)))))))
+      (let [names (->> (get-page-names-by-ids repo alias-ids)
+                       distinct
+                       (remove #(= (string/lower-case %) (string/lower-case page-name))))
+            lookup-refs (map (fn [name]
+                               [:block/name (string/lower-case name)]) names)]
+        (->> (db-utils/pull-many repo '[:block/name :block/original-name] lookup-refs)
+             (map (fn [m]
+                    (or (:block/original-name m) (:block/name m)))))))))
 
 (defn with-block-refs-count
   [repo blocks]
@@ -471,6 +479,15 @@
   [repo page-id]
   (zero? (get-page-blocks-count repo page-id)))
 
+(defn page-empty-or-dummy?
+  [repo page-id]
+  (or
+   (page-empty? repo page-id)
+   (when-let [db (conn/get-conn repo)]
+     (let [datoms (d/datoms db :avet :block/page page-id)]
+       (and (= (count datoms) 1)
+            (= "" (:block/content (db-utils/pull (:e (first datoms))))))))))
+
 (defn get-block-parent
   ([block-id]
    (get-block-parent (state/get-current-repo) block-id))

+ 5 - 3
src/main/frontend/debug.cljs

@@ -1,7 +1,9 @@
 (ns frontend.debug
-  (:require [cljs.pprint :as pprint]))
+  (:require [cljs.pprint :as pprint]
+            [frontend.state :as state]))
 
 (defn pprint
   [& xs]
-  (doseq [x xs]
-    (pprint/pprint x)))
+  (when (state/developer-mode?)
+    (doseq [x xs]
+      (pprint/pprint x))))

+ 1 - 1
src/main/frontend/format/adoc.cljs

@@ -29,5 +29,5 @@
      ok-handler))
   (exportMarkdown [this content config references]
     (throw "not support"))
-  (exportOPML [this content config title]
+  (exportOPML [this content config title references]
     (throw "not support")))

+ 2 - 2
src/main/frontend/format/block.cljs

@@ -410,7 +410,7 @@
 
 (defn- remove-indentations
   [format level element]
-  (if (or (= level 1) (= format :org))
+  (if (= format :org)
     element
     (case (first element)
       "Paragraph"
@@ -418,7 +418,7 @@
        (let [level (if (= (ffirst (second element)) "Plain")
                      (count (re-find #"^[\s\t]+" (second (first (second element)))))
                      level)]
-         (->> (partition-by #(= ["Break_Line"] %) (second element))
+         (->> (partition-by #(contains? #{["Break_Line"] ["Hard_Break_Line"]} %) (second element))
              (map (fn [c]
                     (if (and (= (ffirst c) "Plain")
                              (>= (count (re-find #"^[\s\t]+" (second (first c)))) level))

+ 7 - 4
src/main/frontend/format/mldoc.cljs

@@ -62,8 +62,11 @@
                           (or references default-references)))
 
 (defn parse-export-opml
-  [content config title]
-  (parseAndExportOPML content config title))
+  [content config title references]
+  (parseAndExportOPML content
+                      config
+                      title
+                      (or references default-references)))
 
 (defn ast-export-markdown
   [ast config references]
@@ -244,8 +247,8 @@
     true)
   (exportMarkdown [this content config references]
     (parse-export-markdown content config references))
-  (exportOPML [this content config title]
-    (parse-export-opml content config title)))
+  (exportOPML [this content config title references]
+    (parse-export-opml content config title references)))
 
 (defn plain->text
   [plains]

+ 1 - 1
src/main/frontend/format/protocol.cljs

@@ -6,4 +6,4 @@
   (loaded? [this])
   (lazyLoad [this ok-handler])
   (exportMarkdown [this content config references])
-  (exportOPML [this content config title]))
+  (exportOPML [this content config title references]))

+ 22 - 15
src/main/frontend/handler/editor.cljs

@@ -912,21 +912,12 @@
        (set-block-property! block-id "id" (str block-id))))
    (util/copy-to-clipboard! (tap-clipboard block-id))))
 
-(defn exit-editing-and-set-selected-blocks!
-  ([blocks]
-   (exit-editing-and-set-selected-blocks! blocks :down))
-  ([blocks direction]
-   (util/clear-selection!)
-   (state/clear-edit!)
-   (state/set-selection-blocks! blocks direction)
-   (util/select-highlight! blocks)))
-
 (defn select-block!
   [block-uuid]
   (when-let [block (-> (str block-uuid)
                        (js/document.getElementsByClassName)
                        first)]
-    (exit-editing-and-set-selected-blocks! [block])))
+    (state/exit-editing-and-set-selected-blocks! [block])))
 
 (defn select-all-blocks!
   []
@@ -934,7 +925,7 @@
     (let [input (gdom/getElement current-input-id)
           blocks-container (util/rec-get-blocks-container input)
           blocks (dom/by-class blocks-container "ls-block")]
-      (exit-editing-and-set-selected-blocks! blocks))))
+      (state/exit-editing-and-set-selected-blocks! blocks))))
 
 (defn- get-selected-blocks-with-children
   []
@@ -1186,7 +1177,7 @@
           blocks (if (= :up direction)
                    (reverse blocks)
                    blocks)]
-      (exit-editing-and-set-selected-blocks! blocks direction))))
+      (state/exit-editing-and-set-selected-blocks! blocks direction))))
 
 (defn on-select-block
   [direction]
@@ -1194,7 +1185,7 @@
     (cond
       ;; when editing, quit editing and select current block
       (state/editing?)
-      (exit-editing-and-set-selected-blocks! [(gdom/getElement (state/get-editing-block-dom-id))])
+      (state/exit-editing-and-set-selected-blocks! [(gdom/getElement (state/get-editing-block-dom-id))])
 
       ;; when selection and one block selected, select next block
       (and (state/selection?) (== 1 (count (state/get-selection-blocks))))
@@ -2112,7 +2103,7 @@
                    (f))]
     (when block
       (.scrollIntoView block #js {:behavior "smooth" :block "center"})
-      (exit-editing-and-set-selected-blocks! [block]))))
+      (state/exit-editing-and-set-selected-blocks! [block]))))
 
 (defn- select-up-down [direction]
   (let [selected (first (state/get-selection-blocks))
@@ -2123,7 +2114,7 @@
     (when (and sibling-block (dom/attr sibling-block "blockid"))
       (clear-selection! nil)
       (.scrollIntoView sibling-block #js {:behavior "smooth" :block "center"})
-      (exit-editing-and-set-selected-blocks! [sibling-block]))))
+      (state/exit-editing-and-set-selected-blocks! [sibling-block]))))
 
 (defn- move-cross-boundrary-up-down
   [direction]
@@ -2469,6 +2460,22 @@
           value (gobj/get input "value")
           c (util/nth-safe value (dec current-pos))]
       (when-not (state/get-editor-show-input)
+        (when (and (= k "【")
+                   (> current-pos 0)
+                   (= "【" (util/nth-safe value (dec (dec current-pos)))))
+          (commands/handle-step [:editor/input "[[]]" {:last-pattern "【【"
+                                                       :backward-pos 2}])
+          (commands/handle-step [:editor/search-page])
+          (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
+
+        (when (and (= k "(")
+                   (> current-pos 0)
+                   (= "(" (util/nth-safe value (dec (dec current-pos)))))
+          (commands/handle-step [:editor/input "(())" {:last-pattern "(("
+                                                       :backward-pos 2}])
+          (commands/handle-step [:editor/search-block :reference])
+          (reset! commands/*slash-caret-pos (util/get-caret-pos input)))
+
         (when (= c " ")
           (when (or (= (util/nth-safe value (dec (dec current-pos))) "#")
                     (not (state/get-editor-show-page-search?)))

+ 1 - 0
src/main/frontend/handler/events.cljs

@@ -76,6 +76,7 @@
         [:b (config/get-local-dir repo)]]
        (ui/button
         "Grant"
+        :class "ui__modal-enter"
         :on-click (fn []
                     (nfs/check-directory-permission! repo)
                     (close-fn)))])))

+ 17 - 20
src/main/frontend/handler/export.cljs

@@ -93,16 +93,6 @@
         (.setAttribute anchor "download" (str (last (string/split repo #"/")) ".json"))
         (.click anchor)))))
 
-(defn export-repo-as-edn!
-  [repo]
-  (when-let [db (db/get-conn repo)]
-    (let [db-edn (db/db->edn-str db)
-          data-str (str "data:text/edn;charset=utf-8," (js/encodeURIComponent db-edn))]
-      (when-let [anchor (gdom/getElement "download-as-edn")]
-        (.setAttribute anchor "href" data-str)
-        (.setAttribute anchor "download" (str (last (string/split repo #"/")) ".edn"))
-        (.click anchor)))))
-
 (defn download-file!
   [file-path]
   (when-let [content (get-file-content file-path)]
@@ -436,16 +426,23 @@
 
 (defn- export-files-as-opml
   [repo files]
-  (->> files
-       (mapv (fn [{:keys [path content names format]}]
-               (when (first names)
-                 (let [path
-                       (string/replace
-                        (string/lower-case path) #"(.+)\.(md|markdown|org)" "$1.opml")]
-                   [path (fp/exportOPML f/mldoc-record content
-                                        (f/get-default-config format)
-                                        (first names))]))))
-       (remove nil?)))
+  (let [get-page&block-refs-by-query-aux (get-embed-and-refs-blocks-pages-aux)
+        f (if (< (count files) 30)      ;query db for per page if (< (count files) 30), or pre-compute whole graph's page&block-refs
+            #(get-page&block-refs-by-query repo % get-page&block-refs-by-query-aux)
+            (let [page&block-refs (page&block-refs repo)]
+              #(get-page&block-refs repo % page&block-refs)))]
+    (->> files
+         (mapv (fn [{:keys [path content names format]}]
+                 (when (first names)
+                   (let [path
+                         (string/replace
+                          (string/lower-case path) #"(.+)\.(md|markdown|org)" "$1.opml")]
+                     [path (fp/exportOPML f/mldoc-record content
+                                          (f/get-default-config format)
+                                          (first names)
+                                          (js/JSON.stringify
+                                           (clj->js (f (first names)))))]))))
+         (remove nil?))))
 
 (defn- convert-md-files-unordered-list-or-heading
   [repo files heading-to-list?]

+ 14 - 6
src/main/frontend/handler/graph.cljs

@@ -56,7 +56,10 @@
     (or (util/uuid-string? id)
        (string/starts-with? id "../assets/")
        (= id "..")
-       (string/starts-with? id "assets/"))))
+       (string/starts-with? id "assets/")
+       (string/ends-with? id ".gif")
+       (string/ends-with? id ".jpg")
+       (string/ends-with? id ".png"))))
 
 (defn- remove-uuids-and-files!
   [nodes]
@@ -80,14 +83,19 @@
                              (get x :block/original-name (:block/name x))) names))
         nodes (mapv (fn [node] (assoc node :id (get names (:id node) (:id node)))) nodes)
         links (->>
+               links
+               (remove (fn [{:keys [source target]}]
+                         (or (nil? source) (nil? target))))
                (mapv (fn [{:keys [source target]}]
                        (when (and (not (uuid-or-asset? source))
                                   (not (uuid-or-asset? target)))
-                         {:source (get names source)
-                          :target (get names target)}))
-                     links)
-               (remove nil?))
-        nodes (remove-uuids-and-files! nodes)]
+                         {:source (get names (string/lower-case source))
+                          :target (get names (string/lower-case target))})))
+               (remove nil?)
+               (remove (fn [{:keys [source target]}]
+                         (or (nil? source) (nil? target)))))
+        nodes (->> (remove-uuids-and-files! nodes)
+                   (util/distinct-by #(string/lower-case (:id %))))]
     {:nodes nodes
      :links links}))
 

+ 1 - 10
src/main/frontend/handler/ui.cljs

@@ -78,22 +78,13 @@
       (let [elements (array-seq (js/document.getElementsByClassName id))]
         (when (first elements)
           (util/scroll-to-element (gobj/get (first elements) "id")))
-        (doseq [element elements]
-          (dom/add-class! element "block-highlight")
-          (js/setTimeout #(dom/remove-class! element "block-highlight")
-                         4000)))
+        (state/exit-editing-and-set-selected-blocks! elements))
       (when-let [element (gdom/getElement fragment)]
         (util/scroll-to-element fragment)
         (dom/add-class! element "block-highlight")
         (js/setTimeout #(dom/remove-class! element "block-highlight")
                        4000)))))
 
-(defn scroll-and-highlight!
-  [state]
-  (if-let [fragment (util/get-fragment)]
-    (highlight-element! fragment))
-  state)
-
 (defn add-style-if-exists!
   []
   (when-let [style (or

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

@@ -1036,6 +1036,10 @@
   (set-state! :ui/developer-mode? value)
   (storage/set "developer-mode" (str value)))
 
+(defn developer-mode?
+  []
+  (:ui/developer-mode? @state))
+
 (defn get-notification-contents
   []
   (get @state :notification/contents))
@@ -1310,3 +1314,12 @@
 (defn get-editor-cp
   []
   (get-in @state [:view/components :editor]))
+
+(defn exit-editing-and-set-selected-blocks!
+  ([blocks]
+   (exit-editing-and-set-selected-blocks! blocks :down))
+  ([blocks direction]
+   (util/clear-selection!)
+   (clear-edit!)
+   (set-selection-blocks! blocks direction)
+   (util/select-highlight! blocks)))

+ 13 - 5
src/main/frontend/ui.cljs

@@ -118,13 +118,13 @@
        (merge
         {:type  "button"
          :class (str (util/hiccup->class klass) " " class)}
-        (dissoc option :background))
+        (dissoc option :background :class))
        text]
       [:button.ui__button
        (merge
         {:type  "button"
          :class (str (util/hiccup->class klass) " " class)}
-        (dissoc option :background))
+        (dissoc option :background :class))
        text])))
 
 (rum/defc notification-content
@@ -413,9 +413,17 @@
      (mixins/hide-when-esc-or-outside
       state
       :on-hide (fn []
-                 (-> (.querySelector (rum/dom-node state) "button.ui__modal-close")
-                     (.click)))
-      :outside? false)))
+                 (some->
+                  (.querySelector (rum/dom-node state) "button.ui__modal-close")
+                  (.click)))
+      :outside? false)
+     (mixins/on-key-down
+      state
+      {;; enter
+       13 (fn [state e]
+            (some->
+             (.querySelector (rum/dom-node state) "button.ui__modal-enter")
+             (.click)))})))
   []
   (let [modal-panel-content (state/sub :modal/panel-content)
         fullscreen? (state/sub :modal/fullscreen?)

+ 0 - 15
src/main/frontend/util.cljc

@@ -359,21 +359,6 @@
    (defn stop [e]
      (when e (doto e (.preventDefault) (.stopPropagation)))))
 
-#?(:cljs
-   (defn get-fragment
-     []
-     (when-let [hash js/window.location.hash]
-       (when (> (count hash) 2)
-         (-> (subs hash 1)
-             (string/split #"\?")
-             (first))))))
-
-#?(:cljs
-   (defn fragment-with-anchor
-     [anchor]
-     (let [fragment (get-fragment)]
-       (str "#" fragment "?anchor=" anchor))))
-
 (def speed 500)
 (def moving-frequency 15)
 

+ 1 - 1
src/main/frontend/version.cljs

@@ -1,3 +1,3 @@
 (ns frontend.version)
 
-(defonce version "0.1.4-1")
+(defonce version "0.1.4-2")

+ 1 - 2
src/test/frontend/modules/outliner/core_test.cljs

@@ -270,12 +270,11 @@
   (let [start-node (build-block 6 2 3)
         end-node (build-block 11 9 10)
         block-ids [7 8 9 10]]
-    ;; FIXME: not sibling, nothing happens
     (outliner-core/delete-nodes start-node end-node block-ids)
     (let [children-of-2 (->> (build-block 2)
                           (tree/-get-children)
                           (mapv #(-> % :data :block/uuid)))]
-      (is (= [3 6 9] children-of-2)))))
+      (is (= [3] children-of-2)))))
 
 (comment
   (run-test test-delete-nodes))

+ 4 - 4
yarn.lock

@@ -3917,10 +3917,10 @@ mkdirp@^0.5.4, mkdirp@~0.5.1:
   dependencies:
     minimist "^1.2.5"
 
[email protected].1:
-  version "0.7.1"
-  resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.7.1.tgz#315f4f288083f141d5d0ecba7d28af8e8e4a5435"
-  integrity sha512-8eemlusips0cr+L7tYYFmjBo01A/Q1Al8kkXdnH6AnetajhnaSRje/i+kDBj4C4k7FRVdeTJJpiA+1EHqbLhJA==
[email protected].4:
+  version "0.7.4"
+  resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.7.4.tgz#a5cefe8222eaf70a9955c444bca92647c0943611"
+  integrity sha512-MSySomC15snUsQ7ex7XVs5W+LHWIF5fHuOySooygShFCm2Fd7F6lZK7KwcYTfOV+f98kMh0NUw7ijswKmqvkQQ==
   dependencies:
     yargs "^12.0.2"