devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
55 lines (54 loc) • 3.06 kB
JavaScript
import { ControlOptions } from '../../model/options/control';
import { TableCellMergingState } from '../../model/tables/secondary-structures/table-base-structures';
import { TableConditionalFormattingCalculator } from '../../model/tables/table-utils';
import { FixedInterval } from '@devexpress/utils/lib/intervals/fixed';
import { SelectionHistoryItem } from '../../model/history/selection/selection-history-item';
import { CommandBase } from '../command-base';
import { SimpleCommandState } from '../command-states';
export class InsertTableCellWithShiftToTheHorizontallyCommandBase extends CommandBase {
getState() {
return new SimpleCommandState(this.isEnabled());
}
isEnabled() {
const tableInfo = this.selection.tableInfo;
return super.isEnabled() && ControlOptions.isEnabled(this.control.modelManager.richOptions.control.tables) &&
tableInfo.extendedData.numRows > 0 && this.canModifySelectedRows(this.selection.activeSubDocument, tableInfo);
}
executeCore(_state, options) {
this.history.beginTransaction();
const tblInfo = this.selection.tableInfo;
const table = tblInfo.table;
const subDocument = options.subDocument;
const newCells = [];
tblInfo.extendedData.foreach(() => { }, (cellInfo, rowInfo) => {
if (cellInfo.cell.verticalMerging !== TableCellMergingState.Continue)
newCells.push(this.insertTableCell(subDocument, table, rowInfo.rowIndex, cellInfo.cellIndex));
});
this.modelManipulator.table.normalizeVerticalSpans(subDocument, table);
const newCellIntervals = [];
for (let i = 0, newCell; newCell = newCells[i]; i++)
newCellIntervals.push(newCell.interval);
TableConditionalFormattingCalculator.updateTable(this.control.modelManager, table, subDocument);
this.history.addAndRedo(new SelectionHistoryItem(this.modelManipulator, this.selection, this.selection.getState(), this.selection.getState().setIntervals(newCellIntervals).setEndOfLine(false)));
this.history.endTransaction();
return true;
}
canModifySelectedRows(subDocument, tableInfo) {
const table = tableInfo.table;
const rawRows = tableInfo.rawData.rows;
if (!table || !rawRows || rawRows.length === 0)
return false;
const intervals = [];
for (let i = 0; i < table.rows.length; i++) {
const row = table.rows[i];
intervals.push(FixedInterval.fromPositions(row.getStartPosition(), row.getEndPosition()));
}
return subDocument.isEditable(intervals);
}
}
export class InsertTableCellWithShiftToTheLeftCommand extends InsertTableCellWithShiftToTheHorizontallyCommandBase {
insertTableCell(subDocument, table, rowIndex, cellIndex) {
this.modelManipulator.table.insertCellToTheLeft(subDocument, table, rowIndex, cellIndex, this.inputPosition);
return table.rows[rowIndex].cells[cellIndex];
}
}