1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 12:07:19 +00:00
TiddlyWiki5/plugins/tiddlywiki/innerwiki/screenshot.js
Jermolene ca7b62a5f6 Introduce "innerwiki" plugin
From the readme:

This plugin enables TiddlyWiki to embed a modified copy of itself (an "innerwiki"). The primary motivation is to be able to produce screenshot illustrations that are automatically up-to-date with the appearance of TiddlyWiki as it changes over time, or to produce the same screenshot in different languages
2019-01-27 10:57:56 +00:00

84 lines
2.2 KiB
JavaScript

/*\
title: $:/plugins/tiddlywiki/innerwiki/screenshot.js
type: application/javascript
module-type: command
Commands to render tiddlers identified by a filter and save any screenshots identified by <$innerwiki> widgets
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var InnerWikiWidget = require("$:/plugins/tiddlywiki/innerwiki/innerwiki.js").innerwiki;
exports.info = {
name: "screenshot",
synchronous: false
};
var Command = function(params,commander,callback) {
this.params = params;
this.commander = commander;
this.callback = callback;
};
Command.prototype.execute = function() {
var self = this;
if(this.params.length < 1) {
return "Missing filter";
}
var filter = this.params[0],
deviceScaleFactor = parseInt(this.params[1],10) || 1,
tiddlers = this.commander.wiki.filterTiddlers(filter);
// Render each tiddler into a widget tree
var innerWikiWidgets = [];
$tw.utils.each(tiddlers,function(title) {
var parser = self.commander.wiki.parseTiddler(title),
variables = {currentTiddler: title},
widgetNode = self.commander.wiki.makeWidget(parser,{variables: variables}),
container = $tw.fakeDocument.createElement("div");
widgetNode.render(container,null);
// Find any innerwiki widgets
Array.prototype.push.apply(innerWikiWidgets,self.findInnerWikiWidgets(widgetNode));
});
// Asynchronously tell each innerwiki widget to save a screenshot
var processNextInnerWikiWidget = function() {
if(innerWikiWidgets.length > 0) {
var widget = innerWikiWidgets[0];
innerWikiWidgets.shift();
widget.saveScreenshot({
basepath: self.commander.outputPath,
deviceScaleFactor: deviceScaleFactor
},function(err) {
if(err) {
self.callback(err);
}
processNextInnerWikiWidget();
});
} else {
self.callback(null);
}
};
processNextInnerWikiWidget();
return null;
};
Command.prototype.findInnerWikiWidgets = function(widgetNode) {
var self = this,
results = [];
if(widgetNode.saveScreenshot) {
results.push(widgetNode)
}
$tw.utils.each(widgetNode.children,function(childWidget) {
Array.prototype.push.apply(results,self.findInnerWikiWidgets(childWidget));
});
return results;
};
exports.Command = Command;
})();