@sigmacomputing/plugin 0.7.47 → 0.7.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,14 +1,12 @@
1
1
  {
2
2
  "name": "@sigmacomputing/plugin",
3
- "version": "0.7.47",
3
+ "version": "0.7.50",
4
4
  "description": "Plugin client api",
5
5
  "module": "./src/index.ts",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
8
  "files": [
9
- "/dist",
10
- "/src",
11
- "!/src/**/__tests__/**"
9
+ "/dist"
12
10
  ],
13
11
  "scripts": {
14
12
  "clean": "rimraf dist tsconfig*.tsbuildinfo",
@@ -24,10 +22,10 @@
24
22
  "tsc": "tsc --build tsconfig.build.json"
25
23
  },
26
24
  "dependencies": {
27
- "@sigmacomputing/plugin-types": "^0.7.47"
25
+ "@sigmacomputing/plugin-types": "^0.7.50"
28
26
  },
29
27
  "peerDependencies": {
30
28
  "react": "^16.8.0 || ^17.0.0 | ^18.0.0"
31
29
  },
32
- "gitHead": "146efa6343e32c1abb44287804c424db219a7d28"
30
+ "gitHead": "8bb6bcf1bf89d51b97c3132a1a21d69fb911be44"
33
31
  }
package/src/client.ts DELETED
@@ -1,3 +0,0 @@
1
- import { initialize } from './initialize';
2
-
3
- export const client = initialize();
package/src/deepEqual.ts DELETED
@@ -1,42 +0,0 @@
1
- // Deep Equality comparison example
2
- //
3
- // This is an example of how to implement an object-comparison function in
4
- // JavaScript (ES5+). A few points of interest here:
5
- //
6
- // * You can get an array of all an object's properties in ES5+ by calling
7
- // the class method Object.keys(obj).
8
- // * The function recursively calls itself in the for / in loop when it
9
- // compares the contents of each property
10
- // * You can hide a "private" function inside a function of this kind by
11
- // placing one function declaration inside of another. The inner function
12
- // is not hoisted out into the global scope, so it is only visible inside
13
- // of the parent function.
14
- // * The reason this nested helper function is necessary is that
15
- // `typeof null` is still "object" in JS, a major "gotcha" to watch out for.
16
- //
17
- // https://gist.github.com/egardner/efd34f270cc33db67c0246e837689cb9
18
-
19
- export function deepEqual(obj1: any, obj2: any) {
20
- if (obj1 === obj2) {
21
- return true;
22
- } else if (isObject(obj1) && isObject(obj2)) {
23
- if (Object.keys(obj1).length !== Object.keys(obj2).length) {
24
- return false;
25
- }
26
- for (const prop in obj1) {
27
- if (!deepEqual(obj1[prop], obj2[prop])) {
28
- return false;
29
- }
30
- }
31
- return true;
32
- }
33
-
34
- // Private
35
- function isObject(obj: any) {
36
- if (typeof obj === 'object' && obj != null) {
37
- return true;
38
- } else {
39
- return false;
40
- }
41
- }
42
- }
package/src/index.ts DELETED
@@ -1,8 +0,0 @@
1
- export { initialize } from './initialize';
2
- export { client } from './client';
3
- /* Ideally we want this to be tree-shakeable.
4
- * Node 12 added support for `exports` in package.json,
5
- * however typescript does not yet support this.
6
- * Tracked: https://github.com/microsoft/TypeScript/issues/33079
7
- */
8
- export * from './react';
@@ -1,28 +0,0 @@
1
- import { initialize } from './initialize';
2
-
3
- describe('initialize', () => {
4
- let originalAddEventListener: any;
5
- let originalRemoveEventListener: any;
6
- beforeAll(() => {
7
- originalAddEventListener = window.addEventListener;
8
- originalRemoveEventListener = window.removeEventListener;
9
- window.addEventListener = jest.fn();
10
- window.removeEventListener = jest.fn();
11
- });
12
-
13
- beforeEach(() => {
14
- jest.resetAllMocks();
15
- });
16
- test('should initialize and be destroyable', () => {
17
- const client = initialize();
18
- expect(window.addEventListener).toHaveBeenCalled();
19
-
20
- client.destroy();
21
- expect(window.removeEventListener).toHaveBeenCalled();
22
- });
23
-
24
- afterAll(() => {
25
- window.addEventListener = originalAddEventListener;
26
- window.removeEventListener = originalRemoveEventListener;
27
- });
28
- });
package/src/initialize.ts DELETED
@@ -1,190 +0,0 @@
1
- import * as plugin from '@sigmacomputing/plugin-types';
2
-
3
- export function initialize<T = {}>(): plugin.PluginInstance<T> {
4
- const pluginConfig: Partial<plugin.PluginConfig<T>> = {
5
- config: {} as T,
6
- };
7
- let subscribedInteractions: Record<string, plugin.WorkbookSelection[]> = {};
8
- let subscribedWorkbookVars: Record<string, plugin.WorkbookVariable> = {};
9
-
10
- const listeners: {
11
- [event: string]: Function[];
12
- } = {};
13
-
14
- for (const [key, value] of new URL(
15
- document.location.toString(),
16
- ).searchParams.entries())
17
- pluginConfig[key] = JSON.parse(value);
18
-
19
- const listener = (e: plugin.PluginMessageResponse) => {
20
- emit(e.data.type, e.data.result, e.data.error);
21
- };
22
-
23
- window.addEventListener('message', listener, false);
24
- window.addEventListener('click', () => execPromise('wb:plugin:focus'));
25
-
26
- on('wb:plugin:config:update', (config: plugin.PluginConfig<T>) => {
27
- Object.assign(pluginConfig, config);
28
- emit('config', pluginConfig.config ?? {});
29
- });
30
-
31
- // send initialize event
32
- void execPromise('wb:plugin:init', require('../package.json').version).then(
33
- config => {
34
- Object.assign(pluginConfig, config);
35
- emit('init', pluginConfig);
36
- emit('config', pluginConfig.config);
37
- },
38
- );
39
-
40
- on(
41
- 'wb:plugin:variable:update',
42
- (updatedVariables: Record<string, plugin.WorkbookVariable>) => {
43
- subscribedWorkbookVars = {};
44
- Object.assign(subscribedWorkbookVars, updatedVariables);
45
- },
46
- );
47
-
48
- on('wb:plugin:selection:update', (updatedInteractions: unknown) => {
49
- subscribedInteractions = {};
50
- Object.assign(subscribedInteractions, updatedInteractions);
51
- });
52
-
53
- function on(event: string, listener: Function) {
54
- listeners[event] = listeners[event] || [];
55
- listeners[event].push(listener);
56
- }
57
-
58
- function off(event: string, listener: Function) {
59
- if (listeners[event] == null) return;
60
- listeners[event] = listeners[event].filter(a => a !== listener);
61
- }
62
-
63
- function emit(event: string, ...args: any) {
64
- Object.values(listeners[event] || []).forEach(fn => fn(...args));
65
- }
66
-
67
- function execPromise<R>(event: string, ...args: any): Promise<R> {
68
- return new Promise((resolve, reject) => {
69
- const callback = (data: R, error: any) => {
70
- if (error) reject(error);
71
- else resolve(data);
72
- off(event, callback);
73
- };
74
- on(event, callback);
75
- window.parent.postMessage(
76
- { type: event, args, elementId: pluginConfig.id },
77
- pluginConfig?.wbOrigin ?? '*',
78
- );
79
- });
80
- }
81
-
82
- return {
83
- get sigmaEnv() {
84
- return pluginConfig.sigmaEnv;
85
- },
86
- get isScreenshot() {
87
- return pluginConfig.screenshot;
88
- },
89
- config: {
90
- // @ts-ignore
91
- getKey(key) {
92
- return pluginConfig?.config?.[key]!;
93
- },
94
- get() {
95
- return pluginConfig.config;
96
- },
97
- set(partialConfig) {
98
- void execPromise('wb:plugin:config:update', partialConfig);
99
- },
100
- setKey(key, value) {
101
- void execPromise('wb:plugin:config:update', {
102
- [key]: value,
103
- });
104
- },
105
- subscribe(listener) {
106
- on('config', listener);
107
- return () => off('config', listener);
108
- },
109
- getVariable(id: string): plugin.WorkbookVariable {
110
- return subscribedWorkbookVars[id];
111
- },
112
- setVariable(id: string, ...values: unknown[]) {
113
- void execPromise('wb:plugin:variable:set', id, ...values);
114
- },
115
- getInteraction(id: string) {
116
- return subscribedInteractions[id];
117
- },
118
- setInteraction(
119
- id: string,
120
- elementId: string,
121
- selection:
122
- | string[]
123
- | Array<Record<string, { type: string; val?: unknown }>>,
124
- ) {
125
- void execPromise('wb:plugin:selection:set', id, elementId, selection);
126
- },
127
- configureEditorPanel(options) {
128
- void execPromise('wb:plugin:config:inspector', options);
129
- },
130
- setLoadingState(loadingState) {
131
- void execPromise('wb:plugin:config:loading-state', loadingState);
132
- },
133
- subscribeToWorkbookVariable(
134
- id: string,
135
- callback: (input: plugin.WorkbookVariable) => void,
136
- ): plugin.Unsubscriber {
137
- const setValues = (values: Record<string, plugin.WorkbookVariable>) => {
138
- callback(values[id]);
139
- };
140
- on('wb:plugin:variable:update', setValues);
141
- return () => {
142
- off('wb:plugin:variable:update', setValues);
143
- };
144
- },
145
- subscribeToWorkbookInteraction(
146
- id: string,
147
- callback: (input: plugin.WorkbookSelection[]) => void,
148
- ): plugin.Unsubscriber {
149
- const setValues = (
150
- values: Record<string, plugin.WorkbookSelection[]>,
151
- ) => {
152
- callback(values[id]);
153
- };
154
- on('wb:plugin:selection:update', setValues);
155
- return () => {
156
- off('wb:plugin:selection:update', setValues);
157
- };
158
- },
159
- },
160
- elements: {
161
- getElementColumns(id) {
162
- return execPromise('wb:plugin:element:columns:get', id);
163
- },
164
- subscribeToElementColumns(id, callback) {
165
- const eventName = `wb:plugin:element:${id}:columns`;
166
- on(eventName, callback);
167
- void execPromise('wb:plugin:element:subscribe:columns', id);
168
-
169
- return () => {
170
- off(eventName, callback);
171
- void execPromise('wb:plugin:element:unsubscribe:columns', id);
172
- };
173
- },
174
- subscribeToElementData(id, callback) {
175
- const eventName = `wb:plugin:element:${id}:data`;
176
- on(eventName, callback);
177
- void execPromise('wb:plugin:element:subscribe:data', id);
178
-
179
- return () => {
180
- off(eventName, callback);
181
- void execPromise('wb:plugin:element:unsubscribe:data', id);
182
- };
183
- },
184
- },
185
- destroy: function () {
186
- Object.keys(listeners).forEach(event => delete listeners[event]);
187
- window.removeEventListener('message', listener, false);
188
- },
189
- };
190
- }
@@ -1,183 +0,0 @@
1
- import React, {
2
- Context,
3
- createContext,
4
- useContext,
5
- useEffect,
6
- useRef,
7
- useState,
8
- } from 'react';
9
- import * as plugin from '@sigmacomputing/plugin-types';
10
-
11
- import { deepEqual } from '../deepEqual';
12
- import { client } from '../client';
13
-
14
- const Context = createContext<plugin.PluginInstance<any>>(client);
15
-
16
- /**
17
- * Wrapper for plugin client using a Provider
18
- * @param {{client: plugin.PluginInstance, children: React.ReachChildren}} props Plugin instance and any children elements
19
- * @returns {JSXElement} Context Provider for passed in props
20
- */
21
- export function SigmaClientProvider(props: {
22
- client: plugin.PluginInstance<any>;
23
- children?: React.ReactChildren;
24
- }) {
25
- return (
26
- <Context.Provider value={props.client}>{props.children}</Context.Provider>
27
- );
28
- }
29
-
30
- /**
31
- * A constantly updating getter for the entire Plugin Instance
32
- * @returns {plugin.PluginInstance} Context for the current plugin instance
33
- */
34
- export function usePlugin(): plugin.PluginInstance<any> {
35
- return useContext(Context);
36
- }
37
-
38
- /**
39
- * Provides a setter for the Plugin's Config Options
40
- * @param {plugin.CustomPluginConfigOptions[]} nextOptions Updated possible Config Options
41
- */
42
- export function useEditorPanelConfig(
43
- nextOptions: plugin.CustomPluginConfigOptions[],
44
- ): void {
45
- const client = usePlugin();
46
- const optionsRef = useRef({});
47
- useEffect(() => {
48
- if (nextOptions == null) return;
49
- if (!deepEqual(nextOptions, optionsRef.current)) {
50
- client.config.configureEditorPanel(nextOptions);
51
- optionsRef.current = nextOptions;
52
- }
53
- }, [client, nextOptions]);
54
- }
55
-
56
- /**
57
- * React hook for Plugin Config loading state
58
- * @param {boolean} initialState Initial value to set loading state to
59
- * @returns {[boolean, Function]} Boolean value corresponding to loading state for plugin config and setter for loading state
60
- */
61
- export function useLoadingState(
62
- initialState: boolean,
63
- ): [boolean, (nextState: boolean) => void] {
64
- const client = usePlugin();
65
- const [loading, setLoading] = useState(() => {
66
- client.config.setLoadingState(initialState);
67
- return initialState;
68
- });
69
- return [
70
- loading,
71
- nextState => {
72
- if (nextState === loading) return;
73
- setLoading(nextState);
74
- client.config.setLoadingState(nextState);
75
- },
76
- ];
77
- }
78
-
79
- /**
80
- * Provides constantly updating column values from corresponding sheet
81
- * @param {string} id Sheet ID to retrieve from workbook
82
- * @returns {plugin.WbElementColumns} Values of corresponding columns contained within the sheet
83
- */
84
- export function useElementColumns(id: string): plugin.WbElementColumns {
85
- const client = usePlugin();
86
- const [columns, setColumns] = useState<plugin.WbElementColumns>({});
87
- useEffect(() => {
88
- if (id) {
89
- return client.elements.subscribeToElementColumns(id, setColumns);
90
- }
91
- }, [client, id]);
92
- return columns;
93
- }
94
-
95
- /**
96
- * Provides constantly updating data values from corresponding sheet
97
- * @param {string} id Sheet ID to get element data from
98
- * @returns {plugin.WbElementData} Element Data for corresponding sheet, if any
99
- */
100
- export function useElementData(id: string): plugin.WbElementData {
101
- const client = usePlugin();
102
- const [data, setData] = useState<plugin.WbElementData>({});
103
- useEffect(() => {
104
- if (id) return client.elements.subscribeToElementData(id, setData);
105
- }, [client, id]);
106
- return data;
107
- }
108
-
109
- /**
110
- * Provides a constantly updating value for entire config or certain key within config
111
- * @param {string} key Key within Plugin Config, optional
112
- * @returns Entire config if no key passed in or value for key within plugin config
113
- */
114
- export function useConfig(key?: string): any {
115
- const client = usePlugin();
116
- const [config, setConfig] = useState<any>(
117
- key != null ? client.config.getKey(key) : client.config.get(),
118
- );
119
- useEffect(
120
- () =>
121
- client.config.subscribe(newConfig => {
122
- if (key != null && newConfig[key] !== config[key]) {
123
- setConfig(newConfig[key]);
124
- } else {
125
- setConfig(newConfig);
126
- }
127
- }),
128
- [client],
129
- );
130
- return config;
131
- }
132
-
133
- /**
134
- * React hook for accessing a workbook variable
135
- * @param {string} id ID of variable within Plugin Config to use
136
- * @returns {[(plugin.WorkbookVariable | undefined), Function]} Constantly updating value of the variable and setter for the variable
137
- */
138
- export function useVariable(
139
- id: string,
140
- ): [plugin.WorkbookVariable | undefined, Function] {
141
- const client = usePlugin();
142
- const [workbookVariable, setWorkbookVariable] =
143
- useState<plugin.WorkbookVariable>();
144
- useEffect(() => {
145
- return client.config.subscribeToWorkbookVariable(id, setWorkbookVariable);
146
- }, [client, id]);
147
- return [
148
- workbookVariable,
149
- React.useCallback(
150
- (...values: unknown[]) => client.config.setVariable(id, ...values),
151
- [id],
152
- ),
153
- ];
154
- }
155
-
156
- /**
157
- * React hook for accessing a workbook interaction selections state
158
- * @param {string} id ID of variable within Plugin Config to use
159
- * @returns {[(plugin.WorkbookSelection | undefined), Function]} Constantly updating selection state and setter thereof
160
- */
161
- export function useInteraction(
162
- id: string,
163
- elementId: string,
164
- ): [unknown, Function] {
165
- const client = usePlugin();
166
- const [workbookInteraction, setWorkbookInteraction] =
167
- useState<plugin.WorkbookSelection[]>();
168
- useEffect(() => {
169
- return client.config.subscribeToWorkbookInteraction(
170
- id,
171
- setWorkbookInteraction,
172
- );
173
- }, [client, id]);
174
- return [
175
- workbookInteraction,
176
- React.useCallback(
177
- (value: plugin.WorkbookSelection[]) => {
178
- client.config.setInteraction(id, elementId, value);
179
- },
180
- [id],
181
- ),
182
- ];
183
- }