This commit is contained in:
fyears 2024-01-06 12:01:41 +08:00
commit 635db41d0d
7 changed files with 5212 additions and 10 deletions

5087
pnpm-lock.yaml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -79,6 +79,7 @@ export interface RemotelySavePluginSettings {
currLogLevel?: string;
autoRunEveryMilliseconds?: number;
initRunAfterMilliseconds?: number;
syncOnSaveAfterMilliseconds?: number;
agreeToUploadExtraMetadata?: boolean;
concurrency?: number;
syncConfigDir?: boolean;

View File

@ -83,6 +83,7 @@ const DEFAULT_SETTINGS: RemotelySavePluginSettings = {
// vaultRandomID: "", // deprecated
autoRunEveryMilliseconds: -1,
initRunAfterMilliseconds: -1,
syncOnSaveAfterMilliseconds: -1,
agreeToUploadExtraMetadata: false,
concurrency: 5,
syncConfigDir: false,
@ -138,6 +139,7 @@ export default class RemotelySavePlugin extends Plugin {
currSyncMsg?: string;
syncRibbon?: HTMLElement;
autoRunIntervalID?: number;
syncOnSaveIntervalID?: number;
i18n: I18n;
vaultRandomID: string;
@ -174,8 +176,7 @@ export default class RemotelySavePlugin extends Plugin {
try {
log.info(
`${
this.manifest.id
`${this.manifest.id
}-${Date.now()}: start sync, triggerSource=${triggerSource}`
);
@ -381,8 +382,7 @@ export default class RemotelySavePlugin extends Plugin {
}
log.info(
`${
this.manifest.id
`${this.manifest.id
}-${Date.now()}: finish sync, triggerSource=${triggerSource}`
);
} catch (error) {
@ -809,6 +809,7 @@ export default class RemotelySavePlugin extends Plugin {
} else {
this.enableAutoSyncIfSet();
this.enableInitSyncIfSet();
this.enableSyncOnSaveIfSet();
}
}
@ -990,6 +991,47 @@ export default class RemotelySavePlugin extends Plugin {
this.vaultRandomID = vaultRandomID;
}
enableSyncOnSaveIfSet() {
if (
this.settings.syncOnSaveAfterMilliseconds !== undefined &&
this.settings.syncOnSaveAfterMilliseconds !== null &&
this.settings.syncOnSaveAfterMilliseconds > 0
) {
let runScheduled = false;
this.app.workspace.onLayoutReady(() => {
const intervalID = window.setInterval(() => {
const currentFile = this.app.workspace.getActiveFile();
if (currentFile) {
// get the last modified time of the current file
// if it has been modified within the last syncOnSaveAfterMilliseconds
// then schedule a run for syncOnSaveAfterMilliseconds after it was modified
const lastModified = currentFile.stat.mtime;
const currentTime = Date.now();
log.debug(
`Checking if file was modified within last ${this.settings.syncOnSaveAfterMilliseconds / 1000} seconds, last modified: ${(currentTime - lastModified) / 1000} seconds ago`
);
if (currentTime - lastModified < this.settings.syncOnSaveAfterMilliseconds) {
if (!runScheduled) {
const scheduleTimeFromNow = this.settings.syncOnSaveAfterMilliseconds - (currentTime - lastModified)
log.info(`schedule a run for ${scheduleTimeFromNow} milliseconds later`)
runScheduled = true
setTimeout(() => {
this.syncRun("auto")
runScheduled = false
},
scheduleTimeFromNow
)
}
}
}
}, 1_000);
this.syncOnSaveIntervalID = intervalID;
this.registerInterval(intervalID);
});
}
}
enableAutoSyncIfSet() {
if (
this.settings.autoRunEveryMilliseconds !== undefined &&

View File

@ -21,6 +21,7 @@ export const applyLogWriterInplace = function (writer: (...msg: any[]) => any) {
logLevel: LogLevelNumbers,
loggerName: string | symbol
) {
// @ts-ignore
const rawMethod = originalFactory(methodName, logLevel, loggerName);
return function (...msg: any[]) {

View File

@ -831,8 +831,7 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
dropdown
.setValue(
`${
this.plugin.settings.s3.bypassCorsLocally ? "enable" : "disable"
`${this.plugin.settings.s3.bypassCorsLocally ? "enable" : "disable"
}`
)
.onChange(async (value) => {
@ -1517,6 +1516,67 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
});
});
new Setting(basicDiv)
.setName(t("settings_saverun"))
.setDesc(t("settings_saverun_desc"))
.addDropdown((dropdown) => {
dropdown.addOption("-1", t("settings_saverun_notset"));
dropdown.addOption(`${1000 * 1}`, t("settings_saverun_1sec"));
dropdown.addOption(`${1000 * 5}`, t("settings_saverun_5sec"));
dropdown.addOption(`${1000 * 10}`, t("settings_saverun_10sec"));
dropdown.addOption(`${1000 * 60}`, t("settings_saverun_1min"));
let runScheduled = false
dropdown
.setValue(`${this.plugin.settings.syncOnSaveAfterMilliseconds}`)
.onChange(async (val: string) => {
const realVal = parseInt(val);
this.plugin.settings.syncOnSaveAfterMilliseconds = realVal;
await this.plugin.saveSettings();
if (
(realVal === undefined || realVal === null || realVal <= 0) &&
this.plugin.syncOnSaveIntervalID !== undefined
) {
// clear
window.clearInterval(this.plugin.syncOnSaveIntervalID);
this.plugin.syncOnSaveIntervalID = undefined;
} else if (
realVal !== undefined &&
realVal !== null &&
realVal > 0
) {
const intervalID = window.setInterval(() => {
const currentFile = this.app.workspace.getActiveFile();
if (currentFile) {
// get the last modified time of the current file
// if it has been modified within the last syncOnSaveAfterMilliseconds
// then schedule a run for syncOnSaveAfterMilliseconds after it was modified
const lastModified = currentFile.stat.mtime;
const currentTime = Date.now();
log.debug(
`Checking if file was modified within last ${this.plugin.settings.syncOnSaveAfterMilliseconds / 1000} seconds, last modified: ${(currentTime - lastModified) / 1000} seconds ago`
);
if (currentTime - lastModified < this.plugin.settings.syncOnSaveAfterMilliseconds) {
if (!runScheduled) {
const scheduleTimeFromNow = this.plugin.settings.syncOnSaveAfterMilliseconds - (currentTime - lastModified)
log.info(`schedule a run for ${scheduleTimeFromNow} milliseconds later`)
runScheduled = true
setTimeout(() => {
this.plugin.syncRun("auto")
runScheduled = false
},
scheduleTimeFromNow
)
}
}
}
}, realVal);
this.plugin.syncOnSaveIntervalID = intervalID;
this.plugin.registerInterval(intervalID);
}
});
});
new Setting(basicDiv)
.setName(t("settings_autorun"))
.setDesc(t("settings_autorun_desc"))
@ -1546,6 +1606,8 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
realVal > 0
) {
const intervalID = window.setInterval(() => {
log.info("auto run from settings.ts");
console.log("auto run from settings.ts");
this.plugin.syncRun("auto");
}, realVal);
this.plugin.autoRunIntervalID = intervalID;
@ -1579,6 +1641,7 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
});
});
new Setting(basicDiv)
.setName(t("settings_skiplargefiles"))
.setDesc(t("settings_skiplargefiles_desc"))

View File

@ -55,6 +55,7 @@ export class SyncAlgoV2Modal extends Modal {
this.plugin.saveAgreeToUseNewSyncAlgorithm();
this.plugin.enableAutoSyncIfSet();
this.plugin.enableInitSyncIfSet();
this.plugin.enableSyncOnSaveIfSet();
} else {
log.info("do not agree to use the new algorithm");
this.plugin.unload();

View File

@ -4,7 +4,7 @@
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "es6",
"target": "ESNext",
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
@ -13,7 +13,14 @@
"esModuleInterop": true,
"importHelpers": true,
"isolatedModules": true,
"lib": ["dom", "es5", "scripthost", "es2015"]
"lib": [
"dom",
"es5",
"scripthost",
"es2015"
]
},
"include": ["**/*.ts"]
}
"include": [
"**/*.ts"
]
}