-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from ryohey/refactor
Refactor
- Loading branch information
Showing
12 changed files
with
153 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,22 @@ | ||
import cloneDeep from "lodash/cloneDeep" | ||
import { deserialize, serialize } from "serializr" | ||
import Song from "../../common/song/Song" | ||
import ArrangeViewStore from "../stores/ArrangeViewStore" | ||
import { ControlStore } from "../stores/ControlStore" | ||
import PianoRollStore from "../stores/PianoRollStore" | ||
import RootStore from "../stores/RootStore" | ||
|
||
// we use any for now. related: https://github.com/Microsoft/TypeScript/issues/1897 | ||
type Json = any | ||
|
||
export interface SerializedState { | ||
song: Json | ||
selection: PianoRollStore["selection"] | ||
selectedNoteIds: PianoRollStore["selectedNoteIds"] | ||
selectedControllerEventIds: ControlStore["selectedEventIds"] | ||
arrangeSelection: ArrangeViewStore["selection"] | ||
arrangeSelectedEventIds: ArrangeViewStore["selectedEventIds"] | ||
} | ||
|
||
const serializeUndoableState = (rootStore: RootStore): SerializedState => { | ||
return { | ||
song: serialize(rootStore.song), | ||
selection: cloneDeep(rootStore.pianoRollStore.selection), | ||
selectedNoteIds: cloneDeep(rootStore.pianoRollStore.selectedNoteIds), | ||
selectedControllerEventIds: cloneDeep( | ||
rootStore.controlStore.selectedEventIds, | ||
), | ||
arrangeSelection: cloneDeep(rootStore.arrangeViewStore.selection), | ||
arrangeSelectedEventIds: cloneDeep( | ||
rootStore.arrangeViewStore.selectedEventIds, | ||
), | ||
} | ||
} | ||
|
||
const restoreState = | ||
(rootStore: RootStore) => (serializedState: SerializedState) => { | ||
const song = deserialize(Song, serializedState.song) | ||
rootStore.song = song | ||
rootStore.pianoRollStore.selection = serializedState.selection | ||
rootStore.pianoRollStore.selectedNoteIds = serializedState.selectedNoteIds | ||
rootStore.controlStore.selectedEventIds = | ||
serializedState.selectedControllerEventIds | ||
rootStore.arrangeViewStore.selection = serializedState.arrangeSelection | ||
rootStore.arrangeViewStore.selectedEventIds = | ||
serializedState.arrangeSelectedEventIds | ||
} | ||
|
||
export const pushHistory = (rootStore: RootStore) => () => { | ||
const state = serializeUndoableState(rootStore) | ||
const state = rootStore.serialize() | ||
rootStore.historyStore.push(state) | ||
} | ||
|
||
export const undo = (rootStore: RootStore) => () => { | ||
const currentState = serializeUndoableState(rootStore) | ||
const currentState = rootStore.serialize() | ||
const nextState = rootStore.historyStore.undo(currentState) | ||
if (nextState !== undefined) { | ||
restoreState(rootStore)(nextState) | ||
rootStore.restore(nextState) | ||
} | ||
} | ||
|
||
export const redo = (rootStore: RootStore) => () => { | ||
const currentState = serializeUndoableState(rootStore) | ||
const currentState = rootStore.serialize() | ||
const nextState = rootStore.historyStore.redo(currentState) | ||
if (nextState !== undefined) { | ||
restoreState(rootStore)(nextState) | ||
rootStore.restore(nextState) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
6f509d6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
signal – ./
signal.vercel.app
signal-ryohey.vercel.app
signal-git-main-ryohey.vercel.app