add selection

This commit is contained in:
Sergey Krylov 2022-06-24 10:28:59 +05:00
parent 2c80d20fb8
commit 93b96ad8ff
7 changed files with 88 additions and 7 deletions

View File

@ -1,10 +1,15 @@
import { $ } from '../../core/dom';
import { ExcelComponent } from '../../core/ExcelComponent'; import { ExcelComponent } from '../../core/ExcelComponent';
import { isCell } from './table.functions';
import { TableSelection } from './TableSelection';
import { createTable } from './table.template'; import { createTable } from './table.template';
import { resizeHandler } from './table.resize'; import { resizeHandler } from './handlers/table.resize';
export class Table extends ExcelComponent { export class Table extends ExcelComponent {
static className = 'excel__table'; static className = 'excel__table';
private selection: any;
constructor($root: any) { constructor($root: any) {
super($root, { super($root, {
name: 'Table', name: 'Table',
@ -16,7 +21,23 @@ export class Table extends ExcelComponent {
return createTable(50, 24); return createTable(50, 24);
} }
prepare() {
this.selection = new TableSelection();
}
init() {
super.init();
const $cell = this.$root.find('[data-id="0:0"]');
this.selection.select($cell);
}
onMousedown(event: MouseEvent) { onMousedown(event: MouseEvent) {
if (isCell(event)) {
this.selection.select($(event.target as HTMLElement));
return;
}
resizeHandler(this.$root, event); resizeHandler(this.$root, event);
} }
} }

View File

@ -0,0 +1,25 @@
import { Dom } from '../../core/dom';
export class TableSelection {
static selectedClassName = 'selected';
private group: Dom[];
constructor() {
this.group = [];
}
select($el: Dom) {
this.clearSelection();
this.group = [$el];
$el.addClass(TableSelection.selectedClassName);
}
clearSelection() {
this.group.forEach(el => el.removeClass(TableSelection.selectedClassName));
this.group = [];
}
selectGroup() {
}
}

View File

@ -1,4 +1,4 @@
import { $, Dom } from '../../core/dom'; import { $, Dom } from '../../../core/dom';
type CustomElementType = Element & { css: any }; type CustomElementType = Element & { css: any };

View File

@ -0,0 +1,5 @@
import { $ } from '../../core/dom';
export function isCell(event: Event): boolean {
return $(event.target as HTMLElement).data.type === 'cell';
}

View File

@ -3,9 +3,19 @@ const CODES = {
Z: 90, Z: 90,
}; };
function createCell(cellContent = '', index = 1) { function createCell(cellContent = '', colIndex = 1, rowIndex = 1) {
return ` return `
<div class="cell" contenteditable data-col="${index}">${cellContent}</div> <div
class="cell"
contenteditable
data-col="${colIndex}"
data-row=${rowIndex}
data-id="${rowIndex}:${colIndex}"
data-type="cell"
>
${cellContent}
</div>
`; `;
} }
@ -42,12 +52,13 @@ export function createTable(rowsCount = 10, columnCount = 10) {
rows.push(createRow(cols, '', false)); rows.push(createRow(cols, '', false));
for (let i = 0; i < rowsCount; i++) { for (let row = 0; row < rowsCount; row++) {
const cells = new Array(colsCount) const cells = new Array(colsCount)
.fill('') .fill('')
.map((el, index) => createCell('', index)) .map((el, index) => createCell('', index, row))
.join(''); .join('');
rows.push(createRow(cells, `${i + 1}`));
rows.push(createRow(cells, `${row + 1}`));
} }
return rows.join(''); return rows.join('');

View File

@ -2,6 +2,7 @@ import { DomListener } from './DomListener';
interface ExcelComponentClass { interface ExcelComponentClass {
toHTML: () => string; toHTML: () => string;
prepare: () => void;
} }
type OptionsType = { type OptionsType = {
@ -15,6 +16,12 @@ export class ExcelComponent extends DomListener implements ExcelComponentClass {
constructor($root: any, options: OptionsType) { constructor($root: any, options: OptionsType) {
super($root, options?.listeners); super($root, options?.listeners);
this.name = options?.name; this.name = options?.name;
this.prepare();
}
prepare() {
} }
toHTML() { toHTML() {

View File

@ -65,6 +65,10 @@ export class Dom implements DomClass {
return this.$el.getBoundingClientRect(); return this.$el.getBoundingClientRect();
} }
find(selector: string): Dom {
return $(this.$el.querySelector(selector) as HTMLElement);
}
findAll(selector: string) { findAll(selector: string) {
return this.$el.querySelectorAll(selector); return this.$el.querySelectorAll(selector);
} }
@ -74,6 +78,14 @@ export class Dom implements DomClass {
this.$el.style[key] = styles[key]; this.$el.style[key] = styles[key];
}); });
} }
addClass(className: string) {
this.$el.classList.add(className);
}
removeClass(className: string) {
this.$el.classList.remove(className);
}
} }
export function $(selector: SelectorType) { export function $(selector: SelectorType) {