1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-25 08:26:52 +00:00

Fix editing colours that are not in 6 digit hex format

See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/color#value
This commit is contained in:
Jeremy Ruston 2025-01-08 09:59:04 +00:00
parent 5d1cf251b9
commit 6b39d6aa43
2 changed files with 22 additions and 1 deletions

View File

@ -31,7 +31,12 @@ function SimpleEngine(options) {
if(this.widget.editTag === "textarea") {
this.domNode.appendChild(this.widget.document.createTextNode(this.value));
} else {
this.domNode.value = this.value;
if(this.widget.editType === "color") {
// The <input type="color"> element requires a six digit hex value
this.domNode.value = $tw.utils.convertColorToCSSRGBString(this.value);
} else {
this.domNode.value = this.value;
}
}
// Set the attributes
if(this.widget.editType && this.widget.editTag !== "textarea") {

View File

@ -41,4 +41,20 @@ exports.parseCSSColorObject = function(colourString) {
return c;
};
/*
Convert a Color.js colour to a CSS RGB string suitable for use with the <input type="color"> element
*/
exports.convertColorToCSSRGBString = function(colourString) {
var c = exports.parseCSSColorObject(colourString);
if(c) {
var hex = c.toString({format: "hex"});
if(hex.length === 4) {
hex = "#" + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
}
return hex;
} else {
return null;
}
};
})();