/
commitEntity.ts
31 lines (28 loc) · 996 Bytes
/
commitEntity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { EntityClasses } from 'roosterjs-editor-types';
const CONTENT_EDITABLE = 'contenteditable';
/**
* Commit information of an entity (type, isReadonly, id) into the wrapper node as CSS Classes
* @param wrapper The entity wrapper element
* @param type Entity type
* @param isReadonly Whether this is a readonly entity
* @param id Optional Id of the entity
*/
export default function commitEntity(
wrapper: HTMLElement,
type: string,
isReadonly: boolean,
id?: string
) {
if (wrapper) {
wrapper.className = `${EntityClasses.ENTITY_INFO_NAME} ${
EntityClasses.ENTITY_TYPE_PREFIX
}${type} ${id ? `${EntityClasses.ENTITY_ID_PREFIX}${id} ` : ''}${
EntityClasses.ENTITY_READONLY_PREFIX
}${isReadonly ? '1' : '0'}`;
if (isReadonly) {
wrapper.contentEditable = 'false';
} else if (wrapper.getAttribute(CONTENT_EDITABLE)) {
wrapper.removeAttribute(CONTENT_EDITABLE);
}
}
}