@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
231 lines (229 loc) • 7.27 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/editor/src/components/collaborators-overlay/compute-selection.ts
var compute_selection_exports = {};
__export(compute_selection_exports, {
computeSelectionVisual: () => computeSelectionVisual,
resolveTargetElement: () => resolveTargetElement
});
module.exports = __toCommonJS(compute_selection_exports);
var import_core_data = require("@wordpress/core-data");
var import_lock_unlock = require("../../lock-unlock.cjs");
var import_cursor_dom_utils = require("./cursor-dom-utils.cjs");
var { SelectionDirection, SelectionType } = (0, import_lock_unlock.unlock)(
import_core_data.privateApis
);
function resolveTargetElement(editorDocument, resolvedSelection) {
if (!resolvedSelection.localClientId) {
return null;
}
const blockElement = editorDocument.querySelector(
`[data-block="${resolvedSelection.localClientId}"]`
);
if (!blockElement || !resolvedSelection.attributeKey) {
return blockElement;
}
const attrKey = CSS.escape(resolvedSelection.attributeKey);
const byKey = blockElement.querySelector(
`[data-wp-block-attribute-key="${attrKey}"]`
);
if (byKey) {
return byKey;
}
const editable = blockElement.querySelector(
'[contenteditable="true"]'
);
if (editable) {
return editable;
}
const textEl = blockElement.querySelector(
"p, h1, h2, h3, h4, h5, h6, pre, blockquote, td, th, li, figcaption"
);
return textEl ?? blockElement;
}
function computeSelectionVisual(selection, start, end, overlayContext) {
if (selection.type === SelectionType.None || selection.type === SelectionType.WholeBlock) {
return {};
}
if (selection.type === SelectionType.Cursor) {
return computeCursorOnly(start, overlayContext);
}
if (!end) {
return {};
}
return computeTextSelection(selection, start, end, overlayContext);
}
function computeCursorOnly(start, overlayContext) {
if (!start.localClientId) {
return {};
}
const targetElement = resolveTargetElement(
overlayContext.editorDocument,
start
);
return {
coords: (0, import_cursor_dom_utils.getCursorPosition)(
start.richTextOffset,
targetElement,
overlayContext.editorDocument,
overlayContext.overlayRect
)
};
}
function computeTextSelection(selection, start, end, overlayContext) {
if (!start.localClientId || !end.localClientId) {
return {};
}
const isReverse = selection.selectionDirection === SelectionDirection.Backward;
const activeEnd = isReverse ? start : end;
if (selection.type === SelectionType.SelectionInOneBlock) {
if (start.richTextOffset === null || end.richTextOffset === null) {
return {};
}
const result = computeSingleBlockRects(start, end, overlayContext);
if (result.rects.length > 0) {
return {
coords: (0, import_cursor_dom_utils.getCursorPosition)(
activeEnd.richTextOffset,
result.blockElement,
overlayContext.editorDocument,
overlayContext.overlayRect
),
selectionRects: result.rects
};
}
return {
coords: (0, import_cursor_dom_utils.getCursorPosition)(
start.richTextOffset,
resolveTargetElement(overlayContext.editorDocument, start),
overlayContext.editorDocument,
overlayContext.overlayRect
)
};
}
return computeMultiBlockOverlayRects(start, end, overlayContext);
}
function blockBoundingRect(blockEl, overlayRect) {
const r = blockEl.getBoundingClientRect();
return {
x: r.left - overlayRect.left,
y: r.top - overlayRect.top,
width: r.width,
height: r.height
};
}
function computeMultiBlockOverlayRects(start, end, overlayContext) {
const { editorDocument, overlayRect } = overlayContext;
const range = (0, import_cursor_dom_utils.getOrderedBlockRange)(
start.localClientId,
end.localClientId,
editorDocument
);
if (!range) {
return {};
}
const docFirst = range.firstId === start.localClientId ? start : end;
const docLast = range.firstId === start.localClientId ? end : start;
const {
firstEl: docFirstEl,
lastEl: docLastEl,
middleEls,
sameContainer
} = range;
if (sameContainer) {
return docFirstEl.innerText?.trim() ? {
selectionRects: [
blockBoundingRect(docFirstEl, overlayRect)
]
} : {};
}
const MAX = Number.MAX_SAFE_INTEGER;
const rects = [];
if (docFirstEl.innerText?.trim()) {
const firstOffset = docFirst.richTextOffset;
if (firstOffset === null || firstOffset === 0) {
rects.push(blockBoundingRect(docFirstEl, overlayRect));
} else {
const el = resolveTargetElement(editorDocument, docFirst);
const textRects = el ? (0, import_cursor_dom_utils.getSelectionRects)(
el,
firstOffset,
MAX,
editorDocument,
overlayRect
) : null;
rects.push(
...textRects ?? [
blockBoundingRect(docFirstEl, overlayRect)
]
);
}
}
for (const blockEl of middleEls) {
if (blockEl.innerText?.trim()) {
rects.push(blockBoundingRect(blockEl, overlayRect));
}
}
if (docLastEl.innerText?.trim()) {
const lastOffset = docLast.richTextOffset;
if (lastOffset === null) {
rects.push(blockBoundingRect(docLastEl, overlayRect));
} else if (lastOffset > 0) {
const el = resolveTargetElement(editorDocument, docLast);
const textRects = el ? (0, import_cursor_dom_utils.getSelectionRects)(
el,
0,
lastOffset,
editorDocument,
overlayRect
) : null;
rects.push(
...textRects ?? [
blockBoundingRect(docLastEl, overlayRect)
]
);
}
}
return rects.length > 0 ? { selectionRects: rects } : {};
}
function computeSingleBlockRects(start, end, overlayContext) {
const blockElement = resolveTargetElement(
overlayContext.editorDocument,
start
);
if (!blockElement || start.richTextOffset === null || end.richTextOffset === null) {
return { rects: [], blockElement: null };
}
return {
rects: (0, import_cursor_dom_utils.getSelectionRects)(
blockElement,
start.richTextOffset,
end.richTextOffset,
overlayContext.editorDocument,
overlayContext.overlayRect
) ?? [],
blockElement
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
computeSelectionVisual,
resolveTargetElement
});
//# sourceMappingURL=compute-selection.cjs.map