add bookmarks sync

This commit is contained in:
fyears 2024-07-14 17:04:05 +08:00
parent 5c86bc039d
commit 12ed023cd1
10 changed files with 92 additions and 6 deletions

View File

@ -74,6 +74,7 @@ export const generateClearDupFilesSettingsPart = (
const fsLocal = new FakeFsLocal(
app.vault,
plugin.settings.syncConfigDir ?? false,
plugin.settings.syncBookmarks ?? false,
app.vault.configDir,
plugin.manifest.id,
undefined,

View File

@ -116,9 +116,21 @@ const isInsideObsFolder = (x: string, configDir: string) => {
return x === configDir || x.startsWith(`${configDir}/`);
};
const isBookmarksFile = (x: string, configDir: string) => {
if (!configDir.startsWith(".")) {
throw Error(`configDir should starts with . but we get ${configDir}`);
}
return (
x === configDir ||
x === `${configDir}/` ||
x === `${configDir}/bookmarks.json`
);
};
const isSkipItemByName = (
key: string,
syncConfigDir: boolean,
syncBookmarks: boolean,
syncUnderscoreItems: boolean,
configDir: string,
ignorePaths: string[]
@ -136,6 +148,11 @@ const isSkipItemByName = (
if (syncConfigDir && isInsideObsFolder(key, configDir)) {
return false;
}
if (syncBookmarks && isBookmarksFile(key, configDir)) {
return false;
}
if (isSpecialFolderNameToSkip(key, [])) {
// some special dirs and files are always skipped
return true;
@ -157,6 +174,7 @@ const ensembleMixedEnties = async (
remoteEntityList: Entity[],
syncConfigDir: boolean,
syncBookmarks: boolean,
configDir: string,
syncUnderscoreItems: boolean,
ignorePaths: string[],
@ -184,6 +202,7 @@ const ensembleMixedEnties = async (
isSkipItemByName(
key,
syncConfigDir,
syncBookmarks,
syncUnderscoreItems,
configDir,
ignorePaths
@ -216,6 +235,7 @@ const ensembleMixedEnties = async (
isSkipItemByName(
key,
syncConfigDir,
syncBookmarks,
syncUnderscoreItems,
configDir,
ignorePaths
@ -251,6 +271,7 @@ const ensembleMixedEnties = async (
isSkipItemByName(
key,
syncConfigDir,
syncBookmarks,
syncUnderscoreItems,
configDir,
ignorePaths
@ -911,13 +932,14 @@ const getSyncPlanInplace = async (
mixedEntityMappings["/$@meta"] = {
key: "/$@meta", // don't mess up with the types
sideNotes: {
version: "20240616 fs version",
version: "20240714 fs version",
generateTime: currTime,
generateTimeFmt: currTimeFmt,
service: settings.serviceType,
concurrency: settings.concurrency,
hasPassword: settings.password !== "",
syncConfigDir: settings.syncConfigDir,
syncBookmarks: settings.syncBookmarks,
syncUnderscoreItems: settings.syncUnderscoreItems,
skipSizeLargerThan: settings.skipSizeLargerThan,
protectModifyPercentage: settings.protectModifyPercentage,
@ -1709,6 +1731,7 @@ export async function syncer(
prevSyncEntityList,
remoteEntityList,
settings.syncConfigDir ?? false,
settings.syncBookmarks ?? false,
configDir,
settings.syncUnderscoreItems ?? false,
settings.ignorePaths ?? [],

View File

@ -167,6 +167,7 @@ export interface RemotelySavePluginSettings {
concurrency?: number;
syncConfigDir?: boolean;
syncBookmarks?: boolean;
syncUnderscoreItems?: boolean;
lang?: LangTypeAndAuto;
agreeToUseSyncV3?: boolean;

View File

@ -9,6 +9,7 @@ import type { Profiler } from "./profiler";
export class FakeFsLocal extends FakeFs {
vault: Vault;
syncConfigDir: boolean;
syncBookmarks: boolean;
configDir: string;
pluginID: string;
profiler: Profiler | undefined;
@ -17,6 +18,7 @@ export class FakeFsLocal extends FakeFs {
constructor(
vault: Vault,
syncConfigDir: boolean,
syncBookmarks: boolean,
configDir: string,
pluginID: string,
profiler: Profiler | undefined,
@ -26,6 +28,7 @@ export class FakeFsLocal extends FakeFs {
this.vault = vault;
this.syncConfigDir = syncConfigDir;
this.syncBookmarks = syncBookmarks;
this.configDir = configDir;
this.pluginID = pluginID;
this.profiler = profiler;
@ -96,13 +99,16 @@ export class FakeFsLocal extends FakeFs {
this.profiler?.insert("finish transforming walk for local");
if (this.syncConfigDir) {
this.profiler?.insert("into syncConfigDir");
if (this.syncConfigDir || this.syncBookmarks) {
this.profiler?.insert("into syncConfigDir or syncBookmarks");
const bookmarksOnly = !this.syncConfigDir;
const syncFiles = await listFilesInObsFolder(
this.configDir,
this.vault,
this.pluginID
this.pluginID,
bookmarksOnly
);
// console.debug(`syncFiles in obs: ${JSON.stringify(syncFiles, null, 2)}`);
for (const f of syncFiles) {
local.push(f);
}

View File

@ -282,6 +282,8 @@
"settings_syncunderscore_desc": "Sync files or folders starting with _ (\"underscore\") or not",
"settings_configdir": "Sync Config Dir (experimental)",
"settings_configdir_desc": "Sync config dir {{configDir}} or not (inner folder .git and node_modules would be ignored). Please be aware that this may impact all your plugins' or Obsidian's settings, and may require you restart Obsidian after sync. Enable this at your own risk.",
"settings_bookmarks": "Sync Bookmarks (experimental)",
"settings_bookmarks_desc": "Sync {{configDir}}/bookmarks.json or not. If you sync config dir, this option will be ignore and treated as \"enabled\".",
"settings_deletetowhere": "Deletion Destination",
"settings_deletetowhere_desc": "Which trash should the plugin put the files into while deleting?",
"settings_deletetowhere_system_trash": "system trash (default)",

View File

@ -281,6 +281,8 @@
"settings_syncunderscore_desc": "是否同步“_”下划线开始的文件或文件夹。",
"settings_configdir": "同步配置文件夹(实验性质)",
"settings_configdir_desc": "是否同步 Obsidian 的配置文件夹 {{configDir}}(其中的 .git 和 node_modules 依然会被忽略)。请注意,此设置可能会影响到您 Obsidian 或者所有的插件设置,也可能需要您同步完毕后重启 Obsidian。如果您同意自行承受其风险则可以开启本设置。",
"settings_bookmarks": "同步书签(实验性质)",
"settings_bookmarks_desc": "是否同步书签 {{configDir}}/bookmarks.json 。如果您设置了同步配置文件夹,此选项会被忽略并设为“开启”。",
"settings_deletetowhere": "删除文件到哪里",
"settings_deletetowhere_desc": "插件触发删除操作时候,删除到哪里?",
"settings_deletetowhere_system_trash": "系统回收站(默认)",

View File

@ -280,6 +280,8 @@
"settings_syncunderscore_desc": "是否同步“_”下劃線開始的檔案或資料夾。",
"settings_configdir": "同步配置資料夾(實驗性質)",
"settings_configdir_desc": "是否同步 Obsidian 的配置資料夾 {{configDir}}(其中的 .git 和 node_modules 依然會被忽略)。請注意,此設定可能會影響到您 Obsidian 或者所有的外掛設定,也可能需要您同步完畢後重啟 Obsidian。如果您同意自行承受其風險則可以開啟本設定。",
"settings_bookmarks": "同步書籤(實驗性質)",
"settings_bookmarks_desc": "是否同步書籤 {{configDir}}/bookmarks.json 。如果您設定了同步配置資料夾,此選項會被忽略並設為“開啟”。",
"settings_deletetowhere": "刪除檔案到哪裡",
"settings_deletetowhere_desc": "外掛觸發刪除操作時候,刪除到哪裡?",
"settings_deletetowhere_system_trash": "系統回收站(預設)",

View File

@ -135,6 +135,7 @@ const DEFAULT_SETTINGS: RemotelySavePluginSettings = {
agreeToUploadExtraMetadata: true, // as of 20240106, it's safe to assume every new user agrees with this
concurrency: 5,
syncConfigDir: false,
syncBookmarks: false,
syncUnderscoreItems: false,
lang: "auto",
logToDB: false,
@ -239,6 +240,7 @@ export default class RemotelySavePlugin extends Plugin {
const fsLocal = new FakeFsLocal(
this.app.vault,
this.settings.syncConfigDir ?? false,
this.settings.syncBookmarks ?? false,
this.app.vault.configDir,
this.manifest.id,
profiler,
@ -1337,6 +1339,10 @@ export default class RemotelySavePlugin extends Plugin {
messyConfigToNormal(await this.loadData())
);
if (this.settings.syncBookmarks === undefined) {
this.settings.syncBookmarks = false;
}
if (this.settings.dropbox.clientID === "") {
this.settings.dropbox.clientID = DEFAULT_SETTINGS.dropbox.clientID;
}
@ -1879,6 +1885,7 @@ export default class RemotelySavePlugin extends Plugin {
const fsLocal = new FakeFsLocal(
this.app.vault,
this.settings.syncConfigDir ?? false,
this.settings.syncBookmarks ?? false,
this.app.vault.configDir,
this.manifest.id,
undefined,

View File

@ -34,11 +34,15 @@ const isLikelyPluginSubFiles = (x: string) => {
export const listFilesInObsFolder = async (
configDir: string,
vault: Vault,
pluginId: string
pluginId: string,
bookmarksOnly: boolean
): Promise<Entity[]> => {
const q = new Queue([configDir]);
const CHUNK_SIZE = 10;
const contents: Entity[] = [];
let contents: Entity[] = [];
let iterRound = 0;
while (q.length > 0) {
const itemsToFetch: string[] = [];
while (q.length > 0) {
@ -115,6 +119,23 @@ export const listFilesInObsFolder = async (
}
}
}
if (bookmarksOnly && iterRound > 1) {
// list until bookmarks.json is found or next level is arrived.
break;
}
iterRound += 1;
}
// console.debug(`contents in obs config: ${JSON.stringify(contents)}`);
if (bookmarksOnly) {
contents = contents.filter(
(e) =>
e.key === `${configDir}/` || e.key === `${configDir}/bookmarks.json`
);
}
return contents;
};

View File

@ -2325,6 +2325,27 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
});
});
new Setting(advDiv)
.setName(t("settings_bookmarks"))
.setDesc(
t("settings_bookmarks_desc", {
configDir: this.app.vault.configDir,
})
)
.addDropdown((dropdown) => {
dropdown.addOption("disable", t("disable"));
dropdown.addOption("enable", t("enable"));
dropdown
.setValue(
`${this.plugin.settings.syncBookmarks ? "enable" : "disable"}`
)
.onChange(async (val) => {
this.plugin.settings.syncBookmarks = val === "enable";
await this.plugin.saveSettings();
});
});
new Setting(advDiv)
.setName(t("settings_deletetowhere"))
.setDesc(t("settings_deletetowhere_desc"))