From a4041c82bd712dc19674b7e62faa39b9679a8eea Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 22 Jan 2025 12:53:57 +0100 Subject: [PATCH] Improve highlight tool (#659) --- src/components/App/modals/highlight-tool.tsx | 40 +++++++++++++++++--- src/util/highlighting.tsx | 6 +-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/components/App/modals/highlight-tool.tsx b/src/components/App/modals/highlight-tool.tsx index 3bd692f58..2e30c4663 100644 --- a/src/components/App/modals/highlight-tool.tsx +++ b/src/components/App/modals/highlight-tool.tsx @@ -1,15 +1,16 @@ -import { Button, Divider, Modal, SimpleGrid, Stack } from "@mantine/core"; +import { Button, Divider, Group, Modal, Select, SimpleGrid, Stack } from "@mantine/core"; import { surrealql } from "@surrealdb/codemirror"; import { useCallback, useEffect, useMemo, useState } from "react"; import { s } from "surrealdb"; import { CodeEditor } from "~/components/CodeEditor"; import { PrimaryTitle } from "~/components/PrimaryTitle"; import { RadioSelect } from "~/components/RadioSelect"; +import { DRIVERS } from "~/constants"; import { useBoolean } from "~/hooks/boolean"; import { useIntent } from "~/hooks/routing"; import { useStable } from "~/hooks/stable"; import { useConfigStore } from "~/stores/config"; -import type { ColorScheme, SyntaxTheme } from "~/types"; +import { CodeLang, type ColorScheme, type SyntaxTheme } from "~/types"; import { useFeatureFlags } from "~/util/feature-flags"; import { renderHighlighting } from "~/util/highlighting"; import { formatQuery } from "~/util/surrealql"; @@ -17,10 +18,11 @@ import { formatQuery } from "~/util/surrealql"; function Render({ value, theme, + lang, syntaxTheme, -}: { value: string; theme: ColorScheme; syntaxTheme: SyntaxTheme }) { +}: { value: string; theme: ColorScheme; lang: CodeLang; syntaxTheme: SyntaxTheme }) { const render = useStable(() => { - const rendered = renderHighlighting(value, "surrealql", theme, syntaxTheme); + const rendered = renderHighlighting(value, lang, theme, syntaxTheme); const clipboardItem = new ClipboardItem({ "text/html": new Blob([rendered], { type: "text/html" }), "text/plain": new Blob([rendered], { type: "text/plain" }), @@ -42,11 +44,20 @@ function Render({ export function HighlightToolModal() { const [value, onChange] = useState(""); + const [lang, setLang] = useState("cli"); const [isOpen, openedHandle] = useBoolean(); const [_, setFeatureFlags] = useFeatureFlags(); const [theme, setTheme] = useState("dark"); const syntaxTheme = useConfigStore((state) => state.settings.appearance.syntaxTheme); + const languages = useMemo(() => { + return DRIVERS.map((driver) => ({ + label: driver.name, + value: driver.id, + icon: driver.icon, + })); + }, []); + const format = useCallback(() => { onChange(formatQuery(value)); }, [value]); @@ -57,7 +68,7 @@ export function HighlightToolModal() { } }, [isOpen]); - const extensions = useMemo(() => [surrealql()], []); + const extensions = useMemo(() => (lang === "cli" ? [surrealql()] : []), [lang]); useIntent("highlight-tool", () => { openedHandle.open(); @@ -74,7 +85,23 @@ export function HighlightToolModal() { trapFocus={false} withCloseButton size="xl" - title={Highlight Tool} + title={ + <> + Highlight Tool +