Skip to content

Commit

Permalink
Content Model code improvement 1: remove "includeRoot" (#1922)
Browse files Browse the repository at this point in the history
* Content Model code improvement 1

* fix build

* fix build

* Remove unnecessary isDarkMode

* Reorganize index
  • Loading branch information
JiuqingSong authored Jun 29, 2023
1 parent 3fddfb4 commit 2ede8ef
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export function createDomToModelContext(
options?: DomToModelOption
): DomToModelContext {
const context: DomToModelContext = {
...(editorContext || {
isDarkMode: false,
}),
...editorContext,

blockFormat: {},
segmentFormat: {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createContentModelDocument } from '../modelApi/creators/createContentModelDocument';
import { createDomToModelContext } from './context/createDomToModelContext';
import { isNodeOfType } from '../domUtils/isNodeOfType';
import { NodeType } from 'roosterjs-editor-types';
import { normalizeContentModel } from '../modelApi/common/normalizeContentModel';
import { parseFormat } from './utils/parseFormat';
import { rootDirectionFormatHandler } from '../formatHandlers/root/rootDirectionFormatHandler';
import { safeInstanceOf } from 'roosterjs-editor-dom';
import { zoomScaleFormatHandler } from '../formatHandlers/root/zoomScaleFormatHandler';
import {
ContentModelDocument,
Expand All @@ -20,28 +21,22 @@ import {
*/
export function domToContentModel(
root: HTMLElement | DocumentFragment,
editorContext: EditorContext,
option: DomToModelOption
editorContext?: EditorContext,
option?: DomToModelOption
): ContentModelDocument {
const model = createContentModelDocument(editorContext.defaultFormat);
const model = createContentModelDocument(editorContext?.defaultFormat);
const context = createDomToModelContext(editorContext, option);

if (safeInstanceOf(root, 'DocumentFragment')) {
context.elementProcessors.child(model, root, context);
} else {
if (isNodeOfType(root, NodeType.Element)) {
// Need to calculate direction (ltr or rtl), use it as initial value
parseFormat(root, [rootDirectionFormatHandler.parse], context.blockFormat, context);

// Need to calculate zoom scale value from root element, use this value to calculate sizes for elements
parseFormat(root, [zoomScaleFormatHandler.parse], context.zoomScaleFormat, context);

const processor = option.includeRoot
? context.elementProcessors.element
: context.elementProcessors.child;

processor(model, root, context);
}

context.elementProcessors.child(model, root, context);

normalizeContentModel(model);

return model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const backgroundColorFormatHandler: FormatHandler<BackgroundColorFormat>
element,
true /*isBackground*/,
context.darkColorHandler,
context.isDarkMode
!!context.isDarkMode
) || defaultStyle.backgroundColor;

if (backgroundColor) {
Expand All @@ -26,7 +26,7 @@ export const backgroundColorFormatHandler: FormatHandler<BackgroundColorFormat>
format.backgroundColor,
true /*isBackground*/,
context.darkColorHandler,
context.isDarkMode
!!context.isDarkMode
);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const textColorFormatHandler: FormatHandler<TextColorFormat> = {
element,
false /*isBackground*/,
context.darkColorHandler,
context.isDarkMode
!!context.isDarkMode
) || defaultStyle.color;

if (textColor && textColor != 'inherit') {
Expand All @@ -28,7 +28,7 @@ export const textColorFormatHandler: FormatHandler<TextColorFormat> = {
format.textColor,
false /*isBackground*/,
context.darkColorHandler,
context.isDarkMode
!!context.isDarkMode
);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ export function createModelToDomContext(
options = options || {};

return {
...(editorContext || {
isDarkMode: false,
}),
...editorContext,

regularSelection: {
current: {
block: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
} from '../../../lib/formatHandlers/defaultFormatHandlers';

describe('createDomToModelContext', () => {
const editorContext: EditorContext = {
isDarkMode: false,
};
const editorContext: EditorContext = {};
const listFormat: DomToModelListFormat = {
threadItemCounts: [],
levels: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import {
} from '../../../lib/formatHandlers/defaultFormatHandlers';

describe('createModelToDomContext', () => {
const editorContext: EditorContext = {
isDarkMode: false,
};
const editorContext: EditorContext = {};
const defaultResult: ModelToDomContext = {
...editorContext,
regularSelection: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ describe('domToContentModel', () => {
spyOn(normalizeContentModel, 'normalizeContentModel');

const rootElement = document.createElement('div');
const options = {
includeRoot: false,
};
const options = {};
const editorContext: EditorContext = {
isDarkMode: false,
defaultFormat: {
Expand Down Expand Up @@ -55,44 +53,4 @@ describe('domToContentModel', () => {
expect(normalizeContentModel.normalizeContentModel).toHaveBeenCalledTimes(1);
expect(normalizeContentModel.normalizeContentModel).toHaveBeenCalledWith(result);
});

it('Include root', () => {
const elementProcessor = jasmine.createSpy('elementProcessor');
const childProcessor = jasmine.createSpy('childProcessor');
const mockContext = ({
elementProcessors: {
element: elementProcessor,
child: childProcessor,
},
defaultStyles: {},
zoomScaleFormat: {},
segmentFormat: {},
} as any) as DomToModelContext;

spyOn(createDomToModelContext, 'createDomToModelContext').and.returnValue(mockContext);
spyOn(normalizeContentModel, 'normalizeContentModel');

const rootElement = document.createElement('div');
const options = {
includeRoot: true,
};
const editorContext: EditorContext = { isDarkMode: false };
const model = domToContentModel(rootElement, editorContext, options);
const result: ContentModelDocument = {
blockGroupType: 'Document',
blocks: [],
};

expect(model).toEqual(result);
expect(createDomToModelContext.createDomToModelContext).toHaveBeenCalledTimes(1);
expect(createDomToModelContext.createDomToModelContext).toHaveBeenCalledWith(
editorContext,
options
);
expect(childProcessor).not.toHaveBeenCalled();
expect(elementProcessor).toHaveBeenCalledTimes(1);
expect(elementProcessor).toHaveBeenCalledWith(result, rootElement, mockContext);
expect(normalizeContentModel.normalizeContentModel).toHaveBeenCalledTimes(1);
expect(normalizeContentModel.normalizeContentModel).toHaveBeenCalledWith(result);
});
});
69 changes: 65 additions & 4 deletions packages-content-model/roosterjs-content-model-editor/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,68 @@
export * from './publicTypes/index';
export * from './publicApi/index';
export { ContentModelFormatState } from './publicTypes/format/formatState/ContentModelFormatState';
export { ImageFormatState } from './publicTypes/format/formatState/ImageFormatState';
export { Border } from './publicTypes/interface/Border';
export {
CreateEditorContext,
ContentModelCoreApiMap,
ContentModelEditorCore,
CreateContentModel,
SetContentModel,
} from './publicTypes/ContentModelEditorCore';
export {
default as ContentModelBeforePasteEvent,
ContentModelBeforePasteEventData,
CompatibleContentModelBeforePasteEvent,
} from './publicTypes/event/ContentModelBeforePasteEvent';
export { IContentModelEditor, ContentModelEditorOptions } from './publicTypes/IContentModelEditor';
export { InsertPoint } from './publicTypes/selection/InsertPoint';
export { TableSelectionContext } from './publicTypes/selection/TableSelectionContext';

export { default as insertTable } from './publicApi/table/insertTable';
export { default as formatTable } from './publicApi/table/formatTable';
export { default as setTableCellShade } from './publicApi/table/setTableCellShade';
export { default as editTable } from './publicApi/table/editTable';
export { default as toggleBullet } from './publicApi/list/toggleBullet';
export { default as toggleNumbering } from './publicApi/list/toggleNumbering';
export { default as toggleBold } from './publicApi/segment/toggleBold';
export { default as toggleItalic } from './publicApi/segment/toggleItalic';
export { default as toggleUnderline } from './publicApi/segment/toggleUnderline';
export { default as toggleStrikethrough } from './publicApi/segment/toggleStrikethrough';
export { default as toggleSubscript } from './publicApi/segment/toggleSubscript';
export { default as toggleSuperscript } from './publicApi/segment/toggleSuperscript';
export { default as setBackgroundColor } from './publicApi/segment/setBackgroundColor';
export { default as setFontName } from './publicApi/segment/setFontName';
export { default as setFontSize } from './publicApi/segment/setFontSize';
export { default as setTextColor } from './publicApi/segment/setTextColor';
export { default as changeFontSize } from './publicApi/segment/changeFontSize';
export { default as applySegmentFormat } from './publicApi/segment/applySegmentFormat';
export { default as changeCapitalization } from './publicApi/segment/changeCapitalization';
export { default as insertImage } from './publicApi/image/insertImage';
export { default as setListStyle } from './publicApi/list/setListStyle';
export { default as setListStartNumber } from './publicApi/list/setListStartNumber';
export { default as hasSelectionInBlock } from './publicApi/selection/hasSelectionInBlock';
export { default as hasSelectionInSegment } from './publicApi/selection/hasSelectionInSegment';
export { default as hasSelectionInBlockGroup } from './publicApi/selection/hasSelectionInBlockGroup';
export { default as setIndentation } from './publicApi/block/setIndentation';
export { default as setAlignment } from './publicApi/block/setAlignment';
export { default as setDirection } from './publicApi/block/setDirection';
export { default as setHeaderLevel } from './publicApi/block/setHeaderLevel';
export { default as toggleBlockQuote } from './publicApi/block/toggleBlockQuote';
export { default as setSpacing } from './publicApi/block/setSpacing';
export { default as setImageBorder } from './publicApi/image/setImageBorder';
export { default as setImageBoxShadow } from './publicApi/image/setImageBoxShadow';
export { default as changeImage } from './publicApi/image/changeImage';
export { default as getFormatState } from './publicApi/format/getFormatState';
export { default as getSegmentFormat } from './publicApi/format/getSegmentFormat';
export { default as applyPendingFormat } from './publicApi/format/applyPendingFormat';
export { default as clearFormat } from './publicApi/format/clearFormat';
export { default as insertLink } from './publicApi/link/insertLink';
export { default as removeLink } from './publicApi/link/removeLink';
export { default as adjustLinkSelection } from './publicApi/link/adjustLinkSelection';
export { default as setImageAltText } from './publicApi/image/setImageAltText';
export { default as adjustImageSelection } from './publicApi/image/adjustImageSelection';
export { default as setParagraphMargin } from './publicApi/block/setParagraphMargin';
export { default as toggleCode } from './publicApi/segment/toggleCode';
export { default as paste } from './publicApi/utils/paste';

export { default as ContentModelEditor } from './editor/ContentModelEditor';
export { default as isContentModelEditor } from './editor/isContentModelEditor';
Expand All @@ -16,5 +79,3 @@ export { combineBorderValue, extractBorderValues } from './domUtils/borderValues
export { updateImageMetadata } from './domUtils/metadata/updateImageMetadata';
export { updateTableCellMetadata } from './domUtils/metadata/updateTableCellMetadata';
export { updateTableMetadata } from './domUtils/metadata/updateTableMetadata';

export { IContentModelEditor, ContentModelEditorOptions } from './publicTypes/IContentModelEditor';

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ describe('link parser test', () => {
{
isDarkMode: false,
},
{
...event.domToModelOption,
includeRoot: true,
}
event.domToModelOption
);
if (expectedModel) {
expect(model).toEqual(expectedModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe('processPastedContentFromExcelTest', () => {
},
{
...event.domToModelOption,
includeRoot: true,
disableCacheElement: true,
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('processPastedContentFromWacTest', () => {
},
{
...event.domToModelOption,
includeRoot: true,
}
);
if (expectedModel) {
Expand Down Expand Up @@ -137,7 +136,6 @@ describe('wordOnlineHandler', () => {
},
{
...event.domToModelOption,
includeRoot: true,
disableCacheElement: true,
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ describe('processPastedContentFromWordDesktopTest', () => {
},
{
...event.domToModelOption,
includeRoot: true,
disableCacheElement: true,
}
);
Expand Down
Loading

0 comments on commit 2ede8ef

Please sign in to comment.