From 6418b0daa41547cf65f230aaa8593cd27dea8ae7 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 20 Nov 2023 00:26:19 +0100 Subject: [PATCH] Added barcode format option Added simple option to input format for barcode generation as mentioned by the author of the library. Default remains with CODE128 to be backward compatible with current implementations --- src/columns/barcode.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/columns/barcode.ts b/src/columns/barcode.ts index a6d83ae6..d4c9ee15 100644 --- a/src/columns/barcode.ts +++ b/src/columns/barcode.ts @@ -3,17 +3,6 @@ import * as glide from "../glide"; import JsBarcode from "jsbarcode"; import svgToMiniDataURI from "mini-svg-data-uri"; -function barcode(props: { content: string }): string { - const { content } = props; - - const parent = document.createElement("div"); - const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); - parent.appendChild(svg); - - JsBarcode(svg, content); - return svgToMiniDataURI(parent.innerHTML); -} - export default glide .columnNamed("Barcode") .withCategory("Image") @@ -29,11 +18,24 @@ export default glide ) .withRequiredStringParam("content") + .withStringParam("format", "Format - see https://github.com/lindell/JsBarcode") .withImageResult() .withFailingTest( - { content: "0123456890" }, + { + content: "0123456890", + format: "CODE128" + }, "data:image/svg+xml,%3csvg width='200px' height='142px' x='0px' y='0px' viewBox='0 0 200 142' xmlns='http://www.w3.org/2000/svg' version='1.1' style='transform: translate(0%2c0)'%3e%3crect x='0' y='0' width='200' height='142' style='fill:white%3b'%3e%3c/rect%3e%3cg transform='translate(10%2c 10)' style='fill:black%3b'%3e%3crect x='0' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='6' y='0' width='2' height='100'%3e%3c/rect%3e%3crect x='12' y='0' width='6' height='100'%3e%3c/rect%3e%3crect x='22' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='30' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='36' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='44' y='0' width='6' height='100'%3e%3c/rect%3e%3crect x='52' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='58' y='0' width='6' height='100'%3e%3c/rect%3e%3crect x='66' y='0' width='2' height='100'%3e%3c/rect%3e%3crect x='70' y='0' width='6' height='100'%3e%3c/rect%3e%3crect x='78' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='88' y='0' width='2' height='100'%3e%3c/rect%3e%3crect x='98' y='0' width='2' height='100'%3e%3c/rect%3e%3crect x='104' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='110' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='116' y='0' width='8' height='100'%3e%3c/rect%3e%3crect x='126' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='132' y='0' width='2' height='100'%3e%3c/rect%3e%3crect x='138' y='0' width='2' height='100'%3e%3c/rect%3e%3crect x='144' y='0' width='8' height='100'%3e%3c/rect%3e%3crect x='154' y='0' width='4' height='100'%3e%3c/rect%3e%3crect x='164' y='0' width='6' height='100'%3e%3c/rect%3e%3crect x='172' y='0' width='2' height='100'%3e%3c/rect%3e%3crect x='176' y='0' width='4' height='100'%3e%3c/rect%3e%3ctext style='font: 20px monospace' text-anchor='middle' x='90' y='122'%3e0123456890%3c/text%3e%3c/g%3e%3c/svg%3e" ) - .run(({ content }) => barcode({ content })); + .run(({ content, format }) => { + const codeFormat = format ?? "CODE128"; + + const parent = document.createElement("div"); + const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); + parent.appendChild(svg); + + JsBarcode(svg, content, {format: codeFormat}); + return svgToMiniDataURI(parent.innerHTML); + });