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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 194x 194x 94x 100x 100x 100x 100x 100x 100x 100x 100x 7x 7x 7x 7x 7x 7x 7x 90x 18x 18x 18x 18x 18x 18x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 66x 66x 66x 9x 9x 100x 99x 98x 98x 24x 23x 23x 23x 23x 23x 17x 6x 6x 6x 6x 23x | import { areSameSelections } from '../../corePlugin/cache/areSameSelections';
import { ensureUniqueId } from '../setEditorStyle/ensureUniqueId';
import { findLastedCoInMergedCell } from './findLastedCoInMergedCell';
import { findTableCellElement } from './findTableCellElement';
import { getSafeIdSelector, parseTableCells } from 'roosterjs-content-model-dom';
import { setTableCellsStyle } from './setTableCellsStyle';
import { toggleCaret } from './toggleCaret';
import type {
EditorCore,
SelectionChangedEvent,
SetDOMSelection,
} from 'roosterjs-content-model-types';
const DOM_SELECTION_CSS_KEY = '_DOMSelection';
const HIDE_SELECTION_CSS_KEY = '_DOMSelectionHideSelection';
const IMAGE_ID = 'image';
const TRANSPARENT_SELECTION_CSS_RULE = 'background-color: transparent !important;';
const SELECTION_SELECTOR = '*::selection';
const DEFAULT_SELECTION_BORDER_COLOR = '#DB626C';
/**
* @internal
*/
export const setDOMSelection: SetDOMSelection = (core, selection, skipSelectionChangedEvent) => {
const existingSelection = core.api.getDOMSelection(core);
if (existingSelection && selection && areSameSelections(existingSelection, selection)) {
return;
}
// We are applying a new selection, so we don't need to apply cached selection in DOMEventPlugin.
// Set skipReselectOnFocus to skip this behavior
const skipReselectOnFocus = core.selection.skipReselectOnFocus;
const isDarkMode = core.lifecycle.isDarkMode;
core.selection.skipReselectOnFocus = true;
core.api.setEditorStyle(core, DOM_SELECTION_CSS_KEY, null /*cssRule*/);
core.api.setEditorStyle(core, HIDE_SELECTION_CSS_KEY, null /*cssRule*/);
toggleCaret(core, false /* hide */);
try {
switch (selection?.type) {
case 'image':
const image = selection.image;
core.selection.selection = selection;
const imageSelectionColor = isDarkMode
? core.selection.imageSelectionBorderColorDark
: core.selection.imageSelectionBorderColor;
core.api.setEditorStyle(
core,
DOM_SELECTION_CSS_KEY,
`outline-style:solid!important; outline-color:${
imageSelectionColor || DEFAULT_SELECTION_BORDER_COLOR
}!important;`,
[getSafeIdSelector(ensureUniqueId(image, IMAGE_ID))]
);
core.api.setEditorStyle(
core,
HIDE_SELECTION_CSS_KEY,
TRANSPARENT_SELECTION_CSS_RULE,
[SELECTION_SELECTOR]
);
setRangeSelection(core, image, false /* collapse */);
break;
case 'table':
const { table, firstColumn, firstRow, lastColumn, lastRow } = selection;
const parsedTable = parseTableCells(selection.table);
let firstCell = {
row: Math.min(firstRow, lastRow),
col: Math.min(firstColumn, lastColumn),
cell: <HTMLTableCellElement | null>null,
};
let lastCell = {
row: Math.max(firstRow, lastRow),
col: Math.max(firstColumn, lastColumn),
};
firstCell = findTableCellElement(parsedTable, firstCell) || firstCell;
lastCell = findLastedCoInMergedCell(parsedTable, lastCell) || lastCell;
if (
isNaN(firstCell.row) ||
isNaN(firstCell.col) ||
isNaN(lastCell.row) ||
isNaN(lastCell.col)
) {
return;
}
selection = {
type: 'table',
table,
firstRow: firstCell.row,
firstColumn: firstCell.col,
lastRow: lastCell.row,
lastColumn: lastCell.col,
tableSelectionInfo: selection.tableSelectionInfo,
};
core.selection.selection = selection;
setTableCellsStyle(core, table, parsedTable, firstCell, lastCell);
core.api.setEditorStyle(
core,
HIDE_SELECTION_CSS_KEY,
TRANSPARENT_SELECTION_CSS_RULE,
[SELECTION_SELECTOR]
);
toggleCaret(core, true /* hide */);
const nodeToSelect = firstCell.cell?.firstElementChild || firstCell.cell;
Eif (nodeToSelect) {
setRangeSelection(
core,
(nodeToSelect as HTMLElement) || undefined,
true /* collapse */
);
}
break;
case 'range':
core.domHelper.setSelectionRange(selection.range, selection.isReverted);
core.selection.selection = core.domHelper.hasFocus() ? null : selection;
break;
default:
core.selection.selection = null;
break;
}
} finally {
core.selection.skipReselectOnFocus = skipReselectOnFocus;
}
if (!skipSelectionChangedEvent) {
const eventData: SelectionChangedEvent = {
eventType: 'selectionChanged',
newSelection: selection,
};
core.api.triggerEvent(core, eventData, true /*broadcast*/);
}
};
function setRangeSelection(core: EditorCore, element: HTMLElement | undefined, collapse: boolean) {
if (element && core.domHelper.isNodeInEditor(element)) {
const doc = core.physicalRoot.ownerDocument;
const range = doc.createRange();
let isReverted: boolean | undefined = undefined;
range.selectNode(element);
if (collapse) {
range.collapse();
} else {
const selection = doc.defaultView?.getSelection();
const range = selection && selection.rangeCount > 0 && selection.getRangeAt(0);
Eif (selection && range) {
isReverted =
selection.focusNode != range.endContainer ||
selection.focusOffset != range.endOffset;
}
}
core.domHelper.setSelectionRange(range, isReverted);
}
}
|