Socket
Socket
Sign inDemoInstall

@lexical/clipboard

Package Overview
Dependencies
Maintainers
5
Versions
171
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lexical/clipboard - npm Package Compare versions

Comparing version 0.17.1-nightly.20240823.0 to 0.17.1-nightly.20240826.0

30

clipboard.d.ts

@@ -9,2 +9,7 @@ /**

import { BaseSelection, LexicalEditor, LexicalNode } from 'lexical';
export interface LexicalClipboardData {
'text/html'?: string | undefined;
'application/x-lexical-editor'?: string | undefined;
'text/plain': string;
}
/**

@@ -17,5 +22,6 @@ * Returns the *currently selected* Lexical content as an HTML string, relying on the

* @param editor - LexicalEditor instance to get HTML content from
* @param selection - The selection to use (default is $getSelection())
* @returns a string of HTML content
*/
export declare function $getHtmlContent(editor: LexicalEditor): string;
export declare function $getHtmlContent(editor: LexicalEditor, selection?: BaseSelection | null): string;
/**

@@ -28,5 +34,6 @@ * Returns the *currently selected* Lexical content as a JSON string, relying on the

* @param editor - LexicalEditor instance to get the JSON content from
* @param selection - The selection to use (default is $getSelection())
* @returns
*/
export declare function $getLexicalContent(editor: LexicalEditor): null | string;
export declare function $getLexicalContent(editor: LexicalEditor, selection?: BaseSelection | null): null | string;
/**

@@ -96,2 +103,19 @@ * Attempts to insert content of the mime-types text/plain or text/uri-list from

*/
export declare function copyToClipboard(editor: LexicalEditor, event: null | ClipboardEvent): Promise<boolean>;
export declare function copyToClipboard(editor: LexicalEditor, event: null | ClipboardEvent, data?: LexicalClipboardData): Promise<boolean>;
/**
* Serialize the content of the current selection to strings in
* text/plain, text/html, and application/x-lexical-editor (Lexical JSON)
* formats (as available).
*
* @param selection the selection to serialize (defaults to $getSelection())
* @returns LexicalClipboardData
*/
export declare function $getClipboardDataFromSelection(selection?: BaseSelection | null): LexicalClipboardData;
/**
* Call setData on the given clipboardData for each MIME type present
* in the given data (from {@link $getClipboardDataFromSelection})
*
* @param clipboardData the event.clipboardData to populate from data
* @param data The lexical data
*/
export declare function setLexicalClipboardDataTransfer(clipboardData: DataTransfer, data: LexicalClipboardData): void;

2

index.d.ts

@@ -8,2 +8,2 @@ /**

*/
export { $generateJSONFromSelectedNodes, $generateNodesFromSerializedNodes, $getHtmlContent, $getLexicalContent, $insertDataTransferForPlainText, $insertDataTransferForRichText, $insertGeneratedNodes, copyToClipboard, } from './clipboard';
export { $generateJSONFromSelectedNodes, $generateNodesFromSerializedNodes, $getClipboardDataFromSelection, $getHtmlContent, $getLexicalContent, $insertDataTransferForPlainText, $insertDataTransferForRichText, $insertGeneratedNodes, copyToClipboard, type LexicalClipboardData, setLexicalClipboardDataTransfer, } from './clipboard';

@@ -35,3 +35,2 @@ /**

const getDOMSelection = targetWindow => CAN_USE_DOM ? (targetWindow || window).getSelection() : null;
/**

@@ -44,6 +43,6 @@ * Returns the *currently selected* Lexical content as an HTML string, relying on the

* @param editor - LexicalEditor instance to get HTML content from
* @param selection - The selection to use (default is $getSelection())
* @returns a string of HTML content
*/
function $getHtmlContent(editor) {
const selection = lexical.$getSelection();
function $getHtmlContent(editor, selection = lexical.$getSelection()) {
if (selection == null) {

@@ -69,6 +68,6 @@ {

* @param editor - LexicalEditor instance to get the JSON content from
* @param selection - The selection to use (default is $getSelection())
* @returns
*/
function $getLexicalContent(editor) {
const selection = lexical.$getSelection();
function $getLexicalContent(editor, selection = lexical.$getSelection()) {
if (selection == null) {

@@ -305,3 +304,3 @@ {

*/
async function copyToClipboard(editor, event) {
async function copyToClipboard(editor, event, data) {
if (clipboardEventTimeout !== null) {

@@ -315,3 +314,3 @@ // Prevent weird race conditions that can happen when this function is run multiple times

editor.update(() => {
resolve($copyToClipboardEvent(editor, event));
resolve($copyToClipboardEvent(editor, event, data));
});

@@ -343,3 +342,3 @@ });

}
resolve($copyToClipboardEvent(editor, secondEvent));
resolve($copyToClipboardEvent(editor, secondEvent, data));
}

@@ -362,32 +361,67 @@ // Block the entire copy flow while we wait for the next ClipboardEvent

// TODO shouldn't pass editor (pass namespace directly)
function $copyToClipboardEvent(editor, event) {
const domSelection = getDOMSelection(editor._window);
if (!domSelection) {
return false;
function $copyToClipboardEvent(editor, event, data) {
if (data === undefined) {
const domSelection = getDOMSelection(editor._window);
if (!domSelection) {
return false;
}
const anchorDOM = domSelection.anchorNode;
const focusDOM = domSelection.focusNode;
if (anchorDOM !== null && focusDOM !== null && !lexical.isSelectionWithinEditor(editor, anchorDOM, focusDOM)) {
return false;
}
const selection = lexical.$getSelection();
if (selection === null) {
return false;
}
data = $getClipboardDataFromSelection(selection);
}
const anchorDOM = domSelection.anchorNode;
const focusDOM = domSelection.focusNode;
if (anchorDOM !== null && focusDOM !== null && !lexical.isSelectionWithinEditor(editor, anchorDOM, focusDOM)) {
return false;
}
event.preventDefault();
const clipboardData = event.clipboardData;
const selection = lexical.$getSelection();
if (clipboardData === null || selection === null) {
if (clipboardData === null) {
return false;
}
const htmlString = $getHtmlContent(editor);
const lexicalString = $getLexicalContent(editor);
let plainString = '';
if (selection !== null) {
plainString = selection.getTextContent();
setLexicalClipboardDataTransfer(clipboardData, data);
return true;
}
const clipboardDataFunctions = [['text/html', $getHtmlContent], ['application/x-lexical-editor', $getLexicalContent]];
/**
* Serialize the content of the current selection to strings in
* text/plain, text/html, and application/x-lexical-editor (Lexical JSON)
* formats (as available).
*
* @param selection the selection to serialize (defaults to $getSelection())
* @returns LexicalClipboardData
*/
function $getClipboardDataFromSelection(selection = lexical.$getSelection()) {
const clipboardData = {
'text/plain': selection ? selection.getTextContent() : ''
};
if (selection) {
const editor = lexical.$getEditor();
for (const [mimeType, $editorFn] of clipboardDataFunctions) {
const v = $editorFn(editor, selection);
if (v !== null) {
clipboardData[mimeType] = v;
}
}
}
if (htmlString !== null) {
clipboardData.setData('text/html', htmlString);
return clipboardData;
}
/**
* Call setData on the given clipboardData for each MIME type present
* in the given data (from {@link $getClipboardDataFromSelection})
*
* @param clipboardData the event.clipboardData to populate from data
* @param data The lexical data
*/
function setLexicalClipboardDataTransfer(clipboardData, data) {
for (const k in data) {
const v = data[k];
if (v !== undefined) {
clipboardData.setData(k, v);
}
}
if (lexicalString !== null) {
clipboardData.setData('application/x-lexical-editor', lexicalString);
}
clipboardData.setData('text/plain', plainString);
return true;
}

@@ -397,2 +431,3 @@

exports.$generateNodesFromSerializedNodes = $generateNodesFromSerializedNodes;
exports.$getClipboardDataFromSelection = $getClipboardDataFromSelection;
exports.$getHtmlContent = $getHtmlContent;

@@ -404,1 +439,2 @@ exports.$getLexicalContent = $getLexicalContent;

exports.copyToClipboard = copyToClipboard;
exports.setLexicalClipboardDataTransfer = setLexicalClipboardDataTransfer;

@@ -10,11 +10,12 @@ /**

'use strict';var e=require("@lexical/html"),m=require("@lexical/selection"),n=require("@lexical/utils"),p=require("lexical"),t;function u(a){let b=new URLSearchParams;b.append("code",a);for(let c=1;c<arguments.length;c++)b.append("v",arguments[c]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${b} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
t=u&&u.__esModule&&Object.prototype.hasOwnProperty.call(u,"default")?u["default"]:u;let v="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement;function w(a){let b=p.$getSelection();null==b&&t(166);return p.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?"":e.$generateHtmlFromNodes(a,b)}
function x(a){let b=p.$getSelection();null==b&&t(166);return p.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?null:JSON.stringify(y(a,b))}function z(a,b,c){a.dispatchCommand(p.SELECTION_INSERT_CLIPBOARD_NODES_COMMAND,{nodes:b,selection:c})||c.insertNodes(b)}
function A(a,b,c,d=[]){let f=null!==b?c.isSelected(b):!0,h=p.$isElementNode(c)&&c.excludeFromCopy("html");var g=c;if(null!==b){var k=p.$cloneWithProperties(c);g=k=p.$isTextNode(k)&&null!==b?m.$sliceSelectedTextNodeContent(b,k):k}let q=p.$isElementNode(g)?g.getChildren():[];var l=g;k=l.exportJSON();var r=l.constructor;k.type!==r.getType()&&t(58,r.name);p.$isElementNode(l)&&(Array.isArray(k.children)||t(59,r.name));p.$isTextNode(g)&&(g=g.__text,0<g.length?k.text=g:f=!1);for(g=0;g<q.length;g++)l=q[g],
r=A(a,b,l,k.children),!f&&p.$isElementNode(c)&&r&&c.extractWithChild(l,b,"clone")&&(f=!0);if(f&&!h)d.push(k);else if(Array.isArray(k.children))for(a=0;a<k.children.length;a++)d.push(k.children[a]);return f}function y(a,b){let c=[],d=p.$getRoot().getChildren();for(let f=0;f<d.length;f++)A(a,b,d[f],c);return{namespace:a._config.namespace,nodes:c}}function B(a){let b=[];for(let c=0;c<a.length;c++){let d=p.$parseSerializedNode(a[c]);p.$isTextNode(d)&&m.$addNodeStyle(d);b.push(d)}return b}let C=null;
function D(a,b){var c=v?(a._window||window).getSelection():null;if(!c)return!1;var d=c.anchorNode;c=c.focusNode;if(null!==d&&null!==c&&!p.isSelectionWithinEditor(a,d,c))return!1;b.preventDefault();b=b.clipboardData;d=p.$getSelection();if(null===b||null===d)return!1;c=w(a);a=x(a);let f="";null!==d&&(f=d.getTextContent());null!==c&&b.setData("text/html",c);null!==a&&b.setData("application/x-lexical-editor",a);b.setData("text/plain",f);return!0}exports.$generateJSONFromSelectedNodes=y;
exports.$generateNodesFromSerializedNodes=B;exports.$getHtmlContent=w;exports.$getLexicalContent=x;exports.$insertDataTransferForPlainText=function(a,b){a=a.getData("text/plain")||a.getData("text/uri-list");null!=a&&b.insertRawText(a)};
exports.$insertDataTransferForRichText=function(a,b,c){var d=a.getData("application/x-lexical-editor");if(d)try{let h=JSON.parse(d);if(h.namespace===c._config.namespace&&Array.isArray(h.nodes)){let g=B(h.nodes);return z(c,g,b)}}catch(h){}if(d=a.getData("text/html"))try{var f=(new DOMParser).parseFromString(d,"text/html");let h=e.$generateNodesFromDOM(c,f);return z(c,h,b)}catch(h){}a=a.getData("text/plain")||a.getData("text/uri-list");if(null!=a)if(p.$isRangeSelection(b))for(b=a.split(/(\r?\n|\t)/),
t=u&&u.__esModule&&Object.prototype.hasOwnProperty.call(u,"default")?u["default"]:u;let v="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement;function w(a,b=p.$getSelection()){null==b&&t(166);return p.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?"":e.$generateHtmlFromNodes(a,b)}
function x(a,b=p.$getSelection()){null==b&&t(166);return p.$isRangeSelection(b)&&b.isCollapsed()||0===b.getNodes().length?null:JSON.stringify(y(a,b))}function z(a,b,c){a.dispatchCommand(p.SELECTION_INSERT_CLIPBOARD_NODES_COMMAND,{nodes:b,selection:c})||c.insertNodes(b)}
function A(a,b,c,d=[]){let f=null!==b?c.isSelected(b):!0,k=p.$isElementNode(c)&&c.excludeFromCopy("html");var g=c;if(null!==b){var h=p.$cloneWithProperties(c);g=h=p.$isTextNode(h)&&null!==b?m.$sliceSelectedTextNodeContent(b,h):h}let q=p.$isElementNode(g)?g.getChildren():[];var l=g;h=l.exportJSON();var r=l.constructor;h.type!==r.getType()&&t(58,r.name);p.$isElementNode(l)&&(Array.isArray(h.children)||t(59,r.name));p.$isTextNode(g)&&(g=g.__text,0<g.length?h.text=g:f=!1);for(g=0;g<q.length;g++)l=q[g],
r=A(a,b,l,h.children),!f&&p.$isElementNode(c)&&r&&c.extractWithChild(l,b,"clone")&&(f=!0);if(f&&!k)d.push(h);else if(Array.isArray(h.children))for(a=0;a<h.children.length;a++)d.push(h.children[a]);return f}function y(a,b){let c=[],d=p.$getRoot().getChildren();for(let f=0;f<d.length;f++)A(a,b,d[f],c);return{namespace:a._config.namespace,nodes:c}}function B(a){let b=[];for(let c=0;c<a.length;c++){let d=p.$parseSerializedNode(a[c]);p.$isTextNode(d)&&m.$addNodeStyle(d);b.push(d)}return b}let C=null;
function D(a,b,c){if(void 0===c){var d=v?(a._window||window).getSelection():null;if(!d)return!1;c=d.anchorNode;d=d.focusNode;if(null!==c&&null!==d&&!p.isSelectionWithinEditor(a,c,d))return!1;a=p.$getSelection();if(null===a)return!1;c=E(a)}b.preventDefault();b=b.clipboardData;if(null===b)return!1;F(b,c);return!0}let G=[["text/html",w],["application/x-lexical-editor",x]];
function E(a=p.$getSelection()){let b={"text/plain":a?a.getTextContent():""};if(a){let c=p.$getEditor();for(let [d,f]of G){let k=f(c,a);null!==k&&(b[d]=k)}}return b}function F(a,b){for(let c in b){let d=b[c];void 0!==d&&a.setData(c,d)}}exports.$generateJSONFromSelectedNodes=y;exports.$generateNodesFromSerializedNodes=B;exports.$getClipboardDataFromSelection=E;exports.$getHtmlContent=w;exports.$getLexicalContent=x;
exports.$insertDataTransferForPlainText=function(a,b){a=a.getData("text/plain")||a.getData("text/uri-list");null!=a&&b.insertRawText(a)};
exports.$insertDataTransferForRichText=function(a,b,c){var d=a.getData("application/x-lexical-editor");if(d)try{let k=JSON.parse(d);if(k.namespace===c._config.namespace&&Array.isArray(k.nodes)){let g=B(k.nodes);return z(c,g,b)}}catch(k){}if(d=a.getData("text/html"))try{var f=(new DOMParser).parseFromString(d,"text/html");let k=e.$generateNodesFromDOM(c,f);return z(c,k,b)}catch(k){}a=a.getData("text/plain")||a.getData("text/uri-list");if(null!=a)if(p.$isRangeSelection(b))for(b=a.split(/(\r?\n|\t)/),
""===b[b.length-1]&&b.pop(),a=0;a<b.length;a++)c=p.$getSelection(),p.$isRangeSelection(c)&&(f=b[a],"\n"===f||"\r\n"===f?c.insertParagraph():"\t"===f?c.insertNodes([p.$createTabNode()]):c.insertText(f));else b.insertRawText(a)};exports.$insertGeneratedNodes=z;
exports.copyToClipboard=async function(a,b){if(null!==C)return!1;if(null!==b)return new Promise(g=>{a.update(()=>{g(D(a,b))})});var c=a.getRootElement();let d=null==a._window?window.document:a._window.document,f=v?(a._window||window).getSelection():null;if(null===c||null===f)return!1;let h=d.createElement("span");h.style.cssText="position: fixed; top: -1000px;";h.append(d.createTextNode("#"));c.append(h);c=new Range;c.setStart(h,0);c.setEnd(h,1);f.removeAllRanges();f.addRange(c);return new Promise(g=>
{let k=a.registerCommand(p.COPY_COMMAND,q=>{n.objectKlassEquals(q,ClipboardEvent)&&(k(),null!==C&&(window.clearTimeout(C),C=null),g(D(a,q)));return!0},p.COMMAND_PRIORITY_CRITICAL);C=window.setTimeout(()=>{k();C=null;g(!1)},50);d.execCommand("copy");h.remove()})}
exports.copyToClipboard=async function(a,b,c){if(null!==C)return!1;if(null!==b)return new Promise(h=>{a.update(()=>{h(D(a,b,c))})});var d=a.getRootElement();let f=null==a._window?window.document:a._window.document,k=v?(a._window||window).getSelection():null;if(null===d||null===k)return!1;let g=f.createElement("span");g.style.cssText="position: fixed; top: -1000px;";g.append(f.createTextNode("#"));d.append(g);d=new Range;d.setStart(g,0);d.setEnd(g,1);k.removeAllRanges();k.addRange(d);return new Promise(h=>
{let q=a.registerCommand(p.COPY_COMMAND,l=>{n.objectKlassEquals(l,ClipboardEvent)&&(q(),null!==C&&(window.clearTimeout(C),C=null),h(D(a,l,c)));return!0},p.COMMAND_PRIORITY_CRITICAL);C=window.setTimeout(()=>{q();C=null;h(!1)},50);f.execCommand("copy");g.remove()})};exports.setLexicalClipboardDataTransfer=F

@@ -12,11 +12,11 @@ {

"license": "MIT",
"version": "0.17.1-nightly.20240823.0",
"version": "0.17.1-nightly.20240826.0",
"main": "LexicalClipboard.js",
"types": "index.d.ts",
"dependencies": {
"@lexical/html": "0.17.1-nightly.20240823.0",
"@lexical/list": "0.17.1-nightly.20240823.0",
"@lexical/selection": "0.17.1-nightly.20240823.0",
"@lexical/utils": "0.17.1-nightly.20240823.0",
"lexical": "0.17.1-nightly.20240823.0"
"@lexical/html": "0.17.1-nightly.20240826.0",
"@lexical/list": "0.17.1-nightly.20240826.0",
"@lexical/selection": "0.17.1-nightly.20240826.0",
"@lexical/utils": "0.17.1-nightly.20240826.0",
"lexical": "0.17.1-nightly.20240826.0"
},

@@ -23,0 +23,0 @@ "repository": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc