浏览代码

Centralize cache invalidation on file urls (#136)

Leonardo Jeanteur 4 年之前
父节点
当前提交
d258cf3f20
共有 4 个文件被更改,包括 11 次插入7 次删除
  1. 5 0
      hide/Ide.hx
  2. 2 2
      hide/comp/Scene.hx
  3. 1 2
      hide/comp/TileSelector.hx
  4. 3 3
      hide/comp/cdb/Cell.hx

+ 5 - 0
hide/Ide.hx

@@ -823,6 +823,11 @@ class Ide {
 		return path;
 	}
 
+	public function getUnCachedUrl( path : String ) {
+		var timestamp = Std.int(Date.now().getTime() / 1000);
+		return "file://" + getPath(path) + "?t=" + timestamp;
+	}
+
 	public static var IMG_EXTS = ["jpg", "jpeg", "gif", "png", "raw", "dds", "hdr", "tga"];
 	public function chooseImage( onSelect, allowNull=false ) {
 		chooseFile(IMG_EXTS, onSelect, allowNull);

+ 2 - 2
hide/comp/Scene.hx

@@ -258,7 +258,7 @@ class Scene extends Component implements h3d.IDrawable {
 
 	function _loadTextureData( img : hxd.res.Image, onReady : Void -> Void, t : h3d.mat.Texture ) {
 		var path = ide.getPath(img.entry.path);
-		var img = new Element('<img src="file://$path" crossorigin="anonymous"/>');
+		var img = new Element('<img src="${ide.getUnCachedUrl(path)}" crossorigin="anonymous"/>');
 		function onLoaded() {
 			if( engine.driver == null ) return;
 			setCurrent();
@@ -280,7 +280,7 @@ class Scene extends Component implements h3d.IDrawable {
 		}
 		img.on("load", onLoaded);
 		function onChange() {
-			img.attr("src", 'file://$path?t=' + Std.int(Date.now().getTime() / 1000));
+			img.attr("src", ide.getUnCachedUrl(path));
 		}
 		ide.fileWatcher.register( path, onChange, true, element );
 		cleanup.push(function() { ide.fileWatcher.unregister( path, onChange ); });

+ 1 - 2
hide/comp/TileSelector.hx

@@ -113,8 +113,7 @@ class TileSelector extends Component {
 		if( tool.element.children().length == 0 )
 			tool.remove();
 
-		var timestamp = Std.int(Date.now().getTime() / 1000);
-		var url = "file://" + ide.getPath(file) + "?t=" + timestamp;
+		var url = ide.getUnCachedUrl(file);
 		var scroll = new Element("<div class='flex-scroll'><div class='scroll'>").appendTo(element).find(".scroll");
 		image = new Element('<div class="tile" style="background-image:url(\'$url\')"></div>').appendTo(scroll);
 

+ 3 - 3
hide/comp/cdb/Cell.hx

@@ -455,12 +455,12 @@ class Cell extends Component {
 		if( tiles.length == 0 ) return;
 		tiles.removeClass("toload");
 		var imap = new Map();
-		var timestamp = Std.int(Date.now().getTime() / 1000);
 		for( t in tiles )
 			imap.set(t.getAttribute("path"), t);
 		for( path => elt in imap ) {
 			var img = js.Browser.document.createImageElement();
-			img.src = "file://"+path;
+			var url = Ide.inst.getUnCachedUrl(path);
+			img.src = url;
 			img.setAttribute("style","display:none");
 			img.onload = function() {
 				var iwidth = img.width;
@@ -473,7 +473,7 @@ class Cell extends Component {
 						var zoom = Std.parseFloat(pos[2]);
 						var bgw = Std.int(iwidth*zoom);
 						var bgh = Std.int(iheight*zoom);
-						var bg = 'url("$path?t=$timestamp") -${px}px -${py}px / ${bgw}px ${bgh}px';
+						var bg = 'url("$url") -${px}px -${py}px / ${bgw}px ${bgh}px';
 						if( zoom > 1 )
 							bg += ";image-rendering:pixelated";
 						t.setAttribute("style", t.getAttribute("style")+" background : "+bg+"; opacity : 1;");