Skip to content

Commit

Permalink
Merge branch 'u/jisong/keyboardenter' into u/jisong/reconcilechildlist
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Jun 5, 2024
2 parents becf627 + 1f90c24 commit 5491860
Show file tree
Hide file tree
Showing 18 changed files with 2,542 additions and 47 deletions.
9 changes: 9 additions & 0 deletions packages/roosterjs-content-model-core/lib/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
CachedElementHandler,
DomToModelOptionForCreateModel,
AnnounceData,
ExperimentalFeature,
} from 'roosterjs-content-model-types';

/**
Expand Down Expand Up @@ -406,6 +407,14 @@ export class Editor implements IEditor {
core.api.announce(core, announceData);
}

/**
* Check if a given feature is enabled
* @param featureName The name of feature to check
*/
isExperimentalFeatureEnabled(featureName: ExperimentalFeature | string): boolean {
return this.getCore().experimentalFeatures.indexOf(featureName) >= 0;
}

/**
* @returns the current EditorCore object
* @throws a standard Error if there's no core object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('paste with content model & paste plugin', () => {
paste(editor!, clipboardData);

expect(setProcessorF.setProcessor).toHaveBeenCalledTimes(1);
expect(addParserF.addParser).toHaveBeenCalledTimes(DEFAULT_TIMES_ADD_PARSER_CALLED + 4);
expect(addParserF.addParser).toHaveBeenCalledTimes(DEFAULT_TIMES_ADD_PARSER_CALLED + 5);
expect(WordDesktopFile.processPastedContentFromWordDesktop).toHaveBeenCalledTimes(1);
});

Expand Down
32 changes: 32 additions & 0 deletions packages/roosterjs-content-model-core/test/editor/EditorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,4 +1116,36 @@ describe('Editor', () => {
expect(resetSpy).toHaveBeenCalledWith();
expect(() => editor.announce(mockedData)).toThrow();
});

it('isExperimentalFeatureEnabled', () => {
const div = document.createElement('div');
const resetSpy = jasmine.createSpy('reset');
const mockedCore = {
plugins: [],
darkColorHandler: {
updateKnownColor: updateKnownColorSpy,
reset: resetSpy,
},
api: {
setContentModel: setContentModelSpy,
},
experimentalFeatures: ['Feature1', 'Feature2'],
} as any;

createEditorCoreSpy.and.returnValue(mockedCore);

const editor = new Editor(div);

const result1 = editor.isExperimentalFeatureEnabled('Feature1');
const result2 = editor.isExperimentalFeatureEnabled('Feature2');
const result3 = editor.isExperimentalFeatureEnabled('Feature3');

expect(result1).toBeTrue();
expect(result2).toBeTrue();
expect(result3).toBeFalse();

editor.dispose();
expect(resetSpy).toHaveBeenCalledWith();
expect(() => editor.isExperimentalFeatureEnabled('Feature4')).toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isNodeOfType } from '../domUtils/isNodeOfType';
import { toArray } from '../domUtils/toArray';
import type { ContentModelDocumentWithPersistedCache } from '../modelApi/selection/iterateSelections';
import type {
ContentModelDocument,
DOMSelection,
Expand Down Expand Up @@ -31,6 +32,10 @@ export function contentModelToDom(
range.isReverted = true;
}

if (context.domIndexer && context.allowCacheElement) {
(model as ContentModelDocumentWithPersistedCache).persistCache = true;
}

root.normalize();

return range;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,304 @@ describe('Normalize paragraph with segmentFormat', () => {
});
});
});

describe('Move up format', () => {
const mockedCache = {} as any;

it('No format', () => {
const para = createParagraph();
const text1 = createText('test1');
const text2 = createText('test2');

para.segments.push(text1, text2);
para.cachedElement = mockedCache;

normalizeParagraph(para);

expect(para).toEqual({
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test1',
format: {},
},
{
segmentType: 'Text',
text: 'test2',
format: {},
},
],
format: {},
cachedElement: mockedCache,
});
});

it('All segments have the same format', () => {
const para = createParagraph();
const text1 = createText('test1', {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
});
const text2 = createText('test2', {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: true,
});

para.segments.push(text1, text2);
para.cachedElement = mockedCache;

normalizeParagraph(para);

expect(para).toEqual({
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test1',
format: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
},
},
{
segmentType: 'Text',
text: 'test2',
format: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: true,
},
},
],
format: {},
segmentFormat: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
},
});
});

it('All segments have the same format, paragraph has different format', () => {
const para = createParagraph(false, undefined, {
fontFamily: 'Arial',
fontSize: '12pt',
textColor: 'green',
});
const text1 = createText('test1', {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
});
const text2 = createText('test2', {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: true,
});

para.segments.push(text1, text2);
para.cachedElement = mockedCache;

normalizeParagraph(para);

expect(para).toEqual({
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test1',
format: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
},
},
{
segmentType: 'Text',
text: 'test2',
format: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: true,
},
},
],
format: {},
segmentFormat: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
},
});
});

it('Some format are different', () => {
const para = createParagraph();
const text1 = createText('test1', {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
});
const text2 = createText('test2', {
fontFamily: 'Calibri',
fontSize: '12pt',
textColor: 'red',
italic: true,
});

para.segments.push(text1, text2);
para.cachedElement = mockedCache;

normalizeParagraph(para);

expect(para).toEqual({
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test1',
format: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
},
},
{
segmentType: 'Text',
text: 'test2',
format: {
fontFamily: 'Calibri',
fontSize: '12pt',
textColor: 'red',
italic: true,
},
},
],
format: {},
segmentFormat: { fontFamily: 'Calibri', textColor: 'red' },
});
});

it('All formats are different', () => {
const para = createParagraph();
const text1 = createText('test1', {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
});
const text2 = createText('test2', {
fontFamily: 'Arial',
fontSize: '12pt',
textColor: 'green',
italic: true,
});

para.segments.push(text1, text2);
para.cachedElement = mockedCache;

normalizeParagraph(para);

expect(para).toEqual({
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test1',
format: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
},
},
{
segmentType: 'Text',
text: 'test2',
format: {
fontFamily: 'Arial',
fontSize: '12pt',
textColor: 'green',
italic: true,
},
},
],
format: {},
cachedElement: mockedCache,
});
});

it('Already has same format in paragraph', () => {
const para = createParagraph(false, undefined, {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
});
const text1 = createText('test1', {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
});
const text2 = createText('test2', {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: true,
});

para.segments.push(text1, text2);
para.cachedElement = mockedCache;

normalizeParagraph(para);

expect(para).toEqual({
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test1',
format: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
},
},
{
segmentType: 'Text',
text: 'test2',
format: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: true,
},
},
],
format: {},
segmentFormat: {
fontFamily: 'Calibri',
fontSize: '10pt',
textColor: 'red',
italic: false,
},
cachedElement: mockedCache,
});
});
});
Loading

0 comments on commit 5491860

Please sign in to comment.