Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Nov 20, 2024
1 parent b41ba36 commit 573d6b2
Show file tree
Hide file tree
Showing 32 changed files with 142 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ export const setContentModel: SetContentModel = (

if (isInitializing) {
// When initialize, we should not trigger event until all plugins are initialized, so put these node in lifecycle state temporarily
core.lifecycle.domModification = modelToDomContext.domModification;
core.lifecycle.rewriteFromModel = modelToDomContext.rewriteFromModel;
} else {
// Otherwise, trigger DomModification event immediately
// Otherwise, trigger RewriteFromModel event immediately
core.api.triggerEvent(
core,
{
eventType: 'domModification',
...modelToDomContext.domModification,
eventType: 'rewriteFromModel',
...modelToDomContext.rewriteFromModel,
},
true /*broadcast*/
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
PluginEvent,
PluginWithState,
EditorOptions,
DomModification,
RewriteFromModel,
} from 'roosterjs-content-model-types';

const ContentEditableAttributeName = 'contenteditable';
Expand Down Expand Up @@ -75,13 +75,13 @@ class LifecyclePlugin implements PluginWithState<LifecyclePluginState> {
this.adjustColor();

// Let other plugins know that we are ready
const domModification: DomModification = this.state.domModification ?? {
const rewriteFromModel: RewriteFromModel = this.state.rewriteFromModel ?? {
addedBlockElements: [],
removedBlockElements: [],
};

this.editor.triggerEvent('editorReady', domModification, true /*broadcast*/);
delete this.state.domModification;
this.editor.triggerEvent('editorReady', rewriteFromModel, true /*broadcast*/);
delete this.state.rewriteFromModel;

// Initialize the Announce container.
this.state.announceContainer = createAriaLiveElement(editor.getDocument());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { setContentModel } from '../../../lib/coreApi/setContentModel/setContent
const mockedDoc = 'DOCUMENT' as any;
const mockedModel = 'MODEL' as any;
const mockedEditorContext = 'EDITORCONTEXT' as any;
const mockedContext = { name: 'CONTEXT', domModification: {} } as any;
const mockedContext = { name: 'CONTEXT', rewriteFromModel: {} } as any;
const mockedDiv = { ownerDocument: mockedDoc } as any;
const mockedConfig = 'CONFIG' as any;

Expand Down Expand Up @@ -286,8 +286,8 @@ describe('setContentModel', () => {
model: ContentModelDocument,
context: ModelToDomContext
) => {
context.domModification.addedBlockElements = mockedAddedNodes;
context.domModification.removedBlockElements = mockedRemovedNodes;
context.rewriteFromModel.addedBlockElements = mockedAddedNodes;
context.rewriteFromModel.removedBlockElements = mockedRemovedNodes;
return {} as any;
}
);
Expand All @@ -298,13 +298,13 @@ describe('setContentModel', () => {
expect(triggerEventSpy).toHaveBeenCalledWith(
core,
{
eventType: 'domModification',
eventType: 'rewriteFromModel',
addedBlockElements: mockedAddedNodes,
removedBlockElements: mockedRemovedNodes,
},
true
);
expect(core.lifecycle.domModification).toBeUndefined();
expect(core.lifecycle.rewriteFromModel).toBeUndefined();
});

it('Receive modified DOM elements, in init', () => {
Expand All @@ -318,16 +318,16 @@ describe('setContentModel', () => {
model: ContentModelDocument,
context: ModelToDomContext
) => {
context.domModification.addedBlockElements = mockedAddedNodes;
context.domModification.removedBlockElements = mockedRemovedNodes;
context.rewriteFromModel.addedBlockElements = mockedAddedNodes;
context.rewriteFromModel.removedBlockElements = mockedRemovedNodes;
return {} as any;
}
);

setContentModel(core, mockedModel, undefined, undefined, true);

expect(triggerEventSpy).not.toHaveBeenCalled();
expect(core.lifecycle.domModification).toEqual({
expect(core.lifecycle.rewriteFromModel).toEqual({
addedBlockElements: mockedAddedNodes,
removedBlockElements: mockedRemovedNodes,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('LifecyclePlugin', () => {
expect(div.isContentEditable).toBeFalse();
});

it('init with domModifications', () => {
it('init with rewriteFromModel', () => {
const div = document.createElement('div');
const plugin = createLifecyclePlugin({}, div);
const triggerEvent = jasmine.createSpy('triggerEvent');
Expand All @@ -108,7 +108,7 @@ describe('LifecyclePlugin', () => {
const mockedAddedElements = 'ADD' as any;
const mockedRemovedElements = 'REMOVE' as any;

state.domModification = {
state.rewriteFromModel = {
addedBlockElements: mockedAddedElements,
removedBlockElements: mockedRemovedElements,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isEntityElement } from './entityUtils';
import { isNodeOfType } from './isNodeOfType';
import type { DomModification } from 'roosterjs-content-model-types';
import type { RewriteFromModel } from 'roosterjs-content-model-types';

/**
* When set a DOM tree into editor, reuse the existing element in editor and no need to change it
Expand All @@ -15,7 +15,7 @@ export function reuseCachedElement(
parent: Node,
element: Node,
refNode: Node | null,
context?: DomModification
context?: RewriteFromModel
): Node | null {
if (element.parentNode == parent) {
const isEntity = isEntityElement(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
defaultFormatKeysPerCategory,
} from '../../formatHandlers/defaultFormatHandlers';
import type {
DomModificationContext,
RewriteFromModelContext,
EditorContext,
FormatApplier,
FormatAppliers,
Expand Down Expand Up @@ -44,7 +44,7 @@ export function createModelToDomContextWithConfig(
editorContext,
createModelToDomSelectionContext(),
createModelToDomFormatContext(),
createDomModificationContext(),
createRewriteFromModelContext(),
config
);
}
Expand All @@ -70,9 +70,9 @@ function createModelToDomFormatContext(): ModelToDomFormatContext {
};
}

function createDomModificationContext(): DomModificationContext {
function createRewriteFromModelContext(): RewriteFromModelContext {
return {
domModification: {
rewriteFromModel: {
addedBlockElements: [],
removedBlockElements: [],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const handleBlockGroupChildren: ContentModelHandler<ContentModelBlockGrou
const next = refNode.nextSibling;

if (isNodeOfType(refNode, 'ELEMENT_NODE')) {
context.domModification.removedBlockElements.push(refNode);
context.rewriteFromModel.removedBlockElements.push(refNode);
}

refNode.parentNode?.removeChild(refNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const handleDivider: ContentModelBlockHandler<ContentModelDivider> = (
let element = context.allowCacheElement ? divider.cachedElement : undefined;

if (element && !divider.isSelected) {
refNode = reuseCachedElement(parent, element, refNode, context.domModification);
refNode = reuseCachedElement(parent, element, refNode, context.rewriteFromModel);
} else {
element = doc.createElement(divider.tagName);

Expand All @@ -28,7 +28,7 @@ export const handleDivider: ContentModelBlockHandler<ContentModelDivider> = (
}

parent.insertBefore(element, refNode);
context.domModification.addedBlockElements.push(element);
context.rewriteFromModel.addedBlockElements.push(element);

applyFormat(element, context.formatAppliers.divider, divider.format, context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const handleEntityBlock: ContentModelBlockHandler<ContentModelEntity> = (
const isContained = wrapper.parentElement?.classList.contains(BlockEntityContainer);
const elementToReuse = isContained && isCursorAroundEntity ? wrapper.parentElement! : wrapper;

refNode = reuseCachedElement(parent, elementToReuse, refNode, context.domModification);
refNode = reuseCachedElement(parent, elementToReuse, refNode, context.rewriteFromModel);

if (isCursorAroundEntity) {
if (!isContained) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const handleFormatContainer: ContentModelBlockHandler<ContentModelFormatC
let element = context.allowCacheElement ? container.cachedElement : undefined;

if (element) {
refNode = reuseCachedElement(parent, element, refNode, context.domModification);
refNode = reuseCachedElement(parent, element, refNode, context.rewriteFromModel);

context.modelHandlers.blockGroupChildren(doc, element, container, context);
} else if (!isBlockGroupEmpty(container)) {
Expand All @@ -39,7 +39,7 @@ export const handleFormatContainer: ContentModelBlockHandler<ContentModelFormatC
}

parent.insertBefore(containerNode, refNode);
context.domModification.addedBlockElements.push(containerNode);
context.rewriteFromModel.addedBlockElements.push(containerNode);

stackFormat(context, container.tagName, () => {
applyFormat(containerNode, context.formatAppliers.container, container.format, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export const handleGeneralBlock: ContentModelBlockHandler<ContentModelGeneralBlo
let node: HTMLElement = group.element;

if (refNode && node.parentNode == parent) {
refNode = reuseCachedElement(parent, node, refNode, context.domModification);
refNode = reuseCachedElement(parent, node, refNode, context.rewriteFromModel);
} else {
node = node.cloneNode() as HTMLElement;
group.element = node as HTMLElement;

applyFormat(node, context.formatAppliers.general, group.format, context);

parent.insertBefore(node, refNode);
context.domModification.addedBlockElements.push(node);
context.rewriteFromModel.addedBlockElements.push(node);
}

context.onNodeCreated?.(group, node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const handleListItem: ContentModelBlockHandler<ContentModelListItem> = (
// It is possible listParent is the same with parent param.
// This happens when outdent a list item to cause it has no list level
listParent.insertBefore(li, refNode?.parentNode == listParent ? refNode : null);
context.domModification.addedBlockElements.push(li);
context.rewriteFromModel.addedBlockElements.push(li);

if (level) {
applyFormat(li, context.formatAppliers.segment, listItem.formatHolder.format, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const handleParagraph: ContentModelBlockHandler<ContentModelParagraph> =
let container = context.allowCacheElement ? paragraph.cachedElement : undefined;

if (container && paragraph.segments.every(x => x.segmentType != 'General' && !x.isSelected)) {
refNode = reuseCachedElement(parent, container, refNode, context.domModification);
refNode = reuseCachedElement(parent, container, refNode, context.rewriteFromModel);
} else {
stackFormat(context, paragraph.decorator?.tagName || null, () => {
const needParagraphWrapper =
Expand Down Expand Up @@ -118,7 +118,7 @@ export const handleParagraph: ContentModelBlockHandler<ContentModelParagraph> =
paragraph.cachedElement = container;
}

context.domModification.addedBlockElements.push(container);
context.rewriteFromModel.addedBlockElements.push(container);
} else {
unwrap(container);
container = undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const handleTable: ContentModelBlockHandler<ContentModelTable> = (
let tableNode = context.allowCacheElement ? table.cachedElement : undefined;

if (tableNode) {
refNode = reuseCachedElement(parent, tableNode, refNode, context.domModification);
refNode = reuseCachedElement(parent, tableNode, refNode, context.rewriteFromModel);

moveChildNodes(tableNode);
} else {
Expand All @@ -39,7 +39,7 @@ export const handleTable: ContentModelBlockHandler<ContentModelTable> = (
}

parent.insertBefore(tableNode, refNode);
context.domModification.addedBlockElements.push(tableNode);
context.rewriteFromModel.addedBlockElements.push(tableNode);

applyFormat(tableNode, context.formatAppliers.block, table.format, context);
applyFormat(tableNode, context.formatAppliers.table, table.format, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { reuseCachedElement } from '../../lib/domUtils/reuseCachedElement';
import { setEntityElementClasses } from './entityUtilTest';
import type { DomModification } from 'roosterjs-content-model-types';
import type { RewriteFromModel } from 'roosterjs-content-model-types';

describe('reuseCachedElement', () => {
it('No refNode', () => {
const parent = document.createElement('div');
const element = document.createElement('span');
const context: DomModification = {
const context: RewriteFromModel = {
addedBlockElements: [],
removedBlockElements: [],
};
Expand All @@ -29,7 +29,7 @@ describe('reuseCachedElement', () => {

parent.appendChild(refNode);

const context: DomModification = {
const context: RewriteFromModel = {
addedBlockElements: [],
removedBlockElements: [],
};
Expand All @@ -53,7 +53,7 @@ describe('reuseCachedElement', () => {
parent.appendChild(element);
parent.appendChild(nextNode);

const context: DomModification = {
const context: RewriteFromModel = {
addedBlockElements: [],
removedBlockElements: [],
};
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('reuseCachedElement', () => {
parent.appendChild(element);
parent.appendChild(nextNode);

const context: DomModification = {
const context: RewriteFromModel = {
addedBlockElements: [],
removedBlockElements: [],
};
Expand Down Expand Up @@ -111,7 +111,7 @@ describe('reuseCachedElement', () => {

setEntityElementClasses(refNode, 'TestEntity', true);

const context: DomModification = {
const context: RewriteFromModel = {
addedBlockElements: [],
removedBlockElements: [],
};
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('reuseCachedElement', () => {
setEntityElementClasses(refNode, 'TestEntity', true);
setEntityElementClasses(element, 'TestEntity2', true);

const context: DomModification = {
const context: RewriteFromModel = {
addedBlockElements: [],
removedBlockElements: [],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('createModelToDomContext', () => {
defaultModelHandlers: defaultContentModelHandlers,
defaultFormatAppliers,
metadataAppliers: {},
domModification: {
rewriteFromModel: {
addedBlockElements: [],
removedBlockElements: [],
},
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('createModelToDomContext', () => {
defaultModelHandlers: defaultContentModelHandlers,
defaultFormatAppliers,
metadataAppliers: {},
domModification: {
rewriteFromModel: {
addedBlockElements: [],
removedBlockElements: [],
},
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('createModelToDomContext', () => {
defaultModelHandlers: defaultContentModelHandlers,
defaultFormatAppliers,
metadataAppliers: {},
domModification: {
rewriteFromModel: {
addedBlockElements: [],
removedBlockElements: [],
},
Expand Down
Loading

0 comments on commit 573d6b2

Please sign in to comment.