Skip to content

Commit 4712aff

Browse files
authored
Merge pull request #845 from Lemoncode/dev
Merge to Main
2 parents 791f439 + cfee516 commit 4712aff

13 files changed

Lines changed: 114 additions & 20 deletions

File tree

apps/web/src/pods/toolbar/components/new-button/new-button.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { NewIcon } from '#common/components/icons/new-button.components';
2-
import classes from '#pods/toolbar/toolbar.pod.module.css';
2+
import { isVSCodeEnv } from '#common/utils/env.utils';
3+
import { sendToExtension } from '#common/utils/vscode-bridge.utils';
34
import { useCanvasContext } from '#core/providers';
4-
import { ToolbarButton } from '../toolbar-button';
5+
import classes from '#pods/toolbar/toolbar.pod.module.css';
6+
import { APP_MESSAGE_TYPE } from '@lemoncode/quickmock-bridge-protocol';
57
import { SHORTCUTS } from '../../shortcut/shortcut.const';
8+
import { ToolbarButton } from '../toolbar-button';
69

710
export const NewButton = () => {
811
const { createNewFullDocument: clearCanvas } = useCanvasContext();
912

1013
const handleClick = () => {
14+
if (isVSCodeEnv()) {
15+
sendToExtension({ type: APP_MESSAGE_TYPE.NEW_FILE });
16+
return;
17+
}
1118
clearCanvas();
1219
};
1320

packages/bridge-protocol/src/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export const APP_MESSAGE_TYPE = {
1010
SAVE: 'qm:save',
1111
RENDER_COMPLETE: 'qm:render-complete',
1212
WEBVIEW_READY: 'WEBVIEW_READY',
13+
NEW_FILE: 'qm:new-file',
1314
} as const;

packages/bridge-protocol/src/model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export type AppMessage =
3434
| {
3535
type: typeof APP_MESSAGE_TYPE.RENDER_COMPLETE;
3636
payload?: ContentBbox;
37-
};
37+
}
38+
| { type: typeof APP_MESSAGE_TYPE.NEW_FILE };
3839

3940
export type PayloadOf<U extends { type: string }, T extends U['type']> =
4041
Extract<U, { type: T }> extends { payload: infer P } ? P : undefined;

packages/vscode-extension/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# @lemoncode/quickmock-vscode-extension
22

3+
## 0.2.0
4+
5+
### Minor Changes
6+
7+
- 3cd4892: Toolbar's **New** button now creates a real `.qm` file when running inside the VS Code extension instead of just clearing the canvas.
8+
9+
### Patch Changes
10+
11+
- b7f9cf1: Fix editor failing to load files opened from outside the workspace. The webview HTML was assigned before registering `onDidReceiveMessage`, causing a race where the initial `READY` / `WEBVIEW_READY` message from the app could be lost and the file content never delivered. The listener is now registered before the HTML assignment.
12+
313
## 0.1.0
414

515
### Minor Changes

packages/vscode-extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quickmock",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"type": "module",
55
"main": "./dist/index.cjs",
66
"module": "./dist/index.mjs",

packages/vscode-extension/src/commands/new-wireframe.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './new-wireframe.id';
2+
export * from './new-wireframe.command';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as vscode from 'vscode';
2+
import { QUICKMOCK_NEW_WIREFRAME_COMMAND_ID } from './new-wireframe.id';
3+
import { handleNewWireframe } from './new-wireframe.handler';
4+
5+
export const registerNewWireframeCommand = (
6+
context: vscode.ExtensionContext
7+
): void => {
8+
context.subscriptions.push(
9+
vscode.commands.registerCommand(
10+
QUICKMOCK_NEW_WIREFRAME_COMMAND_ID,
11+
handleNewWireframe
12+
)
13+
);
14+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { getPrimaryWorkspaceFolder } from '#core';
2+
import { writeFile } from '#editor/document';
3+
import * as vscode from 'vscode';
4+
import {
5+
createEmptyQuickMockContent,
6+
DEFAULT_QUICKMOCK_FILE_NAME,
7+
QUICKMOCK_FILE_EXTENSION,
8+
} from './new-wireframe.template';
9+
10+
const CUSTOM_EDITOR_VIEW_TYPE = 'quickmock.editor';
11+
12+
const pickSaveTarget = async (): Promise<vscode.Uri | undefined> => {
13+
const folder = getPrimaryWorkspaceFolder();
14+
const defaultUri = folder
15+
? vscode.Uri.joinPath(folder.uri, DEFAULT_QUICKMOCK_FILE_NAME)
16+
: undefined;
17+
18+
return vscode.window.showSaveDialog({
19+
defaultUri,
20+
filters: { 'QuickMock Wireframe': [QUICKMOCK_FILE_EXTENSION] },
21+
saveLabel: 'Create',
22+
title: 'Create QuickMock File',
23+
});
24+
};
25+
26+
export const handleNewWireframe = async (): Promise<void> => {
27+
const target = await pickSaveTarget();
28+
if (!target) return;
29+
30+
try {
31+
await writeFile(target, createEmptyQuickMockContent());
32+
} catch (error) {
33+
const message = error instanceof Error ? error.message : 'Unknown error';
34+
vscode.window.showErrorMessage(
35+
`Failed to create QuickMock file: ${message}`
36+
);
37+
return;
38+
}
39+
40+
await vscode.commands.executeCommand(
41+
'vscode.openWith',
42+
target,
43+
CUSTOM_EDITOR_VIEW_TYPE
44+
);
45+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const QUICKMOCK_NEW_WIREFRAME_COMMAND_ID = 'quickmock.newWireframe';

0 commit comments

Comments
 (0)