@lexical/rich-text 0.3.0 → 0.3.1

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.
@@ -299,33 +299,45 @@ function onPasteForRichText(event, editor) {
299
299
 
300
300
  function onCopyForRichText(event, editor) {
301
301
  event.preventDefault();
302
- editor.update(() => {
303
- const clipboardData = event.clipboardData;
304
- const selection = lexical.$getSelection();
302
+ const selection = lexical.$getSelection();
305
303
 
306
- if (selection !== null) {
307
- if (clipboardData != null) {
308
- const htmlString = clipboard.$getHtmlContent(editor);
304
+ if (selection !== null) {
305
+ const clipboardData = event.clipboardData;
306
+ const htmlString = clipboard.$getHtmlContent(editor);
309
307
 
310
- if (htmlString !== null) {
311
- clipboardData.setData('text/html', htmlString);
312
- }
308
+ if (clipboardData != null) {
309
+ if (htmlString !== null) {
310
+ clipboardData.setData('text/html', htmlString);
311
+ }
313
312
 
314
- clipboardData.setData('text/plain', selection.getTextContent());
313
+ const plainString = selection.getTextContent();
314
+ clipboardData.setData('text/plain', plainString);
315
+ } else {
316
+ const clipboard = navigator.clipboard;
317
+
318
+ if (clipboard != null) {
319
+ // Most browsers only support a single item in the clipboard at one time.
320
+ // So we optimize by only putting in HTML.
321
+ const data = [new ClipboardItem({
322
+ 'text/html': new Blob([htmlString], {
323
+ type: 'text/html'
324
+ })
325
+ })];
326
+ clipboard.write(data);
315
327
  }
316
328
  }
317
- });
329
+ }
318
330
  }
319
331
 
320
332
  function onCutForRichText(event, editor) {
321
333
  onCopyForRichText(event, editor);
322
- editor.update(() => {
323
- const selection = lexical.$getSelection();
334
+ const selection = lexical.$getSelection();
324
335
 
325
- if (lexical.$isRangeSelection(selection)) {
326
- selection.removeText();
327
- }
328
- });
336
+ if (lexical.$isRangeSelection(selection)) {
337
+ selection.removeText();
338
+ } else if (lexical.$isNodeSelection(selection)) {
339
+ selection.getNodes().forEach(node => node.remove());
340
+ }
329
341
  }
330
342
 
331
343
  function handleIndentAndOutdent(insertTab, indentOrOutdent) {
@@ -628,23 +640,11 @@ function registerRichText(editor, initialEditorState) {
628
640
  event.preventDefault();
629
641
  return true;
630
642
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.COPY_COMMAND, event => {
631
- const selection = lexical.$getSelection();
632
-
633
- if (lexical.$isRangeSelection(selection) || lexical.$isGridSelection(selection)) {
634
- onCopyForRichText(event, editor);
635
- return true;
636
- }
637
-
638
- return false;
643
+ onCopyForRichText(event, editor);
644
+ return true;
639
645
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.CUT_COMMAND, event => {
640
- const selection = lexical.$getSelection();
641
-
642
- if (lexical.$isRangeSelection(selection) || lexical.$isGridSelection(selection)) {
643
- onCutForRichText(event, editor);
644
- return true;
645
- }
646
-
647
- return false;
646
+ onCutForRichText(event, editor);
647
+ return true;
648
648
  }, lexical.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical.PASTE_COMMAND, event => {
649
649
  const selection = lexical.$getSelection();
650
650
 
@@ -12,9 +12,9 @@ class y extends l.ElementNode{static getType(){return"heading"}static clone(b){r
12
12
  priority:0})}}static importJSON(b){let e=A(b.tag);e.setFormat(b.format);e.setIndent(b.indent);e.setDirection(b.direction);return e}exportJSON(){return{...super.exportJSON(),tag:this.getTag(),type:"heading",version:1}}insertNewAfter(){let b=l.$createParagraphNode(),e=this.getDirection();b.setDirection(e);this.insertAfter(b);return b}collapseAtStart(){let b=l.$createParagraphNode();this.getChildren().forEach(e=>b.append(e));this.replace(b);return!0}extractWithChild(){return!0}}
13
13
  function z(b){b=b.nodeName.toLowerCase();let e=null;if("h1"===b||"h2"===b||"h3"===b||"h4"===b||"h5"===b)e=A(b);return{node:e}}function w(){return{node:x()}}function A(b){return new y(b)}
14
14
  function B(b,e){if(null!==e)if(void 0===e)b.update(()=>{var f=l.$getRoot();if(null===f.getFirstChild()){let c=l.$createParagraphNode();f.append(c);f=document.activeElement;(null!==l.$getSelection()||null!==f&&f===b.getRootElement())&&c.select()}},t);else if(null!==e)switch(typeof e){case "string":e=b.parseEditorState(e);b.setEditorState(e,t);break;case "object":b.setEditorState(e,t);break;case "function":b.update(e,t)}}
15
- function C(b,e){b.preventDefault();e.update(()=>{let f=l.$getSelection(),c=b.clipboardData;null!=c&&(l.$isRangeSelection(f)||l.$isGridSelection(f))&&a.$insertDataTransferForRichText(c,f,e)})}function D(b,e){b.preventDefault();e.update(()=>{let f=b.clipboardData,c=l.$getSelection();if(null!==c&&null!=f){let d=a.$getHtmlContent(e);null!==d&&f.setData("text/html",d);f.setData("text/plain",c.getTextContent())}})}
16
- function E(b,e){D(b,e);e.update(()=>{let f=l.$getSelection();l.$isRangeSelection(f)&&f.removeText()})}function F(b,e){var f=l.$getSelection();if(l.$isRangeSelection(f)){var c=new Set;f=f.getNodes();for(let g=0;g<f.length;g++){let v=f[g];var d=v.getKey();c.has(d)||(c.add(d),d=k.$getNearestBlockElementAncestorOrThrow(v),d.canInsertTab()?b(v):d.canIndent()&&e(d))}}}exports.$createHeadingNode=A;exports.$createQuoteNode=x;exports.$isHeadingNode=function(b){return b instanceof y};
17
- exports.$isQuoteNode=function(b){return b instanceof u};exports.HeadingNode=y;exports.QuoteNode=u;
15
+ function C(b,e){b.preventDefault();e.update(()=>{let f=l.$getSelection(),c=b.clipboardData;null!=c&&(l.$isRangeSelection(f)||l.$isGridSelection(f))&&a.$insertDataTransferForRichText(c,f,e)})}
16
+ function D(b,e){b.preventDefault();var f=l.$getSelection();null!==f&&(b=b.clipboardData,e=a.$getHtmlContent(e),null!=b?(null!==e&&b.setData("text/html",e),f=f.getTextContent(),b.setData("text/plain",f)):(f=navigator.clipboard,null!=f&&(b=[new ClipboardItem({"text/html":new Blob([e],{type:"text/html"})})],f.write(b))))}function E(b,e){D(b,e);b=l.$getSelection();l.$isRangeSelection(b)?b.removeText():l.$isNodeSelection(b)&&b.getNodes().forEach(f=>f.remove())}
17
+ function F(b,e){var f=l.$getSelection();if(l.$isRangeSelection(f)){var c=new Set;f=f.getNodes();for(let g=0;g<f.length;g++){let v=f[g];var d=v.getKey();c.has(d)||(c.add(d),d=k.$getNearestBlockElementAncestorOrThrow(v),d.canInsertTab()?b(v):d.canIndent()&&e(d))}}}exports.$createHeadingNode=A;exports.$createQuoteNode=x;exports.$isHeadingNode=function(b){return b instanceof y};exports.$isQuoteNode=function(b){return b instanceof u};exports.HeadingNode=y;exports.QuoteNode=u;
18
18
  exports.registerRichText=function(b,e){let f=k.mergeRegister(b.registerCommand(l.CLICK_COMMAND,()=>{const c=l.$getSelection();return l.$isNodeSelection(c)?(c.clear(),!0):!1},0),b.registerCommand(l.DELETE_CHARACTER_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;d.deleteCharacter(c);return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.DELETE_WORD_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;d.deleteWord(c);return!0},l.COMMAND_PRIORITY_EDITOR),
19
19
  b.registerCommand(l.DELETE_LINE_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;d.deleteLine(c);return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.CONTROLLED_TEXT_INSERTION_COMMAND,c=>{const d=l.$getSelection();if("string"===typeof c)l.$isRangeSelection(d)?d.insertText(c):l.$isGridSelection(d);else{if(!l.$isRangeSelection(d)&&!l.$isGridSelection(d))return!1;const g=c.dataTransfer;null!=g?a.$insertDataTransferForRichText(g,d,b):l.$isRangeSelection(d)&&(c=c.data)&&d.insertText(c)}return!0},
20
20
  l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.REMOVE_TEXT_COMMAND,()=>{const c=l.$getSelection();if(!l.$isRangeSelection(c))return!1;c.removeText();return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.FORMAT_TEXT_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;d.formatText(c);return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.FORMAT_ELEMENT_COMMAND,c=>{var d=l.$getSelection();if(!l.$isRangeSelection(d)&&!l.$isNodeSelection(d))return!1;d=d.getNodes();for(const g of d)k.$getNearestBlockElementAncestorOrThrow(g).setFormat(c);
@@ -23,5 +23,5 @@ c.getIndent();10!==d&&c.setIndent(d+1)});return!0},l.COMMAND_PRIORITY_EDITOR),b.
23
23
  !0)?(c.preventDefault(),h.$moveCharacter(d,g,!0),!0):!1},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.KEY_ARROW_RIGHT_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;const g=c.shiftKey;return h.$shouldOverrideDefaultCharacterSelection(d,!1)?(c.preventDefault(),h.$moveCharacter(d,g,!1),!0):!1},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.KEY_BACKSPACE_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;c.preventDefault();({anchor:c}=d);return d.isCollapsed()&&
24
24
  0===c.offset&&0<k.$getNearestBlockElementAncestorOrThrow(c.getNode()).getIndent()?b.dispatchCommand(l.OUTDENT_CONTENT_COMMAND,void 0):b.dispatchCommand(l.DELETE_CHARACTER_COMMAND,!0)},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.KEY_DELETE_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;c.preventDefault();return b.dispatchCommand(l.DELETE_CHARACTER_COMMAND,!1)},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.KEY_ENTER_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;
25
25
  if(null!==c){if((r||q)&&p)return!1;c.preventDefault();if(c.shiftKey)return b.dispatchCommand(l.INSERT_LINE_BREAK_COMMAND,!1)}return b.dispatchCommand(l.INSERT_PARAGRAPH_COMMAND,void 0)},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.KEY_TAB_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;c.preventDefault();return b.dispatchCommand(c.shiftKey?l.OUTDENT_CONTENT_COMMAND:l.INDENT_CONTENT_COMMAND,void 0)},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.KEY_ESCAPE_COMMAND,()=>{const c=
26
- l.$getSelection();if(!l.$isRangeSelection(c))return!1;b.blur();return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.DROP_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;c.preventDefault();return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.DRAGSTART_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;c.preventDefault();return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.COPY_COMMAND,c=>{const d=l.$getSelection();return l.$isRangeSelection(d)||
27
- l.$isGridSelection(d)?(D(c,b),!0):!1},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.CUT_COMMAND,c=>{const d=l.$getSelection();return l.$isRangeSelection(d)||l.$isGridSelection(d)?(E(c,b),!0):!1},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.PASTE_COMMAND,c=>{const d=l.$getSelection();return l.$isRangeSelection(d)||l.$isGridSelection(d)?(C(c,b),!0):!1},l.COMMAND_PRIORITY_EDITOR));B(b,e);return f}
26
+ l.$getSelection();if(!l.$isRangeSelection(c))return!1;b.blur();return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.DROP_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;c.preventDefault();return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.DRAGSTART_COMMAND,c=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;c.preventDefault();return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.COPY_COMMAND,c=>{D(c,b);return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.CUT_COMMAND,
27
+ c=>{E(c,b);return!0},l.COMMAND_PRIORITY_EDITOR),b.registerCommand(l.PASTE_COMMAND,c=>{const d=l.$getSelection();return l.$isRangeSelection(d)||l.$isGridSelection(d)?(C(c,b),!0):!1},l.COMMAND_PRIORITY_EDITOR));B(b,e);return f}
package/package.json CHANGED
@@ -7,13 +7,13 @@
7
7
  "rich-text"
8
8
  ],
9
9
  "license": "MIT",
10
- "version": "0.3.0",
10
+ "version": "0.3.1",
11
11
  "main": "LexicalRichText.js",
12
12
  "peerDependencies": {
13
- "lexical": "0.3.0",
14
- "@lexical/selection": "0.3.0",
15
- "@lexical/clipboard": "0.3.0",
16
- "@lexical/utils": "0.3.0"
13
+ "lexical": "0.3.1",
14
+ "@lexical/selection": "0.3.1",
15
+ "@lexical/clipboard": "0.3.1",
16
+ "@lexical/utils": "0.3.1"
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",