forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInlineTextEditor.js
More file actions
1 lines (1 loc) · 5.53 KB
/
InlineTextEditor.js
File metadata and controls
1 lines (1 loc) · 5.53 KB
1
define(function(require,exports,module){var CodeMirror=require("thirdparty/CodeMirror/lib/codemirror"),EventDispatcher=require("utils/EventDispatcher"),DocumentManager=require("document/DocumentManager"),EditorManager=require("editor/EditorManager"),CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),InlineWidget=require("editor/InlineWidget").InlineWidget,KeyEvent=require("utils/KeyEvent");function _showDirtyIndicator($indicatorDiv,isDirty){$indicatorDiv.css("width",isDirty?16:0)}function _dirtyFlagChangeHandler(event,doc){var $dirtyIndicators,$indicator;$(".inline-text-editor .dirty-indicator").each(function(index,indicator){($indicator=$(this)).data("fullPath")===doc.file.fullPath&&_showDirtyIndicator($indicator,doc.isDirty)})}function InlineTextEditor(){InlineWidget.call(this),this.editor=null,this.handleKeyDown=this.handleKeyDown.bind(this),this.htmlContent.addEventListener("keydown",this.handleKeyDown,!0)}function _syncGutterWidths(hostEditor){var allHostedEditors=EditorManager.getInlineEditors(hostEditor);allHostedEditors.push(hostEditor);var maxWidth=0;allHostedEditors.forEach(function(editor){var $gutter=$(editor._codeMirror.getGutterElement()).find(".CodeMirror-linenumbers");$gutter.css("min-width","");var curWidth=$gutter.width();curWidth>maxWidth&&(maxWidth=curWidth)}),1!==allHostedEditors.length?(maxWidth+="px",allHostedEditors.forEach(function(editor){$(editor._codeMirror.getGutterElement()).find(".CodeMirror-linenumbers").css("min-width",maxWidth),editor._codeMirror.setOption("gutters",editor._codeMirror.getOption("gutters"))})):allHostedEditors[0]._codeMirror.setOption("gutters",allHostedEditors[0]._codeMirror.getOption("gutters"))}InlineTextEditor.prototype=Object.create(InlineWidget.prototype),InlineTextEditor.prototype.constructor=InlineTextEditor,InlineTextEditor.prototype.parentClass=InlineWidget.prototype,InlineTextEditor.prototype.$wrapper=null,InlineTextEditor.prototype.editor=null,InlineTextEditor.prototype.$editorHolder=null,InlineTextEditor.prototype.$header=null,InlineTextEditor.prototype.$filename=null,InlineTextEditor.prototype.onClosed=function(){InlineTextEditor.prototype.parentClass.onClosed.apply(this,arguments),_syncGutterWidths(this.hostEditor),this.setInlineContent(null),this.htmlContent.removeEventListener("keydown",this.handleKeyDown,!0)},InlineTextEditor.prototype.sizeInlineWidgetToContents=function(){},InlineTextEditor.prototype.onAdded=function(){var self=this;InlineTextEditor.prototype.parentClass.onAdded.apply(this,arguments),this.editor&&this.editor.refresh(),CodeMirror.on(this.info,"redraw",function(){window.setTimeout(function(){self.editor&&self.editor.refresh()},0)}),_syncGutterWidths(this.hostEditor),this.editor&&this.editor.focus()},InlineTextEditor.prototype.getFocusedEditor=function(){return this.editor&&this.editor.hasFocus()?this.editor:null},InlineTextEditor.prototype.handleKeyDown=function(e){e.keyCode===KeyEvent.DOM_VK_ESCAPE&&this.editor&&this.editor.getSelections().length>1&&(CodeMirror.commands.singleSelection(this.editor._codeMirror),e.stopImmediatePropagation())},InlineTextEditor.prototype.setInlineContent=function(doc,startLine,endLine){var self=this;if(this.editor&&(this.editor.off(".InlineTextEditor"),this.editor.destroy(),this.editor=null,this.$filename.off(".InlineTextEditor").removeAttr("title"),this.$filename.html("")),doc){var range={startLine:startLine,endLine:endLine},$dirtyIndicatorDiv=$("<div/>").addClass("dirty-indicator").html("•").width(0);$dirtyIndicatorDiv.data("fullPath",doc.file.fullPath),this.$lineNumber=$("<span class='line-number'/>"),this.$filename.append($dirtyIndicatorDiv).append(doc.file.name+" : ").append(this.$lineNumber).attr("title",doc.file.fullPath),this.$filename.on("click.InlineTextEditor",function(){CommandManager.execute(Commands.FILE_OPEN,{fullPath:doc.file.fullPath}).done(function(){EditorManager.getCurrentFullEditor().setCursorPos(startLine,0,!0)})});var inlineInfo=EditorManager.createInlineEditorForDocument(doc,range,this.$editorHolder.get(0));this.editor=inlineInfo.editor,this._updateLineRange(inlineInfo.editor),this.editor.on("update.InlineTextEditor",function(event,editor){self.sizeInlineWidgetToContents()}),this.editor.on("change.InlineTextEditor",function(event,editor){self.hostEditor.isFullyVisible()&&(self.sizeInlineWidgetToContents(),self._updateLineRange(editor))}),this.editor.on("lostContent.InlineTextEditor",function(){self._onLostContent.apply(self,arguments)}),_showDirtyIndicator($dirtyIndicatorDiv,doc.isDirty)}},InlineTextEditor.prototype._updateLineRange=function(editor){this._startLine=editor.getFirstVisibleLine(),this._endLine=editor.getLastVisibleLine(),this._lineCount=this._endLine-this._startLine,this.$lineNumber.text(this._startLine+1)},InlineTextEditor.prototype.load=function(hostEditor){InlineTextEditor.prototype.parentClass.load.apply(this,arguments),this.$wrapper=$("<div/>").addClass("inline-text-editor").appendTo(this.$htmlContent),this.$header=$("<div/>").addClass("inline-editor-header").appendTo(this.$wrapper),this.$filename=$("<a/>").addClass("filename").appendTo(this.$header),this.$editorHolder=$("<div/>").addClass("inline-editor-holder").appendTo(this.$wrapper)},InlineTextEditor.prototype.onParentShown=function(){InlineTextEditor.prototype.parentClass.onParentShown.apply(this,arguments),this.editor&&(this._updateLineRange(this.editor),this.editor.refresh()),this.sizeInlineWidgetToContents()},InlineTextEditor.prototype._onLostContent=function(){this.close()},EventDispatcher.on_duringInit(DocumentManager,"dirtyFlagChange",_dirtyFlagChangeHandler),exports.InlineTextEditor=InlineTextEditor});