Skip to content

Commit

Permalink
feat: Add tofrac button & fraction calculation result
Browse files Browse the repository at this point in the history
  • Loading branch information
NriotHrreion committed Sep 25, 2023
1 parent 26a2f05 commit 0c83050
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/style/calculator/calculator.less
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
font-size: 11pt;
color: var(--ca-gray1) !important;
}
.tofrac-switcher {
position: absolute;
left: 80px;
bottom: 15px;
font-size: 11pt;
border: 2px solid var(--ca-gray2);
border-radius: 50px;
background-color: var(--ca-gray5);
outline: none;
cursor: pointer;
&.active {
border-color: var(--ca-gray4);
background-color: var(--ca-gray9);
}
}
.number-box-list {
width: 40%;
margin: 20px;
Expand Down
19 changes: 17 additions & 2 deletions src/views/general/Output.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useRef, useCallback } from "react";
import { BlockMath } from "react-katex";
import { BlockMath, InlineMath } from "react-katex";
import { useContextMenu, ContextMenuItem } from "use-context-menu";

import { HistoryItemInfo } from "@/components/sidebar/History";
Expand All @@ -8,6 +8,7 @@ import { errorText, acTable } from "@/global";
import Emitter from "@/utils/Emitter";
import Utils from "@/utils/Utils";
import Compiler from "@/compiler/Compiler";
import Compute from "@/compiler/Compute";
import Is from "@/compiler/Is";
import Logger from "@/utils/Logger";
import { NumberSys, RecordType } from "@/types";
Expand All @@ -26,6 +27,7 @@ import ProdDialog from "@/dialogs/ProdDialog";

const Output: React.FC = () => {
const [outputContent, setOutputContent] = useState<string>("");
const [isTofrac, setIsTofrac] = useState<boolean>(false);
const variableRef = useRef<Map<string, string>>(new Map<string, string>());
const inputRef = useRef<InputBox>(null);
const varsDialogRef = useRef<Dialog>(null);
Expand All @@ -34,6 +36,10 @@ const Output: React.FC = () => {
const intDialogRef = useRef<Dialog>(null);
const prodDialogRef = useRef<Dialog>(null);

const handleTofracSwitch = () => {
setIsTofrac(!isTofrac);
};

const handleResult = useCallback((currentContent: string) => {
if(currentContent.split(" ").length <= 1) return;

Expand Down Expand Up @@ -75,6 +81,12 @@ const Output: React.FC = () => {

if(result.indexOf("NaN") > -1 || result === "") error = true;

// tofrac
if(isTofrac && result.indexOf(".") > -1 && result.split(".")[1].length <= 7) {
const [a, b] = Compute.toFrac(parseFloat(result));
result = "\\frac{"+ a +"}{"+ b +"}";
}

// Display the result
if(result.indexOf("Infinity") > -1) result = result.replace("Infinity", "\\infty");
if(!error) {
Expand All @@ -89,7 +101,7 @@ const Output: React.FC = () => {

// Add the result to history list
Emitter.get().emit("add-record", rawText, result, RecordType.GENERAL, NumberSys.DEC);
}, []);
}, [isTofrac]);

const handleInput = useCallback((symbol: string) => {
if(!inputRef.current) return;
Expand Down Expand Up @@ -223,6 +235,9 @@ const Output: React.FC = () => {
{Utils.isMobile() && <SidebarOpener />}

<span className="output-tag">Output</span>
<button className={"tofrac-switcher"+ (isTofrac ? " active" : "")} onClick={() => handleTofracSwitch()}>
<InlineMath math="\frac{a}{b}"/>
</button>
<InputBox
ref={inputRef}
ltr={false}
Expand Down

1 comment on commit 0c83050

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.