@yuntijs/ui 1.1.0-beta.7 → 1.1.0-beta.8

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.
@@ -19,6 +19,12 @@ export interface MentionsProps extends MentionPickerPluginProps {
19
19
  onChange?: (text: string) => void;
20
20
  onBlur?: () => void;
21
21
  onFocus?: () => void;
22
+ /**
23
+ * 按下回车的回调,指定后会改变回车的默认行为,换行需要使用 shift + enter
24
+ */
25
+ onPressEnter?: (value: string, { event }: {
26
+ event: KeyboardEvent | null;
27
+ }) => void;
22
28
  variant?: 'outlined' | 'filled' | 'borderless';
23
29
  autoSize?: AutoSize;
24
30
  code?: boolean;
@@ -18,6 +18,7 @@ import { EditorRefPlugin } from "./plugins/editor-ref";
18
18
  import { MentionNode, MentionNodePlugin, MentionNodePluginReplacement } from "./plugins/mention-node";
19
19
  import { MentionPickerPlugin } from "./plugins/mention-picker";
20
20
  import { OnBlurBlockPlugin } from "./plugins/on-blur-block";
21
+ import { ShiftEnterKeyPlugin } from "./plugins/shift-enter-key";
21
22
  import { MentionsConfigProvider } from "./provider";
22
23
  import { useStyles } from "./style";
23
24
  import { textToEditorState } from "./utils";
@@ -51,7 +52,8 @@ export var Mentions = /*#__PURE__*/forwardRef(function (_ref, ref) {
51
52
  onSelect = _ref.onSelect,
52
53
  _ref$code = _ref.code,
53
54
  code = _ref$code === void 0 ? false : _ref$code,
54
- getPopContainer = _ref.getPopContainer;
55
+ getPopContainer = _ref.getPopContainer,
56
+ onPressEnter = _ref.onPressEnter;
55
57
  var _ConfigProvider$useCo = ConfigProvider.useConfig(),
56
58
  componentDisabled = _ConfigProvider$useCo.componentDisabled;
57
59
  var _useStyles = useStyles({
@@ -160,6 +162,8 @@ export var Mentions = /*#__PURE__*/forwardRef(function (_ref, ref) {
160
162
  editable: editable
161
163
  }), /*#__PURE__*/_jsx(EditorRefPlugin, {
162
164
  editorRef: ref
165
+ }), onPressEnter && /*#__PURE__*/_jsx(ShiftEnterKeyPlugin, {
166
+ onPressEnter: onPressEnter
163
167
  })]
164
168
  })
165
169
  })
@@ -3,6 +3,7 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
3
  import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
4
4
  import { LexicalTypeaheadMenuPlugin } from '@lexical/react/LexicalTypeaheadMenuPlugin';
5
5
  import { ConfigProvider, Tree } from 'antd';
6
+ import { COMMAND_PRIORITY_NORMAL } from 'lexical';
6
7
  import { flatMap } from 'lodash-es';
7
8
  import React, { memo, useCallback, useMemo, useRef, useState } from 'react';
8
9
  import ReactDOM from 'react-dom';
@@ -163,7 +164,10 @@ export var MentionPickerPlugin = /*#__PURE__*/memo(function (_ref) {
163
164
  return null;
164
165
  }, [cx, handleDisabledItem, options, overlayClassName, queryString, renderMenuTree, styles.menuOverlay]);
165
166
  return /*#__PURE__*/_jsx(LexicalTypeaheadMenuPlugin, {
166
- anchorClassName: styles.anchor,
167
+ anchorClassName: styles.anchor
168
+ // 优先级要高于 ShiftEnterKeyPlugin
169
+ ,
170
+ commandPriority: COMMAND_PRIORITY_NORMAL,
167
171
  menuRenderFn: renderMenu,
168
172
  onQueryChange: setQueryString,
169
173
  onSelectOption: onSelectOption,
@@ -0,0 +1,6 @@
1
+ import React from 'react';
2
+ import type { MentionsProps } from '..';
3
+ export interface ShiftEnterKeyPluginProps {
4
+ onPressEnter: Required<MentionsProps>['onPressEnter'];
5
+ }
6
+ export declare const ShiftEnterKeyPlugin: React.FC<ShiftEnterKeyPluginProps>;
@@ -0,0 +1,48 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
3
+ import { CAN_USE_BEFORE_INPUT, IS_APPLE_WEBKIT, IS_IOS, IS_SAFARI } from '@lexical/utils';
4
+ import { $getRoot, $getSelection, $isRangeSelection, COMMAND_PRIORITY_LOW, INSERT_PARAGRAPH_COMMAND, KEY_ENTER_COMMAND } from 'lexical';
5
+ import { useEffect } from 'react';
6
+ export var ShiftEnterKeyPlugin = function ShiftEnterKeyPlugin(_ref) {
7
+ var onPressEnter = _ref.onPressEnter;
8
+ var _useLexicalComposerCo = useLexicalComposerContext(),
9
+ _useLexicalComposerCo2 = _slicedToArray(_useLexicalComposerCo, 1),
10
+ editor = _useLexicalComposerCo2[0];
11
+ useEffect(function () {
12
+ // https://github.com/facebook/lexical/discussions/4464#discussioncomment-5833227
13
+ editor.registerCommand(KEY_ENTER_COMMAND, function (event) {
14
+ var selection = $getSelection();
15
+ if (!$isRangeSelection(selection)) {
16
+ return false;
17
+ }
18
+ if (event !== null) {
19
+ // If we have beforeinput, then we can avoid blocking
20
+ // the default behavior. This ensures that the iOS can
21
+ // intercept that we're actually inserting a paragraph,
22
+ // and autocomplete, autocapitalize etc work as intended.
23
+ // This can also cause a strange performance issue in
24
+ // Safari, where there is a noticeable pause due to
25
+ // preventing the key down of enter.
26
+ if ((IS_IOS || IS_SAFARI || IS_APPLE_WEBKIT) && CAN_USE_BEFORE_INPUT) {
27
+ return false;
28
+ }
29
+ event.preventDefault();
30
+ if (event.shiftKey) {
31
+ return editor.dispatchCommand(INSERT_PARAGRAPH_COMMAND, void 0);
32
+ }
33
+ }
34
+ event === null || event === void 0 || event.preventDefault();
35
+ var text = editor.read(function () {
36
+ return $getRoot().getTextContent();
37
+ });
38
+ var value = text.replaceAll('\n\n', '\n');
39
+ onPressEnter(value, {
40
+ event: event
41
+ });
42
+ return true;
43
+ },
44
+ // 优先级要低于 MentionPickerPlugin
45
+ COMMAND_PRIORITY_LOW);
46
+ }, [editor, onPressEnter]);
47
+ return null;
48
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuntijs/ui",
3
- "version": "1.1.0-beta.7",
3
+ "version": "1.1.0-beta.8",
4
4
  "description": "☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps",
5
5
  "keywords": [
6
6
  "yuntijs",