@sigmacomputing/plugin 0.7.45 → 0.7.48
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/dist/initialize.d.ts.map +1 -1
- package/dist/initialize.js +16 -1
- package/dist/react/index.d.ts +6 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +20 -1
- package/package.json +5 -7
- package/src/client.ts +0 -3
- package/src/deepEqual.ts +0 -42
- package/src/index.ts +0 -8
- package/src/initialize.test.ts +0 -28
- package/src/initialize.ts +0 -164
- package/src/react/index.tsx +0 -154
package/dist/initialize.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initialize.d.ts","sourceRoot":"","sources":["../src/initialize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,8BAA8B,CAAC;AAEvD,wBAAgB,UAAU,CAAC,CAAC,GAAG,EAAE,KAAK,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"initialize.d.ts","sourceRoot":"","sources":["../src/initialize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,8BAA8B,CAAC;AAEvD,wBAAgB,UAAU,CAAC,CAAC,GAAG,EAAE,KAAK,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CA2L7D"}
|
package/dist/initialize.js
CHANGED
|
@@ -30,7 +30,7 @@ function initialize() {
|
|
|
30
30
|
subscribedWorkbookVars = {};
|
|
31
31
|
Object.assign(subscribedWorkbookVars, updatedVariables);
|
|
32
32
|
});
|
|
33
|
-
on('wb:plugin:
|
|
33
|
+
on('wb:plugin:selection:update', (updatedInteractions) => {
|
|
34
34
|
subscribedInteractions = {};
|
|
35
35
|
Object.assign(subscribedInteractions, updatedInteractions);
|
|
36
36
|
});
|
|
@@ -94,6 +94,12 @@ function initialize() {
|
|
|
94
94
|
setVariable(id, ...values) {
|
|
95
95
|
void execPromise('wb:plugin:variable:set', id, ...values);
|
|
96
96
|
},
|
|
97
|
+
getInteraction(id) {
|
|
98
|
+
return subscribedInteractions[id];
|
|
99
|
+
},
|
|
100
|
+
setInteraction(id, elementId, selection) {
|
|
101
|
+
void execPromise('wb:plugin:selection:set', id, elementId, selection);
|
|
102
|
+
},
|
|
97
103
|
configureEditorPanel(options) {
|
|
98
104
|
void execPromise('wb:plugin:config:inspector', options);
|
|
99
105
|
},
|
|
@@ -109,6 +115,15 @@ function initialize() {
|
|
|
109
115
|
off('wb:plugin:variable:update', setValues);
|
|
110
116
|
};
|
|
111
117
|
},
|
|
118
|
+
subscribeToWorkbookInteraction(id, callback) {
|
|
119
|
+
const setValues = (values) => {
|
|
120
|
+
callback(values[id]);
|
|
121
|
+
};
|
|
122
|
+
on('wb:plugin:selection:update', setValues);
|
|
123
|
+
return () => {
|
|
124
|
+
off('wb:plugin:selection:update', setValues);
|
|
125
|
+
};
|
|
126
|
+
},
|
|
112
127
|
},
|
|
113
128
|
elements: {
|
|
114
129
|
getElementColumns(id) {
|
package/dist/react/index.d.ts
CHANGED
|
@@ -49,4 +49,10 @@ export declare function useConfig(key?: string): any;
|
|
|
49
49
|
* @returns {[(plugin.WorkbookVariable | undefined), Function]} Constantly updating value of the variable and setter for the variable
|
|
50
50
|
*/
|
|
51
51
|
export declare function useVariable(id: string): [plugin.WorkbookVariable | undefined, Function];
|
|
52
|
+
/**
|
|
53
|
+
* React hook for accessing a workbook interaction selections state
|
|
54
|
+
* @param {string} id ID of variable within Plugin Config to use
|
|
55
|
+
* @returns {[(plugin.WorkbookSelection | undefined), Function]} Constantly updating selection state and setter thereof
|
|
56
|
+
*/
|
|
57
|
+
export declare function useInteraction(id: string, elementId: string): [unknown, Function];
|
|
52
58
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAON,MAAM,OAAO,CAAC;AACf,OAAO,KAAK,MAAM,MAAM,8BAA8B,CAAC;AAOvD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE;IACzC,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;IACnC,QAAQ,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;CAChC,eAIA;AAED;;;GAGG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAEtD;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,MAAM,CAAC,yBAAyB,EAAE,GAC9C,IAAI,CAUN;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,YAAY,EAAE,OAAO,GACpB,CAAC,OAAO,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC,CAczC;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC,gBAAgB,CASrE;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC,aAAa,CAO/D;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAiB3C;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,EAAE,EAAE,MAAM,GACT,CAAC,MAAM,CAAC,gBAAgB,GAAG,SAAS,EAAE,QAAQ,CAAC,CAcjD"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAON,MAAM,OAAO,CAAC;AACf,OAAO,KAAK,MAAM,MAAM,8BAA8B,CAAC;AAOvD;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE;IACzC,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;IACnC,QAAQ,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;CAChC,eAIA;AAED;;;GAGG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAEtD;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,WAAW,EAAE,MAAM,CAAC,yBAAyB,EAAE,GAC9C,IAAI,CAUN;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,YAAY,EAAE,OAAO,GACpB,CAAC,OAAO,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC,CAczC;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC,gBAAgB,CASrE;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC,aAAa,CAO/D;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAiB3C;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,EAAE,EAAE,MAAM,GACT,CAAC,MAAM,CAAC,gBAAgB,GAAG,SAAS,EAAE,QAAQ,CAAC,CAcjD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,EAAE,EAAE,MAAM,EACV,SAAS,EAAE,MAAM,GAChB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAmBrB"}
|
package/dist/react/index.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.useVariable = exports.useConfig = exports.useElementData = exports.useElementColumns = exports.useLoadingState = exports.useEditorPanelConfig = exports.usePlugin = exports.SigmaClientProvider = void 0;
|
|
26
|
+
exports.useInteraction = exports.useVariable = exports.useConfig = exports.useElementData = exports.useElementColumns = exports.useLoadingState = exports.useEditorPanelConfig = exports.usePlugin = exports.SigmaClientProvider = void 0;
|
|
27
27
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
28
28
|
const react_1 = __importStar(require("react"));
|
|
29
29
|
const deepEqual_1 = require("../deepEqual");
|
|
@@ -152,3 +152,22 @@ function useVariable(id) {
|
|
|
152
152
|
];
|
|
153
153
|
}
|
|
154
154
|
exports.useVariable = useVariable;
|
|
155
|
+
/**
|
|
156
|
+
* React hook for accessing a workbook interaction selections state
|
|
157
|
+
* @param {string} id ID of variable within Plugin Config to use
|
|
158
|
+
* @returns {[(plugin.WorkbookSelection | undefined), Function]} Constantly updating selection state and setter thereof
|
|
159
|
+
*/
|
|
160
|
+
function useInteraction(id, elementId) {
|
|
161
|
+
const client = usePlugin();
|
|
162
|
+
const [workbookInteraction, setWorkbookInteraction] = (0, react_1.useState)();
|
|
163
|
+
(0, react_1.useEffect)(() => {
|
|
164
|
+
return client.config.subscribeToWorkbookInteraction(id, setWorkbookInteraction);
|
|
165
|
+
}, [client, id]);
|
|
166
|
+
return [
|
|
167
|
+
workbookInteraction,
|
|
168
|
+
react_1.default.useCallback((value) => {
|
|
169
|
+
client.config.setInteraction(id, elementId, value);
|
|
170
|
+
}, [id]),
|
|
171
|
+
];
|
|
172
|
+
}
|
|
173
|
+
exports.useInteraction = useInteraction;
|
package/package.json
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sigmacomputing/plugin",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.48",
|
|
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",
|
|
@@ -18,16 +16,16 @@
|
|
|
18
16
|
"lint": "eslint --cache src jest.config.js .eslintrc.js",
|
|
19
17
|
"lint:clean": "rimraf .eslintcache",
|
|
20
18
|
"lint:fix": "yarn lint --fix",
|
|
21
|
-
"prepublish": "yarn build",
|
|
19
|
+
"prepublish": "yarn clean && tsc --build tsconfig.publish.json",
|
|
22
20
|
"test": "cd ../.. && jest --ci --selectProjects @sigmacomputing/plugin",
|
|
23
21
|
"test:watch": "yarn test --watch",
|
|
24
22
|
"tsc": "tsc --build tsconfig.build.json"
|
|
25
23
|
},
|
|
26
24
|
"dependencies": {
|
|
27
|
-
"@sigmacomputing/plugin-types": "^0.7.
|
|
25
|
+
"@sigmacomputing/plugin-types": "^0.7.48"
|
|
28
26
|
},
|
|
29
27
|
"peerDependencies": {
|
|
30
28
|
"react": "^16.8.0 || ^17.0.0 | ^18.0.0"
|
|
31
29
|
},
|
|
32
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "81a4a6c9f2334b2273bcb2a1f3b63046e442da09"
|
|
33
31
|
}
|
package/src/client.ts
DELETED
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';
|
package/src/initialize.test.ts
DELETED
|
@@ -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,164 +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, unknown> = {};
|
|
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:interaction: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
|
-
configureEditorPanel(options) {
|
|
116
|
-
void execPromise('wb:plugin:config:inspector', options);
|
|
117
|
-
},
|
|
118
|
-
setLoadingState(loadingState) {
|
|
119
|
-
void execPromise('wb:plugin:config:loading-state', loadingState);
|
|
120
|
-
},
|
|
121
|
-
subscribeToWorkbookVariable(
|
|
122
|
-
id: string,
|
|
123
|
-
callback: (input: plugin.WorkbookVariable) => void,
|
|
124
|
-
): plugin.Unsubscriber {
|
|
125
|
-
const setValues = (values: Record<string, plugin.WorkbookVariable>) => {
|
|
126
|
-
callback(values[id]);
|
|
127
|
-
};
|
|
128
|
-
on('wb:plugin:variable:update', setValues);
|
|
129
|
-
return () => {
|
|
130
|
-
off('wb:plugin:variable:update', setValues);
|
|
131
|
-
};
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
elements: {
|
|
135
|
-
getElementColumns(id) {
|
|
136
|
-
return execPromise('wb:plugin:element:columns:get', id);
|
|
137
|
-
},
|
|
138
|
-
subscribeToElementColumns(id, callback) {
|
|
139
|
-
const eventName = `wb:plugin:element:${id}:columns`;
|
|
140
|
-
on(eventName, callback);
|
|
141
|
-
void execPromise('wb:plugin:element:subscribe:columns', id);
|
|
142
|
-
|
|
143
|
-
return () => {
|
|
144
|
-
off(eventName, callback);
|
|
145
|
-
void execPromise('wb:plugin:element:unsubscribe:columns', id);
|
|
146
|
-
};
|
|
147
|
-
},
|
|
148
|
-
subscribeToElementData(id, callback) {
|
|
149
|
-
const eventName = `wb:plugin:element:${id}:data`;
|
|
150
|
-
on(eventName, callback);
|
|
151
|
-
void execPromise('wb:plugin:element:subscribe:data', id);
|
|
152
|
-
|
|
153
|
-
return () => {
|
|
154
|
-
off(eventName, callback);
|
|
155
|
-
void execPromise('wb:plugin:element:unsubscribe:data', id);
|
|
156
|
-
};
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
destroy: function () {
|
|
160
|
-
Object.keys(listeners).forEach(event => delete listeners[event]);
|
|
161
|
-
window.removeEventListener('message', listener, false);
|
|
162
|
-
},
|
|
163
|
-
};
|
|
164
|
-
}
|
package/src/react/index.tsx
DELETED
|
@@ -1,154 +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
|
-
}
|