FEATURE: adds support for optional delimiter

This commit is contained in:
Joffrey JAFFEUX 2019-06-21 16:41:53 +02:00
parent 4f2a5a2f89
commit 179e86d3e4
1 changed files with 26 additions and 17 deletions

View File

@ -30,6 +30,7 @@
function processChange($cooked, inputEvent, mappings) { function processChange($cooked, inputEvent, mappings) {
const value = inputEvent.target.value; const value = inputEvent.target.value;
const key = inputEvent.target.dataset.key; const key = inputEvent.target.dataset.key;
const delimiter = inputEvent.target.dataset.delimiter;
if (postWidget) { if (postWidget) {
const placeholderIdentifier = `d-placeholder-${postWidget.widget.attrs.topicId}-${postWidget.widget.attrs.id}-${key}`; const placeholderIdentifier = `d-placeholder-${postWidget.widget.attrs.topicId}-${postWidget.widget.attrs.id}-${key}`;
@ -44,7 +45,7 @@
if (value && value.length && value !== "none") { if (value && value.length && value !== "none") {
newValue = value; newValue = value;
} else { } else {
newValue = `${DELIMITER}${key}${DELIMITER}`; newValue = `${delimiter}${key}${delimiter}`;
} }
$cooked.find(VALID_TAGS).each((index, elem) => { $cooked.find(VALID_TAGS).each((index, elem) => {
@ -58,7 +59,7 @@
} }
mapping.forEach(m => { mapping.forEach(m => {
if (m.pattern !== `${DELIMITER}${key}${DELIMITER}`) { if (m.pattern !== `${delimiter}${key}${delimiter}`) {
m.position = m.position + diff; m.position = m.position + diff;
return; return;
} }
@ -98,23 +99,27 @@
} }
function processPlaceholders(placeholders, $cooked, mappings) { function processPlaceholders(placeholders, $cooked, mappings) {
const keys = Object.keys(placeholders); mappings.length = 0;
const pattern = `(${DELIMITER}(?:${keys.join("|")})${DELIMITER})`;
const regex = new RegExp(pattern, "g");
$cooked.find(VALID_TAGS).each((index, elem) => { Object.keys(placeholders).map(placeholderKey => {
const innerHTML = elem.innerHTML; const placeholder = placeholders[placeholderKey];
let match; const pattern = `(${placeholder.delimiter}(?:${placeholderKey})${placeholder.delimiter})`;
const regex = new RegExp(pattern, "g");
mappings[index] = mappings[index] || []; $cooked.find(VALID_TAGS).each((index, elem) => {
const innerHTML = elem.innerHTML;
let match;
while ((match = regex.exec(innerHTML)) != null) { mappings[index] = mappings[index] || [];
mappings[index].push({
pattern: match[1], while ((match = regex.exec(innerHTML)) != null) {
position: match.index, mappings[index].push({
length: match[1].length pattern: match[1],
}); position: match.index,
} length: match[1].length
});
}
});
}); });
} }
@ -133,10 +138,12 @@
const defaultValue = valueFromCookie || elem.dataset.default; const defaultValue = valueFromCookie || elem.dataset.default;
const defaultValues = (elem.dataset.defaults || "").split(",").filter(x => x); const defaultValues = (elem.dataset.defaults || "").split(",").filter(x => x);
const description = elem.dataset.description; const description = elem.dataset.description;
const delimiter = elem.dataset.delimiter || DELIMITER;
placeholders[dataKey] = { placeholders[dataKey] = {
default: defaultValue, default: defaultValue,
defaults: defaultValues, defaults: defaultValues,
delimiter,
description description
} }
@ -151,6 +158,7 @@
const select = document.createElement("select"); const select = document.createElement("select");
select.classList.add("discourse-placeholder-select") select.classList.add("discourse-placeholder-select")
select.dataset.key = dataKey; select.dataset.key = dataKey;
select.dataset.delimiter = delimiter;
if (description) { if (description) {
addSelectOption(select, { value: "none", description }); addSelectOption(select, { value: "none", description });
@ -168,6 +176,7 @@
const input = document.createElement("input"); const input = document.createElement("input");
input.classList.add("discourse-placeholder-value") input.classList.add("discourse-placeholder-value")
input.dataset.key = dataKey; input.dataset.key = dataKey;
input.dataset.delimiter = delimiter;
if (description) { if (description) {
input.setAttribute("placeholder", description); input.setAttribute("placeholder", description);
@ -214,7 +223,7 @@
processChange( processChange(
$cooked, $cooked,
{ target: { value, dataset: { key: placeholderKey } } }, { target: { value, dataset: { key: placeholderKey, delimiter: placeholder.delimiter } } },
mappings mappings
); );
}); });