Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-9690: Character counter #272

Merged
merged 11 commits into from
Mar 6, 2024
Merged
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.11.0
20.11.1
2 changes: 1 addition & 1 deletion dist/css/styles.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/characterCounter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 2 additions & 37 deletions hdbt_admin.theme
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Contains HDBT Admin theme.
*/

declare(strict_types=1);

use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
Expand Down Expand Up @@ -84,12 +86,6 @@ function _hdbt_admin_form_template_suggestions(array &$suggestions, array &$vari
if (in_array('koro', $variables['element']['#parents'], TRUE)) {
$suggestions[] = $variables['theme_hook_original'] . '__koro';
}
if (
in_array('field_hero_title', $variables['element']['#parents'], TRUE) ||
in_array('field_hero_desc', $variables['element']['#parents'], TRUE)
) {
$suggestions[] = $variables['theme_hook_original'] . '__character_count';
}
}

/**
Expand Down Expand Up @@ -455,34 +451,3 @@ function hdbt_admin_preprocess_field_multiple_value_form(&$variables) {
}
}
}

/**
* Implements hook_preprocess_HOOK().
*/
function hdbt_admin_preprocess_form_element(array &$variables) {
// Map the counter input tag, step value and total value to variables.
$counter_elements = [
'field_hero_title' => [
'counter_input_tag' => 'input',
'counter_step' => 0,
'counter_total' => 55,
],
'field_hero_desc' => [
'counter_input_tag' => 'textarea',
'counter_step' => 160,
'counter_total' => 200,
],
];

// Set the mapped values to corresponding fields.
foreach ($counter_elements as $counter_element => $counter_values) {
if (
array_key_exists('name', $variables) &&
str_contains($variables['name'], $counter_element)
) {
foreach ($counter_values as $key => $value) {
$variables[$key] = $value;
}
}
}
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 19 additions & 10 deletions src/js/characterCounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
// Get all character counter instances using once().
const counterInstances = once('character-counter', '[data-character-counter]', context);

if (!counterInstances) {
if (counterInstances.length === 0) {
return;
}

Expand Down Expand Up @@ -110,21 +110,23 @@
.insertAdjacentElement('afterend', counterInstance);
}

const updateCharacterCounter = (charCount) => {
warningType = processWarningType(charCount, counterStepChars, counterTotalChars);
charCounter.textContent = characterCounter(charCount, counterTotalChars);
charWarning.textContent = characterWarning(warningType, counterStepChars, counterTotalChars, counterInputTag, counterWarning);
};

// Set initial value for the character counter.
if (textInput.value.length > 0) {
warningType = processWarningType(textInput.value.length, counterStepChars, counterTotalChars);
charCounter.textContent = characterCounter(textInput.value.length, counterTotalChars);
charWarning.textContent = characterWarning(warningType, counterStepChars, counterTotalChars, counterInputTag, counterWarning);
updateCharacterCounter(textInput.value.length);
}

// Handle input tag and textarea tags separately.
if (counterInputTag === 'input') {
// Add event listener to the input tag and process
// the charCounter and charWarning.
textInput.addEventListener('input', function () {
warningType = processWarningType(textInput.value.length, counterStepChars, counterTotalChars);
charCounter.textContent = characterCounter(textInput.value.length, counterTotalChars);
charWarning.textContent = characterWarning(warningType, counterStepChars, counterTotalChars, counterInputTag, counterWarning);
updateCharacterCounter(textInput.value.length);
});
} else {
setTimeout(function () {
Expand All @@ -136,9 +138,16 @@
const editor = ckeditorEditable.ckeditorInstance;
editor.model.document.on('change:data', () => {
// Output the number of words to the counter.
warningType = processWarningType(convertHtmlTags(editor.getData()), counterStepChars, counterTotalChars);
charCounter.textContent = characterCounter(convertHtmlTags(editor.getData()), counterTotalChars);
charWarning.textContent = characterWarning(warningType, counterStepChars, counterTotalChars, counterInputTag, counterWarning);
updateCharacterCounter(convertHtmlTags(editor.getData()));
});
}
// The CKEditor is not used in this textarea. Handle current text
// input normally.
else {
// Add event listener to the input tag and process
// the charCounter and charWarning.
textInput.addEventListener('input', function () {
updateCharacterCounter(textInput.value.length);
});
}
});
Expand Down
7 changes: 6 additions & 1 deletion src/scss/06_components/forms/_character-counter.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.character-counter {
margin-top: -$spacing-quarter;
line-height: calc(17rem / 16);
margin-block: calc(6rem / 16);
}

.character-counter__count {
Expand All @@ -24,3 +25,7 @@
color: $color-black;
margin-inline-start: $spacing-half;
}

.form-item__description + .character-counter {
margin-top: -$spacing-quarter;
}
1 change: 1 addition & 0 deletions src/scss/06_components/layout/_page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
}

// Hide "About text formats" link from text-area fields on node form.
.field--widget-formatted-text-character-counter,
.node-form .field--widget-text-textarea {
.form-type--textarea + .filter-wrapper {
display: none;
Expand Down
1 change: 1 addition & 0 deletions templates/form/form-element--character-count.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ js-form-type-[something] exists on form items. We have to keep them.
<div class="character-counter__warning-text" data-warning-id="form-item--{{ name|clean_class }}"></div>
</div>
{{ attach_library('hdbt_admin/character-counter')}}
{{ attach_library('hdbt/hdbt-icons')}}
</div>
{% endif %}
</div>