forked from sunweixin8/SuperFileNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
241 lines (219 loc) · 7.04 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const vscode = require('vscode');
const RenderNote = require('./utils/setNote.js');
const delNote = require('./utils/delNote.js');
const getNote = require('./utils/getNote.js');
const writeFile = require('./utils/writeFile.js');
const historyNote = require('./utils/historyNote.js');
const $config = require('./utils/config.js');
function alertAndCopy(message) {
vscode.window.showInformationMessage(message, { modal: true });
}
async function activate(context) {
// 存储对象列表
let disposedList = [];
// 项目路径
const projectPath = $config.projectPathFull;
// 打开项目,读取历史
try {
let historyList = await historyNote();
// console.log(historyList);
historyList.forEach((x) => {
let registration = RenderNote(context, x);
disposedList.push({
path: x.path,
registration: registration,
});
});
} catch (error) {
console.log('文件备注插件:恢复备注时出现了异常', error);
}
// 注册命令 'addRemark'
let addRemark = vscode.commands.registerCommand('addRemark', async (uri) => {
console.clear();
// 获取当前活动的文本编辑器,如果有文本编辑器,获取其 URI;(快捷键触发)
const activeEditor = vscode.window.activeTextEditor;
if (!uri && activeEditor) {
uri = activeEditor.document.uri;
}
// 如果没有选择文件或文件夹,显示错误信息
if (!uri || !uri.fsPath) {
return;
}
// 获取当前点击的绝对路径
// const absolutePath = uri.fsPath;
// 获取当前点击的相对路径
const relativePath = vscode.workspace.asRelativePath(uri.fsPath);
// console.table({
// 项目路径: projectPath,
// 当前点击的绝对路径: absolutePath,
// 当前点击的相对路径: relativePath,
// });
if (projectPath == relativePath) {
vscode.window.showInformationMessage('不能在根目录备注');
return;
}
// 显示输入框,等待用户输入
const userInput = await vscode.window.showInputBox({
placeHolder: `给【${relativePath}】输入备注...`,
prompt: '请输入:',
value: '', // 初始值为空
});
// 检查用户是否输入了内容
if (userInput == undefined) {
// 用户取消了输入
console.log('文件备注插件:用户取消了输入');
// vscode.window.showInformationMessage('取消输入');
return;
}
let createTime = new Date().toLocaleString().substring(2);
let INFO = {
text: userInput,
path: relativePath,
time: createTime,
};
//设置单条
let registration = RenderNote(context, INFO);
// 存储
writeFile(INFO);
disposedList.push({
path: relativePath,
registration: registration,
});
});
// 注册命令 'delRemark'
let delRemark = vscode.commands.registerCommand('delRemark', async (uri) => {
// 如果没有选择文件或文件夹,显示错误信息
if (!uri || !uri.fsPath) {
return;
}
// // 获取文件信息
// const fileInfo = await vscode.workspace.fs.stat(uri);
// const fileInfoTypeName = fileInfo.type ==1?'文件':'文件夹';
// 获取当前点击的绝对路径
// const absolutePath = uri.fsPath;
// 获取当前点击的相对路径
const relativePath = vscode.workspace.asRelativePath(uri.fsPath);
// console.table({
// 项目路径: projectPath,
// 当前点击的绝对路径: absolutePath,
// 当前点击的相对路径: relativePath,
// });
if (projectPath == relativePath) {
vscode.window.showInformationMessage('不能在根目录备注');
return;
}
// 弹出确认框
const response = await vscode.window.showInformationMessage(`确定要删除【${relativePath}】的所有备注?`, { modal: true }, '确定');
if (response === '确定') {
delNote(relativePath, disposedList);
vscode.window.showInformationMessage('已清除');
}
});
// 注册命令 'viewRemark'
let viewRemark = vscode.commands.registerCommand('viewRemark', async (uri) => {
// 如果没有选择文件或文件夹,显示错误信息
if (!uri || !uri.fsPath) {
return;
}
// 获取当前点击的相对路径
const relativePath = vscode.workspace.asRelativePath(uri.fsPath);
if (projectPath == relativePath) {
vscode.window.showInformationMessage('不能在根目录备注');
return;
}
let html = '';
let historyList = await historyNote();
historyList.forEach((x, i) => {
if (x.path == relativePath) {
html += `
\r\n♐备注: ${x.text}
♐备注时间: ${x.time}
`;
}
});
// 如果为空
if (html.length == 0) {
html = `【${relativePath}】未设置备注`;
} else {
html = `【${relativePath}】共${historyList.length}条备注\r\n` + html;
}
// vscode.window.showInformationMessage(html, { modal: true });
alertAndCopy(html);
});
// 注册命令 'viewAllRemark'
let viewAllRemark = vscode.commands.registerCommand('viewAllRemark', async (uri) => {
// 如果没有选择文件或文件夹,显示错误信息
if (!uri || !uri.fsPath) {
return;
}
let html = '';
let historyList = await historyNote();
historyList.forEach((x, i) => {
html += `${i + 1}.路径:${x.path}--备注:${x.text}\r\r`;
});
alertAndCopy(html);
});
// 注册文件重命名事件监听器
const renameFilesDisposable = vscode.workspace.onDidRenameFiles((event) => {
// 处理文件重命名事件
const renamedFiles = event.files;
renamedFiles.forEach(async (x) => {
let newPath = x.newUri.fsPath.replace(projectPath, '').replace(/\\/g, '/');
let oldPath = x.oldUri.fsPath.replace(projectPath, '').replace(/\\/g, '/');
console.log('监听到了移动', { newPath, oldPath });
let fileStat = await vscode.workspace.fs.stat(x.newUri);
let isDir = fileStat.type === vscode.FileType.Directory;
console.log('是否是文件夹', isDir);
if (isDir) {
let historyList = await historyNote();
historyList.forEach(async (x, i) => {
// ==0,才是正确的路径
if (x.path.indexOf(oldPath) == 0) {
let newNoteInfo = {
text: x.text,
path: x.path.replace(oldPath, newPath),
time: x.time,
};
//设置新的
let registration = RenderNote(context, newNoteInfo);
await writeFile(newNoteInfo);
disposedList.push({
path: newPath,
registration: registration,
});
// 删除老的
await delNote(x.path, disposedList);
}
});
} else {
let oldNoteList = getNote(oldPath);
// console.log('读取到了老的', oldNoteList);
oldNoteList.forEach(async (x) => {
let newNoteInfo = {
text: x.text,
path: newPath,
time: x.time,
};
// console.log({ newNoteInfo });
//设置新的
let registration = RenderNote(context, newNoteInfo);
await writeFile(newNoteInfo);
disposedList.push({
path: newPath,
registration: registration,
});
});
// 删除老的
await delNote(oldPath, disposedList);
}
});
});
// 将命令注册到上下文中
context.subscriptions.push(addRemark, delRemark, viewRemark, viewAllRemark, renameFilesDisposable);
}
//当你的扩展被停用时,这个方法被调用
function deactivate() {}
module.exports = {
activate,
deactivate,
};