Skip to content

Commit

Permalink
Merge pull request #2517 from microsoft/u/juliaroldi/patch-fixes
Browse files Browse the repository at this point in the history
Patch Fixes for Content Model Core and Plugins
  • Loading branch information
juliaroldi authored Mar 20, 2024
2 parents c7c1259 + b434470 commit 4934b8d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cloneModelForPaste, mergePasteContent } from '../../utils/paste/mergePasteContent';
import { convertInlineCss } from '../../utils/convertInlineCss';
import { createPasteFragment } from '../../utils/paste/createPasteFragment';
import { generatePasteOptionFromPlugins } from '../../utils/paste/generatePasteOptionFromPlugins';
import { mergePasteContent } from '../../utils/paste/mergePasteContent';
import { retrieveHtmlInfo } from '../../utils/paste/retrieveHtmlInfo';
import type {
PasteType,
Expand All @@ -26,7 +26,9 @@ export function paste(
const trustedHTMLHandler = editor.getTrustedHTMLHandler();

if (!clipboardData.modelBeforePaste) {
clipboardData.modelBeforePaste = editor.getContentModelCopy('connected');
clipboardData.modelBeforePaste = cloneModelForPaste(
editor.getContentModelCopy('connected')
);
}

// 1. Prepare variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ const EmptySegmentFormat: Required<ContentModelSegmentFormat> = {
textColor: '',
underline: false,
};

const CloneOption: CloneModelOptions = {
includeCachedElement: (node, type) => (type == 'cache' ? undefined : node),
};

/**
* @internal
*/
export function cloneModelForPaste(model: ContentModelDocument) {
return cloneModel(model, CloneOption);
}

/**
* @internal
*/
Expand All @@ -45,7 +53,7 @@ export function mergePasteContent(
editor.formatContentModel(
(model, context) => {
if (clipboardData.modelBeforePaste) {
const clonedModel = cloneModel(clipboardData.modelBeforePaste, CloneOption);
const clonedModel = cloneModelForPaste(clipboardData.modelBeforePaste);
model.blocks = clonedModel.blocks;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export function handleTabOnParagraph(
selectedSegments.length === 1 && selectedSegments[0].segmentType === 'SelectionMarker';
const isAllSelected = paragraph.segments.every(segment => segment.isSelected);
if ((paragraph.segments[0].segmentType === 'SelectionMarker' && isCollapsed) || isAllSelected) {
const { marginLeft, marginRight, direction } = paragraph.format;
const isRtl = direction === 'rtl';
if (
rawEvent.shiftKey &&
((!isRtl && (!marginLeft || marginLeft == '0px')) ||
(isRtl && (!marginRight || marginRight == '0px')))
) {
return false;
}
setModelIndentation(model, rawEvent.shiftKey ? 'outdent' : 'indent');
} else {
if (!isCollapsed) {
Expand Down Expand Up @@ -84,7 +93,6 @@ export function handleTabOnParagraph(
}
}
}

rawEvent.preventDefault();
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('handleTabOnParagraph', () => {
runTest(model, paragraph, rawEvent, selection, true);
});

it('Outdent - collapsed range should return true when cursor is at the start', () => {
it('Outdent - collapsed range should return false when cursor is at the start', () => {
const model: ContentModelDocument = {
blockGroupType: 'Document',
blocks: [
Expand Down Expand Up @@ -165,6 +165,45 @@ describe('handleTabOnParagraph', () => {
collapsed: true,
},
} as RangeSelection;
runTest(model, paragraph, rawEvent, selection, false);
});

it('Outdent - collapsed range should return true when cursor is at the start and exist indentation', () => {
const model: ContentModelDocument = {
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
{
segmentType: 'Text',
text: 'test',
format: {},
},
],
format: {
marginLeft: '4px',
},
},
],
format: {},
};
const paragraph = model.blocks[0] as ContentModelParagraph;
const rawEvent = new KeyboardEvent('keydown', {
key: 'Tab',
shiftKey: true,
});
const selection = {
type: 'range',
range: {
collapsed: true,
},
} as RangeSelection;
runTest(model, paragraph, rawEvent, selection, true);
});

Expand Down
5 changes: 4 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"react": "8.55.0",
"main": "0.28.0",
"legacyAdapter": "0.28.0",
"overrides": {}
"overrides": {
"roosterjs-content-model-core": "0.28.1",
"roosterjs-content-model-plugins": "0.28.1"
}
}

0 comments on commit 4934b8d

Please sign in to comment.