From b1fb933f27a15830c0852942f6ac8bb9aa4162e6 Mon Sep 17 00:00:00 2001 From: mohamadreza Date: Mon, 6 Jan 2025 04:49:52 +0330 Subject: [PATCH] feat(import): add import file feature --- src/components/editor/Editor.jsx | 9 ++++++++- src/components/header/Header.jsx | 13 ++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/components/editor/Editor.jsx b/src/components/editor/Editor.jsx index 8ac95a1..433b52a 100644 --- a/src/components/editor/Editor.jsx +++ b/src/components/editor/Editor.jsx @@ -2,6 +2,7 @@ import {useEffect, useState} from "react"; import "../../scripts/codemirror.js" import EditorService from "../../services/EditorService.js"; import { Rnd } from "react-rnd"; +import {useLocation} from "react-router-dom"; const salamAdd = () => { const script = document.createElement('script'); @@ -16,8 +17,14 @@ const salamAdd = () => { const Editor = () => { const [size, setSize] = useState({ width: 300, height: 200 }); const [position, setPosition] = useState({ x: 50, y: 50 }); + const location = useLocation() useEffect(() => { + const editor = document.querySelector("#editor"); + if (editor) { + editor.innerHTML = ""; + } + // salamAdd(); EditorService(() => { return [{ @@ -31,7 +38,7 @@ const Editor = () => { }, (updateText) => { // SalamService(updateText); }) - }, []) + }, [location]) return (<>
{ const [fileContent, setFileContent] = useState(""); + const navigate = useNavigate() + // Read File const handleFileChange = (event) => { const file = event.target.files[0]; if (file) { @@ -13,23 +16,26 @@ const Header = () => { const reader = new FileReader(); reader.onload = (e) => { setFileContent(e.target.result); - toast.success("فایل وارد شد" , { + toast.success("فایل وارد شد", { position: "bottom-right", }) + localStorage.setItem("code", e.target.result); + navigate("/") }; reader.readAsText(file); } else { - toast.error("فرمت فایل وارد شده اشتباه است." , { + toast.error("فرمت فایل وارد شده اشتباه است.", { position: "bottom-right", }) } } }; - const openFilePicker = () => { document.getElementById("fileInput").click(); }; + // Export file + return (<>
{ { openFilePicker() }}/> + {/*
*/}