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 | 1x 1x 342x 1x 411x 1x 1x 1x 681x 1x 297x 297x 297x 1x 2x 1x 1x 1x 1x 1x 5x 1x 431x 431x 431x 1x 7x 7x 12x 5x 7x 2x 1x 3196x 3196x 1x 230x 230x 230x 1x 269x 269x 269x 269x 269x 1x 2x 1x 1x 1x 1x 1x 4x 4x 4x 1x 1x 1x 1x 556x 342x | import {
getColor,
getRangesByText,
isBlockElement,
isNodeOfType,
parseValueWithUnit,
toArray,
} from 'roosterjs-content-model-dom';
import type {
ContentModelSegmentFormat,
DarkColorHandler,
DOMHelper,
} from 'roosterjs-content-model-types';
/**
* @internal
*/
export interface DOMHelperImplOption {
cloneIndependentRoot?: boolean;
}
class DOMHelperImpl implements DOMHelper {
constructor(private contentDiv: HTMLElement, private options: DOMHelperImplOption) {}
queryElements(selector: string): HTMLElement[] {
return toArray(this.contentDiv.querySelectorAll(selector)) as HTMLElement[];
}
getTextContent(): string {
return this.contentDiv.textContent || '';
}
isNodeInEditor(node: Node, excludeRoot?: boolean): boolean {
return excludeRoot && node == this.contentDiv ? false : this.contentDiv.contains(node);
}
calculateZoomScale(): number {
const originalWidth = this.contentDiv.getBoundingClientRect()?.width || 0;
const visualWidth = this.contentDiv.offsetWidth;
return visualWidth > 0 && originalWidth > 0
? Math.round((originalWidth / visualWidth) * 100) / 100
: 1;
}
setDomAttribute(name: string, value: string | null) {
if (value === null) {
this.contentDiv.removeAttribute(name);
} else {
this.contentDiv.setAttribute(name, value);
}
}
getDomAttribute(name: string): string | null {
return this.contentDiv.getAttribute(name);
}
getDomStyle<T extends keyof CSSStyleDeclaration>(style: T): CSSStyleDeclaration[T] {
return this.contentDiv.style[style];
}
findClosestElementAncestor(startFrom: Node, selector?: string): HTMLElement | null {
const startElement = isNodeOfType(startFrom, 'ELEMENT_NODE')
? startFrom
: startFrom.parentElement;
const closestElement = selector
? (startElement?.closest(selector) as HTMLElement | null)
: startElement;
return closestElement &&
this.isNodeInEditor(closestElement) &&
closestElement != this.contentDiv
? closestElement
: null;
}
/**
* Find the closest block element ancestor from the given node within current editing scope
* @param startFrom The node to start the search from
* @returns The closest block element ancestor
*/
findClosestBlockElement(startFrom: Node): HTMLElement {
let node: Node | null = startFrom;
while (node && this.isNodeInEditor(node)) {
if (isNodeOfType(node, 'ELEMENT_NODE') && isBlockElement(node)) {
return node;
}
node = node.parentElement;
}
return this.contentDiv;
}
hasFocus(): boolean {
const activeElement = this.contentDiv.ownerDocument.activeElement;
return !!(activeElement && this.contentDiv.contains(activeElement));
}
/**
* Check if the root element is in RTL mode
*/
isRightToLeft(): boolean {
const contentDiv = this.contentDiv;
const style = contentDiv.ownerDocument.defaultView?.getComputedStyle(contentDiv);
return style?.direction == 'rtl';
}
/**
* Get the width of the editable area of the editor content div
*/
getClientWidth(): number {
const contentDiv = this.contentDiv;
const style = contentDiv.ownerDocument.defaultView?.getComputedStyle(contentDiv);
const paddingLeft = parseValueWithUnit(style?.paddingLeft);
const paddingRight = parseValueWithUnit(style?.paddingRight);
return this.contentDiv.clientWidth - (paddingLeft + paddingRight);
}
/**
* Get a deep cloned root element
*/
getClonedRoot(): HTMLElement {
if (this.options.cloneIndependentRoot) {
const doc = this.contentDiv.ownerDocument.implementation.createHTMLDocument();
const clone = doc.importNode(this.contentDiv, true /*deep*/);
return clone;
} else {
return this.contentDiv.cloneNode(true /*deep*/) as HTMLElement;
}
}
/**
* Get format of the container element
* @param isInDarkMode Optional flag to indicate if the environment is in dark mode
* @param darkColorHandler Optional DarkColorHandler to retrieve dark mode colors
*/
getContainerFormat(
isInDarkMode?: boolean,
darkColorHandler?: DarkColorHandler
): ContentModelSegmentFormat {
const window = this.contentDiv.ownerDocument.defaultView;
const style = window?.getComputedStyle(this.contentDiv);
return style
? {
fontSize: style.fontSize,
fontFamily: style.fontFamily,
fontWeight: style.fontWeight,
textColor: getColor(
this.contentDiv,
false /*isBackgroundColor*/,
!!isInDarkMode,
darkColorHandler,
style.color
),
backgroundColor: getColor(
this.contentDiv,
true /*isBackgroundColor*/,
!!isInDarkMode,
darkColorHandler,
style.backgroundColor
),
italic: style.fontStyle == 'italic',
letterSpacing: style.letterSpacing,
lineHeight: style.lineHeight,
strikethrough: style.textDecoration?.includes('line-through'),
superOrSubScriptSequence: style.verticalAlign,
underline: style.textDecoration?.includes('underline'),
}
: {};
}
/**
* Get text ranges by searching for a specific text, with options to match case and whole word.
* This will only search within editable elements.
* @param text The text to search for
* @param matchCase Whether to match case
* @param wholeWord Whether to match whole word
* @returns An array of Ranges that match the search criteria
*/
getRangesByText(text: string, matchCase: boolean, wholeWord: boolean): Range[] {
return getRangesByText(this.contentDiv, text, matchCase, wholeWord, true /*editableOnly*/);
}
}
/**
* @internal Create new instance of DOMHelper
*/
export function createDOMHelper(
contentDiv: HTMLElement,
options: DOMHelperImplOption = {}
): DOMHelper {
return new DOMHelperImpl(contentDiv, options);
}
|