All files / roosterjs-content-model-api/lib/publicApi/segment setTextColor.ts

100% Statements 10/10
85.71% Branches 12/14
100% Functions 3/3
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 341x               1x 10x   10x         2x   2x 1x       8x   8x 1x              
import { formatSegmentWithContentModel } from '../utils/formatSegmentWithContentModel';
import type { IEditor } from 'roosterjs-content-model-types';
 
/**
 * Set text color
 * @param editor The editor to operate on
 * @param textColor The text color to set. Pass null to remove existing color.
 */
export function setTextColor(editor: IEditor, textColor: string | null) {
    editor.focus();
 
    formatSegmentWithContentModel(
        editor,
        'setTextColor',
        textColor === null
            ? (format, _, segment) => {
                  delete format.textColor;
 
                  if (segment?.link) {
                      delete segment.link.format.textColor;
                  }
              }
            : (format, _, segment) => {
                  format.textColor = textColor;
 
                  if (segment?.link) {
                      segment.link.format.textColor = textColor;
                  }
              },
        undefined /* segmentHasStyleCallback*/,
        true /*includingFormatHandler*/
    );
}