From 7e64932f96c575ebf266e08a5e95f9917170e93d Mon Sep 17 00:00:00 2001 From: Alison Lai Date: Wed, 17 Jul 2024 14:01:42 -0700 Subject: [PATCH 1/8] add type --- src/types.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/types.ts b/src/types.ts index cc01b4a..c370668 100644 --- a/src/types.ts +++ b/src/types.ts @@ -171,6 +171,11 @@ export type CustomPluginConfigOptions = type: 'action-trigger'; name: string; label?: string; + } + | { + type: 'action-effect'; + name: string; + label?: string; }; /** From 7c5716002f1204c89ad98918f5b3f65d28625867 Mon Sep 17 00:00:00 2001 From: Alison Lai Date: Wed, 17 Jul 2024 14:06:13 -0700 Subject: [PATCH 2/8] add hook and registeration of effects --- src/client/initialize.ts | 15 +++++++++++++++ src/react/hooks.ts | 17 +++++++++++++++++ src/types.ts | 15 ++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/client/initialize.ts b/src/client/initialize.ts index b22422e..a8ebf02 100644 --- a/src/client/initialize.ts +++ b/src/client/initialize.ts @@ -14,6 +14,7 @@ export function initialize(): PluginInstance { let subscribedInteractions: Record = {}; let subscribedWorkbookVars: Record = {}; + let registeredEffects: Record = {}; const listeners: { [event: string]: Function[]; @@ -59,6 +60,14 @@ export function initialize(): PluginInstance { Object.assign(subscribedInteractions, updatedInteractions); }); + on('wb:plugin:action-effect:invoke', (id: string) => { + const effect = registeredEffects[id]; + if (!effect) { + throw new Error('No effect found.'); + } + effect(); + }); + function on(event: string, listener: Function) { listeners[event] = listeners[event] || []; listeners[event].push(listener); @@ -138,6 +147,12 @@ export function initialize(): PluginInstance { triggerAction(id: string) { void execPromise('wb:plugin:action-trigger:invoke', id); }, + registerEffect(id: string, effect: Function) { + registeredEffects[id] = effect; + }, + unregisterEffect(id: string) { + delete registeredEffects[id]; + }, configureEditorPanel(options) { void execPromise('wb:plugin:config:inspector', options); }, diff --git a/src/react/hooks.ts b/src/react/hooks.ts index 384ca83..f458db5 100644 --- a/src/react/hooks.ts +++ b/src/react/hooks.ts @@ -190,3 +190,20 @@ export function useActionTrigger(id: string) { client.config.triggerAction(id); }, [client, id]); } + +/** + * React hook for registering and unregistering an action effect + * @param {string} id ID of action effect + * @param {Function} effect The function to be called when the action is triggered + */ +export function useActionEffect(id: string, effect: Function) { + const client = usePlugin(); + + useEffect(() => { + client.config.registerEffect(id, effect); + console.log('effectId', id); + return () => { + client.config.unregisterEffect(id); + }; + }, [client, id]); +} diff --git a/src/types.ts b/src/types.ts index c370668..9136b25 100644 --- a/src/types.ts +++ b/src/types.ts @@ -266,11 +266,24 @@ export interface PluginInstance { ): void; /** - * Triggers an action based on the provided action trigger Id + * Triggers an action based on the provided action trigger ID * @param {string} id ID from action-trigger type in Plugin Config */ triggerAction(id: string): void; + /** + * Registers an effect with the provided action effect ID + * @param {string} id ID from action-effect type in Plugin Config + * @param effect The effect function to register + */ + registerEffect(id: string, effect: Function): void; + + /** + * Unregisters an effect based on the provided action effect ID + * @param {string} id ID from action-effect type in Plugin Config + */ + unregisterEffect(id: string): void; + /** * Overrider function for Config Ready state * @param {boolean} loadingState Boolean representing if Plugin Config is still loading From a9d0f2174062936fbf8233eea583cf2df459cb6f Mon Sep 17 00:00:00 2001 From: Alison Lai Date: Wed, 17 Jul 2024 14:08:36 -0700 Subject: [PATCH 3/8] update readme --- README.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d81fce..f62f4cb 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,11 @@ type CustomPluginConfigOptions = type: 'action-trigger'; name: string; label?: string; + } + | { + type: 'action-effect'; + name: string; + label?: string; }; ``` @@ -447,10 +452,20 @@ interface PluginInstance { ): void; /** - * Triggers an action based on the provided action trigger Id + * Triggers an action based on the provided action trigger ID */ triggerAction(id: string): void; + /** + * Registers an effect with the provided action effect ID + */ + registerEffect(id: string, effect: Function): void; + + /** + * Unregisters an effect based on the provided action effect ID + */ + unregisterEffect(id: string): void; + /** * Overrider function for Config Ready state */ @@ -709,6 +724,19 @@ The function that can be called to trigger the action function triggerActionCallback(): void; ``` +#### useActionEffect() + +Registers and unregisters an action effect within the plugin + +```ts +function useActionEffect(effectId: string, effect: Function); +``` + +Arguments + +- `effectId : string` - The ID of the action effect +- `effect : Function` - The function to be called when the effect is triggered + #### useConfig() Returns the workbook element’s current configuration. If a key is provided, only From 1491c1dc3e1d2a2c148dee33f636882706cb3547 Mon Sep 17 00:00:00 2001 From: Alison Lai Date: Wed, 17 Jul 2024 14:16:05 -0700 Subject: [PATCH 4/8] delete consolelog --- src/react/hooks.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/react/hooks.ts b/src/react/hooks.ts index f458db5..a953e76 100644 --- a/src/react/hooks.ts +++ b/src/react/hooks.ts @@ -201,7 +201,6 @@ export function useActionEffect(id: string, effect: Function) { useEffect(() => { client.config.registerEffect(id, effect); - console.log('effectId', id); return () => { client.config.unregisterEffect(id); }; From f54cd72a02315c4da2bb490be2d1770bee3ac963 Mon Sep 17 00:00:00 2001 From: Alison Lai Date: Wed, 24 Jul 2024 09:51:43 -0700 Subject: [PATCH 5/8] change error to console.warn --- package.json | 3 ++- src/client/initialize.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 94f8d32..1f6e238 100644 --- a/package.json +++ b/package.json @@ -53,5 +53,6 @@ "ttypescript": "^1.5.13", "typescript": "^4.8.2", "typescript-transform-paths": "^3.3.1" - } + }, + "packageManager": "yarn@4.3.1+sha512.af78262d7d125afbfeed740602ace8c5e4405cd7f4735c08feb327286b2fdb2390fbca01589bfd1f50b1240548b74806767f5a063c94b67e431aabd0d86f7774" } diff --git a/src/client/initialize.ts b/src/client/initialize.ts index a8ebf02..3e3eace 100644 --- a/src/client/initialize.ts +++ b/src/client/initialize.ts @@ -63,7 +63,7 @@ export function initialize(): PluginInstance { on('wb:plugin:action-effect:invoke', (id: string) => { const effect = registeredEffects[id]; if (!effect) { - throw new Error('No effect found.'); + console.warn('No effect found.'); } effect(); }); From 6d689d13d95e5f5fe11d170b207b762d71481c09 Mon Sep 17 00:00:00 2001 From: Alison Lai Date: Wed, 24 Jul 2024 09:53:24 -0700 Subject: [PATCH 6/8] Revert "change error to console.warn" This reverts commit f54cd72a02315c4da2bb490be2d1770bee3ac963. --- package.json | 3 +-- src/client/initialize.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 1f6e238..94f8d32 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,5 @@ "ttypescript": "^1.5.13", "typescript": "^4.8.2", "typescript-transform-paths": "^3.3.1" - }, - "packageManager": "yarn@4.3.1+sha512.af78262d7d125afbfeed740602ace8c5e4405cd7f4735c08feb327286b2fdb2390fbca01589bfd1f50b1240548b74806767f5a063c94b67e431aabd0d86f7774" + } } diff --git a/src/client/initialize.ts b/src/client/initialize.ts index 3e3eace..a8ebf02 100644 --- a/src/client/initialize.ts +++ b/src/client/initialize.ts @@ -63,7 +63,7 @@ export function initialize(): PluginInstance { on('wb:plugin:action-effect:invoke', (id: string) => { const effect = registeredEffects[id]; if (!effect) { - console.warn('No effect found.'); + throw new Error('No effect found.'); } effect(); }); From 0d46b9740f991808da817aee435bb67eec872ac9 Mon Sep 17 00:00:00 2001 From: Alison Lai Date: Wed, 24 Jul 2024 09:54:13 -0700 Subject: [PATCH 7/8] change error to console.warn --- src/client/initialize.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/initialize.ts b/src/client/initialize.ts index a8ebf02..3e3eace 100644 --- a/src/client/initialize.ts +++ b/src/client/initialize.ts @@ -63,7 +63,7 @@ export function initialize(): PluginInstance { on('wb:plugin:action-effect:invoke', (id: string) => { const effect = registeredEffects[id]; if (!effect) { - throw new Error('No effect found.'); + console.warn('No effect found.'); } effect(); }); From 3de7549c532ed2f2add69b8344d7007ef8a35a21 Mon Sep 17 00:00:00 2001 From: Alison Lai Date: Wed, 24 Jul 2024 12:44:36 -0700 Subject: [PATCH 8/8] small fix --- src/client/initialize.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/client/initialize.ts b/src/client/initialize.ts index 3e3eace..6fc2ac1 100644 --- a/src/client/initialize.ts +++ b/src/client/initialize.ts @@ -62,10 +62,8 @@ export function initialize(): PluginInstance { on('wb:plugin:action-effect:invoke', (id: string) => { const effect = registeredEffects[id]; - if (!effect) { - console.warn('No effect found.'); - } - effect(); + if (effect) effect(); + else console.warn('No effect found.'); }); function on(event: string, listener: Function) {