devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
110 lines (109 loc) • 5.73 kB
JavaScript
import { TableCellUtils } from '../../model/tables/table-utils';
import { SearchUtils } from '@devexpress/utils/lib/utils/search';
import { CommandBase } from '../command-base';
import { SimpleCommandState } from '../command-states';
import { TableNavDirection } from '../../selection/table-nav-direction';
export { TableNavDirection };
export class SelectionCommandBase extends CommandBase {
getState() {
return new SimpleCommandState(this.isEnabled());
}
isEnabledInReadOnlyMode() {
return true;
}
shouldCollapseSingleCellTableSelection(entryDirection, shrinkDirection) {
const selection = this.selection;
const hasSingleCellAnchor = selection.tableSelectionAnchorCellPosition >= 0 &&
selection.tableSelectionAnchorCellPosition === selection.tableSelectionActiveCellPosition;
const collapseByEntryDirection = selection.tableSingleCellEntryDirection === entryDirection;
const collapseByShrinkDirection = selection.tableSingleCellShrinkDirection === shrinkDirection;
return selection.tableInfo.isSelected && hasSingleCellAnchor && (collapseByEntryDirection || collapseByShrinkDirection);
}
collapseSingleCellTableSelectionToCaret() {
const selection = this.selection;
if (selection.tableTextSelectionAnchorPosition >= 0 && selection.tableTextSelectionActivePosition >= 0) {
selection.deprecatedSetSelection(selection.tableTextSelectionAnchorPosition, selection.tableTextSelectionActivePosition, selection.tableTextSelectionEndOfLine, -1, true);
return true;
}
if (selection.tableSingleCellEntryDirection !== TableNavDirection.None && selection.prevState.tableSelection === null) {
const prev = selection.prevState;
selection.deprecatedSetSelection(prev.anchorPosition, prev.activePosition, prev.endOfLine, -1, true);
return true;
}
const activeCell = selection.getTableSelectionActiveCell();
const pos = activeCell ? activeCell.startParagraphPosition.value : 0;
selection.deprecatedSetSelection(pos, pos, false, -1, true);
return true;
}
collapseTableCellSelectionSpatialFocus(direction) {
const selection = this.selection;
if (selection.tableSelectionAnchorCellPosition < 0 || selection.tableSelectionActiveCellPosition < 0)
return false;
const anchorCell = selection.getTableSelectionAnchorCell();
const activeCell = selection.getTableSelectionActiveCell();
if (!anchorCell || !activeCell)
return false;
const table = anchorCell.parentRow.parentTable;
if (table !== activeCell.parentRow.parentTable)
return false;
const anchorColStart = TableCellUtils.getStartColumnIndex(anchorCell);
const anchorColEnd = anchorColStart + anchorCell.columnSpan - 1;
const activeColStart = TableCellUtils.getStartColumnIndex(activeCell);
const activeColEnd = activeColStart + activeCell.columnSpan - 1;
const minCol = Math.min(anchorColStart, activeColStart);
const maxCol = Math.max(anchorColEnd, activeColEnd);
const anchorRowIndex = SearchUtils.normedInterpolationIndexOf(table.rows, r => r.getStartPosition(), anchorCell.startParagraphPosition.value);
const activeRowIndex = SearchUtils.normedInterpolationIndexOf(table.rows, r => r.getStartPosition(), activeCell.startParagraphPosition.value);
const minRowIndex = Math.min(anchorRowIndex, activeRowIndex);
const maxRowIndex = Math.max(anchorRowIndex, activeRowIndex);
let targetCell;
let placeAtEnd;
switch (direction) {
case TableNavDirection.Left: {
const row = activeCell.parentRow;
const idx = TableCellUtils.getCellIndexByColumnIndex(row, minCol);
targetCell = idx >= 0 ? row.cells[idx] : row.cells[0];
placeAtEnd = false;
break;
}
case TableNavDirection.Right: {
const row = activeCell.parentRow;
const idx = TableCellUtils.getCellIndexByColumnIndex(row, maxCol);
targetCell = idx >= 0 ? row.cells[idx] : row.cells[row.cells.length - 1];
placeAtEnd = true;
break;
}
case TableNavDirection.Up: {
const row = table.rows[minRowIndex];
const idx = TableCellUtils.getCellIndexByColumnIndex(row, activeColStart);
targetCell = idx >= 0 ? row.cells[idx] : row.cells[0];
placeAtEnd = false;
break;
}
case TableNavDirection.Down: {
const row = table.rows[maxRowIndex];
const idx = TableCellUtils.getCellIndexByColumnIndex(row, activeColStart);
targetCell = idx >= 0 ? row.cells[idx] : row.cells[row.cells.length - 1];
placeAtEnd = true;
break;
}
default:
return false;
}
if (!targetCell)
return false;
const pos = placeAtEnd
? targetCell.endParagraphPosition.value - 1
: targetCell.startParagraphPosition.value;
selection.deprecatedSetSelection(pos, pos, false, -1, true);
return true;
}
beforeExecute() {
super.beforeExecute();
this.control.screenReaderManager.beginExecute(this.commandId);
}
afterExecute() {
super.afterExecute();
this.control.screenReaderManager.endExecute();
}
}