@react-fabric/lexical 1.0.0-beta.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.
Files changed (136) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/_virtual/prism.js +5 -0
  4. package/dist/cjs/editor/Lexical.module.css +1 -0
  5. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +432 -0
  6. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.js +21 -0
  7. package/dist/cjs/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +27 -0
  8. package/dist/cjs/node_modules/@lexical/code/LexicalCode.dev.js +1139 -0
  9. package/dist/cjs/node_modules/@lexical/code/LexicalCode.js +25 -0
  10. package/dist/cjs/node_modules/@lexical/code/LexicalCode.prod.js +50 -0
  11. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.dev.js +245 -0
  12. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.js +19 -0
  13. package/dist/cjs/node_modules/@lexical/html/LexicalHtml.prod.js +18 -0
  14. package/dist/cjs/node_modules/@lexical/link/LexicalLink.dev.js +471 -0
  15. package/dist/cjs/node_modules/@lexical/link/LexicalLink.js +17 -0
  16. package/dist/cjs/node_modules/@lexical/link/LexicalLink.prod.js +24 -0
  17. package/dist/cjs/node_modules/@lexical/list/LexicalList.dev.js +1275 -0
  18. package/dist/cjs/node_modules/@lexical/list/LexicalList.js +29 -0
  19. package/dist/cjs/node_modules/@lexical/list/LexicalList.prod.js +29 -0
  20. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.dev.js +789 -0
  21. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.js +29 -0
  22. package/dist/cjs/node_modules/@lexical/rich-text/LexicalRichText.prod.js +26 -0
  23. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.dev.js +976 -0
  24. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.js +35 -0
  25. package/dist/cjs/node_modules/@lexical/selection/LexicalSelection.prod.js +35 -0
  26. package/dist/cjs/node_modules/@lexical/table/LexicalTable.dev.js +2792 -0
  27. package/dist/cjs/node_modules/@lexical/table/LexicalTable.js +23 -0
  28. package/dist/cjs/node_modules/@lexical/table/LexicalTable.prod.js +55 -0
  29. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.dev.js +888 -0
  30. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.js +35 -0
  31. package/dist/cjs/node_modules/@lexical/utils/LexicalUtils.prod.js +66 -0
  32. package/dist/cjs/node_modules/@mdi/js/mdi.js +8 -0
  33. package/dist/cjs/node_modules/prismjs/components/prism-c.js +82 -0
  34. package/dist/cjs/node_modules/prismjs/components/prism-clike.js +33 -0
  35. package/dist/cjs/node_modules/prismjs/components/prism-cpp.js +101 -0
  36. package/dist/cjs/node_modules/prismjs/components/prism-css.js +66 -0
  37. package/dist/cjs/node_modules/prismjs/components/prism-java.js +126 -0
  38. package/dist/cjs/node_modules/prismjs/components/prism-javascript.js +174 -0
  39. package/dist/cjs/node_modules/prismjs/components/prism-markdown.js +417 -0
  40. package/dist/cjs/node_modules/prismjs/components/prism-markup.js +188 -0
  41. package/dist/cjs/node_modules/prismjs/components/prism-objectivec.js +14 -0
  42. package/dist/cjs/node_modules/prismjs/components/prism-powershell.js +60 -0
  43. package/dist/cjs/node_modules/prismjs/components/prism-python.js +67 -0
  44. package/dist/cjs/node_modules/prismjs/components/prism-rust.js +130 -0
  45. package/dist/cjs/node_modules/prismjs/components/prism-sql.js +34 -0
  46. package/dist/cjs/node_modules/prismjs/components/prism-swift.js +150 -0
  47. package/dist/cjs/node_modules/prismjs/components/prism-typescript.js +62 -0
  48. package/dist/cjs/node_modules/prismjs/prism.js +1951 -0
  49. package/dist/cjs/packages/lexical/src/components/StickyComponent.js +221 -0
  50. package/dist/cjs/packages/lexical/src/editor/LexicalEditor.js +156 -0
  51. package/dist/cjs/packages/lexical/src/editor/theme.js +97 -0
  52. package/dist/cjs/packages/lexical/src/hooks/useFormatWatcher.js +451 -0
  53. package/dist/cjs/packages/lexical/src/index.js +7 -0
  54. package/dist/cjs/packages/lexical/src/nodes/StickyNode.js +218 -0
  55. package/dist/cjs/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
  56. package/dist/cjs/packages/lexical/src/plugins/StickyPlugin.js +20 -0
  57. package/dist/cjs/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
  58. package/dist/cjs/packages/lexical/src/utils/index.js +214 -0
  59. package/dist/esm/_virtual/prism.js +5 -0
  60. package/dist/esm/editor/Lexical.module.css +1 -0
  61. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.dev.js +423 -0
  62. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.js +19 -0
  63. package/dist/esm/node_modules/@lexical/clipboard/LexicalClipboard.prod.js +18 -0
  64. package/dist/esm/node_modules/@lexical/code/LexicalCode.dev.js +1121 -0
  65. package/dist/esm/node_modules/@lexical/code/LexicalCode.js +21 -0
  66. package/dist/esm/node_modules/@lexical/code/LexicalCode.prod.js +32 -0
  67. package/dist/esm/node_modules/@lexical/html/LexicalHtml.dev.js +244 -0
  68. package/dist/esm/node_modules/@lexical/html/LexicalHtml.js +18 -0
  69. package/dist/esm/node_modules/@lexical/html/LexicalHtml.prod.js +17 -0
  70. package/dist/esm/node_modules/@lexical/link/LexicalLink.dev.js +463 -0
  71. package/dist/esm/node_modules/@lexical/link/LexicalLink.js +17 -0
  72. package/dist/esm/node_modules/@lexical/link/LexicalLink.prod.js +16 -0
  73. package/dist/esm/node_modules/@lexical/list/LexicalList.dev.js +1262 -0
  74. package/dist/esm/node_modules/@lexical/list/LexicalList.js +23 -0
  75. package/dist/esm/node_modules/@lexical/list/LexicalList.prod.js +16 -0
  76. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.dev.js +781 -0
  77. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.js +23 -0
  78. package/dist/esm/node_modules/@lexical/rich-text/LexicalRichText.prod.js +18 -0
  79. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.dev.js +957 -0
  80. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.js +26 -0
  81. package/dist/esm/node_modules/@lexical/selection/LexicalSelection.prod.js +16 -0
  82. package/dist/esm/node_modules/@lexical/table/LexicalTable.dev.js +2754 -0
  83. package/dist/esm/node_modules/@lexical/table/LexicalTable.js +20 -0
  84. package/dist/esm/node_modules/@lexical/table/LexicalTable.prod.js +17 -0
  85. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.dev.js +839 -0
  86. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.js +26 -0
  87. package/dist/esm/node_modules/@lexical/utils/LexicalUtils.prod.js +17 -0
  88. package/dist/esm/node_modules/@mdi/js/mdi.js +7 -0
  89. package/dist/esm/node_modules/prismjs/components/prism-c.js +82 -0
  90. package/dist/esm/node_modules/prismjs/components/prism-clike.js +33 -0
  91. package/dist/esm/node_modules/prismjs/components/prism-cpp.js +101 -0
  92. package/dist/esm/node_modules/prismjs/components/prism-css.js +66 -0
  93. package/dist/esm/node_modules/prismjs/components/prism-java.js +126 -0
  94. package/dist/esm/node_modules/prismjs/components/prism-javascript.js +174 -0
  95. package/dist/esm/node_modules/prismjs/components/prism-markdown.js +417 -0
  96. package/dist/esm/node_modules/prismjs/components/prism-markup.js +188 -0
  97. package/dist/esm/node_modules/prismjs/components/prism-objectivec.js +14 -0
  98. package/dist/esm/node_modules/prismjs/components/prism-powershell.js +60 -0
  99. package/dist/esm/node_modules/prismjs/components/prism-python.js +67 -0
  100. package/dist/esm/node_modules/prismjs/components/prism-rust.js +130 -0
  101. package/dist/esm/node_modules/prismjs/components/prism-sql.js +34 -0
  102. package/dist/esm/node_modules/prismjs/components/prism-swift.js +150 -0
  103. package/dist/esm/node_modules/prismjs/components/prism-typescript.js +62 -0
  104. package/dist/esm/node_modules/prismjs/prism.js +1951 -0
  105. package/dist/esm/packages/lexical/src/components/StickyComponent.js +221 -0
  106. package/dist/esm/packages/lexical/src/editor/LexicalEditor.js +156 -0
  107. package/dist/esm/packages/lexical/src/editor/theme.js +97 -0
  108. package/dist/esm/packages/lexical/src/hooks/useFormatWatcher.js +450 -0
  109. package/dist/esm/packages/lexical/src/index.js +3 -0
  110. package/dist/esm/packages/lexical/src/nodes/StickyNode.js +216 -0
  111. package/dist/esm/packages/lexical/src/plugins/DraggableBlockPlugin.js +314 -0
  112. package/dist/esm/packages/lexical/src/plugins/StickyPlugin.js +20 -0
  113. package/dist/esm/packages/lexical/src/plugins/ToolbarPlugin.js +258 -0
  114. package/dist/esm/packages/lexical/src/utils/index.js +211 -0
  115. package/dist/types/components/StickyComponent.d.ts +9 -0
  116. package/dist/types/editor/LexicalEditor.d.ts +13 -0
  117. package/dist/types/editor/theme.d.ts +70 -0
  118. package/dist/types/hooks/useFormatWatcher.d.ts +40 -0
  119. package/dist/types/index.d.ts +1 -0
  120. package/dist/types/nodes/StickyNode.d.ts +35 -0
  121. package/dist/types/plugins/DraggableBlockPlugin.d.ts +3 -0
  122. package/dist/types/plugins/StickyPlugin.d.ts +1 -0
  123. package/dist/types/plugins/ToolbarPlugin.d.ts +1 -0
  124. package/dist/types/utils/index.d.ts +57 -0
  125. package/package.json +55 -0
  126. package/src/components/StickyComponent.tsx +271 -0
  127. package/src/editor/Lexical.module.css +218 -0
  128. package/src/editor/LexicalEditor.tsx +222 -0
  129. package/src/editor/theme.ts +93 -0
  130. package/src/hooks/useFormatWatcher.ts +504 -0
  131. package/src/index.ts +24 -0
  132. package/src/nodes/StickyNode.tsx +249 -0
  133. package/src/plugins/DraggableBlockPlugin.tsx +418 -0
  134. package/src/plugins/StickyPlugin.ts +38 -0
  135. package/src/plugins/ToolbarPlugin.tsx +279 -0
  136. package/src/utils/index.ts +303 -0
@@ -0,0 +1,214 @@
1
+ 'use strict';
2
+
3
+ var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
4
+ var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
5
+ var _createClass = require('@babel/runtime/helpers/createClass');
6
+ require('@react-fabric/utilities');
7
+
8
+ function isHTMLElement(x) {
9
+ return x instanceof HTMLElement;
10
+ }
11
+ var Point = /*#__PURE__*/function () {
12
+ function Point(x, y) {
13
+ _classCallCheck(this, Point);
14
+ this._x = x;
15
+ this._y = y;
16
+ }
17
+ return _createClass(Point, [{
18
+ key: "x",
19
+ get: function get() {
20
+ return this._x;
21
+ }
22
+ }, {
23
+ key: "y",
24
+ get: function get() {
25
+ return this._y;
26
+ }
27
+ }, {
28
+ key: "equals",
29
+ value: function equals(_ref) {
30
+ var x = _ref.x,
31
+ y = _ref.y;
32
+ return this.x === x && this.y === y;
33
+ }
34
+ }, {
35
+ key: "calcDeltaXTo",
36
+ value: function calcDeltaXTo(_ref2) {
37
+ var x = _ref2.x;
38
+ return this.x - x;
39
+ }
40
+ }, {
41
+ key: "calcDeltaYTo",
42
+ value: function calcDeltaYTo(_ref3) {
43
+ var y = _ref3.y;
44
+ return this.y - y;
45
+ }
46
+ }, {
47
+ key: "calcHorizontalDistanceTo",
48
+ value: function calcHorizontalDistanceTo(point) {
49
+ return Math.abs(this.calcDeltaXTo(point));
50
+ }
51
+ }, {
52
+ key: "calcVerticalDistance",
53
+ value: function calcVerticalDistance(point) {
54
+ return Math.abs(this.calcDeltaYTo(point));
55
+ }
56
+ }, {
57
+ key: "calcDistanceTo",
58
+ value: function calcDistanceTo(point) {
59
+ return Math.sqrt(Math.pow(this.calcDeltaXTo(point), 2) + Math.pow(this.calcDeltaYTo(point), 2));
60
+ }
61
+ }]);
62
+ }();
63
+ function isPoint(x) {
64
+ return x instanceof Point;
65
+ }
66
+ var Rect = /*#__PURE__*/function () {
67
+ function Rect(left, top, right, bottom) {
68
+ _classCallCheck(this, Rect);
69
+ var _ref4 = top <= bottom ? [top, bottom] : [bottom, top],
70
+ _ref5 = _slicedToArray(_ref4, 2),
71
+ physicTop = _ref5[0],
72
+ physicBottom = _ref5[1];
73
+ var _ref6 = left <= right ? [left, right] : [right, left],
74
+ _ref7 = _slicedToArray(_ref6, 2),
75
+ physicLeft = _ref7[0],
76
+ physicRight = _ref7[1];
77
+ this._top = physicTop;
78
+ this._right = physicRight;
79
+ this._left = physicLeft;
80
+ this._bottom = physicBottom;
81
+ }
82
+ return _createClass(Rect, [{
83
+ key: "top",
84
+ get: function get() {
85
+ return this._top;
86
+ }
87
+ }, {
88
+ key: "right",
89
+ get: function get() {
90
+ return this._right;
91
+ }
92
+ }, {
93
+ key: "bottom",
94
+ get: function get() {
95
+ return this._bottom;
96
+ }
97
+ }, {
98
+ key: "left",
99
+ get: function get() {
100
+ return this._left;
101
+ }
102
+ }, {
103
+ key: "width",
104
+ get: function get() {
105
+ return Math.abs(this._left - this._right);
106
+ }
107
+ }, {
108
+ key: "height",
109
+ get: function get() {
110
+ return Math.abs(this._bottom - this._top);
111
+ }
112
+ }, {
113
+ key: "equals",
114
+ value: function equals(_ref8) {
115
+ var top = _ref8.top,
116
+ left = _ref8.left,
117
+ bottom = _ref8.bottom,
118
+ right = _ref8.right;
119
+ return top === this._top && bottom === this._bottom && left === this._left && right === this._right;
120
+ }
121
+ }, {
122
+ key: "contains",
123
+ value: function contains(target) {
124
+ if (isPoint(target)) {
125
+ var x = target.x,
126
+ y = target.y;
127
+ var isOnTopSide = y < this._top;
128
+ var isOnBottomSide = y > this._bottom;
129
+ var isOnLeftSide = x < this._left;
130
+ var isOnRightSide = x > this._right;
131
+ var result = !isOnTopSide && !isOnBottomSide && !isOnLeftSide && !isOnRightSide;
132
+ return {
133
+ reason: {
134
+ isOnBottomSide: isOnBottomSide,
135
+ isOnLeftSide: isOnLeftSide,
136
+ isOnRightSide: isOnRightSide,
137
+ isOnTopSide: isOnTopSide
138
+ },
139
+ result: result
140
+ };
141
+ } else {
142
+ var top = target.top,
143
+ left = target.left,
144
+ bottom = target.bottom,
145
+ right = target.right;
146
+ return top >= this._top && top <= this._bottom && bottom >= this._top && bottom <= this._bottom && left >= this._left && left <= this._right && right >= this._left && right <= this._right;
147
+ }
148
+ }
149
+ }, {
150
+ key: "intersectsWith",
151
+ value: function intersectsWith(rect) {
152
+ var x1 = rect.left,
153
+ y1 = rect.top,
154
+ w1 = rect.width,
155
+ h1 = rect.height;
156
+ var x2 = this.left,
157
+ y2 = this.top,
158
+ w2 = this.width,
159
+ h2 = this.height;
160
+ var maxX = x1 + w1 >= x2 + w2 ? x1 + w1 : x2 + w2;
161
+ var maxY = y1 + h1 >= y2 + h2 ? y1 + h1 : y2 + h2;
162
+ var minX = x1 <= x2 ? x1 : x2;
163
+ var minY = y1 <= y2 ? y1 : y2;
164
+ return maxX - minX <= w1 + w2 && maxY - minY <= h1 + h2;
165
+ }
166
+ }, {
167
+ key: "generateNewRect",
168
+ value: function generateNewRect(_ref9) {
169
+ var _ref9$left = _ref9.left,
170
+ left = _ref9$left === void 0 ? this.left : _ref9$left,
171
+ _ref9$top = _ref9.top,
172
+ top = _ref9$top === void 0 ? this.top : _ref9$top,
173
+ _ref9$right = _ref9.right,
174
+ right = _ref9$right === void 0 ? this.right : _ref9$right,
175
+ _ref9$bottom = _ref9.bottom,
176
+ bottom = _ref9$bottom === void 0 ? this.bottom : _ref9$bottom;
177
+ return new Rect(left, top, right, bottom);
178
+ }
179
+ }], [{
180
+ key: "fromLTRB",
181
+ value: function fromLTRB(left, top, right, bottom) {
182
+ return new Rect(left, top, right, bottom);
183
+ }
184
+ }, {
185
+ key: "fromLWTH",
186
+ value: function fromLWTH(left, width, top, height) {
187
+ return new Rect(left, top, left + width, top + height);
188
+ }
189
+ }, {
190
+ key: "fromPoints",
191
+ value: function fromPoints(startPoint, endPoint) {
192
+ var top = startPoint.y,
193
+ left = startPoint.x;
194
+ var bottom = endPoint.y,
195
+ right = endPoint.x;
196
+ return Rect.fromLTRB(left, top, right, bottom);
197
+ }
198
+ }, {
199
+ key: "fromDOM",
200
+ value: function fromDOM(dom) {
201
+ var _dom$getBoundingClien = dom.getBoundingClientRect(),
202
+ top = _dom$getBoundingClien.top,
203
+ width = _dom$getBoundingClien.width,
204
+ left = _dom$getBoundingClien.left,
205
+ height = _dom$getBoundingClien.height;
206
+ return Rect.fromLWTH(left, width, top, height);
207
+ }
208
+ }]);
209
+ }();
210
+
211
+ exports.Point = Point;
212
+ exports.Rect = Rect;
213
+ exports.isHTMLElement = isHTMLElement;
214
+ exports.isPoint = isPoint;
@@ -0,0 +1,5 @@
1
+ 'use client';
2
+
3
+ var prism = {exports: {}};
4
+
5
+ export { prism as __module };
@@ -0,0 +1 @@
1
+ @layer util{.lexicalEditor,.lexicalToolbar{margin:0 auto;width:21cm}.lexicalEditor :global{.editor-container.editor-container.editor-container{background-color:#fff;color:#0f172a;font-family:Sakkal Majalla;font-size:18px}.editor-heading-h1{font-size:1.5em;margin-bottom:1.5em;text-transform:capitalize}.editor-heading-h1,.editor-heading-h2{font-weight:600;line-height:1.5;margin-top:1.5em;padding:0 4px}.editor-heading-h2{font-size:1.25em;margin-bottom:1em}.editor-heading-h6{color:#b45309;font-size:.875em;font-weight:600;line-height:1.25;margin-bottom:1.5em;margin-top:1.5em}.editor-paragraph{font-size:1em}.editor-paragraph,.editor-quote{line-height:1.5;margin-bottom:1.5em;margin-top:1.5em}.editor-quote{border-color:#64748b;padding-inline-start:1em}.editor-code,.editor-quote{border-inline-start-width:4px;color:#1e293b;display:block;font-size:1.125em}.editor-code{background-color:#e2e8f0;line-height:1.5;margin-bottom:1.5em;margin-top:1.5em;padding:1em}.editor-textUnderline{text-decoration:underline}.editor-textStrikethrough{text-decoration:line-through}.editor-textUnderlineStrikethrough{text-decoration:underline line-through}.editor-page-break{page-break-after:always}.editor-no-break{page-break-inside:avoid}.editor-textSmall{font-size:.875em}.editor-textBold{font-weight:600}.editor-textItalic{font-style:italic}.editor-textMuted{color:#4b5563}.editor-list-ul{list-style:disc;padding-inline-start:.5em}.editor-list-ol{list-style:decimal;padding-inline-start:.5em}.editor-listItem{margin-inline-start:1em;text-align:start}@media print{body,html{margin:0;min-height:100vh}div,h1,h2,h3,h4,h5,h6,p,span{box-sizing:border-box}body.report-template{background-size:0;min-height:100vh}body:after{background-image:inherit;content:"";height:100vh;inset:0;margin:0 auto;max-width:21cm;position:fixed;-webkit-print-color-adjust:exact;print-color-adjust:exact;width:100vw;z-index:10}.editor-page-break>div,.sticky__container{display:none}.editor-container{position:relative}.editor-editor{box-sizing:border-box;padding:0 2cm}}}}
@@ -0,0 +1,423 @@
1
+ 'use client';
2
+
3
+ import { $generateHtmlFromNodes, $generateNodesFromDOM } from '../html/LexicalHtml.js';
4
+ import { $addNodeStyle, $sliceSelectedTextNodeContent } from '../selection/LexicalSelection.js';
5
+ import { objectKlassEquals } from '../utils/LexicalUtils.js';
6
+ import { $isRangeSelection, $getSelection, $createTabNode, SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, $getRoot, $parseSerializedNode, $isTextNode, COPY_COMMAND, COMMAND_PRIORITY_CRITICAL, isSelectionWithinEditor, $getEditor, $isElementNode, $cloneWithProperties } from 'lexical';
7
+
8
+ /**
9
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
10
+ *
11
+ * This source code is licensed under the MIT license found in the
12
+ * LICENSE file in the root directory of this source tree.
13
+ *
14
+ */
15
+
16
+
17
+ /**
18
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
19
+ *
20
+ * This source code is licensed under the MIT license found in the
21
+ * LICENSE file in the root directory of this source tree.
22
+ *
23
+ */
24
+
25
+ const CAN_USE_DOM = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.document.createElement !== 'undefined';
26
+
27
+ /**
28
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
29
+ *
30
+ * This source code is licensed under the MIT license found in the
31
+ * LICENSE file in the root directory of this source tree.
32
+ *
33
+ */
34
+
35
+ const getDOMSelection = targetWindow => CAN_USE_DOM ? (targetWindow || window).getSelection() : null;
36
+ /**
37
+ * Returns the *currently selected* Lexical content as an HTML string, relying on the
38
+ * logic defined in the exportDOM methods on the LexicalNode classes. Note that
39
+ * this will not return the HTML content of the entire editor (unless all the content is included
40
+ * in the current selection).
41
+ *
42
+ * @param editor - LexicalEditor instance to get HTML content from
43
+ * @param selection - The selection to use (default is $getSelection())
44
+ * @returns a string of HTML content
45
+ */
46
+ function $getHtmlContent(editor, selection = $getSelection()) {
47
+ if (selection == null) {
48
+ {
49
+ throw Error(`Expected valid LexicalSelection`);
50
+ }
51
+ }
52
+
53
+ // If we haven't selected anything
54
+ if ($isRangeSelection(selection) && selection.isCollapsed() || selection.getNodes().length === 0) {
55
+ return '';
56
+ }
57
+ return $generateHtmlFromNodes(editor, selection);
58
+ }
59
+
60
+ /**
61
+ * Returns the *currently selected* Lexical content as a JSON string, relying on the
62
+ * logic defined in the exportJSON methods on the LexicalNode classes. Note that
63
+ * this will not return the JSON content of the entire editor (unless all the content is included
64
+ * in the current selection).
65
+ *
66
+ * @param editor - LexicalEditor instance to get the JSON content from
67
+ * @param selection - The selection to use (default is $getSelection())
68
+ * @returns
69
+ */
70
+ function $getLexicalContent(editor, selection = $getSelection()) {
71
+ if (selection == null) {
72
+ {
73
+ throw Error(`Expected valid LexicalSelection`);
74
+ }
75
+ }
76
+
77
+ // If we haven't selected anything
78
+ if ($isRangeSelection(selection) && selection.isCollapsed() || selection.getNodes().length === 0) {
79
+ return null;
80
+ }
81
+ return JSON.stringify($generateJSONFromSelectedNodes(editor, selection));
82
+ }
83
+
84
+ /**
85
+ * Attempts to insert content of the mime-types text/plain or text/uri-list from
86
+ * the provided DataTransfer object into the editor at the provided selection.
87
+ * text/uri-list is only used if text/plain is not also provided.
88
+ *
89
+ * @param dataTransfer an object conforming to the [DataTransfer interface] (https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface)
90
+ * @param selection the selection to use as the insertion point for the content in the DataTransfer object
91
+ */
92
+ function $insertDataTransferForPlainText(dataTransfer, selection) {
93
+ const text = dataTransfer.getData('text/plain') || dataTransfer.getData('text/uri-list');
94
+ if (text != null) {
95
+ selection.insertRawText(text);
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Attempts to insert content of the mime-types application/x-lexical-editor, text/html,
101
+ * text/plain, or text/uri-list (in descending order of priority) from the provided DataTransfer
102
+ * object into the editor at the provided selection.
103
+ *
104
+ * @param dataTransfer an object conforming to the [DataTransfer interface] (https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface)
105
+ * @param selection the selection to use as the insertion point for the content in the DataTransfer object
106
+ * @param editor the LexicalEditor the content is being inserted into.
107
+ */
108
+ function $insertDataTransferForRichText(dataTransfer, selection, editor) {
109
+ const lexicalString = dataTransfer.getData('application/x-lexical-editor');
110
+ if (lexicalString) {
111
+ try {
112
+ const payload = JSON.parse(lexicalString);
113
+ if (payload.namespace === editor._config.namespace && Array.isArray(payload.nodes)) {
114
+ const nodes = $generateNodesFromSerializedNodes(payload.nodes);
115
+ return $insertGeneratedNodes(editor, nodes, selection);
116
+ }
117
+ } catch (_unused) {
118
+ // Fail silently.
119
+ }
120
+ }
121
+ const htmlString = dataTransfer.getData('text/html');
122
+ if (htmlString) {
123
+ try {
124
+ const parser = new DOMParser();
125
+ const dom = parser.parseFromString(htmlString, 'text/html');
126
+ const nodes = $generateNodesFromDOM(editor, dom);
127
+ return $insertGeneratedNodes(editor, nodes, selection);
128
+ } catch (_unused2) {
129
+ // Fail silently.
130
+ }
131
+ }
132
+
133
+ // Multi-line plain text in rich text mode pasted as separate paragraphs
134
+ // instead of single paragraph with linebreaks.
135
+ // Webkit-specific: Supports read 'text/uri-list' in clipboard.
136
+ const text = dataTransfer.getData('text/plain') || dataTransfer.getData('text/uri-list');
137
+ if (text != null) {
138
+ if ($isRangeSelection(selection)) {
139
+ const parts = text.split(/(\r?\n|\t)/);
140
+ if (parts[parts.length - 1] === '') {
141
+ parts.pop();
142
+ }
143
+ for (let i = 0; i < parts.length; i++) {
144
+ const currentSelection = $getSelection();
145
+ if ($isRangeSelection(currentSelection)) {
146
+ const part = parts[i];
147
+ if (part === '\n' || part === '\r\n') {
148
+ currentSelection.insertParagraph();
149
+ } else if (part === '\t') {
150
+ currentSelection.insertNodes([$createTabNode()]);
151
+ } else {
152
+ currentSelection.insertText(part);
153
+ }
154
+ }
155
+ }
156
+ } else {
157
+ selection.insertRawText(text);
158
+ }
159
+ }
160
+ }
161
+
162
+ /**
163
+ * Inserts Lexical nodes into the editor using different strategies depending on
164
+ * some simple selection-based heuristics. If you're looking for a generic way to
165
+ * to insert nodes into the editor at a specific selection point, you probably want
166
+ * {@link lexical.$insertNodes}
167
+ *
168
+ * @param editor LexicalEditor instance to insert the nodes into.
169
+ * @param nodes The nodes to insert.
170
+ * @param selection The selection to insert the nodes into.
171
+ */
172
+ function $insertGeneratedNodes(editor, nodes, selection) {
173
+ if (!editor.dispatchCommand(SELECTION_INSERT_CLIPBOARD_NODES_COMMAND, {
174
+ nodes,
175
+ selection
176
+ })) {
177
+ selection.insertNodes(nodes);
178
+ }
179
+ return;
180
+ }
181
+ function exportNodeToJSON(node) {
182
+ const serializedNode = node.exportJSON();
183
+ const nodeClass = node.constructor;
184
+ if (serializedNode.type !== nodeClass.getType()) {
185
+ {
186
+ throw Error(`LexicalNode: Node ${nodeClass.name} does not implement .exportJSON().`);
187
+ }
188
+ }
189
+ if ($isElementNode(node)) {
190
+ const serializedChildren = serializedNode.children;
191
+ if (!Array.isArray(serializedChildren)) {
192
+ {
193
+ throw Error(`LexicalNode: Node ${nodeClass.name} is an element but .exportJSON() does not have a children array.`);
194
+ }
195
+ }
196
+ }
197
+ return serializedNode;
198
+ }
199
+ function $appendNodesToJSON(editor, selection, currentNode, targetArray = []) {
200
+ let shouldInclude = selection !== null ? currentNode.isSelected(selection) : true;
201
+ const shouldExclude = $isElementNode(currentNode) && currentNode.excludeFromCopy('html');
202
+ let target = currentNode;
203
+ if (selection !== null) {
204
+ let clone = $cloneWithProperties(currentNode);
205
+ clone = $isTextNode(clone) && selection !== null ? $sliceSelectedTextNodeContent(selection, clone) : clone;
206
+ target = clone;
207
+ }
208
+ const children = $isElementNode(target) ? target.getChildren() : [];
209
+ const serializedNode = exportNodeToJSON(target);
210
+
211
+ // TODO: TextNode calls getTextContent() (NOT node.__text) within its exportJSON method
212
+ // which uses getLatest() to get the text from the original node with the same key.
213
+ // This is a deeper issue with the word "clone" here, it's still a reference to the
214
+ // same node as far as the LexicalEditor is concerned since it shares a key.
215
+ // We need a way to create a clone of a Node in memory with its own key, but
216
+ // until then this hack will work for the selected text extract use case.
217
+ if ($isTextNode(target)) {
218
+ const text = target.__text;
219
+ // If an uncollapsed selection ends or starts at the end of a line of specialized,
220
+ // TextNodes, such as code tokens, we will get a 'blank' TextNode here, i.e., one
221
+ // with text of length 0. We don't want this, it makes a confusing mess. Reset!
222
+ if (text.length > 0) {
223
+ serializedNode.text = text;
224
+ } else {
225
+ shouldInclude = false;
226
+ }
227
+ }
228
+ for (let i = 0; i < children.length; i++) {
229
+ const childNode = children[i];
230
+ const shouldIncludeChild = $appendNodesToJSON(editor, selection, childNode, serializedNode.children);
231
+ if (!shouldInclude && $isElementNode(currentNode) && shouldIncludeChild && currentNode.extractWithChild(childNode, selection, 'clone')) {
232
+ shouldInclude = true;
233
+ }
234
+ }
235
+ if (shouldInclude && !shouldExclude) {
236
+ targetArray.push(serializedNode);
237
+ } else if (Array.isArray(serializedNode.children)) {
238
+ for (let i = 0; i < serializedNode.children.length; i++) {
239
+ const serializedChildNode = serializedNode.children[i];
240
+ targetArray.push(serializedChildNode);
241
+ }
242
+ }
243
+ return shouldInclude;
244
+ }
245
+
246
+ // TODO why $ function with Editor instance?
247
+ /**
248
+ * Gets the Lexical JSON of the nodes inside the provided Selection.
249
+ *
250
+ * @param editor LexicalEditor to get the JSON content from.
251
+ * @param selection Selection to get the JSON content from.
252
+ * @returns an object with the editor namespace and a list of serializable nodes as JavaScript objects.
253
+ */
254
+ function $generateJSONFromSelectedNodes(editor, selection) {
255
+ const nodes = [];
256
+ const root = $getRoot();
257
+ const topLevelChildren = root.getChildren();
258
+ for (let i = 0; i < topLevelChildren.length; i++) {
259
+ const topLevelNode = topLevelChildren[i];
260
+ $appendNodesToJSON(editor, selection, topLevelNode, nodes);
261
+ }
262
+ return {
263
+ namespace: editor._config.namespace,
264
+ nodes
265
+ };
266
+ }
267
+
268
+ /**
269
+ * This method takes an array of objects conforming to the BaseSeralizedNode interface and returns
270
+ * an Array containing instances of the corresponding LexicalNode classes registered on the editor.
271
+ * Normally, you'd get an Array of BaseSerialized nodes from {@link $generateJSONFromSelectedNodes}
272
+ *
273
+ * @param serializedNodes an Array of objects conforming to the BaseSerializedNode interface.
274
+ * @returns an Array of Lexical Node objects.
275
+ */
276
+ function $generateNodesFromSerializedNodes(serializedNodes) {
277
+ const nodes = [];
278
+ for (let i = 0; i < serializedNodes.length; i++) {
279
+ const serializedNode = serializedNodes[i];
280
+ const node = $parseSerializedNode(serializedNode);
281
+ if ($isTextNode(node)) {
282
+ $addNodeStyle(node);
283
+ }
284
+ nodes.push(node);
285
+ }
286
+ return nodes;
287
+ }
288
+ const EVENT_LATENCY = 50;
289
+ let clipboardEventTimeout = null;
290
+
291
+ // TODO custom selection
292
+ // TODO potentially have a node customizable version for plain text
293
+ /**
294
+ * Copies the content of the current selection to the clipboard in
295
+ * text/plain, text/html, and application/x-lexical-editor (Lexical JSON)
296
+ * formats.
297
+ *
298
+ * @param editor the LexicalEditor instance to copy content from
299
+ * @param event the native browser ClipboardEvent to add the content to.
300
+ * @returns
301
+ */
302
+ async function copyToClipboard(editor, event, data) {
303
+ if (clipboardEventTimeout !== null) {
304
+ // Prevent weird race conditions that can happen when this function is run multiple times
305
+ // synchronously. In the future, we can do better, we can cancel/override the previously running job.
306
+ return false;
307
+ }
308
+ if (event !== null) {
309
+ return new Promise((resolve, reject) => {
310
+ editor.update(() => {
311
+ resolve($copyToClipboardEvent(editor, event, data));
312
+ });
313
+ });
314
+ }
315
+ const rootElement = editor.getRootElement();
316
+ const windowDocument = editor._window == null ? window.document : editor._window.document;
317
+ const domSelection = getDOMSelection(editor._window);
318
+ if (rootElement === null || domSelection === null) {
319
+ return false;
320
+ }
321
+ const element = windowDocument.createElement('span');
322
+ element.style.cssText = 'position: fixed; top: -1000px;';
323
+ element.append(windowDocument.createTextNode('#'));
324
+ rootElement.append(element);
325
+ const range = new Range();
326
+ range.setStart(element, 0);
327
+ range.setEnd(element, 1);
328
+ domSelection.removeAllRanges();
329
+ domSelection.addRange(range);
330
+ return new Promise((resolve, reject) => {
331
+ const removeListener = editor.registerCommand(COPY_COMMAND, secondEvent => {
332
+ if (objectKlassEquals(secondEvent, ClipboardEvent)) {
333
+ removeListener();
334
+ if (clipboardEventTimeout !== null) {
335
+ window.clearTimeout(clipboardEventTimeout);
336
+ clipboardEventTimeout = null;
337
+ }
338
+ resolve($copyToClipboardEvent(editor, secondEvent, data));
339
+ }
340
+ // Block the entire copy flow while we wait for the next ClipboardEvent
341
+ return true;
342
+ }, COMMAND_PRIORITY_CRITICAL);
343
+ // If the above hack execCommand hack works, this timeout code should never fire. Otherwise,
344
+ // the listener will be quickly freed so that the user can reuse it again
345
+ clipboardEventTimeout = window.setTimeout(() => {
346
+ removeListener();
347
+ clipboardEventTimeout = null;
348
+ resolve(false);
349
+ }, EVENT_LATENCY);
350
+ windowDocument.execCommand('copy');
351
+ element.remove();
352
+ });
353
+ }
354
+
355
+ // TODO shouldn't pass editor (pass namespace directly)
356
+ function $copyToClipboardEvent(editor, event, data) {
357
+ if (data === undefined) {
358
+ const domSelection = getDOMSelection(editor._window);
359
+ if (!domSelection) {
360
+ return false;
361
+ }
362
+ const anchorDOM = domSelection.anchorNode;
363
+ const focusDOM = domSelection.focusNode;
364
+ if (anchorDOM !== null && focusDOM !== null && !isSelectionWithinEditor(editor, anchorDOM, focusDOM)) {
365
+ return false;
366
+ }
367
+ const selection = $getSelection();
368
+ if (selection === null) {
369
+ return false;
370
+ }
371
+ data = $getClipboardDataFromSelection(selection);
372
+ }
373
+ event.preventDefault();
374
+ const clipboardData = event.clipboardData;
375
+ if (clipboardData === null) {
376
+ return false;
377
+ }
378
+ setLexicalClipboardDataTransfer(clipboardData, data);
379
+ return true;
380
+ }
381
+ const clipboardDataFunctions = [['text/html', $getHtmlContent], ['application/x-lexical-editor', $getLexicalContent]];
382
+
383
+ /**
384
+ * Serialize the content of the current selection to strings in
385
+ * text/plain, text/html, and application/x-lexical-editor (Lexical JSON)
386
+ * formats (as available).
387
+ *
388
+ * @param selection the selection to serialize (defaults to $getSelection())
389
+ * @returns LexicalClipboardData
390
+ */
391
+ function $getClipboardDataFromSelection(selection = $getSelection()) {
392
+ const clipboardData = {
393
+ 'text/plain': selection ? selection.getTextContent() : ''
394
+ };
395
+ if (selection) {
396
+ const editor = $getEditor();
397
+ for (const [mimeType, $editorFn] of clipboardDataFunctions) {
398
+ const v = $editorFn(editor, selection);
399
+ if (v !== null) {
400
+ clipboardData[mimeType] = v;
401
+ }
402
+ }
403
+ }
404
+ return clipboardData;
405
+ }
406
+
407
+ /**
408
+ * Call setData on the given clipboardData for each MIME type present
409
+ * in the given data (from {@link $getClipboardDataFromSelection})
410
+ *
411
+ * @param clipboardData the event.clipboardData to populate from data
412
+ * @param data The lexical data
413
+ */
414
+ function setLexicalClipboardDataTransfer(clipboardData, data) {
415
+ for (const k in data) {
416
+ const v = data[k];
417
+ if (v !== undefined) {
418
+ clipboardData.setData(k, v);
419
+ }
420
+ }
421
+ }
422
+
423
+ export { $generateJSONFromSelectedNodes, $generateNodesFromSerializedNodes, $getClipboardDataFromSelection, $getHtmlContent, $getLexicalContent, $insertDataTransferForPlainText, $insertDataTransferForRichText, $insertGeneratedNodes, copyToClipboard, setLexicalClipboardDataTransfer };
@@ -0,0 +1,19 @@
1
+ 'use client';
2
+
3
+ import * as LexicalClipboard_dev from './LexicalClipboard.dev.js';
4
+ import * as LexicalClipboard_prod from './LexicalClipboard.prod.js';
5
+
6
+ /**
7
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
8
+ *
9
+ * This source code is licensed under the MIT license found in the
10
+ * LICENSE file in the root directory of this source tree.
11
+ *
12
+ */
13
+
14
+ const mod = process.env.NODE_ENV === 'development' ? LexicalClipboard_dev : LexicalClipboard_prod;
15
+ const $getClipboardDataFromSelection = mod.$getClipboardDataFromSelection;
16
+ const $insertDataTransferForRichText = mod.$insertDataTransferForRichText;
17
+ const copyToClipboard = mod.copyToClipboard;
18
+
19
+ export { $getClipboardDataFromSelection, $insertDataTransferForRichText, copyToClipboard };