@leonabcd123/modern-caps-lock 3.1.4 → 3.1.5

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.
Files changed (2) hide show
  1. package/lib/index.js +27 -25
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  var _a, _b;
2
2
  import { getCurrentOs } from "./os-detection.js";
3
- let previousCapsState = false;
4
3
  let capsState = false;
5
4
  const os = getCurrentOs();
6
5
  const onCapsChangeCallbacks = [];
@@ -9,10 +8,9 @@ const isMobile = (_b = (_a = navigator.userAgentData) === null || _a === void 0
9
8
  const isiPad = os === "Mac" && isMobile;
10
9
  let isSendingCapsLockState = !isiPad;
11
10
  const afterKeyup = new Map();
12
- function callCallbackIfNeeded() {
13
- const callCallback = previousCapsState !== capsState;
14
- previousCapsState = capsState;
15
- if (callCallback) {
11
+ function setCapsState(newCapsState) {
12
+ if (capsState !== newCapsState) {
13
+ capsState = newCapsState;
16
14
  onCapsChangeCallbacks.forEach((callback) => callback(capsState));
17
15
  }
18
16
  }
@@ -25,8 +23,7 @@ mouseEventsToUpdateOn.forEach((eventType) => {
25
23
  if (!isiPad) {
26
24
  const currentCapsState = getCapsLockModifierState(event);
27
25
  if (!isMobile || !currentCapsState) {
28
- capsState = currentCapsState;
29
- callCallbackIfNeeded();
26
+ setCapsState(currentCapsState);
30
27
  }
31
28
  }
32
29
  });
@@ -34,41 +31,46 @@ mouseEventsToUpdateOn.forEach((eventType) => {
34
31
  document.addEventListener("keyup", (event) => {
35
32
  const setAfterKeyupValue = afterKeyup.get(event.code);
36
33
  if (setAfterKeyupValue !== undefined) {
37
- capsState = setAfterKeyupValue;
38
- callCallbackIfNeeded();
34
+ setCapsState(setAfterKeyupValue);
39
35
  afterKeyup.delete(event.code);
40
36
  return;
41
37
  }
42
38
  if (os === "Mac") {
43
39
  if (event.key === "CapsLock") {
44
- capsState = false;
40
+ setCapsState(false);
41
+ return;
45
42
  }
46
- else {
47
- const currentCapsState = getCapsLockModifierState(event);
48
- if (isSendingCapsLockState || currentCapsState) {
49
- capsState = currentCapsState;
50
- isSendingCapsLockState = true;
51
- }
43
+ const currentCapsState = getCapsLockModifierState(event);
44
+ if (isSendingCapsLockState || currentCapsState) {
45
+ setCapsState(currentCapsState);
46
+ isSendingCapsLockState = true;
52
47
  }
53
48
  }
54
- else if (os === "Windows") {
55
- capsState = getCapsLockModifierState(event);
56
- }
57
49
  else if (event.key !== "CapsLock" && event.key !== "Unidentified") {
58
- capsState = getCapsLockModifierState(event);
50
+ setCapsState(getCapsLockModifierState(event));
59
51
  }
60
- callCallbackIfNeeded();
61
52
  });
62
53
  document.addEventListener("keydown", (event) => {
63
- if (os === "Mac") {
54
+ if (afterKeyup.get(event.code) !== undefined) {
55
+ afterKeyup.delete(event.code);
56
+ }
57
+ if (os === "Windows") {
58
+ setCapsState(getCapsLockModifierState(event));
59
+ }
60
+ else if (os === "Mac") {
64
61
  if (event.key === "CapsLock") {
65
- capsState = true;
66
- callCallbackIfNeeded();
62
+ setCapsState(true);
67
63
  }
68
64
  }
69
65
  else if (os === "Linux") {
70
66
  if (event.key === "CapsLock") {
71
- afterKeyup.set(event.code, !getCapsLockModifierState(event));
67
+ const flippedCapsState = !getCapsLockModifierState(event);
68
+ if (flippedCapsState) {
69
+ setCapsState(true);
70
+ }
71
+ else {
72
+ afterKeyup.set(event.code, flippedCapsState);
73
+ }
72
74
  }
73
75
  }
74
76
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonabcd123/modern-caps-lock",
3
- "version": "3.1.4",
3
+ "version": "3.1.5",
4
4
  "description": "A package that allows you to check whether caps lock is active or not",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",