mirror of
https://github.com/siyuan-note/plugin-sample-vite-svelte.git
synced 2025-06-07 18:46:01 +00:00
add per device condition
This commit is contained in:
parent
953d15c661
commit
5b73589267
4 changed files with 171 additions and 0 deletions
|
@ -15,6 +15,16 @@
|
|||
"hello": {
|
||||
"makesure": "Before using this template, please read the <a href=\"https://github.com/siyuan-note/plugin-sample\">offical sample</a>, make sure that you've known about the pipeline for plugin developing."
|
||||
},
|
||||
"onlyEnableListedDevices": "Enable Only on Listed Devices",
|
||||
"onlyEnableListedDevicesDesc": "When the switch is on, this plugin enabled only on the devices listed in the list. When the switch is off, this plugin enabled on all devices.",
|
||||
"enableDeviceList": "List of Enabled Devices",
|
||||
"enableDeviceListDesc": "Devices listed in the table will enable this plugin function when the switch is on. Please avoid manual editing of this table as much as possible.",
|
||||
"addCurrentDeviceIntoList": "Add Current Device to List",
|
||||
"addCurrentDeviceIntoListDesc": "Add the current device to the table.",
|
||||
"addCurrentDeviceIntoListLabel": "Add",
|
||||
"removeCurrentDeviceFromList": "Remove Current Device from List",
|
||||
"removeCurrentDeviceFromListDesc": "Remove the current device from the table.",
|
||||
"removeCurrentDeviceFromListLabel": "Remove",
|
||||
"hintTitle":"About",
|
||||
"hintDesc":"<a href='https://github.com/siyuan-note/plugin-sample-vite-svelte'>plugin-sample-vite-svelte</a><br>@frostime<br>@88250<br>@zxkmm"
|
||||
}
|
|
@ -15,6 +15,16 @@
|
|||
"hello": {
|
||||
"makesure": "使用这个模板之前,请阅读<a href=\"https://github.com/siyuan-note/plugin-sample\">官方教程</a>, 确保自己已经理解了插件的基本开发流程。"
|
||||
},
|
||||
"onlyEnableListedDevices": "只在以下设备上启用",
|
||||
"onlyEnableListedDevicesDesc": "当打开开关时,只在表中列出的设备上启用该插件功能。当关闭开关时,在所有设备上启用该插件功能。",
|
||||
"enableDeviceList": "启用的设备的列表",
|
||||
"enableDeviceListDesc": "表中列出的设备会在开关打开时启用该插件功能。请尽量避免手动编辑此表",
|
||||
"addCurrentDeviceIntoList": "添加当前设备",
|
||||
"addCurrentDeviceIntoListDesc": "把当前设备添加到表中",
|
||||
"addCurrentDeviceIntoListLabel": "添加",
|
||||
"removeCurrentDeviceFromList": "移除当前设备",
|
||||
"removeCurrentDeviceFromListDesc": "把当前设备从表中移除",
|
||||
"removeCurrentDeviceFromListLabel": "移除",
|
||||
"hintTitle":"关于",
|
||||
"hintDesc":"<a href='https://github.com/siyuan-note/plugin-sample-vite-svelte'>🔗 plugin-sample-vite-svelte</a><br>💻 @frostime<br>💻 @88250<br>💻 @zxkmm"
|
||||
|
||||
|
|
143
src/index.ts
143
src/index.ts
|
@ -227,6 +227,46 @@ export default class PluginSample extends Plugin {
|
|||
}
|
||||
}
|
||||
});
|
||||
this.settingUtils.addItem({
|
||||
key: "onlyEnableListedDevices",
|
||||
value: false,
|
||||
type: "checkbox",
|
||||
title: this.i18n.onlyEnableListedDevices,
|
||||
description: this.i18n.onlyEnableListedDevicesDesc,
|
||||
});
|
||||
this.settingUtils.addItem({
|
||||
key: "enableDeviceList",
|
||||
value: "",
|
||||
type: "textarea",
|
||||
title: this.i18n.enableDeviceList,
|
||||
description: this.i18n.enableDeviceListDesc,
|
||||
});
|
||||
this.settingUtils.addItem({
|
||||
key: "addCurrentDeviceIntoList",
|
||||
value: "",
|
||||
type: "button",
|
||||
title: this.i18n.addCurrentDeviceIntoList,
|
||||
description: this.i18n.addCurrentDeviceIntoListDesc,
|
||||
button: {
|
||||
label: this.i18n.addCurrentDeviceIntoListLabel,
|
||||
callback: () => {
|
||||
this.appendCurrentDeviceIntoList();
|
||||
}
|
||||
}
|
||||
});
|
||||
this.settingUtils.addItem({
|
||||
key: "removeCurrentDeviceFromList",
|
||||
value: "",
|
||||
type: "button",
|
||||
title: this.i18n.removeCurrentDeviceFromList,
|
||||
description: this.i18n.removeCurrentDeviceFromListDesc,
|
||||
button: {
|
||||
label: this.i18n.removeCurrentDeviceFromListLabel,
|
||||
callback: () => {
|
||||
this.removeCurrentDeviceFromList();
|
||||
}
|
||||
}
|
||||
});
|
||||
this.settingUtils.addItem({
|
||||
key: "Hint",
|
||||
value: "",
|
||||
|
@ -291,6 +331,29 @@ export default class PluginSample extends Plugin {
|
|||
this.settingUtils.get("Select") + "\n"
|
||||
);
|
||||
|
||||
// within layoutready async caller sample
|
||||
const layoutReadyAsyncHandler = async () => {
|
||||
try {
|
||||
|
||||
/*分设备caller真值表:
|
||||
当前设备真, 仅允许开关开,后半段为假 :真||假: 执行
|
||||
当前设备真, 仅允许开关关,后半段为真 :真||真: 执行
|
||||
当前设备假, 仅允许开关开,后半段为假 :假||假: 不执行
|
||||
当前设备假, 仅允许开关关,后半段为真 :假||真: 执行
|
||||
*/
|
||||
if ((await this.currentDeviceInList() || !this.settingUtils.get("onlyEnableListedDevices"))) {
|
||||
console.log("per device enable logic: true\nAKA device in list or onlyEnableListedDevices is false");
|
||||
}else{
|
||||
console.log("per device enable logic: false\nAKA device not in list and onlyEnableListedDevices is true");
|
||||
}
|
||||
|
||||
}
|
||||
catch (error) {
|
||||
console.error("within layoutready async caller calling fail", error);
|
||||
}
|
||||
}
|
||||
layoutReadyAsyncHandler();
|
||||
|
||||
let tabDiv = document.createElement("div");
|
||||
new HelloExample({
|
||||
target: tabDiv,
|
||||
|
@ -852,4 +915,84 @@ export default class PluginSample extends Plugin {
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
async currentDeviceInList() {
|
||||
try {
|
||||
var current_device_info = await this.fetchCurrentDeviceInfo();
|
||||
|
||||
var enableDeviceList = await this.settingUtils.get("enableDeviceList");
|
||||
var enableDeviceListArray = enableDeviceList.split("\n");
|
||||
|
||||
return enableDeviceListArray.includes(current_device_info);
|
||||
} catch (error) {
|
||||
console.error("Error checking if current device is enabled:", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fetchCurrentDeviceInfo(): Promise<string> {
|
||||
var current_device_uuid = window.siyuan.config.system.id;
|
||||
var current_device_name = window.siyuan.config.system.name;
|
||||
var current_device_info = current_device_uuid + " " + current_device_name;
|
||||
|
||||
return Promise.resolve(current_device_info.toString());
|
||||
}
|
||||
|
||||
|
||||
async appendCurrentDeviceIntoList() {
|
||||
try {
|
||||
// await!!!!!
|
||||
var current_device_info = await this.fetchCurrentDeviceInfo();
|
||||
|
||||
var enableDeviceList = this.settingUtils.get("enableDeviceList");
|
||||
var enableDeviceListArray = enableDeviceList.split("\n");
|
||||
var enableDeviceListArrayLength = enableDeviceListArray.length;
|
||||
var enableDeviceListArrayLast = enableDeviceListArray[enableDeviceListArrayLength - 1];
|
||||
|
||||
// remove empty line
|
||||
if (enableDeviceListArrayLast === "") {
|
||||
enableDeviceListArray.pop();
|
||||
}
|
||||
|
||||
enableDeviceListArray.push(current_device_info);
|
||||
|
||||
var enableDeviceListArrayString = enableDeviceListArray.join("\n");
|
||||
|
||||
this.settingUtils.assignValue("enableDeviceList", enableDeviceListArrayString);
|
||||
this.settingUtils.save();
|
||||
} catch (error) {
|
||||
console.error("Error appending current device into list:", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
async removeCurrentDeviceFromList() {
|
||||
|
||||
try {
|
||||
|
||||
var current_device_info = await this.fetchCurrentDeviceInfo();
|
||||
|
||||
var enableDeviceList = this.settingUtils.get("enableDeviceList");
|
||||
var enableDeviceListArray = enableDeviceList.split("\n");
|
||||
|
||||
// make sure visited the entire list
|
||||
for (var i = enableDeviceListArray.length - 1; i >= 0; i--) {
|
||||
var deviceInfo = enableDeviceListArray[i];
|
||||
|
||||
if (deviceInfo === current_device_info) {
|
||||
enableDeviceListArray.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// reassemble list
|
||||
var enableDeviceListArrayString = enableDeviceListArray.join("\n");
|
||||
|
||||
this.settingUtils.assignValue("enableDeviceList", enableDeviceListArrayString);
|
||||
this.settingUtils.save();
|
||||
} catch (error) {
|
||||
console.error("Error removing current device from list:", error);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -81,6 +81,14 @@ export class SettingUtils {
|
|||
}
|
||||
}
|
||||
|
||||
async assignValue(_key_: string, _value_: any) {
|
||||
let item = this.settings.get(_key_);
|
||||
item.value = _value_;
|
||||
this.plugin.data[this.name] = item.value;
|
||||
await this.save();
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将设置项目导出为 JSON 对象
|
||||
* @returns object
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue