From 96187dfb8042415603bd9875663036a38ef5f766 Mon Sep 17 00:00:00 2001 From: jin-sir <942725119@qq.com> Date: Fri, 14 Jun 2024 10:38:15 +0800 Subject: [PATCH] fix: compare incoming value with modal's value --- .../__tests__/editorService.test.tsx | 3 +++ src/services/workbench/editorService.ts | 20 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/services/workbench/__tests__/editorService.test.tsx b/src/services/workbench/__tests__/editorService.test.tsx index 56c06e59c..c203c37b4 100644 --- a/src/services/workbench/__tests__/editorService.test.tsx +++ b/src/services/workbench/__tests__/editorService.test.tsx @@ -208,9 +208,12 @@ describe('Test EditorService', () => { expect(groups?.length).toBe(1); const setValFn = jest.fn(); + const getValFn = jest.fn(); (MonacoEditor.getModel as jest.Mock).mockImplementation(() => ({ setValue: setValFn, + getValue: getValFn, })); + act(() => { editor.updateTab({ id: mockTab.id, diff --git a/src/services/workbench/editorService.ts b/src/services/workbench/editorService.ts index c1fd37027..2534f1c7c 100644 --- a/src/services/workbench/editorService.ts +++ b/src/services/workbench/editorService.ts @@ -1,6 +1,6 @@ import 'reflect-metadata'; import { singleton, container } from 'tsyringe'; -import { cloneDeep } from 'lodash'; +import { cloneDeep, isString } from 'lodash'; import { Component } from 'mo/react'; import { EditorModel, @@ -337,8 +337,13 @@ export class EditorService const model = MonacoEditor.getModel( Uri.parse(tab.id.toString()) ); - if (model) { - model.setValue(editorValue || ''); + const currentValue = model?.getValue(); + if ( + model && + isString(editorValue) && + currentValue !== editorValue + ) { + model.setValue(editorValue); } this.updateGroup(groupId, group); @@ -362,8 +367,13 @@ export class EditorService const model = MonacoEditor.getModel( Uri.parse(tab.id.toString()) ); - if (model) { - model.setValue(editorValue || ''); + const currentValue = model?.getValue(); + if ( + model && + isString(editorValue) && + currentValue !== editorValue + ) { + model.setValue(editorValue); } });