Skip to content

Commit

Permalink
Merge pull request #1 from alfonso-salces/dev
Browse files Browse the repository at this point in the history
fix: Remove style attribute from body for prevent unexpected backgrou…
  • Loading branch information
NoelDeMartin authored Sep 5, 2022
2 parents 1c61a0c + d6fc8ba commit 03d34ca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
29 changes: 26 additions & 3 deletions src/common/src/createQRScannerAdapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module.exports = function createQRScanner(cordova){
var initialStyles = { ...document.body.style };
var backgroundTransparent = true;
var backgroundTimeouts = [];

// The native implementations should return their status as ['string':'string']
// dictionaries. Boolean values are encoded to '0' and '1', respectively.
function stringToBool(string) {
Expand Down Expand Up @@ -30,6 +34,21 @@ function convertStatus(statusDictionary) {
};
}

// Reset body style attribute and clear pending timeouts for omit unexpected style changes.
function resetBodyStyles() {
setTimeout(function() {
backgroundTransparent = false;
for (var backgroundTimeout of backgroundTimeouts) {
if (backgroundTimeout) {
clearTimeout(backgroundTimeout);
}
}
for (var key of Object.keys(document.body.style)) {
document.body.style[key] = initialStyles[key] ?? '';
}
}, 100);
}

// Simple utility method to ensure the background is transparent. Used by the
// plugin to force re-rendering immediately after the native webview background
// is made transparent.
Expand All @@ -38,9 +57,11 @@ function clearBackground() {
if (body.style) {
body.style.backgroundColor = 'rgba(0,0,0,0.01)';
body.style.backgroundImage = '';
setTimeout(function() {
body.style.backgroundColor = 'transparent';
}, 1);
if (backgroundTransparent) {
backgroundTimeouts.push(setTimeout(function () {
body.style.backgroundColor = 'transparent';
}, 1));
}
if (body.parentNode && body.parentNode.style) {
body.parentNode.style.backgroundColor = 'transparent';
body.parentNode.style.backgroundImage = '';
Expand Down Expand Up @@ -155,10 +176,12 @@ function doneCallback(callback, clear) {

return {
prepare: function(callback) {
backgroundTransparent = true;
cordova.exec(successCallback(callback), errorCallback(callback), 'QRScanner', 'prepare', []);
},
destroy: function(callback) {
cordova.exec(doneCallback(callback, true), null, 'QRScanner', 'destroy', []);
resetBodyStyles();
},
scan: function(callback) {
if (!callback) {
Expand Down
29 changes: 26 additions & 3 deletions www/www.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ cordovaModule.exports = QRScannerAdapter;
/***/ (function(module, exports) {

module.exports = function createQRScanner(cordova){
var initialStyles = {...document.body.style};
var backgroundTransparent = true;
var backgroundTimeouts = [];

// The native implementations should return their status as ['string':'string']
// dictionaries. Boolean values are encoded to '0' and '1', respectively.
function stringToBool(string) {
Expand Down Expand Up @@ -134,6 +138,21 @@ function convertStatus(statusDictionary) {
};
}

// Reset body style attribute and clear pending timeouts for omit unexpected style changes.
function resetBodyStyles() {
setTimeout(function() {
backgroundTransparent = false;
for (var backgroundTimeout of backgroundTimeouts) {
if (backgroundTimeout) {
clearTimeout(backgroundTimeout);
}
}
for (var key of Object.keys(document.body.style)) {
document.body.style[key] = initialStyles[key] ?? '';
}
}, 100);
}

// Simple utility method to ensure the background is transparent. Used by the
// plugin to force re-rendering immediately after the native webview background
// is made transparent.
Expand All @@ -142,9 +161,11 @@ function clearBackground() {
if (body.style) {
body.style.backgroundColor = 'rgba(0,0,0,0.01)';
body.style.backgroundImage = '';
setTimeout(function() {
body.style.backgroundColor = 'transparent';
}, 1);
if (backgroundTransparent) {
backgroundTimeouts.push(setTimeout(function () {
body.style.backgroundColor = 'transparent';
}, 1));
}
if (body.parentNode && body.parentNode.style) {
body.parentNode.style.backgroundColor = 'transparent';
body.parentNode.style.backgroundImage = '';
Expand Down Expand Up @@ -259,10 +280,12 @@ function doneCallback(callback, clear) {

return {
prepare: function(callback) {
backgroundTransparent = true;
cordova.exec(successCallback(callback), errorCallback(callback), 'QRScanner', 'prepare', []);
},
destroy: function(callback) {
cordova.exec(doneCallback(callback, true), null, 'QRScanner', 'destroy', []);
resetBodyStyles();
},
scan: function(callback) {
if (!callback) {
Expand Down

0 comments on commit 03d34ca

Please sign in to comment.