@@ -76,32 +76,7 @@ export default function init(inputIds, operations) {
7676 const suppliedText = document . getElementById ( 'asciiSuppliedText' ) . innerHTML ;
7777 const output = document . getElementById ( 'asciiContainerRow' ) ;
7878 const frameId = ( typeof FRAME_ID !== 'undefined' ) ? FRAME_ID : null ;
79- let syncedScrollPosition = 0 ;
80- let syncScrollPosition = ( ) => undefined ;
81-
82- if ( markdownContainerId && frameId ) {
83- syncScrollPosition = ( ) => {
84- setScrollPosition ( output , syncedScrollPosition ) ;
85- } ;
86-
87- window . addEventListener ( 'message' , ( event ) => {
88- const message = JSON . parse ( event . data ) ;
89- if ( message . tgt !== frameId || message . type !== 'input-scroll-position' || message . name !== markdownContainerId ) {
90- return ;
91- }
92- syncedScrollPosition = message . position ;
93- syncScrollPosition ( ) ;
94- } ) ;
95-
96- const registration = {
97- version : 'STACK-JS:1.6.0' ,
98- type : 'track-input-scroll' ,
99- name : markdownContainerId ,
100- 'limit-to-question' : true ,
101- src : frameId
102- } ;
103- window . parent . postMessage ( JSON . stringify ( registration ) , '*' ) ;
104- }
79+ const syncScrollPosition = createScrollSyncHandler ( markdownContainerId , frameId , output ) ;
10580
10681 // inputIds[1..N] correspond to each extractor's target answer input in order.
10782 const alloperations = operations ;
@@ -224,3 +199,44 @@ function setScrollPosition(element, position) {
224199 element . scrollTop = maxScroll > 0 ? position * maxScroll : 0 ;
225200 element . style . scrollBehavior = previousScrollBehavior ;
226201}
202+
203+ /**
204+ * Register iframe scroll syncing for the rendered ASCII output and return a
205+ * callback that reapplies the last known input scroll position.
206+ *
207+ * @param {?string } markdownContainerId textarea element id.
208+ * @param {?string } frameId parent frame id for postMessage coordination.
209+ * @param {HTMLElement } output rendered output container.
210+ * @returns {Function }
211+ */
212+ function createScrollSyncHandler ( markdownContainerId , frameId , output ) {
213+ // We don't have two containers so nothing to synch.
214+ if ( ! markdownContainerId || ! frameId ) {
215+ return ( ) => undefined ;
216+ }
217+
218+ let syncedScrollPosition = 0 ;
219+ const syncScrollPosition = ( ) => {
220+ setScrollPosition ( output , syncedScrollPosition ) ;
221+ } ;
222+
223+ window . addEventListener ( 'message' , ( event ) => {
224+ const message = JSON . parse ( event . data ) ;
225+ if ( message . tgt !== frameId || message . type !== 'input-scroll-position' || message . name !== markdownContainerId ) {
226+ return ;
227+ }
228+ syncedScrollPosition = message . position ;
229+ syncScrollPosition ( ) ;
230+ } ) ;
231+
232+ const registration = {
233+ version : 'STACK-JS:1.6.0' ,
234+ type : 'track-input-scroll' ,
235+ name : markdownContainerId ,
236+ 'limit-to-question' : true ,
237+ src : frameId
238+ } ;
239+ window . parent . postMessage ( JSON . stringify ( registration ) , '*' ) ;
240+
241+ return syncScrollPosition ;
242+ }
0 commit comments