All files / roosterjs-content-model-dom/lib/modelApi/editing cloneModel.ts

97.56% Statements 80/82
97.56% Branches 40/41
100% Functions 27/27
97.56% Lines 80/82

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408                                                                                                              1x       121x   120x 30x     120x             603x   200x   23x   8x   169x       6x   5x   371x   21x               446x   40x   3x   2x   13x   47x   341x             2148x               460x               607x   607x                       840x   420x   603x             2476x   619x               619x 2x   619x 23x     619x       16x   8x                           1855x   371x       446x             371x 80x               371x             63x   21x       43x                     129x   43x       97x                   485x   97x                                   46x 23x           23x       23x             507x   169x                       306x   306x           18x   6x                           10x   10x                       216x       52x   13x                     2x       341x 341x                                       748x   748x 680x 68x 15x 53x 9x   44x   44x 1x     43x      
import type {
    ContentModelBlock,
    ContentModelBlockBase,
    ContentModelBlockGroupBase,
    ContentModelBlockGroupType,
    ContentModelBlockType,
    ContentModelDivider,
    ContentModelDocument,
    ContentModelEntity,
    ContentModelFormatBase,
    ContentModelFormatContainer,
    ContentModelGeneralBlock,
    ContentModelImage,
    ContentModelListItem,
    ContentModelParagraph,
    ContentModelSegment,
    ContentModelSegmentBase,
    ContentModelSegmentType,
    ContentModelSelectionMarker,
    ContentModelTable,
    ContentModelTableCell,
    ContentModelWithDataset,
    ContentModelWithFormat,
    ContentModelGeneralSegment,
    ContentModelText,
    ContentModelTableRow,
    ContentModelListLevel,
    CloneModelOptions,
    ReadonlyContentModelDocument,
    ReadonlyContentModelBlockGroupBase,
    ReadonlyContentModelBlock,
    ReadonlyContentModelFormatContainer,
    ReadonlyContentModelBlockBase,
    ReadonlyContentModelGeneralBlock,
    ReadonlyContentModelListItem,
    ReadonlyContentModelSelectionMarker,
    ReadonlyContentModelSegmentBase,
    ReadonlyContentModelWithDataset,
    ReadonlyContentModelDivider,
    ReadonlyContentModelListLevel,
    ReadonlyContentModelParagraph,
    ReadonlyContentModelSegment,
    ReadonlyContentModelTable,
    ReadonlyContentModelTableRow,
    ReadonlyContentModelTableCell,
    ReadonlyContentModelGeneralSegment,
    ReadonlyContentModelImage,
    ReadonlyContentModelText,
} from 'roosterjs-content-model-types';
 
/**
 * Clone a content model
 * @param model The content model to clone
 * @param options @optional Options to specify customize the clone behavior
 */
export function cloneModel(
    model: ReadonlyContentModelDocument,
    options?: CloneModelOptions
): ContentModelDocument {
    const newModel: ContentModelDocument = cloneBlockGroupBase(model, options || {});
 
    if (model.format) {
        newModel.format = Object.assign({}, model.format);
    }
 
    return newModel;
}
 
function cloneBlock(
    block: ReadonlyContentModelBlock,
    options: CloneModelOptions
): ContentModelBlock {
    switch (block.blockType) {
        case 'BlockGroup':
            switch (block.blockGroupType) {
                case 'FormatContainer':
                    return cloneFormatContainer(block, options);
                case 'General':
                    return cloneGeneralBlock(block, options);
                case 'ListItem':
                    return cloneListItem(block, options);
            }
            break;
        case 'Divider':
            return cloneDivider(block, options);
        case 'Entity':
            return cloneEntity(block, options);
        case 'Paragraph':
            return cloneParagraph(block, options);
        case 'Table':
            return cloneTable(block, options);
    }
}
 
function cloneSegment(
    segment: ReadonlyContentModelSegment,
    options: CloneModelOptions
): ContentModelSegment {
    switch (segment.segmentType) {
        case 'Br':
            return cloneSegmentBase(segment);
        case 'Entity':
            return cloneEntity(segment, options);
        case 'General':
            return cloneGeneralSegment(segment, options);
        case 'Image':
            return cloneImage(segment);
        case 'SelectionMarker':
            return cloneSelectionMarker(segment);
        case 'Text':
            return cloneText(segment);
    }
}
 
function cloneModelWithFormat<T extends ContentModelFormatBase>(
    model: ContentModelWithFormat<T>
): ContentModelWithFormat<T> {
    return {
        format: Object.assign({}, model.format),
    };
}
 
function cloneModelWithDataset<T>(
    model: ReadonlyContentModelWithDataset<T>
): ContentModelWithDataset<T> {
    return {
        dataset: Object.assign({}, model.dataset),
    };
}
 
function cloneBlockBase<T extends ContentModelBlockType>(
    block: ReadonlyContentModelBlockBase<T>
): ContentModelBlockBase<T> {
    const { blockType } = block;
 
    return Object.assign(
        {
            blockType,
        },
        cloneModelWithFormat(block)
    );
}
 
function cloneBlockGroupBase<T extends ContentModelBlockGroupType>(
    group: ReadonlyContentModelBlockGroupBase<T>,
    options: CloneModelOptions
): ContentModelBlockGroupBase<T> {
    const { blockGroupType, blocks } = group;
 
    return {
        blockGroupType: blockGroupType,
        blocks: blocks.map(block => cloneBlock(block, options)),
    };
}
 
function cloneSegmentBase<T extends ContentModelSegmentType>(
    segment: ReadonlyContentModelSegmentBase<T>
): ContentModelSegmentBase<T> {
    const { segmentType, isSelected, code, link } = segment;
 
    const newSegment: ContentModelSegmentBase<T> = Object.assign(
        {
            segmentType,
            isSelected,
        },
        cloneModelWithFormat(segment)
    );
 
    if (code) {
        newSegment.code = cloneModelWithFormat(code);
    }
    if (link) {
        newSegment.link = Object.assign(cloneModelWithFormat(link), cloneModelWithDataset(link));
    }
 
    return newSegment;
}
 
function cloneEntity(entity: ContentModelEntity, options: CloneModelOptions): ContentModelEntity {
    const { wrapper, entityFormat } = entity;
 
    return Object.assign(
        {
            wrapper: handleCachedElement(wrapper, 'entity', options),
            entityFormat: { ...entityFormat },
        },
        cloneBlockBase(entity),
        cloneSegmentBase(entity)
    );
}
 
function cloneParagraph(
    paragraph: ReadonlyContentModelParagraph,
    options: CloneModelOptions
): ContentModelParagraph {
    const { cachedElement, segments, isImplicit, decorator, segmentFormat } = paragraph;
 
    const newParagraph: ContentModelParagraph = Object.assign(
        {
            cachedElement: handleCachedElement(cachedElement, 'cache', options),
            isImplicit,
            segments: segments.map(segment => cloneSegment(segment, options)),
            segmentFormat: segmentFormat ? { ...segmentFormat } : undefined,
        },
        cloneBlockBase(paragraph),
        cloneModelWithFormat(paragraph)
    );
 
    if (decorator) {
        newParagraph.decorator = Object.assign(
            {
                tagName: decorator.tagName,
            },
            cloneModelWithFormat(decorator)
        );
    }
 
    return newParagraph;
}
 
function cloneTable(
    table: ReadonlyContentModelTable,
    options: CloneModelOptions
): ContentModelTable {
    const { cachedElement, widths, rows } = table;
 
    return Object.assign(
        {
            cachedElement: handleCachedElement(cachedElement, 'cache', options),
            widths: Array.from(widths),
            rows: rows.map(row => cloneTableRow(row, options)),
        },
        cloneBlockBase(table),
        cloneModelWithDataset(table)
    );
}
 
function cloneTableRow(
    row: ReadonlyContentModelTableRow,
    options: CloneModelOptions
): ContentModelTableRow {
    const { height, cells, cachedElement } = row;
 
    return Object.assign(
        {
            height,
            cachedElement: handleCachedElement(cachedElement, 'cache', options),
            cells: cells.map(cell => cloneTableCell(cell, options)),
        },
        cloneModelWithFormat(row)
    );
}
 
function cloneTableCell(
    cell: ReadonlyContentModelTableCell,
    options: CloneModelOptions
): ContentModelTableCell {
    const { cachedElement, isSelected, spanAbove, spanLeft, isHeader } = cell;
 
    return Object.assign(
        {
            cachedElement: handleCachedElement(cachedElement, 'cache', options),
            isSelected,
            spanAbove,
            spanLeft,
            isHeader,
        },
        cloneBlockGroupBase(cell, options),
        cloneModelWithFormat(cell),
        cloneModelWithDataset(cell)
    );
}
 
function cloneFormatContainer(
    container: ReadonlyContentModelFormatContainer,
    options: CloneModelOptions
): ContentModelFormatContainer {
    const { tagName, cachedElement } = container;
    const newContainer: ContentModelFormatContainer = Object.assign(
        { tagName, cachedElement: handleCachedElement(cachedElement, 'cache', options) },
        cloneBlockBase(container),
        cloneBlockGroupBase(container, options)
    );
 
    Iif (container.zeroFontSize) {
        newContainer.zeroFontSize = true;
    }
 
    return newContainer;
}
 
function cloneListItem(
    item: ReadonlyContentModelListItem,
    options: CloneModelOptions
): ContentModelListItem {
    const { formatHolder, levels, cachedElement } = item;
 
    return Object.assign(
        {
            formatHolder: cloneSelectionMarker(formatHolder),
            levels: levels.map(cloneListLevel),
            cachedElement: handleCachedElement(cachedElement, 'cache', options),
        },
        cloneBlockBase(item),
        cloneBlockGroupBase(item, options)
    );
}
 
function cloneListLevel(level: ReadonlyContentModelListLevel): ContentModelListLevel {
    const { listType } = level;
 
    return Object.assign({ listType }, cloneModelWithFormat(level), cloneModelWithDataset(level));
}
function cloneDivider(
    divider: ReadonlyContentModelDivider,
    options: CloneModelOptions
): ContentModelDivider {
    const { tagName, isSelected, cachedElement } = divider;
 
    return Object.assign(
        {
            isSelected,
            tagName,
            cachedElement: handleCachedElement(cachedElement, 'cache', options),
        },
        cloneBlockBase(divider)
    );
}
 
function cloneGeneralBlock(
    general: ReadonlyContentModelGeneralBlock,
    options: CloneModelOptions
): ContentModelGeneralBlock {
    const { element } = general;
 
    return Object.assign(
        {
            element: handleCachedElement(element, 'general', options),
        },
        cloneBlockBase(general),
        cloneBlockGroupBase(general, options)
    );
}
 
function cloneSelectionMarker(
    marker: ReadonlyContentModelSelectionMarker
): ContentModelSelectionMarker {
    return Object.assign({ isSelected: marker.isSelected }, cloneSegmentBase(marker));
}
 
function cloneImage(image: ReadonlyContentModelImage): ContentModelImage {
    const { src, alt, title, isSelectedAsImageSelection } = image;
 
    return Object.assign(
        { src, alt, title, isSelectedAsImageSelection },
        cloneSegmentBase(image),
        cloneModelWithDataset(image)
    );
}
 
function cloneGeneralSegment(
    general: ReadonlyContentModelGeneralSegment,
    options: CloneModelOptions
): ContentModelGeneralSegment {
    return Object.assign(cloneGeneralBlock(general, options), cloneSegmentBase(general));
}
 
function cloneText(textSegment: ReadonlyContentModelText): ContentModelText {
    const { text } = textSegment;
    return Object.assign({ text }, cloneSegmentBase(textSegment));
}
 
function handleCachedElement<T extends HTMLElement>(
    node: T,
    type: 'general' | 'entity',
    options: CloneModelOptions
): T;
 
function handleCachedElement<T extends HTMLElement>(
    node: T | undefined,
    type: 'cache',
    options: CloneModelOptions
): T | undefined;
 
function handleCachedElement<T extends HTMLElement>(
    node: T | undefined,
    type: 'general' | 'entity' | 'cache',
    options: CloneModelOptions
): T | undefined {
    const { includeCachedElement } = options;
 
    if (!node) {
        return undefined;
    } else if (!includeCachedElement) {
        return type == 'cache' ? undefined : (node.cloneNode(true /*deep*/) as T);
    } else if (includeCachedElement === true) {
        return node;
    } else {
        const result = includeCachedElement(node, type) as T | undefined;
 
        if ((type == 'general' || type == 'entity') && !result) {
            throw new Error('Entity and General Model must has wrapper element');
        }
 
        return result;
    }
}