From 408acb623014b8461bf70f1e0f54421e73801bfb Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Sat, 25 May 2024 14:37:37 +0800 Subject: [PATCH] add custom protection --- src/langs/en.json | 3 ++ src/langs/zh_cn.json | 3 ++ src/langs/zh_tw.json | 3 ++ src/settings.ts | 87 ++++++++++++++++++++++++++++++++++---------- styles.css | 4 ++ 5 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/langs/en.json b/src/langs/en.json index 872f1f7..3cbd239 100644 --- a/src/langs/en.json +++ b/src/langs/en.json @@ -289,6 +289,9 @@ "settings_protectmodifypercentage_000_desc": "0 (always block)", "settings_protectmodifypercentage_050_desc": "50 (default)", "settings_protectmodifypercentage_100_desc": "100 (disable the protection)", + "settings_protectmodifypercentage_custom_desc": "custom", + "settings_protectmodifypercentage_customfield": "Custom Abort Sync If Modification Above Percentage", + "settings_protectmodifypercentage_customfield_desc": "You need to enter a number between 0 (inclusive) and 100 (inclusive). Float number is also allowed.", "setting_syncdirection": "Sync Direction", "setting_syncdirection_desc": "Which direction should the plugin sync to? Please be aware that only CHANGED files (based on time and size) are synced regardless any option.", "setting_syncdirection_bidirectional_desc": "Bidirectional (default)", diff --git a/src/langs/zh_cn.json b/src/langs/zh_cn.json index adfa6d9..56084ef 100644 --- a/src/langs/zh_cn.json +++ b/src/langs/zh_cn.json @@ -288,6 +288,9 @@ "settings_protectmodifypercentage_000_desc": "0(总是强制中止)", "settings_protectmodifypercentage_050_desc": "50(默认值)", "settings_protectmodifypercentage_100_desc": "100(去除此保护)", + "settings_protectmodifypercentage_custom_desc": "自定义", + "settings_protectmodifypercentage_customfield": "如果修改超过自定义百分比则中止同步", + "settings_protectmodifypercentage_customfield_desc": "您需要输入 0(含)~ 100(含)的数字。小数也是可以的。", "setting_syncdirection": "同步方向", "setting_syncdirection_desc": "插件应该向哪里同步?注意每个选项都是只有修改了的文件(基于修改时间和大小判断)才会触发同步动作。", "setting_syncdirection_bidirectional_desc": "双向同步(默认)", diff --git a/src/langs/zh_tw.json b/src/langs/zh_tw.json index ff48510..e2e0d25 100644 --- a/src/langs/zh_tw.json +++ b/src/langs/zh_tw.json @@ -287,6 +287,9 @@ "settings_protectmodifypercentage_000_desc": "0(總是強制中止)", "settings_protectmodifypercentage_050_desc": "50(預設值)", "settings_protectmodifypercentage_100_desc": "100(去除此保護)", + "settings_protectmodifypercentage_custom_desc": "自定義", + "settings_protectmodifypercentage_customfield": "如果修改超過自定義百分比則中止同步", + "settings_protectmodifypercentage_customfield_desc": "您需要輸入 0(含)~ 100(含)的數字。小數也是可以的。", "setting_syncdirection": "同步方向", "setting_syncdirection_desc": "外掛應該向哪裡同步?注意每個選項都是隻有修改了的檔案(基於修改時間和大小判斷)才會觸發同步動作。", "setting_syncdirection_bidirectional_desc": "雙向同步(預設)", diff --git a/src/settings.ts b/src/settings.ts index 8992372..116c556 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -2168,28 +2168,77 @@ export class RemotelySaveSettingTab extends PluginSettingTab { }); }); - new Setting(advDiv) + const percentage1 = new Setting(advDiv) .setName(t("settings_protectmodifypercentage")) - .setDesc(t("settings_protectmodifypercentage_desc")) - .addDropdown((dropdown) => { - for (const i of Array.from({ length: 11 }, (x, i) => i * 10)) { - let desc = `${i}`; - if (i === 0) { - desc = t("settings_protectmodifypercentage_000_desc"); - } else if (i === 50) { - desc = t("settings_protectmodifypercentage_050_desc"); - } else if (i === 100) { - desc = t("settings_protectmodifypercentage_100_desc"); - } - dropdown.addOption(`${i}`, desc); - } - dropdown - .setValue(`${this.plugin.settings.protectModifyPercentage ?? 50}`) - .onChange(async (val) => { - this.plugin.settings.protectModifyPercentage = Number.parseInt(val); + .setDesc(t("settings_protectmodifypercentage_desc")); + + const percentage2 = new Setting(advDiv) + .setName(t("settings_protectmodifypercentage_customfield")) + .setDesc(t("settings_protectmodifypercentage_customfield_desc")); + if ((this.plugin.settings.protectModifyPercentage ?? 50) % 10 === 0) { + percentage2.settingEl.addClass("settings-percentage-custom-hide"); + } + let percentage2Text: TextComponent | undefined = undefined; + percentage2.addText((text) => { + text.inputEl.type = "number"; + percentage2Text = text; + text + .setPlaceholder("0 ~ 100") + .setValue(`${this.plugin.settings.protectModifyPercentage ?? 50}`) + .onChange(async (val) => { + let k = Number.parseFloat(val.trim()); + if (Number.isNaN(k)) { + // do nothing! + } else { + if (k < 0) { + k = 0; + } else if (k > 100) { + k = 100; + } + this.plugin.settings.protectModifyPercentage = k; await this.plugin.saveSettings(); - }); + } + }); + }); + + percentage1.addDropdown((dropdown) => { + for (const i of Array.from({ length: 11 }, (x, i) => i * 10)) { + let desc = `${i}`; + if (i === 0) { + desc = t("settings_protectmodifypercentage_000_desc"); + } else if (i === 50) { + desc = t("settings_protectmodifypercentage_050_desc"); + } else if (i === 100) { + desc = t("settings_protectmodifypercentage_100_desc"); + } + dropdown.addOption(`${i}`, desc); + } + dropdown.addOption( + "custom", + t("settings_protectmodifypercentage_custom_desc") + ); + + const p = this.plugin.settings.protectModifyPercentage ?? 50; + let initVal = "custom"; + if (p % 10 === 0) { + initVal = `${p}`; + } else { + // show custom + percentage2.settingEl.removeClass("settings-percentage-custom"); + } + dropdown.setValue(initVal).onChange(async (val) => { + const k = Number.parseInt(val); + if (val === "custom" || Number.isNaN(k)) { + // do nothing until user changes something in custom field + percentage2.settingEl.removeClass("settings-percentage-custom-hide"); + } else { + this.plugin.settings.protectModifyPercentage = k; + percentage2.settingEl.addClass("settings-percentage-custom-hide"); + percentage2Text?.setValue(`${k}`); + await this.plugin.saveSettings(); + } }); + }); new Setting(advDiv) .setName(t("setting_syncdirection")) diff --git a/styles.css b/styles.css index ed09911..2804efe 100644 --- a/styles.css +++ b/styles.css @@ -17,6 +17,10 @@ padding-top: 18px; } +.settings-percentage-custom-hide { + display: none; +} + .s3-disclaimer { font-weight: bold; }