@@ -111,6 +111,8 @@ mathWorker.onmessage = function (oEvent) {
111111 case "math" :
112112 out . text . forEach ( e => {
113113 const pre = document . createElement ( "pre" ) ;
114+ pre . setAttribute ( 'data-from-line' , e . from ) ;
115+ pre . setAttribute ( 'data-to-line' , e . to ) ;
114116 if ( e . visible ) {
115117 const type = e . type ;
116118 const value = e . result ;
@@ -119,29 +121,21 @@ mathWorker.onmessage = function (oEvent) {
119121 case "string" :
120122 div = document . createElement ( "code" ) ;
121123 div . innerHTML = value ;
122- div . setAttribute ( 'data-from-line' , e . from )
123- div . setAttribute ( 'data-to-line' , e . to )
124124 pre . appendChild ( div ) ;
125125 break ;
126126 case "any" :
127127 div = document . createElement ( "div" ) ;
128128 div . textContent = value ;
129- div . setAttribute ( 'data-from-line' , e . from )
130- div . setAttribute ( 'data-to-line' , e . to )
131129 pre . appendChild ( div ) ;
132130 break ;
133131 case "error" :
134132 div = document . createElement ( "div" ) ;
135133 div . style . color = "red" ;
136134 div . innerHTML = value ;
137- div . setAttribute ( 'data-from-line' , e . from )
138- div . setAttribute ( 'data-to-line' , e . to )
139135 pre . appendChild ( div ) ;
140136 break ;
141137 case "plot" :
142138 div = document . createElement ( "div" ) ;
143- div . setAttribute ( 'data-from-line' , e . from )
144- div . setAttribute ( 'data-to-line' , e . to )
145139 try {
146140 Plotly . newPlot ( div , e . result . data , e . result . layout , e . result . config )
147141 } catch ( error ) {
@@ -150,8 +144,10 @@ mathWorker.onmessage = function (oEvent) {
150144 pre . appendChild ( div ) ;
151145 break ;
152146 }
153- outputs . appendChild ( pre ) ;
147+ } else {
148+ pre . style . display = 'none'
154149 }
150+ outputs . appendChild ( pre ) ;
155151 } ) ;
156152 break ;
157153 case "markdown" :
@@ -163,6 +159,7 @@ mathWorker.onmessage = function (oEvent) {
163159 break ;
164160 }
165161 } ) ;
162+ updateSelection ( )
166163 clearTimeout ( timerSave ) ;
167164 sessions [ lastTab ] = editor . state
168165 timerSave = setTimeout ( saveSession , waitToSave , tabToSave )
@@ -187,12 +184,42 @@ function createState(ID) {
187184 const text = update . state . doc . toString ( )
188185 clearTimeout ( timer ) ;
189186 timer = setTimeout ( sendWorkToMathWorker , wait , text ) ;
187+ } else if ( update . selectionSet ) {
188+ updateSelection ( )
190189 }
191190 } )
192191 ]
193192 } )
194193}
195194
195+ function updateSelection ( ) {
196+ const selectedFrom = editor . state . doc . lineAt (
197+ editor . state . selection . ranges [ editor . state . selection . mainIndex ] . from
198+ ) . number - 1 ;
199+
200+ const selectedTo = editor . state . doc . lineAt (
201+ editor . state . selection . ranges [ editor . state . selection . mainIndex ] . to
202+ ) . number - 1 ;
203+
204+ const outputs = document . querySelector ( '#OUTPUT' ) . childNodes ;
205+
206+ outputs . forEach ( code => {
207+ const thisNode = code ;
208+ const fromLine = parseInt ( thisNode . getAttribute ( 'data-from-line' ) , 10 ) ;
209+ const toLine = parseInt ( thisNode . getAttribute ( 'data-to-line' ) , 10 ) ;
210+ if (
211+ ( fromLine >= selectedFrom ) && ( fromLine <= selectedTo )
212+ ||
213+ ( toLine >= selectedFrom ) && ( toLine <= selectedTo )
214+ ) {
215+ code . classList . add ( 'highlight' ) ;
216+ code . scrollIntoView ( { block : 'nearest' , inline : 'start' } ) ;
217+ } else {
218+ code . classList . remove ( 'highlight' ) ;
219+ }
220+ } ) ;
221+ }
222+
196223function getSessionName ( ID ) {
197224 return 'localSession' + ID
198225}
@@ -233,7 +260,7 @@ function sendWorkToMathWorker(mathExpressoins) {
233260 if ( mathExpressoins != "" ) {
234261 const expressions = mathExpressoins
235262 . replace ( / \r ? \n / g, '\n' )
236- . trim ( )
263+ // .trim()
237264 const request = { expr : expressions }
238265 mathWorker . postMessage ( JSON . stringify ( request ) )
239266 }
0 commit comments