@lexical/table 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LexicalTable.dev.js +30 -22
- package/LexicalTable.prod.js +46 -46
- package/package.json +3 -3
package/LexicalTable.dev.js
CHANGED
@@ -313,6 +313,7 @@ class TableSelection {
|
|
313
313
|
this.focusCellNodeKey = null;
|
314
314
|
this.anchorCell = null;
|
315
315
|
this.focusCell = null;
|
316
|
+
this.hasHijackedSelectionStyles = false;
|
316
317
|
this.trackTableGrid();
|
317
318
|
}
|
318
319
|
|
@@ -393,6 +394,7 @@ class TableSelection {
|
|
393
394
|
this.focusCellNodeKey = null;
|
394
395
|
this.anchorCell = null;
|
395
396
|
this.focusCell = null;
|
397
|
+
this.hasHijackedSelectionStyles = false;
|
396
398
|
$updateDOMForSelection(grid, null);
|
397
399
|
lexical.$setSelection(null);
|
398
400
|
this.editor.dispatchCommand(lexical.SELECTION_CHANGE_COMMAND);
|
@@ -409,6 +411,7 @@ class TableSelection {
|
|
409
411
|
}
|
410
412
|
|
411
413
|
tableElement.classList.remove('disable-selection');
|
414
|
+
this.hasHijackedSelectionStyles = false;
|
412
415
|
});
|
413
416
|
}
|
414
417
|
|
@@ -421,6 +424,7 @@ class TableSelection {
|
|
421
424
|
}
|
422
425
|
|
423
426
|
tableElement.classList.add('disable-selection');
|
427
|
+
this.hasHijackedSelectionStyles = true;
|
424
428
|
});
|
425
429
|
}
|
426
430
|
|
@@ -993,34 +997,19 @@ function applyTableHandlers(tableNode, tableElement, editor) {
|
|
993
997
|
const tableIndex = tableNode.getIndexWithinParent();
|
994
998
|
const parentKey = tableNode.getParentOrThrow().getKey();
|
995
999
|
isRangeSelectionHijacked = true;
|
996
|
-
tableSelection.disableHighlightStyle();
|
997
1000
|
(isBackward ? modifiedSelection.focus : modifiedSelection.anchor).set(startNode.getKey(), (isBackward ? selection.focus : selection.anchor).offset, lexical.$isTextNode(startNode) ? 'text' : 'element');
|
998
1001
|
(isBackward ? modifiedSelection.anchor : modifiedSelection.focus).set(parentKey, isBackward ? tableIndex - 1 : tableIndex + 1, 'element');
|
999
1002
|
lexical.$setSelection(modifiedSelection);
|
1000
|
-
$
|
1001
|
-
const elem = cell.elem;
|
1002
|
-
cell.highlighted = true;
|
1003
|
-
elem.style.setProperty('background-color', 'rgb(172, 206, 247)');
|
1004
|
-
elem.style.setProperty('caret-color', 'transparent');
|
1005
|
-
});
|
1003
|
+
$addHighlightStyleToTable(tableSelection);
|
1006
1004
|
return true;
|
1007
1005
|
}
|
1008
1006
|
}
|
1009
1007
|
|
1010
|
-
if (
|
1011
|
-
tableSelection
|
1012
|
-
$forEachGridCell(tableSelection.grid, cell => {
|
1013
|
-
const elem = cell.elem;
|
1014
|
-
cell.highlighted = false;
|
1015
|
-
elem.style.removeProperty('background-color');
|
1016
|
-
elem.style.removeProperty('caret-color');
|
1017
|
-
|
1018
|
-
if (!elem.getAttribute('style')) {
|
1019
|
-
elem.removeAttribute('style');
|
1020
|
-
}
|
1021
|
-
});
|
1008
|
+
if (tableSelection.hasHijackedSelectionStyles && !tableNode.isSelected()) {
|
1009
|
+
$removeHighlightStyleToTable(tableSelection);
|
1022
1010
|
isRangeSelectionHijacked = false;
|
1023
|
-
|
1011
|
+
} else if (!tableSelection.hasHijackedSelectionStyles && tableNode.isSelected()) {
|
1012
|
+
$addHighlightStyleToTable(tableSelection);
|
1024
1013
|
}
|
1025
1014
|
|
1026
1015
|
return false;
|
@@ -1149,7 +1138,6 @@ function $updateDOMForSelection(grid, selection) {
|
|
1149
1138
|
return highlightedCells;
|
1150
1139
|
}
|
1151
1140
|
function $forEachGridCell(grid, cb) {
|
1152
|
-
const highlightedCells = [];
|
1153
1141
|
const {
|
1154
1142
|
cells
|
1155
1143
|
} = grid;
|
@@ -1169,8 +1157,28 @@ function $forEachGridCell(grid, cb) {
|
|
1169
1157
|
}
|
1170
1158
|
}
|
1171
1159
|
}
|
1160
|
+
}
|
1161
|
+
function $addHighlightStyleToTable(tableSelection) {
|
1162
|
+
tableSelection.disableHighlightStyle();
|
1163
|
+
$forEachGridCell(tableSelection.grid, cell => {
|
1164
|
+
const elem = cell.elem;
|
1165
|
+
cell.highlighted = true;
|
1166
|
+
elem.style.setProperty('background-color', 'rgb(172, 206, 247)');
|
1167
|
+
elem.style.setProperty('caret-color', 'transparent');
|
1168
|
+
});
|
1169
|
+
}
|
1170
|
+
function $removeHighlightStyleToTable(tableSelection) {
|
1171
|
+
tableSelection.enableHighlightStyle();
|
1172
|
+
$forEachGridCell(tableSelection.grid, cell => {
|
1173
|
+
const elem = cell.elem;
|
1174
|
+
cell.highlighted = false;
|
1175
|
+
elem.style.removeProperty('background-color');
|
1176
|
+
elem.style.removeProperty('caret-color');
|
1172
1177
|
|
1173
|
-
|
1178
|
+
if (!elem.getAttribute('style')) {
|
1179
|
+
elem.removeAttribute('style');
|
1180
|
+
}
|
1181
|
+
});
|
1174
1182
|
}
|
1175
1183
|
|
1176
1184
|
const selectGridNodeInDirection = (tableSelection, tableNode, x, y, direction) => {
|
package/LexicalTable.prod.js
CHANGED
@@ -5,53 +5,53 @@
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
6
6
|
*/
|
7
7
|
var g=require("lexical"),p=require("@lexical/utils");const q={NO_STATUS:0,ROW:1,COLUMN:2,BOTH:3};
|
8
|
-
class u extends g.GridCellNode{static getType(){return"tablecell"}static clone(a){return new u(a.__headerState,a.__colSpan,a.__width,a.__key)}static importDOM(){return{td:()=>({conversion:
|
8
|
+
class u extends g.GridCellNode{static getType(){return"tablecell"}static clone(a){return new u(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
|
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)&&!g.$isElementNode(b)){d=g.$createParagraphNode();if(g.$isLineBreakNode(b)&&"\n"===b.getTextContent())return null;d.append(b);return d}return b}}}function x(a,b=1,d){return new u(a,b,d)}function y(a){return a instanceof u}
|
12
|
+
function A(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 B extends g.GridRowNode{static getType(){return"tablerow"}static clone(a){return new B(a.__height,a.__key)}static importDOM(){return{tr:()=>({conversion:C,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 C(){return{node:D()}}function D(a){return new B(a)}function E(a){return a instanceof B}
|
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
|
-
{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
|
-
if(!
|
20
|
-
{const a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a.classList.remove("disable-selection")})}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")})}adjustFocusCellForSelection(a,b=!1){this.editor.update(()=>{var d=g.$getNodeByKey(this.tableNodeKey);
|
21
|
-
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
|
-
null!=this.gridSelection&&null!=this.anchorCellNodeKey&&
|
23
|
-
0,a.elem,0);var b=g.$getNearestNodeFromDOMNode(a.elem);
|
24
|
-
{const a=g.$getNodeByKey(this.tableNodeKey);if(!
|
25
|
-
function
|
26
|
-
function
|
27
|
-
function
|
28
|
-
function M(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=g.$getNearestNodeFromDOMNode(k.elem);null!==n&&b(k,n,{x:c,y:d})}}
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
b
|
35
|
-
|
36
|
-
|
37
|
-
exports.$
|
38
|
-
exports.$
|
39
|
-
exports.$
|
40
|
-
exports.$
|
41
|
-
|
42
|
-
|
43
|
-
exports
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
if(!
|
48
|
-
if((
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
c.grid);e.preventDefault()
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
exports.getCellFromTarget=
|
17
|
+
class F{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=G(d)}})});this.editor.update(()=>{const b=this.editor.getElementByKey(this.tableNodeKey);if(!b)throw Error("Expected to find TableElement in DOM");this.grid=G(b);a.observe(b,{childList:!0,subtree:!0})})}clearHighlight(){this.editor.update(()=>{var a=
|
19
|
+
g.$getNodeByKey(this.tableNodeKey);if(!H(a))throw Error("Expected TableNode.");a=this.editor.getElementByKey(this.tableNodeKey);if(!a)throw Error("Expected to find TableElement in DOM");a=G(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;I(a,null);g.$setSelection(null);this.editor.dispatchCommand(g.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=g.$getNodeByKey(this.tableNodeKey);
|
21
|
+
if(!H(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=g.$getNearestNodeFromDOMNode(a.elem),null!=this.gridSelection&&null!=this.anchorCellNodeKey&&y(d)&&(d=d.getKey(),this.gridSelection=g.$createGridSelection(),this.focusCellNodeKey=d,this.gridSelection.set(this.tableNodeKey,this.anchorCellNodeKey,this.focusCellNodeKey),g.$setSelection(this.gridSelection),this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND),I(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=g.$getNearestNodeFromDOMNode(a.elem);y(b)&&(b=b.getKey(),this.gridSelection=g.$createGridSelection(),this.anchorCellNodeKey=b)})}formatCells(a){this.editor.update(()=>{const b=g.$getSelection();g.$isGridSelection(b)||A(79);const d=g.$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))});g.$setSelection(b);
|
24
|
+
this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND)})}clearText(){this.editor.update(()=>{const a=g.$getNodeByKey(this.tableNodeKey);if(!H(a))throw Error("Expected TableNode.");var b=g.$getSelection();g.$isGridSelection(b)||A(79);b=b.getNodes().filter(y);b.length===this.grid.columns*this.grid.rows?(a.selectPrevious(),a.remove(),this.clearHighlight()):(b.forEach(d=>{if(g.$isElementNode(d)){const h=g.$createParagraphNode(),c=g.$createTextNode();h.append(c);d.append(h);d.getChildren().forEach(k=>
|
25
|
+
{k!==h&&k.remove()})}}),I(this.grid,null),g.$setSelection(null),this.editor.dispatchCommand(g.SELECTION_CHANGE_COMMAND))})}}function J(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 G(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 I(a,b){const d=[],h=new Set(b?b.getNodes():[]);M(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 M(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=g.$getNearestNodeFromDOMNode(k.elem);null!==n&&b(k,n,{x:c,y:d})}}}function N(a){a.disableHighlightStyle();M(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();M(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
|
+
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
|
+
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=>g.$isParagraphNode(d));g.$isParagraphNode(b)?b.selectEnd():a.selectEnd()}
|
33
|
+
class S extends g.GridNode{static getType(){return"table"}static clone(a){return new S(a.__key)}static importDOM(){return{table:()=>({conversion:T,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(!E(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||A(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})=>g.$getNearestNodeFromDOMNode(k)===a);if(-1!==c)return{x:c,y:b}}throw Error("Cell not found in table.");}getCellFromCords(a,
|
35
|
+
b,d){d||A(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=g.$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 T(){return{node:U()}}function U(){return new S}function H(a){return a instanceof S}function V(a){a=p.$findMatchingParent(a,b=>E(b));if(E(a))return a;throw Error("Expected table cell to be inside of table row.");}function W(a){a=p.$findMatchingParent(a,b=>H(b));if(H(a))return a;throw Error("Expected table cell to be inside of table.");}const X=g.createCommand();exports.$createTableCellNode=x;exports.$createTableNode=U;
|
37
|
+
exports.$createTableNodeWithDimensions=function(a,b,d=!0){const h=U();for(let k=0;k<a;k++){const n=D();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=g.$createParagraphNode();v.append(g.$createTextNode());c.append(v);n.append(c)}h.append(n)}return h};exports.$createTableRowNode=D;
|
38
|
+
exports.$deleteTableColumn=function(a,b){const d=a.getChildren();for(let c=0;c<d.length;c++){var h=d[c];if(E(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 G(a)};exports.$getTableCellNodeFromLexicalNode=function(a){a=p.$findMatchingParent(a,b=>y(b));return y(a)?a:null};
|
39
|
+
exports.$getTableColumnIndexFromTableCellNode=function(a){return V(a).getChildren().findIndex(b=>b.is(a))};exports.$getTableNodeFromLexicalNodeOrThrow=W;exports.$getTableRowIndexFromTableCellNode=function(a){const b=V(a);return W(b).getChildren().findIndex(d=>d.is(b))};exports.$getTableRowNodeFromTableCellNodeOrThrow=V;
|
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(E(v))for(let e=0;e<h;e++){var k=q.NO_STATUS;0===r&&(k|=q.ROW);k=x(k);k.append(g.$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(E(b))for(k=0;k<h;k++){const v=b.getChildren(),e=v.length,f=D();for(let l=0;l<e;l++){var n=v[l];y(n)||A(73);var r=c;const m=W(n),{x:t,y:z}=m.getCordsFromCellNode(n,r);n={above:m.getCellNodeFromCords(t,z-1,r),below:m.getCellNodeFromCords(t,z+1,r),left:m.getCellNodeFromCords(t-1,z,r),right:m.getCellNodeFromCords(t+1,z,r)};const {above:K,below:L}=n;n=q.NO_STATUS;
|
42
|
+
r=K&&K.getWidth()||L&&L.getWidth()||null;if(K&&K.hasHeaderState(q.COLUMN)||L&&L.hasHeaderState(q.COLUMN))n|=q.COLUMN;n=x(n,1,r);n.append(g.$createParagraphNode());f.append(n)}d?b.insertAfter(f):b.insertBefore(f)}else throw Error("Row before insertion index does not exist.");return a};exports.$isTableCellNode=y;exports.$isTableNode=H;exports.$isTableRowNode=E;
|
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=X;exports.TableCellHeaderStates=q;exports.TableCellNode=u;exports.TableNode=S;exports.TableRowNode=B;exports.TableSelection=F;
|
44
|
+
exports.applyTableHandlers=function(a,b,d){const h=d.getRootElement();if(null===h)throw Error("No root element.");const c=new F(d,a.getKey());b.__lexicalTableSelection=c;let k=!1,n=!1;b.addEventListener("dblclick",e=>{const f=J(e.target);null!==f&&(e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),c.setAnchorCellForSelection(f),c.adjustFocusCellForSelection(f,!0),k=!1)});b.addEventListener("mousedown",e=>{setTimeout(()=>{if(0===e.button){var f=J(e.target);null!==f&&(c.setAnchorCellForSelection(f),
|
45
|
+
document.addEventListener("mouseup",()=>{k=!1},{capture:!0,once:!0}))}},0)});b.addEventListener("mousemove",e=>{n&&(e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation());if(k){const f=J(e.target);if(null!==f){const l=f.x,m=f.y;k&&(c.startX!==l||c.startY!==m||c.isHighlightingCells)&&(e.preventDefault(),k=!0,c.adjustFocusCellForSelection(f))}}});b.addEventListener("mouseup",()=>{k&&(k=!1)});b.addEventListener("mouseleave",()=>{});const r=e=>{k=!0;0===e.button&&d.update(()=>{const f=g.$getSelection();
|
46
|
+
if(g.$isGridSelection(f)&&f.gridKey===c.tableNodeKey&&h.contains(e.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(g.KEY_ARROW_DOWN_COMMAND,e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){if(f.isCollapsed()){var l=p.$findMatchingParent(f.anchor.getNode(),
|
47
|
+
t=>y(t));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);f=p.$findMatchingParent(f.anchor.getNode(),t=>g.$isElementNode(t));if(null==f)throw Error("Expected BlockNode Parent");if((l=l.getLastChild())&&f.isParentOf(l)||f===l||e.shiftKey)return e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),e.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(g.$isGridSelection(f)&&e.shiftKey){m=f.focus.getNode();
|
48
|
+
if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);e.preventDefault();e.stopImmediatePropagation();e.stopPropagation();return R(c,a,m.x,m.y,"down")}return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_ARROW_UP_COMMAND,e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){if(f.isCollapsed()){var l=p.$findMatchingParent(f.anchor.getNode(),t=>y(t));if(!y(l))return!1;var m=a.getCordsFromCellNode(l,c.grid);f=p.$findMatchingParent(f.anchor.getNode(),t=>g.$isElementNode(t));if(null==f)throw Error("Expected BlockNode Parent");
|
49
|
+
if((l=l.getLastChild())&&f.isParentOf(l)||f===l||e.shiftKey)return e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),e.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(g.$isGridSelection(f)&&e.shiftKey){m=f.focus.getNode();if(!y(m))return!1;m=a.getCordsFromCellNode(m,c.grid);e.preventDefault();e.stopImmediatePropagation();e.stopPropagation();return R(c,a,m.x,m.y,"up")}return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_ARROW_LEFT_COMMAND,
|
50
|
+
e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){if(f.isCollapsed()){var l=p.$findMatchingParent(f.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,c.grid);if(null==p.$findMatchingParent(f.anchor.getNode(),m=>g.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(0===f.anchor.offset||e.shiftKey)return e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),e.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,l.y,c.grid)),R(c,a,l.x,
|
51
|
+
l.y,"backward")):Q(c,a,l.x,l.y,"backward")}}else if(g.$isGridSelection(f)&&e.shiftKey){f=f.focus.getNode();if(!y(f))return!1;f=a.getCordsFromCellNode(f,c.grid);e.preventDefault();e.stopImmediatePropagation();e.stopPropagation();return R(c,a,f.x,f.y,"backward")}return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_ARROW_RIGHT_COMMAND,e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){if(f.isCollapsed()){var l=p.$findMatchingParent(f.anchor.getNode(),m=>y(m));if(!y(l))return!1;l=a.getCordsFromCellNode(l,
|
52
|
+
c.grid);if(null==p.$findMatchingParent(f.anchor.getNode(),m=>g.$isElementNode(m)))throw Error("Expected BlockNode Parent");if(f.anchor.offset===f.anchor.getNode().getTextContentSize()||e.shiftKey)return e.preventDefault(),e.stopImmediatePropagation(),e.stopPropagation(),e.shiftKey?(c.setAnchorCellForSelection(a.getCellFromCordsOrThrow(l.x,l.y,c.grid)),R(c,a,l.x,l.y,"forward")):Q(c,a,l.x,l.y,"forward")}}else if(g.$isGridSelection(f)&&e.shiftKey){f=f.focus.getNode();if(!y(f))return!1;f=a.getCordsFromCellNode(f,
|
53
|
+
c.grid);e.preventDefault();e.stopImmediatePropagation();e.stopPropagation();return R(c,a,f.x,f.y,"forward")}return!1},4));c.listenersToRemove.add(d.registerCommand(g.DELETE_CHARACTER_COMMAND,()=>{const e=g.$getSelection();if(g.$isGridSelection(e))return c.clearText(),!0;if(g.$isRangeSelection(e)){const f=p.$findMatchingParent(e.anchor.getNode(),l=>y(l));if(!y(f))return!1;if(e.isCollapsed()&&0===e.anchor.offset&&0===e.anchor.getNode().getPreviousSiblings().length)return!0}return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_BACKSPACE_COMMAND,
|
54
|
+
e=>{const f=g.$getSelection();if(g.$isGridSelection(f))return e.preventDefault(),e.stopPropagation(),c.clearText(),!0;g.$isRangeSelection(f)&&(e=p.$findMatchingParent(f.anchor.getNode(),l=>y(l)),y(e));return!1},4));c.listenersToRemove.add(d.registerCommand(g.FORMAT_TEXT_COMMAND,e=>{const f=g.$getSelection();if(g.$isGridSelection(f))return c.formatCells(e),!0;g.$isRangeSelection(f)&&(e=p.$findMatchingParent(f.anchor.getNode(),l=>y(l)),y(e));return!1},4));c.listenersToRemove.add(d.registerCommand(g.INSERT_TEXT_COMMAND,
|
55
|
+
()=>{var e=g.$getSelection();g.$isGridSelection(e)?c.clearHighlight():g.$isRangeSelection(e)&&(e=p.$findMatchingParent(e.anchor.getNode(),f=>y(f)),y(e));return!1},4));c.listenersToRemove.add(d.registerCommand(g.KEY_TAB_COMMAND,e=>{var f=g.$getSelection();if(g.$isRangeSelection(f)){const l=p.$findMatchingParent(f.anchor.getNode(),m=>y(m));if(!y(l))return!1;if(f.isCollapsed())return f=a.getCordsFromCellNode(l,c.grid),e.preventDefault(),Q(c,a,f.x,f.y,e.shiftKey?"backward":"forward"),!0}return!1},4));
|
56
|
+
c.listenersToRemove.add(d.registerCommand(g.SELECTION_CHANGE_COMMAND,()=>{const e=g.$getSelection();if(e&&g.$isRangeSelection(e)&&!e.isCollapsed()){var f=e.anchor.getNode(),l=e.focus.getNode(),m=a.isParentOf(f),t=a.isParentOf(l);if(m&&!t||t&&!m){f=(m=e.isBackward())?l:f;l=g.$createRangeSelection();t=a.getIndexWithinParent();const z=a.getParentOrThrow().getKey();n=!0;(m?l.focus:l.anchor).set(f.getKey(),(m?e.focus:e.anchor).offset,g.$isTextNode(f)?"text":"element");(m?l.anchor:l.focus).set(z,m?t-1:
|
57
|
+
t+1,"element");g.$setSelection(l);N(c);return!0}}c.hasHijackedSelectionStyles&&!a.isSelected()?(O(c),n=!1):!c.hasHijackedSelectionStyles&&a.isSelected()&&N(c);return!1},4));return c};exports.getCellFromTarget=J;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.1",
|
12
12
|
"main": "LexicalTable.js",
|
13
13
|
"peerDependencies": {
|
14
|
-
"lexical": "0.2.
|
14
|
+
"lexical": "0.2.1"
|
15
15
|
},
|
16
16
|
"dependencies": {
|
17
|
-
"@lexical/utils": "0.2.
|
17
|
+
"@lexical/utils": "0.2.1"
|
18
18
|
},
|
19
19
|
"repository": {
|
20
20
|
"type": "git",
|