-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
122 lines (116 loc) · 3.12 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* @Author: luhaifeng666 youzui@hotmail.com
* @Date: 2022-08-09 11:38:39
* @LastEditors: luhaifeng666
* @LastEditTime: 2022-10-26 18:41:04
* @Description:
*/
import { Plugin } from "obsidian";
import { TranslatorSettingTab } from "./settings";
import { TranslatorModal } from "./modals";
import { noticeHandler, cleanMarkup } from "./utils";
import { TranslatorSetting } from "./interfaces";
const DEFAULT_SETTINGS: TranslatorSetting = {
// Youdao Settings
youdaoEnable: false,
appId: "",
secretKey: "",
yFrom: "",
yTo: "",
audio: false,
// Microsoft Settings
microsoftEnable: false,
microsoftSecretKey: "",
microsoftLocation: "",
mFrom: "",
mTo: "",
// Baidu Settings
baiduEnable: false,
baiduSecretKey: "",
baiduAppId: "",
bFrom: "",
bTo: "",
};
type Config = keyof TranslatorSetting;
export default class TranslatorPlugin extends Plugin {
settings: TranslatorSetting = DEFAULT_SETTINGS;
async onload() {
// load settings
await this.loadSettings();
// add setting tab
this.addSettingTab(new TranslatorSettingTab(this.app, this));
// add ribbon icon
this.addRibbonIcon("book", "Translate", () => {
// @ts-ignore
this.app.commands.executeCommandById("obsidian-translator:translate");
});
// validator
const validator = () => {
const {
youdaoEnable,
appId,
secretKey,
baiduAppId,
baiduEnable,
baiduSecretKey,
microsoftEnable,
microsoftLocation,
microsoftSecretKey,
} = this.settings;
const getKeys = (obj: { [name: string]: string }): string[] => {
return Object.keys(obj).filter((key: string) => !obj[key]);
};
const getRes = (
enable: boolean,
idOrLocation: string,
key: string
): boolean => (enable && !!idOrLocation && !!key) || !enable;
const validateFailedList = [
...(getRes(youdaoEnable, appId, secretKey)
? []
: getKeys({ appId, secretKey })),
...(getRes(baiduEnable, baiduAppId, baiduSecretKey)
? []
: getKeys({ baiduAppId, baiduSecretKey })),
...(getRes(microsoftEnable, microsoftLocation, microsoftSecretKey)
? []
: getKeys({ microsoftLocation, microsoftSecretKey })),
];
return validateFailedList;
};
// add command
this.addCommand({
id: "translate",
name: "translate",
editorCallback: (editor) => {
const { settings } = this;
const enableKeys = Object.keys(settings).filter(
(key) =>
key.toLowerCase().includes("enable") &&
settings[key as keyof TranslatorSetting]
);
if (enableKeys.length) {
const messages = validator();
if (!messages.length) {
const sel = cleanMarkup(editor.getSelection())
.replace(/[^\w\s]/gi, " ")
.trim();
new TranslatorModal(this.app, sel, settings).open();
} else {
noticeHandler(`${messages.join(", ")} can not be empty!`);
}
}
},
});
}
async loadSettings() {
const settings = await this.loadData();
this.settings = {
...DEFAULT_SETTINGS,
...(settings || {}),
};
}
async saveSettings() {
await this.saveData(this.settings);
}
}