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