From 9ade52440577b4e681593f80e2a52fa877f372af Mon Sep 17 00:00:00 2001 From: lalala <56628290+jin-sir@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:33:43 +0800 Subject: [PATCH] fix: compare incoming value with modal's value (#887) --- .../__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); } });