From c93d56667e9382bb72a490d1ec21f3ca59c49ce6 Mon Sep 17 00:00:00 2001 From: Ke Wang Date: Tue, 17 Oct 2023 16:47:46 +0800 Subject: [PATCH] Extend ImageWidget to generate the src based on the encoding format of the image entries (#7783) * Make ImageWidget rendering image tiddler based on encoding of type * change indent * use deserializerType instead of type --- core/modules/widgets/image.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/core/modules/widgets/image.js b/core/modules/widgets/image.js index f14e84c44..533b657cc 100644 --- a/core/modules/widgets/image.js +++ b/core/modules/widgets/image.js @@ -58,24 +58,25 @@ ImageWidget.prototype.render = function(parent,nextSibling) { if(this.wiki.isImageTiddler(this.imageSource)) { var type = tiddler.fields.type, text = tiddler.fields.text, - _canonical_uri = tiddler.fields._canonical_uri; + _canonical_uri = tiddler.fields._canonical_uri, + typeInfo = $tw.config.contentTypeInfo[type] || {}, + deserializerType = typeInfo.deserializerType || type; // If the tiddler has body text then it doesn't need to be lazily loaded if(text) { - // Render the appropriate element for the image type - switch(type) { - case "application/pdf": + // Render the appropriate element for the image type by looking up the encoding in the content type info + var encoding = typeInfo.encoding || "utf8"; + if (encoding === "base64") { + // .pdf .png .jpg etc. + src = "data:" + deserializerType + ";base64," + text; + if (deserializerType === "application/pdf") { tag = "embed"; - src = "data:application/pdf;base64," + text; - break; - case "image/svg+xml": - src = "data:image/svg+xml," + encodeURIComponent(text); - break; - default: - src = "data:" + type + ";base64," + text; - break; + } + } else { + // .svg .tid .xml etc. + src = "data:" + deserializerType + "," + encodeURIComponent(text); } } else if(_canonical_uri) { - switch(type) { + switch(deserializerType) { case "application/pdf": tag = "embed"; src = _canonical_uri;