Skip to content

Commit

Permalink
🩹 fix: fix minor editing issues & improve UX
Browse files Browse the repository at this point in the history
  • Loading branch information
ShayanTheNerd committed Feb 8, 2024
1 parent e8b4bfb commit 5b65b6c
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 18 deletions.
File renamed without changes.
13 changes: 10 additions & 3 deletions src/assets/ts/imgStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface ImgStore {
activeFilter: Filter;
_hasFilter: boolean;
_isRotated: boolean;
isLandscape: boolean;
_isFlipped: boolean;
isEdited: boolean;
activeFilterIndex: string;
Expand Down Expand Up @@ -122,13 +123,19 @@ const imgStore: ImgStore = {
return filters[activeFilterIndex];
},
get _hasFilter() {
return this.state.filters.some((filter: Filter, index: number) => {
filter.value !== this._defaultState.filters[index].value;
});
const isFilterValueChanged = (filter: Filter, index: number) => {
const defaultFilterValue = this._defaultState.filters[index].value;
return filter.value !== defaultFilterValue;
};

return this.state.filters.some(isFilterValueChanged);
},
get _isRotated() {
return this.state.rotationDeg % 360 !== 0;
},
get isLandscape() {
return this.state.rotationDeg % 180 !== 0;
},
get _isFlipped() {
const { verticalFlip, horizontalFlip } = this.state;
const { verticalFlip: defaultVerticalFlip, horizontalFlip: defaultHorizontalFlip } = this._defaultState;
Expand Down
3 changes: 2 additions & 1 deletion src/assets/ts/modules/createImgCanvas.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { DOMElements } from '@ts/app.ts';
export default function createImgCanvas() {
const { selectedImg } = DOMElements;
const { naturalWidth, naturalHeight } = selectedImg;
const imgIsLandscape = imgStore.isLandscape;
const { rotationDeg, CSSFilters, verticalFlip, horizontalFlip } = imgStore.state;
const rotationRadian: number = (rotationDeg * Math.PI) / 180;
const imgIsLandscape: boolean = rotationDeg % 180 !== 0;

const canvas = document.createElement('canvas') as HTMLCanvasElement;
canvas.width = imgIsLandscape ? naturalHeight : naturalWidth;
canvas.height = imgIsLandscape ? naturalWidth : naturalHeight;

const centerX: number = canvas.width / 2;
const centerY: number = canvas.height / 2;
const destinationX: number = -(naturalWidth / 2);
Expand Down
2 changes: 1 addition & 1 deletion src/assets/ts/modules/extractImgInfo.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const lastDotIndex: Function = (string: string) => string.lastIndexOf('.');
const lastDotIndex: Function = (str: string) => str.lastIndexOf('.');

export default function extractImgInfo(imgFile: File) {
const fileName: string = imgFile.name;
Expand Down
3 changes: 1 addition & 2 deletions src/assets/ts/modules/renderImg.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ export default function renderImg(imgFile: File) {
imgStore.newNameAndExtension = extractImgInfo(imgFile);

const img = DOMElements.selectedImg.cloneNode() as HTMLImageElement;
img.classList.remove('object-cover');
img.classList.add('object-contain');
img.classList.replace('object-cover', 'object-contain');
img.removeAttribute('style'); // Remove old filters
img.title = img.alt = imgStore.title;
img.src = URL.createObjectURL(imgFile);
Expand Down
9 changes: 7 additions & 2 deletions src/assets/ts/modules/spinImg.mts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import imgStore from '@ts/imgStore.ts';
import { DOMElements } from '@ts/app.ts';

const { width, height } = DOMElements.selectedImg;

export default function spinImg(event: Event = null) {
if (event?.target !== event?.currentTarget) {
const target = event.target as HTMLButtonElement;
const spinBtn = target.closest('button') as HTMLButtonElement;
const spinType: string = spinBtn.title.toLowerCase();
const spinType: string = spinBtn.title.toLowerCase().trim();
imgStore.spin = spinType;
}

const { selectedImg } = DOMElements;
const { verticalFlip, horizontalFlip, rotationDeg } = imgStore.state;
DOMElements.selectedImg.style.transform = `scale(${verticalFlip}, ${horizontalFlip}) rotate(${rotationDeg}deg)`;
selectedImg.style.width = imgStore.isLandscape ? `${height}px` : '100%';
selectedImg.style.height = imgStore.isLandscape ? `${width}px` : '100%';
selectedImg.style.transform = `scale(${verticalFlip}, ${horizontalFlip}) rotate(${rotationDeg}deg)`;
}
16 changes: 8 additions & 8 deletions src/components/TheImagePreview.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ const placeholderImgURL = `${DEV ? baseURL.slice(1) : baseURL}/img-placeholder.w
<figure
id="img_drop_zone"
class="order-1 flex h-52 min-h-full items-center justify-center overflow-hidden rounded border-2 border-gray-400 bg-none shadow-inner shadow-gray-400 md:order-2 md:col-span-8">
<img
src={placeholderImgURL}
alt="Placeholder"
height="408"
title=""
id="selected_img"
class="h-full object-cover transition-transform duration-300"
/>
<img src={placeholderImgURL} alt="Placeholder" height="408" title="" id="selected_img" class="h-full object-cover" />
</figure>

<style>
img {
/* prettier-ignore */
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1), filter 0s;
}
</style>

<script>
import { DOMElements } from '@ts/app.ts';
import autoAnimate from '@formkit/auto-animate';
Expand Down
2 changes: 1 addition & 1 deletion tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('tailwindcss').Config} */

export default {
content: ['./src/pages/index.astro', './src/components/*.astro', './src/assets/ts/**/*.{ts, mts}'],
content: ['./src/pages/index.astro', './src/components/*.astro', './src/assets/ts/modules/*.mts'],
future: { hoverOnlyWhenSupported: true },
theme: {
extend: {
Expand Down

0 comments on commit 5b65b6c

Please sign in to comment.