From 8301cea0bdf1c8acddf95ad44848418bf215cde1 Mon Sep 17 00:00:00 2001 From: 1ilit <1ilit@proton.me> Date: Fri, 29 Nov 2024 20:45:21 +0400 Subject: [PATCH] Add copy button to code exports --- src/components/EditorHeader/Modal/Code.jsx | 52 +++++++++++++++++++++ src/components/EditorHeader/Modal/Modal.jsx | 22 +-------- 2 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 src/components/EditorHeader/Modal/Code.jsx diff --git a/src/components/EditorHeader/Modal/Code.jsx b/src/components/EditorHeader/Modal/Code.jsx new file mode 100644 index 0000000..1bb86ae --- /dev/null +++ b/src/components/EditorHeader/Modal/Code.jsx @@ -0,0 +1,52 @@ +import { useState } from "react"; +import { sql } from "@codemirror/lang-sql"; +import { json } from "@codemirror/lang-json"; +import { vscodeDark } from "@uiw/codemirror-theme-vscode"; +import { githubLight } from "@uiw/codemirror-theme-github"; +import { useSettings } from "../../../hooks"; +import { useTranslation } from "react-i18next"; +import CodeMirror from "@uiw/react-codemirror"; + +const languageExtension = { + sql: [sql()], + json: [json()], +}; + +export default function Code({ value, language }) { + const { t } = useTranslation(); + const { settings } = useSettings(); + const [copied, setCopied] = useState(false); + + const copyCode = () => { + navigator.clipboard + .writeText(value) + .then(() => { + setCopied(true); + setTimeout(() => { + setCopied(false); + }, 2000); + }) + .catch((e) => { + console.log(e); + }); + }; + + return ( +
+ + +
+ ); +} diff --git a/src/components/EditorHeader/Modal/Modal.jsx b/src/components/EditorHeader/Modal/Modal.jsx index f9a8054..aa069e8 100644 --- a/src/components/EditorHeader/Modal/Modal.jsx +++ b/src/components/EditorHeader/Modal/Modal.jsx @@ -12,7 +12,6 @@ import { useAreas, useEnums, useNotes, - useSettings, useDiagram, useTransform, useTypes, @@ -34,21 +33,12 @@ import ImportSource from "./ImportSource"; import SetTableWidth from "./SetTableWidth"; import Language from "./Language"; import Share from "./Share"; -import CodeMirror from "@uiw/react-codemirror"; -import { sql } from "@codemirror/lang-sql"; -import { vscodeDark } from "@uiw/codemirror-theme-vscode"; -import { json } from "@codemirror/lang-json"; -import { githubLight } from "@uiw/codemirror-theme-github"; +import Code from "./Code"; import { useTranslation } from "react-i18next"; import { importSQL } from "../../../utils/importSQL"; import { databases } from "../../../data/databases"; import { isRtl } from "../../../i18n/utils/rtl"; -const languageExtension = { - sql: [sql()], - json: [json()], -}; - export default function Modal({ modal, setModal, @@ -64,7 +54,6 @@ export default function Modal({ const { setNotes } = useNotes(); const { setAreas } = useAreas(); const { setTypes } = useTypes(); - const { settings } = useSettings(); const { setEnums } = useEnums(); const { setTasks } = useTasks(); const { setTransform } = useTransform(); @@ -303,14 +292,7 @@ export default function Modal({ {modal === MODAL.IMG ? ( Diagram ) : ( - {}} - editable={false} - theme={settings.mode === "dark" ? vscodeDark : githubLight} - /> + )}
{t("filename")}: