virtual-keypad 5.14.2 → 5.15.0

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,258 +1,260 @@
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("[Connection established]", true);
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
- onErrorReconnectMessage: this.onErrorReconnectMessage,
231
- });
232
- this.onHandshake();
233
- break;
234
- case "Keypress":
235
- this.onData(data);
236
- break;
237
- // TODO factor out into keypadPeer
238
- case "Heartbeat":
239
- this.lastHeartbeat = performance.now();
240
- break;
241
- default:
242
- console.log("Message type: ", data.message);
243
- }
244
- });
245
- this.conn.on("close", () => {
246
- this.onClose();
247
- this.displayUpdate("Connection reset. Awaiting connection...");
248
- this.conn = null;
249
- });
250
- };
251
- }
252
- /*
253
- Referenced links:
254
- https://stackoverflow.com/questions/28016664/when-you-pass-this-as-an-argument/28016676#28016676
255
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
256
- */
257
-
258
- 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.controlButtons = keypadParameters["controlButtons"];
34
+ this.onErrorReconnectMessage =
35
+ keypadParameters.onErrorReconnectMessage ??
36
+ "Connection lost. Please reconnect...";
37
+
38
+ this.onData = onDataCallback; // What to do on a button-press
39
+ this.onHandshake = () => {
40
+ handshakeCallback();
41
+ this._setupHeartBeatIntervals();
42
+ }; // What to do when the connection is established
43
+ this.onConnection = (connection) => {
44
+ this.#onPeerConnection(connection);
45
+ customConnectionCallback(connection);
46
+ };
47
+ this.onClose = () => {
48
+ this.onPeerClose();
49
+ customCloseCallback();
50
+ };
51
+ this.onError = (err) => {
52
+ this.onPeerError(err);
53
+ customErrorCallback(err);
54
+ };
55
+
56
+ /* Set up callbacks that handle any events related to our peer object. */
57
+ this.peer.on("open", this.#onPeerOpen); // On creation of Receiver (local) Peer object
58
+ this.peer.on("connection", this.onConnection); // On connection with Keypad (remote) Peer object
59
+ this.peer.on("disconnected", this.onPeerDisconnected);
60
+ this.peer.on("close", this.onClose);
61
+ this.peer.on("error", this.onError);
62
+ }
63
+ update = (alphabet = undefined, font = undefined) => {
64
+ // Update alphabet
65
+ if (typeof alphabet !== "undefined") {
66
+ const validAlphabet = this.checkAlphabet(alphabet);
67
+ if (String(this.alphabet) !== String(validAlphabet))
68
+ this.displayUpdate("New alphabet: " + String(validAlphabet), true); // DEBUG
69
+ this.alphabet = validAlphabet; // Store new alphabet
70
+ }
71
+
72
+ // Update font
73
+ // TODO check if the font is supported, somehow
74
+ this.font = font ?? this.font; // Store new font
75
+
76
+ // Update keypad
77
+ try {
78
+ this.conn.send({
79
+ message: "Update",
80
+ alphabet: this.alphabet,
81
+ font: this.font,
82
+ peerID: this.peer.id,
83
+ });
84
+ } catch (e) {
85
+ this.displayUpdate(
86
+ `Error updating! Alphabet: ${String(this.alphabet)}, font: ${
87
+ this.font
88
+ }`,
89
+ e
90
+ ); // DEBUG
91
+ console.error(e);
92
+ }
93
+ };
94
+ disableKeys = (whichKeys = undefined) => {
95
+ const message = {
96
+ message: "Disable",
97
+ };
98
+ if (whichKeys) message.keys = whichKeys;
99
+ try {
100
+ this.conn.send(message);
101
+ } catch (e) {
102
+ this.displayUpdate(`Error disabling keys. Keys: ${whichKeys}`);
103
+ console.error(e);
104
+ }
105
+ };
106
+ enableKeys = (whichKeys = undefined) => {
107
+ const message = {
108
+ message: "Enable",
109
+ };
110
+ if (whichKeys) message.keys = whichKeys;
111
+ try {
112
+ this.conn.send(message);
113
+ } catch (e) {
114
+ this.displayUpdate(`Error enabling keys. Keys: ${whichKeys}`);
115
+ console.error(e);
116
+ }
117
+ };
118
+ updateDisplayMessage = (message) => {
119
+ try {
120
+ this.conn.send({
121
+ message: "UpdateHeader",
122
+ headerContent: message,
123
+ });
124
+ } catch (e) {
125
+ this.displayUpdate("Error in updating message!"); // DEBUG
126
+ console.error(e);
127
+ }
128
+ };
129
+ updateFooterMessage = (message) => {
130
+ try {
131
+ this.conn.send({
132
+ message: "UpdateFooter",
133
+ headerContent: message,
134
+ });
135
+ } catch (e) {
136
+ this.displayUpdate("Error in updating footer message."); // Debug
137
+ console.error(e);
138
+ }
139
+ };
140
+ #verifyKeypadParameters = (keypadParameters) => {
141
+ if (!keypadParameters.hasOwnProperty("alphabet")) {
142
+ console.error(
143
+ "Must provide 'alphabet' parameter to Receiver object. Defaulting to 'CDHKNORSVZ'"
144
+ );
145
+ keypadParameters["alphabet"] = "CDHKNORSVZ".split("");
146
+ } else {
147
+ keypadParameters["alphabet"] = this.checkAlphabet(
148
+ keypadParameters.alphabet
149
+ );
150
+ }
151
+ if (!keypadParameters.hasOwnProperty("font")) {
152
+ console.error(
153
+ "Must provide 'font' parameter to Receiver object. Defaulting to 'Arial'"
154
+ );
155
+ keypadParameters["alphabet"] = "Arial";
156
+ } else {
157
+ // FUTURE verify that the selected font is available
158
+ }
159
+ return keypadParameters;
160
+ };
161
+ #onPeerOpen = (id) => {
162
+ // Workaround for peer.reconnect deleting previous id
163
+ if (this.id === null) {
164
+ this.displayUpdate("Received null id from peer open"); // DEBUG
165
+ this.peer.id = this.lastPeerId;
166
+ } else {
167
+ this.lastPeerId = this.peer.id;
168
+ }
169
+
170
+ const params = {
171
+ // alphabet: this.alphabet,
172
+ // font: this.font,
173
+ peerID: this.peer.id,
174
+ };
175
+
176
+ let queryString = this.queryStringFromObject(params);
177
+ const uri = this.keypadUrl + queryString;
178
+ this.qrURL = uri;
179
+ console.log(this.qrURL);
180
+
181
+ // Display QR code for the participant to scan
182
+ const qrCanvas = document.createElement("canvas");
183
+
184
+ QRCode.toCanvas(qrCanvas, uri, function (error) {
185
+ if (error) console.error(error);
186
+ });
187
+
188
+ // Store encoding of QR code, eg to use as an image source
189
+ this.qrURI = qrCanvas.toDataURL();
190
+
191
+ if (!!document.getElementById(this.targetElement)) {
192
+ document.getElementById(this.targetElement).appendChild(qrCanvas);
193
+ } else {
194
+ console.log("Peer reachable at: ", uri);
195
+ }
196
+ };
197
+ #onPeerConnection = (connection) => {
198
+ // Allow only a single connection
199
+ if (this.conn && this.conn.open) {
200
+ connection.on("open", function () {
201
+ connection.send({
202
+ message: "Rejected",
203
+ info: "Already connected to another client",
204
+ });
205
+ setTimeout(function () {
206
+ connection.close();
207
+ }, 500);
208
+ });
209
+ return;
210
+ }
211
+ this.conn = connection;
212
+ console.log("Connection: ", connection);
213
+ this.displayUpdate("[Connection established]", true);
214
+ this.#ready();
215
+ };
216
+ #ready = () => {
217
+ /*
218
+ * Triggered once a connection has been achieved.
219
+ * Defines callbacks to handle incoming data and connection events.
220
+ */
221
+ // Perform callback with data
222
+ this.conn.on("data", (data) => {
223
+ data = data; // data = JSON.parse(data);
224
+ console.log("Received data: ", data);
225
+ switch (data.message) {
226
+ case "Handshake":
227
+ this.conn.send({
228
+ message: "KeypadParameters",
229
+ alphabet: this.alphabet,
230
+ controlButtons: this.controlButtons,
231
+ font: this.font,
232
+ onErrorReconnectMessage: this.onErrorReconnectMessage,
233
+ });
234
+ this.onHandshake();
235
+ break;
236
+ case "Keypress":
237
+ this.onData(data);
238
+ break;
239
+ // TODO factor out into keypadPeer
240
+ case "Heartbeat":
241
+ this.lastHeartbeat = performance.now();
242
+ break;
243
+ default:
244
+ console.log("Message type: ", data.message);
245
+ }
246
+ });
247
+ this.conn.on("close", () => {
248
+ this.onClose();
249
+ this.displayUpdate("Connection reset. Awaiting connection...");
250
+ this.conn = null;
251
+ });
252
+ };
253
+ }
254
+ /*
255
+ Referenced links:
256
+ https://stackoverflow.com/questions/28016664/when-you-pass-this-as-an-argument/28016676#28016676
257
+ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
258
+ */
259
+
260
+ 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
  });