mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-25 02:19:12 -04:00
Rename module
HighlightTags was bad because: - we haven’t called placeholders ‘tags’ for a long time - it also does resizing of the `<textarea>`, not just highlighting the placeholders
This commit is contained in:
85
app/assets/javascripts/enhancedTextbox.js
Normal file
85
app/assets/javascripts/enhancedTextbox.js
Normal file
@@ -0,0 +1,85 @@
|
||||
(function(Modules) {
|
||||
"use strict";
|
||||
|
||||
if (
|
||||
!('oninput' in document.createElement('input'))
|
||||
) return;
|
||||
|
||||
const tagPattern = /\(\(([^\)\((\?)]+)(\?\?)?([^\)\(]*)\)\)/g;
|
||||
|
||||
Modules.EnhancedTextbox = function() {
|
||||
|
||||
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' />
|
||||
`)
|
||||
.after(this.$background = $(`
|
||||
<div class="textbox-highlight-background" aria-hidden="true" />
|
||||
`))
|
||||
.on("input", this.update);
|
||||
|
||||
visibleTextbox = this.$textbox.clone().appendTo("body").css({
|
||||
position: 'absolute',
|
||||
visibility: 'hidden',
|
||||
display: 'block'
|
||||
});
|
||||
this.initialHeight = visibleTextbox.height();
|
||||
|
||||
this.$background.css({
|
||||
'border-width': this.$textbox.css('border-width')
|
||||
});
|
||||
|
||||
visibleTextbox.remove();
|
||||
|
||||
this.$textbox
|
||||
.trigger("input");
|
||||
|
||||
};
|
||||
|
||||
this.resize = () => {
|
||||
|
||||
this.$background.width(this.$textbox.outerWidth());
|
||||
|
||||
this.$textbox.height(
|
||||
Math.max(
|
||||
this.initialHeight,
|
||||
this.$background.outerHeight()
|
||||
)
|
||||
);
|
||||
|
||||
if ('stickAtBottomWhenScrolling' in GOVUK) {
|
||||
GOVUK.stickAtBottomWhenScrolling.recalculate();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
this.contentEscaped = () => $('<div/>').text(this.$textbox.val()).html();
|
||||
|
||||
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.$background.html(
|
||||
this.highlightPlaceholders ? this.contentReplaced() : this.contentEscaped()
|
||||
);
|
||||
|
||||
this.resize();
|
||||
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
})(window.GOVUK.Modules);
|
||||
Reference in New Issue
Block a user