]> source.dussan.org Git - gitblit.git/commitdiff
Opacity adjustments for image diffs
authorTom <tw201207@gmail.com>
Wed, 12 Nov 2014 19:31:12 +0000 (20:31 +0100)
committerTom <tw201207@gmail.com>
Wed, 12 Nov 2014 19:31:12 +0000 (20:31 +0100)
* ImageDiffHandler adds the slider; styled in gitblit.css
* imgdiff.js is a little bottom-loaded Javascript that adjusts the
  opacity on sliders' scroll events.
* The three diff pages add this bottom script to the page if needed
* GitBlitDiffFormatter: center image diffs.

src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java
src/main/java/com/gitblit/wicket/pages/BlobDiffPage.java
src/main/java/com/gitblit/wicket/pages/CommitDiffPage.java
src/main/java/com/gitblit/wicket/pages/ComparePage.java
src/main/java/com/gitblit/wicket/pages/ImageDiffHandler.java
src/main/java/com/gitblit/wicket/pages/scripts/imgdiff.js [new file with mode: 0644]
src/main/resources/gitblit.css

index edaed70fcca9d3fde9284223a5bb788b7a9ba9e7..3c65267baf3397cb6c50640e73f919996b82811b 100644 (file)
@@ -150,7 +150,7 @@ public class GitBlitDiffFormatter extends DiffFormatter {
                        {
                                String binaryDiff = binaryDiffHandler.renderBinaryDiff(formatter.entry);
                                if (binaryDiff != null) {
-                                       byte[] bb = ("<tr><td colspan='4'>" + binaryDiff + "</td></tr>").getBytes(StandardCharsets.UTF_8);
+                                       byte[] bb = ("<tr><td colspan='4' align='center'>" + binaryDiff + "</td></tr>").getBytes(StandardCharsets.UTF_8);
                                        super.write(bb, 0, bb.length);
                                        return;
                                }
index 517e80b83cd1a0ead9aefad08682403d8d7fcb99..71516ec8265b8d675fe8d2b5e4da9eda53172fdb 100644 (file)
@@ -55,6 +55,9 @@ public class BlobDiffPage extends RepositoryPage {
                        ImageDiffHandler handler = new ImageDiffHandler(getContextUrl(), repositoryName,\r
                                        parent.getName(), commit.getName(), imageExtensions);\r
                        diff = DiffUtils.getDiff(r, commit, blobPath, DiffOutputType.HTML, handler).content;\r
+                       if (handler.getImgDiffCount() > 0) {\r
+                               addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs\r
+                       }\r
                        add(new BookmarkablePageLink<Void>("patchLink", PatchPage.class,\r
                                        WicketUtils.newPathParameter(repositoryName, objectId, blobPath)));\r
                } else {\r
@@ -63,6 +66,9 @@ public class BlobDiffPage extends RepositoryPage {
                        ImageDiffHandler handler = new ImageDiffHandler(getContextUrl(), repositoryName,\r
                                        baseCommit.getName(), commit.getName(), imageExtensions);\r
                        diff = DiffUtils.getDiff(r, baseCommit, commit, blobPath, DiffOutputType.HTML, handler).content;\r
+                       if (handler.getImgDiffCount() > 0) {\r
+                               addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs\r
+                       }\r
                        add(new BookmarkablePageLink<Void>("patchLink", PatchPage.class,\r
                                        WicketUtils.newBlobDiffParameter(repositoryName, baseObjectId, objectId,\r
                                                        blobPath)));\r
index 77d5ccf365a815e0917d962dac3af27778da766f..e40af5159c24db8b8c291bcf3407bbd3fc32783c 100644 (file)
@@ -85,6 +85,9 @@ public class CommitDiffPage extends RepositoryPage {
                final ImageDiffHandler handler = new ImageDiffHandler(getContextUrl(), repositoryName,
                                parents.isEmpty() ? null : parents.get(0), commit.getName(), imageExtensions);
                final DiffOutput diff = DiffUtils.getCommitDiff(r, commit, DiffOutputType.HTML, handler);
+               if (handler.getImgDiffCount() > 0) {
+                       addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs
+               }
 
                // add commit diffstat
                int insertions = 0;
index dae8d8ea19ea2de5c87b9905429306b2b20dfc2e..c0141eba7637070eba3cf2635c90426728913c20 100644 (file)
@@ -117,6 +117,9 @@ public class ComparePage extends RepositoryPage {
                                        fromCommit.getName(), toCommit.getName(), imageExtensions);
 
                        final DiffOutput diff = DiffUtils.getDiff(r, fromCommit, toCommit, DiffOutputType.HTML, handler);
+                       if (handler.getImgDiffCount() > 0) {
+                               addBottomScript("scripts/imgdiff.js"); // Tiny support script for image diffs
+                       }
 
                        // add compare diffstat
                        int insertions = 0;
index 69d84f4efb77b8fba811f15d53aedeb8a867a310..4278f238504dc1a9e1dabd72918a71c31fca7be5 100644 (file)
@@ -40,6 +40,8 @@ public class ImageDiffHandler implements DiffUtils.BinaryDiffHandler {
        private final String baseUrl;
        private final List<String> imageExtensions;
 
+       private int imgDiffCount = 0;
+
        public ImageDiffHandler(final String baseUrl, final String repositoryName, final String oldCommitId,
                        final String newCommitId, final List<String> imageExtensions) {
                this.baseUrl = baseUrl;
@@ -62,8 +64,10 @@ public class ImageDiffHandler implements DiffUtils.BinaryDiffHandler {
                        String oldUrl = getImageUrl(diffEntry, Side.OLD);
                        String newUrl = getImageUrl(diffEntry, Side.NEW);
                        if (oldUrl != null && newUrl != null) {
+                               imgDiffCount++;
+                               String id = "imgdiff" + imgDiffCount;
                                HtmlBuilder builder = new HtmlBuilder("div");
-                               Element container = builder.root().appendElement("div").attr("class", "imgdiff");
+                               Element container = builder.root().attr("align", "center").appendElement("div").attr("class", "imgdiff");
                                Element resizeable = container.appendElement("div").attr("class", "imgdiff-left");
                                // style='max-width:640px;' is necessary for ensuring that the browser limits large images
                                // to some reasonable width, and to override the "img { max-width: 100%; }" from bootstrap.css,
@@ -73,8 +77,11 @@ public class ImageDiffHandler implements DiffUtils.BinaryDiffHandler {
                                // is too wide.
                                // XXX: Maybe add a max-height, too, to limit portrait-oriented images to some reasonable height?
                                // (Like a 300x10000px image...)
-                               resizeable.appendElement("img").attr("class", "imgdiff imgdiff-left").attr("style", "max-width:640px;").attr("src", oldUrl);
+                               resizeable.appendElement("img").attr("class", "imgdiff-left").attr("id", id).attr("style", "max-width:640px;").attr("src", oldUrl);
                                container.appendElement("img").attr("class", "imgdiff").attr("style", "max-width:640px;").attr("src", newUrl);
+                               builder.root().appendElement("br");
+                               Element slider = builder.root().appendElement("div").attr("class", "imgdiff-slider").attr("id", "slider-" + id);
+                               slider.appendElement("div").attr("class", "imgdiff-slider-inner");
                                return builder.toString();
                        }
                        break;
@@ -90,6 +97,11 @@ public class ImageDiffHandler implements DiffUtils.BinaryDiffHandler {
                return null;
        }
 
+       /** Returns the number of image diffs generated so far by this {@link ImageDiffHandler}. */
+       public int getImgDiffCount() {
+               return imgDiffCount;
+       }
+
        /**
         * Constructs a URL that will fetch the designated resource in the git repository. The returned string will
         * contain the URL fully URL-escaped, but note that it may still contain unescaped ampersands, so the result
diff --git a/src/main/java/com/gitblit/wicket/pages/scripts/imgdiff.js b/src/main/java/com/gitblit/wicket/pages/scripts/imgdiff.js
new file mode 100644 (file)
index 0000000..faa2f33
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2014 Tom <tw201207@gmail.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+jQuery(function () {
+       // Runs on jQuery's document.ready and sets up the scroll event handlers for all image diffs.
+       jQuery(".imgdiff-slider").scroll(function() {
+               var w = 1.0 - (this.scrollLeft / (this.scrollWidth - (this.clientWidth || this.offsetWidth)));
+               // We encode the target img id in the slider's id: slider-imgdiffNNN.
+               jQuery('#' + this.id.substr(this.id.indexOf('-') + 1)).css("opacity", w);
+       })
+});
index f6d6b24dd2c2ba488e23c5ea43ac5b1b8d3a7194..bdab20567bca440139d8647d0093d86d982a899e 100644 (file)
@@ -1486,14 +1486,43 @@ div.imgdiff-left:before {
 \r
 img.imgdiff-left {\r
        margin-left: 18px; /* Compensate for padding on outer div. */\r
+       user-select: none;\r
 }\r
 \r
 img.imagediff {\r
        user-select: none;\r
+       /* Checkerboard background */\r
+    background-color: white;\r
+    background-image: linear-gradient(45deg, #DDD 25%, transparent 25%, transparent 75%, #DDD 75%, #DDD), linear-gradient(45deg, #DDD 25%, transparent 25%, transparent 75%, #DDD 75%, #DDD);\r
+    background-size: 16px 16px;\r
+    background-position: 0 0, 8px 8px;\r
 }\r
 \r
 .diff-img {\r
-       margin: 2px 2px;\r
+       margin: 2px;\r
+}\r
+\r
+div.imgdiff-slider {\r
+       display: inline-block;\r
+       position: relative;\r
+       margin: 0px 2px;\r
+       width: 420px;\r
+       max-width: 420px;\r
+       height: 24px;\r
+       min-height: 18px;\r
+       overflow-x: scroll;\r
+       background: linear-gradient(to right, #F00, #0F0);\r
+}\r
+\r
+div.imgdiff-slider-inner {\r
+       position: absolute;\r
+       bottom: 0;\r
+       margin: 0;\r
+       padding: 0;\r
+       width : 1000%;\r
+       height: 1px;\r
+       border: none;\r
+       background: transparent;\r
 }\r
 \r
 /* End image diffs */\r