Linted & Formatted project

This commit is contained in:
Eugene 2023-07-26 15:18:56 +03:00
parent 322378904c
commit fc9a0df38d
8 changed files with 159 additions and 136 deletions

View File

@ -38,7 +38,8 @@
"predeploy": "npm run dist", "predeploy": "npm run dist",
"deploy": "gh-pages -d dist", "deploy": "gh-pages -d dist",
"lint": "eslint src --ext .ts", "lint": "eslint src --ext .ts",
"lint:fix": "eslint src --ext .ts --fix" "lint:fix": "eslint src --ext .ts --fix",
"format": "prettier --write ./src/**/*.ts"
}, },
"devDependencies": { "devDependencies": {
"@rollup/plugin-typescript": "^11.1.2", "@rollup/plugin-typescript": "^11.1.2",

View File

@ -61,8 +61,8 @@ export class Editor {
values: { values: {
value: this.element.value, value: this.element.value,
displayValue: this.element.value, displayValue: this.element.value,
} },
}) });
this.hide(); this.hide();
} }

View File

@ -43,7 +43,7 @@ export class Scroller {
} }
public setSelectingMode(mode: boolean) { public setSelectingMode(mode: boolean) {
this.isSelecting = mode this.isSelecting = mode;
} }
private handleMouseMove = (event: MouseEvent) => { private handleMouseMove = (event: MouseEvent) => {
@ -51,37 +51,42 @@ export class Scroller {
const { offsetX, offsetY } = event; const { offsetX, offsetY } = event;
const lastSelectedCell = this.root.getCellByCoords(offsetX, offsetY); const lastSelectedCell = this.root.getCellByCoords(offsetX, offsetY);
let isRangeChanged = false let isRangeChanged = false;
if (this.root.selection.selectedRange) { if (this.root.selection.selectedRange) {
isRangeChanged = !checkEqualCellSelections(this.root.selection.selectedRange.to, lastSelectedCell) isRangeChanged = !checkEqualCellSelections(
this.root.selection.selectedRange.to,
lastSelectedCell,
);
if (isRangeChanged) { if (isRangeChanged) {
this.root.selection.selectedRange.to = lastSelectedCell; this.root.selection.selectedRange.to = lastSelectedCell;
this.root.events.dispatch({ this.root.events.dispatch({
type: EventTypes.SELECTION_CHANGE, type: EventTypes.SELECTION_CHANGE,
selection: this.root.selection, selection: this.root.selection,
enableCallback: true enableCallback: true,
}) });
} }
} }
}; };
private handleMouseUp = () => { private handleMouseUp = () => {
this.isSelecting = false; this.isSelecting = false;
const newSelection = {...this.root.selection} const newSelection = { ...this.root.selection };
if (this.root.selection.selectedRange) { if (this.root.selection.selectedRange) {
if ( if (
checkEqualCellSelections(this.root.selection.selectedRange.from, this.root.selection.selectedRange.to) checkEqualCellSelections(
this.root.selection.selectedRange.from,
this.root.selection.selectedRange.to,
)
) { ) {
newSelection.selectedRange = null; newSelection.selectedRange = null;
this.root.events.dispatch({ this.root.events.dispatch({
type: EventTypes.SELECTION_CHANGE, type: EventTypes.SELECTION_CHANGE,
selection: newSelection, selection: newSelection,
enableCallback: false enableCallback: false,
}) });
} }
} }
@ -150,8 +155,8 @@ export class Scroller {
this.root.events.dispatch({ this.root.events.dispatch({
type: EventTypes.SELECTION_CHANGE, type: EventTypes.SELECTION_CHANGE,
selection: this.root.selection, selection: this.root.selection,
enableCallback: true enableCallback: true,
}) });
} }
//* Start typings //* Start typings
@ -183,8 +188,8 @@ export class Scroller {
this.root.events.dispatch({ this.root.events.dispatch({
type: EventTypes.CELL_CLICK, type: EventTypes.CELL_CLICK,
event, event,
scroller: this scroller: this,
}) });
}; };
private handleScroll = () => { private handleScroll = () => {

View File

@ -2,19 +2,18 @@ import Spreadsheet, { SpreadsheetConstructorProperties } from "./main";
const options: SpreadsheetConstructorProperties = { const options: SpreadsheetConstructorProperties = {
onCellClick: (event, cell) => { onCellClick: (event, cell) => {
console.log('Cell click', event, cell) console.log("Cell click", event, cell);
}, },
onSelectionChange: (selection) => { onSelectionChange: (selection) => {
console.log("Changed selection: ", selection) console.log("Changed selection: ", selection);
}, },
onCellChange(cell) { onCellChange(cell) {
console.log("Cell changed: ", cell) console.log("Cell changed: ", cell);
}, },
} };
const sheet = new Spreadsheet("#spreadsheet", options); const sheet = new Spreadsheet("#spreadsheet", options);
function saveDataToLS() { function saveDataToLS() {
const serializableData = sheet.serializeData(); const serializableData = sheet.serializeData();
localStorage.setItem("sheet", JSON.stringify(serializableData)); localStorage.setItem("sheet", JSON.stringify(serializableData));

View File

@ -10,7 +10,13 @@ import {
Position, Position,
SerializableCell, SerializableCell,
} from "./modules/cell"; } from "./modules/cell";
import { CellChangeEvent, CellClickEvent, Config, SelectionChangeEvent, ViewProperties } from "./modules/config"; import {
CellChangeEvent,
CellClickEvent,
Config,
SelectionChangeEvent,
ViewProperties,
} from "./modules/config";
import { RangeSelectionType, Selection } from "./modules/selection"; import { RangeSelectionType, Selection } from "./modules/selection";
import { Styles } from "./modules/styles"; import { Styles } from "./modules/styles";
import { Viewport } from "./modules/viewport"; import { Viewport } from "./modules/viewport";
@ -37,9 +43,9 @@ import { Events } from "./modules/events";
export interface SpreadsheetConstructorProperties { export interface SpreadsheetConstructorProperties {
view?: ViewProperties; view?: ViewProperties;
onCellClick?: CellClickEvent | null onCellClick?: CellClickEvent | null;
onSelectionChange?: SelectionChangeEvent | null onSelectionChange?: SelectionChangeEvent | null;
onCellChange?: CellChangeEvent | null onCellChange?: CellChangeEvent | null;
} }
export const CSS_PREFIX = "modern_sc_"; export const CSS_PREFIX = "modern_sc_";
@ -58,7 +64,7 @@ export default class Spreadsheet {
public viewport: Viewport; public viewport: Viewport;
public selection: Selection; public selection: Selection;
public cache: Cache; public cache: Cache;
public events: Events public events: Events;
constructor( constructor(
target: string | HTMLElement, target: string | HTMLElement,
@ -75,9 +81,9 @@ export default class Spreadsheet {
this.config = new Config(config); this.config = new Config(config);
this.config.onCellClick = props?.onCellClick ?? null this.config.onCellClick = props?.onCellClick ?? null;
this.config.onSelectonChange = props?.onSelectionChange ?? null this.config.onSelectonChange = props?.onSelectionChange ?? null;
this.config.onCellChange = props?.onCellChange ?? null this.config.onCellChange = props?.onCellChange ?? null;
this.rowsBar = new RowsBar(this); this.rowsBar = new RowsBar(this);
this.columnsBar = new ColumnsBar(this); this.columnsBar = new ColumnsBar(this);
@ -92,8 +98,7 @@ export default class Spreadsheet {
this.scroller.getViewportBoundlingRect(), this.scroller.getViewportBoundlingRect(),
); );
this.selection = new Selection(); this.selection = new Selection();
this.events = new Events(this) this.events = new Events(this);
this.data = data; this.data = data;
this.styles = new Styles(); this.styles = new Styles();
@ -376,7 +381,7 @@ export default class Spreadsheet {
view, view,
rows, rows,
columns, columns,
onCellClick: null onCellClick: null,
}); });
return config; return config;

View File

@ -7,9 +7,9 @@ export interface ViewProperties {
width: number; width: number;
height: number; height: number;
} }
export type CellClickEvent = (event: MouseEvent, cell: Cell) => void export type CellClickEvent = (event: MouseEvent, cell: Cell) => void;
export type SelectionChangeEvent = (selection: Selection) => void export type SelectionChangeEvent = (selection: Selection) => void;
export type CellChangeEvent = (cell: Cell) => void export type CellChangeEvent = (cell: Cell) => void;
export type ConfigProperties = { export type ConfigProperties = {
/** Please, end it with '_' symbol. /** Please, end it with '_' symbol.
@ -21,9 +21,9 @@ export type ConfigProperties = {
rows: Row[]; rows: Row[];
columns: Column[]; columns: Column[];
view: ViewProperties; view: ViewProperties;
onCellClick?: CellClickEvent | null onCellClick?: CellClickEvent | null;
onSelectionChange?: SelectionChangeEvent | null onSelectionChange?: SelectionChangeEvent | null;
onCellChange?: CellChangeEvent | null onCellChange?: CellChangeEvent | null;
}; };
export type SheetConfigConstructorProps = { export type SheetConfigConstructorProps = {
@ -39,17 +39,17 @@ export class Config {
height: 600, height: 600,
}; };
onCellClick: ((event: MouseEvent, cell: Cell) => void) | null = null onCellClick: ((event: MouseEvent, cell: Cell) => void) | null = null;
onSelectonChange: SelectionChangeEvent | null = null onSelectonChange: SelectionChangeEvent | null = null;
onCellChange: CellChangeEvent | null = null onCellChange: CellChangeEvent | null = null;
constructor(props: ConfigProperties) { constructor(props: ConfigProperties) {
this.columns = props.columns; this.columns = props.columns;
this.rows = props.rows; this.rows = props.rows;
this.view = props.view; this.view = props.view;
this.onCellClick = props.onCellClick ?? null this.onCellClick = props.onCellClick ?? null;
this.onSelectonChange = props.onSelectionChange ?? null this.onSelectonChange = props.onSelectionChange ?? null;
this.onCellChange = props.onCellChange ?? null this.onCellChange = props.onCellChange ?? null;
} }
} }

View File

@ -4,63 +4,65 @@ import Spreadsheet, { Cell, CellConstructorProps, Selection } from "../main";
export enum EventTypes { export enum EventTypes {
CELL_CLICK = "CELL_CLICK", CELL_CLICK = "CELL_CLICK",
SELECTION_CHANGE = "CHANGE_SELECTION", SELECTION_CHANGE = "CHANGE_SELECTION",
CELL_CHANGE = "CELL_CHANGE" CELL_CHANGE = "CELL_CHANGE",
} }
export type CellClickEvent = { export type CellClickEvent = {
type: EventTypes.CELL_CLICK type: EventTypes.CELL_CLICK;
event: MouseEvent event: MouseEvent;
scroller: Scroller scroller: Scroller;
} };
export type ChangeSelectionEvent = { export type ChangeSelectionEvent = {
type: EventTypes.SELECTION_CHANGE, type: EventTypes.SELECTION_CHANGE;
selection: Selection selection: Selection;
enableCallback?: boolean enableCallback?: boolean;
} };
export type ChangeCellEvent = { export type ChangeCellEvent = {
type: EventTypes.CELL_CHANGE, type: EventTypes.CELL_CHANGE;
cell: Cell, cell: Cell;
values: Partial<Omit<CellConstructorProps, "position">> values: Partial<Omit<CellConstructorProps, "position">>;
} };
export type ActionTypes = CellClickEvent | ChangeSelectionEvent | ChangeCellEvent export type ActionTypes =
| CellClickEvent
| ChangeSelectionEvent
| ChangeCellEvent;
export class Events { export class Events {
root: Spreadsheet;
root: Spreadsheet
constructor(root: Spreadsheet) { constructor(root: Spreadsheet) {
this.root = root this.root = root;
} }
dispatch(action: ActionTypes) { dispatch(action: ActionTypes) {
switch (action.type) { switch (action.type) {
case EventTypes.CELL_CLICK: { case EventTypes.CELL_CLICK: {
const { event, scroller } = action const { event, scroller } = action;
// //
//* Here may be side effects //* Here may be side effects
// //
this.cellClick(event, scroller) this.cellClick(event, scroller);
break; break;
} }
case EventTypes.SELECTION_CHANGE: { case EventTypes.SELECTION_CHANGE: {
const { selection, enableCallback } = action const { selection, enableCallback } = action;
// //
//* Here may be side effects //* Here may be side effects
// //
this.changeSelection(selection, enableCallback) this.changeSelection(selection, enableCallback);
break; break;
} }
case EventTypes.CELL_CHANGE: { case EventTypes.CELL_CHANGE: {
const { cell, values } = action const { cell, values } = action;
// //
//* Here may be side effects //* Here may be side effects
// //
this.changeCellValues(cell, values) this.changeCellValues(cell, values);
break; break;
} }
@ -74,10 +76,10 @@ export class Events {
if (event.button !== 0) return; // Left mouse button if (event.button !== 0) return; // Left mouse button
const { offsetX, offsetY } = event; const { offsetX, offsetY } = event;
const clickedCell = this.root.getCellByCoords(offsetX, offsetY); const clickedCell = this.root.getCellByCoords(offsetX, offsetY);
const cell = this.root.getCell(clickedCell) const cell = this.root.getCell(clickedCell);
const selection = new Selection() const selection = new Selection();
selection.selectedCell = clickedCell selection.selectedCell = clickedCell;
selection.selectedRange = { selection.selectedRange = {
from: clickedCell, from: clickedCell,
to: clickedCell, to: clickedCell,
@ -85,23 +87,26 @@ export class Events {
scroller.setSelectingMode(true); scroller.setSelectingMode(true);
this.changeSelection(selection, true) this.changeSelection(selection, true);
this.root.config.onCellClick?.(event, cell) this.root.config.onCellClick?.(event, cell);
} };
private changeSelection = (selection: Selection, enableCallback = false) => { private changeSelection = (selection: Selection, enableCallback = false) => {
this.root.selection = selection this.root.selection = selection;
if (enableCallback) this.root.config.onSelectonChange?.(selection) if (enableCallback) this.root.config.onSelectonChange?.(selection);
this.root.renderSheet(); this.root.renderSheet();
this.root.renderColumnsBar(); this.root.renderColumnsBar();
this.root.renderRowsBar(); this.root.renderRowsBar();
} };
private changeCellValues(cell: Cell, values: Partial<Omit<CellConstructorProps, "position">>) { private changeCellValues(
this.root.changeCellValues(cell.position, values) cell: Cell,
values: Partial<Omit<CellConstructorProps, "position">>,
) {
this.root.changeCellValues(cell.position, values);
this.root.config.onCellChange?.(cell) this.root.config.onCellChange?.(cell);
} }
} }

View File

@ -1,12 +1,20 @@
import { BaseSelectionType, RangeSelectionType } from "../main"; import { BaseSelectionType, RangeSelectionType } from "../main";
export function checkEqualRanges(range1: RangeSelectionType, range2: RangeSelectionType) { export function checkEqualRanges(
const equalRows = range1.from.row === range2.to.row range1: RangeSelectionType,
const equalColumns = range1.from.column === range2.to.column range2: RangeSelectionType,
) {
const equalRows = range1.from.row === range2.to.row;
const equalColumns = range1.from.column === range2.to.column;
return equalRows && equalColumns return equalRows && equalColumns;
} }
export function checkEqualCellSelections(selection1: BaseSelectionType, selection2: BaseSelectionType) { export function checkEqualCellSelections(
return selection1.column === selection2.column && selection1.row === selection2.row selection1: BaseSelectionType,
selection2: BaseSelectionType,
) {
return (
selection1.column === selection2.column && selection1.row === selection2.row
);
} }