ugcinc-render 1.5.5 → 1.5.7
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/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +90 -75
- package/dist/index.mjs +80 -75
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -892,6 +892,11 @@ interface TextElementProps {
|
|
|
892
892
|
* - Padding (uniform and individual)
|
|
893
893
|
* - Auto-width with box alignment
|
|
894
894
|
* - Rotation
|
|
895
|
+
*
|
|
896
|
+
* When autoWidth is enabled:
|
|
897
|
+
* - Text wraps at maxWidth (the element's width)
|
|
898
|
+
* - Background shrinks to the widest line
|
|
899
|
+
* - boxAlign positions the box within the maxWidth container
|
|
895
900
|
*/
|
|
896
901
|
declare function TextElement({ segment, scale }: TextElementProps): react_jsx_runtime.JSX.Element;
|
|
897
902
|
|
package/dist/index.d.ts
CHANGED
|
@@ -892,6 +892,11 @@ interface TextElementProps {
|
|
|
892
892
|
* - Padding (uniform and individual)
|
|
893
893
|
* - Auto-width with box alignment
|
|
894
894
|
* - Rotation
|
|
895
|
+
*
|
|
896
|
+
* When autoWidth is enabled:
|
|
897
|
+
* - Text wraps at maxWidth (the element's width)
|
|
898
|
+
* - Background shrinks to the widest line
|
|
899
|
+
* - boxAlign positions the box within the maxWidth container
|
|
895
900
|
*/
|
|
896
901
|
declare function TextElement({ segment, scale }: TextElementProps): react_jsx_runtime.JSX.Element;
|
|
897
902
|
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -86,7 +96,7 @@ var import_react3 = require("react");
|
|
|
86
96
|
var import_remotion2 = require("remotion");
|
|
87
97
|
|
|
88
98
|
// src/components/TextElement.tsx
|
|
89
|
-
var import_react = require("react");
|
|
99
|
+
var import_react = __toESM(require("react"));
|
|
90
100
|
|
|
91
101
|
// src/utils/defaults.ts
|
|
92
102
|
var TEXT_DEFAULTS = {
|
|
@@ -294,7 +304,7 @@ function hexToRgba(hex, opacity = 100) {
|
|
|
294
304
|
|
|
295
305
|
// src/components/TextElement.tsx
|
|
296
306
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
297
|
-
function
|
|
307
|
+
function calculateAutoWidthAndLines({
|
|
298
308
|
text,
|
|
299
309
|
maxWidth,
|
|
300
310
|
paddingLeft,
|
|
@@ -306,52 +316,17 @@ function calculateAutoWidth({
|
|
|
306
316
|
lineHeight
|
|
307
317
|
}) {
|
|
308
318
|
if (typeof document === "undefined") {
|
|
309
|
-
|
|
319
|
+
return { width: maxWidth, lines: [text] };
|
|
310
320
|
}
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
visibility: hidden;
|
|
315
|
-
pointer-events: none;
|
|
316
|
-
width: ${maxWidth}px;
|
|
317
|
-
padding: 0 ${paddingRight}px 0 ${paddingLeft}px;
|
|
318
|
-
box-sizing: border-box;
|
|
319
|
-
font-family: ${fontFamily};
|
|
320
|
-
font-size: ${fontSize}px;
|
|
321
|
-
font-weight: ${fontWeight};
|
|
322
|
-
letter-spacing: ${letterSpacing}px;
|
|
323
|
-
line-height: ${lineHeight};
|
|
324
|
-
white-space: pre-wrap;
|
|
325
|
-
word-break: break-word;
|
|
326
|
-
`;
|
|
327
|
-
container.textContent = text;
|
|
328
|
-
document.body.appendChild(container);
|
|
329
|
-
const totalHeight = container.offsetHeight;
|
|
330
|
-
const lineHeightPx = fontSize * lineHeight;
|
|
331
|
-
const numLines = Math.max(1, Math.round(totalHeight / lineHeightPx));
|
|
332
|
-
if (numLines === 1) {
|
|
333
|
-
const span = document.createElement("span");
|
|
334
|
-
span.style.cssText = `
|
|
335
|
-
font-family: ${fontFamily};
|
|
336
|
-
font-size: ${fontSize}px;
|
|
337
|
-
font-weight: ${fontWeight};
|
|
338
|
-
letter-spacing: ${letterSpacing}px;
|
|
339
|
-
white-space: nowrap;
|
|
340
|
-
`;
|
|
341
|
-
span.textContent = text;
|
|
342
|
-
document.body.appendChild(span);
|
|
343
|
-
const textWidth = span.offsetWidth;
|
|
344
|
-
document.body.removeChild(span);
|
|
345
|
-
document.body.removeChild(container);
|
|
346
|
-
return Math.min(textWidth + paddingLeft + paddingRight, maxWidth);
|
|
321
|
+
const availableWidth = maxWidth - paddingLeft - paddingRight;
|
|
322
|
+
if (availableWidth <= 0) {
|
|
323
|
+
return { width: maxWidth, lines: [text] };
|
|
347
324
|
}
|
|
348
|
-
const words = text.split(" ");
|
|
349
|
-
const lines = [];
|
|
350
|
-
let currentLine = "";
|
|
351
325
|
const measureSpan = document.createElement("span");
|
|
352
326
|
measureSpan.style.cssText = `
|
|
353
327
|
position: absolute;
|
|
354
328
|
visibility: hidden;
|
|
329
|
+
pointer-events: none;
|
|
355
330
|
font-family: ${fontFamily};
|
|
356
331
|
font-size: ${fontSize}px;
|
|
357
332
|
font-weight: ${fontWeight};
|
|
@@ -359,28 +334,74 @@ function calculateAutoWidth({
|
|
|
359
334
|
white-space: nowrap;
|
|
360
335
|
`;
|
|
361
336
|
document.body.appendChild(measureSpan);
|
|
362
|
-
|
|
363
|
-
|
|
337
|
+
measureSpan.textContent = "M";
|
|
338
|
+
const charWidth = measureSpan.offsetWidth;
|
|
339
|
+
const words = text.split(/(\s+)/);
|
|
340
|
+
const lines = [];
|
|
341
|
+
let currentLine = "";
|
|
342
|
+
for (let i = 0; i < words.length; i++) {
|
|
343
|
+
const word = words[i];
|
|
344
|
+
if (word === "\n" || word === "\r\n") {
|
|
345
|
+
if (currentLine) {
|
|
346
|
+
lines.push(currentLine);
|
|
347
|
+
}
|
|
348
|
+
currentLine = "";
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
364
351
|
if (!word) continue;
|
|
365
|
-
|
|
352
|
+
if (/^\s+$/.test(word)) {
|
|
353
|
+
if (currentLine) {
|
|
354
|
+
currentLine += word;
|
|
355
|
+
}
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
const testLine = currentLine + word;
|
|
366
359
|
measureSpan.textContent = testLine;
|
|
367
360
|
const testWidth = measureSpan.offsetWidth;
|
|
368
|
-
if (testWidth > availableWidth && currentLine) {
|
|
369
|
-
lines.push(currentLine);
|
|
361
|
+
if (testWidth > availableWidth && currentLine.trim()) {
|
|
362
|
+
lines.push(currentLine.trimEnd());
|
|
370
363
|
currentLine = word;
|
|
364
|
+
measureSpan.textContent = word;
|
|
365
|
+
const wordWidth = measureSpan.offsetWidth;
|
|
366
|
+
if (wordWidth > availableWidth) {
|
|
367
|
+
let remainingWord = word;
|
|
368
|
+
while (remainingWord) {
|
|
369
|
+
let breakPoint = 1;
|
|
370
|
+
for (let j = 1; j <= remainingWord.length; j++) {
|
|
371
|
+
measureSpan.textContent = remainingWord.substring(0, j);
|
|
372
|
+
if (measureSpan.offsetWidth > availableWidth) {
|
|
373
|
+
breakPoint = Math.max(1, j - 1);
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
breakPoint = j;
|
|
377
|
+
}
|
|
378
|
+
if (breakPoint >= remainingWord.length) {
|
|
379
|
+
currentLine = remainingWord;
|
|
380
|
+
break;
|
|
381
|
+
} else {
|
|
382
|
+
lines.push(remainingWord.substring(0, breakPoint));
|
|
383
|
+
remainingWord = remainingWord.substring(breakPoint);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
371
387
|
} else {
|
|
372
388
|
currentLine = testLine;
|
|
373
389
|
}
|
|
374
390
|
}
|
|
375
|
-
if (currentLine
|
|
391
|
+
if (currentLine.trim()) {
|
|
392
|
+
lines.push(currentLine.trimEnd());
|
|
393
|
+
}
|
|
394
|
+
if (lines.length === 0) {
|
|
395
|
+
lines.push("");
|
|
396
|
+
}
|
|
376
397
|
let widestLineWidth = 0;
|
|
377
398
|
for (const line of lines) {
|
|
378
399
|
measureSpan.textContent = line;
|
|
379
400
|
widestLineWidth = Math.max(widestLineWidth, measureSpan.offsetWidth);
|
|
380
401
|
}
|
|
381
402
|
document.body.removeChild(measureSpan);
|
|
382
|
-
|
|
383
|
-
return
|
|
403
|
+
const calculatedWidth = Math.min(widestLineWidth + paddingLeft + paddingRight, maxWidth);
|
|
404
|
+
return { width: calculatedWidth, lines };
|
|
384
405
|
}
|
|
385
406
|
function TextElement({ segment, scale = 1 }) {
|
|
386
407
|
const fontType = segment.fontType ?? TEXT_DEFAULTS.fontType;
|
|
@@ -409,9 +430,9 @@ function TextElement({ segment, scale = 1 }) {
|
|
|
409
430
|
const backgroundOpacity = segment.backgroundOpacity ?? TEXT_DEFAULTS.backgroundOpacity;
|
|
410
431
|
const backgroundBorderRadius = segment.backgroundBorderRadius;
|
|
411
432
|
const fontFamily = getFontFamily(fontType);
|
|
412
|
-
const
|
|
413
|
-
if (!autoWidth) return
|
|
414
|
-
return
|
|
433
|
+
const autoWidthResult = (0, import_react.useMemo)(() => {
|
|
434
|
+
if (!autoWidth) return null;
|
|
435
|
+
return calculateAutoWidthAndLines({
|
|
415
436
|
text: segment.text,
|
|
416
437
|
maxWidth: width,
|
|
417
438
|
paddingLeft,
|
|
@@ -423,6 +444,8 @@ function TextElement({ segment, scale = 1 }) {
|
|
|
423
444
|
lineHeight
|
|
424
445
|
});
|
|
425
446
|
}, [autoWidth, segment.text, width, paddingLeft, paddingRight, fontSize, fontWeight, fontFamily, letterSpacing, lineHeight]);
|
|
447
|
+
const calculatedWidth = autoWidthResult?.width ?? width;
|
|
448
|
+
const calculatedLines = autoWidthResult?.lines ?? [segment.text];
|
|
426
449
|
const borderRadiusStyle = (0, import_react.useMemo)(() => {
|
|
427
450
|
if (!backgroundBorderRadius) return void 0;
|
|
428
451
|
const radii = getBorderRadii(backgroundBorderRadius);
|
|
@@ -517,34 +540,26 @@ function TextElement({ segment, scale = 1 }) {
|
|
|
517
540
|
autoWidth,
|
|
518
541
|
verticalAlign
|
|
519
542
|
]);
|
|
520
|
-
if (autoWidth
|
|
521
|
-
const
|
|
522
|
-
|
|
523
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
543
|
+
if (autoWidth) {
|
|
544
|
+
const textContent = calculatedLines.map((line, index) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react.default.Fragment, { children: [
|
|
545
|
+
line,
|
|
546
|
+
index < calculatedLines.length - 1 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("br", {})
|
|
547
|
+
] }, index));
|
|
548
|
+
if (backgroundColor) {
|
|
549
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: positioningContainerStyle, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
524
550
|
"div",
|
|
525
551
|
{
|
|
526
552
|
style: {
|
|
527
|
-
position: "absolute",
|
|
528
|
-
left: bgOffsetX,
|
|
529
|
-
top: 0,
|
|
530
553
|
width: calculatedWidth,
|
|
531
|
-
|
|
554
|
+
maxWidth: width,
|
|
532
555
|
backgroundColor: hexToRgba(backgroundColor, backgroundOpacity),
|
|
533
|
-
borderRadius: borderRadiusStyle
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
}
|
|
556
|
+
borderRadius: borderRadiusStyle
|
|
557
|
+
},
|
|
558
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: textStyle, children: textContent })
|
|
537
559
|
}
|
|
538
|
-
)
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
}
|
|
542
|
-
if (autoWidth) {
|
|
543
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: positioningContainerStyle, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { width: calculatedWidth, maxWidth: width }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: {
|
|
544
|
-
...textStyle,
|
|
545
|
-
width,
|
|
546
|
-
boxSizing: "border-box"
|
|
547
|
-
}, children: segment.text }) }) });
|
|
560
|
+
) });
|
|
561
|
+
}
|
|
562
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: positioningContainerStyle, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { width: calculatedWidth, maxWidth: width }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: textStyle, children: textContent }) }) });
|
|
548
563
|
}
|
|
549
564
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: positioningContainerStyle, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: backgroundBoxStyle, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: textStyle, children: segment.text }) }) });
|
|
550
565
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -12,7 +12,7 @@ import { useMemo as useMemo3 } from "react";
|
|
|
12
12
|
import { AbsoluteFill, Img as Img2 } from "remotion";
|
|
13
13
|
|
|
14
14
|
// src/components/TextElement.tsx
|
|
15
|
-
import { useMemo } from "react";
|
|
15
|
+
import React, { useMemo } from "react";
|
|
16
16
|
|
|
17
17
|
// src/utils/defaults.ts
|
|
18
18
|
var TEXT_DEFAULTS = {
|
|
@@ -220,7 +220,7 @@ function hexToRgba(hex, opacity = 100) {
|
|
|
220
220
|
|
|
221
221
|
// src/components/TextElement.tsx
|
|
222
222
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
223
|
-
function
|
|
223
|
+
function calculateAutoWidthAndLines({
|
|
224
224
|
text,
|
|
225
225
|
maxWidth,
|
|
226
226
|
paddingLeft,
|
|
@@ -232,52 +232,17 @@ function calculateAutoWidth({
|
|
|
232
232
|
lineHeight
|
|
233
233
|
}) {
|
|
234
234
|
if (typeof document === "undefined") {
|
|
235
|
-
|
|
235
|
+
return { width: maxWidth, lines: [text] };
|
|
236
236
|
}
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
visibility: hidden;
|
|
241
|
-
pointer-events: none;
|
|
242
|
-
width: ${maxWidth}px;
|
|
243
|
-
padding: 0 ${paddingRight}px 0 ${paddingLeft}px;
|
|
244
|
-
box-sizing: border-box;
|
|
245
|
-
font-family: ${fontFamily};
|
|
246
|
-
font-size: ${fontSize}px;
|
|
247
|
-
font-weight: ${fontWeight};
|
|
248
|
-
letter-spacing: ${letterSpacing}px;
|
|
249
|
-
line-height: ${lineHeight};
|
|
250
|
-
white-space: pre-wrap;
|
|
251
|
-
word-break: break-word;
|
|
252
|
-
`;
|
|
253
|
-
container.textContent = text;
|
|
254
|
-
document.body.appendChild(container);
|
|
255
|
-
const totalHeight = container.offsetHeight;
|
|
256
|
-
const lineHeightPx = fontSize * lineHeight;
|
|
257
|
-
const numLines = Math.max(1, Math.round(totalHeight / lineHeightPx));
|
|
258
|
-
if (numLines === 1) {
|
|
259
|
-
const span = document.createElement("span");
|
|
260
|
-
span.style.cssText = `
|
|
261
|
-
font-family: ${fontFamily};
|
|
262
|
-
font-size: ${fontSize}px;
|
|
263
|
-
font-weight: ${fontWeight};
|
|
264
|
-
letter-spacing: ${letterSpacing}px;
|
|
265
|
-
white-space: nowrap;
|
|
266
|
-
`;
|
|
267
|
-
span.textContent = text;
|
|
268
|
-
document.body.appendChild(span);
|
|
269
|
-
const textWidth = span.offsetWidth;
|
|
270
|
-
document.body.removeChild(span);
|
|
271
|
-
document.body.removeChild(container);
|
|
272
|
-
return Math.min(textWidth + paddingLeft + paddingRight, maxWidth);
|
|
237
|
+
const availableWidth = maxWidth - paddingLeft - paddingRight;
|
|
238
|
+
if (availableWidth <= 0) {
|
|
239
|
+
return { width: maxWidth, lines: [text] };
|
|
273
240
|
}
|
|
274
|
-
const words = text.split(" ");
|
|
275
|
-
const lines = [];
|
|
276
|
-
let currentLine = "";
|
|
277
241
|
const measureSpan = document.createElement("span");
|
|
278
242
|
measureSpan.style.cssText = `
|
|
279
243
|
position: absolute;
|
|
280
244
|
visibility: hidden;
|
|
245
|
+
pointer-events: none;
|
|
281
246
|
font-family: ${fontFamily};
|
|
282
247
|
font-size: ${fontSize}px;
|
|
283
248
|
font-weight: ${fontWeight};
|
|
@@ -285,28 +250,74 @@ function calculateAutoWidth({
|
|
|
285
250
|
white-space: nowrap;
|
|
286
251
|
`;
|
|
287
252
|
document.body.appendChild(measureSpan);
|
|
288
|
-
|
|
289
|
-
|
|
253
|
+
measureSpan.textContent = "M";
|
|
254
|
+
const charWidth = measureSpan.offsetWidth;
|
|
255
|
+
const words = text.split(/(\s+)/);
|
|
256
|
+
const lines = [];
|
|
257
|
+
let currentLine = "";
|
|
258
|
+
for (let i = 0; i < words.length; i++) {
|
|
259
|
+
const word = words[i];
|
|
260
|
+
if (word === "\n" || word === "\r\n") {
|
|
261
|
+
if (currentLine) {
|
|
262
|
+
lines.push(currentLine);
|
|
263
|
+
}
|
|
264
|
+
currentLine = "";
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
290
267
|
if (!word) continue;
|
|
291
|
-
|
|
268
|
+
if (/^\s+$/.test(word)) {
|
|
269
|
+
if (currentLine) {
|
|
270
|
+
currentLine += word;
|
|
271
|
+
}
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
const testLine = currentLine + word;
|
|
292
275
|
measureSpan.textContent = testLine;
|
|
293
276
|
const testWidth = measureSpan.offsetWidth;
|
|
294
|
-
if (testWidth > availableWidth && currentLine) {
|
|
295
|
-
lines.push(currentLine);
|
|
277
|
+
if (testWidth > availableWidth && currentLine.trim()) {
|
|
278
|
+
lines.push(currentLine.trimEnd());
|
|
296
279
|
currentLine = word;
|
|
280
|
+
measureSpan.textContent = word;
|
|
281
|
+
const wordWidth = measureSpan.offsetWidth;
|
|
282
|
+
if (wordWidth > availableWidth) {
|
|
283
|
+
let remainingWord = word;
|
|
284
|
+
while (remainingWord) {
|
|
285
|
+
let breakPoint = 1;
|
|
286
|
+
for (let j = 1; j <= remainingWord.length; j++) {
|
|
287
|
+
measureSpan.textContent = remainingWord.substring(0, j);
|
|
288
|
+
if (measureSpan.offsetWidth > availableWidth) {
|
|
289
|
+
breakPoint = Math.max(1, j - 1);
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
breakPoint = j;
|
|
293
|
+
}
|
|
294
|
+
if (breakPoint >= remainingWord.length) {
|
|
295
|
+
currentLine = remainingWord;
|
|
296
|
+
break;
|
|
297
|
+
} else {
|
|
298
|
+
lines.push(remainingWord.substring(0, breakPoint));
|
|
299
|
+
remainingWord = remainingWord.substring(breakPoint);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
297
303
|
} else {
|
|
298
304
|
currentLine = testLine;
|
|
299
305
|
}
|
|
300
306
|
}
|
|
301
|
-
if (currentLine
|
|
307
|
+
if (currentLine.trim()) {
|
|
308
|
+
lines.push(currentLine.trimEnd());
|
|
309
|
+
}
|
|
310
|
+
if (lines.length === 0) {
|
|
311
|
+
lines.push("");
|
|
312
|
+
}
|
|
302
313
|
let widestLineWidth = 0;
|
|
303
314
|
for (const line of lines) {
|
|
304
315
|
measureSpan.textContent = line;
|
|
305
316
|
widestLineWidth = Math.max(widestLineWidth, measureSpan.offsetWidth);
|
|
306
317
|
}
|
|
307
318
|
document.body.removeChild(measureSpan);
|
|
308
|
-
|
|
309
|
-
return
|
|
319
|
+
const calculatedWidth = Math.min(widestLineWidth + paddingLeft + paddingRight, maxWidth);
|
|
320
|
+
return { width: calculatedWidth, lines };
|
|
310
321
|
}
|
|
311
322
|
function TextElement({ segment, scale = 1 }) {
|
|
312
323
|
const fontType = segment.fontType ?? TEXT_DEFAULTS.fontType;
|
|
@@ -335,9 +346,9 @@ function TextElement({ segment, scale = 1 }) {
|
|
|
335
346
|
const backgroundOpacity = segment.backgroundOpacity ?? TEXT_DEFAULTS.backgroundOpacity;
|
|
336
347
|
const backgroundBorderRadius = segment.backgroundBorderRadius;
|
|
337
348
|
const fontFamily = getFontFamily(fontType);
|
|
338
|
-
const
|
|
339
|
-
if (!autoWidth) return
|
|
340
|
-
return
|
|
349
|
+
const autoWidthResult = useMemo(() => {
|
|
350
|
+
if (!autoWidth) return null;
|
|
351
|
+
return calculateAutoWidthAndLines({
|
|
341
352
|
text: segment.text,
|
|
342
353
|
maxWidth: width,
|
|
343
354
|
paddingLeft,
|
|
@@ -349,6 +360,8 @@ function TextElement({ segment, scale = 1 }) {
|
|
|
349
360
|
lineHeight
|
|
350
361
|
});
|
|
351
362
|
}, [autoWidth, segment.text, width, paddingLeft, paddingRight, fontSize, fontWeight, fontFamily, letterSpacing, lineHeight]);
|
|
363
|
+
const calculatedWidth = autoWidthResult?.width ?? width;
|
|
364
|
+
const calculatedLines = autoWidthResult?.lines ?? [segment.text];
|
|
352
365
|
const borderRadiusStyle = useMemo(() => {
|
|
353
366
|
if (!backgroundBorderRadius) return void 0;
|
|
354
367
|
const radii = getBorderRadii(backgroundBorderRadius);
|
|
@@ -443,34 +456,26 @@ function TextElement({ segment, scale = 1 }) {
|
|
|
443
456
|
autoWidth,
|
|
444
457
|
verticalAlign
|
|
445
458
|
]);
|
|
446
|
-
if (autoWidth
|
|
447
|
-
const
|
|
448
|
-
|
|
449
|
-
/* @__PURE__ */ jsx(
|
|
459
|
+
if (autoWidth) {
|
|
460
|
+
const textContent = calculatedLines.map((line, index) => /* @__PURE__ */ jsxs(React.Fragment, { children: [
|
|
461
|
+
line,
|
|
462
|
+
index < calculatedLines.length - 1 && /* @__PURE__ */ jsx("br", {})
|
|
463
|
+
] }, index));
|
|
464
|
+
if (backgroundColor) {
|
|
465
|
+
return /* @__PURE__ */ jsx("div", { style: positioningContainerStyle, children: /* @__PURE__ */ jsx(
|
|
450
466
|
"div",
|
|
451
467
|
{
|
|
452
468
|
style: {
|
|
453
|
-
position: "absolute",
|
|
454
|
-
left: bgOffsetX,
|
|
455
|
-
top: 0,
|
|
456
469
|
width: calculatedWidth,
|
|
457
|
-
|
|
470
|
+
maxWidth: width,
|
|
458
471
|
backgroundColor: hexToRgba(backgroundColor, backgroundOpacity),
|
|
459
|
-
borderRadius: borderRadiusStyle
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
}
|
|
472
|
+
borderRadius: borderRadiusStyle
|
|
473
|
+
},
|
|
474
|
+
children: /* @__PURE__ */ jsx("div", { style: textStyle, children: textContent })
|
|
463
475
|
}
|
|
464
|
-
)
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
}
|
|
468
|
-
if (autoWidth) {
|
|
469
|
-
return /* @__PURE__ */ jsx("div", { style: positioningContainerStyle, children: /* @__PURE__ */ jsx("div", { style: { width: calculatedWidth, maxWidth: width }, children: /* @__PURE__ */ jsx("div", { style: {
|
|
470
|
-
...textStyle,
|
|
471
|
-
width,
|
|
472
|
-
boxSizing: "border-box"
|
|
473
|
-
}, children: segment.text }) }) });
|
|
476
|
+
) });
|
|
477
|
+
}
|
|
478
|
+
return /* @__PURE__ */ jsx("div", { style: positioningContainerStyle, children: /* @__PURE__ */ jsx("div", { style: { width: calculatedWidth, maxWidth: width }, children: /* @__PURE__ */ jsx("div", { style: textStyle, children: textContent }) }) });
|
|
474
479
|
}
|
|
475
480
|
return /* @__PURE__ */ jsx("div", { style: positioningContainerStyle, children: /* @__PURE__ */ jsx("div", { style: backgroundBoxStyle, children: /* @__PURE__ */ jsx("div", { style: textStyle, children: segment.text }) }) });
|
|
476
481
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ugcinc-render",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.7",
|
|
4
4
|
"description": "Unified rendering package for UGC Inc - shared types, components, and compositions for pixel-perfect client/server rendering",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|