This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
145 lines (127 loc) · 4.22 KB
/
extension.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const {
window,
workspace,
commands
} = require('vscode');
// 引入api
const mddocs = require("./lib/mddocs");
const upload = require("./lib/upload");
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "mddocs" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
// 发布文章
let publishMd = commands.registerCommand('extension.publishMd', function () {
// 验证
if (!validate()) {
return;
}
let docs = new mddocs.MdDocs();
let data = docs.validateData(false);
if (data != null) {
let config = workspace.getConfiguration('mddocs');
docs.request(config.publishUri, data);
}
});
context.subscriptions.push(publishMd);
// 编辑文章
let updateMd = commands.registerCommand('extension.updateMd', function () {
// 验证
if (!validate()) {
return;
}
let docs = new mddocs.MdDocs();
let data = docs.validateData(true);
if (data != null) {
let config = workspace.getConfiguration('mddocs');
docs.request(config.modifyUri, data);
}
});
context.subscriptions.push(updateMd);
// 删除文章
let deleteMd = commands.registerCommand('extension.deleteMd', function () {
// 验证
if (!validate()) {
return;
}
let docs = new mddocs.MdDocs();
let data = docs.validateData(true);
if (data != null) {
let config = workspace.getConfiguration('mddocs');
docs.request(config.deleteUri, data);
}
});
context.subscriptions.push(deleteMd);
// 搜索文章
let searchMd = commands.registerCommand('extension.searchMd', function () {
// 验证
if (!validate()) {
return;
}
window.showInputBox({
placeHolder: "输入文档标题搜索"
}).then(function (keyword) {
if (keyword == undefined || keyword.length <= 0) {
window.showWarningMessage("输入点什么");
return;
}
let data = {
"keyword": keyword
};
if (data != null) {
let config = workspace.getConfiguration('mddocs');
new mddocs.MdDocs().request(config.searchUri, data);
}
});
});
context.subscriptions.push(searchMd);
// 上传图片
let uploadImg = commands.registerCommand('extension.uploadImg', function () {
// 验证
if (!validate()) {
return;
}
// @ts-ignore
window.showOpenDialog({
canSelectMany: false,
filters: {
'Images': ['png', 'jpeg', 'jpg', 'gif', 'bmp']
}
}).then(uri => {
if (uri) {
new upload.Upload().uploadImage(uri[0].fsPath);
}
});
});
context.subscriptions.push(uploadImg);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
exports.deactivate = deactivate;
/**
* 数据验证
*/
function validate() {
let config = workspace.getConfiguration('mddocs');
if(!config.enable){
window.showWarningMessage("插件未启用");
return false;
}
if (!window.activeTextEditor) {
window.showWarningMessage("未打开编辑器");
return false;
}
if ("markdown" != window.activeTextEditor.document.languageId) {
window.showWarningMessage("不支持的文档类型");
return false;
}
return true;
}