Skip to content

Commit

Permalink
Merge branch 'u/jisong/config4' into u/jisong/cache5
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Sep 18, 2023
2 parents e7b83d4 + 089b5ee commit 4bc2505
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as createGeneralBlock from '../lib/modelApi/creators/createGeneralBlock';
import DarkColorHandlerImpl from 'roosterjs-editor-core/lib/editor/DarkColorHandlerImpl';
import { contentModelToDom } from '../lib/modelToDom/contentModelToDom';
import { createDomToModelContext, createModelToDomContext } from '../lib';
import { domToContentModel } from '../lib/domToModel/domToContentModel';
Expand All @@ -8,19 +7,9 @@ import {
ContentModelBlockFormat,
ContentModelDocument,
ContentModelGeneralBlock,
EditorContext,
} from 'roosterjs-content-model-types';

describe('End to end test for DOM => Model', () => {
let context: EditorContext;

beforeEach(() => {
context = {
isDarkMode: false,
darkColorHandler: new DarkColorHandlerImpl({} as any, s => 'darkMock: ' + s),
};
});

function runTest(
html: string,
expectedModel: ContentModelDocument,
Expand All @@ -30,13 +19,13 @@ describe('End to end test for DOM => Model', () => {
const div1 = document.createElement('div');
div1.innerHTML = html;

const model = domToContentModel(div1, createDomToModelContext(context));
const model = domToContentModel(div1, createDomToModelContext());

expect(model).toEqual(expectedModel);

const div2 = document.createElement('div');

contentModelToDom(document, div2, model, createModelToDomContext(context));
contentModelToDom(document, div2, model, createModelToDomContext());
const possibleHTML = [
expectedHtml, //chrome or firefox
expectedHTMLFirefox, //firefox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ describe('createModelToDomContext', () => {
const mockedBoldApplier = 'bold' as any;
const mockedBlockApplier = 'block' as any;
const mockedBrHandler = 'br' as any;

const context = createModelToDomContext(undefined, {
modelHandlerOverride: {
br: mockedBrHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ContentModelSegmentFormat } from 'roosterjs-content-model-types';
import { ContentModelParagraph, ContentModelSegmentFormat } from 'roosterjs-content-model-types';
import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';
import { IContentModelEditor } from '../../publicTypes/IContentModelEditor';
import { parseValueWithUnit } from 'roosterjs-content-model-dom';
import { setFontSizeInternal } from './setFontSize';

/**
* Default font size sequence, in pt. Suggest editor UI use this sequence as your font size list,
Expand All @@ -24,23 +25,24 @@ export default function changeFontSize(
formatSegmentWithContentModel(
editor,
'changeFontSize',
format => changeFontSizeInternal(format, change),
(format, _, __, paragraph) => changeFontSizeInternal(change, format, paragraph),
undefined /* segmentHasStyleCallback*/,
true /*includingFormatHandler*/
);
}

function changeFontSizeInternal(
change: 'increase' | 'decrease',
format: ContentModelSegmentFormat,
change: 'increase' | 'decrease'
paragraph: ContentModelParagraph | null
) {
if (format.fontSize) {
let sizeInPt = parseValueWithUnit(format.fontSize, undefined /*element*/, 'pt');

if (sizeInPt > 0) {
const newSize = getNewFontSize(sizeInPt, change == 'increase' ? 1 : -1, FONT_SIZES);

format.fontSize = newSize + 'pt';
setFontSizeInternal(newSize + 'pt', format, paragraph);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ContentModelParagraph, ContentModelSegmentFormat } from 'roosterjs-content-model-types';
import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';
import { IContentModelEditor } from '../../publicTypes/IContentModelEditor';

Expand All @@ -10,10 +11,33 @@ export default function setFontSize(editor: IContentModelEditor, fontSize: strin
formatSegmentWithContentModel(
editor,
'setFontSize',
format => {
format.fontSize = fontSize;
},
(format, _, __, paragraph) => setFontSizeInternal(fontSize, format, paragraph),
undefined /* segmentHasStyleCallback*/,
true /*includingFormatHandler*/
);
}

/**
* @internal
* Internal set font function shared by setFontSize and changeFontSize
*/
export function setFontSizeInternal(
fontSize: string,
format: ContentModelSegmentFormat,
paragraph: ContentModelParagraph | null
) {
format.fontSize = fontSize;

// Since we have set font size to segment, it can be smaller than the one in paragraph format, so delete font size from paragraph
if (paragraph?.segmentFormat?.fontSize) {
const size = paragraph.segmentFormat.fontSize;

paragraph.segments.forEach(segment => {
if (!segment.format.fontSize) {
segment.format.fontSize = size;
}
});

delete paragraph.segmentFormat.fontSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,47 @@ describe('changeFontSize', () => {
undefined
);
});

it('Paragraph has font size', () => {
runTest(
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {},
segmentFormat: { fontSize: '20pt' },
segments: [
{
segmentType: 'Text',
text: 'test',
format: { fontSize: '10pt' },
isSelected: true,
},
],
},
],
},
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {},
segmentFormat: {},
segments: [
{
segmentType: 'Text',
text: 'test',
format: { fontSize: '11pt' },
isSelected: true,
},
],
},
],
},
1,
'increase'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,56 @@ describe('setFontSize', () => {
1
);
});

it('With font size in paragraph', () => {
runTest(
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {},
segmentFormat: { fontSize: '8pt', fontFamily: 'Arial' },
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
format: {},
},
],
},
],
},
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {},
segmentFormat: { fontFamily: 'Arial' },
segments: [
{
segmentType: 'Text',
text: 'test',
format: { fontSize: '10pt' },
isSelected: true,
},
{
segmentType: 'Text',
text: 'test',
format: { fontSize: '8pt' },
},
],
},
],
},
1
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,35 @@ export function updateRotateHandleState(
} else {
rotateCenter.style.display = '';
rotateHandle.style.display = '';
const rotateHandleRect = rotateHandle.getBoundingClientRect();
const rotateCenterRect = rotateCenter.getBoundingClientRect();
const wrapperRect = wrapper.getBoundingClientRect();

if (rotateHandleRect && wrapperRect) {
const ROTATOR_HEIGHT = ROTATE_SIZE + ROTATE_GAP + RESIZE_HANDLE_MARGIN;
if (rotateCenterRect && wrapperRect) {
let adjustedDistance = Number.MAX_SAFE_INTEGER;
const angle = angleRad * DEG_PER_RAD;

if (angle < 45 && angle > -45 && wrapperRect.top - editorRect.top < ROTATE_GAP) {
const top = rotateHandleRect.top - editorRect.top;
if (angle < 45 && angle > -45 && wrapperRect.top - editorRect.top < ROTATOR_HEIGHT) {
const top = rotateCenterRect.top - editorRect.top;
adjustedDistance = top;
} else if (
angle <= -80 &&
angle >= -100 &&
wrapperRect.left - editorRect.left < ROTATE_GAP
wrapperRect.left - editorRect.left < ROTATOR_HEIGHT
) {
const left = rotateHandleRect.left - editorRect.left;
const left = rotateCenterRect.left - editorRect.left;
adjustedDistance = left;
} else if (
angle >= 80 &&
angle <= 100 &&
editorRect.right - wrapperRect.right < ROTATE_GAP
editorRect.right - wrapperRect.right < ROTATOR_HEIGHT
) {
const right = rotateHandleRect.right - editorRect.right;
const right = rotateCenterRect.right - editorRect.right;
adjustedDistance = Math.min(editorRect.right - wrapperRect.right, right);
} else if (
(angle <= -160 || angle >= 160) &&
editorRect.bottom - wrapperRect.bottom < ROTATE_GAP
editorRect.bottom - wrapperRect.bottom < ROTATOR_HEIGHT
) {
const bottom = rotateHandleRect.bottom - editorRect.bottom;
const bottom = rotateCenterRect.bottom - editorRect.bottom;
adjustedDistance = Math.min(editorRect.bottom - wrapperRect.bottom, bottom);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ describe('updateRotateHandlePosition', () => {
y: 3,
toJSON: () => {},
},
'-7px',
'1px',
'-6px',
'0px',
'0px',
{
top: 2,
Expand Down

0 comments on commit 4bc2505

Please sign in to comment.