@zero-library/common 2.4.2 → 2.4.3

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
@@ -8526,6 +8526,55 @@ var SearchAndReplace = core.Extension.create({
8526
8526
  }
8527
8527
  });
8528
8528
  var searchAndReplace_default = SearchAndReplace;
8529
+ var usePageNo = (pageNoConfig) => {
8530
+ const [editor, setEditor] = React.useState(null);
8531
+ const lastScrollToPageNoRef = React.useRef(void 0);
8532
+ const setPageNoEditor = (newEditor) => {
8533
+ setEditor(newEditor);
8534
+ };
8535
+ const onPageChange = useDebounce_default((pageNo = 0) => {
8536
+ lastScrollToPageNoRef.current = pageNo;
8537
+ pageNoConfig?.onScrollPage?.(pageNo);
8538
+ }, 300);
8539
+ React.useEffect(() => {
8540
+ if (!editor) return;
8541
+ if (!pageNoConfig?.enabled) return;
8542
+ const targetPageNo = pageNoConfig?.pageNo;
8543
+ if (isNullOrUnDef(targetPageNo)) return;
8544
+ if (lastScrollToPageNoRef.current === targetPageNo) return;
8545
+ let rafId = 0;
8546
+ const targetPageNoStr = String(targetPageNo);
8547
+ const findTargetPos = () => {
8548
+ let foundPos = null;
8549
+ editor.state.doc.descendants((node, pos) => {
8550
+ if (!isNullOrUnDef(foundPos)) return false;
8551
+ if (node.type.name !== PAGE_NO_KEY) return true;
8552
+ if (String(node.attrs?.[PAGE_NO_TAG]) === targetPageNoStr) {
8553
+ foundPos = pos;
8554
+ return false;
8555
+ }
8556
+ return true;
8557
+ });
8558
+ return foundPos;
8559
+ };
8560
+ let tries = 0;
8561
+ const tryScroll = () => {
8562
+ const foundPos = findTargetPos();
8563
+ if (!isNullOrUnDef(foundPos)) {
8564
+ scrollEditorViewToPosInNextFrame(editor.view, foundPos, { align: "start", behavior: "smooth" });
8565
+ return;
8566
+ }
8567
+ tries += 1;
8568
+ if (tries < 10) rafId = requestAnimationFrame(tryScroll);
8569
+ };
8570
+ rafId = requestAnimationFrame(tryScroll);
8571
+ return () => cancelAnimationFrame(rafId);
8572
+ }, [editor, pageNoConfig?.enabled, pageNoConfig?.pageNo]);
8573
+ return {
8574
+ setPageNoEditor,
8575
+ onPageChange
8576
+ };
8577
+ };
8529
8578
  var MarkdownEditor_default = ({
8530
8579
  value = ``,
8531
8580
  onChange,
@@ -8542,9 +8591,7 @@ var MarkdownEditor_default = ({
8542
8591
  }) => {
8543
8592
  const isMobile = useIsBreakpoint();
8544
8593
  const lastContentRef = React.useRef("");
8545
- const onPageChange = useDebounce_default((pageIndex) => {
8546
- pageNoConfig?.onScrollPage?.(pageIndex ?? 0);
8547
- }, 300);
8594
+ const { setPageNoEditor, onPageChange } = usePageNo(pageNoConfig);
8548
8595
  const {
8549
8596
  setAnnotationEditor,
8550
8597
  annotations,
@@ -8680,6 +8727,7 @@ var MarkdownEditor_default = ({
8680
8727
  if (!editor) return;
8681
8728
  setAnnotationEditor(editor);
8682
8729
  setCollectionEditor(editor);
8730
+ setPageNoEditor(editor);
8683
8731
  }, [editor]);
8684
8732
  React.useEffect(() => {
8685
8733
  if (!editor) return;