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

Fix 262677: Allow insert entity to a specified position without changing selection #2537

Merged
merged 5 commits into from
Mar 28, 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 @@ -13,9 +13,9 @@ import type {
ContentModelDocument,
ContentModelEntity,
ContentModelParagraph,
DeleteSelectionResult,
FormatContentModelContext,
InsertEntityPosition,
InsertPoint,
} from 'roosterjs-content-model-types';

/**
Expand All @@ -27,11 +27,12 @@ export function insertEntityModel(
position: InsertEntityPosition,
isBlock: boolean,
focusAfterEntity?: boolean,
context?: FormatContentModelContext
context?: FormatContentModelContext,
insertPointOverride?: InsertPoint
) {
let blockParent: ContentModelBlockGroup | undefined;
let blockIndex = -1;
let deleteResult: DeleteSelectionResult;
let insertPoint: InsertPoint | null;

if (position == 'begin' || position == 'end') {
blockParent = model;
Expand All @@ -40,12 +41,8 @@ export function insertEntityModel(
if (!isBlock) {
Object.assign(entityModel.format, model.format);
}
} else if ((deleteResult = deleteSelection(model, [], context)).insertPoint) {
const { marker, paragraph, path } = deleteResult.insertPoint;

if (deleteResult.deleteResult == 'range') {
normalizeContentModel(model);
}
} else if ((insertPoint = getInsertPoint(model, insertPointOverride, context))) {
const { marker, paragraph, path } = insertPoint;

if (!isBlock) {
const index = paragraph.segments.indexOf(marker);
Expand Down Expand Up @@ -111,3 +108,22 @@ export function insertEntityModel(
}
}
}

function getInsertPoint(
model: ContentModelDocument,
insertPointOverride?: InsertPoint,
context?: FormatContentModelContext
): InsertPoint | null {
if (insertPointOverride) {
return insertPointOverride;
} else {
const deleteResult = deleteSelection(model, [], context);
const insertPoint = deleteResult.insertPoint;

if (deleteResult.deleteResult == 'range') {
normalizeContentModel(model);
}

return insertPoint;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { formatInsertPointWithContentModel } from '../utils/formatInsertPointWithContentModel';
import { insertEntityModel } from '../../modelApi/entity/insertEntityModel';
import {
ChangeSource,
Expand All @@ -7,11 +8,15 @@ import {
} from 'roosterjs-content-model-dom';
import type {
ContentModelEntity,
DOMSelection,
InsertEntityPosition,
InsertEntityOptions,
IEditor,
EntityState,
DOMInsertPoint,
FormatContentModelOptions,
ContentModelDocument,
FormatContentModelContext,
InsertPoint,
} from 'roosterjs-content-model-types';

const BlockEntityTag = 'div';
Expand All @@ -32,7 +37,7 @@ export function insertEntity(
editor: IEditor,
type: string,
isBlock: boolean,
position: 'focus' | 'begin' | 'end' | DOMSelection,
position: 'focus' | 'begin' | 'end' | DOMInsertPoint,
options?: InsertEntityOptions
): ContentModelEntity | null;

Expand All @@ -51,15 +56,15 @@ export function insertEntity(
editor: IEditor,
type: string,
isBlock: true,
position: InsertEntityPosition | DOMSelection,
position: InsertEntityPosition | DOMInsertPoint,
options?: InsertEntityOptions
): ContentModelEntity | null;

export function insertEntity(
editor: IEditor,
type: string,
isBlock: boolean,
position?: InsertEntityPosition | DOMSelection,
position?: InsertEntityPosition | DOMInsertPoint,
options?: InsertEntityOptions
): ContentModelEntity | null {
const { contentNode, focusAfterEntity, wrapperDisplay, skipUndoSnapshot, initialEntityState } =
Expand All @@ -85,36 +90,45 @@ export function insertEntity(
editor.takeSnapshot();
}

editor.formatContentModel(
(model, context) => {
insertEntityModel(
model,
entityModel,
typeof position == 'string' ? position : 'focus',
isBlock,
focusAfterEntity,
context
);

normalizeContentModel(model);

context.skipUndoSnapshot = true;
context.newEntities.push(entityModel);

return true;
},
{
selectionOverride: typeof position === 'object' ? position : undefined,
changeSource: ChangeSource.InsertEntity,
getChangeData: () => ({
wrapper,
type,
id: '',
isReadonly: true,
}),
apiName: 'insertEntity',
}
);
const formatOptions: FormatContentModelOptions = {
changeSource: ChangeSource.InsertEntity,
getChangeData: () => ({
wrapper,
type,
id: '',
isReadonly: true,
}),
apiName: 'insertEntity',
};

const callback = (
model: ContentModelDocument,
context: FormatContentModelContext,
insertPoint?: InsertPoint
) => {
insertEntityModel(
model,
entityModel,
typeof position == 'string' ? position : 'focus',
isBlock,
focusAfterEntity,
context,
insertPoint
);

normalizeContentModel(model);

context.skipUndoSnapshot = true;
context.newEntities.push(entityModel);

return true;
};

if (typeof position == 'object') {
formatInsertPointWithContentModel(editor, position, callback, formatOptions);
} else {
editor.formatContentModel(callback, formatOptions);
}

if (!skipUndoSnapshot) {
let entityState: EntityState | undefined;
Expand Down
Loading
Loading