grid-component-editor
Version:
延长loading超时时间;转义伪类中文,防止乱码
76 lines (66 loc) • 2.58 kB
text/typescript
import { _assign } from '../utils/tool';
import { createContext } from 'react';
import {
IState, Action, RequestType, IContext, OptoinalEditorCom,
EditorComponent, ReducerType, ReducerPayload
} from '../types'
import { ReducerKey as RT, _countIncrement } from '../utils/const'
const Context: IContext = {
curFocusIndex: '',
curFocusCom: {} as EditorComponent,
components: {},
globalConfig: {},
dispatch: (type: ReducerType, payload?: any): any => { },
requestFunc(type: RequestType, param?: any) { } // 基于type
}
export const GridContext = createContext(Context)
export const GridProvider = GridContext.Provider
const snapshotArr: IState['components'][] = []
const pushSnap = (state: IState) => {
snapshotArr.push(JSON.parse(JSON.stringify(state.components)))
state.snapshotIndex++
}
const popSnap = () => {
return snapshotArr.pop()
}
export function reducer(state: IState, { type, payload }: Action): IState | any {
switch (type) {
case RT.INIT:
return { ...state, ...payload }
case RT.COMPONENT:
pushSnap(state)
_assign(state.components, payload)
return { ...state }
case RT.U_COM:
{
const com = state.components[(payload as OptoinalEditorCom).i]
_countIncrement(com)
_assign(com, payload)
return { ...state }
}
case RT.U_COM_PROP:
{
const com = state.components[payload.i]
_countIncrement(com)
_assign(com.props, payload.props)
return { ...state }
}
case RT.DELETE_COMPONENT:
pushSnap(state)
// payload equal to component's index
const { components, curFocusIndex } = state
delete components[payload]
// 'curFocusIndex' should be empty if payload equal to 'curFocusIndex'
curFocusIndex == payload && (state.curFocusIndex = '')
return { ...state }
case RT.CUR_FOCUS_INDEX:
if (state.curFocusIndex == payload) return
return { ...state, curFocusIndex: payload }
case RT.RECALL:
return _assign(state, state.snapshotIndex && { components: popSnap(), snapshotIndex: --state.snapshotIndex })
case RT.RESET:
pushSnap(state)
return { ...state, components: [], curFocusIndex: '' }
default: return state
}
}