@leonabcd123/modern-caps-lock 3.1.3 → 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.
package/README.md CHANGED
@@ -83,7 +83,7 @@ if (isCapsLockOn()) {
83
83
 
84
84
  - Windows
85
85
  - Mac
86
- - Linux
86
+ - Linux (excluding Firefox on X11/Xwayland)
87
87
  - iPad
88
88
  - Platforms using GBoard
89
89
 
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  type OnCapsChangeCallback = (capsState: boolean) => void;
2
- export declare function isCapsLockOn(): boolean;
3
- export declare function onCapsLockChange(callback: OnCapsChangeCallback): void;
4
- export {};
2
+ declare function isCapsLockOn(): boolean;
3
+ declare function onCapsLockChange(callback: OnCapsChangeCallback): void;
4
+ export { isCapsLockOn, onCapsLockChange };
package/lib/index.js CHANGED
@@ -1,17 +1,16 @@
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
- let onCapsChangeCallbacks = [];
5
+ const onCapsChangeCallbacks = [];
7
6
  const mouseEventsToUpdateOn = ["mousedown", "mousemove", "wheel"];
8
7
  const isMobile = (_b = (_a = navigator.userAgentData) === null || _a === void 0 ? void 0 : _a.mobile) !== null && _b !== void 0 ? _b : navigator.maxTouchPoints > 1;
9
8
  const isiPad = os === "Mac" && isMobile;
10
9
  let isSendingCapsLockState = !isiPad;
11
- function callCallbackIfNeeded() {
12
- const callCallback = previousCapsState !== capsState;
13
- previousCapsState = capsState;
14
- if (callCallback) {
10
+ const afterKeyup = new Map();
11
+ function setCapsState(newCapsState) {
12
+ if (capsState !== newCapsState) {
13
+ capsState = newCapsState;
15
14
  onCapsChangeCallbacks.forEach((callback) => callback(capsState));
16
15
  }
17
16
  }
@@ -24,49 +23,61 @@ mouseEventsToUpdateOn.forEach((eventType) => {
24
23
  if (!isiPad) {
25
24
  const currentCapsState = getCapsLockModifierState(event);
26
25
  if (!isMobile || !currentCapsState) {
27
- capsState = currentCapsState;
28
- callCallbackIfNeeded();
26
+ setCapsState(currentCapsState);
29
27
  }
30
28
  }
31
29
  });
32
30
  });
33
31
  document.addEventListener("keyup", (event) => {
32
+ const setAfterKeyupValue = afterKeyup.get(event.code);
33
+ if (setAfterKeyupValue !== undefined) {
34
+ setCapsState(setAfterKeyupValue);
35
+ afterKeyup.delete(event.code);
36
+ return;
37
+ }
34
38
  if (os === "Mac") {
35
39
  if (event.key === "CapsLock") {
36
- capsState = false;
40
+ setCapsState(false);
41
+ return;
37
42
  }
38
- else {
39
- const currentCapsState = getCapsLockModifierState(event);
40
- if (isSendingCapsLockState || currentCapsState) {
41
- capsState = currentCapsState;
42
- isSendingCapsLockState = true;
43
- }
43
+ const currentCapsState = getCapsLockModifierState(event);
44
+ if (isSendingCapsLockState || currentCapsState) {
45
+ setCapsState(currentCapsState);
46
+ isSendingCapsLockState = true;
44
47
  }
45
48
  }
46
- else if (os === "Windows") {
47
- capsState = getCapsLockModifierState(event);
48
- }
49
49
  else if (event.key !== "CapsLock" && event.key !== "Unidentified") {
50
- capsState = getCapsLockModifierState(event);
50
+ setCapsState(getCapsLockModifierState(event));
51
51
  }
52
- callCallbackIfNeeded();
53
52
  });
54
53
  document.addEventListener("keydown", (event) => {
55
- 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") {
56
61
  if (event.key === "CapsLock") {
57
- capsState = true;
58
- callCallbackIfNeeded();
62
+ setCapsState(true);
59
63
  }
60
64
  }
61
65
  else if (os === "Linux") {
62
66
  if (event.key === "CapsLock") {
63
- capsState = !getCapsLockModifierState(event);
67
+ const flippedCapsState = !getCapsLockModifierState(event);
68
+ if (flippedCapsState) {
69
+ setCapsState(true);
70
+ }
71
+ else {
72
+ afterKeyup.set(event.code, flippedCapsState);
73
+ }
64
74
  }
65
75
  }
66
76
  });
67
- export function isCapsLockOn() {
77
+ function isCapsLockOn() {
68
78
  return capsState;
69
79
  }
70
- export function onCapsLockChange(callback) {
80
+ function onCapsLockChange(callback) {
71
81
  onCapsChangeCallbacks.push(callback);
72
82
  }
83
+ export { isCapsLockOn, onCapsLockChange };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonabcd123/modern-caps-lock",
3
- "version": "3.1.3",
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",