@@ -10,7 +10,7 @@ import React, {
1010import { shallowEqual } from 'react-redux' ;
1111import { MusicNotation } from '../MusicNotation' ;
1212import { StepTimeController } from 'MemoryFlashCore/src/lib/stepTimeController' ;
13- import { scoreToQuestion } from 'MemoryFlashCore/src/lib/scoreBuilder' ;
13+ import { questionToScore , scoreToQuestion } from 'MemoryFlashCore/src/lib/scoreBuilder' ;
1414import { Duration , BaseDuration } from 'MemoryFlashCore/src/lib/measure' ;
1515import { StaffEnum } from 'MemoryFlashCore/src/types/Cards' ;
1616import { SheetNote , MultiSheetQuestion } from 'MemoryFlashCore/src/types/MultiSheetCard' ;
@@ -66,8 +66,20 @@ const isFull = (score: Score) => {
6666 } ) ;
6767} ;
6868
69- function useStepCtrl ( keySig : string , resetSignal : number , notify : ScoreChangeHandler ) {
70- const ctrlRef = useRef ( new StepTimeController ( ) ) ;
69+ function useStepCtrl (
70+ keySig : string ,
71+ resetSignal : number ,
72+ notify : ScoreChangeHandler ,
73+ initialQuestion ?: MultiSheetQuestion ,
74+ ) {
75+ const controllerFactory = useMemo (
76+ ( ) => ( ) =>
77+ new StepTimeController (
78+ initialQuestion ? questionToScore ( initialQuestion ) : new Score ( ) ,
79+ ) ,
80+ [ initialQuestion ] ,
81+ ) ;
82+ const ctrlRef = useRef ( controllerFactory ( ) ) ;
7183 const [ dur , setDurState ] = useState < BaseDuration > ( 'q' ) ;
7284 const [ dotted , setDotted ] = useState ( false ) ;
7385 const [ extraDurations , setExtraDurations ] = useState < Duration [ ] > ( [ ] ) ;
@@ -80,7 +92,7 @@ function useStepCtrl(keySig: string, resetSignal: number, notify: ScoreChangeHan
8092 const midi = useAppSelector ( ( s ) => s . midi . notes . map ( ( n ) => n . number ) , shallowEqual ) ;
8193 const prev = useRef < number [ ] > ( [ ] ) ;
8294 const maxChord = useRef < number [ ] > ( [ ] ) ;
83- const [ question , setQuestion ] = useState ( ( ) => scoreToQuestion ( new Score ( ) , keySig ) ) ;
95+ const [ question , setQuestion ] = useState ( ( ) => scoreToQuestion ( ctrlRef . current . score , keySig ) ) ;
8496
8597 const emit = useCallback ( ( ) => {
8698 const ctrl = ctrlRef . current ;
@@ -144,6 +156,15 @@ function useStepCtrl(keySig: string, resetSignal: number, notify: ScoreChangeHan
144156 emitRef . current ( ) ;
145157 } , [ resetSignal ] ) ;
146158
159+ useEffect ( ( ) => {
160+ ctrlRef . current = controllerFactory ( ) ;
161+ applyDurRef . current ( ) ;
162+ ctrlRef . current . setStaff ( staffRef . current ) ;
163+ maxChord . current = [ ] ;
164+ prev . current = [ ] ;
165+ emitRef . current ( ) ;
166+ } , [ controllerFactory ] ) ;
167+
147168 useEffect ( ( ) => {
148169 applyDur ( ) ;
149170 emit ( ) ;
@@ -199,16 +220,18 @@ interface ProviderProps {
199220 keySig : string ;
200221 resetSignal : number ;
201222 onChange : ScoreChangeHandler ;
223+ initialQuestion ?: MultiSheetQuestion ;
202224 children : React . ReactNode ;
203225}
204226
205227export const ScoreEditorProvider : React . FC < ProviderProps > = ( {
206228 keySig,
207229 resetSignal,
208230 onChange,
231+ initialQuestion,
209232 children,
210233} ) => {
211- const value = useStepCtrl ( keySig , resetSignal , onChange ) ;
234+ const value = useStepCtrl ( keySig , resetSignal , onChange , initialQuestion ) ;
212235 return < ScoreEditorContext . Provider value = { value } > { children } </ ScoreEditorContext . Provider > ;
213236} ;
214237
0 commit comments