Skip to content

Commit 5a02bd0

Browse files
committed
Implemented default ordering for anchored spaces
1 parent d4ced35 commit 5a02bd0

File tree

6 files changed

+65
-29
lines changed

6 files changed

+65
-29
lines changed

debug.log

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1212/224221.239:ERROR:directory_reader_win.cc(43)] FindFirstFile: The system cannot find the path specified. (0x3)

src/components/stories/02-components/Experimental.stories.mdx

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Meta, Story, Preview, Props } from "@storybook/addon-docs/blocks";
2+
import { StateDriven, AnchoredDefaultOrdering } from "../Utils";
3+
4+
<Meta title="Testing/State" />
5+
6+
<Preview>
7+
<Story name="Spaces using global store with state driven sizing and switched spaces">
8+
<StateDriven />
9+
</Story>
10+
</Preview>
11+
12+
<Preview>
13+
<Story name="Anchored spaces with default ordering">
14+
<AnchoredDefaultOrdering />
15+
</Story>
16+
</Preview>

src/components/stories/Utils.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as React from "react";
22
import { CSSProperties } from "react";
3-
import { shortuuid } from "../../core-utils";
43
import {
54
Info,
65
Fixed,
76
ViewPort,
87
Top,
98
Fill,
9+
Left,
1010
LeftResizable,
1111
Right,
1212
BottomResizable,
@@ -204,7 +204,7 @@ export const DemoUI = () => {
204204
);
205205
};
206206

207-
export const DemoExperimental: React.FC = () => {
207+
export const StateDriven: React.FC = () => {
208208
const [visible, setVisible] = React.useState(true);
209209
const [size, setSize] = React.useState(true);
210210
const [side, setSide] = React.useState(true);
@@ -277,6 +277,15 @@ export const DemoExperimental: React.FC = () => {
277277
);
278278
};
279279

280+
export const AnchoredDefaultOrdering = () => {
281+
return <ViewPort as="main">
282+
<Left size="25%" style={blue} centerContent={CenterType.HorizontalVertical}>Left 1</Left>
283+
<Left size="25%" style={green} centerContent={CenterType.HorizontalVertical}>Left 2</Left>
284+
<Left size="25%" style={red} centerContent={CenterType.HorizontalVertical}>Left 3</Left>
285+
<Fill style={blue} centerContent={CenterType.HorizontalVertical}>Fill</Fill>
286+
</ViewPort>
287+
}
288+
280289
const white = { backgroundColor: "#ffffff", padding: 15 };
281290
export const blue: CSSProperties = { backgroundColor: "rgb(224, 238, 238, 0.7)" };
282291
export const red: CSSProperties = { backgroundColor: "rgb(238, 224, 224, 0.7)" };

src/core-types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export enum ResizeType {
3535
export enum ResizeHandlePlacement {
3636
OverlayInside = "overlay-inside",
3737
Inside = "inside",
38-
OverlayBoundary = "overlay-boundary"
38+
OverlayBoundary = "overlay-boundary",
3939
}
4040

4141
export enum CenterType {
@@ -123,7 +123,7 @@ export interface ISpaceDefinition {
123123
adjustTop: (adjusted: SizeUnit[]) => boolean;
124124
adjustBottom: (adjusted: SizeUnit[]) => boolean;
125125
adjustEdge: (adjusted: SizeUnit[]) => boolean;
126-
anchoredChildren: (anchor: AnchorType, zIndex: number) => ISpaceDefinition[];
126+
anchoredChildren: (children: ISpaceDefinition[], anchor: AnchorType, zIndex: number) => ISpaceDefinition[];
127127
onResizeStart?: (() => void | boolean) | undefined;
128128
onResizeEnd?: ((newSize: SizeUnit, domRect: DOMRect) => void) | undefined;
129129
element: HTMLElement;
@@ -132,7 +132,7 @@ export interface ISpaceDefinition {
132132
anchor?: AnchorType;
133133
orientation: Orientation;
134134
scrollable: boolean;
135-
order: number;
135+
order?: number;
136136
position: "fixed" | "absolute" | "relative";
137137
children: ISpaceDefinition[];
138138
parentId: string | undefined;

src/core.ts

+34-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { updateStyleDefinition, removeStyleDefinition, coalesce, adjustmentsEqua
44

55
const spaceDefaults: Partial<ISpaceDefinition> = {
66
id: "",
7-
order: 0,
87
zIndex: 0,
98
scrollable: false,
109
resizing: false,
@@ -21,6 +20,8 @@ const spaceDefaults: Partial<ISpaceDefinition> = {
2120
anchoredChildren: () => [],
2221
};
2322

23+
const anchorTypes = [AnchorType.Left, AnchorType.Top, AnchorType.Right, AnchorType.Bottom];
24+
2425
function getPosition(type: Type) {
2526
if (type === Type.ViewPort) {
2627
return "fixed";
@@ -74,14 +75,37 @@ export function createStore(): ISpaceStore {
7475
const getSpaces = () => spaces;
7576

7677
const recalcSpaces = (parent: ISpaceDefinition) => {
77-
for (var i = 0, len = parent.children.length; i < len; i++) {
78-
const space = parent.children[i];
78+
const onlyUnique = (value: number, index: number, self: number[]) => {
79+
return self.indexOf(value) === index;
80+
};
81+
82+
const addDefaultOrders = (spaces: ISpaceDefinition[]) => {
83+
let result: ISpaceDefinition[] = [];
84+
85+
anchorTypes.forEach((t) => {
86+
const anchoredSpaces = spaces.filter((s) => s.anchor !== undefined && s.anchor === t);
87+
const zIndices = anchoredSpaces.map((s) => s.zIndex).filter(onlyUnique);
88+
zIndices.forEach((i) => {
89+
const anchoredSpacesInLayer = anchoredSpaces.filter((s) => s.zIndex === i);
90+
const orderedSpaces = anchoredSpacesInLayer.filter((c) => c.order !== undefined);
91+
const unorderedSpaces = anchoredSpacesInLayer.filter((c) => c.order === undefined);
92+
var maxOrder = orderedSpaces.length > 0 ? orderedSpaces.map((a) => a.order!).reduce((a, b) => Math.max(a, b)) : 0;
93+
result = [...result, ...[...orderedSpaces, ...unorderedSpaces.map((c, idx) => ({ ...c, ...{ order: maxOrder + idx + 1 } }))]];
94+
});
95+
});
96+
97+
return [...result, ...spaces.filter((s) => s.anchor === undefined)];
98+
};
99+
100+
const orderedSpaces = addDefaultOrders(parent.children);
101+
for (var i = 0, len = orderedSpaces.length; i < len; i++) {
102+
const space = orderedSpaces[i];
79103
let changed = false;
80104

81105
if (space.type === Type.Fill) {
82106
anchorUpdates(space).forEach((info) => {
83107
const adjusted: SizeUnit[] = [];
84-
const anchoredSpaces = parent.anchoredChildren(info.anchor, space.zIndex);
108+
const anchoredSpaces = parent.anchoredChildren(orderedSpaces, info.anchor, space.zIndex);
85109

86110
anchoredSpaces.forEach((as) => {
87111
if (as.orientation === Orientation.Vertical) {
@@ -108,8 +132,8 @@ export function createStore(): ISpaceStore {
108132
} else if (space.type === Type.Anchored) {
109133
const adjusted: SizeUnit[] = [];
110134
const anchoredSpaces = parent
111-
.anchoredChildren(space.anchor!, space.zIndex)
112-
.filter((s) => s.id !== space.id && s.order <= space.order);
135+
.anchoredChildren(orderedSpaces, space.anchor!, space.zIndex)
136+
.filter((s) => s.id !== space.id && s.order! <= space.order!);
113137

114138
anchoredSpaces.forEach((as) => {
115139
if (as.orientation === Orientation.Vertical) {
@@ -191,7 +215,7 @@ export function createStore(): ISpaceStore {
191215
maximumSize,
192216
handleSize,
193217
touchHandleSize,
194-
handlePlacement
218+
handlePlacement,
195219
} = props;
196220
const canResizeLeft = (position && position.rightResizable) || false;
197221
const canResizeRight = (position && position.leftResizable) || false;
@@ -382,13 +406,9 @@ export function createStore(): ISpaceStore {
382406
},
383407
} as ISpaceDefinition;
384408

385-
newSpace.anchoredChildren = (anchor, zIndex) => {
386-
const children = newSpace.children.filter((s) => s.type === Type.Anchored && s.anchor === anchor && s.zIndex === zIndex);
387-
const orderedChildren = children.filter(c => c.order !== undefined);
388-
const unorderedChildren = children.filter(c => c.order === undefined);
389-
var maxOrder = orderedChildren.length > 0 ? orderedChildren.map(a => a.order).reduce((a, b) => Math.max(a, b)) : 0;
390-
return [...orderedChildren, ...unorderedChildren.map((c, idx) => ({...c, ...{ order: maxOrder + idx + 1 }}))];
391-
}
409+
newSpace.anchoredChildren = (children, anchor, zIndex) => {
410+
return children.filter((s) => s.type === Type.Anchored && s.anchor === anchor && s.zIndex === zIndex);
411+
};
392412

393413
newSpace.adjustLeft = (adjusted) => {
394414
if (adjustmentsEqual(newSpace.left.adjusted, adjusted)) {

0 commit comments

Comments
 (0)