Added delete for range by press Del button

This commit is contained in:
Eugene 2023-07-21 19:10:17 +03:00
parent 29b95c6b73
commit d80cb707fe
2 changed files with 84 additions and 27 deletions

View File

@ -31,7 +31,14 @@ export class Scroller {
this.element.addEventListener('scroll', this.handleScroll)
this.element.addEventListener('mousedown', this.handleClick)
this.element.addEventListener('mousemove', event => {
this.element.addEventListener('mousemove', this.handleMouseMove)
this.element.addEventListener('mouseup', this.handleMouseUp)
this.element.addEventListener('dblclick', this.handleDoubleClick)
this.element.addEventListener('keydown', this.handleKeydown)
}
private handleMouseMove = (event: MouseEvent) => {
if (!this.isSelecting) return;
const { offsetX, offsetY } = event
const lastSelectedCell = this.root.getCellByCoords(offsetX, offsetY)
@ -39,18 +46,27 @@ export class Scroller {
this.root.selection.selectedRange.to = lastSelectedCell
}
this.root.renderSheet()
})
this.element.addEventListener('mouseup', () => {
}
private handleMouseUp = () => {
this.isSelecting = false
if (this.root.selection.selectedRange) {
if (
(this.root.selection.selectedRange.from.row === this.root.selection.selectedRange.to.row) &&
(this.root.selection.selectedRange.from.column === this.root.selection.selectedRange.to.column)
) {
this.root.selection.selectedRange = null
}
}
this.root.renderSheet()
})
this.element.addEventListener('dblclick', event => {
}
private handleDoubleClick = (event: MouseEvent) => {
event.preventDefault();
const position = this.root.getCellByCoords(event.offsetX, event.offsetY)
this.root.showEditor(position)
})
this.element.addEventListener('keydown', this.handleKeydown)
}
private handleKeydown = (event: KeyboardEvent) => {
@ -96,6 +112,11 @@ export class Scroller {
if (!this.root.selection.selectedCell) return;
this.root.showEditor(this.root.selection.selectedCell)
}
if (event.key === 'Delete') {
this.root.deleteSelectedCellsValues()
this.root.renderSheet()
}
}
private handleClick = (event: MouseEvent) => {

View File

@ -6,7 +6,7 @@ import { Table } from "./components/table";
import { Toolbar } from "./components/toolbar";
import { Cell, CellConstructorProps, Position } from "./modules/cell";
import { Config, ViewProperties } from "./modules/config";
import { Selection } from "./modules/selection";
import { RangeSelectionType, Selection } from "./modules/selection";
import { Styles } from "./modules/styles";
import { Viewport } from "./modules/viewport";
import './scss/main.scss'
@ -119,6 +119,42 @@ export class Spreadsheet {
this.renderCell(row, column)
}
applyActionToRange(range: RangeSelectionType, callback: (cell: Cell) => any): void {
const fromRow = Math.min(range.from.row, range.to.row)
const toRow = Math.max(range.from.row, range.to.row)
const fromCol = Math.min(range.from.column, range.to.column)
const toCol = Math.max(range.from.column, range.to.column)
for (let row = fromRow; row <= toRow; row++) {
for (let col = fromCol; col <= toCol; col++) {
const cell = this.data[row][col]
callback(cell)
}
}
}
deleteSelectedCellsValues() {
if (this.selection.selectedRange !== null) {
this.applyActionToRange(this.selection.selectedRange, cell => {
this.changeCellValues(cell.position, {
displayValue: '',
resultValue: '',
value: ''
})
})
} else {
if (!this.selection.selectedCell) return;
this.changeCellValues(this.selection.selectedCell, {
displayValue: '',
resultValue: '',
value: ''
})
}
}
showEditor(position: Position) {
this.editor.show(position)
}