@lexical/plain-text 0.4.0 → 0.5.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.
@@ -30,12 +30,6 @@ const IS_IOS = CAN_USE_DOM && /iPad|iPhone|iPod/.test(navigator.userAgent) && !w
30
30
  // export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
31
31
 
32
32
  /** @module @lexical/plain-text */
33
- // Convoluted logic to make this work with Flow. Order matters.
34
- const options = {
35
- tag: 'history-merge'
36
- };
37
- const setEditorOptions = options;
38
- const updateOptions = options;
39
33
 
40
34
  function onCopyForPlainText(event, editor) {
41
35
  editor.update(() => {
@@ -59,7 +53,7 @@ function onPasteForPlainText(event, editor) {
59
53
  event.preventDefault();
60
54
  editor.update(() => {
61
55
  const selection = lexical.$getSelection();
62
- const clipboardData = event instanceof InputEvent ? null : event.clipboardData;
56
+ const clipboardData = event instanceof InputEvent || event instanceof KeyboardEvent ? null : event.clipboardData;
63
57
 
64
58
  if (clipboardData != null && lexical.$isRangeSelection(selection)) {
65
59
  clipboard.$insertDataTransferForPlainText(clipboardData, selection);
@@ -80,54 +74,7 @@ function onCutForPlainText(event, editor) {
80
74
  });
81
75
  }
82
76
 
83
- function initializeEditor(editor, initialEditorState) {
84
- if (initialEditorState === null) {
85
- return;
86
- } else if (initialEditorState === undefined) {
87
- editor.update(() => {
88
- const root = lexical.$getRoot();
89
-
90
- if (root.isEmpty()) {
91
- const paragraph = lexical.$createParagraphNode();
92
- root.append(paragraph);
93
- const activeElement = document.activeElement;
94
-
95
- if (lexical.$getSelection() !== null || activeElement !== null && activeElement === editor.getRootElement()) {
96
- paragraph.select();
97
- }
98
- }
99
- }, updateOptions);
100
- } else if (initialEditorState !== null) {
101
- switch (typeof initialEditorState) {
102
- case 'string':
103
- {
104
- const parsedEditorState = editor.parseEditorState(initialEditorState);
105
- editor.setEditorState(parsedEditorState, setEditorOptions);
106
- break;
107
- }
108
-
109
- case 'object':
110
- {
111
- editor.setEditorState(initialEditorState, setEditorOptions);
112
- break;
113
- }
114
-
115
- case 'function':
116
- {
117
- editor.update(() => {
118
- const root = lexical.$getRoot();
119
-
120
- if (root.isEmpty()) {
121
- initialEditorState(editor);
122
- }
123
- }, updateOptions);
124
- break;
125
- }
126
- }
127
- }
128
- }
129
-
130
- function registerPlainText(editor, initialEditorState) {
77
+ function registerPlainText(editor) {
131
78
  const removeListener = utils.mergeRegister(editor.registerCommand(lexical.DELETE_CHARACTER_COMMAND, isBackward => {
132
79
  const selection = lexical.$getSelection();
133
80
 
@@ -329,7 +276,6 @@ function registerPlainText(editor, initialEditorState) {
329
276
  event.preventDefault();
330
277
  return true;
331
278
  }, lexical.COMMAND_PRIORITY_EDITOR));
332
- initializeEditor(editor, initialEditorState);
333
279
  return removeListener;
334
280
  }
335
281
 
@@ -7,13 +7,5 @@
7
7
  * @flow strict
8
8
  */
9
9
  import type {EditorState, LexicalEditor} from 'lexical';
10
- export type InitialEditorStateType =
11
- | null
12
- | string
13
- | EditorState
14
- | ((editor: LexicalEditor) => void);
15
10
 
16
- declare export function registerPlainText(
17
- editor: LexicalEditor,
18
- initialEditorState?: InitialEditorStateType,
19
- ): () => void;
11
+ declare export function registerPlainText(editor: LexicalEditor): () => void;
@@ -4,14 +4,13 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- 'use strict';var a=require("@lexical/clipboard"),g=require("@lexical/selection"),k=require("@lexical/utils"),l=require("lexical");let m="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement,n=m&&"documentMode"in document?document.documentMode:null;m&&/Mac|iPod|iPhone|iPad/.test(navigator.platform);m&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
8
- let p=m&&"InputEvent"in window&&!n?"getTargetRanges"in new window.InputEvent("input"):!1,q=m&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),r=m&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream,t={tag:"history-merge"};function u(d,f){f.update(()=>{let e=d instanceof KeyboardEvent?null:d.clipboardData,b=l.$getSelection();if(null!==b&&null!=e){d.preventDefault();let c=a.$getHtmlContent(f);null!==c&&e.setData("text/html",c);e.setData("text/plain",b.getTextContent())}})}
9
- function v(d,f){d.preventDefault();f.update(()=>{let e=l.$getSelection(),b=d instanceof InputEvent?null:d.clipboardData;null!=b&&l.$isRangeSelection(e)&&a.$insertDataTransferForPlainText(b,e)},{tag:"paste"})}function w(d,f){u(d,f);f.update(()=>{let e=l.$getSelection();l.$isRangeSelection(e)&&e.removeText()})}
10
- function x(d,f){if(null!==f)if(void 0===f)d.update(()=>{var e=l.$getRoot();if(e.isEmpty()){let b=l.$createParagraphNode();e.append(b);e=document.activeElement;(null!==l.$getSelection()||null!==e&&e===d.getRootElement())&&b.select()}},t);else if(null!==f)switch(typeof f){case "string":let e=d.parseEditorState(f);d.setEditorState(e,t);break;case "object":d.setEditorState(f,t);break;case "function":d.update(()=>{l.$getRoot().isEmpty()&&f(d)},t)}}
11
- exports.registerPlainText=function(d,f){let e=k.mergeRegister(d.registerCommand(l.DELETE_CHARACTER_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;c.deleteCharacter(b);return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.DELETE_WORD_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;c.deleteWord(b);return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.DELETE_LINE_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;c.deleteLine(b);
12
- return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.CONTROLLED_TEXT_INSERTION_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;if("string"===typeof b)c.insertText(b);else{const h=b.dataTransfer;null!=h?a.$insertDataTransferForPlainText(h,c):(b=b.data)&&c.insertText(b)}return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.REMOVE_TEXT_COMMAND,()=>{const b=l.$getSelection();if(!l.$isRangeSelection(b))return!1;b.removeText();return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.INSERT_LINE_BREAK_COMMAND,
13
- b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;c.insertLineBreak(b);return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.INSERT_PARAGRAPH_COMMAND,()=>{const b=l.$getSelection();if(!l.$isRangeSelection(b))return!1;b.insertLineBreak();return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.KEY_ARROW_LEFT_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;const h=b.shiftKey;return g.$shouldOverrideDefaultCharacterSelection(c,!0)?(b.preventDefault(),g.$moveCharacter(c,
14
- h,!0),!0):!1},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.KEY_ARROW_RIGHT_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;const h=b.shiftKey;return g.$shouldOverrideDefaultCharacterSelection(c,!1)?(b.preventDefault(),g.$moveCharacter(c,h,!1),!0):!1},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.KEY_BACKSPACE_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;b.preventDefault();return d.dispatchCommand(l.DELETE_CHARACTER_COMMAND,!0)},l.COMMAND_PRIORITY_EDITOR),
15
- d.registerCommand(l.KEY_DELETE_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;b.preventDefault();return d.dispatchCommand(l.DELETE_CHARACTER_COMMAND,!1)},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.KEY_ENTER_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;if(null!==b){if((r||q)&&p)return!1;b.preventDefault()}return d.dispatchCommand(l.INSERT_LINE_BREAK_COMMAND,!1)},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.COPY_COMMAND,b=>{const c=l.$getSelection();
16
- if(!l.$isRangeSelection(c))return!1;u(b,d);return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.CUT_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;w(b,d);return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.PASTE_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;v(b,d);return!0},l.COMMAND_PRIORITY_EDITOR),d.registerCommand(l.DROP_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;b.preventDefault();return!0},l.COMMAND_PRIORITY_EDITOR),
17
- d.registerCommand(l.DRAGSTART_COMMAND,b=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;b.preventDefault();return!0},l.COMMAND_PRIORITY_EDITOR));x(d,f);return e}
7
+ 'use strict';var a=require("@lexical/clipboard"),f=require("@lexical/selection"),g=require("@lexical/utils"),h=require("lexical");let k="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement,m=k&&"documentMode"in document?document.documentMode:null;k&&/Mac|iPod|iPhone|iPad/.test(navigator.platform);k&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
8
+ let n=k&&"InputEvent"in window&&!m?"getTargetRanges"in new window.InputEvent("input"):!1,p=k&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),q=k&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream;function r(d,b){b.update(()=>{let c=d instanceof KeyboardEvent?null:d.clipboardData,e=h.$getSelection();if(null!==e&&null!=c){d.preventDefault();let l=a.$getHtmlContent(b);null!==l&&c.setData("text/html",l);c.setData("text/plain",e.getTextContent())}})}
9
+ function t(d,b){d.preventDefault();b.update(()=>{let c=h.$getSelection(),e=d instanceof InputEvent||d instanceof KeyboardEvent?null:d.clipboardData;null!=e&&h.$isRangeSelection(c)&&a.$insertDataTransferForPlainText(e,c)},{tag:"paste"})}function u(d,b){r(d,b);b.update(()=>{let c=h.$getSelection();h.$isRangeSelection(c)&&c.removeText()})}
10
+ exports.registerPlainText=function(d){return g.mergeRegister(d.registerCommand(h.DELETE_CHARACTER_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;c.deleteCharacter(b);return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.DELETE_WORD_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;c.deleteWord(b);return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.DELETE_LINE_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;c.deleteLine(b);
11
+ return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.CONTROLLED_TEXT_INSERTION_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;if("string"===typeof b)c.insertText(b);else{const e=b.dataTransfer;null!=e?a.$insertDataTransferForPlainText(e,c):(b=b.data)&&c.insertText(b)}return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.REMOVE_TEXT_COMMAND,()=>{const b=h.$getSelection();if(!h.$isRangeSelection(b))return!1;b.removeText();return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.INSERT_LINE_BREAK_COMMAND,
12
+ b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;c.insertLineBreak(b);return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.INSERT_PARAGRAPH_COMMAND,()=>{const b=h.$getSelection();if(!h.$isRangeSelection(b))return!1;b.insertLineBreak();return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.KEY_ARROW_LEFT_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;const e=b.shiftKey;return f.$shouldOverrideDefaultCharacterSelection(c,!0)?(b.preventDefault(),f.$moveCharacter(c,
13
+ e,!0),!0):!1},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.KEY_ARROW_RIGHT_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;const e=b.shiftKey;return f.$shouldOverrideDefaultCharacterSelection(c,!1)?(b.preventDefault(),f.$moveCharacter(c,e,!1),!0):!1},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.KEY_BACKSPACE_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;b.preventDefault();return d.dispatchCommand(h.DELETE_CHARACTER_COMMAND,!0)},h.COMMAND_PRIORITY_EDITOR),
14
+ d.registerCommand(h.KEY_DELETE_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;b.preventDefault();return d.dispatchCommand(h.DELETE_CHARACTER_COMMAND,!1)},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.KEY_ENTER_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;if(null!==b){if((q||p)&&n)return!1;b.preventDefault()}return d.dispatchCommand(h.INSERT_LINE_BREAK_COMMAND,!1)},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.COPY_COMMAND,b=>{const c=h.$getSelection();
15
+ if(!h.$isRangeSelection(c))return!1;r(b,d);return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.CUT_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;u(b,d);return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.PASTE_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;t(b,d);return!0},h.COMMAND_PRIORITY_EDITOR),d.registerCommand(h.DROP_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;b.preventDefault();return!0},h.COMMAND_PRIORITY_EDITOR),
16
+ d.registerCommand(h.DRAGSTART_COMMAND,b=>{const c=h.$getSelection();if(!h.$isRangeSelection(c))return!1;b.preventDefault();return!0},h.COMMAND_PRIORITY_EDITOR))}
package/README.md CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  This package provides a starting point for Lexical users by registering listeners for a set of basic commands that cover simple text-editing behavior such as entering text, deleting characters, copy + paste, or changing the selection with arrow keys.
4
4
 
5
- You can use this package as a starting point, and then add additional command listeners to customize the functionality of your editor. If you want to add rich-text features, such as headings, blockquotes, or formatted text, you may want to consider using [@lexical/rich-text](https://lexical.dev/docs/api/lexical-rich-text) instead.
5
+ You can use this package as a starting point, and then add additional command listeners to customize the functionality of your editor. If you want to add rich-text features, such as headings, blockquotes, or formatted text, you may want to consider using [@lexical/rich-text](https://lexical.dev/docs/packages/lexical-rich-text) instead.
package/index.d.ts CHANGED
@@ -6,6 +6,5 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  *
8
8
  */
9
- import type { EditorState, LexicalEditor } from 'lexical';
10
- export declare type InitialEditorStateType = null | string | EditorState | ((editor: LexicalEditor) => void);
11
- export declare function registerPlainText(editor: LexicalEditor, initialEditorState?: InitialEditorStateType): () => void;
9
+ import type { LexicalEditor } from 'lexical';
10
+ export declare function registerPlainText(editor: LexicalEditor): () => void;
package/package.json CHANGED
@@ -7,13 +7,13 @@
7
7
  "plain-text"
8
8
  ],
9
9
  "license": "MIT",
10
- "version": "0.4.0",
10
+ "version": "0.5.0",
11
11
  "main": "LexicalPlainText.js",
12
12
  "peerDependencies": {
13
- "lexical": "0.4.0",
14
- "@lexical/utils": "0.4.0",
15
- "@lexical/selection": "0.4.0",
16
- "@lexical/clipboard": "0.4.0"
13
+ "lexical": "0.5.0",
14
+ "@lexical/utils": "0.5.0",
15
+ "@lexical/selection": "0.5.0",
16
+ "@lexical/clipboard": "0.5.0"
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",