@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
182 lines (180 loc) • 6.14 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
});
module.exports = __toCommonJS(compute_selection_exports);
var import_core_data = require("@wordpress/core-data");
var import_cursor_dom_utils = require("./cursor-dom-utils.cjs");
function computeSelectionVisual(selection, start, end, overlayContext) {
if (selection.type === import_core_data.SelectionType.None || selection.type === import_core_data.SelectionType.WholeBlock) {
return {};
}
if (selection.type === import_core_data.SelectionType.Cursor) {
return computeCursorOnly(start, overlayContext);
}
if (!end) {
return {};
}
return computeTextSelection(selection, start, end, overlayContext);
}
function computeCursorOnly(start, overlayContext) {
if (!start.localClientId) {
return {};
}
const blockElement = overlayContext.editorDocument.querySelector(
`[data-block="${start.localClientId}"]`
);
return {
coords: (0, import_cursor_dom_utils.getCursorPosition)(
start.richTextOffset,
blockElement,
overlayContext.editorDocument,
overlayContext.overlayRect
)
};
}
function computeTextSelection(selection, start, end, overlayContext) {
if (!start.localClientId || !end.localClientId || start.richTextOffset === null || end.richTextOffset === null) {
return {};
}
const isReverse = selection.selectionDirection === import_core_data.SelectionDirection.Backward;
const activeEnd = isReverse ? start : end;
let allRects;
let activeEndBlock = null;
if (selection.type === import_core_data.SelectionType.SelectionInOneBlock) {
const result = computeSingleBlockRects(start, end, overlayContext);
allRects = result.rects;
activeEndBlock = result.blockElement;
} else {
const result = computeMultiBlockRects(start, end, overlayContext);
allRects = result.rects;
activeEndBlock = activeEnd.localClientId === result.firstBlockClientId ? result.firstBlock : result.lastBlock;
}
if (allRects.length > 0) {
return {
coords: (0, import_cursor_dom_utils.getCursorPosition)(
activeEnd.richTextOffset,
activeEndBlock,
overlayContext.editorDocument,
overlayContext.overlayRect
),
selectionRects: allRects
};
}
const startBlock = overlayContext.editorDocument.querySelector(
`[data-block="${start.localClientId}"]`
);
return {
coords: (0, import_cursor_dom_utils.getCursorPosition)(
start.richTextOffset,
startBlock,
overlayContext.editorDocument,
overlayContext.overlayRect
)
};
}
function computeSingleBlockRects(start, end, overlayContext) {
const blockElement = overlayContext.editorDocument.querySelector(
`[data-block="${start.localClientId}"]`
);
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
};
}
function computeMultiBlockRects(start, end, overlayContext) {
let docFirst = start;
let docLast = end;
let firstBlock = overlayContext.editorDocument.querySelector(
`[data-block="${docFirst.localClientId}"]`
);
let lastBlock = overlayContext.editorDocument.querySelector(
`[data-block="${docLast.localClientId}"]`
);
if (firstBlock && lastBlock && (0, import_cursor_dom_utils.isNodeBefore)(lastBlock, firstBlock)) {
docFirst = end;
docLast = start;
[firstBlock, lastBlock] = [lastBlock, firstBlock];
}
if (!firstBlock || !lastBlock || docFirst.richTextOffset === null || docLast.richTextOffset === null || !docFirst.localClientId || !docLast.localClientId) {
return {
rects: [],
firstBlock: null,
lastBlock: null,
firstBlockClientId: null
};
}
const allRects = [];
const startRects = (0, import_cursor_dom_utils.getSelectionRects)(
firstBlock,
docFirst.richTextOffset,
Number.MAX_SAFE_INTEGER,
overlayContext.editorDocument,
overlayContext.overlayRect
);
if (startRects) {
allRects.push(...startRects);
}
const intermediateBlocks = (0, import_cursor_dom_utils.getBlocksBetween)(
docFirst.localClientId,
docLast.localClientId,
overlayContext.editorDocument
);
for (const intermediateBlock of intermediateBlocks) {
const rects = (0, import_cursor_dom_utils.getFullBlockSelectionRects)(
intermediateBlock,
overlayContext.editorDocument,
overlayContext.overlayRect
);
allRects.push(...rects);
}
const endRects = (0, import_cursor_dom_utils.getSelectionRects)(
lastBlock,
0,
docLast.richTextOffset,
overlayContext.editorDocument,
overlayContext.overlayRect
);
if (endRects) {
allRects.push(...endRects);
}
return {
rects: allRects,
firstBlock,
lastBlock,
firstBlockClientId: docFirst.localClientId
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
computeSelectionVisual
});
//# sourceMappingURL=compute-selection.cjs.map