Skip to content

Commit c64a376

Browse files
committed
Improvements for #5 and some logging
1 parent 7dbbdfc commit c64a376

File tree

4 files changed

+89
-9
lines changed

4 files changed

+89
-9
lines changed

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@
5656
"properties": {
5757
"resx-editor.experimentalDelete": {
5858
"type": "boolean",
59-
"default": false,
59+
"default": true,
6060
"description": "Enable experimental delete functionality"
61+
},
62+
"resx-editor.verboseLogging": {
63+
"type": "boolean",
64+
"default": false,
65+
"description": "Enable verbose logging"
6166
}
6267
}
6368
},
@@ -67,6 +72,12 @@
6772
"command": "resx-editor.deleteResource",
6873
"when": "config.resx-editor.experimentalDelete == true && webviewId == 'resx-editor.editor'"
6974
}
75+
],
76+
"commandPalette": [
77+
{
78+
"command": "resx-editor.deleteResource",
79+
"when": "false"
80+
}
7081
]
7182
},
7283
"commands": [

src/extension.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
import * as vscode from 'vscode';
22
import { ResxProvider } from './resxProvider';
33

4+
let outputChannel: vscode.OutputChannel;
45

56
export function activate(context: vscode.ExtensionContext) {
67

8+
outputChannel = vscode.window.createOutputChannel("ResX Editor");
9+
10+
printChannelOutput("ResX Editor extension activated.", true);
11+
712
context.subscriptions.push(ResxProvider.register(context));
813

914
}
1015

16+
/**
17+
* Prints the given content on the output channel.
18+
*
19+
* @param content The content to be printed.
20+
* @param reveal Whether the output channel should be revealed.
21+
*/
22+
export const printChannelOutput = (content: string, verbose: boolean, reveal = false): void => {
23+
24+
// do not throw on logging, just log to console in the event of an error
25+
try {
26+
if (!outputChannel) {
27+
return;
28+
}
29+
// if it is verbose logging and verbose is not enabled, return
30+
if (verbose && !vscode.workspace.getConfiguration("resx-editor").get("verboseLogging")) {
31+
return;
32+
}
33+
34+
const timestamp = new Date().toISOString();
35+
const fileName = vscode.window.activeTextEditor?.document.fileName;
36+
37+
outputChannel.appendLine(`[${timestamp}] [${fileName}] ${content}`);
38+
39+
if (reveal) {
40+
outputChannel.show(true);
41+
}
42+
}
43+
catch (e)
44+
{
45+
console.log(e);
46+
}
47+
};
48+
1149
export function deactivate() {}

src/resxProvider.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import * as path from 'path';
22
import * as vscode from 'vscode';
33
import resx from 'resx';
44
import { getNonce } from './utilities/getNonce';
5+
import { printChannelOutput } from './extension';
56

67
export class ResxProvider implements vscode.CustomTextEditorProvider {
78

89
public static register(context: vscode.ExtensionContext): vscode.Disposable {
910
const provider = new ResxProvider(context);
1011
const providerRegistration = vscode.window.registerCustomEditorProvider(ResxProvider.viewType, provider);
12+
printChannelOutput("ResX Editor custom editor provider registered.", true);
1113
return providerRegistration;
1214
}
1315

1416
private static readonly viewType = 'resx-editor.editor';
1517
private registered = false;
16-
18+
private currentPanel: vscode.WebviewPanel | undefined = undefined;
19+
1720
constructor(
1821
private readonly context: vscode.ExtensionContext
1922
) { }
@@ -23,20 +26,32 @@ export class ResxProvider implements vscode.CustomTextEditorProvider {
2326
webviewPanel: vscode.WebviewPanel,
2427
_token: vscode.CancellationToken
2528
): Promise<void> {
29+
this.currentPanel = webviewPanel;
2630
webviewPanel.webview.options = {
2731
enableScripts: true,
2832
localResourceRoots: [vscode.Uri.joinPath(this.context.extensionUri, 'out')]
2933
};
3034
webviewPanel.webview.html = this._getWebviewContent(webviewPanel.webview);
3135

32-
let disposable = vscode.commands.registerCommand('resx-editor.deleteResource', () => {
36+
try
37+
{
38+
if (!this.registered) {
39+
printChannelOutput("deleteResource command registered", true);
40+
this.registered = true;
41+
let disposable = vscode.commands.registerCommand('resx-editor.deleteResource', () => {
3342

34-
webviewPanel.webview.postMessage({
35-
type: 'delete'
43+
this.currentPanel?.webview.postMessage({
44+
type: 'delete'
45+
});
3646
});
37-
});
3847

39-
this.context.subscriptions.push(disposable);
48+
this.context.subscriptions.push(disposable);
49+
}
50+
}
51+
catch (e)
52+
{
53+
console.log(e);
54+
}
4055

4156
async function updateWebview() {
4257
webviewPanel.webview.postMessage({
@@ -60,6 +75,9 @@ export class ResxProvider implements vscode.CustomTextEditorProvider {
6075
case 'update':
6176
this.updateTextDocument(document, e.json);
6277
return;
78+
case 'log':
79+
printChannelOutput(e.message, true);
80+
return;
6381
}
6482
});
6583

@@ -87,6 +105,10 @@ export class ResxProvider implements vscode.CustomTextEditorProvider {
87105
<head>
88106
<meta charset="UTF-8">
89107
<meta name="viewport" content="width=device-width, initial-scale=1.0">
108+
<meta
109+
http-equiv="Content-Security-Policy"
110+
content="default-src 'none'; img-src ${webview.cspSource} https:; script-src ${webview.cspSource}; style-src ${webview.cspSource};script-src 'nonce-${nonce}';"
111+
/>
90112
</head>
91113
<body>
92114
<vscode-data-grid id="resource-table" aria-label="Basic" generate-header="sticky" aria-label="Sticky Header"></vscode-data-grid>

src/webview/webview.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ let currentRowData = null;
2121
currentRowData = sourceElement._rowData;
2222

2323
if (sourceElement && sourceElement.className !== "column-header") {
24+
2425
const handleChange = (target) => {
2526
const column = target._columnDefinition;
2627
const originalRow = target._rowData;
2728
const originalValue = originalRow[column.columnDataKey];
2829
const newValue = target.innerText;
30+
2931

3032
if (originalValue !== newValue) {
33+
sendLog("Value changed...Original value: " + originalValue + "; " + "New value: " + newValue);
3134
target._rowData[column.columnDataKey] = newValue;
3235
refreshResxData();
3336
}
@@ -69,8 +72,7 @@ let currentRowData = null;
6972

7073
return;
7174
case 'delete':
72-
console.log("delete");
73-
console.log(currentRowData);
75+
sendLog("Deleting row: " + JSON.stringify(currentRowData));
7476
if (currentRowData) {
7577
const index = table.rowsData.indexOf(currentRowData);
7678
if (index > -1) {
@@ -98,6 +100,13 @@ let currentRowData = null;
98100
});
99101
}
100102

103+
function sendLog(message) {
104+
vscode.postMessage({
105+
type: 'log',
106+
message: message
107+
});
108+
}
109+
101110
function updateContent(/** @type {string} **/ text) {
102111
if (text) {
103112

0 commit comments

Comments
 (0)