@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 +1 -1
- package/src/export-funccall.ts +97 -160
package/package.json
CHANGED
package/src/export-funccall.ts
CHANGED
|
@@ -64,185 +64,122 @@ export async function exportFuncCall(call: DG.FuncCall) {
|
|
|
64
64
|
}
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
90
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
let height = 0;
|
|
109
|
+
if (plotDiv && !titleDiv.getElementsByClassName('svg-table').length &&
|
|
110
|
+
(plotDiv as HTMLElement).getAttribute('name') === 'viewer-Grid') return;
|
|
95
111
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
133
|
-
|
|
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
|
-
|
|
144
|
+
if ((blockDiv as HTMLElement).style.display === 'none') continue;
|
|
142
145
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
skipNext = false;
|
|
175
|
-
continue;
|
|
153
|
+
if ((blockDiv.firstChild?.nextSibling as HTMLElement).getAttribute('name') === 'viewer-OutliersSelectionViewer') {
|
|
154
|
+
plotCount--;
|
|
176
155
|
}
|
|
177
156
|
|
|
178
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
201
|
-
|
|
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
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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) {
|