-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
108 lines (88 loc) · 3.3 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
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
function activate(context) {
console.log("Extension 'simple-fits-viewer' is now active!");
context.subscriptions.push(
vscode.window.registerCustomEditorProvider('fitFileViewer', new FITSFileEditor(context))
);
}
class FITSFileDocument {
constructor(uri) {
this.uri = uri;
}
}
class FITSFileEditor {
constructor(context) {
this.context = context;
}
openCustomDocument(uri, openContext, token) {
return new FITSFileDocument(uri);
}
async resolveCustomEditor(document, webviewPanel, token) {
// Set up the webview content
webviewPanel.webview.options = {
enableScripts: true,
localResourceRoots: [
vscode.Uri.file(path.dirname(document.uri.fsPath))
]
};
// Function to update webview content
const updateWebview = () => {
try {
// Step 1: Update the webview content
webviewPanel.webview.html = this.getWebviewContent();
// Step 2: Send data to webview (SLOW?)
webviewPanel.webview.onDidReceiveMessage(
message => {
if (message.command === 'ready') {
// Convert the document URI to a webview URI
const fitsFileUri = webviewPanel.webview.asWebviewUri(document.uri);
// Send the data to the webview
webviewPanel.webview.postMessage({
command: 'loadData',
fileUri: fitsFileUri.toString()
});
}
},
undefined,
this.context.subscriptions
);
} catch (error) {
console.log(error);
vscode.window.showErrorMessage(`Error reading FITS file: ${error.message}`);
}
};
// Initial update
updateWebview();
// Handle document changes
const changeDocumentSubscription = vscode.workspace.onDidChangeTextDocument(event => {
if (event.document.uri.toString() === document.uri.toString()) {
updateWebview();
}
});
// Clean up subscription when panel is disposed
webviewPanel.onDidDispose(() => {
changeDocumentSubscription.dispose();
});
}
getWebviewContent() {
const filePath = path.join(__dirname, 'webview.html');
let content = fs.readFileSync(filePath, 'utf8');
// Attach utils.js
const utilsPath = path.join(__dirname, 'utils.js');
const utilsContent = fs.readFileSync(utilsPath, 'utf8');
// Attach styles.css
const stylePath = path.join(__dirname, 'style.css');
const styleContent = fs.readFileSync(stylePath, 'utf8');
// Inject utils.js and styles.css content into the webview HTML
content = content.replace('</body>', `<script>${utilsContent}</script><style>${styleContent}</style></body>`);
return content;
}
}
exports.activate = activate;
function deactivate() { }
module.exports = {
activate,
deactivate
}