@bycrux/editor 0.8.0 → 0.8.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bycrux/editor",
3
- "version": "0.8.0",
3
+ "version": "0.8.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -122,6 +122,69 @@ interface Props {
122
122
  hiddenElementIds?: string[]
123
123
  }
124
124
 
125
+ // Plain default for the inline editor when we can't read the rendered text's
126
+ // real style (e.g. no matching node found).
127
+ const FALLBACK_EDIT_STYLE: Partial<CSSStyleDeclaration> = {
128
+ color: '#ffffff',
129
+ fontSize: '18px',
130
+ fontFamily: 'system-ui, sans-serif',
131
+ whiteSpace: 'pre-wrap',
132
+ wordBreak: 'break-word',
133
+ }
134
+
135
+ // Find the innermost element under `root` whose full text equals `text` — the
136
+ // overlay's text container. Used to copy its computed style onto the editor.
137
+ function findTextNode(root: HTMLElement, text: string): HTMLElement | null {
138
+ const want = text.trim()
139
+ if (!want) return null
140
+ const matches: HTMLElement[] = []
141
+ const walk = (el: HTMLElement) => {
142
+ if ((el.textContent ?? '').trim() === want) matches.push(el)
143
+ for (const child of Array.from(el.children)) {
144
+ if (child instanceof HTMLElement) walk(child)
145
+ }
146
+ }
147
+ walk(root)
148
+ if (matches.length === 0) return null
149
+ // Innermost match = the one with the fewest descendant elements.
150
+ return matches.reduce((best, el) =>
151
+ el.querySelectorAll('*').length < best.querySelectorAll('*').length ? el : best,
152
+ )
153
+ }
154
+
155
+ // Snapshot the rendered overlay text's computed style so the inline editor
156
+ // matches it instead of a generic stub. Overlays are authored at native slide
157
+ // pixels then CSS-scaled by `scale`; the editor is positioned in already-scaled
158
+ // display coords with no transform, so px metrics must be multiplied by `scale`.
159
+ // Returns null when no text node is found (caller falls back).
160
+ function captureTextStyle(
161
+ wrapper: HTMLElement | undefined,
162
+ text: string,
163
+ scale: number,
164
+ ): Partial<CSSStyleDeclaration> | null {
165
+ if (!wrapper || typeof window === 'undefined') return null
166
+ const target = findTextNode(wrapper, text)
167
+ if (!target) return null
168
+ const cs = window.getComputedStyle(target)
169
+ const scalePx = (v: string) => {
170
+ const n = parseFloat(v)
171
+ return Number.isFinite(n) ? `${n * scale}px` : v
172
+ }
173
+ return {
174
+ color: cs.color,
175
+ fontFamily: cs.fontFamily,
176
+ fontWeight: cs.fontWeight,
177
+ fontStyle: cs.fontStyle,
178
+ textAlign: cs.textAlign,
179
+ textTransform: cs.textTransform,
180
+ fontSize: scalePx(cs.fontSize),
181
+ lineHeight: cs.lineHeight === 'normal' ? 'normal' : scalePx(cs.lineHeight),
182
+ letterSpacing: cs.letterSpacing === 'normal' ? 'normal' : scalePx(cs.letterSpacing),
183
+ whiteSpace: 'pre-wrap',
184
+ wordBreak: 'break-word',
185
+ } as Partial<CSSStyleDeclaration>
186
+ }
187
+
125
188
  export default function SlideCanvas({
126
189
  slide,
127
190
  slideId,
@@ -158,6 +221,7 @@ export default function SlideCanvas({
158
221
  // Inline text edit state.
159
222
  const [editingId, setEditingId] = useState<string | null>(null)
160
223
  const [editRect, setEditRect] = useState<{ left: number; top: number; width: number; height: number } | null>(null)
224
+ const [editStyle, setEditStyle] = useState<Partial<CSSStyleDeclaration> | null>(null)
161
225
 
162
226
  // Crop mode local state — source fraction window + loaded natural dims.
163
227
  const [cropState, setCropState] = useState<CropMode>(null)
@@ -266,6 +330,10 @@ export default function SlideCanvas({
266
330
  // ── Inline text edit ──
267
331
  function beginTextEdit(element: OverlayElement) {
268
332
  if (!interactive || typeof element.overlay.props.text !== 'string') return
333
+ // Snapshot the live text's style BEFORE we hide it / re-render.
334
+ setEditStyle(
335
+ captureTextStyle(wrapperRefs.current.get(element.id), element.overlay.props.text, scale),
336
+ )
269
337
  setEditRect({
270
338
  left: element.x * scale,
271
339
  top: element.y * scale,
@@ -275,10 +343,15 @@ export default function SlideCanvas({
275
343
  setEditingId(element.id)
276
344
  }
277
345
 
278
- function commitTextEdit(element: OverlayElement, value: string) {
279
- void updateOverlayProp?.(sid, element.id, 'text', value)
346
+ function endTextEdit() {
280
347
  setEditingId(null)
281
348
  setEditRect(null)
349
+ setEditStyle(null)
350
+ }
351
+
352
+ function commitTextEdit(element: OverlayElement, value: string) {
353
+ void updateOverlayProp?.(sid, element.id, 'text', value)
354
+ endTextEdit()
282
355
  }
283
356
 
284
357
  // ── Pointer wiring shared by drag/resize/rotate ──
@@ -461,6 +534,9 @@ export default function SlideCanvas({
461
534
  height: element.h,
462
535
  transform: `scale(${scale})`,
463
536
  transformOrigin: 'top left',
537
+ // Hide the live text while it's being edited in place so the
538
+ // InlineTextEditor isn't doubled over it.
539
+ visibility: editingId === element.id ? 'hidden' : undefined,
464
540
  }}
465
541
  >
466
542
  <OverlayErrorBoundary
@@ -637,16 +713,10 @@ export default function SlideCanvas({
637
713
  key={editingId}
638
714
  initialValue={initial}
639
715
  rect={editRect}
640
- styleSnapshot={{
641
- color: '#ffffff',
642
- fontSize: '18px',
643
- fontFamily: 'system-ui, sans-serif',
644
- whiteSpace: 'pre-wrap',
645
- wordBreak: 'break-word',
646
- }}
716
+ styleSnapshot={editStyle ?? FALLBACK_EDIT_STYLE}
647
717
  onChange={() => {}}
648
718
  onCommit={(value) => commitTextEdit(el, value)}
649
- onCancel={() => { setEditingId(null); setEditRect(null) }}
719
+ onCancel={endTextEdit}
650
720
  />
651
721
  )
652
722
  })()}
package/src/index.ts CHANGED
@@ -29,6 +29,8 @@ export type {
29
29
  OverlayFactory,
30
30
  RenderEvent,
31
31
  RenderOptions,
32
+ RenderStatus,
33
+ RenderPhase,
32
34
  CaptionEvent,
33
35
  GenerateCaptionsOptions,
34
36
  MediaScope,