The Unified and Autonomous Development Platform
Fix the issue that textare caret position is not accurate on zoomed screen
robin shenbe65ddf2b391
diff --git a/web/src/main/java/com/gitplex/server/web/assets/js/textareacaretposition/textarea-caret-position.js b/web/src/main/java/com/gitplex/server/web/assets/js/textareacaretposition/textarea-caret-position.jsindex 878b278fe0..3fb05b0dea 100644--- a/web/src/main/java/com/gitplex/server/web/assets/js/textareacaretposition/textarea-caret-position.js+++ b/web/src/main/java/com/gitplex/server/web/assets/js/textareacaretposition/textarea-caret-position.js@@ -48,9 +48,20 @@ var properties = [];-var isFirefox = window.mozInnerScreenX != null;+var isBrowser = (typeof window !== 'undefined');+var isFirefox = (isBrowser && window.mozInnerScreenX != null);++function getCaretCoordinates(element, position, options) {+ if(!isBrowser) {+ throw new Error('textarea-caret-position#getCaretCoordinates should only be called in a browser');+ }++ var debug = options && options.debug || false;+ if (debug) {+ var el = document.querySelector('#input-textarea-caret-position-mirror-div');+ if ( el ) { el.parentNode.removeChild(el); }+ }-function getCaretCoordinates(element, position) {// mirrored divvar div = document.createElement('div');div.id = 'input-textarea-caret-position-mirror-div';@@ -66,13 +77,18 @@ function getCaretCoordinates(element, position) {// position off-screenstyle.position = 'absolute'; // required to return coordinates properly- style.visibility = 'hidden'; // not 'display: none' because we want rendering+ if (!debug)+ style.visibility = 'hidden'; // not 'display: none' because we want rendering// transfer the element's properties to the divproperties.forEach(function (prop) {style[prop] = computed[prop];});+ // Comment out below as it makes position calculation inaccurate when caret is at+ // end of textarea with scroll bars on a zoomed screen (for instance, Windows+ // display setting, show font size as 150%)+ /*if (isFirefox) {// Firefox lies about the overflow property for textareas: https://bugzilla.mozilla.org/show_bug.cgi?id=984275if (element.scrollHeight > parseInt(computed.height))@@ -80,11 +96,12 @@ function getCaretCoordinates(element, position) {} else {style.overflow = 'hidden'; // for Chrome to not render a scrollbar; IE keeps overflowY = 'scroll'}+ */div.textContent = element.value.substring(0, position);// the second special handling for input type="text" vs textarea: spaces need to be replaced with non-breaking spaces - http://stackoverflow.com/a/13402035/1269037if (element.nodeName === 'INPUT')- div.textContent = div.textContent.replace(/\s/g, "\u00a0");+ div.textContent = div.textContent.replace(/\s/g, '\u00a0');var span = document.createElement('span');// Wrapping must be replicated *exactly*, including when a long word gets@@ -100,15 +117,19 @@ function getCaretCoordinates(element, position) {left: span.offsetLeft + parseInt(computed['borderLeftWidth'])};- document.body.removeChild(div);+ if (debug) {+ span.style.backgroundColor = '#aaa';+ } else {+ document.body.removeChild(div);+ }return coordinates;}-if (typeof module != "undefined" && typeof module.exports != "undefined") {+if (typeof module != 'undefined' && typeof module.exports != 'undefined') {module.exports = getCaretCoordinates;-} else {+} else if(isBrowser){window.getCaretCoordinates = getCaretCoordinates;}-}());+}());\ No newline at end of filediff --git a/web/src/main/java/com/gitplex/server/web/component/markdown/markdown.js b/web/src/main/java/com/gitplex/server/web/component/markdown/markdown.jsindex 9b23abb365..0d39905e06 100644--- a/web/src/main/java/com/gitplex/server/web/component/markdown/markdown.js+++ b/web/src/main/java/com/gitplex/server/web/component/markdown/markdown.js@@ -99,6 +99,17 @@ gitplex.server.markdown = {}}, 500);+ function dispatchInputEvent() {+ if(document.createEventObject) {+ $input[0].fireEvent("input");+ } else {+ var evt = document.createEvent("HTMLEvents");+ evt.initEvent("input", false, true);+ $input[0].dispatchEvent(evt);+ }+ }++ var fontSize = parseInt(getComputedStyle($input[0]).getPropertyValue('font-size'));/** Padding same leading spaces as last line when add a new line. This is useful when* add several list items@@ -124,8 +135,13 @@ gitplex.server.markdown = {} else {$input.caret("\n" + spaces);}- $input[0].dispatchEvent(new Event("input"));- $input.scrollTop(getCaretCoordinates($input[0], $input.caret()).top + 18);++ var caretBottom = getCaretCoordinates($input[0], $input.caret()).top + fontSize;+ if (caretBottom > $input.scrollTop() + $input.height()) {+ $input.scrollTop(caretBottom - $input.height());+ }++ dispatchInputEvent();}});@@ -194,7 +210,7 @@ gitplex.server.markdown = {$input.range("**strong text**").range(selected.start+2, selected.end+2+"strong text".length);}$input.focus();- $input[0].dispatchEvent(new Event("input"));+ dispatchInputEvent();});$head.find(".do-italic").click(function() {@@ -205,7 +221,7 @@ gitplex.server.markdown = {$input.range("_emphasized text_").range(selected.start+1, selected.end+1+"emphasized text".length);}$input.focus();- $input[0].dispatchEvent(new Event("input"));+ dispatchInputEvent();});$head.find(".do-header").click(function() {@@ -216,7 +232,7 @@ gitplex.server.markdown = {$input.range("### heading text").range(selected.start+4, selected.end+4+"heading text".length);}$input.focus();- $input[0].dispatchEvent(new Event("input"));+ dispatchInputEvent();});$head.find(".do-list, .do-orderlist").click(function() {@@ -235,7 +251,7 @@ gitplex.server.markdown = {$input.range(leading + " list text here").range(selected.start+leading.length+1, selected.start+leading.length+1+"list text here".length);}$input.focus();- $input[0].dispatchEvent(new Event("input"));+ dispatchInputEvent();});$head.find(".do-code").click(function() {@@ -253,7 +269,7 @@ gitplex.server.markdown = {$input.range("`code text here`").range(selected.start+1, selected.end+1+"code text here".length);}$input.focus();- $input[0].dispatchEvent(new Event("input"));+ dispatchInputEvent();});$head.find(".do-quote").click(function() {@@ -263,7 +279,7 @@ gitplex.server.markdown = {else$input.range("> quote here").range(selected.start+2, selected.start+2+"quote here".length);$input.focus();- $input[0].dispatchEvent(new Event("input"));+ dispatchInputEvent();});$head.find(".do-emoji").click(function() {@@ -299,7 +315,7 @@ gitplex.server.markdown = {$input.caret(" " + atChar);}$input.atwho("run");- $input[0].dispatchEvent(new Event("input"));+ dispatchInputEvent();});$head.find(".do-image, .do-link").click(function() {@@ -613,7 +629,7 @@ gitplex.server.markdown = {return;$input.caret(":" + $(this).attr("title") + ": ");- $input[0].dispatchEvent(new Event("input"));+ dispatchInputEvent();});$(window).resize();},@@ -639,7 +655,7 @@ gitplex.server.markdown = {$input.range($input.caret()-message.length+offset, $input.caret()-message.length+defaultDescription.length+offset);}- $input[0].dispatchEvent(new Event("input"));+ dispatchInputEvent();},updateUploadMessage: function($input, message, replaceMessage) {var isError = message.indexOf("!!") == 0;