fix tsc errs
This commit is contained in:
parent
5bf1e7357a
commit
f69e738e2f
3 changed files with 30 additions and 31 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import React from "react";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { render } from "@testing-library/react";
|
||||
import Shortcut from "./Shortcut";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import * as util from "../../util";
|
||||
import { Platform } from "../../util";
|
||||
import Shortcut from "./Shortcut";
|
||||
|
||||
vi.mock("../../util", () => ({
|
||||
...vi.importActual("../../util"),
|
||||
|
|
@ -15,13 +15,12 @@ vi.mock("../../util", () => ({
|
|||
describe("Shortcut component", () => {
|
||||
beforeEach(() => {
|
||||
vi.resetAllMocks();
|
||||
vi.mocked(util.getPlatform).mockReturnValue('windows');
|
||||
vi.mocked(util.getPlatform).mockReturnValue("windows");
|
||||
vi.mocked(util.getFontSize).mockReturnValue(14);
|
||||
});
|
||||
|
||||
describe("Meta key rendering across platforms", () => {
|
||||
const metaKeyInputs = ["meta", "cmd", "ctrl", "^", "⌘"];
|
||||
const platforms = ["windows", "mac", "linux"];
|
||||
const platforms: Platform[] = ["windows", "mac", "linux"];
|
||||
|
||||
platforms.forEach((platform) => {
|
||||
metaKeyInputs.forEach((metaKey) => {
|
||||
|
|
@ -36,7 +35,7 @@ describe("Shortcut component", () => {
|
|||
const { container } = render(<Shortcut>{metaKey}</Shortcut>);
|
||||
const kbdElement = container.querySelector("kbd");
|
||||
expect(kbdElement?.textContent).toBe(
|
||||
platform === "mac" ? "⌘" : "Ctrl"
|
||||
platform === "mac" ? "⌘" : "Ctrl",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -59,7 +58,7 @@ describe("Shortcut component", () => {
|
|||
|
||||
describe("Delete key rendering across platforms", () => {
|
||||
const deleteKeyInputs = ["backspace", "delete", "⌫"];
|
||||
const platforms = ["windows", "mac", "linux"];
|
||||
const platforms: Platform[] = ["windows", "mac", "linux"];
|
||||
|
||||
platforms.forEach((platform) => {
|
||||
deleteKeyInputs.forEach((deleteKey) => {
|
||||
|
|
@ -67,8 +66,7 @@ describe("Shortcut component", () => {
|
|||
vi.mocked(util.getPlatform).mockReturnValue(platform);
|
||||
const { container } = render(<Shortcut>{deleteKey}</Shortcut>);
|
||||
const kbdElement = container.querySelector("kbd");
|
||||
const expectedText =
|
||||
platform === "mac" ? "Delete ⌫" : "Backspace ⌫";
|
||||
const expectedText = platform === "mac" ? "Delete ⌫" : "Backspace ⌫";
|
||||
expect(kbdElement?.textContent).toBe(expectedText);
|
||||
expect(kbdElement).toHaveClass("keyboard-key-special");
|
||||
});
|
||||
|
|
@ -95,7 +93,7 @@ describe("Shortcut component", () => {
|
|||
|
||||
describe("Option/Alt key rendering across platforms", () => {
|
||||
const optionKeyInputs = ["⌥", "option", "opt", "alt"];
|
||||
const platforms = ["windows", "mac"];
|
||||
const platforms: util.Platform[] = ["windows", "mac"];
|
||||
|
||||
platforms.forEach((platform) => {
|
||||
optionKeyInputs.forEach((optionKey) => {
|
||||
|
|
@ -118,12 +116,12 @@ describe("Shortcut component", () => {
|
|||
|
||||
describe("Font class application for option/alt key", () => {
|
||||
it("should apply 'keyboard-key-special' class for option/alt key", () => {
|
||||
vi.mocked(util.getPlatform).mockReturnValue('windows');
|
||||
vi.mocked(util.getPlatform).mockReturnValue("windows");
|
||||
vi.mocked(util.getFontSize).mockReturnValue(14);
|
||||
vi.mocked(util.getAltKeyLabel).mockImplementation(() => 'Alt');
|
||||
vi.mocked(util.getAltKeyLabel).mockImplementation(() => "Alt");
|
||||
const { container } = render(<Shortcut>alt</Shortcut>);
|
||||
const kbdElement = container.querySelector("kbd");
|
||||
expect(kbdElement).toHaveClass("keyboard-key-special");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import React from "react";
|
||||
import {
|
||||
getFontSize,
|
||||
getPlatform,
|
||||
getMetaKeyLabel,
|
||||
getAltKeyLabel,
|
||||
} from "../../util";
|
||||
import "../../../src/index.css";
|
||||
import {
|
||||
getAltKeyLabel,
|
||||
getFontSize,
|
||||
getMetaKeyLabel,
|
||||
getPlatform,
|
||||
} from "../../util";
|
||||
import "./Shortcut.css";
|
||||
|
||||
interface ShortcutProps {
|
||||
|
|
@ -18,7 +18,7 @@ const metaKeys = ["meta", "⌘", "ctrl", "cmd", "^"];
|
|||
const altKeys = ["alt", "option", "opt", "⌥"];
|
||||
const modifierKeys = [...metaKeys, ...altKeys];
|
||||
|
||||
const getSpecialKeyMap = (platform: string) => ({
|
||||
const getSpecialKeyMap = (platform: string): Record<string, string> => ({
|
||||
uparrow: "UpArrow ↑",
|
||||
downarrow: "DownArrow ↓",
|
||||
leftarrow: "LeftArrow ←",
|
||||
|
|
@ -33,7 +33,7 @@ const getSpecialKeyMap = (platform: string) => ({
|
|||
const parseShortcut = (shortcut: string, platform: string) => {
|
||||
if (!shortcut || typeof shortcut !== "string") {
|
||||
console.warn("Invalid shortcut provided:", shortcut);
|
||||
return [];
|
||||
return [];
|
||||
}
|
||||
|
||||
const specialKeyMap = getSpecialKeyMap(platform);
|
||||
|
|
@ -43,15 +43,15 @@ const parseShortcut = (shortcut: string, platform: string) => {
|
|||
combo
|
||||
.trim()
|
||||
.split(" ")
|
||||
.filter((key) => key)
|
||||
.filter((key) => key)
|
||||
.map((key) => {
|
||||
const lowerKey = key.toLowerCase();
|
||||
if (metaKeys.includes(lowerKey)) return getMetaKeyLabel();
|
||||
if (altKeys.includes(lowerKey)) return getAltKeyLabel();
|
||||
return specialKeyMap[lowerKey] || capitalizeKey(key);
|
||||
})
|
||||
}),
|
||||
)
|
||||
.filter((combo) => combo.length > 0);
|
||||
.filter((combo) => combo.length > 0);
|
||||
};
|
||||
|
||||
const capitalizeKey = (key: string) =>
|
||||
|
|
@ -82,7 +82,8 @@ const Shortcut: React.FC<ShortcutProps> = ({ children }) => {
|
|||
}`}
|
||||
style={{
|
||||
fontSize:
|
||||
key && modifierKeys.includes(key.toLowerCase()) &&
|
||||
key &&
|
||||
modifierKeys.includes(key.toLowerCase()) &&
|
||||
platform === "mac"
|
||||
? `${fontSize - 2}px`
|
||||
: `${fontSize - 3}px`,
|
||||
|
|
@ -104,4 +105,4 @@ const Shortcut: React.FC<ShortcutProps> = ({ children }) => {
|
|||
);
|
||||
};
|
||||
|
||||
export default Shortcut;
|
||||
export default Shortcut;
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import _ from "lodash";
|
||||
import { getLocalStorage } from "./localStorage";
|
||||
import { KeyboardEvent } from "react";
|
||||
import { ProfileDescription } from "core/config/ProfileLifecycleManager";
|
||||
import _ from "lodash";
|
||||
import { KeyboardEvent } from "react";
|
||||
import { getLocalStorage } from "./localStorage";
|
||||
|
||||
type Platform = "mac" | "linux" | "windows" | "unknown";
|
||||
export type Platform = "mac" | "linux" | "windows" | "unknown";
|
||||
|
||||
export function getPlatform(): Platform {
|
||||
const platform = window.navigator.platform.toUpperCase();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue