The Unified and Autonomous Development Platform
Enhance codemirror simplescrollbar to speed up scroll when hold mouse for a while.
robin shine96dd0d923cec
diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/CodeMirrorResourceReference.java b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/CodeMirrorResourceReference.javaindex 0d2b11a740..c837e54464 100644--- a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/CodeMirrorResourceReference.java+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/CodeMirrorResourceReference.java@@ -46,7 +46,7 @@ public class CodeMirrorResourceReference extends WebjarsJavaScriptResourceRefereJavaScriptHeaderItem.forReference(new WebjarsJavaScriptResourceReference("codemirror/current/addon/selection/active-line.js")),JavaScriptHeaderItem.forReference(new WebjarsJavaScriptResourceReference("codemirror/current/addon/selection/mark-selection.js")),JavaScriptHeaderItem.forReference(new WebjarsJavaScriptResourceReference("codemirror/current/addon/scroll/annotatescrollbar.js")),- JavaScriptHeaderItem.forReference(new WebjarsJavaScriptResourceReference("codemirror/current/addon/scroll/simplescrollbars.js")),+ JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(CodeMirrorResourceReference.class, "simplescrollbars.js")),JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(CodeMirrorResourceReference.class, "identifier-highlighter.js")),diff --git a/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/simplescrollbars.js b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/simplescrollbars.jsnew file mode 100644index 0000000000..e17fe6bee4--- /dev/null+++ b/commons.wicket/src/main/java/com/pmease/commons/wicket/assets/codemirror/simplescrollbars.js@@ -0,0 +1,180 @@+// CodeMirror, copyright (c) by Marijn Haverbeke and others+// Distributed under an MIT license: http://codemirror.net/LICENSE++(function(mod) {+ if (typeof exports == "object" && typeof module == "object") // CommonJS+ mod(require("../../lib/codemirror"));+ else if (typeof define == "function" && define.amd) // AMD+ define(["../../lib/codemirror"], mod);+ else // Plain browser env+ mod(CodeMirror);+})(function(CodeMirror) {+ "use strict";++ function Bar(cls, orientation, scroll) {+ this.orientation = orientation;+ this.scroll = scroll;+ this.screen = this.total = this.size = 1;+ this.pos = 0;++ this.node = document.createElement("div");+ this.node.className = cls + "-" + orientation;+ this.inner = this.node.appendChild(document.createElement("div"));++ var self = this;+ CodeMirror.on(this.inner, "mousedown", function(e) {+ if (e.which != 1) return;+ CodeMirror.e_preventDefault(e);+ var axis = self.orientation == "horizontal" ? "pageX" : "pageY";+ var start = e[axis], startpos = self.pos;+ function done() {+ CodeMirror.off(document, "mousemove", move);+ CodeMirror.off(document, "mouseup", done);+ }+ function move(e) {+ if (e.which != 1) return done();+ self.moveTo(startpos + (e[axis] - start) * (self.total / self.size));+ }+ CodeMirror.on(document, "mousemove", move);+ CodeMirror.on(document, "mouseup", done);+ });++ // downRepeat is used to speed up scroll button when user holds mouse for a while+ this.downRepeat = 1;+ CodeMirror.on(document, "mouseup", function() {+ if (self.downRepeatTimer) {+ clearTimeout(self.downRepeatTimer);+ self.downRepeatTimer = null;+ self.downRepeat = 1;+ }+ });++ CodeMirror.on(this.node, "mousedown", onBarMouseDown);++ function onBarMouseDown(e) {+ if (self.inner == e.target)+ return;++ console.log("down");+ CodeMirror.e_preventDefault(e);+ var innerBox = self.inner.getBoundingClientRect(), where;+ if (self.orientation == "horizontal")+ where = e.clientX < innerBox.left ? -1 : e.clientX > innerBox.right ? 1 : 0;+ else+ where = e.clientY < innerBox.top ? -1 : e.clientY > innerBox.bottom ? 1 : 0;++ if (self.downRepeat == 1)+ self.originWhere = where;++ if (self.originWhere<0 && where<0 || self.originWhere>0 && where>0) {+ self.moveTo(self.pos + where * self.screen * self.downRepeat);++ var timeout;+ if (self.downRepeat == 1)+ timeout = 500;+ else+ timeout = 100;++ self.downRepeatTimer = setTimeout(function() {+ self.downRepeat++;+ onBarMouseDown(e);+ }, timeout);+ }+ }++ function onWheel(e) {+ var moved = CodeMirror.wheelEventPixels(e)[self.orientation == "horizontal" ? "x" : "y"];+ var oldPos = self.pos;+ self.moveTo(self.pos + moved);+ if (self.pos != oldPos) CodeMirror.e_preventDefault(e);+ }+ CodeMirror.on(this.node, "mousewheel", onWheel);+ CodeMirror.on(this.node, "DOMMouseScroll", onWheel);+ }++ Bar.prototype.moveTo = function(pos, update) {+ if (pos < 0) pos = 0;+ if (pos > this.total - this.screen) pos = this.total - this.screen;+ if (pos == this.pos) return;+ this.pos = pos;+ this.inner.style[this.orientation == "horizontal" ? "left" : "top"] =+ (pos * (this.size / this.total)) + "px";+ if (update !== false) this.scroll(pos, this.orientation);+ };++ var minButtonSize = 10;++ Bar.prototype.update = function(scrollSize, clientSize, barSize) {+ this.screen = clientSize;+ this.total = scrollSize;+ this.size = barSize;++ var buttonSize = this.screen * (this.size / this.total);+ if (buttonSize < minButtonSize) {+ this.size -= minButtonSize - buttonSize;+ buttonSize = minButtonSize;+ }+ this.inner.style[this.orientation == "horizontal" ? "width" : "height"] =+ buttonSize + "px";+ this.inner.style[this.orientation == "horizontal" ? "left" : "top"] =+ this.pos * (this.size / this.total) + "px";+ };++ function SimpleScrollbars(cls, place, scroll) {+ this.addClass = cls;+ this.horiz = new Bar(cls, "horizontal", scroll);+ place(this.horiz.node);+ this.vert = new Bar(cls, "vertical", scroll);+ place(this.vert.node);+ this.width = null;+ }++ SimpleScrollbars.prototype.update = function(measure) {+ if (this.width == null) {+ var style = window.getComputedStyle ? window.getComputedStyle(this.horiz.node) : this.horiz.node.currentStyle;+ if (style) this.width = parseInt(style.height);+ }+ var width = this.width || 0;++ var needsH = measure.scrollWidth > measure.clientWidth + 1;+ var needsV = measure.scrollHeight > measure.clientHeight + 1;+ this.vert.node.style.display = needsV ? "block" : "none";+ this.horiz.node.style.display = needsH ? "block" : "none";++ if (needsV) {+ this.vert.update(measure.scrollHeight, measure.clientHeight,+ measure.viewHeight - (needsH ? width : 0));+ this.vert.node.style.display = "block";+ this.vert.node.style.bottom = needsH ? width + "px" : "0";+ }+ if (needsH) {+ this.horiz.update(measure.scrollWidth, measure.clientWidth,+ measure.viewWidth - (needsV ? width : 0) - measure.barLeft);+ this.horiz.node.style.right = needsV ? width + "px" : "0";+ this.horiz.node.style.left = measure.barLeft + "px";+ }++ return {right: needsV ? width : 0, bottom: needsH ? width : 0};+ };++ SimpleScrollbars.prototype.setScrollTop = function(pos) {+ this.vert.moveTo(pos, false);+ };++ SimpleScrollbars.prototype.setScrollLeft = function(pos) {+ this.horiz.moveTo(pos, false);+ };++ SimpleScrollbars.prototype.clear = function() {+ var parent = this.horiz.node.parentNode;+ parent.removeChild(this.horiz.node);+ parent.removeChild(this.vert.node);+ };++ CodeMirror.scrollbarModel.simple = function(place, scroll) {+ return new SimpleScrollbars("CodeMirror-simplescroll", place, scroll);+ };+ CodeMirror.scrollbarModel.overlay = function(place, scroll) {+ return new SimpleScrollbars("CodeMirror-overlayscroll", place, scroll);+ };+});