Flatten transparent product images to white and allow cropper unzoom.
PNG/WebP uploads no longer bake to black on JPEG; product croppers can zoom out with white padding, and SEO tools move into the product overflow menu. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
b743c09e4d
commit
06e2442aaf
@@ -1,5 +1,6 @@
|
|||||||
.modal {
|
.modal {
|
||||||
width: min(560px, calc(100vw - 32px));
|
width: min(820px, calc(100vw - 32px));
|
||||||
|
max-width: 820px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.headerBlock {
|
.headerBlock {
|
||||||
@@ -16,9 +17,6 @@
|
|||||||
|
|
||||||
.prompt {
|
.prompt {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
min-height: 120px;
|
|
||||||
resize: vertical;
|
|
||||||
line-height: 1.55;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.promptHint {
|
.promptHint {
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ export function AddProductByAiModal({
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className={modalStyles.field}>
|
<div className={`${modalStyles.field} ${aiStyles.aiPromptField}`}>
|
||||||
<label htmlFor="ai-product-prompt">{t('products.ai.prompt')}</label>
|
<label htmlFor="ai-product-prompt">{t('products.ai.prompt')}</label>
|
||||||
<textarea
|
<textarea
|
||||||
id="ai-product-prompt"
|
id="ai-product-prompt"
|
||||||
@@ -180,11 +180,12 @@ export function AddProductByAiModal({
|
|||||||
onChange={(e) => setPrompt(e.target.value)}
|
onChange={(e) => setPrompt(e.target.value)}
|
||||||
placeholder={t('products.ai.promptPlaceholder')}
|
placeholder={t('products.ai.promptPlaceholder')}
|
||||||
dir={promptDir}
|
dir={promptDir}
|
||||||
className={`${styles.prompt}${language === 'fa' ? ' faText' : ''}`}
|
className={`${aiStyles.aiPromptTextarea} ${styles.prompt}${language === 'fa' ? ' faText' : ''}`}
|
||||||
disabled={isSubmitting}
|
disabled={isSubmitting}
|
||||||
rows={5}
|
rows={14}
|
||||||
required
|
required
|
||||||
minLength={2}
|
minLength={2}
|
||||||
|
lang={language === 'fa' ? 'fa' : 'en'}
|
||||||
/>
|
/>
|
||||||
<p className={styles.promptHint}>{t('products.ai.promptHint')}</p>
|
<p className={styles.promptHint}>{t('products.ai.promptHint')}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -11,6 +11,11 @@ interface ImageCropperProps {
|
|||||||
/** Crop aspect ratio. Defaults to square (1). */
|
/** Crop aspect ratio. Defaults to square (1). */
|
||||||
aspect?: number
|
aspect?: number
|
||||||
outputFormat?: 'jpeg' | 'png'
|
outputFormat?: 'jpeg' | 'png'
|
||||||
|
/** Fill color for JPEG and for crop areas outside the image. */
|
||||||
|
fillColor?: string
|
||||||
|
/** Minimum zoom. Values below 1 shrink the image inside the crop frame. */
|
||||||
|
minZoom?: number
|
||||||
|
maxZoom?: number
|
||||||
accept?: string
|
accept?: string
|
||||||
uploadLabel?: string
|
uploadLabel?: string
|
||||||
hint?: string
|
hint?: string
|
||||||
@@ -24,6 +29,9 @@ export function ImageCropper({
|
|||||||
onChange,
|
onChange,
|
||||||
aspect = 1,
|
aspect = 1,
|
||||||
outputFormat = 'jpeg',
|
outputFormat = 'jpeg',
|
||||||
|
fillColor,
|
||||||
|
minZoom = 1,
|
||||||
|
maxZoom = 3,
|
||||||
accept = 'image/*',
|
accept = 'image/*',
|
||||||
uploadLabel,
|
uploadLabel,
|
||||||
hint,
|
hint,
|
||||||
@@ -34,6 +42,8 @@ export function ImageCropper({
|
|||||||
const resolvedUploadLabel = uploadLabel ?? t('products.form.thumbnailUpload')
|
const resolvedUploadLabel = uploadLabel ?? t('products.form.thumbnailUpload')
|
||||||
const resolvedHint = hint ?? t('products.form.thumbnailHint')
|
const resolvedHint = hint ?? t('products.form.thumbnailHint')
|
||||||
const resolvedChangeLabel = changeLabel ?? t('products.form.thumbnailChange')
|
const resolvedChangeLabel = changeLabel ?? t('products.form.thumbnailChange')
|
||||||
|
const resolvedFillColor = fillColor ?? (outputFormat === 'jpeg' ? '#ffffff' : undefined)
|
||||||
|
const cropSurfaceColor = resolvedFillColor ?? '#1e293b'
|
||||||
const [imageSrc, setImageSrc] = useState<string | null>(null)
|
const [imageSrc, setImageSrc] = useState<string | null>(null)
|
||||||
const [crop, setCrop] = useState({ x: 0, y: 0 })
|
const [crop, setCrop] = useState({ x: 0, y: 0 })
|
||||||
const [zoom, setZoom] = useState(1)
|
const [zoom, setZoom] = useState(1)
|
||||||
@@ -65,7 +75,12 @@ export function ImageCropper({
|
|||||||
|
|
||||||
async function applyCrop() {
|
async function applyCrop() {
|
||||||
if (!imageSrc || !croppedArea) return
|
if (!imageSrc || !croppedArea) return
|
||||||
const cropped = await getCroppedImage(imageSrc, croppedArea, outputFormat)
|
const cropped = await getCroppedImage(
|
||||||
|
imageSrc,
|
||||||
|
croppedArea,
|
||||||
|
outputFormat,
|
||||||
|
resolvedFillColor,
|
||||||
|
)
|
||||||
onChange(cropped)
|
onChange(cropped)
|
||||||
setImageSrc(null)
|
setImageSrc(null)
|
||||||
setZoom(1)
|
setZoom(1)
|
||||||
@@ -128,16 +143,25 @@ export function ImageCropper({
|
|||||||
|
|
||||||
{imageSrc && (
|
{imageSrc && (
|
||||||
<div className={styles.cropPanel}>
|
<div className={styles.cropPanel}>
|
||||||
<div className={styles.cropArea} style={frameStyle}>
|
<div
|
||||||
|
className={styles.cropArea}
|
||||||
|
style={{ ...frameStyle, background: cropSurfaceColor }}
|
||||||
|
>
|
||||||
<Cropper
|
<Cropper
|
||||||
key={String(aspect)}
|
key={String(aspect)}
|
||||||
image={imageSrc}
|
image={imageSrc}
|
||||||
crop={crop}
|
crop={crop}
|
||||||
zoom={zoom}
|
zoom={zoom}
|
||||||
|
minZoom={minZoom}
|
||||||
|
maxZoom={maxZoom}
|
||||||
aspect={aspect}
|
aspect={aspect}
|
||||||
onCropChange={setCrop}
|
onCropChange={setCrop}
|
||||||
onZoomChange={setZoom}
|
onZoomChange={setZoom}
|
||||||
onCropComplete={onCropComplete}
|
onCropComplete={onCropComplete}
|
||||||
|
restrictPosition={minZoom >= 1}
|
||||||
|
style={{
|
||||||
|
containerStyle: { background: cropSurfaceColor },
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.cropControls}>
|
<div className={styles.cropControls}>
|
||||||
@@ -145,8 +169,8 @@ export function ImageCropper({
|
|||||||
{t('products.form.zoom')}
|
{t('products.form.zoom')}
|
||||||
<input
|
<input
|
||||||
type="range"
|
type="range"
|
||||||
min={1}
|
min={minZoom}
|
||||||
max={3}
|
max={maxZoom}
|
||||||
step={0.05}
|
step={0.05}
|
||||||
value={zoom}
|
value={zoom}
|
||||||
onChange={(e) => setZoom(Number(e.target.value))}
|
onChange={(e) => setZoom(Number(e.target.value))}
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ import {
|
|||||||
ClipboardList,
|
ClipboardList,
|
||||||
ImageOff,
|
ImageOff,
|
||||||
ExternalLink,
|
ExternalLink,
|
||||||
|
Info,
|
||||||
|
MessageSquare,
|
||||||
|
Search,
|
||||||
type LucideIcon,
|
type LucideIcon,
|
||||||
} from 'lucide-react'
|
} from 'lucide-react'
|
||||||
import { useLocale } from '@meshkee/dashboard-ui'
|
import { useLocale } from '@meshkee/dashboard-ui'
|
||||||
@@ -22,14 +25,15 @@ import { OpenOnWebsiteControl } from './OpenOnWebsiteControl'
|
|||||||
import { ProductCardOverflowMenu } from './ProductCardOverflowMenu'
|
import { ProductCardOverflowMenu } from './ProductCardOverflowMenu'
|
||||||
import styles from './ProductCard.module.css'
|
import styles from './ProductCard.module.css'
|
||||||
|
|
||||||
/** Max icon slots in the card footer, including the overflow trigger when needed. */
|
|
||||||
const MAX_VISIBLE_ACTIONS = 6
|
|
||||||
|
|
||||||
interface ProductCardProps {
|
interface ProductCardProps {
|
||||||
product: Product
|
product: Product
|
||||||
|
commentCount: number
|
||||||
variantCount: number
|
variantCount: number
|
||||||
onEdit: (id: string) => void
|
onEdit: (id: string) => void
|
||||||
onTranslate: (id: string) => void
|
onTranslate: (id: string) => void
|
||||||
|
onQuickInfo: (id: string) => void
|
||||||
|
onComments: (id: string) => void
|
||||||
|
onSeo: (id: string) => void
|
||||||
onTechnicalInfo: (id: string) => void
|
onTechnicalInfo: (id: string) => void
|
||||||
onRemove: (id: string) => void
|
onRemove: (id: string) => void
|
||||||
onVariations: (id: string) => void
|
onVariations: (id: string) => void
|
||||||
@@ -47,9 +51,13 @@ type CardAction = {
|
|||||||
|
|
||||||
export function ProductCard({
|
export function ProductCard({
|
||||||
product,
|
product,
|
||||||
|
commentCount,
|
||||||
variantCount,
|
variantCount,
|
||||||
onEdit,
|
onEdit,
|
||||||
onTranslate,
|
onTranslate,
|
||||||
|
onQuickInfo,
|
||||||
|
onComments,
|
||||||
|
onSeo,
|
||||||
onTechnicalInfo,
|
onTechnicalInfo,
|
||||||
onRemove,
|
onRemove,
|
||||||
onVariations,
|
onVariations,
|
||||||
@@ -76,7 +84,7 @@ export function ProductCard({
|
|||||||
setImageFailed(false)
|
setImageFailed(false)
|
||||||
}, [imageSrc])
|
}, [imageSrc])
|
||||||
|
|
||||||
const actions: CardAction[] = [
|
const primaryActions: CardAction[] = [
|
||||||
{
|
{
|
||||||
key: 'edit',
|
key: 'edit',
|
||||||
label: t('products.card.edit'),
|
label: t('products.card.edit'),
|
||||||
@@ -116,6 +124,30 @@ export function ProductCard({
|
|||||||
icon: ClipboardList,
|
icon: ClipboardList,
|
||||||
onClick: () => onTechnicalInfo(product.id),
|
onClick: () => onTechnicalInfo(product.id),
|
||||||
},
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
const menuActions = [
|
||||||
|
{
|
||||||
|
key: 'seo',
|
||||||
|
label: t('products.card.seo'),
|
||||||
|
icon: Search,
|
||||||
|
onClick: () => onSeo(product.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'quickInfo',
|
||||||
|
label: t('products.card.quickInfo'),
|
||||||
|
icon: Info,
|
||||||
|
onClick: () => onQuickInfo(product.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'comments',
|
||||||
|
label:
|
||||||
|
commentCount > 0
|
||||||
|
? `${t('products.card.comments')} (${commentCount})`
|
||||||
|
: t('products.card.comments'),
|
||||||
|
icon: MessageSquare,
|
||||||
|
onClick: () => onComments(product.id),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
key: 'remove',
|
key: 'remove',
|
||||||
label: t('products.card.remove'),
|
label: t('products.card.remove'),
|
||||||
@@ -125,20 +157,6 @@ export function ProductCard({
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
const needsOverflow = actions.length > MAX_VISIBLE_ACTIONS
|
|
||||||
const visibleActions = needsOverflow
|
|
||||||
? actions.slice(0, MAX_VISIBLE_ACTIONS - 1)
|
|
||||||
: actions
|
|
||||||
const overflowActions = needsOverflow
|
|
||||||
? actions.slice(MAX_VISIBLE_ACTIONS - 1).map(({ key, label, icon, onClick, danger }) => ({
|
|
||||||
key,
|
|
||||||
label,
|
|
||||||
icon,
|
|
||||||
onClick,
|
|
||||||
danger,
|
|
||||||
}))
|
|
||||||
: []
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<article className={styles.card}>
|
<article className={styles.card}>
|
||||||
<Link to={`/products/detail/${product.id}`} className={styles.clickable}>
|
<Link to={`/products/detail/${product.id}`} className={styles.clickable}>
|
||||||
@@ -180,7 +198,7 @@ export function ProductCard({
|
|||||||
</Link>
|
</Link>
|
||||||
|
|
||||||
<div className={styles.controls}>
|
<div className={styles.controls}>
|
||||||
{visibleActions.map((action) => {
|
{primaryActions.map((action) => {
|
||||||
if (action.render) return <span key={action.key}>{action.render()}</span>
|
if (action.render) return <span key={action.key}>{action.render()}</span>
|
||||||
|
|
||||||
const Icon = action.icon
|
const Icon = action.icon
|
||||||
@@ -188,7 +206,7 @@ export function ProductCard({
|
|||||||
<Tooltip key={action.key} label={action.label}>
|
<Tooltip key={action.key} label={action.label}>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className={`${styles.iconBtn}${action.danger ? ` ${styles.danger}` : ''}`}
|
className={styles.iconBtn}
|
||||||
onClick={action.onClick}
|
onClick={action.onClick}
|
||||||
aria-label={
|
aria-label={
|
||||||
action.badge != null ? `${action.label} (${action.badge})` : action.label
|
action.badge != null ? `${action.label} (${action.badge})` : action.label
|
||||||
@@ -202,12 +220,7 @@ export function ProductCard({
|
|||||||
</Tooltip>
|
</Tooltip>
|
||||||
)
|
)
|
||||||
})}
|
})}
|
||||||
{overflowActions.length > 0 ? (
|
<ProductCardOverflowMenu productName={primaryName} items={menuActions} />
|
||||||
<ProductCardOverflowMenu
|
|
||||||
productName={primaryName}
|
|
||||||
items={overflowActions}
|
|
||||||
/>
|
|
||||||
) : null}
|
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
.modal {
|
||||||
|
width: min(560px, calc(100vw - 32px));
|
||||||
|
}
|
||||||
|
|
||||||
|
.titleRow {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
import { createPortal } from 'react-dom'
|
||||||
|
import { Search, X } from 'lucide-react'
|
||||||
|
import { ApiError } from '../lib/api'
|
||||||
|
import { useT } from '../i18n/useT'
|
||||||
|
import { getProduct, updateProduct } from '../services/productService'
|
||||||
|
import { seoMetaPayload } from '../utils/seoMeta'
|
||||||
|
import { SeoMetaFields } from './SeoMetaFields'
|
||||||
|
import styles from './VariationsModal.module.css'
|
||||||
|
import seoStyles from './ProductSeoModal.module.css'
|
||||||
|
|
||||||
|
interface ProductSeoModalProps {
|
||||||
|
open: boolean
|
||||||
|
productId: string
|
||||||
|
productName: string
|
||||||
|
onClose: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const ANIMATION_MS = 220
|
||||||
|
|
||||||
|
export function ProductSeoModal({
|
||||||
|
open,
|
||||||
|
productId,
|
||||||
|
productName,
|
||||||
|
onClose,
|
||||||
|
}: ProductSeoModalProps) {
|
||||||
|
const t = useT()
|
||||||
|
const [mounted, setMounted] = useState(open)
|
||||||
|
const [closing, setClosing] = useState(false)
|
||||||
|
const [seoMetaTitle, setSeoMetaTitle] = useState('')
|
||||||
|
const [seoMetaDescription, setSeoMetaDescription] = useState('')
|
||||||
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
|
const [isSaving, setIsSaving] = useState(false)
|
||||||
|
const [error, setError] = useState('')
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (open) {
|
||||||
|
setMounted(true)
|
||||||
|
setClosing(false)
|
||||||
|
} else if (mounted) {
|
||||||
|
setClosing(true)
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
setMounted(false)
|
||||||
|
setClosing(false)
|
||||||
|
}, ANIMATION_MS)
|
||||||
|
return () => clearTimeout(timer)
|
||||||
|
}
|
||||||
|
}, [open, mounted])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open || !productId) return
|
||||||
|
|
||||||
|
const controller = new AbortController()
|
||||||
|
void loadData(controller.signal)
|
||||||
|
return () => controller.abort()
|
||||||
|
}, [open, productId])
|
||||||
|
|
||||||
|
async function loadData(signal?: AbortSignal) {
|
||||||
|
setIsLoading(true)
|
||||||
|
setError('')
|
||||||
|
try {
|
||||||
|
const product = await getProduct(productId, signal)
|
||||||
|
setSeoMetaTitle(product.seoMetaTitle ?? '')
|
||||||
|
setSeoMetaDescription(product.seoMetaDescription ?? '')
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof DOMException && err.name === 'AbortError') return
|
||||||
|
if (err instanceof ApiError) {
|
||||||
|
setError(err.message)
|
||||||
|
} else {
|
||||||
|
setError(t('products.seo.errorLoad'))
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleSave() {
|
||||||
|
setIsSaving(true)
|
||||||
|
setError('')
|
||||||
|
try {
|
||||||
|
await updateProduct(productId, seoMetaPayload(seoMetaTitle, seoMetaDescription))
|
||||||
|
onClose()
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof ApiError) {
|
||||||
|
setError(err.message)
|
||||||
|
} else {
|
||||||
|
setError(t('products.seo.errorSave'))
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
setIsSaving(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mounted) return null
|
||||||
|
|
||||||
|
return createPortal(
|
||||||
|
<div
|
||||||
|
className={`${styles.overlay} ${closing ? styles.overlayOut : styles.overlayIn}`}
|
||||||
|
onClick={() => !isSaving && onClose()}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={`${styles.modal} ${seoStyles.modal} ${closing ? styles.modalOut : styles.modalIn}`}
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
aria-labelledby="product-seo-title"
|
||||||
|
>
|
||||||
|
<div className={styles.header}>
|
||||||
|
<div>
|
||||||
|
<h3 id="product-seo-title" className={`${styles.title} ${seoStyles.titleRow}`}>
|
||||||
|
<Search size={18} aria-hidden="true" />
|
||||||
|
<span>{t('products.seo.title')}</span>
|
||||||
|
</h3>
|
||||||
|
{productName ? <p className={styles.subtitle}>{productName}</p> : null}
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={styles.closeBtn}
|
||||||
|
onClick={onClose}
|
||||||
|
disabled={isSaving}
|
||||||
|
aria-label={t('common.close')}
|
||||||
|
>
|
||||||
|
<X size={20} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={styles.body}>
|
||||||
|
{isLoading ? (
|
||||||
|
<p className={styles.emptyText}>{t('products.seo.loading')}</p>
|
||||||
|
) : (
|
||||||
|
<SeoMetaFields
|
||||||
|
seoMetaTitle={seoMetaTitle}
|
||||||
|
seoMetaDescription={seoMetaDescription}
|
||||||
|
onTitleChange={setSeoMetaTitle}
|
||||||
|
onDescriptionChange={setSeoMetaDescription}
|
||||||
|
disabled={isSaving}
|
||||||
|
hint={t('seo.meta.hint')}
|
||||||
|
titleLabel={t('seo.meta.title')}
|
||||||
|
descriptionLabel={t('seo.meta.description')}
|
||||||
|
titlePlaceholder={t('seo.meta.titlePlaceholder')}
|
||||||
|
descriptionPlaceholder={t('seo.meta.descriptionPlaceholder')}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{error ? <p className={styles.errorText}>{error}</p> : null}
|
||||||
|
|
||||||
|
<div className={styles.actions}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={styles.cancelBtn}
|
||||||
|
onClick={onClose}
|
||||||
|
disabled={isSaving}
|
||||||
|
>
|
||||||
|
{t('common.cancel')}
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={styles.submitBtn}
|
||||||
|
onClick={() => void handleSave()}
|
||||||
|
disabled={isLoading || isSaving}
|
||||||
|
>
|
||||||
|
{isSaving ? t('products.seo.saving') : t('products.seo.save')}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>,
|
||||||
|
document.body,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -96,8 +96,8 @@
|
|||||||
.dropdownInFlow {
|
.dropdownInFlow {
|
||||||
position: static;
|
position: static;
|
||||||
margin-top: 6px;
|
margin-top: 6px;
|
||||||
max-height: none;
|
max-height: min(280px, 40vh);
|
||||||
overflow: visible;
|
overflow-y: auto;
|
||||||
z-index: auto;
|
z-index: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -553,12 +553,19 @@ const en = {
|
|||||||
'products.card.edit': 'Edit product',
|
'products.card.edit': 'Edit product',
|
||||||
'products.card.translate': 'Translate',
|
'products.card.translate': 'Translate',
|
||||||
'products.card.moreActions': 'More actions',
|
'products.card.moreActions': 'More actions',
|
||||||
|
'products.card.seo': 'SEO tools',
|
||||||
'products.card.quickInfo': 'Quick info',
|
'products.card.quickInfo': 'Quick info',
|
||||||
'products.card.variations': 'Manage variations',
|
'products.card.variations': 'Manage variations',
|
||||||
'products.card.comments': 'View comments',
|
'products.card.comments': 'View comments',
|
||||||
'products.card.technical': 'Technical data',
|
'products.card.technical': 'Technical data',
|
||||||
'products.card.remove': 'Remove product',
|
'products.card.remove': 'Remove product',
|
||||||
'products.card.draft': 'Draft',
|
'products.card.draft': 'Draft',
|
||||||
|
'products.seo.title': 'SEO tools',
|
||||||
|
'products.seo.loading': 'Loading SEO settings…',
|
||||||
|
'products.seo.save': 'Save',
|
||||||
|
'products.seo.saving': 'Saving…',
|
||||||
|
'products.seo.errorLoad': 'Unable to load SEO settings.',
|
||||||
|
'products.seo.errorSave': 'Unable to save SEO settings.',
|
||||||
|
|
||||||
'products.details.loading': 'Loading product...',
|
'products.details.loading': 'Loading product...',
|
||||||
'products.details.notFound': 'Product not found.',
|
'products.details.notFound': 'Product not found.',
|
||||||
@@ -3119,12 +3126,19 @@ const fa: Record<MessageKey, string> = {
|
|||||||
'products.card.edit': 'ویرایش محصول',
|
'products.card.edit': 'ویرایش محصول',
|
||||||
'products.card.translate': 'ترجمه',
|
'products.card.translate': 'ترجمه',
|
||||||
'products.card.moreActions': 'عملیات بیشتر',
|
'products.card.moreActions': 'عملیات بیشتر',
|
||||||
|
'products.card.seo': 'ابزارهای SEO',
|
||||||
'products.card.quickInfo': 'اطلاعات سریع',
|
'products.card.quickInfo': 'اطلاعات سریع',
|
||||||
'products.card.variations': 'مدیریت تنوعها',
|
'products.card.variations': 'مدیریت تنوعها',
|
||||||
'products.card.comments': 'مشاهده نظرات',
|
'products.card.comments': 'مشاهده نظرات',
|
||||||
'products.card.technical': 'اطلاعات فنی',
|
'products.card.technical': 'اطلاعات فنی',
|
||||||
'products.card.remove': 'حذف محصول',
|
'products.card.remove': 'حذف محصول',
|
||||||
'products.card.draft': 'پیشنویس',
|
'products.card.draft': 'پیشنویس',
|
||||||
|
'products.seo.title': 'ابزارهای SEO',
|
||||||
|
'products.seo.loading': 'در حال بارگذاری تنظیمات SEO…',
|
||||||
|
'products.seo.save': 'ذخیره',
|
||||||
|
'products.seo.saving': 'در حال ذخیره…',
|
||||||
|
'products.seo.errorLoad': 'بارگذاری تنظیمات SEO ممکن نشد.',
|
||||||
|
'products.seo.errorSave': 'ذخیره تنظیمات SEO ممکن نشد.',
|
||||||
|
|
||||||
'products.details.loading': 'در حال بارگذاری محصول...',
|
'products.details.loading': 'در حال بارگذاری محصول...',
|
||||||
'products.details.notFound': 'محصول یافت نشد.',
|
'products.details.notFound': 'محصول یافت نشد.',
|
||||||
|
|||||||
@@ -751,6 +751,7 @@ export function AddCustomerProductPage() {
|
|||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
aspect={1}
|
aspect={1}
|
||||||
|
minZoom={0.4}
|
||||||
hint={t('customerProducts.form.images.thumbnailHint')}
|
hint={t('customerProducts.form.images.thumbnailHint')}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import { ImageCropper } from '../components/ImageCropper'
|
|||||||
import { ImageUploader } from '../components/ImageUploader'
|
import { ImageUploader } from '../components/ImageUploader'
|
||||||
import { TagInput } from '../components/TagInput'
|
import { TagInput } from '../components/TagInput'
|
||||||
import { RichTextEditor } from '../components/RichTextEditor'
|
import { RichTextEditor } from '../components/RichTextEditor'
|
||||||
import { SeoMetaFields } from '../components/SeoMetaFields'
|
|
||||||
import { Breadcrumbs } from '../components/Breadcrumbs'
|
import { Breadcrumbs } from '../components/Breadcrumbs'
|
||||||
import { ProductFieldAiPromptModal } from '../components/ProductFieldAiPromptModal'
|
import { ProductFieldAiPromptModal } from '../components/ProductFieldAiPromptModal'
|
||||||
import { Tooltip } from '../components/Tooltip'
|
import { Tooltip } from '../components/Tooltip'
|
||||||
@@ -30,7 +29,6 @@ import {
|
|||||||
} from '../services/productAiService'
|
} from '../services/productAiService'
|
||||||
import type { Category, FlatCategory } from '../types/category'
|
import type { Category, FlatCategory } from '../types/category'
|
||||||
import { flattenCategories } from '../utils/categories'
|
import { flattenCategories } from '../utils/categories'
|
||||||
import { seoMetaPayload } from '../utils/seoMeta'
|
|
||||||
import pageStyles from '../components/PageContent.module.css'
|
import pageStyles from '../components/PageContent.module.css'
|
||||||
import aiStyles from '../styles/ai.module.css'
|
import aiStyles from '../styles/ai.module.css'
|
||||||
import styles from './AddNewProductPage.module.css'
|
import styles from './AddNewProductPage.module.css'
|
||||||
@@ -54,8 +52,6 @@ export function AddNewProductPage() {
|
|||||||
const [thumbnail, setThumbnail] = useState<string | null>(null)
|
const [thumbnail, setThumbnail] = useState<string | null>(null)
|
||||||
const [images, setImages] = useState<string[]>([])
|
const [images, setImages] = useState<string[]>([])
|
||||||
const [tags, setTags] = useState<string[]>([])
|
const [tags, setTags] = useState<string[]>([])
|
||||||
const [seoMetaTitle, setSeoMetaTitle] = useState('')
|
|
||||||
const [seoMetaDescription, setSeoMetaDescription] = useState('')
|
|
||||||
const [thumbnailMediaId, setThumbnailMediaId] = useState<string | null>(null)
|
const [thumbnailMediaId, setThumbnailMediaId] = useState<string | null>(null)
|
||||||
const [galleryMediaIds, setGalleryMediaIds] = useState<string[]>([])
|
const [galleryMediaIds, setGalleryMediaIds] = useState<string[]>([])
|
||||||
const [loaded, setLoaded] = useState(!isEdit)
|
const [loaded, setLoaded] = useState(!isEdit)
|
||||||
@@ -175,8 +171,6 @@ export function AddNewProductPage() {
|
|||||||
setThumbnail(form.thumbnail)
|
setThumbnail(form.thumbnail)
|
||||||
setImages(form.images)
|
setImages(form.images)
|
||||||
setTags(form.tags)
|
setTags(form.tags)
|
||||||
setSeoMetaTitle(form.seoMetaTitle)
|
|
||||||
setSeoMetaDescription(form.seoMetaDescription)
|
|
||||||
setThumbnailMediaId(form.thumbnailMediaId)
|
setThumbnailMediaId(form.thumbnailMediaId)
|
||||||
setGalleryMediaIds(form.galleryMediaIds)
|
setGalleryMediaIds(form.galleryMediaIds)
|
||||||
setLoaded(true)
|
setLoaded(true)
|
||||||
@@ -225,7 +219,6 @@ export function AddNewProductPage() {
|
|||||||
featuredMediaId: featuredMediaId ?? undefined,
|
featuredMediaId: featuredMediaId ?? undefined,
|
||||||
galleryMediaIds: nextGalleryMediaIds,
|
galleryMediaIds: nextGalleryMediaIds,
|
||||||
tags,
|
tags,
|
||||||
...seoMetaPayload(seoMetaTitle, seoMetaDescription),
|
|
||||||
status: 'published' as const,
|
status: 'published' as const,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,7 +286,7 @@ export function AddNewProductPage() {
|
|||||||
<div className={styles.formGrid}>
|
<div className={styles.formGrid}>
|
||||||
<div className={`${styles.field} ${styles.col2} ${styles.thumbnailField}`}>
|
<div className={`${styles.field} ${styles.col2} ${styles.thumbnailField}`}>
|
||||||
<label>{t('products.form.thumbnail')}</label>
|
<label>{t('products.form.thumbnail')}</label>
|
||||||
<ImageCropper value={thumbnail} onChange={setThumbnail} />
|
<ImageCropper value={thumbnail} onChange={setThumbnail} minZoom={0.4} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className={styles.col10}>
|
<div className={styles.col10}>
|
||||||
@@ -445,19 +438,6 @@ export function AddNewProductPage() {
|
|||||||
placeholder={t('products.form.tagsPlaceholder')}
|
placeholder={t('products.form.tagsPlaceholder')}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<SeoMetaFields
|
|
||||||
seoMetaTitle={seoMetaTitle}
|
|
||||||
seoMetaDescription={seoMetaDescription}
|
|
||||||
onTitleChange={setSeoMetaTitle}
|
|
||||||
onDescriptionChange={setSeoMetaDescription}
|
|
||||||
disabled={isSubmitting}
|
|
||||||
hint={t('seo.meta.hint')}
|
|
||||||
titleLabel={t('seo.meta.title')}
|
|
||||||
descriptionLabel={t('seo.meta.description')}
|
|
||||||
titlePlaceholder={t('seo.meta.titlePlaceholder')}
|
|
||||||
descriptionPlaceholder={t('seo.meta.descriptionPlaceholder')}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className={styles.actions}>
|
<div className={styles.actions}>
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import { Pagination } from '../components/Pagination'
|
|||||||
import { ConfirmDeleteModal } from '../components/ConfirmDeleteModal'
|
import { ConfirmDeleteModal } from '../components/ConfirmDeleteModal'
|
||||||
import { ProductVariantsModal } from '../components/ProductVariantsModal'
|
import { ProductVariantsModal } from '../components/ProductVariantsModal'
|
||||||
import { ProductTechnicalInfoModal } from '../components/ProductTechnicalInfoModal'
|
import { ProductTechnicalInfoModal } from '../components/ProductTechnicalInfoModal'
|
||||||
|
import { ProductCommentsModal } from '../components/ProductCommentsModal'
|
||||||
|
import { ProductSeoModal } from '../components/ProductSeoModal'
|
||||||
import { TranslationModal } from '../components/TranslationModal'
|
import { TranslationModal } from '../components/TranslationModal'
|
||||||
import { TranslateAllLanguageModal } from '../components/TranslateAllLanguageModal'
|
import { TranslateAllLanguageModal } from '../components/TranslateAllLanguageModal'
|
||||||
import { Breadcrumbs } from '../components/Breadcrumbs'
|
import { Breadcrumbs } from '../components/Breadcrumbs'
|
||||||
@@ -52,12 +54,15 @@ export function MyProductsPage() {
|
|||||||
const [isDeleting, setIsDeleting] = useState(false)
|
const [isDeleting, setIsDeleting] = useState(false)
|
||||||
const [error, setError] = useState('')
|
const [error, setError] = useState('')
|
||||||
const [variantCounts, setVariantCounts] = useState<Record<string, number>>({})
|
const [variantCounts, setVariantCounts] = useState<Record<string, number>>({})
|
||||||
|
const [commentCounts, setCommentCounts] = useState<Record<string, number>>({})
|
||||||
const [deleteTarget, setDeleteTarget] = useState<{ id: string; name: string } | null>(null)
|
const [deleteTarget, setDeleteTarget] = useState<{ id: string; name: string } | null>(null)
|
||||||
const [variantsTarget, setVariantsTarget] = useState<{
|
const [variantsTarget, setVariantsTarget] = useState<{
|
||||||
id: string
|
id: string
|
||||||
name: string
|
name: string
|
||||||
categoryId: string
|
categoryId: string
|
||||||
} | null>(null)
|
} | null>(null)
|
||||||
|
const [commentsTarget, setCommentsTarget] = useState<{ id: string; name: string } | null>(null)
|
||||||
|
const [seoTarget, setSeoTarget] = useState<{ id: string; name: string } | null>(null)
|
||||||
const [technicalTarget, setTechnicalTarget] = useState<{
|
const [technicalTarget, setTechnicalTarget] = useState<{
|
||||||
id: string
|
id: string
|
||||||
name: string
|
name: string
|
||||||
@@ -205,6 +210,24 @@ export function MyProductsPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleComments(id: string) {
|
||||||
|
const product = products.find((p) => p.id === id)
|
||||||
|
if (product) {
|
||||||
|
setCommentsTarget({ id, name: product.nameFa || product.nameEn })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleSeo(id: string) {
|
||||||
|
const product = products.find((p) => p.id === id)
|
||||||
|
if (product) {
|
||||||
|
setSeoTarget({ id, name: product.nameFa || product.nameEn })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleQuickInfo(id: string) {
|
||||||
|
navigate(`/products/detail/${id}`)
|
||||||
|
}
|
||||||
|
|
||||||
function handleVariations(id: string) {
|
function handleVariations(id: string) {
|
||||||
const product = products.find((p) => p.id === id)
|
const product = products.find((p) => p.id === id)
|
||||||
if (product) {
|
if (product) {
|
||||||
@@ -273,6 +296,14 @@ export function MyProductsPage() {
|
|||||||
writeListQuery('', 1)
|
writeListQuery('', 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleCommentCountChange(count: number) {
|
||||||
|
if (!commentsTarget) return
|
||||||
|
setCommentCounts((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[commentsTarget.id]: count,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className={pageStyles.content}>
|
<main className={pageStyles.content}>
|
||||||
<Breadcrumbs
|
<Breadcrumbs
|
||||||
@@ -356,9 +387,13 @@ export function MyProductsPage() {
|
|||||||
<ProductCard
|
<ProductCard
|
||||||
key={product.id}
|
key={product.id}
|
||||||
product={product}
|
product={product}
|
||||||
|
commentCount={commentCounts[product.id] ?? product.commentCount}
|
||||||
variantCount={variantCounts[product.id] ?? product.variantCount ?? 0}
|
variantCount={variantCounts[product.id] ?? product.variantCount ?? 0}
|
||||||
onEdit={handleEdit}
|
onEdit={handleEdit}
|
||||||
onTranslate={handleTranslate}
|
onTranslate={handleTranslate}
|
||||||
|
onQuickInfo={handleQuickInfo}
|
||||||
|
onComments={handleComments}
|
||||||
|
onSeo={handleSeo}
|
||||||
onTechnicalInfo={handleTechnicalInfo}
|
onTechnicalInfo={handleTechnicalInfo}
|
||||||
onVariations={handleVariations}
|
onVariations={handleVariations}
|
||||||
onRemove={handleRemoveRequest}
|
onRemove={handleRemoveRequest}
|
||||||
@@ -414,6 +449,25 @@ export function MyProductsPage() {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{commentsTarget && (
|
||||||
|
<ProductCommentsModal
|
||||||
|
open={!!commentsTarget}
|
||||||
|
productId={commentsTarget.id}
|
||||||
|
productName={commentsTarget.name}
|
||||||
|
onClose={() => setCommentsTarget(null)}
|
||||||
|
onCountChange={handleCommentCountChange}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{seoTarget && (
|
||||||
|
<ProductSeoModal
|
||||||
|
open={!!seoTarget}
|
||||||
|
productId={seoTarget.id}
|
||||||
|
productName={seoTarget.name}
|
||||||
|
onClose={() => setSeoTarget(null)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
{technicalTarget && (
|
{technicalTarget && (
|
||||||
<ProductTechnicalInfoModal
|
<ProductTechnicalInfoModal
|
||||||
open={!!technicalTarget}
|
open={!!technicalTarget}
|
||||||
|
|||||||
@@ -9,34 +9,63 @@ function createImage(url: string): Promise<HTMLImageElement> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Crop `imageSrc` to `pixelCrop`. Areas outside the source image use `fillColor`
|
||||||
|
* when provided. JPEG always defaults to white so transparent PNG/WebP uploads
|
||||||
|
* do not become black.
|
||||||
|
*/
|
||||||
export async function getCroppedImage(
|
export async function getCroppedImage(
|
||||||
imageSrc: string,
|
imageSrc: string,
|
||||||
pixelCrop: Area,
|
pixelCrop: Area,
|
||||||
format: 'jpeg' | 'png' = 'jpeg',
|
format: 'jpeg' | 'png' = 'jpeg',
|
||||||
|
fillColor?: string,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const image = await createImage(imageSrc)
|
const image = await createImage(imageSrc)
|
||||||
const canvas = document.createElement('canvas')
|
const canvas = document.createElement('canvas')
|
||||||
const ctx = canvas.getContext('2d')
|
const ctx = canvas.getContext('2d')
|
||||||
if (!ctx) throw new Error('Could not get canvas context')
|
if (!ctx) throw new Error('Could not get canvas context')
|
||||||
|
|
||||||
canvas.width = pixelCrop.width
|
const outW = Math.max(1, Math.round(pixelCrop.width))
|
||||||
canvas.height = pixelCrop.height
|
const outH = Math.max(1, Math.round(pixelCrop.height))
|
||||||
|
canvas.width = outW
|
||||||
|
canvas.height = outH
|
||||||
|
|
||||||
if (format === 'png') {
|
const background = format === 'jpeg' ? (fillColor ?? '#ffffff') : fillColor
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
if (background) {
|
||||||
|
ctx.fillStyle = background
|
||||||
|
ctx.fillRect(0, 0, outW, outH)
|
||||||
|
} else {
|
||||||
|
ctx.clearRect(0, 0, outW, outH)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.drawImage(
|
const srcX = pixelCrop.x
|
||||||
image,
|
const srcY = pixelCrop.y
|
||||||
pixelCrop.x,
|
const srcW = pixelCrop.width
|
||||||
pixelCrop.y,
|
const srcH = pixelCrop.height
|
||||||
pixelCrop.width,
|
|
||||||
pixelCrop.height,
|
const intersectLeft = Math.max(0, srcX)
|
||||||
0,
|
const intersectTop = Math.max(0, srcY)
|
||||||
0,
|
const intersectRight = Math.min(image.naturalWidth, srcX + srcW)
|
||||||
pixelCrop.width,
|
const intersectBottom = Math.min(image.naturalHeight, srcY + srcH)
|
||||||
pixelCrop.height,
|
|
||||||
)
|
const intersectW = intersectRight - intersectLeft
|
||||||
|
const intersectH = intersectBottom - intersectTop
|
||||||
|
|
||||||
|
if (intersectW > 0 && intersectH > 0) {
|
||||||
|
const destX = intersectLeft - srcX
|
||||||
|
const destY = intersectTop - srcY
|
||||||
|
ctx.drawImage(
|
||||||
|
image,
|
||||||
|
intersectLeft,
|
||||||
|
intersectTop,
|
||||||
|
intersectW,
|
||||||
|
intersectH,
|
||||||
|
destX,
|
||||||
|
destY,
|
||||||
|
intersectW,
|
||||||
|
intersectH,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (format === 'png') {
|
if (format === 'png') {
|
||||||
return canvas.toDataURL('image/png')
|
return canvas.toDataURL('image/png')
|
||||||
|
|||||||
@@ -37,15 +37,25 @@ export function dataUrlToFile(dataUrl: string, filename: string): File {
|
|||||||
return new File([copy], filename, { type: mime })
|
return new File([copy], filename, { type: mime })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare a data URL (or image URL) for upload. When the target is JPEG,
|
||||||
|
* transparent PNG/WebP pixels are flattened onto white — never black.
|
||||||
|
*/
|
||||||
export async function ensureUploadFile(dataUrl: string, filename: string): Promise<File> {
|
export async function ensureUploadFile(dataUrl: string, filename: string): Promise<File> {
|
||||||
|
const wantsPng = filename.toLowerCase().endsWith('.png')
|
||||||
|
|
||||||
if (dataUrl.startsWith('data:')) {
|
if (dataUrl.startsWith('data:')) {
|
||||||
const mime = dataUrl.slice(5, dataUrl.indexOf(';'))
|
const mime = dataUrl.slice(5, dataUrl.indexOf(';'))
|
||||||
const normalized = mime === 'image/jpg' ? 'image/jpeg' : mime
|
const normalized = mime === 'image/jpg' ? 'image/jpeg' : mime
|
||||||
|
|
||||||
if (ALLOWED_IMAGE_TYPES.has(normalized)) {
|
if (ALLOWED_IMAGE_TYPES.has(normalized)) {
|
||||||
const file = dataUrlToFile(dataUrl, filename)
|
const alreadyPng = wantsPng && normalized === 'image/png'
|
||||||
if (file.size > 0) {
|
const alreadyJpeg = !wantsPng && normalized === 'image/jpeg'
|
||||||
return file
|
if (alreadyPng || alreadyJpeg) {
|
||||||
|
const file = dataUrlToFile(dataUrl, filename)
|
||||||
|
if (file.size > 0) {
|
||||||
|
return file
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,12 +67,14 @@ export async function ensureUploadFile(dataUrl: string, filename: string): Promi
|
|||||||
throw new Error('Could not prepare image for upload')
|
throw new Error('Could not prepare image for upload')
|
||||||
}
|
}
|
||||||
|
|
||||||
const wantsPng = filename.toLowerCase().endsWith('.png')
|
|
||||||
canvas.width = image.naturalWidth
|
canvas.width = image.naturalWidth
|
||||||
canvas.height = image.naturalHeight
|
canvas.height = image.naturalHeight
|
||||||
|
|
||||||
if (wantsPng) {
|
if (wantsPng) {
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
||||||
|
} else {
|
||||||
|
ctx.fillStyle = '#ffffff'
|
||||||
|
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.drawImage(image, 0, 0)
|
ctx.drawImage(image, 0, 0)
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ interface ImageCropperProps {
|
|||||||
onChange: (value: string | null) => void
|
onChange: (value: string | null) => void
|
||||||
aspect?: number
|
aspect?: number
|
||||||
outputFormat?: 'jpeg' | 'png'
|
outputFormat?: 'jpeg' | 'png'
|
||||||
|
/** Fill color for JPEG and for crop areas outside the image. */
|
||||||
|
fillColor?: string
|
||||||
|
/** Minimum zoom. Values below 1 shrink the image inside the crop frame. */
|
||||||
|
minZoom?: number
|
||||||
|
maxZoom?: number
|
||||||
accept?: string
|
accept?: string
|
||||||
uploadLabel?: string
|
uploadLabel?: string
|
||||||
hint?: string
|
hint?: string
|
||||||
@@ -21,6 +26,9 @@ export function ImageCropper({
|
|||||||
onChange,
|
onChange,
|
||||||
aspect = 1,
|
aspect = 1,
|
||||||
outputFormat = 'jpeg',
|
outputFormat = 'jpeg',
|
||||||
|
fillColor,
|
||||||
|
minZoom = 1,
|
||||||
|
maxZoom = 3,
|
||||||
accept = 'image/*',
|
accept = 'image/*',
|
||||||
uploadLabel,
|
uploadLabel,
|
||||||
hint,
|
hint,
|
||||||
@@ -30,6 +38,8 @@ export function ImageCropper({
|
|||||||
const resolvedUploadLabel = uploadLabel ?? t('myProducts.images.thumbnailUpload')
|
const resolvedUploadLabel = uploadLabel ?? t('myProducts.images.thumbnailUpload')
|
||||||
const resolvedHint = hint ?? t('myProducts.images.thumbnailHint')
|
const resolvedHint = hint ?? t('myProducts.images.thumbnailHint')
|
||||||
const resolvedChangeLabel = changeLabel ?? t('myProducts.images.thumbnailChange')
|
const resolvedChangeLabel = changeLabel ?? t('myProducts.images.thumbnailChange')
|
||||||
|
const resolvedFillColor = fillColor ?? (outputFormat === 'jpeg' ? '#ffffff' : undefined)
|
||||||
|
const cropSurfaceColor = resolvedFillColor ?? '#1e293b'
|
||||||
const [imageSrc, setImageSrc] = useState<string | null>(null)
|
const [imageSrc, setImageSrc] = useState<string | null>(null)
|
||||||
const [crop, setCrop] = useState({ x: 0, y: 0 })
|
const [crop, setCrop] = useState({ x: 0, y: 0 })
|
||||||
const [zoom, setZoom] = useState(1)
|
const [zoom, setZoom] = useState(1)
|
||||||
@@ -61,7 +71,12 @@ export function ImageCropper({
|
|||||||
|
|
||||||
async function applyCrop() {
|
async function applyCrop() {
|
||||||
if (!imageSrc || !croppedArea) return
|
if (!imageSrc || !croppedArea) return
|
||||||
const cropped = await getCroppedImage(imageSrc, croppedArea, outputFormat)
|
const cropped = await getCroppedImage(
|
||||||
|
imageSrc,
|
||||||
|
croppedArea,
|
||||||
|
outputFormat,
|
||||||
|
resolvedFillColor,
|
||||||
|
)
|
||||||
onChange(cropped)
|
onChange(cropped)
|
||||||
setImageSrc(null)
|
setImageSrc(null)
|
||||||
setZoom(1)
|
setZoom(1)
|
||||||
@@ -122,17 +137,23 @@ export function ImageCropper({
|
|||||||
<div className={styles.cropPanel}>
|
<div className={styles.cropPanel}>
|
||||||
<div
|
<div
|
||||||
className={`${styles.cropArea} ${isPortrait ? styles.cropAreaPortrait : ''}`}
|
className={`${styles.cropArea} ${isPortrait ? styles.cropAreaPortrait : ''}`}
|
||||||
style={frameStyle}
|
style={{ ...frameStyle, background: cropSurfaceColor }}
|
||||||
>
|
>
|
||||||
<Cropper
|
<Cropper
|
||||||
key={String(aspect)}
|
key={String(aspect)}
|
||||||
image={imageSrc}
|
image={imageSrc}
|
||||||
crop={crop}
|
crop={crop}
|
||||||
zoom={zoom}
|
zoom={zoom}
|
||||||
|
minZoom={minZoom}
|
||||||
|
maxZoom={maxZoom}
|
||||||
aspect={aspect}
|
aspect={aspect}
|
||||||
onCropChange={setCrop}
|
onCropChange={setCrop}
|
||||||
onZoomChange={setZoom}
|
onZoomChange={setZoom}
|
||||||
onCropComplete={onCropComplete}
|
onCropComplete={onCropComplete}
|
||||||
|
restrictPosition={minZoom >= 1}
|
||||||
|
style={{
|
||||||
|
containerStyle: { background: cropSurfaceColor },
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.cropControls}>
|
<div className={styles.cropControls}>
|
||||||
@@ -140,8 +161,8 @@ export function ImageCropper({
|
|||||||
{t('myProducts.images.zoom')}
|
{t('myProducts.images.zoom')}
|
||||||
<input
|
<input
|
||||||
type="range"
|
type="range"
|
||||||
min={1}
|
min={minZoom}
|
||||||
max={3}
|
max={maxZoom}
|
||||||
step={0.05}
|
step={0.05}
|
||||||
value={zoom}
|
value={zoom}
|
||||||
onChange={(e) => setZoom(Number(e.target.value))}
|
onChange={(e) => setZoom(Number(e.target.value))}
|
||||||
|
|||||||
@@ -713,6 +713,7 @@ export function AddMyProductPage() {
|
|||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
aspect={1}
|
aspect={1}
|
||||||
|
minZoom={0.4}
|
||||||
hint={t('myProducts.images.thumbnailHint')}
|
hint={t('myProducts.images.thumbnailHint')}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -9,34 +9,63 @@ function createImage(url: string): Promise<HTMLImageElement> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Crop `imageSrc` to `pixelCrop`. Areas outside the source image use `fillColor`
|
||||||
|
* when provided. JPEG always defaults to white so transparent PNG/WebP uploads
|
||||||
|
* do not become black.
|
||||||
|
*/
|
||||||
export async function getCroppedImage(
|
export async function getCroppedImage(
|
||||||
imageSrc: string,
|
imageSrc: string,
|
||||||
pixelCrop: Area,
|
pixelCrop: Area,
|
||||||
format: 'jpeg' | 'png' = 'jpeg',
|
format: 'jpeg' | 'png' = 'jpeg',
|
||||||
|
fillColor?: string,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const image = await createImage(imageSrc)
|
const image = await createImage(imageSrc)
|
||||||
const canvas = document.createElement('canvas')
|
const canvas = document.createElement('canvas')
|
||||||
const ctx = canvas.getContext('2d')
|
const ctx = canvas.getContext('2d')
|
||||||
if (!ctx) throw new Error('Could not get canvas context')
|
if (!ctx) throw new Error('Could not get canvas context')
|
||||||
|
|
||||||
canvas.width = pixelCrop.width
|
const outW = Math.max(1, Math.round(pixelCrop.width))
|
||||||
canvas.height = pixelCrop.height
|
const outH = Math.max(1, Math.round(pixelCrop.height))
|
||||||
|
canvas.width = outW
|
||||||
|
canvas.height = outH
|
||||||
|
|
||||||
if (format === 'png') {
|
const background = format === 'jpeg' ? (fillColor ?? '#ffffff') : fillColor
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
if (background) {
|
||||||
|
ctx.fillStyle = background
|
||||||
|
ctx.fillRect(0, 0, outW, outH)
|
||||||
|
} else {
|
||||||
|
ctx.clearRect(0, 0, outW, outH)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.drawImage(
|
const srcX = pixelCrop.x
|
||||||
image,
|
const srcY = pixelCrop.y
|
||||||
pixelCrop.x,
|
const srcW = pixelCrop.width
|
||||||
pixelCrop.y,
|
const srcH = pixelCrop.height
|
||||||
pixelCrop.width,
|
|
||||||
pixelCrop.height,
|
const intersectLeft = Math.max(0, srcX)
|
||||||
0,
|
const intersectTop = Math.max(0, srcY)
|
||||||
0,
|
const intersectRight = Math.min(image.naturalWidth, srcX + srcW)
|
||||||
pixelCrop.width,
|
const intersectBottom = Math.min(image.naturalHeight, srcY + srcH)
|
||||||
pixelCrop.height,
|
|
||||||
)
|
const intersectW = intersectRight - intersectLeft
|
||||||
|
const intersectH = intersectBottom - intersectTop
|
||||||
|
|
||||||
|
if (intersectW > 0 && intersectH > 0) {
|
||||||
|
const destX = intersectLeft - srcX
|
||||||
|
const destY = intersectTop - srcY
|
||||||
|
ctx.drawImage(
|
||||||
|
image,
|
||||||
|
intersectLeft,
|
||||||
|
intersectTop,
|
||||||
|
intersectW,
|
||||||
|
intersectH,
|
||||||
|
destX,
|
||||||
|
destY,
|
||||||
|
intersectW,
|
||||||
|
intersectH,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (format === 'png') {
|
if (format === 'png') {
|
||||||
return canvas.toDataURL('image/png')
|
return canvas.toDataURL('image/png')
|
||||||
|
|||||||
@@ -37,15 +37,25 @@ export function dataUrlToFile(dataUrl: string, filename: string): File {
|
|||||||
return new File([copy], filename, { type: mime })
|
return new File([copy], filename, { type: mime })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare a data URL (or image URL) for upload. When the target is JPEG,
|
||||||
|
* transparent PNG/WebP pixels are flattened onto white — never black.
|
||||||
|
*/
|
||||||
export async function ensureUploadFile(dataUrl: string, filename: string): Promise<File> {
|
export async function ensureUploadFile(dataUrl: string, filename: string): Promise<File> {
|
||||||
|
const wantsPng = filename.toLowerCase().endsWith('.png')
|
||||||
|
|
||||||
if (dataUrl.startsWith('data:')) {
|
if (dataUrl.startsWith('data:')) {
|
||||||
const mime = dataUrl.slice(5, dataUrl.indexOf(';'))
|
const mime = dataUrl.slice(5, dataUrl.indexOf(';'))
|
||||||
const normalized = mime === 'image/jpg' ? 'image/jpeg' : mime
|
const normalized = mime === 'image/jpg' ? 'image/jpeg' : mime
|
||||||
|
|
||||||
if (ALLOWED_IMAGE_TYPES.has(normalized)) {
|
if (ALLOWED_IMAGE_TYPES.has(normalized)) {
|
||||||
const file = dataUrlToFile(dataUrl, filename)
|
const alreadyPng = wantsPng && normalized === 'image/png'
|
||||||
if (file.size > 0) {
|
const alreadyJpeg = !wantsPng && normalized === 'image/jpeg'
|
||||||
return file
|
if (alreadyPng || alreadyJpeg) {
|
||||||
|
const file = dataUrlToFile(dataUrl, filename)
|
||||||
|
if (file.size > 0) {
|
||||||
|
return file
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -57,12 +67,14 @@ export async function ensureUploadFile(dataUrl: string, filename: string): Promi
|
|||||||
throw new Error('Could not prepare image for upload')
|
throw new Error('Could not prepare image for upload')
|
||||||
}
|
}
|
||||||
|
|
||||||
const wantsPng = filename.toLowerCase().endsWith('.png')
|
|
||||||
canvas.width = image.naturalWidth
|
canvas.width = image.naturalWidth
|
||||||
canvas.height = image.naturalHeight
|
canvas.height = image.naturalHeight
|
||||||
|
|
||||||
if (wantsPng) {
|
if (wantsPng) {
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
||||||
|
} else {
|
||||||
|
ctx.fillStyle = '#ffffff'
|
||||||
|
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.drawImage(image, 0, 0)
|
ctx.drawImage(image, 0, 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user