Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
feat: Add delete unused assets command and dialog as well as button in assets panel (#5635)
Oleg Isonen994a4f787aa4
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsxindex 6820048bed..a5eddec201 100644--- a/apps/builder/app/builder/builder.tsx+++ b/apps/builder/app/builder/builder.tsx@@ -63,6 +63,7 @@ import { CommandPanel } from "./features/command-panel";import { DeleteUnusedTokensDialog } from "~/builder/shared/style-source-actions";import { DeleteUnusedDataVariablesDialog } from "~/builder/shared/data-variable-utils";import { DeleteUnusedCssVariablesDialog } from "~/builder/shared/css-variable-utils";+import { DeleteUnusedAssetsDialog } from "~/builder/shared/asset-manager/delete-unused-assets";import { KeyboardShortcutsDialog } from "./features/keyboard-shortcuts-dialog";import { TokenConflictDialog } from "~/shared/token-conflict-dialog";@@ -478,6 +479,7 @@ export const Builder = ({<DeleteUnusedTokensDialog /><DeleteUnusedDataVariablesDialog /><DeleteUnusedCssVariablesDialog />+ <DeleteUnusedAssetsDialog /><KeyboardShortcutsDialog /><TokenConflictDialog /><RemoteDialog />diff --git a/apps/builder/app/builder/features/assets/assets.tsx b/apps/builder/app/builder/features/assets/assets.tsxindex f935a5d0c5..fa201c576a 100644--- a/apps/builder/app/builder/features/assets/assets.tsx+++ b/apps/builder/app/builder/features/assets/assets.tsx@@ -1,11 +1,31 @@-import { PanelTitle, Separator } from "@webstudio-is/design-system";+import {+ IconButton,+ PanelTitle,+ Separator,+ Tooltip,+} from "@webstudio-is/design-system";+import { BrushCleaningIcon } from "@webstudio-is/icons";import { AssetManager } from "~/builder/shared/asset-manager";import { AssetUpload } from "~/builder/shared/assets";+import { openDeleteUnusedAssetsDialog } from "~/builder/shared/asset-manager/delete-unused-assets";export const AssetsPanel = (_props: { onClose: () => void }) => {return (<>- <PanelTitle suffix={<AssetUpload type="file" />}>Assets</PanelTitle>+ <PanelTitle+ suffix={+ <>+ <Tooltip content="Delete unused assets">+ <IconButton onClick={openDeleteUnusedAssetsDialog}>+ <BrushCleaningIcon />+ </IconButton>+ </Tooltip>+ <AssetUpload type="file" />+ </>+ }+ >+ Assets+ </PanelTitle><Separator /><AssetManager /></>diff --git a/apps/builder/app/builder/shared/asset-manager/asset-info.tsx b/apps/builder/app/builder/shared/asset-manager/asset-info.tsxindex d436066133..1331fd9847 100644--- a/apps/builder/app/builder/shared/asset-manager/asset-info.tsx+++ b/apps/builder/app/builder/shared/asset-manager/asset-info.tsx@@ -109,7 +109,7 @@ const traverseStyleValue = (}};-const calculateUsagesByAssetId = ({+export const calculateUsagesByAssetId = ({pages,props,styles,diff --git a/apps/builder/app/builder/shared/asset-manager/delete-unused-assets.tsx b/apps/builder/app/builder/shared/asset-manager/delete-unused-assets.tsxnew file mode 100644index 0000000000..05f46be057--- /dev/null+++ b/apps/builder/app/builder/shared/asset-manager/delete-unused-assets.tsx@@ -0,0 +1,130 @@+import { atom } from "nanostores";+import { useStore } from "@nanostores/react";+import {+ Dialog,+ DialogContent,+ DialogTitle,+ DialogClose,+ ScrollArea,+ Button,+ Text,+ Flex,+ theme,+ toast,+ Box,+} from "@webstudio-is/design-system";+import type { Asset } from "@webstudio-is/sdk";+import { $assets, $pages, $props, $styles } from "~/shared/nano-states";+import { deleteAssets } from "~/builder/shared/assets";+import { formatAssetName } from "~/builder/shared/assets/asset-utils";+import { calculateUsagesByAssetId } from "./asset-info";++const $isDeleteUnusedAssetsDialogOpen = atom(false);++export const openDeleteUnusedAssetsDialog = () => {+ $isDeleteUnusedAssetsDialogOpen.set(true);+};++const getUnusedAssets = () => {+ const assets = $assets.get();+ const usagesByAssetId = calculateUsagesByAssetId({+ pages: $pages.get(),+ props: $props.get(),+ styles: $styles.get(),+ assets,+ });+ const unusedAssets: Asset[] = [];+ for (const asset of assets.values()) {+ const usages = usagesByAssetId.get(asset.id);+ if (usages === undefined || usages.length === 0) {+ unusedAssets.push(asset);+ }+ }+ return unusedAssets;+};++const DeleteUnusedAssetsDialogContent = ({+ onClose,+}: {+ onClose: () => void;+}) => {+ const unusedAssets = getUnusedAssets();++ return (+ <Flex gap="3" direction="column" css={{ padding: theme.panel.padding }}>+ {unusedAssets.length === 0 ? (+ <Text>There are no unused assets to delete.</Text>+ ) : (+ <>+ <Text>+ Delete {unusedAssets.length} unused{" "}+ {unusedAssets.length === 1 ? "asset" : "assets"} from the project?+ </Text>++ <ScrollArea>+ <Box css={{ maxHeight: 200 }}>+ <Flex direction="column" gap="1">+ {unusedAssets.map((asset) => (+ <Text key={asset.id} variant="mono" truncate>+ {formatAssetName(asset)}+ </Text>+ ))}+ </Flex>+ </Box>+ </ScrollArea>+ </>+ )}+ <Flex direction="rowReverse" gap="2">+ {unusedAssets.length > 0 && (+ <Button+ color="destructive"+ onClick={() => {+ const count = unusedAssets.length;+ deleteAssets(unusedAssets.map((asset) => asset.id));+ onClose();+ toast.success(+ `Deleted ${count} unused ${count === 1 ? "asset" : "assets"}`+ );+ }}+ autoFocus+ >+ Delete+ </Button>+ )}+ <DialogClose>+ <Button color="ghost">+ {unusedAssets.length > 0 ? "Cancel" : "Close"}+ </Button>+ </DialogClose>+ </Flex>+ </Flex>+ );+};++export const DeleteUnusedAssetsDialog = () => {+ const open = useStore($isDeleteUnusedAssetsDialogOpen);+ const handleClose = () => {+ $isDeleteUnusedAssetsDialogOpen.set(false);+ };++ return (+ <Dialog+ open={open}+ onOpenChange={(isOpen) => {+ if (isOpen === false) {+ handleClose();+ }+ }}+ >+ <DialogContent+ width={400}+ onKeyDown={(event) => {+ event.stopPropagation();+ }}+ >+ <DialogTitle>Delete unused assets</DialogTitle>+ <DeleteUnusedAssetsDialogContent onClose={handleClose} />+ </DialogContent>+ </Dialog>+ );+};diff --git a/apps/builder/app/builder/shared/commands.ts b/apps/builder/app/builder/shared/commands.tsindex 08b28ed117..dae1a47f50 100644--- a/apps/builder/app/builder/shared/commands.ts+++ b/apps/builder/app/builder/shared/commands.ts@@ -52,6 +52,7 @@ import { isSyncIdle } from "~/shared/sync/project-queue";import { openDeleteUnusedTokensDialog } from "~/builder/shared/style-source-actions";import { openDeleteUnusedDataVariablesDialog } from "~/builder/shared/data-variable-utils";import { openDeleteUnusedCssVariablesDialog } from "~/builder/shared/css-variable-utils";+import { openDeleteUnusedAssetsDialog } from "~/builder/shared/asset-manager/delete-unused-assets";import { openKeyboardShortcutsDialog } from "~/builder/features/keyboard-shortcuts-dialog";import {copyInstance,@@ -558,6 +559,15 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({},},+ {+ name: "deleteUnusedAssets",+ label: "Delete unused assets",+ description: "Remove unused assets",+ handler: () => {+ openDeleteUnusedAssetsDialog();+ },+ },+{name: "openKeyboardShortcuts",description: "View keyboard shortcuts",diff --git a/apps/builder/app/builder/shared/css-variable-utils.tsx b/apps/builder/app/builder/shared/css-variable-utils.tsxindex f8ee99897d..78a2f7c2f0 100644--- a/apps/builder/app/builder/shared/css-variable-utils.tsx+++ b/apps/builder/app/builder/shared/css-variable-utils.tsx@@ -640,6 +640,7 @@ const DeleteUnusedCssVariablesDialogContent = ({{unusedVariablesArray.length > 0 && (<Buttoncolor="destructive"+ autoFocusonClick={() => {const deletedCount = deleteUnusedCssVariables();onClose();diff --git a/apps/builder/app/builder/shared/data-variable-utils.tsx b/apps/builder/app/builder/shared/data-variable-utils.tsxindex 5fd1aa610f..caaa828274 100644--- a/apps/builder/app/builder/shared/data-variable-utils.tsx+++ b/apps/builder/app/builder/shared/data-variable-utils.tsx@@ -338,6 +338,7 @@ export const DeleteUnusedDataVariablesDialog = () => {{unusedVariables.length > 0 && (<Buttoncolor="destructive"+ autoFocusonClick={() => {const deletedCount = deleteUnusedDataVariables();handleClose();diff --git a/apps/builder/app/builder/shared/style-source-actions.tsx b/apps/builder/app/builder/shared/style-source-actions.tsxindex 5346537446..879b1198a4 100644--- a/apps/builder/app/builder/shared/style-source-actions.tsx+++ b/apps/builder/app/builder/shared/style-source-actions.tsx@@ -326,6 +326,7 @@ export const DeleteUnusedTokensDialog = () => {{unusedTokens.length > 0 && (<Buttoncolor="destructive"+ autoFocusonClick={() => {const deletedCount = deleteUnusedTokens();handleClose();diff --git a/packages/icons/icons/brush-cleaning.svg b/packages/icons/icons/brush-cleaning.svgnew file mode 100644index 0000000000..c722c7d121--- /dev/null+++ b/packages/icons/icons/brush-cleaning.svg@@ -0,0 +1,14 @@+<svg+ width="16"+ height="16"+ viewBox="0 0 16 16"+ fill="none"+ xmlns="http://www.w3.org/2000/svg"+>+ <path+ d="M10.6667 14.6667L10 12M12.6667 9.33333C12.8435 9.33333 13.0131 9.26309 13.1381 9.13807C13.2631 9.01305 13.3334 8.84348 13.3334 8.66667V8C13.3334 7.64638 13.1929 7.30724 12.9428 7.05719C12.6928 6.80714 12.3536 6.66667 12 6.66667H10C9.82321 6.66667 9.65364 6.59643 9.52862 6.4714C9.40359 6.34638 9.33335 6.17681 9.33335 6V2.66667C9.33335 2.31304 9.19288 1.9739 8.94283 1.72386C8.69278 1.47381 8.35364 1.33333 8.00002 1.33333C7.6464 1.33333 7.30726 1.47381 7.05721 1.72386C6.80716 1.9739 6.66669 2.31304 6.66669 2.66667V6C6.66669 6.17681 6.59645 6.34638 6.47143 6.4714C6.3464 6.59643 6.17683 6.66667 6.00002 6.66667H4.00002C3.6464 6.66667 3.30726 6.80714 3.05721 7.05719C2.80716 7.30724 2.66669 7.64638 2.66669 8V8.66667C2.66669 8.84348 2.73693 9.01305 2.86195 9.13807C2.98697 9.26309 3.15654 9.33333 3.33335 9.33333M12.6667 9.33333H3.33335M12.6667 9.33333L13.982 13.8447C14.0055 13.9427 14.0064 14.0447 13.9848 14.1432C13.9631 14.2416 13.9194 14.3339 13.857 14.413C13.7946 14.4921 13.715 14.556 13.6243 14.6C13.5336 14.6439 13.4341 14.6667 13.3333 14.6667H2.66665C2.56586 14.6667 2.46637 14.6439 2.37567 14.6C2.28496 14.556 2.20539 14.4921 2.14296 14.413C2.08052 14.3339 2.03684 14.2416 2.01519 14.1432C1.99354 14.0447 1.9945 13.9427 2.01798 13.8447L3.33335 9.33333M5.33331 14.6667L5.99998 12"+ stroke="currentColor"+ stroke-linecap="round"+ stroke-linejoin="round"+ />+</svg>diff --git a/packages/icons/src/__generated__/components.tsx b/packages/icons/src/__generated__/components.tsxindex 4af2103dee..4e7b66bc4f 100644--- a/packages/icons/src/__generated__/components.tsx+++ b/packages/icons/src/__generated__/components.tsx@@ -1476,6 +1476,30 @@ export const BracesIcon: IconComponent = forwardRef();BracesIcon.displayName = "BracesIcon";+export const BrushCleaningIcon: IconComponent = forwardRef(+ ({ fill = "none", size = 16, ...props }, forwardedRef) => {+ return (+ <svg+ xmlns="http://www.w3.org/2000/svg"+ viewBox="0 0 16 16"+ width={size}+ height={size}+ fill={fill}+ {...props}+ ref={forwardedRef}+ >+ <path+ stroke="currentColor"+ strokeLinecap="round"+ strokeLinejoin="round"+ d="M10.667 14.667 10 12m2.667-2.667a.667.667 0 0 0 .666-.666V8A1.333 1.333 0 0 0 12 6.667h-2A.667.667 0 0 1 9.333 6V2.667a1.333 1.333 0 1 0-2.666 0V6A.667.667 0 0 1 6 6.667H4A1.333 1.333 0 0 0 2.667 8v.667a.667.667 0 0 0 .666.666m9.334 0H3.333m9.334 0 1.315 4.512a.667.667 0 0 1-.649.822H2.667a.668.668 0 0 1-.649-.822l1.315-4.512m2 5.334L6 12"+ />+ </svg>+ );+ }+);+BrushCleaningIcon.displayName = "BrushCleaningIcon";+export const BugIcon: IconComponent = forwardRef(({ fill = "none", size = 16, ...props }, forwardedRef) => {return (diff --git a/packages/icons/src/__generated__/svg.ts b/packages/icons/src/__generated__/svg.tsindex 46697d0f56..ebed9c4526 100644--- a/packages/icons/src/__generated__/svg.ts+++ b/packages/icons/src/__generated__/svg.ts@@ -110,6 +110,8 @@ export const BoxIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewexport const BracesIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M5.333 2h-.666a1.333 1.333 0 0 0-1.334 1.333v3.334A1.333 1.333 0 0 1 2 8a1.333 1.333 0 0 1 1.333 1.333v3.334c0 .733.6 1.333 1.334 1.333h.666M10.667 14h.666a1.333 1.333 0 0 0 1.334-1.333V9.333C12.667 8.6 13.267 8 14 8a1.333 1.333 0 0 1-1.333-1.333V3.333A1.333 1.333 0 0 0 11.333 2h-.666"/></svg>`;+export const BrushCleaningIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M10.667 14.667 10 12m2.667-2.667a.667.667 0 0 0 .666-.666V8A1.333 1.333 0 0 0 12 6.667h-2A.667.667 0 0 1 9.333 6V2.667a1.333 1.333 0 1 0-2.666 0V6A.667.667 0 0 1 6 6.667H4A1.333 1.333 0 0 0 2.667 8v.667a.667.667 0 0 0 .666.666m9.334 0H3.333m9.334 0 1.315 4.512a.667.667 0 0 1-.649.822H2.667a.668.668 0 0 1-.649-.822l1.315-4.512m2 5.334L6 12"/></svg>`;+export const BugIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="m5.333 1.333 1.254 1.254M9.413 2.587l1.254-1.254M6 4.753v-.666a2.002 2.002 0 1 1 4 0v.666"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M8 13.333c-2.2 0-4-1.8-4-4v-2a2.667 2.667 0 0 1 2.667-2.666h2.666A2.667 2.667 0 0 1 12 7.333v2c0 2.2-1.8 4-4 4ZM8 13.333v-6"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M4.353 6C3.067 5.867 2 4.733 2 3.333M4 8.667H1.333M2 14c0-1.4 1.133-2.6 2.533-2.667M13.98 3.333c0 1.4-1.067 2.534-2.333 2.667M14.667 8.667H12M11.467 11.333C12.867 11.4 14 12.6 14 14"/></svg>`;export const ButtonElementIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path fill="currentColor" fill-rule="evenodd" d="M1.833 8a3.5 3.5 0 0 1 3.5-3.5h5.334a3.5 3.5 0 1 1 0 7H5.333a3.5 3.5 0 0 1-3.5-3.5Zm3.5-4.5a4.5 4.5 0 0 0 0 9h5.334a4.5 4.5 0 1 0 0-9H5.333ZM5.72 8a1 1 0 1 1-2 0 1 1 0 0 1 2 0ZM8 9a1 1 0 1 0 0-2 1 1 0 0 0 0 2Zm4.28-1a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z" clip-rule="evenodd"/></svg>`;@@ -168,7 +170,7 @@ export const DotIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewexport const DottedBorderIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M5.464 8a.667.667 0 1 0 1.333 0 .667.667 0 0 0-1.333 0ZM9.202 8a.667.667 0 1 0 1.334 0 .667.667 0 0 0-1.334 0ZM12.94 8a.667.667 0 1 0 1.334 0 .667.667 0 0 0-1.333 0ZM1.726 8a.667.667 0 1 0 1.333 0 .667.667 0 0 0-1.333 0Z"/></svg>`;-export const DownloadIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="lucide lucide-download-icon lucide-download" viewBox="0 0 24 24" width="100%" height="100%" style="display: block;"><path d="M12 15V3M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m7 10 5 5 5-5"/></svg>`;+export const DownloadIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24" width="100%" height="100%" style="display: block;"><path d="M12 15V3M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m7 10 5 5 5-5"/></svg>`;export const DragHandleIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M6 8.667a.667.667 0 1 0 0-1.334.667.667 0 0 0 0 1.334ZM6 4a.667.667 0 1 0 0-1.333A.667.667 0 0 0 6 4ZM6 13.333A.667.667 0 1 0 6 12a.667.667 0 0 0 0 1.333ZM10 8.667a.667.667 0 1 0 0-1.334.667.667 0 0 0 0 1.334ZM10 4a.667.667 0 1 0 0-1.333A.667.667 0 0 0 10 4ZM10 13.333A.667.667 0 1 0 10 12a.667.667 0 0 0 0 1.333Z"/></svg>`;@@ -342,7 +344,7 @@ export const ShadowInsetIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="noexport const ShadowNormalIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M8 12.327a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11Z"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" d="M15 10v0c-2.774 5.944-11.226 5.944-14 0v0"/></svg>`;-export const ShieldIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-shield-icon lucide-shield" viewBox="0 0 24 24" width="100%" height="100%" style="display: block;"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></svg>`;+export const ShieldIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24" width="100%" height="100%" style="display: block;"><path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/></svg>`;export const ShrinkIcon = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 16" width="100%" height="100%" style="display: block;"><path fill="currentColor" fill-rule="evenodd" d="M8 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-1 0v-1A.5.5 0 0 1 8 1ZM1.333 7.5a.5.5 0 0 0 0 1h2.793L2.98 9.646a.5.5 0 0 0 .707.708l2-2a.499.499 0 0 0 .146-.351v-.006a.498.498 0 0 0-.146-.35l-2-2a.5.5 0 1 0-.707.707L4.126 7.5H1.333Zm8.834.503a.496.496 0 0 0 .146.35l2 2a.5.5 0 0 0 .707-.707L11.874 8.5h2.793a.5.5 0 1 0 0-1h-2.793l1.146-1.146a.5.5 0 1 0-.707-.708l-2 2a.498.498 0 0 0-.146.351v.006ZM8.5 5.5a.5.5 0 0 0-1 0v1a.5.5 0 0 0 1 0v-1ZM8 9a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-1 0v-1A.5.5 0 0 1 8 9Zm.5 4.5a.5.5 0 0 0-1 0v1a.5.5 0 0 0 1 0v-1Z" clip-rule="evenodd"/></svg>`;