Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: delete prop resources #4839

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions apps/builder/app/builder/features/ai/apply-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
$styles,
} from "~/shared/nano-states";
import type { InstanceSelector } from "~/shared/tree-utils";
import { $selectedInstance } from "~/shared/awareness";
import { $selectedInstance, getInstancePath } from "~/shared/awareness";
import { isInstanceDetachable } from "~/shared/matcher";

export const applyOperations = (operations: operations.WsOperations) => {
Expand Down Expand Up @@ -107,7 +107,10 @@ const deleteInstanceByOp = (
) {
return;
}
deleteInstanceMutable(data, instanceSelector);
deleteInstanceMutable(
data,
getInstancePath(instanceSelector, data.instances)
);
});
}
};
Expand Down
12 changes: 10 additions & 2 deletions apps/builder/app/builder/features/pages/page-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import {
$variableValuesByInstanceSelector,
} from "~/shared/nano-states";
import { insertPageCopyMutable } from "~/shared/page-utils";
import { $selectedPage, getInstanceKey, selectPage } from "~/shared/awareness";
import {
$selectedPage,
getInstanceKey,
getInstancePath,
selectPage,
} from "~/shared/awareness";

/**
* When page or folder needs to be deleted or moved to a different parent,
Expand Down Expand Up @@ -209,7 +214,10 @@ export const deletePageMutable = (pageId: Page["id"], data: WebstudioData) => {
}
const rootInstanceId = findPageByIdOrPath(pageId, pages)?.rootInstanceId;
if (rootInstanceId !== undefined) {
deleteInstanceMutable(data, [rootInstanceId]);
deleteInstanceMutable(
data,
getInstancePath([rootInstanceId], data.instances)
);
}
removeByMutable(pages.pages, (page) => page.id === pageId);
cleanupChildRefsMutable(pageId, pages.folders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { skipInertHandlersAttribute } from "~/builder/shared/inert-handlers";

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

export const TemplatesMenu = ({
onOpenChange,
Expand Down Expand Up @@ -382,7 +383,10 @@ export const BlockChildHoveredInstanceOutline = () => {
}

updateWebstudioData((data) => {
deleteInstanceMutable(data, outline.selector);
deleteInstanceMutable(
data,
getInstancePath(outline.selector, data.instances)
);
});

setButtonOutline(undefined);
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/app/builder/shared/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const deleteSelectedInstance = () => {
newSelectedInstanceSelector = parentInstanceSelector;
}
updateWebstudioData((data) => {
if (deleteInstanceMutable(data, selectedInstanceSelector)) {
if (deleteInstanceMutable(data, instancePath)) {
selectInstance(newSelectedInstanceSelector);
}
});
Expand Down
23 changes: 19 additions & 4 deletions apps/builder/app/canvas/features/text-editor/text-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import { setDataCollapsed } from "~/canvas/collapsed";
import {
$selectedPage,
addTemporaryInstance,
getInstancePath,
selectInstance,
} from "~/shared/awareness";
import { shallowEqual } from "shallow-equal";
Expand Down Expand Up @@ -1044,7 +1045,10 @@ const RichTextContentPluginInternal = ({

if (blockChildSelector) {
updateWebstudioData((data) => {
deleteInstanceMutable(data, rootInstanceSelector);
deleteInstanceMutable(
data,
getInstancePath(rootInstanceSelector, data.instances)
);
});
}
}
Expand Down Expand Up @@ -1113,7 +1117,10 @@ const RichTextContentPluginInternal = ({
updateWebstudioData((data) => {
deleteInstanceMutable(
data,
isLastChild ? parentInstanceSelector : rootInstanceSelector
getInstancePath(
isLastChild ? parentInstanceSelector : rootInstanceSelector,
data.instances
)
);
});

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

updateWebstudioData((data) => {
deleteInstanceMutable(data, blockChildSelector);
deleteInstanceMutable(
data,
getInstancePath(blockChildSelector, data.instances)
);
});

event.preventDefault();
Expand Down Expand Up @@ -1218,7 +1228,12 @@ const RichTextContentPluginInternal = ({
updateWebstudioData((data) => {
deleteInstanceMutable(
data,
isLastChild ? parentInstanceSelector : rootInstanceSelector
getInstancePath(
isLastChild
? parentInstanceSelector
: rootInstanceSelector,
data.instances
)
);
});
}
Expand Down
20 changes: 10 additions & 10 deletions apps/builder/app/shared/awareness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ export type InstancePath = Array<{
instanceSelector: string[];
}>;

const getInstancePath = (
export const getInstancePath = (
instanceSelector: string[],
instances: Instances,
virtualInstances: Instances,
temporaryInstances: Instances,
instanceSelector: string[]
virtualInstances?: Instances,
temporaryInstances?: Instances
): InstancePath => {
const instancePath: InstancePath = [];
for (let index = 0; index < instanceSelector.length; index += 1) {
const instanceId = instanceSelector[index];
const instance =
instances.get(instanceId) ??
virtualInstances.get(instanceId) ??
temporaryInstances.get(instanceId);
virtualInstances?.get(instanceId) ??
temporaryInstances?.get(instanceId);
// collection item can be undefined
if (instance === undefined) {
continue;
Expand All @@ -114,10 +114,10 @@ export const $selectedInstancePath = computed(
return;
}
return getInstancePath(
instanceSelector,
instances,
virtualInstances,
temporaryInstances,
instanceSelector
temporaryInstances
);
}
);
Expand All @@ -134,10 +134,10 @@ export const $selectedInstancePathWithRoot = computed(
instanceSelector = [...instanceSelector, ROOT_INSTANCE_ID];
}
return getInstancePath(
instanceSelector,
instances,
virtualInstances,
temporaryInstances,
instanceSelector
temporaryInstances
);
}
);
Expand Down
11 changes: 6 additions & 5 deletions apps/builder/app/shared/copy-paste/plugin-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
findClosestInsertable,
} from "../instance-utils";
import { isInstanceDetachable } from "../matcher";
import { $selectedInstancePath } from "../awareness";

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

Expand Down Expand Up @@ -203,20 +204,20 @@ export const onCopy = () => {
};

export const onCut = () => {
const selectedInstanceSelector = $selectedInstanceSelector.get();
if (selectedInstanceSelector === undefined) {
const instancePath = $selectedInstancePath.get();
if (instancePath === undefined) {
return;
}
// @todo tell user they can't delete root
if (selectedInstanceSelector.length === 1) {
if (instancePath.length === 1) {
return;
}
const data = getTreeData(selectedInstanceSelector);
const data = getTreeData(instancePath[0].instanceSelector);
if (data === undefined) {
return;
}
updateWebstudioData((data) => {
deleteInstanceMutable(data, selectedInstanceSelector);
deleteInstanceMutable(data, instancePath);
});
if (data === undefined) {
return;
Expand Down
Loading