-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Fix Live Preview for Docs not in Working Set #8605
Changes from 1 commit
e02e71d
ba79595
1f799cf
15285fa
e058969
00fa553
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ define(function HTMLDocumentModule(require, exports, module) { | |
|
||
var DocumentManager = require("document/DocumentManager"), | ||
DOMAgent = require("LiveDevelopment/Agents/DOMAgent"), | ||
EditorManager = require("editor/EditorManager"), | ||
HighlightAgent = require("LiveDevelopment/Agents/HighlightAgent"), | ||
HTMLInstrumentation = require("language/HTMLInstrumentation"), | ||
Inspector = require("LiveDevelopment/Inspector/Inspector"), | ||
|
@@ -64,36 +65,26 @@ define(function HTMLDocumentModule(require, exports, module) { | |
var self = this; | ||
|
||
this.doc = doc; | ||
if (!editor) { | ||
return; | ||
if (this.doc) { | ||
this.doc.addRef(); | ||
} | ||
|
||
this.editor = editor; | ||
this._instrumentationEnabled = false; | ||
|
||
// Performance optimization to use closures instead of Function.bind() | ||
// to improve responsiveness during cursor movement and keyboard events | ||
$(this.editor).on("cursorActivity.HTMLDocument", function (event, editor) { | ||
self._onCursorActivity(event, editor); | ||
}); | ||
|
||
$(this.editor).on("change.HTMLDocument", function (event, editor, change) { | ||
self._onChange(event, editor, change); | ||
}); | ||
this.onActiveEditorChange = this.onActiveEditorChange.bind(this); | ||
$(EditorManager).on("activeEditorChange", this.onActiveEditorChange); | ||
|
||
// Experimental code | ||
if (LiveDevelopment.config.experimental) { | ||
$(HighlightAgent).on("highlight.HTMLDocument", function (event, node) { | ||
self._onHighlight(event, node); | ||
}); | ||
} | ||
// Attach now | ||
this.attachToEditor(editor); | ||
}; | ||
|
||
/** | ||
* Enable instrumented HTML | ||
* @param enabled {boolean} | ||
*/ | ||
HTMLDocument.prototype.setInstrumentationEnabled = function setInstrumentationEnabled(enabled) { | ||
if (enabled && !this._instrumentationEnabled) { | ||
if (enabled && !this._instrumentationEnabled && this.editor) { | ||
HTMLInstrumentation.scanDocument(this.doc); | ||
HTMLInstrumentation._markText(this.editor); | ||
} | ||
|
@@ -115,7 +106,7 @@ define(function HTMLDocumentModule(require, exports, module) { | |
*/ | ||
HTMLDocument.prototype.getResponseData = function getResponseData(enabled) { | ||
var body; | ||
if (this._instrumentationEnabled) { | ||
if (this._instrumentationEnabled && this.editor) { | ||
body = HTMLInstrumentation.generateInstrumentedHTML(this.editor); | ||
} | ||
|
||
|
@@ -126,11 +117,15 @@ define(function HTMLDocumentModule(require, exports, module) { | |
|
||
/** Close the document */ | ||
HTMLDocument.prototype.close = function close() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good time to add some JSDoc here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. n/m i guess this is fully documented :) |
||
if (!this.editor) { | ||
return; | ||
if (this.editor) { | ||
$(this.editor).off(".HTMLDocument"); | ||
} | ||
|
||
if (this.doc) { | ||
this.doc.releaseRef(); | ||
} | ||
|
||
$(this.editor).off(".HTMLDocument"); | ||
$(EditorManager).off("activeEditorChange", this.onActiveEditorChange); | ||
|
||
// Experimental code | ||
if (LiveDevelopment.config.experimental) { | ||
|
@@ -139,8 +134,53 @@ define(function HTMLDocumentModule(require, exports, module) { | |
} | ||
}; | ||
|
||
/** Attach new editor */ | ||
HTMLDocument.prototype.attachToEditor = function (editor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs JSDoc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. FYI, I made a few other updates to to jsdocs beyond the places you mentioned. |
||
var self = this; | ||
this.editor = editor; | ||
|
||
if (this.editor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this check from all the the ctor to here because it seemed safer to always do it here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you still don't need it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
// Performance optimization to use closures instead of Function.bind() | ||
// to improve responsiveness during cursor movement and keyboard events | ||
$(this.editor).on("cursorActivity.HTMLDocument", function (event, editor) { | ||
self._onCursorActivity(event, editor); | ||
}); | ||
|
||
$(this.editor).on("change.HTMLDocument", function (event, editor, change) { | ||
self._onChange(event, editor, change); | ||
}); | ||
|
||
// Experimental code | ||
if (LiveDevelopment.config.experimental) { | ||
$(HighlightAgent).on("highlight.HTMLDocument", function (event, node) { | ||
self._onHighlight(event, node); | ||
}); | ||
} | ||
|
||
if (this._instrumentationEnabled) { | ||
// Update instrumentation for new editor | ||
HTMLInstrumentation.scanDocument(this.doc); | ||
HTMLInstrumentation._markText(this.editor); | ||
} | ||
} | ||
}; | ||
|
||
/** Detach current editor */ | ||
HTMLDocument.prototype.detachFromEditor = function () { | ||
if (this.editor) { | ||
HighlightAgent.hide(); | ||
$(this.editor).off(".HTMLDocument"); | ||
this._onHighlight(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we shouldn't call a handler without event data, you should move the top of the event handler to a new function here's the top of if (!node || !node.location || !this.editor) {
this._removeHighlight();
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea -- much cleaner. |
||
this.editor = null; | ||
} | ||
}; | ||
|
||
/** Update the highlight */ | ||
HTMLDocument.prototype.updateHighlight = function () { | ||
if (!this.editor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you shouldn't need this. It will not get called unless there is an editor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
return; | ||
} | ||
|
||
var editor = this.editor, | ||
codeMirror = editor._codeMirror, | ||
ids = []; | ||
|
@@ -294,6 +334,15 @@ define(function HTMLDocumentModule(require, exports, module) { | |
// } | ||
}; | ||
|
||
/** Triggered when the active editor changes */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to keep all the 2 code blocks that are commented out in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not my old code, so I left it in. |
||
HTMLDocument.prototype.onActiveEditorChange = function (event, newActive, oldActive) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs JSDoc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this up above the private handlers for better organization There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also private, to renamed to |
||
this.detachFromEditor(); | ||
|
||
if (newActive && newActive.document === this.doc) { | ||
this.attachToEditor(newActive); | ||
} | ||
}; | ||
|
||
/** Triggered by the HighlightAgent to highlight a node in the editor */ | ||
HTMLDocument.prototype._onHighlight = function (event, node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should clean this up with some JSDoc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
if (!node || !node.location || !this.editor) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!