-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
157 lines (136 loc) · 4.95 KB
/
Copy pathextension.js
File metadata and controls
157 lines (136 loc) · 4.95 KB
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
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');
const VIEW_TYPE = 'mtEditor.text';
class MtCustomDocument {
constructor(uri, initialText) {
this.uri = uri;
this._text = initialText;
}
getText() {
return this._text;
}
updateText(text) {
this._text = text;
}
}
class MtEditorProvider {
constructor(context) {
this._context = context;
this._documents = new Map();
this._onDidChangeCustomDocument = new vscode.EventEmitter();
this.onDidChangeCustomDocument = this._onDidChangeCustomDocument.event;
}
static register(context) {
const provider = new MtEditorProvider(context);
context.subscriptions.push(
vscode.window.registerCustomEditorProvider(VIEW_TYPE, provider, {
webviewOptions: { retainContextWhenHidden: true },
supportsMultipleEditorsPerDocument: false
})
);
return provider;
}
async openCustomDocument(uri, _openContext, _token) {
let text = '';
try {
const data = await vscode.workspace.fs.readFile(uri);
text = Buffer.from(data).toString('utf8');
} catch (err) {
// 文件不存在或不可读,按空文档处理
}
const doc = new MtCustomDocument(uri, text);
this._documents.set(uri.toString(), doc);
return doc;
}
async resolveCustomEditor(document, webviewPanel, _token) {
const webview = webviewPanel.webview;
const webviewDir = path.join(this._context.extensionPath, 'webview');
webview.options = {
enableScripts: true,
localResourceRoots: [vscode.Uri.file(webviewDir)]
};
const appUri = webview.asWebviewUri(vscode.Uri.file(path.join(webviewDir, 'app.js')));
const pixiUri = webview.asWebviewUri(vscode.Uri.file(path.join(webviewDir, 'vendor', 'pixi.min.js')));
const styleUri = webview.asWebviewUri(vscode.Uri.file(path.join(webviewDir, 'style.css')));
const iconsDir = webview.asWebviewUri(vscode.Uri.file(path.join(webviewDir, 'icons')));
let html = fs.readFileSync(path.join(webviewDir, 'index.html'), 'utf8');
html = html.replace(/__CSP_SOURCE__/g, webview.cspSource)
.replace('./style.css', styleUri.toString())
.replace('./app.js', appUri.toString())
.replace('./vendor/pixi.min.js', pixiUri.toString())
.replace(/\.\/icons\//g, iconsDir.toString() + '/');
webview.html = html;
webview.onDidReceiveMessage(async (msg) => {
switch (msg && msg.type) {
case 'ready':
webview.postMessage({ type: 'load', text: document.getText() });
break;
case 'save':
await this._save(document, msg.text, webview);
break;
case 'openFile':
vscode.window.showInformationMessage('请在 VSCode 资源管理器中打开 .mt 文件');
break;
case 'openText':
vscode.commands.executeCommand('vscode.openWith', document.uri, 'default');
break;
}
});
}
async _save(document, text, webview) {
if (text !== document.getText()) {
document.updateText(text);
this._onDidChangeCustomDocument.fire({
document,
undo: () => {},
redo: () => {}
});
}
try {
await this.saveCustomDocument(document, new vscode.CancellationTokenSource().token);
webview.postMessage({ type: 'saved' });
} catch (err) {
webview.postMessage({ type: 'saveError', message: String((err && err.message) || err) });
}
}
async saveCustomDocument(document, _token) {
await vscode.workspace.fs.writeFile(document.uri, Buffer.from(document.getText(), 'utf8'));
}
async saveCustomDocumentAs(document, destination, _token) {
await vscode.workspace.fs.writeFile(destination, Buffer.from(document.getText(), 'utf8'));
}
async revertCustomDocument(document, _token) {
const data = await vscode.workspace.fs.readFile(document.uri);
document.updateText(Buffer.from(data).toString('utf8'));
this._documents.set(document.uri.toString(), document);
}
async backupCustomDocument(document, context, _token) {
await vscode.workspace.fs.writeFile(context.destination, Buffer.from(document.getText(), 'utf8'));
return {
id: document.uri.toString(),
delete: () => {}
};
}
}
function activate(context) {
MtEditorProvider.register(context);
context.subscriptions.push(
vscode.commands.registerCommand('mtEditor.openWithEditor', (uri) => {
const target = uri || (vscode.window.activeTextEditor && vscode.window.activeTextEditor.document.uri);
if (!target) {
return;
}
vscode.commands.executeCommand('vscode.openWith', target, VIEW_TYPE);
}),
vscode.commands.registerCommand('mtEditor.openAsText', (uri) => {
const target = uri || (vscode.window.activeTextEditor && vscode.window.activeTextEditor.document.uri);
if (!target) {
return;
}
vscode.commands.executeCommand('vscode.openWith', target, 'default');
})
);
}
function deactivate() {}
module.exports = { activate, deactivate };