@react-spectrum/card 3.0.0-alpha.52 → 3.0.0-alpha.54

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.
Files changed (240) hide show
  1. package/dist/import.mjs +6 -6
  2. package/dist/main.js +10 -10
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +6 -6
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +9 -0
  7. package/package.json +12 -40
  8. package/src/index.ts +9 -9
  9. package/dist/BaseLayout.main.js +0 -213
  10. package/dist/BaseLayout.main.js.map +0 -1
  11. package/dist/BaseLayout.mjs +0 -208
  12. package/dist/BaseLayout.module.js +0 -208
  13. package/dist/BaseLayout.module.js.map +0 -1
  14. package/dist/Card.main.js +0 -52
  15. package/dist/Card.main.js.map +0 -1
  16. package/dist/Card.mjs +0 -43
  17. package/dist/Card.module.js +0 -43
  18. package/dist/Card.module.js.map +0 -1
  19. package/dist/CardBase.main.js +0 -205
  20. package/dist/CardBase.main.js.map +0 -1
  21. package/dist/CardBase.mjs +0 -200
  22. package/dist/CardBase.module.js +0 -200
  23. package/dist/CardBase.module.js.map +0 -1
  24. package/dist/CardView.main.js +0 -238
  25. package/dist/CardView.main.js.map +0 -1
  26. package/dist/CardView.mjs +0 -233
  27. package/dist/CardView.module.js +0 -233
  28. package/dist/CardView.module.js.map +0 -1
  29. package/dist/CardViewContext.main.js +0 -32
  30. package/dist/CardViewContext.main.js.map +0 -1
  31. package/dist/CardViewContext.mjs +0 -22
  32. package/dist/CardViewContext.module.js +0 -22
  33. package/dist/CardViewContext.module.js.map +0 -1
  34. package/dist/GalleryLayout.main.js +0 -230
  35. package/dist/GalleryLayout.main.js.map +0 -1
  36. package/dist/GalleryLayout.mjs +0 -225
  37. package/dist/GalleryLayout.module.js +0 -225
  38. package/dist/GalleryLayout.module.js.map +0 -1
  39. package/dist/GridLayout.main.js +0 -175
  40. package/dist/GridLayout.main.js.map +0 -1
  41. package/dist/GridLayout.mjs +0 -170
  42. package/dist/GridLayout.module.js +0 -170
  43. package/dist/GridLayout.module.js.map +0 -1
  44. package/dist/WaterfallLayout.main.js +0 -182
  45. package/dist/WaterfallLayout.main.js.map +0 -1
  46. package/dist/WaterfallLayout.mjs +0 -177
  47. package/dist/WaterfallLayout.module.js +0 -177
  48. package/dist/WaterfallLayout.module.js.map +0 -1
  49. package/dist/ar-AE.main.js +0 -7
  50. package/dist/ar-AE.main.js.map +0 -1
  51. package/dist/ar-AE.mjs +0 -9
  52. package/dist/ar-AE.module.js +0 -9
  53. package/dist/ar-AE.module.js.map +0 -1
  54. package/dist/bg-BG.main.js +0 -7
  55. package/dist/bg-BG.main.js.map +0 -1
  56. package/dist/bg-BG.mjs +0 -9
  57. package/dist/bg-BG.module.js +0 -9
  58. package/dist/bg-BG.module.js.map +0 -1
  59. package/dist/card.4d5d6544.css +0 -1671
  60. package/dist/card.4d5d6544.css.map +0 -1
  61. package/dist/card_vars_css.main.js +0 -116
  62. package/dist/card_vars_css.main.js.map +0 -1
  63. package/dist/card_vars_css.mjs +0 -118
  64. package/dist/card_vars_css.module.js +0 -118
  65. package/dist/card_vars_css.module.js.map +0 -1
  66. package/dist/cs-CZ.main.js +0 -7
  67. package/dist/cs-CZ.main.js.map +0 -1
  68. package/dist/cs-CZ.mjs +0 -9
  69. package/dist/cs-CZ.module.js +0 -9
  70. package/dist/cs-CZ.module.js.map +0 -1
  71. package/dist/da-DK.main.js +0 -7
  72. package/dist/da-DK.main.js.map +0 -1
  73. package/dist/da-DK.mjs +0 -9
  74. package/dist/da-DK.module.js +0 -9
  75. package/dist/da-DK.module.js.map +0 -1
  76. package/dist/de-DE.main.js +0 -7
  77. package/dist/de-DE.main.js.map +0 -1
  78. package/dist/de-DE.mjs +0 -9
  79. package/dist/de-DE.module.js +0 -9
  80. package/dist/de-DE.module.js.map +0 -1
  81. package/dist/el-GR.main.js +0 -7
  82. package/dist/el-GR.main.js.map +0 -1
  83. package/dist/el-GR.mjs +0 -9
  84. package/dist/el-GR.module.js +0 -9
  85. package/dist/el-GR.module.js.map +0 -1
  86. package/dist/en-US.main.js +0 -7
  87. package/dist/en-US.main.js.map +0 -1
  88. package/dist/en-US.mjs +0 -9
  89. package/dist/en-US.module.js +0 -9
  90. package/dist/en-US.module.js.map +0 -1
  91. package/dist/es-ES.main.js +0 -7
  92. package/dist/es-ES.main.js.map +0 -1
  93. package/dist/es-ES.mjs +0 -9
  94. package/dist/es-ES.module.js +0 -9
  95. package/dist/es-ES.module.js.map +0 -1
  96. package/dist/et-EE.main.js +0 -7
  97. package/dist/et-EE.main.js.map +0 -1
  98. package/dist/et-EE.mjs +0 -9
  99. package/dist/et-EE.module.js +0 -9
  100. package/dist/et-EE.module.js.map +0 -1
  101. package/dist/fi-FI.main.js +0 -7
  102. package/dist/fi-FI.main.js.map +0 -1
  103. package/dist/fi-FI.mjs +0 -9
  104. package/dist/fi-FI.module.js +0 -9
  105. package/dist/fi-FI.module.js.map +0 -1
  106. package/dist/fr-FR.main.js +0 -7
  107. package/dist/fr-FR.main.js.map +0 -1
  108. package/dist/fr-FR.mjs +0 -9
  109. package/dist/fr-FR.module.js +0 -9
  110. package/dist/fr-FR.module.js.map +0 -1
  111. package/dist/he-IL.main.js +0 -7
  112. package/dist/he-IL.main.js.map +0 -1
  113. package/dist/he-IL.mjs +0 -9
  114. package/dist/he-IL.module.js +0 -9
  115. package/dist/he-IL.module.js.map +0 -1
  116. package/dist/hr-HR.main.js +0 -7
  117. package/dist/hr-HR.main.js.map +0 -1
  118. package/dist/hr-HR.mjs +0 -9
  119. package/dist/hr-HR.module.js +0 -9
  120. package/dist/hr-HR.module.js.map +0 -1
  121. package/dist/hu-HU.main.js +0 -7
  122. package/dist/hu-HU.main.js.map +0 -1
  123. package/dist/hu-HU.mjs +0 -9
  124. package/dist/hu-HU.module.js +0 -9
  125. package/dist/hu-HU.module.js.map +0 -1
  126. package/dist/intlStrings.main.js +0 -108
  127. package/dist/intlStrings.main.js.map +0 -1
  128. package/dist/intlStrings.mjs +0 -110
  129. package/dist/intlStrings.module.js +0 -110
  130. package/dist/intlStrings.module.js.map +0 -1
  131. package/dist/it-IT.main.js +0 -7
  132. package/dist/it-IT.main.js.map +0 -1
  133. package/dist/it-IT.mjs +0 -9
  134. package/dist/it-IT.module.js +0 -9
  135. package/dist/it-IT.module.js.map +0 -1
  136. package/dist/ja-JP.main.js +0 -7
  137. package/dist/ja-JP.main.js.map +0 -1
  138. package/dist/ja-JP.mjs +0 -9
  139. package/dist/ja-JP.module.js +0 -9
  140. package/dist/ja-JP.module.js.map +0 -1
  141. package/dist/ko-KR.main.js +0 -7
  142. package/dist/ko-KR.main.js.map +0 -1
  143. package/dist/ko-KR.mjs +0 -9
  144. package/dist/ko-KR.module.js +0 -9
  145. package/dist/ko-KR.module.js.map +0 -1
  146. package/dist/lt-LT.main.js +0 -7
  147. package/dist/lt-LT.main.js.map +0 -1
  148. package/dist/lt-LT.mjs +0 -9
  149. package/dist/lt-LT.module.js +0 -9
  150. package/dist/lt-LT.module.js.map +0 -1
  151. package/dist/lv-LV.main.js +0 -7
  152. package/dist/lv-LV.main.js.map +0 -1
  153. package/dist/lv-LV.mjs +0 -9
  154. package/dist/lv-LV.module.js +0 -9
  155. package/dist/lv-LV.module.js.map +0 -1
  156. package/dist/nb-NO.main.js +0 -7
  157. package/dist/nb-NO.main.js.map +0 -1
  158. package/dist/nb-NO.mjs +0 -9
  159. package/dist/nb-NO.module.js +0 -9
  160. package/dist/nb-NO.module.js.map +0 -1
  161. package/dist/nl-NL.main.js +0 -7
  162. package/dist/nl-NL.main.js.map +0 -1
  163. package/dist/nl-NL.mjs +0 -9
  164. package/dist/nl-NL.module.js +0 -9
  165. package/dist/nl-NL.module.js.map +0 -1
  166. package/dist/pl-PL.main.js +0 -7
  167. package/dist/pl-PL.main.js.map +0 -1
  168. package/dist/pl-PL.mjs +0 -9
  169. package/dist/pl-PL.module.js +0 -9
  170. package/dist/pl-PL.module.js.map +0 -1
  171. package/dist/pt-BR.main.js +0 -7
  172. package/dist/pt-BR.main.js.map +0 -1
  173. package/dist/pt-BR.mjs +0 -9
  174. package/dist/pt-BR.module.js +0 -9
  175. package/dist/pt-BR.module.js.map +0 -1
  176. package/dist/pt-PT.main.js +0 -7
  177. package/dist/pt-PT.main.js.map +0 -1
  178. package/dist/pt-PT.mjs +0 -9
  179. package/dist/pt-PT.module.js +0 -9
  180. package/dist/pt-PT.module.js.map +0 -1
  181. package/dist/ro-RO.main.js +0 -7
  182. package/dist/ro-RO.main.js.map +0 -1
  183. package/dist/ro-RO.mjs +0 -9
  184. package/dist/ro-RO.module.js +0 -9
  185. package/dist/ro-RO.module.js.map +0 -1
  186. package/dist/ru-RU.main.js +0 -7
  187. package/dist/ru-RU.main.js.map +0 -1
  188. package/dist/ru-RU.mjs +0 -9
  189. package/dist/ru-RU.module.js +0 -9
  190. package/dist/ru-RU.module.js.map +0 -1
  191. package/dist/sk-SK.main.js +0 -7
  192. package/dist/sk-SK.main.js.map +0 -1
  193. package/dist/sk-SK.mjs +0 -9
  194. package/dist/sk-SK.module.js +0 -9
  195. package/dist/sk-SK.module.js.map +0 -1
  196. package/dist/sl-SI.main.js +0 -7
  197. package/dist/sl-SI.main.js.map +0 -1
  198. package/dist/sl-SI.mjs +0 -9
  199. package/dist/sl-SI.module.js +0 -9
  200. package/dist/sl-SI.module.js.map +0 -1
  201. package/dist/sr-SP.main.js +0 -7
  202. package/dist/sr-SP.main.js.map +0 -1
  203. package/dist/sr-SP.mjs +0 -9
  204. package/dist/sr-SP.module.js +0 -9
  205. package/dist/sr-SP.module.js.map +0 -1
  206. package/dist/sv-SE.main.js +0 -7
  207. package/dist/sv-SE.main.js.map +0 -1
  208. package/dist/sv-SE.mjs +0 -9
  209. package/dist/sv-SE.module.js +0 -9
  210. package/dist/sv-SE.module.js.map +0 -1
  211. package/dist/tr-TR.main.js +0 -7
  212. package/dist/tr-TR.main.js.map +0 -1
  213. package/dist/tr-TR.mjs +0 -9
  214. package/dist/tr-TR.module.js +0 -9
  215. package/dist/tr-TR.module.js.map +0 -1
  216. package/dist/types.d.ts +0 -194
  217. package/dist/types.d.ts.map +0 -1
  218. package/dist/uk-UA.main.js +0 -7
  219. package/dist/uk-UA.main.js.map +0 -1
  220. package/dist/uk-UA.mjs +0 -9
  221. package/dist/uk-UA.module.js +0 -9
  222. package/dist/uk-UA.module.js.map +0 -1
  223. package/dist/zh-CN.main.js +0 -7
  224. package/dist/zh-CN.main.js.map +0 -1
  225. package/dist/zh-CN.mjs +0 -9
  226. package/dist/zh-CN.module.js +0 -9
  227. package/dist/zh-CN.module.js.map +0 -1
  228. package/dist/zh-TW.main.js +0 -7
  229. package/dist/zh-TW.main.js.map +0 -1
  230. package/dist/zh-TW.mjs +0 -9
  231. package/dist/zh-TW.module.js +0 -9
  232. package/dist/zh-TW.module.js.map +0 -1
  233. package/src/BaseLayout.tsx +0 -290
  234. package/src/Card.tsx +0 -48
  235. package/src/CardBase.tsx +0 -188
  236. package/src/CardView.tsx +0 -250
  237. package/src/CardViewContext.tsx +0 -27
  238. package/src/GalleryLayout.tsx +0 -321
  239. package/src/GridLayout.tsx +0 -242
  240. package/src/WaterfallLayout.tsx +0 -241
@@ -1,321 +0,0 @@
1
- // @ts-nocheck
2
- /*
3
- * Copyright 2021 Adobe. All rights reserved.
4
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License. You may obtain a copy
6
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
7
- *
8
- * Unless required by applicable law or agreed to in writing, software distributed under
9
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10
- * OF ANY KIND, either express or implied. See the License for the specific language
11
- * governing permissions and limitations under the License.
12
- */
13
-
14
- import {BaseLayout, BaseLayoutOptions} from './BaseLayout';
15
- import {LayoutInfo, Rect, Size} from '@react-stately/virtualizer';
16
-
17
- export interface GalleryLayoutOptions extends BaseLayoutOptions {
18
- // /**
19
- // * The card size in the grid.
20
- // */
21
- // cardSize?: 'S' | 'M' | 'L',
22
- /**
23
- * The the default row height. Note this must be larger than the min item height.
24
- * @default 208
25
- */
26
- idealRowHeight?: number,
27
- /**
28
- * The spacing between items.
29
- * @default 18 x 18
30
- */
31
- itemSpacing?: Size,
32
- /**
33
- * The vertical padding for an item.
34
- * @default 78
35
- */
36
- itemPadding?: number,
37
- /**
38
- * Minimum size for a item in the grid.
39
- * @default 136 x 136
40
- */
41
- minItemSize?: Size,
42
- /**
43
- * Target for adding extra weight to elements during linear partitioning. Anything with an aspect ratio smaler than this value
44
- * will be targeted.
45
- * @type {number}
46
- */
47
- threshold?: number
48
- }
49
-
50
- const DEFAULT_OPTIONS = {
51
- S: {
52
- idealRowHeight: 112,
53
- minItemSize: new Size(96, 96),
54
- itemSpacing: new Size(8, 16),
55
- itemPadding: 24,
56
- dropSpacing: 50,
57
- margin: 8
58
- },
59
- L: {
60
- idealRowHeight: 208,
61
- minItemSize: new Size(136, 136),
62
- itemSpacing: new Size(18, 18),
63
- itemPadding: {
64
- 'medium': 78,
65
- 'large': 99
66
- },
67
- dropSpacing: 100,
68
- margin: 24
69
- }
70
- };
71
-
72
- export class GalleryLayout<T> extends BaseLayout<T> {
73
- protected idealRowHeight: number;
74
- protected itemSpacing: Size;
75
- itemPadding: number;
76
- protected minItemSize: Size;
77
- protected threshold: number;
78
-
79
- constructor(options: GalleryLayoutOptions = {}) {
80
- super(options);
81
- let cardSize = 'L';
82
- this.idealRowHeight = options.idealRowHeight || DEFAULT_OPTIONS[cardSize].idealRowHeight;
83
- this.itemSpacing = options.itemSpacing || DEFAULT_OPTIONS[cardSize].itemSpacing;
84
- this.itemPadding = options.itemPadding != null ? options.itemPadding : DEFAULT_OPTIONS[cardSize].itemPadding[this.scale];
85
- this.minItemSize = options.minItemSize || DEFAULT_OPTIONS[cardSize].minItemSize;
86
- this.threshold = options.threshold || 1;
87
- this.margin = options.margin != null ? options.margin : DEFAULT_OPTIONS[cardSize].margin;
88
- }
89
-
90
- get layoutType(): string {
91
- return 'gallery';
92
- }
93
-
94
- /**
95
- * Takes a row of widths and if there are any widths smaller than the min-width, leech width starting from
96
- * the widest in the row until it can't give anymore, then move to the second widest and so forth.
97
- * Do this until all assets meet the min-width.
98
- * */
99
- _distributeWidths(widths: number[]): boolean {
100
- // create a copy of the widths array and sort it largest to smallest
101
- let sortedWidths = widths.concat().sort((a, b) => a[1] > b[1] ? -1 : 1);
102
- for (let width of widths) {
103
- // for each width, if it's smaller than the min width
104
- if (width[1] < this.minItemSize.width) {
105
- // then figure out how much smaller
106
- let delta = this.minItemSize.width - width[1];
107
- for (let item of sortedWidths) {
108
- // go from the largest width in the row to the smallest
109
- // if the width is greater than the min width
110
- if (widths[item[0]][1] > this.minItemSize.width) {
111
- // subtract the delta from the width, if it's still greater than the min width
112
- // then we have finished, subtract the delta permanently from that width
113
- // eslint-disable-next-line max-depth
114
- if (widths[item[0]][1] - delta > this.minItemSize.width) {
115
- widths[item[0]][1] -= delta;
116
- delta = 0;
117
- break;
118
- } else {
119
- // otherwise, we take as much as we can from the current width and then move on to
120
- // the next largest and take some width from it
121
- let maxChange = widths[item[0]][1] - this.minItemSize.width;
122
- delta -= maxChange;
123
- widths[item[0]][1] -= maxChange;
124
- }
125
- }
126
- }
127
- if (delta > 0) {
128
- return false;
129
- }
130
- // force the width to be the min width that we just rebalanced for
131
- width[1] = this.minItemSize.width;
132
- }
133
- }
134
- return true;
135
- }
136
-
137
- buildCollection(): void {
138
- let visibleWidth = this.virtualizer.visibleRect.width;
139
- let visibleHeight = this.virtualizer.visibleRect.height;
140
- let y = this.margin;
141
- let availableWidth = visibleWidth - this.margin * 2;
142
-
143
- // If available width is not greater than 0, skip node layout calculations
144
- if (availableWidth > 0) {
145
- // Compute aspect ratios for all of the items, and the total width if all items were on in a single row.
146
- let ratios = [];
147
- let totalWidth = 0;
148
- let minRatio = this.minItemSize.width / this.minItemSize.height;
149
- let maxRatio = availableWidth / this.minItemSize.height;
150
-
151
- for (let node of this.collection) {
152
- let ratio = node.props.width / node.props.height;
153
- if (ratio < minRatio) {
154
- ratio = minRatio;
155
- } else if (ratio > maxRatio && ratio !== minRatio) {
156
- ratio = maxRatio;
157
- }
158
-
159
- let itemWidth = ratio * this.minItemSize.height;
160
- ratios.push(ratio);
161
- totalWidth += itemWidth;
162
- }
163
-
164
- totalWidth += this.itemSpacing.width * (this.collection.size - 1);
165
-
166
- // Determine how many rows we'll need, and partition the items into rows
167
- // using the aspect ratios as weights.
168
- let rows = Math.max(1, Math.ceil(totalWidth / availableWidth));
169
- // if the available width can't hold two items, then every item will get its own row
170
- // this leads to a faster run through linear partition and more dependable output for small row widths
171
- if (availableWidth <= (this.minItemSize.width * 2) + (this.itemPadding * 2)) {
172
- rows = this.collection.size;
173
- }
174
-
175
- let weightedRatios = ratios.map(ratio => ratio < this.threshold ? ratio + (0.5 * (1 / ratio)) : ratio);
176
- let partition = linearPartition(weightedRatios, rows);
177
-
178
- let index = 0;
179
- for (let row of partition) {
180
- // Compute the total weight for this row
181
- let totalWeight = 0;
182
- for (let j = index; j < index + row.length; j++) {
183
- totalWeight += ratios[j];
184
- }
185
-
186
- // Determine the row height based on the total available width and weight of this row.
187
- let bestRowHeight = (availableWidth - (row.length - 1) * this.itemSpacing.width) / totalWeight;
188
-
189
- // if this is the last row and the row height is >2x the ideal row height, then cap to the ideal height
190
- // probably doing this because if the last row has one extremely tall image, then the row becomes huge
191
- // though that can happen anywhere if a row has lots of tall images... so i'm not sure why this one matters
192
- if (row === partition[partition.length - 1] && bestRowHeight > this.idealRowHeight * 2) {
193
- bestRowHeight = this.idealRowHeight;
194
- }
195
- let itemHeight = Math.round(bestRowHeight) + this.itemPadding;
196
- let x = this.margin;
197
-
198
- // if any items are going to end up too small, add a bit of width to them and subtract it from wider objects
199
- let widths = [];
200
- for (let j = index; j < index + row.length; j++) {
201
- let width = Math.round(bestRowHeight * ratios[j]);
202
- widths.push([j - index, width]);
203
- }
204
- this._distributeWidths(widths);
205
-
206
- // Create items for this row.
207
- for (let j = index; j < index + row.length; j++) {
208
- let node = this.collection.rows[j];
209
- let itemWidth = Math.max(widths[j - index][1], this.minItemSize.width);
210
- let rect = new Rect(x, y, itemWidth, itemHeight);
211
- let layoutInfo = new LayoutInfo(node.type, node.key, rect);
212
- layoutInfo.allowOverflow = true;
213
- this.layoutInfos.set(node.key, layoutInfo);
214
- x += itemWidth + this.itemSpacing.width;
215
- }
216
-
217
- y += itemHeight + this.itemSpacing.height;
218
- index += row.length;
219
- }
220
-
221
- if (this.isLoading) {
222
- let loaderY = y;
223
- let loaderHeight = 60;
224
- // If there aren't any items, make loader take all available room and remove margin from y calculation
225
- // so it doesn't scroll
226
- if (this.collection.size === 0) {
227
- loaderY = 0;
228
- loaderHeight = visibleHeight || 60;
229
- }
230
-
231
- let rect = new Rect(0, loaderY, visibleWidth, loaderHeight);
232
- let loader = new LayoutInfo('loader', 'loader', rect);
233
- this.layoutInfos.set('loader', loader);
234
- y = loader.rect.maxY;
235
- }
236
-
237
- if (this.collection.size === 0 && !this.isLoading) {
238
- let rect = new Rect(0, 0, visibleWidth, visibleHeight);
239
- let placeholder = new LayoutInfo('placeholder', 'placeholder', rect);
240
- this.layoutInfos.set('placeholder', placeholder);
241
- y = placeholder.rect.maxY;
242
- }
243
- }
244
- this.contentSize = new Size(visibleWidth, y);
245
- }
246
- }
247
-
248
- // https://www8.cs.umu.se/kurser/TDBA77/VT06/algorithms/BOOK/BOOK2/NODE45.HTM
249
- function linearPartition(seq, k) {
250
- let n = seq.length;
251
- if (k <= 0) {
252
- return [];
253
- }
254
-
255
- if (k >= n) {
256
- return seq.map(x => [x]);
257
- }
258
-
259
- if (n === 1) {
260
- return [seq];
261
- }
262
-
263
- let table = Array(n).fill(0).map(() => Array(k).fill(0));
264
- let solution = Array(n - 1).fill(0).map(() => Array(k - 1).fill(0));
265
-
266
- for (let i = 0; i < n; i++) {
267
- table[i][0] = seq[i] + (i > 0 ? table[i - 1][0] : 0);
268
- }
269
-
270
- for (let i = 0; i < k; i++) {
271
- table[0][i] = seq[0];
272
- }
273
-
274
- for (let i = 1; i < n; i++) {
275
- for (let j = 1; j < k; j++) {
276
- let currentMin = 0;
277
- let minX = Infinity;
278
-
279
- for (let x = 0; x < i; x++) {
280
- let c1 = table[x][j - 1];
281
- let c2 = table[i][0] - table[x][0];
282
- let cost = Math.max(c1, c2);
283
-
284
- if (!x || cost < currentMin) {
285
- currentMin = cost;
286
- minX = x;
287
- }
288
- }
289
-
290
- table[i][j] = currentMin;
291
- solution[i - 1][j - 1] = minX;
292
- }
293
- }
294
-
295
- n = n - 1;
296
- k = k - 2;
297
-
298
- let result = [];
299
- while (k >= 0) {
300
- if (n >= 1) {
301
- let row = [];
302
- for (let i = solution[n - 1][k] + 1; i < n + 1; i++) {
303
- row.push(seq[i]);
304
- }
305
-
306
- result.unshift(row);
307
- n = solution[n - 1][k];
308
- }
309
-
310
- k--;
311
- }
312
-
313
- let row = [];
314
- for (let i = 0; i < n + 1; i++) {
315
- row.push(seq[i]);
316
- }
317
-
318
- result.unshift(row);
319
-
320
- return result;
321
- }
@@ -1,242 +0,0 @@
1
- // @ts-nocheck
2
- /*
3
- * Copyright 2021 Adobe. All rights reserved.
4
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License. You may obtain a copy
6
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
7
- *
8
- * Unless required by applicable law or agreed to in writing, software distributed under
9
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10
- * OF ANY KIND, either express or implied. See the License for the specific language
11
- * governing permissions and limitations under the License.
12
- */
13
-
14
- import {BaseLayout, BaseLayoutOptions} from './BaseLayout';
15
- import {getChildNodes, getFirstItem} from '@react-stately/collections';
16
- import {Key, Node, Orientation} from '@react-types/shared';
17
- import {LayoutInfo, Rect, Size} from '@react-stately/virtualizer';
18
-
19
- export interface GridLayoutOptions extends BaseLayoutOptions {
20
- // /**
21
- // * The card size in the grid.
22
- // */
23
- // cardSize?: 'S' | 'M' | 'L',
24
- /**
25
- * The minimum item size.
26
- * @default 208 x 208 for horizontal card orientation. 102 x 102 for vertical card orientation.
27
- */
28
- minItemSize?: Size,
29
- /**
30
- * The maximum item size.
31
- * @default Infinity
32
- */
33
- maxItemSize?: Size,
34
- /**
35
- * The minimum space required between items.
36
- * @default 18 x 18
37
- */
38
- minSpace?: Size,
39
- /**
40
- * The maximum number of columns.
41
- * @default Infinity
42
- */
43
- maxColumns?: number,
44
- /**
45
- * The additional padding along the card's main axis. Affects the sizing of the content area following the card image.
46
- * @default 95
47
- */
48
- itemPadding?: number,
49
- /**
50
- * The orientation of the cards withn the grid.
51
- * @default vertical
52
- */
53
- cardOrientation?: Orientation
54
- }
55
-
56
- const DEFAULT_OPTIONS = {
57
- S: {
58
- itemPadding: 20,
59
- minItemSize: {
60
- 'vertical': new Size(96, 96)
61
- },
62
- maxItemSize: new Size(Infinity, Infinity),
63
- margin: 8,
64
- minSpace: new Size(6, 6),
65
- maxColumns: Infinity,
66
- dropSpacing: 50
67
- },
68
- L: {
69
- itemPadding: {
70
- 'vertical': {
71
- 'medium': 78,
72
- 'large': 98
73
- },
74
- 'horizontal': {
75
- 'medium': 150,
76
- 'large': 170
77
- }
78
- },
79
- minItemSize: {
80
- 'vertical': new Size(208, 208),
81
- 'horizontal': new Size(102, 102)
82
- },
83
- maxItemSize: new Size(Infinity, Infinity),
84
- margin: 24,
85
- minSpace: new Size(18, 18),
86
- maxColumns: Infinity,
87
- dropSpacing: 100
88
- }
89
- };
90
-
91
- export class GridLayout<T> extends BaseLayout<T> {
92
- protected minItemSize: Size;
93
- protected maxItemSize: Size;
94
- protected minSpace: Size;
95
- protected maxColumns: number;
96
- itemPadding: number;
97
- cardOrientation: Orientation;
98
- protected itemSize: Size;
99
- protected numColumns: number;
100
- protected numRows: number;
101
- protected horizontalSpacing: number;
102
-
103
- constructor(options: GridLayoutOptions = {}) {
104
- super(options);
105
- let cardSize = 'L';
106
- this.cardOrientation = options.cardOrientation || 'vertical';
107
- this.minItemSize = options.minItemSize || DEFAULT_OPTIONS[cardSize].minItemSize[this.cardOrientation];
108
- this.maxItemSize = options.maxItemSize || DEFAULT_OPTIONS[cardSize].maxItemSize;
109
- this.margin = options.margin != null ? options.margin : DEFAULT_OPTIONS[cardSize].margin;
110
- this.minSpace = options.minSpace || DEFAULT_OPTIONS[cardSize].minSpace;
111
- this.maxColumns = options.maxColumns || DEFAULT_OPTIONS[cardSize].maxColumns;
112
- this.itemPadding = options.itemPadding != null ? options.itemPadding : DEFAULT_OPTIONS[cardSize].itemPadding[this.cardOrientation][this.scale];
113
- this.itemSize = null;
114
- this.numColumns = 0;
115
- this.numRows = 0;
116
- this.horizontalSpacing = 0;
117
- }
118
-
119
- get layoutType(): string {
120
- return 'grid';
121
- }
122
-
123
- getIndexAtPoint(x: number, y: number, allowInsertingAtEnd = false): number {
124
- let itemHeight = this.itemSize.height + this.minSpace.height;
125
- let itemWidth = this.itemSize.width + this.horizontalSpacing;
126
- return Math.max(0,
127
- Math.min(
128
- this.collection.size - (allowInsertingAtEnd ? 0 : 1),
129
- Math.floor(y / itemHeight) * this.numColumns + Math.floor((x - this.horizontalSpacing) / itemWidth)
130
- )
131
- );
132
- }
133
-
134
- buildCollection(): void {
135
- let visibleWidth = this.virtualizer.visibleRect.width;
136
- let visibleHeight = this.virtualizer.visibleRect.height;
137
- let horizontalItemPadding = this.cardOrientation === 'horizontal' ? this.itemPadding : 0;
138
- let verticalItemPadding = this.cardOrientation === 'vertical' ? this.itemPadding : 0;
139
- let minCardWidth = this.minItemSize.width + horizontalItemPadding;
140
-
141
- // Compute the number of rows and columns needed to display the content
142
- let availableWidth = visibleWidth - this.margin * 2;
143
- let columns = Math.floor((availableWidth + this.minSpace.width) / (minCardWidth + this.minSpace.width));
144
- this.numColumns = Math.max(1, Math.min(this.maxColumns, columns));
145
- this.numRows = Math.ceil(this.collection.size / this.numColumns);
146
-
147
- // Compute the available width (minus the space between items)
148
- let width = availableWidth - (this.minSpace.width * Math.max(0, this.numColumns - 1));
149
-
150
- // Compute the item width based on the space available
151
- let itemWidth = Math.floor(width / this.numColumns);
152
- itemWidth = Math.max(minCardWidth, Math.min(this.maxItemSize.width, itemWidth));
153
- // Compute the item height, which is proportional to the item width
154
- let t = ((itemWidth - minCardWidth) / minCardWidth);
155
- let itemHeight = Math.floor(this.minItemSize.height + this.minItemSize.height * t);
156
- itemHeight = Math.max(this.minItemSize.height, Math.min(this.maxItemSize.height, itemHeight)) + verticalItemPadding;
157
- this.itemSize = new Size(itemWidth, itemHeight);
158
-
159
- // Compute the horizontal spacing and content height
160
- this.horizontalSpacing = this.numColumns < 2 ? 0 : Math.floor((availableWidth - this.numColumns * this.itemSize.width) / (this.numColumns - 1));
161
-
162
- let y = this.margin;
163
- let index = 0;
164
- for (let node of this.collection) {
165
- let layoutInfo = this.buildChild(node, y, index);
166
- y = layoutInfo.rect.maxY;
167
- index++;
168
- }
169
-
170
- if (this.isLoading) {
171
- let loaderY = y;
172
- let loaderHeight = 60;
173
- // If there aren't any items, make loader take all avaliable room and remove margin from y calculation
174
- // so it doesn't scroll
175
- if (this.collection.size === 0) {
176
- loaderY = 0;
177
- loaderHeight = visibleHeight || 60;
178
- }
179
-
180
- let rect = new Rect(0, loaderY, visibleWidth, loaderHeight);
181
- let loader = new LayoutInfo('loader', 'loader', rect);
182
- this.layoutInfos.set('loader', loader);
183
- y = loader.rect.maxY;
184
- }
185
-
186
- if (this.collection.size === 0 && !this.isLoading) {
187
- let rect = new Rect(0, 0, visibleWidth, visibleHeight);
188
- let placeholder = new LayoutInfo('placeholder', 'placeholder', rect);
189
- this.layoutInfos.set('placeholder', placeholder);
190
- y = placeholder.rect.maxY;
191
- }
192
-
193
- this.contentSize = new Size(visibleWidth, y);
194
- }
195
-
196
- buildChild(node: Node<T>, y: number, index: number): LayoutInfo {
197
- let row = Math.floor(index / this.numColumns);
198
- let column = index % this.numColumns;
199
- let x = this.margin + column * (this.itemSize.width + this.horizontalSpacing);
200
- y = this.margin + row * (this.itemSize.height + this.minSpace.height);
201
-
202
- let rect = new Rect(x, y, this.itemSize.width, this.itemSize.height);
203
- // TODO: Perhaps have it so that the child key for each row is stored with the layoutInfo?
204
- let layoutInfo = new LayoutInfo(node.type, node.key, rect);
205
- layoutInfo.allowOverflow = true;
206
- this.layoutInfos.set(node.key, layoutInfo);
207
- return layoutInfo;
208
- }
209
-
210
- // Since the collection doesn't represent the visual layout, need to calculate what row and column the current key is in,
211
- // then return the key that occupies the row + column below. This can be done by figuring out how many cards exist per column then dividing the
212
- // collection contents by that number (which will give us the row distribution)
213
- getKeyBelow(key: Key): Node<T> | undefined | null {
214
- // Expected key is the currently focused cell so we need the parent row key
215
- let parentRowKey = this.collection.getItem(key).parentKey;
216
- let indexRowBelow;
217
- let index = this.collection.rows.findIndex(card => card.key === parentRowKey);
218
- if (index !== -1) {
219
- indexRowBelow = index + this.numColumns;
220
- } else {
221
- return null;
222
- }
223
-
224
- let row = this.collection.rows[indexRowBelow];
225
- return row ? getFirstItem(getChildNodes(row, this.collection))?.key : null;
226
- }
227
-
228
- getKeyAbove(key: Key): Node<T> | undefined | null {
229
- // Expected key is the currently focused cell so we need the parent row key
230
- let parentRowKey = this.collection.getItem(key).parentKey;
231
- let indexRowAbove;
232
- let index = this.collection.rows.findIndex(card => card.key === parentRowKey);
233
- if (index !== -1) {
234
- indexRowAbove = index - this.numColumns;
235
- } else {
236
- return null;
237
- }
238
-
239
- let row = this.collection.rows[indexRowAbove];
240
- return row ? getFirstItem(getChildNodes(row, this.collection))?.key : null;
241
- }
242
- }