Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2855 Add a parameter for formatContentModel to allow auto detection of new/removed entities #2887

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,25 @@ function handlePendingFormat(
}
}

function getChangedEntities(context: FormatContentModelContext, rawEvent?: Event): ChangedEntity[] {
return context.newEntities
.map(
(entity): ChangedEntity => ({
entity,
operation: 'newEntity',
rawEvent,
})
)
.concat(
context.deletedEntities.map(entry => ({
entity: entry.entity,
operation: entry.operation,
rawEvent,
}))
);
function getChangedEntities(
context: FormatContentModelContext,
rawEvent?: Event
): ChangedEntity[] | undefined {
return context.autoDetectChangedEntities
? undefined
: context.newEntities
.map(
(entity): ChangedEntity => ({
entity,
operation: 'newEntity',
rawEvent,
})
)
.concat(
context.deletedEntities.map(entry => ({
entity: entry.entity,
operation: entry.operation,
rawEvent,
}))
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1070,4 +1070,138 @@ describe('formatContentModel', () => {
expect(announce).toHaveBeenCalledWith(core, mockedData);
});
});

describe('Changed entities', () => {
it('Callback return true, changed entities are not specified', () => {
const callback = jasmine.createSpy('callback').and.returnValue(true);

formatContentModel(core, callback, { apiName });

expect(callback).toHaveBeenCalledWith(mockedModel, {
newEntities: [],
deletedEntities: [],
rawEvent: undefined,
newImages: [],
});
expect(createContentModel).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalled();
expect(setContentModel).toHaveBeenCalled();
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: 'contentChanged',
contentModel: mockedModel,
selection: mockedSelection,
source: 'Format',
data: undefined,
formatApiName: 'mockedApi',
changedEntities: [],
},
true
);
});

it('Callback return true, changed entities are specified', () => {
const wrapper1 = document.createElement('span');
const wrapper2 = document.createElement('div');
const callback = jasmine
.createSpy('callback')
.and.callFake((model: any, context: FormatContentModelContext) => {
context.newEntities.push({
segmentType: 'Entity',
blockType: 'Entity',
entityFormat: {
entityType: 'test',
},
format: {},
wrapper: wrapper1,
});
context.deletedEntities.push({
entity: {
segmentType: 'Entity',
blockType: 'Entity',
entityFormat: {
entityType: 'test',
},
format: {},
wrapper: wrapper2,
},
operation: 'overwrite',
});
return true;
});

formatContentModel(core, callback, { apiName });

expect(callback).toHaveBeenCalled();
expect(createContentModel).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalled();
expect(setContentModel).toHaveBeenCalled();
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: 'contentChanged',
contentModel: mockedModel,
selection: mockedSelection,
source: 'Format',
data: undefined,
formatApiName: 'mockedApi',
changedEntities: [
{
entity: {
segmentType: 'Entity',
blockType: 'Entity',
entityFormat: { entityType: 'test' },
format: {},
wrapper: wrapper1,
},
operation: 'newEntity',
rawEvent: undefined,
},
{
entity: {
segmentType: 'Entity',
blockType: 'Entity',
entityFormat: { entityType: 'test' },
format: {},
wrapper: wrapper2,
},
operation: 'overwrite',
rawEvent: undefined,
},
],
},
true
);
});

it('Callback return true, auto detect entity change', () => {
const callback = jasmine
.createSpy('callback')
.and.callFake((model: any, context: FormatContentModelContext) => {
context.autoDetectChangedEntities = true;
return true;
});

formatContentModel(core, callback, { apiName });

expect(callback).toHaveBeenCalled();
expect(createContentModel).toHaveBeenCalledTimes(1);
expect(addUndoSnapshot).toHaveBeenCalled();
expect(setContentModel).toHaveBeenCalled();
expect(triggerEvent).toHaveBeenCalledWith(
core,
{
eventType: 'contentChanged',
contentModel: mockedModel,
selection: mockedSelection,
source: 'Format',
data: undefined,
formatApiName: 'mockedApi',
changedEntities: undefined,
},
true
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ export interface DeletedEntity {
*/
export interface FormatContentModelContext {
/**
* New entities added during the format process
* New entities added during the format process. This value is only respected when autoDetectChangedEntities is not set to true
*/
readonly newEntities: ContentModelEntity[];

/**
* Entities got deleted during formatting. Need to be set by the formatter function
* This value is only respected when autoDetectChangedEntities is not set to true
*/
readonly deletedEntities: DeletedEntity[];

Expand Down Expand Up @@ -113,4 +114,9 @@ export interface FormatContentModelContext {
* @optional Set this value to tell AnnouncePlugin to announce the given information
*/
announceData?: AnnounceData | null;

/**
* @optional When set to true, EntityPlugin will detect any entity changes during this process, newEntities and deletedEntities will be ignored
*/
autoDetectChangedEntities?: boolean;
}
Loading