|
| 1 | +import type { EditorEngine } from '@/components/store/editor/engine'; |
| 2 | +import { toast } from '@onlook/ui/sonner'; |
| 3 | +import { useCallback, useEffect, useMemo, useState } from 'react'; |
| 4 | + |
| 5 | +export enum ImageFit { |
| 6 | + FILL = 'fill', |
| 7 | + FIT = 'fit', |
| 8 | + STRETCH = 'stretch', |
| 9 | + CENTER = 'center', |
| 10 | + TILE = 'tile', |
| 11 | + AUTO = 'auto', |
| 12 | +} |
| 13 | + |
| 14 | +export const IMAGE_FIT_OPTIONS = [ |
| 15 | + { value: ImageFit.FILL, label: 'Fill' }, |
| 16 | + { value: ImageFit.FIT, label: 'Fit' }, |
| 17 | + { value: ImageFit.STRETCH, label: 'Stretch' }, |
| 18 | + { value: ImageFit.CENTER, label: 'Center' }, |
| 19 | + { value: ImageFit.TILE, label: 'Tile' }, |
| 20 | + { value: ImageFit.AUTO, label: 'Auto' }, |
| 21 | +] as const; |
| 22 | + |
| 23 | +const FitToStyle: Record<ImageFit, Record<string, string>> = { |
| 24 | + [ImageFit.FILL]: { |
| 25 | + backgroundSize: 'cover', |
| 26 | + backgroundPosition: 'center', |
| 27 | + backgroundRepeat: 'no-repeat', |
| 28 | + }, |
| 29 | + [ImageFit.FIT]: { |
| 30 | + backgroundSize: 'contain', |
| 31 | + backgroundPosition: 'center', |
| 32 | + backgroundRepeat: 'no-repeat', |
| 33 | + }, |
| 34 | + [ImageFit.STRETCH]: { |
| 35 | + backgroundSize: '100% 100%', |
| 36 | + backgroundPosition: 'center', |
| 37 | + backgroundRepeat: 'no-repeat', |
| 38 | + }, |
| 39 | + [ImageFit.CENTER]: { |
| 40 | + backgroundSize: 'auto', |
| 41 | + backgroundPosition: 'center', |
| 42 | + backgroundRepeat: 'no-repeat', |
| 43 | + }, |
| 44 | + [ImageFit.TILE]: { |
| 45 | + backgroundSize: 'auto', |
| 46 | + backgroundPosition: 'center', |
| 47 | + backgroundRepeat: 'repeat', |
| 48 | + }, |
| 49 | + [ImageFit.AUTO]: { |
| 50 | + backgroundSize: 'auto', |
| 51 | + backgroundPosition: 'center', |
| 52 | + backgroundRepeat: 'no-repeat', |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +const cssToImageFit = (backgroundSize: string, backgroundRepeat: string): ImageFit => { |
| 57 | + if (backgroundSize === 'cover') return ImageFit.FILL; |
| 58 | + if (backgroundSize === 'contain') return ImageFit.FIT; |
| 59 | + if (backgroundSize === '100% 100%') return ImageFit.STRETCH; |
| 60 | + if (backgroundSize === 'auto' && backgroundRepeat === 'repeat') return ImageFit.TILE; |
| 61 | + if (backgroundSize === 'auto') return ImageFit.CENTER; |
| 62 | + return ImageFit.AUTO; |
| 63 | +}; |
| 64 | + |
| 65 | +export const useBackgroundImage = (editorEngine: EditorEngine) => { |
| 66 | + const [fillOption, setFillOption] = useState<ImageFit>(ImageFit.FILL); |
| 67 | + |
| 68 | + const currentBackgroundImage = useMemo(() => { |
| 69 | + const selectedImage = editorEngine.style.selectedStyle?.styles.computed.backgroundImage; |
| 70 | + if (selectedImage && selectedImage !== 'none') { |
| 71 | + return selectedImage; |
| 72 | + } |
| 73 | + return null; |
| 74 | + }, [editorEngine.style.selectedStyle?.styles.computed.backgroundImage]); |
| 75 | + |
| 76 | + const currentBackgroundSize = useMemo(() => { |
| 77 | + const selectedStyle = editorEngine.style.selectedStyle?.styles.computed.backgroundSize; |
| 78 | + const selectedRepeat = editorEngine.style.selectedStyle?.styles.computed.backgroundRepeat; |
| 79 | + |
| 80 | + if (!selectedStyle) return null; |
| 81 | + |
| 82 | + return cssToImageFit(selectedStyle, selectedRepeat ?? 'no-repeat'); |
| 83 | + }, [ |
| 84 | + editorEngine.style.selectedStyle?.styles.computed.backgroundSize, |
| 85 | + editorEngine.style.selectedStyle?.styles.computed.backgroundRepeat, |
| 86 | + ]); |
| 87 | + |
| 88 | + const applyFillOption = useCallback( |
| 89 | + (fillOptionValue: ImageFit) => { |
| 90 | + try { |
| 91 | + const selected = editorEngine.elements.selected; |
| 92 | + |
| 93 | + if (!selected || selected.length === 0) { |
| 94 | + console.warn('No elements selected to apply fill option'); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const cssStyles = FitToStyle[fillOptionValue]; |
| 99 | + editorEngine.style.updateMultiple(cssStyles); |
| 100 | + } catch (error) { |
| 101 | + console.error('Failed to apply fill option:', error); |
| 102 | + toast.error('Failed to apply fill option', { |
| 103 | + description: error instanceof Error ? error.message : String(error), |
| 104 | + }); |
| 105 | + } |
| 106 | + }, |
| 107 | + [], |
| 108 | + ); |
| 109 | + |
| 110 | + const handleFillOptionChange = useCallback( |
| 111 | + (option: ImageFit) => { |
| 112 | + setFillOption(option); |
| 113 | + applyFillOption(option); |
| 114 | + }, |
| 115 | + [applyFillOption], |
| 116 | + ); |
| 117 | + |
| 118 | + const removeBackground = useCallback(async () => { |
| 119 | + try { |
| 120 | + const styles = { |
| 121 | + backgroundImage: 'none', |
| 122 | + backgroundSize: 'auto', |
| 123 | + backgroundRepeat: 'repeat', |
| 124 | + backgroundPosition: 'auto', |
| 125 | + }; |
| 126 | + |
| 127 | + editorEngine.style.updateMultiple(styles); |
| 128 | + editorEngine.image.setSelectedImage(null); |
| 129 | + editorEngine.image.setPreviewImage(null); |
| 130 | + } catch (error) { |
| 131 | + console.error('Failed to remove background:', error); |
| 132 | + toast.error('Failed to remove background', { |
| 133 | + description: error instanceof Error ? error.message : String(error), |
| 134 | + }); |
| 135 | + } |
| 136 | + }, [editorEngine]); |
| 137 | + |
| 138 | + useEffect(() => { |
| 139 | + if (currentBackgroundSize) { |
| 140 | + setFillOption(currentBackgroundSize); |
| 141 | + } |
| 142 | + }, [currentBackgroundSize]); |
| 143 | + |
| 144 | + useEffect(() => { |
| 145 | + return () => { |
| 146 | + if (editorEngine.image.isSelectingImage) { |
| 147 | + editorEngine.image.setIsSelectingImage(false); |
| 148 | + editorEngine.image.setSelectedImage(null); |
| 149 | + editorEngine.image.setPreviewImage(null); |
| 150 | + } |
| 151 | + }; |
| 152 | + }, [editorEngine]); |
| 153 | + |
| 154 | + return { |
| 155 | + fillOption, |
| 156 | + currentBackgroundImage, |
| 157 | + handleFillOptionChange, |
| 158 | + removeBackground, |
| 159 | + ImageFit, |
| 160 | + IMAGE_FIT_OPTIONS, |
| 161 | + }; |
| 162 | +}; |
0 commit comments