瀏覽代碼

feat(electron): support upload any asset types

charlie 4 年之前
父節點
當前提交
32c2d4f268

+ 18 - 0
gulpfile.js

@@ -89,6 +89,24 @@ exports.electron = () => {
   })
 }
 
+exports.electronMaker = () => {
+  cp.execSync('yarn release', {
+    stdio: 'inherit'
+  })
+
+  if (!fs.existsSync(path.join(outputPath, 'node_modules'))) {
+    cp.execSync('yarn', {
+      cwd: outputPath,
+      stdio: 'inherit'
+    })
+  }
+
+  cp.execSync('yarn electron:make', {
+    cwd: outputPath,
+    stdio: 'inherit'
+  })
+}
+
 exports.clean = common.clean
 exports.watch = gulp.parallel(common.keepSyncResourceFile, css.watchCSS)
 exports.build = gulp.series(common.clean, common.syncResourceFile, css.buildCSS)

+ 1 - 0
package.json

@@ -33,6 +33,7 @@
         "release-publishing": "run-s gulp:build cljs:release-publishing",
         "dev-release-app": "run-s gulp:build cljs:dev-release-app",
         "dev-electron-app": "gulp electron",
+        "release-electron-app": "gulp electronMaker",
         "debug-electron-app": "cd static/ && yarn electron:debug",
         "clean": "gulp clean",
         "test": "run-s cljs:test cljs:run-test",

+ 2 - 0
src/electron/electron/core.cljs

@@ -27,6 +27,7 @@
                   {:nodeIntegration         false
                    :nodeIntegrationInWorker false
                    :contextIsolation        true
+                   :spellcheck              true
                    :preload                 (path/join js/__dirname "js/preload.js")}}
         url MAIN_WINDOW_ENTRY
         win (BrowserWindow. (clj->js win-opts))]
@@ -73,6 +74,7 @@
                      (js/console.error e))))))
     (.. win -webContents (on "new-window"
                              (fn [e url]
+                               (.. log (info "new-window" url))
                                (open url)
                                (.preventDefault e))))
     #(do (.removeHandler ipcMain toggle-win-channel)

+ 7 - 1
src/main/frontend/commands.cljs

@@ -3,6 +3,7 @@
             [frontend.date :as date]
             [frontend.state :as state]
             [frontend.search :as search]
+            [frontend.config :as config]
             [clojure.string :as string]
             [goog.dom :as gdom]
             [goog.object :as gobj]
@@ -123,7 +124,12 @@
                   [:editor/search-template]]]
      ;; same as link
      ["Image Link" link-steps]
-     (when (state/logged?)
+     (cond
+       (and (util/electron?) (config/local-db? (state/get-current-repo)))
+
+       ["Upload an asset" [[:editor/click-hidden-file-input :id]]]
+
+       (state/logged?)
        ["Upload an image" [[:editor/click-hidden-file-input :id]]])
      ["Embed Youtube Video" [[:editor/input "{{youtube }}" {:last-pattern slash
                                                             :backward-pos 2}]]]

+ 10 - 0
src/main/frontend/components/block.cljs

@@ -376,6 +376,12 @@
            label
            original-page-name))])))
 
+(rum/defc asset-reference
+  [title path]
+  (let [repo-path (config/get-repo-dir (state/get-current-repo))
+        full-path (str repo-path (string/replace path "../" "/"))]
+    [:a.asset-ref {:target "_blank" :href full-path} (or title path)]))
+
 (rum/defc page-reference < rum/reactive
   [html-export? s config label]
   (let [show-brackets? (state/show-brackets?)
@@ -622,6 +628,7 @@
 
           (= \# (first s))
           (->elem :a {:href (str "#" (mldoc/anchorLink (subs s 1)))} (map-inline config label))
+
           ;; FIXME: same headline, see more https://orgmode.org/manual/Internal-Links.html
           (and (= \* (first s))
                (not= \* (last s)))
@@ -631,6 +638,9 @@
           (->elem :a {:href s}
                   (map-inline config label))
 
+          (and (util/electron?) (config/local-asset? s))
+          (asset-reference (second (first label)) s)
+
           :else
           (page-reference html-export? s config label))
 

+ 2 - 1
src/main/frontend/handler/editor.cljs

@@ -1610,7 +1610,8 @@
              (when-let [[url file] (and (seq res) (first res))]
                (insert-command!
                 id
-                (get-image-link format (get-asset-link url) (if file (.-name file) "image"))
+                (get-image-link format (get-asset-link url)
+                                (if file (.-name file) (if (util/ext-of-image? url) "image" "asset")))
                 format
                 {:last-pattern (if drop-or-paste? "" commands/slash)
                  :restore?     true}))))

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

@@ -155,6 +155,10 @@
             col)
        (into {})))
 
+(defn ext-of-image? [s]
+  (some #(string/ends-with? s %)
+        [".png" ".jpg" ".jpeg" ".bmp" ".gif" ".webp"]))
+
 ;; ".lg:absolute.lg:inset-y-0.lg:right-0.lg:w-1/2"
 (defn hiccup->class
   [class]