@threlte/flex 0.0.8 → 0.0.9

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.
@@ -23,6 +23,8 @@ parentNodeContext?.insertNode(node, order);
23
23
  onDestroy(() => {
24
24
  parentNodeContext?.removeNode(node);
25
25
  });
26
+ // update the order of the node
27
+ $: parentNodeContext?.updateNodeOrder(node, order);
26
28
  addNode(node, group, $$restProps);
27
29
  updateNodeProps(node, { ...classParser?.(_class, {}), ...$$restProps }, true);
28
30
  $: updateNodeProps(node, { ...classParser?.(_class, {}), ...$$restProps });
@@ -22,8 +22,6 @@ export { _class as class };
22
22
  const dispatch = createRawEventDispatcher();
23
23
  const rootGroup = new Group();
24
24
  rootGroup.userData.isNode = true;
25
- const rootNode = yoga.Node.create();
26
- createNodeContext(rootNode);
27
25
  const boundingBox = new Box3();
28
26
  const vec3 = new Vector3();
29
27
  /**
@@ -115,6 +113,8 @@ const flexContext = createFlexContext({
115
113
  reflow,
116
114
  classParser
117
115
  });
116
+ const rootNode = yoga.Node.create();
117
+ createNodeContext(rootNode);
118
118
  const { mainAxis, crossAxis, depthAxis } = flexContext;
119
119
  $: rootNode.setWidth(width * scaleFactor), rootNode.setHeight(height * scaleFactor);
120
120
  // prettier-ignore
@@ -2,6 +2,7 @@ import type { Node } from 'yoga-layout';
2
2
  export type NodeContext = {
3
3
  insertNode: (childNode: Node, order?: number) => void;
4
4
  removeNode: (childNode: Node) => void;
5
+ updateNodeOrder: (childNode: Node, order?: number) => void;
5
6
  };
6
7
  export declare const nodeContextName = "__threlte-node";
7
8
  export declare const createNodeContext: (node: Node) => NodeContext | undefined;
@@ -1,21 +1,32 @@
1
1
  import { getContext, setContext } from 'svelte';
2
+ import { useReflow } from '..';
2
3
  export const nodeContextName = '__threlte-node';
3
4
  export const createNodeContext = (node) => {
5
+ const reflow = useReflow();
6
+ /** Set to keep track of all child nodes */
4
7
  const childNodes = new Set();
8
+ /** Map to keep track of the requested order of nodes */
5
9
  const childNodesOrderMap = new Map();
6
10
  const parentNodeContext = getContext(nodeContextName);
11
+ const removeAllChildNodes = () => {
12
+ childNodes.forEach((childNode) => {
13
+ node.removeChild(childNode);
14
+ });
15
+ };
7
16
  setContext(nodeContextName, {
8
17
  insertNode(childNode, order) {
9
18
  // we want to keep track of all child nodes
10
19
  childNodes.add(childNode);
11
- // Additionally, we need to keep track of child nodes that need to be inserted at a specific order
20
+ // Additionally, we need to keep track of child nodes that need to be
21
+ // inserted at a specific order
12
22
  if (order !== undefined) {
13
23
  childNodesOrderMap.set(childNode, {
14
24
  requestedOrder: order
15
25
  });
16
26
  }
17
27
  if (childNodesOrderMap.size) {
18
- // we need to sort the child nodes by their requested order. We leave the nodes that don't have a requested order untouched.
28
+ // we need to sort the child nodes by their requested order. We leave
29
+ // the nodes that don't have a requested order untouched.
19
30
  const sorted = Array.from(childNodes)
20
31
  .map((node, index) => {
21
32
  return {
@@ -25,10 +36,9 @@ export const createNodeContext = (node) => {
25
36
  })
26
37
  .sort((a, b) => a.order - b.order)
27
38
  .map(({ node }) => node);
28
- // Then we need to remove all child nodes from the node and insert them in the correct order.
29
- sorted.forEach((childNode) => {
30
- node.removeChild(childNode);
31
- });
39
+ // Then we need to remove all child nodes from the node and insert them
40
+ // in the correct order.
41
+ removeAllChildNodes();
32
42
  sorted.forEach((childNode, index) => {
33
43
  node.insertChild(childNode, index);
34
44
  });
@@ -36,11 +46,57 @@ export const createNodeContext = (node) => {
36
46
  else {
37
47
  node.insertChild(childNode, node.getChildCount());
38
48
  }
49
+ reflow();
39
50
  },
40
51
  removeNode(childNode) {
41
52
  node.removeChild(childNode);
42
53
  childNodes.delete(childNode);
43
54
  childNodesOrderMap.delete(childNode);
55
+ reflow();
56
+ },
57
+ updateNodeOrder(childNode, order) {
58
+ let update = false;
59
+ const oldOrder = childNodesOrderMap.get(childNode)?.requestedOrder;
60
+ if (order === undefined) {
61
+ // if the order is undefined, we remove the node from the map
62
+ if (oldOrder !== undefined) {
63
+ childNodesOrderMap.delete(childNode);
64
+ update = true;
65
+ }
66
+ }
67
+ else {
68
+ // if the order is defined, we update the node in the map
69
+ const oldOrder = childNodesOrderMap.get(childNode)?.requestedOrder;
70
+ if (oldOrder !== order) {
71
+ childNodesOrderMap.set(childNode, {
72
+ requestedOrder: order
73
+ });
74
+ update = true;
75
+ }
76
+ }
77
+ // if there's no update, return early
78
+ if (!update)
79
+ return;
80
+ // remove all child nodes from the node
81
+ removeAllChildNodes();
82
+ // we need to sort the child nodes by their requested order. We leave the
83
+ // nodes that don't have a requested order untouched.
84
+ const sorted = Array.from(childNodes)
85
+ .map((node, index) => {
86
+ return {
87
+ order: childNodesOrderMap.get(node)?.requestedOrder ?? index,
88
+ node
89
+ };
90
+ })
91
+ .sort((a, b) => a.order - b.order)
92
+ .map(({ node }) => node);
93
+ // Then we need to remove all child nodes from the node and insert them in
94
+ // the correct order.
95
+ removeAllChildNodes();
96
+ sorted.forEach((childNode, index) => {
97
+ node.insertChild(childNode, index);
98
+ });
99
+ reflow();
44
100
  }
45
101
  });
46
102
  return parentNodeContext;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threlte/flex",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "author": "Grischa Erbe <hello@legrisch.com> (https://legrisch.com)",
5
5
  "license": "MIT",
6
6
  "devDependencies": {