import { List } from "immutable" ; import { IMoveMoveFormat } from "json-kifu-format/dist/src/Formats" ; import { REQUEST_GET_JKF , RECEIVE_GET_JKF , REQUEST_PUT_JKF , RECEIVE_PUT_JKF , FAIL_PUT_JKF , CLEAR_MESSAGE , CHANGE_AUTO_SAVE , MOVE_PIECE , CHANGE_COMMENTS , UPDATE_COMMENTS , GOTO_PATH , MOVE_UP_FORK , MOVE_DOWN_FORK , REMOVE_FORK , } from "./actions" ; import { KifuNotebookState } from "./store" ; import { KifuTree , KifuTreeNode , Path } from "./models" ; const initialState = { kifuTree : KifuTree . fromJKF ( { header : { } , moves : [ { } ] } ) , message : "" , autoSaveEnabled : false , needSave : false , } ; export default function kifuNotebookReducer ( state = initialState , action ) { switch ( action . type ) { case REQUEST_GET_JKF : { return { ... state , message : "Fetching..." } ; } case RECEIVE_GET_JKF : { const jkf = action . jkf ; const kifuTree = KifuTree . fromJKF ( jkf ) ; return { ... state , ... initialState , kifuTree , message : "Fetched" } ; } case REQUEST_PUT_JKF : { return { ... state , message : "Saving..." , needSave : false } ; } case RECEIVE_PUT_JKF : { return { ... state , message : "Saved" } ; } case FAIL_PUT_JKF : { return { ... state , message : "Failed" } ; } case CLEAR_MESSAGE : { return { ... state , message : "" } ; } case CHANGE_AUTO_SAVE : { const enabled = action . enabled ; return { ... state , autoSaveEnabled : enabled } ; } case MOVE_PIECE : { const { kifuTree } = state ; const move = action . move ; return { ... state , ... movePiece ( kifuTree , move ) } ; } case GOTO_PATH : { const { kifuTree } = state ; const path = action . path ; return { ... state , kifuTree : kifuTree . setCurrentPath ( path ) } ; } case CHANGE_COMMENTS : { const { kifuTree } = state ; const value = action . value ; const newKifuTree = kifuTree . updateNode ( kifuTree . currentPath , ( node ) => { return node . set ( "comment" , value ) ; } ) ; return { ... state , kifuTree : newKifuTree } ; } case UPDATE_COMMENTS : { return { ... state , needSave : true } ; } case MOVE_UP_FORK : { const { kifuTree } = state ; const path = action . path ; return { ... state , ... updateFork ( kifuTree , path , ( children , lastIndex ) => { if ( lastIndex === 0 ) { return children ; } const prevNode = children . get ( lastIndex - 1 ) ! ; return children . delete ( lastIndex - 1 ) . insert ( lastIndex , prevNode ) ; } ) , } ; } case MOVE_DOWN_FORK : { const { kifuTree } = state ; const path = action . path ; return { ... state , ... updateFork ( kifuTree , path , ( children , lastIndex ) => { if ( lastIndex === children . size - 1 ) { return children ; } const targetNode = children . get ( lastIndex ) ! ; return children . delete ( lastIndex ) . insert ( lastIndex + 1 , targetNode ) ; } ) , } ; } case REMOVE_FORK : { const { kifuTree } = state ; const path = action . path ; return { ... state , ... updateFork ( kifuTree , path , ( children , lastIndex ) => { return children . delete ( lastIndex ) ; } ) , } ; } default : return state ; } } function movePiece ( kifuTree , move ) { if ( ! move . to ) { return { } ; } if ( move . from && move . from . x === move . to . x && move . from . y === move . to . y ) { return { } ; } move . to = { x : move . to . x , y : move . to . y } ; let newKifuTree ; try { newKifuTree = kifuTree . movePiece ( move ) ; if ( newKifuTree === false ) { move . promote = window . confirm ( "成りますか？" ) ; newKifuTree = kifuTree . movePiece ( move ) as KifuTree ; } } catch ( e ) { alert ( e ) ; return { } ; } const needSave = newKifuTree . rootNode !== kifuTree . rootNode ? true : undefined ; return { kifuTree : newKifuTree , needSave : needSave } ; } function updateFork ( kifuTree , path , forkUpdater ) { const newKifuTree = kifuTree . updateFork ( path , forkUpdater ) ; if ( newKifuTree === kifuTree ) { return { } ; } return { kifuTree : newKifuTree , needSave : true } ; }