From 0cff95ed394e8eafeb069b13700280bedeb90563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E5=A3=B0?= Date: Fri, 13 Sep 2024 15:20:03 +0800 Subject: [PATCH] fix(diff-viewer): use slash as sep (#181) --- packages/core/src/api/renderDiffViewer.tsx | 171 +++++++++--------- .../src/core/diff-viewer/internal/base.ts | 20 +- 2 files changed, 96 insertions(+), 95 deletions(-) diff --git a/packages/core/src/api/renderDiffViewer.tsx b/packages/core/src/api/renderDiffViewer.tsx index bf0ca080..21a8668a 100644 --- a/packages/core/src/api/renderDiffViewer.tsx +++ b/packages/core/src/api/renderDiffViewer.tsx @@ -10,7 +10,7 @@ import { SlotRenderer, } from '@opensumi/ide-core-browser'; import merge from 'lodash/merge'; -import React, { useEffect } from 'react'; +import React, { useEffect, useLayoutEffect, useMemo } from 'react'; import { IDiffViewerProps } from '../core/diff-viewer'; import { DiffViewerModule } from '../core/diff-viewer/module'; import { BoxPanel, SplitPanel } from '../editor'; @@ -52,105 +52,100 @@ export function DiffViewerLayoutComponent(): React.ReactElement { } export const DiffViewerRenderer = (props: IDiffViewerProps) => { - const mergedProps = merge({ - appConfig: {}, - runtimeConfig: { - onWillApplyTheme: props.onWillApplyTheme, - tabBarRightExtraContent: props.tabBarRightExtraContent, - } as RuntimeConfig, - }, props) as IAppRendererProps; + const workspaceDir = useConstant(() => props?.appConfig?.workspaceDir || 'workspace-' + randomString(8)); - if (!mergedProps.appConfig.injector) { - mergedProps.appConfig.injector = new Injector(); - } - - const injector = mergedProps.appConfig.injector; - - injector.addProviders({ - token: IDiffViewerProps, - useValue: mergedProps, - }); - - const appConfig = mergedProps.appConfig; - - let appModules: ModuleConstructor[] = appConfig?.modules || []; - if (!appModules.includes(DiffViewerModule)) { - appModules = [ - DiffViewerModule, - ...appModules, - ]; - } - - delete appConfig?.modules; - - const workspaceDir = appConfig?.workspaceDir || 'workspace-' + randomString(8); - delete (appConfig as Partial)?.workspaceDir; - - const layoutConfig = appConfig?.layoutConfig || defaultLayoutConfig; - delete appConfig?.layoutConfig; - - const layoutComponent = appConfig?.layoutComponent || DiffViewerLayoutComponent; - delete appConfig?.layoutComponent; - - useEffect(() => { - const prefix = randomString(8); - registerLocalStorageProvider(GeneralSettingsId.Theme, workspaceDir, prefix); + useLayoutEffect(() => { + registerLocalStorageProvider(GeneralSettingsId.Theme, workspaceDir, workspaceDir); return () => { for (var i = 0; i < localStorage.length; i++) { const key = localStorage.key(i)!; - if (key.startsWith(prefix)) { + if (key.startsWith(workspaceDir)) { localStorage.removeItem(key); } } }; - }, []); - - const diffViewerAppConfig: IAppRendererProps = merge>({ - appConfig: { - modules: appModules, - workspaceDir, - layoutComponent, - layoutConfig, - disableRestoreEditorGroupState: true, - extensionMetadata, - defaultPreferences: { - 'general.theme': 'opensumi-light', - 'editor.minimap': false, - 'ai.native.inlineDiff.preview.mode': 'inlineLive', - 'editor.showActionWhenGroupEmpty': true, - 'editor.autoSave': 'afterDelay', - 'application.confirmExit': 'never', - 'editor.guides.bracketPairs': false, - 'editor.quickSuggestionsDelay': 10, - 'editor.previewMode': false, - 'editor.autoSaveDelay': 1000, // one second - 'editor.fixedOverflowWidgets': true, // widget editor 默认改为 fixed - 'editor.unicodeHighlight.ambiguousCharacters': false, - 'editor.preventScrollAfterFocused': true, - 'files.exclude': { - ...FILES_DEFAULTS.filesExclude, - // browserfs OverlayFS 用来记录删除的文件 - [`**${deletionLogPath}`]: true, + }, [workspaceDir]); + + const diffViewerAppConfig: IAppRendererProps = useMemo(() => { + const mergedProps = merge({ + appConfig: {}, + runtimeConfig: { + onWillApplyTheme: props.onWillApplyTheme, + tabBarRightExtraContent: props.tabBarRightExtraContent, + } as RuntimeConfig, + }, props) as IAppRendererProps; + + if (!mergedProps.appConfig.injector) { + mergedProps.appConfig.injector = new Injector(); + } + + const injector = mergedProps.appConfig.injector; + + injector.addProviders({ + token: IDiffViewerProps, + useValue: mergedProps, + }); + + const appConfig = mergedProps.appConfig; + + let appModules: ModuleConstructor[] = appConfig?.modules || []; + if (!appModules.includes(DiffViewerModule)) { + appModules = [ + DiffViewerModule, + ...appModules, + ]; + } + + delete props?.appConfig?.modules; + delete props?.appConfig?.workspaceDir; + + return merge>({ + appConfig: { + modules: appModules, + workspaceDir, + layoutComponent: DiffViewerLayoutComponent, + layoutConfig: defaultLayoutConfig, + disableRestoreEditorGroupState: true, + extensionMetadata, + defaultPreferences: { + 'general.theme': 'opensumi-light', + 'editor.minimap': false, + 'ai.native.inlineDiff.preview.mode': 'inlineLive', + 'editor.showActionWhenGroupEmpty': true, + 'editor.autoSave': 'afterDelay', + 'application.confirmExit': 'never', + 'editor.guides.bracketPairs': false, + 'editor.quickSuggestionsDelay': 10, + 'editor.previewMode': false, + 'editor.autoSaveDelay': 1000, // one second + 'editor.fixedOverflowWidgets': true, // widget editor 默认改为 fixed + 'editor.unicodeHighlight.ambiguousCharacters': false, + 'editor.preventScrollAfterFocused': true, + 'files.exclude': { + ...FILES_DEFAULTS.filesExclude, + // browserfs OverlayFS 用来记录删除的文件 + [`**${deletionLogPath}`]: true, + }, }, }, - }, - runtimeConfig: ({ - aiNative: { - enable: true, - capabilities: { - supportsInlineChat: true, + runtimeConfig: ({ + aiNative: { + enable: true, + capabilities: { + // 必须要开,否则采纳按钮不可用 + supportsInlineChat: true, + }, }, - }, - startupEditor: 'none', - workspace: { - filesystem: { - fs: 'InMemory', - options: {}, + startupEditor: 'none', + workspace: { + filesystem: { + fs: 'InMemory', + options: {}, + }, }, - }, - }), - }, mergedProps); - + }), + }, mergedProps); + }, [props]); return ( { - return this.stripDirectory(editor.currentUri!.codeUri.fsPath); + return this.normalizePath(editor.currentUri!.codeUri.fsPath); }; getAllTabs = (): IDiffViewerTab[] => { @@ -248,12 +254,12 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri return resources.map((editor, idx) => ({ index: idx, - filePath: this.stripDirectory(editor.uri.codeUri.fsPath), + filePath: this.normalizePath(editor.uri.codeUri.fsPath), })); }; getFileIndex = (filePath: string) => { - const aPath = this.stripDirectory(filePath); + const aPath = this.normalizePath(filePath); return this.getAllTabs().findIndex((tab) => tab.filePath === aPath); }; @@ -359,7 +365,7 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri const fsPath = e.uri.fsPath; this._onPartialEditEvent.fire({ - filePath: this.stripDirectory(fsPath), + filePath: this.normalizePath(fsPath), ...e, }); })); @@ -370,7 +376,7 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri let currentIndex = -1; if (newPath) { currentIndex = this.getFileIndex(newPath); - newPath = this.stripDirectory(newPath); + newPath = this.normalizePath(newPath); } const event = {