TiddlyWiki5/plugins/tiddlywiki/qrcode/makeqr.js
Mario Pietsch 8aa558eb2c
Remove module function wrapper and add matching configurations for dprint and eslint (#7596)
* remove blks first try

* dprint.json seems to be OK, some forgotten functions

* add some more space-after-keyword settings

* server remove blks

* add **/files to dprint exclude

* dprint.js fixes a typo

* add boot.js and bootprefix.js to dprint exclude

* dprint change dprint.json

* add dprint fmt as script

* remove jslint comments

* fix whitespace

* fix whitespace

* remove function-wrapper from geospatial plugin

* fix whitespace

* add function wrapper to dyannotate-startup

* remove dpring.json
2025-03-21 17:22:57 +00:00

68 lines
1.7 KiB
JavaScript

/*\
title: $:/plugins/tiddlywiki/qrcode/makeqr.js
type: application/javascript
module-type: macro
Macro to convert a string into a QR Code
\*/
"use strict";
/*
Information about this macro
*/
var qrcode = require("$:/plugins/tiddlywiki/qrcode/qrcode/qrcode.js");
var QRCODE_GENERATION_ERROR_PREFIX = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 300"><text x="0" y="30" fill="red" font-family="Helvetica, sans-serif" font-size="18">',
QRCODE_GENERATION_ERROR_SUFFIX = '</text></svg>';
exports.name = "makeqr";
exports.params = [
{name: "text"},
{name: "size"},
{name: "errorCorrectLevel"},
{name: "fallback"}
];
/*
Run the macro
*/
exports.run = function(text,size,errorCorrectLevel,fallback) {
var result;
try {
result = generateQrCode(text,{size: size, errorCorrectLevel: errorCorrectLevel});
} catch(ex) {
console.log("makeqr error: " + ex);
result = fallback || ("data:image/svg+xml," + encodeURI(QRCODE_GENERATION_ERROR_PREFIX + ex + QRCODE_GENERATION_ERROR_SUFFIX));
}
return result || "";
};
function generateQrCode(text,options) {
options = options || {};
var typeNumber = options.typeNumber || 4,
errorCorrectLevel = options.errorCorrectLevel || "M",
size = options.size || 500,
qr;
try {
qr = qrcode(typeNumber,errorCorrectLevel);
qr.addData(text);
qr.make();
} catch(e) {
if(typeNumber >= 40) {
throw new Error("Text too long to encode");
} else {
return generateQrCode(text, {
size: size,
errorCorrectLevel: errorCorrectLevel,
typeNumber: typeNumber + 1
});
}
}
var cellsize = parseInt(size / qr.getModuleCount()),
margin = parseInt((size - qr.getModuleCount() * cellsize) / 2);
return qr.createImgTag(cellsize, margin, size);
}