Added delete for range by press Del button
This commit is contained in:
parent
29b95c6b73
commit
d80cb707fe
|
|
@ -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) => {
|
||||
|
|
@ -92,10 +108,15 @@ export class Scroller {
|
|||
}
|
||||
}
|
||||
|
||||
if(event.key === 'F2') {
|
||||
if(!this.root.selection.selectedCell) return;
|
||||
if (event.key === 'F2') {
|
||||
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) => {
|
||||
|
|
|
|||
44
src/main.ts
44
src/main.ts
|
|
@ -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'
|
||||
|
|
@ -44,7 +44,7 @@ export class Spreadsheet {
|
|||
|
||||
constructor(target: string | HTMLElement, props?: SpreadsheetConstructorProperties) {
|
||||
const config = createSampleConfig(750, 750)
|
||||
if(props?.view) {
|
||||
if (props?.view) {
|
||||
config.view = props.view
|
||||
}
|
||||
|
||||
|
|
@ -108,17 +108,53 @@ export class Spreadsheet {
|
|||
}
|
||||
|
||||
getCell(position: Position): Cell {
|
||||
const {column, row} = position
|
||||
const { column, row } = position
|
||||
return this.data[row][column]
|
||||
}
|
||||
|
||||
changeCellValues(position: Position, values: Partial<Omit<CellConstructorProps, 'position'>>) {
|
||||
const {column, row} = position
|
||||
const { column, row } = position
|
||||
|
||||
this.data[row][column].changeValues(values)
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue