@use-kona/editor 0.1.8-rc.8 → 0.1.9

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.
@@ -1,4 +1,4 @@
1
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import is_hotkey from "is-hotkey";
3
3
  import react, { isValidElement, useCallback } from "react";
4
4
  import { Transforms } from "slate";
@@ -87,7 +87,11 @@ const createEditable = (editor, plugins)=>({ readOnly })=>{
87
87
  className: styles_module.editor,
88
88
  children: children
89
89
  });
90
- return /*#__PURE__*/ jsxs(Fragment, {
90
+ return /*#__PURE__*/ jsxs("div", {
91
+ className: styles_module.ui,
92
+ onClick: (event)=>{
93
+ event.stopPropagation();
94
+ },
91
95
  children: [
92
96
  ui.map((p, index)=>/*#__PURE__*/ jsx(react.Fragment, {
93
97
  children: p.ui({
@@ -1,6 +1,7 @@
1
1
  import "./styles_module.css";
2
2
  const styles_module = {
3
3
  root: "root-e1gdll",
4
+ ui: "ui-I68yAA",
4
5
  editor: "editor-K2itFp"
5
6
  };
6
7
  export { styles_module as default };
@@ -1,5 +1,6 @@
1
1
  .root-e1gdll {
2
2
  height: 100%;
3
+ min-height: 0;
3
4
  color: var(--kona-editor-text-color, #444);
4
5
  background-color: var(--kona-editor-background-color, #fff);
5
6
  flex-direction: column;
@@ -7,8 +8,13 @@
7
8
  position: relative;
8
9
  }
9
10
 
11
+ .ui-I68yAA {
12
+ height: 100%;
13
+ }
14
+
10
15
  .editor-K2itFp {
11
16
  outline: none;
17
+ height: 100%;
12
18
  overflow: auto;
13
19
 
14
20
  &::-webkit-scrollbar {
package/dist/editor.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
2
  import { forwardRef, useCallback, useImperativeHandle, useState } from "react";
3
- import { Transforms } from "slate";
3
+ import { Editor, Transforms } from "slate";
4
4
  import { ReactEditor, Slate } from "slate-react";
5
5
  import { createEditable } from "./core/createEditable.js";
6
6
  import { createEditor } from "./core/createEditor.js";
@@ -23,7 +23,11 @@ const KonaEditor = /*#__PURE__*/ forwardRef((props, ref)=>{
23
23
  deserialize: deserialize(plugins),
24
24
  deleteNode,
25
25
  isEmpty: ()=>isEmpty(editor.children),
26
- focus: ()=>{
26
+ focus: (mode)=>{
27
+ if ('end' === mode) {
28
+ const endPoint = Editor.end(editor, []);
29
+ Transforms.select(editor, endPoint);
30
+ }
27
31
  ReactEditor.focus(editor);
28
32
  }
29
33
  };
@@ -48,6 +48,9 @@ class BasicFormattingPlugin {
48
48
  });
49
49
  return /*#__PURE__*/ jsx("span", {
50
50
  ...attributes,
51
+ style: {
52
+ paddingRight: '0.001em'
53
+ },
51
54
  children: content
52
55
  });
53
56
  },
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import type React from 'react';
2
2
  import { type RenderElementProps } from 'slate-react';
3
3
  import type { LinkElement, Options } from './types';
4
4
  type Props = {
@@ -1,4 +1,4 @@
1
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useLayoutEffect, useRef, useState } from "react";
3
3
  import { createPortal } from "react-dom";
4
4
  import { useReadOnly, useSlateStatic } from "slate-react";
@@ -19,7 +19,7 @@ const Link = (props)=>{
19
19
  setOpen(true);
20
20
  }
21
21
  };
22
- const handleMenuClick = (event)=>{
22
+ const handleMenuClick = ()=>{
23
23
  setOpen(false);
24
24
  };
25
25
  useLayoutEffect(()=>{
@@ -43,7 +43,6 @@ const Link = (props)=>{
43
43
  return /*#__PURE__*/ jsxs(Component, {
44
44
  ref: ref,
45
45
  className: styles_module.link,
46
- contentEditable: false,
47
46
  children: [
48
47
  /*#__PURE__*/ jsxs("a", {
49
48
  ...attributes,
@@ -57,24 +56,16 @@ const Link = (props)=>{
57
56
  /*#__PURE__*/ jsx(InlineChromiumBugfix, {})
58
57
  ]
59
58
  }),
60
- isOpen && /*#__PURE__*/ createPortal(/*#__PURE__*/ jsxs(Fragment, {
61
- children: [
62
- /*#__PURE__*/ jsx("div", {
63
- className: styles_module.backdrop,
64
- onClick: ()=>setOpen(false)
65
- }),
66
- /*#__PURE__*/ jsx("div", {
67
- contentEditable: false,
68
- className: styles_module.hint,
69
- style: style,
70
- onMouseUp: handleMenuClick,
71
- children: renderHint({
72
- getLinkElement,
73
- getUrl,
74
- getEditor
75
- })
76
- })
77
- ]
59
+ isOpen && /*#__PURE__*/ createPortal(/*#__PURE__*/ jsx("div", {
60
+ contentEditable: false,
61
+ className: styles_module.hint,
62
+ style: style,
63
+ onMouseUp: handleMenuClick,
64
+ children: renderHint({
65
+ getLinkElement,
66
+ getUrl,
67
+ getEditor
68
+ })
78
69
  }), document.body)
79
70
  ]
80
71
  });
@@ -33,10 +33,11 @@
33
33
  }
34
34
 
35
35
  .link-I7FEHR {
36
+ padding-right: 1px;
36
37
  position: relative;
37
38
 
38
39
  & a {
39
- color: var(--link-color, #21afec);
40
+ color: #21afec;
40
41
  }
41
42
  }
42
43
 
@@ -1,5 +1,5 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
- import { Editor, Element, Node, Transforms } from "slate";
2
+ import { Editor, Element, Node, Path, Transforms } from "slate";
3
3
  import { jsx as external_slate_hyperscript_jsx } from "slate-hyperscript";
4
4
  import styles_module from "./styles.module.js";
5
5
  class ListsPlugin {
@@ -48,6 +48,7 @@ class ListsPlugin {
48
48
  });
49
49
  }
50
50
  if (this.isList(editor, node)) {
51
+ if (!Path.hasPrevious(path)) return void normalizeNode(entry);
51
52
  const prevListItemPath = Editor.before(editor, path, {
52
53
  unit: 'block'
53
54
  });
@@ -49,7 +49,8 @@ class TableOfContentsPlugin {
49
49
  event.preventDefault();
50
50
  const element = ReactEditor.toDOMNode(params.editor, node);
51
51
  element?.scrollIntoView({
52
- behavior: 'smooth'
52
+ behavior: 'smooth',
53
+ block: 'nearest'
53
54
  });
54
55
  };
55
56
  return /*#__PURE__*/ jsx("div", {
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { KeyboardEvent, ReactElement, ReactNode } from 'react';
2
- import type { DecoratedRange, Descendant, Editor, NodeEntry } from 'slate';
2
+ import type { DecoratedRange, Descendant, Editor, NodeEntry, NodeMatch } from 'slate';
3
3
  import type { RenderElementProps, RenderLeafProps } from 'slate-react';
4
4
  import type { CustomElement, CustomText } from '../types';
5
5
  export interface IPlugin<TEditor extends Editor = Editor, TBlock extends CustomElement = CustomElement, TLeaf extends CustomText = CustomText> {
@@ -46,6 +46,9 @@ export type UiParams = {
46
46
  export type EditorRef = {
47
47
  serialize: (node: CustomElement | CustomText) => string;
48
48
  deserialize: (element: HTMLElement) => (Descendant | string)[] | string | Descendant | null;
49
+ isEmpty: () => boolean;
50
+ deleteNode: (match: NodeMatch<CustomElement>) => void;
51
+ focus: (mode?: 'end') => void;
49
52
  };
50
53
  export type Serialize = (node: CustomElement | CustomText, children?: string) => string | undefined;
51
54
  export type Deserialize = (element: HTMLElement, children?: (string | Descendant)[]) => CustomElement | CustomText[] | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@use-kona/editor",
3
- "version": "0.1.8-rc.8",
3
+ "version": "0.1.9",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -154,14 +154,16 @@ export const createEditable =
154
154
  }
155
155
 
156
156
  return (
157
- <>
157
+ <div className={styles.ui} onClick={(event) => {
158
+ event.stopPropagation();
159
+ }}>
158
160
  {ui.map((p, index) => (
159
161
  <React.Fragment key={index}>
160
162
  {p.ui!({ readOnly, children, editor })}
161
163
  </React.Fragment>
162
164
  ))}
163
165
  <div className={styles.editor}>{children}</div>
164
- </>
166
+ </div>
165
167
  );
166
168
  },
167
169
  [editor, plugins.filter],
@@ -3,12 +3,18 @@
3
3
  display: flex;
4
4
  flex-direction: column;
5
5
  height: 100%;
6
+ min-height: 0;
6
7
  color: var(--kona-editor-text-color, #444);
7
8
  background-color: var(--kona-editor-background-color, #fff);
8
9
  }
9
10
 
11
+ .ui {
12
+ height: 100%;
13
+ }
14
+
10
15
  .editor {
11
16
  outline: none;
17
+ height: 100%;
12
18
  overflow: auto;
13
19
 
14
20
  &::-webkit-scrollbar {
package/src/editor.tsx CHANGED
@@ -1,5 +1,5 @@
1
1
  import { forwardRef, useCallback, useImperativeHandle, useState } from 'react';
2
- import { type Descendant, type NodeMatch, Transforms } from 'slate';
2
+ import {type Descendant, Editor, type NodeMatch, Transforms} from 'slate';
3
3
  import { ReactEditor, Slate } from 'slate-react';
4
4
  import type { CustomElement } from '../types';
5
5
  import { createEditable } from './core/createEditable';
@@ -36,7 +36,12 @@ export const KonaEditor = forwardRef<EditorRef, KonaEditorProps>(
36
36
  isEmpty: () => {
37
37
  return isEmpty(editor.children);
38
38
  },
39
- focus: () => {
39
+ focus: (mode?: 'end') => {
40
+ if (mode === 'end') {
41
+ const endPoint = Editor.end(editor, [])
42
+ Transforms.select(editor, endPoint)
43
+ }
44
+
40
45
  ReactEditor.focus(editor);
41
46
  },
42
47
  };
@@ -63,7 +63,14 @@ export class BasicFormattingPlugin
63
63
  content = <s>{content}</s>;
64
64
  }
65
65
 
66
- return <span {...attributes}>{content}</span>;
66
+ /**
67
+ * https://github.com/ianstormtaylor/slate/issues/4704#issuecomment-1006696364
68
+ */
69
+ return (
70
+ <span {...attributes} style={{ paddingRight: '0.001em' }}>
71
+ {content}
72
+ </span>
73
+ );
67
74
  },
68
75
  serialize: (node: CustomElement | CustomLeaf) => {
69
76
  if (Text.isText(node)) {
@@ -1,4 +1,5 @@
1
- import React, { useLayoutEffect, useRef, useState } from 'react';
1
+ import type React from 'react';
2
+ import { useLayoutEffect, useRef, useState } from 'react';
2
3
  import { createPortal } from 'react-dom';
3
4
  import {
4
5
  type RenderElementProps,
@@ -36,7 +37,7 @@ export const Link = (props: Props) => {
36
37
  }
37
38
  };
38
39
 
39
- const handleMenuClick = (event: React.MouseEvent<HTMLDivElement>) => {
40
+ const handleMenuClick = () => {
40
41
  setOpen(false);
41
42
  };
42
43
 
@@ -59,7 +60,7 @@ export const Link = (props: Props) => {
59
60
  const getEditor = () => editor;
60
61
 
61
62
  return (
62
- <Component ref={ref} className={styles.link} contentEditable={false}>
63
+ <Component ref={ref} className={styles.link}>
63
64
  <a
64
65
  {...attributes}
65
66
  onClick={handleLinkClick}
@@ -73,21 +74,18 @@ export const Link = (props: Props) => {
73
74
  </a>
74
75
  {isOpen &&
75
76
  createPortal(
76
- <>
77
- <div className={styles.backdrop} onClick={() => setOpen(false)} />
78
- <div
79
- contentEditable={false}
80
- className={styles.hint}
81
- style={style}
82
- onMouseUp={handleMenuClick}
83
- >
84
- {renderHint({
85
- getLinkElement,
86
- getUrl,
87
- getEditor,
88
- })}
89
- </div>
90
- </>,
77
+ <div
78
+ contentEditable={false}
79
+ className={styles.hint}
80
+ style={style}
81
+ onMouseUp={handleMenuClick}
82
+ >
83
+ {renderHint({
84
+ getLinkElement,
85
+ getUrl,
86
+ getEditor,
87
+ })}
88
+ </div>,
91
89
  document.body,
92
90
  )}
93
91
  </Component>
@@ -34,9 +34,10 @@
34
34
 
35
35
  .link {
36
36
  position: relative;
37
+ padding-right: 1px;
37
38
 
38
39
  a {
39
- color: var(--link-color, #21afec);
40
+ color: #21afec;
40
41
  }
41
42
  }
42
43
 
@@ -69,5 +70,5 @@
69
70
  left: 0;
70
71
  right: 0;
71
72
  bottom: 0;
72
- z-index: 2
73
+ z-index: 2;
73
74
  }
@@ -5,13 +5,12 @@ import {
5
5
  Element,
6
6
  Node,
7
7
  type NodeEntry,
8
- type Path,
8
+ Path,
9
9
  Transforms,
10
10
  } from 'slate';
11
11
  import { jsx } from 'slate-hyperscript';
12
12
  import type { RenderElementProps } from 'slate-react';
13
13
  import type { CustomElement } from '../../../types';
14
- import { getPrev } from '../../core/queries';
15
14
  import type { IPlugin } from '../../types';
16
15
  import styles from './styles.module.css';
17
16
 
@@ -89,6 +88,11 @@ export class ListsPlugin implements IPlugin {
89
88
  }
90
89
 
91
90
  if (this.isList(editor, node as CustomElement)) {
91
+ if (!Path.hasPrevious(path)) {
92
+ normalizeNode(entry);
93
+ return;
94
+ }
95
+
92
96
  const prevListItemPath = Editor.before(editor, path, {
93
97
  unit: 'block',
94
98
  });
@@ -77,6 +77,7 @@ export class TableOfContentsPlugin implements IPlugin {
77
77
  ) as HTMLElement;
78
78
  element?.scrollIntoView({
79
79
  behavior: 'smooth',
80
+ block: 'nearest'
80
81
  });
81
82
  };
82
83
 
package/src/types.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { KeyboardEvent, ReactElement, ReactNode } from 'react';
2
- import type { DecoratedRange, Descendant, Editor, NodeEntry } from 'slate';
2
+ import type {DecoratedRange, Descendant, Editor, NodeEntry, NodeMatch} from 'slate';
3
3
  import type { RenderElementProps, RenderLeafProps } from 'slate-react';
4
4
  import type { CustomElement, CustomText } from '../types';
5
5
 
@@ -71,6 +71,9 @@ export type EditorRef = {
71
71
  deserialize: (
72
72
  element: HTMLElement,
73
73
  ) => (Descendant | string)[] | string | Descendant | null;
74
+ isEmpty: () => boolean;
75
+ deleteNode: (match: NodeMatch<CustomElement>) => void;
76
+ focus: (mode?: 'end') => void;
74
77
  };
75
78
 
76
79
  export type Serialize = (