Skip to content

Commit e60c440

Browse files
is.ichildren
1 parent c11fa48 commit e60c440

File tree

7 files changed

+38
-13
lines changed

7 files changed

+38
-13
lines changed

editor/grida-canvas-react-starter-kit/starterkit-hierarchy/tree-node.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export function NodeHierarchyList() {
262262
},
263263
isItemFolder: (item) => {
264264
const node = item.getItemData();
265-
return "children" in node;
265+
return grida.program.nodes.is.ichildren(node);
266266
},
267267
onDrop(items, target) {
268268
const ids = items.map((item) => item.getId());

editor/grida-canvas/reducers/document.reducer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,10 @@ function __self_post_hierarchy_change_commit<
20252025
// Only check boolean and group nodes
20262026
if (parent_node.type === "boolean" || parent_node.type === "group") {
20272027
// Check if the node has children property and if it's empty
2028-
if ("children" in parent_node && Array.isArray(parent_node.children)) {
2028+
if (
2029+
grida.program.nodes.is.ichildren(parent_node) &&
2030+
Array.isArray(parent_node.children)
2031+
) {
20292032
if (parent_node.children.length === 0) {
20302033
// Remove the empty boolean/group node
20312034
self_try_remove_node(draft, parent_id);

editor/grida-canvas/reducers/methods/insert.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function self_insertSubDocument<S extends editor.state.IEditorState>(
2121
const parent_node = draft.document.nodes[parent_id];
2222
assert(parent_node, `Parent node not found with id: "${parent_id}"`);
2323
assert(
24-
"children" in parent_node,
24+
grida.program.nodes.is.ichildren(parent_node),
2525
`Parent must be a container node: "${parent_id}"`
2626
);
2727

@@ -98,7 +98,10 @@ export function self_try_insert_node<S extends editor.state.IEditorState>(
9898

9999
// TODO: this part shall be removed and ensured with data strictness
100100
// Initialize the parent's children array if it doesn't exist
101-
if (!("children" in parent_node) || !parent_node.children) {
101+
if (
102+
!grida.program.nodes.is.ichildren(parent_node) ||
103+
!parent_node.children
104+
) {
102105
assert(
103106
parent_node.type === "container",
104107
"Parent must be a container node"

editor/grida-canvas/reducers/methods/move.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Draft } from "immer";
2-
import type grida from "@grida/schema";
2+
import grida from "@grida/schema";
33
import { editor } from "@/grida-canvas";
44
import { mv } from "@grida/tree";
55
import { dq } from "@/grida-canvas/query";
@@ -30,7 +30,7 @@ export function self_moveNode<S extends editor.state.IEditorState>(
3030

3131
// validate target is a container
3232
const target = itree[target_id];
33-
if (!("children" in target)) {
33+
if (!grida.program.nodes.is.ichildren(target)) {
3434
return false;
3535
}
3636

editor/grida-canvas/reducers/methods/wrap.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ export function self_ungroup<S extends editor.state.IEditorState>(
196196
const node = dq.__getNodeById(draft, node_id);
197197

198198
// Ensure the node has children (group or boolean nodes)
199-
if (!("children" in node) || !Array.isArray(node.children)) {
199+
if (
200+
!grida.program.nodes.is.ichildren(node) ||
201+
!Array.isArray(node.children)
202+
) {
200203
return;
201204
}
202205

@@ -252,7 +255,10 @@ export function self_ungroup<S extends editor.state.IEditorState>(
252255
} else {
253256
// Remove from parent's children
254257
const parent = dq.__getNodeById(draft, parent_id);
255-
if ("children" in parent && Array.isArray(parent.children)) {
258+
if (
259+
grida.program.nodes.is.ichildren(parent) &&
260+
Array.isArray(parent.children)
261+
) {
256262
const index = parent.children.indexOf(node_id);
257263
if (index !== -1) {
258264
parent.children.splice(index, 1);

editor/grida-canvas/reducers/tools/target.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function getRayTarget(
4242
.filter((node_id) => {
4343
const node = nodes[node_id];
4444
const top_id = dq.getTopId(context.document_ctx, node_id);
45-
const maybeichildren = ichildren(node);
45+
const maybeichildren = childrenOf(node);
4646

4747
// Check if this is a root node with children that should be ignored
4848
if (
@@ -131,7 +131,7 @@ export function getMarqueeSelection(
131131
const hit = dq.__getNodeById(state, hit_id);
132132

133133
// (1) shall not be a root node (if configured)
134-
const maybeichildren = ichildren(hit);
134+
const maybeichildren = childrenOf(hit);
135135
if (
136136
maybeichildren &&
137137
maybeichildren.length > 0 &&
@@ -167,9 +167,9 @@ export function getMarqueeSelection(
167167
return target_node_ids;
168168
}
169169

170-
function ichildren(node: any): Array<any> | undefined {
170+
function childrenOf<T>(node: T): Array<string> | undefined {
171171
if (!node) return undefined;
172-
if ("children" in node && Array.isArray(node.children)) {
172+
if (grida.program.nodes.is.ichildren(node) && Array.isArray(node.children)) {
173173
return node.children;
174174
}
175175
return undefined;

packages/grida-canvas-schema/grida.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,16 @@ export namespace grida.program.nodes {
19231923
}
19241924
}
19251925

1926+
export namespace is {
1927+
/**
1928+
* @param node node to check
1929+
* @returns true if the node is a children reference
1930+
*/
1931+
export function ichildren(node: any): node is i.IChildrenReference {
1932+
return "children" in node;
1933+
}
1934+
}
1935+
19261936
type __ReplaceSubset<T, TSubset extends Partial<T>, TNew> = Omit<
19271937
T,
19281938
keyof TSubset
@@ -2644,7 +2654,10 @@ export namespace grida.program.nodes {
26442654
delete prototype._$id;
26452655

26462656
// Handle children recursively, if the node has children
2647-
if ("children" in node && Array.isArray(node.children)) {
2657+
if (
2658+
grida.program.nodes.is.ichildren(node) &&
2659+
Array.isArray(node.children)
2660+
) {
26482661
(prototype as __IPrototypeNodeChildren).children = node.children.map(
26492662
(childId) => createPrototypeFromSnapshot(snapshot, childId)
26502663
);

0 commit comments

Comments
 (0)