Skip to content

Added barcode format option #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/columns/barcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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);
});