Skip to content

Commit 3f29387

Browse files
committed
add css for webview
1 parent 510986d commit 3f29387

File tree

2 files changed

+66
-28
lines changed

2 files changed

+66
-28
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"onCommand:tcTerraform.refresh",
4343
"onCommand:tcTerraform.destroy",
4444
"onCommand:tcTerraformer.import",
45+
"onCommand:tcTerraform.doc.show",
4546
"workspaceContains:**/*.tf",
4647
"onLanguage:terraform"
4748
],
@@ -186,8 +187,8 @@
186187
"keybindings": [
187188
{
188189
"command": "tcTerraform.doc.show",
189-
"key": "ctrl+alt+d",
190-
"mac": "cmd+alt+d",
190+
"key": "ctrl+shift+d",
191+
"mac": "ctrl+shift+d",
191192
"when": "editorTextFocus"
192193
}
193194
],

src/autocomplete/TerraformResDocProvider.ts

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as _ from "lodash";
33
import * as fs from "fs";
44
import * as path from "path";
55
import { marked } from 'marked';
6-
import { dispose } from "vscode-extension-telemetry-wrapper";
76

87
export class TerraformResDocProvider {
98
// public provideDefinition(document: TextDocument, position: Position, token: CancellationToken): Definition {
@@ -24,38 +23,30 @@ export class TerraformResDocProvider {
2423
private readonly _extensionUri: vscode.Uri;
2524
private _disposables: vscode.Disposable[] = [];
2625
public static readonly viewType = 'tcTerraform.doc.show.id';
26+
private static tempFile: vscode.TextDocument | null = null;
2727

2828
public static async createOrShow(context: vscode.ExtensionContext, resType: string) {
29-
const column = vscode.window.activeTextEditor
30-
? vscode.window.activeTextEditor.viewColumn
31-
: undefined;
32-
33-
34-
const targetColumn = column + 1;
35-
const rightEditor = vscode.window.visibleTextEditors.find((editor) => editor.viewColumn === targetColumn);
36-
37-
let newEditor = rightEditor;
38-
39-
if (!newEditor) {
40-
// new editor to the right of the current editor
41-
const tempFile = await vscode.workspace.openTextDocument({ content: '', language: 'plaintext' });
42-
newEditor = await vscode.window.showTextDocument(tempFile, { viewColumn: targetColumn, preview: false });
43-
}
44-
45-
// If we already have a panel, show it.
29+
// If we already have a panel, clean it.
4630
if (TerraformResDocProvider.currentProvider) {
4731
// TerraformResDocProvider.currentProvider._panel.reveal(targetColumn);
4832
TerraformResDocProvider.currentProvider.dispose();
49-
return;
5033
}
5134

35+
const column = vscode.window.activeTextEditor
36+
? vscode.window.activeTextEditor.viewColumn
37+
: undefined;
38+
39+
const targetColumn = column + 1;
40+
// display the definition beside the current editor-begin++++++
5241
// Otherwise, create a new panel.
5342
const panel = vscode.window.createWebviewPanel(
5443
TerraformResDocProvider.viewType,
5544
`Doc Definition: ${resType}`,
56-
newEditor.viewColumn,
45+
targetColumn,
5746
getWebviewOptions(context.extensionUri),
5847
);
48+
// display the definition beside the current editor-end+++++++
49+
5950
// construct the _panel
6051
TerraformResDocProvider.currentProvider = new TerraformResDocProvider(panel, context.extensionUri);
6152
const current = TerraformResDocProvider.currentProvider;
@@ -64,6 +55,7 @@ export class TerraformResDocProvider {
6455
const markdownPath = path.join(docsRoot, `${mdResType}.html.markdown`);
6556
if (!fs.existsSync(markdownPath)) {
6657
console.error('Can not find the markdownFile: %s', markdownPath);
58+
TerraformResDocProvider.currentProvider.dispose();
6759
return;
6860
}
6961
const markdownFile = fs.readFileSync(markdownPath, 'utf8');
@@ -72,21 +64,25 @@ export class TerraformResDocProvider {
7264
try {
7365
const cleanedMarkdownFile = markdownFile.replace(/---[\s\S]*?---/, '');
7466
markdown = marked(cleanedMarkdownFile);
75-
current._panel.webview.html = markdown;
67+
const htmlMarkdown = htmlTemplate.replace('{{content}}', markdown);
68+
current._panel.webview.html = htmlMarkdown;
7669
} catch (error) {
7770
console.error('Error processing the Markdown file:', error);
7871
return;
7972
}
8073
// Listen for when the panel is disposed
81-
current._panel.onDidDispose(() => current.dispose(), null, current._disposables);
74+
current._panel.onDidDispose(() => {
75+
current.dispose();
76+
}, null, current._disposables);
8277
}
8378

8479
dispose() {
85-
TerraformResDocProvider.currentProvider = undefined;
86-
80+
// Close the WebviewPanel
81+
if (this._panel) {
82+
this._panel.dispose();
83+
}
8784
// Clean up our resources
88-
this._panel.dispose();
89-
85+
TerraformResDocProvider.currentProvider = undefined;
9086
while (this._disposables.length) {
9187
const x = this._disposables.pop();
9288
if (x) {
@@ -111,3 +107,44 @@ function getWebviewOptions(extensionUri: vscode.Uri): vscode.WebviewOptions {
111107
};
112108
}
113109

110+
const htmlTemplate = `
111+
<!DOCTYPE html>
112+
<html lang="en">
113+
<head>
114+
<meta charset="UTF-8">
115+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
116+
<style>
117+
body {
118+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
119+
font-size: 14px;
120+
line-height: 1.5;
121+
word-wrap: break-word;
122+
}
123+
h2 {
124+
font-size: 1.5em;
125+
font-weight: 600;
126+
padding-bottom: 0.3em;
127+
border-bottom: 1px solid #ccc;
128+
margin-bottom: 1em;
129+
}
130+
pre {
131+
background-color: #333;
132+
color: #fff; /* Change the font color to white for higher contrast */
133+
padding: 16px;
134+
overflow: auto;
135+
font-size: 100%;
136+
line-height: 1.45;
137+
border-radius: 3px;
138+
}
139+
pre code.hcl {
140+
background-color: transparent; /* Set the background color to transparent */
141+
color: #fff;
142+
}
143+
</style>
144+
</head>
145+
<body>
146+
{{content}}
147+
</body>
148+
</html>
149+
`;
150+

0 commit comments

Comments
 (0)