@particle-academy/fancy-code 0.4.0 → 0.4.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/dist/index.js CHANGED
@@ -83,7 +83,7 @@ function CodeEditorPanel({ className }) {
83
83
  children: lineNumberElements
84
84
  }
85
85
  ),
86
- /* @__PURE__ */ jsxs("div", { className: "relative min-w-0 flex-1", children: [
86
+ /* @__PURE__ */ jsxs("div", { className: cn("relative flex-1", wordWrap && "min-w-0"), children: [
87
87
  /* @__PURE__ */ jsx(
88
88
  "pre",
89
89
  {
@@ -108,10 +108,14 @@ function CodeEditorPanel({ className }) {
108
108
  "textarea",
109
109
  {
110
110
  ref: textareaRef,
111
- className: "relative m-0 block w-full resize-none overflow-hidden border-none bg-transparent p-2.5 text-[13px] leading-[1.5] text-transparent outline-none",
111
+ className: cn(
112
+ "relative m-0 block resize-none border-none bg-transparent p-2.5 text-[13px] leading-[1.5] text-transparent outline-none",
113
+ wordWrap ? "w-full" : "min-w-full"
114
+ ),
112
115
  style: {
113
116
  caretColor: themeColors.cursorColor,
114
117
  minHeight: _minHeight ? _minHeight - 40 : 80,
118
+ overflow: "hidden",
115
119
  whiteSpace: wordWrap ? "pre-wrap" : "pre",
116
120
  overflowWrap: wordWrap ? "break-word" : "normal"
117
121
  },
@@ -1292,6 +1296,7 @@ function useEditorEngine({
1292
1296
  language,
1293
1297
  theme,
1294
1298
  readOnly,
1299
+ wordWrap,
1295
1300
  tabSize,
1296
1301
  onCursorChange
1297
1302
  }) {
@@ -1325,14 +1330,29 @@ function useEditorEngine({
1325
1330
  }
1326
1331
  return count;
1327
1332
  }, [value]);
1333
+ const autoResize = useCallback(() => {
1334
+ const ta = textareaRef.current;
1335
+ if (!ta) return;
1336
+ ta.style.height = "auto";
1337
+ ta.style.height = ta.scrollHeight + "px";
1338
+ if (!wordWrap) {
1339
+ ta.style.width = "0";
1340
+ ta.style.width = ta.scrollWidth + "px";
1341
+ } else {
1342
+ ta.style.width = "";
1343
+ }
1344
+ }, [wordWrap]);
1328
1345
  useEffect(() => {
1329
1346
  const ta = textareaRef.current;
1330
- if (!ta || ta.value === value) return;
1331
- const { selectionStart, selectionEnd } = ta;
1332
- ta.value = value;
1333
- ta.selectionStart = selectionStart;
1334
- ta.selectionEnd = selectionEnd;
1335
- }, [value]);
1347
+ if (!ta) return;
1348
+ if (ta.value !== value) {
1349
+ const { selectionStart, selectionEnd } = ta;
1350
+ ta.value = value;
1351
+ ta.selectionStart = selectionStart;
1352
+ ta.selectionEnd = selectionEnd;
1353
+ }
1354
+ autoResize();
1355
+ }, [value, autoResize]);
1336
1356
  const updateCursorInfo = useCallback(() => {
1337
1357
  const ta = textareaRef.current;
1338
1358
  if (!ta) return;
@@ -1349,8 +1369,9 @@ function useEditorEngine({
1349
1369
  const ta = textareaRef.current;
1350
1370
  if (!ta) return;
1351
1371
  onChangeRef.current?.(ta.value);
1372
+ autoResize();
1352
1373
  updateCursorInfo();
1353
- }, [updateCursorInfo]);
1374
+ }, [autoResize, updateCursorInfo]);
1354
1375
  const handleSelect = useCallback(() => {
1355
1376
  updateCursorInfo();
1356
1377
  }, [updateCursorInfo]);
@@ -1410,6 +1431,7 @@ function useEditorEngine({
1410
1431
  ta.selectionStart = ta.selectionEnd = start + tabSize;
1411
1432
  onChangeRef.current?.(ta.value);
1412
1433
  }
1434
+ autoResize();
1413
1435
  updateCursorInfo();
1414
1436
  return;
1415
1437
  }
@@ -1428,11 +1450,12 @@ function useEditorEngine({
1428
1450
  ta.value = before + insertion + after;
1429
1451
  ta.selectionStart = ta.selectionEnd = start + insertion.length;
1430
1452
  onChangeRef.current?.(ta.value);
1453
+ autoResize();
1431
1454
  updateCursorInfo();
1432
1455
  return;
1433
1456
  }
1434
1457
  },
1435
- [readOnly, tabSize, updateCursorInfo]
1458
+ [readOnly, tabSize, autoResize, updateCursorInfo]
1436
1459
  );
1437
1460
  return {
1438
1461
  textareaRef,
@@ -1506,6 +1529,7 @@ function CodeEditorRoot({
1506
1529
  language: currentLanguage,
1507
1530
  theme: resolvedTheme,
1508
1531
  readOnly,
1532
+ wordWrap: isWordWrap,
1509
1533
  tabSize: tabSizeProp,
1510
1534
  onCursorChange: ({ line, col, selectionLength: sel }) => {
1511
1535
  setCursorPosition({ line, col });