Skip to content

A platform for community discussion. Free, open, simple.

DEV: Replace jquery.putCursorAtEnd (#9390)

Penar Musarajc3593dd9e212
diff --git a/app/assets/javascripts/discourse/components/composer-editor.js b/app/assets/javascripts/discourse/components/composer-editor.js
index 4ff3b961b10..b14b0af74ac 100644
--- a/app/assets/javascripts/discourse/components/composer-editor.js
+++ b/app/assets/javascripts/discourse/components/composer-editor.js
@@ -30,7 +30,8 @@ import {
clipboardData,
safariHacksDisabled,
caretPosition,
- inCodeBlock
+ inCodeBlock,
+ putCursorAtEnd
} from "discourse/lib/utilities";
import {
validateUploadedFiles,
@@ -123,7 +124,7 @@ export default Component.extend({
@observes("focusTarget")
setFocus() {
if (this.focusTarget === "editor") {
- $(this.element.querySelector("textarea")).putCursorAtEnd();
+ putCursorAtEnd(this.element.querySelector("textarea"));
}
},
@@ -213,7 +214,7 @@ export default Component.extend({
!this.get("composer.canEditTitle") &&
(!this.capabilities.isIOS || safariHacksDisabled())
) {
- $(this.element.querySelector(".d-editor-input")).putCursorAtEnd();
+ putCursorAtEnd(this.element.querySelector(".d-editor-input"));
}
this._bindUploadTarget();
diff --git a/app/assets/javascripts/discourse/components/composer-title.js b/app/assets/javascripts/discourse/components/composer-title.js
index 9ba4f501bfb..e036beef1ad 100644
--- a/app/assets/javascripts/discourse/components/composer-title.js
+++ b/app/assets/javascripts/discourse/components/composer-title.js
@@ -5,9 +5,9 @@ import discourseComputed, { observes } from "discourse-common/utils/decorators";
import { load } from "pretty-text/oneboxer";
import { lookupCache } from "pretty-text/oneboxer-cache";
import { ajax } from "discourse/lib/ajax";
-import afterTransition from "discourse/lib/after-transition";
import ENV from "discourse-common/config/environment";
import EmberObject from "@ember/object";
+import { putCursorAtEnd } from "discourse/lib/utilities";
export default Component.extend({
classNames: ["title-input"],
@@ -17,11 +17,7 @@ export default Component.extend({
didInsertElement() {
this._super(...arguments);
if (this.focusTarget === "title") {
- const $input = $(this.element.querySelector("input"));
-
- afterTransition($(this.element).closest("#reply-control"), () => {
- $input.putCursorAtEnd();
- });
+ putCursorAtEnd(this.element.querySelector("input"));
}
if (this.get("composer.titleLength") > 0) {
@@ -136,14 +132,14 @@ export default Component.extend({
.finally(() => {
this.set("composer.loading", false);
schedule("afterRender", () => {
- $(this.element.querySelector("input")).putCursorAtEnd();
+ putCursorAtEnd(this.element.querySelector("input"));
});
});
} else {
this._updatePost(loadOnebox);
this.set("composer.loading", false);
schedule("afterRender", () => {
- $(this.element.querySelector("input")).putCursorAtEnd();
+ putCursorAtEnd(this.element.querySelector("input"));
});
}
}
diff --git a/app/assets/javascripts/discourse/components/composer-user-selector.js b/app/assets/javascripts/discourse/components/composer-user-selector.js
index 913a9356f7b..34186972076 100644
--- a/app/assets/javascripts/discourse/components/composer-user-selector.js
+++ b/app/assets/javascripts/discourse/components/composer-user-selector.js
@@ -1,6 +1,7 @@
import { schedule } from "@ember/runloop";
import Component from "@ember/component";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
+import { putCursorAtEnd } from "discourse/lib/utilities";
export default Component.extend({
showSelector: true,
@@ -11,7 +12,7 @@ export default Component.extend({
this._super(...arguments);
if (this.focusTarget === "usernames") {
- $(this.element.querySelector("input")).putCursorAtEnd();
+ putCursorAtEnd(this.element.querySelector("input"));
}
},
diff --git a/app/assets/javascripts/discourse/lib/utilities.js b/app/assets/javascripts/discourse/lib/utilities.js
index 03ef200016b..c753f4b0cf4 100644
--- a/app/assets/javascripts/discourse/lib/utilities.js
+++ b/app/assets/javascripts/discourse/lib/utilities.js
@@ -444,5 +444,11 @@ export function inCodeBlock(text, pos) {
return result;
}
+export function putCursorAtEnd(element) {
+ element.focus();
+ const len = element.value.length;
+ element.setSelectionRange(len, len);
+}
+
// This prevents a mini racer crash
export default {};
diff --git a/app/assets/javascripts/vendor.js b/app/assets/javascripts/vendor.js
index 3b36c27ea1e..adc682eba5e 100644
--- a/app/assets/javascripts/vendor.js
+++ b/app/assets/javascripts/vendor.js
@@ -17,7 +17,6 @@
//= require jquery.cookie.js
//= require jquery.fileupload.js
//= require jquery.iframe-transport.js
-//= require jquery.putcursoratend.js
//= require jquery.tagsinput.js
//= require jquery.sortable.js
//= require lodash.js
diff --git a/vendor/assets/javascripts/jquery.putcursoratend.js b/vendor/assets/javascripts/jquery.putcursoratend.js
deleted file mode 100644
index d957d03f748..00000000000
--- a/vendor/assets/javascripts/jquery.putcursoratend.js
+++ /dev/null
@@ -1,38 +0,0 @@
-// jQuery plugin: PutCursorAtEnd 1.0
-// http://plugins.jquery.com/project/PutCursorAtEnd
-// by teedyay
-//
-// Puts the cursor at the end of a textbox/ textarea
-
-// codesnippet: 691e18b1-f4f9-41b4-8fe8-bc8ee51b48d4
-(function($)
-{
- jQuery.fn.putCursorAtEnd = function()
- {
- return this.each(function()
- {
- $(this).focus()
-
- // If this function exists...
- if (this.setSelectionRange)
- {
- // ... then use it
- // (Doesn't work in IE)
-
- // Double the length because Opera is inconsistent about whether a carriage return is one character or two. Sigh.
- var len = $(this).val().length * 2;
- this.setSelectionRange(len, len);
- }
- else
- {
- // ... otherwise replace the contents with itself
- // (Doesn't work in Google Chrome)
- $(this).val($(this).val());
- }
-
- // Scroll to the bottom, in case we're in a tall textarea
- // (Necessary for Firefox and Google Chrome)
- this.scrollTop = 999999;
- });
- };
-})(jQuery);
\ No newline at end of file