Skip to content

Commit

Permalink
Feat(web): Attachment image preview object fit #DS-1005
Browse files Browse the repository at this point in the history
- Now accepts cover/contain value for data-spirit-object-fit prop to
`.FileUploaderAttachment__image` element
  • Loading branch information
pavelklibani committed Oct 23, 2023
1 parent 0cb30d5 commit c091afd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
22 changes: 20 additions & 2 deletions packages/web/src/js/FileUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ class FileUploader extends BaseComponent {
const AttachmentSvgIcon = item?.querySelector('svg');
const AttachmentPreviewImage = snippet.querySelector('.FileUploaderAttachment__image');
const isFileImg = file.type.includes('image');
const itemObjectFit = (item?.querySelector('.FileUploaderAttachment__image') as HTMLElement).dataset
?.spiritObjectFit;

if (hasImagePreview && isFileImg) {
AttachmentSvgIcon?.remove();
Expand All @@ -276,6 +278,13 @@ class FileUploader extends BaseComponent {
AttachmentPreviewImage?.remove();
}

if (itemObjectFit) {
AttachmentPreviewImage?.querySelector('img')?.setAttribute(
'style',
`--file-uploader-attachment-image-object-fit: ${itemObjectFit}`,
);
}

item!.appendChild(attachmentInputElement);
item!.setAttribute('id', id);
itemName!.appendChild(document.createTextNode(file.name));
Expand Down Expand Up @@ -376,13 +385,22 @@ class FileUploader extends BaseComponent {
this.fileQueue.set(name, newValue);

const itemImgElement = SelectorEngine.findOne(`#${name} .FileUploaderAttachment__image img`);
const itemImgWrapperElement = SelectorEngine.findOne(`#${name} .FileUploaderAttachment__image`);
const itemObjectFit = itemImgWrapperElement?.dataset?.spiritObjectFit;
let cropStyles;

if (meta && itemImgElement && FileUploader.isCoordsInMeta(meta)) {
const cropStyles = `
cropStyles = `
--file-uploader-attachment-image-top: -${meta.y}px;
--file-uploader-attachment-image-left: -${meta.x}px;
--file-uploader-attachment-image-width: ${meta.width}px;
--file-uploader-attachment-image-height: ${meta.height}px
--file-uploader-attachment-image-height: ${meta.height}px;
`;

if (itemObjectFit) {
cropStyles += `--file-uploader-attachment-image-object-fit: ${itemObjectFit};`;
}

itemImgElement?.setAttribute('style', cropStyles);
}

Expand Down
7 changes: 4 additions & 3 deletions packages/web/src/scss/components/FileUploader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,11 @@ pick up the template and apply it on any attachments the user wants to upload.

### Preview Image

You can add a preview image to the FileUploaderAttachment.
You can add a preview image and image object fit to the FileUploaderAttachment.
Object fit is `contain` by default.

```html
<span class="FileUploaderAttachment__image">
<span class="FileUploaderAttachment__image" data-spirit-object-fit="contain">
<img src="http://placekitten.com/200/300" alt="" />
</span>
```
Expand All @@ -350,7 +351,7 @@ Full example:
```html
<li class="FileUploaderAttachment">
<!-- Preview image: start -->
<span class="FileUploaderAttachment__image">
<span class="FileUploaderAttachment__image" data-spirit-object-fit="contain">
<img src="http://placekitten.com/200/300" alt="" />
</span>
<!-- Preview image: end -->
Expand Down

0 comments on commit c091afd

Please sign in to comment.