add change table size

This commit is contained in:
Sergey Krylov 2022-07-13 16:27:41 +05:00
parent d1b83a26a3
commit b282490279
6 changed files with 50 additions and 11 deletions

View File

@ -3,9 +3,9 @@ import * as actions from 'redux/action-creators';
import { $, Dom } from 'core/Dom'; import { $, Dom } from 'core/Dom';
import { ComponentOptionsType, ExcelComponent } from 'core/ExcelComponent'; import { ComponentOptionsType, ExcelComponent } from 'core/ExcelComponent';
import { TableSelection } from 'components/table/TableSelection'; import { TableSelection } from 'components/table/TableSelection';
import { changeCurrentStyles, changeCurrentText } from 'redux/action-creators'; import { changeCurrentStyles, changeCurrentText, changeTableSize } from 'redux/action-creators';
import { createTable, getNewRowHTML } from 'components/table/table.template'; import { createTable, getNewRowHTML } from 'components/table/table.template';
import { initialStyleState } from 'src/constants'; import { initialState, initialStyleState } from 'src/constants';
import { parse } from 'core/utils'; import { parse } from 'core/utils';
import { resizeHandler } from 'components/table/handlers/table.resize'; import { resizeHandler } from 'components/table/handlers/table.resize';
import { selectHandler } from 'components/table/handlers/table.select.handler'; import { selectHandler } from 'components/table/handlers/table.select.handler';
@ -16,10 +16,7 @@ export class Table extends ExcelComponent {
private selection: TableSelection; private selection: TableSelection;
private isMouseDowned: boolean; private isMouseDowned: boolean;
private tableResizing = false; private tableResizing = false;
public tableSize = { public tableSize = { row: initialState.tableSize.row, col: initialState.tableSize.col };
row: 50,
col: 24,
};
constructor($root: Dom, options: ComponentOptionsType) { constructor($root: Dom, options: ComponentOptionsType) {
super($root, { super($root, {
@ -29,9 +26,12 @@ export class Table extends ExcelComponent {
}); });
this.isMouseDowned = false; this.isMouseDowned = false;
const { col, row } = this.getTableSize();
this.tableSize = { col, row };
} }
toHTML(): string { toHTML(): string {
console.log('Table size', this.tableSize);
return createTable(this.tableSize.row, this.tableSize.col); return createTable(this.tableSize.row, this.tableSize.col);
} }
@ -39,6 +39,23 @@ export class Table extends ExcelComponent {
this.selection = new TableSelection(this); this.selection = new TableSelection(this);
} }
getTableSize() {
const { tableSize: { col, row }, colState, rowState } = this.store.getState();
const maxRowFromState = Math.max(...Object.keys(rowState).map(el => +el));
const maxColFromState = Math.max(...Object.keys(colState).map(el => +el));
const normalTableSize = {
col: Math.max(maxRowFromState, row),
row: Math.max(maxColFromState, col),
};
if ((maxColFromState !== this.tableSize.col) || (maxRowFromState !== this.tableSize.row)) {
this.dispatchToStore(changeTableSize(normalTableSize));
}
return normalTableSize;
}
init() { init() {
super.init(); super.init();
@ -152,6 +169,8 @@ export class Table extends ExcelComponent {
this.$root.$el.insertAdjacentHTML('beforeend', getNewRowHTML(this.tableSize.row, this.tableSize.col)); this.$root.$el.insertAdjacentHTML('beforeend', getNewRowHTML(this.tableSize.row, this.tableSize.col));
const $newRow = $(`[data-row="${this.tableSize.row - 1}"]`); const $newRow = $(`[data-row="${this.tableSize.row - 1}"]`);
this.initColSizes($newRow); this.initColSizes($newRow);
this.dispatchToStore(changeTableSize(this.tableSize));
}; };
onMousedown(event: MouseEvent) { onMousedown(event: MouseEvent) {

View File

@ -38,6 +38,10 @@ export const initialState: StateType = {
currentStyles: initialStyleState, currentStyles: initialStyleState,
currentText: 'initial text', currentText: 'initial text',
id: '0', id: '0',
tableSize: {
col: 10,
row: 10,
},
}; };
export function getNormalizeInitialState(params: string): StateType { export function getNormalizeInitialState(params: string): StateType {
@ -45,6 +49,7 @@ export function getNormalizeInitialState(params: string): StateType {
if (!state) return initialState; if (!state) return initialState;
return { return {
...initialState,
...state, ...state,
id: params, id: params,
currentStyles: state?.stylesState?.[startCellId] || initialStyleState, currentStyles: state?.stylesState?.[startCellId] || initialStyleState,

View File

@ -6,3 +6,4 @@ export const DELETE_TABLE = 'DELETE_TABLE';
export const TABLE_RESIZE = 'TABLE_RESIZE'; export const TABLE_RESIZE = 'TABLE_RESIZE';
export const UPDATE_DATE = 'UPDATE_DATE'; export const UPDATE_DATE = 'UPDATE_DATE';
export const CHANGE_CURRENT_TEXT = 'CHANGE_CURRENT_TEXT'; export const CHANGE_CURRENT_TEXT = 'CHANGE_CURRENT_TEXT';
export const CHANGE_TABLE_SIZE = 'CHANGE_TABLE_SIZE';

View File

@ -7,7 +7,7 @@ import {
APPLY_STYLES, APPLY_STYLES,
CHANGE_TITLE, CHANGE_TITLE,
DELETE_TABLE, DELETE_TABLE,
UPDATE_DATE, CHANGE_CURRENT_TEXT, UPDATE_DATE, CHANGE_CURRENT_TEXT, CHANGE_TABLE_SIZE,
} from 'redux/action-constants'; } from 'redux/action-constants';
export function tableResize(resizeData: ResizeReturnDataType): ActionType { export function tableResize(resizeData: ResizeReturnDataType): ActionType {
@ -65,3 +65,10 @@ export function changeCurrentText(data: string): ActionType {
data, data,
}; };
} }
export function changeTableSize(data: { col: number, row: number }): ActionType {
return {
type: CHANGE_TABLE_SIZE,
data,
};
}

View File

@ -8,11 +8,10 @@ import {
APPLY_STYLES, APPLY_STYLES,
CHANGE_TITLE, CHANGE_TITLE,
DELETE_TABLE, DELETE_TABLE,
UPDATE_DATE, CHANGE_CURRENT_TEXT, UPDATE_DATE, CHANGE_CURRENT_TEXT, CHANGE_TABLE_SIZE,
} from 'redux/action-constants'; } from 'redux/action-constants';
export function rootReducer(state: StateType, action: ActionType) { export function rootReducer(state: StateType, action: ActionType): StateType {
// export const rootReducer: ReducerType = function (state: StateType, action: ActionType) {
switch (action.type) { switch (action.type) {
case TABLE_RESIZE: { case TABLE_RESIZE: {
const newState: StateType = { ...state }; const newState: StateType = { ...state };
@ -60,7 +59,7 @@ export function rootReducer(state: StateType, action: ActionType) {
localStorage.removeItem(name); localStorage.removeItem(name);
ActiveRoute.navigateTo = ''; ActiveRoute.navigateTo = '';
return null; return null as any;
} }
case UPDATE_DATE: { case UPDATE_DATE: {
@ -71,6 +70,10 @@ export function rootReducer(state: StateType, action: ActionType) {
return { ...state, currentText: action.data }; return { ...state, currentText: action.data };
} }
case CHANGE_TABLE_SIZE: {
return { ...state, tableSize: action.data };
}
default: return state; default: return state;
} }
} }

View File

@ -15,6 +15,10 @@ export type StateType = {
stylesState: { [k: string]: ToolbarStateType }; stylesState: { [k: string]: ToolbarStateType };
title: string; title: string;
currentText: string; currentText: string;
tableSize: {
col: number;
row: number;
};
}; };
export type ReducerType = (state: StateType, action: ActionType) => StateType | null; export type ReducerType = (state: StateType, action: ActionType) => StateType | null;