import { CloseFullscreenRounded, ContentPasteRounded, FormatPaintRounded, OpenInFullRounded, RestartAltRounded, } from '@mui/icons-material' import { Button, ButtonGroup, Dialog, DialogActions, DialogContent, DialogTitle, IconButton, } from '@mui/material' import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow' import { useLockFn } from 'ahooks' import { type ReactNode, useCallback, useEffect, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { BaseLoadingOverlay, MonacoEditor } from '@/components/base' import { showNotice } from '@/services/notice-service' import { useThemeMode } from '@/services/states' import type { MonacoEditorInstance, MonacoMarker } from '@/types/monaco' import debounce from '@/utils/debounce' import { MONACO_FONT_FAMILY } from '@/utils/font-family' const appWindow = getCurrentWebviewWindow() type EditorLanguage = 'yaml' | 'javascript' | 'css' export interface EditorViewerProps { open: boolean title?: string | ReactNode description?: ReactNode value: string language: EditorLanguage path: string readOnly?: boolean loading?: boolean dirty?: boolean saveDisabled?: boolean onChange?: (value: string) => void onSave?: () => void | Promise onResetToDefault?: () => void onClose: () => void onValidate?: (markers: MonacoMarker[]) => void } export const EditorViewer = ({ open, title, description, value, language, path, readOnly = false, loading = false, dirty, saveDisabled = false, onChange, onSave, onResetToDefault, onClose, onValidate, }: EditorViewerProps) => { const { t } = useTranslation() const themeMode = useThemeMode() const [isMaximized, setIsMaximized] = useState(false) const editorRef = useRef(null) const resolvedTitle = title ?? t('profiles.components.menu.editFile') const disableSave = loading || saveDisabled || dirty === false const syncEditorValue = useCallback(() => { const model = editorRef.current?.getModel() if (model && model.getValue() !== value) { model.setValue(value) } }, [value]) const syncMaximizedState = useCallback(async () => { try { setIsMaximized(await appWindow.isMaximized()) } catch { setIsMaximized(false) } }, []) const handleSave = useLockFn(async () => { try { if (!readOnly) { await onSave?.() } onClose() } catch (error) { showNotice.error(error) } }) const handleClose = () => { try { onClose() } catch (error) { showNotice.error(error) } } const handlePaste = useLockFn(async () => { try { if (readOnly || loading || !editorRef.current) return const text = await navigator.clipboard.readText() if (!text) return const editorInstance = editorRef.current const model = editorInstance.getModel() const selections = editorInstance.getSelections() if (!model || !selections || selections.length === 0) return editorInstance.pushUndoStop() editorInstance.executeEdits( 'explicit-paste', selections.map((selection) => ({ range: selection, text, forceMoveMarkers: true, })), ) editorInstance.pushUndoStop() editorInstance.focus() } catch (error) { showNotice.error(error) } }) const handleFormat = useLockFn(async () => { try { if (loading) return await editorRef.current?.getAction('editor.action.formatDocument')?.run() } catch (error) { showNotice.error(error) } }) const handleToggleMaximize = useLockFn(async () => { try { await appWindow.toggleMaximize() await syncMaximizedState() editorRef.current?.layout() } catch (error) { showNotice.error(error) } }) useEffect(() => { if (!open) return void syncMaximizedState() }, [open, syncMaximizedState]) useEffect(() => { if (!open || loading) return syncEditorValue() }, [loading, open, syncEditorValue]) useEffect(() => { if (!open) return const onResized = debounce(() => { void syncMaximizedState() try { editorRef.current?.layout() } catch { // Ignore transient layout errors during window transitions. } }, 100) const unlistenResized = appWindow.onResized(onResized) return () => { unlistenResized.then((unlisten) => unlisten()) } }, [open, syncMaximizedState]) useEffect(() => { return () => { editorRef.current?.dispose() editorRef.current = null } }, []) return ( {resolvedTitle} {description}
{!loading && ( { editorRef.current = editorInstance syncEditorValue() }} onChange={(nextValue) => onChange?.(nextValue ?? '')} onValidate={onValidate} options={{ automaticLayout: true, tabSize: 2, minimap: { enabled: typeof document !== 'undefined' && document.documentElement.clientWidth >= 1500, }, mouseWheelZoom: true, readOnly, readOnlyMessage: { value: t('profiles.modals.editor.messages.readOnly'), }, renderValidationDecorations: 'on', quickSuggestions: { strings: true, comments: true, other: true, }, padding: { top: 33, }, fontFamily: MONACO_FONT_FAMILY, fontLigatures: false, smoothScrolling: true, }} /> )}
{ void handlePaste() }} > { void handleFormat() }} > { void handleToggleMaximize() }} > {isMaximized ? : }
{!readOnly && onResetToDefault && ( )} {!readOnly && ( )}
) }