forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDragAndDrop.js
More file actions
1 lines (1 loc) · 7.53 KB
/
DragAndDrop.js
File metadata and controls
1 lines (1 loc) · 7.53 KB
1
define(function(require,exports,module){const Async=require("utils/Async"),CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),Dialogs=require("widgets/Dialogs"),DefaultDialogs=require("widgets/DefaultDialogs"),MainViewManager=require("view/MainViewManager"),FileSystem=require("filesystem/FileSystem"),PreferencesManager=require("preferences/PreferencesManager"),FileUtils=require("file/FileUtils"),ProjectManager=require("project/ProjectManager"),Strings=require("strings"),Metrics=require("utils/Metrics"),StringUtils=require("utils/StringUtils"),_PREF_DRAG_AND_DROP="dragAndDrop";function isValidDrop(items){var i,len=items.length;for(i=0;i<len;i++)if("file"===items[i].kind){var entry;if(items[i].webkitGetAsEntry().isFile)return!0;if(1===len)return!0}return!1}function stopURIListPropagation(files,event){var types=event.dataTransfer.types;files&&files.length||!types||types.forEach(function(value){if("text/uri-list"===value)return event.stopPropagation(),void event.preventDefault()})}function openDroppedFiles(paths){var errorFiles=[],ERR_MULTIPLE_ITEMS_WITH_DIR={};return Async.doInParallel(paths,function(path,idx){var result=new $.Deferred;return FileSystem.resolve(path,function(err,item){if(!err&&item.isFile){if(idx<paths.length-1&&-1!==MainViewManager.findInWorkingSet(MainViewManager.ALL_PANES,path))return void result.resolve();Metrics.countEvent(Metrics.EVENT_TYPE.PLATFORM,"dragAndDrop","fileOpen"),CommandManager.execute(Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN,{fullPath:path,silent:!0}).done(function(){result.resolve()}).fail(function(openErr){errorFiles.push({path:path,error:openErr}),result.reject()})}else!err&&item.isDirectory&&1===paths.length?(Metrics.countEvent(Metrics.EVENT_TYPE.PLATFORM,"dragAndDrop","projectOpen"),ProjectManager.openProject(path).done(function(){result.resolve()}).fail(function(){result.reject()})):(errorFiles.push({path:path,error:err||ERR_MULTIPLE_ITEMS_WITH_DIR}),result.reject())}),result.promise()},!1).fail(function(){function errorToString(err){return err===ERR_MULTIPLE_ITEMS_WITH_DIR?Strings.ERROR_MIXED_DRAGDROP:FileUtils.getFileErrorString(err)}if(errorFiles.length>0){var message=Strings.ERROR_OPENING_FILES;message+="<ul class='dialog-list'>",errorFiles.forEach(function(info){message+="<li><span class='dialog-filename'>"+StringUtils.breakableUrl(ProjectManager.getProjectRelativeOrDisplayPath(info.path))+"</span> - "+errorToString(info.error)+"</li>"}),message+="</ul>",Dialogs.showModalDialog(DefaultDialogs.DIALOG_ID_ERROR,Strings.ERROR_OPENING_FILE_TITLE,message)}})}async function _focusAndOpenDroppedFiles(droppedPaths){try{if(window.__TAURI__){const currentWindow=window.__TAURI__.window.getCurrent();await currentWindow.setAlwaysOnTop(!0),await currentWindow.setAlwaysOnTop(!1)}else window.__ELECTRON__&&await window.electronAPI.focusWindow()}catch(e){console.error("Error focusing window")}openDroppedFiles(droppedPaths)}async function _computeNewPositionAndSizeWebkit(){const currentWindow=window.__TAURI__.window.getCurrent(),newSize=await currentWindow.innerSize(),newPosition=await currentWindow.innerPosition();return{newSize:newSize,newPosition:newPosition}}async function _computeNewPositionAndSizeWindows(){const currentWindow=window.__TAURI__.window.getCurrent(),newSize=await currentWindow.innerSize(),newPosition=await currentWindow.innerPosition();return{newSize:newSize,newPosition:newPosition}}async function _computeNewPositionAndSize(){return"win"===Phoenix.platform?_computeNewPositionAndSizeWindows():_computeNewPositionAndSizeWebkit()}async function showAndResizeFileDropWindow(event){let $activeElement;const fileDropWindow=window.__TAURI__.window.WebviewWindow.getByLabel("fileDrop");if($("#editor-holder").has(event.target).length?$activeElement=$("#editor-holder"):$("#sidebar").has(event.target).length?$activeElement=$("#sidebar"):await fileDropWindow.hide(),!$activeElement)return;const{newSize:newSize,newPosition:newPosition}=await _computeNewPositionAndSize(),currentSize=await fileDropWindow.innerSize(),currentPosition=await fileDropWindow.innerPosition(),isSameSize=currentSize.width===newSize.width&¤tSize.height===newSize.height,isSamePosition=currentPosition.x===newPosition.x&¤tPosition.y===newPosition.y;window.__TAURI__.event.emit("drop-attach-on-window",{projectName:window.path.basename(ProjectManager.getProjectRoot().fullPath),dropMessage:Strings.DROP_TO_OPEN_FILES,dropMessageOneFile:Strings.DROP_TO_OPEN_FILE,dropProjectMessage:Strings.DROP_TO_OPEN_PROJECT,windowLabelOfListener:window.__TAURI__.window.appWindow.label,platform:Phoenix.platform}),isSameSize&&isSamePosition&&await fileDropWindow.isVisible()||(await fileDropWindow.setSize(newSize),await fileDropWindow.setPosition(newPosition),await fileDropWindow.show(),await fileDropWindow.setAlwaysOnTop(!0))}function attachHandlers(){function handleDragOver(event){var files=(event=event.originalEvent||event).dataTransfer.files;if(stopURIListPropagation(files,event),PreferencesManager.get(_PREF_DRAG_AND_DROP)&&event.dataTransfer.types&&event.dataTransfer.types.includes("Files")&&window.__TAURI__&&showAndResizeFileDropWindow(event),window.__ELECTRON__&&event.dataTransfer.types&&event.dataTransfer.types.includes("Files"))event.stopPropagation(),event.preventDefault(),0===$(".modal.instance").length?event.dataTransfer.dropEffect="copy":event.dataTransfer.dropEffect="none";else if(files&&files.length){event.stopPropagation(),event.preventDefault();var dropEffect="none";0===$(".modal.instance").length&&isValidDrop(event.dataTransfer.items)&&(dropEffect="copy"),event.dataTransfer.dropEffect=dropEffect}}function handleDrop(event){const files=(event=event.originalEvent||event).dataTransfer.files;if(Metrics.countEvent(Metrics.EVENT_TYPE.PLATFORM,"dragAndDrop","any"),stopURIListPropagation(files,event),files&&files.length)if(event.stopPropagation(),event.preventDefault(),window.__ELECTRON__){const droppedVirtualPaths=[];for(const file of files){const filePath=window.electronAPI.getPathForFile(file);if(filePath)try{droppedVirtualPaths.push(window.fs.getTauriVirtualPath(filePath))}catch(e){console.error("Error resolving dropped path: ",filePath)}}droppedVirtualPaths.length&&openDroppedFiles(droppedVirtualPaths)}else brackets.app.getDroppedFiles(function(err,paths){err||openDroppedFiles(paths)})}$(window.document.body).on("dragover",handleDragOver).on("drop",handleDrop),window.document.body.addEventListener("dragover",function(event){$(event.target).closest(".CodeMirror").length&&handleDragOver(event)},!0),window.document.body.addEventListener("drop",function(event){$(event.target).closest(".CodeMirror").length&&handleDrop(event)},!0)}PreferencesManager.definePreference(_PREF_DRAG_AND_DROP,"boolean",Phoenix.isNativeApp&&"linux"!==Phoenix.platform,{description:Strings.DESCRIPTION_DRAG_AND_DROP_ENABLED}),window.__TAURI__&&window.__TAURI__.event.listen("file-drop-event-phoenix",({payload:payload})=>{if(!(payload&&payload.pathList&&payload.pathList.length&&payload.windowLabelOfListener&&payload.windowLabelOfListener===window.__TAURI__.window.appWindow.label))return;Metrics.countEvent(Metrics.EVENT_TYPE.PLATFORM,"dragAndDrop","any");const droppedVirtualPaths=[];for(const droppedPath of payload.pathList)try{droppedVirtualPaths.push(window.fs.getTauriVirtualPath(droppedPath))}catch(e){console.error("Error resolving dropped path: ",droppedPath)}_focusAndOpenDroppedFiles(droppedVirtualPaths)}),CommandManager.register(Strings.CMD_OPEN_DROPPED_FILES,Commands.FILE_OPEN_DROPPED_FILES,openDroppedFiles),exports.attachHandlers=attachHandlers,exports.isValidDrop=isValidDrop,exports.openDroppedFiles=openDroppedFiles,exports._PREF_DRAG_AND_DROP=_PREF_DRAG_AND_DROP});