File size: 5,480 Bytes
45b3fab
 
 
 
 
 
 
3b8892c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * ImageAndTextLayout.tsx
 * Purpose: Implements one of the generic fallback slide layouts used when a template-specific renderer is unavailable.
 * Used by: components/slides/SlideFactory.
 * Depends on: Template styling props and inline field-edit callbacks.
 */

'use client';

import React, { useState } from 'react';
import { TemplateStyles } from '@/data/templates';

export interface ImageAndTextLayoutProps {
  title: string;
  body?: string;
  imageUrl?: string;
  styles: TemplateStyles;
  slideId?: string;
  isEditable?: boolean;
  onFieldUpdate?: (slideId: string, field: string, value: string) => void;
}

export default function ImageAndTextLayout({
  title,
  body,
  imageUrl,
  styles,
  slideId,
  isEditable = false,
  onFieldUpdate,
}: ImageAndTextLayoutProps) {
  const [editingField, setEditingField] = useState<string | null>(null);
  const [tempTitle, setTempTitle] = useState(title);
  const [tempBody, setTempBody] = useState(body || '');

  const handleBlur = (field: string) => {
    if (!slideId || !onFieldUpdate) return;
    if (field === 'title' && tempTitle !== title) onFieldUpdate(slideId, 'title', tempTitle);
    if (field === 'body' && tempBody !== body) onFieldUpdate(slideId, 'body', tempBody);
    setEditingField(null);
  };

  const handleKeyDown = (e: React.KeyboardEvent, field: string) => {
    if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleBlur(field); }
    if (e.key === 'Escape') {
      setEditingField(null);
      setTempTitle(title);
      setTempBody(body || '');
    }
  };

  return (
    <div
      className="w-full h-full flex relative overflow-hidden"
      style={{ backgroundColor: styles.colors.background, color: styles.colors.text }}
    >
      {styles.dotPattern && (
        <div aria-hidden className="absolute inset-0 opacity-30" style={{ background: styles.dotPattern }} />
      )}

      {/* Image Side */}
      <div
        className="w-[45%] h-full relative shrink-0"
        style={{
          backgroundColor: styles.colors.cardBg,
          borderRight: `${styles.border.width} solid ${styles.colors.border}`,
        }}
      >
        {imageUrl ? (
          <img
            src={imageUrl}
            alt={title}
            className="w-full h-full object-cover"
          />
        ) : (
          <div className="w-full h-full flex items-center justify-center" style={{ backgroundColor: styles.colors.cardBg }}>
            <span style={{ fontFamily: styles.fonts.body, fontSize: '14px', opacity: 0.5 }}>
              Image placeholder
            </span>
          </div>
        )}
      </div>

      {/* Text Side */}
      <div className="flex-1 p-10 flex flex-col justify-center relative z-10">
        {/* Title */}
        {editingField === 'title' ? (
          <input
            type="text"
            value={tempTitle}
            onChange={(e) => setTempTitle(e.target.value)}
            onBlur={() => handleBlur('title')}
            onKeyDown={(e) => handleKeyDown(e, 'title')}
            className="bg-transparent outline-none mb-6"
            style={{
              fontFamily: styles.fonts.heading,
              fontSize: '36px',
              fontWeight: 'bold',
              textTransform: 'uppercase',
              color: styles.colors.text,
              backgroundColor: styles.colors.accent,
              border: `${styles.border.width} solid ${styles.colors.border}`,
              boxShadow: styles.border.shadow,
              padding: '8px 20px',
            }}
            autoFocus
          />
        ) : (
          <h2
            className={`mb-6 ${isEditable ? 'cursor-pointer' : ''}`}
            style={{
              fontFamily: styles.fonts.heading,
              fontSize: '36px',
              fontWeight: 'bold',
              textTransform: 'uppercase',
              color: styles.colors.text,
              display: 'inline-block',
              alignSelf: 'flex-start',
              backgroundColor: styles.colors.accent,
              border: `${styles.border.width} solid ${styles.colors.border}`,
              boxShadow: styles.border.shadow,
              padding: '8px 20px',
            }}
            onClick={() => { if (isEditable) { setEditingField('title'); setTempTitle(title); } }}
          >
            {title}
          </h2>
        )}

        {/* Body text */}
        {editingField === 'body' ? (
          <textarea
            value={tempBody}
            onChange={(e) => setTempBody(e.target.value)}
            onBlur={() => handleBlur('body')}
            onKeyDown={(e) => handleKeyDown(e, 'body')}
            className="bg-transparent outline-none resize-none flex-1"
            style={{
              fontFamily: styles.fonts.body,
              fontSize: '17px',
              color: styles.colors.text,
              lineHeight: 1.7,
              opacity: 0.85,
            }}
            autoFocus
          />
        ) : (
          <p
            className={`${isEditable ? 'cursor-pointer hover:opacity-70' : ''}`}
            style={{
              fontFamily: styles.fonts.body,
              fontSize: '17px',
              color: styles.colors.text,
              lineHeight: 1.7,
              opacity: 0.85,
            }}
            onClick={() => { if (isEditable) { setEditingField('body'); setTempBody(body || ''); } }}
          >
            {body || (isEditable ? 'Click to add description' : '')}
          </p>
        )}
      </div>
    </div>
  );
}