@git-diff-view/react 0.0.30 → 0.0.32

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.
@@ -204,6 +204,16 @@ const syncScroll = (left, right) => {
204
204
  right.onscroll = null;
205
205
  };
206
206
  };
207
+ const getElementRoot = (element) => {
208
+ if (element) {
209
+ const root = element.getRootNode();
210
+ if (root instanceof ShadowRoot) {
211
+ return root;
212
+ }
213
+ return element.ownerDocument;
214
+ }
215
+ return document;
216
+ };
207
217
 
208
218
  const diffFontSizeName = "--diff-font-size--";
209
219
  const diffAsideWidthName = "--diff-aside-width--";
@@ -289,6 +299,7 @@ const DiffSplitAddWidget = ({ side, className, lineNumber, onWidgetClick, onOpen
289
299
  (className ? " " + className : ""), style: {
290
300
  width: `calc(var(${diffFontSizeName}) * 1.4)`,
291
301
  height: `calc(var(${diffFontSizeName}) * 1.4)`,
302
+ top: `calc(var(${diffFontSizeName}) * 0.1)`,
292
303
  } },
293
304
  React__namespace.createElement("button", { className: "diff-add-widget z-[1] flex h-full w-full origin-center cursor-pointer items-center justify-center rounded-md text-[1.2em]", style: {
294
305
  color: `var(${addWidgetColorName})`,
@@ -299,9 +310,10 @@ const DiffSplitAddWidget = ({ side, className, lineNumber, onWidgetClick, onOpen
299
310
  } }, "+")));
300
311
  };
301
312
  const DiffUnifiedAddWidget = ({ lineNumber, side, onWidgetClick, onOpenAddWidget, }) => {
302
- return (React__namespace.createElement("div", { className: "diff-add-widget-wrapper invisible absolute left-[100%] top-[1px] translate-x-[-50%] select-none transition-transform hover:scale-110 group-hover:visible", style: {
313
+ return (React__namespace.createElement("div", { className: "diff-add-widget-wrapper invisible absolute left-[100%] translate-x-[-50%] select-none transition-transform hover:scale-110 group-hover:visible", style: {
303
314
  width: `calc(var(${diffFontSizeName}) * 1.4)`,
304
315
  height: `calc(var(${diffFontSizeName}) * 1.4)`,
316
+ top: `calc(var(${diffFontSizeName}) * 0.1)`,
305
317
  } },
306
318
  React__namespace.createElement("button", { className: "diff-add-widget z-[1] flex h-full w-full origin-center cursor-pointer items-center justify-center rounded-md text-[1.2em]", style: {
307
319
  color: `var(${addWidgetColorName})`,
@@ -537,10 +549,15 @@ const DiffSplitContentLine$1 = ({ index, diffFile, lineNumber, side, enableAddWi
537
549
  const useDomWidth = ({ selector, enable }) => {
538
550
  const [width, setWidth] = React.useState(0);
539
551
  const { useDiffContext } = useDiffViewContext();
540
- const { id, mounted } = useDiffContext.useShallowStableSelector((s) => ({ id: s.id, mounted: s.mounted }));
552
+ const { id, mounted, dom } = useDiffContext.useShallowStableSelector((s) => ({
553
+ id: s.id,
554
+ mounted: s.mounted,
555
+ dom: s.dom,
556
+ }));
541
557
  React.useEffect(() => {
542
558
  if (enable) {
543
- const container = document.querySelector(`#diff-root${id}`);
559
+ const rootDocument = getElementRoot(dom);
560
+ const container = rootDocument.querySelector(`#diff-root${id}`);
544
561
  const wrapper = container === null || container === void 0 ? void 0 : container.querySelector(selector);
545
562
  if (!wrapper)
546
563
  return;
@@ -573,16 +590,21 @@ const useDomWidth = ({ selector, enable }) => {
573
590
  typedWrapper.setAttribute("data-observe", "height");
574
591
  return () => cleanCb();
575
592
  }
576
- }, [selector, enable, id, mounted]);
593
+ }, [selector, enable, id, mounted, dom]);
577
594
  return width;
578
595
  };
579
596
 
580
597
  const useSyncHeight = ({ selector, wrapper, side, enable, }) => {
581
598
  const { useDiffContext } = useDiffViewContext();
582
- const { id, mounted } = useDiffContext.useShallowStableSelector((s) => ({ id: s.id, mounted: s.mounted }));
599
+ const { id, mounted, dom } = useDiffContext.useShallowStableSelector((s) => ({
600
+ id: s.id,
601
+ mounted: s.mounted,
602
+ dom: s.dom,
603
+ }));
583
604
  React.useEffect(() => {
584
605
  if (enable) {
585
- const container = document.querySelector(`#diff-root${id}`);
606
+ const rootDocument = getElementRoot(dom);
607
+ const container = rootDocument.querySelector(`#diff-root${id}`);
586
608
  const elements = Array.from((container === null || container === void 0 ? void 0 : container.querySelectorAll(selector)) || []);
587
609
  const wrappers = wrapper ? Array.from((container === null || container === void 0 ? void 0 : container.querySelectorAll(wrapper)) || []) : elements;
588
610
  if (elements.length === 2 && wrappers.length === 2) {
@@ -627,7 +649,7 @@ const useSyncHeight = ({ selector, wrapper, side, enable, }) => {
627
649
  return () => cleanCb();
628
650
  }
629
651
  }
630
- }, [selector, enable, side, id, wrapper, mounted]);
652
+ }, [selector, enable, side, id, wrapper, mounted, dom]);
631
653
  };
632
654
 
633
655
  const InternalDiffSplitExtendLine$1 = ({ index, diffFile, oldLineExtend, newLineExtend, side, lineNumber, }) => {
@@ -866,7 +888,7 @@ const DiffSplitViewTable = ({ side, diffFile, enableAddWidget, enableHighlight,
866
888
  React__namespace.createElement("th", { scope: "col" },
867
889
  core.SplitSide[side],
868
890
  " line content"))),
869
- React__namespace.createElement("tbody", { className: "diff-table-body leading-[1.4]", onMouseDownCapture: onMouseDown },
891
+ React__namespace.createElement("tbody", { className: "diff-table-body leading-[1.6]", onMouseDownCapture: onMouseDown },
870
892
  lines.map((line) => (React__namespace.createElement(React.Fragment, { key: line.index },
871
893
  React__namespace.createElement(DiffSplitHunkLine$1, { index: line.index, side: side, lineNumber: line.lineNumber, diffFile: diffFile }),
872
894
  React__namespace.createElement(DiffSplitContentLine$1, { index: line.index, side: side, lineNumber: line.lineNumber, diffFile: diffFile, enableAddWidget: enableAddWidget, enableHighlight: enableHighlight }),
@@ -990,7 +1012,10 @@ const InternalDiffSplitLine = ({ index, diffFile, lineNumber, enableAddWidget, e
990
1012
  const newLineNumberBG = getLineNumberBG(newLineIsAdded, false, hasDiff);
991
1013
  return (React__namespace.createElement("tr", { "data-line": lineNumber, "data-state": hasDiff ? "diff" : "plain", className: "diff-line" },
992
1014
  hasOldLine ? (React__namespace.createElement(React__namespace.Fragment, null,
993
- React__namespace.createElement("td", { className: "diff-line-old-num group relative w-[1%] min-w-[40px] select-none pl-[10px] pr-[10px] text-right align-top", "data-side": core.SplitSide[core.SplitSide.old], style: { backgroundColor: oldLineNumberBG, color: `var(${plainLineNumberColorName})` } },
1015
+ React__namespace.createElement("td", { className: "diff-line-old-num group relative w-[1%] min-w-[40px] select-none pl-[10px] pr-[10px] text-right align-top", "data-side": core.SplitSide[core.SplitSide.old], style: {
1016
+ backgroundColor: oldLineNumberBG,
1017
+ color: `var(${hasDiff ? plainLineNumberColorName : expandLineNumberColorName})`,
1018
+ } },
994
1019
  hasDiff && enableAddWidget && (React__namespace.createElement(DiffSplitAddWidget, { index: index, lineNumber: oldLine.lineNumber, side: core.SplitSide.old, diffFile: diffFile, onWidgetClick: (...props) => { var _a; return (_a = onAddWidgetClick.current) === null || _a === void 0 ? void 0 : _a.call(onAddWidgetClick, ...props); }, className: "absolute left-[100%] z-[1] translate-x-[-50%]", onOpenAddWidget: (lineNumber, side) => setWidget({ lineNumber: lineNumber, side: side }) })),
995
1020
  React__namespace.createElement("span", { "data-line-num": oldLine.lineNumber, style: { opacity: hasChange ? undefined : 0.5 } }, oldLine.lineNumber)),
996
1021
  React__namespace.createElement("td", { className: "diff-line-old-content group relative pr-[10px] align-top", "data-side": core.SplitSide[core.SplitSide.old], style: { backgroundColor: oldLineContentBG } },
@@ -1300,7 +1325,7 @@ const DiffSplitViewWrap = React.memo(({ diffFile }) => {
1300
1325
  React__namespace.createElement("th", { scope: "col" }, "old line content"),
1301
1326
  React__namespace.createElement("th", { scope: "col" }, "new line number"),
1302
1327
  React__namespace.createElement("th", { scope: "col" }, "new line content"))),
1303
- React__namespace.createElement("tbody", { className: "diff-table-body leading-[1.4]", onMouseDownCapture: onMouseDown },
1328
+ React__namespace.createElement("tbody", { className: "diff-table-body leading-[1.6]", onMouseDownCapture: onMouseDown },
1304
1329
  lines.map((line) => (React__namespace.createElement(React.Fragment, { key: line.index },
1305
1330
  React__namespace.createElement(DiffSplitHunkLine, { index: line.index, lineNumber: line.lineNumber, diffFile: diffFile }),
1306
1331
  React__namespace.createElement(DiffSplitContentLine, { index: line.index, lineNumber: line.lineNumber, diffFile: diffFile, enableAddWidget: enableAddWidget, enableHighlight: enableHighlight }),
@@ -1313,6 +1338,8 @@ DiffSplitViewWrap.displayName = "DiffSplitViewWrap";
1313
1338
  const createDiffConfigStore = (props, diffFileId) => {
1314
1339
  return reactivityStore.createStore(() => {
1315
1340
  var _a, _b;
1341
+ const dom = reactivityStore.ref();
1342
+ const setDom = (_dom) => (dom.value = _dom);
1316
1343
  const id = reactivityStore.ref(diffFileId);
1317
1344
  const setId = (_id) => (id.value = _id);
1318
1345
  const mode = reactivityStore.ref(props.diffViewMode);
@@ -1365,6 +1392,8 @@ const createDiffConfigStore = (props, diffFileId) => {
1365
1392
  return {
1366
1393
  id,
1367
1394
  setId,
1395
+ dom,
1396
+ setDom,
1368
1397
  mode,
1369
1398
  setMode,
1370
1399
  mounted,
@@ -1520,20 +1549,21 @@ const DiffUnifiedContentLine = ({ index, diffFile, lineNumber, enableWrap, enabl
1520
1549
  return (React__namespace.createElement(_DiffUnifiedLine, { index: index, diffFile: diffFile, lineNumber: lineNumber, enableWrap: enableWrap, enableHighlight: enableHighlight, enableAddWidget: enableAddWidget }));
1521
1550
  };
1522
1551
 
1523
- const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, oldLineExtend, newLineExtend, }) => {
1552
+ const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, enableWrap, oldLineExtend, newLineExtend, }) => {
1524
1553
  const { useDiffContext } = useDiffViewContext();
1525
1554
  const renderExtendLine = useDiffContext.useShallowStableSelector((s) => s.renderExtendLine);
1526
1555
  const unifiedItem = diffFile.getUnifiedLine(index);
1556
+ // TODO use css variable to get width
1527
1557
  const width = useDomWidth({
1528
1558
  selector: ".unified-diff-table-wrapper",
1529
- enable: typeof renderExtendLine === "function",
1559
+ enable: typeof renderExtendLine === "function" && !enableWrap,
1530
1560
  });
1531
1561
  if (!renderExtendLine)
1532
1562
  return null;
1533
1563
  return (React__namespace.createElement("tr", { "data-line": `${lineNumber}-extend`, "data-state": "extend", className: "diff-line diff-line-extend" },
1534
1564
  React__namespace.createElement("td", { className: "diff-line-extend-content p-0 align-top", colSpan: 2 },
1535
1565
  React__namespace.createElement("div", { className: "diff-line-extend-wrapper sticky left-0 z-[1]", style: { width } },
1536
- width > 0 &&
1566
+ (enableWrap ? true : width > 0) &&
1537
1567
  (oldLineExtend === null || oldLineExtend === void 0 ? void 0 : oldLineExtend.data) !== undefined &&
1538
1568
  (oldLineExtend === null || oldLineExtend === void 0 ? void 0 : oldLineExtend.data) !== null &&
1539
1569
  (renderExtendLine === null || renderExtendLine === void 0 ? void 0 : renderExtendLine({
@@ -1543,7 +1573,7 @@ const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, oldLineExt
1543
1573
  data: oldLineExtend.data,
1544
1574
  onUpdate: diffFile.notifyAll,
1545
1575
  })),
1546
- width > 0 &&
1576
+ (enableWrap ? true : width > 0) &&
1547
1577
  (newLineExtend === null || newLineExtend === void 0 ? void 0 : newLineExtend.data) !== undefined &&
1548
1578
  (newLineExtend === null || newLineExtend === void 0 ? void 0 : newLineExtend.data) !== null &&
1549
1579
  (renderExtendLine === null || renderExtendLine === void 0 ? void 0 : renderExtendLine({
@@ -1554,7 +1584,7 @@ const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, oldLineExt
1554
1584
  onUpdate: diffFile.notifyAll,
1555
1585
  }))))));
1556
1586
  };
1557
- const DiffUnifiedExtendLine = ({ index, diffFile, lineNumber, }) => {
1587
+ const DiffUnifiedExtendLine = ({ index, diffFile, lineNumber, enableWrap, }) => {
1558
1588
  const { useDiffContext } = useDiffViewContext();
1559
1589
  const unifiedItem = diffFile.getUnifiedLine(index);
1560
1590
  const { oldLineExtend, newLineExtend } = useDiffContext(React.useCallback((s) => {
@@ -1567,7 +1597,7 @@ const DiffUnifiedExtendLine = ({ index, diffFile, lineNumber, }) => {
1567
1597
  const hasExtend = (oldLineExtend === null || oldLineExtend === void 0 ? void 0 : oldLineExtend.data) || (newLineExtend === null || newLineExtend === void 0 ? void 0 : newLineExtend.data);
1568
1598
  if (!hasExtend || !unifiedItem || unifiedItem.isHidden)
1569
1599
  return null;
1570
- return (React__namespace.createElement(InternalDiffUnifiedExtendLine, { index: index, diffFile: diffFile, lineNumber: lineNumber, oldLineExtend: oldLineExtend, newLineExtend: newLineExtend }));
1600
+ return (React__namespace.createElement(InternalDiffUnifiedExtendLine, { index: index, diffFile: diffFile, lineNumber: lineNumber, enableWrap: enableWrap, oldLineExtend: oldLineExtend, newLineExtend: newLineExtend }));
1571
1601
  };
1572
1602
 
1573
1603
  const InternalDiffUnifiedHunkLine = ({ index, diffFile, lineNumber, }) => {
@@ -1615,7 +1645,7 @@ const DiffUnifiedHunkLine = ({ index, diffFile, lineNumber, }) => {
1615
1645
  return React__namespace.createElement(InternalDiffUnifiedHunkLine, { index: index, diffFile: diffFile, lineNumber: lineNumber });
1616
1646
  };
1617
1647
 
1618
- const InternalDiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1648
+ const InternalDiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, enableWrap, }) => {
1619
1649
  const { useWidget } = useDiffWidgetContext();
1620
1650
  const setWidget = useWidget.getReadonlyState().setWidget;
1621
1651
  const unifiedItem = diffFile.getUnifiedLine(index);
@@ -1629,21 +1659,21 @@ const InternalDiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1629
1659
  const renderWidgetLine = useDiffContext.useShallowStableSelector((s) => s.renderWidgetLine);
1630
1660
  const width = useDomWidth({
1631
1661
  selector: ".unified-diff-table-wrapper",
1632
- enable: typeof renderWidgetLine === "function",
1662
+ enable: typeof renderWidgetLine === "function" && !enableWrap,
1633
1663
  });
1634
1664
  if (!renderWidgetLine)
1635
1665
  return null;
1636
1666
  return (React__namespace.createElement("tr", { "data-line": `${lineNumber}-widget`, "data-state": "widget", className: "diff-line diff-line-widget" },
1637
1667
  React__namespace.createElement("td", { className: "diff-line-widget-content p-0", colSpan: 2 },
1638
1668
  React__namespace.createElement("div", { className: "diff-line-widget-wrapper sticky left-0 z-[1]", style: { width } },
1639
- width > 0 &&
1669
+ (enableWrap ? true : width > 0) &&
1640
1670
  oldWidget &&
1641
1671
  (renderWidgetLine === null || renderWidgetLine === void 0 ? void 0 : renderWidgetLine({ diffFile, side: core.SplitSide.old, lineNumber: unifiedItem.oldLineNumber, onClose })),
1642
- width > 0 &&
1672
+ (enableWrap ? true : width > 0) &&
1643
1673
  newWidget &&
1644
1674
  (renderWidgetLine === null || renderWidgetLine === void 0 ? void 0 : renderWidgetLine({ diffFile, side: core.SplitSide.new, lineNumber: unifiedItem.newLineNumber, onClose }))))));
1645
1675
  };
1646
- const DiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1676
+ const DiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, enableWrap, }) => {
1647
1677
  const { useWidget } = useDiffWidgetContext();
1648
1678
  const currentIsShow = useWidget.useShallowSelector(React__namespace.useCallback((s) => {
1649
1679
  const widgetLineNumber = s.widgetLineNumber;
@@ -1656,7 +1686,7 @@ const DiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1656
1686
  }, [diffFile, index]), (p, c) => p === c);
1657
1687
  if (!currentIsShow)
1658
1688
  return null;
1659
- return React__namespace.createElement(InternalDiffUnifiedWidgetLine, { index: index, diffFile: diffFile, lineNumber: lineNumber });
1689
+ return (React__namespace.createElement(InternalDiffUnifiedWidgetLine, { index: index, diffFile: diffFile, lineNumber: lineNumber, enableWrap: enableWrap }));
1660
1690
  };
1661
1691
 
1662
1692
  /* eslint-disable @typescript-eslint/ban-ts-comment */
@@ -1749,12 +1779,12 @@ const DiffUnifiedView = React.memo(({ diffFile }) => {
1749
1779
  React__namespace.createElement("tr", null,
1750
1780
  React__namespace.createElement("th", { scope: "col" }, "line number"),
1751
1781
  React__namespace.createElement("th", { scope: "col" }, "line content"))),
1752
- React__namespace.createElement("tbody", { className: "diff-table-body leading-[1.4]", onMouseDownCapture: onMouseDown },
1782
+ React__namespace.createElement("tbody", { className: "diff-table-body leading-[1.6]", onMouseDownCapture: onMouseDown },
1753
1783
  lines.map((item) => (React__namespace.createElement(React.Fragment, { key: item.index },
1754
1784
  React__namespace.createElement(DiffUnifiedHunkLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile }),
1755
1785
  React__namespace.createElement(DiffUnifiedContentLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile, enableWrap: enableWrap, enableHighlight: enableHighlight, enableAddWidget: enableAddWidget }),
1756
- React__namespace.createElement(DiffUnifiedWidgetLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile }),
1757
- React__namespace.createElement(DiffUnifiedExtendLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile })))),
1786
+ React__namespace.createElement(DiffUnifiedWidgetLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile, enableWrap: enableWrap }),
1787
+ React__namespace.createElement(DiffUnifiedExtendLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile, enableWrap: enableWrap })))),
1758
1788
  React__namespace.createElement(DiffUnifiedHunkLine, { index: diffFile.unifiedLineLength, lineNumber: diffFile.unifiedLineLength, diffFile: diffFile })))))));
1759
1789
  });
1760
1790
  DiffUnifiedView.displayName = "DiffUnifiedView";
@@ -1820,9 +1850,12 @@ const InternalDiffView = (props) => {
1820
1850
  onAddWidgetClick,
1821
1851
  onCreateUseWidgetHook,
1822
1852
  ]);
1853
+ React.useEffect(() => {
1854
+ useDiffContext.getReadonlyState().setDom(wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current);
1855
+ }, [useDiffContext, wrapperRef]);
1823
1856
  const value = React.useMemo(() => ({ useDiffContext }), [useDiffContext]);
1824
1857
  return (React__namespace.createElement(DiffViewContext.Provider, { value: value },
1825
- React__namespace.createElement("div", { className: "diff-tailwindcss-wrapper", "data-component": "git-diff-view", "data-theme": diffFile._getTheme() || "light", "data-version": "0.0.30", "data-highlighter": diffFile._getHighlighterName(), ref: wrapperRef },
1858
+ React__namespace.createElement("div", { className: "diff-tailwindcss-wrapper", "data-component": "git-diff-view", "data-theme": diffFile._getTheme() || "light", "data-version": "0.0.32", "data-highlighter": diffFile._getHighlighterName(), ref: wrapperRef },
1826
1859
  React__namespace.createElement("div", { className: "diff-style-root", style: {
1827
1860
  // @ts-ignore
1828
1861
  [diffFontSizeName]: diffViewFontSize + "px",
@@ -1876,9 +1909,9 @@ const DiffViewWithRef = (props, ref) => {
1876
1909
  return;
1877
1910
  if (props.diffViewHighlight) {
1878
1911
  diffFile.initSyntax({ registerHighlighter });
1879
- diffFile.notifyAll();
1880
1912
  }
1881
- }, [diffFile, props.diffViewHighlight, registerHighlighter]);
1913
+ diffFile.notifyAll();
1914
+ }, [diffFile, props.diffViewHighlight, registerHighlighter, diffViewTheme]);
1882
1915
  React.useEffect(() => {
1883
1916
  if (!diffFile)
1884
1917
  return;
@@ -1890,7 +1923,7 @@ const DiffViewWithRef = (props, ref) => {
1890
1923
  init();
1891
1924
  const cb = diffFile.subscribe(init);
1892
1925
  return cb;
1893
- }, [diffFile, diffViewTheme]);
1926
+ }, [diffFile]);
1894
1927
  // fix react strict mode error
1895
1928
  useUnmount(() => { var _b; return ((_b = diffFile === null || diffFile === void 0 ? void 0 : diffFile.clear) === null || _b === void 0 ? void 0 : _b.call(diffFile)); }, [diffFile]);
1896
1929
  React.useImperativeHandle(ref, () => ({ getDiffFileInstance: () => diffFile }), [diffFile]);
@@ -1901,7 +1934,7 @@ const DiffViewWithRef = (props, ref) => {
1901
1934
  const InnerDiffView = React.forwardRef(DiffViewWithRef);
1902
1935
  InnerDiffView.displayName = "DiffView";
1903
1936
  const DiffView = InnerDiffView;
1904
- const version = "0.0.30";
1937
+ const version = "0.0.32";
1905
1938
 
1906
1939
  Object.defineProperty(exports, "SplitSide", {
1907
1940
  enumerable: true,