create 'useIsOSREnabled'

This commit is contained in:
Test 2024-11-14 11:33:23 -08:00
parent 6f923244b8
commit 3da4234bd7
5 changed files with 33 additions and 30 deletions

View file

@ -37,7 +37,7 @@ export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & {
insertAtCursor: [{ text: string }, void];
copyText: [{ text: string }, void];
"jetbrains/editorInsetHeight": [{ height: number }, void];
"jetbrains/isOSREnabled": [boolean, void];
"jetbrains/isOSREnabled": [undefined, void];
"vscode/openMoveRightMarkdown": [undefined, void];
setGitHubAuthToken: [{ token: string }, void];
acceptDiff: [{ filepath: string }, void];
@ -104,6 +104,7 @@ export type ToWebviewFromIdeProtocol = ToWebviewFromIdeOrCoreProtocol & {
setTheme: [{ theme: any }, void];
setColors: [{ [key: string]: string }, void];
"jetbrains/editorInsetRefresh": [undefined, void];
"jetbrains/isOSREnabled": [boolean, void];
addApiKey: [undefined, void];
setupLocalConfig: [undefined, void];
incrementFtc: [undefined, void];

View file

@ -135,7 +135,7 @@ class ContinueBrowser(val project: Project, url: String) {
}
"jetbrains/isOSREnabled" -> {
respond(isOSREnabled)
sendToWebview( "jetbrains/isOSREnabled", isOSREnabled)
}
"onLoad" -> {

View file

@ -64,6 +64,7 @@ import {
handleVSCMetaKeyIssues,
} from "./handleMetaKeyIssues";
import { ComboBoxItem } from "./types";
import useIsOSREnabled from "../../hooks/useIsOSREnabled";
const InputBoxDiv = styled.div<{ border?: string }>`
resize: none;
@ -181,6 +182,8 @@ function TipTapEditor(props: TipTapEditorProps) {
const inSubmenuRef = useRef<string | undefined>(undefined);
const inDropdownRef = useRef(false);
const isOSREnabled = useIsOSREnabled();
const enterSubmenu = async (editor: Editor, providerId: string) => {
const contents = editor.getText();
const indexOfAt = contents.lastIndexOf("@");
@ -555,9 +558,7 @@ function TipTapEditor(props: TipTapEditorProps) {
if (!editorFocusedRef?.current || !isMetaEquivalentKeyPressed(e)) return;
const isOSREnabled = isJetBrains()
? await ideMessenger.request("jetbrains/isOSREnabled", undefined)
: false;
console.log({ isOSREnabled });
if (isOSREnabled) {
handleJetBrainsOSRMetaKeyIssues(e, editor);

View file

@ -6,41 +6,21 @@ const isWebEnv = isWebEnvironment();
/**
* This handles various keypress issues when OSR is enabled
* @param e
* @param editor
*/
export const handleJetBrainsOSRMetaKeyIssues = (
e: KeyboardEvent,
editor: Editor,
) => {
const selection = window.getSelection();
const alter = e.shiftKey ? "extend" : "move";
const handlers: Record<string, () => void> = {
Backspace: () => handleJetBrainsMetaBackspace(editor),
ArrowLeft: () =>
selection.modify(
e.shiftKey ? "extend" : "move",
"backward",
"lineboundary",
),
ArrowRight: () =>
selection.modify(
e.shiftKey ? "extend" : "move",
"forward",
"lineboundary",
),
ArrowDown: () =>
selection.modify(
e.shiftKey ? "extend" : "move",
"forward",
"documentboundary",
),
ArrowLeft: () => selection.modify(alter, "backward", "lineboundary"),
ArrowRight: () => selection.modify(alter, "forward", "lineboundary"),
ArrowDown: () => selection.modify(alter, "forward", "documentboundary"),
ArrowUp: () => {
selection.modify(
e.shiftKey ? "extend" : "move",
"backward",
"documentboundary",
);
selection.modify(alter, "backward", "documentboundary");
},
};

View file

@ -0,0 +1,21 @@
import { useState, useEffect, useContext } from "react";
import { isJetBrains } from "../util";
import { useWebviewListener } from "./useWebviewListener";
import { IdeMessengerContext } from "../context/IdeMessenger";
export default function useIsOSREnabled() {
const [isOSREnabled, setIsOSREnabled] = useState(false);
const ideMessenger = useContext(IdeMessengerContext);
useWebviewListener("jetbrains/isOSREnabled", async (isOSREnabled) => {
setIsOSREnabled(isOSREnabled);
});
useEffect(() => {
if (isJetBrains()) {
ideMessenger.request("jetbrains/isOSREnabled", undefined);
}
}, []);
return isOSREnabled;
}