@lexical/html 0.4.1 → 0.5.1-next.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.
@@ -36,7 +36,7 @@ function $generateNodesFromDOM(editor, dom) {
36
36
  return lexicalNodes;
37
37
  }
38
38
  function $generateHtmlFromNodes(editor, selection) {
39
- if (document == null || window == null) {
39
+ if (typeof document === 'undefined' || typeof window === 'undefined') {
40
40
  throw new Error('To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.');
41
41
  }
42
42
 
@@ -46,25 +46,28 @@ function $generateHtmlFromNodes(editor, selection) {
46
46
 
47
47
  for (let i = 0; i < topLevelChildren.length; i++) {
48
48
  const topLevelNode = topLevelChildren[i];
49
-
50
- if (selection !== undefined) {
51
- $appendNodesToHTML(editor, selection, topLevelNode, container);
52
- }
49
+ $appendNodesToHTML(editor, topLevelNode, container, selection);
53
50
  }
54
51
 
55
52
  return container.innerHTML;
56
53
  }
57
54
 
58
- function $appendNodesToHTML(editor, selection$1, currentNode, parentElement) {
55
+ function $appendNodesToHTML(editor, currentNode, parentElement, selection$1 = null) {
59
56
  let shouldInclude = selection$1 != null ? currentNode.isSelected() : true;
60
57
  const shouldExclude = lexical.$isElementNode(currentNode) && currentNode.excludeFromCopy('html');
61
- let clone = selection.$cloneWithProperties(currentNode);
62
- clone = lexical.$isTextNode(clone) && selection$1 != null ? selection.$sliceSelectedTextNodeContent(selection$1, clone) : clone;
63
- const children = lexical.$isElementNode(clone) ? clone.getChildren() : [];
58
+ let target = currentNode;
59
+
60
+ if (selection$1 !== null) {
61
+ let clone = selection.$cloneWithProperties(currentNode);
62
+ clone = lexical.$isTextNode(clone) && selection$1 != null ? selection.$sliceSelectedTextNodeContent(selection$1, clone) : clone;
63
+ target = clone;
64
+ }
65
+
66
+ const children = lexical.$isElementNode(target) ? target.getChildren() : [];
64
67
  const {
65
68
  element,
66
69
  after
67
- } = clone.exportDOM(editor);
70
+ } = target.exportDOM(editor);
68
71
 
69
72
  if (!element) {
70
73
  return false;
@@ -74,7 +77,7 @@ function $appendNodesToHTML(editor, selection$1, currentNode, parentElement) {
74
77
 
75
78
  for (let i = 0; i < children.length; i++) {
76
79
  const childNode = children[i];
77
- const shouldIncludeChild = $appendNodesToHTML(editor, selection$1, childNode, fragment);
80
+ const shouldIncludeChild = $appendNodesToHTML(editor, childNode, fragment, selection$1);
78
81
 
79
82
  if (!shouldInclude && lexical.$isElementNode(currentNode) && shouldIncludeChild && currentNode.extractWithChild(childNode, selection$1, 'html')) {
80
83
  shouldInclude = true;
@@ -86,7 +89,7 @@ function $appendNodesToHTML(editor, selection$1, currentNode, parentElement) {
86
89
  parentElement.append(element);
87
90
 
88
91
  if (after) {
89
- const newElement = after.call(clone, element);
92
+ const newElement = after.call(target, element);
90
93
  if (newElement) element.replaceWith(newElement);
91
94
  }
92
95
  } else {
@@ -120,7 +123,7 @@ function getConversionFunction(domNode, editor) {
120
123
 
121
124
  const IGNORE_TAGS = new Set(['STYLE']);
122
125
 
123
- function $createNodesFromDOM(node, editor, forChildMap = new Map(), parentLexicalNode) {
126
+ function $createNodesFromDOM(node, editor, forChildMap = new Map(), parentLexicalNode, preformatted = false) {
124
127
  let lexicalNodes = [];
125
128
 
126
129
  if (IGNORE_TAGS.has(node.nodeName)) {
@@ -129,7 +132,7 @@ function $createNodesFromDOM(node, editor, forChildMap = new Map(), parentLexica
129
132
 
130
133
  let currentLexicalNode = null;
131
134
  const transformFunction = getConversionFunction(node, editor);
132
- const transformOutput = transformFunction ? transformFunction(node) : null;
135
+ const transformOutput = transformFunction ? transformFunction(node, undefined, preformatted) : null;
133
136
  let postTransform = null;
134
137
 
135
138
  if (transformOutput !== null) {
@@ -161,7 +164,7 @@ function $createNodesFromDOM(node, editor, forChildMap = new Map(), parentLexica
161
164
  let childLexicalNodes = [];
162
165
 
163
166
  for (let i = 0; i < children.length; i++) {
164
- childLexicalNodes.push(...$createNodesFromDOM(children[i], editor, new Map(forChildMap), currentLexicalNode));
167
+ childLexicalNodes.push(...$createNodesFromDOM(children[i], editor, new Map(forChildMap), currentLexicalNode, preformatted || (transformOutput && transformOutput.preformatted) === true));
165
168
  }
166
169
 
167
170
  if (postTransform != null) {
@@ -4,9 +4,9 @@
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 m=require("@lexical/selection"),q=require("lexical");
8
- function r(c,d,e,h){let a=null!=d?e.isSelected():!0,b=q.$isElementNode(e)&&e.excludeFromCopy("html"),f=m.$cloneWithProperties(e);f=q.$isTextNode(f)&&null!=d?m.$sliceSelectedTextNodeContent(d,f):f;let g=q.$isElementNode(f)?f.getChildren():[],{element:k,after:l}=f.exportDOM(c);if(!k)return!1;let n=new DocumentFragment;for(let p=0;p<g.length;p++){let t=g[p],w=r(c,d,t,n);!a&&q.$isElementNode(e)&&w&&e.extractWithChild(t,d,"html")&&(a=!0)}a&&!b?(k.append(n),h.append(k),l&&(c=l.call(f,k))&&k.replaceWith(c)):
9
- h.append(n);return a}let u=new Set(["STYLE"]);
10
- function v(c,d,e=new Map,h){let a=[];if(u.has(c.nodeName))return a;let b=null;var f,{nodeName:g}=c,k=d._htmlConversions.get(g.toLowerCase());g=null;if(void 0!==k)for(f of k)k=f(c),null!==k&&(null===g||g.priority<k.priority)&&(g=k);g=(f=null!==g?g.conversion:null)?f(c):null;f=null;if(null!==g){f=g.after;b=g.node;if(null!==b){for(var [,l]of e)if(b=l(b,h),!b)break;b&&a.push(b)}null!=g.forChild&&e.set(c.nodeName,g.forChild)}c=c.childNodes;h=[];for(l=0;l<c.length;l++)h.push(...v(c[l],d,new Map(e),b));
11
- null!=f&&(h=f(h));null==b?a=a.concat(h):q.$isElementNode(b)&&b.append(...h);return a}exports.$generateHtmlFromNodes=function(c,d){if(null==document||null==window)throw Error("To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.");let e=document.createElement("div"),h=q.$getRoot().getChildren();for(let a=0;a<h.length;a++){let b=h[a];void 0!==d&&r(c,d,b,e)}return e.innerHTML};
12
- exports.$generateNodesFromDOM=function(c,d){let e=[];d=d.body?Array.from(d.body.childNodes):[];let h=d.length;for(let b=0;b<h;b++){var a=d[b];u.has(a.nodeName)||(a=v(a,c),null!==a&&(e=e.concat(a)))}return e}
7
+ 'use strict';var n=require("@lexical/selection"),q=require("lexical");
8
+ function r(c,d,h,b=null){let e=null!=b?d.isSelected():!0,l=q.$isElementNode(d)&&d.excludeFromCopy("html");var a=d;null!==b&&(a=n.$cloneWithProperties(d),a=q.$isTextNode(a)&&null!=b?n.$sliceSelectedTextNodeContent(b,a):a);let f=q.$isElementNode(a)?a.getChildren():[],{element:k,after:g}=a.exportDOM(c);if(!k)return!1;let m=new DocumentFragment;for(let p=0;p<f.length;p++){let t=f[p],w=r(c,t,m,b);!e&&q.$isElementNode(d)&&w&&d.extractWithChild(t,b,"html")&&(e=!0)}e&&!l?(k.append(m),h.append(k),g&&(c=g.call(a,
9
+ k))&&k.replaceWith(c)):h.append(m);return e}let u=new Set(["STYLE"]);
10
+ function v(c,d,h=new Map,b,e=!1){let l=[];if(u.has(c.nodeName))return l;let a=null;var {nodeName:f}=c,k=d._htmlConversions.get(f.toLowerCase());f=null;if(void 0!==k)for(g of k)k=g(c),null!==k&&(null===f||f.priority<k.priority)&&(f=k);var g=(g=null!==f?f.conversion:null)?g(c,void 0,e):null;f=null;if(null!==g){f=g.after;a=g.node;if(null!==a){for(var [,m]of h)if(a=m(a,b),!a)break;a&&l.push(a)}null!=g.forChild&&h.set(c.nodeName,g.forChild)}c=c.childNodes;b=[];for(m=0;m<c.length;m++)b.push(...v(c[m],d,
11
+ new Map(h),a,e||!0===(g&&g.preformatted)));null!=f&&(b=f(b));null==a?l=l.concat(b):q.$isElementNode(a)&&a.append(...b);return l}exports.$generateHtmlFromNodes=function(c,d){if("undefined"===typeof document||"undefined"===typeof window)throw Error("To use $generateHtmlFromNodes in headless mode please initialize a headless browser implementation such as JSDom before calling this function.");let h=document.createElement("div"),b=q.$getRoot().getChildren();for(let e=0;e<b.length;e++)r(c,b[e],h,d);return h.innerHTML};
12
+ exports.$generateNodesFromDOM=function(c,d){let h=[];d=d.body?Array.from(d.body.childNodes):[];let b=d.length;for(let l=0;l<b;l++){var e=d[l];u.has(e.nodeName)||(e=v(e,c),null!==e&&(h=h.concat(e)))}return h}
package/README.md CHANGED
@@ -30,6 +30,5 @@ const nodes = $generateNodesFromDOM(editor, dom);
30
30
  const editor = createEditor({ ...config, nodes });
31
31
 
32
32
  // Or insert them at a selection.
33
- const selection = $getSelection();
34
- selection.insertNodes(nodes);
33
+ $insertNodes(nodes);
35
34
  ```
package/package.json CHANGED
@@ -8,10 +8,10 @@
8
8
  "html"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.4.1",
11
+ "version": "0.5.1-next.0",
12
12
  "main": "LexicalHtml.js",
13
13
  "peerDependencies": {
14
- "lexical": "0.4.1"
14
+ "lexical": "0.5.1-next.0"
15
15
  },
16
16
  "repository": {
17
17
  "type": "git",
@@ -19,6 +19,6 @@
19
19
  "directory": "packages/lexical-html"
20
20
  },
21
21
  "dependencies": {
22
- "@lexical/selection": "0.4.1"
22
+ "@lexical/selection": "0.5.1-next.0"
23
23
  }
24
24
  }