Skip to content

Commit

Permalink
Knip 5: consolidate Mathquill Field types (#1775)
Browse files Browse the repository at this point in the history
## Summary:
Knip detected some of these unused enums, which helped me realize there was a duplicate type declaration; so I was able to consolidate them into one type.

Author: handeyeco

Reviewers: benchristel

Required Reviewers:

Approved By: benchristel

Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x), ✅ gerald

Pull Request URL: #1775
  • Loading branch information
handeyeco authored Oct 22, 2024
1 parent cb3b8f3 commit c91cba9
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-wasps-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/math-input": patch
---

Consolidate MathFieldActionTypes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {MathFieldActionType} from "../../types";

import {CursorContext} from "./cursor-contexts";
import {mathQuillInstance} from "./mathquill-instance";
import {MathFieldActionType} from "./mathquill-types";

import type {MathFieldInterface} from "./mathquill-types";

Expand Down
7 changes: 0 additions & 7 deletions packages/math-input/src/components/input/mathquill-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ export type MathFieldInterface = MathQuill.v3.EditableMathQuill & {
controller: () => MathQuill.Controller;
};

export enum MathFieldActionType {
WRITE = "write",
CMD = "cmd",
KEYSTROKE = "keystroke",
MQ_END = 0,
}

export type MathFieldUpdaterCallback = (
mathField: MathFieldInterface,
key: Key,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {MathFieldActionType} from "../../types";
import {
maybeFindCommand,
maybeFindCommandBeforeParens,
} from "../input/mathquill-helpers";
import {mathQuillInstance} from "../input/mathquill-instance";
import {MathFieldActionType} from "../input/mathquill-types";

import type Key from "../../data/keys";
import type {MathFieldInterface} from "../input/mathquill-types";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {MathFieldActionType} from "../../types";
import {
isFraction,
isSquareRoot,
Expand All @@ -9,7 +10,6 @@ import {
maybeFindCommandBeforeParens,
} from "../input/mathquill-helpers";
import {mathQuillInstance} from "../input/mathquill-instance";
import {MathFieldActionType} from "../input/mathquill-types";

import type {MathFieldInterface} from "../input/mathquill-types";
import type MathQuill from "mathquill";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {MathFieldActionType} from "../../types";
import {mathQuillInstance} from "../input/mathquill-instance";
import {MathFieldActionType} from "../input/mathquill-types";

import type Key from "../../data/keys";
import type {MathFieldInterface} from "../input/mathquill-types";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {MathFieldActionType} from "../../types";
import {CursorContext} from "../input/cursor-contexts";
import {
isFraction,
isParens,
getCursorContext,
} from "../input/mathquill-helpers";
import {mathQuillInstance} from "../input/mathquill-instance";
import {MathFieldActionType} from "../input/mathquill-types";

import type Key from "../../data/keys";
import type {MathFieldInterface} from "../input/mathquill-types";
Expand Down
42 changes: 18 additions & 24 deletions packages/math-input/src/components/key-handlers/key-translator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {MathFieldActionType} from "../../types";
import {getDecimalSeparator} from "../../utils";
import {mathQuillInstance} from "../input/mathquill-instance";

Expand All @@ -11,28 +12,21 @@ import type {
MathFieldUpdaterCallback,
} from "../input/mathquill-types";

enum ActionType {
WRITE = "write",
CMD = "cmd",
KEYSTROKE = "keystroke",
MQ_END = 0,
}

function buildGenericCallback(
str: string,
type: ActionType = ActionType.WRITE,
type: MathFieldActionType = MathFieldActionType.WRITE,
): MathFieldUpdaterCallback {
return function (mathQuill: MathFieldInterface) {
switch (type) {
case ActionType.WRITE: {
case MathFieldActionType.WRITE: {
mathQuill.write(str);
return;
}
case ActionType.CMD: {
case MathFieldActionType.CMD: {
mathQuill.cmd(str);
return;
}
case ActionType.KEYSTROKE: {
case MathFieldActionType.KEYSTROKE: {
mathQuill.keystroke(str);
return;
}
Expand Down Expand Up @@ -120,18 +114,18 @@ export const getKeyTranslator = (

// The `FRAC_EXCLUSIVE` variant is handled manually, since we may need to do
// some additional navigation depending on the cursor position.
FRAC_INCLUSIVE: buildGenericCallback("/", ActionType.CMD),
FRAC: buildGenericCallback("\\frac", ActionType.CMD),
LEFT_PAREN: buildGenericCallback("(", ActionType.CMD),
RIGHT_PAREN: buildGenericCallback(")", ActionType.CMD),
SQRT: buildGenericCallback("sqrt", ActionType.CMD),
PI: buildGenericCallback("pi", ActionType.CMD),
THETA: buildGenericCallback("theta", ActionType.CMD),
RADICAL: buildGenericCallback("nthroot", ActionType.CMD),

BACKSPACE: buildGenericCallback("Backspace", ActionType.KEYSTROKE),
UP: buildGenericCallback("Up", ActionType.KEYSTROKE),
DOWN: buildGenericCallback("Down", ActionType.KEYSTROKE),
FRAC_INCLUSIVE: buildGenericCallback("/", MathFieldActionType.CMD),
FRAC: buildGenericCallback("\\frac", MathFieldActionType.CMD),
LEFT_PAREN: buildGenericCallback("(", MathFieldActionType.CMD),
RIGHT_PAREN: buildGenericCallback(")", MathFieldActionType.CMD),
SQRT: buildGenericCallback("sqrt", MathFieldActionType.CMD),
PI: buildGenericCallback("pi", MathFieldActionType.CMD),
THETA: buildGenericCallback("theta", MathFieldActionType.CMD),
RADICAL: buildGenericCallback("nthroot", MathFieldActionType.CMD),

BACKSPACE: buildGenericCallback("Backspace", MathFieldActionType.KEYSTROKE),
UP: buildGenericCallback("Up", MathFieldActionType.KEYSTROKE),
DOWN: buildGenericCallback("Down", MathFieldActionType.KEYSTROKE),

CUBE_ROOT: (mathQuill) => {
mathQuill.write("\\sqrt[3]{}");
Expand All @@ -143,7 +137,7 @@ export const getKeyTranslator = (
// If there's nothing to the left of the cursor, then we want to
// leave the cursor to the left of the fraction after creating it.
const shouldNavigateLeft =
cursor[mathQuillInstance.L] === ActionType.MQ_END;
cursor[mathQuillInstance.L] === MathFieldActionType.MQ_END;
mathQuill.cmd("\\frac");
if (shouldNavigateLeft) {
mathQuill.keystroke("Left");
Expand Down
7 changes: 7 additions & 0 deletions packages/math-input/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import type {KeypadContextRendererInterface} from "@khanacademy/perseus-core";
import type * as React from "react";
import type ReactDOM from "react-dom";

export enum MathFieldActionType {
WRITE = "write",
CMD = "cmd",
KEYSTROKE = "keystroke",
MQ_END = 0,
}

export type IconConfig = {
data: string;
};
Expand Down

0 comments on commit c91cba9

Please sign in to comment.