Skip to content

Commit

Permalink
Do not focus to editor when formatWithContentModel (#2130)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong authored Oct 6, 2023
1 parent 565ac17 commit bee10b7
Show file tree
Hide file tree
Showing 43 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ export default function setAlignment(
editor: IContentModelEditor,
alignment: 'left' | 'center' | 'right'
) {
editor.focus();

formatWithContentModel(editor, 'setAlignment', model => setModelAlignment(model, alignment));
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param direction Direction value: ltr (Left to right) or rtl (Right to left)
*/
export default function setDirection(editor: IContentModelEditor, direction: 'ltr' | 'rtl') {
editor.focus();

formatWithContentModel(editor, 'setDirection', model => setModelDirection(model, direction));
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default function setHeadingLevel(
editor: IContentModelEditor,
headingLevel: 0 | 1 | 2 | 3 | 4 | 5 | 6
) {
editor.focus();

formatParagraphWithContentModel(editor, 'setHeadingLevel', para => {
const tagName =
headingLevel > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default function setIndentation(
indentation: 'indent' | 'outdent',
length?: number
) {
editor.focus();

formatWithContentModel(
editor,
'setIndentation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default function setParagraphMargin(
marginTop?: string | null,
marginBottom?: string | null
) {
editor.focus();

formatParagraphWithContentModel(editor, 'setParagraphMargin', para => {
if (!para.decorator) {
para.decorator = createParagraphDecorator('p');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param spacing Unitless/px value to set line height
*/
export default function setSpacing(editor: IContentModelEditor, spacing: number | string) {
editor.focus();

formatParagraphWithContentModel(editor, 'setSpacing', paragraph => {
paragraph.format.lineHeight = spacing.toString();
paragraph.segments.forEach(segment => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export default function toggleBlockQuote(
...quoteFormat,
};

editor.focus();

formatWithContentModel(
editor,
'toggleBlockQuote',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type {
* @param editor The editor to clear format from
*/
export default function clearFormat(editor: IContentModelEditor) {
editor.focus();

formatWithContentModel(editor, 'clearFormat', model => {
const blocksToClear: [ContentModelBlockGroup[], ContentModelBlock][] = [];
const segmentsToClear: ContentModelSegment[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param file The image file
*/
export default function changeImage(editor: IContentModelEditor, file: File) {
editor.focus();

const selection = editor.getDOMSelection();
readFile(file, dataUrl => {
if (dataUrl && !editor.isDisposed() && selection?.type === 'image') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param file Image Blob file or source string
*/
export default function insertImage(editor: IContentModelEditor, imageFileOrSrc: File | string) {
editor.focus();

if (typeof imageFileOrSrc == 'string') {
insertImageWithSrc(editor, imageFileOrSrc);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param altText The image alt text
*/
export default function setImageAltText(editor: IContentModelEditor, altText: string) {
editor.focus();

formatImageWithContentModel(editor, 'setImageAltText', (image: ContentModelImage) => {
image.alt = altText;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default function setImageBorder(
border: Border | null,
borderRadius?: string
) {
editor.focus();

formatImageWithContentModel(editor, 'setImageBorder', (image: ContentModelImage) => {
applyImageBorderFormat(image, border, borderRadius);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default function setImageBoxShadow(
boxShadow: string,
margin?: string | null
) {
editor.focus();

formatImageWithContentModel(editor, 'setImageBoxShadow', (image: ContentModelImage) => {
image.format.boxShadow = boxShadow;
if (margin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export default function insertLink(
displayText?: string,
target?: string
) {
editor.focus();

let url = (checkXss(link) || '').trim();
if (url) {
const linkData = matchLink(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param editor The editor instance
*/
export default function removeLink(editor: IContentModelEditor) {
editor.focus();

formatWithContentModel(editor, 'removeLink', model => {
adjustSegmentSelection(
model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param value The number to set to, must be equal or greater than 1
*/
export default function setListStartNumber(editor: IContentModelEditor, value: number) {
editor.focus();

formatWithContentModel(editor, 'setListStartNumber', model => {
const listItem = getFirstSelectedListItem(model);
const level = listItem?.levels[listItem?.levels.length - 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { ListMetadataFormat } from 'roosterjs-content-model-types';
* @param style The target list item style to set
*/
export default function setListStyle(editor: IContentModelEditor, style: ListMetadataFormat) {
editor.focus();

formatWithContentModel(editor, 'setListStyle', model => {
const listItem = getFirstSelectedListItem(model);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param editor The editor to operate on
*/
export default function toggleBullet(editor: IContentModelEditor) {
editor.focus();

formatWithContentModel(editor, 'toggleBullet', model => setListType(model, 'UL'), {
preservePendingFormat: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param editor The editor to operate on
*/
export default function toggleNumbering(editor: IContentModelEditor) {
editor.focus();

formatWithContentModel(editor, 'toggleNumbering', model => setListType(model, 'OL'), {
preservePendingFormat: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default function changeCapitalization(
capitalization: 'sentence' | 'lowerCase' | 'upperCase' | 'capitalize',
language?: string
) {
editor.focus();

formatSegmentWithContentModel(editor, 'changeCapitalization', (_, __, segment) => {
if (segment?.segmentType == 'Text') {
switch (capitalization) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default function changeFontSize(
editor: IContentModelEditor,
change: 'increase' | 'decrease'
) {
editor.focus();

formatSegmentWithContentModel(
editor,
'changeFontSize',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default function setBackgroundColor(
editor: IContentModelEditor,
backgroundColor: string | null
) {
editor.focus();

let lastParagraph: ContentModelParagraph | null = null;
let lastSegmentIndex: number = -1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param fontName The font name to set
*/
export default function setFontName(editor: IContentModelEditor, fontName: string) {
editor.focus();

formatSegmentWithContentModel(
editor,
'setFontName',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param fontSize The font size to set
*/
export default function setFontSize(editor: IContentModelEditor, fontSize: string) {
editor.focus();

formatSegmentWithContentModel(
editor,
'setFontSize',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param textColor The text color to set. Pass null to remove existing color.
*/
export default function setTextColor(editor: IContentModelEditor, textColor: string | null) {
editor.focus();

formatSegmentWithContentModel(
editor,
'setTextColor',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param editor The editor to operate on
*/
export default function toggleBold(editor: IContentModelEditor) {
editor.focus();

formatSegmentWithContentModel(
editor,
'toggleBold',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const DefaultCode: ContentModelCode = {
* @param editor The editor to operate on
*/
export default function toggleCode(editor: IContentModelEditor) {
editor.focus();

formatSegmentWithContentModel(
editor,
'toggleCode',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param editor The editor to operate on
*/
export default function toggleItalic(editor: IContentModelEditor) {
editor.focus();

formatSegmentWithContentModel(
editor,
'toggleItalic',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param editor The editor to operate on
*/
export default function toggleStrikethrough(editor: IContentModelEditor) {
editor.focus();

formatSegmentWithContentModel(
editor,
'toggleStrikethrough',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param editor The editor to operate on
*/
export default function toggleSubscript(editor: IContentModelEditor) {
editor.focus();

formatSegmentWithContentModel(
editor,
'toggleSubscript',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param editor The editor to operate on
*/
export default function toggleSuperscript(editor: IContentModelEditor) {
editor.focus();

formatSegmentWithContentModel(
editor,
'toggleSuperscript',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param editor The editor to operate on
*/
export default function toggleUnderline(editor: IContentModelEditor) {
editor.focus();

formatSegmentWithContentModel(
editor,
'toggleUnderline',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
* @param operation The table operation to apply
*/
export default function editTable(editor: IContentModelEditor, operation: TableOperation) {
editor.focus();

formatWithContentModel(editor, 'editTable', model => {
const [tableModel, path] = getFirstSelectedTable(model);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default function formatTable(
format: TableMetadataFormat,
keepCellShade?: boolean
) {
editor.focus();

formatWithContentModel(editor, 'formatTable', model => {
const [tableModel] = getFirstSelectedTable(model);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default function insertTable(
rows: number,
format?: Partial<TableMetadataFormat>
) {
editor.focus();

formatWithContentModel(editor, 'insertTable', (model, context) => {
const insertPosition = deleteSelection(model, [], context).insertPoint;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type { IContentModelEditor } from '../../publicTypes/IContentModelEditor'
* @param color The color to set. Pass null to remove existing shade color
*/
export default function setTableCellShade(editor: IContentModelEditor, color: string | null) {
editor.focus();

formatWithContentModel(editor, 'setTableCellShade', model => {
const [table] = getFirstSelectedTable(model);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ export function formatWithContentModel(
selectionOverride,
} = options || {};

editor.focus();

const model = editor.createContentModel(undefined /*option*/, selectionOverride);
const context: FormatWithContentModelContext = {
newEntities: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export default function paste(
clipboardData.snapshotBeforePaste = editor.getContent(GetContentMode.RawHTMLWithSelection);
}

editor.focus();

formatWithContentModel(
editor,
'Paste',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { IContentModelEditor } from '../../../lib/publicTypes/IContentModelEdito

describe('clearFormat', () => {
it('Clear format', () => {
const editor = ({} as any) as IContentModelEditor;
const editor = ({
focus: () => {},
} as any) as IContentModelEditor;
const model = ('Model' as any) as ContentModelDocument;

spyOn(formatWithContentModel, 'formatWithContentModel').and.callFake(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ describe('setListStartNumber', () => {
}
);

setListStartNumber(null!, 2);
setListStartNumber(
{
focus: () => {},
} as any,
2
);

expect(formatWithContentModel.formatWithContentModel).toHaveBeenCalledTimes(1);
expect(input).toEqual(expectedModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ describe('setListStyle', () => {
}
);

setListStyle(null!, style);
setListStyle(
{
focus: () => {},
} as any,
style
);

expect(formatWithContentModel.formatWithContentModel).toHaveBeenCalledTimes(1);
expect(input).toEqual(expectedModel);
Expand Down
Loading

0 comments on commit bee10b7

Please sign in to comment.