define(function(require,exports,module){const CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),EditorManager=require("editor/EditorManager"),Menus=require("command/Menus"),PreferencesManager=require("preferences/PreferencesManager"),Strings=require("strings"),AppInit=require("utils/AppInit"),Resizer=require("utils/Resizer"),WorkspaceManager=require("view/WorkspaceManager"),EventDispatcher=require("utils/EventDispatcher"),ProviderRegistrationHandler=require("features/PriorityBasedRegistration").RegistrationHandler,previewContainerHTML='
\n
\n
\n
';EventDispatcher.makeEventDispatcher(exports);const _EVENT_POPUP_CONTENT_MUTATED="_popupContentMutated",observer=new MutationObserver(mutations=>{for(let mutation of mutations)if("childList"===mutation.type||"subtree"===mutation.type){exports.trigger("_popupContentMutated",mutations);break}}),_providerRegistrationHandler=new ProviderRegistrationHandler,registerSelectionViewProvider=_providerRegistrationHandler.registerProvider.bind(_providerRegistrationHandler),removeSelectionViewProvider=_providerRegistrationHandler.removeProvider.bind(_providerRegistrationHandler);function _getSelectionViewProviders(editor){let SelectionViewProviders=[],language=editor.getLanguageForSelection(),enabledProviders=_providerRegistrationHandler.getProvidersForLanguageId(language.getId());for(let item of enabledProviders)SelectionViewProviders.push(item.provider);return SelectionViewProviders}let enabled,prefs=null,$previewContainer,lastMouseX=0,lastMouseY=0,$previewContent;const CMD_ENABLE_SELECTION_VIEW="view.enableSelectionView",POPUP_DELAY=200,POINTER_HEIGHT=10,POPOVER_HORZ_MARGIN=5;(prefs=PreferencesManager.getExtensionPrefs("SelectionView")).definePreference("enabled","boolean",!0,{description:Strings.DESCRIPTION_SELECTION_VIEW_ENABLED});let popoverState=null,popupTimer=null,currentQueryID=0,queryInProgress=!1,layoutFrame=0,layoutTimer=null;const resizingPanels=new Set,LAYOUT_SETTLE_DELAY=120;function hidePreview(focusEditor){clearTimeout(popupTimer),clearTimeout(layoutTimer),layoutTimer=null,cancelAnimationFrame(layoutFrame),layoutFrame=0,popupTimer=null,currentQueryID++,queryInProgress=!1,popoverState&&(popoverState.visible&&($previewContent.empty(),$previewContainer.hide(),$previewContainer.css("visibility",""),$previewContainer.removeClass("active"),!1!==focusEditor&&EditorManager.getActiveEditor()&&EditorManager.getActiveEditor().focus()),popoverState=null)}function getVisibleSelectionBounds(editor,bounds){let selection=null;const root=editor.getRootElement();for(const highlight of root.querySelectorAll(".CodeMirror-selected")){if(highlight.closest(".CodeMirror")!==root)continue;const rect=highlight.getBoundingClientRect(),left=Math.max(bounds.left,rect.left),right=Math.min(bounds.right,rect.right),top=Math.max(bounds.top,rect.top),bottom=Math.min(bounds.bottom,rect.bottom);right<=left||bottom<=top||(selection?(selection.left=Math.min(selection.left,left),selection.right=Math.max(selection.right,right),selection.top=Math.min(selection.top,top),selection.bottom=Math.max(selection.bottom,bottom)):selection={left:left,right:right,top:top,bottom:bottom,rectangles:[]},selection.rectangles.push({left:left,right:right,top:top,bottom:bottom}))}return selection}function positionPreview(editor,refreshBounds){if($previewContent.find("#selection-view-popover-root").is(":empty"))return void hidePreview();const root=editor.getRootElement(),bounds=root.getBoundingClientRect();if(!root.isConnected||bounds.width<=2*POPOVER_HORZ_MARGIN||bounds.height<=2*POPOVER_HORZ_MARGIN)return void hidePreview(!1);const minX=Math.max(0,bounds.left)+POPOVER_HORZ_MARGIN,maxX=Math.min(window.innerWidth,bounds.right)-POPOVER_HORZ_MARGIN,minY=Math.max(0,bounds.top)+POPOVER_HORZ_MARGIN,maxY=Math.min(window.innerHeight,bounds.bottom)-POPOVER_HORZ_MARGIN;if(!popoverState.anchor){const overEditor=lastMouseX>=minX&&lastMouseX<=maxX&&lastMouseY>=minY&&lastMouseY<=maxY;popoverState.anchor={x:overEditor?lastMouseX:popoverState.xpos,y:overEditor?lastMouseY:popoverState.ytop},refreshBounds=!0}refreshBounds&&(popoverState.selectionBounds=getVisibleSelectionBounds(editor,{left:minX,right:maxX,top:minY,bottom:maxY}));const anchorX=Math.max(minX,Math.min(maxX,popoverState.anchor.x)),anchorY=Math.max(minY,Math.min(maxY,popoverState.anchor.y)),previewWidth=$previewContainer.outerWidth(),previewHeight=$previewContainer.outerHeight(),selection=popoverState.selectionBounds||{left:anchorX,right:anchorX,top:anchorY,bottom:anchorY};let left=Math.max(minX,Math.min(maxX-previewWidth,selection.right-previewWidth));const occupied=(selection.rectangles||[selection]).filter(rect=>rect.right>left&&rect.leftrect.bottom)):selection.bottom;let top=bottom+POINTER_HEIGHT,below=!0;top+previewHeight>maxY&&(below=!1,(top=selection.top-previewHeight-POINTER_HEIGHT)=minX&&(left=selection.left-POINTER_HEIGHT-previewWidth))),top=Math.max(minY,Math.min(maxY-previewHeight,top)),left=Math.max(minX,Math.min(maxX-previewWidth,left)),$previewContainer.toggleClass("preview-bubble-below",below).toggleClass("preview-bubble-above",!below),$previewContainer.css({left:left,top:top}).addClass("active")}function suspendPreviewLayout(){clearTimeout(layoutTimer),layoutTimer=null,cancelAnimationFrame(layoutFrame),layoutFrame=0,popoverState&&popoverState.visible&&!popoverState.layoutPending&&(popoverState.layoutPending=!0,$previewContainer.css("visibility","hidden"))}function restorePreviewLayout(){clearTimeout(layoutTimer),layoutTimer=null,popoverState&&popoverState.visible&&!resizingPanels.size&&!layoutFrame&&(layoutFrame=requestAnimationFrame(function(){if(layoutFrame=0,!popoverState||!popoverState.visible||resizingPanels.size)return;const editor=popoverState.editor,cursor=editor.charCoords(editor.getCursorPos(),"window");popoverState.anchor={x:cursor.left,y:cursor.top},positionPreview(editor,!0),popoverState&&popoverState.visible&&(popoverState.layoutPending=!1,$previewContainer.css("visibility",""))}))}function schedulePreviewLayout(){popoverState&&popoverState.visible&&(suspendPreviewLayout(),resizingPanels.size||(layoutTimer=setTimeout(restorePreviewLayout,LAYOUT_SETTLE_DELAY)))}function onPanelResizeStart(event){resizingPanels.add(event.target),popoverState&&popoverState.visible?suspendPreviewLayout():hidePreview(!1)}function onPanelResizeEnd(event){resizingPanels.delete(event.target)&&!resizingPanels.size&&restorePreviewLayout()}function _createPopoverState(editor,popoverResults){if(popoverResults&&popoverResults.length){let popover={content:$("
")};for(let result of popoverResults)popover.content.append(result.content);let pos=editor.getCursorPos(),startCoord=editor.charCoords(pos),endCoord=editor.charCoords(pos);return popover.xpos=(endCoord.left-startCoord.left)/2+startCoord.left,endCoord.left{popupTimer=null;let editor=EditorManager.getActiveEditor();editor&&showPreview(editor,editor.getSelections())},POPUP_DELAY)))}function _processMouseMove(event){if(!enabled||resizingPanels.size||null!==popupTimer||queryInProgress||popoverState&&popoverState.visible)return;if(lastMouseX=event.clientX,lastMouseY=event.clientY,0!==event.buttons)return;let editor=EditorManager.getHoveredEditor(event);if(editor){let mousePos=editor.coordsChar({left:event.clientX,top:event.clientY}),selectionObj=editor.getSelections();if(1!==selectionObj.length)return;let selection=editor.getSelection();if(selection.start.line===selection.end.line&&selection.start.ch===selection.end.ch)return;editor.posWithinRange(mousePos,selection.start,selection.end,!0)&&showPreview(editor,selectionObj)}}function onEditorScroll(){resizingPanels.size||popoverState&&popoverState.layoutPending||hidePreview(!1)}function onActiveEditorChange(_event,current,previous){hidePreview(),previous&&previous.document&&(previous.document.off("change",hidePreview),previous.off("scroll",onEditorScroll)),current&¤t.document&&(current.document.on("change",hidePreview),current.on("scroll",onEditorScroll))}function updateMenuItemCheckmark(){CommandManager.get(CMD_ENABLE_SELECTION_VIEW).setChecked(enabled)}function setEnabled(_enabled,doNotSave){if(enabled!==_enabled){enabled=_enabled;let editorHolder=$("#editor-holder")[0];enabled?(editorHolder.addEventListener("mouseup",handleMouseUp,!0),editorHolder.addEventListener("mousemove",_processMouseMove,!0),onActiveEditorChange(null,EditorManager.getActiveEditor(),null),EditorManager.on("activeEditorChange",onActiveEditorChange)):(editorHolder.removeEventListener("mouseup",handleMouseUp,!0),editorHolder.removeEventListener("mousemove",_processMouseMove,!0),onActiveEditorChange(null,null,EditorManager.getActiveEditor()),EditorManager.off("activeEditorChange",onActiveEditorChange),hidePreview()),doNotSave||(prefs.set("enabled",enabled),prefs.save())}updateMenuItemCheckmark()}function toggleEnableSelectionView(){setEnabled(!enabled)}function _forceShow(popover){hidePreview(),popoverState=popover,_renderPreview(popover.editor)}function _handleEscapeKeyEvent(event){return!(!popoverState||!popoverState.visible)&&(hidePreview(),event.preventDefault(),event.stopPropagation(),!0)}function isSelectionViewShown(){return popoverState&&popoverState.visible&&!popoverState.layoutPending||!1}AppInit.appReady(function(){$previewContainer=$(previewContainerHTML).appendTo($("body")),$previewContent=$previewContainer.find(".preview-content"),observer.observe($previewContent[0],{childList:!0,subtree:!0}),exports.on("_popupContentMutated",function(){isSelectionViewShown()&&positionPreview(popoverState.editor)}),WorkspaceManager.on("workspaceUpdateLayout",schedulePreviewLayout),$(document).on(Resizer.EVENT_PANEL_RESIZE_START+".selectionView",onPanelResizeStart).on(Resizer.EVENT_PANEL_RESIZE_END+".selectionView",onPanelResizeEnd),CommandManager.register(Strings.CMD_ENABLE_SELECTION_VIEW,CMD_ENABLE_SELECTION_VIEW,toggleEnableSelectionView),Menus.getMenu(Menus.AppMenuBar.VIEW_MENU).addMenuItem(CMD_ENABLE_SELECTION_VIEW,null,Menus.AFTER,Commands.VIEW_TOGGLE_INSPECTION),setEnabled(prefs.get("enabled"),!0),prefs.on("change","enabled",function(){setEnabled(prefs.get("enabled"),!0)}),WorkspaceManager.addEscapeKeyEventHandler("selectionView",_handleEscapeKeyEvent)}),exports._queryPreviewProviders=queryPreviewProviders,exports._forceShow=_forceShow,exports.registerSelectionViewProvider=registerSelectionViewProvider,exports.removeSelectionViewProvider=removeSelectionViewProvider,exports.hidePreview=hidePreview,exports.isSelectionViewShown=isSelectionViewShown});