add selection all cells in row or col

This commit is contained in:
Sergey Krylov 2022-07-11 17:52:51 +05:00
parent c92ae3a38d
commit 2b053a9d47
5 changed files with 48 additions and 12 deletions

View File

@ -28,7 +28,7 @@ export class Table extends ExcelComponent {
} }
prepare() { prepare() {
this.selection = new TableSelection(); this.selection = new TableSelection(this.$root);
} }
init() { init() {

View File

@ -5,9 +5,11 @@ export class TableSelection {
static selectedClassName = 'selected'; static selectedClassName = 'selected';
private group: Dom[]; private group: Dom[];
public current: Dom; public current: Dom;
public rootTable: Dom;
constructor() { constructor(rootTable: Dom) {
this.group = []; this.group = [];
this.rootTable = rootTable;
} }
get selectedIds() { get selectedIds() {
@ -40,7 +42,7 @@ export class TableSelection {
this.group = []; this.group = [];
} }
selectGroup($el: Dom) { selectTo($el: Dom) {
const startCellParams = getParamsFromCellId(this.current.data.id || startCellId); const startCellParams = getParamsFromCellId(this.current.data.id || startCellId);
const selectedCellParams = getParamsFromCellId($el.data.id || startCellId); const selectedCellParams = getParamsFromCellId($el.data.id || startCellId);
@ -53,14 +55,22 @@ export class TableSelection {
for (let row = startRow; row <= endRow; row++) { for (let row = startRow; row <= endRow; row++) {
for (let col = startCol; col <= endCol; col++) { for (let col = startCol; col <= endCol; col++) {
const cell = $(`[data-id="${row}:${col}"]`); const $cell = $(`[data-id="${row}:${col}"]`);
this.addCellToSelection($cell);
this.group.push(cell);
cell.addClass(TableSelection.selectedClassName);
} }
} }
} }
selectGroupies($cells: Dom[]) {
this.clearSelection();
$cells.forEach($cell => this.addCellToSelection($cell));
}
addCellToSelection($cell: Dom) {
this.group.push($cell);
$cell.addClass(TableSelection.selectedClassName);
}
applyStyle(style: CSSStyleRule) { applyStyle(style: CSSStyleRule) {
this.group.forEach(el => el.css(style)); this.group.forEach(el => el.css(style));
} }

View File

@ -19,9 +19,29 @@ export function selectHandler(event: MouseEvent | KeyboardEvent, selection: Tabl
callback?.(); callback?.();
function onMouseDownHandler() { function onMouseDownHandler() {
const target = $(event.target as HTMLElement);
if (isCell(event)) { if (isCell(event)) {
if (event.shiftKey) selection.selectGroup($(event.target as HTMLElement)); if (event.shiftKey) selection.selectTo(target);
else selection.select($(event.target as HTMLElement)); else if (event.ctrlKey) selection.addCellToSelection(target);
else selection.select(target);
} else {
const row = target.closest('[data-header="row"]');
const col = target.closest('[data-header="col"]');
const resizer = target.closest('[data-resizer]');
if (row.$el && !resizer.$el) {
const cells = row.closest('[data-row]').findAll('[data-type="cell"]');
const $cells = Array.from(cells).map(cell => $(cell as HTMLElement));
selection.selectGroupies($cells);
} else if (col.$el && !resizer.$el) {
const colNumber = col.data.col;
const colls = selection.rootTable.findAll(`[data-col="${colNumber}"]`);
const $cells = Array.from(colls).filter(el => el !== col.$el).map(el => $(el as HTMLElement));
selection.selectGroupies($cells);
}
} }
} }

View File

@ -20,7 +20,7 @@ function createCell(cellContent = '', colIndex = 1, rowIndex = 1) {
function createCol(columnContent = '', index = 1) { function createCol(columnContent = '', index = 1) {
return ` return `
<div class="column" data-type="resizable" data-col="${index}"> <div class="column" data-type="resizable" data-col="${index}" data-header="col">
${columnContent} ${columnContent}
<div class="col-resize" data-resize="col"></div> <div class="col-resize" data-resize="col"></div>
</div> </div>
@ -30,7 +30,7 @@ function createCol(columnContent = '', index = 1) {
function createRow(dataContent = '', infoContent = '', needResize = true, rowIndex = -1) { function createRow(dataContent = '', infoContent = '', needResize = true, rowIndex = -1) {
return ` return `
<div class="row" data-type="resizable" data-row="${rowIndex}"> <div class="row" data-type="resizable" data-row="${rowIndex}">
<div class="row-info"> <div class="row-info" data-header="row">
${infoContent} ${infoContent}
${needResize ? '<div class="row-resize" data-resize="row"></div>' : ''} ${needResize ? '<div class="row-resize" data-resize="row"></div>' : ''}
</div> </div>

View File

@ -8,6 +8,7 @@
top: $header-height + $toolbar-height + $formula-height; top: $header-height + $toolbar-height + $formula-height;
overflow: auto; overflow: auto;
font-size: $default-cell-font-size; font-size: $default-cell-font-size;
cursor: url('https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Small_arrow_pointing_left.svg/1200px-Small_arrow_pointing_left.svg.png') !important;
.row{ .row{
display: flex; display: flex;
flex-direction: row; flex-direction: row;
@ -75,7 +76,6 @@
opacity: 1 !important; opacity: 1 !important;
} }
} }
.row-resize{ .row-resize{
position: absolute; position: absolute;
bottom: 0; bottom: 0;
@ -91,4 +91,10 @@
opacity: 1 !important; opacity: 1 !important;
} }
} }
[data-header="row"] {
cursor: e-resize;
}
[data-header="col"] {
cursor: n-resize;
}
} }