An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve startup prompt after workspace loss https://github.com/siyuan-note/siyuan/issues/14748
Daniel71676eda82b4
diff --git a/app/electron/main.js b/app/electron/main.jsindex ec4f98bfe9..e553e5ec55 100644--- a/app/electron/main.js+++ b/app/electron/main.js@@ -111,6 +111,40 @@ try {app.exit();}+// 解析命令行参数,参数需以 `name=value` 形式传入 https://github.com/siyuan-note/siyuan/issues/14748+const getArg = (name) => {+ for (let i = 0; i < process.argv.length; i++) {+ if (process.argv[i].startsWith(name)) {+ return process.argv[i].split("=")[1];+ }+ }+};++// 检测上次打开的工作空间是否丢失 https://github.com/siyuan-note/siyuan/issues/14748+let lastWorkspaceMissing = false;+let missingWorkspacePath = "";+let availableWorkspaces = [];+if (!firstOpen && !getArg("--workspace")) {+ // 显式通过命令行指定工作空间时尊重用户参数,跳过检测+ try {+ const wsFile = path.join(confDir, "workspace.json");+ if (fs.existsSync(wsFile)) {+ const wsList = JSON.parse(fs.readFileSync(wsFile, "utf8"));+ if (Array.isArray(wsList) && 0 < wsList.length) {+ const last = wsList[wsList.length - 1];+ if (!fs.existsSync(last) || !fs.statSync(last).isDirectory()) {+ lastWorkspaceMissing = true;+ missingWorkspacePath = last;+ availableWorkspaces = wsList.slice(0, -1).filter(p =>+ fs.existsSync(p) && fs.statSync(p).isDirectory());+ }+ }+ }+ } catch (e) {+ writeLog("check missing workspace failed: " + e);+ }+}+const windowNavigate = (currentWindow, windowType) => {currentWindow.webContents.on("will-navigate", (event) => {const url = event.url;@@ -1391,15 +1425,49 @@ app.whenReady().then(() => {});firstOpenWindow.destroy();});- } else {- const getArg = (name) => {- for (let i = 0; i < process.argv.length; i++) {- if (process.argv[i].startsWith(name)) {- return process.argv[i].split("=")[1];- }- }- };+ } else if (lastWorkspaceMissing) {+ // 上次使用的工作空间丢失,弹出选择工作空间窗口 https://github.com/siyuan-note/siyuan/issues/14748+ const missingWorkspaceWindow = new BrowserWindow({+ width: Math.floor(screen.getPrimaryDisplay().size.width * 0.55),+ height: Math.floor(screen.getPrimaryDisplay().workAreaSize.height * 0.65),+ frame: "darwin" === process.platform,+ titleBarStyle: "hidden",+ fullscreenable: false,+ icon: path.join(appDir, "stage", "icon-large.png"),+ transparent: "darwin" === process.platform,+ webPreferences: {+ nodeIntegration: true, webviewTag: true, webSecurity: false, contextIsolation: false,+ },+ });+ let missingWorkspaceHTMLPath = path.join(appDir, "app", "electron", "missing-workspace.html");+ if (isDevEnv) {+ missingWorkspaceHTMLPath = path.join(appDir, "electron", "missing-workspace.html");+ }+ // 改进桌面端初始化时使用的外观语言 https://github.com/siyuan-note/siyuan/issues/6803+ const languages = app.getPreferredSystemLanguages();+ const language = resolveAppLanguage(languages);+ missingWorkspaceWindow.loadFile(missingWorkspaceHTMLPath, {+ query: {+ lang: language,+ home: app.getPath("home"),+ v: appVer,+ icon: path.join(appDir, "stage", "icon-large.png"),+ missing: missingWorkspacePath,+ workspaces: availableWorkspaces.join("\n"),+ },+ });+ missingWorkspaceWindow.show();+ // 选择工作空间后启动内核+ ipcMain.on("siyuan-select-workspace", (event, data) => {+ initKernel(data.workspace, "", data.lang).then((isSucc) => {+ if (isSucc) {+ initMainWindow();+ }+ });+ missingWorkspaceWindow.destroy();+ });+ } else {const workspace = getArg("--workspace");if (workspace) {writeLog("got arg [--workspace=" + workspace + "]");diff --git a/app/electron/missing-workspace.html b/app/electron/missing-workspace.htmlnew file mode 100644index 0000000000..81db29bb5b--- /dev/null+++ b/app/electron/missing-workspace.html@@ -0,0 +1,876 @@+<!DOCTYPE html>+<html>+<head>+ <title>SiYuan</title>+ <meta charset="UTF-8">+ <meta name="color-scheme" content="light dark">+ <style>+ body {+ margin: 0;+ background-color: #fff;+ text-align: center;+ color: #222;+ font-size: 16px;+ font-family: BlinkMacSystemFont, Helvetica, "PingFang SC", "Luxi Sans", "DejaVu Sans", arial, "Microsoft Yahei", "Hiragino Sans GB", "Source Han Sans SC", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", "Segoe UI Symbol", "Twemoji Mozilla", "Android Emoji", "EmojiSymbols";+ }++ .layout {+ display: flex;+ min-height: 100vh;+ }++ .wsPanel {+ flex: none;+ width: 300px;+ border-right: 1px solid #e0e0e0;+ padding: 24px;+ overflow-y: auto;+ text-align: left;+ box-sizing: border-box;+ background-color: #f6f6f6;+ }++ .wsTitle {+ font-size: 16px;+ font-weight: bold;+ color: rgba(0, 0, 0, .6);+ margin-bottom: 16px;+ }++ .ws-item {+ padding: 12px;+ display: flex;+ align-items: center;+ gap: 12px;+ border-radius: 6px;+ cursor: default;+ }++ .ws-item:hover {+ background: #dfe0e1;+ }++ .ws-text {+ flex: 1;+ min-width: 0;+ }++ .ws-name {+ font-size: 16px;+ color: #222;+ line-height: 1.5;+ }++ .ws-path {+ font-size: 12px;+ color: rgba(0, 0, 0, .6);+ word-break: break-all;+ line-height: 1.5;+ }++ .fn__flex-column {+ display: flex;+ flex-direction: column;+ width: 100%;+ margin: 0 auto;+ min-height: 100vh;+ box-sizing: border-box;+ padding: 0 calc((100vw - 608px) * 0.3);+ }++ .right {+ flex: 1;+ padding: 0 32px;+ }++ .fn__flex-1 {+ flex: 1;+ min-height: 32px;+ flex-shrink: 0;+ min-width: 8px;+ }++ h2 {+ margin: 0 0 16px 0;+ font-size: 28px;+ }++ .b3-select {+ border-radius: 6px;+ color: #222;+ height: 28px;+ width: 96px;+ border: 0;+ padding: 4px 26px 4px 8px;+ box-sizing: border-box;+ line-height: 20px;+ transition: box-shadow 120ms 0ms cubic-bezier(0, 0, 0.2, 1), background-color 0.2s cubic-bezier(0, 0, 0.2, 1) 0ms;+ font-size: 14px;+ box-shadow: inset 0 0 0 0.6px rgba(0, 0, 0, .6);+ background: url("data:image/svg+xml;utf8,<svg fill='rgba(0, 0, 0, .4)' height='24' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg'><path d='M7 10l5 5 5-5z'/><path d='M0 0h24v24H0z' fill='none'/></svg>") no-repeat right 2px center #fff;+ appearance: none;+ cursor: pointer;+ overflow: hidden;+ flex-shrink: 0;+ }++ .b3-select:focus {+ box-shadow: inset 0 0 0 1px #3575f0, 0 0 0 3px rgba(53, 117, 240, .12);+ }++ .b3-button {+ cursor: pointer;+ color: #fff;+ border-radius: 6px;+ line-height: 20px;+ padding: 4px 8px;+ background-color: #3575f0;+ white-space: nowrap;+ display: inline-flex;+ align-items: center;+ justify-content: center;+ transition: box-shadow 280ms ease;+ border: 0;+ box-sizing: border-box;+ text-align: center;+ width: 96px;+ flex-shrink: 0;+ }++ .b3-button:hover, .b3-button:focus {+ text-decoration: none;+ box-shadow: 0 1px 2px 0 rgb(0 0 0 / .3), 0 1px 3px 1px rgb(0 0 0 /.15);+ }++ .b3-button:active {+ box-shadow: 0 2px 3px 1px rgb(0 0 0 / .3), 0 2px 4px 2px rgb(0 0 0 /.15);+ }++ .ws-open {+ width: auto;+ padding: 2px 12px;+ color: rgba(0, 0, 0, .6);+ background-color: transparent;+ box-shadow: inset 0 0 0 0.6px rgba(0, 0, 0, .25);+ opacity: 0;+ pointer-events: none;+ transition: opacity 120ms ease;+ }++ .ws-item:hover .ws-open {+ opacity: 1;+ pointer-events: auto;+ }++ .ws-open:hover, .ws-open:focus {+ color: #222;+ background-color: rgba(0, 0, 0, .06);+ box-shadow: inset 0 0 0 0.6px rgba(0, 0, 0, .4);+ }++ .ws-open:active {+ box-shadow: inset 0 0 0 0.6px rgba(0, 0, 0, .4);+ }++ .b3-label {+ padding: 16px 24px 0;+ margin-bottom: 16px;+ display: flex;+ text-align: left;+ box-shadow: 0 15px 0 0 #fff, 0 16px 0 0 #e0e0e0;+ }++ .b3-label__text {+ color: rgba(0, 0, 0, .6);+ font-size: 14px;+ margin-top: 4px;+ overflow: hidden;+ line-height: 1.65;+ }++ .fn__flex-center {+ align-self: center;+ }++ .svg {+ position: fixed;+ right: 32px;+ top: 0;+ width: 13px;+ fill: rgba(0, 0, 0, .6);+ padding: 9.5px;+ cursor: pointer;+ z-index: 1;+ }++ .svg:hover {+ background: #dfe0e1;+ fill: #222;+ }++ #close {+ right: 0;+ width: 10px;+ padding: 11px 11px;+ }++ #close:hover {+ background-color: #d23f31;+ fill: #fff;+ }++ a {+ text-decoration: none;+ color: #3575f0;+ }++ a:hover {+ text-decoration: underline;+ }++ .fn__none {+ display: none !important;+ }++ .slogan {+ color: rgba(0, 0, 0, .6);+ font-size: 15px;+ }++ .notice {+ font-size: 14px;+ color: rgba(0, 0, 0, .6);+ text-align: center;+ }++ .missing-tip {+ font-size: 18px;+ color: #222;+ text-align: left;+ padding: 0 24px;+ line-height: 1.6;+ }++ .missing-path {+ font-size: 14px;+ color: rgba(0, 0, 0, .6);+ margin-top: 8px;+ word-break: break-all;+ }++ code {+ word-break: break-all;+ }++ kbd {+ font: 75% SFMono-Regular, Consolas, "Liberation Mono";+ line-height: 1;+ border-radius: 6px;+ padding: 2px 4px;+ color: rgba(0, 0, 0, .6);+ vertical-align: middle;+ background-color: #f6f6f6;+ border: solid 1px #e0e0e0;+ box-shadow: inset 0 -1px 0 #e0e0e0;+ }++ .icon {+ width: 96px;+ margin: 0 auto 16px;+ }++ .feedback {+ display: flex;+ justify-content: space-around;+ font-size: 14px+ }++ .drag {+ -webkit-app-region: drag;+ height: 32px;+ cursor: pointer;+ position: fixed;+ top: 0;+ left: 0;+ right: 64px;+ }++ .darwin #min,+ .darwin #close {+ display: none;+ }++ .darwin .drag {+ right: 0;+ }++ @media (prefers-color-scheme: dark) {+ body {+ background-color: #1e1e1e;+ color: #dadada;+ }++ .wsPanel {+ background-color: #2c2c2c;+ border-right: 1px solid #363636;+ }++ .wsTitle {+ color: #9aa0a6;+ }++ .ws-item:hover {+ background: rgba(255, 255, 255, .075);+ }++ .ws-name {+ color: #dadada;+ }++ .ws-path {+ color: #9aa0a6;+ }++ .b3-button {+ background-color: #3573f0;+ }++ .b3-button:hover, .b3-button:focus {+ box-shadow: 0 1px 2px 0 rgb(0 0 0 / .3), 0 1px 3px 1px rgb(255 255 255 /.15);+ }++ .b3-button:active {+ box-shadow: 0 2px 3px 1px rgb(0 0 0 / .3), 0 2px 4px 2px rgb(255 255 255 /.15);+ }++ .ws-open {+ color: #9aa0a6;+ background-color: transparent;+ box-shadow: inset 0 0 0 0.6px rgba(255, 255, 255, .25);+ }++ .ws-open:hover, .ws-open:focus {+ color: #dadada;+ background-color: rgba(255, 255, 255, .075);+ box-shadow: inset 0 0 0 0.6px rgba(255, 255, 255, .4);+ }++ .ws-open:active {+ box-shadow: inset 0 0 0 0.6px rgba(255, 255, 255, .4);+ }++ .b3-select {+ color: #dadada;+ box-shadow: inset 0 0 0 0.6px #9aa0a6;+ background: url("data:image/svg+xml;utf8,<svg fill='rgba(154, 160, 166, .68)' height='24' viewBox='0 0 24 24' width='24' xmlns='http://www.w3.org/2000/svg'><path d='M7 10l5 5 5-5z'/><path d='M0 0h24v24H0z' fill='none'/></svg>") no-repeat right 2px center #1e1e1e;+ }++ .b3-select:focus {+ box-shadow: inset 0 0 0 1px #3573f0, 0 0 0 3px rgba(53, 115, 240, .24);+ }++ .b3-label {+ box-shadow: 0 15px 0 0 #1e1e1e, 0 16px 0 0 #363636;+ }++ .b3-label__text {+ color: #9aa0a6;+ }++ .svg {+ fill: #9aa0a6;+ }++ .svg:hover {+ background: rgba(255, 255, 255, .075);+ fill: #dadada;+ }++ a {+ color: #3573f0;+ }++ .slogan {+ color: #9aa0a6;+ }++ .notice {+ color: #9aa0a6;+ }++ .missing-tip {+ color: #dadada;+ }++ .missing-path {+ color: #9aa0a6;+ }++ kbd {+ color: #9aa0a6;+ background-color: #262626;+ border: solid 1px rgba(230, 230, 230, .06);+ box-shadow: inset 0 -1px 0 rgba(230, 230, 230, .06);+ }+ }+ </style>+</head>+<body>+<div id="zhCN" class="layout">+ <div class="wsPanel">+ <div class="wsTitle">工作空间</div>+ <div class="wsList"></div>+ </div>+ <div class="fn__flex-column right">+ <div class="fn__flex-1"></div>+ <img class="icon">+ <h2>思源笔记</h2>+ <div class="fn__flex-1"></div>+ <div class="missing-tip">+ <div>⚠️ 找不到上次打开的工作空间路径</div>+ <div class="missing-path"><code class="missing"></code></div>+ </div>+ <div class="fn__flex-1"></div>+ <div class="notice fn__none emptyHint">+ <span>当前没有可用的工作空间,请选择新的工作空间路径</span>+ </div>+ <label class="b3-label">+ <div>+ 🗂️ 选择新的工作空间路径+ <div class="b3-label__text">工作空间用于存放数据,后续可以在顶栏左上角的主菜单中进行切换</div>+ </div>+ <div class="fn__flex-1"></div>+ <button class="b3-button fn__flex-center choosePath">选择</button>+ </label>+ <label class="b3-label">+ <div>+ 🌐 外观语言+ <div class="b3-label__text">用户界面语言,后续可以在 <kbd>设置</kbd> - <kbd>外观</kbd> 中进行切换</div>+ </div>+ <div class="fn__flex-1"></div>+ <select class="b3-select lang">+ <option value="ar">العربية</option>+ <option value="de">Deutsch</option>+ <option value="en">English</option>+ <option value="es">Español</option>+ <option value="fr">Français</option>+ <option value="he">עברית</option>+ <option value="hi">हिन्दी</option>+ <option value="id">Bahasa Indonesia</option>+ <option value="it">Italiano</option>+ <option value="ja">日本語</option>+ <option value="ko">한국어</option>+ <option value="nl">Nederlands</option>+ <option value="pl">Polski</option>+ <option value="pt-BR">Português (Brasil)</option>+ <option value="ru">Русский</option>+ <option value="sk">Slovenčina</option>+ <option value="th">ไทย</option>+ <option value="tr">Türkçe</option>+ <option value="uk">Українська</option>+ <option value="zh-TW">繁體中文</option>+ <option value="zh-CN">简体中文</option>+ </select>+ </label>+ <div class="fn__flex-1"></div>+ <div class="feedback">+ <span>❤️ <a href="https://ld246.com/article/1649901726096" target="_blank">求助反馈建议</a></span>+ <span>🏘️ <a href="https://ld246.com/article/1640266171309" target="_blank">用户社区汇总</a></span>+ <span>🔖 <a href="https://b3log.org/siyuan/download.html" target="_blank" class="version"></a></span>+ </div>+ <div class="fn__flex-1"></div>+ </div>+</div>+<div class="fn__none layout" id="enUS">+ <div class="wsPanel">+ <div class="wsTitle">Workspaces</div>+ <div class="wsList"></div>+ </div>+ <div class="fn__flex-column right">+ <div class="fn__flex-1"></div>+ <img class="icon">+ <h2>SiYuan</h2>+ <div class="fn__flex-1"></div>+ <div class="missing-tip">+ <div>⚠️ The last opened workspace path could not be found</div>+ <div class="missing-path"><code class="missing"></code></div>+ </div>+ <div class="fn__flex-1"></div>+ <div class="notice fn__none emptyHint">+ <span>No available workspaces, please select a new workspace path</span>+ </div>+ <label class="b3-label">+ <div>+ 🗂️ Select a new workspace path+ <div class="b3-label__text">The workspace is used to store data, which can be switched later in the top bar+ menu later+ </div>+ </div>+ <div class="fn__flex-1"></div>+ <button class="b3-button fn__flex-center choosePath">Select</button>+ </label>+ <label class="b3-label">+ <div>+ 🌐 Language+ <div class="b3-label__text">User interface language, which can be switched later in <kbd>Settings</kbd> -+ <kbd>Appearance</kbd>+ </div>+ </div>+ <div class="fn__flex-1"></div>+ <select class="b3-select lang">+ <option value="ar">العربية</option>+ <option value="de">Deutsch</option>+ <option value="en">English</option>+ <option value="es">Español</option>+ <option value="fr">Français</option>+ <option value="he">עברית</option>+ <option value="hi">हिन्दी</option>+ <option value="id">Bahasa Indonesia</option>+ <option value="it">Italiano</option>+ <option value="ja">日本語</option>+ <option value="ko">한국어</option>+ <option value="nl">Nederlands</option>+ <option value="pl">Polski</option>+ <option value="pt-BR">Português (Brasil)</option>+ <option value="ru">Русский</option>+ <option value="sk">Slovenčina</option>+ <option value="th">ไทย</option>+ <option value="tr">Türkçe</option>+ <option value="uk">Українська</option>+ <option value="zh-TW">繁體中文</option>+ <option value="zh-CN">简体中文</option>+ </select>+ </label>+ <div class="fn__flex-1"></div>+ <div class="feedback">+ <span>❤️ <a href="https://liuyun.io/article/1686530886208" target="_blank">Support and Feedback</a></span>+ <span>🏘️ <a href="https://liuyun.io/article/1687779743723" target="_blank">User community summary</a></span>+ <span>🔖 <a href="https://b3log.org/siyuan/en/download.html" target="_blank" class="version"></a></span>+ </div>+ <div class="fn__flex-1"></div>+ </div>+</div>+<svg class="svg" id="min" viewBox="0 0 32 32">+ <path d="M1.333 14.667h29.333q1.333 0 1.333 1.333v0q0 1.333-1.333 1.333h-29.333q-1.333 0-1.333-1.333v0q0-1.333 1.333-1.333z"></path>+</svg>+<svg class="svg" id="close" viewBox="0 0 32 32">+ <path d="M32 3.221l-12.779 12.779 12.779 12.779-3.221 3.221-12.779-12.779-12.779 12.779-3.221-3.221 12.779-12.779-12.779-12.779 3.221-3.221 12.779 12.779 12.779-12.779z"></path>+</svg>+<div class="drag"></div>+<script>+ const isChinese = (lang) => {+ return 'zh-CN' === lang || 'zh-TW' === lang+ }++ const getSearch = (key) => {+ if (window.location.search.indexOf('?') === -1) {+ return ''+ }+ let value = ''+ const data = window.location.search.split('?')[1].split('&')+ data.find(item => {+ const keyValue = item.split('=')+ if (keyValue[0] === key) {+ value = keyValue[1]+ return true+ }+ })+ return value+ }++ let currentLang = decodeURIComponent(getSearch('lang'))+ const setLang = (lang) => {+ if (isChinese(lang)) {+ document.title = `思源笔记 v${getSearch('v')}`+ document.getElementById('zhCN').classList.remove('fn__none')+ document.getElementById('enUS').classList.add('fn__none')+ } else {+ document.title = `SiYuan v${getSearch('v')}`+ document.getElementById('zhCN').classList.add('fn__none')+ document.getElementById('enUS').classList.remove('fn__none')+ }+ currentLang = lang+ }+ setLang(currentLang)++ document.querySelectorAll('.version').forEach(item => {+ item.textContent = `v${getSearch('v')}`+ })++ document.querySelectorAll('.icon').forEach(item => {+ item.src = decodeURIComponent(`${getSearch('icon')}`)+ })++ document.querySelectorAll('.missing').forEach(item => {+ item.textContent = decodeURIComponent(getSearch('missing'))+ })++ document.querySelectorAll('.lang').forEach(item => {+ item.value = currentLang+ item.addEventListener('change', () => {+ document.querySelectorAll('.lang').forEach(item1 => {+ item1.value = item.value+ })+ setLang(item.value)+ })+ })++ if (process.platform === 'darwin') {+ document.body.classList.add('darwin');+ }++ document.getElementById('close').addEventListener('click', () => {+ const {ipcRenderer} = require('electron')+ ipcRenderer.send('siyuan-first-quit')+ })+ document.getElementById('min').addEventListener('click', () => {+ const {ipcRenderer} = require('electron')+ ipcRenderer.send("siyuan-cmd", "minimize");+ })++ // 渲染可用工作空间列表 https://github.com/siyuan-note/siyuan/issues/14748+ const path = require('path')+ const {ipcRenderer} = require('electron')+ const wsRaw = decodeURIComponent(getSearch('workspaces'))+ const wsList = wsRaw.split('\n').filter(item => '' !== item.trim())+ const renderWsList = (root, lang) => {+ root.innerHTML = ''+ wsList.forEach(p => {+ const div = document.createElement('div')+ div.className = 'ws-item'+ const text = document.createElement('div')+ text.className = 'ws-text'+ const name = document.createElement('div')+ name.className = 'ws-name'+ name.textContent = path.basename(p)+ const wsPath = document.createElement('div')+ wsPath.className = 'ws-path'+ wsPath.textContent = p+ text.appendChild(name)+ text.appendChild(wsPath)+ const btn = document.createElement('button')+ btn.className = 'b3-button ws-open'+ btn.textContent = isChinese(lang) ? '打开' : 'Open'+ btn.dataset.path = p+ btn.addEventListener('click', () => {+ ipcRenderer.send('siyuan-select-workspace', {+ workspace: p,+ lang: lang,+ })+ })+ div.appendChild(text)+ div.appendChild(btn)+ root.appendChild(div)+ })+ }+ if (0 < wsList.length) {+ // 存在可用工作空间时渲染左列列表,隐藏右列空提示+ renderWsList(document.querySelector('#zhCN .wsList'), 'zh-CN')+ renderWsList(document.querySelector('#enUS .wsList'), 'en')+ } else {+ // 没有可用工作空间时隐藏左列,显示右列空提示+ document.querySelectorAll('.wsPanel').forEach(item => {+ item.classList.add('fn__none')+ })+ document.querySelectorAll('.emptyHint').forEach(item => {+ item.classList.remove('fn__none')+ })+ }++ document.querySelectorAll('.choosePath').forEach((item) => {+ item.addEventListener('click', async () => {+ const fs = require('fs')++ let defaultWorkspace = path.join(decodeURIComponent(getSearch('home')), "SiYuan")+ if ("darwin" === process.platform) {+ // Change the initial workspace path to ~/Library/Application Support/SiYuan on macOS https://github.com/siyuan-note/siyuan/issues/17095+ defaultWorkspace = path.join(decodeURIComponent(getSearch('home')), "Library", "Application Support", "SiYuan")+ }+ if (!fs.existsSync(defaultWorkspace)) {+ fs.mkdirSync(defaultWorkspace, {mode: 0o755, recursive: true})+ }++ const result = await ipcRenderer.invoke("siyuan-get", {+ cmd: "showOpenDialog",+ defaultPath: defaultWorkspace,+ properties: ['openDirectory', 'createDirectory'],+ });++ if (result.canceled) {+ return+ }+ const initPath = result.filePaths[0]++ if (isPartitionRootPath(initPath)) {+ let msg = '⚠️ Do not create the workspace in the partition root path, please create a new folder as the workspace'+ if (isChinese(currentLang)) {+ msg = '⚠️ 请勿在分区根路径上创建工作空间,请新建一个文件夹作为工作空间'+ }+ alert(msg)+ return+ }++ if (!isWorkspaceDir(initPath) && !isEmptyDir(initPath)) {+ let msg = '⚠️ This folder contains other files, please create a new folder as the workspace'+ if (isChinese(currentLang)) {+ msg = '⚠️ 该文件夹包含了其他文件,请新建一个文件夹作为工作空间'+ }+ alert(msg)+ return+ }++ if (isICloudPath(initPath)) {+ let msg = '⚠️ This folder is under the iCloud sync path, please change another path'+ if (isChinese(currentLang)) {+ msg = '⚠️ 该文件夹位于 iCloud 同步路径下,请更换其他路径'+ }+ alert(msg)+ return+ }++ if (isCloudDrivePath(initPath)) {+ let msg = '⚠️ The folder path can not contain onedrive, dropbox, google drive, pcloud, nutstore, baidunetdisk, weiyun,坚果云,百度网盘 and 腾讯微云, etc., please change another path'+ if (isChinese(currentLang)) {+ msg = '⚠️ 文件夹路径不能包含 onedrive、dropbox、google drive、pcloud、nutstore、baidunetdisk、weiyun、坚果云、百度网盘和腾讯微云等字样,请更换其他路径'+ }+ alert(msg)+ return+ }+ let msg = '⚠️ Please confirm that the workspace is not set under the path of a third-party sync disk, otherwise it will cause data damage (iCloud/OneDrive/Dropbox/Google Drive/Nutstore/Baidu Netdisk/Tencent Weiyun, etc.), continue?'+ if (isChinese(currentLang)) {+ msg = '⚠️ 请确认没有将工作空间设置在第三方同步盘路径下,否则会造成数据损坏(iCloud/OneDrive/Dropbox/Google Drive/坚果云/百度网盘/腾讯微云等),是否继续?'+ }+ if (!confirm(msg)) {+ return+ }+ if (!fs.existsSync(initPath)) {+ fs.mkdirSync(initPath, {mode: 0o755, recursive: true})+ }+ ipcRenderer.send('siyuan-select-workspace', {+ workspace: initPath,+ lang: currentLang,+ })+ })+ })++ const isPartitionRootPath = (absPath) => {+ const parsedPath = path.parse(absPath)+ return parsedPath.root === absPath+ }++ const isEmptyDir = (absPath) => {+ const fs = require('fs')+ let files;+ try {+ files = fs.readdirSync(absPath).filter(file => file !== '.DS_Store')+ } catch (err) {+ return false+ }+ return 0 === files.length+ }++ const isWorkspaceDir = (absPath) => {+ const fs = require('fs');+ const conf = path.join(absPath, 'conf', 'conf.json');+ let data;+ try {+ data = fs.readFileSync(conf, 'utf8');+ } catch (err) {+ return false;+ }+ return data.includes('kernelVersion');+ }++ const isCloudDrivePath = (absPath) => {+ const absPathLower = absPath.toLowerCase()+ return -1 < absPathLower.indexOf("onedrive") || -1 < absPathLower.indexOf("dropbox") ||+ -1 < absPathLower.indexOf("google drive") || -1 < absPathLower.indexOf("pcloud") ||+ -1 < absPathLower.indexOf("坚果云") || -1 < absPathLower.indexOf("nutstore") ||+ -1 < absPathLower.indexOf("百度网盘") || -1 < absPathLower.indexOf("baidunetdisk") ||+ -1 < absPathLower.indexOf("腾讯微云") || -1 < absPathLower.indexOf("weiyun");+ }++ const isICloudPath = (absPath) => {+ const os = require('os')+ if ('darwin' !== os.platform()) {+ return false+ }++ // macOS 端对工作空间放置在 iCloud 路径下做检查 https://github.com/siyuan-note/siyuan/issues/7747+ const iCloudRoot = path.join(decodeURIComponent(getSearch('home')), 'Library', 'Mobile Documents')+ const absPathLower = absPath.toLowerCase()+ if (!simpleCheckIcloudPath(absPath)) {+ // 简单判断无法通过则复杂验证+ const allFiles = walk(iCloudRoot)+ for (const file of allFiles) {+ if (-1 < absPathLower.indexOf(file.toLowerCase())) {+ return true+ }+ }+ }+ return false+ }++ // 简单判断 iCloud 同步目录+ // 不允许 为桌面 文档 和 iCloud 文件夹 和软链接+ const simpleCheckIcloudPath = (absPath) => {+ const fs = require('fs')+ let stat = fs.lstatSync(absPath)+ if (stat.isSymbolicLink()) {+ return false+ }++ const homePath = decodeURIComponent(getSearch('home'))+ const absPathLower = absPath.toLowerCase()+ const iCloudRoot = path.join(homePath, 'Library', 'Mobile Documents')+ if (absPathLower.startsWith(iCloudRoot.toLowerCase())) {+ return false+ }++ const documentsRoot = path.join(homePath, 'Documents')+ if (absPathLower.startsWith(documentsRoot.toLowerCase())) {+ return false+ }++ const desktopRoot = path.join(homePath, 'Desktop')+ if (absPathLower.startsWith(desktopRoot.toLowerCase())) {+ return false+ }+ return true+ }++ const walk = (dir, files = []) => {+ let dirFiles;+ const fs = require('fs')+ try {+ if (!fs.existsSync(dir)) {+ console.log("dir [" + dir + "] not exists")+ return files+ }+ dirFiles = fs.readdirSync(dir)+ } catch (e) {+ console.error("read dir [" + dir + "] failed: ", e)+ return files+ }++ for (const f of dirFiles) {+ let stat = fs.lstatSync(dir + path.sep + f)+ if (stat.isSymbolicLink()) {+ files.push(fs.readlinkSync(dir + path.sep + f))+ continue+ }+ if (stat.isDirectory()) {+ // 如果已经遍历过则不再遍历+ if (files.includes(dir + path.sep + f)) {+ continue+ }+ files.push(dir + path.sep + f)+ walk(dir + path.sep + f, files)+ }+ }+ return files+ }++</script>+</body>+</html>