@jhits/plugin-images 0.0.3 → 0.0.5

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.
@@ -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,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,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
+ }