@@ -46,6 +46,7 @@ define(function HTMLDocumentModule(require, exports, module) {
4646
4747 var DocumentManager = require ( "document/DocumentManager" ) ,
4848 DOMAgent = require ( "LiveDevelopment/Agents/DOMAgent" ) ,
49+ EditorManager = require ( "editor/EditorManager" ) ,
4950 HighlightAgent = require ( "LiveDevelopment/Agents/HighlightAgent" ) ,
5051 HTMLInstrumentation = require ( "language/HTMLInstrumentation" ) ,
5152 Inspector = require ( "LiveDevelopment/Inspector/Inspector" ) ,
@@ -64,36 +65,26 @@ define(function HTMLDocumentModule(require, exports, module) {
6465 var self = this ;
6566
6667 this . doc = doc ;
67- if ( ! editor ) {
68- return ;
68+ if ( this . doc ) {
69+ this . doc . addRef ( ) ;
6970 }
71+
7072 this . editor = editor ;
7173 this . _instrumentationEnabled = false ;
7274
73- // Performance optimization to use closures instead of Function.bind()
74- // to improve responsiveness during cursor movement and keyboard events
75- $ ( this . editor ) . on ( "cursorActivity.HTMLDocument" , function ( event , editor ) {
76- self . _onCursorActivity ( event , editor ) ;
77- } ) ;
78-
79- $ ( this . editor ) . on ( "change.HTMLDocument" , function ( event , editor , change ) {
80- self . _onChange ( event , editor , change ) ;
81- } ) ;
75+ this . _onActiveEditorChange = this . _onActiveEditorChange . bind ( this ) ;
76+ $ ( EditorManager ) . on ( "activeEditorChange" , this . _onActiveEditorChange ) ;
8277
83- // Experimental code
84- if ( LiveDevelopment . config . experimental ) {
85- $ ( HighlightAgent ) . on ( "highlight.HTMLDocument" , function ( event , node ) {
86- self . _onHighlight ( event , node ) ;
87- } ) ;
88- }
78+ // Attach now
79+ this . attachToEditor ( editor ) ;
8980 } ;
9081
9182 /**
92- * Enable instrumented HTML
93- * @param enabled {boolean}
83+ * Enable or disable instrumented HTML
84+ * @param {boolean } enabled Whether to enable or disable
9485 */
9586 HTMLDocument . prototype . setInstrumentationEnabled = function setInstrumentationEnabled ( enabled ) {
96- if ( enabled && ! this . _instrumentationEnabled ) {
87+ if ( enabled && ! this . _instrumentationEnabled && this . editor ) {
9788 HTMLInstrumentation . scanDocument ( this . doc ) ;
9889 HTMLInstrumentation . _markText ( this . editor ) ;
9990 }
@@ -111,11 +102,12 @@ define(function HTMLDocumentModule(require, exports, module) {
111102
112103 /**
113104 * Returns a JSON object with HTTP response overrides
105+ * @param {boolean } enabled (Unused)
114106 * @return {{body: string} }
115107 */
116108 HTMLDocument . prototype . getResponseData = function getResponseData ( enabled ) {
117109 var body ;
118- if ( this . _instrumentationEnabled ) {
110+ if ( this . _instrumentationEnabled && this . editor ) {
119111 body = HTMLInstrumentation . generateInstrumentedHTML ( this . editor ) ;
120112 }
121113
@@ -124,13 +116,19 @@ define(function HTMLDocumentModule(require, exports, module) {
124116 } ;
125117 } ;
126118
127- /** Close the document */
119+ /**
120+ * Close the document
121+ */
128122 HTMLDocument . prototype . close = function close ( ) {
129- if ( ! this . editor ) {
130- return ;
123+ if ( this . editor ) {
124+ $ ( this . editor ) . off ( ".HTMLDocument" ) ;
131125 }
132126
133- $ ( this . editor ) . off ( ".HTMLDocument" ) ;
127+ if ( this . doc ) {
128+ this . doc . releaseRef ( ) ;
129+ }
130+
131+ $ ( EditorManager ) . off ( "activeEditorChange" , this . _onActiveEditorChange ) ;
134132
135133 // Experimental code
136134 if ( LiveDevelopment . config . experimental ) {
@@ -139,7 +137,53 @@ define(function HTMLDocumentModule(require, exports, module) {
139137 }
140138 } ;
141139
142- /** Update the highlight */
140+ /**
141+ * Attach new editor
142+ * @param {!Editor } editor The editor for this document
143+ */
144+ HTMLDocument . prototype . attachToEditor = function ( editor ) {
145+ var self = this ;
146+ this . editor = editor ;
147+
148+ // Performance optimization to use closures instead of Function.bind()
149+ // to improve responsiveness during cursor movement and keyboard events
150+ $ ( this . editor ) . on ( "cursorActivity.HTMLDocument" , function ( event , editor ) {
151+ self . _onCursorActivity ( event , editor ) ;
152+ } ) ;
153+
154+ $ ( this . editor ) . on ( "change.HTMLDocument" , function ( event , editor , change ) {
155+ self . _onChange ( event , editor , change ) ;
156+ } ) ;
157+
158+ // Experimental code
159+ if ( LiveDevelopment . config . experimental ) {
160+ $ ( HighlightAgent ) . on ( "highlight.HTMLDocument" , function ( event , node ) {
161+ self . _onHighlight ( event , node ) ;
162+ } ) ;
163+ }
164+
165+ if ( this . _instrumentationEnabled ) {
166+ // Update instrumentation for new editor
167+ HTMLInstrumentation . scanDocument ( this . doc ) ;
168+ HTMLInstrumentation . _markText ( this . editor ) ;
169+ }
170+ } ;
171+
172+ /**
173+ * Detach current editor
174+ */
175+ HTMLDocument . prototype . detachFromEditor = function ( ) {
176+ if ( this . editor ) {
177+ HighlightAgent . hide ( ) ;
178+ $ ( this . editor ) . off ( ".HTMLDocument" ) ;
179+ this . _removeHighlight ( ) ;
180+ this . editor = null ;
181+ }
182+ } ;
183+
184+ /**
185+ * Update the highlight
186+ */
143187 HTMLDocument . prototype . updateHighlight = function ( ) {
144188 var editor = this . editor ,
145189 codeMirror = editor . _codeMirror ,
@@ -165,9 +209,13 @@ define(function HTMLDocumentModule(require, exports, module) {
165209
166210 /** Event Handlers *******************************************************/
167211
168- /** Triggered on cursor activity by the editor */
212+ /**
213+ * Triggered on cursor activity by the editor
214+ * @param {$.Event } event Event
215+ * @param {!Editor } editor The editor for this document
216+ */
169217 HTMLDocument . prototype . _onCursorActivity = function ( event , editor ) {
170- if ( ! this . editor ) {
218+ if ( this . editor !== editor ) {
171219 return ;
172220 }
173221 this . updateHighlight ( ) ;
@@ -218,7 +266,12 @@ define(function HTMLDocumentModule(require, exports, module) {
218266 } ) ;
219267 } ;
220268
221- /** Triggered on change by the editor */
269+ /**
270+ * Triggered on change by the editor
271+ * @param {$.Event } event Event
272+ * @param {!Editor } editor The editor for this document
273+ * @param {Object } change CodeMirror editor change data
274+ */
222275 HTMLDocument . prototype . _onChange = function ( event , editor , change ) {
223276 // Make sure LiveHTML is turned on
224277 if ( ! this . _instrumentationEnabled ) {
@@ -294,29 +347,53 @@ define(function HTMLDocumentModule(require, exports, module) {
294347 // }
295348 } ;
296349
297- /** Triggered by the HighlightAgent to highlight a node in the editor */
350+ /**
351+ * Triggered when the active editor changes
352+ * @param {$.Event } event Event
353+ * @param {!Editor } newActive The new active editor
354+ * @param {!Editor } oldActive The old active editor
355+ */
356+ HTMLDocument . prototype . _onActiveEditorChange = function ( event , newActive , oldActive ) {
357+ this . detachFromEditor ( ) ;
358+
359+ if ( newActive && newActive . document === this . doc ) {
360+ this . attachToEditor ( newActive ) ;
361+ }
362+ } ;
363+
364+ /**
365+ * Triggered by the HighlightAgent to highlight a node in the editor
366+ * @param {$.Event } event Event
367+ * @param {DOMElement } node Element to highlight
368+ */
298369 HTMLDocument . prototype . _onHighlight = function ( event , node ) {
370+ this . _removeHighlight ( ) ;
299371 if ( ! node || ! node . location || ! this . editor ) {
300- if ( this . _highlight ) {
301- this . _highlight . clear ( ) ;
302- delete this . _highlight ;
303- }
304372 return ;
305373 }
374+
306375 var codeMirror = this . editor . _codeMirror ;
307376 var to , from = codeMirror . posFromIndex ( node . location ) ;
308377 if ( node . closeLocation ) {
309378 to = node . closeLocation + node . closeLength ;
310379 } else {
311380 to = node . location + node . length ;
312381 }
382+
313383 to = codeMirror . posFromIndex ( to ) ;
384+ this . _highlight = codeMirror . markText ( from , to , { className : "highlight" } ) ;
385+ } ;
386+
387+ /**
388+ * Remove all highlighting
389+ */
390+ HTMLDocument . prototype . _removeHighlight = function ( ) {
314391 if ( this . _highlight ) {
315392 this . _highlight . clear ( ) ;
393+ this . _highlight = null ;
316394 }
317- this . _highlight = codeMirror . markText ( from , to , { className : "highlight" } ) ;
318395 } ;
319396
320397 // Export the class
321398 module . exports = HTMLDocument ;
322- } ) ;
399+ } ) ;
0 commit comments