Skip to content

Commit

Permalink
#2878 Provide a callback function to allow fixup model before write b…
Browse files Browse the repository at this point in the history
…ack (#2890)
  • Loading branch information
JiuqingSong authored Nov 22, 2024
1 parent 24f5af0 commit 64eb364
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export const setContentModel: SetContentModel = (

modelToDomContext.onNodeCreated = onNodeCreated;

core.onFixUpModel?.(model);

const selection = contentModelToDom(
core.logicalRoot.ownerDocument,
core.logicalRoot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function createEditorCore(contentDiv: HTMLDivElement, options: EditorOpti
domHelper: createDOMHelper(contentDiv),
...getPluginState(corePlugins),
disposeErrorHandler: options.disposeErrorHandler,
onFixUpModel: options.onFixUpModel,
experimentalFeatures: options.experimentalFeatures ? [...options.experimentalFeatures] : [],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,13 @@ describe('setContentModel', () => {
const mockedRange = {
type: 'image',
} as any;
const mockedOnFixUpModel = jasmine.createSpy('fixupModel');

contentModelToDomSpy.and.returnValue(mockedRange);

core.environment.modelToDomSettings.builtIn = defaultOption;
(core as any).onFixUpModel = mockedOnFixUpModel;

setContentModel(core, mockedModel, additionalOption);

expect(createModelToDomContextSpy).toHaveBeenCalledWith(
Expand All @@ -136,6 +139,8 @@ describe('setContentModel', () => {
mockedContext
);
expect(setDOMSelectionSpy).toHaveBeenCalledWith(core, mockedRange);
expect(mockedOnFixUpModel).toHaveBeenCalledWith(mockedModel);
expect(mockedOnFixUpModel).toHaveBeenCalledBefore(contentModelToDomSpy);
});

it('no default option, with shadow edit', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ describe('createEditorCore', () => {
domHelper: mockedDOMHelper,
disposeErrorHandler: undefined,
experimentalFeatures: [],
onFixUpModel: undefined,
...additionalResult,
});

Expand Down Expand Up @@ -149,6 +150,7 @@ describe('createEditorCore', () => {
const mockedDisposeErrorHandler = 'DISPOSE' as any;
const mockedGenerateColorKey = 'KEY' as any;
const mockedKnownColors = 'COLORS' as any;
const mockedOnFixUpModel = 'FIXUP' as any;
const mockedOptions = {
coreApiOverride: {
a: 'b',
Expand All @@ -159,6 +161,7 @@ describe('createEditorCore', () => {
disposeErrorHandler: mockedDisposeErrorHandler,
generateColorKey: mockedGenerateColorKey,
knownColors: mockedKnownColors,
onFixUpModel: mockedOnFixUpModel,
} as any;

runTest(mockedDiv, mockedOptions, {
Expand All @@ -181,6 +184,7 @@ describe('createEditorCore', () => {
darkColorHandler: mockedDarkColorHandler,
trustedHTMLHandler: mockedTrustHtmlHandler,
disposeErrorHandler: mockedDisposeErrorHandler,
onFixUpModel: mockedOnFixUpModel,
});

expect(DarkColorHandlerImpl.createDarkColorHandler).toHaveBeenCalledWith(
Expand Down
12 changes: 11 additions & 1 deletion packages/roosterjs-content-model-types/lib/editor/EditorCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import type { DOMEventRecord } from '../parameter/DOMEventRecord';
import type { Snapshot } from '../parameter/Snapshot';
import type { EntityState } from '../parameter/FormatContentModelContext';
import type { DarkColorHandler } from '../context/DarkColorHandler';
import type { ContentModelDocument } from '../contentModel/blockGroup/ContentModelDocument';
import type {
ContentModelDocument,
ReadonlyContentModelDocument,
} from '../contentModel/blockGroup/ContentModelDocument';
import type { DOMSelection } from '../selection/DOMSelection';
import type { DomToModelOptionForCreateModel } from '../context/DomToModelOption';
import type { EditorContext } from '../context/EditorContext';
Expand Down Expand Up @@ -376,6 +379,13 @@ export interface EditorCore extends PluginState {
*/
readonly disposeErrorHandler?: (plugin: EditorPlugin, error: Error) => void;

/**
* An optional callback function that will be invoked before write content model back to editor.
* This is used for make sure model can satisfy some customized requirement
* @param model The model to fix up
*/
readonly onFixUpModel?: (model: ReadonlyContentModelDocument) => void;

/**
* Enabled experimental features
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import type { ContentModelSegmentFormat } from '../contentModel/format/ContentMo
import type { CoreApiMap } from './EditorCore';
import type { DomToModelOption } from '../context/DomToModelOption';
import type { ModelToDomOption } from '../context/ModelToDomOption';
import type { ContentModelDocument } from '../contentModel/blockGroup/ContentModelDocument';
import type {
ContentModelDocument,
ReadonlyContentModelDocument,
} from '../contentModel/blockGroup/ContentModelDocument';
import type { Snapshots } from '../parameter/Snapshot';
import type { TrustedHTMLHandler } from '../parameter/TrustedHTMLHandler';

Expand Down Expand Up @@ -68,6 +71,13 @@ export interface ContentModelOptions {
*/
defaultSegmentFormat?: ContentModelSegmentFormat;

/**
* An optional callback function that will be invoked before write content model back to editor.
* This is used for make sure model can satisfy some customized requirement
* @param model The model to fix up
*/
onFixUpModel?: (model: ReadonlyContentModelDocument) => void;

/**
* @deprecated
*/
Expand Down

0 comments on commit 64eb364

Please sign in to comment.