Skip to content

Commit a1daec3

Browse files
authored
Watch for file changes (#7)
* Watch for file changes * Display error messages in the window * Update changelog
1 parent 9cb034c commit a1daec3

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
- Added file system watcher to monitor for changes to the current document.
6+
This allows the preview to be automatically updated whenever the ZPL file is changed.
7+
- Improved error handling. If an error occurs, an error message is displayed to
8+
the user.
9+
510
## [0.1.0]
611

712
- Included a GIF into README.md.

src/labelary.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ export function zplToPng(zpl: string, dpmm: number): Promise<string | void> {
99
responseType: 'arraybuffer',
1010
}
1111
)
12-
.then((res) => Buffer.from(res.data).toString('base64'))
13-
.catch((err) => console.log('err', err));
12+
.then((res) => Buffer.from(res.data).toString('base64'));
1413
}

src/zplProvider.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export class ZPLEditorProvider implements vscode.CustomTextEditorProvider {
2525
webviewPanel: vscode.WebviewPanel,
2626
token: vscode.CancellationToken
2727
): void | Thenable<void> {
28+
vscode.workspace
29+
.createFileSystemWatcher(document.uri.fsPath)
30+
.onDidChange(() => {
31+
this.updateWebview(document, webviewPanel);
32+
});
33+
2834
webviewPanel.webview.options = {
2935
enableScripts: true,
3036
};
@@ -126,15 +132,19 @@ export class ZPLEditorProvider implements vscode.CustomTextEditorProvider {
126132
document: vscode.TextDocument,
127133
webviewPanel: vscode.WebviewPanel
128134
): Promise<void> {
129-
return zplToPng(document.getText(), this.dpmm).then(
130-
(label: string | void) => {
135+
return zplToPng(document.getText(), this.dpmm)
136+
.then((label: string | void) => {
131137
if (label) {
132138
webviewPanel.webview.html = this.getHtmlForWebview(
133139
webviewPanel.webview,
134140
label
135141
);
136142
}
137-
}
138-
);
143+
})
144+
.catch((err: Error) => {
145+
vscode.window.showErrorMessage(
146+
`Error rendering ZPL preview: ${err.message}`
147+
);
148+
});
139149
}
140150
}

0 commit comments

Comments
 (0)