1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2026-01-22 10:54:46 +00:00

Compare commits

...

14 Commits

Author SHA1 Message Date
Jeremy Ruston
41c7948c02 Merge branch 'master' into tm-save-dom-to-image 2024-12-21 09:47:54 +00:00
Jeremy Ruston
9aa65564d4 Remove ELS marker
Thanks @ericshulman
2024-12-19 09:44:37 +00:00
Jeremy Ruston
ed53a8d580 Add support for oncompletion handler 2024-12-18 19:24:55 +00:00
Jeremy Ruston
42b79213dd Refactor image-to-dom to be a separate plugin 2024-12-11 13:58:50 +00:00
Jeremy Ruston
6e1b58fca7 Document what happens if the selector returns multiple DOM nodes 2024-12-10 12:49:55 +00:00
Jeremy Ruston
925d3b0b4c Allow format="jpg" as well as the more technically correct "jpeg" 2024-12-10 12:48:04 +00:00
Jeremy Ruston
4eed4cbaa5 Document peculiarities of JPEG quality parameter 2024-12-10 12:47:40 +00:00
Jeremy Ruston
fd21908896 Add library version number 2024-12-10 12:47:18 +00:00
Jeremy Ruston
f36b9f248c Add example of saving in SVG format 2024-12-10 09:29:24 +00:00
Jeremy Ruston
c9ce9b192d Fix saving SVG images 2024-12-10 09:23:53 +00:00
Jeremy Ruston
2c271077aa Scale should default to 1x 2024-12-10 09:13:29 +00:00
Jeremy Ruston
377be1e4d4 Merge branch 'master' into tm-save-dom-to-image 2024-12-09 16:30:26 +00:00
Jeremy Ruston
9fb763f991 Temporarily include the geospatial plugin in the Netlify previews 2024-12-09 16:00:06 +00:00
Jeremy Ruston
407cd050aa Add tm-save-dom-to-image message 2024-12-09 15:59:47 +00:00
9 changed files with 278 additions and 1 deletions

View File

@@ -7,7 +7,8 @@
"tiddlywiki/menubar",
"tiddlywiki/confetti",
"tiddlywiki/dynannotate",
"tiddlywiki/tour"
"tiddlywiki/tour",
"tiddlywiki/dom-to-image"
],
"themes": [
"tiddlywiki/vanilla",

View File

@@ -0,0 +1,66 @@
title: $:/plugins/tiddlywiki/dom-to-image/docs
caption: tm-save-dom-to-image message
!! `tm-save-dom-to-image` message
The `tm-save-dom-to-image` message uses a [[third-party library|https://github.com/1904labs/dom-to-image-more]] to save a DOM node as an image. The image can be saved to a file or to a tiddler.
Note that there are some limitations to saving content loaded from external domains due to the same-origin policy. See the documentation for more details.
The following parameters are supported:
|!Parameters |!Description |
|''selector'' |CSS selector identifying the DOM node to be saved as an image (defaults to `body`). If multiple DOM nodes are returned by the selector then the first one is used |
|''format'' |Save format: ''png'', ''jpeg'' or ''svg'' (defaults to ''png'') |
|''quality'' |Optional quality 0 to 1 for JPEG images (note that the default quality is 1, and this default is applied even if the quality is explicitly specified as `0`. To force a low quality JPEG image it is therefore necessary to specify a small non-zero value such as `0.001`) |
|''scale'' |Optional scale factor for the image (defaults to 1) |
|''width'' |Optional width of the image in pixels |
|''height'' |Optional height of the image in pixels |
|''save-file'' |Optional filename to save the image to |
|''save-title'' |Optional title of tiddler to save the image to |
|''oncompletion'' |Optional action string to be invoked when the save operation has completed |
|''var-*'' |Variables to be passed to the completion handler (without the "var-" prefix) |
!! Examples
<$button>
<$action-sendmessage
$message="tm-save-dom-to-image"
selector="body.tc-body"
scale="2"
save-file="screenshot.png"
/>
Save the screen as an image file in PNG format
</$button>
<$button>
<$action-sendmessage
$message="tm-save-dom-to-image"
selector="body.tc-body"
scale="2"
save-title="$:/temp/screenshot.png"
/>
Save the screen as an image tiddler in PNG format
</$button> [[$:/temp/screenshot.png]]
<$button>
<$action-sendmessage
$message="tm-save-dom-to-image"
selector="body.tc-body"
format="jpeg"
scale="1"
quality="0.01"
save-title="$:/temp/screenshot.jpeg"
/>
Save the screen as an image tiddler in low quality JPEG format
</$button> [[$:/temp/screenshot.jpeg]]
<$button>
<$action-sendmessage
$message="tm-save-dom-to-image"
selector="body.tc-body"
format="svg"
save-title="$:/temp/screenshot.svg"
/>
Save the screen as an image tiddler in SVG format
</$button> [[$:/temp/screenshot.svg]]

View File

@@ -0,0 +1,29 @@
The MIT License (MIT)
Copyright 2018 Marc Brooks
https://about.me/idisposable
Copyright 2015 Anatolii Saienko
https://github.com/tsayen
Copyright 2012 Paul Bakaus
http://paulbakaus.com/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,21 @@
{
"tiddlers": [
{
"file": "dom-to-image-more.min.js",
"fields": {
"type": "application/javascript",
"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"
}
},
{
"file": "LICENSE",
"fields": {
"type": "text/plain",
"title": "$:/plugins/tiddlywiki/dom-to-image/dom-to-image-more/LICENSE"
}
}
]
}

View File

@@ -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"
}

View File

@@ -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

View File

@@ -0,0 +1,92 @@
/*\
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() {
var getPropertiesWithPrefix = function(properties,prefix) {
var result = Object.create(null);
$tw.utils.each(properties,function(value,name) {
if(name.indexOf(prefix) === 0) {
result[name.substring(prefix.length)] = properties[name];
}
});
return result;
};
$tw.rootWidget.addEventListener("tm-save-dom-to-image",function(event) {
var self=this,
params = event.paramObject || {},
domToImage = require("$:/plugins/tiddlywiki/dom-to-image/dom-to-image-more.js"),
domNode = document.querySelector(params.selector || "body.tc-body"),
oncompletion = params.oncompletion,
variables = getPropertiesWithPrefix(params,"var-");
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]
}));
}
}
self.wiki.invokeActionString(oncompletion,self,variables,{parentWidget: $tw.rootWidget});
})
.catch(function(error) {
console.error('oops, something went wrong!', error);
});
}
});
};
})();

View File

@@ -28,6 +28,61 @@ exports.startup = function() {
require("$:/plugins/tiddlywiki/geospatial/leaflet.markercluster.js");
}
// Install geolocation message handler
$tw.rootWidget.addEventListener("tm-save-dom-to-image",function(event) {
var params = event.paramObject || {},
domToImage = require("$:/plugins/tiddlywiki/geospatial/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);
});
}
});
// Install geolocation message handler
$tw.rootWidget.addEventListener("tm-request-geolocation",function(event) {
var widget = event.widget,
wiki = widget.wiki || $tw.wiki,