@thinkpixellab-public/px-vue 3.0.10 → 3.0.11
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/utils/cardGridLayoutHelper.js +370 -0
package/package.json
CHANGED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// prettier-ignore
|
|
2
|
+
const baseTemplates = [
|
|
3
|
+
|
|
4
|
+
// TWO column layouts
|
|
5
|
+
|
|
6
|
+
// 2x1
|
|
7
|
+
|
|
8
|
+
[
|
|
9
|
+
'-----'
|
|
10
|
+
],
|
|
11
|
+
[
|
|
12
|
+
'--'
|
|
13
|
+
],
|
|
14
|
+
|
|
15
|
+
// 2x2
|
|
16
|
+
|
|
17
|
+
[
|
|
18
|
+
'-----',
|
|
19
|
+
'-----'
|
|
20
|
+
],
|
|
21
|
+
[
|
|
22
|
+
'-- --'
|
|
23
|
+
],
|
|
24
|
+
|
|
25
|
+
// 2x3
|
|
26
|
+
|
|
27
|
+
[
|
|
28
|
+
'-----',
|
|
29
|
+
'-- --'
|
|
30
|
+
],
|
|
31
|
+
[
|
|
32
|
+
'-----',
|
|
33
|
+
'-- --'
|
|
34
|
+
],
|
|
35
|
+
[
|
|
36
|
+
'-- =='
|
|
37
|
+
],
|
|
38
|
+
|
|
39
|
+
// 2x4
|
|
40
|
+
|
|
41
|
+
[
|
|
42
|
+
'-- --',
|
|
43
|
+
'-- --'
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
'-----',
|
|
47
|
+
'-- =='
|
|
48
|
+
],
|
|
49
|
+
|
|
50
|
+
// 2x5
|
|
51
|
+
|
|
52
|
+
[
|
|
53
|
+
'-----',
|
|
54
|
+
'-- --',
|
|
55
|
+
'-- --'
|
|
56
|
+
],
|
|
57
|
+
[
|
|
58
|
+
'-----',
|
|
59
|
+
'-----',
|
|
60
|
+
'-- =='
|
|
61
|
+
],
|
|
62
|
+
[
|
|
63
|
+
'-- --',
|
|
64
|
+
'-- =='
|
|
65
|
+
],
|
|
66
|
+
|
|
67
|
+
// 2x6
|
|
68
|
+
|
|
69
|
+
[
|
|
70
|
+
'-- --',
|
|
71
|
+
'-- --',
|
|
72
|
+
'-- --'
|
|
73
|
+
],
|
|
74
|
+
[
|
|
75
|
+
'-----',
|
|
76
|
+
'-- --',
|
|
77
|
+
'-- =='
|
|
78
|
+
],
|
|
79
|
+
[
|
|
80
|
+
'-- --',
|
|
81
|
+
'== =='
|
|
82
|
+
],
|
|
83
|
+
|
|
84
|
+
// 2x7
|
|
85
|
+
|
|
86
|
+
[
|
|
87
|
+
'-----',
|
|
88
|
+
'-- --',
|
|
89
|
+
'-- --',
|
|
90
|
+
'-- --'
|
|
91
|
+
],
|
|
92
|
+
[
|
|
93
|
+
'-- --',
|
|
94
|
+
'-- --',
|
|
95
|
+
'-- =='
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
'-----',
|
|
99
|
+
'-- --',
|
|
100
|
+
'== =='
|
|
101
|
+
],
|
|
102
|
+
|
|
103
|
+
// 2x8
|
|
104
|
+
|
|
105
|
+
[
|
|
106
|
+
'-- --',
|
|
107
|
+
'-- --',
|
|
108
|
+
'-- --',
|
|
109
|
+
'-- --'
|
|
110
|
+
],
|
|
111
|
+
[
|
|
112
|
+
'-----',
|
|
113
|
+
'-- --',
|
|
114
|
+
'-----',
|
|
115
|
+
'-- --',
|
|
116
|
+
'-- --'
|
|
117
|
+
],
|
|
118
|
+
[
|
|
119
|
+
'-----',
|
|
120
|
+
'-- --',
|
|
121
|
+
'-----',
|
|
122
|
+
'== =='
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
// THREE column layouts
|
|
126
|
+
|
|
127
|
+
// 3x1
|
|
128
|
+
|
|
129
|
+
[
|
|
130
|
+
'-----'
|
|
131
|
+
],
|
|
132
|
+
[
|
|
133
|
+
'--'
|
|
134
|
+
],
|
|
135
|
+
|
|
136
|
+
// 3x2
|
|
137
|
+
|
|
138
|
+
[
|
|
139
|
+
'----- --'
|
|
140
|
+
],
|
|
141
|
+
[
|
|
142
|
+
'-- --'
|
|
143
|
+
],
|
|
144
|
+
|
|
145
|
+
// 3x3
|
|
146
|
+
|
|
147
|
+
[
|
|
148
|
+
'-- -- --'
|
|
149
|
+
],
|
|
150
|
+
[
|
|
151
|
+
'----- =='
|
|
152
|
+
],
|
|
153
|
+
|
|
154
|
+
// 3x4
|
|
155
|
+
|
|
156
|
+
[
|
|
157
|
+
'----- --',
|
|
158
|
+
'-- -----'
|
|
159
|
+
],
|
|
160
|
+
[
|
|
161
|
+
'-- -- =='
|
|
162
|
+
],
|
|
163
|
+
|
|
164
|
+
// 3x5
|
|
165
|
+
|
|
166
|
+
[
|
|
167
|
+
'----- --',
|
|
168
|
+
'-- -- --'
|
|
169
|
+
],
|
|
170
|
+
[
|
|
171
|
+
'----- --',
|
|
172
|
+
'----- =='
|
|
173
|
+
],
|
|
174
|
+
[
|
|
175
|
+
'-- == =='
|
|
176
|
+
],
|
|
177
|
+
|
|
178
|
+
// 3x6
|
|
179
|
+
|
|
180
|
+
[
|
|
181
|
+
'-- -- --',
|
|
182
|
+
'-- -- --'
|
|
183
|
+
],
|
|
184
|
+
[
|
|
185
|
+
'----- --',
|
|
186
|
+
'-- -- =='
|
|
187
|
+
],
|
|
188
|
+
[
|
|
189
|
+
'----- ==',
|
|
190
|
+
'-- -- --'
|
|
191
|
+
],
|
|
192
|
+
|
|
193
|
+
// 3x7
|
|
194
|
+
|
|
195
|
+
[
|
|
196
|
+
'----- --',
|
|
197
|
+
'----- --',
|
|
198
|
+
'-- -- --'
|
|
199
|
+
],
|
|
200
|
+
[
|
|
201
|
+
'-- -- ==',
|
|
202
|
+
'-- -- --'
|
|
203
|
+
],
|
|
204
|
+
[
|
|
205
|
+
'----- ==',
|
|
206
|
+
'-- -- =='
|
|
207
|
+
],
|
|
208
|
+
|
|
209
|
+
// 3x8
|
|
210
|
+
|
|
211
|
+
[
|
|
212
|
+
'----- --',
|
|
213
|
+
'-- -- --',
|
|
214
|
+
'-- -- --'
|
|
215
|
+
],
|
|
216
|
+
[
|
|
217
|
+
'----- --',
|
|
218
|
+
'-- -----',
|
|
219
|
+
'-- -- =='
|
|
220
|
+
],
|
|
221
|
+
[
|
|
222
|
+
'----- --',
|
|
223
|
+
'== == =='
|
|
224
|
+
],
|
|
225
|
+
|
|
226
|
+
// 3x9
|
|
227
|
+
|
|
228
|
+
[
|
|
229
|
+
'----- --',
|
|
230
|
+
'-- -----',
|
|
231
|
+
'----- --',
|
|
232
|
+
'-- -- --'
|
|
233
|
+
],
|
|
234
|
+
[
|
|
235
|
+
'-- -- --',
|
|
236
|
+
'-- -- --',
|
|
237
|
+
'-- -- --'
|
|
238
|
+
],
|
|
239
|
+
[
|
|
240
|
+
'----- --',
|
|
241
|
+
'-- -- --',
|
|
242
|
+
'-- -- =='
|
|
243
|
+
],
|
|
244
|
+
|
|
245
|
+
// 3x10
|
|
246
|
+
|
|
247
|
+
[
|
|
248
|
+
'----- --',
|
|
249
|
+
'-- -----',
|
|
250
|
+
'-- -- --',
|
|
251
|
+
'-- -- --'
|
|
252
|
+
],
|
|
253
|
+
[
|
|
254
|
+
'----- --',
|
|
255
|
+
'-- -- ==',
|
|
256
|
+
'== -- --'
|
|
257
|
+
],
|
|
258
|
+
[
|
|
259
|
+
'----- ==',
|
|
260
|
+
'-- -- --',
|
|
261
|
+
'== -- --'
|
|
262
|
+
]
|
|
263
|
+
|
|
264
|
+
];
|
|
265
|
+
|
|
266
|
+
// returns the reversed version of a string
|
|
267
|
+
// fastest according to https://jsbench.me/4bkfflcm2z
|
|
268
|
+
const reverseString = str => {
|
|
269
|
+
return str.split('').reduce((reversed, character) => character + reversed, '');
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// returns the reversed version of a layout template
|
|
273
|
+
const reverseTemplate = template => {
|
|
274
|
+
return template.map(str => reverseString(str));
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// merge two templates horizontally
|
|
278
|
+
// e.g. mergeH(['-- -----'], ['==']) => ['-- ----- ==']
|
|
279
|
+
const mergeH = (a, b) => {
|
|
280
|
+
if (!a || !a.length || !b || b.length || a.length !== b.length) {
|
|
281
|
+
console.warn(
|
|
282
|
+
'Cannot horizontally merge empty templates or templates of different lengths.'
|
|
283
|
+
);
|
|
284
|
+
return [];
|
|
285
|
+
}
|
|
286
|
+
return a.map((val, idx) => {
|
|
287
|
+
return a[idx] + ' ' + b[idx];
|
|
288
|
+
});
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
// merge two templates vertically
|
|
292
|
+
const mergeV = (a, b) => {
|
|
293
|
+
if (!a || !a.length || !b || b.length || a[0].length !== b[0].length) {
|
|
294
|
+
console.warn('Cannot vertical merge empty templates or templates of different widths.');
|
|
295
|
+
return [];
|
|
296
|
+
}
|
|
297
|
+
return [...a, ...b];
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// get cell counts for a template
|
|
301
|
+
const getCounts = t => {
|
|
302
|
+
if (!t || !t.length) {
|
|
303
|
+
return { cols: 0, cellCount: 0 };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const spaces = t
|
|
307
|
+
.join(' ')
|
|
308
|
+
.split(' ')
|
|
309
|
+
.map(s => s.trim());
|
|
310
|
+
|
|
311
|
+
// (cols, totalCells, wideCells, stackedCells)
|
|
312
|
+
const defaults = spaces.filter(s => s == '--').length;
|
|
313
|
+
const wides = spaces.filter(s => s == '-----').length;
|
|
314
|
+
const stacks = spaces.filter(s => s == '==').length;
|
|
315
|
+
|
|
316
|
+
return {
|
|
317
|
+
cols: (t[0].length + 1) / 3,
|
|
318
|
+
cells: defaults + wides + stacks * 2,
|
|
319
|
+
defaults,
|
|
320
|
+
wides,
|
|
321
|
+
stacks,
|
|
322
|
+
};
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
const templates = {};
|
|
326
|
+
|
|
327
|
+
// organize base templates by column and cell count
|
|
328
|
+
|
|
329
|
+
// TODO make this an on-demand generated things
|
|
330
|
+
baseTemplates.forEach(t => {
|
|
331
|
+
const counts = getCounts(t);
|
|
332
|
+
|
|
333
|
+
if (counts.cols && counts.cells) {
|
|
334
|
+
// by column count
|
|
335
|
+
if (!templates[counts.cols]) {
|
|
336
|
+
templates[counts.cols] = {};
|
|
337
|
+
}
|
|
338
|
+
const byCols = templates[counts.cols];
|
|
339
|
+
|
|
340
|
+
// by cell count
|
|
341
|
+
if (!byCols[counts.cells]) {
|
|
342
|
+
byCols[counts.cells] = [];
|
|
343
|
+
}
|
|
344
|
+
const cellsList = byCols[counts.cells];
|
|
345
|
+
|
|
346
|
+
// add to the list of templates
|
|
347
|
+
cellsList.push({ template: t, ...counts });
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
const getLayouts = (cols, cells) => {
|
|
352
|
+
if (!templates[cols]) {
|
|
353
|
+
// TODO: support undefined sizes by merging if needed
|
|
354
|
+
// generate usine mergeH
|
|
355
|
+
console.log(`No templates for ${cols} columns.`);
|
|
356
|
+
return [];
|
|
357
|
+
}
|
|
358
|
+
if (!templates[cols][cells]) {
|
|
359
|
+
// TODO: support undefined sizes by merging if needed
|
|
360
|
+
// generate using mergeV
|
|
361
|
+
console.log(`No templates for ${cols} columns and ${cells} items.`);
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// find templates that have the correct number of columns and cells
|
|
366
|
+
return templates[cols][cells];
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
export default { getLayouts };
|
|
370
|
+
export { mergeH, mergeV, reverseString, reverseTemplate };
|