-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: replace asset option for image assets in asset details #5676
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8822b67
feat(assets): add replace asset button for image assets in asset details
shreyanshkotak f0bb724
chrore: documentation cleanup
shreyanshkotak 67fd9bb
feat: new icon for asset replacement
shreyanshkotak a2541ff
Merge branch 'webstudio-is:main' into replace-assets
shreyanshkotak 04ae7f8
test: add unit tests for replace asset
shreyanshkotak b6abb6e
docs: add test cases for asset replacement
shreyanshkotak 50d16d2
chore: documentation cleanup
shreyanshkotak 6904334
minor: move import to the top
shreyanshkotak a71086d
refactor: replace polling in waitForAsset with nanostores subscription
shreyanshkotak 668d0e7
fix: remove unreachable width/height in replaceAsset
shreyanshkotak ce11207
refactor: use styles.values() instead of destructuring entries
shreyanshkotak 9ac4421
minor: use new filename instead of copying
shreyanshkotak dee155b
update: replace icon replaced
shreyanshkotak e5f8268
Merge branch 'webstudio-is:main' into replace-assets
shreyanshkotak 8ac2608
fix: resolve TDZ error when replacing a previously deleted asset
shreyanshkotak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
apps/builder/app/builder/shared/assets/replace-asset.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, test, expect } from "vitest"; | ||
| import type { StyleValue } from "@webstudio-is/css-engine"; | ||
| import { __testing__ } from "./replace-asset"; | ||
|
|
||
| const { replaceAssetInStyleValue } = __testing__; | ||
|
|
||
| describe("replaceAssetInStyleValue", () => { | ||
| test("replaces asset id in a direct image value", () => { | ||
| const value: StyleValue = { | ||
| type: "image", | ||
| value: { type: "asset", value: "old-id" }, | ||
| }; | ||
| replaceAssetInStyleValue(value, "old-id", "new-id"); | ||
| expect(value).toEqual({ | ||
| type: "image", | ||
| value: { type: "asset", value: "new-id" }, | ||
| }); | ||
| }); | ||
|
|
||
| test("does not mutate image value with a different asset id", () => { | ||
| const value: StyleValue = { | ||
| type: "image", | ||
| value: { type: "asset", value: "other-id" }, | ||
| }; | ||
| replaceAssetInStyleValue(value, "old-id", "new-id"); | ||
| expect(value).toEqual({ | ||
| type: "image", | ||
| value: { type: "asset", value: "other-id" }, | ||
| }); | ||
| }); | ||
|
|
||
| test("does not mutate image value with url type", () => { | ||
| const value: StyleValue = { | ||
| type: "image", | ||
| value: { type: "url", url: "https://example.com/img.png" }, | ||
| }; | ||
| replaceAssetInStyleValue(value, "old-id", "new-id"); | ||
| expect(value).toEqual({ | ||
| type: "image", | ||
| value: { type: "url", url: "https://example.com/img.png" }, | ||
| }); | ||
| }); | ||
|
|
||
| test("replaces asset id inside a tuple value", () => { | ||
| const value: StyleValue = { | ||
| type: "tuple", | ||
| value: [ | ||
| { type: "image", value: { type: "asset", value: "old-id" } }, | ||
| { type: "image", value: { type: "asset", value: "other-id" } }, | ||
| ], | ||
| }; | ||
| replaceAssetInStyleValue(value, "old-id", "new-id"); | ||
| expect(value).toEqual({ | ||
| type: "tuple", | ||
| value: [ | ||
| { type: "image", value: { type: "asset", value: "new-id" } }, | ||
| { type: "image", value: { type: "asset", value: "other-id" } }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| test("replaces asset id inside a layers value", () => { | ||
| const value: StyleValue = { | ||
| type: "layers", | ||
| value: [ | ||
| { type: "image", value: { type: "asset", value: "old-id" } }, | ||
| { type: "image", value: { type: "asset", value: "old-id" } }, | ||
| ], | ||
| }; | ||
| replaceAssetInStyleValue(value, "old-id", "new-id"); | ||
| expect(value).toEqual({ | ||
| type: "layers", | ||
| value: [ | ||
| { type: "image", value: { type: "asset", value: "new-id" } }, | ||
| { type: "image", value: { type: "asset", value: "new-id" } }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| test("does not mutate non-image value types", () => { | ||
| const value: StyleValue = { type: "keyword", value: "auto" }; | ||
| replaceAssetInStyleValue(value, "old-id", "new-id"); | ||
| expect(value).toEqual({ type: "keyword", value: "auto" }); | ||
| }); | ||
| }); |
125 changes: 125 additions & 0 deletions
125
apps/builder/app/builder/shared/assets/replace-asset.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import type { Asset } from "@webstudio-is/sdk"; | ||
| import { toast } from "@webstudio-is/design-system"; | ||
| import { $assets, $pages, $props, $styles } from "~/shared/nano-states"; | ||
| import { serverSyncStore } from "~/shared/sync/sync-stores"; | ||
| import { onNextTransactionComplete } from "~/shared/sync/project-queue"; | ||
| import { invalidateAssets } from "~/shared/resources"; | ||
| import { uploadAssets } from "./upload-assets"; | ||
| import type { StyleValue } from "@webstudio-is/css-engine"; | ||
|
|
||
| /** | ||
| * Replace an image asset with a new file. | ||
| * | ||
| * 1. Uploads the new file via the existing upload pipeline | ||
| * 2. Waits for the upload to complete (new asset appears in $assets) | ||
| * 3. Re-points all references (props, styles, page meta) from old → new asset | ||
| * 4. Copies filename & description from old asset to new asset | ||
| * 5. Deletes the old asset | ||
| */ | ||
| export const replaceAsset = async ( | ||
| oldAssetId: Asset["id"], | ||
| file: File | ||
| ): Promise<void> => { | ||
| const oldAsset = $assets.get().get(oldAssetId); | ||
| if (!oldAsset) { | ||
| toast.error("Original asset not found"); | ||
| return; | ||
| } | ||
|
|
||
| const fileToAssetId = await uploadAssets("image", [file]); | ||
| const newAssetId = fileToAssetId.get(file); | ||
|
|
||
| if (!newAssetId) { | ||
| toast.error("Failed to upload replacement asset"); | ||
| return; | ||
| } | ||
|
|
||
| await waitForAsset(newAssetId); | ||
|
|
||
| serverSyncStore.createTransaction( | ||
| [$pages, $props, $styles, $assets], | ||
| (pages, props, styles, assets) => { | ||
| const updatedNewAsset = assets.get(newAssetId); | ||
| if (updatedNewAsset) { | ||
| updatedNewAsset.description = oldAsset.description; | ||
| } | ||
|
|
||
| for (const prop of props.values()) { | ||
| if (prop.type === "asset" && prop.value === oldAssetId) { | ||
| prop.value = newAssetId; | ||
| } | ||
| } | ||
|
|
||
| for (const styleDecl of styles.values()) { | ||
| replaceAssetInStyleValue(styleDecl.value, oldAssetId, newAssetId); | ||
| } | ||
|
|
||
| if (pages) { | ||
| if (pages.meta?.faviconAssetId === oldAssetId) { | ||
| pages.meta.faviconAssetId = newAssetId; | ||
| } | ||
| for (const page of [pages.homePage, ...pages.pages]) { | ||
| if (page.meta.socialImageAssetId === oldAssetId) { | ||
| page.meta.socialImageAssetId = newAssetId; | ||
| } | ||
| if (page.marketplace?.thumbnailAssetId === oldAssetId) { | ||
| page.marketplace.thumbnailAssetId = newAssetId; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| assets.delete(oldAssetId); | ||
| } | ||
| ); | ||
|
|
||
| onNextTransactionComplete(() => { | ||
| invalidateAssets(); | ||
| }); | ||
|
|
||
| toast.success("Asset replaced successfully"); | ||
| }; | ||
|
|
||
| const replaceAssetInStyleValue = ( | ||
| styleValue: StyleValue, | ||
| oldAssetId: string, | ||
| newAssetId: string | ||
| ): void => { | ||
| if ( | ||
| styleValue.type === "image" && | ||
| styleValue.value.type === "asset" && | ||
| styleValue.value.value === oldAssetId | ||
| ) { | ||
| styleValue.value.value = newAssetId; | ||
| } | ||
| if (styleValue.type === "tuple") { | ||
| for (const item of styleValue.value) { | ||
| replaceAssetInStyleValue(item, oldAssetId, newAssetId); | ||
| } | ||
| } | ||
| if (styleValue.type === "layers") { | ||
| for (const item of styleValue.value) { | ||
| replaceAssetInStyleValue(item, oldAssetId, newAssetId); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
|
kof marked this conversation as resolved.
|
||
| const waitForAsset = (assetId: string): Promise<Asset> => { | ||
| // Check if asset already exists (avoids TDZ with synchronous subscribe callback) | ||
| const existingAsset = $assets.get().get(assetId); | ||
| if (existingAsset !== undefined) { | ||
| return Promise.resolve(existingAsset); | ||
| } | ||
|
|
||
| // Use .listen() instead of .subscribe(), so the `unsubscribe` variable is always assigned before the callback runs. | ||
| return new Promise((resolve) => { | ||
| const unsubscribe = $assets.listen((assets) => { | ||
| const asset = assets.get(assetId); | ||
| if (asset !== undefined) { | ||
| unsubscribe(); | ||
| resolve(asset); | ||
| } | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| export const __testing__ = { replaceAssetInStyleValue }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.