editor.js/src/components/store/demo.ts
Ilya Moroz e6db8d5140
[Feature] Add state manager (#2018)
* add state manager with demo file

* remove initial state

* move Store type to Store.ts

* add new actions

* change store schema

* add docs

* type -> interface, add deepCopy function

* move types to the /types/ folder

* rename types files, change state type (add blocks: key)

* fix createStore.ts func

* add documentation for reducer

* use BlockMutationType instead of ActionType

* add doc

* deep copy of initial state

* add doc for createStore

* Apply suggestions from code review

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>

* rename `reducer` to `blocksReducer`

* add a listener type, pass changed state to the listener

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>
2022-05-05 14:26:04 +03:00

80 lines
1.6 KiB
TypeScript

import store from './index';
import { BlockMutationType } from '../../../types/events/block/mutation-type';
import { EditorState } from '../../../types/store/editorState';
import { Listener } from '../../../types/store/listener';
/**
* Handle changes with previous and current states
*/
const onDataChange = (): Listener => {
/**
* Initial state
*/
let currentState = store.getState();
/**
* onChange handler
*
* @param changedState - changed state after dispatching
*/
return (changedState: EditorState): void => {
const prevState = currentState;
currentState = changedState;
console.log('***');
console.log('Previous state:', prevState);
console.log('Current state:', currentState);
console.log('***');
};
};
const unsubscribeOnDataChange = store.subscribe(onDataChange());
const block1 = {
id: '3JPEqh8_Wc',
type: 'header',
data: {
text: 'Editor.js',
level: 2,
},
};
const block2 = {
id: 'AsbMKCuatV',
type: 'paragraph',
data: {
text: 'Hey. Meet the new <b>Editor</b>. On this page you can see it in action — try to edit this text.',
},
};
const block2Changed = {
id: 'AsbMKCuatV',
type: 'paragraph',
data: {
text: 'Hey. Meet the new Editor. On this page you can see it in action — try to edit this text.',
},
};
store.dispatch({
type: BlockMutationType.Added,
data: block1,
});
store.dispatch({
type: BlockMutationType.Added,
data: block2,
});
store.dispatch({
type: BlockMutationType.Changed,
data: block2Changed,
});
store.dispatch({
type: BlockMutationType.Removed,
blockId: block1.id,
});
unsubscribeOnDataChange();