Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/web-ui/src/tools/editor/components/MarkdownEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createLogger } from '@/shared/utils/logger';
import { sendDebugProbe } from '@/shared/utils/debugProbe';
import { elapsedMs, nowMs } from '@/shared/utils/timing';
import { globalEventBus } from '@/infrastructure/event-bus';
import { isSamePath } from '@/shared/utils/pathUtils';
import { CubeLoading, Button } from '@/component-library';
import { useI18n } from '@/infrastructure/i18n';
import { useTheme } from '@/infrastructure/theme/hooks/useTheme';
Expand Down Expand Up @@ -275,11 +276,16 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({
}
}, [filePath, initialContent, loadFileContent]);

const checkMarkdownDisk = useCallback(async () => {
if (!filePath || !isActiveTab || isUnmountedRef.current || isCheckingDiskRef.current) {
const syncMarkdownFromDisk = useCallback(async (source: 'poll' | 'event') => {
if (!filePath || isUnmountedRef.current || isCheckingDiskRef.current) {
return;
}
if (typeof document !== 'undefined' && document.visibilityState !== 'visible') {

if (
source === 'poll' &&
(!isActiveTab ||
(typeof document !== 'undefined' && document.visibilityState !== 'visible'))
) {
return;
}

Expand Down Expand Up @@ -378,6 +384,7 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({
'Markdown editor disk sync completed',
{
filePath,
source,
outcome,
durationMs,
error: probeError,
Expand All @@ -388,6 +395,10 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({
}
}, [fetchFileMetadata, filePath, isActiveTab, reportFileMissingFromDisk, t, toNormalizedMarkdown]);

const checkMarkdownDisk = useCallback(async () => {
await syncMarkdownFromDisk('poll');
}, [syncMarkdownFromDisk]);

const isUnsafeSplitUi =
!!filePath &&
(editability.mode === 'unsafe' ||
Expand Down Expand Up @@ -416,6 +427,19 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({
};
}, [checkMarkdownDisk, filePath, isActiveTab, pollMarkdownDisk]);

useEffect(() => {
if (!filePath || !pollMarkdownDisk) {
return;
}

return globalEventBus.on('editor:file-changed', (data: { filePath?: string }) => {
if (!isSamePath(data.filePath || '', filePath)) {
return;
}
void syncMarkdownFromDisk('event');
});
}, [filePath, pollMarkdownDisk, syncMarkdownFromDisk]);

const saveFileContent = useCallback(async () => {
if (!hasChanges || isUnmountedRef.current) return;

Expand Down
Loading