Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 1x 1x 1x 1079x 1079x 1079x 1079x 1079x 1x 1024x 1024x 989x 989x 499x 490x 485x 1024x 1x 12x | import { validate } from './validate'; import type { Definition, ReadonlyContentModelWithDataset, ShallowMutableContentModelWithDataset, } from 'roosterjs-content-model-types'; const EditingInfoDatasetName = 'editingInfo'; /** * Retrieve metadata from the given model. * @param model The Content Model to retrieve metadata from * @param definition Definition of this metadata type, used for validate the metadata object * @returns Metadata of the model, or null if it does not contain a valid metadata */ export function getMetadata<T>( model: ReadonlyContentModelWithDataset<T>, definition?: Definition<T> ): T | null { const metadataString = model.dataset[EditingInfoDatasetName]; let obj: Object | null = null; try { obj = JSON.parse(metadataString); } catch {} return !definition || validate(obj, definition) ? (obj as T) : null; } /** * Update metadata of the given model * @param model The model to update metadata to * @param callback A callback function to update metadata * @param definition @optional Metadata definition used for verify the metadata object * @returns The metadata object if any, or null */ export function updateMetadata<T>( model: ShallowMutableContentModelWithDataset<T>, callback?: (metadata: T | null) => T | null, definition?: Definition<T> ): T | null { let obj = getMetadata(model, definition); if (callback) { obj = callback(obj); if (!obj) { delete model.dataset[EditingInfoDatasetName]; } else if (!definition || validate(obj, definition)) { model.dataset[EditingInfoDatasetName] = JSON.stringify(obj); } } return obj; } /** * Check if the given model has metadata * @param model The content model to check */ export function hasMetadata<T>(model: ReadonlyContentModelWithDataset<T> | HTMLElement): boolean { return !!model.dataset[EditingInfoDatasetName]; } |