From 66a545c30692b994d19fa63d810cec5670896065 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 27 Jan 2021 21:38:18 +0800 Subject: [PATCH] feat(electron): fix windows path --- src/main/frontend/handler/file.cljs | 10 ++++++++-- src/main/frontend/utils.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/frontend/handler/file.cljs b/src/main/frontend/handler/file.cljs index 6e5e0111e6..a2dc0b1236 100644 --- a/src/main/frontend/handler/file.cljs +++ b/src/main/frontend/handler/file.cljs @@ -132,11 +132,17 @@ [repo-url file content] (let [electron-local-repo? (and (util/electron?) (config/local-db? repo-url)) + ;; FIXME: store relative path in db file (cond - (and electron-local-repo? (util/win32?)) + (and electron-local-repo? + util/win32? + (utils/win32 file)) file - electron-local-repo? + (and electron-local-repo? util/win32?) + (str (config/get-repo-dir repo-url) "/" file) + + (and electron-local-repo? (not= "/" (first file))) (str (config/get-repo-dir repo-url) "/" file) :else diff --git a/src/main/frontend/utils.js b/src/main/frontend/utils.js index 96ddb492ed..7580debd8f 100644 --- a/src/main/frontend/utils.js +++ b/src/main/frontend/utils.js @@ -192,3 +192,15 @@ export const reversePatch = patch => { length2: patchObj.length1 })); }; + +// Copied from https://github.com/sindresorhus/path-is-absolute/blob/main/index.js +export const win32 = path => { + // https://github.com/nodejs/node/blob/b3fcc245fb25539909ef1d5eaa01dbf92e168633/lib/path.js#L56 + var splitDeviceRe = /^([a-zA-Z]:|[\\/]{2}[^\\/]+[\\/]+[^\\/]+)?([\\/])?([\s\S]*?)$/; + var result = splitDeviceRe.exec(path); + var device = result[1] || ''; + var isUnc = Boolean(device && device.charAt(1) !== ':'); + + // UNC paths are always absolute + return Boolean(result[2] || isUnc); +}