@@ -6,7 +6,6 @@ import React, {
66 useState ,
77 useEffect ,
88 CSSProperties ,
9- useMemo ,
109} from 'react' ;
1110import { useStyles } from './style' ;
1211import { OperationLine , MonacoEditorErrorTips } from '../../components' ;
@@ -53,10 +52,9 @@ import {
5352} from '../../helper/databaseObjectTreeNode' ;
5453import { treeConfig } from '@/blocks/NewTree/treeConfig' ;
5554import {
56- ContentDiffSurface ,
55+ ContentDiffDenyReason ,
5756 getContentDiffEligibility ,
58- guardContentDiffTexts ,
59- isContentDiffSurfaceEnabled ,
57+ getContentDiffOpenBlockReason ,
6058} from '../../helper/contentDiffGuard' ;
6159
6260interface ISQLEditorWithOperationProps {
@@ -150,8 +148,6 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
150148 const [ contextTableIdentifier , setContextTableIdentifier ] = useState < EditorTableIdentifier | null > ( null ) ;
151149 const [ errorMessage , setErrorMessage ] = useState < string | null > ( null ) ;
152150 const [ hasEditorContent , setHasEditorContent ] = useState < boolean > ( ! ! defaultSQL ?. trim ( ) ) ;
153- const [ editorReady , setEditorReady ] = useState ( false ) ;
154- const [ hasContentDiffChanges , setHasContentDiffChanges ] = useState ( false ) ;
155151 const [ routineExecutionModal , setRoutineExecutionModal ] = useState ( {
156152 open : false ,
157153 title : '' ,
@@ -172,43 +168,6 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
172168 } ) ;
173169 const sqlEditorRef = useRef < SQLEditorRef > ( null ) ;
174170 const routineExecutionEditorRef = useRef < SQLEditorRef > ( null ) ;
175- const contentDiffEligibility = useMemo (
176- ( ) => getContentDiffEligibility ( { editorType : type , dbInfo, readOnly : isReadOnly } ) ,
177- [
178- dbInfo . consoleId ,
179- dbInfo . dataSourceId ,
180- dbInfo . databaseName ,
181- dbInfo . filePath ,
182- dbInfo . schemaName ,
183- dbInfo . status ,
184- dbInfo . tableName ,
185- dbInfo . viewName ,
186- ( dbInfo as Record < string , unknown > ) . functionName ,
187- ( dbInfo as Record < string , unknown > ) . procedureName ,
188- ( dbInfo as Record < string , unknown > ) . triggerName ,
189- isReadOnly ,
190- type ,
191- ] ,
192- ) ;
193- const enableContentDiffHints = contentDiffEligibility . enabled ;
194- const refreshContentDiffChanges = useCallback ( ( value ?: string ) => {
195- if ( ! enableContentDiffHints ) {
196- setHasContentDiffChanges ( false ) ;
197- return ;
198- }
199- if ( ! editorReady || ! sqlEditorRef . current ) {
200- setHasContentDiffChanges ( false ) ;
201- return ;
202- }
203-
204- const baseline = sqlEditorRef . current ?. getContentDiffBaseline ( ) ?? defaultSQL ?? '' ;
205- const currentValue = value ?? sqlEditorRef . current ?. getValue ( ) ?? '' ;
206- setHasContentDiffChanges ( guardContentDiffTexts ( baseline , currentValue ) . enabled ) ;
207- } , [ defaultSQL , editorReady , enableContentDiffHints ] ) ;
208-
209- useEffect ( ( ) => {
210- refreshContentDiffChanges ( ) ;
211- } , [ refreshContentDiffChanges ] ) ;
212171 const { consoleAiInputParams } = useWorkspaceStore ( ( state ) => {
213172 return {
214173 consoleAiInputParams : state . consoleAiInputParams ,
@@ -323,7 +282,7 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
323282 }
324283 } , [ active , consoleAiInputParams ] ) ;
325284
326- const { saveConsole } = useSaveEditorData ( {
285+ const { saveConsole, hasSavedSqlRecord } = useSaveEditorData ( {
327286 editorRef : sqlEditorRef ,
328287 isActive : active ,
329288 boundInfo : dbInfo ,
@@ -333,6 +292,12 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
333292 type,
334293 } ) ;
335294
295+ const { enabled : enableContentDiffHints , sourceId : contentDiffSourceId } = getContentDiffEligibility ( {
296+ editorType : type ,
297+ dbInfo,
298+ savedSqlRecord : hasSavedSqlRecord ,
299+ } ) ;
300+
336301 const handleAction = ( actionType : SQLOptType , params ?: any ) => {
337302 if ( isReadOnly && ! READONLY_ALLOWED_ACTIONS . has ( actionType ) ) {
338303 return ;
@@ -564,7 +529,6 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
564529 const sql = await reloadSQL ( ) ;
565530 setValue ( sql || '' , 'reset' ) ;
566531 sqlEditorRef . current ?. resetContentDiffBaseline ( sql || '' ) ;
567- setHasContentDiffChanges ( false ) ;
568532 if ( ! options ?. silent ) {
569533 staticMessage . success ( i18n ( 'workspace.routine.tips.refreshSuccess' ) ) ;
570534 }
@@ -577,7 +541,6 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
577541 const baseline = sqlEditorRef . current ?. getContentDiffBaseline ( ) ?? '' ;
578542 setValue ( baseline , 'reset' ) ;
579543 sqlEditorRef . current ?. resetContentDiffBaseline ( baseline ) ;
580- setHasContentDiffChanges ( false ) ;
581544 staticMessage . success ( i18n ( 'workspace.routine.tips.revertSuccess' ) ) ;
582545 } ;
583546
@@ -920,7 +883,6 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
920883 } ) ;
921884 try {
922885 sqlEditorRef . current ?. resetContentDiffBaseline ( fileContent ) ;
923- setHasContentDiffChanges ( false ) ;
924886 } catch {
925887 // Content diff is only a hint and must not affect file saving.
926888 }
@@ -938,20 +900,19 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
938900
939901 const handleOpenContentDiff = useCallback ( ( ) => {
940902 try {
941- const eligibility = getContentDiffEligibility ( { editorType : type , dbInfo, readOnly : isReadOnly } ) ;
942- if ( ! eligibility . enabled || ! isContentDiffSurfaceEnabled ( ContentDiffSurface . DiffTab ) ) {
903+ if ( ! enableContentDiffHints ) {
943904 return ;
944905 }
945906
946907 const originalText = sqlEditorRef . current ?. getContentDiffBaseline ( ) ?? '' ;
947908 const modifiedText = sqlEditorRef . current ?. getValue ( ) ?? '' ;
948- const guard = guardContentDiffTexts ( originalText , modifiedText ) ;
949- if ( ! guard . enabled ) {
909+ if ( getContentDiffOpenBlockReason ( originalText , modifiedText ) === ContentDiffDenyReason . TextTooLarge ) {
910+ staticMessage . warning ( i18n ( 'monaco.text.diffContentTooLarge' ) ) ;
950911 return ;
951912 }
952913
953914 const sourceId =
954- eligibility . sourceId || String ( id || dbInfo . consoleId || workspaceTabsTitle || sqlFileName || 'editor' ) ;
915+ contentDiffSourceId || String ( id || dbInfo . consoleId || workspaceTabsTitle || sqlFileName || 'editor' ) ;
955916 const tabId = `${ WorkspaceTabType . ContentDiff } :${ sourceId } ` ;
956917 const title = `${ getContentDiffSourceTitle ( { workspaceTabsTitle, sqlFileName, dbInfo } ) } @` ;
957918 const tab = {
@@ -978,7 +939,7 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
978939 } catch {
979940 // Content diff is only a hint and must not affect editor workflows.
980941 }
981- } , [ dbInfo , id , sqlFileName , workspaceTabsTitle ] ) ;
942+ } , [ contentDiffSourceId , dbInfo , enableContentDiffHints , id , sqlFileName , workspaceTabsTitle ] ) ;
982943
983944 const handleFormat = async ( ) => {
984945 const sql = sqlEditorRef . current ?. getValue ( ) || '' ;
@@ -1026,11 +987,7 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
1026987 hasEditorContent = { hasEditorContent }
1027988 isConsole = { isConsole }
1028989 setDBInfo = { setDBInfo }
1029- contentDiffEnabled = {
1030- enableContentDiffHints &&
1031- hasContentDiffChanges &&
1032- isContentDiffSurfaceEnabled ( ContentDiffSurface . Toolbar )
1033- }
990+ contentDiffEnabled = { enableContentDiffHints }
1034991 action = { handleAction }
1035992 />
1036993 ) }
@@ -1045,13 +1002,8 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
10451002 readOnly = { isReadOnly }
10461003 action = { handleAction }
10471004 enableContentDiffHints = { enableContentDiffHints }
1048- onReady = { ( ) => {
1049- setEditorReady ( true ) ;
1050- refreshContentDiffChanges ( ) ;
1051- } }
10521005 onChange = { ( value ) => {
10531006 setHasEditorContent ( ! ! value ?. trim ( ) ) ;
1054- refreshContentDiffChanges ( value ) ;
10551007 } }
10561008 contextMenuInfo = { contextMenuInfo }
10571009 onTableIdentifierContextChange = { setContextTableIdentifier }
@@ -1066,11 +1018,7 @@ const SQLEditorWithOperation = forwardRef<ISQLEditorWithOperationRef, ISQLEditor
10661018 config = { contextMenuInfo }
10671019 canEditTable = { ! ! contextTableIdentifier }
10681020 sqlActionEnabled = { sqlActionEnabled }
1069- contentDiffEnabled = {
1070- enableContentDiffHints &&
1071- hasContentDiffChanges &&
1072- isContentDiffSurfaceEnabled ( ContentDiffSurface . ContextMenu )
1073- }
1021+ contentDiffEnabled = { enableContentDiffHints }
10741022 onClick = { handleAction }
10751023 onCloseContextMenu = { ( ) => setContextMenuInfo ( contextMenuDefaultConfig ) }
10761024 />
0 commit comments