Skip to content

Commit

Permalink
Saving an image as a new asset now automatically replaces the previou…
Browse files Browse the repository at this point in the history
…s selected asset when editing assets in an assets field.

Resolve #8974
  • Loading branch information
andris-sevcenko committed Oct 6, 2021
1 parent 905fc5f commit 6764c9a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
- All control panel templates end in `.twig` now. ([#9743](https://github.com/craftcms/cms/pull/9743))
- The `users/save-user` action no longer includes a `unverifiedEmail` key in failure responses.
- When using GraphQL to mutate entries, the `enabled` status is now affected on a per-site basis when specifying both the `enabled` and `siteId` parameters. ([#9771](https://github.com/craftcms/cms/issues/9771))
- Saving an image as a new asset now automatically replaces the previous selected asset when editing assets in an assets field. ([#8974](https://github.com/craftcms/cms/discussions/8974))
- Elements’ `searchScore` GraphQL fields are now returned as integers.
- Plugins’ `$changelogUrl` properties must now have a `?string` type declaration.
- Plugins’ `$description` properties must now have a `?string` type declaration.
Expand Down
7 changes: 6 additions & 1 deletion src/controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,8 @@ public function actionSaveImage(): Response
$image->saveAs($imageCopy);
}

$output = [];

if ($replace) {
$oldFocal = $asset->getHasFocalPoint() ? $asset->getFocalPoint() : null;
$focalChanged = $focal !== $oldFocal;
Expand Down Expand Up @@ -1065,12 +1067,15 @@ public function actionSaveImage(): Response

// Don't validate required custom fields
Craft::$app->getElements()->saveElement($newAsset);

$output['elementId'] = $newAsset->id;
}
} catch (Throwable $exception) {
return $this->asErrorJson($exception->getMessage());
}

return $this->asJson(['success' => true]);
$output['success'] = true;
return $this->asJson($output);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/js/Craft.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/js/Craft.min.js.map

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions src/web/assets/cp/src/js/AssetEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Craft.AssetEditor = Craft.BaseElementEditor.extend({
originalBasename: null,
originalExtension: null,
reloadIndex: false,
replaceAsset: null,

init: function(element, settings) {
this.on('updateForm', () => {
Expand All @@ -24,7 +25,11 @@ Craft.AssetEditor = Craft.BaseElementEditor.extend({
if (this.settings.elementIndex) {
this.settings.elementIndex.updateElements();
} else if (this.settings.input) {
this.settings.input.refreshThumbnail(this.$element.data('id'));
if (this.replaceAsset) {
this.settings.input.replaceElement(this.$element.data('id'), this.replaceAsset);
} else {
this.settings.input.refreshThumbnail(this.$element.data('id'));
}
}
}
});
Expand All @@ -36,7 +41,12 @@ Craft.AssetEditor = Craft.BaseElementEditor.extend({

showImageEditor: function() {
new Craft.AssetImageEditor(this.$element.data('id'), {
onSave: () => {

onSave: (data) => {
if (data.elementId) {
this.settings.elementId = this.replaceAsset = data.elementId;
}

this.reloadIndex = true;
this.load();
},
Expand Down
6 changes: 3 additions & 3 deletions src/web/assets/cp/src/js/AssetImageEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ Craft.AssetImageEditor = Garnish.Modal.extend({
return;
}

this.onSave();
this.onSave(data);
this.hide();
Craft.cp.runQueue();
});
Expand Down Expand Up @@ -1448,8 +1448,8 @@ Craft.AssetImageEditor = Garnish.Modal.extend({
/**
* onSave callback.
*/
onSave: function() {
this.settings.onSave();
onSave: function(data) {
this.settings.onSave(data);
this.trigger('save');
},

Expand Down
19 changes: 19 additions & 0 deletions src/web/assets/cp/src/js/AssetSelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ Craft.AssetSelectInput = Craft.BaseElementSelectInput.extend({
}
},

replaceElement: function(elementId, replaceWithId) {
var parameters = {
elementId: replaceWithId,
siteId: this.settings.criteria.siteId,
size: this.settings.viewMode
};

Craft.postActionRequest('elements/get-element-html', parameters, data => {
if (data.error) {
alert(data.error);
} else {
var $existing = this.$elements.filter('[data-id="' + elementId + '"]');
this.removeElement($existing);
let elementInfo = Craft.getElementInfo(data.html);
this.selectElements([elementInfo]);
}
});
},

refreshThumbnail: function(elementId) {
var parameters = {
elementId: elementId,
Expand Down

0 comments on commit 6764c9a

Please sign in to comment.