Autosize textboxes w/out highlighting placeholders

Scrolling within textareas on the page is a bit grim. Which is why we
don’t do it for the textboxes that people use to edit templates.

This commit will allow us to extend the auto-resizing of `<textarea>`s
to those which don’t need the highlighting of placeholders.

The code is still quite coupled to the placeholder highlighting code,
because both work by copying the content of the `<textarea>` into a
`<div>` that underlaps the textbox. This `<div>` is used for both
rendering the placeholder highlights, and calculating the natural height
of the content. So it would be hard/confusing to split the two bits of
code into separate modules.
This commit is contained in:
Chris Hill-Scott
2019-10-16 14:35:18 +01:00
parent 58cfce8760
commit d0ce4d07a3
3 changed files with 78 additions and 11 deletions

View File

@@ -11,6 +11,13 @@
this.start = function(textarea) {
let visibleTextbox;
this.highlightPlaceholders = (
typeof textarea.data('highlightPlaceholders') === 'undefined' ||
!!textarea.data('highlightPlaceholders')
);
this.$textbox = $(textarea)
.wrap(`
<div class='textbox-highlight-wrapper' />
@@ -20,13 +27,19 @@
`))
.on("input", this.update);
this.initialHeight = this.$textbox.height();
visibleTextbox = this.$textbox.clone().appendTo("body").css({
position: 'absolute',
visibility: 'hidden',
display: 'block'
});
this.initialHeight = visibleTextbox.height();
this.$background.css({
'width': this.$textbox.outerWidth(),
'border-width': this.$textbox.css('border-width')
});
visibleTextbox.remove();
this.$textbox
.trigger("input");
@@ -34,6 +47,8 @@
this.resize = () => {
this.$background.width(this.$textbox.outerWidth());
this.$textbox.height(
Math.max(
this.initialHeight,
@@ -47,17 +62,23 @@
};
this.escapedMessage = () => $('<div/>').text(this.$textbox.val()).html();
this.contentEscaped = () => $('<div/>').text(this.$textbox.val()).html();
this.replacePlaceholders = () => this.$background.html(
this.escapedMessage().replace(
tagPattern, (match, name, separator, value) => value && separator ?
`<span class='placeholder-conditional'>((${name}??</span>${value}))` :
`<span class='placeholder'>((${name}${value}))</span>`
)
this.contentReplaced = () => this.contentEscaped().replace(
tagPattern, (match, name, separator, value) => value && separator ?
`<span class='placeholder-conditional'>((${name}??</span>${value}))` :
`<span class='placeholder'>((${name}${value}))</span>`
);
this.update = () => this.replacePlaceholders() && this.resize();
this.update = () => {
this.$background.html(
this.highlightPlaceholders ? this.contentReplaced() : this.contentEscaped()
);
this.resize();
};
};