@git-diff-view/react 0.0.31 → 0.0.33

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,35 @@ 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
+ };
217
+ const getDiffIdFromElement = (element) => {
218
+ if (element) {
219
+ if (typeof element.closest === "function") {
220
+ const diffRoot = element.closest('[data-component="git-diff-view"]');
221
+ const ele = diffRoot.querySelector(".diff-view-wrapper");
222
+ return ele.getAttribute("id");
223
+ }
224
+ else {
225
+ let el = element;
226
+ while (el) {
227
+ if (el.getAttribute && el.getAttribute("data-component") === "git-diff-view") {
228
+ const ele = el.querySelector(".diff-view-wrapper");
229
+ return ele.getAttribute("id");
230
+ }
231
+ el = el.parentElement;
232
+ }
233
+ }
234
+ }
235
+ };
207
236
 
208
237
  const diffFontSizeName = "--diff-font-size--";
209
238
  const diffAsideWidthName = "--diff-aside-width--";
@@ -289,6 +318,7 @@ const DiffSplitAddWidget = ({ side, className, lineNumber, onWidgetClick, onOpen
289
318
  (className ? " " + className : ""), style: {
290
319
  width: `calc(var(${diffFontSizeName}) * 1.4)`,
291
320
  height: `calc(var(${diffFontSizeName}) * 1.4)`,
321
+ top: `calc(var(${diffFontSizeName}) * 0.1)`,
292
322
  } },
293
323
  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
324
  color: `var(${addWidgetColorName})`,
@@ -299,9 +329,10 @@ const DiffSplitAddWidget = ({ side, className, lineNumber, onWidgetClick, onOpen
299
329
  } }, "+")));
300
330
  };
301
331
  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: {
332
+ 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
333
  width: `calc(var(${diffFontSizeName}) * 1.4)`,
304
334
  height: `calc(var(${diffFontSizeName}) * 1.4)`,
335
+ top: `calc(var(${diffFontSizeName}) * 0.1)`,
305
336
  } },
306
337
  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
338
  color: `var(${addWidgetColorName})`,
@@ -537,10 +568,15 @@ const DiffSplitContentLine$1 = ({ index, diffFile, lineNumber, side, enableAddWi
537
568
  const useDomWidth = ({ selector, enable }) => {
538
569
  const [width, setWidth] = React.useState(0);
539
570
  const { useDiffContext } = useDiffViewContext();
540
- const { id, mounted } = useDiffContext.useShallowStableSelector((s) => ({ id: s.id, mounted: s.mounted }));
571
+ const { id, mounted, dom } = useDiffContext.useShallowStableSelector((s) => ({
572
+ id: s.id,
573
+ mounted: s.mounted,
574
+ dom: s.dom,
575
+ }));
541
576
  React.useEffect(() => {
542
577
  if (enable) {
543
- const container = document.querySelector(`#diff-root${id}`);
578
+ const rootDocument = getElementRoot(dom);
579
+ const container = rootDocument.querySelector(`#diff-root${id}`);
544
580
  const wrapper = container === null || container === void 0 ? void 0 : container.querySelector(selector);
545
581
  if (!wrapper)
546
582
  return;
@@ -573,16 +609,21 @@ const useDomWidth = ({ selector, enable }) => {
573
609
  typedWrapper.setAttribute("data-observe", "height");
574
610
  return () => cleanCb();
575
611
  }
576
- }, [selector, enable, id, mounted]);
612
+ }, [selector, enable, id, mounted, dom]);
577
613
  return width;
578
614
  };
579
615
 
580
616
  const useSyncHeight = ({ selector, wrapper, side, enable, }) => {
581
617
  const { useDiffContext } = useDiffViewContext();
582
- const { id, mounted } = useDiffContext.useShallowStableSelector((s) => ({ id: s.id, mounted: s.mounted }));
618
+ const { id, mounted, dom } = useDiffContext.useShallowStableSelector((s) => ({
619
+ id: s.id,
620
+ mounted: s.mounted,
621
+ dom: s.dom,
622
+ }));
583
623
  React.useEffect(() => {
584
624
  if (enable) {
585
- const container = document.querySelector(`#diff-root${id}`);
625
+ const rootDocument = getElementRoot(dom);
626
+ const container = rootDocument.querySelector(`#diff-root${id}`);
586
627
  const elements = Array.from((container === null || container === void 0 ? void 0 : container.querySelectorAll(selector)) || []);
587
628
  const wrappers = wrapper ? Array.from((container === null || container === void 0 ? void 0 : container.querySelectorAll(wrapper)) || []) : elements;
588
629
  if (elements.length === 2 && wrappers.length === 2) {
@@ -627,7 +668,7 @@ const useSyncHeight = ({ selector, wrapper, side, enable, }) => {
627
668
  return () => cleanCb();
628
669
  }
629
670
  }
630
- }, [selector, enable, side, id, wrapper, mounted]);
671
+ }, [selector, enable, side, id, wrapper, mounted, dom]);
631
672
  };
632
673
 
633
674
  const InternalDiffSplitExtendLine$1 = ({ index, diffFile, oldLineExtend, newLineExtend, side, lineNumber, }) => {
@@ -918,6 +959,10 @@ const DiffSplitViewNormal = React.memo(({ diffFile }) => {
918
959
  removeAllSelection();
919
960
  return;
920
961
  }
962
+ const id = getDiffIdFromElement(ele);
963
+ if (id && id !== `diff-root${diffFile.getId()}`) {
964
+ return;
965
+ }
921
966
  while (ele && ele instanceof HTMLElement) {
922
967
  const state = ele.getAttribute("data-state");
923
968
  const side = ele.getAttribute("data-side");
@@ -1257,6 +1302,10 @@ const DiffSplitViewWrap = React.memo(({ diffFile }) => {
1257
1302
  removeAllSelection();
1258
1303
  return;
1259
1304
  }
1305
+ const id = getDiffIdFromElement(ele);
1306
+ if (id && id !== `diff-root${diffFile.getId()}`) {
1307
+ return;
1308
+ }
1260
1309
  while (ele && ele instanceof HTMLElement) {
1261
1310
  const state = ele.getAttribute("data-state");
1262
1311
  const side = ele.getAttribute("data-side");
@@ -1316,6 +1365,8 @@ DiffSplitViewWrap.displayName = "DiffSplitViewWrap";
1316
1365
  const createDiffConfigStore = (props, diffFileId) => {
1317
1366
  return reactivityStore.createStore(() => {
1318
1367
  var _a, _b;
1368
+ const dom = reactivityStore.ref();
1369
+ const setDom = (_dom) => (dom.value = _dom);
1319
1370
  const id = reactivityStore.ref(diffFileId);
1320
1371
  const setId = (_id) => (id.value = _id);
1321
1372
  const mode = reactivityStore.ref(props.diffViewMode);
@@ -1368,6 +1419,8 @@ const createDiffConfigStore = (props, diffFileId) => {
1368
1419
  return {
1369
1420
  id,
1370
1421
  setId,
1422
+ dom,
1423
+ setDom,
1371
1424
  mode,
1372
1425
  setMode,
1373
1426
  mounted,
@@ -1523,20 +1576,21 @@ const DiffUnifiedContentLine = ({ index, diffFile, lineNumber, enableWrap, enabl
1523
1576
  return (React__namespace.createElement(_DiffUnifiedLine, { index: index, diffFile: diffFile, lineNumber: lineNumber, enableWrap: enableWrap, enableHighlight: enableHighlight, enableAddWidget: enableAddWidget }));
1524
1577
  };
1525
1578
 
1526
- const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, oldLineExtend, newLineExtend, }) => {
1579
+ const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, enableWrap, oldLineExtend, newLineExtend, }) => {
1527
1580
  const { useDiffContext } = useDiffViewContext();
1528
1581
  const renderExtendLine = useDiffContext.useShallowStableSelector((s) => s.renderExtendLine);
1529
1582
  const unifiedItem = diffFile.getUnifiedLine(index);
1583
+ // TODO use css variable to get width
1530
1584
  const width = useDomWidth({
1531
1585
  selector: ".unified-diff-table-wrapper",
1532
- enable: typeof renderExtendLine === "function",
1586
+ enable: typeof renderExtendLine === "function" && !enableWrap,
1533
1587
  });
1534
1588
  if (!renderExtendLine)
1535
1589
  return null;
1536
1590
  return (React__namespace.createElement("tr", { "data-line": `${lineNumber}-extend`, "data-state": "extend", className: "diff-line diff-line-extend" },
1537
1591
  React__namespace.createElement("td", { className: "diff-line-extend-content p-0 align-top", colSpan: 2 },
1538
1592
  React__namespace.createElement("div", { className: "diff-line-extend-wrapper sticky left-0 z-[1]", style: { width } },
1539
- width > 0 &&
1593
+ (enableWrap ? true : width > 0) &&
1540
1594
  (oldLineExtend === null || oldLineExtend === void 0 ? void 0 : oldLineExtend.data) !== undefined &&
1541
1595
  (oldLineExtend === null || oldLineExtend === void 0 ? void 0 : oldLineExtend.data) !== null &&
1542
1596
  (renderExtendLine === null || renderExtendLine === void 0 ? void 0 : renderExtendLine({
@@ -1546,7 +1600,7 @@ const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, oldLineExt
1546
1600
  data: oldLineExtend.data,
1547
1601
  onUpdate: diffFile.notifyAll,
1548
1602
  })),
1549
- width > 0 &&
1603
+ (enableWrap ? true : width > 0) &&
1550
1604
  (newLineExtend === null || newLineExtend === void 0 ? void 0 : newLineExtend.data) !== undefined &&
1551
1605
  (newLineExtend === null || newLineExtend === void 0 ? void 0 : newLineExtend.data) !== null &&
1552
1606
  (renderExtendLine === null || renderExtendLine === void 0 ? void 0 : renderExtendLine({
@@ -1557,7 +1611,7 @@ const InternalDiffUnifiedExtendLine = ({ index, diffFile, lineNumber, oldLineExt
1557
1611
  onUpdate: diffFile.notifyAll,
1558
1612
  }))))));
1559
1613
  };
1560
- const DiffUnifiedExtendLine = ({ index, diffFile, lineNumber, }) => {
1614
+ const DiffUnifiedExtendLine = ({ index, diffFile, lineNumber, enableWrap, }) => {
1561
1615
  const { useDiffContext } = useDiffViewContext();
1562
1616
  const unifiedItem = diffFile.getUnifiedLine(index);
1563
1617
  const { oldLineExtend, newLineExtend } = useDiffContext(React.useCallback((s) => {
@@ -1570,7 +1624,7 @@ const DiffUnifiedExtendLine = ({ index, diffFile, lineNumber, }) => {
1570
1624
  const hasExtend = (oldLineExtend === null || oldLineExtend === void 0 ? void 0 : oldLineExtend.data) || (newLineExtend === null || newLineExtend === void 0 ? void 0 : newLineExtend.data);
1571
1625
  if (!hasExtend || !unifiedItem || unifiedItem.isHidden)
1572
1626
  return null;
1573
- return (React__namespace.createElement(InternalDiffUnifiedExtendLine, { index: index, diffFile: diffFile, lineNumber: lineNumber, oldLineExtend: oldLineExtend, newLineExtend: newLineExtend }));
1627
+ return (React__namespace.createElement(InternalDiffUnifiedExtendLine, { index: index, diffFile: diffFile, lineNumber: lineNumber, enableWrap: enableWrap, oldLineExtend: oldLineExtend, newLineExtend: newLineExtend }));
1574
1628
  };
1575
1629
 
1576
1630
  const InternalDiffUnifiedHunkLine = ({ index, diffFile, lineNumber, }) => {
@@ -1618,7 +1672,7 @@ const DiffUnifiedHunkLine = ({ index, diffFile, lineNumber, }) => {
1618
1672
  return React__namespace.createElement(InternalDiffUnifiedHunkLine, { index: index, diffFile: diffFile, lineNumber: lineNumber });
1619
1673
  };
1620
1674
 
1621
- const InternalDiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1675
+ const InternalDiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, enableWrap, }) => {
1622
1676
  const { useWidget } = useDiffWidgetContext();
1623
1677
  const setWidget = useWidget.getReadonlyState().setWidget;
1624
1678
  const unifiedItem = diffFile.getUnifiedLine(index);
@@ -1632,21 +1686,21 @@ const InternalDiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1632
1686
  const renderWidgetLine = useDiffContext.useShallowStableSelector((s) => s.renderWidgetLine);
1633
1687
  const width = useDomWidth({
1634
1688
  selector: ".unified-diff-table-wrapper",
1635
- enable: typeof renderWidgetLine === "function",
1689
+ enable: typeof renderWidgetLine === "function" && !enableWrap,
1636
1690
  });
1637
1691
  if (!renderWidgetLine)
1638
1692
  return null;
1639
1693
  return (React__namespace.createElement("tr", { "data-line": `${lineNumber}-widget`, "data-state": "widget", className: "diff-line diff-line-widget" },
1640
1694
  React__namespace.createElement("td", { className: "diff-line-widget-content p-0", colSpan: 2 },
1641
1695
  React__namespace.createElement("div", { className: "diff-line-widget-wrapper sticky left-0 z-[1]", style: { width } },
1642
- width > 0 &&
1696
+ (enableWrap ? true : width > 0) &&
1643
1697
  oldWidget &&
1644
1698
  (renderWidgetLine === null || renderWidgetLine === void 0 ? void 0 : renderWidgetLine({ diffFile, side: core.SplitSide.old, lineNumber: unifiedItem.oldLineNumber, onClose })),
1645
- width > 0 &&
1699
+ (enableWrap ? true : width > 0) &&
1646
1700
  newWidget &&
1647
1701
  (renderWidgetLine === null || renderWidgetLine === void 0 ? void 0 : renderWidgetLine({ diffFile, side: core.SplitSide.new, lineNumber: unifiedItem.newLineNumber, onClose }))))));
1648
1702
  };
1649
- const DiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1703
+ const DiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, enableWrap, }) => {
1650
1704
  const { useWidget } = useDiffWidgetContext();
1651
1705
  const currentIsShow = useWidget.useShallowSelector(React__namespace.useCallback((s) => {
1652
1706
  const widgetLineNumber = s.widgetLineNumber;
@@ -1659,7 +1713,7 @@ const DiffUnifiedWidgetLine = ({ index, diffFile, lineNumber, }) => {
1659
1713
  }, [diffFile, index]), (p, c) => p === c);
1660
1714
  if (!currentIsShow)
1661
1715
  return null;
1662
- return React__namespace.createElement(InternalDiffUnifiedWidgetLine, { index: index, diffFile: diffFile, lineNumber: lineNumber });
1716
+ return (React__namespace.createElement(InternalDiffUnifiedWidgetLine, { index: index, diffFile: diffFile, lineNumber: lineNumber, enableWrap: enableWrap }));
1663
1717
  };
1664
1718
 
1665
1719
  /* eslint-disable @typescript-eslint/ban-ts-comment */
@@ -1712,6 +1766,10 @@ const DiffUnifiedView = React.memo(({ diffFile }) => {
1712
1766
  removeAllSelection();
1713
1767
  return;
1714
1768
  }
1769
+ const id = getDiffIdFromElement(ele);
1770
+ if (id && id !== `diff-root${diffFile.getId()}`) {
1771
+ return;
1772
+ }
1715
1773
  while (ele && ele instanceof HTMLElement) {
1716
1774
  const state = ele.getAttribute("data-state");
1717
1775
  if (state) {
@@ -1756,8 +1814,8 @@ const DiffUnifiedView = React.memo(({ diffFile }) => {
1756
1814
  lines.map((item) => (React__namespace.createElement(React.Fragment, { key: item.index },
1757
1815
  React__namespace.createElement(DiffUnifiedHunkLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile }),
1758
1816
  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 })))),
1817
+ React__namespace.createElement(DiffUnifiedWidgetLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile, enableWrap: enableWrap }),
1818
+ React__namespace.createElement(DiffUnifiedExtendLine, { index: item.index, lineNumber: item.lineNumber, diffFile: diffFile, enableWrap: enableWrap })))),
1761
1819
  React__namespace.createElement(DiffUnifiedHunkLine, { index: diffFile.unifiedLineLength, lineNumber: diffFile.unifiedLineLength, diffFile: diffFile })))))));
1762
1820
  });
1763
1821
  DiffUnifiedView.displayName = "DiffUnifiedView";
@@ -1823,9 +1881,12 @@ const InternalDiffView = (props) => {
1823
1881
  onAddWidgetClick,
1824
1882
  onCreateUseWidgetHook,
1825
1883
  ]);
1884
+ React.useEffect(() => {
1885
+ useDiffContext.getReadonlyState().setDom(wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current);
1886
+ }, [useDiffContext, wrapperRef]);
1826
1887
  const value = React.useMemo(() => ({ useDiffContext }), [useDiffContext]);
1827
1888
  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 },
1889
+ React__namespace.createElement("div", { className: "diff-tailwindcss-wrapper", "data-component": "git-diff-view", "data-theme": diffFile._getTheme() || "light", "data-version": "0.0.33", "data-highlighter": diffFile._getHighlighterName(), ref: wrapperRef },
1829
1890
  React__namespace.createElement("div", { className: "diff-style-root", style: {
1830
1891
  // @ts-ignore
1831
1892
  [diffFontSizeName]: diffViewFontSize + "px",
@@ -1904,7 +1965,7 @@ const DiffViewWithRef = (props, ref) => {
1904
1965
  const InnerDiffView = React.forwardRef(DiffViewWithRef);
1905
1966
  InnerDiffView.displayName = "DiffView";
1906
1967
  const DiffView = InnerDiffView;
1907
- const version = "0.0.31";
1968
+ const version = "0.0.33";
1908
1969
 
1909
1970
  Object.defineProperty(exports, "SplitSide", {
1910
1971
  enumerable: true,