@trafica/editor 1.0.21 → 1.0.22

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/dist/index.mjs CHANGED
@@ -8627,6 +8627,53 @@ function prettyPrintHTML(html) {
8627
8627
  }
8628
8628
 
8629
8629
  // src/editor/table/TablePlugin.ts
8630
+ function getCellDOMElement(cellPathJson) {
8631
+ return document.querySelector(`[data-block-path='${cellPathJson}']`);
8632
+ }
8633
+ function isCaretAtContainerStart(container) {
8634
+ const sel = window.getSelection();
8635
+ if (!sel || sel.rangeCount === 0 || !sel.isCollapsed) return false;
8636
+ const range = sel.getRangeAt(0);
8637
+ if (range.startOffset !== 0) return false;
8638
+ let node = range.startContainer;
8639
+ while (node && node !== container) {
8640
+ if (node.previousSibling) return false;
8641
+ node = node.parentNode;
8642
+ }
8643
+ return node === container;
8644
+ }
8645
+ function isCaretAtContainerEnd(container) {
8646
+ var _a, _b;
8647
+ const sel = window.getSelection();
8648
+ if (!sel || sel.rangeCount === 0 || !sel.isCollapsed) return false;
8649
+ const range = sel.getRangeAt(0);
8650
+ const endNode = range.endContainer;
8651
+ const endOffset = range.endOffset;
8652
+ const len = endNode.nodeType === Node.TEXT_NODE ? (_b = (_a = endNode.textContent) == null ? void 0 : _a.length) != null ? _b : 0 : endNode.childNodes.length;
8653
+ if (endOffset !== len) return false;
8654
+ let node = endNode;
8655
+ while (node && node !== container) {
8656
+ if (node.nextSibling) return false;
8657
+ node = node.parentNode;
8658
+ }
8659
+ return node === container;
8660
+ }
8661
+ function isCaretOnEdgeLine(container, direction) {
8662
+ const sel = window.getSelection();
8663
+ if (!sel || sel.rangeCount === 0 || !sel.isCollapsed) return false;
8664
+ const caretRange = sel.getRangeAt(0).cloneRange();
8665
+ caretRange.collapse(true);
8666
+ const caretRects = caretRange.getClientRects();
8667
+ if (!caretRects.length) return true;
8668
+ const caretRect = caretRects[0];
8669
+ const containerRect = container.getBoundingClientRect();
8670
+ const lineHeight = parseFloat(getComputedStyle(container).lineHeight) || 20;
8671
+ if (direction === "up") {
8672
+ return caretRect.top < containerRect.top + lineHeight;
8673
+ } else {
8674
+ return caretRect.bottom > containerRect.bottom - lineHeight;
8675
+ }
8676
+ }
8630
8677
  var TablePlugin = {
8631
8678
  name: "table",
8632
8679
  keyBindings: {
@@ -8677,6 +8724,125 @@ var TablePlugin = {
8677
8724
  engine.dispatch(tr);
8678
8725
  return true;
8679
8726
  },
8727
+ "ArrowRight": (engine) => {
8728
+ var _a;
8729
+ const state = engine.getState();
8730
+ const sel = state.selection;
8731
+ if (!sel) return false;
8732
+ const cellPos = findCellPosition(state.doc, sel.anchor.path);
8733
+ if (!cellPos) return false;
8734
+ const { tablePath, row, col } = cellPos;
8735
+ const cellPath = [...tablePath, row, col];
8736
+ const cellEl = getCellDOMElement(JSON.stringify(cellPath));
8737
+ if (!cellEl || !isCaretAtContainerEnd(cellEl)) return false;
8738
+ const table = getNodeAtPath(state.doc, tablePath);
8739
+ const { rows, cols } = getTableDimensions(table);
8740
+ let nextRow = row;
8741
+ let nextCol = col + 1;
8742
+ while (nextRow < rows) {
8743
+ if (nextCol >= cols) {
8744
+ nextCol = 0;
8745
+ nextRow++;
8746
+ continue;
8747
+ }
8748
+ const c = getNodeAtPath(state.doc, [...tablePath, nextRow, nextCol]);
8749
+ if (!((_a = c == null ? void 0 : c.attrs) == null ? void 0 : _a.covered)) break;
8750
+ nextCol++;
8751
+ }
8752
+ if (nextRow >= rows) return true;
8753
+ const pos = getCellFirstPosition(state.doc, tablePath, nextRow, nextCol);
8754
+ if (!pos) return false;
8755
+ const tr = createTransaction();
8756
+ tr.steps.push(tr_setSelection(makeCollapsedSelection(pos)));
8757
+ engine.dispatch(tr);
8758
+ return true;
8759
+ },
8760
+ "ArrowLeft": (engine) => {
8761
+ var _a;
8762
+ const state = engine.getState();
8763
+ const sel = state.selection;
8764
+ if (!sel) return false;
8765
+ const cellPos = findCellPosition(state.doc, sel.anchor.path);
8766
+ if (!cellPos) return false;
8767
+ const { tablePath, row, col } = cellPos;
8768
+ const cellPath = [...tablePath, row, col];
8769
+ const cellEl = getCellDOMElement(JSON.stringify(cellPath));
8770
+ if (!cellEl || !isCaretAtContainerStart(cellEl)) return false;
8771
+ const table = getNodeAtPath(state.doc, tablePath);
8772
+ const { cols } = getTableDimensions(table);
8773
+ let prevRow = row;
8774
+ let prevCol = col - 1;
8775
+ while (prevRow >= 0) {
8776
+ if (prevCol < 0) {
8777
+ prevCol = cols - 1;
8778
+ prevRow--;
8779
+ continue;
8780
+ }
8781
+ const c = getNodeAtPath(state.doc, [...tablePath, prevRow, prevCol]);
8782
+ if (!((_a = c == null ? void 0 : c.attrs) == null ? void 0 : _a.covered)) break;
8783
+ prevCol--;
8784
+ }
8785
+ if (prevRow < 0) return true;
8786
+ const pos = getCellLastPosition(state.doc, tablePath, prevRow, prevCol);
8787
+ if (!pos) return false;
8788
+ const tr = createTransaction();
8789
+ tr.steps.push(tr_setSelection(makeCollapsedSelection(pos)));
8790
+ engine.dispatch(tr);
8791
+ return true;
8792
+ },
8793
+ "ArrowDown": (engine) => {
8794
+ var _a;
8795
+ const state = engine.getState();
8796
+ const sel = state.selection;
8797
+ if (!sel) return false;
8798
+ const cellPos = findCellPosition(state.doc, sel.anchor.path);
8799
+ if (!cellPos) return false;
8800
+ const { tablePath, row, col } = cellPos;
8801
+ const cellPath = [...tablePath, row, col];
8802
+ const cellEl = getCellDOMElement(JSON.stringify(cellPath));
8803
+ if (!cellEl || !isCaretOnEdgeLine(cellEl, "down")) return false;
8804
+ const table = getNodeAtPath(state.doc, tablePath);
8805
+ const { rows, cols } = getTableDimensions(table);
8806
+ let nextRow = row + 1;
8807
+ while (nextRow < rows) {
8808
+ const c = getNodeAtPath(state.doc, [...tablePath, nextRow, col]);
8809
+ if (c && !((_a = c.attrs) == null ? void 0 : _a.covered)) break;
8810
+ nextRow++;
8811
+ }
8812
+ if (nextRow >= rows) return false;
8813
+ const pos = getCellFirstPosition(state.doc, tablePath, nextRow, col < cols ? col : 0);
8814
+ if (!pos) return false;
8815
+ const tr = createTransaction();
8816
+ tr.steps.push(tr_setSelection(makeCollapsedSelection(pos)));
8817
+ engine.dispatch(tr);
8818
+ return true;
8819
+ },
8820
+ "ArrowUp": (engine) => {
8821
+ var _a;
8822
+ const state = engine.getState();
8823
+ const sel = state.selection;
8824
+ if (!sel) return false;
8825
+ const cellPos = findCellPosition(state.doc, sel.anchor.path);
8826
+ if (!cellPos) return false;
8827
+ const { tablePath, row, col } = cellPos;
8828
+ const cellPath = [...tablePath, row, col];
8829
+ const cellEl = getCellDOMElement(JSON.stringify(cellPath));
8830
+ if (!cellEl || !isCaretOnEdgeLine(cellEl, "up")) return false;
8831
+ let prevRow = row - 1;
8832
+ while (prevRow >= 0) {
8833
+ const c = getNodeAtPath(state.doc, [...tablePath, prevRow, col]);
8834
+ if (c && !((_a = c.attrs) == null ? void 0 : _a.covered)) break;
8835
+ prevRow--;
8836
+ }
8837
+ if (prevRow < 0) return false;
8838
+ const { cols } = getTableDimensions(getNodeAtPath(state.doc, tablePath));
8839
+ const pos = getCellLastPosition(state.doc, tablePath, prevRow, col < cols ? col : 0);
8840
+ if (!pos) return false;
8841
+ const tr = createTransaction();
8842
+ tr.steps.push(tr_setSelection(makeCollapsedSelection(pos)));
8843
+ engine.dispatch(tr);
8844
+ return true;
8845
+ },
8680
8846
  "Shift+Tab": (engine) => {
8681
8847
  var _a;
8682
8848
  const state = engine.getState();