diff --git a/%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%AA%E4%BB%A3%E7%A0%81.md b/%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%AA%E4%BB%A3%E7%A0%81.md index e023e49..2f9b619 100644 --- a/%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%AA%E4%BB%A3%E7%A0%81.md +++ b/%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%AA%E4%BB%A3%E7%A0%81.md @@ -1,20 +1,33 @@ # 这是一段typescript代码333 ```ts -import ForwardedIconComponent from "@/components/common/genericIconComponent"; -import VditorEditor from "@/components/common/VditorEditor"; -import { Button } from "@/components/ui/button"; -import { Card } from "@/components/ui/card"; -import { Input } from "@/components/ui/input"; -import { Label } from "@/components/ui/label"; -import { - useCreateWiki, - useGetWikiPage, - usePatchUpdateWikiPage, -} from "@/controllers/API/queries/repository"; -import useAlertStore from "@/stores/alertStore"; -import { useRepositoryStore } from "@/stores/repositoryStore"; -import { Loader2 } from "lucide-react"; -import { useEffect, useState } from "react"; -import { useNavigate, useParams } from "react-router-dom"; +import +``` + +```ts + + const handleKeyDown = (e: KeyboardEvent) => { + // 在代码块内编辑时启用保护 + const selection = window.getSelection(); + if (selection && selection.rangeCount > 0) { + const range = selection.getRangeAt(0); + const preElement = range.commonAncestorContainer.closest('pre'); + if (preElement) { + isInternalChangeRef.current = true; + } + } + }; + + const handleKeyUp = () => { + setTimeout(() => { + if (!vditorElement.matches(':focus')) { + isInternalChangeRef.current = false; + } + }, 100); + }; + + vditorElement.addEventListener('focusin', handleFocusIn); + vditorElement.addEventListener('focusout', handleFocusOut); + vditorElement.addEventListener('keydown', handleKeyDown); + vditorElement.addEventListener('keyup', handleKeyUp); ```