Repository navigation
Expand file tree
/
Copy pathLiveDocument.js
More file actions
1 lines (1 loc) · 5.16 KB
/
Copy pathLiveDocument.js
File metadata and controls
1 lines (1 loc) · 5.16 KB
1
define(function(require,exports,module){const CONSTANTS=require("LiveDevelopment/LivePreviewConstants"),EditorManager=require("editor/EditorManager"),EventDispatcher=require("utils/EventDispatcher"),PreferencesManager=require("preferences/PreferencesManager"),_=require("thirdparty/lodash");var SYNC_ERROR_CLASS="live-preview-sync-error";const CURSOR_HIGHLIGHT_DEBOUNCE_MS=80,HELD_HIGHLIGHT_PREFIX="held:";function _simpleHash(str){let hash=5381;for(let i=0;i<str.length;)hash=33*hash^str.charCodeAt(i++);return hash>>>0}function LiveDocument(protocol,urlResolver,doc,editor,roots){this.protocol=protocol,this.urlResolver=urlResolver,this.doc=doc,this.roots=roots||[],this._onActiveEditorChange=this._onActiveEditorChange.bind(this),this._onCursorActivity=this._onCursorActivity.bind(this),this.fileHashForEvents=_simpleHash(this.doc.file.fullPath),EditorManager.off(`activeEditorChange.LiveDocument-${this.fileHashForEvents}`),EditorManager.on(`activeEditorChange.LiveDocument-${this.fileHashForEvents}`,this._onActiveEditorChange),editor&&this._attachToEditor(editor)}EventDispatcher.makeEventDispatcher(LiveDocument.prototype),LiveDocument.prototype.close=function(){EditorManager.off(`activeEditorChange.LiveDocument-${this.fileHashForEvents}`),this._clearErrorDisplay(),this._detachFromEditor()},LiveDocument.prototype.isLiveEditingEnabled=function(){return!1},LiveDocument.prototype.setInstrumentationEnabled=function(enabled){},LiveDocument.prototype.getResponseData=function(enabled){return{body:this.doc.getText()}},LiveDocument.prototype._onActiveEditorChange=function(event,newActive,oldActive){this._detachFromEditor(),newActive&&newActive.document.file.fullPath===this.doc.file.fullPath&&this._attachToEditor(newActive)},LiveDocument.prototype._attachToEditor=function(editor){this.editor=editor,this.editor&&(this.setInstrumentationEnabled(!0,!0),this.editor.off("cursorActivity",this._onCursorActivity),this.editor.on("cursorActivity",this._onCursorActivity),this.updateHighlight())},LiveDocument.prototype._detachFromEditor=function(){this.editor&&(this._cancelPendingHighlight(),this.hideHighlight(),this.editor.off("cursorActivity",this._onCursorActivity))};let _disableHighlightOnCursor=!1,_cursorHighlightGeneration=0,_selectionHolder=null;function _isSelectionHeld(liveDoc){return!!_selectionHolder&&!0===_selectionHolder(liveDoc)}LiveDocument.setSelectionHolder=function(holder){_selectionHolder=holder||null},LiveDocument.CURSOR_HIGHLIGHT_DEBOUNCE_MS=80,LiveDocument.prototype.disableHighlightOnCursorActivity=function(shouldDisable){_disableHighlightOnCursor=shouldDisable,shouldDisable&&(_cursorHighlightGeneration++,this._cancelPendingHighlight())},LiveDocument.prototype.isCursorHighlightDisabled=function(){return _disableHighlightOnCursor},LiveDocument.prototype._onCursorActivity=function(event,editor){if(this._cancelPendingHighlight(),!this.editor||_disableHighlightOnCursor)return;const self=this,generation=_cursorHighlightGeneration;this._highlightTimer=window.setTimeout(function(){self._highlightTimer=null,self.editor&&!_disableHighlightOnCursor&&generation===_cursorHighlightGeneration&&self.updateHighlight()},80)},LiveDocument.prototype._cancelPendingHighlight=function(){this._highlightTimer&&(window.clearTimeout(this._highlightTimer),this._highlightTimer=null)},LiveDocument.prototype._updateErrorDisplay=function(){var self=this,startLine,endLine,i,lineHandle;this.editor&&(this.editor._codeMirror.operation(function(){self._clearErrorDisplay(),self._errorLineHandles=self._errorLineHandles||[],self.errors.forEach(function(error){for(startLine=error.startPos.line,endLine=error.endPos.line,i=startLine;i<endLine+1;i++)lineHandle=self.editor._codeMirror.addLineClass(i,"wrap",SYNC_ERROR_CLASS),self._errorLineHandles.push(lineHandle)})}),this.trigger("errorStatusChanged",!!this.errors.length))},LiveDocument.prototype._clearErrorDisplay=function(){var self=this,lineHandle;this.editor&&this._errorLineHandles&&this._errorLineHandles.length&&this.editor._codeMirror.operation(function(){for(;lineHandle=self._errorLineHandles.pop();)self.editor._codeMirror.removeLineClass(lineHandle,"wrap",SYNC_ERROR_CLASS)})},LiveDocument.prototype.isHighlightEnabled=function(){return PreferencesManager.get(CONSTANTS.PREFERENCE_LIVE_PREVIEW_MODE)!==CONSTANTS.LIVE_PREVIEW_MODE},LiveDocument.prototype.updateHighlight=function(){},LiveDocument.prototype.hideHighlight=function(temporary){temporary||(this._lastHighlight=null),this.protocol.evaluate("_LD.hideHighlight("+_isSelectionHeld(this)+")")},LiveDocument.prototype.highlightRule=function(name){const keepSelection=_isSelectionHeld(this),highlight=(keepSelection?"held:":"")+name;this._lastHighlight!==highlight&&(this._lastHighlight=highlight,this.protocol.evaluate("_LD.highlightRule("+JSON.stringify(name)+", "+keepSelection+")").fail(()=>{this._lastHighlight===highlight&&(this._lastHighlight=null)}))},LiveDocument.prototype.highlightDomElement=function(ids){var selector="";Array.isArray(ids)||(ids=[ids]),_.each(ids,function(id){""!==selector&&(selector+=","),selector+="[data-brackets-id='"+id+"']"}),this.highlightRule(selector)},LiveDocument.prototype.redrawHighlights=function(){this.isHighlightEnabled()&&this.protocol.evaluate("_LD.redrawHighlights()")},module.exports=LiveDocument});