Skip to content

Commit b123fa6

Browse files
committed
fix: logger not working in browser
Signed-off-by: Saulo Vallory <me@saulo.engineer>
1 parent 72908e8 commit b123fa6

24 files changed

+210
-270
lines changed

packages/plugma/apps/plugma-runtime/interceptors/custom-show-ui.ts packages/plugma/apps/plugma-runtime/figma-api-interceptors/custom-show-ui.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { PlugmaRuntimeData, ShowUIOptions } from '../types';
22
import { getCommandHistory } from '../utils/get-command-history';
33
import {
4-
DEFAULT_WINDOW_SETTINGS,
5-
getWindowSettings,
4+
DEFAULT_WINDOW_SETTINGS,
5+
getWindowSettings,
66
} from '../utils/get-window-settings';
77
import { figmaApi } from './figma-api';
88

@@ -24,7 +24,7 @@ export function customShowUI(
2424
figmaApi.showUI(htmlString, mergedOptions);
2525

2626
getCommandHistory().then((commandHistory) => {
27-
getWindowSettings(DEFAULT_WINDOW_SETTINGS[options.comm]).then(
27+
getWindowSettings(DEFAULT_WINDOW_SETTINGS['dev']).then(
2828
(pluginWindowSettings) => {
2929
const hasCommandChanged =
3030
commandHistory.previousCommand !== runtimeData.command;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* References to native Figma functions.
3+
* Since our runtime code is injected after all Vite transformations,
4+
* we can safely access Figma APIs directly.
5+
*/
6+
export declare const figmaApi: {
7+
readonly resize: (width: number, height: number) => void;
8+
readonly showUI: (html: string, options?: ShowUIOptions) => void;
9+
readonly reposition: (x: number, y: number) => void;
10+
};

packages/plugma/apps/plugma-runtime/gather-build-outputs.ts

-226
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Global runtime data
3+
* Vite will inject the runtimeData object below
4+
*/
5+
/*--[ RUNTIME_DATA ]--*/
6+
/**
7+
* Handles deletion of all plugin data from the root node
8+
*/
9+
export async function handleDeleteRootPluginData() {
10+
const pluginDataKeys = figma.root.getPluginDataKeys();
11+
for (const key of pluginDataKeys) {
12+
figma.root.setPluginData(key, "");
13+
console.log(`[plugma] ${key} deleted from root pluginData`);
14+
}
15+
figma.notify("Root pluginData deleted");
16+
}
17+
handleDeleteRootPluginData.EVENT_NAME = "PLUGMA_DELETE_ROOT_PLUGIN_DATA";

packages/plugma/apps/plugma-runtime/listeners/hide-toolbar.ts packages/plugma/apps/plugma-runtime/handlers/hide-toolbar.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { figmaApi } from '../interceptors/figma-api';
1+
import { figmaApi } from '../figma-api-interceptors/figma-api';
22
import type { PluginMessage } from '../types';
33
import { getWindowSettings } from '../utils/get-window-settings';
44
import { saveWindowSettings } from './save-window-settings';
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//@index('./*', f => `export * from '${f.path}.js';`)
22
export * from './delete-client-storage.js';
33
export * from './delete-file-storage.js';
4+
export * from './delete-root-pluginData.js';
45
export * from './get-on-run-messages.js';
56
export * from './hide-toolbar.js';
67
export * from './maximize-window.js';
78
export * from './minimize-window.js';
89
export * from './save-on-run-messages.js';
910
export * from './save-window-settings.js';
11+
export * from './save-window-settings.test.js';
1012
export * from './setup.js';
1113
//@endindex

packages/plugma/apps/plugma-runtime/listeners/maximize-window.ts packages/plugma/apps/plugma-runtime/handlers/maximize-window.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { figmaApi } from '../interceptors/figma-api';
1+
import { figmaApi } from '../figma-api-interceptors/figma-api';
22
import type { PluginMessage } from '../types';
33
import { getWindowSettings } from '../utils/get-window-settings';
44
import { saveWindowSettings } from './save-window-settings';

packages/plugma/apps/plugma-runtime/listeners/minimize-window.ts packages/plugma/apps/plugma-runtime/handlers/minimize-window.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { figmaApi } from '../interceptors/figma-api.js';
1+
import { figmaApi } from '../figma-api-interceptors/figma-api.js';
22
import type { PluginMessage } from '../types.js';
33
import { getWindowSettings } from '../utils/get-window-settings.js';
44
import { saveWindowSettings } from './save-window-settings.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { WINDOW_SETTINGS_KEY, type WindowSettings } from '../types.js';
3+
import { handleSaveWindowSettings } from './save-window-settings.js';
4+
5+
// Mock Figma API
6+
const mocks = vi.hoisted(() => ({
7+
clientStorage: {
8+
getAsync: vi.fn(),
9+
setAsync: vi.fn(),
10+
},
11+
resize: vi.fn(),
12+
}));
13+
14+
// @ts-expect-error - Mocking global figma object
15+
global.figma = {
16+
clientStorage: mocks.clientStorage,
17+
ui: {
18+
// Mock the resize function using string concatenation
19+
// to match the production code's Vite workaround
20+
['re' + 'size']: mocks.resize,
21+
},
22+
};
23+
24+
describe('Save Window Settings', () => {
25+
const defaultSettings: WindowSettings = {
26+
width: 300,
27+
height: 200,
28+
minimized: false,
29+
toolbarEnabled: true,
30+
};
31+
32+
beforeEach(() => {
33+
mocks.clientStorage.getAsync.mockResolvedValue(defaultSettings);
34+
mocks.clientStorage.setAsync.mockResolvedValue(undefined);
35+
});
36+
37+
afterEach(() => {
38+
vi.clearAllMocks();
39+
});
40+
41+
it('should save new window settings', async () => {
42+
const newSettings = {
43+
width: 400,
44+
height: 300,
45+
};
46+
47+
await handleSaveWindowSettings({
48+
event: handleSaveWindowSettings.EVENT_NAME,
49+
data: newSettings,
50+
});
51+
52+
expect(mocks.resize).toHaveBeenCalledWith(400, 341); // height + 41 for toolbar
53+
expect(mocks.clientStorage.setAsync).toHaveBeenCalledWith(
54+
WINDOW_SETTINGS_KEY,
55+
expect.objectContaining(newSettings),
56+
);
57+
});
58+
59+
it('should handle missing data', async () => {
60+
await handleSaveWindowSettings({
61+
event: handleSaveWindowSettings.EVENT_NAME,
62+
});
63+
64+
expect(mocks.resize).not.toHaveBeenCalled();
65+
expect(mocks.clientStorage.setAsync).not.toHaveBeenCalled();
66+
});
67+
68+
it('should handle settings without height change', async () => {
69+
await handleSaveWindowSettings({
70+
event: handleSaveWindowSettings.EVENT_NAME,
71+
data: { width: 400 },
72+
});
73+
74+
expect(mocks.resize).not.toHaveBeenCalled();
75+
expect(mocks.clientStorage.setAsync).toHaveBeenCalledWith(
76+
WINDOW_SETTINGS_KEY,
77+
expect.objectContaining({ width: 400 }),
78+
);
79+
});
80+
});

0 commit comments

Comments
 (0)