@git-diff-view/react 0.0.31 → 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, }) => {
@@ -1316,6 +1338,8 @@ DiffSplitViewWrap.displayName = "DiffSplitViewWrap";
1316
1338
  const createDiffConfigStore = (props, diffFileId) => {
1317
1339
  return reactivityStore.createStore(() => {
1318
1340
  var _a, _b;
1341
+ const dom = reactivityStore.ref();
1342
+ const setDom = (_dom) => (dom.value = _dom);
1319
1343
  const id = reactivityStore.ref(diffFileId);
1320
1344
  const setId = (_id) => (id.value = _id);
1321
1345
  const mode = reactivityStore.ref(props.diffViewMode);
@@ -1368,6 +1392,8 @@ const createDiffConfigStore = (props, diffFileId) => {
1368
1392
  return {
1369
1393
  id,
1370
1394
  setId,
1395
+ dom,
1396
+ setDom,
1371
1397
  mode,
1372
1398
  setMode,
1373
1399
  mounted,
@@ -1523,20 +1549,21 @@ const DiffUnifiedContentLine = ({ index, diffFile, lineNumber, enableWrap, enabl
1523
1549
  return (React__namespace.createElement(_DiffUnifiedLine, { index: index, diffFile: diffFile, lineNumber: lineNumber, enableWrap: enableWrap, enableHighlight: enableHighlight, enableAddWidget: enableAddWidget }));
1524
1550
  };
1525
1551
 
1526
- const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, oldLineExtend, newLineExtend, }) => {
1552
+ const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, enableWrap, oldLineExtend, newLineExtend, }) => {
1527
1553
  const { useDiffContext } = useDiffViewContext();
1528
1554
  const renderExtendLine = useDiffContext.useShallowStableSelector((s) => s.renderExtendLine);
1529
1555
  const unifiedItem = diffFile.getUnifiedLine(index);
1556
+ // TODO use css variable to get width
1530
1557
  const width = useDomWidth({
1531
1558
  selector: ".unified-diff-table-wrapper",
1532
- enable: typeof renderExtendLine === "function",
1559
+ enable: typeof renderExtendLine === "function" && !enableWrap,
1533
1560
  });
1534
1561
  if (!renderExtendLine)
1535
1562
  return null;
1536
1563
  return (React__namespace.createElement("tr", { "data-line": `${lineNumber}-extend`, "data-state": "extend", className: "diff-line diff-line-extend" },
1537
1564
  React__namespace.createElement("td", { className: "diff-line-extend-content p-0 align-top", colSpan: 2 },
1538
1565
  React__namespace.createElement("div", { className: "diff-line-extend-wrapper sticky left-0 z-[1]", style: { width } },
1539
- width > 0 &&
1566
+ (enableWrap ? true : width > 0) &&
1540
1567
  (oldLineExtend === null || oldLineExtend === void 0 ? void 0 : oldLineExtend.data) !== undefined &&
1541
1568
  (oldLineExtend === null || oldLineExtend === void 0 ? void 0 : oldLineExtend.data) !== null &&
1542
1569
  (renderExtendLine === null || renderExtendLine === void 0 ? void 0 : renderExtendLine({
@@ -1546,7 +1573,7 @@ const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, oldLineExt
1546
1573
  data: oldLineExtend.data,
1547
1574
  onUpdate: diffFile.notifyAll,
1548
1575
  })),
1549
- width > 0 &&
1576
+ (enableWrap ? true : width > 0) &&
1550
1577
  (newLineExtend === null || newLineExtend === void 0 ? void 0 : newLineExtend.data) !== undefined &&
1551
1578
  (newLineExtend === null || newLineExtend === void 0 ? void 0 : newLineExtend.data) !== null &&
1552
1579
  (renderExtendLine === null || renderExtendLine === void 0 ? void 0 : renderExtendLine({
@@ -1557,7 +1584,7 @@ const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, oldLineExt
1557
1584
  onUpdate: diffFile.notifyAll,
1558
1585
  }))))));
1559
1586
  };
1560
- const DiffUnifiedExtendLine = ({ index, diffFile, lineNumber, }) => {
1587
+ const DiffUnifiedExtendLine = ({ index, diffFile, lineNumber, enableWrap, }) => {
1561
1588
  const { useDiffContext } = useDiffViewContext();
1562
1589
  const unifiedItem = diffFile.getUnifiedLine(index);
1563
1590
  const { oldLineExtend, newLineExtend } = useDiffContext(React.useCallback((s) => {
@@ -1570,7 +1597,7 @@ const DiffUnifiedExtendLine = ({ index, diffFile, lineNumber, }) => {
1570
1597
  const hasExtend = (oldLineExtend === null || oldLineExtend === void 0 ? void 0 : oldLineExtend.data) || (newLineExtend === null || newLineExtend === void 0 ? void 0 : newLineExtend.data);
1571
1598
  if (!hasExtend || !unifiedItem || unifiedItem.isHidden)
1572
1599
  return null;
1573
- 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 }));
1574
1601
  };
1575
1602
 
1576
1603
  const InternalDiffUnifiedHunkLine = ({ index, diffFile, lineNumber, }) => {
@@ -1618,7 +1645,7 @@ const DiffUnifiedHunkLine = ({ index, diffFile, lineNumber, }) => {
1618
1645
  return React__namespace.createElement(InternalDiffUnifiedHunkLine, { index: index, diffFile: diffFile, lineNumber: lineNumber });
1619
1646
  };
1620
1647
 
1621
- const InternalDiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1648
+ const InternalDiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, enableWrap, }) => {
1622
1649
  const { useWidget } = useDiffWidgetContext();
1623
1650
  const setWidget = useWidget.getReadonlyState().setWidget;
1624
1651
  const unifiedItem = diffFile.getUnifiedLine(index);
@@ -1632,21 +1659,21 @@ const InternalDiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1632
1659
  const renderWidgetLine = useDiffContext.useShallowStableSelector((s) => s.renderWidgetLine);
1633
1660
  const width = useDomWidth({
1634
1661
  selector: ".unified-diff-table-wrapper",
1635
- enable: typeof renderWidgetLine === "function",
1662
+ enable: typeof renderWidgetLine === "function" && !enableWrap,
1636
1663
  });
1637
1664
  if (!renderWidgetLine)
1638
1665
  return null;
1639
1666
  return (React__namespace.createElement("tr", { "data-line": `${lineNumber}-widget`, "data-state": "widget", className: "diff-line diff-line-widget" },
1640
1667
  React__namespace.createElement("td", { className: "diff-line-widget-content p-0", colSpan: 2 },
1641
1668
  React__namespace.createElement("div", { className: "diff-line-widget-wrapper sticky left-0 z-[1]", style: { width } },
1642
- width > 0 &&
1669
+ (enableWrap ? true : width > 0) &&
1643
1670
  oldWidget &&
1644
1671
  (renderWidgetLine === null || renderWidgetLine === void 0 ? void 0 : renderWidgetLine({ diffFile, side: core.SplitSide.old, lineNumber: unifiedItem.oldLineNumber, onClose })),
1645
- width > 0 &&
1672
+ (enableWrap ? true : width > 0) &&
1646
1673
  newWidget &&
1647
1674
  (renderWidgetLine === null || renderWidgetLine === void 0 ? void 0 : renderWidgetLine({ diffFile, side: core.SplitSide.new, lineNumber: unifiedItem.newLineNumber, onClose }))))));
1648
1675
  };
1649
- const DiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1676
+ const DiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, enableWrap, }) => {
1650
1677
  const { useWidget } = useDiffWidgetContext();
1651
1678
  const currentIsShow = useWidget.useShallowSelector(React__namespace.useCallback((s) => {
1652
1679
  const widgetLineNumber = s.widgetLineNumber;
@@ -1659,7 +1686,7 @@ const DiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1659
1686
  }, [diffFile, index]), (p, c) => p === c);
1660
1687
  if (!currentIsShow)
1661
1688
  return null;
1662
- 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 }));
1663
1690
  };
1664
1691
 
1665
1692
  /* eslint-disable @typescript-eslint/ban-ts-comment */
@@ -1756,8 +1783,8 @@ const DiffUnifiedView = React.memo(({ diffFile }) => {
1756
1783
  lines.map((item) => (React__namespace.createElement(React.Fragment, { key: item.index },
1757
1784
  React__namespace.createElement(DiffUnifiedHunkLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile }),
1758
1785
  React__namespace.createElement(DiffUnifiedContentLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile, enableWrap: enableWrap, enableHighlight: enableHighlight, enableAddWidget: enableAddWidget }),
1759
- React__namespace.createElement(DiffUnifiedWidgetLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile }),
1760
- 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 })))),
1761
1788
  React__namespace.createElement(DiffUnifiedHunkLine, { index: diffFile.unifiedLineLength, lineNumber: diffFile.unifiedLineLength, diffFile: diffFile })))))));
1762
1789
  });
1763
1790
  DiffUnifiedView.displayName = "DiffUnifiedView";
@@ -1823,9 +1850,12 @@ const InternalDiffView = (props) => {
1823
1850
  onAddWidgetClick,
1824
1851
  onCreateUseWidgetHook,
1825
1852
  ]);
1853
+ React.useEffect(() => {
1854
+ useDiffContext.getReadonlyState().setDom(wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current);
1855
+ }, [useDiffContext, wrapperRef]);
1826
1856
  const value = React.useMemo(() => ({ useDiffContext }), [useDiffContext]);
1827
1857
  return (React__namespace.createElement(DiffViewContext.Provider, { value: value },
1828
- React__namespace.createElement("div", { className: "diff-tailwindcss-wrapper", "data-component": "git-diff-view", "data-theme": diffFile._getTheme() || "light", "data-version": "0.0.31", "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 },
1829
1859
  React__namespace.createElement("div", { className: "diff-style-root", style: {
1830
1860
  // @ts-ignore
1831
1861
  [diffFontSizeName]: diffViewFontSize + "px",
@@ -1904,7 +1934,7 @@ const DiffViewWithRef = (props, ref) => {
1904
1934
  const InnerDiffView = React.forwardRef(DiffViewWithRef);
1905
1935
  InnerDiffView.displayName = "DiffView";
1906
1936
  const DiffView = InnerDiffView;
1907
- const version = "0.0.31";
1937
+ const version = "0.0.32";
1908
1938
 
1909
1939
  Object.defineProperty(exports, "SplitSide", {
1910
1940
  enumerable: true,