Skip to content

Commit e49d7e0

Browse files
committed
feat (vs-code-ext): keybindings for the opening of the preview
1 parent 3e4a7aa commit e49d7e0

File tree

6 files changed

+43
-20
lines changed

6 files changed

+43
-20
lines changed

packages/vs-code-extension/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
66

77
## [Unreleased]
88

9-
- Initial release
9+
- Initial release

packages/vs-code-extension/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# react-email preview
22

33
A easy way to have a preview of an email written with react-email from your editor.
4-

packages/vs-code-extension/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@
1919
"contributes": {
2020
"commands": [
2121
{
22-
"command": "react-email-preview.preview",
23-
"title": "Live preview of email open"
22+
"command": "react-email-preview.open",
23+
"title": "Start live preview of email open"
24+
}
25+
],
26+
"keybindings": [
27+
{
28+
"command": "react-email-preview.open",
29+
"key": "shift+ctrl+e",
30+
"mac": "shift+cmd+e"
2431
}
2532
]
2633
},

packages/vs-code-extension/src/convertAllEmailAssetSourcesIntoWebviewURIs.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import * as vscode from 'vscode';
1+
import * as vscode from "vscode";
22

33
/**
4-
* @description Replaces all occurrences of src="/static/..." with a
4+
* @description Replaces all occurrences of src="/static/..." with a
55
* uri vscode will allow using because of their restrictness about accessing
66
* local files.
7-
*
7+
*
88
* @param originalHTML The HTML generated for the webview that will be used to find the src="/static/..."
99
* occurrences.
1010
* @param emailsDirVSCodeURI The path to the emails folder that the email is contained in
1111
* @param previewPanel The panel that the extensions is running the preview on
12-
*
12+
*
1313
* @returns A new version of the HTML with the proper static paths so the user can see them
1414
*/
1515
export function convertAllEmailAssetSourcesIntoWebviewURIs(
@@ -27,4 +27,4 @@ export function convertAllEmailAssetSourcesIntoWebviewURIs(
2727
return `src="${newSource}"`;
2828
},
2929
);
30-
}
30+
}

packages/vs-code-extension/src/extension.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,24 @@ export function activate(context: vscode.ExtensionContext) {
3737
), // the emails folder
3838
previewPanel,
3939
);
40-
}
40+
}
4141
// keeps the current content if the email is invalid and did not error
42-
// this invalidness can happen if the focused content is a image,
42+
// this invalidness can happen if the focused content is a image,
4343
// does not a export a default and for some other similar situations
4444
} catch (exception) {
45-
previewPanel.title = `react-email preview - error on the email ${basename(vscode.window.activeTextEditor.document.fileName)}`;
45+
previewPanel.title = `react-email preview - error on the email ${basename(
46+
vscode.window.activeTextEditor.document.fileName,
47+
)}`;
4648
let errorMessage: string;
4749
if (exception instanceof Error) {
4850
errorMessage = exception.stack ?? exception.message;
4951
} else {
5052
errorMessage = exception as string;
5153
}
52-
previewPanel.webview.html = emailWithErrorHTML.replace('{ERROR MESSAGE}', errorMessage);
54+
previewPanel.webview.html = emailWithErrorHTML.replace(
55+
"{ERROR MESSAGE}",
56+
errorMessage,
57+
);
5358
}
5459
} else if (previewPanel.webview.html.trim().length === 0) {
5560
previewPanel.title = `react-email preview - try opening an email!`;
@@ -59,7 +64,7 @@ export function activate(context: vscode.ExtensionContext) {
5964
};
6065

6166
let disposable = vscode.commands.registerCommand(
62-
"react-email-preview.preview",
67+
"react-email-preview.open",
6368
() => {
6469
previewPanel = vscode.window.createWebviewPanel(
6570
"react-email preview",
@@ -71,7 +76,10 @@ export function activate(context: vscode.ExtensionContext) {
7176
updatePreviewPanelContent();
7277

7378
vscode.workspace.onDidChangeTextDocument((ev) => {
74-
if (ev.document.fileName === vscode.window.activeTextEditor?.document.fileName) {
79+
if (
80+
ev.document.fileName ===
81+
vscode.window.activeTextEditor?.document.fileName
82+
) {
7583
updatePreviewPanelContent();
7684
}
7785
});

packages/vs-code-extension/src/renderOpenEmailFile.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export async function renderOpenEmailFile(
2929
return { valid: false };
3030
}
3131

32-
3332
const currentlyOpenTabFilePath = activeEditor.document.fileName; // actually a path not the name of the file
3433
const currentlyOpenTabFilename = basename(currentlyOpenTabFilePath, ".tsx");
3534

@@ -38,11 +37,20 @@ export async function renderOpenEmailFile(
3837

3938
// this is necessary so that we can still build things in a stable way
4039
// and have a up-to date version of the email preview on the extension
41-
const currentlyOpenTabFilesPathWithCurrentContents = join(emailsDirectory, `${currentlyOpenTabFilename}.vscpreview.tsx`);
40+
const currentlyOpenTabFilesPathWithCurrentContents = join(
41+
emailsDirectory,
42+
`${currentlyOpenTabFilename}.vscpreview.tsx`,
43+
);
4244
const currentContents = activeEditor.document.getText();
43-
await writeFile(currentlyOpenTabFilesPathWithCurrentContents, currentContents);
45+
await writeFile(
46+
currentlyOpenTabFilesPathWithCurrentContents,
47+
currentContents,
48+
);
4449

45-
const builtFileWithCurrentContents = join(previewDirectory, `${currentlyOpenTabFilename}.js`);
50+
const builtFileWithCurrentContents = join(
51+
previewDirectory,
52+
`${currentlyOpenTabFilename}.js`,
53+
);
4654

4755
try {
4856
await esbuild.build({
@@ -59,7 +67,8 @@ export async function renderOpenEmailFile(
5967
// we need to use require since it has a way to programatically invalidate its cache
6068
const email = require(builtFileWithCurrentContents);
6169

62-
if (typeof email.default === "undefined") { // this means there is no "export default ..." in the file
70+
if (typeof email.default === "undefined") {
71+
// this means there is no "export default ..." in the file
6372
return { valid: false };
6473
}
6574

0 commit comments

Comments
 (0)