Skip to content
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

feat: add configurable HTML-ID prefix for SVG codes #218

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14.21.3
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ dotsOptions |object | |Dots styling opt
cornersSquareOptions |object | |Square in the corners styling options
cornersDotOptionsHelper|object | |Dots in the corners styling options
backgroundOptions |object | |QR background styling options
svgOptions |object | |Only specify when you need to set things specific to the SVG implementation.
nodeCanvas |node-canvas | |Only specify when running on a node server for canvas type, please refer to node section below
jsDom |jsdom | |Only specify when running on a node server for svg type, please refer to node section below

Expand Down Expand Up @@ -186,6 +187,12 @@ Property|Type |Default Value|Description
offset |number (`0 - 1`)| |Position of color in gradient range
color |string | |Color of stop in gradient range

`options.svgOptions` structure

Property|Type |Default Value|Description
--------|-------|-------------|----------------------------------------------------------------------------------------------
idPrefix|string?|`undefined` |Use this to set different HTML IDs, for when you are using multiple QR-Codes on the same page.

#### QRCodeStyling methods
`QRCodeStyling.append(container) => void`

Expand Down
26 changes: 19 additions & 7 deletions src/core/QRSVG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export default class QRSVG {
if (element) {
const gradientOptions = options.backgroundOptions?.gradient;
const color = options.backgroundOptions?.color;
const name = this._createId("background-color");

if (gradientOptions || color) {
this._createColor({
Expand All @@ -168,7 +169,7 @@ export default class QRSVG {
y: 0,
height: options.height,
width: options.width,
name: "background-color"
name: name
});
}
}
Expand All @@ -195,9 +196,10 @@ export default class QRSVG {
type: options.dotsOptions.type,
window: this._window
});
const name = this._createId("dot-color");

this._dotsClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
this._dotsClipPath.setAttribute("id", "clip-path-dot-color");
this._dotsClipPath.setAttribute("id", `clip-path-${name}`);
this._defs.appendChild(this._dotsClipPath);

this._createColor({
Expand All @@ -208,7 +210,7 @@ export default class QRSVG {
y: yBeginning,
height: count * dotSize,
width: count * dotSize,
name: "dot-color"
name: name
});

for (let i = 0; i < count; i++) {
Expand Down Expand Up @@ -269,8 +271,10 @@ export default class QRSVG {
let cornersDotClipPath = this._dotsClipPath;

if (options.cornersSquareOptions?.gradient || options.cornersSquareOptions?.color) {
const name = this._createId(`corners-square-color-${column}-${row}`);

cornersSquareClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
cornersSquareClipPath.setAttribute("id", `clip-path-corners-square-color-${column}-${row}`);
cornersSquareClipPath.setAttribute("id", `clip-path-${name}`);
this._defs.appendChild(cornersSquareClipPath);
this._cornersSquareClipPath = this._cornersDotClipPath = cornersDotClipPath = cornersSquareClipPath;

Expand All @@ -282,7 +286,7 @@ export default class QRSVG {
y,
height: cornersSquareSize,
width: cornersSquareSize,
name: `corners-square-color-${column}-${row}`
name: name
});
}

Expand Down Expand Up @@ -326,8 +330,10 @@ export default class QRSVG {
}

if (options.cornersDotOptions?.gradient || options.cornersDotOptions?.color) {
const name = this._createId(`corners-dot-color-${column}-${row}`);

cornersDotClipPath = this._window.document.createElementNS("http://www.w3.org/2000/svg", "clipPath");
cornersDotClipPath.setAttribute("id", `clip-path-corners-dot-color-${column}-${row}`);
cornersDotClipPath.setAttribute("id", `clip-path-${name}`);
this._defs.appendChild(cornersDotClipPath);
this._cornersDotClipPath = cornersDotClipPath;

Expand All @@ -339,7 +345,7 @@ export default class QRSVG {
y: y + dotSize * 2,
height: cornersDotSize,
width: cornersDotSize,
name: `corners-dot-color-${column}-${row}`
name: name
});
}

Expand Down Expand Up @@ -557,4 +563,10 @@ export default class QRSVG {

this._element.appendChild(rect);
}

_createId(name: string): string {
const prefix = this._options.svgOptions?.idPrefix;

return prefix ? `${prefix}-${name}` : name;
}
}
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ export type Options = {
color?: string;
gradient?: Gradient;
};
svgOptions?: {
idPrefix?: string;
};
};

export type FilterFunction = (i: number, j: number) => boolean;
Expand Down