Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity: Add class list rather than replace directly #2752

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { findAllEntities } from './findAllEntities';
import {
createEntity,
generateEntityClassNames,
generateEntityClassList,
getAllEntityWrappers,
getObjectKeys,
isEntityElement,
Expand Down Expand Up @@ -132,7 +132,7 @@ class EntityPlugin implements PluginWithState<EntityPluginState> {
if (entityType && !isFakeEntity) {
if (operation == 'newEntity') {
entity.entityFormat.id = this.ensureUniqueId(entityType, id ?? '', wrapper);
wrapper.className = generateEntityClassNames(entity.entityFormat);
wrapper.classList.add(...generateEntityClassList(entity.entityFormat));

if (entity.entityFormat.isReadonly) {
wrapper.contentEditable = 'false';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,11 @@ describe('EntityPlugin', () => {
const entity = createEntity(wrapper, true, undefined, entityType, id);
const doc = createContentModelDocument();

wrapper.className = entityUtils.generateEntityClassNames({
wrapper.classList.add(...entityUtils.generateEntityClassList({
entityType,
id: id,
isReadonly: true,
});
}));
doc.blocks.push(entity);
mockedModel = doc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,11 @@ function parseEntityClassName(
* @param format The source entity format object
* @returns A combined CSS class name string for entity wrapper
*/
export function generateEntityClassNames(format: ContentModelEntityFormat): string {
export function generateEntityClassList(format: ContentModelEntityFormat): string[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the function name may be a breaking change as it is already exported in the index file, suggest create a new function.

return format.isFakeEntity
? ''
: `${ENTITY_INFO_NAME} ${ENTITY_TYPE_PREFIX}${format.entityType ?? ''} ${
format.id ? `${ENTITY_ID_PREFIX}${format.id} ` : ''
}${ENTITY_READONLY_PREFIX}${format.isReadonly ? '1' : '0'}`;
? []
: [`${ENTITY_INFO_NAME}`, `${ENTITY_TYPE_PREFIX}${format.entityType ?? ''}`, `${format.id ? `${ENTITY_ID_PREFIX}${format.id} ` : ''
}${ENTITY_READONLY_PREFIX}${format.isReadonly ? '1' : '0'}`];
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateEntityClassNames, parseEntityFormat } from '../../domUtils/entityUtils';
import { generateEntityClassList, parseEntityFormat } from '../../domUtils/entityUtils';
import type { EntityInfoFormat, IdFormat } from 'roosterjs-content-model-types';
import type { FormatHandler } from '../FormatHandler';

Expand All @@ -12,7 +12,7 @@ export const entityFormatHandler: FormatHandler<EntityInfoFormat & IdFormat> = {

apply: (format, element) => {
if (!format.isFakeEntity) {
element.className = generateEntityClassNames(format);
element.classList.add(...generateEntityClassList(format));
}

if (format.isReadonly) {
Expand Down
2 changes: 1 addition & 1 deletion packages/roosterjs-content-model-dom/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export {
findClosestEntityWrapper,
getAllEntityWrappers,
parseEntityFormat,
generateEntityClassNames,
generateEntityClassList,
addDelimiters,
isEntityDelimiter,
isBlockEntityContainer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
addDelimiters,
findClosestBlockEntityContainer,
findClosestEntityWrapper,
generateEntityClassNames,
generateEntityClassList,
getAllEntityWrappers,
isBlockEntityContainer,
isEntityDelimiter,
Expand Down Expand Up @@ -106,23 +106,23 @@ describe('parseEntityFormat', () => {
});
});

describe('generateEntityClassNames', () => {
describe('generateEntityClassList', () => {
it('Empty format', () => {
const format: ContentModelEntityFormat = {};

const className = generateEntityClassNames(format);
const className = generateEntityClassList(format);

expect(className).toBe('_Entity _EType_ _EReadonly_0');
expect(className).toBe(['_Entity _EType_ _EReadonly_0']);
});

it('Format with type', () => {
const format: ContentModelEntityFormat = {
entityType: 'A',
};

const className = generateEntityClassNames(format);
const className = generateEntityClassList(format);

expect(className).toBe('_Entity _EType_A _EReadonly_0');
expect(className).toBe(['_Entity _EType_A _EReadonly_0']);
});

it('Format with type and id and readonly', () => {
Expand All @@ -132,9 +132,9 @@ describe('generateEntityClassNames', () => {
isReadonly: true,
};

const className = generateEntityClassNames(format);
const className = generateEntityClassList(format);

expect(className).toBe('_Entity _EType_A _EId_B _EReadonly_1');
expect(className).toBe(['_Entity _EType_A _EId_B _EReadonly_1']);
});

it('Fake entity format with type and id and readonly', () => {
Expand All @@ -145,9 +145,9 @@ describe('generateEntityClassNames', () => {
isFakeEntity: true,
};

const className = generateEntityClassNames(format);
const className = generateEntityClassList(format);

expect(className).toBe('');
expect(className).toBe([]);
});
});

Expand Down
Loading