@lexical/plain-text 0.8.1 → 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.
@@ -18,7 +18,6 @@ var lexical = require('lexical');
18
18
  * LICENSE file in the root directory of this source tree.
19
19
  *
20
20
  */
21
-
22
21
  const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
23
22
 
24
23
  /**
@@ -33,35 +32,39 @@ CAN_USE_DOM && /Mac|iPod|iPhone|iPad/.test(navigator.platform);
33
32
  CAN_USE_DOM && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
34
33
  const CAN_USE_BEFORE_INPUT = CAN_USE_DOM && 'InputEvent' in window && !documentMode ? 'getTargetRanges' in new window.InputEvent('input') : false;
35
34
  const IS_SAFARI = CAN_USE_DOM && /Version\/[\d.]+.*Safari/.test(navigator.userAgent);
36
- const IS_IOS = CAN_USE_DOM && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
37
-
38
- // Keep these in case we need to use them in the future.
35
+ const IS_IOS = CAN_USE_DOM && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream; // Keep these in case we need to use them in the future.
39
36
  // export const IS_WINDOWS: boolean = CAN_USE_DOM && /Win/.test(navigator.platform);
40
- const IS_CHROME = CAN_USE_DOM && /^(?=.*Chrome).*/i.test(navigator.userAgent);
41
- // export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
37
+
38
+ const IS_CHROME = CAN_USE_DOM && /^(?=.*Chrome).*/i.test(navigator.userAgent); // export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
42
39
 
43
40
  const IS_APPLE_WEBKIT = CAN_USE_DOM && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !IS_CHROME;
44
41
 
45
42
  /** @module @lexical/plain-text */
43
+
46
44
  function onCopyForPlainText(event, editor) {
47
45
  editor.update(() => {
48
46
  const clipboardData = event instanceof KeyboardEvent ? null : event.clipboardData;
49
47
  const selection = lexical.$getSelection();
48
+
50
49
  if (selection !== null && clipboardData != null) {
51
50
  event.preventDefault();
52
51
  const htmlString = clipboard.$getHtmlContent(editor);
52
+
53
53
  if (htmlString !== null) {
54
54
  clipboardData.setData('text/html', htmlString);
55
55
  }
56
+
56
57
  clipboardData.setData('text/plain', selection.getTextContent());
57
58
  }
58
59
  });
59
60
  }
61
+
60
62
  function onPasteForPlainText(event, editor) {
61
63
  event.preventDefault();
62
64
  editor.update(() => {
63
65
  const selection = lexical.$getSelection();
64
66
  const clipboardData = event instanceof InputEvent || event instanceof KeyboardEvent ? null : event.clipboardData;
67
+
65
68
  if (clipboardData != null && lexical.$isRangeSelection(selection)) {
66
69
  clipboard.$insertDataTransferForPlainText(clipboardData, selection);
67
70
  }
@@ -69,122 +72,156 @@ function onPasteForPlainText(event, editor) {
69
72
  tag: 'paste'
70
73
  });
71
74
  }
75
+
72
76
  function onCutForPlainText(event, editor) {
73
77
  onCopyForPlainText(event, editor);
74
78
  editor.update(() => {
75
79
  const selection = lexical.$getSelection();
80
+
76
81
  if (lexical.$isRangeSelection(selection)) {
77
82
  selection.removeText();
78
83
  }
79
84
  });
80
85
  }
86
+
81
87
  function registerPlainText(editor) {
82
88
  const removeListener = utils.mergeRegister(editor.registerCommand(lexical.DELETE_CHARACTER_COMMAND, isBackward => {
83
89
  const selection = lexical.$getSelection();
90
+
84
91
  if (!lexical.$isRangeSelection(selection)) {
85
92
  return false;
86
93
  }
94
+
87
95
  selection.deleteCharacter(isBackward);
88
96
  return true;
89
97
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DELETE_WORD_COMMAND, isBackward => {
90
98
  const selection = lexical.$getSelection();
99
+
91
100
  if (!lexical.$isRangeSelection(selection)) {
92
101
  return false;
93
102
  }
103
+
94
104
  selection.deleteWord(isBackward);
95
105
  return true;
96
106
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DELETE_LINE_COMMAND, isBackward => {
97
107
  const selection = lexical.$getSelection();
108
+
98
109
  if (!lexical.$isRangeSelection(selection)) {
99
110
  return false;
100
111
  }
112
+
101
113
  selection.deleteLine(isBackward);
102
114
  return true;
103
115
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.CONTROLLED_TEXT_INSERTION_COMMAND, eventOrText => {
104
116
  const selection = lexical.$getSelection();
117
+
105
118
  if (!lexical.$isRangeSelection(selection)) {
106
119
  return false;
107
120
  }
121
+
108
122
  if (typeof eventOrText === 'string') {
109
123
  selection.insertText(eventOrText);
110
124
  } else {
111
125
  const dataTransfer = eventOrText.dataTransfer;
126
+
112
127
  if (dataTransfer != null) {
113
128
  clipboard.$insertDataTransferForPlainText(dataTransfer, selection);
114
129
  } else {
115
130
  const data = eventOrText.data;
131
+
116
132
  if (data) {
117
133
  selection.insertText(data);
118
134
  }
119
135
  }
120
136
  }
137
+
121
138
  return true;
122
139
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.REMOVE_TEXT_COMMAND, () => {
123
140
  const selection = lexical.$getSelection();
141
+
124
142
  if (!lexical.$isRangeSelection(selection)) {
125
143
  return false;
126
144
  }
145
+
127
146
  selection.removeText();
128
147
  return true;
129
148
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.INSERT_LINE_BREAK_COMMAND, selectStart => {
130
149
  const selection = lexical.$getSelection();
150
+
131
151
  if (!lexical.$isRangeSelection(selection)) {
132
152
  return false;
133
153
  }
154
+
134
155
  selection.insertLineBreak(selectStart);
135
156
  return true;
136
157
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.INSERT_PARAGRAPH_COMMAND, () => {
137
158
  const selection = lexical.$getSelection();
159
+
138
160
  if (!lexical.$isRangeSelection(selection)) {
139
161
  return false;
140
162
  }
163
+
141
164
  selection.insertLineBreak();
142
165
  return true;
143
166
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_ARROW_LEFT_COMMAND, payload => {
144
167
  const selection$1 = lexical.$getSelection();
168
+
145
169
  if (!lexical.$isRangeSelection(selection$1)) {
146
170
  return false;
147
171
  }
172
+
148
173
  const event = payload;
149
174
  const isHoldingShift = event.shiftKey;
175
+
150
176
  if (selection.$shouldOverrideDefaultCharacterSelection(selection$1, true)) {
151
177
  event.preventDefault();
152
178
  selection.$moveCharacter(selection$1, isHoldingShift, true);
153
179
  return true;
154
180
  }
181
+
155
182
  return false;
156
183
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_ARROW_RIGHT_COMMAND, payload => {
157
184
  const selection$1 = lexical.$getSelection();
185
+
158
186
  if (!lexical.$isRangeSelection(selection$1)) {
159
187
  return false;
160
188
  }
189
+
161
190
  const event = payload;
162
191
  const isHoldingShift = event.shiftKey;
192
+
163
193
  if (selection.$shouldOverrideDefaultCharacterSelection(selection$1, false)) {
164
194
  event.preventDefault();
165
195
  selection.$moveCharacter(selection$1, isHoldingShift, false);
166
196
  return true;
167
197
  }
198
+
168
199
  return false;
169
200
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_BACKSPACE_COMMAND, event => {
170
201
  const selection = lexical.$getSelection();
202
+
171
203
  if (!lexical.$isRangeSelection(selection)) {
172
204
  return false;
173
205
  }
206
+
174
207
  event.preventDefault();
175
208
  return editor.dispatchCommand(lexical.DELETE_CHARACTER_COMMAND, true);
176
209
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_DELETE_COMMAND, event => {
177
210
  const selection = lexical.$getSelection();
211
+
178
212
  if (!lexical.$isRangeSelection(selection)) {
179
213
  return false;
180
214
  }
215
+
181
216
  event.preventDefault();
182
217
  return editor.dispatchCommand(lexical.DELETE_CHARACTER_COMMAND, false);
183
218
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.KEY_ENTER_COMMAND, event => {
184
219
  const selection = lexical.$getSelection();
220
+
185
221
  if (!lexical.$isRangeSelection(selection)) {
186
222
  return false;
187
223
  }
224
+
188
225
  if (event !== null) {
189
226
  // If we have beforeinput, then we can avoid blocking
190
227
  // the default behavior. This ensures that the iOS can
@@ -196,46 +233,56 @@ function registerPlainText(editor) {
196
233
  if ((IS_IOS || IS_SAFARI || IS_APPLE_WEBKIT) && CAN_USE_BEFORE_INPUT) {
197
234
  return false;
198
235
  }
236
+
199
237
  event.preventDefault();
200
238
  }
239
+
201
240
  return editor.dispatchCommand(lexical.INSERT_LINE_BREAK_COMMAND, false);
202
241
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.COPY_COMMAND, event => {
203
242
  const selection = lexical.$getSelection();
243
+
204
244
  if (!lexical.$isRangeSelection(selection)) {
205
245
  return false;
206
246
  }
247
+
207
248
  onCopyForPlainText(event, editor);
208
249
  return true;
209
250
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.CUT_COMMAND, event => {
210
251
  const selection = lexical.$getSelection();
252
+
211
253
  if (!lexical.$isRangeSelection(selection)) {
212
254
  return false;
213
255
  }
256
+
214
257
  onCutForPlainText(event, editor);
215
258
  return true;
216
259
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.PASTE_COMMAND, event => {
217
260
  const selection = lexical.$getSelection();
261
+
218
262
  if (!lexical.$isRangeSelection(selection)) {
219
263
  return false;
220
264
  }
265
+
221
266
  onPasteForPlainText(event, editor);
222
267
  return true;
223
268
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DROP_COMMAND, event => {
224
269
  const selection = lexical.$getSelection();
270
+
225
271
  if (!lexical.$isRangeSelection(selection)) {
226
272
  return false;
227
- }
273
+ } // TODO: Make drag and drop work at some point.
274
+
228
275
 
229
- // TODO: Make drag and drop work at some point.
230
276
  event.preventDefault();
231
277
  return true;
232
278
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.DRAGSTART_COMMAND, event => {
233
279
  const selection = lexical.$getSelection();
280
+
234
281
  if (!lexical.$isRangeSelection(selection)) {
235
282
  return false;
236
- }
283
+ } // TODO: Make drag and drop work at some point.
284
+
237
285
 
238
- // TODO: Make drag and drop work at some point.
239
286
  event.preventDefault();
240
287
  return true;
241
288
  }, lexical.COMMAND_PRIORITY_EDITOR));
package/package.json CHANGED
@@ -7,13 +7,13 @@
7
7
  "plain-text"
8
8
  ],
9
9
  "license": "MIT",
10
- "version": "0.8.1",
10
+ "version": "0.9.0",
11
11
  "main": "LexicalPlainText.js",
12
12
  "peerDependencies": {
13
- "lexical": "0.8.1",
14
- "@lexical/utils": "0.8.1",
15
- "@lexical/selection": "0.8.1",
16
- "@lexical/clipboard": "0.8.1"
13
+ "lexical": "0.9.0",
14
+ "@lexical/utils": "0.9.0",
15
+ "@lexical/selection": "0.9.0",
16
+ "@lexical/clipboard": "0.9.0"
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",