Skip to content

Commit 498f2dd

Browse files
committed
fix: format fix
1 parent a494978 commit 498f2dd

7 files changed

Lines changed: 265 additions & 120 deletions

File tree

src/components/monacoEditor/DraggableMenu.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,8 @@ const DraggableMenu: React.FC<DraggableMenuProps> = ({
926926
isSelected={imageDecoratorsEnabled ?? true}
927927
size="sm"
928928
onChange={() =>
929-
onImageDecoratorsChange && onImageDecoratorsChange(!(imageDecoratorsEnabled ?? true))
929+
onImageDecoratorsChange &&
930+
onImageDecoratorsChange(!(imageDecoratorsEnabled ?? true))
930931
}
931932
/>
932933
</div>

src/components/monacoEditor/MonacoJsonEditor.tsx

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
toggleTimestampDecorators,
3838
updateTimestampDecorations,
3939
handleTimestampContentChange,
40+
setTimestampDecorationEnabled,
4041
} from "@/components/monacoEditor/decorations/timestampDecoration.ts";
4142
import {
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

src/components/monacoEditor/decorations/base64Decoration.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const registerBase64HoverProvider = () => {
2727
provideHover: (model, position) => {
2828
// 如果提供者被禁用,直接返回null
2929
if (!isBase64ProviderEnabled) return null;
30-
3130
const lineContent = model.getLineContent(position.lineNumber);
3231
const wordInfo = model?.getWordAtPosition(position);
3332

@@ -91,9 +90,18 @@ export const updateBase64Decorations = (
9190

9291
if (!visibleRanges.length) return;
9392

93+
// 检查行数,少于3行时清空装饰器
9494
const model = editor.getModel();
9595

96-
if (!model) return;
96+
if (!model) {
97+
return;
98+
}
99+
// 检查行数,少于3行时清空装饰器,
100+
if (model.getLineCount() < 3) {
101+
clearBase64Cache(state);
102+
103+
return;
104+
}
97105

98106
// 定期清理过期缓存
99107
decorationManager.cleanupExpiredCache();
@@ -210,8 +218,20 @@ export const handleBase64ContentChange = (
210218
const editor = state.editorRef.current;
211219
const decorationManager = state.decorationManagerRef.current;
212220

213-
// 检查是否为完全替换
221+
// 检查行数,少于3行时清空装饰器
214222
const model = editor.getModel();
223+
224+
if (!model) {
225+
return;
226+
}
227+
// 检查行数,少于3行时清空装饰器,
228+
if (model.getLineCount() < 3) {
229+
clearBase64Cache(state);
230+
231+
return;
232+
}
233+
234+
// 检查是否为完全替换
215235
const isFullReplacement =
216236
model &&
217237
e.changes.some(

src/components/monacoEditor/decorations/imageDecoration.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,17 @@ export const updateImageDecorations = (
538538

539539
const model = editor.getModel();
540540

541-
if (!model) return;
541+
if (!model) {
542+
return;
543+
}
544+
// 检查行数,少于3行时清空装饰器,
545+
const lineCount = model.getLineCount();
546+
547+
if (lineCount < 3) {
548+
clearImageCache(state);
549+
550+
return;
551+
}
542552

543553
// 定期清理过期缓存
544554
decorationManager.cleanupExpiredCache();
@@ -667,6 +677,18 @@ export const handleImageContentChange = (
667677
const decorationManager = state.decorationManagerRef.current;
668678
const model = editor.getModel();
669679

680+
if (!model) {
681+
return;
682+
}
683+
// 检查行数,少于3行时清空装饰器,
684+
const lineCount = model.getLineCount();
685+
686+
if (lineCount < 3) {
687+
clearImageCache(state);
688+
689+
return;
690+
}
691+
670692
// 检查是否为完全替换
671693
const isFullReplacement =
672694
model &&

0 commit comments

Comments
 (0)