From 8630677ad8d2da1e2b0b5956f279b3bedede6ee4 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sat, 13 Apr 2024 10:21:59 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20refactor=20out=20react=20context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/src/App.tsx | 17 +-- gui/src/components/dialogs/AddDocsDialog.tsx | 2 +- gui/src/components/mainInput/TipTapEditor.tsx | 2 +- .../markdown/SyntaxHighlightedPre.tsx | 2 +- gui/src/context/SubmenuContextProviders.ts | 13 ++ gui/src/context/VscTheme.ts | 3 + gui/src/util/jetbrains.ts | 114 ------------------ 7 files changed, 21 insertions(+), 132 deletions(-) create mode 100644 gui/src/context/SubmenuContextProviders.ts create mode 100644 gui/src/context/VscTheme.ts delete mode 100644 gui/src/util/jetbrains.ts diff --git a/gui/src/App.tsx b/gui/src/App.tsx index 50ae708a1..71b5c7606 100644 --- a/gui/src/App.tsx +++ b/gui/src/App.tsx @@ -1,4 +1,3 @@ -import { createContext } from "react"; import { useDispatch } from "react-redux"; import { RouterProvider, createMemoryRouter } from "react-router-dom"; import Layout from "./components/Layout"; @@ -12,7 +11,8 @@ import Models from "./pages/models"; import MonacoPage from "./pages/monaco"; import SettingsPage from "./pages/settings"; -import { ContextSubmenuItem } from "core"; +import { SubmenuContextProvidersContext } from "./context/SubmenuContextProviders"; +import { VscThemeContext } from "./context/VscTheme"; import useSubmenuContextProviders from "./hooks/useSubmenuContextProviders"; import { useVscTheme } from "./hooks/useVscTheme"; import GUI from "./pages/gui"; @@ -82,19 +82,6 @@ const router = createMemoryRouter([ }, ]); -export const SubmenuContextProvidersContext = createContext<{ - getSubmenuContextItems: ( - providerTitle: string | undefined, - query: string, - ) => (ContextSubmenuItem & { providerTitle: string })[]; - addItem: (providerTitle: string, item: ContextSubmenuItem) => void; -}>({ - getSubmenuContextItems: () => [], - addItem: () => {}, -}); - -export const VscThemeContext = createContext(undefined); - function App() { const dispatch = useDispatch(); diff --git a/gui/src/components/dialogs/AddDocsDialog.tsx b/gui/src/components/dialogs/AddDocsDialog.tsx index 5c70b19a6..82e53dfcf 100644 --- a/gui/src/components/dialogs/AddDocsDialog.tsx +++ b/gui/src/components/dialogs/AddDocsDialog.tsx @@ -3,7 +3,7 @@ import React, { useContext, useLayoutEffect } from "react"; import { useDispatch } from "react-redux"; import styled from "styled-components"; import { Button, Input } from ".."; -import { SubmenuContextProvidersContext } from "../../App"; +import { SubmenuContextProvidersContext } from "../../context/SubmenuContextProviders"; import { setShowDialog } from "../../redux/slices/uiStateSlice"; import { postToIde } from "../../util/ide"; diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index a65bb39ee..8532a8606 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -25,7 +25,7 @@ import { vscInputBorder, vscInputBorderFocus, } from ".."; -import { SubmenuContextProvidersContext } from "../../App"; +import { SubmenuContextProvidersContext } from "../../context/SubmenuContextProviders"; import useHistory from "../../hooks/useHistory"; import useUpdatingRef from "../../hooks/useUpdatingRef"; import { useWebviewListener } from "../../hooks/useWebviewListener"; diff --git a/gui/src/components/markdown/SyntaxHighlightedPre.tsx b/gui/src/components/markdown/SyntaxHighlightedPre.tsx index acec86461..b18cd0278 100644 --- a/gui/src/components/markdown/SyntaxHighlightedPre.tsx +++ b/gui/src/components/markdown/SyntaxHighlightedPre.tsx @@ -1,7 +1,7 @@ import { useContext } from "react"; import styled from "styled-components"; import { vscForeground } from ".."; -import { VscThemeContext } from "../../App"; +import { VscThemeContext } from "../../context/VscTheme"; const StyledPre = styled.pre<{ theme: any }>` & .hljs { diff --git a/gui/src/context/SubmenuContextProviders.ts b/gui/src/context/SubmenuContextProviders.ts new file mode 100644 index 000000000..5be351a08 --- /dev/null +++ b/gui/src/context/SubmenuContextProviders.ts @@ -0,0 +1,13 @@ +import { ContextSubmenuItem } from "core"; +import { createContext } from "react"; + +export const SubmenuContextProvidersContext = createContext<{ + getSubmenuContextItems: ( + providerTitle: string | undefined, + query: string, + ) => (ContextSubmenuItem & { providerTitle: string })[]; + addItem: (providerTitle: string, item: ContextSubmenuItem) => void; +}>({ + getSubmenuContextItems: () => [], + addItem: () => {}, +}); diff --git a/gui/src/context/VscTheme.ts b/gui/src/context/VscTheme.ts new file mode 100644 index 000000000..574427d64 --- /dev/null +++ b/gui/src/context/VscTheme.ts @@ -0,0 +1,3 @@ +import { createContext } from "react"; + +export const VscThemeContext = createContext(undefined); diff --git a/gui/src/util/jetbrains.ts b/gui/src/util/jetbrains.ts deleted file mode 100644 index 6fb32a998..000000000 --- a/gui/src/util/jetbrains.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { isMetaEquivalentKeyPressed } from "."; - -// JetBrains IDEs cause odd behavior by overriding certain keybindings, so we simulate the expected behavior here. - -export const handleKeyDownJetBrainsMac = (e, text: string, setText) => { - const isCmdOrCtrlPressed = isMetaEquivalentKeyPressed(e); - - if (isCmdOrCtrlPressed && e.key === "Backspace") { - e.preventDefault(); - - const startPos = e.target.selectionStart; - const endPos = e.target.selectionEnd; - - if (startPos !== endPos) { - // If some text is selected, just delete that text - const newValue = text.slice(0, startPos) + text.slice(endPos); - setText(newValue); - // Set the cursor to where the selection started - e.target.selectionStart = startPos; - e.target.selectionEnd = startPos; - } else { - // Find the start of the current line - const prevNewLineIndex = text.lastIndexOf("\n", startPos - 1); - - // Delete from the current position to the beginning of the line - const newValue = - text.slice(0, prevNewLineIndex + 1) + text.slice(startPos); - setText(newValue); - // Set the cursor to the start of the line - e.target.selectionStart = prevNewLineIndex + 1; - e.target.selectionEnd = prevNewLineIndex + 1; - } - } else if (isCmdOrCtrlPressed) { - const startPos = e.target.selectionStart; - const endPos = e.target.selectionEnd; - let newStartPos = startPos; - let newEndPos = endPos; - const isShiftPressed = e.shiftKey; - - if (e.key === "ArrowLeft") { - const prevNewLineIndex = text.lastIndexOf("\n", startPos - 1); - - newStartPos = prevNewLineIndex === -1 ? 0 : prevNewLineIndex + 1; - newEndPos = isShiftPressed ? endPos : newStartPos; - } else if (e.key === "ArrowRight") { - const nextNewLineIndex = text.indexOf("\n", startPos); - - newEndPos = nextNewLineIndex === -1 ? text.length : nextNewLineIndex; - newStartPos = isShiftPressed ? newStartPos : newEndPos; - } - - e.target.selectionStart = newStartPos; - e.target.selectionEnd = newEndPos; - e.preventDefault(); - } -}; - -export const handleKeyDownJetBrains = (e, text: string, setText) => { - const startPos = e.target.selectionStart; - const endPos = e.target.selectionEnd; - - const isCtrlPressed = isMetaEquivalentKeyPressed(e); - - if (isCtrlPressed && e.key === "Backspace") { - e.preventDefault(); - - if (startPos !== endPos) { - // If some text is selected, just delete that text - const newValue = text.slice(0, startPos) + text.slice(endPos); - setText(newValue); - - // Set the cursor to where the selection started - e.target.selectionStart = startPos; - e.target.selectionEnd = startPos; - } else { - // Find the start of the current word - const prevSpaceIndex = text.slice(0, startPos).trimEnd().lastIndexOf(" "); - - // Delete from the current position to the beginning of the word - const newValue = text.slice(0, prevSpaceIndex + 1) + text.slice(startPos); - setText(newValue); - - // Set the cursor to the start of the word - setTimeout(() => { - // The browser likes to set the cursor to the end bc setText is delayed, so we wait, imperceptibly - e.target.selectionStart = prevSpaceIndex + 1; - e.target.selectionEnd = prevSpaceIndex + 1; - }, 10); - } - } else if (isCtrlPressed && e.key === "c") { - // If Ctrl+C is pressed, copy the selected text - e.preventDefault(); - e.stopPropagation(); - if (startPos !== endPos) { - const selectedText = text.slice(startPos, endPos); - navigator.clipboard.writeText(selectedText); - } - } else if (isCtrlPressed && e.key === "v") { - // If Ctrl+V is pressed, paste the copied text at the cursor position - e.preventDefault(); - e.stopPropagation(); - navigator.clipboard.readText().then((clipText) => { - const newValue = text.slice(0, startPos) + clipText + text.slice(endPos); - setText(newValue); - - // Set the cursor to the end of the pasted text - const newCursorPos = startPos + clipText.length; - setTimeout(() => { - e.target.selectionStart = newCursorPos; - e.target.selectionEnd = newCursorPos; - }, 10); - }); - } -};