فهرست منبع

FileTree: add Explore & Clone action on context menu

Tom SPIRA 6 سال پیش
والد
کامیت
f80238f05c
1فایلهای تغییر یافته به همراه67 افزوده شده و 1 حذف شده
  1. 67 1
      hide/view/FileTree.hx

+ 67 - 1
hide/view/FileTree.hx

@@ -114,6 +114,23 @@ class FileTree extends FileView {
 			newMenu.unshift({ label : "Directory", click : createNew.bind(current, { options : { createNew : "Directory" }, extensions : null, component : null }) });
 			new hide.comp.ContextMenu([
 				{ label : "New..", menu:newMenu },
+				{ label : "Explore", enabled : current != null, click : function() { onExploreFile(current); } },
+				{ label : "Clone", enabled : current != null, click : function() { 
+						try {
+							if (onCloneFile(current)) {
+								tree.refresh();
+							}
+						} catch (e : Dynamic) {
+							js.Browser.window.alert(e);
+						}
+					} },
+				{ label : "Rename", enabled : current != null, click : function() { 
+					try {
+						onRenameFile(current); 
+					} catch (e : Dynamic) {
+						js.Browser.window.alert(e);
+					}
+					} },
 				{ label : "Delete", enabled : current != null, click : function() if( js.Browser.window.confirm("Delete " + current + "?") ) { onDeleteFile(current); tree.refresh(); } },
 			]);
 		});
@@ -121,6 +138,20 @@ class FileTree extends FileView {
 		tree.init();
 	}
 
+	function onRenameFile( path : String ) {
+		var newFilename = ide.ask("New name:", path.substring( path.lastIndexOf("/") + 1 ));
+
+		while ( newFilename != null && sys.FileSystem.exists(ide.getPath(newFilename))) {
+			newFilename = ide.ask("This file already exists. Another new name:");
+		}
+		if (newFilename == null) {
+			return false;
+		}
+
+		onRename(path, newFilename);
+		return true;
+	}
+
 	function onRename(path:String, name:String) {
 		var parts = path.split("/");
 		parts.pop();
@@ -142,7 +173,6 @@ class FileTree extends FileView {
 			return false;
 		}
 
-
 		var isDir = sys.FileSystem.isDirectory(ide.getPath(path));
 		if( isDir )
 			throw "TODO : rename directory";
@@ -171,6 +201,42 @@ class FileTree extends FileView {
 		return true;
 	}
 
+	function onExploreFile( path : String ) {
+		var fullPath = sys.FileSystem.absolutePath(getFilePath(path));
+		Sys.command("explorer.exe /select," + fullPath);
+	}
+
+	function onCloneFile( path : String ) {
+		var sourcePath = getFilePath(path);
+		var nameNewFile = ide.ask("New filename:");
+		if (nameNewFile == null || nameNewFile.length == 0) {
+			return false;
+		}
+
+		var targetPath = new haxe.io.Path(sourcePath).dir + "/" + nameNewFile;
+		if ( sys.FileSystem.exists(targetPath) ) {
+			throw "File already exists";
+		}
+
+		if( sys.FileSystem.isDirectory(sourcePath) ) {
+			sys.FileSystem.createDirectory(targetPath + "/");
+			for( f in sys.FileSystem.readDirectory(sourcePath) ) {
+				sys.io.File.saveBytes(targetPath + "/" + f, sys.io.File.getBytes(sourcePath + "/" + f));
+			}
+		} else {
+			var extensionNewFile = getExtension(targetPath);
+
+			if (extensionNewFile == null) {
+				var extensionSourceFile = getExtension(sourcePath).extensions[0];
+				if (extensionSourceFile != null) {
+					targetPath =  targetPath + "." + extensionSourceFile;
+				}
+			}
+			sys.io.File.saveBytes(targetPath, sys.io.File.getBytes(sourcePath));
+		}
+		return true;
+	}
+
 	function onDeleteFile( path : String ) {
 		var fullPath = getFilePath(path);
 		if( sys.FileSystem.isDirectory(fullPath) ) {