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

Feature/59 create with html element instead of id only #697

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c667224
Send HTML Element instead of Id to scanner
bilal-elchami Feb 8, 2023
dcabcdc
Add backward compatibility
bilal-elchami Feb 8, 2023
6bb601f
Rename variables & format documents
bilal-elchami Feb 8, 2023
006e728
Remove unused var & format documents
bilal-elchami Feb 13, 2023
73d0ebf
Merge branch 'master' into feature/59_Create_With_HTML_Element_Instea…
bilal-elchami Feb 13, 2023
2d937ea
Update package-lock
bilal-elchami Feb 13, 2023
891ce00
Add shadow dom example
bilal-elchami Feb 13, 2023
240dcc8
Update Example & Documentation
bilal-elchami Feb 13, 2023
ddea928
Format example
bilal-elchami Feb 13, 2023
735ef23
Revert 3rd party fix
bilal-elchami Feb 13, 2023
a70cd3a
Reduce constructor complexity & fix back quotes
bilal-elchami Feb 13, 2023
133a752
Initialize the container in the constructor
bilal-elchami Feb 13, 2023
2db7242
Replace single quotes with double quotes
bilal-elchami Feb 13, 2023
d987f39
Revert html5 example & upload new build
bilal-elchami Feb 13, 2023
2c875e8
Merge branch 'master' into feature/59_Create_With_HTML_Element_Instea…
bilal-elchami Feb 25, 2023
88b5a26
Remove manual scan call
bilal-elchami Feb 27, 2023
1bad070
Merge branch 'master' into feature/59_Create_With_HTML_Element_Instea…
bilal-elchami Mar 3, 2023
313e4cb
Revert minified and package lock files
bilal-elchami Mar 3, 2023
33c3428
Update readmes
bilal-elchami Mar 3, 2023
df96228
Revert package lock and minified files
bilal-elchami Mar 3, 2023
77c78b8
Fix Code Analyzer warning
bilal-elchami Mar 3, 2023
bcc6017
Merge branch 'master' into feature/59_Create_With_HTML_Element_Instea…
bilal-elchami Jun 29, 2023
f67598c
Merge latest master and fix test errors
bilal-elchami Jun 29, 2023
5d80902
Fix readme table
bilal-elchami Jun 29, 2023
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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Find more information about this at [developers.google.com](https://developers.g

And in JavaScript code initialize the object and attach listener like this:
```js
const html5QrCode = new Html5Qrcode(/* element id */ "reader");
const html5QrCode = new Html5Qrcode(/* element id or HTML Element*/ "reader");
bilal-elchami marked this conversation as resolved.
Show resolved Hide resolved
// File based scanning
const fileinput = document.getElementById('qr-input-file');
fileinput.addEventListener('change', e => {
Expand Down Expand Up @@ -579,6 +579,14 @@ class Html5Qrcode {
*/
constructor(elementId: string, config: Html5QrcodeFullConfig | undefined);

/**
* Initialize QR Code scanner.
*
* @param element - The HTML DOM element.
* @param verbose - optional configuration object
*/
constructor(element: HTMLElement, config: Html5QrcodeFullConfig | undefined);
bilal-elchami marked this conversation as resolved.
Show resolved Hide resolved

/**
* Start scanning QR codes or barcodes for a given camera.
*
Expand Down Expand Up @@ -715,6 +723,18 @@ class Html5QrcodeScanner {
config: Html5QrcodeScannerConfig | undefined,
verbose: boolean | undefined);

/**
* Creates instance of this class.
*
* @param element The HTML DOM element.
* @param config Extra configurations to tune the code scanner.
* @param verbose - If true, all logs would be printed to console.
*/
public constructor(
element: HTMLElement,
bilal-elchami marked this conversation as resolved.
Show resolved Hide resolved
config: Html5QrcodeScannerConfig | undefined,
verbose: boolean | undefined);

/**
* Renders the User Interface.
*
Expand Down
15 changes: 15 additions & 0 deletions examples/html5-shadow-dom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# html5-qrcode without any external frameworks

## Include the js library in your project
```html
<script src="https://unpkg.com/html5-qrcode"></script>
```

## Query an HTML element from DOM or Shadow DOM and pass it to the library

```js
const myCustomComponent = document.querySelector('custom-component');
const childComponentQrReaderElement = myCustomComponent.shadowRoot.querySelector('div.qr-reader')
var html5QrCodeScanner = new Html5QrcodeScanner(childComponentQrReaderElement, { fps: 10, qrbox: 250 });
html5QrCodeScanner.render(onScanSuccess);
```
bilal-elchami marked this conversation as resolved.
Show resolved Hide resolved
67 changes: 67 additions & 0 deletions examples/html5-shadow-dom/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<html>

<head>
<title>Html-Qrcode Demo</title>

<body>
<h1>This is a Shadow Dom example</h1>
<custom-component></custom-component>
</body>
<script src="/html5-qrcode.min.js"></script>
<script>
// Create a class for the element
class CustomComponent extends HTMLElement {
constructor() {
// Always call super first in constructor
super();

// Create a shadow root
const shadow = this.attachShadow({ mode: 'open' });

// Create elements
const wrapper = document.createElement('div');
const title = document.createElement('h2');
title.innerText = 'HTML5 qr-code Scanner with shadow dom components';
const idReader = document.createElement('div');
idReader.className = 'qr-reader';
idReader.style.width = '500px';
wrapper.appendChild(title);
wrapper.appendChild(idReader);
// Attach the created elements to the shadow dom
shadow.appendChild(wrapper);
}
}

// Define the new element
customElements.define('custom-component', CustomComponent);

function docReady(fn) {
// see if DOM is already available
if (document.readyState === "complete"
|| document.readyState === "interactive") {
// call on next available tick
setTimeout(fn, 1);
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}

docReady(function () {
var lastResult, countResults = 0;
function onScanSuccess(decodedText, decodedResult) {
if (decodedText !== lastResult) {
++countResults;
lastResult = decodedText;
// Handle on success condition with the decoded message.
console.log(`Scan result ${decodedText}`, decodedResult);
}
}
const myCustomComponent = document.querySelector('custom-component');
const childComponentQrReaderElement = myCustomComponent.shadowRoot.querySelector('div.qr-reader')
var html5QrCodeScanner = new Html5QrcodeScanner(childComponentQrReaderElement, { fps: 10, qrbox: 250 });
html5QrCodeScanner.render(onScanSuccess);
});
</script>
</head>

</html>
2 changes: 1 addition & 1 deletion minified/html5-qrcode.min.js

Large diffs are not rendered by default.

Loading