@@ -204,10 +204,10 @@ export default function MultiMarkdownInput(props: Props) {
204
204
205
205
const editBar2 = useRef < JSX . Element | undefined > ( undefined ) ;
206
206
207
+ const getKey = ( ) => `${ project_id } ${ path } :${ cacheId } ` ;
208
+
207
209
function getCache ( ) {
208
- return cacheId === undefined
209
- ? undefined
210
- : multimodeStateCache . get ( `${ project_id } ${ path } :${ cacheId } ` ) ;
210
+ return cacheId == null ? undefined : multimodeStateCache . get ( getKey ( ) ) ;
211
211
}
212
212
213
213
const [ mode , setMode0 ] = useState < Mode > (
@@ -248,23 +248,34 @@ export default function MultiMarkdownInput(props: Props) {
248
248
} | null > ( null ) ;
249
249
250
250
useEffect ( ( ) => {
251
- if ( cacheId == null ) return ;
251
+ if ( cacheId == null ) {
252
+ return ;
253
+ }
252
254
const cache = getCache ( ) ;
253
255
if ( cache ?. [ mode ] != null && selectionRef . current != null ) {
254
256
// restore selection on mount.
255
257
try {
256
258
selectionRef . current . setSelection ( cache ?. [ mode ] ) ;
257
259
} catch ( _err ) {
258
- // console.warn(_err); // definitely don't need this.
259
- // This is expected to fail, since the selection from last
260
- // use will be invalid now if another user changed the
261
- // document, etc., or you did in a different mode, possibly.
260
+ // it might just be that the document isn't initialized yet
261
+ setTimeout ( ( ) => {
262
+ try {
263
+ selectionRef . current ?. setSelection ( cache ?. [ mode ] ) ;
264
+ } catch ( _err2 ) {
265
+ // console.warn(_err2); // definitely don't need this.
266
+ // This is expected to fail, since the selection from last
267
+ // use will be invalid now if another user changed the
268
+ // document, etc., or you did in a different mode, possibly.
269
+ }
270
+ } , 100 ) ;
262
271
}
263
272
}
264
273
return ( ) => {
265
- if ( selectionRef . current == null || cacheId == null ) return ;
274
+ if ( selectionRef . current == null || cacheId == null ) {
275
+ return ;
276
+ }
266
277
const selection = selectionRef . current . getSelection ( ) ;
267
- multimodeStateCache . set ( ` ${ project_id } ${ path } : ${ cacheId } ` , {
278
+ multimodeStateCache . set ( getKey ( ) , {
268
279
...getCache ( ) ,
269
280
[ mode ] : selection ,
270
281
} ) ;
0 commit comments