@lexical/react 0.8.0 → 0.9.0

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.
Files changed (94) hide show
  1. package/LexicalAutoEmbedPlugin.d.ts +1 -0
  2. package/LexicalAutoEmbedPlugin.dev.js +11 -0
  3. package/LexicalAutoFocusPlugin.dev.js +1 -0
  4. package/LexicalAutoLinkPlugin.d.ts +1 -0
  5. package/LexicalAutoLinkPlugin.dev.js +57 -11
  6. package/LexicalAutoLinkPlugin.prod.js +7 -6
  7. package/LexicalBlockWithAlignableContents.dev.js +13 -2
  8. package/LexicalBlockWithAlignableContents.prod.js +4 -4
  9. package/LexicalCharacterLimitPlugin.d.ts +1 -0
  10. package/LexicalCharacterLimitPlugin.dev.js +46 -7
  11. package/LexicalCheckListPlugin.dev.js +50 -12
  12. package/LexicalCheckListPlugin.prod.js +1 -1
  13. package/LexicalClearEditorPlugin.d.ts +1 -0
  14. package/LexicalClearEditorPlugin.dev.js +1 -1
  15. package/LexicalCollaborationContext.dev.js +3 -0
  16. package/LexicalCollaborationPlugin.d.ts +3 -2
  17. package/LexicalCollaborationPlugin.dev.js +35 -6
  18. package/LexicalComposer.d.ts +1 -0
  19. package/LexicalComposer.dev.js +10 -6
  20. package/LexicalComposerContext.dev.js +6 -0
  21. package/LexicalContentEditable.dev.js +0 -1
  22. package/LexicalContentEditable.js.flow +1 -0
  23. package/LexicalDecoratorBlockNode.d.ts +1 -0
  24. package/LexicalDecoratorBlockNode.dev.js +5 -0
  25. package/LexicalErrorBoundary.d.ts +1 -0
  26. package/LexicalHashtagPlugin.d.ts +1 -0
  27. package/LexicalHashtagPlugin.dev.js +43 -73
  28. package/LexicalHorizontalRuleNode.d.ts +1 -0
  29. package/LexicalHorizontalRuleNode.dev.js +22 -0
  30. package/LexicalHorizontalRulePlugin.dev.js +4 -0
  31. package/LexicalLinkPlugin.dev.js +10 -4
  32. package/LexicalListPlugin.dev.js +2 -0
  33. package/LexicalMarkdownShortcutPlugin.dev.js +2 -2
  34. package/LexicalNestedComposer.dev.js +12 -6
  35. package/LexicalNestedComposer.prod.js +2 -2
  36. package/LexicalNodeEventPlugin.dev.js +3 -5
  37. package/LexicalOnChangePlugin.dev.js +1 -1
  38. package/LexicalPlainTextPlugin.d.ts +1 -0
  39. package/LexicalPlainTextPlugin.dev.js +12 -8
  40. package/LexicalRichTextPlugin.d.ts +1 -0
  41. package/LexicalRichTextPlugin.dev.js +12 -8
  42. package/LexicalTabIndentationPlugin.dev.js +3 -1
  43. package/{LexicalTableOfContents__EXPERIMENTAL.d.ts → LexicalTableOfContents.d.ts} +1 -0
  44. package/{LexicalTableOfContents__EXPERIMENTAL.dev.js → LexicalTableOfContents.dev.js} +33 -5
  45. package/{DEPRECATED_useLexical.js → LexicalTableOfContents.js} +2 -2
  46. package/LexicalTablePlugin.d.ts +1 -0
  47. package/LexicalTablePlugin.dev.js +19 -5
  48. package/LexicalTreeView.d.ts +1 -0
  49. package/LexicalTreeView.dev.js +113 -21
  50. package/LexicalTreeView.prod.js +16 -15
  51. package/LexicalTypeaheadMenuPlugin.dev.js +123 -17
  52. package/LexicalTypeaheadMenuPlugin.prod.js +18 -18
  53. package/package.json +19 -19
  54. package/shared/useYjsCollaboration.d.ts +3 -4
  55. package/useLexicalEditable.dev.js +5 -1
  56. package/useLexicalIsTextContentEmpty.dev.js +0 -1
  57. package/useLexicalNodeSelection.dev.js +7 -0
  58. package/useLexicalSubscription.dev.js +3 -1
  59. package/DEPRECATED_useLexical.d.ts +0 -18
  60. package/DEPRECATED_useLexical.dev.js +0 -104
  61. package/DEPRECATED_useLexical.js.flow +0 -25
  62. package/DEPRECATED_useLexical.prod.js +0 -8
  63. package/DEPRECATED_useLexicalCanShowPlaceholder.d.ts +0 -9
  64. package/DEPRECATED_useLexicalCanShowPlaceholder.dev.js +0 -72
  65. package/DEPRECATED_useLexicalCanShowPlaceholder.js +0 -9
  66. package/DEPRECATED_useLexicalCanShowPlaceholder.js.flow +0 -15
  67. package/DEPRECATED_useLexicalCanShowPlaceholder.prod.js +0 -8
  68. package/DEPRECATED_useLexicalCharacterLimit.d.ts +0 -8
  69. package/DEPRECATED_useLexicalCharacterLimit.dev.js +0 -213
  70. package/DEPRECATED_useLexicalCharacterLimit.js +0 -9
  71. package/DEPRECATED_useLexicalCharacterLimit.js.flow +0 -31
  72. package/DEPRECATED_useLexicalCharacterLimit.prod.js +0 -13
  73. package/DEPRECATED_useLexicalEditor.d.ts +0 -9
  74. package/DEPRECATED_useLexicalEditor.dev.js +0 -87
  75. package/DEPRECATED_useLexicalEditor.js +0 -9
  76. package/DEPRECATED_useLexicalEditor.prod.js +0 -8
  77. package/DEPRECATED_useLexicalHistory.d.ts +0 -12
  78. package/DEPRECATED_useLexicalHistory.dev.js +0 -38
  79. package/DEPRECATED_useLexicalHistory.js +0 -9
  80. package/DEPRECATED_useLexicalHistory.js.flow +0 -34
  81. package/DEPRECATED_useLexicalHistory.prod.js +0 -7
  82. package/DEPRECATED_useLexicalPlainText.d.ts +0 -10
  83. package/DEPRECATED_useLexicalPlainText.dev.js +0 -88
  84. package/DEPRECATED_useLexicalPlainText.js +0 -9
  85. package/DEPRECATED_useLexicalPlainText.js.flow +0 -17
  86. package/DEPRECATED_useLexicalPlainText.prod.js +0 -8
  87. package/DEPRECATED_useLexicalRichText.d.ts +0 -10
  88. package/DEPRECATED_useLexicalRichText.dev.js +0 -88
  89. package/DEPRECATED_useLexicalRichText.js +0 -9
  90. package/DEPRECATED_useLexicalRichText.js.flow +0 -17
  91. package/DEPRECATED_useLexicalRichText.prod.js +0 -8
  92. package/LexicalTableOfContents__EXPERIMENTAL.js +0 -9
  93. /package/{LexicalTableOfContents__EXPERIMENTAL.js.flow → LexicalTableOfContents.js.flow} +0 -0
  94. /package/{LexicalTableOfContents__EXPERIMENTAL.prod.js → LexicalTableOfContents.prod.js} +0 -0
@@ -5,13 +5,12 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
7
  */
8
- import type { Binding } from '@lexical/yjs';
8
+ import type { Binding, Provider } from '@lexical/yjs';
9
9
  import type { LexicalEditor } from 'lexical';
10
10
  import * as React from 'react';
11
- import { WebsocketProvider } from 'y-websocket';
12
11
  import { Doc } from 'yjs';
13
12
  import { InitialEditorStateType } from '../LexicalComposer';
14
13
  export declare type CursorsContainerRef = React.MutableRefObject<HTMLElement | null>;
15
- export declare function useYjsCollaboration(editor: LexicalEditor, id: string, provider: WebsocketProvider, docMap: Map<string, Doc>, name: string, color: string, shouldBootstrap: boolean, cursorsContainerRef?: CursorsContainerRef, initialEditorState?: InitialEditorStateType): [JSX.Element, Binding];
16
- export declare function useYjsFocusTracking(editor: LexicalEditor, provider: WebsocketProvider, name: string, color: string): void;
14
+ export declare function useYjsCollaboration(editor: LexicalEditor, id: string, provider: Provider, docMap: Map<string, Doc>, name: string, color: string, shouldBootstrap: boolean, cursorsContainerRef?: CursorsContainerRef, initialEditorState?: InitialEditorStateType): [JSX.Element, Binding];
15
+ export declare function useYjsFocusTracking(editor: LexicalEditor, provider: Provider, name: string, color: string): void;
17
16
  export declare function useYjsHistory(editor: LexicalEditor, binding: Binding): () => void;
@@ -16,7 +16,6 @@ var react = require('react');
16
16
  * LICENSE file in the root directory of this source tree.
17
17
  *
18
18
  */
19
-
20
19
  const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
21
20
 
22
21
  /**
@@ -36,6 +35,7 @@ var useLayoutEffect = useLayoutEffectImpl;
36
35
  * LICENSE file in the root directory of this source tree.
37
36
  *
38
37
  */
38
+
39
39
  /**
40
40
  * Shortcut to Lexical subscriptions when values are used for render.
41
41
  */
@@ -50,10 +50,12 @@ function useLexicalSubscription(subscription) {
50
50
  subscribe
51
51
  } = initializedSubscription;
52
52
  const currentValue = initialValueFn();
53
+
53
54
  if (valueRef.current !== currentValue) {
54
55
  valueRef.current = currentValue;
55
56
  setValue(currentValue);
56
57
  }
58
+
57
59
  return subscribe(newValue => {
58
60
  valueRef.current = newValue;
59
61
  setValue(newValue);
@@ -69,6 +71,7 @@ function useLexicalSubscription(subscription) {
69
71
  * LICENSE file in the root directory of this source tree.
70
72
  *
71
73
  */
74
+
72
75
  function subscription(editor) {
73
76
  return {
74
77
  initialValueFn: () => editor.isEditable(),
@@ -77,6 +80,7 @@ function subscription(editor) {
77
80
  }
78
81
  };
79
82
  }
83
+
80
84
  function useLexicalEditable() {
81
85
  return useLexicalSubscription(subscription);
82
86
  }
@@ -16,7 +16,6 @@ var react = require('react');
16
16
  * LICENSE file in the root directory of this source tree.
17
17
  *
18
18
  */
19
-
20
19
  const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
21
20
 
22
21
  /**
@@ -17,15 +17,19 @@ var react = require('react');
17
17
  * LICENSE file in the root directory of this source tree.
18
18
  *
19
19
  */
20
+
20
21
  function isNodeSelected(editor, key) {
21
22
  return editor.getEditorState().read(() => {
22
23
  const node = lexical.$getNodeByKey(key);
24
+
23
25
  if (node === null) {
24
26
  return false;
25
27
  }
28
+
26
29
  return node.isSelected();
27
30
  });
28
31
  }
32
+
29
33
  function useLexicalNodeSelection(key) {
30
34
  const [editor] = LexicalComposerContext.useLexicalComposerContext();
31
35
  const [isSelected, setIsSelected] = react.useState(() => isNodeSelected(editor, key));
@@ -44,10 +48,12 @@ function useLexicalNodeSelection(key) {
44
48
  const setSelected = react.useCallback(selected => {
45
49
  editor.update(() => {
46
50
  let selection = lexical.$getSelection();
51
+
47
52
  if (!lexical.$isNodeSelection(selection)) {
48
53
  selection = lexical.$createNodeSelection();
49
54
  lexical.$setSelection(selection);
50
55
  }
56
+
51
57
  if (selected) {
52
58
  selection.add(key);
53
59
  } else {
@@ -58,6 +64,7 @@ function useLexicalNodeSelection(key) {
58
64
  const clearSelected = react.useCallback(() => {
59
65
  editor.update(() => {
60
66
  const selection = lexical.$getSelection();
67
+
61
68
  if (lexical.$isNodeSelection(selection)) {
62
69
  selection.clear();
63
70
  }
@@ -16,7 +16,6 @@ var react = require('react');
16
16
  * LICENSE file in the root directory of this source tree.
17
17
  *
18
18
  */
19
-
20
19
  const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
21
20
 
22
21
  /**
@@ -36,6 +35,7 @@ var useLayoutEffect = useLayoutEffectImpl;
36
35
  * LICENSE file in the root directory of this source tree.
37
36
  *
38
37
  */
38
+
39
39
  /**
40
40
  * Shortcut to Lexical subscriptions when values are used for render.
41
41
  */
@@ -50,10 +50,12 @@ function useLexicalSubscription(subscription) {
50
50
  subscribe
51
51
  } = initializedSubscription;
52
52
  const currentValue = initialValueFn();
53
+
53
54
  if (valueRef.current !== currentValue) {
54
55
  valueRef.current = currentValue;
55
56
  setValue(currentValue);
56
57
  }
58
+
57
59
  return subscribe(newValue => {
58
60
  valueRef.current = newValue;
59
61
  setValue(newValue);
@@ -1,18 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import type { EditorState, EditorThemeClasses, Klass, LexicalEditor, LexicalNode } from 'lexical';
9
- export declare function useLexical(editorConfig: {
10
- disableEvents?: boolean;
11
- editorState?: EditorState;
12
- namespace: string;
13
- nodes?: ReadonlyArray<Klass<LexicalNode>>;
14
- onError: (error: Error) => void;
15
- parentEditor?: LexicalEditor;
16
- readOnly?: boolean;
17
- theme?: EditorThemeClasses;
18
- }): [LexicalEditor, (arg0: null | HTMLElement) => void, boolean];
@@ -1,104 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- 'use strict';
8
-
9
- var lexical = require('lexical');
10
- var react = require('react');
11
- var text = require('@lexical/text');
12
- var utils = require('@lexical/utils');
13
-
14
- /**
15
- * Copyright (c) Meta Platforms, Inc. and affiliates.
16
- *
17
- * This source code is licensed under the MIT license found in the
18
- * LICENSE file in the root directory of this source tree.
19
- *
20
- */
21
-
22
- const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
23
-
24
- /**
25
- * Copyright (c) Meta Platforms, Inc. and affiliates.
26
- *
27
- * This source code is licensed under the MIT license found in the
28
- * LICENSE file in the root directory of this source tree.
29
- *
30
- */
31
- const useLayoutEffectImpl = CAN_USE_DOM ? react.useLayoutEffect : react.useEffect;
32
- var useLayoutEffect = useLayoutEffectImpl;
33
-
34
- /**
35
- * Copyright (c) Meta Platforms, Inc. and affiliates.
36
- *
37
- * This source code is licensed under the MIT license found in the
38
- * LICENSE file in the root directory of this source tree.
39
- *
40
- */
41
- function canShowPlaceholderFromCurrentEditorState(editor) {
42
- const currentCanShowPlaceholder = editor.getEditorState().read(text.$canShowPlaceholderCurry(editor.isComposing()));
43
- return currentCanShowPlaceholder;
44
- }
45
- function useCanShowPlaceholder(editor) {
46
- const [canShowPlaceholder, setCanShowPlaceholder] = react.useState(() => canShowPlaceholderFromCurrentEditorState(editor));
47
- useLayoutEffect(() => {
48
- function resetCanShowPlaceholder() {
49
- const currentCanShowPlaceholder = canShowPlaceholderFromCurrentEditorState(editor);
50
- setCanShowPlaceholder(currentCanShowPlaceholder);
51
- }
52
- resetCanShowPlaceholder();
53
- return utils.mergeRegister(editor.registerUpdateListener(() => {
54
- resetCanShowPlaceholder();
55
- }), editor.registerEditableListener(() => {
56
- resetCanShowPlaceholder();
57
- }));
58
- }, [editor]);
59
- return canShowPlaceholder;
60
- }
61
-
62
- /**
63
- * Copyright (c) Meta Platforms, Inc. and affiliates.
64
- *
65
- * This source code is licensed under the MIT license found in the
66
- * LICENSE file in the root directory of this source tree.
67
- *
68
- */
69
- function useLexicalCanShowPlaceholder(editor) {
70
- return useCanShowPlaceholder(editor);
71
- }
72
-
73
- /**
74
- * Copyright (c) Meta Platforms, Inc. and affiliates.
75
- *
76
- * This source code is licensed under the MIT license found in the
77
- * LICENSE file in the root directory of this source tree.
78
- *
79
- */
80
- function useLexicalEditor(editor) {
81
- const showPlaceholder = useLexicalCanShowPlaceholder(editor);
82
- const rootElementRef = react.useCallback(rootElement => {
83
- editor.setRootElement(rootElement);
84
- }, [editor]);
85
- return [rootElementRef, showPlaceholder];
86
- }
87
-
88
- /**
89
- * Copyright (c) Meta Platforms, Inc. and affiliates.
90
- *
91
- * This source code is licensed under the MIT license found in the
92
- * LICENSE file in the root directory of this source tree.
93
- *
94
- */
95
- function useLexical(editorConfig) {
96
- const editor = react.useMemo(() => lexical.createEditor(editorConfig),
97
- // Init
98
- // eslint-disable-next-line react-hooks/exhaustive-deps
99
- []);
100
- const [rootElementRef, showPlaceholder] = useLexicalEditor(editor);
101
- return [editor, rootElementRef, showPlaceholder];
102
- }
103
-
104
- exports.useLexical = useLexical;
@@ -1,25 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @flow strict
8
- * @format
9
- */
10
-
11
- import type {
12
- EditorState,
13
- EditorThemeClasses,
14
- LexicalEditor,
15
- LexicalNode,
16
- } from 'lexical';
17
-
18
- declare export function useLexical<EditorContext>(editorConfig?: {
19
- namespace?: string,
20
- onError: (error: Error) => void,
21
- theme?: EditorThemeClasses,
22
- context?: EditorContext,
23
- nodes?: Array<Class<LexicalNode>>,
24
- parentEditor?: LexicalEditor,
25
- }): [LexicalEditor, (null | HTMLElement) => void, boolean];
@@ -1,8 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- 'use strict';var c=require("lexical"),f=require("react"),g=require("@lexical/text"),h=require("@lexical/utils"),k="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?f.useLayoutEffect:f.useEffect;function l(a){return a.getEditorState().read(g.$canShowPlaceholderCurry(a.isComposing()))}
8
- function m(a){let [b,d]=f.useState(()=>l(a));k(()=>{function e(){let n=l(a);d(n)}e();return h.mergeRegister(a.registerUpdateListener(()=>{e()}),a.registerEditableListener(()=>{e()}))},[a]);return b}function p(a){let b=m(a);return[f.useCallback(d=>{a.setRootElement(d)},[a]),b]}exports.useLexical=function(a){let b=f.useMemo(()=>c.createEditor(a),[]),[d,e]=p(b);return[b,d,e]}
@@ -1,9 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import type { LexicalEditor } from 'lexical';
9
- export declare function useLexicalCanShowPlaceholder(editor: LexicalEditor): boolean;
@@ -1,72 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- 'use strict';
8
-
9
- var text = require('@lexical/text');
10
- var utils = require('@lexical/utils');
11
- var react = require('react');
12
-
13
- /**
14
- * Copyright (c) Meta Platforms, Inc. and affiliates.
15
- *
16
- * This source code is licensed under the MIT license found in the
17
- * LICENSE file in the root directory of this source tree.
18
- *
19
- */
20
-
21
- const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
22
-
23
- /**
24
- * Copyright (c) Meta Platforms, Inc. and affiliates.
25
- *
26
- * This source code is licensed under the MIT license found in the
27
- * LICENSE file in the root directory of this source tree.
28
- *
29
- */
30
- const useLayoutEffectImpl = CAN_USE_DOM ? react.useLayoutEffect : react.useEffect;
31
- var useLayoutEffect = useLayoutEffectImpl;
32
-
33
- /**
34
- * Copyright (c) Meta Platforms, Inc. and affiliates.
35
- *
36
- * This source code is licensed under the MIT license found in the
37
- * LICENSE file in the root directory of this source tree.
38
- *
39
- */
40
- function canShowPlaceholderFromCurrentEditorState(editor) {
41
- const currentCanShowPlaceholder = editor.getEditorState().read(text.$canShowPlaceholderCurry(editor.isComposing()));
42
- return currentCanShowPlaceholder;
43
- }
44
- function useCanShowPlaceholder(editor) {
45
- const [canShowPlaceholder, setCanShowPlaceholder] = react.useState(() => canShowPlaceholderFromCurrentEditorState(editor));
46
- useLayoutEffect(() => {
47
- function resetCanShowPlaceholder() {
48
- const currentCanShowPlaceholder = canShowPlaceholderFromCurrentEditorState(editor);
49
- setCanShowPlaceholder(currentCanShowPlaceholder);
50
- }
51
- resetCanShowPlaceholder();
52
- return utils.mergeRegister(editor.registerUpdateListener(() => {
53
- resetCanShowPlaceholder();
54
- }), editor.registerEditableListener(() => {
55
- resetCanShowPlaceholder();
56
- }));
57
- }, [editor]);
58
- return canShowPlaceholder;
59
- }
60
-
61
- /**
62
- * Copyright (c) Meta Platforms, Inc. and affiliates.
63
- *
64
- * This source code is licensed under the MIT license found in the
65
- * LICENSE file in the root directory of this source tree.
66
- *
67
- */
68
- function useLexicalCanShowPlaceholder(editor) {
69
- return useCanShowPlaceholder(editor);
70
- }
71
-
72
- exports.useLexicalCanShowPlaceholder = useLexicalCanShowPlaceholder;
@@ -1,9 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- 'use strict'
8
- const DEPRECATED_useLexicalCanShowPlaceholder = process.env.NODE_ENV === 'development' ? require('./DEPRECATED_useLexicalCanShowPlaceholder.dev.js') : require('./DEPRECATED_useLexicalCanShowPlaceholder.prod.js')
9
- module.exports = DEPRECATED_useLexicalCanShowPlaceholder;
@@ -1,15 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @flow strict
8
- * @format
9
- */
10
-
11
- import type {LexicalEditor} from 'lexical';
12
-
13
- declare export function useLexicalCanShowPlaceholder(
14
- editor: LexicalEditor,
15
- ): boolean;
@@ -1,8 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- 'use strict';var b=require("@lexical/text"),d=require("@lexical/utils"),e=require("react"),f="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?e.useLayoutEffect:e.useEffect;function g(a){return a.getEditorState().read(b.$canShowPlaceholderCurry(a.isComposing()))}
8
- function h(a){let [k,l]=e.useState(()=>g(a));f(()=>{function c(){let m=g(a);l(m)}c();return d.mergeRegister(a.registerUpdateListener(()=>{c()}),a.registerEditableListener(()=>{c()}))},[a]);return k}exports.useLexicalCanShowPlaceholder=function(a){return h(a)}
@@ -1,8 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- export { mergePrevious, useCharacterLimit } from './shared/useCharacterLimit';
@@ -1,213 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- 'use strict';
8
-
9
- var overflow = require('@lexical/overflow');
10
- var text = require('@lexical/text');
11
- var utils = require('@lexical/utils');
12
- var lexical = require('lexical');
13
- var react = require('react');
14
-
15
- /**
16
- * Copyright (c) Meta Platforms, Inc. and affiliates.
17
- *
18
- * This source code is licensed under the MIT license found in the
19
- * LICENSE file in the root directory of this source tree.
20
- *
21
- */
22
- function useCharacterLimit(editor, maxCharacters, optional = Object.freeze({})) {
23
- const {
24
- strlen = input => input.length,
25
- // UTF-16
26
- remainingCharacters = () => {
27
- return;
28
- }
29
- } = optional;
30
- react.useEffect(() => {
31
- if (!editor.hasNodes([overflow.OverflowNode])) {
32
- {
33
- throw Error(`useCharacterLimit: OverflowNode not registered on editor`);
34
- }
35
- }
36
- }, [editor]);
37
- react.useEffect(() => {
38
- let text$1 = editor.getEditorState().read(text.$rootTextContent);
39
- let lastComputedTextLength = 0;
40
- return utils.mergeRegister(editor.registerTextContentListener(currentText => {
41
- text$1 = currentText;
42
- }), editor.registerUpdateListener(({
43
- dirtyLeaves
44
- }) => {
45
- const isComposing = editor.isComposing();
46
- const hasDirtyLeaves = dirtyLeaves.size > 0;
47
- if (isComposing || !hasDirtyLeaves) {
48
- return;
49
- }
50
- const textLength = strlen(text$1);
51
- const textLengthAboveThreshold = textLength > maxCharacters || lastComputedTextLength !== null && lastComputedTextLength > maxCharacters;
52
- const diff = maxCharacters - textLength;
53
- remainingCharacters(diff);
54
- if (lastComputedTextLength === null || textLengthAboveThreshold) {
55
- const offset = findOffset(text$1, maxCharacters, strlen);
56
- editor.update(() => {
57
- $wrapOverflowedNodes(offset);
58
- }, {
59
- tag: 'history-merge'
60
- });
61
- }
62
- lastComputedTextLength = textLength;
63
- }));
64
- }, [editor, maxCharacters, remainingCharacters, strlen]);
65
- }
66
- function findOffset(text, maxCharacters, strlen) {
67
- // @ts-ignore This is due to be added in a later version of TS
68
- const Segmenter = Intl.Segmenter;
69
- let offsetUtf16 = 0;
70
- let offset = 0;
71
- if (typeof Segmenter === 'function') {
72
- const segmenter = new Segmenter();
73
- const graphemes = segmenter.segment(text);
74
- for (const {
75
- segment: grapheme
76
- } of graphemes) {
77
- const nextOffset = offset + strlen(grapheme);
78
- if (nextOffset > maxCharacters) {
79
- break;
80
- }
81
- offset = nextOffset;
82
- offsetUtf16 += grapheme.length;
83
- }
84
- } else {
85
- const codepoints = Array.from(text);
86
- const codepointsLength = codepoints.length;
87
- for (let i = 0; i < codepointsLength; i++) {
88
- const codepoint = codepoints[i];
89
- const nextOffset = offset + strlen(codepoint);
90
- if (nextOffset > maxCharacters) {
91
- break;
92
- }
93
- offset = nextOffset;
94
- offsetUtf16 += codepoint.length;
95
- }
96
- }
97
- return offsetUtf16;
98
- }
99
- function $wrapOverflowedNodes(offset) {
100
- const dfsNodes = utils.$dfs();
101
- const dfsNodesLength = dfsNodes.length;
102
- let accumulatedLength = 0;
103
- for (let i = 0; i < dfsNodesLength; i += 1) {
104
- const {
105
- node
106
- } = dfsNodes[i];
107
- if (overflow.$isOverflowNode(node)) {
108
- const previousLength = accumulatedLength;
109
- const nextLength = accumulatedLength + node.getTextContentSize();
110
- if (nextLength <= offset) {
111
- const parent = node.getParent();
112
- const previousSibling = node.getPreviousSibling();
113
- const nextSibling = node.getNextSibling();
114
- $unwrapNode(node);
115
- const selection = lexical.$getSelection();
116
-
117
- // Restore selection when the overflow children are removed
118
- if (lexical.$isRangeSelection(selection) && (!selection.anchor.getNode().isAttached() || !selection.focus.getNode().isAttached())) {
119
- if (lexical.$isTextNode(previousSibling)) {
120
- previousSibling.select();
121
- } else if (lexical.$isTextNode(nextSibling)) {
122
- nextSibling.select();
123
- } else if (parent !== null) {
124
- parent.select();
125
- }
126
- }
127
- } else if (previousLength < offset) {
128
- const descendant = node.getFirstDescendant();
129
- const descendantLength = descendant !== null ? descendant.getTextContentSize() : 0;
130
- const previousPlusDescendantLength = previousLength + descendantLength;
131
- // For simple text we can redimension the overflow into a smaller and more accurate
132
- // container
133
- const firstDescendantIsSimpleText = lexical.$isTextNode(descendant) && descendant.isSimpleText();
134
- const firstDescendantDoesNotOverflow = previousPlusDescendantLength <= offset;
135
- if (firstDescendantIsSimpleText || firstDescendantDoesNotOverflow) {
136
- $unwrapNode(node);
137
- }
138
- }
139
- } else if (lexical.$isLeafNode(node)) {
140
- const previousAccumulatedLength = accumulatedLength;
141
- accumulatedLength += node.getTextContentSize();
142
- if (accumulatedLength > offset && !overflow.$isOverflowNode(node.getParent())) {
143
- const previousSelection = lexical.$getSelection();
144
- let overflowNode;
145
-
146
- // For simple text we can improve the limit accuracy by splitting the TextNode
147
- // on the split point
148
- if (previousAccumulatedLength < offset && lexical.$isTextNode(node) && node.isSimpleText()) {
149
- const [, overflowedText] = node.splitText(offset - previousAccumulatedLength);
150
- overflowNode = $wrapNode(overflowedText);
151
- } else {
152
- overflowNode = $wrapNode(node);
153
- }
154
- if (previousSelection !== null) {
155
- lexical.$setSelection(previousSelection);
156
- }
157
- mergePrevious(overflowNode);
158
- }
159
- }
160
- }
161
- }
162
- function $wrapNode(node) {
163
- const overflowNode = overflow.$createOverflowNode();
164
- node.insertBefore(overflowNode);
165
- overflowNode.append(node);
166
- return overflowNode;
167
- }
168
- function $unwrapNode(node) {
169
- const children = node.getChildren();
170
- const childrenLength = children.length;
171
- for (let i = 0; i < childrenLength; i++) {
172
- node.insertBefore(children[i]);
173
- }
174
- node.remove();
175
- return childrenLength > 0 ? children[childrenLength - 1] : null;
176
- }
177
- function mergePrevious(overflowNode) {
178
- const previousNode = overflowNode.getPreviousSibling();
179
- if (!overflow.$isOverflowNode(previousNode)) {
180
- return;
181
- }
182
- const firstChild = overflowNode.getFirstChild();
183
- const previousNodeChildren = previousNode.getChildren();
184
- const previousNodeChildrenLength = previousNodeChildren.length;
185
- if (firstChild === null) {
186
- overflowNode.append(...previousNodeChildren);
187
- } else {
188
- for (let i = 0; i < previousNodeChildrenLength; i++) {
189
- firstChild.insertBefore(previousNodeChildren[i]);
190
- }
191
- }
192
- const selection = lexical.$getSelection();
193
- if (lexical.$isRangeSelection(selection)) {
194
- const anchor = selection.anchor;
195
- const anchorNode = anchor.getNode();
196
- const focus = selection.focus;
197
- const focusNode = anchor.getNode();
198
- if (anchorNode.is(previousNode)) {
199
- anchor.set(overflowNode.getKey(), anchor.offset, 'element');
200
- } else if (anchorNode.is(overflowNode)) {
201
- anchor.set(overflowNode.getKey(), previousNodeChildrenLength + anchor.offset, 'element');
202
- }
203
- if (focusNode.is(previousNode)) {
204
- focus.set(overflowNode.getKey(), focus.offset, 'element');
205
- } else if (focusNode.is(overflowNode)) {
206
- focus.set(overflowNode.getKey(), previousNodeChildrenLength + focus.offset, 'element');
207
- }
208
- }
209
- previousNode.remove();
210
- }
211
-
212
- exports.mergePrevious = mergePrevious;
213
- exports.useCharacterLimit = useCharacterLimit;
@@ -1,9 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- 'use strict'
8
- const DEPRECATED_useLexicalCharacterLimit = process.env.NODE_ENV === 'development' ? require('./DEPRECATED_useLexicalCharacterLimit.dev.js') : require('./DEPRECATED_useLexicalCharacterLimit.prod.js')
9
- module.exports = DEPRECATED_useLexicalCharacterLimit;