From 96eca32b1105416c317101aa91dd284f835a8ba8 Mon Sep 17 00:00:00 2001 From: Lee Sheng Long Date: Fri, 31 Jan 2020 20:13:28 +1100 Subject: [PATCH] Add download button/link to binary tiddler warning banners in view and edit mode (#4423) --- core/modules/parsers/binaryparser.js | 55 +++++++++++++++++++++++++--- core/modules/widgets/edit-binary.js | 55 +++++++++++++++++++++++++--- core/ui/BinaryWarning.tid | 4 -- 3 files changed, 98 insertions(+), 16 deletions(-) diff --git a/core/modules/parsers/binaryparser.js b/core/modules/parsers/binaryparser.js index ebfd2beec..fb3d38678 100644 --- a/core/modules/parsers/binaryparser.js +++ b/core/modules/parsers/binaryparser.js @@ -3,7 +3,7 @@ title: $:/core/modules/parsers/binaryparser.js type: application/javascript module-type: parser -The video parser parses a video tiddler into an embeddable HTML element +The binary parser parses a binary tiddler into a warning message and download link \*/ (function(){ @@ -13,14 +13,57 @@ The video parser parses a video tiddler into an embeddable HTML element "use strict"; var BINARY_WARNING_MESSAGE = "$:/core/ui/BinaryWarning"; +var EXPORT_BUTTON_IMAGE = "$:/core/images/export-button"; var BinaryParser = function(type,text,options) { - this.tree = [{ - type: "transclude", + // Transclude the binary data tiddler warning message + var warn = { + type: "element", + tag: "p", + children: [{ + type: "transclude", + attributes: { + tiddler: {type: "string", value: BINARY_WARNING_MESSAGE} + } + }] + }; + // Create download link based on binary tiddler title + var link = { + type: "element", + tag: "a", attributes: { - tiddler: {type: "string", value: BINARY_WARNING_MESSAGE} - } - }]; + title: {type: "indirect", textReference: "!!title"}, + download: {type: "indirect", textReference: "!!title"} + }, + children: [{ + type: "transclude", + attributes: { + tiddler: {type: "string", value: EXPORT_BUTTON_IMAGE} + } + }] + }; + // Set the link href to external or internal data URI + if(options._canonical_uri) { + link.attributes.href = { + type: "string", + value: options._canonical_uri + }; + } else if(text) { + link.attributes.href = { + type: "string", + value: "data:" + type + ";base64," + text + }; + } + // Combine warning message and download link in a div + var element = { + type: "element", + tag: "div", + attributes: { + class: {type: "string", value: "tc-binary-warning"} + }, + children: [warn, link] + } + this.tree = [element]; }; exports["application/octet-stream"] = BinaryParser; diff --git a/core/modules/widgets/edit-binary.js b/core/modules/widgets/edit-binary.js index cf2e2ad2c..a4a18f63d 100644 --- a/core/modules/widgets/edit-binary.js +++ b/core/modules/widgets/edit-binary.js @@ -13,6 +13,7 @@ Edit-binary widget; placeholder for editing binary tiddlers "use strict"; var BINARY_WARNING_MESSAGE = "$:/core/ui/BinaryWarning"; +var EXPORT_BUTTON_IMAGE = "$:/core/images/export-button"; var Widget = require("$:/core/modules/widgets/widget.js").widget; @@ -43,13 +44,55 @@ EditBinaryWidget.prototype.render = function(parent,nextSibling) { Compute the internal state of the widget */ EditBinaryWidget.prototype.execute = function() { - // Construct the child widgets - this.makeChildWidgets([{ - type: "transclude", + // Get our parameters + var editTitle = this.getAttribute("tiddler",this.getVariable("currentTiddler")); + var tiddler = this.wiki.getTiddler(editTitle); + var type = tiddler.fields.type; + var text = tiddler.fields.text; + // Transclude the binary data tiddler warning message + var warn = { + type: "element", + tag: "p", + children: [{ + type: "transclude", + attributes: { + tiddler: {type: "string", value: BINARY_WARNING_MESSAGE} + } + }] + }; + // Create download link based on draft tiddler title + var link = { + type: "element", + tag: "a", attributes: { - tiddler: {type: "string", value: BINARY_WARNING_MESSAGE} - } - }]); + title: {type: "indirect", textReference: "!!draft.title"}, + download: {type: "indirect", textReference: "!!draft.title"} + }, + children: [{ + type: "transclude", + attributes: { + tiddler: {type: "string", value: EXPORT_BUTTON_IMAGE} + } + }] + }; + // Set the link href to internal data URI (no external) + if(text) { + link.attributes.href = { + type: "string", + value: "data:" + type + ";base64," + text + }; + } + // Combine warning message and download link in a div + var element = { + type: "element", + tag: "div", + attributes: { + class: {type: "string", value: "tc-binary-warning"} + }, + children: [warn, link] + } + // Construct the child widgets + this.makeChildWidgets([element]); }; /* diff --git a/core/ui/BinaryWarning.tid b/core/ui/BinaryWarning.tid index c6e8d3139..bbf7aef66 100644 --- a/core/ui/BinaryWarning.tid +++ b/core/ui/BinaryWarning.tid @@ -1,8 +1,4 @@ title: $:/core/ui/BinaryWarning \define lingo-base() $:/language/BinaryWarning/ -
- <> - -