1 |
|
2 |
|
3 | import { UUID, JSONExt } from '@lumino/coreutils';
|
4 | import _isEqual from 'lodash/isEqual';
|
5 |
|
6 |
|
7 |
|
8 | export function difference(a, b) {
|
9 | return a.filter((v) => b.indexOf(v) === -1);
|
10 | }
|
11 |
|
12 |
|
13 |
|
14 | export function isEqual(a, b) {
|
15 | return _isEqual(a, b);
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export const assign = Object.assign ||
|
23 | function (t, ...args) {
|
24 | for (let i = 1; i < args.length; i++) {
|
25 | const s = args[i];
|
26 | for (const p in s) {
|
27 | if (Object.prototype.hasOwnProperty.call(s, p)) {
|
28 | t[p] = s[p];
|
29 | }
|
30 | }
|
31 | }
|
32 | return t;
|
33 | };
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | export function uuid() {
|
40 | return UUID.uuid4();
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | export function resolvePromisesDict(d) {
|
47 | const keys = Object.keys(d);
|
48 | const values = [];
|
49 | keys.forEach(function (key) {
|
50 | values.push(d[key]);
|
51 | });
|
52 | return Promise.all(values).then((v) => {
|
53 | const d = {};
|
54 | for (let i = 0; i < keys.length; i++) {
|
55 | d[keys[i]] = v[i];
|
56 | }
|
57 | return d;
|
58 | });
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | export function reject(message, log) {
|
67 | return function promiseRejection(error) {
|
68 | if (log) {
|
69 | console.error(new Error(message));
|
70 | }
|
71 | throw error;
|
72 | };
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | export function put_buffers(state, buffer_paths, buffers) {
|
84 | for (let i = 0; i < buffer_paths.length; i++) {
|
85 | const buffer_path = buffer_paths[i];
|
86 |
|
87 | let buffer = buffers[i];
|
88 | if (!(buffer instanceof DataView)) {
|
89 | buffer = new DataView(buffer instanceof ArrayBuffer ? buffer : buffer.buffer);
|
90 | }
|
91 |
|
92 | let obj = state;
|
93 |
|
94 | for (let j = 0; j < buffer_path.length - 1; j++) {
|
95 | obj = obj[buffer_path[j]];
|
96 | }
|
97 |
|
98 | obj[buffer_path[buffer_path.length - 1]] = buffer;
|
99 | }
|
100 | }
|
101 | export function isSerializable(object) {
|
102 | var _a;
|
103 | return (_a = (typeof object === 'object' && object && 'toJSON' in object)) !== null && _a !== void 0 ? _a : false;
|
104 | }
|
105 | export function isObject(data) {
|
106 | return JSONExt.isObject(data);
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | export function remove_buffers(state) {
|
117 | const buffers = [];
|
118 | const buffer_paths = [];
|
119 |
|
120 |
|
121 |
|
122 | function remove(obj, path) {
|
123 | if (isSerializable(obj)) {
|
124 |
|
125 |
|
126 | obj = obj.toJSON();
|
127 | }
|
128 | if (Array.isArray(obj)) {
|
129 | let is_cloned = false;
|
130 | for (let i = 0; i < obj.length; i++) {
|
131 | const value = obj[i];
|
132 | if (value) {
|
133 | if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
|
134 | if (!is_cloned) {
|
135 | obj = obj.slice();
|
136 | is_cloned = true;
|
137 | }
|
138 | buffers.push(ArrayBuffer.isView(value) ? value.buffer : value);
|
139 | buffer_paths.push(path.concat([i]));
|
140 |
|
141 |
|
142 | obj[i] = null;
|
143 | }
|
144 | else {
|
145 | const new_value = remove(value, path.concat([i]));
|
146 |
|
147 | if (new_value !== value) {
|
148 | if (!is_cloned) {
|
149 | obj = obj.slice();
|
150 | is_cloned = true;
|
151 | }
|
152 | obj[i] = new_value;
|
153 | }
|
154 | }
|
155 | }
|
156 | }
|
157 | }
|
158 | else if (isObject(obj)) {
|
159 | for (const key in obj) {
|
160 | let is_cloned = false;
|
161 | if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
162 | const value = obj[key];
|
163 | if (value) {
|
164 | if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
|
165 | if (!is_cloned) {
|
166 | obj = Object.assign({}, obj);
|
167 | is_cloned = true;
|
168 | }
|
169 | buffers.push(ArrayBuffer.isView(value) ? value.buffer : value);
|
170 | buffer_paths.push(path.concat([key]));
|
171 | delete obj[key];
|
172 | }
|
173 | else {
|
174 | const new_value = remove(value, path.concat([key]));
|
175 |
|
176 | if (new_value !== value) {
|
177 | if (!is_cloned) {
|
178 | obj = Object.assign({}, obj);
|
179 | is_cloned = true;
|
180 | }
|
181 | obj[key] = new_value;
|
182 | }
|
183 | }
|
184 | }
|
185 | }
|
186 | }
|
187 | }
|
188 | return obj;
|
189 | }
|
190 | const new_state = remove(state, []);
|
191 | return { state: new_state, buffers: buffers, buffer_paths: buffer_paths };
|
192 | }
|
193 | export const BROKEN_FILE_SVG_ICON = `<svg style="height:50%;max-height: 50px;" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48">
|
194 | <g >
|
195 | <g transform="translate(0.24520123,0.93464292)">
|
196 | <path d="M 8.2494641,21.074514 V 5.6225142 c 0,-0.314 0.254,-0.567 0.57,-0.567 H 29.978464 c 2.388,0 9.268,5.8269998 9.268,8.3029998 v 5.5835 l -3.585749,4.407396 -2.772971,-3.535534 -5.126524,3.414213 -5.944543,-3.237436 -5.722718,3.06066 z m 30.9969999,3.8675 v 15.5835 c 0,0.314 -0.254,0.567 -0.57,0.567 H 8.8194641 c -0.315,0.002 -0.57,-0.251 -0.57,-0.566 v -15.452 l 7.8444949,2.628449 5.656854,-2.65165 4.24264,3.005204 5.833631,-3.237437 3.712311,3.944543 z" style="fill:url(#linearGradient3448);stroke:#888a85" />
|
197 | <path d="m 30.383464,12.110514 c 4.108,0.159 7.304,-0.978 8.867,1.446 0.304,-3.9679998 -7.254,-8.8279998 -9.285,-8.4979998 0.813,0.498 0.418,7.0519998 0.418,7.0519998 z" style="fill:url(#linearGradient3445);stroke:#868a84" />
|
198 | <path enable-background="new" d="m 31.443464,11.086514 c 2.754,-0.019 4.106,-0.49 5.702,0.19 -1.299,-1.8809998 -4.358,-3.3439998 -5.728,-4.0279998 0.188,0.775 0.026,3.8379998 0.026,3.8379998 z" style="opacity:0.36930003;fill:none;stroke:url(#linearGradient3442)" />
|
199 | </g>
|
200 | </g>
|
201 | </svg>`;
|
202 |
|
\ | No newline at end of file |