🎨 refactor out react context

This commit is contained in:
Nate Sesti 2024-04-13 10:21:59 -07:00
parent 36a0800677
commit 8630677ad8
7 changed files with 21 additions and 132 deletions

View file

@ -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<any>(undefined);
function App() {
const dispatch = useDispatch();

View file

@ -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";

View file

@ -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";

View file

@ -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 {

View file

@ -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: () => {},
});

View file

@ -0,0 +1,3 @@
import { createContext } from "react";
export const VscThemeContext = createContext<any>(undefined);

View file

@ -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);
});
}
};