forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLiveHTMLDocument.js
More file actions
1 lines (1 loc) · 5.64 KB
/
Copy pathLiveHTMLDocument.js
File metadata and controls
1 lines (1 loc) · 5.64 KB
1
define(function(require,exports,module){var EventDispatcher=require("utils/EventDispatcher"),PerfUtils=require("utils/PerfUtils"),_=require("thirdparty/lodash"),LiveDocument=require("LiveDevelopment/MultiBrowserImpl/documents/LiveDocument"),HTMLInstrumentation=require("LiveDevelopment/MultiBrowserImpl/language/HTMLInstrumentation"),HTMLUtils=require("language/HTMLUtils"),CSSUtils=require("language/CSSUtils");function LiveHTMLDocument(protocol,urlResolver,doc,editor){LiveDocument.apply(this,arguments),this.doc.addRef(),this._instrumentationEnabled=!1,this._relatedDocuments={stylesheets:{},scripts:{}},this._onChange=this._onChange.bind(this),this.doc.on("change",this._onChange),this._onDeleted=this._onDeleted.bind(this),this.doc.on("deleted",this._onDeleted),this._onRelated=this._onRelated.bind(this),this.protocol.on("DocumentRelated",this._onRelated),this._onStylesheetAdded=this._onStylesheetAdded.bind(this),this.protocol.on("StylesheetAdded",this._onStylesheetAdded),this._onStylesheetRemoved=this._onStylesheetRemoved.bind(this),this.protocol.on("StylesheetRemoved",this._onStylesheetRemoved),this._onScriptAdded=this._onScriptAdded.bind(this),this.protocol.on("ScriptAdded",this._onScriptAdded),this._onScriptRemoved=this._onScriptRemoved.bind(this),this.protocol.on("ScriptRemoved",this._onScriptRemoved)}function _isSamePos(position,line,char){return position.line===line&&position.ch===char}LiveHTMLDocument.prototype=Object.create(LiveDocument.prototype),LiveHTMLDocument.prototype.constructor=LiveHTMLDocument,LiveHTMLDocument.prototype.parentClass=LiveDocument.prototype,EventDispatcher.makeEventDispatcher(LiveHTMLDocument.prototype),LiveHTMLDocument.prototype.isLiveEditingEnabled=function(){return this._instrumentationEnabled},LiveHTMLDocument.prototype.setInstrumentationEnabled=function(enabled,force){this.editor&&(!enabled||!force&&this._instrumentationEnabled||(HTMLInstrumentation.scanDocument(this.doc),HTMLInstrumentation._markText(this.editor)),this._instrumentationEnabled=enabled)},LiveHTMLDocument.prototype.getResponseData=function(enabled){var body;return this._instrumentationEnabled&&(body=HTMLInstrumentation.generateInstrumentedHTML(this.editor,this.protocol.getRemoteScript())),body||(body=this.doc.getText(),this._instrumentationEnabled&&(body+=this.protocol.getRemoteScript())),{body:body}},LiveHTMLDocument.prototype.close=function(){this.doc.releaseRef(),this.doc.off("change",this._onChange),this.doc.off("deleted",this._onDeleted),this.protocol.off("DocumentRelated",this._onRelated),this.protocol.off("StylesheetAdded",this._onStylesheetAdded),this.protocol.off("StylesheetRemoved",this._onStylesheetRemoved),this.protocol.off("ScriptAdded",this._onScriptAdded),this.protocol.off("ScriptRemoved",this._onScriptRemoved),this.parentClass.close.call(this)},LiveHTMLDocument.prototype._onDeleted=function(event){this.close(),this.trigger("deleted",[this])},LiveHTMLDocument.prototype.updateHighlight=function(){if(this.editor&&this.isHighlightEnabled()){var editor=this.editor,mode=editor.getModeForSelection(),ids=[],selectors=[];if("css"===mode||"text/x-scss"===mode||"text/x-less"===mode){var primarySel=editor.getSelection(),tagInfo=HTMLUtils.getTagInfo(editor,primarySel.start,!0),isInlineStyle;if(!(tagInfo.position.tokenType===HTMLUtils.ATTR_VALUE&&"style"===tagInfo.attr.name.toLowerCase())&&(_.each(this.editor.getSelections(),function(sel){let selector=CSSUtils.findSelectorAtDocumentPos(editor,sel.reversed?sel.end:sel.start);selector&&selectors.push(selector)}),selectors.length))return void this.highlightRule(selectors.join(","))}_.each(this.editor.getSelections(),function(sel){var tagID=HTMLInstrumentation._getTagIDAtDocumentPos(editor,sel.reversed?sel.end:sel.start);-1!==tagID&&ids.push(tagID)}),ids.length?this.highlightDomElement(ids):this.hideHighlight()}},LiveHTMLDocument.prototype._compareWithBrowser=function(change){},LiveHTMLDocument.prototype._onChange=function(event,doc,change){if(this._instrumentationEnabled){var perfTimerName="LiveHTMLDocument applyDOMEdits",isNestedTimer=PerfUtils.isActive(perfTimerName);isNestedTimer||PerfUtils.markStart(perfTimerName);var self=this,result=HTMLInstrumentation.getUnappliedEditList(this.editor,change),applyEditsPromise;if(result.edits){let changedLineCount=1===change.length?change[0].to.line-change[0].from.line+1:null;changedLineCount&&changedLineCount>1&&changedLineCount===change[0].removed.length&&_isSamePos(change[0].from,0,0)?(this.protocol.reload(),PerfUtils.addMeasurement(perfTimerName)):(applyEditsPromise=this.protocol.evaluate("_LD.applyDOMEdits("+JSON.stringify(result.edits)+")")).always(function(){isNestedTimer||PerfUtils.addMeasurement(perfTimerName)})}this.errors=result.errors||[],this._updateErrorDisplay(),this._debug&&(console.log("Edits applied to browser were:"),console.log(JSON.stringify(result.edits,null,2)),applyEditsPromise.done(function(){self._compareWithBrowser(change)}))}},LiveHTMLDocument.prototype._onRelated=function(event,msg){this._relatedDocuments=msg.related},LiveHTMLDocument.prototype._onStylesheetAdded=function(event,msg){this._relatedDocuments.stylesheets[msg.href]=!0},LiveHTMLDocument.prototype._onStylesheetRemoved=function(event,msg){delete this._relatedDocuments.stylesheets[msg.href]},LiveHTMLDocument.prototype._onScriptAdded=function(event,msg){this._relatedDocuments.scripts[msg.src]=!0},LiveHTMLDocument.prototype._onScriptRemoved=function(event,msg){delete this._relatedDocuments.scripts[msg.src]},LiveHTMLDocument.prototype.isRelated=function(fullPath){return this._relatedDocuments.scripts[this.urlResolver(fullPath)]||this._relatedDocuments.stylesheets[this.urlResolver(fullPath)]},LiveHTMLDocument.prototype.getRelated=function(){return this._relatedDocuments},module.exports=LiveHTMLDocument});