@zero-library/common 2.4.2 → 2.4.4

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.cjs.js CHANGED
@@ -2476,6 +2476,7 @@ var HorizontalRule = TiptapHorizontalRule__default.default.extend({
2476
2476
  // src/components/MarkdownEditor/pageNo/constant.ts
2477
2477
  var PAGE_NO_TAG = "page-no";
2478
2478
  var PAGE_NO_KEY = "PageNo";
2479
+ var SCROLL_TOP_OFFSET = 80;
2479
2480
 
2480
2481
  // src/components/MarkdownEditor/pageNo/pageNoMark.ts
2481
2482
  var pageNoMark_default = core.Node.create({
@@ -2605,7 +2606,7 @@ var PageNoExtension = core.Extension.create({
2605
2606
  pageNos.forEach((node) => {
2606
2607
  const rect = node.getBoundingClientRect();
2607
2608
  const top = rect.top - containerTop;
2608
- if (Math.abs(top) <= 80) {
2609
+ if (Math.abs(top) <= SCROLL_TOP_OFFSET) {
2609
2610
  closestNode = node;
2610
2611
  closestTopDiff = 0;
2611
2612
  } else if (top < 0 && top > closestTopDiff) {
@@ -3670,13 +3671,45 @@ function scrollEditorViewToPosInNextFrame(view, pos, options) {
3670
3671
  const { align = "start", behavior = "auto", offset: offset3 = 0 } = options || {};
3671
3672
  requestAnimationFrame(() => {
3672
3673
  try {
3673
- const result = view.domAtPos(pos);
3674
- if (!result) return;
3675
- const el = result.node.nodeType === Node.TEXT_NODE ? result.node.parentElement : result.node;
3676
- if (!el) return;
3677
3674
  const scrollParent = getScrollParent(view.dom);
3675
+ if (!scrollParent) return;
3678
3676
  const parentRect = scrollParent.getBoundingClientRect();
3679
- const targetRect = el.getBoundingClientRect();
3677
+ if (!parentRect.height) return;
3678
+ let el = null;
3679
+ const nodeDom = view.nodeDOM?.(pos);
3680
+ if (nodeDom) {
3681
+ el = nodeDom.nodeType === Node.TEXT_NODE ? nodeDom.parentElement : nodeDom;
3682
+ }
3683
+ if (!el) {
3684
+ const result = view.domAtPos(pos);
3685
+ if (result) {
3686
+ if (result.node.nodeType === Node.TEXT_NODE) {
3687
+ el = result.node.parentElement;
3688
+ } else {
3689
+ const container = result.node;
3690
+ const nodeAtOffset = container.childNodes?.[result.offset];
3691
+ const nodeBeforeOffset = container.childNodes?.[result.offset - 1];
3692
+ if (nodeAtOffset?.nodeType === Node.ELEMENT_NODE) {
3693
+ el = nodeAtOffset;
3694
+ } else if (nodeBeforeOffset?.nodeType === Node.ELEMENT_NODE) {
3695
+ el = nodeBeforeOffset;
3696
+ } else {
3697
+ el = container;
3698
+ }
3699
+ }
3700
+ }
3701
+ }
3702
+ let targetRect = null;
3703
+ if (el) {
3704
+ targetRect = el.getBoundingClientRect();
3705
+ } else {
3706
+ try {
3707
+ const coords = view.coordsAtPos(pos);
3708
+ targetRect = new DOMRect(coords.left, coords.top, coords.right - coords.left, coords.bottom - coords.top);
3709
+ } catch {
3710
+ return;
3711
+ }
3712
+ }
3680
3713
  let delta = 0;
3681
3714
  if (align === "start") {
3682
3715
  delta = targetRect.top - parentRect.top;
@@ -3685,8 +3718,10 @@ function scrollEditorViewToPosInNextFrame(view, pos, options) {
3685
3718
  } else if (align === "end") {
3686
3719
  delta = targetRect.bottom - parentRect.bottom;
3687
3720
  }
3721
+ const maxTop = Math.max(0, scrollParent.scrollHeight - scrollParent.clientHeight);
3722
+ const nextTop = Math.max(0, Math.min(scrollParent.scrollTop + delta - offset3, maxTop));
3688
3723
  scrollParent.scrollTo({
3689
- top: scrollParent.scrollTop + delta - offset3,
3724
+ top: nextTop,
3690
3725
  behavior
3691
3726
  });
3692
3727
  } catch (e) {
@@ -8526,6 +8561,55 @@ var SearchAndReplace = core.Extension.create({
8526
8561
  }
8527
8562
  });
8528
8563
  var searchAndReplace_default = SearchAndReplace;
8564
+ var usePageNo = (pageNoConfig) => {
8565
+ const [editor, setEditor] = React.useState(null);
8566
+ const lastScrollToPageNoRef = React.useRef(void 0);
8567
+ const setPageNoEditor = (newEditor) => {
8568
+ setEditor(newEditor);
8569
+ };
8570
+ const onPageChange = useDebounce_default((pageNo = 1) => {
8571
+ lastScrollToPageNoRef.current = pageNo;
8572
+ pageNoConfig?.onScrollPage?.(pageNo);
8573
+ }, 300);
8574
+ React.useEffect(() => {
8575
+ if (!editor) return;
8576
+ if (!pageNoConfig?.enabled) return;
8577
+ const targetPageNo = pageNoConfig?.pageNo;
8578
+ if (isNullOrUnDef(targetPageNo)) return;
8579
+ if (lastScrollToPageNoRef.current === targetPageNo) return;
8580
+ let rafId = 0;
8581
+ const targetPageNoStr = String(targetPageNo);
8582
+ const findTargetPos = () => {
8583
+ let foundPos = null;
8584
+ editor.state.doc.descendants((node, pos) => {
8585
+ if (!isNullOrUnDef(foundPos)) return false;
8586
+ if (node.type.name !== PAGE_NO_KEY) return true;
8587
+ if (String(node.attrs?.[PAGE_NO_TAG]) === targetPageNoStr) {
8588
+ foundPos = pos;
8589
+ return false;
8590
+ }
8591
+ return true;
8592
+ });
8593
+ return foundPos;
8594
+ };
8595
+ let tries = 0;
8596
+ const tryScroll = () => {
8597
+ const foundPos = findTargetPos();
8598
+ if (!isNullOrUnDef(foundPos)) {
8599
+ scrollEditorViewToPosInNextFrame(editor.view, foundPos, { align: "start", offset: SCROLL_TOP_OFFSET });
8600
+ return;
8601
+ }
8602
+ tries += 1;
8603
+ if (tries < 10) rafId = requestAnimationFrame(tryScroll);
8604
+ };
8605
+ rafId = requestAnimationFrame(tryScroll);
8606
+ return () => cancelAnimationFrame(rafId);
8607
+ }, [editor, pageNoConfig?.enabled, pageNoConfig?.pageNo]);
8608
+ return {
8609
+ setPageNoEditor,
8610
+ onPageChange
8611
+ };
8612
+ };
8529
8613
  var MarkdownEditor_default = ({
8530
8614
  value = ``,
8531
8615
  onChange,
@@ -8542,9 +8626,7 @@ var MarkdownEditor_default = ({
8542
8626
  }) => {
8543
8627
  const isMobile = useIsBreakpoint();
8544
8628
  const lastContentRef = React.useRef("");
8545
- const onPageChange = useDebounce_default((pageIndex) => {
8546
- pageNoConfig?.onScrollPage?.(pageIndex ?? 0);
8547
- }, 300);
8629
+ const { setPageNoEditor, onPageChange } = usePageNo(pageNoConfig);
8548
8630
  const {
8549
8631
  setAnnotationEditor,
8550
8632
  annotations,
@@ -8680,6 +8762,7 @@ var MarkdownEditor_default = ({
8680
8762
  if (!editor) return;
8681
8763
  setAnnotationEditor(editor);
8682
8764
  setCollectionEditor(editor);
8765
+ setPageNoEditor(editor);
8683
8766
  }, [editor]);
8684
8767
  React.useEffect(() => {
8685
8768
  if (!editor) return;