From 5141fe33780ccacc177d5404f66a26a891e578e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlia=20Roldi?= Date: Wed, 5 Jun 2024 19:22:42 -0300 Subject: [PATCH 1/4] auto format trigger --- .../lib/autoFormat/AutoFormatPlugin.ts | 9 +++++-- .../test/autoFormat/AutoFormatPluginTest.ts | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/roosterjs-content-model-plugins/lib/autoFormat/AutoFormatPlugin.ts b/packages/roosterjs-content-model-plugins/lib/autoFormat/AutoFormatPlugin.ts index b0936bdf511..bdb3484c1fa 100644 --- a/packages/roosterjs-content-model-plugins/lib/autoFormat/AutoFormatPlugin.ts +++ b/packages/roosterjs-content-model-plugins/lib/autoFormat/AutoFormatPlugin.ts @@ -220,9 +220,14 @@ export class AutoFormatPlugin implements EditorPlugin { shouldFraction || shouldOrdinals ); - }, - formatOptions + } ); + if (formatOptions.changeSource) { + editor.triggerEvent('contentChanged', { + source: formatOptions.changeSource, + formatApiName: formatOptions.apiName, + }); + } break; } diff --git a/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts b/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts index 9203fa27645..58937a2eaac 100644 --- a/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts +++ b/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts @@ -108,6 +108,11 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOptions.apiName, }); + const triggerEvent = spyOn(editor, 'triggerEvent'); + expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + source: 'AutoFormat', + formatApiName: formatOptions.apiName, + }); }); } @@ -316,6 +321,11 @@ describe('Content Model Auto Format Plugin Test', () => { } ); }); + const triggerEvent = spyOn(editor, 'triggerEvent'); + expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + source: 'AutoLink', + formatApiName: '', + }); } it('should call createLinkAfterSpace', () => { @@ -393,6 +403,11 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOption.apiName, }); + const triggerEvent = spyOn(editor, 'triggerEvent'); + expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + source: 'AutoFormat', + formatApiName: formatOption.apiName, + }); }); } @@ -473,6 +488,11 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOption.apiName, }); + const triggerEvent = spyOn(editor, 'triggerEvent'); + expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + source: 'AutoFormat', + formatApiName: formatOption.apiName, + }); }); } @@ -539,6 +559,11 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOption.apiName, }); + const triggerEvent = spyOn(editor, 'triggerEvent'); + expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + source: 'AutoFormat', + formatApiName: formatOption.apiName, + }); }); } From e2bebafd443a65bec5e72584c615780239a6afda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlia=20Roldi?= Date: Wed, 5 Jun 2024 19:34:30 -0300 Subject: [PATCH 2/4] fix test --- .../test/autoFormat/AutoFormatPluginTest.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts b/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts index 58937a2eaac..56075fddfa3 100644 --- a/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts +++ b/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts @@ -108,8 +108,7 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOptions.apiName, }); - const triggerEvent = spyOn(editor, 'triggerEvent'); - expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { source: 'AutoFormat', formatApiName: formatOptions.apiName, }); @@ -321,8 +320,8 @@ describe('Content Model Auto Format Plugin Test', () => { } ); }); - const triggerEvent = spyOn(editor, 'triggerEvent'); - expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + + expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { source: 'AutoLink', formatApiName: '', }); @@ -403,8 +402,7 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOption.apiName, }); - const triggerEvent = spyOn(editor, 'triggerEvent'); - expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { source: 'AutoFormat', formatApiName: formatOption.apiName, }); @@ -488,8 +486,8 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOption.apiName, }); - const triggerEvent = spyOn(editor, 'triggerEvent'); - expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + + expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { source: 'AutoFormat', formatApiName: formatOption.apiName, }); @@ -559,8 +557,8 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOption.apiName, }); - const triggerEvent = spyOn(editor, 'triggerEvent'); - expect(triggerEvent).toHaveBeenCalledWith('contentChanged', { + + expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { source: 'AutoFormat', formatApiName: formatOption.apiName, }); From bf1533ec8414f2f843f0373756594f1e8456056c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlia=20Roldi?= Date: Wed, 5 Jun 2024 19:42:23 -0300 Subject: [PATCH 3/4] fix test --- .../test/autoFormat/AutoFormatPluginTest.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts b/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts index 56075fddfa3..7c741599870 100644 --- a/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts +++ b/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts @@ -320,11 +320,6 @@ describe('Content Model Auto Format Plugin Test', () => { } ); }); - - expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { - source: 'AutoLink', - formatApiName: '', - }); } it('should call createLinkAfterSpace', () => { From 08c69bf745227790645afe080aa1081f70a2ec2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlia=20Roldi?= Date: Thu, 6 Jun 2024 12:08:22 -0300 Subject: [PATCH 4/4] formatContentModel --- .../formatContentModel/formatContentModel.ts | 15 ++++----------- .../lib/autoFormat/AutoFormatPlugin.ts | 9 ++------- .../test/autoFormat/AutoFormatPluginTest.ts | 18 ------------------ 3 files changed, 6 insertions(+), 36 deletions(-) diff --git a/packages/roosterjs-content-model-core/lib/coreApi/formatContentModel/formatContentModel.ts b/packages/roosterjs-content-model-core/lib/coreApi/formatContentModel/formatContentModel.ts index bcebd23f23b..6a54afab304 100644 --- a/packages/roosterjs-content-model-core/lib/coreApi/formatContentModel/formatContentModel.ts +++ b/packages/roosterjs-content-model-core/lib/coreApi/formatContentModel/formatContentModel.ts @@ -24,15 +24,8 @@ export const formatContentModel: FormatContentModel = ( options, domToModelOptions ) => { - const { - apiName, - onNodeCreated, - getChangeData, - changeSource, - rawEvent, - selectionOverride, - scrollCaretIntoView, - } = options || {}; + const { onNodeCreated, getChangeData, rawEvent, selectionOverride, scrollCaretIntoView } = + options || {}; const model = core.api.createContentModel(core, domToModelOptions, selectionOverride); const context: FormatContentModelContext = { newEntities: [], @@ -82,9 +75,9 @@ export const formatContentModel: FormatContentModel = ( eventType: 'contentChanged', contentModel: clearModelCache ? undefined : model, selection: clearModelCache ? undefined : selection, - source: changeSource || ChangeSource.Format, + source: options?.changeSource || ChangeSource.Format, data: getChangeData?.(), - formatApiName: apiName, + formatApiName: options?.apiName, changedEntities: getChangedEntities(context, rawEvent), }; diff --git a/packages/roosterjs-content-model-plugins/lib/autoFormat/AutoFormatPlugin.ts b/packages/roosterjs-content-model-plugins/lib/autoFormat/AutoFormatPlugin.ts index bdb3484c1fa..b0936bdf511 100644 --- a/packages/roosterjs-content-model-plugins/lib/autoFormat/AutoFormatPlugin.ts +++ b/packages/roosterjs-content-model-plugins/lib/autoFormat/AutoFormatPlugin.ts @@ -220,14 +220,9 @@ export class AutoFormatPlugin implements EditorPlugin { shouldFraction || shouldOrdinals ); - } + }, + formatOptions ); - if (formatOptions.changeSource) { - editor.triggerEvent('contentChanged', { - source: formatOptions.changeSource, - formatApiName: formatOptions.apiName, - }); - } break; } diff --git a/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts b/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts index 7c741599870..9203fa27645 100644 --- a/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts +++ b/packages/roosterjs-content-model-plugins/test/autoFormat/AutoFormatPluginTest.ts @@ -108,10 +108,6 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOptions.apiName, }); - expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { - source: 'AutoFormat', - formatApiName: formatOptions.apiName, - }); }); } @@ -397,10 +393,6 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOption.apiName, }); - expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { - source: 'AutoFormat', - formatApiName: formatOption.apiName, - }); }); } @@ -481,11 +473,6 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOption.apiName, }); - - expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { - source: 'AutoFormat', - formatApiName: formatOption.apiName, - }); }); } @@ -552,11 +539,6 @@ describe('Content Model Auto Format Plugin Test', () => { changeSource: 'AutoFormat', apiName: formatOption.apiName, }); - - expect(triggerEventSpy).toHaveBeenCalledWith('contentChanged', { - source: 'AutoFormat', - formatApiName: formatOption.apiName, - }); }); }