@jhits/plugin-images 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/package.json +3 -2
  2. package/src/api/fallback/route.ts +69 -0
  3. package/src/api/index.ts +10 -0
  4. package/src/api/list/index.ts +96 -0
  5. package/src/api/resolve/route.ts +241 -0
  6. package/src/api/router.ts +85 -0
  7. package/src/api/upload/index.ts +88 -0
  8. package/src/api/uploads/[filename]/route.ts +93 -0
  9. package/src/api-server.ts +11 -0
  10. package/src/assets/noimagefound.jpg +0 -0
  11. package/src/components/BackgroundImage.d.ts +11 -0
  12. package/src/components/BackgroundImage.d.ts.map +1 -0
  13. package/src/components/BackgroundImage.tsx +92 -0
  14. package/src/components/GlobalImageEditor/config.d.ts +9 -0
  15. package/src/components/GlobalImageEditor/config.d.ts.map +1 -0
  16. package/src/components/GlobalImageEditor/config.ts +21 -0
  17. package/src/components/GlobalImageEditor/eventHandlers.d.ts +20 -0
  18. package/src/components/GlobalImageEditor/eventHandlers.d.ts.map +1 -0
  19. package/src/components/GlobalImageEditor/eventHandlers.ts +267 -0
  20. package/src/components/GlobalImageEditor/imageDetection.d.ts +16 -0
  21. package/src/components/GlobalImageEditor/imageDetection.d.ts.map +1 -0
  22. package/src/components/GlobalImageEditor/imageDetection.ts +160 -0
  23. package/src/components/GlobalImageEditor/imageSetup.d.ts +9 -0
  24. package/src/components/GlobalImageEditor/imageSetup.d.ts.map +1 -0
  25. package/src/components/GlobalImageEditor/imageSetup.ts +306 -0
  26. package/src/components/GlobalImageEditor/saveLogic.d.ts +26 -0
  27. package/src/components/GlobalImageEditor/saveLogic.d.ts.map +1 -0
  28. package/src/components/GlobalImageEditor/saveLogic.ts +133 -0
  29. package/src/components/GlobalImageEditor/stylingDetection.d.ts +9 -0
  30. package/src/components/GlobalImageEditor/stylingDetection.d.ts.map +1 -0
  31. package/src/components/GlobalImageEditor/stylingDetection.ts +122 -0
  32. package/src/components/GlobalImageEditor/transformParsing.d.ts +16 -0
  33. package/src/components/GlobalImageEditor/transformParsing.d.ts.map +1 -0
  34. package/src/components/GlobalImageEditor/transformParsing.ts +83 -0
  35. package/src/components/GlobalImageEditor/types.d.ts +36 -0
  36. package/src/components/GlobalImageEditor/types.d.ts.map +1 -0
  37. package/src/components/GlobalImageEditor/types.ts +39 -0
  38. package/src/components/GlobalImageEditor.d.ts +8 -0
  39. package/src/components/GlobalImageEditor.d.ts.map +1 -0
  40. package/src/components/GlobalImageEditor.tsx +327 -0
  41. package/src/components/Image.d.ts +22 -0
  42. package/src/components/Image.d.ts.map +1 -0
  43. package/src/components/Image.tsx +343 -0
  44. package/src/components/ImageBrowserModal.d.ts +13 -0
  45. package/src/components/ImageBrowserModal.d.ts.map +1 -0
  46. package/src/components/ImageBrowserModal.tsx +837 -0
  47. package/src/components/ImageEditor.d.ts +27 -0
  48. package/src/components/ImageEditor.d.ts.map +1 -0
  49. package/src/components/ImageEditor.tsx +323 -0
  50. package/src/components/ImageEffectsPanel.tsx +116 -0
  51. package/src/components/ImagePicker.d.ts +3 -0
  52. package/src/components/ImagePicker.d.ts.map +1 -0
  53. package/src/components/ImagePicker.tsx +265 -0
  54. package/src/components/ImagesPluginInit.d.ts +24 -0
  55. package/src/components/ImagesPluginInit.d.ts.map +1 -0
  56. package/src/components/ImagesPluginInit.tsx +31 -0
  57. package/src/components/index.ts +10 -0
  58. package/src/config.ts +179 -0
  59. package/src/hooks/useImagePicker.d.ts +20 -0
  60. package/src/hooks/useImagePicker.d.ts.map +1 -0
  61. package/src/hooks/useImagePicker.ts +344 -0
  62. package/src/index.server.ts +12 -0
  63. package/src/index.tsx +56 -0
  64. package/src/init.tsx +58 -0
  65. package/src/types/index.d.ts +80 -0
  66. package/src/types/index.d.ts.map +1 -0
  67. package/src/types/index.ts +84 -0
  68. package/src/utils/fallback.d.ts +27 -0
  69. package/src/utils/fallback.d.ts.map +1 -0
  70. package/src/utils/fallback.ts +73 -0
  71. package/src/utils/transforms.d.ts +26 -0
  72. package/src/utils/transforms.d.ts.map +1 -0
  73. package/src/utils/transforms.ts +54 -0
  74. package/src/views/ImageManager.d.ts +10 -0
  75. package/src/views/ImageManager.d.ts.map +1 -0
  76. package/src/views/ImageManager.tsx +30 -0
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Utilities for detecting styling from DOM elements
3
+ */
4
+
5
+ import type { DetectedStyling } from './types';
6
+
7
+ /**
8
+ * Detect styling properties from an element and its parent containers
9
+ */
10
+ export function detectStyling(
11
+ element: HTMLElement,
12
+ imgElement: HTMLImageElement | null
13
+ ): DetectedStyling {
14
+ // Find the container element (could be the element itself or a parent)
15
+ let container = element;
16
+ let parent = element.parentElement;
17
+
18
+ // Look for styling in parent containers (up to 3 levels)
19
+ for (let i = 0; i < 3 && parent; i++) {
20
+ const classes = parent.className || '';
21
+ const computedStyle = window.getComputedStyle(parent);
22
+
23
+ // Check for rounded-full (circular)
24
+ if (classes.includes('rounded-full')) {
25
+ container = parent;
26
+ break;
27
+ }
28
+ parent = parent.parentElement;
29
+ }
30
+
31
+ const containerClasses = container.className || '';
32
+ const containerStyle = window.getComputedStyle(container);
33
+ const imgStyle = imgElement ? window.getComputedStyle(imgElement) : null;
34
+
35
+ // Detect border radius
36
+ let borderRadius = 'rounded-xl'; // default
37
+ if (containerClasses.includes('rounded-full')) {
38
+ borderRadius = 'rounded-full';
39
+ } else if (containerClasses.includes('rounded-3xl')) {
40
+ borderRadius = 'rounded-3xl';
41
+ } else if (containerClasses.includes('rounded-2xl')) {
42
+ borderRadius = 'rounded-2xl';
43
+ } else if (containerClasses.includes('rounded-xl')) {
44
+ borderRadius = 'rounded-xl';
45
+ } else if (containerClasses.includes('rounded-lg')) {
46
+ borderRadius = 'rounded-lg';
47
+ }
48
+
49
+ // Detect aspect ratio
50
+ let aspectRatio = '16/9'; // default
51
+
52
+ // First check the container's computed style
53
+ const aspectRatioStyle = containerStyle.aspectRatio;
54
+ if (aspectRatioStyle && aspectRatioStyle !== 'auto') {
55
+ // Convert CSS aspect-ratio (e.g., "1 / 1") to our format ("1/1")
56
+ aspectRatio = aspectRatioStyle.replace(/\s+/g, '');
57
+ } else {
58
+ // Check for Tailwind aspect ratio classes in container or parents
59
+ let currentElement: HTMLElement | null = container;
60
+ let foundAspectRatio = false;
61
+
62
+ // Check up to 3 parent levels for aspect ratio
63
+ for (let i = 0; i < 3 && currentElement && !foundAspectRatio; i++) {
64
+ const classes = currentElement.className || '';
65
+ const computedStyle = window.getComputedStyle(currentElement);
66
+
67
+ // Check computed style first
68
+ if (computedStyle.aspectRatio && computedStyle.aspectRatio !== 'auto') {
69
+ aspectRatio = computedStyle.aspectRatio.replace(/\s+/g, '');
70
+ foundAspectRatio = true;
71
+ break;
72
+ }
73
+
74
+ // Check for Tailwind aspect ratio classes (aspect-*, aspect-square, aspect-video, etc.)
75
+ const aspectMatch = classes.match(/aspect-(\d+\/\d+)|aspect-square|aspect-video/);
76
+ if (aspectMatch) {
77
+ if (aspectMatch[1]) {
78
+ // Matched aspect-4/5, aspect-16/9, etc.
79
+ aspectRatio = aspectMatch[1];
80
+ } else if (aspectMatch[0] === 'aspect-square') {
81
+ aspectRatio = '1/1';
82
+ } else if (aspectMatch[0] === 'aspect-video') {
83
+ aspectRatio = '16/9';
84
+ }
85
+ foundAspectRatio = true;
86
+ break;
87
+ }
88
+
89
+ currentElement = currentElement.parentElement;
90
+ }
91
+
92
+ // Fallback checks
93
+ if (!foundAspectRatio) {
94
+ if (containerClasses.includes('rounded-full')) {
95
+ // Circular images are typically 1:1
96
+ aspectRatio = '1/1';
97
+ } else {
98
+ // Try to detect from size classes
99
+ if (containerClasses.includes('size-') || containerClasses.match(/w-\d+.*h-\d+/)) {
100
+ aspectRatio = '1/1';
101
+ }
102
+ }
103
+ }
104
+ }
105
+
106
+ // Detect object-fit
107
+ let objectFit: 'cover' | 'contain' | 'fill' | 'none' | 'scale-down' = 'cover';
108
+ if (imgStyle) {
109
+ const objectFitValue = imgStyle.objectFit || containerStyle.objectFit;
110
+ if (objectFitValue && ['cover', 'contain', 'fill', 'none', 'scale-down'].includes(objectFitValue)) {
111
+ objectFit = objectFitValue as 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
112
+ }
113
+ }
114
+
115
+ // Detect object-position
116
+ let objectPosition = 'center';
117
+ if (imgStyle) {
118
+ objectPosition = imgStyle.objectPosition || containerStyle.objectPosition || 'center';
119
+ }
120
+
121
+ return { aspectRatio, borderRadius, objectFit, objectPosition };
122
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Utilities for parsing CSS transforms to extract scale and position values
3
+ */
4
+ /**
5
+ * Parse scale from CSS transform string
6
+ */
7
+ export declare function parseScaleFromTransform(transform: string, container: HTMLElement | null, imgElement: HTMLImageElement | null): number;
8
+ /**
9
+ * Parse position (X, Y) from CSS transform string
10
+ * Returns container-relative percentages
11
+ */
12
+ export declare function parsePositionFromTransform(transform: string): {
13
+ x: number;
14
+ y: number;
15
+ };
16
+ //# sourceMappingURL=transformParsing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transformParsing.d.ts","sourceRoot":"","sources":["transformParsing.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,wBAAgB,uBAAuB,CACnC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,WAAW,GAAG,IAAI,EAC7B,UAAU,EAAE,gBAAgB,GAAG,IAAI,GACpC,MAAM,CA+BR;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,SAAS,EAAE,MAAM,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAkCtF"}
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Utilities for parsing CSS transforms to extract scale and position values
3
+ */
4
+
5
+ /**
6
+ * Parse scale from CSS transform string
7
+ */
8
+ export function parseScaleFromTransform(
9
+ transform: string,
10
+ container: HTMLElement | null,
11
+ imgElement: HTMLImageElement | null
12
+ ): number {
13
+ const scaleMatch = transform.match(/scale\(([\d.]+)\)/);
14
+ if (!scaleMatch) return 1.0;
15
+
16
+ const totalScale = parseFloat(scaleMatch[1]);
17
+
18
+ // If totalScale is very small (< 0.1), it's likely wrong
19
+ if (totalScale < 0.1) {
20
+ return 1.0;
21
+ }
22
+
23
+ // Calculate baseScale to extract the actual user scale
24
+ if (container && imgElement && imgElement.naturalWidth > 0 && imgElement.naturalHeight > 0) {
25
+ const containerWidth = container.clientWidth || container.offsetWidth;
26
+ const containerHeight = container.clientHeight || container.offsetHeight;
27
+
28
+ if (containerWidth > 0 && containerHeight > 0) {
29
+ const widthRatio = containerWidth / imgElement.naturalWidth;
30
+ const heightRatio = containerHeight / imgElement.naturalHeight;
31
+ const baseScale = Math.max(widthRatio, heightRatio);
32
+
33
+ if (baseScale > 0 && baseScale < 1) {
34
+ // Extract the actual scale by dividing totalScale by baseScale
35
+ const scale = totalScale / baseScale;
36
+ // Clamp to valid range
37
+ return Math.max(0.1, Math.min(5.0, scale));
38
+ }
39
+ }
40
+ }
41
+
42
+ return 1.0;
43
+ }
44
+
45
+ /**
46
+ * Parse position (X, Y) from CSS transform string
47
+ * Returns container-relative percentages
48
+ */
49
+ export function parsePositionFromTransform(transform: string): { x: number; y: number } {
50
+ // Match all translate() calls - skip the first one if it's the centering translate(-50%, -50%)
51
+ const allTranslates = transform.matchAll(/translate\(([-\d.]+)%,?\s*([-\d.]+)?%?\)/g);
52
+ const translateArray = Array.from(allTranslates);
53
+
54
+ let positionX = 0;
55
+ let positionY = 0;
56
+
57
+ if (translateArray.length > 1) {
58
+ // Use the second translate (the position offset)
59
+ const positionTranslate = translateArray[1];
60
+ const appliedX = parseFloat(positionTranslate[1] || '0');
61
+ const appliedY = parseFloat(positionTranslate[2] || '0');
62
+
63
+ // Only use if it's not the centering translate (safety check)
64
+ if (appliedX !== -50 || appliedY !== -50) {
65
+ // With new system, applied value IS the stored value (container-relative)
66
+ positionX = appliedX;
67
+ positionY = appliedY;
68
+ }
69
+ } else if (translateArray.length === 1) {
70
+ // Only one translate - check if it's the centering one
71
+ const translate = translateArray[0];
72
+ const appliedX = parseFloat(translate[1] || '0');
73
+ const appliedY = parseFloat(translate[2] || '0');
74
+
75
+ // If it's not the centering translate, use it as position
76
+ if (appliedX !== -50 || appliedY !== -50) {
77
+ positionX = appliedX;
78
+ positionY = appliedY;
79
+ }
80
+ }
81
+
82
+ return { x: positionX, y: positionY };
83
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Types for GlobalImageEditor component
3
+ */
4
+ export interface SelectedImage {
5
+ element: HTMLElement;
6
+ originalSrc: string;
7
+ brightness: number;
8
+ blur: number;
9
+ scale: number;
10
+ positionX: number;
11
+ positionY: number;
12
+ isBackground: boolean;
13
+ aspectRatio?: string;
14
+ borderRadius?: string;
15
+ objectFit?: 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
16
+ objectPosition?: string;
17
+ }
18
+ export interface PluginConfig {
19
+ enabled: boolean;
20
+ className?: string;
21
+ overlayClassName?: string;
22
+ }
23
+ export interface DetectedStyling {
24
+ aspectRatio: string;
25
+ borderRadius: string;
26
+ objectFit: 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
27
+ objectPosition: string;
28
+ }
29
+ export interface PendingTransform {
30
+ scale: number;
31
+ positionX: number;
32
+ positionY: number;
33
+ semanticId: string;
34
+ filename: string;
35
+ }
36
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,WAAW,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,OAAO,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,YAAY,CAAC;IACjE,cAAc,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,MAAM,WAAW,YAAY;IACzB,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,eAAe;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,YAAY,CAAC;IAChE,cAAc,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,gBAAgB;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;CACpB"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Types for GlobalImageEditor component
3
+ */
4
+
5
+ export interface SelectedImage {
6
+ element: HTMLElement;
7
+ originalSrc: string;
8
+ brightness: number;
9
+ blur: number;
10
+ scale: number;
11
+ positionX: number;
12
+ positionY: number;
13
+ isBackground: boolean;
14
+ aspectRatio?: string;
15
+ borderRadius?: string;
16
+ objectFit?: 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
17
+ objectPosition?: string;
18
+ }
19
+
20
+ export interface PluginConfig {
21
+ enabled: boolean;
22
+ className?: string;
23
+ overlayClassName?: string;
24
+ }
25
+
26
+ export interface DetectedStyling {
27
+ aspectRatio: string;
28
+ borderRadius: string;
29
+ objectFit: 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
30
+ objectPosition: string;
31
+ }
32
+
33
+ export interface PendingTransform {
34
+ scale: number;
35
+ positionX: number;
36
+ positionY: number;
37
+ semanticId: string;
38
+ filename: string;
39
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Global Image Editor Component
3
+ * Allows clicking on any image in the client app to edit it (admin/dev only)
4
+ *
5
+ * Reads configuration from window.__JHITS_PLUGIN_PROPS__['plugin-images']
6
+ */
7
+ export declare function GlobalImageEditor(): import("react/jsx-runtime").JSX.Element | null;
8
+ //# sourceMappingURL=GlobalImageEditor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GlobalImageEditor.d.ts","sourceRoot":"","sources":["GlobalImageEditor.tsx"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,wBAAgB,iBAAiB,mDAkThC"}
@@ -0,0 +1,327 @@
1
+ /**
2
+ * Global Image Editor Component
3
+ * Allows clicking on any image in the client app to edit it (admin/dev only)
4
+ *
5
+ * Reads configuration from window.__JHITS_PLUGIN_PROPS__['plugin-images']
6
+ */
7
+
8
+ 'use client';
9
+
10
+ import React, { useState, useEffect, useRef, useMemo, useCallback } from 'react';
11
+ import { X, Edit2 } from 'lucide-react';
12
+ import { ImagePicker } from './ImagePicker';
13
+ import type { ImageMetadata } from '../types';
14
+ import type { SelectedImage, PendingTransform } from './GlobalImageEditor/types';
15
+ import { getPluginConfig } from './GlobalImageEditor/config';
16
+ import { parseImageData } from './GlobalImageEditor/imageDetection';
17
+ import { setupImageHandlers } from './GlobalImageEditor/imageSetup';
18
+ import { saveTransformToAPI, flushPendingSave, getFilename, normalizePosition } from './GlobalImageEditor/saveLogic';
19
+ import { handleImageChange, handleBrightnessChange, handleBlurChange } from './GlobalImageEditor/eventHandlers';
20
+
21
+ export function GlobalImageEditor() {
22
+ // Configuration
23
+ const config = useMemo(() => getPluginConfig(), []);
24
+
25
+ // State
26
+ const [selectedImage, setSelectedImage] = useState<SelectedImage | null>(null);
27
+ const [isOpen, setIsOpen] = useState(false);
28
+ const [userRole, setUserRole] = useState<string | null>(null);
29
+ const [isLoading, setIsLoading] = useState(true);
30
+
31
+ // Refs for save debouncing
32
+ const saveTransformTimeoutRef = useRef<NodeJS.Timeout | null>(null);
33
+ const pendingTransformRef = useRef<PendingTransform | null>(null);
34
+
35
+ // Check if user is admin/dev
36
+ useEffect(() => {
37
+ const checkUser = async () => {
38
+ try {
39
+ const res = await fetch('/api/me');
40
+ const data = await res.json();
41
+ if (data.loggedIn && (data.user?.role === 'admin' || data.user?.role === 'dev')) {
42
+ setUserRole(data.user.role);
43
+ }
44
+ } catch (error) {
45
+ console.error('Failed to check user role:', error);
46
+ } finally {
47
+ setIsLoading(false);
48
+ }
49
+ };
50
+ checkUser();
51
+ }, []);
52
+
53
+ // Listen for open-image-editor custom event from Image/BackgroundImage components
54
+ useEffect(() => {
55
+ if (!userRole) return;
56
+
57
+ const handleOpenEditor = async (e: CustomEvent) => {
58
+ const { id, currentBrightness, currentBlur } = e.detail || {};
59
+ if (!id) return;
60
+
61
+
62
+ // Find the element by data-image-id or data-background-image-id
63
+ let element = document.querySelector(`[data-image-id="${id}"], [data-background-image-id="${id}"]`) as HTMLElement;
64
+
65
+ if (!element) {
66
+ element = document.querySelector(`[data-background-image-component="true"][data-background-image-id="${id}"]`) as HTMLElement;
67
+ }
68
+
69
+ if (!element) {
70
+ console.error('[GlobalImageEditor] Element not found for id:', id);
71
+ return;
72
+ }
73
+
74
+ // Extract semantic ID
75
+ const semanticId = element.getAttribute('data-image-id') ||
76
+ element.getAttribute('data-background-image-id') ||
77
+ id;
78
+
79
+ // Parse image data
80
+ const imageData = parseImageData(element, semanticId, currentBrightness, currentBlur);
81
+
82
+ if (!imageData) {
83
+ console.error('[GlobalImageEditor] Failed to parse image data');
84
+ return;
85
+ }
86
+
87
+ // Store semantic ID on element if not already set
88
+ if (imageData.isBackground && !element.hasAttribute('data-background-image-id')) {
89
+ element.setAttribute('data-background-image-id', semanticId);
90
+ } else if (!imageData.isBackground && !element.hasAttribute('data-image-id')) {
91
+ element.setAttribute('data-image-id', semanticId);
92
+ }
93
+
94
+ setSelectedImage(imageData);
95
+ setIsOpen(true);
96
+ };
97
+
98
+ window.addEventListener('open-image-editor', handleOpenEditor as unknown as EventListener);
99
+
100
+ return () => {
101
+ window.removeEventListener('open-image-editor', handleOpenEditor as unknown as EventListener);
102
+ };
103
+ }, [userRole]);
104
+
105
+ // Add click handlers to all images
106
+ useEffect(() => {
107
+ if (isLoading || !userRole) {
108
+ return;
109
+ }
110
+
111
+
112
+ const cleanup = setupImageHandlers((imageData) => {
113
+ setSelectedImage(imageData);
114
+ setIsOpen(true);
115
+ });
116
+
117
+ // Setup immediately and also after a short delay to catch dynamically loaded images
118
+ const timeoutId = setTimeout(() => {
119
+ const cleanup2 = setupImageHandlers((imageData) => {
120
+ setSelectedImage(imageData);
121
+ setIsOpen(true);
122
+ });
123
+ // Note: This creates a new cleanup, but the first one will handle the initial setup
124
+ }, 500);
125
+
126
+ return () => {
127
+ clearTimeout(timeoutId);
128
+ cleanup();
129
+ };
130
+ }, [isLoading, userRole]);
131
+
132
+ // Save image transform (scale/position) to API with debouncing
133
+ const saveImageTransformHandler = useCallback(async (
134
+ scale: number,
135
+ positionX: number,
136
+ positionY: number,
137
+ immediate: boolean = false,
138
+ brightnessOverride?: number,
139
+ blurOverride?: number
140
+ ) => {
141
+ if (!selectedImage) return;
142
+
143
+ const { element } = selectedImage;
144
+ const semanticId = element.getAttribute('data-image-id') ||
145
+ element.getAttribute('data-background-image-id');
146
+
147
+ if (!semanticId) return;
148
+
149
+ const filename = await getFilename(semanticId, selectedImage);
150
+ if (!filename) return;
151
+
152
+ // Store pending transform
153
+ pendingTransformRef.current = {
154
+ scale,
155
+ positionX,
156
+ positionY,
157
+ semanticId,
158
+ filename
159
+ };
160
+
161
+ // Clear existing timeout
162
+ if (saveTransformTimeoutRef.current) {
163
+ clearTimeout(saveTransformTimeoutRef.current);
164
+ saveTransformTimeoutRef.current = null;
165
+ }
166
+
167
+ // If immediate save is requested (e.g., when "Done" is clicked), save right away
168
+ if (immediate) {
169
+ const normalizedPositionX = normalizePosition(positionX);
170
+ const normalizedPositionY = normalizePosition(positionY);
171
+
172
+ // Use override values if provided, otherwise use selectedImage values
173
+ // IMPORTANT: Overrides contain the latest values from the editor
174
+ const finalBrightness = brightnessOverride !== undefined ? brightnessOverride : (selectedImage?.brightness ?? 100);
175
+ const finalBlur = blurOverride !== undefined ? blurOverride : (selectedImage?.blur ?? 0);
176
+
177
+ await saveTransformToAPI(
178
+ semanticId,
179
+ filename,
180
+ scale,
181
+ normalizedPositionX,
182
+ normalizedPositionY,
183
+ finalBrightness,
184
+ finalBlur
185
+ );
186
+ pendingTransformRef.current = null;
187
+ return;
188
+ }
189
+
190
+ // Debounce: save after 300ms of no changes
191
+ saveTransformTimeoutRef.current = setTimeout(async () => {
192
+ const pending = pendingTransformRef.current;
193
+ if (pending && selectedImage) {
194
+ await flushPendingSave(pending, selectedImage);
195
+ }
196
+ }, 300);
197
+ }, [selectedImage]);
198
+
199
+ // Cleanup timeout on unmount
200
+ useEffect(() => {
201
+ return () => {
202
+ if (saveTransformTimeoutRef.current) {
203
+ clearTimeout(saveTransformTimeoutRef.current);
204
+ }
205
+ };
206
+ }, []);
207
+
208
+ // Event handlers
209
+ const handleImageChangeWrapper = useCallback(async (image: ImageMetadata | null) => {
210
+ if (!selectedImage) return;
211
+ await handleImageChange(image, selectedImage, handleClose);
212
+ }, [selectedImage]);
213
+
214
+ const handleBrightnessChangeWrapper = useCallback(async (brightness: number) => {
215
+ if (!selectedImage) return;
216
+ // Don't save immediately when in editor - will be saved when "Done" is pressed
217
+ await handleBrightnessChange(brightness, selectedImage, setSelectedImage, false);
218
+ }, [selectedImage]);
219
+
220
+ const handleBlurChangeWrapper = useCallback(async (blur: number) => {
221
+ if (!selectedImage) return;
222
+ // Don't save immediately when in editor - will be saved when "Done" is pressed
223
+ await handleBlurChange(blur, selectedImage, setSelectedImage, false);
224
+ }, [selectedImage]);
225
+
226
+ const handleClose = useCallback(() => {
227
+ setIsOpen(false);
228
+ setSelectedImage(null);
229
+ }, []);
230
+
231
+ // Don't render if disabled or user is not admin/dev
232
+ if (!config.enabled || isLoading || !userRole) {
233
+ return null;
234
+ }
235
+
236
+ if (!isOpen || !selectedImage) {
237
+ return null;
238
+ }
239
+
240
+ return (
241
+ <div
242
+ className={`fixed inset-0 z-50 flex items-center justify-center ${config.overlayClassName || 'bg-black/50 backdrop-blur-sm'}`}
243
+ data-image-editor="true"
244
+ onClick={(e) => {
245
+ if (e.target === e.currentTarget) {
246
+ handleClose();
247
+ }
248
+ }}
249
+ >
250
+ <div
251
+ className={`relative bg-white dark:bg-neutral-900 rounded-2xl shadow-2xl max-w-4xl w-full mx-4 max-h-[90vh] flex flex-col ${config.className || ''}`}
252
+ onClick={(e) => e.stopPropagation()}
253
+ >
254
+ {/* Header */}
255
+ <div className="flex items-center justify-between p-6 border-b dark:border-neutral-800">
256
+ <h2 className="text-2xl font-bold dark:text-white">Edit Image</h2>
257
+ <button
258
+ onClick={handleClose}
259
+ className="p-2 hover:bg-neutral-100 dark:hover:bg-neutral-800 rounded-lg transition-colors"
260
+ aria-label="Close editor"
261
+ >
262
+ <X className="w-5 h-5" />
263
+ </button>
264
+ </div>
265
+
266
+ {/* Content */}
267
+ <div className="p-6">
268
+ <ImagePicker
269
+ value={selectedImage.element.getAttribute('data-image-id') || selectedImage.element.getAttribute('data-background-image-id') || selectedImage.originalSrc}
270
+ onChange={handleImageChangeWrapper}
271
+ brightness={selectedImage.brightness}
272
+ blur={selectedImage.blur}
273
+ scale={selectedImage.scale}
274
+ positionX={selectedImage.positionX}
275
+ positionY={selectedImage.positionY}
276
+ onBrightnessChange={handleBrightnessChangeWrapper}
277
+ onBlurChange={handleBlurChangeWrapper}
278
+ onScaleChange={async (newScale: number) => {
279
+ if (selectedImage) {
280
+ const updated = { ...selectedImage, scale: newScale };
281
+ setSelectedImage(updated);
282
+ await saveImageTransformHandler(newScale, updated.positionX, updated.positionY);
283
+ }
284
+ }}
285
+ onPositionXChange={async (newPositionX: number) => {
286
+ if (selectedImage) {
287
+ const updated = { ...selectedImage, positionX: newPositionX };
288
+ setSelectedImage(updated);
289
+ await saveImageTransformHandler(updated.scale, newPositionX, updated.positionY);
290
+ }
291
+ }}
292
+ onPositionYChange={async (newPositionY: number) => {
293
+ if (selectedImage) {
294
+ const updated = { ...selectedImage, positionY: newPositionY };
295
+ setSelectedImage(updated);
296
+ await saveImageTransformHandler(updated.scale, updated.positionX, newPositionY);
297
+ }
298
+ }}
299
+ onEditorSave={async (finalScale: number, finalPositionX: number, finalPositionY: number, finalBrightness?: number, finalBlur?: number) => {
300
+ if (selectedImage) {
301
+ // Update selectedImage with final values
302
+ // Use provided brightness/blur if available, otherwise use current values
303
+ const updated = {
304
+ ...selectedImage,
305
+ scale: finalScale,
306
+ positionX: finalPositionX,
307
+ positionY: finalPositionY,
308
+ brightness: finalBrightness !== undefined ? finalBrightness : selectedImage.brightness,
309
+ blur: finalBlur !== undefined ? finalBlur : selectedImage.blur
310
+ };
311
+ setSelectedImage(updated);
312
+ // Save with the final brightness and blur values
313
+ await saveImageTransformHandler(finalScale, finalPositionX, finalPositionY, true, updated.brightness, updated.blur);
314
+ }
315
+ }}
316
+ showEffects={true}
317
+ darkMode={false}
318
+ aspectRatio={selectedImage.aspectRatio}
319
+ borderRadius={selectedImage.borderRadius}
320
+ objectFit={selectedImage.objectFit}
321
+ objectPosition={selectedImage.objectPosition}
322
+ />
323
+ </div>
324
+ </div>
325
+ </div>
326
+ );
327
+ }
@@ -0,0 +1,22 @@
1
+ import React from 'react';
2
+ export interface PluginImageProps {
3
+ id: string;
4
+ alt: string;
5
+ width?: number;
6
+ height?: number;
7
+ className?: string;
8
+ fill?: boolean;
9
+ sizes?: string;
10
+ priority?: boolean;
11
+ objectFit?: 'contain' | 'cover' | 'fill' | 'none' | 'scale-down';
12
+ objectPosition?: string;
13
+ style?: React.CSSProperties;
14
+ editable?: boolean;
15
+ scale?: number;
16
+ positionX?: number;
17
+ positionY?: number;
18
+ brightness?: number;
19
+ blur?: number;
20
+ }
21
+ export declare function Image({ id, alt, width, height, className, fill, sizes, priority, objectFit, objectPosition, style, editable, ...props }: PluginImageProps): import("react/jsx-runtime").JSX.Element;
22
+ //# sourceMappingURL=Image.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Image.d.ts","sourceRoot":"","sources":["Image.tsx"],"names":[],"mappings":"AAGA,OAAO,KAA4D,MAAM,OAAO,CAAC;AAKjF,MAAM,WAAW,gBAAgB;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,YAAY,CAAC;IACjE,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;IAC5B,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,KAAK,CAAC,EAClB,EAAE,EACF,GAAG,EACH,KAAK,EACL,MAAM,EACN,SAAc,EACd,IAAY,EACZ,KAAK,EACL,QAAgB,EAChB,SAAmB,EACnB,cAAyB,EACzB,KAAK,EACL,QAAe,EACf,GAAG,KAAK,EACX,EAAE,gBAAgB,2CA2SlB"}