Skip to content

Commit

Permalink
Fix entity issue
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Sep 21, 2023
1 parent 6f1886b commit 7b905aa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ export default class ContentModelEventViewPane extends React.Component<
<pre className={styles.eventContent}>
{JSON.stringify(
(event as ContentModelContentChangedEvent).contentModel,
null,
(key, value) =>
safeInstanceOf(value, 'Node')
? Object.prototype.toString.apply(value)
: key == 'src'
? value.length > 100
? value.substring(0, 97) + '...'
: value
: value,
2
)}
</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default function insertEntity(
commitEntity(wrapper, type, true /*isReadonly*/);

const entityModel = createEntity(wrapper, true /*isReadonly*/, type);
let newEntity: Entity | null = null;

formatWithContentModel(
editor,
Expand All @@ -93,12 +94,13 @@ export default function insertEntity(
},
{
selectionOverride: typeof position === 'object' ? position : undefined,
changeSource: ChangeSource.InsertEntity,
getChangeData: () => {
newEntity = getEntityFromElement(wrapper);
return newEntity;
},
}
);

const newEntity = getEntityFromElement(wrapper);

editor.triggerContentChangedEvent(ChangeSource.InsertEntity, newEntity);

return newEntity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('insertEntity', () => {
'formatWithContentModel'
).and.callFake((editor, apiName, formatter, options) => {
formatter(model, context);
options?.getChangeData?.();
});
getEntityFromElementSpy = spyOn(getEntityFromElement, 'default').and.returnValue(newEntity);
commitEntitySpy = spyOn(commitEntity, 'default');
Expand Down Expand Up @@ -82,9 +83,9 @@ describe('insertEntity', () => {
expect(commitEntitySpy).toHaveBeenCalledWith(wrapper, type, true);
expect(formatWithContentModelSpy.calls.argsFor(0)[0]).toBe(editor);
expect(formatWithContentModelSpy.calls.argsFor(0)[1]).toBe(apiName);
expect(formatWithContentModelSpy.calls.argsFor(0)[3]).toEqual({
selectionOverride: undefined,
});
expect(formatWithContentModelSpy.calls.argsFor(0)[3].changeSource).toEqual(
ChangeSource.InsertEntity
);
expect(insertEntityModelSpy).toHaveBeenCalledWith(
model,
{
Expand All @@ -102,10 +103,7 @@ describe('insertEntity', () => {
context
);
expect(getEntityFromElementSpy).toHaveBeenCalledWith(wrapper);
expect(triggerContentChangedEventSpy).toHaveBeenCalledWith(
ChangeSource.InsertEntity,
newEntity
);
expect(triggerContentChangedEventSpy).not.toHaveBeenCalled();
expect(transformToDarkColorSpy).not.toHaveBeenCalled();
expect(normalizeContentModelSpy).toHaveBeenCalled();

Expand All @@ -121,9 +119,9 @@ describe('insertEntity', () => {
expect(commitEntitySpy).toHaveBeenCalledWith(wrapper, type, true);
expect(formatWithContentModelSpy.calls.argsFor(0)[0]).toBe(editor);
expect(formatWithContentModelSpy.calls.argsFor(0)[1]).toBe(apiName);
expect(formatWithContentModelSpy.calls.argsFor(0)[3]).toEqual({
selectionOverride: undefined,
});
expect(formatWithContentModelSpy.calls.argsFor(0)[3].changeSource).toEqual(
ChangeSource.InsertEntity
);
expect(insertEntityModelSpy).toHaveBeenCalledWith(
model,
{
Expand All @@ -141,10 +139,7 @@ describe('insertEntity', () => {
context
);
expect(getEntityFromElementSpy).toHaveBeenCalledWith(wrapper);
expect(triggerContentChangedEventSpy).toHaveBeenCalledWith(
ChangeSource.InsertEntity,
newEntity
);
expect(triggerContentChangedEventSpy).not.toHaveBeenCalled();
expect(transformToDarkColorSpy).not.toHaveBeenCalled();
expect(normalizeContentModelSpy).toHaveBeenCalled();

Expand All @@ -167,9 +162,10 @@ describe('insertEntity', () => {
expect(commitEntitySpy).toHaveBeenCalledWith(wrapper, type, true);
expect(formatWithContentModelSpy.calls.argsFor(0)[0]).toBe(editor);
expect(formatWithContentModelSpy.calls.argsFor(0)[1]).toBe(apiName);
expect(formatWithContentModelSpy.calls.argsFor(0)[3]).toEqual({
selectionOverride: range,
});
expect(formatWithContentModelSpy.calls.argsFor(0)[3].changeSource).toEqual(
ChangeSource.InsertEntity
);

expect(insertEntityModelSpy).toHaveBeenCalledWith(
model,
{
Expand All @@ -187,10 +183,7 @@ describe('insertEntity', () => {
context
);
expect(getEntityFromElementSpy).toHaveBeenCalledWith(wrapper);
expect(triggerContentChangedEventSpy).toHaveBeenCalledWith(
ChangeSource.InsertEntity,
newEntity
);
expect(triggerContentChangedEventSpy).not.toHaveBeenCalled();
expect(transformToDarkColorSpy).not.toHaveBeenCalled();
expect(normalizeContentModelSpy).toHaveBeenCalled();

Expand All @@ -208,9 +201,9 @@ describe('insertEntity', () => {
expect(commitEntitySpy).toHaveBeenCalledWith(wrapper, type, true);
expect(formatWithContentModelSpy.calls.argsFor(0)[0]).toBe(editor);
expect(formatWithContentModelSpy.calls.argsFor(0)[1]).toBe(apiName);
expect(formatWithContentModelSpy.calls.argsFor(0)[3]).toEqual({
selectionOverride: undefined,
});
expect(formatWithContentModelSpy.calls.argsFor(0)[3].changeSource).toEqual(
ChangeSource.InsertEntity
);
expect(insertEntityModelSpy).toHaveBeenCalledWith(
model,
{
Expand All @@ -228,10 +221,7 @@ describe('insertEntity', () => {
context
);
expect(getEntityFromElementSpy).toHaveBeenCalledWith(wrapper);
expect(triggerContentChangedEventSpy).toHaveBeenCalledWith(
ChangeSource.InsertEntity,
newEntity
);
expect(triggerContentChangedEventSpy).not.toHaveBeenCalled();
expect(normalizeContentModelSpy).toHaveBeenCalled();

expect(context.newEntities).toEqual([
Expand Down

0 comments on commit 7b905aa

Please sign in to comment.