global-input-react
Advanced tools
Comparing version 4.4.0 to 4.5.0
@@ -6,16 +6,6 @@ "use strict"; | ||
}); | ||
exports.displayQRCode = exports.qrCodeLabel = exports.loading = exports.keepConnection = exports.getStateData = exports.reducer = exports.getParingCode = exports.startConnect = exports.sendInitData = exports.isValidInitData = exports.sendValue = exports.disconnect = exports.initialState = void 0; | ||
exports.keepConnection = exports.reducer = exports.startConnect = exports.sendInitData = exports.isValidInitData = exports.sendValue = exports.disconnect = exports.initialState = void 0; | ||
var _react = _interopRequireWildcard(require("react")); | ||
var _qrcode = _interopRequireDefault(require("qrcode.react")); | ||
var _globalInputMessage = require("global-input-message"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
const ACTION_TYPES = { | ||
@@ -43,2 +33,3 @@ START_CONNECT: 1, | ||
connectionCode: null, | ||
pairingCode: null, | ||
errorMessage: null, | ||
@@ -359,4 +350,2 @@ field: null, | ||
exports.getParingCode = getParingCode; | ||
const reducer = (state, action) => { | ||
@@ -378,2 +367,3 @@ switch (action.type) { | ||
connectionCode: action.connectionCode, | ||
pairingCode: getParingCode(), | ||
isConnectionDenied: false | ||
@@ -436,119 +426,4 @@ }; | ||
exports.getStateData = getStateData; | ||
const keepConnection = () => !!mobileData.sender; | ||
exports.keepConnection = keepConnection; | ||
const styles = { | ||
label: { | ||
paddingTop: 20, | ||
color: "#A9C8E6" //#4880ED | ||
}, | ||
qrCode: { | ||
display: "flex", | ||
flexDirection: 'column', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
padding: 5, | ||
backgroundColor: "white" | ||
} | ||
}; | ||
const loading = /*#__PURE__*/_react.default.createElement("svg", { | ||
xmlns: "http://www.w3.org/2000/svg", | ||
width: "50", | ||
height: "50", | ||
viewBox: "0 0 50 50" | ||
}, /*#__PURE__*/_react.default.createElement("path", { | ||
fill: "#C779D0", | ||
d: "M25,5A20.14,20.14,0,0,1,45,22.88a2.51,2.51,0,0,0,2.49,2.26h0A2.52,2.52,0,0,0,50,22.33a25.14,25.14,0,0,0-50,0,2.52,2.52,0,0,0,2.5,2.81h0A2.51,2.51,0,0,0,5,22.88,20.14,20.14,0,0,1,25,5Z" | ||
}, /*#__PURE__*/_react.default.createElement("animateTransform", { | ||
attributeName: "transform", | ||
type: "rotate", | ||
from: "0 25 25", | ||
to: "360 25 25", | ||
dur: "0.8s", | ||
repeatCount: "indefinite" | ||
}))); | ||
exports.loading = loading; | ||
const qrCodeLabel = /*#__PURE__*/_react.default.createElement("div", { | ||
style: styles.label | ||
}, "Scan with ", /*#__PURE__*/_react.default.createElement("a", { | ||
href: "https://globalinput.co.uk/global-input-app/get-app", | ||
rel: "noopener noreferrer", | ||
target: "_blank" | ||
}, " Global Input App")); | ||
exports.qrCodeLabel = qrCodeLabel; | ||
const displayQRCode = (codeContent, level, size, label, maxSize, vspace, hspace) => { | ||
if (!codeContent || size === 0) { | ||
return null; | ||
} | ||
if (size) { | ||
return /*#__PURE__*/_react.default.createElement("div", { | ||
style: styles.qrCode | ||
}, /*#__PURE__*/_react.default.createElement(_qrcode.default, { | ||
value: codeContent, | ||
level: level, | ||
size: size | ||
}), label); | ||
} else { | ||
return /*#__PURE__*/_react.default.createElement("div", { | ||
style: styles.qrCode | ||
}, /*#__PURE__*/_react.default.createElement(ResizeQRCode, { | ||
value: codeContent, | ||
level: level, | ||
maxSize: maxSize, | ||
vspace: vspace, | ||
hspace: hspace | ||
}), label); | ||
} | ||
}; | ||
exports.displayQRCode = displayQRCode; | ||
const ResizeQRCode = ({ | ||
value, | ||
level, | ||
maxSize, | ||
vspace, | ||
hspace | ||
}) => { | ||
const [size, setSize] = (0, _react.useState)(0); | ||
(0, _react.useEffect)(() => { | ||
const handleResize = () => { | ||
let size = 0; | ||
if (window && window.innerWidth && window.innerHeight) { | ||
if (window.innerWidth < window.innerHeight) { | ||
size = window.innerWidth - hspace; | ||
} else { | ||
size = window.innerHeight - vspace; | ||
} | ||
} | ||
setSize(size > maxSize ? maxSize : size); | ||
}; | ||
handleResize(); | ||
window.addEventListener('resize', handleResize); | ||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}); | ||
if (!size) { | ||
return null; | ||
} | ||
return /*#__PURE__*/_react.default.createElement(_qrcode.default, { | ||
value: value, | ||
level: level, | ||
size: size | ||
}); | ||
}; | ||
exports.keepConnection = keepConnection; |
@@ -6,12 +6,3 @@ "use strict"; | ||
}); | ||
var _exportNames = { | ||
useGlobalInputApp: true, | ||
getGlobalInputState: true | ||
}; | ||
exports.getGlobalInputState = exports.useGlobalInputApp = void 0; | ||
var globalInput = _interopRequireWildcard(require("./globalinput")); | ||
var _react = require("react"); | ||
var _globalInputMessage = require("global-input-message"); | ||
@@ -21,3 +12,2 @@ | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _globalInputMessage[key]) return; | ||
@@ -32,168 +22,26 @@ Object.defineProperty(exports, key, { | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } | ||
var _useGlobalInputApp = require("./useGlobalInputApp"); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
const useGlobalInputApp = (config, canConnect = true, configId = "") => { | ||
const [{ | ||
connectionCode, | ||
errorMessage, | ||
field, | ||
isLoading, | ||
isReady, | ||
isError, | ||
isDisconnected, | ||
isConnected, | ||
isConnectionDenied, | ||
initData, | ||
senders | ||
}, dispatch] = (0, _react.useReducer)(globalInput.reducer, globalInput.initialState); | ||
const attached = (0, _react.useRef)(true); | ||
const notify = st => { | ||
if (attached.current) { | ||
dispatch(st); | ||
} else { | ||
console.log(` after-detach-${st.type} `); | ||
Object.keys(_useGlobalInputApp).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _useGlobalInputApp[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _useGlobalInputApp[key]; | ||
} | ||
}); | ||
}); | ||
; | ||
}; | ||
var _widgets = require("./widgets"); | ||
(0, _react.useEffect)(() => { | ||
attached.current = true; | ||
return () => { | ||
attached.current = false; | ||
}; | ||
}, []); | ||
const configRef = (0, _react.useRef)(null); | ||
configRef.current = config; | ||
(0, _react.useEffect)(() => { | ||
if (typeof configRef.current === 'function') { | ||
configRef.current = configRef.current(); | ||
Object.keys(_widgets).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _widgets[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _widgets[key]; | ||
} | ||
if (typeof configRef.current.initData === 'function') { | ||
configRef.current.initData = configRef.current.initData(); | ||
} | ||
if (canConnect || globalInput.keepConnection()) { | ||
globalInput.startConnect(configRef.current, notify); | ||
} // eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [canConnect, configId]); //You don't need to memoize the input parameter of this hook. | ||
const restart = (0, _react.useCallback)(config => { | ||
if (!attached.current) { | ||
console.log(" -restart-not-attached- "); | ||
return; | ||
} | ||
globalInput.disconnect(notify); | ||
globalInput.startConnect(config ? config : configRef.current, notify); | ||
}, []); | ||
const sendInitData = (0, _react.useCallback)(initData => { | ||
if (!attached.current) { | ||
return; | ||
} | ||
if (typeof initData === 'function') { | ||
initData = initData(); | ||
} | ||
if (!globalInput.isValidInitData(initData)) { | ||
console.log(" init-data-set-empty "); | ||
return; | ||
} | ||
globalInput.sendInitData(initData, notify); | ||
}, []); | ||
const disconnect = (0, _react.useCallback)(() => { | ||
globalInput.disconnect(notify); | ||
}, []); | ||
const sendValue = globalInput.sendValue; | ||
const onchange = (0, _react.useRef)(null); | ||
const setOnchange = (0, _react.useCallback)(listener => onchange.current = listener, []); | ||
(0, _react.useEffect)(() => { | ||
if (field && onchange.current && attached.current) { | ||
onchange.current({ | ||
field, | ||
initData, | ||
sendInitData, | ||
sendValue | ||
}); | ||
} | ||
}, [field, initData, sendInitData, sendValue]); | ||
const ConnectQR = (0, _react.useCallback)(({ | ||
level = 'H', | ||
size, | ||
label = globalInput.qrCodeLabel, | ||
loading = globalInput.loading, | ||
maxSize = 400, | ||
vspace = 130, | ||
hspace = 50 | ||
}) => { | ||
if (isLoading) { | ||
return loading; | ||
} | ||
if (!isReady) { | ||
return null; | ||
} | ||
if (!connectionCode || size === 0) { | ||
return null; | ||
} | ||
return globalInput.displayQRCode(connectionCode, level, size, label, maxSize, vspace, hspace); | ||
}, [connectionCode, isReady, isLoading]); | ||
const PairingQR = (0, _react.useCallback)(({ | ||
level = 'H', | ||
size, | ||
label = globalInput.qrCodeLabel, | ||
loading = globalInput.loading, | ||
maxSize = 400, | ||
vspace = 130, | ||
hspace = 50 | ||
}) => { | ||
if (isLoading) { | ||
return loading; | ||
} | ||
if (!isReady) { | ||
return null; | ||
} | ||
const pairingCode = globalInput.getParingCode(); | ||
if (!pairingCode || size === 0) { | ||
return null; | ||
} | ||
return globalInput.displayQRCode(pairingCode, level, size, label, maxSize, vspace, hspace); | ||
}, [isReady, isLoading]); | ||
return { | ||
ConnectQR, | ||
PairingQR, | ||
connectionCode, | ||
field, | ||
errorMessage, | ||
isLoading, | ||
isReady, | ||
isError, | ||
isDisconnected, | ||
isConnected, | ||
isConnectionDenied, | ||
initData, | ||
senders, | ||
sendValue, | ||
sendInitData, | ||
setOnchange, | ||
disconnect, | ||
restart | ||
}; | ||
}; | ||
exports.useGlobalInputApp = useGlobalInputApp; | ||
const getGlobalInputState = globalInput.getStateData; | ||
exports.getGlobalInputState = getGlobalInputState; | ||
}); | ||
}); |
@@ -6,8 +6,25 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
var _exportNames = { | ||
useGlobalInputApp: true | ||
}; | ||
exports.useGlobalInputApp = void 0; | ||
var _react = _interopRequireWildcard(require("react")); | ||
var globalInput = _interopRequireWildcard(require("./globalinput")); | ||
var _react = require("react"); | ||
var _globalInputMessage = require("global-input-message"); | ||
Object.keys(_globalInputMessage).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _globalInputMessage[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _globalInputMessage[key]; | ||
} | ||
}); | ||
}); | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } | ||
@@ -17,5 +34,6 @@ | ||
var _default = (configData, dependencies) => { | ||
const useGlobalInputApp = (config, canConnect = true, configId = "") => { | ||
const [{ | ||
connectionCode, | ||
pairingCode, | ||
errorMessage, | ||
@@ -28,37 +46,85 @@ field, | ||
isConnected, | ||
initData | ||
isConnectionDenied, | ||
initData, | ||
senders | ||
}, dispatch] = (0, _react.useReducer)(globalInput.reducer, globalInput.initialState); | ||
const attached = (0, _react.useRef)(true); | ||
const notify = st => { | ||
if (attached.current) { | ||
dispatch(st); | ||
} else { | ||
console.log(` after-detach-${st.type} `); | ||
} | ||
; | ||
}; | ||
(0, _react.useEffect)(() => { | ||
globalInput.startConnect(dispatch, configData); | ||
}, dependencies ? dependencies : []); //default connect once for the component | ||
attached.current = true; | ||
return () => { | ||
attached.current = false; | ||
}; | ||
}, []); | ||
const configRef = (0, _react.useRef)(null); | ||
configRef.current = config; | ||
(0, _react.useEffect)(() => { | ||
if (typeof configRef.current === 'function') { | ||
configRef.current = configRef.current(); | ||
} | ||
if (typeof configRef.current.initData === 'function') { | ||
configRef.current.initData = configRef.current.initData(); | ||
} | ||
if (canConnect || globalInput.keepConnection()) { | ||
globalInput.startConnect(configRef.current, notify); | ||
} // eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [canConnect, configId]); //You don't need to memoize the input parameter of this hook. | ||
const restart = (0, _react.useCallback)(config => { | ||
if (!attached.current) { | ||
console.log(" -restart-not-attached- "); | ||
return; | ||
} | ||
globalInput.disconnect(notify); | ||
globalInput.startConnect(config ? config : configRef.current, notify); | ||
}, []); | ||
const sendInitData = (0, _react.useCallback)(initData => { | ||
if (!attached.current) { | ||
return; | ||
} | ||
if (typeof initData === 'function') { | ||
initData = initData(); | ||
} | ||
if (!globalInput.isValidInitData(initData)) { | ||
console.log(" init-data-set-empty "); | ||
return; | ||
} | ||
globalInput.sendInitData(initData, notify); | ||
}, []); | ||
const disconnect = (0, _react.useCallback)(() => { | ||
globalInput.disconnect(notify); | ||
}, []); | ||
const sendValue = globalInput.sendValue; | ||
const onchange = (0, _react.useRef)(null); | ||
const setOnchange = (0, _react.useCallback)(listener => onchange.current = listener, []); | ||
(0, _react.useEffect)(() => { | ||
if (field && globalInput.mobileData.onchange) { | ||
globalInput.mobileData.onchange({ | ||
if (field && onchange.current && attached.current) { | ||
onchange.current({ | ||
field, | ||
initData, | ||
sendInitData: globalInput.mobileData.sendInitData, | ||
sendValue: globalInput.sendValue | ||
sendInitData, | ||
sendValue | ||
}); | ||
} | ||
}, [field]); | ||
const ConnectQR = (0, _react.useCallback)(({ | ||
level, | ||
size, | ||
container, | ||
children | ||
}) => { | ||
return globalInput.displayQRCode({ | ||
level, | ||
size, | ||
container, | ||
connectionCode, | ||
isReady, | ||
isLoading, | ||
children | ||
}); | ||
}, [connectionCode, isReady, isLoading]); | ||
}, [field, initData, sendInitData, sendValue]); | ||
return { | ||
ConnectQR, | ||
connectionCode, | ||
pairingCode, | ||
field, | ||
@@ -71,10 +137,13 @@ errorMessage, | ||
isConnected, | ||
isConnectionDenied, | ||
initData, | ||
sendValue: globalInput.sendValue, | ||
sendInitData: globalInput.mobileData.sendInitData, | ||
disconnect: globalInput.mobileData.disconnect, | ||
setOnchange: globalInput.setOnchange | ||
senders, | ||
sendValue, | ||
sendInitData, | ||
setOnchange, | ||
disconnect, | ||
restart | ||
}; | ||
}; | ||
exports.default = _default; | ||
exports.useGlobalInputApp = useGlobalInputApp; |
@@ -55,7 +55,7 @@ import React from 'react'; | ||
hspace?: number; | ||
mobile:GlobalInputData; | ||
}; | ||
export interface GlobalInputData { | ||
ConnectQR: React.FC<ConnectQRProps>, | ||
PairingQR: React.FC<ConnectQRProps>, | ||
connectionCode: string; | ||
pairingCode: string; | ||
field: FormField; | ||
@@ -81,1 +81,3 @@ errorMessage: string; | ||
export function decrypt(content: string, password: string): string; | ||
export const ConnectQR:React.FC<ConnectQRProps>; | ||
export const PairingQR:React.FC<ConnectQRProps>; |
{ | ||
"name": "global-input-react", | ||
"version": "4.4.0", | ||
"version": "4.5.0", | ||
"description": "global input react component", | ||
@@ -45,15 +45,15 @@ "main": "./dist/index", | ||
"devDependencies": { | ||
"@babel/cli": "^7.12.1", | ||
"@babel/core": "^7.12.3", | ||
"@babel/cli": "^7.12.10", | ||
"@babel/core": "^7.12.10", | ||
"@babel/plugin-proposal-class-properties": "^7.12.1", | ||
"@babel/preset-env": "^7.12.1", | ||
"@babel/preset-react": "^7.12.5", | ||
"@babel/register": "^7.12.1", | ||
"@testing-library/jest-dom": "^5.11.5", | ||
"@testing-library/react": "^11.1.1", | ||
"@testing-library/react-hooks": "^3.4.2", | ||
"@testing-library/user-event": "^12.2.0", | ||
"@babel/preset-env": "^7.12.11", | ||
"@babel/preset-react": "^7.12.10", | ||
"@babel/register": "^7.12.10", | ||
"@testing-library/jest-dom": "^5.11.6", | ||
"@testing-library/react": "^11.2.2", | ||
"@testing-library/react-hooks": "^3.7.0", | ||
"@testing-library/user-event": "^12.6.0", | ||
"babel-jest": "^26.6.3", | ||
"babel-loader": "^8.1.0", | ||
"eslint": "^7.13.0", | ||
"babel-loader": "^8.2.2", | ||
"eslint": "^7.16.0", | ||
"eslint-config-airbnb": "^18.2.1", | ||
@@ -66,7 +66,7 @@ "eslint-plugin-import": "^2.22.1", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.1.2", | ||
"prettier": "^2.2.1", | ||
"react": "^17.0.1", | ||
"react-dom": "^17.0.1", | ||
"react-test-renderer": "^17.0.1", | ||
"webpack": "^5.4.0" | ||
"webpack": "^5.11.0" | ||
}, | ||
@@ -73,0 +73,0 @@ "dependencies": { |
@@ -19,3 +19,3 @@ # Global Input React | ||
import {useGlobalInputApp} from 'global-input-react'; | ||
import {useGlobalInputApp,ConnectQR} from 'global-input-react'; | ||
@@ -55,3 +55,3 @@ ``` | ||
```JavaScript | ||
<mobile.ConnectQR/> | ||
<ConnectQR mobile={mobile}/> | ||
``` | ||
@@ -123,3 +123,3 @@ It displays an encrypted QR Code for mobile apps to scan to connect to your application. It contains the content of ```mobile.connectionCode```, which holds an encrypted string value. When decrypted, it provides information on how to connect to your application, including a one-time-use encryption key for initiating an end-to-end encryption process between your application and a mobile app. | ||
When ```useGlobalInputApp``` is invoked for the first time, the module will start to initialize itself. In this phase, ```mobile.isLoading``` is set to true, and ```<mobile.ConnectQR/>``` displays a loading symbol. After the initialization is completed, if the application is ready to accept connection, ```mobile.isReady``` is set to true, and ```<mobile.ConnectQR/>``` displays an encrypted QR Code. When a mobile app has connected to your application, ```mobile.isConnected``` is set to true, and ```<mobile.ConnectQR/>``` displays nothing. Those variables are useful if you would like to control what to display during different phases: | ||
When ```mobile=useGlobalInputApp(...)``` is invoked for the first time, the module will start to initialize itself. In this phase, ```mobile.isLoading``` is set to true, and ```<ConnectQR mobile={mobile}/>``` displays a loading symbol. After the initialization is completed, if the application is ready to accept connection, ```mobile.isReady``` is set to true, and ```<ConnectQR mobile={mobile}/>``` displays an encrypted QR Code. When a mobile app has connected to your application, ```mobile.isConnected``` is set to true, and ```<ConnectQR mobile={mobile}/>``` displays nothing. Those variables are useful if you would like to control what to display during different phases: | ||
```JavaScript | ||
@@ -126,0 +126,0 @@ {mobile.isConnected && (<> |
import React, { useState, useEffect } from "react"; | ||
import QRCode from "qrcode.react"; | ||
import { createMessageConnector } from 'global-input-message'; | ||
@@ -30,2 +28,3 @@ | ||
connectionCode: null, | ||
pairingCode: null, | ||
errorMessage: null, | ||
@@ -275,3 +274,3 @@ field: null, | ||
export const getParingCode = () => { | ||
const getParingCode = () => { | ||
return mobileData.session && mobileData.session.buildPairingData(); | ||
@@ -288,3 +287,3 @@ }; | ||
case ACTION_TYPES.REGISTERED: | ||
state = { ...state, errorMessage: '', field: null, connectionCode: action.connectionCode, isConnectionDenied: false }; | ||
state = { ...state, errorMessage: '', field: null, connectionCode: action.connectionCode, pairingCode: getParingCode(), isConnectionDenied: false }; | ||
break; | ||
@@ -314,3 +313,3 @@ case ACTION_TYPES.RECEIVED_FIELD: | ||
}; | ||
export const getStateData = () => { | ||
const getStateData = () => { | ||
return { | ||
@@ -328,75 +327,1 @@ isLoading: mobileData.mobileState === MobileState.INITIALIZING, | ||
export const keepConnection = () => !!mobileData.sender; | ||
const styles = { | ||
label: { | ||
paddingTop: 20, | ||
color: "#A9C8E6", //#4880ED | ||
}, | ||
qrCode: { | ||
display: "flex", | ||
flexDirection: 'column', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
padding: 5, | ||
backgroundColor: "white" | ||
} | ||
}; | ||
export const loading = (<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50"> | ||
<path fill="#C779D0" d="M25,5A20.14,20.14,0,0,1,45,22.88a2.51,2.51,0,0,0,2.49,2.26h0A2.52,2.52,0,0,0,50,22.33a25.14,25.14,0,0,0-50,0,2.52,2.52,0,0,0,2.5,2.81h0A2.51,2.51,0,0,0,5,22.88,20.14,20.14,0,0,1,25,5Z"> | ||
<animateTransform attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="0.8s" repeatCount="indefinite" /> | ||
</path> | ||
</svg>); | ||
export const qrCodeLabel = ( | ||
<div style={styles.label}> | ||
Scan with <a href="https://globalinput.co.uk/global-input-app/get-app" rel="noopener noreferrer" target="_blank"> Global Input App</a> | ||
</div> | ||
); | ||
export const displayQRCode = (codeContent, level, size, label, maxSize, vspace, hspace) => { | ||
if ((!codeContent) || size === 0) { | ||
return null; | ||
} | ||
if (size) { | ||
return ( | ||
<div style={styles.qrCode}> | ||
<QRCode value={codeContent} level={level} size={size} /> | ||
{label} | ||
</div> | ||
); | ||
} | ||
else { | ||
return ( | ||
<div style={styles.qrCode}> | ||
<ResizeQRCode value={codeContent} level={level} maxSize={maxSize} vspace={vspace} hspace={hspace} /> | ||
{label} | ||
</div> | ||
); | ||
} | ||
} | ||
const ResizeQRCode = ({ value, level, maxSize, vspace, hspace }) => { | ||
const [size, setSize] = useState(0); | ||
useEffect(() => { | ||
const handleResize = () => { | ||
let size = 0; | ||
if (window && window.innerWidth && window.innerHeight) { | ||
if (window.innerWidth < window.innerHeight) { | ||
size = window.innerWidth - hspace; | ||
} | ||
else { | ||
size = window.innerHeight - vspace; | ||
} | ||
} | ||
setSize(size > maxSize ? maxSize : size); | ||
}; | ||
handleResize(); | ||
window.addEventListener('resize', handleResize); | ||
return () => { window.removeEventListener('resize', handleResize) } | ||
}); | ||
if (!size) { | ||
return null; | ||
} | ||
return (<QRCode value={value} level={level} size={size} />); | ||
}; |
146
src/index.js
@@ -1,145 +0,3 @@ | ||
import * as globalInput from './globalinput'; | ||
import { useReducer, useRef, useEffect, useCallback } from "react"; | ||
export * from 'global-input-message'; | ||
export const useGlobalInputApp = (config, canConnect = true, configId = "") => { | ||
const [{ | ||
connectionCode, | ||
errorMessage, | ||
field, | ||
isLoading, | ||
isReady, | ||
isError, | ||
isDisconnected, | ||
isConnected, | ||
isConnectionDenied, | ||
initData, | ||
senders | ||
}, dispatch] = useReducer(globalInput.reducer, globalInput.initialState); | ||
const attached = useRef(true); | ||
const notify = (st) => { | ||
if (attached.current) { | ||
dispatch(st); | ||
} | ||
else { | ||
console.log(` after-detach-${st.type} `); | ||
}; | ||
}; | ||
useEffect(() => { | ||
attached.current = true; | ||
return () => { | ||
attached.current = false; | ||
} | ||
}, []); | ||
const configRef = useRef(null); | ||
configRef.current = config; | ||
useEffect(() => { | ||
if (typeof configRef.current === 'function') { | ||
configRef.current = configRef.current(); | ||
} | ||
if (typeof configRef.current.initData === 'function') { | ||
configRef.current.initData = configRef.current.initData(); | ||
} | ||
if (canConnect || globalInput.keepConnection()) { | ||
globalInput.startConnect(configRef.current, notify); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [canConnect, configId]); //You don't need to memoize the input parameter of this hook. | ||
const restart = useCallback((config) => { | ||
if (!attached.current) { | ||
console.log(" -restart-not-attached- "); | ||
return; | ||
} | ||
globalInput.disconnect(notify); | ||
globalInput.startConnect(config ? config : configRef.current, notify); | ||
}, []) | ||
const sendInitData = useCallback((initData) => { | ||
if (!attached.current) { | ||
return; | ||
} | ||
if (typeof initData === 'function') { | ||
initData = initData(); | ||
} | ||
if (!globalInput.isValidInitData(initData)) { | ||
console.log(" init-data-set-empty "); | ||
return; | ||
} | ||
globalInput.sendInitData(initData, notify); | ||
}, []); | ||
const disconnect = useCallback(() => { | ||
globalInput.disconnect(notify); | ||
}, []); | ||
const sendValue = globalInput.sendValue; | ||
const onchange = useRef(null); | ||
const setOnchange = useCallback((listener) => onchange.current = listener, []); | ||
useEffect(() => { | ||
if (field && onchange.current && attached.current) { | ||
onchange.current({ | ||
field, | ||
initData, | ||
sendInitData, | ||
sendValue | ||
}); | ||
} | ||
}, [field, initData, sendInitData, sendValue]); | ||
const ConnectQR = useCallback(({ level = 'H', size, label = globalInput.qrCodeLabel, loading = globalInput.loading, maxSize = 400, vspace = 130, hspace = 50 }) => { | ||
if (isLoading) { | ||
return loading; | ||
} | ||
if (!isReady) { | ||
return null; | ||
} | ||
if ((!connectionCode) || size === 0) { | ||
return null; | ||
} | ||
return globalInput.displayQRCode(connectionCode, level, size, label, maxSize, vspace, hspace); | ||
}, [connectionCode, isReady, isLoading]); | ||
const PairingQR = useCallback(({ level = 'H', size, label = globalInput.qrCodeLabel, loading = globalInput.loading, maxSize = 400, vspace = 130, hspace = 50 }) => { | ||
if (isLoading) { | ||
return loading; | ||
} | ||
if (!isReady) { | ||
return null; | ||
} | ||
const pairingCode = globalInput.getParingCode(); | ||
if ((!pairingCode) || size === 0) { | ||
return null; | ||
} | ||
return globalInput.displayQRCode(pairingCode, level, size, label, maxSize, vspace, hspace); | ||
}, [isReady, isLoading]); | ||
return { | ||
ConnectQR, | ||
PairingQR, | ||
connectionCode, | ||
field, | ||
errorMessage, | ||
isLoading, | ||
isReady, | ||
isError, | ||
isDisconnected, | ||
isConnected, | ||
isConnectionDenied, | ||
initData, | ||
senders, | ||
sendValue, | ||
sendInitData, | ||
setOnchange, | ||
disconnect, | ||
restart, | ||
}; | ||
}; | ||
const getGlobalInputState = globalInput.getStateData; | ||
export { getGlobalInputState }; | ||
export * from './useGlobalInputApp'; | ||
export * from './widgets'; |
Sorry, the diff of this file is too big to display
15
37553
1254694