@datagrok/compute 0.6.6 → 0.6.7

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@datagrok/compute",
3
3
  "friendlyName": "Compute",
4
- "version": "0.6.6",
4
+ "version": "0.6.7",
5
5
  "description": "",
6
6
  "dependencies": {
7
7
  "cash-dom": "latest",
@@ -64,185 +64,122 @@ export async function exportFuncCall(call: DG.FuncCall) {
64
64
  }
65
65
  });
66
66
 
67
- // UWAGA: very fragile solution
68
- const funcView = document.getElementsByClassName('grok-view grok-view-func ui-box')[0];
69
- const resultView = funcView.getElementsByClassName('d4-tab-host ui-box')[0];
70
- if (resultView) {
71
- const tabs = resultView.getElementsByClassName('d4-tab-header-stripe')[0].children;
72
- const selectedByUser = resultView.getElementsByClassName('d4-tab-header selected')[0];
73
- for (let i=0; i< tabs.length; i++) {
74
- (tabs[i] as HTMLElement).click();
75
- await new Promise((r) => setTimeout(r, 150));
76
-
77
- const titleDivs = document.getElementsByClassName('grok-func-results-header');
78
- if (!titleDivs.length) continue;
79
- let skipNext = false;
67
+ const addToFile = (imageId: number, visibleTitle: string, width: number, height: number, plotCount?: number) => {
68
+ if (imageId === null || imageId === undefined) return;
69
+
70
+ const worksheet = exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.INPUT)) ||
71
+ exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.OUTPUT));
72
+ if (worksheet) {
73
+ const columnForImage = (
74
+ ((call.inputs[realNames[visibleTitle]] ||
75
+ call.outputs[realNames[visibleTitle]]) as DG.DataFrame)
76
+ .columns as DG.ColumnList)
77
+ .length + 1 + (plotCount ? plotCount * (Math.ceil(width / 64.0) + 1): 0);
78
+ worksheet.addImage(imageId, {
79
+ tl: {col: columnForImage, row: 0},
80
+ ext: {width, height},
81
+ });
82
+ } else {
83
+ const newWorksheet = exportWorkbook.addWorksheet(`Output - Plot - ${visibleTitle}`);
84
+ newWorksheet.addImage(imageId, {
85
+ tl: {col: 0, row: 0},
86
+ ext: {width, height},
87
+ });
88
+ }
89
+ };
80
90
 
81
- for (let i = 0; i < titleDivs.length; i++) {
82
- if (skipNext) {
83
- skipNext = false;
84
- continue;
85
- }
86
- const titleDiv = titleDivs[i];
87
- if (titleDiv.parentElement?.style.display === 'none') continue;
91
+ const exportImage = async (imageDiv: Element, visibleTitle: string) => {
92
+ const regex = /background-image: url\(.*\)/;
93
+ const urlAttr = imageDiv.outerHTML.match(regex)?.[0];
94
+ if (!urlAttr) return;
95
+ const url = urlAttr.substring(23, urlAttr?.length - 2);
96
+ const pic = (await(await fetch(url)).arrayBuffer());
97
+ const imageId = exportWorkbook.addImage({
98
+ buffer: pic,
99
+ extension: 'png',
100
+ });
101
+ addToFile(imageId, visibleTitle, imageDiv.clientWidth, imageDiv.clientHeight);
102
+ };
88
103
 
89
- const visibleTitle = titleDiv.firstChild?.textContent;
90
- if (visibleTitle === null || visibleTitle === undefined) continue;
104
+ const exportPlot = async (plotDiv: ChildNode | null | undefined,
105
+ blockDiv: Element, visibleTitle: string, plotCount? : number) => {
106
+ const titleDiv = blockDiv.firstChild as HTMLElement;
107
+ if (!titleDiv) return;
91
108
 
92
- let imageId;
93
- let width = 0;
94
- let height = 0;
109
+ if (plotDiv && !titleDiv.getElementsByClassName('svg-table').length &&
110
+ (plotDiv as HTMLElement).getAttribute('name') === 'viewer-Grid') return;
95
111
 
96
- const imageDiv = titleDiv.parentElement?.getElementsByClassName('grok-scripting-image-container')[0];
97
- if (imageDiv) {
98
- const regex = /background-image: url\(.*\)/;
99
- const urlAttr = imageDiv.outerHTML.match(regex)?.[0];
100
- if (!urlAttr) continue;
101
- const url = urlAttr.substring(23, urlAttr?.length - 2);
102
- const pic = (await (await fetch(url)).arrayBuffer());
103
- imageId = exportWorkbook.addImage({
104
- buffer: pic,
105
- extension: 'png',
106
- });
107
- width = imageDiv.clientWidth;
108
- height = imageDiv.clientHeight;
109
- } else {
110
- let plotDiv: ChildNode | null | undefined = titleDiv.nextSibling;
112
+ if (plotDiv && titleDiv.getElementsByClassName('svg-table').length) {
113
+ if ((plotDiv as HTMLElement).getAttribute('name') === 'viewer-OutliersSelectionViewer') {
114
+ return;
115
+ }
116
+ if ((plotDiv as HTMLElement).getAttribute('name') === 'viewer-Grid') {
117
+ (titleDiv.getElementsByClassName('svg-table')[0] as HTMLElement).click();
118
+ await new Promise((r) => setTimeout(r, 150));
119
+ plotDiv = titleDiv.nextSibling;
120
+ }
121
+ }
111
122
 
112
- if (plotDiv && !titleDiv.getElementsByClassName('svg-table').length && (plotDiv as HTMLElement).getAttribute('name') === 'viewer-Grid') continue;
123
+ if (plotDiv) {
124
+ const plot = (plotDiv as HTMLElement).getElementsByClassName('d4-layout-middle')[0];
125
+ const canvas = await DG.HtmlUtils.renderToCanvas(plot as HTMLElement);
126
+ const dataUrl = canvas.toDataURL('image/png');
113
127
 
114
- if (plotDiv && titleDiv.getElementsByClassName('svg-table').length) {
115
- if ((plotDiv as HTMLElement).getAttribute('name') === 'viewer-OutliersSelectionViewer') {
116
- plotDiv = titleDiv.parentElement?.nextSibling?.firstChild?.nextSibling;
117
- skipNext = true;
118
- }
119
- if ((plotDiv as HTMLElement).getAttribute('name') === 'viewer-Grid') {
120
- (titleDiv.getElementsByClassName('svg-table')[0] as HTMLElement).click();
121
- await new Promise((r) => setTimeout(r, 150));
122
- plotDiv = titleDiv.nextSibling;
123
- }
124
- }
125
- console.log(plotDiv);
128
+ const imageId = exportWorkbook.addImage({
129
+ base64: dataUrl,
130
+ extension: 'png',
131
+ });
132
+ addToFile(imageId, visibleTitle, canvas.width, canvas.height, plotCount);
133
+ }
134
+ };
126
135
 
127
- if (plotDiv) {
128
- const plot = (plotDiv as HTMLElement).getElementsByClassName('d4-layout-middle')[0];
129
- const canvas = await DG.HtmlUtils.renderToCanvas(plot as HTMLElement);
130
- const dataUrl = canvas.toDataURL('image/png');
136
+ const exportTab = async (tab: HTMLElement) => {
137
+ const blockDivs = document.getElementsByClassName('ui-div ui-block');
138
+ let visibleTitle: string = '';
139
+ let plotCount: number = 0;
131
140
 
132
- imageId = exportWorkbook.addImage({
133
- base64: dataUrl,
134
- extension: 'png',
135
- });
136
- width = canvas.width;
137
- height = canvas.height;
138
- }
139
- }
141
+ for (let i = 0; i < blockDivs.length; i++) {
142
+ const blockDiv = blockDivs[i];
140
143
 
141
- if (imageId === null || imageId === undefined) continue;
144
+ if ((blockDiv as HTMLElement).style.display === 'none') continue;
142
145
 
143
- const worksheet = exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.INPUT)) ||
144
- exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.OUTPUT));
145
- if (worksheet) {
146
- console.log(visibleTitle);
147
- console.log(realNames[visibleTitle]);
148
- const columnForImage = (((call
149
- .inputs[realNames[visibleTitle]] || call
150
- .outputs[realNames[visibleTitle]]) as DG.DataFrame)
151
- .columns as DG.ColumnList)
152
- .length + 1;
153
- worksheet.addImage(imageId, {
154
- tl: {col: columnForImage, row: 0},
155
- ext: {width, height},
156
- });
157
- } else {
158
- const newWorksheet = exportWorkbook.addWorksheet(`Output - Plot - ${visibleTitle}`);
159
- newWorksheet.addImage(imageId, {
160
- tl: {col: 0, row: 0},
161
- ext: {width, height},
162
- });
163
- }
146
+ if (blockDiv.firstChild?.firstChild?.textContent) {
147
+ visibleTitle = blockDiv.firstChild?.firstChild?.textContent;
148
+ plotCount = 0;
149
+ } else {
150
+ plotCount++;
164
151
  }
165
- }
166
- (selectedByUser as HTMLElement).click();
167
- } else {
168
- const resultView = document.getElementsByClassName('ui-panel grok-func-results')[0];
169
- const titleDivs = resultView.getElementsByClassName('grok-func-results-header');
170
- let skipNext = false;
171
152
 
172
- for (let i=0; i< titleDivs.length; i++) {
173
- if (skipNext) {
174
- skipNext = false;
175
- continue;
153
+ if ((blockDiv.firstChild?.nextSibling as HTMLElement).getAttribute('name') === 'viewer-OutliersSelectionViewer') {
154
+ plotCount--;
176
155
  }
177
156
 
178
- const titleDiv = titleDivs[i];
179
- const visibleTitle = titleDiv.firstChild?.textContent;
180
- if (visibleTitle === null || visibleTitle === undefined) continue;
181
-
182
- let imageId;
183
- let width = 0;
184
- let height = 0;
185
-
186
- const imageDiv = titleDiv.parentElement?.getElementsByClassName('grok-scripting-image-container')[0];
157
+ const imageDiv = blockDiv.getElementsByClassName('grok-scripting-image-container')[0];
187
158
  if (imageDiv) {
188
- const regex = /background-image: url\(.*\)/;
189
- const urlAttr = imageDiv.outerHTML.match(regex)?.[0];
190
- if (!urlAttr) continue;
191
- const url = urlAttr.substring(23, urlAttr?.length - 2);
192
- const pic = (await (await fetch(url)).arrayBuffer());
193
- imageId = exportWorkbook.addImage({
194
- buffer: pic,
195
- extension: 'png',
196
- });
197
- width = imageDiv.clientWidth;
198
- height = imageDiv.clientHeight;
159
+ await exportImage(imageDiv, visibleTitle);
199
160
  } else {
200
- let plotDiv: ChildNode | null | undefined = titleDiv.nextSibling;
201
- if (plotDiv &&
202
- ((plotDiv as HTMLElement).getAttribute('name') === 'viewer-Grid' ||
203
- (plotDiv as HTMLElement).getAttribute('name') === 'viewer-OutliersSelectionViewer')) {
204
- if (titleDiv.getElementsByClassName('svg-table').length) {
205
- plotDiv = titleDiv.parentElement?.nextSibling?.firstChild?.nextSibling;
206
- skipNext = true;
207
- } else {
208
- continue;
209
- }
210
- }
211
-
212
- if (plotDiv) {
213
- const plot = (plotDiv as HTMLElement).getElementsByClassName('d4-layout-middle')[0];
214
- const canvas = await DG.HtmlUtils.renderToCanvas(plot as HTMLElement);
215
- const dataUrl = canvas.toDataURL('image/png');
216
-
217
- imageId = exportWorkbook.addImage({
218
- base64: dataUrl,
219
- extension: 'png',
220
- });
221
- width = canvas.width;
222
- height = canvas.height;
223
- }
161
+ const plotDiv: ChildNode | null | undefined = blockDiv.firstChild?.nextSibling;
162
+ await exportPlot(plotDiv, blockDiv, visibleTitle, plotCount);
224
163
  }
164
+ };
165
+ };
225
166
 
226
- if (imageId === null || imageId === undefined) continue;
227
-
228
- const worksheet = exportWorkbook.getWorksheet(getSheetName(visibleTitle, DIRECTION.OUTPUT));
229
- if (worksheet) {
230
- const columnForImage = ((call
231
- .outputs[realNames[visibleTitle]] as DG.DataFrame)
232
- .columns as DG.ColumnList)
233
- .length + 1;
234
- worksheet.addImage(imageId, {
235
- tl: {col: columnForImage, row: 0},
236
- ext: {width, height},
237
- });
238
- } else {
239
- const newWorksheet = exportWorkbook.addWorksheet(`Output - Plot - ${visibleTitle}`);
240
- newWorksheet.addImage(imageId, {
241
- tl: {col: 0, row: 0},
242
- ext: {width, height},
243
- });
244
- }
167
+ // UWAGA: very fragile solution
168
+ const funcView = document.getElementsByClassName('grok-view grok-view-func ui-box')[0];
169
+ const resultView = funcView.getElementsByClassName('d4-tab-host ui-box')[0];
170
+ if (resultView) {
171
+ const tabs = resultView.getElementsByClassName('d4-tab-header-stripe')[0].children;
172
+ const selectedByUser = resultView.getElementsByClassName('d4-tab-header selected')[0];
173
+ for (let i=0; i< tabs.length; i++) {
174
+ const tab = tabs[i] as HTMLElement;
175
+ tab.click();
176
+ await new Promise((r) => setTimeout(r, 150));
177
+ await exportTab(tab);
245
178
  }
179
+ (selectedByUser as HTMLElement).click();
180
+ } else {
181
+ const resultView = document.getElementsByClassName('ui-panel grok-func-results')[0];
182
+ await exportTab(resultView as HTMLElement);
246
183
  }
247
184
 
248
185
  if (scalarOutputs.length) {