@genome-spy/core 0.19.1 → 0.22.0
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/dist/index.js +42 -42
- package/dist/schema.json +213 -21
- package/package.json +3 -3
- package/src/genomeSpy.js +16 -11
- package/src/gl/dataToVertices.js +43 -46
- package/src/gl/includes/common.glsl +12 -12
- package/src/gl/includes/picking.fragment.glsl +0 -2
- package/src/gl/includes/picking.vertex.glsl +0 -2
- package/src/marks/link.js +32 -39
- package/src/marks/mark.js +168 -95
- package/src/marks/pointMark.js +28 -59
- package/src/marks/rectMark.js +38 -33
- package/src/marks/rule.js +31 -21
- package/src/marks/text.js +18 -14
- package/src/spec/mark.d.ts +0 -3
- package/src/spec/title.d.ts +102 -0
- package/src/spec/view.d.ts +6 -4
- package/src/tooltip/dataTooltipHandler.js +3 -2
- package/src/utils/binnedIndex.js +147 -0
- package/src/utils/binnedIndex.test.js +73 -0
- package/src/utils/layout/flexLayout.js +35 -3
- package/src/utils/layout/flexLayout.test.js +14 -0
- package/src/utils/layout/grid.js +95 -0
- package/src/utils/layout/grid.test.js +71 -0
- package/src/utils/layout/padding.js +13 -0
- package/src/utils/layout/rectangle.js +6 -0
- package/src/view/axisView.js +3 -5
- package/src/view/concatView.js +24 -275
- package/src/view/containerView.js +18 -0
- package/src/view/gridView.js +774 -0
- package/src/view/implicitRootView.js +14 -0
- package/src/view/layerView.js +15 -1
- package/src/view/renderingContext/deferredViewRenderingContext.js +3 -1
- package/src/view/renderingContext/simpleViewRenderingContext.js +3 -1
- package/src/view/title.js +165 -0
- package/src/view/unitView.js +9 -5
- package/src/view/view.js +35 -14
- package/src/view/viewContext.d.ts +6 -1
- package/src/view/viewUtils.js +1 -93
- package/src/view/zoom.js +89 -0
- package/src/utils/binnedRangeIndex.js +0 -83
- package/src/view/decoratorView.js +0 -513
package/src/marks/text.js
CHANGED
|
@@ -214,13 +214,12 @@ export default class TextMark extends Mark {
|
|
|
214
214
|
charCount,
|
|
215
215
|
this.properties.minBufferSize || 0
|
|
216
216
|
),
|
|
217
|
-
buildXIndex: this.properties.buildIndex,
|
|
218
217
|
});
|
|
219
218
|
|
|
220
219
|
builder.addBatches(collector.facetBatches);
|
|
221
220
|
|
|
222
221
|
const vertexData = builder.toArrays();
|
|
223
|
-
this.rangeMap
|
|
222
|
+
this.rangeMap.migrateEntries(vertexData.rangeMap);
|
|
224
223
|
|
|
225
224
|
this.updateBufferInfo(vertexData);
|
|
226
225
|
}
|
|
@@ -229,7 +228,7 @@ export default class TextMark extends Mark {
|
|
|
229
228
|
* @param {import("../view/rendering").GlobalRenderingOptions} options
|
|
230
229
|
*/
|
|
231
230
|
prepareRender(options) {
|
|
232
|
-
super.prepareRender(options);
|
|
231
|
+
const ops = super.prepareRender(options);
|
|
233
232
|
|
|
234
233
|
let q = 0.35; // TODO: Ensure that this makes sense. Now chosen by trial & error
|
|
235
234
|
if (this.properties.logoLetters) {
|
|
@@ -239,17 +238,23 @@ export default class TextMark extends Mark {
|
|
|
239
238
|
q /= 2;
|
|
240
239
|
}
|
|
241
240
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
241
|
+
ops.push(() =>
|
|
242
|
+
setUniforms(this.programInfo, {
|
|
243
|
+
uTexture: this.font.texture,
|
|
244
|
+
uSdfNumerator:
|
|
245
|
+
this.font.metrics.common.base / (this.glHelper.dpr / q),
|
|
246
|
+
})
|
|
247
|
+
);
|
|
247
248
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
249
|
+
ops.push(() =>
|
|
250
|
+
setBuffersAndAttributes(
|
|
251
|
+
this.gl,
|
|
252
|
+
this.programInfo,
|
|
253
|
+
this.vertexArrayInfo
|
|
254
|
+
)
|
|
252
255
|
);
|
|
256
|
+
|
|
257
|
+
return ops;
|
|
253
258
|
}
|
|
254
259
|
|
|
255
260
|
/**
|
|
@@ -267,8 +272,7 @@ export default class TextMark extends Mark {
|
|
|
267
272
|
count,
|
|
268
273
|
offset
|
|
269
274
|
),
|
|
270
|
-
options
|
|
271
|
-
() => this.rangeMap
|
|
275
|
+
options
|
|
272
276
|
);
|
|
273
277
|
}
|
|
274
278
|
}
|
package/src/spec/mark.d.ts
CHANGED
|
@@ -407,9 +407,6 @@ export interface MarkConfig
|
|
|
407
407
|
*/
|
|
408
408
|
strokeWidth?: number;
|
|
409
409
|
|
|
410
|
-
// TODO: get rid of this
|
|
411
|
-
dynamicData?: boolean;
|
|
412
|
-
|
|
413
410
|
/**
|
|
414
411
|
* Minimum size for WebGL buffers (number of data items).
|
|
415
412
|
* Allows for using `bufferSubData()` to update graphics.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Adapted from
|
|
3
|
+
* https://github.com/vega/vega/blob/main/packages/vega-typings/types/spec/title.d.ts
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2015-2018, University of Washington Interactive Data Lab
|
|
6
|
+
* All rights reserved.
|
|
7
|
+
*
|
|
8
|
+
* BSD-3-Clause License: https://github.com/vega/vega-lite/blob/master/LICENSE
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { Align, Baseline, FontStyle, FontWeight } from "./font";
|
|
12
|
+
|
|
13
|
+
export type TitleOrient = "none" | "left" | "right" | "top" | "bottom";
|
|
14
|
+
export type TitleAnchor = null | "start" | "middle" | "end";
|
|
15
|
+
export type TitleFrame = "bounds" | "group";
|
|
16
|
+
|
|
17
|
+
export interface Title {
|
|
18
|
+
/**
|
|
19
|
+
* The title text.
|
|
20
|
+
*/
|
|
21
|
+
text: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A mark style property to apply to the title text mark. If not specified, a default style of `"group-title"` is applied.
|
|
25
|
+
*/
|
|
26
|
+
style?: string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The anchor position for placing the title and subtitle text. One of `"start"`, `"middle"`, or `"end"`. For example, with an orientation of top these anchor positions map to a left-, center-, or right-aligned title.
|
|
30
|
+
*/
|
|
31
|
+
anchor?: TitleAnchor;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The reference frame for the anchor position, one of `"bounds"` (to anchor relative to the full bounding box) or `"group"` (to anchor relative to the group width or height).
|
|
35
|
+
*/
|
|
36
|
+
frame?: TitleFrame;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The orthogonal offset in pixels by which to displace the title group from its position along the edge of the chart.
|
|
40
|
+
*/
|
|
41
|
+
offset?: number;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Default title orientation (`"top"`, `"bottom"`, `"left"`, or `"right"`)
|
|
45
|
+
*/
|
|
46
|
+
orient?: TitleOrient;
|
|
47
|
+
|
|
48
|
+
// ---------- Shared Text Properties ----------
|
|
49
|
+
/**
|
|
50
|
+
* Horizontal text alignment for title text. One of `"left"`, `"center"`, or `"right"`.
|
|
51
|
+
*/
|
|
52
|
+
align?: Align;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Angle in degrees of title and subtitle text.
|
|
56
|
+
*/
|
|
57
|
+
angle?: number;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Vertical text baseline for title and subtitle text. One of `"alphabetic"` (default), `"top"`, `"middle"`, or `"bottom"`.
|
|
61
|
+
*/
|
|
62
|
+
baseline?: Baseline;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Delta offset for title and subtitle text x-coordinate.
|
|
66
|
+
*/
|
|
67
|
+
dx?: number;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Delta offset for title and subtitle text y-coordinate.
|
|
71
|
+
*/
|
|
72
|
+
dy?: number;
|
|
73
|
+
|
|
74
|
+
// ---------- Title Text ----------
|
|
75
|
+
/**
|
|
76
|
+
* Text color for title text.
|
|
77
|
+
*/
|
|
78
|
+
color?: string;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Font name for title text.
|
|
82
|
+
*/
|
|
83
|
+
font?: string;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Font size in pixels for title text.
|
|
87
|
+
*
|
|
88
|
+
* @minimum 0
|
|
89
|
+
*/
|
|
90
|
+
fontSize?: number;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Font style for title text.
|
|
94
|
+
*/
|
|
95
|
+
fontStyle?: FontStyle;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Font weight for title text.
|
|
99
|
+
* This can be either a string (e.g `"bold"`, `"normal"`) or a number (`100`, `200`, `300`, ..., `900` where `"normal"` = `400` and `"bold"` = `700`).
|
|
100
|
+
*/
|
|
101
|
+
fontWeight?: FontWeight;
|
|
102
|
+
}
|
package/src/spec/view.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
MarkType,
|
|
15
15
|
RectProps,
|
|
16
16
|
} from "./mark";
|
|
17
|
+
import { Title } from "./title";
|
|
17
18
|
|
|
18
19
|
export interface SizeDef {
|
|
19
20
|
/** Size in pixels */
|
|
@@ -55,7 +56,7 @@ export type Paddings = Partial<Record<Side, number>>;
|
|
|
55
56
|
|
|
56
57
|
export type PaddingConfig = Paddings | number;
|
|
57
58
|
|
|
58
|
-
export interface
|
|
59
|
+
export interface ViewBackground extends RectProps, FillAndStrokeProps {
|
|
59
60
|
// TODO: style?: string | string[];
|
|
60
61
|
|
|
61
62
|
// TODO: Move to FillAndStrokeProps or something
|
|
@@ -78,7 +79,7 @@ export interface ViewSpecBase extends ResolveSpec {
|
|
|
78
79
|
data?: Data;
|
|
79
80
|
transform?: TransformParams[];
|
|
80
81
|
encoding?: Encoding;
|
|
81
|
-
title?: string;
|
|
82
|
+
title?: string | Title;
|
|
82
83
|
|
|
83
84
|
/**
|
|
84
85
|
* A description of the view. Multiple lines can be provided as an array.
|
|
@@ -115,7 +116,7 @@ export interface ViewSpecBase extends ResolveSpec {
|
|
|
115
116
|
}
|
|
116
117
|
|
|
117
118
|
export interface UnitSpec extends ViewSpecBase, AggregateSamplesSpec {
|
|
118
|
-
view?:
|
|
119
|
+
view?: ViewBackground;
|
|
119
120
|
mark: MarkType | MarkConfigAndType;
|
|
120
121
|
}
|
|
121
122
|
|
|
@@ -125,7 +126,7 @@ export interface AggregateSamplesSpec {
|
|
|
125
126
|
}
|
|
126
127
|
|
|
127
128
|
export interface LayerSpec extends ViewSpecBase, AggregateSamplesSpec {
|
|
128
|
-
view?:
|
|
129
|
+
view?: ViewBackground;
|
|
129
130
|
layer: (LayerSpec | UnitSpec)[];
|
|
130
131
|
}
|
|
131
132
|
|
|
@@ -218,4 +219,5 @@ export interface HConcatSpec extends ConcatBase {
|
|
|
218
219
|
|
|
219
220
|
export interface ConcatSpec extends ConcatBase {
|
|
220
221
|
concat: (ViewSpec | ImportSpec)[];
|
|
222
|
+
columns: number;
|
|
221
223
|
}
|
|
@@ -47,10 +47,11 @@ export default async function dataTooltipHandler(datum, mark, params) {
|
|
|
47
47
|
</table>
|
|
48
48
|
`;
|
|
49
49
|
|
|
50
|
-
const
|
|
50
|
+
const titleText = mark.unitView.getTitleText();
|
|
51
|
+
const title = titleText
|
|
51
52
|
? html`
|
|
52
53
|
<div class="title">
|
|
53
|
-
<strong>${
|
|
54
|
+
<strong>${titleText}</strong>
|
|
54
55
|
</div>
|
|
55
56
|
`
|
|
56
57
|
: "";
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import clamp from "./clamp";
|
|
2
|
+
|
|
3
|
+
const MAX_INTEGER = 2 ** 31 - 1;
|
|
4
|
+
const MIN_INTEGER = -(2 ** 31);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @callback Lookup
|
|
8
|
+
* @param {number} start
|
|
9
|
+
* @param {number} end
|
|
10
|
+
* @param {[number, number]} [arr] Store the result into this array (and return it)
|
|
11
|
+
* @returns {[number, number]}
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A binned index for (overlapping) ranges that are sorted by their start position.
|
|
16
|
+
*
|
|
17
|
+
* @param {number} size Number of bins
|
|
18
|
+
* @param {[number, number]} domain
|
|
19
|
+
* @param {(datum: T) => number} accessor
|
|
20
|
+
* @param {(datum: T) => number} [accessor2]
|
|
21
|
+
* @template T
|
|
22
|
+
*/
|
|
23
|
+
export function createBinningRangeIndexer(
|
|
24
|
+
size,
|
|
25
|
+
domain,
|
|
26
|
+
accessor,
|
|
27
|
+
accessor2 = accessor
|
|
28
|
+
) {
|
|
29
|
+
const startIndices = new Int32Array(size);
|
|
30
|
+
startIndices.fill(MAX_INTEGER);
|
|
31
|
+
|
|
32
|
+
let lastIndex = MIN_INTEGER;
|
|
33
|
+
let unordered = false;
|
|
34
|
+
|
|
35
|
+
const endIndices = new Int32Array(size);
|
|
36
|
+
|
|
37
|
+
const start = domain[0];
|
|
38
|
+
const domainLength = domain[1] - domain[0];
|
|
39
|
+
const divisor = domainLength / size;
|
|
40
|
+
|
|
41
|
+
/** @param {number} pos */
|
|
42
|
+
const getBin = (pos) =>
|
|
43
|
+
clamp(Math.floor((pos - start) / divisor), 0, size - 1);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* @param {T} datum
|
|
48
|
+
* @param {number} startIndex
|
|
49
|
+
* @param {number} endIndex
|
|
50
|
+
*/
|
|
51
|
+
function binningIndexer(datum, startIndex, endIndex) {
|
|
52
|
+
if (startIndex > lastIndex) {
|
|
53
|
+
lastIndex = startIndex;
|
|
54
|
+
} else if (!unordered) {
|
|
55
|
+
unordered = true;
|
|
56
|
+
// TODO: Contextual info like view path
|
|
57
|
+
console.debug(
|
|
58
|
+
"Items are not ordered properly. Disabling binned index."
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const value = accessor(datum);
|
|
63
|
+
const bin = getBin(value);
|
|
64
|
+
|
|
65
|
+
if (startIndices[bin] > startIndex) {
|
|
66
|
+
startIndices[bin] = startIndex;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (endIndices[bin] < endIndex) {
|
|
70
|
+
endIndices[bin] = endIndex;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
*
|
|
76
|
+
* @param {T} datum
|
|
77
|
+
* @param {number} startIndex
|
|
78
|
+
* @param {number} endIndex
|
|
79
|
+
*/
|
|
80
|
+
function binningRangeIndexer(datum, startIndex, endIndex) {
|
|
81
|
+
if (startIndex > lastIndex) {
|
|
82
|
+
lastIndex = startIndex;
|
|
83
|
+
} else if (!unordered) {
|
|
84
|
+
unordered = true;
|
|
85
|
+
// TODO: Contextual info like view path
|
|
86
|
+
console.debug(
|
|
87
|
+
"Items are not ordered properly. Disabling binned index."
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const start = accessor(datum);
|
|
92
|
+
const end = accessor2(datum);
|
|
93
|
+
const startBin = getBin(start);
|
|
94
|
+
const endBin = getBin(end);
|
|
95
|
+
|
|
96
|
+
// TODO: This loop could probably be done as a more efficient post processing
|
|
97
|
+
// step.
|
|
98
|
+
for (let bin = startBin; bin <= endBin; bin++) {
|
|
99
|
+
if (startIndices[bin] > startIndex) {
|
|
100
|
+
startIndices[bin] = startIndex;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (endIndices[bin] < endIndex) {
|
|
104
|
+
endIndices[bin] = endIndex;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @type {Lookup}
|
|
111
|
+
*/
|
|
112
|
+
const lookup = (start, end, arr = [0, 0]) => {
|
|
113
|
+
arr[0] = startIndices[getBin(start)];
|
|
114
|
+
arr[1] = Math.max(endIndices[getBin(end)], arr[0]);
|
|
115
|
+
return arr;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const getIndex = () => {
|
|
119
|
+
for (let i = 1; i < endIndices.length; i++) {
|
|
120
|
+
if (endIndices[i] < endIndices[i - 1]) {
|
|
121
|
+
endIndices[i] = endIndices[i - 1];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let tail = true;
|
|
126
|
+
|
|
127
|
+
for (let i = startIndices.length - 1; i > 0; i--) {
|
|
128
|
+
if (tail && startIndices[i] == MAX_INTEGER) {
|
|
129
|
+
startIndices[i] = endIndices[i];
|
|
130
|
+
tail = false;
|
|
131
|
+
} else if (startIndices[i - 1] > startIndices[i]) {
|
|
132
|
+
startIndices[i - 1] = startIndices[i];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return lookup;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
binningIndexer.getIndex = getIndex;
|
|
140
|
+
binningRangeIndexer.getIndex = getIndex;
|
|
141
|
+
|
|
142
|
+
if (unordered) {
|
|
143
|
+
return undefined;
|
|
144
|
+
} else {
|
|
145
|
+
return accessor == accessor2 ? binningIndexer : binningRangeIndexer;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { createBinningRangeIndexer } from "./binnedIndex.js";
|
|
3
|
+
|
|
4
|
+
describe("Binning Indexer", () => {
|
|
5
|
+
test("Points are binned correctly", () => {
|
|
6
|
+
const items = [0, 1, 4, 10, 35, 36, 80];
|
|
7
|
+
const indexer = createBinningRangeIndexer(10, [0, 100], (x) => x);
|
|
8
|
+
|
|
9
|
+
items.forEach((i) => indexer(i, i, i + 1));
|
|
10
|
+
|
|
11
|
+
const index = indexer.getIndex();
|
|
12
|
+
|
|
13
|
+
expect(index(0, 1)).toEqual([0, 5]);
|
|
14
|
+
expect(index(1, 2)).toEqual([0, 5]);
|
|
15
|
+
expect(index(1, 15)).toEqual([0, 11]);
|
|
16
|
+
expect(index(10, 15)).toEqual([10, 11]);
|
|
17
|
+
expect(index(11, 38)).toEqual([10, 37]);
|
|
18
|
+
expect(index(11, 45)).toEqual([10, 37]);
|
|
19
|
+
expect(index(40, 85)).toEqual([80, 81]);
|
|
20
|
+
expect(index(90, 100)).toEqual([81, 81]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("Non-overlapping ranges are binned correctly", () => {
|
|
24
|
+
const items = [
|
|
25
|
+
[0, 5],
|
|
26
|
+
[25, 50],
|
|
27
|
+
[50, 55],
|
|
28
|
+
];
|
|
29
|
+
const indexer = createBinningRangeIndexer(
|
|
30
|
+
10,
|
|
31
|
+
[0, 100],
|
|
32
|
+
(x) => x[0],
|
|
33
|
+
(x) => x[1]
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
items.forEach((x) => indexer(x, x[0], x[1]));
|
|
37
|
+
|
|
38
|
+
const index = indexer.getIndex();
|
|
39
|
+
|
|
40
|
+
// TODO: More tests. Doesn't work 100%
|
|
41
|
+
|
|
42
|
+
expect(index(0, 1)).toEqual([0, 5]);
|
|
43
|
+
expect(index(3, 40)).toEqual([0, 50]);
|
|
44
|
+
expect(index(6, 40)).toEqual([0, 50]);
|
|
45
|
+
// fails: expect(index(50, 57)).toEqual([50, 55]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("Overlapping ranges are binned correctly", () => {
|
|
49
|
+
const items = [
|
|
50
|
+
[10, 30],
|
|
51
|
+
[25, 50],
|
|
52
|
+
];
|
|
53
|
+
const indexer = createBinningRangeIndexer(
|
|
54
|
+
10,
|
|
55
|
+
[0, 100],
|
|
56
|
+
(x) => x[0],
|
|
57
|
+
(x) => x[1]
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
items.forEach((x) => indexer(x, x[0], x[1]));
|
|
61
|
+
|
|
62
|
+
const index = indexer.getIndex();
|
|
63
|
+
|
|
64
|
+
// TODO: More tests. Doesn't work 100%
|
|
65
|
+
|
|
66
|
+
expect(index(0, 5)).toEqual([10, 10]);
|
|
67
|
+
expect(index(0, 15)).toEqual([10, 30]);
|
|
68
|
+
expect(index(27, 40)).toEqual([10, 50]);
|
|
69
|
+
expect(index(40, 50)).toEqual([25, 50]);
|
|
70
|
+
expect(index(40, 80)).toEqual([25, 50]);
|
|
71
|
+
expect(index(10, 29)).toEqual([10, 50]);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -133,7 +133,7 @@ export function mapToPixelCoords(
|
|
|
133
133
|
/**
|
|
134
134
|
* Returns the minimum size (the sum of pixels sizes) for the flex items
|
|
135
135
|
*
|
|
136
|
-
* @param {SizeDef
|
|
136
|
+
* @param {Iterable<SizeDef>} items
|
|
137
137
|
* @param {FlexOptions} [options]
|
|
138
138
|
*/
|
|
139
139
|
export function getMinimumSize(items, { spacing } = { spacing: 0 }) {
|
|
@@ -144,6 +144,21 @@ export function getMinimumSize(items, { spacing } = { spacing: 0 }) {
|
|
|
144
144
|
return Math.max(0, minimumSize - spacing);
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
/**
|
|
148
|
+
* @param {Iterable<SizeDef>} items
|
|
149
|
+
* @returns {SizeDef}
|
|
150
|
+
*/
|
|
151
|
+
export function getLargestSize(items) {
|
|
152
|
+
let px = 0;
|
|
153
|
+
let grow = 0;
|
|
154
|
+
for (const s of items) {
|
|
155
|
+
px = Math.max(px, s.px ?? 0);
|
|
156
|
+
grow = Math.max(grow, s.grow ?? 0);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return { px, grow };
|
|
160
|
+
}
|
|
161
|
+
|
|
147
162
|
/**
|
|
148
163
|
* Returns true if relative (stretching) elements are present
|
|
149
164
|
* @param {SizeDef[]} items
|
|
@@ -172,13 +187,30 @@ export class FlexDimensions {
|
|
|
172
187
|
* @param {import("./padding").default} padding
|
|
173
188
|
*/
|
|
174
189
|
addPadding(padding) {
|
|
190
|
+
return this.#addPx(padding.width, padding.height);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Subtracts padding from absolute (px) dimensions
|
|
195
|
+
*
|
|
196
|
+
* @param {import("./padding").default} padding
|
|
197
|
+
*/
|
|
198
|
+
subtractPadding(padding) {
|
|
199
|
+
return this.#addPx(-padding.width, -padding.height);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @param {number} width
|
|
204
|
+
* @param {number} height
|
|
205
|
+
*/
|
|
206
|
+
#addPx(width, height) {
|
|
175
207
|
return new FlexDimensions(
|
|
176
208
|
{
|
|
177
|
-
px: (this.width.px
|
|
209
|
+
px: (this.width.px ?? 0) + width,
|
|
178
210
|
grow: this.width.grow,
|
|
179
211
|
},
|
|
180
212
|
{
|
|
181
|
-
px: (this.height.px
|
|
213
|
+
px: (this.height.px ?? 0) + height,
|
|
182
214
|
grow: this.height.grow,
|
|
183
215
|
}
|
|
184
216
|
);
|
|
@@ -2,6 +2,7 @@ import { describe, expect, test } from "vitest";
|
|
|
2
2
|
import {
|
|
3
3
|
mapToPixelCoords,
|
|
4
4
|
getMinimumSize,
|
|
5
|
+
getLargestSize,
|
|
5
6
|
isStretching,
|
|
6
7
|
parseSizeDef,
|
|
7
8
|
} from "./flexLayout";
|
|
@@ -290,6 +291,19 @@ describe("Utility fuctions", () => {
|
|
|
290
291
|
expect(getMinimumSize(items, { spacing: 10 })).toEqual(330);
|
|
291
292
|
});
|
|
292
293
|
|
|
294
|
+
test("getLargestSize", () => {
|
|
295
|
+
const items = [
|
|
296
|
+
{ px: 100 },
|
|
297
|
+
{ px: 0, grow: 0 },
|
|
298
|
+
{ grow: 1 },
|
|
299
|
+
{ grow: 9 },
|
|
300
|
+
{ px: 200 },
|
|
301
|
+
{ px: 50 },
|
|
302
|
+
];
|
|
303
|
+
|
|
304
|
+
expect(getLargestSize(items)).toEqual({ px: 200, grow: 9 });
|
|
305
|
+
});
|
|
306
|
+
|
|
293
307
|
test("isStretching", () => {
|
|
294
308
|
expect(isStretching([{ grow: 1 }])).toBeTruthy();
|
|
295
309
|
expect(isStretching([{ px: 1 }])).toBeFalsy();
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An utility class for indexing cells in a wrapping grid layout
|
|
3
|
+
*/
|
|
4
|
+
export default class Grid {
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @param {number} nChildren
|
|
8
|
+
* @param {number} [maxCols]
|
|
9
|
+
*/
|
|
10
|
+
constructor(nChildren, maxCols) {
|
|
11
|
+
this.n = nChildren;
|
|
12
|
+
this.maxCols = maxCols ?? Infinity;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get nRows() {
|
|
16
|
+
return this.maxCols == Infinity ? 1 : Math.ceil(this.n / this.maxCols);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get nCols() {
|
|
20
|
+
return Math.min(this.n, this.maxCols);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get rowIndices() {
|
|
24
|
+
/** @type {number[][]} */
|
|
25
|
+
const rows = [];
|
|
26
|
+
|
|
27
|
+
const nCols = this.nCols;
|
|
28
|
+
const nRows = this.nRows;
|
|
29
|
+
|
|
30
|
+
for (let row = 0; row < nRows; row++) {
|
|
31
|
+
/** @type {number[]} */
|
|
32
|
+
const arr = [];
|
|
33
|
+
rows.push(arr);
|
|
34
|
+
for (let col = 0; col < nCols; col++) {
|
|
35
|
+
const i = row * nCols + col;
|
|
36
|
+
if (i < this.n) {
|
|
37
|
+
arr.push(i);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return rows;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get colIndices() {
|
|
45
|
+
/** @type {number[][]} */
|
|
46
|
+
const cols = [];
|
|
47
|
+
|
|
48
|
+
const nCols = this.nCols;
|
|
49
|
+
const nRows = this.nRows;
|
|
50
|
+
|
|
51
|
+
for (let col = 0; col < nCols; col++) {
|
|
52
|
+
/** @type {number[]} */
|
|
53
|
+
const arr = [];
|
|
54
|
+
cols.push(arr);
|
|
55
|
+
for (let row = 0; row < nRows; row++) {
|
|
56
|
+
const i = row * nCols + col;
|
|
57
|
+
if (i < this.n) {
|
|
58
|
+
arr.push(i);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return cols;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {number} col
|
|
67
|
+
* @param {number} row
|
|
68
|
+
*/
|
|
69
|
+
getCellIndex(col, row) {
|
|
70
|
+
let i = 0;
|
|
71
|
+
|
|
72
|
+
if (this.maxCols == Infinity) {
|
|
73
|
+
i = row == 0 ? col : undefined;
|
|
74
|
+
} else if (col >= this.maxCols) {
|
|
75
|
+
return undefined;
|
|
76
|
+
} else {
|
|
77
|
+
i = row * this.nCols + col;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return i < this.n ? i : undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
*
|
|
85
|
+
* @param {number} index
|
|
86
|
+
* @returns {[number, number]}
|
|
87
|
+
*/
|
|
88
|
+
getCellCoords(index) {
|
|
89
|
+
if (index < 0 || index >= this.n) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return [index % this.nCols, Math.floor(index / this.nCols)];
|
|
94
|
+
}
|
|
95
|
+
}
|