forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorManager.js
More file actions
1 lines (1 loc) · 10.7 KB
/
EditorManager.js
File metadata and controls
1 lines (1 loc) · 10.7 KB
1
define(function(require,exports,module){const Commands=require("command/Commands"),EventDispatcher=require("utils/EventDispatcher"),WorkspaceManager=require("view/WorkspaceManager"),PreferencesManager=require("preferences/PreferencesManager"),CommandManager=require("command/CommandManager"),DocumentManager=require("document/DocumentManager"),MainViewManager=require("view/MainViewManager"),ViewStateManager=require("view/ViewStateManager"),PerfUtils=require("utils/PerfUtils"),Editor=require("editor/Editor").Editor,InlineTextEditor=require("editor/InlineTextEditor").InlineTextEditor,MultiRangeInlineEditor=require("editor/MultiRangeInlineEditor").MultiRangeInlineEditor,Strings=require("strings"),LanguageManager=require("language/LanguageManager"),DeprecationWarning=require("utils/DeprecationWarning"),EVENT_ACTIVE_EDITOR_CHANGED="activeEditorChange";var _lastFocusedEditor=null,_inlineEditProviders=[],_inlineDocsProviders=[],_$hiddenEditorsContainer;function getCurrentFullEditor(){var currentPath=MainViewManager.getCurrentlyViewedPath(MainViewManager.ACTIVE_PANE),doc=currentPath&&DocumentManager.getOpenDocumentForPath(currentPath);return doc&&doc._masterEditor}function _saveEditorViewState(editor){ViewStateManager.updateViewState(editor)}function _restoreEditorViewState(editor){var viewState=ViewStateManager.getViewState(editor.document.file);viewState&&editor.restoreViewState(viewState)}function _notifyActiveEditorChanged(current){if(_lastFocusedEditor!==current){var previous=_lastFocusedEditor;_lastFocusedEditor=current,exports.trigger(EVENT_ACTIVE_EDITOR_CHANGED,current,previous)}}function _handleCurrentFileChange(e,file){var doc=file&&DocumentManager.getOpenDocumentForPath(file.fullPath);_notifyActiveEditorChanged(doc&&doc._masterEditor)}function _createEditorForDocument(doc,makeMasterEditor,container,range,editorOptions){var editor=new Editor(doc,makeMasterEditor,container,range,editorOptions);return editor.on("focus",function(){_notifyActiveEditorChanged(editor)}),editor.on("beforeDestroy",function(){editor.$el.is(":visible")&&_saveEditorViewState(editor)}),editor}function _openInlineWidget(editor,providers,defaultErrorMsg){PerfUtils.markStart(PerfUtils.INLINE_WIDGET_OPEN);var pos=editor.getCursorPos(),inlinePromise,i,result=new $.Deferred,errorMsg,providerRet;for(i=0;i<providers.length&&!inlinePromise;i++){var provider;(providerRet=(0,providers[i].provider)(editor,pos))&&(providerRet.hasOwnProperty("done")?inlinePromise=providerRet:errorMsg||"string"!=typeof providerRet||(errorMsg=providerRet))}return errorMsg=errorMsg||defaultErrorMsg,inlinePromise?inlinePromise.done(function(inlineWidget){editor.addInlineWidget(pos,inlineWidget).done(function(){PerfUtils.addMeasurement(PerfUtils.INLINE_WIDGET_OPEN),result.resolve()})}).fail(function(){PerfUtils.finalizeMeasurement(PerfUtils.INLINE_WIDGET_OPEN),editor.displayErrorMessageAtCursor(errorMsg),result.reject()}):(PerfUtils.finalizeMeasurement(PerfUtils.INLINE_WIDGET_OPEN),editor.displayErrorMessageAtCursor(errorMsg),result.reject()),result.promise()}function _toggleInlineWidget(providers,errorMsg){var result=new $.Deferred,currentEditor=getCurrentFullEditor();if(currentEditor){var inlineWidget=currentEditor.getFocusedInlineWidget();inlineWidget?(PerfUtils.markStart(PerfUtils.INLINE_WIDGET_CLOSE),inlineWidget.close().done(function(){PerfUtils.addMeasurement(PerfUtils.INLINE_WIDGET_CLOSE),result.resolve(!1)})):_openInlineWidget(currentEditor,providers,errorMsg).done(function(){result.resolve(!0)}).fail(function(){result.reject()})}else result.reject();return result.promise()}function _insertProviderSorted(array,provider,priority){var index,prioritizedProvider={priority:priority,provider:provider};for(index=0;index<array.length&&!(array[index].priority<priority);index++);array.splice(index,0,prioritizedProvider)}function _createUnattachedMasterEditor(doc){var editor;_$hiddenEditorsContainer||(_$hiddenEditorsContainer=$("#hidden-editors")),_createEditorForDocument(doc,!0,_$hiddenEditorsContainer).setVisible(!1)}function closeInlineWidget(hostEditor,inlineWidget){if(inlineWidget.hasFocus()){var widgetLine=hostEditor._codeMirror.getLineNumber(inlineWidget.info.line),cursorLine;hostEditor.getCursorPos().line!==widgetLine&&hostEditor.setCursorPos({line:widgetLine,pos:0}),hostEditor.focus()}return hostEditor.removeInlineWidget(inlineWidget)}function registerInlineEditProvider(provider,priority){void 0===priority&&(priority=0),_insertProviderSorted(_inlineEditProviders,provider,priority)}function registerInlineDocsProvider(provider,priority){void 0===priority&&(priority=0),_insertProviderSorted(_inlineDocsProviders,provider,priority)}function getInlineEditors(hostEditor){var inlineEditors=[];return hostEditor&&hostEditor.getInlineWidgets().forEach(function(widget){(widget instanceof InlineTextEditor&&widget.editor||widget instanceof MultiRangeInlineEditor&&widget.editor)&&inlineEditors.push(widget.editor)}),inlineEditors}function _createFullEditorForDocument(document,pane,editorOptions){var editor=_createEditorForDocument(document,!0,pane.$content,void 0,editorOptions);return editor.setVisible(!1),pane.addView(editor),exports.trigger("_fullEditorCreatedForDocument",document,editor,pane.id),editor}function createInlineEditorForDocument(doc,range,inlineContent){$(inlineContent).hide();var inlineEditor=_createEditorForDocument(doc,!1,inlineContent,range);return inlineEditor._hostEditor=getCurrentFullEditor(),$(inlineContent).show(),{content:inlineContent,editor:inlineEditor}}function focusEditor(){MainViewManager.focusActivePane()}function resizeEditor(){DeprecationWarning.deprecationWarning("Use WorkspaceManager.recomputeLayout() instead of EditorManager.resizeEditor().",!0),WorkspaceManager.recomputeLayout()}function _showEditor(document,pane,editorOptions){var createdNewEditor=!1,editor=document._masterEditor;editor&&editor._paneId&&editor._paneId!==pane.id&&(editor=document._checkAssociatedEditorForPane(pane.id)),editor?editor.$el.parent()[0]!==pane.$content[0]&&pane.addView(editor):(window.getSelection&&window.getSelection().empty&&window.getSelection().empty(),editor=_createFullEditorForDocument(document,pane,editorOptions),createdNewEditor=!0),pane.showView(editor),MainViewManager.getActivePaneId()===pane.id&&editor.focus(),createdNewEditor&&_restoreEditorViewState(editor)}function getCurrentlyViewedPath(){DeprecationWarning.deprecationWarning("Use MainViewManager.getCurrentlyViewedFile() instead of EditorManager.getCurrentlyViewedPath().",!0);var currentPath=MainViewManager.getCurrentlyViewedPath(MainViewManager.ACTIVE_PANE),doc;return currentPath&&(doc=DocumentManager.getOpenDocumentForPath(currentPath)),doc?currentPath:null}function setEditorHolder(){throw new Error("EditorManager.setEditorHolder() has been removed.")}function registerCustomViewer(){throw new Error("EditorManager.registerCustomViewer() has been removed.")}function canOpenPath(fullPath){return!LanguageManager.getLanguageForPath(fullPath).isBinary()}function openDocument(doc,pane,editorOptions){var perfTimerName=PerfUtils.markStart("EditorManager.openDocument():\t"+(!doc||doc.file.fullPath));doc&&pane&&_showEditor(doc,pane,editorOptions),PerfUtils.addMeasurement(perfTimerName)}function getFocusedInlineWidget(){var currentEditor=getCurrentFullEditor();return currentEditor?currentEditor.getFocusedInlineWidget():null}function getFocusedInlineEditor(){var focusedWidget=getFocusedInlineWidget();return focusedWidget instanceof InlineTextEditor?focusedWidget.getFocusedEditor():null}function getFocusedEditor(){var currentEditor=getCurrentFullEditor();if(currentEditor){var focusedInline=getFocusedInlineEditor();if(focusedInline)return focusedInline;if(currentEditor.hasFocus())return currentEditor}return null}function getActiveEditor(){return _lastFocusedEditor}function getHoveredEditor(mousePos){function divContainsMouse($div,mousePos){let offset=$div.offset();return mousePos.clientX>=offset.left&&mousePos.clientX<=offset.left+$div.width()&&mousePos.clientY>=offset.top&&mousePos.clientY<=offset.top+$div.height()}let fullEditor=getCurrentFullEditor();if(!fullEditor||!mousePos)return;let inlines=fullEditor.getInlineWidgets(),i,editor;for(i=0;i<inlines.length;i++){let $inlineEditorRoot=inlines[i].editor&&$(inlines[i].editor.getRootElement()),$otherDiv=inlines[i].$htmlContent;if($inlineEditorRoot&&divContainsMouse($inlineEditorRoot,mousePos)){editor=inlines[i].editor;break}if($otherDiv&&divContainsMouse($otherDiv,mousePos))return}return editor||divContainsMouse($(fullEditor.getRootElement()),mousePos)&&(editor=fullEditor),editor}function _handleRemoveFromPaneView(e,removedFiles){var handleFileRemoved=function(file){var doc=DocumentManager.getOpenDocumentForPath(file.fullPath);doc&&MainViewManager._destroyEditorIfNotNeeded(doc)};$.isArray(removedFiles)?removedFiles.forEach(function(removedFile){handleFileRemoved(removedFile)}):handleFileRemoved(removedFiles)}EventDispatcher.makeEventDispatcher(exports),EventDispatcher.setLeakThresholdForEvent(EVENT_ACTIVE_EDITOR_CHANGED,50),exports.on(EVENT_ACTIVE_EDITOR_CHANGED,function(e,current){current&¤t.document&¤t.document.file&&PreferencesManager._setCurrentFile(current.document.file.fullPath)}),CommandManager.register(Strings.CMD_TOGGLE_QUICK_EDIT,Commands.TOGGLE_QUICK_EDIT,function(){return _toggleInlineWidget(_inlineEditProviders,Strings.ERROR_QUICK_EDIT_PROVIDER_NOT_FOUND)}),CommandManager.register(Strings.CMD_TOGGLE_QUICK_DOCS,Commands.TOGGLE_QUICK_DOCS,function(){return _toggleInlineWidget(_inlineDocsProviders,Strings.ERROR_QUICK_DOCS_PROVIDER_NOT_FOUND)}),MainViewManager.on("currentFileChange",_handleCurrentFileChange),MainViewManager.on("workingSetRemove workingSetRemoveList",_handleRemoveFromPaneView),exports._createFullEditorForDocument=_createFullEditorForDocument,exports._notifyActiveEditorChanged=_notifyActiveEditorChanged,exports._saveEditorViewState=_saveEditorViewState,exports._createUnattachedMasterEditor=_createUnattachedMasterEditor,exports.EVENT_ACTIVE_EDITOR_CHANGED=EVENT_ACTIVE_EDITOR_CHANGED,exports.createInlineEditorForDocument=createInlineEditorForDocument,exports.getFocusedInlineWidget=getFocusedInlineWidget,exports.getInlineEditors=getInlineEditors,exports.closeInlineWidget=closeInlineWidget,exports.openDocument=openDocument,exports.canOpenPath=canOpenPath,exports.getActiveEditor=getActiveEditor,exports.getCurrentFullEditor=getCurrentFullEditor,exports.getFocusedEditor=getFocusedEditor,exports.getFocusedInlineEditor=getFocusedInlineEditor,exports.getHoveredEditor=getHoveredEditor,exports.registerInlineEditProvider=registerInlineEditProvider,exports.registerInlineDocsProvider=registerInlineDocsProvider,exports.registerCustomViewer=registerCustomViewer,exports.resizeEditor=resizeEditor,exports.focusEditor=focusEditor,exports.getCurrentlyViewedPath=getCurrentlyViewedPath,exports.setEditorHolder=setEditorHolder,"http://localhost:8000"===location.origin&&(window.EditorManager=exports)});