Skip to content

Commit 453956b

Browse files
authored
refactor: delete prop resources (#4839)
Ref #4093 Here a few delete instance improvements - fixed deleting resources bound to props (action in webhook form) - rewrote all tests with jsx template - switched from instance selector to instance path - added slot test
1 parent 8aaa48e commit 453956b

File tree

9 files changed

+209
-139
lines changed

9 files changed

+209
-139
lines changed

apps/builder/app/builder/features/ai/apply-operations.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
$styles,
2121
} from "~/shared/nano-states";
2222
import type { InstanceSelector } from "~/shared/tree-utils";
23-
import { $selectedInstance } from "~/shared/awareness";
23+
import { $selectedInstance, getInstancePath } from "~/shared/awareness";
2424
import { isInstanceDetachable } from "~/shared/matcher";
2525

2626
export const applyOperations = (operations: operations.WsOperations) => {
@@ -107,7 +107,10 @@ const deleteInstanceByOp = (
107107
) {
108108
return;
109109
}
110-
deleteInstanceMutable(data, instanceSelector);
110+
deleteInstanceMutable(
111+
data,
112+
getInstancePath(instanceSelector, data.instances)
113+
);
111114
});
112115
}
113116
};

apps/builder/app/builder/features/pages/page-utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ import {
2323
$variableValuesByInstanceSelector,
2424
} from "~/shared/nano-states";
2525
import { insertPageCopyMutable } from "~/shared/page-utils";
26-
import { $selectedPage, getInstanceKey, selectPage } from "~/shared/awareness";
26+
import {
27+
$selectedPage,
28+
getInstanceKey,
29+
getInstancePath,
30+
selectPage,
31+
} from "~/shared/awareness";
2732

2833
/**
2934
* When page or folder needs to be deleted or moved to a different parent,
@@ -209,7 +214,10 @@ export const deletePageMutable = (pageId: Page["id"], data: WebstudioData) => {
209214
}
210215
const rootInstanceId = findPageByIdOrPath(pageId, pages)?.rootInstanceId;
211216
if (rootInstanceId !== undefined) {
212-
deleteInstanceMutable(data, [rootInstanceId]);
217+
deleteInstanceMutable(
218+
data,
219+
getInstancePath([rootInstanceId], data.instances)
220+
);
213221
}
214222
removeByMutable(pages.pages, (page) => page.id === pageId);
215223
cleanupChildRefsMutable(pageId, pages.folders);

apps/builder/app/builder/features/workspace/canvas-tools/outline/block-instance-outline.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { skipInertHandlersAttribute } from "~/builder/shared/inert-handlers";
4848

4949
import { insertTemplateAt } from "./block-utils";
5050
import { useEffectEvent } from "~/shared/hook-utils/effect-event";
51+
import { getInstancePath } from "~/shared/awareness";
5152

5253
export const TemplatesMenu = ({
5354
onOpenChange,
@@ -382,7 +383,10 @@ export const BlockChildHoveredInstanceOutline = () => {
382383
}
383384

384385
updateWebstudioData((data) => {
385-
deleteInstanceMutable(data, outline.selector);
386+
deleteInstanceMutable(
387+
data,
388+
getInstancePath(outline.selector, data.instances)
389+
);
386390
});
387391

388392
setButtonOutline(undefined);

apps/builder/app/builder/shared/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export const deleteSelectedInstance = () => {
133133
newSelectedInstanceSelector = parentInstanceSelector;
134134
}
135135
updateWebstudioData((data) => {
136-
if (deleteInstanceMutable(data, selectedInstanceSelector)) {
136+
if (deleteInstanceMutable(data, instancePath)) {
137137
selectInstance(newSelectedInstanceSelector);
138138
}
139139
});

apps/builder/app/canvas/features/text-editor/text-editor.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import { setDataCollapsed } from "~/canvas/collapsed";
8989
import {
9090
$selectedPage,
9191
addTemporaryInstance,
92+
getInstancePath,
9293
selectInstance,
9394
} from "~/shared/awareness";
9495
import { shallowEqual } from "shallow-equal";
@@ -1044,7 +1045,10 @@ const RichTextContentPluginInternal = ({
10441045

10451046
if (blockChildSelector) {
10461047
updateWebstudioData((data) => {
1047-
deleteInstanceMutable(data, rootInstanceSelector);
1048+
deleteInstanceMutable(
1049+
data,
1050+
getInstancePath(rootInstanceSelector, data.instances)
1051+
);
10481052
});
10491053
}
10501054
}
@@ -1113,7 +1117,10 @@ const RichTextContentPluginInternal = ({
11131117
updateWebstudioData((data) => {
11141118
deleteInstanceMutable(
11151119
data,
1116-
isLastChild ? parentInstanceSelector : rootInstanceSelector
1120+
getInstancePath(
1121+
isLastChild ? parentInstanceSelector : rootInstanceSelector,
1122+
data.instances
1123+
)
11171124
);
11181125
});
11191126

@@ -1128,7 +1135,10 @@ const RichTextContentPluginInternal = ({
11281135
onNext(editor.getEditorState(), { reason: "left" });
11291136

11301137
updateWebstudioData((data) => {
1131-
deleteInstanceMutable(data, blockChildSelector);
1138+
deleteInstanceMutable(
1139+
data,
1140+
getInstancePath(blockChildSelector, data.instances)
1141+
);
11321142
});
11331143

11341144
event.preventDefault();
@@ -1218,7 +1228,12 @@ const RichTextContentPluginInternal = ({
12181228
updateWebstudioData((data) => {
12191229
deleteInstanceMutable(
12201230
data,
1221-
isLastChild ? parentInstanceSelector : rootInstanceSelector
1231+
getInstancePath(
1232+
isLastChild
1233+
? parentInstanceSelector
1234+
: rootInstanceSelector,
1235+
data.instances
1236+
)
12221237
);
12231238
});
12241239
}

apps/builder/app/shared/awareness.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ export type InstancePath = Array<{
8181
instanceSelector: string[];
8282
}>;
8383

84-
const getInstancePath = (
84+
export const getInstancePath = (
85+
instanceSelector: string[],
8586
instances: Instances,
86-
virtualInstances: Instances,
87-
temporaryInstances: Instances,
88-
instanceSelector: string[]
87+
virtualInstances?: Instances,
88+
temporaryInstances?: Instances
8989
): InstancePath => {
9090
const instancePath: InstancePath = [];
9191
for (let index = 0; index < instanceSelector.length; index += 1) {
9292
const instanceId = instanceSelector[index];
9393
const instance =
9494
instances.get(instanceId) ??
95-
virtualInstances.get(instanceId) ??
96-
temporaryInstances.get(instanceId);
95+
virtualInstances?.get(instanceId) ??
96+
temporaryInstances?.get(instanceId);
9797
// collection item can be undefined
9898
if (instance === undefined) {
9999
continue;
@@ -114,10 +114,10 @@ export const $selectedInstancePath = computed(
114114
return;
115115
}
116116
return getInstancePath(
117+
instanceSelector,
117118
instances,
118119
virtualInstances,
119-
temporaryInstances,
120-
instanceSelector
120+
temporaryInstances
121121
);
122122
}
123123
);
@@ -134,10 +134,10 @@ export const $selectedInstancePathWithRoot = computed(
134134
instanceSelector = [...instanceSelector, ROOT_INSTANCE_ID];
135135
}
136136
return getInstancePath(
137+
instanceSelector,
137138
instances,
138139
virtualInstances,
139-
temporaryInstances,
140-
instanceSelector
140+
temporaryInstances
141141
);
142142
}
143143
);

apps/builder/app/shared/copy-paste/plugin-instance.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
findClosestInsertable,
2626
} from "../instance-utils";
2727
import { isInstanceDetachable } from "../matcher";
28+
import { $selectedInstancePath } from "../awareness";
2829

2930
const version = "@webstudio/instance/v0.1";
3031

@@ -203,20 +204,20 @@ export const onCopy = () => {
203204
};
204205

205206
export const onCut = () => {
206-
const selectedInstanceSelector = $selectedInstanceSelector.get();
207-
if (selectedInstanceSelector === undefined) {
207+
const instancePath = $selectedInstancePath.get();
208+
if (instancePath === undefined) {
208209
return;
209210
}
210211
// @todo tell user they can't delete root
211-
if (selectedInstanceSelector.length === 1) {
212+
if (instancePath.length === 1) {
212213
return;
213214
}
214-
const data = getTreeData(selectedInstanceSelector);
215+
const data = getTreeData(instancePath[0].instanceSelector);
215216
if (data === undefined) {
216217
return;
217218
}
218219
updateWebstudioData((data) => {
219-
deleteInstanceMutable(data, selectedInstanceSelector);
220+
deleteInstanceMutable(data, instancePath);
220221
});
221222
if (data === undefined) {
222223
return;

0 commit comments

Comments
 (0)