From 42b79213dd04fe45bc72ee9ca33ba693127224bd Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Wed, 11 Dec 2024 13:58:50 +0000 Subject: [PATCH] Refactor image-to-dom to be a separate plugin --- editions/tw5.com/tiddlywiki.info | 2 +- .../docs.tid} | 3 +- .../files}/LICENSE | 0 .../files}/dom-to-image-more.min.js | 0 .../files}/tiddlywiki.files | 4 +- plugins/tiddlywiki/dom-to-image/plugin.info | 7 ++ plugins/tiddlywiki/dom-to-image/readme.tid | 3 + plugins/tiddlywiki/dom-to-image/startup.js | 78 +++++++++++++++++++ plugins/tiddlywiki/geospatial/readme.tid | 1 - 9 files changed, 92 insertions(+), 6 deletions(-) rename plugins/tiddlywiki/{geospatial/docs/save-dom-to-image.tid => dom-to-image/docs.tid} (96%) rename plugins/tiddlywiki/{geospatial/files/dom-to-image-more => dom-to-image/files}/LICENSE (100%) rename plugins/tiddlywiki/{geospatial/files/dom-to-image-more => dom-to-image/files}/dom-to-image-more.min.js (100%) rename plugins/tiddlywiki/{geospatial/files/dom-to-image-more => dom-to-image/files}/tiddlywiki.files (68%) create mode 100644 plugins/tiddlywiki/dom-to-image/plugin.info create mode 100644 plugins/tiddlywiki/dom-to-image/readme.tid create mode 100644 plugins/tiddlywiki/dom-to-image/startup.js diff --git a/editions/tw5.com/tiddlywiki.info b/editions/tw5.com/tiddlywiki.info index a08cf8876..af3a694b1 100644 --- a/editions/tw5.com/tiddlywiki.info +++ b/editions/tw5.com/tiddlywiki.info @@ -8,7 +8,7 @@ "tiddlywiki/confetti", "tiddlywiki/dynannotate", "tiddlywiki/tour", - "tiddlywiki/geospatial" + "tiddlywiki/dom-to-image" ], "themes": [ "tiddlywiki/vanilla", diff --git a/plugins/tiddlywiki/geospatial/docs/save-dom-to-image.tid b/plugins/tiddlywiki/dom-to-image/docs.tid similarity index 96% rename from plugins/tiddlywiki/geospatial/docs/save-dom-to-image.tid rename to plugins/tiddlywiki/dom-to-image/docs.tid index 8d17a0a42..095406477 100644 --- a/plugins/tiddlywiki/geospatial/docs/save-dom-to-image.tid +++ b/plugins/tiddlywiki/dom-to-image/docs.tid @@ -1,6 +1,5 @@ -title: $:/plugins/tiddlywiki/geospatial/docs/save-dom-to-image +title: $:/plugins/tiddlywiki/dom-to-image/docs caption: tm-save-dom-to-image message -tags: $:/tags/GeospatialDocs !! `tm-save-dom-to-image` message diff --git a/plugins/tiddlywiki/geospatial/files/dom-to-image-more/LICENSE b/plugins/tiddlywiki/dom-to-image/files/LICENSE similarity index 100% rename from plugins/tiddlywiki/geospatial/files/dom-to-image-more/LICENSE rename to plugins/tiddlywiki/dom-to-image/files/LICENSE diff --git a/plugins/tiddlywiki/geospatial/files/dom-to-image-more/dom-to-image-more.min.js b/plugins/tiddlywiki/dom-to-image/files/dom-to-image-more.min.js similarity index 100% rename from plugins/tiddlywiki/geospatial/files/dom-to-image-more/dom-to-image-more.min.js rename to plugins/tiddlywiki/dom-to-image/files/dom-to-image-more.min.js diff --git a/plugins/tiddlywiki/geospatial/files/dom-to-image-more/tiddlywiki.files b/plugins/tiddlywiki/dom-to-image/files/tiddlywiki.files similarity index 68% rename from plugins/tiddlywiki/geospatial/files/dom-to-image-more/tiddlywiki.files rename to plugins/tiddlywiki/dom-to-image/files/tiddlywiki.files index f6069d217..316100c6d 100644 --- a/plugins/tiddlywiki/geospatial/files/dom-to-image-more/tiddlywiki.files +++ b/plugins/tiddlywiki/dom-to-image/files/tiddlywiki.files @@ -4,7 +4,7 @@ "file": "dom-to-image-more.min.js", "fields": { "type": "application/javascript", - "title": "$:/plugins/tiddlywiki/geospatial/dom-to-image-more.js", + "title": "$:/plugins/tiddlywiki/dom-to-image/dom-to-image-more.js", "module-type": "library", "url": "https://github.com/1904labs/dom-to-image-more", "version": "3.5.0" @@ -14,7 +14,7 @@ "file": "LICENSE", "fields": { "type": "text/plain", - "title": "$:/plugins/tiddlywiki/geospatial/dom-to-image-more/LICENSE" + "title": "$:/plugins/tiddlywiki/dom-to-image/dom-to-image-more/LICENSE" } } ] diff --git a/plugins/tiddlywiki/dom-to-image/plugin.info b/plugins/tiddlywiki/dom-to-image/plugin.info new file mode 100644 index 000000000..00c1d740c --- /dev/null +++ b/plugins/tiddlywiki/dom-to-image/plugin.info @@ -0,0 +1,7 @@ +{ + "title": "$:/plugins/tiddlywiki/dom-to-image", + "name": "Dom-to-image", + "description": "Save DOM nodes as images", + "list": "readme docs", + "stability": "STABILITY_1_EXPERIMENTAL" +} diff --git a/plugins/tiddlywiki/dom-to-image/readme.tid b/plugins/tiddlywiki/dom-to-image/readme.tid new file mode 100644 index 000000000..bb041ba4a --- /dev/null +++ b/plugins/tiddlywiki/dom-to-image/readme.tid @@ -0,0 +1,3 @@ +title: $:/plugins/tiddlywiki/dom-to-image/readme + +* [[dom-to-image-more|https://github.com/1904labs/dom-to-image-more]], an open source library for saving web content as images \ No newline at end of file diff --git a/plugins/tiddlywiki/dom-to-image/startup.js b/plugins/tiddlywiki/dom-to-image/startup.js new file mode 100644 index 000000000..adc2ca65d --- /dev/null +++ b/plugins/tiddlywiki/dom-to-image/startup.js @@ -0,0 +1,78 @@ +/*\ +title: $:/plugins/tiddlywiki/dom-to-image/startup.js +type: application/javascript +module-type: startup + +dom-to-image initialisation + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +// Export name and synchronous status +exports.name = "dom-to-image"; +exports.after = ["rootwidget"]; +exports.before = ["render"]; +exports.synchronous = true; + +exports.startup = function() { + $tw.rootWidget.addEventListener("tm-save-dom-to-image",function(event) { + var params = event.paramObject || {}, + domToImage = require("$:/plugins/tiddlywiki/dom-to-image/dom-to-image-more.js"), + domNode = document.querySelector(params.selector || "body.tc-body"); + if(domNode) { + var method = "toPng"; + switch(params.format) { + case "jpeg": + // Intentional fallthrough + case "jpg": + method = "toJpeg"; + break; + case "svg": + method = "toSvg"; + break; + } + domToImage[method](domNode,{ + height: $tw.utils.parseInt(params.height) || domNode.offsetHeight, + width: $tw.utils.parseInt(params.width) || domNode.offsetWidth, + quality: $tw.utils.parseNumber(params.quality), + scale: $tw.utils.parseNumber(params.scale) || 1 + }) + .then(function(dataUrl) { + // Save the image + if(params["save-file"]) { + var link = document.createElement("a"); + link.download = params["save-file"]; + link.href = dataUrl; + link.click(); + } + // Save the tiddler + if(params["save-title"]) { + if(dataUrl.indexOf("data:image/svg+xml;") === 0) { + var commaIndex = dataUrl.indexOf(","); + $tw.wiki.addTiddler(new $tw.Tiddler({ + title: params["save-title"], + type: "image/svg+xml", + "text": decodeURIComponent(dataUrl.substring(commaIndex + 1)) + })); + } else { + var parts = dataUrl.split(";base64,"); + $tw.wiki.addTiddler(new $tw.Tiddler({ + title: params["save-title"], + type: parts[0].split(":")[1], + "text": parts[1] + })); + } + } + }) + .catch(function(error) { + console.error('oops, something went wrong!', error); + }); + } + }); +}; + +})(); diff --git a/plugins/tiddlywiki/geospatial/readme.tid b/plugins/tiddlywiki/geospatial/readme.tid index 8dab3bbb0..df8576218 100644 --- a/plugins/tiddlywiki/geospatial/readme.tid +++ b/plugins/tiddlywiki/geospatial/readme.tid @@ -11,4 +11,3 @@ The Geospatial Plugin incorporates a number of third party libraries and online * [[TravelTime|https://traveltime.com/]], a commercial API for [[geocoding|https://traveltime.com/features/geocoding]], [[routing|https://traveltime.com/features/multi-modal-routing]] and [[isochrones|https://traveltime.com/features/isochrones]] * [[Flickr|https://www.flickr.com/services/api/]], a free API for retrieving geotagged photographs * [[OpenLocationCode|https://github.com/google/open-location-code]], Google's open source library for converting to and from Open Location Codes (also known as [[PlusCodes|https://maps.google.com/pluscodes/]]) -* [[dom-to-image-more|https://github.com/1904labs/dom-to-image-more]], an open source library for saving web content as images