@@ -74,7 +74,7 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
74
74
public _complete_request ?: number ;
75
75
public store : JupyterStore ;
76
76
public syncdb : SyncDB ;
77
- private labels : { [ label : string ] : { tag : string ; id : string } } = { } ;
77
+ private labels ? : { [ label : string ] : { tag : string ; id : string } } ;
78
78
79
79
public _init (
80
80
project_id : string ,
@@ -381,7 +381,7 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
381
381
// Might throw a CellWriteProtectedException
382
382
public set_cell_input ( id : string , input : string , save = true ) : void {
383
383
if ( ! this . store ) return ;
384
- if ( this . store . getIn ( [ "cells " , id , "input" ] ) == input ) {
384
+ if ( this . store . getIn ( [ "this.st " , id , "input" ] ) == input ) {
385
385
// nothing changed. Note, I tested doing the above check using
386
386
// both this.syncdb and this.store, and this.store is orders of magnitude faster.
387
387
return ;
@@ -2729,12 +2729,41 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
2729
2729
} ;
2730
2730
2731
2731
processRenderedMarkdown = ( { value, id } : { value : string ; id : string } ) => {
2732
- const labels = this . labels ;
2733
2732
const labelRegExp = / \s * \\ l a b e l \{ .* ?\} \s * / g;
2733
+ if ( this . labels == null ) {
2734
+ const labels = ( this . labels = { } ) ;
2735
+ // do initial full document scan
2736
+ if ( this . store == null ) {
2737
+ return ;
2738
+ }
2739
+ const cells = this . store . get ( "cells" ) ;
2740
+ if ( cells == null ) {
2741
+ return ;
2742
+ }
2743
+ let n = 0 ;
2744
+ for ( const id of this . store . get_cell_ids_list ( ) ) {
2745
+ const cell = cells . get ( id ) ;
2746
+ if ( cell ?. get ( "cell_type" ) == "markdown" ) {
2747
+ const value = cell . get ( "input" ) ?? "" ;
2748
+ value . replace ( labelRegExp , ( labelContent ) => {
2749
+ const label = extractLabel ( labelContent ) ;
2750
+ n += 1 ;
2751
+ labels [ label ] = { tag : `${ n } ` , id } ;
2752
+ } ) ;
2753
+ }
2754
+ }
2755
+ }
2756
+ const labels = this . labels ;
2757
+ if ( labels == null ) {
2758
+ throw Error ( "bug" ) ;
2759
+ }
2734
2760
const noLabels = value . replace ( labelRegExp , ( labelContent ) => {
2735
2761
const label = extractLabel ( labelContent ) ;
2736
2762
if ( labels [ label ] == null ) {
2737
2763
labels [ label ] = { tag : `${ misc . len ( labels ) + 1 } ` , id } ;
2764
+ } else {
2765
+ // in case it moved to a different cell due to cut/paste
2766
+ labels [ label ] . id = id ;
2738
2767
}
2739
2768
return `\\tag{${ labels [ label ] . tag } }` ;
2740
2769
} ) ;
0 commit comments