@@ -27,17 +27,33 @@ export default function SourceEditor() {
2727 ` . replace ( / ^ \s + / gm, '' ) ;
2828 let savedGeneration ;
2929 let prevMode = NaN ;
30+ /** @type {MozSectionFinder } */
31+ let sectionFinder ;
32+ let sectionWidget ;
3033
3134 $$remove ( '.sectioned-only' ) ;
3235 $id ( 'header' ) . on ( 'wheel' , headerOnScroll ) ;
3336 $id ( 'sections' ) . textContent = '' ;
3437 $id ( 'sections' ) . appendChild ( $create ( '.single-editor' ) ) ;
3538 $id ( 'save-button' ) . on ( 'split-btn' , saveTemplate ) ;
3639
37- const cm = cmFactory . create ( $ ( '.single-editor' ) ) ;
40+ const cm = cmFactory . create ( $ ( '.single-editor' ) , {
41+ value : style . id ? style . sourceCode : setupNewStyle ( editor . template ) ,
42+ finishInit ( me ) {
43+ const si = editor . applyScrollInfo ( me ) ;
44+ editor . viewTo = si . viewTo ;
45+ sectionFinder = MozSectionFinder ( me ) ;
46+ sectionWidget = MozSectionWidget ( me , sectionFinder ) ;
47+ prefs . subscribe ( 'editor.linter' , updateLinterSwitch , true ) ;
48+ prefs . subscribe ( 'editor.appliesToLineWidget' ,
49+ ( k , val ) => sectionWidget . toggle ( val ) , true ) ;
50+ prefs . subscribe ( 'editor.toc.expanded' ,
51+ ( k , val ) => sectionFinder . onOff ( editor . updateToc , val ) , true ) ;
52+ Object . assign ( me . curOp , si . scroll ) ;
53+ editor . viewTo = 0 ;
54+ } ,
55+ } ) ;
3856 const cmpPos = CodeMirror . cmpPos ;
39- const sectionFinder = MozSectionFinder ( cm ) ;
40- const sectionWidget = MozSectionWidget ( cm , sectionFinder ) ;
4157 const metaCompiler = createMetaCompiler ( meta => {
4258 const { vars} = style [ UCD ] || { } ;
4359 if ( vars ) {
@@ -53,7 +69,6 @@ export default function SourceEditor() {
5369 style . url = meta . homepageURL || style . installationUrl ;
5470 updateMeta ( ) ;
5571 } ) ;
56- if ( ! style . id ) setupNewStyle ( editor . template ) ;
5772 updateMeta ( ) ;
5873
5974 /** @namespace Editor */
@@ -110,17 +125,6 @@ export default function SourceEditor() {
110125 scrollToEditor : ( ) => { } ,
111126 } ) ;
112127
113- prefs . subscribe ( 'editor.linter' , updateLinterSwitch , true ) ;
114- prefs . subscribe ( 'editor.appliesToLineWidget' ,
115- ( k , val ) => sectionWidget . toggle ( val ) , true ) ;
116- prefs . subscribe ( 'editor.toc.expanded' ,
117- ( k , val ) => sectionFinder . onOff ( editor . updateToc , val ) , true ) ;
118-
119- if ( style . id ) {
120- cm . setValue ( style . sourceCode ) ;
121- cm . clearHistory ( ) ;
122- cm . markClean ( ) ;
123- }
124128 savedGeneration = cm . changeGeneration ( ) ;
125129 cm . on ( 'changes' , ( _ , changes ) => {
126130 dirty . modify ( 'sourceGeneration' , savedGeneration , cm . changeGeneration ( ) ) ;
@@ -139,7 +143,6 @@ export default function SourceEditor() {
139143 if ( ! $isTextInput ( document . activeElement ) ) {
140144 cm . focus ( ) ;
141145 }
142- editor . applyScrollInfo ( cm ) ; // WARNING! Place it after all cm.XXX calls that change scroll pos
143146
144147 /** Shows the console.log output from the background worker stored in `log` property */
145148 function showLog ( log ) {
@@ -183,19 +186,14 @@ export default function SourceEditor() {
183186 if ( Object . keys ( sec0 ) . length === 1 ) { // the only key is 'code'
184187 sec0 . domains = [ 'example.com' ] ;
185188 }
186- style . sourceCode = ( tpl || DEFAULT_TEMPLATE )
189+ return ( style . sourceCode = ( tpl || DEFAULT_TEMPLATE )
187190 . replace ( / ( @ n a m e ) (?: ( [ \t \x20 ] + ) .* | \n ) / , ( _ , k , space ) => `${ k } ${ space || ' ' } ${ style . name } ` )
188191 . replace ( / \s * @ - m o z - d o c u m e n t [ ^ { ] * { ( [ ^ } ] * ) } \s * $ / g, // stripping dummy sections
189192 ( s , body ) => body . trim ( ) === comment ? '\n\n' : s )
190193 . trim ( ) +
191194 '\n\n' +
192- styleToCss ( style ) ;
193- cm . startOperation ( ) ;
194- cm . setValue ( style . sourceCode ) ;
195- cm . clearHistory ( ) ;
196- cm . markClean ( ) ;
197- cm . endOperation ( ) ;
198- dirty . clear ( 'sourceGeneration' ) ;
195+ styleToCss ( style )
196+ ) ;
199197 }
200198
201199 function updateMeta ( ) {
0 commit comments