Bladeren bron

drag an drop file files/texture from either resource tree or explorer

Nicolas Cannasse 8 jaren geleden
bovenliggende
commit
4ef0e6ce36
3 gewijzigde bestanden met toevoegingen van 46 en 0 verwijderingen
  1. 5 0
      bin/style.css
  2. 5 0
      bin/style.less
  3. 36 0
      hide/comp/FileSelect.hx

+ 5 - 0
bin/style.css

@@ -49,6 +49,10 @@ input:hover {
 input.file {
   cursor: pointer;
 }
+input.file.dragover {
+  border-color: white;
+  background-color: #141414;
+}
 input[type=button] {
   background-color: #444;
   cursor: pointer;
@@ -285,6 +289,7 @@ input[type=checkbox]:checked:after {
   cursor: default;
 }
 .hide-properties dd {
+  position: relative;
   width: 200px;
   margin-left: 10px;
 }

+ 5 - 0
bin/style.less

@@ -47,6 +47,10 @@ select, input {
 
 input.file {
 	cursor : pointer;
+	&.dragover {
+		border-color : white;
+		background-color : rgb(20,20,20);
+	}
 }
 
 input[type=button] {
@@ -311,6 +315,7 @@ input[type=checkbox] {
 	}
 
 	dd {
+		position:relative;
 		width:200px;
 		margin-left:10px;
 		input:not([type=checkbox]):not([type=number]) {

+ 36 - 0
hide/comp/FileSelect.hx

@@ -2,10 +2,12 @@ package hide.comp;
 
 class FileSelect extends Component {
 
+	var extensions : Array<String>;
 	public var path(default, set) : String;
 
 	public function new(root, extensions) {
 		super(root);
+		this.extensions = extensions;
 		path = null;
 		root.mousedown(function(e) {
 			e.preventDefault();
@@ -26,6 +28,40 @@ class FileSelect extends Component {
 			]);
 			return false;
 		});
+		// allow drag files
+		root.on("dragover", function(e) {
+			root.addClass("dragover");
+			e.preventDefault();
+		});
+		root.on("dragleave", function(e) {
+			root.removeClass("dragover");
+		});
+		root.on("drop", function(e:js.jquery.Event) {
+			root.removeClass("dragover");
+			var file = getTransferFile(e);
+			if( file != null ) {
+				path = file;
+				onChange();
+			}
+			e.preventDefault();
+		});
+	}
+
+	function getTransferFile( e : js.jquery.Event ) {
+		var data : js.html.DataTransfer = untyped e.originalEvent.dataTransfer;
+		var text = data.getData("text/html");
+		var path : String = null;
+
+		if( data.files.length > 0 )
+			path = untyped data.files[0].path;
+		else if( StringTools.startsWith(text, "<a") && text.indexOf("jstree-anchor") > 0 ) {
+			var rpath = ~/id="([^"]+)_anchor"/;
+			if( rpath.match(text) )
+				path = rpath.matched(1);
+		}
+		if( path != null && sys.FileSystem.exists(ide.getPath(path)) && extensions.indexOf(path.split(".").pop().toLowerCase()) >= 0 )
+			return ide.makeRelative(path);
+		return null;
 	}
 
 	public function getFullPath() {