@zoneflow/core 0.0.1

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/wrap.js ADDED
@@ -0,0 +1,103 @@
1
+ import { createZoneId } from "./ids";
2
+ export function canWrapZones(model, zoneIds) {
3
+ if (zoneIds.length === 0) {
4
+ return { ok: false, reason: "EMPTY_SELECTION" };
5
+ }
6
+ const uniqueZoneIds = new Set(zoneIds);
7
+ if (uniqueZoneIds.size !== zoneIds.length) {
8
+ return { ok: false, reason: "DUPLICATE_IDS" };
9
+ }
10
+ const zones = zoneIds.map((zoneId) => model.zonesById[zoneId]);
11
+ if (zones.some((zone) => !zone)) {
12
+ return { ok: false, reason: "MISSING_ZONE" };
13
+ }
14
+ const firstParentZoneId = zones[0].parentZoneId;
15
+ const sameParent = zones.every((zone) => zone.parentZoneId === firstParentZoneId);
16
+ if (!sameParent) {
17
+ return { ok: false, reason: "DIFFERENT_PARENTS" };
18
+ }
19
+ return { ok: true };
20
+ }
21
+ export function wrapZonesWithNewParent(model, input) {
22
+ const { zoneIds, name, zoneType = "container", action, meta, newZoneId = createZoneId(), } = input;
23
+ const check = canWrapZones(model, zoneIds);
24
+ if (!check.ok) {
25
+ return {
26
+ model,
27
+ wrapperZoneId: newZoneId,
28
+ };
29
+ }
30
+ const uniqueZoneIds = [...new Set(zoneIds)];
31
+ const firstZone = model.zonesById[uniqueZoneIds[0]];
32
+ const parentZoneId = firstZone.parentZoneId;
33
+ if (model.zonesById[newZoneId]) {
34
+ return {
35
+ model,
36
+ wrapperZoneId: newZoneId,
37
+ };
38
+ }
39
+ const wrapperZone = {
40
+ id: newZoneId,
41
+ parentZoneId,
42
+ name,
43
+ zoneType,
44
+ childZoneIds: [...uniqueZoneIds],
45
+ pathIds: [],
46
+ pathsById: {},
47
+ action,
48
+ meta,
49
+ };
50
+ let nextZonesById = {
51
+ ...model.zonesById,
52
+ [newZoneId]: wrapperZone,
53
+ };
54
+ for (const zoneId of uniqueZoneIds) {
55
+ const zone = nextZonesById[zoneId];
56
+ if (!zone)
57
+ continue;
58
+ nextZonesById[zoneId] = {
59
+ ...zone,
60
+ parentZoneId: newZoneId,
61
+ };
62
+ }
63
+ let nextRootZoneIds = [...model.rootZoneIds];
64
+ if (parentZoneId !== null) {
65
+ const parent = nextZonesById[parentZoneId];
66
+ if (parent) {
67
+ const selectedSet = new Set(uniqueZoneIds);
68
+ const originalChildren = parent.childZoneIds;
69
+ const firstSelectedIndex = originalChildren.findIndex((id) => selectedSet.has(id));
70
+ const remainingChildren = originalChildren.filter((id) => !selectedSet.has(id));
71
+ const insertIndex = firstSelectedIndex >= 0 ? firstSelectedIndex : remainingChildren.length;
72
+ const nextChildZoneIds = [
73
+ ...remainingChildren.slice(0, insertIndex),
74
+ newZoneId,
75
+ ...remainingChildren.slice(insertIndex),
76
+ ];
77
+ nextZonesById[parentZoneId] = {
78
+ ...parent,
79
+ childZoneIds: nextChildZoneIds,
80
+ };
81
+ }
82
+ }
83
+ else {
84
+ const selectedSet = new Set(uniqueZoneIds);
85
+ const originalRoots = model.rootZoneIds;
86
+ const firstSelectedIndex = originalRoots.findIndex((id) => selectedSet.has(id));
87
+ const remainingRoots = originalRoots.filter((id) => !selectedSet.has(id));
88
+ const insertIndex = firstSelectedIndex >= 0 ? firstSelectedIndex : remainingRoots.length;
89
+ nextRootZoneIds = [
90
+ ...remainingRoots.slice(0, insertIndex),
91
+ newZoneId,
92
+ ...remainingRoots.slice(insertIndex),
93
+ ];
94
+ }
95
+ return {
96
+ model: {
97
+ ...model,
98
+ rootZoneIds: nextRootZoneIds,
99
+ zonesById: nextZonesById,
100
+ },
101
+ wrapperZoneId: newZoneId,
102
+ };
103
+ }
@@ -0,0 +1,4 @@
1
+ import type { Zone } from "./types";
2
+ export declare function canZoneContainChildren(zone: Pick<Zone, "zoneType"> | null | undefined): boolean;
3
+ export declare function isZoneInputEnabled(zone: Pick<Zone, "inputDisabled"> | null | undefined): boolean;
4
+ export declare function isZoneOutputEnabled(zone: Pick<Zone, "outputDisabled"> | null | undefined): boolean;
@@ -0,0 +1,9 @@
1
+ export function canZoneContainChildren(zone) {
2
+ return zone?.zoneType === "container";
3
+ }
4
+ export function isZoneInputEnabled(zone) {
5
+ return !zone?.inputDisabled;
6
+ }
7
+ export function isZoneOutputEnabled(zone) {
8
+ return !zone?.outputDisabled;
9
+ }
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@zoneflow/core",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git@github-groobee:groobee/zoneflow.git",
10
+ "directory": "packages/core"
11
+ },
12
+ "publishConfig": {
13
+ "registry": "https://registry.npmjs.org"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc -p tsconfig.json",
20
+ "type-check": "tsc -p tsconfig.json --noEmit"
21
+ },
22
+ "dependencies": {
23
+ "uuid": "^13.0.0"
24
+ }
25
+ }