diff --git a/src/components/scroller.ts b/src/components/scroller.ts index 88f2f13..a09dcca 100644 --- a/src/components/scroller.ts +++ b/src/components/scroller.ts @@ -2,18 +2,75 @@ import { Spreadsheet } from "../main" export class Scroller { element: HTMLDivElement - root: Spreadsheet + private verticalScroller: HTMLDivElement + private horizontalScroller: HTMLDivElement + private root: Spreadsheet + constructor(root: Spreadsheet) { this.root = root - const scroller = document.createElement('div') - scroller.classList.add('scroller') + const {horizontalScroller, scroller, verticalScroller} = this.buildComponent() this.element = scroller + this.verticalScroller = verticalScroller + this.horizontalScroller = horizontalScroller + + this.element.style.height = this.root.config.view.height + 'px' + this.element.style.width = this.root.config.view.width + 'px' + + this.updateScrollerSize() //* Init size set } + + buildComponent() { + const scroller = document.createElement('div') + const verticalScroller = document.createElement('div') + const horizontalScroller = document.createElement('div') + const groupScrollers = document.createElement('div') + const stack = document.createElement('div') + + verticalScroller.style.width = '0px' + verticalScroller.style.pointerEvents = 'none' + + horizontalScroller.style.pointerEvents = 'none' + + groupScrollers.style.display = 'flex' + + stack.appendChild(verticalScroller) + stack.appendChild(horizontalScroller) + groupScrollers.appendChild(stack) + this.verticalScroller = verticalScroller + this.horizontalScroller = horizontalScroller + scroller.appendChild(groupScrollers) + scroller.classList.add('scroller') + + return {scroller, verticalScroller, horizontalScroller} + } + + getActualHeight() { + return this.root.config.rows.reduce((acc, curr) => { + acc += curr.height + return acc + }, 0) + } + + getActualWidth() { + return this.root.config.columns.reduce((acc, curr) => { + acc += curr.width + return acc + }, 0) + } + + updateScrollerSize() { + const totalHeight = this.getActualHeight() + const totalWidth = this.getActualWidth() + + this.setScrollerHeight(totalHeight) + this.setScrollerWidth(totalWidth) + } + setScrollerHeight(height: number) { - this.element.style.height = height + 'px' + this.verticalScroller.style.height = height + 'px' } setScrollerWidth(width: number) { - this.element.style.width = width + 'px' + this.horizontalScroller.style.width = width + 'px' } } \ No newline at end of file diff --git a/src/components/sheet.ts b/src/components/sheet.ts index 2a0506b..75722e2 100644 --- a/src/components/sheet.ts +++ b/src/components/sheet.ts @@ -1,4 +1,5 @@ import { Spreadsheet } from "../main" +import { Position } from "../modules/cell" /** * Display (CANVAS) element where cells render @@ -11,11 +12,24 @@ export class Sheet { this.root = root const canvas = document.createElement('canvas') canvas.classList.add('sheet') + + //* Set up canvas sizes based on provided root config + canvas.height = this.root.config.view.height + canvas.width = this.root.config.view.width + canvas.style.width = this.root.config.view.width + 'px' + canvas.style.height = this.root.config.view.height + 'px' + this.element = canvas const ctx = this.element.getContext('2d') if(!ctx) throw new Error('Enable hardware acceleration') this.ctx = ctx + + } + + renderCell(position: Position) { + const {column, row} = position + this.root.data[row][column].render(this.root) } renderSheet() { @@ -25,8 +39,9 @@ export class Sheet { for(let row = 0; row <= lastRowIdx; row++) { for(let col = 0; col <= lastColIdx; col++ ) { - this.root.data[row][col].render(this.root.ctx, this.root.config) + this.renderCell({column: col, row}) } } } + } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index fabb9ec..5d5faf5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,13 +5,13 @@ import { Sheet } from "./components/sheet"; import { Table } from "./components/table"; import { Toolbar } from "./components/toolbar"; import { Cell } from "./modules/cell"; -import { Config, ConfigProperties } from "./modules/config"; +import { Config, ViewProperties } from "./modules/config"; import { Styles } from "./modules/styles"; import './scss/main.scss' -import { createSampleConfig, createSampleData, makeSpreadsheetConfigAndData } from "./utils/createData"; +import { createSampleConfig, createSampleData } from "./utils/createData"; /* - * Component structure + ! Component structure