virtual-keypad 5.13.0 → 5.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/receiver.js CHANGED
@@ -1,223 +1,257 @@
1
- // import { QRCode } from "qrcode";
2
- var QRCode = require("qrcode");
3
- import "./receiver.css";
4
- import { KeypadPeer } from "./keypadPeer.js";
5
-
6
- const doNothing = () => undefined;
7
-
8
- /**
9
- * @param {{alphabet: string[], font:string}} keypadParameters
10
- * @param {(data) => void} onDataCallback
11
- * @param {() => void} handshakeCallback
12
- * @param {(connection) => void} customConnectionCallback
13
- * @param {() => void} customCloseCallback
14
- * @param {(error) => void} customErrorCallback
15
- */
16
- class Receiver extends KeypadPeer {
17
- constructor(keypadParameters, onDataCallback=doNothing, handshakeCallback=doNothing, customConnectionCallback=doNothing, customCloseCallback=doNothing, customErrorCallback=doNothing) {
18
- super({ targetElementId: keypadParameters.targetElementId });
19
- keypadParameters = this.#verifyKeypadParameters(keypadParameters);
20
-
21
- this.alphabet = this.checkAlphabet(keypadParameters["alphabet"]); // What symbols to display on the keys
22
- this.font = keypadParameters["font"]; // What fontface to display the symbols in
23
-
24
- this.onData = onDataCallback; // What to do on a button-press
25
- this.onHandshake = () => {handshakeCallback(); this._setupHeartBeatIntervals();} // What to do when the connection is established
26
- this.onConnection = (connection) => {this.#onPeerConnection(connection); customConnectionCallback(connection)};
27
- this.onClose = () => {this.onPeerClose(); customCloseCallback()};
28
- this.onError = (err) => {this.onPeerError(err); customErrorCallback(err)};
29
-
30
- /* Set up callbacks that handle any events related to our peer object. */
31
- this.peer.on("open", this.#onPeerOpen); // On creation of Receiver (local) Peer object
32
- this.peer.on("connection", this.onConnection); // On connection with Keypad (remote) Peer object
33
- this.peer.on("disconnected", this.onPeerDisconnected);
34
- this.peer.on("close", this.onClose);
35
- this.peer.on("error", this.onError);
36
- }
37
- update = (alphabet=undefined, font=undefined) => {
38
- // Update alphabet
39
- if (typeof alphabet !== "undefined"){
40
- const validAlphabet = this.checkAlphabet(alphabet);
41
- if (String(this.alphabet) !== String(validAlphabet)) this.displayUpdate("New alphabet: " + String(validAlphabet), true); // DEBUG
42
- this.alphabet = validAlphabet; // Store new alphabet
43
- }
44
-
45
- // Update font
46
- // TODO check if the font is supported, somehow
47
- this.font = font ?? this.font; // Store new font
48
-
49
- // Update keypad
50
- try {
51
- this.conn.send({
52
- message: "Update",
53
- alphabet: this.alphabet,
54
- font: this.font,
55
- peerID: this.peer.id
56
- });
57
- } catch (e) {
58
- this.displayUpdate(`Error updating! Alphabet: ${String(this.alphabet)}, font: ${this.font}`, e); // DEBUG
59
- console.error(e);
60
- }
61
- };
62
- disableKeys = (whichKeys=undefined) => {
63
- const message = {
64
- message: "Disable",
65
- };
66
- if (whichKeys) message.keys = whichKeys;
67
- try {
68
- this.conn.send(message);
69
- } catch (e) {
70
- this.displayUpdate(`Error disabling keys. Keys: ${whichKeys}`);
71
- console.error(e);
72
- }
73
- };
74
- enableKeys = (whichKeys=undefined) => {
75
- const message = {
76
- message: "Enable",
77
- };
78
- if (whichKeys) message.keys = whichKeys;
79
- try {
80
- this.conn.send(message);
81
- } catch (e) {
82
- this.displayUpdate(`Error enabling keys. Keys: ${whichKeys}`);
83
- console.error(e);
84
- }
85
- };
86
- updateDisplayMessage = (message) => {
87
- try {
88
- this.conn.send({
89
- message: "UpdateHeader",
90
- headerContent: message
91
- });
92
- } catch (e) {
93
- this.displayUpdate("Error in updating message!"); // DEBUG
94
- console.error(e);
95
- }
96
- };
97
- updateFooterMessage = (message) => {
98
- try {
99
- this.conn.send({
100
- message: "UpdateFooter",
101
- headerContent: message
102
- })
103
- } catch (e) {
104
- this.displayUpdate("Error in updating footer message.") // Debug
105
- console.error(e);
106
- }
107
- };
108
- #verifyKeypadParameters = (keypadParameters) => {
109
- if (!keypadParameters.hasOwnProperty("alphabet")) {
110
- console.error(
111
- "Must provide 'alphabet' parameter to Receiver object. Defaulting to 'CDHKNORSVZ'"
112
- );
113
- keypadParameters["alphabet"] = "CDHKNORSVZ".split("");
114
- } else {
115
- keypadParameters["alphabet"] = this.checkAlphabet(keypadParameters.alphabet);
116
- }
117
- if (!keypadParameters.hasOwnProperty("font")) {
118
- console.error(
119
- "Must provide 'font' parameter to Receiver object. Defaulting to 'Arial'"
120
- );
121
- keypadParameters["alphabet"] = "Arial";
122
- } else {
123
- // FUTURE verify that the selected font is available
124
- }
125
- return keypadParameters;
126
- };
127
- #onPeerOpen = (id) => {
128
- // Workaround for peer.reconnect deleting previous id
129
- if (this.id === null) {
130
- this.displayUpdate("Received null id from peer open"); // DEBUG
131
- this.peer.id = this.lastPeerId;
132
- } else {
133
- this.lastPeerId = this.peer.id;
134
- }
135
-
136
- const params = {
137
- // alphabet: this.alphabet,
138
- // font: this.font,
139
- peerID: this.peer.id,
140
- };
141
-
142
- let queryString = this.queryStringFromObject(params);
143
- const uri = this.keypadUrl + queryString;
144
- this.qrURL = uri;
145
- console.log(this.qrURL);
146
-
147
- // Display QR code for the participant to scan
148
- const qrCanvas = document.createElement("canvas");
149
-
150
- QRCode.toCanvas(qrCanvas, uri, function (error) {
151
- if (error) console.error(error);
152
- });
153
-
154
- // Store encoding of QR code, eg to use as an image source
155
- this.qrURI = qrCanvas.toDataURL();
156
-
157
- if (!!document.getElementById(this.targetElement)) {
158
- document.getElementById(this.targetElement).appendChild(qrCanvas);
159
- } else {
160
- console.log("Peer reachable at: ", uri);
161
- }
162
- };
163
- #onPeerConnection = (connection) => {
164
- // Allow only a single connection
165
- if (this.conn && this.conn.open) {
166
- connection.on("open", function () {
167
- connection.send({
168
- message: "Rejected",
169
- info: "Already connected to another client"});
170
- setTimeout(function () {
171
- connection.close();
172
- }, 500);
173
- });
174
- return;
175
- }
176
- this.conn = connection;
177
- console.log("Connection: ", connection);
178
- this.displayUpdate("You typed: ");
179
- this.#ready();
180
- };
181
- #ready = () => {
182
- /*
183
- * Triggered once a connection has been achieved.
184
- * Defines callbacks to handle incoming data and connection events.
185
- */
186
- // Perform callback with data
187
- this.conn.on("data", (data) => {
188
- data = data; // data = JSON.parse(data);
189
- console.log("Received data: ", data);
190
- switch (data.message) {
191
- case "Handshake":
192
- this.conn.send({
193
- message: "KeypadParameters",
194
- alphabet: this.alphabet,
195
- font: this.font,
196
- });
197
- this.onHandshake();
198
- break;
199
- case "Keypress":
200
- this.onData(data);
201
- break;
202
- // TODO factor out into keypadPeer
203
- case "Heartbeat":
204
- this.lastHeartbeat = performance.now();
205
- break;
206
- default:
207
- console.log("Message type: ", data.message);
208
- }
209
- });
210
- this.conn.on("close", () => {
211
- this.onClose();
212
- this.displayUpdate("Connection reset. Awaiting connection...");
213
- this.conn = null;
214
- });
215
- };
216
- }
217
- /*
218
- Referenced links:
219
- https://stackoverflow.com/questions/28016664/when-you-pass-this-as-an-argument/28016676#28016676
220
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
221
- */
222
-
223
- export { Receiver };
1
+ // import { QRCode } from "qrcode";
2
+ var QRCode = require("qrcode");
3
+ import "./receiver.css";
4
+ import { KeypadPeer } from "./keypadPeer.js";
5
+
6
+ const doNothing = () => undefined;
7
+
8
+ /**
9
+ * @param {{alphabet: string[], font:string}} keypadParameters
10
+ * @param {(data) => void} onDataCallback
11
+ * @param {() => void} handshakeCallback
12
+ * @param {(connection) => void} customConnectionCallback
13
+ * @param {() => void} customCloseCallback
14
+ * @param {(error) => void} customErrorCallback
15
+ */
16
+ class Receiver extends KeypadPeer {
17
+ constructor(
18
+ keypadParameters,
19
+ onDataCallback = doNothing,
20
+ handshakeCallback = doNothing,
21
+ customConnectionCallback = doNothing,
22
+ customCloseCallback = doNothing,
23
+ customErrorCallback = doNothing
24
+ ) {
25
+ super({
26
+ targetElementId: keypadParameters.targetElementId,
27
+ onErrorReconnectMessage: keypadParameters.onErrorReconnectMessage,
28
+ });
29
+ keypadParameters = this.#verifyKeypadParameters(keypadParameters);
30
+
31
+ this.alphabet = this.checkAlphabet(keypadParameters["alphabet"]); // What symbols to display on the keys
32
+ this.font = keypadParameters["font"]; // What fontface to display the symbols in
33
+ this.onErrorReconnectMessage =
34
+ keypadParameters.onErrorReconnectMessage ??
35
+ "Connection lost. Please reconnect...";
36
+
37
+ this.onData = onDataCallback; // What to do on a button-press
38
+ this.onHandshake = () => {
39
+ handshakeCallback();
40
+ this._setupHeartBeatIntervals();
41
+ }; // What to do when the connection is established
42
+ this.onConnection = (connection) => {
43
+ this.#onPeerConnection(connection);
44
+ customConnectionCallback(connection);
45
+ };
46
+ this.onClose = () => {
47
+ this.onPeerClose();
48
+ customCloseCallback();
49
+ };
50
+ this.onError = (err) => {
51
+ this.onPeerError(err);
52
+ customErrorCallback(err);
53
+ };
54
+
55
+ /* Set up callbacks that handle any events related to our peer object. */
56
+ this.peer.on("open", this.#onPeerOpen); // On creation of Receiver (local) Peer object
57
+ this.peer.on("connection", this.onConnection); // On connection with Keypad (remote) Peer object
58
+ this.peer.on("disconnected", this.onPeerDisconnected);
59
+ this.peer.on("close", this.onClose);
60
+ this.peer.on("error", this.onError);
61
+ }
62
+ update = (alphabet = undefined, font = undefined) => {
63
+ // Update alphabet
64
+ if (typeof alphabet !== "undefined") {
65
+ const validAlphabet = this.checkAlphabet(alphabet);
66
+ if (String(this.alphabet) !== String(validAlphabet))
67
+ this.displayUpdate("New alphabet: " + String(validAlphabet), true); // DEBUG
68
+ this.alphabet = validAlphabet; // Store new alphabet
69
+ }
70
+
71
+ // Update font
72
+ // TODO check if the font is supported, somehow
73
+ this.font = font ?? this.font; // Store new font
74
+
75
+ // Update keypad
76
+ try {
77
+ this.conn.send({
78
+ message: "Update",
79
+ alphabet: this.alphabet,
80
+ font: this.font,
81
+ peerID: this.peer.id,
82
+ });
83
+ } catch (e) {
84
+ this.displayUpdate(
85
+ `Error updating! Alphabet: ${String(this.alphabet)}, font: ${
86
+ this.font
87
+ }`,
88
+ e
89
+ ); // DEBUG
90
+ console.error(e);
91
+ }
92
+ };
93
+ disableKeys = (whichKeys = undefined) => {
94
+ const message = {
95
+ message: "Disable",
96
+ };
97
+ if (whichKeys) message.keys = whichKeys;
98
+ try {
99
+ this.conn.send(message);
100
+ } catch (e) {
101
+ this.displayUpdate(`Error disabling keys. Keys: ${whichKeys}`);
102
+ console.error(e);
103
+ }
104
+ };
105
+ enableKeys = (whichKeys = undefined) => {
106
+ const message = {
107
+ message: "Enable",
108
+ };
109
+ if (whichKeys) message.keys = whichKeys;
110
+ try {
111
+ this.conn.send(message);
112
+ } catch (e) {
113
+ this.displayUpdate(`Error enabling keys. Keys: ${whichKeys}`);
114
+ console.error(e);
115
+ }
116
+ };
117
+ updateDisplayMessage = (message) => {
118
+ try {
119
+ this.conn.send({
120
+ message: "UpdateHeader",
121
+ headerContent: message,
122
+ });
123
+ } catch (e) {
124
+ this.displayUpdate("Error in updating message!"); // DEBUG
125
+ console.error(e);
126
+ }
127
+ };
128
+ updateFooterMessage = (message) => {
129
+ try {
130
+ this.conn.send({
131
+ message: "UpdateFooter",
132
+ headerContent: message,
133
+ });
134
+ } catch (e) {
135
+ this.displayUpdate("Error in updating footer message."); // Debug
136
+ console.error(e);
137
+ }
138
+ };
139
+ #verifyKeypadParameters = (keypadParameters) => {
140
+ if (!keypadParameters.hasOwnProperty("alphabet")) {
141
+ console.error(
142
+ "Must provide 'alphabet' parameter to Receiver object. Defaulting to 'CDHKNORSVZ'"
143
+ );
144
+ keypadParameters["alphabet"] = "CDHKNORSVZ".split("");
145
+ } else {
146
+ keypadParameters["alphabet"] = this.checkAlphabet(
147
+ keypadParameters.alphabet
148
+ );
149
+ }
150
+ if (!keypadParameters.hasOwnProperty("font")) {
151
+ console.error(
152
+ "Must provide 'font' parameter to Receiver object. Defaulting to 'Arial'"
153
+ );
154
+ keypadParameters["alphabet"] = "Arial";
155
+ } else {
156
+ // FUTURE verify that the selected font is available
157
+ }
158
+ return keypadParameters;
159
+ };
160
+ #onPeerOpen = (id) => {
161
+ // Workaround for peer.reconnect deleting previous id
162
+ if (this.id === null) {
163
+ this.displayUpdate("Received null id from peer open"); // DEBUG
164
+ this.peer.id = this.lastPeerId;
165
+ } else {
166
+ this.lastPeerId = this.peer.id;
167
+ }
168
+
169
+ const params = {
170
+ // alphabet: this.alphabet,
171
+ // font: this.font,
172
+ peerID: this.peer.id,
173
+ };
174
+
175
+ let queryString = this.queryStringFromObject(params);
176
+ const uri = this.keypadUrl + queryString;
177
+ this.qrURL = uri;
178
+ console.log(this.qrURL);
179
+
180
+ // Display QR code for the participant to scan
181
+ const qrCanvas = document.createElement("canvas");
182
+
183
+ QRCode.toCanvas(qrCanvas, uri, function (error) {
184
+ if (error) console.error(error);
185
+ });
186
+
187
+ // Store encoding of QR code, eg to use as an image source
188
+ this.qrURI = qrCanvas.toDataURL();
189
+
190
+ if (!!document.getElementById(this.targetElement)) {
191
+ document.getElementById(this.targetElement).appendChild(qrCanvas);
192
+ } else {
193
+ console.log("Peer reachable at: ", uri);
194
+ }
195
+ };
196
+ #onPeerConnection = (connection) => {
197
+ // Allow only a single connection
198
+ if (this.conn && this.conn.open) {
199
+ connection.on("open", function () {
200
+ connection.send({
201
+ message: "Rejected",
202
+ info: "Already connected to another client",
203
+ });
204
+ setTimeout(function () {
205
+ connection.close();
206
+ }, 500);
207
+ });
208
+ return;
209
+ }
210
+ this.conn = connection;
211
+ console.log("Connection: ", connection);
212
+ this.displayUpdate("You typed: ");
213
+ this.#ready();
214
+ };
215
+ #ready = () => {
216
+ /*
217
+ * Triggered once a connection has been achieved.
218
+ * Defines callbacks to handle incoming data and connection events.
219
+ */
220
+ // Perform callback with data
221
+ this.conn.on("data", (data) => {
222
+ data = data; // data = JSON.parse(data);
223
+ console.log("Received data: ", data);
224
+ switch (data.message) {
225
+ case "Handshake":
226
+ this.conn.send({
227
+ message: "KeypadParameters",
228
+ alphabet: this.alphabet,
229
+ font: this.font,
230
+ });
231
+ this.onHandshake();
232
+ break;
233
+ case "Keypress":
234
+ this.onData(data);
235
+ break;
236
+ // TODO factor out into keypadPeer
237
+ case "Heartbeat":
238
+ this.lastHeartbeat = performance.now();
239
+ break;
240
+ default:
241
+ console.log("Message type: ", data.message);
242
+ }
243
+ });
244
+ this.conn.on("close", () => {
245
+ this.onClose();
246
+ this.displayUpdate("Connection reset. Awaiting connection...");
247
+ this.conn = null;
248
+ });
249
+ };
250
+ }
251
+ /*
252
+ Referenced links:
253
+ https://stackoverflow.com/questions/28016664/when-you-pass-this-as-an-argument/28016676#28016676
254
+ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
255
+ */
256
+
257
+ export { Receiver };
package/webpack.common.js CHANGED
@@ -1,30 +1,30 @@
1
- const path = require('path');
2
-
3
- module.exports = {
4
- entry: './src/main.js',
5
- output: {
6
- path: path.resolve(__dirname, 'dist'),
7
- filename: '[name].js',
8
- library: 'virtualKeypad',
9
- libraryTarget: 'umd',
10
- },
11
- module: {
12
- rules: [
13
- { test: /\.css$/,
14
- use: [ 'style-loader', 'css-loader', ],
15
- },
16
- {
17
- test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
18
- use: [
19
- {
20
- loader: 'file-loader',
21
- options: {
22
- name: '[name].[ext]',
23
- outputPath: 'fonts/'
24
- }
25
- }
26
- ],
27
- },
28
- ]
29
- }
30
- };
1
+ const path = require('path');
2
+
3
+ module.exports = {
4
+ entry: './src/main.js',
5
+ output: {
6
+ path: path.resolve(__dirname, 'dist'),
7
+ filename: '[name].js',
8
+ library: 'virtualKeypad',
9
+ libraryTarget: 'umd',
10
+ },
11
+ module: {
12
+ rules: [
13
+ { test: /\.css$/,
14
+ use: [ 'style-loader', 'css-loader', ],
15
+ },
16
+ {
17
+ test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
18
+ use: [
19
+ {
20
+ loader: 'file-loader',
21
+ options: {
22
+ name: '[name].[ext]',
23
+ outputPath: 'fonts/'
24
+ }
25
+ }
26
+ ],
27
+ },
28
+ ]
29
+ }
30
+ };
package/webpack.dev.js CHANGED
@@ -1,10 +1,10 @@
1
- const { merge } = require('webpack-merge');
2
- const common = require('./webpack.common.js');
3
-
4
- module.exports = merge(common, {
5
- mode: 'development',
6
- devtool: 'inline-source-map',
7
- devServer: {
8
- static: './dist',
9
- },
1
+ const { merge } = require('webpack-merge');
2
+ const common = require('./webpack.common.js');
3
+
4
+ module.exports = merge(common, {
5
+ mode: 'development',
6
+ devtool: 'inline-source-map',
7
+ devServer: {
8
+ static: './dist',
9
+ },
10
10
  });
package/webpack.prod.js CHANGED
@@ -1,7 +1,7 @@
1
- const { merge } = require('webpack-merge');
2
- const common = require('./webpack.common.js');
3
-
4
- module.exports = merge(common, {
5
- mode: 'production',
6
- devtool: 'source-map',
1
+ const { merge } = require('webpack-merge');
2
+ const common = require('./webpack.common.js');
3
+
4
+ module.exports = merge(common, {
5
+ mode: 'production',
6
+ devtool: 'source-map',
7
7
  });