@git-diff-view/react 0.0.19 → 0.0.21

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.
@@ -1,3 +1,4 @@
1
+ 'use client';
1
2
  'use strict';
2
3
 
3
4
  var core = require('@git-diff-view/core');
@@ -84,6 +85,12 @@ const useSafeLayout = isClient ? React.useLayoutEffect : React.useEffect;
84
85
 
85
86
  var _TextMeasure_instances, _TextMeasure_key, _TextMeasure_map, _TextMeasure_getInstance;
86
87
  let canvasCtx = null;
88
+ const getKey = (font, text) => {
89
+ return `${font.fontFamily}-${font.fontStyle}-${font.fontSize}-${text}`;
90
+ };
91
+ const getStableKey = (font, text) => {
92
+ return getKey(font, "0".repeat(text.length));
93
+ };
87
94
  class TextMeasure {
88
95
  constructor() {
89
96
  _TextMeasure_instances.add(this);
@@ -91,7 +98,7 @@ class TextMeasure {
91
98
  _TextMeasure_map.set(this, {});
92
99
  }
93
100
  measure(text, font) {
94
- const currentKey = `${font === null || font === void 0 ? void 0 : font.fontFamily}-${font === null || font === void 0 ? void 0 : font.fontStyle}-${font === null || font === void 0 ? void 0 : font.fontSize}-${text}`;
101
+ const currentKey = getStableKey(font, text);
95
102
  if (__classPrivateFieldGet(this, _TextMeasure_map, "f")[currentKey]) {
96
103
  return __classPrivateFieldGet(this, _TextMeasure_map, "f")[currentKey];
97
104
  }
@@ -116,7 +123,12 @@ _TextMeasure_key = new WeakMap(), _TextMeasure_map = new WeakMap(), _TextMeasure
116
123
  };
117
124
  const measureInstance = new TextMeasure();
118
125
  const useTextWidth = ({ text, font, }) => {
119
- const [width, setWidth] = React.useState(0);
126
+ const [width, setWidth] = React.useState(() => {
127
+ const fontSize = parseInt(font.fontSize);
128
+ let baseSize = 6;
129
+ baseSize += fontSize > 10 ? (fontSize - 10) * 0.6 : 0;
130
+ return baseSize * text.length;
131
+ });
120
132
  useSafeLayout(() => {
121
133
  const width = measureInstance.measure(text, font);
122
134
  setWidth(width);
@@ -363,6 +375,18 @@ const getStyleObjectFromString = memoFunc((str) => {
363
375
  });
364
376
  return style;
365
377
  });
378
+ const getSymbol = (symbol) => {
379
+ switch (symbol) {
380
+ case core.NewLineSymbol.LF:
381
+ return "␊";
382
+ case core.NewLineSymbol.CR:
383
+ return "␍";
384
+ case core.NewLineSymbol.CRLF:
385
+ return "␍␊";
386
+ default:
387
+ return "";
388
+ }
389
+ };
366
390
  const DiffString = ({ rawLine, diffLine, operator, enableWrap, }) => {
367
391
  const changes = diffLine === null || diffLine === void 0 ? void 0 : diffLine.changes;
368
392
  if (changes === null || changes === void 0 ? void 0 : changes.hasLineChange) {
@@ -371,24 +395,16 @@ const DiffString = ({ rawLine, diffLine, operator, enableWrap, }) => {
371
395
  const str2 = rawLine.slice(range.location, range.location + range.length);
372
396
  const str3 = rawLine.slice(range.location + range.length);
373
397
  const isLast = str2.includes("\n");
374
- const _str2 = isLast ? str2.replace("\n", "") : str2;
398
+ const _str2 = isLast ? str2.replace("\n", "").replace("\r", "") : str2;
375
399
  const isNewLineSymbolChanged = changes.newLineSymbol;
376
400
  return (React__namespace.createElement("span", { className: "diff-line-content-raw" },
377
401
  React__namespace.createElement("span", { "data-range-start": range.location, "data-range-end": range.location + range.length },
378
402
  str1,
379
403
  React__namespace.createElement("span", { "data-diff-highlight": true, className: "rounded-[0.2em]", style: {
380
404
  backgroundColor: operator === "add" ? `var(${addContentHighlightBGName})` : `var(${delContentHighlightBGName})`,
381
- } }, isLast
382
- ? `${_str2}${isNewLineSymbolChanged === core.NewLineSymbol.LF
383
- ? "␊"
384
- : isNewLineSymbolChanged === core.NewLineSymbol.CR
385
- ? "␍"
386
- : isNewLineSymbolChanged === core.NewLineSymbol.CRLF
387
- ? "␍␊"
388
- : ""}`
389
- : str2),
405
+ } }, isLast ? `${_str2}${getSymbol(isNewLineSymbolChanged)}` : str2),
390
406
  str3),
391
- isNewLineSymbolChanged === core.NewLineSymbol.NEWLINE && (React__namespace.createElement("span", { "data-no-newline-at-end-of-file-symbol": true, className: enableWrap ? "block text-red-500" : "inline-block align-middle text-red-500", style: {
407
+ isNewLineSymbolChanged === core.NewLineSymbol.NEWLINE && (React__namespace.createElement("span", { "data-no-newline-at-end-of-file-symbol": true, className: enableWrap ? "block !text-red-500" : "inline-block align-middle !text-red-500", style: {
392
408
  width: `var(${diffFontSizeName})`,
393
409
  height: `var(${diffFontSizeName})`,
394
410
  } },
@@ -420,7 +436,7 @@ const DiffSyntax = ({ rawLine, diffLine, operator, syntaxLine, enableWrap, }) =>
420
436
  const isStart = str1.length || range.location === node.startIndex;
421
437
  const isEnd = str3.length || node.endIndex === range.location + range.length - 1;
422
438
  const isLast = str2.includes("\n");
423
- const _str2 = isLast ? str2.replace("\n", "") : str2;
439
+ const _str2 = isLast ? str2.replace("\n", "").replace("\r", "") : str2;
424
440
  return (React__namespace.createElement("span", { key: index, "data-start": node.startIndex, "data-end": node.endIndex, className: (_e = (_d = wrapper === null || wrapper === void 0 ? void 0 : wrapper.properties) === null || _d === void 0 ? void 0 : _d.className) === null || _e === void 0 ? void 0 : _e.join(" "), style: getStyleObjectFromString(((_f = wrapper === null || wrapper === void 0 ? void 0 : wrapper.properties) === null || _f === void 0 ? void 0 : _f.style) || "") },
425
441
  str1,
426
442
  React__namespace.createElement("span", { "data-diff-highlight": true, style: {
@@ -429,19 +445,11 @@ const DiffSyntax = ({ rawLine, diffLine, operator, syntaxLine, enableWrap, }) =>
429
445
  borderBottomLeftRadius: isStart ? "0.2em" : undefined,
430
446
  borderTopRightRadius: isEnd || isLast ? "0.2em" : undefined,
431
447
  borderBottomRightRadius: isEnd || isLast ? "0.2em" : undefined,
432
- } }, isLast
433
- ? `${_str2}${isNewLineSymbolChanged === core.NewLineSymbol.LF
434
- ? "␊"
435
- : isNewLineSymbolChanged === core.NewLineSymbol.CR
436
- ? "␍"
437
- : isNewLineSymbolChanged === core.NewLineSymbol.CRLF
438
- ? "␍␊"
439
- : ""}`
440
- : str2),
448
+ } }, isLast ? `${_str2}${getSymbol(isNewLineSymbolChanged)}` : str2),
441
449
  str3));
442
450
  }
443
451
  })),
444
- isNewLineSymbolChanged === core.NewLineSymbol.NEWLINE && (React__namespace.createElement("span", { "data-no-newline-at-end-of-file-symbol": true, className: enableWrap ? "block text-red-500" : "inline-block align-middle text-red-500", style: {
452
+ isNewLineSymbolChanged === core.NewLineSymbol.NEWLINE && (React__namespace.createElement("span", { "data-no-newline-at-end-of-file-symbol": true, className: enableWrap ? "block !text-red-500" : "inline-block align-middle !text-red-500", style: {
445
453
  width: `var(${diffFontSizeName})`,
446
454
  height: `var(${diffFontSizeName})`,
447
455
  } },
@@ -809,6 +817,7 @@ const _DiffSplitWidgetLine$1 = ({ diffFile, side, lineNumber, currentLine, setWi
809
817
  React__namespace.createElement("div", { "data-line": `${lineNumber}-widget-content`, "data-side": exports.SplitSide[side], className: "diff-line-widget-wrapper sticky left-0", style: { width } }, width > 0 && currentWidgetRendered))) : (React__namespace.createElement("td", { className: `diff-line-widget-${exports.SplitSide[side]}-placeholder p-0`, style: { backgroundColor: `var(${emptyBGName})` }, colSpan: 2 },
810
818
  React__namespace.createElement("div", { "data-line": `${lineNumber}-widget-content`, "data-side": exports.SplitSide[side] })))));
811
819
  };
820
+ // TODO! improve performance
812
821
  const DiffSplitWidgetLine$1 = ({ index, diffFile, side, lineNumber, }) => {
813
822
  const { useWidget } = useDiffWidgetContext();
814
823
  const { widgetLineNumber, widgetSide, setWidget } = useWidget.useShallowStableSelector((s) => ({
@@ -866,7 +875,7 @@ const DiffSplitViewNormal = React.memo(({ diffFile }) => {
866
875
  const splitLineLength = Math.max(diffFile.splitLineLength, diffFile.fileLineLength);
867
876
  const { useDiffContext } = useDiffViewContext();
868
877
  const fontSize = useDiffContext.useShallowStableSelector((s) => s.fontSize);
869
- index_js.useSyncExternalStore(diffFile.subscribe, diffFile.getUpdateCount);
878
+ index_js.useSyncExternalStore(diffFile.subscribe, diffFile.getUpdateCount, diffFile.getUpdateCount);
870
879
  React.useEffect(() => {
871
880
  const left = ref1.current;
872
881
  const right = ref2.current;
@@ -1117,6 +1126,7 @@ const _DiffSplitWidgetLine = ({ index, diffFile, lineNumber, oldLineWidget, newL
1117
1126
  newWidgetRendered ? (React__namespace.createElement("td", { className: "diff-line-widget-new-content border-l-[1px] p-0", colSpan: 2, style: { borderLeftColor: `var(${borderColorName})` } },
1118
1127
  React__namespace.createElement("div", { className: "diff-line-widget-wrapper" }, newWidgetRendered))) : (React__namespace.createElement("td", { className: "diff-line-widget-new-placeholder select-none border-l-[1px] p-0", style: { backgroundColor: `var(${emptyBGName})`, borderLeftColor: `var(${borderColorName})` }, colSpan: 2 }, oldWidgetRendered && React__namespace.createElement("span", null, "\u2002")))));
1119
1128
  };
1129
+ // TODO! improve performance
1120
1130
  const DiffSplitWidgetLine = ({ index, diffFile, lineNumber, }) => {
1121
1131
  const { useWidget } = useDiffWidgetContext();
1122
1132
  const { widgetLineNumber, widgetSide } = useWidget((s) => ({
@@ -1147,7 +1157,7 @@ const DiffSplitViewWrap = React.memo(({ diffFile }) => {
1147
1157
  const { useDiffContext } = useDiffViewContext();
1148
1158
  const useSplitConfig = React.useMemo(() => createDiffSplitConfigStore(), []);
1149
1159
  const fontSize = useDiffContext.useShallowStableSelector((s) => s.fontSize);
1150
- index_js.useSyncExternalStore(diffFile.subscribe, diffFile.getUpdateCount);
1160
+ index_js.useSyncExternalStore(diffFile.subscribe, diffFile.getUpdateCount, diffFile.getUpdateCount);
1151
1161
  const onMouseDown = React.useCallback((e) => {
1152
1162
  let ele = e.target;
1153
1163
  const setSelectSide = useSplitConfig.getReadonlyState().setSplit;
@@ -1466,7 +1476,7 @@ const DiffUnifiedView = React.memo(({ diffFile }) => {
1466
1476
  fontSize: s.fontSize,
1467
1477
  enableWrap: s.enableWrap,
1468
1478
  }));
1469
- index_js.useSyncExternalStore(diffFile.subscribe, diffFile.getUpdateCount);
1479
+ index_js.useSyncExternalStore(diffFile.subscribe, diffFile.getUpdateCount, diffFile.getUpdateCount);
1470
1480
  React.useEffect(() => {
1471
1481
  const { setWidget } = useWidget.getReadonlyState();
1472
1482
  setWidget({});
@@ -1553,12 +1563,12 @@ const _InternalDiffView = (props) => {
1553
1563
  }, [diffFile]);
1554
1564
  const value = React.useMemo(() => ({ useDiffContext }), [useDiffContext]);
1555
1565
  return (React__namespace.createElement(DiffViewContext.Provider, { value: value },
1556
- React__namespace.createElement("div", { className: "diff-tailwindcss-wrapper", "data-component": "git-diff-view", "data-theme": diffFile._getTheme() || "light", "data-version": "0.0.19", "data-highlighter": diffFile._getHighlighterName(), ref: wrapperRef },
1566
+ React__namespace.createElement("div", { className: "diff-tailwindcss-wrapper", "data-component": "git-diff-view", "data-theme": diffFile._getTheme() || "light", "data-version": "0.0.21", "data-highlighter": diffFile._getHighlighterName(), ref: wrapperRef },
1557
1567
  React__namespace.createElement("div", { className: "diff-style-root", style: {
1558
1568
  // @ts-ignore
1559
1569
  [diffFontSizeName]: diffViewFontSize + "px",
1560
1570
  } },
1561
- React__namespace.createElement("div", { id: `diff-root${diffFileId}`, className: "diff-view-wrapper" + (className ? ` ${className}` : ""), style: style }, diffViewMode & exports.DiffModeEnum.Split ? (React__namespace.createElement(DiffSplitView, { diffFile: diffFile })) : (React__namespace.createElement(DiffUnifiedView, { diffFile: diffFile })))))));
1571
+ React__namespace.createElement("div", { suppressHydrationWarning: true, id: `diff-root${diffFileId}`, className: "diff-view-wrapper" + (className ? ` ${className}` : ""), style: style }, diffViewMode & exports.DiffModeEnum.Split ? (React__namespace.createElement(DiffSplitView, { diffFile: diffFile })) : (React__namespace.createElement(DiffUnifiedView, { diffFile: diffFile })))))));
1562
1572
  };
1563
1573
  const InternalDiffView = React.memo(_InternalDiffView);
1564
1574
  const DiffViewWithRef = (props, ref) => {
@@ -1615,9 +1625,12 @@ const DiffViewWithRef = (props, ref) => {
1615
1625
  return null;
1616
1626
  return (React__namespace.createElement(InternalDiffView, Object.assign({ key: diffFile.getId() }, restProps, { diffFile: diffFile, diffViewMode: restProps.diffViewMode || exports.DiffModeEnum.SplitGitHub, diffViewFontSize: restProps.diffViewFontSize || 14 })));
1617
1627
  };
1618
- const DiffView = React.forwardRef(DiffViewWithRef);
1628
+ const InnerDiffView = React.forwardRef(DiffViewWithRef);
1629
+ function DiffView(props) {
1630
+ return React__namespace.createElement(InnerDiffView, Object.assign({}, props));
1631
+ }
1619
1632
  DiffView.displayName = "DiffView";
1620
- const version = "0.0.19";
1633
+ const version = "0.0.21";
1621
1634
 
1622
1635
  exports.DiffView = DiffView;
1623
1636
  exports.DiffViewContext = DiffViewContext;