Skip to content

Commit 0f65af6

Browse files
committed
add request color
1 parent c699997 commit 0f65af6

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

src/client/initialize.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export function initialize<T = {}>(): PluginInstance<T> {
3737
emit('config', pluginConfig.config ?? {});
3838
});
3939

40+
on('wb:plugin:style:update', (styleColors: any) => {
41+
pluginConfig.styleColors = styleColors;
42+
emit('style', styleColors);
43+
});
44+
4045
// send initialize event
4146
void execPromise(
4247
'wb:plugin:init',
@@ -223,6 +228,33 @@ export function initialize<T = {}>(): PluginInstance<T> {
223228
void execPromise('wb:plugin:element:fetch-more', configId);
224229
},
225230
},
231+
232+
// Style management functions for hooks
233+
style: {
234+
/**
235+
* Subscribe to style updates
236+
* @param callback Function to call when style updates
237+
* @returns Unsubscriber function
238+
*/
239+
subscribe(callback: (style: any) => void) {
240+
on('style', callback);
241+
return () => off('style', callback);
242+
},
243+
244+
/**
245+
* Request current style from workbook
246+
* @returns Promise with current style
247+
*/
248+
async getStyle() {
249+
try {
250+
return await execPromise('wb:plugin:style:get');
251+
} catch (error) {
252+
// Return default style if request fails
253+
return { backgroundColor: 'transparent' };
254+
}
255+
},
256+
},
257+
226258
destroy() {
227259
Object.keys(listeners).forEach(event => delete listeners[event]);
228260
window.removeEventListener('message', listener, false);

src/react/hooks.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,20 @@ export function useActionEffect(configId: string, effect: () => void) {
249249
}
250250

251251
/**
252-
* React hook for accessing plugin style properties
253-
* @returns {PluginStyle} Style properties
252+
* React hook for accessing plugin style with live updates via PostMessage
253+
* @returns {PluginStyle | undefined} Style properties from the workbook if available
254254
*/
255-
export function usePluginStyle(): PluginStyle {
256-
const client = useConfig();
255+
export function usePluginStyle(): PluginStyle | undefined {
256+
const client = usePlugin();
257+
const [style, setStyle] = useState<PluginStyle | undefined>(undefined);
258+
259+
useEffect(() => {
260+
const unsubscribe = client.style.subscribe(setStyle);
261+
// Request initial style data on mount
262+
void client.style.getStyle().then(setStyle);
263+
264+
return unsubscribe;
265+
}, [client]);
257266

258-
return {
259-
backgroundColor: client?.backgroundColor,
260-
};
267+
return style;
261268
}

src/types.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ export interface PluginConfig<T> {
2727
id: string;
2828
config: T;
2929
screenshot: boolean;
30+
// styleColors: PluginStyle;
3031
[key: string]: any;
3132
}
3233

34+
// /**
35+
// * Style colors available to plugins
36+
// * @typedef {object} PluginStyle
37+
// * @property {string} backgroundColor Background color set from workbook
38+
// */
39+
// export interface PluginStyle {
40+
// backgroundColor: string;
41+
// }
42+
3343
/**
3444
* @typedef {object} WorkbookVariable
3545
* @property {string} name Name of control variable within workbook
@@ -196,6 +206,19 @@ export type CustomPluginConfigOptions =
196206
export interface PluginInstance<T = any> {
197207
sigmaEnv: 'author' | 'viewer' | 'explorer';
198208

209+
// /**
210+
// * Plugin style colors from the workbook
211+
// * @returns {PluginStyle | undefined} Style colors if available
212+
// */
213+
// styleColors?: PluginStyle | undefined;
214+
215+
// /**
216+
// * Listen to style color changes
217+
// * @param {Function} callback Function to call when style colors change
218+
// * @returns {Function} Unsubscriber function
219+
// */
220+
// onStyleChange(callback: (styleColors: PluginStyle) => void): () => void;
221+
199222
config: {
200223
/**
201224
* Getter for entire Plugin Config
@@ -355,6 +378,24 @@ export interface PluginInstance<T = any> {
355378
fetchMoreElementData(configId: string): void;
356379
};
357380

381+
/**
382+
* Style management for plugins
383+
*/
384+
style: {
385+
/**
386+
* Subscribe to style updates
387+
* @param callback Function to call when style updates
388+
* @returns Unsubscriber function
389+
*/
390+
subscribe(callback: (style: PluginStyle) => void): () => void;
391+
392+
/**
393+
* Request current style from workbook
394+
* @returns Promise with current style
395+
*/
396+
getStyle(): Promise<PluginStyle>;
397+
};
398+
358399
/**
359400
* Destroys plugin instance and removes all subscribers
360401
*/

0 commit comments

Comments
 (0)