From 8497457edba02b59045373c8d22a0eba404acfa7 Mon Sep 17 00:00:00 2001 From: Coki Date: Sat, 14 Dec 2024 17:20:50 +0800 Subject: [PATCH 1/3] feat: enhance ModelEditor with new features and UI improvements --- app/components/ModelEditor.tsx | 71 ++++++++++++++++++++++++++++++++ app/components/SidePanelChat.tsx | 3 -- app/components/editor/index.tsx | 3 ++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/app/components/ModelEditor.tsx b/app/components/ModelEditor.tsx index d628916..8013baa 100644 --- a/app/components/ModelEditor.tsx +++ b/app/components/ModelEditor.tsx @@ -10,11 +10,32 @@ import { linter, lintGutter } from '@codemirror/lint'; import { casbinLinter } from '@/app/utils/casbinLinter'; import { newModel } from 'casbin'; import { setError } from '@/app/utils/errorManager'; +import { buttonPlugin } from '@/app/components/editor/ButtonPlugin'; +import { extractPageContent } from '@/app/utils/contentExtractor'; +import { useLang } from '@/app/context/LangContext'; +import SidePanelChat from '@/app/components/SidePanelChat'; +import { example, ModelKind } from '@/app/components/editor/casbin-mode/example'; +import { clsx } from 'clsx'; export const ModelEditor = () => { const [modelText, setModelText] = useState(''); + const [modelKind, setModelKind] = useState('basic'); const editorRef = useRef(null); const cursorPosRef = useRef<{ from: number; to: number } | null>(null); + const sidePanelChatRef = useRef<{ openDrawer: (message: string) => void } | null>(null); + const { t, lang, theme } = useLang(); + const textClass = clsx(theme === 'dark' ? 'text-gray-200' : 'text-gray-800'); + + const openDrawerWithMessage = (message: string) => { + if (sidePanelChatRef.current) { + sidePanelChatRef.current.openDrawer(message); + } + }; + + const extractContent = (boxType: string) => { + const { message } = extractPageContent(boxType, t, lang); + return message; + }; const validateModel = useCallback(async (text: string) => { try { @@ -73,9 +94,55 @@ export const ModelEditor = () => { } }, [modelText]); + useEffect(() => { + if (modelKind && example[modelKind]) { + setModelText(example[modelKind].model); + } + }, [modelKind]); + return (
+
+
{t('Model')}
+ + +
{ EditorView.lineWrapping, linter(casbinLinter), lintGutter(), + buttonPlugin(openDrawerWithMessage, extractContent, 'model'), EditorView.updateListener.of((update) => { if (update.docChanged) { editorRef.current = update.view; @@ -102,6 +170,9 @@ export const ModelEditor = () => { className={'function flex-grow h-[300px]'} value={modelText} /> +
+ +
); diff --git a/app/components/SidePanelChat.tsx b/app/components/SidePanelChat.tsx index 279ad2d..9a52f29 100644 --- a/app/components/SidePanelChat.tsx +++ b/app/components/SidePanelChat.tsx @@ -35,9 +35,6 @@ const SidePanelChat = forwardRef((props, ref) => { return ( <> -
- {t('Why this result')} -
{isOpen &&
}
{
{t('Enforcement Result')}
+
+ {t('Why this result')} +
From 512354069f8e49704e15530b7ea0337e5d6e2e60 Mon Sep 17 00:00:00 2001 From: Coki Date: Sat, 14 Dec 2024 17:58:50 +0800 Subject: [PATCH 2/3] fix: ensure modelText is set only if event.data.modelText exists --- app/components/ModelEditor.tsx | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/components/ModelEditor.tsx b/app/components/ModelEditor.tsx index 8013baa..1a16340 100644 --- a/app/components/ModelEditor.tsx +++ b/app/components/ModelEditor.tsx @@ -19,7 +19,7 @@ import { clsx } from 'clsx'; export const ModelEditor = () => { const [modelText, setModelText] = useState(''); - const [modelKind, setModelKind] = useState('basic'); + const [modelKind, setModelKind] = useState(''); const editorRef = useRef(null); const cursorPosRef = useRef<{ from: number; to: number } | null>(null); const sidePanelChatRef = useRef<{ openDrawer: (message: string) => void } | null>(null); @@ -52,14 +52,18 @@ export const ModelEditor = () => { const handleMessage = useCallback((event: MessageEvent) => { if (event.data.type === 'initializeModel') { - setModelText(event.data.modelText); + if (event.data.modelText) { + setModelText(event.data.modelText); + } } else if (event.data.type === 'getModelText') { window.parent.postMessage({ type: 'modelUpdate', modelText: modelText }, '*'); } else if (event.data.type === 'updateModelText') { - setModelText(event.data.modelText); + if (event.data.modelText) { + setModelText(event.data.modelText); + } } }, [modelText]); @@ -108,7 +112,15 @@ export const ModelEditor = () => {