Skip to content

Commit

Permalink
Merge pull request #380 from BenjaminMedia/WILL-1505/fix-for-characte…
Browse files Browse the repository at this point in the history
…r-count-breaking-on-pages

WILL-1505/fix for character count breaking on pages
  • Loading branch information
SorenThorup committed May 29, 2020
2 parents 7fa286a + 0fa022c commit 1c3360e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion contenthub-editor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: ContentHub Editor
* Version: 5.27.0
* Version: 5.27.1
* Plugin URI: https://github.com/BenjaminMedia/contenthub-editor
* Description: This plugin integrates Bonnier Contenthub and adds a custom post type Composite
* Author: Bonnier - Alf Henderson
Expand Down
45 changes: 33 additions & 12 deletions js/text-field-character-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'.acf-input input[type="text"]:not([disabled])', // FILTERING OUT FIELDS LIKE FOCALPOINT ETC.
'.acf-input textarea:not(.acf-field-simple-mde)' // FILTERING OUT THE HIDDEN TEXTAREAS CONNECTED TO THE MARKDOWN FIELDS
];

return selectors.join();
}

Expand All @@ -23,13 +23,18 @@
}

function initCounters() {
const classes = document.body.classList;
if (!whitelistedContentTypes(classes)) {
return;
}

windowReady = true;
const widgets = jQuery('.acf-field-flexible-content .layout:not(.acf-clone)');
widgets.each(function(index, widget) {
widgets.each(function (index, widget) {
const type = jQuery(widget).data('layout');
if (includedWidgetType(type)) {
const textInputs = jQuery(widget).find(getSelectors());
textInputs.each(function(index, el) {
textInputs.each(function (index, el) {
addCountersAndEventListeners(el);
});
}
Expand All @@ -48,14 +53,14 @@
isRunning = true;
const compositeCounters = jQuery('.composite-character-counter');
let total = 0;
compositeCounters.each(function(index, item) {
compositeCounters.each(function (index, item) {
const value = item.innerHTML;
total += parseInt(value);
})
document.getElementById('wp-admin-bar-character-count').innerHTML = "<span style='margin: 0 10px;'>Characters: " + total + "</span>";
isRunning = false;
}

function countCharacters(e) {
let countTimeout;
let el = e.target;
Expand All @@ -67,29 +72,29 @@
}, 2000);
}

acf.addAction('append', function($el) {
acf.addAction('append', function ($el) {
const type = $el.data('layout');
const itemClass = $el.attr('class');
if (includedWidgetType(type) || itemClass === 'acf-row') {
const textInputs = $el.find(getSelectors());
textInputs.each(function(index, el) {
textInputs.each(function (index, el) {
addCountersAndEventListeners(el);
})
}
});

function characterCount(string) {
if (string.length === undefined) {
return 0;
return 0;
}
return string.replace(/\[.*]\(.*\)/g, '')
.replace( /^[#]+[ ]+(.*)$/gm, '$1')
.replace( /\*\*?(.*?)\*?\*/gm, '$1')
.replace(/^[#]+[ ]+(.*)$/gm, '$1')
.replace(/\*\*?(.*?)\*?\*/gm, '$1')
.length;
}

function includedWidgetType(type) {
includedWidgetTypes = [
const includedWidgetTypes = [
'text',
'image',
'gallery',
Expand All @@ -102,4 +107,20 @@

return includedWidgetTypes.includes(type);
}
})();

function whitelistedContentTypes(types) {
const whitelist = [
'post-type-contenthub_composite'
];

let containsType = false;
types.forEach(function (type) {
if (whitelist.includes(type)) {
containsType = true;
}
});

return containsType;
}
}
)();

0 comments on commit 1c3360e

Please sign in to comment.