@@ -37,6 +37,7 @@ import {
3737 toggleTimestampDecorators ,
3838 updateTimestampDecorations ,
3939 handleTimestampContentChange ,
40+ setTimestampDecorationEnabled ,
4041} from "@/components/monacoEditor/decorations/timestampDecoration.ts" ;
4142import {
4243 ErrorDecoratorState ,
@@ -271,7 +272,7 @@ const MonacoJsonEditor: React.FC<MonacoJsonEditorProps> = ({
271272 cacheRef : imageCacheRef ,
272273 enabled : imageDecoratorsEnabled ,
273274 theme : theme == "vs-dark" ? "dark" : "light" ,
274- editorPrefix : "normal"
275+ editorPrefix : "normal" ,
275276 } ;
276277
277278 // 错误高亮装饰器状态
@@ -726,6 +727,11 @@ const MonacoJsonEditor: React.FC<MonacoJsonEditorProps> = ({
726727 if ( urlDecorationManagerRef . current ) {
727728 urlDecorationManagerRef . current . clearAllDecorations ( editorRef . current ) ;
728729 }
730+
731+ // 清空图片装饰器
732+ if ( imageDecorationManagerRef . current ) {
733+ imageDecorationManagerRef . current . clearAllDecorations ( editorRef . current ) ;
734+ }
729735 } ;
730736
731737 // 监听时间戳装饰器状态变化
@@ -1659,14 +1665,8 @@ const MonacoJsonEditor: React.FC<MonacoJsonEditorProps> = ({
16591665 const isValid = editorValueValidate ( val ) ;
16601666
16611667 if ( isValid ) {
1662- // 检查行数,少于3行时不格式化
1663- const lineCount =
1664- editorRef . current . getModel ( ) ?. getLineCount ( ) || 0 ;
1665-
1666- if ( lineCount >= 3 ) {
1667- // 验证成功后进行格式化
1668- editorFormat ( ) ;
1669- }
1668+ // 验证成功后进行格式化
1669+ editorFormat ( ) ;
16701670 // 设置为非首次粘贴状态,避免重复格式化
16711671 setIsFirstPaste ( false ) ;
16721672 }
@@ -1679,60 +1679,75 @@ const MonacoJsonEditor: React.FC<MonacoJsonEditorProps> = ({
16791679 editorRef . current = editor ;
16801680 setIsEditorReady ( true ) ;
16811681
1682- // 初始化装饰器
1683- setTimeout ( ( ) => {
1684- if ( editorRef . current ) {
1685- // 检查行数,小于3行时不启用装饰器
1686- const lineCount = getEditorLineCount ( ) ;
1682+ // 统一初始化所有装饰器
1683+ // 使用 onDidLayoutChange 确保编辑器布局完成后再初始化装饰器
1684+ let hasInitializedDecorations = false ;
16871685
1688- if ( lineCount >= 3 ) {
1689- // 初始化时间戳装饰器
1690- if ( timestampDecoratorsEnabled ) {
1691- updateTimestampDecorations (
1692- editorRef . current ,
1693- timestampDecoratorState ,
1694- ) ;
1695- }
1686+ const initializeDecorations = ( ) => {
1687+ // 防止重复初始化
1688+ if ( hasInitializedDecorations || ! editorRef . current ) {
1689+ return ;
1690+ }
16961691
1697- // 初始化Base64装饰器
1698- if ( base64DecoratorsEnabled ) {
1699- // 确保全局状态与本地状态同步
1700- setBase64ProviderEnabled ( base64DecoratorsEnabled ) ;
1701- setBase64DecorationEnabled ( base64DecoratorsEnabled ) ;
1702- updateBase64Decorations (
1703- editorRef . current ,
1704- base64DecoratorState ,
1705- ) ;
1706- }
1692+ hasInitializedDecorations = true ;
17071693
1708- // 初始化Unicode装饰器
1709- if ( unicodeDecoratorsEnabled ) {
1710- // 确保全局状态与本地状态同步
1711- setUnicodeProviderEnabled ( unicodeDecoratorsEnabled ) ;
1712- setUnicodeDecorationEnabled ( unicodeDecoratorsEnabled ) ;
1713- updateUnicodeDecorations (
1714- editorRef . current ,
1715- unicodeDecoratorState ,
1716- ) ;
1717- }
1694+ // 检查行数,少于3行时不启用装饰器(图片装饰器除外)
1695+ const lineCount = getEditorLineCount ( ) ;
17181696
1719- // 初始化URL装饰器
1720- if ( urlDecoratorsEnabled ) {
1721- // 确保全局状态与本地状态同步
1722- setUrlProviderEnabled ( urlDecoratorsEnabled ) ;
1723- setUrlDecorationEnabled ( urlDecoratorsEnabled ) ;
1724- updateUrlDecorations ( editorRef . current , urlDecoratorState ) ;
1725- }
1697+ if ( lineCount < 3 ) {
1698+ return ;
1699+ }
17261700
1727- // 初始化图片装饰器
1728- if ( imageDecoratorsEnabled && lineCount >= 1 ) {
1729- // 确保全局状态与本地状态同步
1730- setImageDecorationEnabled ( imageDecoratorsEnabled ) ;
1731- updateImageDecorations ( editorRef . current , imageDecoratorState ) ;
1732- }
1733- }
1701+ // 统一初始化时间戳装饰器 - 先设置状态再更新
1702+ if ( timestampDecoratorsEnabled ) {
1703+ // 确保全局状态与本地状态同步
1704+ setTimestampDecorationEnabled ( timestampDecoratorsEnabled ) ;
1705+ updateTimestampDecorations (
1706+ editorRef . current ,
1707+ timestampDecoratorState ,
1708+ ) ;
1709+ }
1710+
1711+ // 统一初始化Base64装饰器 - 先设置状态再更新
1712+ if ( base64DecoratorsEnabled ) {
1713+ // 确保全局状态与本地状态同步
1714+ setBase64ProviderEnabled ( base64DecoratorsEnabled ) ;
1715+ setBase64DecorationEnabled ( base64DecoratorsEnabled ) ;
1716+ updateBase64Decorations ( editorRef . current , base64DecoratorState ) ;
1717+ }
1718+
1719+ // 统一初始化Unicode装饰器 - 先设置状态再更新
1720+ if ( unicodeDecoratorsEnabled ) {
1721+ // 确保全局状态与本地状态同步
1722+ setUnicodeProviderEnabled ( unicodeDecoratorsEnabled ) ;
1723+ setUnicodeDecorationEnabled ( unicodeDecoratorsEnabled ) ;
1724+ updateUnicodeDecorations ( editorRef . current , unicodeDecoratorState ) ;
1725+ }
1726+
1727+ // 统一初始化URL装饰器 - 先设置状态再更新
1728+ if ( urlDecoratorsEnabled ) {
1729+ // 确保全局状态与本地状态同步
1730+ setUrlProviderEnabled ( urlDecoratorsEnabled ) ;
1731+ setUrlDecorationEnabled ( urlDecoratorsEnabled ) ;
1732+ updateUrlDecorations ( editorRef . current , urlDecoratorState ) ;
1733+ }
1734+
1735+ // 初始化图片装饰器 - 图片装饰器对行数没有严格要求
1736+ if ( imageDecoratorsEnabled ) {
1737+ // 确保全局状态与本地状态同步
1738+ setImageDecorationEnabled ( imageDecoratorsEnabled ) ;
1739+ updateImageDecorations ( editorRef . current , imageDecoratorState ) ;
1740+ }
1741+ } ;
1742+
1743+ // utools 加载慢,延迟初始化
1744+ setTimeout ( ( ) => {
1745+ if ( ! hasInitializedDecorations ) {
1746+ initializeDecorations ( ) ;
1747+
1748+ return ;
17341749 }
1735- } , 300 ) ;
1750+ } , 1000 ) ;
17361751 }
17371752 } , 0 ) ;
17381753
0 commit comments