@yuntijs/ui 1.2.0-beta.1 → 1.2.0-beta.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.
@@ -26,6 +26,13 @@ export interface MentionsProps extends MentionPickerPluginProps {
26
26
  onPressEnter?: (value: string, { event }: {
27
27
  event: KeyboardEvent | null;
28
28
  }) => void;
29
+ /**
30
+ * 用户输入 trigger 后的回调
31
+ */
32
+ /**
33
+ * 按下键盘的回调
34
+ */
35
+ onKeyDown?: (event: KeyboardEvent | null) => void;
29
36
  /**
30
37
  * 用户输入 trigger 后的回调
31
38
  */
@@ -19,6 +19,7 @@ import { EditorRefPlugin } from "./plugins/editor-ref";
19
19
  import { MentionNode, MentionNodePlugin, MentionNodePluginReplacement } from "./plugins/mention-node";
20
20
  import { MentionPickerPlugin } from "./plugins/mention-picker";
21
21
  import { OnBlurBlockPlugin } from "./plugins/on-blur-block";
22
+ import { OnKeyDownPlugin } from "./plugins/on-key-down";
22
23
  import { ShiftEnterKeyPlugin } from "./plugins/shift-enter-key";
23
24
  import { MentionsConfigProvider } from "./provider";
24
25
  import { useStyles } from "./style";
@@ -56,6 +57,7 @@ export var Mentions = /*#__PURE__*/forwardRef(function (_ref, ref) {
56
57
  code = _ref$code === void 0 ? false : _ref$code,
57
58
  getPopContainer = _ref.getPopContainer,
58
59
  onPressEnter = _ref.onPressEnter,
60
+ onKeyDown = _ref.onKeyDown,
59
61
  onTrigger = _ref.onTrigger;
60
62
  var _ConfigProvider$useCo = ConfigProvider.useConfig(),
61
63
  componentDisabled = _ConfigProvider$useCo.componentDisabled;
@@ -171,6 +173,8 @@ export var Mentions = /*#__PURE__*/forwardRef(function (_ref, ref) {
171
173
  editorRef: ref
172
174
  }), /*#__PURE__*/_jsx(ClearEditorPlugin, {}), onPressEnter && /*#__PURE__*/_jsx(ShiftEnterKeyPlugin, {
173
175
  onPressEnter: onPressEnter
176
+ }), onKeyDown && /*#__PURE__*/_jsx(OnKeyDownPlugin, {
177
+ onKeyDown: onKeyDown
174
178
  })]
175
179
  })
176
180
  })
@@ -0,0 +1,6 @@
1
+ import React from 'react';
2
+ import type { MentionsProps } from '..';
3
+ export interface OnKeyDownPluginProps {
4
+ onKeyDown: Required<MentionsProps>['onKeyDown'];
5
+ }
6
+ export declare const OnKeyDownPlugin: React.FC<OnKeyDownPluginProps>;
@@ -0,0 +1,31 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
3
+ import { COMMAND_PRIORITY_LOW, KEY_DOWN_COMMAND } from 'lexical';
4
+ import { useEffect, useRef } from 'react';
5
+ export var OnKeyDownPlugin = function OnKeyDownPlugin(_ref) {
6
+ var onKeyDown = _ref.onKeyDown;
7
+ var _useLexicalComposerCo = useLexicalComposerContext(),
8
+ _useLexicalComposerCo2 = _slicedToArray(_useLexicalComposerCo, 1),
9
+ editor = _useLexicalComposerCo2[0];
10
+ var onKeyDownRef = useRef(onKeyDown);
11
+ useEffect(function () {
12
+ onKeyDownRef.current = onKeyDown;
13
+ }, [onKeyDown]);
14
+ useEffect(function () {
15
+ // https://github.com/facebook/lexical/discussions/4464#discussioncomment-5833227
16
+ editor.registerCommand(KEY_DOWN_COMMAND, function (event) {
17
+ // 这里把 onKeyDown 放在下一次事件循环中触发,是为了避免跟 Lexical 还未结束的输入等事务发生冲突
18
+ if (window.queueMicrotask === undefined) {
19
+ setTimeout(function () {
20
+ onKeyDownRef.current(event);
21
+ }, 0);
22
+ } else {
23
+ queueMicrotask(function () {
24
+ onKeyDownRef.current(event);
25
+ });
26
+ }
27
+ return false;
28
+ }, COMMAND_PRIORITY_LOW);
29
+ }, [editor]);
30
+ return null;
31
+ };
@@ -2,12 +2,16 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
2
  import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
3
3
  import { CAN_USE_BEFORE_INPUT, IS_APPLE_WEBKIT, IS_IOS, IS_SAFARI } from '@lexical/utils';
4
4
  import { $getRoot, $getSelection, $isRangeSelection, COMMAND_PRIORITY_LOW, INSERT_PARAGRAPH_COMMAND, KEY_ENTER_COMMAND } from 'lexical';
5
- import { useEffect } from 'react';
5
+ import { useEffect, useRef } from 'react';
6
6
  export var ShiftEnterKeyPlugin = function ShiftEnterKeyPlugin(_ref) {
7
7
  var onPressEnter = _ref.onPressEnter;
8
8
  var _useLexicalComposerCo = useLexicalComposerContext(),
9
9
  _useLexicalComposerCo2 = _slicedToArray(_useLexicalComposerCo, 1),
10
10
  editor = _useLexicalComposerCo2[0];
11
+ var onPressEnterRef = useRef(onPressEnter);
12
+ useEffect(function () {
13
+ onPressEnterRef.current = onPressEnter;
14
+ }, [onPressEnter]);
11
15
  useEffect(function () {
12
16
  // https://github.com/facebook/lexical/discussions/4464#discussioncomment-5833227
13
17
  editor.registerCommand(KEY_ENTER_COMMAND, function (event) {
@@ -39,13 +43,13 @@ export var ShiftEnterKeyPlugin = function ShiftEnterKeyPlugin(_ref) {
39
43
  // 这里把 onPressEnter 放在下一次事件循环中触发,是为了避免跟 Lexical 还未结束的输入等事务发生冲突
40
44
  if (window.queueMicrotask === undefined) {
41
45
  setTimeout(function () {
42
- onPressEnter(value, {
46
+ onPressEnterRef.current(value, {
43
47
  event: event
44
48
  });
45
49
  }, 0);
46
50
  } else {
47
51
  queueMicrotask(function () {
48
- onPressEnter(value, {
52
+ onPressEnterRef.current(value, {
49
53
  event: event
50
54
  });
51
55
  });
@@ -54,6 +58,6 @@ export var ShiftEnterKeyPlugin = function ShiftEnterKeyPlugin(_ref) {
54
58
  },
55
59
  // 优先级要低于 MentionPickerPlugin
56
60
  COMMAND_PRIORITY_LOW);
57
- }, [editor, onPressEnter]);
61
+ }, [editor]);
58
62
  return null;
59
63
  };
@@ -6,4 +6,8 @@ export declare const DELETE_MENTION_COMMAND: import("lexical").LexicalCommand<un
6
6
  export declare const CLEAR_HIDE_MENU_TIMEOUT: import("lexical").LexicalCommand<unknown>;
7
7
  export declare const UPDATE_MENTIONS_OPTIONS: import("lexical").LexicalCommand<unknown>;
8
8
  export declare const decoratorTransform: (node: CustomTextNode, getMatch: (text: string) => null | EntityMatch, createNode: (textNode: TextNode) => LexicalNode) => void;
9
- export declare function textToEditorState(initialValue: string, triggers: string[]): () => void;
9
+ export interface TextToEditorStateOptions {
10
+ /** 光标位置,默认为 end */
11
+ cursor?: 'start' | 'end' | 'all';
12
+ }
13
+ export declare function textToEditorState(initialValue: string, triggers: string[], options?: TextToEditorStateOptions): () => void;
@@ -1,6 +1,6 @@
1
1
  import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
2
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
- import { $createParagraphNode, $getRoot, $isTextNode, createCommand } from 'lexical';
3
+ import { $createParagraphNode, $getRoot, $isTextNode, $selectAll, createCommand } from 'lexical';
4
4
  import { $convertToMentionNodes } from "./plugins/mention-converter";
5
5
  export var INSERT_MENTION_COMMAND = createCommand('INSERT_MENTION_COMMAND');
6
6
  export var DELETE_MENTION_COMMAND = createCommand('DELETE_MENTION_COMMAND');
@@ -65,12 +65,26 @@ export var decoratorTransform = function decoratorTransform(node, getMatch, crea
65
65
  }
66
66
  }
67
67
  };
68
- export function textToEditorState(initialValue, triggers) {
68
+ export function textToEditorState(initialValue, triggers, options) {
69
69
  return function () {
70
70
  var root = $getRoot();
71
71
  root.clear();
72
72
  var paragraph = $createParagraphNode();
73
73
  paragraph.append.apply(paragraph, _toConsumableArray($convertToMentionNodes(initialValue, triggers)));
74
74
  root.append(paragraph);
75
+ var _ref = options || {
76
+ cursor: 'end'
77
+ },
78
+ cursor = _ref.cursor;
79
+ if (!cursor || cursor === 'end') {
80
+ // 👇 把光标移动到文本末尾
81
+ var lastNode = paragraph.getLastDescendant();
82
+ if ($isTextNode(lastNode)) {
83
+ lastNode.select(); // 选中并聚焦尾部
84
+ }
85
+ }
86
+ if (cursor === 'all') {
87
+ $selectAll();
88
+ }
75
89
  };
76
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuntijs/ui",
3
- "version": "1.2.0-beta.1",
3
+ "version": "1.2.0-beta.3",
4
4
  "description": "☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps",
5
5
  "keywords": [
6
6
  "yuntijs",