-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
149 lines (131 loc) · 3.9 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
const vscode = require("vscode");
const data = require("./assets/data");
const path = require("path");
const docs = {
boring: "https://docs.sailscasts.com/boring-stack/getting-started",
sails: "https://sailsjs.com/documentation/concepts",
};
/**
* @param {vscode.ExtensionContext} context
*/
class Provider {
provideHover(document, position) {
const wordRange = document.getWordRangeAtPosition(position);
const word = wordRange ? document.getText(wordRange) : "";
const info = data[word] || "";
const md = new vscode.MarkdownString(info);
return new vscode.Hover(md);
}
}
function activate(context) {
let currentPanel = undefined;
let sailsPanel = undefined;
const panelOptions = {
enableScripts: true,
retainContextWhenHidden: true,
};
context.subscriptions.push(
vscode.commands.registerCommand("boringdocs.tbjs", function () {
const columnToShowIn = vscode.window.activeTextEditor
? vscode.window.activeTextEditor.viewColumn
: undefined;
if (currentPanel) {
currentPanel.reveal(columnToShowIn);
} else {
currentPanel = vscode.window.createWebviewPanel(
"boringdocs",
"Boring Stack Docs",
columnToShowIn || vscode.ViewColumn.One,
panelOptions
);
}
currentPanel.webview.html = getWebContent("boring");
currentPanel.iconPath = vscode.Uri.joinPath(
context.extensionUri,
"assets",
"boring.png"
);
currentPanel.onDidDispose(
() => {
currentPanel = undefined;
},
null,
context.subscriptions
);
vscode.window.showInformationMessage(
"You are now viewing the boring docs"
);
})
);
context.subscriptions.push(
vscode.commands.registerCommand("boringdocs.sails", function () {
const columnToShowIn = vscode.window.activeTextEditor
? vscode.window.activeTextEditor.viewColumn
: undefined;
if (sailsPanel) {
sailsPanel.reveal(columnToShowIn);
} else {
sailsPanel = vscode.window.createWebviewPanel(
"boringdocs",
"Sailsjs Docs",
columnToShowIn || vscode.ViewColumn.One,
panelOptions
);
}
sailsPanel.webview.html = getWebContent("sails", "white");
sailsPanel.iconPath = vscode.Uri.joinPath(
context.extensionUri,
"assets",
"sails.png"
);
sailsPanel.onDidDispose(
() => {
sailsPanel = undefined;
},
null,
context.subscriptions
);
vscode.window.showInformationMessage(
"You are now viewing the sails docs"
);
})
);
context.subscriptions.push(
vscode.languages.registerHoverProvider("javascript", new Provider())
);
}
function deactivate() {}
function getWebContent(doc, color) {
return ` <!DOCTYPE html>
<html lang="en">
<body style="width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden;">
<iframe
src="${docs[doc]}"
id="myIframe"
frameborder="0"
style="width:100vw; height:100vh; margin:0; padding:0; background:${color}"
></iframe>
<script>
// Function to cache content in localStorage
function cacheContent() {
const iframe = document.getElementById('myIframe');
const cachedContent = iframe.contentDocument.body.innerHTML;
localStorage.setItem('cachedContent', cachedContent);
}
// Load cached content if available
window.onload = function() {
const cachedContent = localStorage.getItem('cachedContent');
if (cachedContent) {
const iframe = document.getElementById('myIframe');
iframe.contentDocument.body.innerHTML = cachedContent;
}
};
</script>
</body>
</html>`;
}
module.exports = {
activate,
deactivate,
Provider,
};