@use-kona/editor 0.1.26 → 0.1.27

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.
@@ -4,6 +4,7 @@ import { map } from "nanostores";
4
4
  import { useEffect } from "react";
5
5
  import { Editor, Operation, Text } from "slate";
6
6
  import { ReactEditor } from "slate-react";
7
+ import { scrollToHeading } from "./scrollToHeading.js";
7
8
  import styles_module from "./styles.module.js";
8
9
  class TableOfContentsPlugin {
9
10
  options;
@@ -48,10 +49,7 @@ class TableOfContentsPlugin {
48
49
  const handleMouseDown = (event)=>{
49
50
  event.preventDefault();
50
51
  const element = ReactEditor.toDOMNode(params.editor, node);
51
- element?.scrollIntoView({
52
- behavior: 'smooth',
53
- block: 'start'
54
- });
52
+ scrollToHeading(element);
55
53
  };
56
54
  return /*#__PURE__*/ jsx("div", {
57
55
  className: styles_module[`h${level}`],
@@ -0,0 +1,9 @@
1
+ const scrollToHeading = (element)=>{
2
+ const options = {
3
+ behavior: 'smooth',
4
+ block: 'start',
5
+ container: 'nearest'
6
+ };
7
+ element.scrollIntoView(options);
8
+ };
9
+ export { scrollToHeading };
@@ -0,0 +1 @@
1
+ export declare const scrollToHeading: (element: HTMLElement) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@use-kona/editor",
3
- "version": "0.1.26",
3
+ "version": "0.1.27",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -5,6 +5,7 @@ import { Editor, type NodeEntry, Operation, Text } from 'slate';
5
5
  import { ReactEditor } from 'slate-react';
6
6
  import type { CustomElement } from '../../../types';
7
7
  import type { IPlugin, UiParams } from '../../types';
8
+ import { scrollToHeading } from './scrollToHeading';
8
9
  import styles from './styles.module.css';
9
10
 
10
11
  type Options = {
@@ -75,10 +76,7 @@ export class TableOfContentsPlugin implements IPlugin {
75
76
  params.editor,
76
77
  node,
77
78
  ) as HTMLElement;
78
- element?.scrollIntoView({
79
- behavior: 'smooth',
80
- block: 'start',
81
- });
79
+ scrollToHeading(element);
82
80
  };
83
81
 
84
82
  return (
@@ -0,0 +1,17 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ import { scrollToHeading } from './scrollToHeading';
3
+
4
+ describe('scrollToHeading', () => {
5
+ it('scrolls only the closest scroll container', () => {
6
+ const scrollIntoView = vi.fn();
7
+ const element = { scrollIntoView } as unknown as HTMLElement;
8
+
9
+ scrollToHeading(element);
10
+
11
+ expect(scrollIntoView).toHaveBeenCalledWith({
12
+ behavior: 'smooth',
13
+ block: 'start',
14
+ container: 'nearest',
15
+ });
16
+ });
17
+ });
@@ -0,0 +1,13 @@
1
+ type ScrollIntoViewOptionsWithContainer = ScrollIntoViewOptions & {
2
+ container: 'all' | 'nearest';
3
+ };
4
+
5
+ export const scrollToHeading = (element: HTMLElement) => {
6
+ const options: ScrollIntoViewOptionsWithContainer = {
7
+ behavior: 'smooth',
8
+ block: 'start',
9
+ container: 'nearest',
10
+ };
11
+
12
+ element.scrollIntoView(options);
13
+ };