From a860f8d09306d6b16863a7bff4d8961213001fb5 Mon Sep 17 00:00:00 2001 From: Sergey Krylov Date: Thu, 30 Jun 2022 09:42:21 +0500 Subject: [PATCH] add saving text --- src/components/formula/Formula.ts | 10 +++++++--- src/components/table/Table.ts | 23 ++++++++++++++++++++--- src/core/createStore.ts | 2 ++ src/redux/actions.ts | 9 ++++++++- src/redux/constants.ts | 1 + src/redux/rootReducer.ts | 10 +++++++++- 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/components/formula/Formula.ts b/src/components/formula/Formula.ts index b542119..0886020 100644 --- a/src/components/formula/Formula.ts +++ b/src/components/formula/Formula.ts @@ -26,12 +26,16 @@ export class Formula extends ExcelComponent { this.formulaInput = this.$root.find('#formula-input'); - this.$on('table:input', text => { - this.formulaInput.text = text; - }); + // this.$on('table:input', text => { + // this.formulaInput.text = text; + // }); this.$on('table:select-cell', text => { this.formulaInput.text = text; }); + + this.$subscribe(state => { + this.formulaInput.text = state.currentText; + }); } onInput(event: Event) { diff --git a/src/components/table/Table.ts b/src/components/table/Table.ts index 1408496..4594945 100644 --- a/src/components/table/Table.ts +++ b/src/components/table/Table.ts @@ -37,6 +37,7 @@ export class Table extends ExcelComponent { this.$on('formula:input', (data) => { this.selection.current.text = data; + this.updateCurrentTextInStore(data); }); this.$on('formula:enter-press', () => { @@ -46,8 +47,12 @@ export class Table extends ExcelComponent { // this.$subscribe(state => { // console.log('State in Table', state); // }); + this.initTable(); + } + initTable() { this.initTableSize(); + this.initTableContent(); } initTableSize() { @@ -66,6 +71,13 @@ export class Table extends ExcelComponent { }); } + initTableContent() { + const tableContent = this.store.getState().dataState; + Object.keys(tableContent).forEach(cellId => { + this.$root.find(`[data-id="${cellId}"]`).text = tableContent[cellId] || ''; + }); + } + emitSelectCallback() { this.$emit('table:select-cell', this.selection.current.text); } @@ -73,13 +85,19 @@ export class Table extends ExcelComponent { async resizeTable(event: MouseEvent) { try { const resizeData = await resizeHandler(this.$root, event); - console.log('Resize data', resizeData); this.$dispatch(actions.tableResize({ resizeData })); } catch (e) { console.warn('Resize error', e.message); } } + updateCurrentTextInStore(text: string) { + this.$dispatch(actions.changeText({ + text, + id: this.selection.current.data.id, + })); + } + onMousedown(event: MouseEvent) { selectHandler(event, this.selection, this.emitSelectCallback.bind(this)); this.resizeTable(event); @@ -90,7 +108,6 @@ export class Table extends ExcelComponent { } onInput(event: InputEvent) { - const inputText = (event.target as HTMLElement).textContent.trim(); - this.$emit('table:input', inputText); + this.updateCurrentTextInStore((event.target as HTMLElement).textContent.trim()); } } diff --git a/src/core/createStore.ts b/src/core/createStore.ts index 2a239e0..022f2c0 100644 --- a/src/core/createStore.ts +++ b/src/core/createStore.ts @@ -6,6 +6,8 @@ import { storage } from './utils'; const initialState: StateType = { colState: {}, rowState: {}, + dataState: {}, + currentText: '', ...storage('excel-state'), }; diff --git a/src/redux/actions.ts b/src/redux/actions.ts index c6b8a4f..69235b4 100644 --- a/src/redux/actions.ts +++ b/src/redux/actions.ts @@ -1,4 +1,4 @@ -import { TABLE_RESIZE } from './constants'; +import { CHANGE_TEXT, TABLE_RESIZE } from './constants'; export function tableResize(resizeData: any) { return { @@ -6,3 +6,10 @@ export function tableResize(resizeData: any) { ...resizeData, }; } + +export function changeText(data: { text: string, id: string }) { + return { + type: CHANGE_TEXT, + data, + }; +} diff --git a/src/redux/constants.ts b/src/redux/constants.ts index 7f678f9..3ca1267 100644 --- a/src/redux/constants.ts +++ b/src/redux/constants.ts @@ -1 +1,2 @@ export const TABLE_RESIZE = 'TABLE_RESIZE'; +export const CHANGE_TEXT = 'CHANGE_TEXT'; diff --git a/src/redux/rootReducer.ts b/src/redux/rootReducer.ts index 4814c44..0402d0a 100644 --- a/src/redux/rootReducer.ts +++ b/src/redux/rootReducer.ts @@ -1,4 +1,4 @@ -import { TABLE_RESIZE } from './constants'; +import { CHANGE_TEXT, TABLE_RESIZE } from './constants'; import { ActionType, StateType } from './types'; export function rootReducer(state: StateType, action: ActionType) { @@ -12,6 +12,14 @@ export function rootReducer(state: StateType, action: ActionType) { return { ...state, ...newState }; } + case CHANGE_TEXT: { + const newState: StateType = state.dataState || {}; + + newState[action.data.id] = action.data.text; + + return { ...state, currentText: action?.data?.text, dataState: newState }; + } + default: return state; } }