FEATURE: saved state

This commit is contained in:
Joffrey JAFFEUX 2019-05-14 12:26:11 +02:00
parent 2461cab04e
commit 6bf6ccb095
1 changed files with 19 additions and 4 deletions

View File

@ -1,5 +1,5 @@
<script type="text/discourse-plugin" version="0.8.30">
api.decorateCooked($cooked => {
api.decorateCooked(($cooked, postWidget) => {
const VALID_TAGS = "h1, h2, h3, h4, h5, h6, p, code, blockquote, .md-table, li";
const DELIMITER = "=";
@ -29,6 +29,15 @@
const value = inputEvent.target.value;
const key = inputEvent.target.dataset.key;
if (postWidget) {
const placeholderIdentifier = `d-placeholder-${postWidget.widget.attrs.topicId}-${postWidget.widget.attrs.id}-${key}`;
if (value) {
$.cookie(placeholderIdentifier, value);
} else {
$.removeCookie(placeholderIdentifier);
}
}
let newValue;
if (value && value.length && value !== "none") {
newValue = value;
@ -116,7 +125,13 @@
return;
}
const defaultValue = elem.dataset.default;
let valueFromCookie;
if (postWidget) {
const placeholderIdentifier = `d-placeholder-${postWidget.widget.attrs.topicId}-${postWidget.widget.attrs.id}`;
valueFromCookie = $.cookie(`${placeholderIdentifier}-${dataKey}`);
}
const defaultValue = valueFromCookie || elem.dataset.default;
const defaultValues = (elem.dataset.defaults || "").split(",").filter(x => x);
const description = elem.dataset.description;
@ -159,8 +174,8 @@
input.setAttribute("placeholder", description);
}
if (defaultValue) {
input.value = defaultValue;
if (valueFromCookie || defaultValue) {
input.value = valueFromCookie || defaultValue;
}
elem.appendChild(input)