diff --git a/hifi-src/package-lock.json b/hifi-src/package-lock.json index 4afad04..c15c1a4 100644 --- a/hifi-src/package-lock.json +++ b/hifi-src/package-lock.json @@ -19,6 +19,7 @@ "qrcode": "^1.5.4", "react": "^18.3.1", "react-dom": "^18.3.1", + "react-easy-crop": "^6.2.3", "react-router-dom": "^6.26.0" }, "devDependencies": { @@ -2642,6 +2643,12 @@ "node": ">=0.10.0" } }, + "node_modules/normalize-wheel": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/normalize-wheel/-/normalize-wheel-1.0.1.tgz", + "integrity": "sha512-1OnlAPZ3zgrk8B91HyRj+eVv+kS5u+Z0SCsak6Xil/kmgEia50ga7zfkumayonZrImffAxPU/5WcyGhzetHNPA==", + "license": "BSD-3-Clause" + }, "node_modules/object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", @@ -3134,6 +3141,19 @@ "react": "^18.3.1" } }, + "node_modules/react-easy-crop": { + "version": "6.2.3", + "resolved": "https://registry.npmjs.org/react-easy-crop/-/react-easy-crop-6.2.3.tgz", + "integrity": "sha512-ebimG3OGlzizjxEZ77Cj9CVLcg5vDZ6QVz8ud1rx4WmsCDWHIROEhgMi0GpJ/jKAuwHrH0M+QZUK4hyvxbYhOA==", + "license": "MIT", + "dependencies": { + "normalize-wheel": "^1.0.1" + }, + "peerDependencies": { + "react": ">=16.4.0", + "react-dom": ">=16.4.0" + } + }, "node_modules/react-refresh": { "version": "0.17.0", "resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.17.0.tgz", diff --git a/hifi-src/package.json b/hifi-src/package.json index 397c07b..0eba79b 100644 --- a/hifi-src/package.json +++ b/hifi-src/package.json @@ -21,6 +21,7 @@ "qrcode": "^1.5.4", "react": "^18.3.1", "react-dom": "^18.3.1", + "react-easy-crop": "^6.2.3", "react-router-dom": "^6.26.0" }, "devDependencies": { diff --git a/hifi-src/src/components/ImageUploadField.jsx b/hifi-src/src/components/ImageUploadField.jsx index bdff10a..caa2b83 100644 --- a/hifi-src/src/components/ImageUploadField.jsx +++ b/hifi-src/src/components/ImageUploadField.jsx @@ -1,21 +1,76 @@ import { useState } from 'react'; +import Cropper from 'react-easy-crop'; import { api } from '../api/client.js'; +import { getCroppedImageBlob } from '../utils/cropImage.js'; -export default function ImageUploadField({ value, onChange, label = 'Bild' }) { +function extensionForMime(mime) { + if (mime === 'image/jpeg') return 'jpg'; + if (mime === 'image/webp') return 'webp'; + return 'png'; +} + +export default function ImageUploadField({ value, onChange, label = 'Bild', aspect }) { const [uploading, setUploading] = useState(false); const [error, setError] = useState(''); - const handleFile = async (e) => { - const file = e.target.files?.[0]; - if (!file) return; + const [cropSrc, setCropSrc] = useState(null); + const [cropMime, setCropMime] = useState('image/png'); + const [crop, setCrop] = useState({ x: 0, y: 0 }); + const [zoom, setZoom] = useState(1); + const [croppedAreaPixels, setCroppedAreaPixels] = useState(null); + const [cropAspect, setCropAspect] = useState(aspect || 1); + const openCropFromFile = (file) => { + setCropMime(file.type || 'image/png'); + setCrop({ x: 0, y: 0 }); + setZoom(1); + setCroppedAreaPixels(null); + setCropAspect(aspect || 1); + setCropSrc(URL.createObjectURL(file)); + }; + + const handleFile = (e) => { + const file = e.target.files?.[0]; + e.target.value = ''; + if (!file) return; + setError(''); + openCropFromFile(file); + }; + + const openCropFromExisting = async () => { + if (!value) return; + setError(''); + try { + const res = await fetch(value); + const blob = await res.blob(); + openCropFromFile(blob); + } catch { + setError('Bild konnte nicht zum Zuschneiden geladen werden.'); + } + }; + + const onMediaLoaded = ({ width, height }) => { + if (!aspect && width && height) { + setCropAspect(width / height); + } + }; + + const closeCrop = () => { + if (cropSrc) URL.revokeObjectURL(cropSrc); + setCropSrc(null); + }; + + const confirmCrop = async () => { + if (!cropSrc || !croppedAreaPixels) return; setUploading(true); setError(''); try { + const blob = await getCroppedImageBlob(cropSrc, croppedAreaPixels, cropMime); const formData = new FormData(); - formData.append('file', file); + formData.append('file', blob, `zugeschnitten.${extensionForMime(cropMime)}`); const result = await api.post('/uploads', formData); onChange(result.path); + closeCrop(); } catch (err) { setError(err.message); } finally { @@ -42,9 +97,66 @@ export default function ImageUploadField({ value, onChange, label = 'Bild' }) { )} + {value && ( + + )} {uploading && Lädt hoch…} {error &&
{error}
} + + {cropSrc && ( +Bild zuschneiden
+{error}
} +this.bindToMotionValue(i,s)),this.reducedMotionConfig==="never"?this.shouldReduceMotion=!1:this.reducedMotionConfig==="always"?this.shouldReduceMotion=!0:(LN.current||A4(),this.shouldReduceMotion=Qp.current),this.shouldSkipAnimations=this.skipAnimationsConfig??!1,(r=this.parent)==null||r.addChild(this),this.update(this.props,this.presenceContext),this.hasBeenMounted=!0}unmount(){var e;this.projection&&this.projection.unmount(),sn(this.notifyUpdate),sn(this.render),this.valueSubscriptions.forEach(n=>n()),this.valueSubscriptions.clear(),this.removeFromVariantTree&&this.removeFromVariantTree(),(e=this.parent)==null||e.removeChild(this);for(const n in this.events)this.events[n].clear();for(const n in this.features){const r=this.features[n];r&&(r.unmount(),r.isMounted=!1)}this.current=null}addChild(e){this.children.add(e),this.enteringChildren??(this.enteringChildren=new Set),this.enteringChildren.add(e)}removeChild(e){this.children.delete(e),this.enteringChildren&&this.enteringChildren.delete(e)}bindToMotionValue(e,n){if(this.valueSubscriptions.has(e)&&this.valueSubscriptions.get(e)(),n.accelerate&&fN.has(e)&&this.current instanceof HTMLElement){const{factory:o,keyframes:a,times:l,ease:u,duration:d}=n.accelerate,h=new dN({element:this.current,name:e,keyframes:a,times:l,ease:u,duration:Ot(d)}),f=o(h);this.valueSubscriptions.set(e,()=>{f(),h.cancel()});return}const r=po.has(e);r&&this.onBindTransform&&this.onBindTransform();const s=n.on("change",o=>{this.latestValues[e]=o,this.props.onUpdate&&ae.preRender(this.notifyUpdate),r&&this.projection&&(this.projection.isTransformDirty=!0),this.scheduleRender()});let i;typeof window<"u"&&window.MotionCheckAppearSync&&(i=window.MotionCheckAppearSync(this,e,n)),this.valueSubscriptions.set(e,()=>{s(),i&&i()})}sortNodePosition(e){return!this.current||!this.sortInstanceNodePosition||this.type!==e.type?0:this.sortInstanceNodePosition(this.current,e.current)}updateFeatures(){let e="animation";for(e in yu){const n=yu[e];if(!n)continue;const{isEnabled:r,Feature:s}=n;if(!this.features[e]&&s&&r(this.props)&&(this.features[e]=new s(this)),this.features[e]){const i=this.features[e];i.isMounted?i.update():(i.mount(),i.isMounted=!0)}}}triggerBuild(){this.build(this.renderState,this.latestValues,this.props)}measureViewportBox(){return this.current?this.measureInstanceViewportBox(this.current,this.props):Ve()}getStaticValue(e){return this.latestValues[e]}setStaticValue(e,n){this.latestValues[e]=n}update(e,n){(e.transformTemplate||this.props.transformTemplate)&&this.scheduleRender(),this.prevProps=this.props,this.props=e,this.prevPresenceContext=this.presenceContext,this.presenceContext=n;for(let r=0;r this.bindToMotionValue(i,s)),this.reducedMotionConfig==="never"?this.shouldReduceMotion=!1:this.reducedMotionConfig==="always"?this.shouldReduceMotion=!0:(uC.current||p4(),this.shouldReduceMotion=hm.current),this.shouldSkipAnimations=this.skipAnimationsConfig??!1,(r=this.parent)==null||r.addChild(this),this.update(this.props,this.presenceContext),this.hasBeenMounted=!0}unmount(){var e;this.projection&&this.projection.unmount(),an(this.notifyUpdate),an(this.render),this.valueSubscriptions.forEach(n=>n()),this.valueSubscriptions.clear(),this.removeFromVariantTree&&this.removeFromVariantTree(),(e=this.parent)==null||e.removeChild(this);for(const n in this.events)this.events[n].clear();for(const n in this.features){const r=this.features[n];r&&(r.unmount(),r.isMounted=!1)}this.current=null}addChild(e){this.children.add(e),this.enteringChildren??(this.enteringChildren=new Set),this.enteringChildren.add(e)}removeChild(e){this.children.delete(e),this.enteringChildren&&this.enteringChildren.delete(e)}bindToMotionValue(e,n){if(this.valueSubscriptions.has(e)&&this.valueSubscriptions.get(e)(),n.accelerate&&WN.has(e)&&this.current instanceof HTMLElement){const{factory:o,keyframes:a,times:l,ease:u,duration:d}=n.accelerate,h=new $N({element:this.current,name:e,keyframes:a,times:l,ease:u,duration:zt(d)}),f=o(h);this.valueSubscriptions.set(e,()=>{f(),h.cancel()});return}const r=bo.has(e);r&&this.onBindTransform&&this.onBindTransform();const s=n.on("change",o=>{this.latestValues[e]=o,this.props.onUpdate&&ae.preRender(this.notifyUpdate),r&&this.projection&&(this.projection.isTransformDirty=!0),this.scheduleRender()});let i;typeof window<"u"&&window.MotionCheckAppearSync&&(i=window.MotionCheckAppearSync(this,e,n)),this.valueSubscriptions.set(e,()=>{s(),i&&i()})}sortNodePosition(e){return!this.current||!this.sortInstanceNodePosition||this.type!==e.type?0:this.sortInstanceNodePosition(this.current,e.current)}updateFeatures(){let e="animation";for(e in Mu){const n=Mu[e];if(!n)continue;const{isEnabled:r,Feature:s}=n;if(!this.features[e]&&s&&r(this.props)&&(this.features[e]=new s(this)),this.features[e]){const i=this.features[e];i.isMounted?i.update():(i.mount(),i.isMounted=!0)}}}triggerBuild(){this.build(this.renderState,this.latestValues,this.props)}measureViewportBox(){return this.current?this.measureInstanceViewportBox(this.current,this.props):Ve()}getStaticValue(e){return this.latestValues[e]}setStaticValue(e,n){this.latestValues[e]=n}update(e,n){(e.transformTemplate||this.props.transformTemplate)&&this.scheduleRender(),this.prevProps=this.props,this.props=e,this.prevPresenceContext=this.presenceContext,this.presenceContext=n;for(let r=0;r 1&&s==this.$to.end(--r);)++s;return s}findCloseLevel(e){e:for(let n=Math.min(this.depth,e.depth);n>=0;n--){let{match:r,type:s}=this.frontier[n],i=nJy&&(e.x=1),e.yt*1e3,en=t=>t/1e3,l0=(t,e)=>e?t*(1e3/e):0,hN=(t,e,n)=>(((1-3*n+3*e)*t+(3*n-6*e))*t+3*e)*t,HD=1e-7,WD=12;function UD(t,e,n,r,s){let i,o,a=0;do o=e+(n-e)/2,i=hN(o,r,s)-t,i>0?n=o:e=o;while(Math.abs(i)>HD&&++a=ju?1/0:e}function E3(t,e=100,n){const r=n({...t,keyframes:[0,e]}),s=Math.min(m0(r),ju);return{type:"keyframes",ease:i=>r.next(s*i).value/e,duration:en(s)}}const Ae={stiffness:100,damping:10,mass:1,velocity:0,duration:800,bounce:.3,visualDuration:.3,restSpeed:{granular:.01,default:2},restDelta:{granular:.005,default:.5},minDuration:.01,maxDuration:10,minDamping:.05,maxDamping:1};function Jp(t,e){return t*Math.sqrt(1-e*e)}const T3=12;function M3(t,e,n){let r=n;for(let s=1;sgb&&(e.x=1),e.y