All files / roosterjs-content-model-dom/lib/modelApi/block copyFormat.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 1/1
100% Lines 10/10

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                1x                   1x                 1x             1x                                               1x           966x 966x 34x       34x 7x          
import type {
    ContentModelBlockFormat,
    ContentModelFormatBase,
} from 'roosterjs-content-model-types';
 
/**
 * When copy format between list and paragraph, these are the formats that we can copy and remove from the source
 */
export const ListFormatsToMove: (keyof ContentModelBlockFormat)[] = [
    'marginRight',
    'marginLeft',
    'paddingRight',
    'paddingLeft',
];
 
/**
 * When copy format between list and paragraph, these are the formats that we can copy and keep in the source
 */
export const ListFormatsToKeep: (keyof ContentModelBlockFormat)[] = [
    'direction',
    'textAlign',
    'htmlAlign',
];
 
/**
 * When copy format from one block to another, these are all the formats that we can copy
 */
export const ListFormats: (keyof ContentModelBlockFormat)[] = ListFormatsToMove.concat(
    ListFormatsToKeep
);
 
/**
 * When copy format between paragraphs, these are the formats that we can copy
 */
export const ParagraphFormats: (keyof ContentModelBlockFormat)[] = [
    'backgroundColor',
    'direction',
    'textAlign',
    'htmlAlign',
    'lineHeight',
    'textIndent',
    'marginTop',
    'marginRight',
    'marginBottom',
    'marginLeft',
    'paddingTop',
    'paddingRight',
    'paddingBottom',
    'paddingLeft',
];
 
/**
 * Copy formats from source to target with only specified keys
 * @param targetFormat The format object to copy format to
 * @param sourceFormat The format object to copy format from
 * @param formatKeys The format keys to copy
 * @param deleteOriginalFormat True to delete the original format from sourceFormat, false to keep it. @default false
 */
export function copyFormat<T extends ContentModelFormatBase>(
    targetFormat: T,
    sourceFormat: T,
    formatKeys: (keyof T)[],
    deleteOriginalFormat?: boolean
) {
    for (const key of formatKeys) {
        if (sourceFormat[key] !== undefined) {
            Object.assign(targetFormat, {
                [key]: sourceFormat[key],
            });
 
            if (deleteOriginalFormat) {
                delete sourceFormat[key];
            }
        }
    }
}