-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebugCopyEditor.tsx
More file actions
302 lines (283 loc) · 9.11 KB
/
Copy pathDebugCopyEditor.tsx
File metadata and controls
302 lines (283 loc) · 9.11 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
'use client';
import { useEffect, useRef, useState } from 'react';
type Entry = { el: HTMLElement; page: string; label: string; original: string };
// Module-level so the React Compiler treats the DOM writes as external to
// the component (mutating ref contents inside a handler trips its lint).
function restoreOriginals(list: Entry[]) {
for (const e of list) {
e.el.textContent = e.original;
}
}
function setSlideHidden(page: string, hidden: boolean) {
const section = document.getElementById(`page-${page}`);
if (section) section.style.display = hidden ? 'none' : '';
}
function slideArchetype(page: string): string {
const section = document.getElementById(`page-${page}`);
return (
section
?.querySelector('[data-archetype]')
?.getAttribute('data-archetype') ?? ''
);
}
function slideTitle(page: string): string {
const section = document.getElementById(`page-${page}`);
const heading = section?.querySelector('h1, h2');
return heading?.textContent?.trim() ?? '(untitled)';
}
// The family-book UI kit icons in public/opportunity/icons/.
const BAND_ICONS = [
'heart',
'tree',
'home',
'notebook',
'lightning',
'microphone',
'stacks',
'settings',
'battery',
'image',
'play',
'close',
] as const;
const DEFAULT_BAND_ICON = 'tree';
function setBandIcon(name: string) {
document.documentElement.style.setProperty(
'--deck-band-icon',
`url('/opportunity/icons/${name}.png')`
);
}
const LAYOUT_VARIANTS = [
'Statement',
'Statement (splash)',
'BigStat',
'Split',
'Split (flipped)',
'EvidenceGrid',
'DiagramPage',
'Ledger',
'Cards',
'Cards (3-col)',
'PricingTiers',
] as const;
/**
* Live copy-editing tool, mounted only when the URL carries ?debug=true.
* Every text block inside the deck pages becomes contentEditable; the Copy
* button writes an OLD/NEW diff of everything touched (plus any requested
* layout-variant changes) to the clipboard so the edits can be pasted back
* and applied to the source files. Layout picks are recorded, not
* live-previewed: slides are pre-composed components.
*/
export default function DebugCopyEditor({
refreshKey,
}: {
refreshKey: string;
}) {
const entries = useRef<Entry[]>([]);
const [layoutPicks, setLayoutPicks] = useState<Map<string, string>>(
() => new Map()
);
const [count, setCount] = useState(0);
const [copied, setCopied] = useState<string | null>(null);
const [currentPage, setCurrentPage] = useState('1');
const [currentArchetype, setCurrentArchetype] = useState('');
const [deleted, setDeleted] = useState<Map<string, string>>(() => new Map());
const [bandIcon, setBandIconState] = useState(DEFAULT_BAND_ICON);
const pickBandIcon = (name: string) => {
setBandIcon(name);
setBandIconState(name);
};
useEffect(() => {
// Wait a frame so the (un)locked page list has rendered before scanning.
const raf = requestAnimationFrame(() => {
for (const e of entries.current) {
const el = e.el;
el.removeAttribute('contenteditable');
el.classList.remove('debug-editable');
}
entries.current = [];
const sections = document.querySelectorAll<HTMLElement>(
'section[id^="page-"]'
);
sections.forEach(section => {
const page = section.id.replace('page-', '');
const targets = section.querySelectorAll<HTMLElement>(
'h1, h2, h3, h4, p, figcaption, li, td, th, ' +
'.deck-stat-tile-value, .deck-stat-tile-label, ' +
'.deck-tier-price, .deck-tier-meta, ' +
'.deck-ledger-label, .deck-ledger-value, .deck-media-caption'
);
let i = 0;
targets.forEach(target => {
const el = target;
const text = el.textContent ?? '';
if (!text.trim()) return;
i += 1;
try {
// Chrome/Safari support plaintext-only, which keeps pasted markup out.
el.contentEditable = 'plaintext-only';
} catch {
el.contentEditable = 'true';
}
el.classList.add('debug-editable');
el.spellcheck = false;
entries.current.push({
el,
page,
label: `${el.tagName.toLowerCase()} #${i}`,
original: text,
});
});
});
setCount(entries.current.length);
});
return () => cancelAnimationFrame(raf);
}, [refreshKey]);
// Track which slide is in view so the layout picker targets it.
useEffect(() => {
const deck = document.querySelector('.deck');
if (!deck) return;
let raf = 0;
const onScroll = () => {
cancelAnimationFrame(raf);
raf = requestAnimationFrame(() => {
let best: { page: string; dist: number } | null = null;
document
.querySelectorAll<HTMLElement>('section[id^="page-"]')
.forEach(section => {
const dist = Math.abs(section.getBoundingClientRect().top);
if (!best || dist < best.dist) {
best = { page: section.id.replace('page-', ''), dist };
}
});
if (best) {
const page = (best as { page: string }).page;
setCurrentPage(page);
setCurrentArchetype(slideArchetype(page));
}
});
};
onScroll();
deck.addEventListener('scroll', onScroll, { passive: true });
return () => {
deck.removeEventListener('scroll', onScroll);
cancelAnimationFrame(raf);
};
}, [refreshKey]);
const changed = () =>
entries.current.filter(
e => (e.el.textContent ?? '').trim() !== e.original.trim()
);
const copy = async () => {
const edits = changed();
const picks = [...layoutPicks.entries()];
const removals = [...deleted.entries()];
const iconChanged = bandIcon !== DEFAULT_BAND_ICON;
if (
edits.length === 0 &&
picks.length === 0 &&
removals.length === 0 &&
!iconChanged
) {
setCopied('No edits yet');
setTimeout(() => setCopied(null), 1500);
return;
}
const lines = ['COPY EDITS from /opportunity?debug=true', ''];
for (const e of edits) {
lines.push(`[page ${e.page} · ${e.label}]`);
lines.push(`OLD: ${e.original.trim()}`);
lines.push(`NEW: ${(e.el.textContent ?? '').trim()}`);
lines.push('');
}
for (const [page, variant] of picks) {
lines.push(`[page ${page} · layout]`);
const current = slideArchetype(page);
if (current) lines.push(`OLD LAYOUT: ${current}`);
lines.push(`NEW LAYOUT: ${variant}`);
lines.push('');
}
for (const [page, title] of removals) {
lines.push(`[page ${page} · slide]`);
lines.push(`DELETE SLIDE: ${title}`);
lines.push('');
}
if (iconChanged) {
lines.push('[accent box · icon]');
lines.push(`OLD ICON: ${DEFAULT_BAND_ICON}`);
lines.push(`NEW ICON: ${bandIcon}`);
lines.push('');
}
await navigator.clipboard.writeText(lines.join('\n'));
const n =
edits.length + picks.length + removals.length + (iconChanged ? 1 : 0);
setCopied(`Copied ${n} edit${n === 1 ? '' : 's'}`);
setTimeout(() => setCopied(null), 2000);
};
const reset = () => {
restoreOriginals(entries.current);
for (const page of deleted.keys()) setSlideHidden(page, false);
setDeleted(new Map());
setLayoutPicks(new Map());
pickBandIcon(DEFAULT_BAND_ICON);
setCopied('Reset');
setTimeout(() => setCopied(null), 1200);
};
const deleteSlide = () => {
const title = slideTitle(currentPage);
setSlideHidden(currentPage, true);
setDeleted(prev => new Map(prev).set(currentPage, title));
};
const pickLayout = (variant: string) => {
setLayoutPicks(prev => {
const next = new Map(prev);
// Re-selecting the slide's real archetype clears the override.
if (variant === '' || variant === currentArchetype) {
next.delete(currentPage);
} else {
next.set(currentPage, variant);
}
return next;
});
};
return (
<div className="debug-bar" role="toolbar" aria-label="Copy editor">
<span className="debug-bar-hint">
debug · p{currentPage} · click text to edit ({count})
</span>
<label className="debug-bar-hint">
layout{' '}
<select
value={layoutPicks.get(currentPage) ?? currentArchetype}
onChange={e => pickLayout(e.target.value)}
>
<option value="">(bespoke)</option>
{LAYOUT_VARIANTS.map(v => (
<option key={v} value={v}>
{v}
</option>
))}
</select>
</label>
<label className="debug-bar-hint">
icon{' '}
<select value={bandIcon} onChange={e => pickBandIcon(e.target.value)}>
{BAND_ICONS.map(name => (
<option key={name} value={name}>
{name}
</option>
))}
</select>
</label>
<button type="button" onClick={deleteSlide}>
Delete slide{deleted.size > 0 ? ` (${deleted.size})` : ''}
</button>
<button type="button" onClick={copy}>
{copied ?? 'Copy edits'}
</button>
<button type="button" onClick={reset}>
Reset
</button>
</div>
);
}