@lexical/table 0.2.1 → 0.2.2
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.
- package/LexicalTable.dev.js +66 -14
- package/LexicalTable.prod.js +46 -45
- package/package.json +3 -3
package/LexicalTable.dev.js
CHANGED
@@ -577,7 +577,6 @@ class TableSelection {
|
|
577
577
|
*
|
578
578
|
*
|
579
579
|
*/
|
580
|
-
const CriticalPriority = 4;
|
581
580
|
const LEXICAL_ELEMENT_KEY = '__lexicalTableSelection';
|
582
581
|
function applyTableHandlers(tableNode, tableElement, editor) {
|
583
582
|
const rootElement = editor.getRootElement();
|
@@ -687,6 +686,11 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
687
686
|
tableSelection.listenersToRemove.add(() => window.removeEventListener('mouseup', mouseUpCallback));
|
688
687
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_DOWN_COMMAND, payload => {
|
689
688
|
const selection = lexical.$getSelection();
|
689
|
+
|
690
|
+
if (!$isSelectionInTable(selection, tableNode)) {
|
691
|
+
return false;
|
692
|
+
}
|
693
|
+
|
690
694
|
const event = payload;
|
691
695
|
const direction = 'down';
|
692
696
|
|
@@ -736,9 +740,14 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
736
740
|
}
|
737
741
|
|
738
742
|
return false;
|
739
|
-
},
|
743
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
740
744
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_UP_COMMAND, payload => {
|
741
745
|
const selection = lexical.$getSelection();
|
746
|
+
|
747
|
+
if (!$isSelectionInTable(selection, tableNode)) {
|
748
|
+
return false;
|
749
|
+
}
|
750
|
+
|
742
751
|
const event = payload;
|
743
752
|
const direction = 'up';
|
744
753
|
|
@@ -788,9 +797,14 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
788
797
|
}
|
789
798
|
|
790
799
|
return false;
|
791
|
-
},
|
800
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
792
801
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_LEFT_COMMAND, payload => {
|
793
802
|
const selection = lexical.$getSelection();
|
803
|
+
|
804
|
+
if (!$isSelectionInTable(selection, tableNode)) {
|
805
|
+
return false;
|
806
|
+
}
|
807
|
+
|
794
808
|
const event = payload;
|
795
809
|
const direction = 'backward';
|
796
810
|
|
@@ -837,9 +851,14 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
837
851
|
}
|
838
852
|
|
839
853
|
return false;
|
840
|
-
},
|
854
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
841
855
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_ARROW_RIGHT_COMMAND, payload => {
|
842
856
|
const selection = lexical.$getSelection();
|
857
|
+
|
858
|
+
if (!$isSelectionInTable(selection, tableNode)) {
|
859
|
+
return false;
|
860
|
+
}
|
861
|
+
|
843
862
|
const event = payload;
|
844
863
|
const direction = 'forward';
|
845
864
|
|
@@ -886,10 +905,14 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
886
905
|
}
|
887
906
|
|
888
907
|
return false;
|
889
|
-
},
|
908
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
890
909
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.DELETE_CHARACTER_COMMAND, () => {
|
891
910
|
const selection = lexical.$getSelection();
|
892
911
|
|
912
|
+
if (!$isSelectionInTable(selection, tableNode)) {
|
913
|
+
return false;
|
914
|
+
}
|
915
|
+
|
893
916
|
if (lexical.$isGridSelection(selection)) {
|
894
917
|
tableSelection.clearText();
|
895
918
|
return true;
|
@@ -906,10 +929,14 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
906
929
|
}
|
907
930
|
|
908
931
|
return false;
|
909
|
-
},
|
932
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
910
933
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_BACKSPACE_COMMAND, payload => {
|
911
934
|
const selection = lexical.$getSelection();
|
912
935
|
|
936
|
+
if (!$isSelectionInTable(selection, tableNode)) {
|
937
|
+
return false;
|
938
|
+
}
|
939
|
+
|
913
940
|
if (lexical.$isGridSelection(selection)) {
|
914
941
|
const event = payload;
|
915
942
|
event.preventDefault();
|
@@ -925,10 +952,14 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
925
952
|
}
|
926
953
|
|
927
954
|
return false;
|
928
|
-
},
|
955
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
929
956
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.FORMAT_TEXT_COMMAND, payload => {
|
930
957
|
const selection = lexical.$getSelection();
|
931
958
|
|
959
|
+
if (!$isSelectionInTable(selection, tableNode)) {
|
960
|
+
return false;
|
961
|
+
}
|
962
|
+
|
932
963
|
if (lexical.$isGridSelection(selection)) {
|
933
964
|
tableSelection.formatCells(payload);
|
934
965
|
return true;
|
@@ -941,10 +972,14 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
941
972
|
}
|
942
973
|
|
943
974
|
return false;
|
944
|
-
},
|
975
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
945
976
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.INSERT_TEXT_COMMAND, payload => {
|
946
977
|
const selection = lexical.$getSelection();
|
947
978
|
|
979
|
+
if (!$isSelectionInTable(selection, tableNode)) {
|
980
|
+
return false;
|
981
|
+
}
|
982
|
+
|
948
983
|
if (lexical.$isGridSelection(selection)) {
|
949
984
|
tableSelection.clearHighlight();
|
950
985
|
return false;
|
@@ -957,10 +992,14 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
957
992
|
}
|
958
993
|
|
959
994
|
return false;
|
960
|
-
},
|
995
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
961
996
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.KEY_TAB_COMMAND, payload => {
|
962
997
|
const selection = lexical.$getSelection();
|
963
998
|
|
999
|
+
if (!$isSelectionInTable(selection, tableNode)) {
|
1000
|
+
return false;
|
1001
|
+
}
|
1002
|
+
|
964
1003
|
if (lexical.$isRangeSelection(selection)) {
|
965
1004
|
const tableCellNode = utils.$findMatchingParent(selection.anchor.getNode(), n => $isTableCellNode(n));
|
966
1005
|
|
@@ -979,7 +1018,10 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
979
1018
|
}
|
980
1019
|
|
981
1020
|
return false;
|
982
|
-
},
|
1021
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
1022
|
+
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.FOCUS_COMMAND, payload => {
|
1023
|
+
return tableNode.isSelected();
|
1024
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
983
1025
|
tableSelection.listenersToRemove.add(editor.registerCommand(lexical.SELECTION_CHANGE_COMMAND, payload => {
|
984
1026
|
const selection = lexical.$getSelection();
|
985
1027
|
|
@@ -992,13 +1034,13 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
992
1034
|
|
993
1035
|
if (containsPartialTable) {
|
994
1036
|
const isBackward = selection.isBackward();
|
995
|
-
const startNode = isBackward ? focusNode : anchorNode;
|
996
1037
|
const modifiedSelection = lexical.$createRangeSelection();
|
997
1038
|
const tableIndex = tableNode.getIndexWithinParent();
|
998
1039
|
const parentKey = tableNode.getParentOrThrow().getKey();
|
1040
|
+
modifiedSelection.anchor.set(selection.anchor.key, selection.anchor.offset, selection.anchor.type); // Set selection to before or after table on the root node.
|
1041
|
+
|
1042
|
+
modifiedSelection.focus.set(parentKey, isBackward ? tableIndex - 1 : tableIndex + 1, 'element');
|
999
1043
|
isRangeSelectionHijacked = true;
|
1000
|
-
(isBackward ? modifiedSelection.focus : modifiedSelection.anchor).set(startNode.getKey(), (isBackward ? selection.focus : selection.anchor).offset, lexical.$isTextNode(startNode) ? 'text' : 'element');
|
1001
|
-
(isBackward ? modifiedSelection.anchor : modifiedSelection.focus).set(parentKey, isBackward ? tableIndex - 1 : tableIndex + 1, 'element');
|
1002
1044
|
lexical.$setSelection(modifiedSelection);
|
1003
1045
|
$addHighlightStyleToTable(tableSelection);
|
1004
1046
|
return true;
|
@@ -1013,7 +1055,7 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
1013
1055
|
}
|
1014
1056
|
|
1015
1057
|
return false;
|
1016
|
-
},
|
1058
|
+
}, lexical.COMMAND_PRIORITY_CRITICAL));
|
1017
1059
|
return tableSelection;
|
1018
1060
|
}
|
1019
1061
|
function attachTableSelectionToTableElement(tableElement, tableSelection) {
|
@@ -1267,6 +1309,16 @@ const adjustFocusNodeInDirection = (tableSelection, tableNode, x, y, direction)
|
|
1267
1309
|
return false;
|
1268
1310
|
};
|
1269
1311
|
|
1312
|
+
function $isSelectionInTable(selection, tableNode) {
|
1313
|
+
if (lexical.$isRangeSelection(selection) || lexical.$isGridSelection(selection)) {
|
1314
|
+
const isAnchorInside = tableNode.isParentOf(selection.anchor.getNode());
|
1315
|
+
const isFocusInside = tableNode.isParentOf(selection.focus.getNode());
|
1316
|
+
return isAnchorInside && isFocusInside;
|
1317
|
+
}
|
1318
|
+
|
1319
|
+
return false;
|
1320
|
+
}
|
1321
|
+
|
1270
1322
|
function selectTableCellNode(tableCell) {
|
1271
1323
|
const possibleParagraph = tableCell.getChildren().find(n => lexical.$isParagraphNode(n));
|
1272
1324
|
|
package/LexicalTable.prod.js
CHANGED
@@ -4,54 +4,55 @@
|
|
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
|
-
var
|
8
|
-
class
|
7
|
+
var f=require("lexical"),p=require("@lexical/utils");const q={NO_STATUS:0,ROW:1,COLUMN:2,BOTH:3};
|
8
|
+
class t extends f.GridCellNode{static getType(){return"tablecell"}static clone(a){return new t(a.__headerState,a.__colSpan,a.__width,a.__key)}static importDOM(){return{td:()=>({conversion:w,priority:0}),th:()=>({conversion:w,priority:0})}}constructor(a=q.NO_STATUS,b=1,d,h){super(b,h);this.__headerState=a;this.__width=d}createDOM(a){const b=document.createElement(this.getTag());this.__width&&(b.style.width=`${this.__width}px`);p.addClassNamesToElement(b,a.theme.tableCell,this.hasHeader()&&a.theme.tableCellHeader);
|
9
9
|
return b}exportDOM(a){({element:a}=super.exportDOM(a));if(a){const b=this.getParentOrThrow().getChildrenSize();a.style.border="1px solid black";a.style.width=`${this.getWidth()||Math.max(90,700/b)}px`;a.style.verticalAlign="top";a.style.textAlign="start";this.hasHeader()&&(a.style.backgroundColor="#f2f3f5")}return{element:a}}getTag(){return this.hasHeader()?"th":"td"}setHeaderStyles(a){this.getWritable().__headerState=a;return this.__headerState}getHeaderStyles(){return this.getLatest().__headerState}setWidth(a){this.getWritable().__width=
|
10
10
|
a;return this.__width}getWidth(){return this.getLatest().__width}toggleHeaderStyle(a){const b=this.getWritable();b.__headerState=(b.__headerState&a)===a?b.__headerState-a:b.__headerState+a;b.__headerState=b.__headerState;return b}hasHeaderState(a){return(this.getHeaderStyles()&a)===a}hasHeader(){return this.getLatest().__headerState!==q.NO_STATUS}updateDOM(a){return a.__headerState!==this.__headerState||a.__width!==this.__width}collapseAtStart(){return!0}canBeEmpty(){return!1}}
|
11
|
-
function w(a){a=a.nodeName.toLowerCase();return{node:x("th"===a?q.ROW:q.NO_STATUS),forChild:(b,d)=>{if(y(d)&&!
|
12
|
-
function
|
13
|
-
class
|
14
|
-
this.__height}canBeEmpty(){return!1}}function
|
11
|
+
function w(a){a=a.nodeName.toLowerCase();return{node:x("th"===a?q.ROW:q.NO_STATUS),forChild:(b,d)=>{if(y(d)&&!f.$isElementNode(b)){d=f.$createParagraphNode();if(f.$isLineBreakNode(b)&&"\n"===b.getTextContent())return null;d.append(b);return d}return b}}}function x(a,b=1,d){return new t(a,b,d)}function y(a){return a instanceof t}
|
12
|
+
function z(a){throw Error(`Minified Lexical error #${a}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
|
13
|
+
class A extends f.GridRowNode{static getType(){return"tablerow"}static clone(a){return new A(a.__height,a.__key)}static importDOM(){return{tr:()=>({conversion:B,priority:0})}}constructor(a,b){super(b);this.__height=a}createDOM(a){const b=document.createElement("tr");this.__height&&(b.style.height=`${this.__height}px`);p.addClassNamesToElement(b,a.theme.tableRow);return b}setHeight(a){this.getWritable().__height=a;return this.__height}getHeight(){return this.getLatest().__height}updateDOM(a){return a.__height!==
|
14
|
+
this.__height}canBeEmpty(){return!1}}function B(){return{node:C()}}function C(a){return new A(a)}function D(a){return a instanceof A}
|
15
15
|
if("undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement){const a=document.createElement("style");a.innerHTML="\n table.disable-selection {\n -webkit-touch-callout: none;\n -webkit-user-select: none; \n -khtml-user-select: none; \n -moz-user-select: none; \n -ms-user-select: none; \n user-select: none;\n }\n \n .disable-selection span::selection{\n background-color: transparent;\n }\n .disable-selection br::selection{\n background-color: transparent;\n }\n ";document.body&&
|
16
16
|
document.body.append(a)}
|
17
|
-
class
|
18
|
-
{this.editor.update(()=>{var d=!1;for(let h=0;h<b.length;h++){const c=b[h].target.nodeName;if("TABLE"===c||"TR"===c){d=!0;break}}if(d){d=this.editor.getElementByKey(this.tableNodeKey);if(!d)throw Error("Expected to find TableElement in DOM");this.grid=
|
19
|
-
|
20
|
-
{const a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.remove("disable-selection");this.hasHijackedSelectionStyles=!1})}disableHighlightStyle(){this.editor.update(()=>{const a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.add("disable-selection");this.hasHijackedSelectionStyles=!0})}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var d=
|
21
|
-
if(!
|
22
|
-
(d=
|
23
|
-
window.getSelection().setBaseAndExtent(a.elem,0,a.elem,0);var b=
|
24
|
-
this.editor.dispatchCommand(
|
25
|
-
{k!==h&&k.remove()})}}),
|
26
|
-
function
|
27
|
-
function
|
28
|
-
function
|
29
|
-
function O(a){a.enableHighlightStyle();
|
17
|
+
class E{constructor(a,b){this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.listenersToRemove=new Set;this.tableNodeKey=b;this.editor=a;this.grid={cells:[],columns:0,rows:0};this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;this.trackTableGrid()}getGrid(){return this.grid}removeListeners(){Array.from(this.listenersToRemove).forEach(a=>a())}trackTableGrid(){const a=new MutationObserver(b=>
|
18
|
+
{this.editor.update(()=>{var d=!1;for(let h=0;h<b.length;h++){const c=b[h].target.nodeName;if("TABLE"===c||"TR"===c){d=!0;break}}if(d){d=this.editor.getElementByKey(this.tableNodeKey);if(!d)throw Error("Expected to find TableElement in DOM");this.grid=F(d)}})});this.editor.update(()=>{const b=this.editor.getElementByKey(this.tableNodeKey);if(!b)throw Error("Expected to find TableElement in DOM");this.grid=F(b);a.observe(b,{childList:!0,subtree:!0})})}clearHighlight(){this.editor.update(()=>{var a=
|
19
|
+
f.$getNodeByKey(this.tableNodeKey);if(!G(a))throw Error("Expected TableNode.");a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a=F(a);this.isHighlightingCells=!1;this.currentY=this.currentX=this.startY=this.startX=-1;this.focusCell=this.anchorCell=this.focusCellNodeKey=this.anchorCellNodeKey=this.gridSelection=null;this.hasHijackedSelectionStyles=!1;H(a,null);f.$setSelection(null);this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND);this.enableHighlightStyle()})}enableHighlightStyle(){this.editor.update(()=>
|
20
|
+
{const a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.remove("disable-selection");this.hasHijackedSelectionStyles=!1})}disableHighlightStyle(){this.editor.update(()=>{const a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.add("disable-selection");this.hasHijackedSelectionStyles=!0})}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var d=f.$getNodeByKey(this.tableNodeKey);
|
21
|
+
if(!G(d))throw Error("Expected TableNode.");if(!this.editor.getElementByKey(this.tableNodeKey))throw Error("Expected to find TableElement in DOM");d=a.x;const h=a.y;this.focusCell=a;const c=window.getSelection();null!==this.anchorCell&&c.setBaseAndExtent(this.anchorCell.elem,0,a.elem,0);if(!this.isHighlightingCells&&(this.startX!==d||this.startY!==h||b))this.isHighlightingCells=!0,this.disableHighlightStyle();else if(d===this.currentX&&h===this.currentY)return;this.currentX=d;this.currentY=h;this.isHighlightingCells&&
|
22
|
+
(d=f.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&y(d)&&(d=d.getKey(),this.gridSelection=f.$createGridSelection(),this.focusCellNodeKey=d,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),f.$setSelection(this.gridSelection),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND),H(this.grid,this.gridSelection)))})}setAnchorCellForSelection(a){this.editor.update(()=>{this.anchorCell=a;this.startX=a.x;this.startY=a.y;
|
23
|
+
window.getSelection().setBaseAndExtent(a.elem,0,a.elem,0);var b=f.$getNearestNodeFromDOMNode(a.elem);y(b)&&(b=b.getKey(),this.gridSelection=f.$createGridSelection(),this.anchorCellNodeKey=b)})}formatCells(a){this.editor.update(()=>{const b=f.$getSelection();f.$isGridSelection(b)||z(79);const d=f.$createRangeSelection(),h=d.anchor,c=d.focus;b.getNodes().forEach(k=>{y(k)&&0!==k.getTextContentSize()&&(h.set(k.getKey(),0,"element"),c.set(k.getKey(),k.getChildrenSize(),"element"),d.formatText(a))});f.$setSelection(b);
|
24
|
+
this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND)})}clearText(){this.editor.update(()=>{const a=f.$getNodeByKey(this.tableNodeKey);if(!G(a))throw Error("Expected TableNode.");var b=f.$getSelection();f.$isGridSelection(b)||z(79);b=b.getNodes().filter(y);b.length===this.grid.columns*this.grid.rows?(a.selectPrevious(),a.remove(),this.clearHighlight()):(b.forEach(d=>{if(f.$isElementNode(d)){const h=f.$createParagraphNode(),c=f.$createTextNode();h.append(c);d.append(h);d.getChildren().forEach(k=>
|
25
|
+
{k!==h&&k.remove()})}}),H(this.grid,null),f.$setSelection(null),this.editor.dispatchCommand(f.SELECTION_CHANGE_COMMAND))})}}function I(a){for(;null!=a;){const b=a.nodeName;if("TD"===b||"TH"===b){a=a._cell;if(void 0===a)break;return a}a=a.parentNode}return null}
|
26
|
+
function F(a){const b=[],d={cells:b,columns:0,rows:0};var h=a.firstChild;let c=a=0;for(b.length=0;null!=h;){var k=h.nodeName;if("TD"===k||"TH"===k)k={elem:h,highlighted:!1,x:a,y:c},h._cell=k,void 0===b[c]&&(b[c]=[]),b[c][a]=k;else if(k=h.firstChild,null!=k){h=k;continue}k=h.nextSibling;if(null!=k)a++,h=k;else if(k=h.parentNode,null!=k){h=k.nextSibling;if(null==h)break;c++;a=0}}d.columns=a+1;d.rows=c+1;return d}
|
27
|
+
function H(a,b){const d=[],h=new Set(b?b.getNodes():[]);J(a,(c,k)=>{const n=c.elem;h.has(k)?(c.highlighted=!0,n.style.setProperty("background-color","rgb(172, 206, 247)"),n.style.setProperty("caret-color","transparent"),d.push(c)):(c.highlighted=!1,n.style.removeProperty("background-color"),n.style.removeProperty("caret-color"),n.getAttribute("style")||n.removeAttribute("style"))});return d}
|
28
|
+
function J(a,b){({cells:a}=a);for(let d=0;d<a.length;d++){const h=a[d];for(let c=0;c<h.length;c++){const k=h[c],n=f.$getNearestNodeFromDOMNode(k.elem);null!==n&&b(k,n,{x:c,y:d})}}}function N(a){a.disableHighlightStyle();J(a.grid,b=>{const d=b.elem;b.highlighted=!0;d.style.setProperty("background-color","rgb(172, 206, 247)");d.style.setProperty("caret-color","transparent")})}
|
29
|
+
function O(a){a.enableHighlightStyle();J(a.grid,b=>{const d=b.elem;b.highlighted=!1;d.style.removeProperty("background-color");d.style.removeProperty("caret-color");d.getAttribute("style")||d.removeAttribute("style")})}
|
30
30
|
const Q=(a,b,d,h,c)=>{switch(c){case "backward":case "forward":return c="forward"===c,d!==(c?a.grid.columns-1:0)?P(b.getCellNodeFromCordsOrThrow(d+(c?1:-1),h,a.grid)):h!==(c?a.grid.rows-1:0)?P(b.getCellNodeFromCordsOrThrow(c?0:a.grid.columns-1,h+(c?1:-1),a.grid)):c?b.selectNext():b.selectPrevious(),!0;case "up":return 0!==h?P(b.getCellNodeFromCordsOrThrow(d,h-1,a.grid)):b.selectPrevious(),!0;case "down":return h!==a.grid.rows-1?P(b.getCellNodeFromCordsOrThrow(d,h+1,a.grid)):b.selectNext(),!0}return!1},
|
31
31
|
R=(a,b,d,h,c)=>{switch(c){case "backward":case "forward":return c="forward"===c,d!==(c?a.grid.columns-1:0)&&a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d+(c?1:-1),h,a.grid)),!0;case "up":if(0!==h)return a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d,h-1,a.grid)),!0;break;case "down":if(h!==a.grid.rows-1)return a.adjustFocusCellForSelection(b.getCellFromCordsOrThrow(d,h+1,a.grid)),!0}return!1};
|
32
|
-
function P(a){const b=a.getChildren().find(d=>
|
33
|
-
class
|
34
|
-
if(!
|
35
|
-
b,d){d||
|
36
|
-
function
|
37
|
-
exports.$createTableNodeWithDimensions=function(a,b,d=!0){const h=
|
38
|
-
exports.$deleteTableColumn=function(a,b){const d=a.getChildren();for(let c=0;c<d.length;c++){var h=d[c];if(
|
39
|
-
exports.$getTableColumnIndexFromTableCellNode=function(a){return
|
40
|
-
exports.$insertTableColumn=function(a,b,d=!0,h){const c=a.getChildren();for(let r=0;r<c.length;r++){const v=c[r];if(
|
41
|
-
exports.$insertTableRow=function(a,b,d=!0,h,c){var k=a.getChildren();if(b>=k.length||0>b)throw Error("Table row target index out of range");b=k[b];if(
|
42
|
-
r=
|
43
|
-
exports.$removeTableRowAtIndex=function(a,b){const d=a.getChildren();if(b>=d.length||0>b)throw Error("Expected table cell to be inside of table row.");d[b].remove();return a};exports.INSERT_TABLE_COMMAND=
|
44
|
-
exports.applyTableHandlers=function(a,b,d){const h=d.getRootElement();if(null===h)throw Error("No root element.");const c=new
|
45
|
-
document.addEventListener("mouseup",()=>{k=!1},{capture:!0,once:!0}))}},0)});b.addEventListener("mousemove",
|
46
|
-
if(
|
47
|
-
|
48
|
-
if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);
|
49
|
-
if((l=l.getLastChild())&&
|
50
|
-
|
51
|
-
l.y,"backward")):Q(c,a,l.x,l.y,"backward")}}else if(
|
52
|
-
c.grid);if(null==p.$findMatchingParent(
|
53
|
-
c.grid);e.preventDefault();
|
54
|
-
|
55
|
-
(
|
56
|
-
c.listenersToRemove.add(d.registerCommand(
|
57
|
-
|
32
|
+
function S(a,b){if(f.$isRangeSelection(a)||f.$isGridSelection(a)){const d=b.isParentOf(a.anchor.getNode());a=b.isParentOf(a.focus.getNode());return d&&a}return!1}function P(a){const b=a.getChildren().find(d=>f.$isParagraphNode(d));f.$isParagraphNode(b)?b.selectEnd():a.selectEnd()}
|
33
|
+
class T extends f.GridNode{static getType(){return"table"}static clone(a){return new T(a.__key)}static importDOM(){return{table:()=>({conversion:U,priority:0})}}constructor(a){super(a)}createDOM(a){const b=document.createElement("table");p.addClassNamesToElement(b,a.theme.table);return b}updateDOM(){return!1}exportDOM(a){return{...super.exportDOM(a),after:b=>{if(b){const d=b.cloneNode(),h=document.createElement("colgroup"),c=document.createElement("tbody");c.append(...b.children);b=this.getFirstChildOrThrow();
|
34
|
+
if(!D(b))throw Error("Expected to find row node.");b=b.getChildrenSize();for(let k=0;k<b;k++){const n=document.createElement("col");h.append(n)}d.replaceChildren(h,c);return d}}}}canExtractContents(){return!1}canBeEmpty(){return!1}getCordsFromCellNode(a,b){b||z(55);const {rows:d,cells:h}=b;for(b=0;b<d;b++){var c=h[b];if(null==c)throw Error(`Row not found at y:${b}`);c=c.findIndex(({elem:k})=>f.$getNearestNodeFromDOMNode(k)===a);if(-1!==c)return{x:c,y:b}}throw Error("Cell not found in table.");}getCellFromCords(a,
|
35
|
+
b,d){d||z(55);({cells:d}=d);b=d[b];if(null==b)return null;a=b[a];return null==a?null:a}getCellFromCordsOrThrow(a,b,d){a=this.getCellFromCords(a,b,d);if(!a)throw Error("Cell not found at cords.");return a}getCellNodeFromCords(a,b,d){a=this.getCellFromCords(a,b,d);if(null==a)return null;a=f.$getNearestNodeFromDOMNode(a.elem);return y(a)?a:null}getCellNodeFromCordsOrThrow(a,b,d){a=this.getCellNodeFromCords(a,b,d);if(!a)throw Error("Node at cords not TableCellNode.");return a}canSelectBefore(){return!0}}
|
36
|
+
function U(){return{node:V()}}function V(){return new T}function G(a){return a instanceof T}function W(a){a=p.$findMatchingParent(a,b=>D(b));if(D(a))return a;throw Error("Expected table cell to be inside of table row.");}function X(a){a=p.$findMatchingParent(a,b=>G(b));if(G(a))return a;throw Error("Expected table cell to be inside of table.");}const Y=f.createCommand();exports.$createTableCellNode=x;exports.$createTableNode=V;
|
37
|
+
exports.$createTableNodeWithDimensions=function(a,b,d=!0){const h=V();for(let k=0;k<a;k++){const n=C();for(let r=0;r<b;r++){var c=q.NO_STATUS;d&&(0===k&&(c|=q.ROW),0===r&&(c|=q.COLUMN));c=x(c);const v=f.$createParagraphNode();v.append(f.$createTextNode());c.append(v);n.append(c)}h.append(n)}return h};exports.$createTableRowNode=C;
|
38
|
+
exports.$deleteTableColumn=function(a,b){const d=a.getChildren();for(let c=0;c<d.length;c++){var h=d[c];if(D(h)){h=h.getChildren();if(b>=h.length||0>b)throw Error("Table column target index out of range");h[b].remove()}}return a};exports.$getElementGridForTableNode=function(a,b){a=a.getElementByKey(b.getKey());if(null==a)throw Error("Table Element Not Found");return F(a)};exports.$getTableCellNodeFromLexicalNode=function(a){a=p.$findMatchingParent(a,b=>y(b));return y(a)?a:null};
|
39
|
+
exports.$getTableColumnIndexFromTableCellNode=function(a){return W(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=X;exports.$getTableRowIndexFromTableCellNode=function(a){const b=W(a);return X(b).getChildren().findIndex(d=>d.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=W;
|
40
|
+
exports.$insertTableColumn=function(a,b,d=!0,h){const c=a.getChildren();for(let r=0;r<c.length;r++){const v=c[r];if(D(v))for(let g=0;g<h;g++){var k=q.NO_STATUS;0===r&&(k|=q.ROW);k=x(k);k.append(f.$createParagraphNode());var n=v.getChildren();if(b>=n.length||0>b)throw Error("Table column target index out of range");n=n[b];d?n.insertAfter(k):n.insertBefore(k)}}return a};
|
41
|
+
exports.$insertTableRow=function(a,b,d=!0,h,c){var k=a.getChildren();if(b>=k.length||0>b)throw Error("Table row target index out of range");b=k[b];if(D(b))for(k=0;k<h;k++){const v=b.getChildren(),g=v.length,e=C();for(let l=0;l<g;l++){var n=v[l];y(n)||z(73);var r=c;const m=X(n),{x:u,y:K}=m.getCordsFromCellNode(n,r);n={above:m.getCellNodeFromCords(u,K-1,r),below:m.getCellNodeFromCords(u,K+1,r),left:m.getCellNodeFromCords(u-1,K,r),right:m.getCellNodeFromCords(u+1,K,r)};const {above:L,below:M}=n;n=q.NO_STATUS;
|
42
|
+
r=L&&L.getWidth()||M&&M.getWidth()||null;if(L&&L.hasHeaderState(q.COLUMN)||M&&M.hasHeaderState(q.COLUMN))n|=q.COLUMN;n=x(n,1,r);n.append(f.$createParagraphNode());e.append(n)}d?b.insertAfter(e):b.insertBefore(e)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=y;exports.$isTableNode=G;exports.$isTableRowNode=D;
|
43
|
+
exports.$removeTableRowAtIndex=function(a,b){const d=a.getChildren();if(b>=d.length||0>b)throw Error("Expected table cell to be inside of table row.");d[b].remove();return a};exports.INSERT_TABLE_COMMAND=Y;exports.TableCellHeaderStates=q;exports.TableCellNode=t;exports.TableNode=T;exports.TableRowNode=A;exports.TableSelection=E;
|
44
|
+
exports.applyTableHandlers=function(a,b,d){const h=d.getRootElement();if(null===h)throw Error("No root element.");const c=new E(d,a.getKey());b.__lexicalTableSelection=c;let k=!1,n=!1;b.addEventListener("dblclick",g=>{const e=I(g.target);null!==e&&(g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),c.setAnchorCellForSelection(e),c.adjustFocusCellForSelection(e,!0),k=!1)});b.addEventListener("mousedown",g=>{setTimeout(()=>{if(0===g.button){var e=I(g.target);null!==e&&(c.setAnchorCellForSelection(e),
|
45
|
+
document.addEventListener("mouseup",()=>{k=!1},{capture:!0,once:!0}))}},0)});b.addEventListener("mousemove",g=>{n&&(g.preventDefault(),g.stopPropagation(),g.stopImmediatePropagation());if(k){const e=I(g.target);if(null!==e){const l=e.x,m=e.y;k&&(c.startX!==l||c.startY!==m||c.isHighlightingCells)&&(g.preventDefault(),k=!0,c.adjustFocusCellForSelection(e))}}});b.addEventListener("mouseup",()=>{k&&(k=!1)});b.addEventListener("mouseleave",()=>{});const r=g=>{k=!0;0===g.button&&d.update(()=>{const e=f.$getSelection();
|
46
|
+
if(f.$isGridSelection(e)&&e.gridKey===c.tableNodeKey&&h.contains(g.target))return c.clearHighlight()})};window.addEventListener("mousedown",r);c.listenersToRemove.add(()=>window.removeEventListener("mousedown",r));const v=()=>{k=!1};window.addEventListener("mouseup",v);c.listenersToRemove.add(()=>window.removeEventListener("mouseup",v));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_DOWN_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=
|
47
|
+
p.$findMatchingParent(e.anchor.getNode(),u=>y(u));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);e=p.$findMatchingParent(e.anchor.getNode(),u=>f.$isElementNode(u));if(null==e)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&e.isParentOf(l)||e===l||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,m.y,c.grid)),R(c,a,m.x,m.y,"down")):Q(c,a,m.x,m.y,"down")}}else if(f.$isGridSelection(e)&&
|
48
|
+
g.shiftKey){m=e.focus.getNode();if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,m.x,m.y,"down")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_UP_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=p.$findMatchingParent(e.anchor.getNode(),u=>y(u));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);e=p.$findMatchingParent(e.anchor.getNode(),
|
49
|
+
u=>f.$isElementNode(u));if(null==e)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&e.isParentOf(l)||e===l||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(m.x,m.y,c.grid)),R(c,a,m.x,m.y,"up")):Q(c,a,m.x,m.y,"up")}}else if(f.$isGridSelection(e)&&g.shiftKey){m=e.focus.getNode();if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();
|
50
|
+
return R(c,a,m.x,m.y,"up")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_LEFT_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=p.$findMatchingParent(e.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,c.grid);if(null==p.$findMatchingParent(e.anchor.getNode(),m=>f.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(0===e.anchor.offset||g.shiftKey)return g.preventDefault(),
|
51
|
+
g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,l.y,c.grid)),R(c,a,l.x,l.y,"backward")):Q(c,a,l.x,l.y,"backward")}}else if(f.$isGridSelection(e)&&g.shiftKey){e=e.focus.getNode();if(!y(e))return!1;e=a.getCordsFromCellNode(e,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,e.x,e.y,"backward")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_ARROW_RIGHT_COMMAND,
|
52
|
+
g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){if(e.isCollapsed()){var l=p.$findMatchingParent(e.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,c.grid);if(null==p.$findMatchingParent(e.anchor.getNode(),m=>f.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(e.anchor.offset===e.anchor.getNode().getTextContentSize()||g.shiftKey)return g.preventDefault(),g.stopImmediatePropagation(),g.stopPropagation(),g.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,
|
53
|
+
l.y,c.grid)),R(c,a,l.x,l.y,"forward")):Q(c,a,l.x,l.y,"forward")}}else if(f.$isGridSelection(e)&&g.shiftKey){e=e.focus.getNode();if(!y(e))return!1;e=a.getCordsFromCellNode(e,c.grid);g.preventDefault();g.stopImmediatePropagation();g.stopPropagation();return R(c,a,e.x,e.y,"forward")}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.DELETE_CHARACTER_COMMAND,()=>{const g=f.$getSelection();if(!S(g,a))return!1;if(f.$isGridSelection(g))return c.clearText(),!0;if(f.$isRangeSelection(g)){const e=
|
54
|
+
p.$findMatchingParent(g.anchor.getNode(),l=>y(l));if(!y(e))return!1;if(g.isCollapsed()&&0===g.anchor.offset&&0===g.anchor.getNode().getPreviousSiblings().length)return!0}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_BACKSPACE_COMMAND,g=>{const e=f.$getSelection();if(!S(e,a))return!1;if(f.$isGridSelection(e))return g.preventDefault(),g.stopPropagation(),c.clearText(),!0;f.$isRangeSelection(e)&&(g=p.$findMatchingParent(e.anchor.getNode(),l=>y(l)),y(g));return!1},
|
55
|
+
f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.FORMAT_TEXT_COMMAND,g=>{const e=f.$getSelection();if(!S(e,a))return!1;if(f.$isGridSelection(e))return c.formatCells(g),!0;f.$isRangeSelection(e)&&(g=p.$findMatchingParent(e.anchor.getNode(),l=>y(l)),y(g));return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.INSERT_TEXT_COMMAND,()=>{var g=f.$getSelection();if(!S(g,a))return!1;f.$isGridSelection(g)?c.clearHighlight():f.$isRangeSelection(g)&&(g=p.$findMatchingParent(g.anchor.getNode(),
|
56
|
+
e=>y(e)),y(g));return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.KEY_TAB_COMMAND,g=>{var e=f.$getSelection();if(!S(e,a))return!1;if(f.$isRangeSelection(e)){const l=p.$findMatchingParent(e.anchor.getNode(),m=>y(m));if(!y(l))return!1;if(e.isCollapsed())return e=a.getCordsFromCellNode(l,c.grid),g.preventDefault(),Q(c,a,e.x,e.y,g.shiftKey?"backward":"forward"),!0}return!1},f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.FOCUS_COMMAND,()=>a.isSelected(),
|
57
|
+
f.COMMAND_PRIORITY_CRITICAL));c.listenersToRemove.add(d.registerCommand(f.SELECTION_CHANGE_COMMAND,()=>{const g=f.$getSelection();if(g&&f.$isRangeSelection(g)&&!g.isCollapsed()){var e=g.anchor.getNode(),l=g.focus.getNode();e=a.isParentOf(e);l=a.isParentOf(l);if(e&&!l||l&&!e){l=g.isBackward();e=f.$createRangeSelection();const m=a.getIndexWithinParent(),u=a.getParentOrThrow().getKey();e.anchor.set(g.anchor.key,g.anchor.offset,g.anchor.type);e.focus.set(u,l?m-1:m+1,"element");n=!0;f.$setSelection(e);
|
58
|
+
N(c);return!0}}c.hasHijackedSelectionStyles&&!a.isSelected()?(O(c),n=!1):!c.hasHijackedSelectionStyles&&a.isSelected()&&N(c);return!1},f.COMMAND_PRIORITY_CRITICAL));return c};exports.getCellFromTarget=I;exports.getTableSelectionFromTableElement=function(a){return a.__lexicalTableSelection};
|
package/package.json
CHANGED
@@ -8,13 +8,13 @@
|
|
8
8
|
"table"
|
9
9
|
],
|
10
10
|
"license": "MIT",
|
11
|
-
"version": "0.2.
|
11
|
+
"version": "0.2.2",
|
12
12
|
"main": "LexicalTable.js",
|
13
13
|
"peerDependencies": {
|
14
|
-
"lexical": "0.2.
|
14
|
+
"lexical": "0.2.2"
|
15
15
|
},
|
16
16
|
"dependencies": {
|
17
|
-
"@lexical/utils": "0.2.
|
17
|
+
"@lexical/utils": "0.2.2"
|
18
18
|
},
|
19
19
|
"repository": {
|
20
20
|
"type": "git",
|