DEV: Replace jquery.putCursorAtEnd (#9390)

This commit is contained in:
Penar Musaraj 2020-04-08 16:13:02 -04:00 committed by GitHub
parent 3b7ba8ec99
commit c3593dd9e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 51 deletions

View File

@ -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();

View File

@ -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"));
});
}
}

View File

@ -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"));
}
},

View File

@ -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 {};

View File

@ -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

View File

@ -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);