@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
173 lines (171 loc) • 6.53 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/core-data/src/utils/crdt-user-selections.ts
var crdt_user_selections_exports = {};
__export(crdt_user_selections_exports, {
SelectionType: () => SelectionType,
areSelectionsStatesEqual: () => areSelectionsStatesEqual,
getSelectionState: () => getSelectionState
});
module.exports = __toCommonJS(crdt_user_selections_exports);
var import_sync = require("@wordpress/sync");
var import_sync2 = require("../sync.cjs");
var import_crdt_utils = require("./crdt-utils.cjs");
var SelectionType = /* @__PURE__ */ ((SelectionType2) => {
SelectionType2["None"] = "none";
SelectionType2["Cursor"] = "cursor";
SelectionType2["SelectionInOneBlock"] = "selection-in-one-block";
SelectionType2["SelectionInMultipleBlocks"] = "selection-in-multiple-blocks";
SelectionType2["WholeBlock"] = "whole-block";
return SelectionType2;
})(SelectionType || {});
function getSelectionState(selectionStart, selectionEnd, yDoc) {
const ymap = (0, import_crdt_utils.getRootMap)(yDoc, import_sync2.CRDT_RECORD_MAP_KEY);
const yBlocks = ymap.get("blocks") ?? new import_sync.Y.Array();
const isSelectionEmpty = Object.keys(selectionStart).length === 0;
const noSelection = {
type: "none" /* None */
};
if (isSelectionEmpty) {
return noSelection;
}
const isSelectionInOneBlock = selectionStart.clientId === selectionEnd.clientId;
const isCursorOnly = isSelectionInOneBlock && selectionStart.offset === selectionEnd.offset;
const isSelectionAWholeBlock = isSelectionInOneBlock && selectionStart.offset === void 0 && selectionEnd.offset === void 0;
if (isSelectionAWholeBlock) {
return {
type: "whole-block" /* WholeBlock */,
blockId: selectionStart.clientId
};
} else if (isCursorOnly) {
const cursorPosition = getCursorPosition(selectionStart, yBlocks);
if (!cursorPosition) {
return noSelection;
}
return {
type: "cursor" /* Cursor */,
blockId: selectionStart.clientId,
cursorPosition
};
} else if (isSelectionInOneBlock) {
const cursorStartPosition2 = getCursorPosition(
selectionStart,
yBlocks
);
const cursorEndPosition2 = getCursorPosition(selectionEnd, yBlocks);
if (!cursorStartPosition2 || !cursorEndPosition2) {
return noSelection;
}
return {
type: "selection-in-one-block" /* SelectionInOneBlock */,
blockId: selectionStart.clientId,
cursorStartPosition: cursorStartPosition2,
cursorEndPosition: cursorEndPosition2
};
}
const cursorStartPosition = getCursorPosition(selectionStart, yBlocks);
const cursorEndPosition = getCursorPosition(selectionEnd, yBlocks);
if (!cursorStartPosition || !cursorEndPosition) {
return noSelection;
}
return {
type: "selection-in-multiple-blocks" /* SelectionInMultipleBlocks */,
blockStartId: selectionStart.clientId,
blockEndId: selectionEnd.clientId,
cursorStartPosition,
cursorEndPosition
};
}
function getCursorPosition(selection, blocks) {
const block = findBlockByClientId(selection.clientId, blocks);
if (!block || !selection.attributeKey || void 0 === selection.offset) {
return null;
}
const attributes = block.get("attributes");
const currentYText = attributes?.get(selection.attributeKey);
const relativePosition = import_sync.Y.createRelativePositionFromTypeIndex(
currentYText,
selection.offset
);
return {
relativePosition,
absoluteOffset: selection.offset
};
}
function findBlockByClientId(blockId, blocks) {
for (const block of blocks) {
if (block.get("clientId") === blockId) {
return block;
}
const innerBlocks = block.get("innerBlocks");
if (innerBlocks && innerBlocks.length > 0) {
const innerBlock = findBlockByClientId(blockId, innerBlocks);
if (innerBlock) {
return innerBlock;
}
}
}
return null;
}
function areSelectionsStatesEqual(selection1, selection2) {
if (selection1.type !== selection2.type) {
return false;
}
switch (selection1.type) {
case "none" /* None */:
return true;
case "cursor" /* Cursor */:
return selection1.blockId === selection2.blockId && areCursorPositionsEqual(
selection1.cursorPosition,
selection2.cursorPosition
);
case "selection-in-one-block" /* SelectionInOneBlock */:
return selection1.blockId === selection2.blockId && areCursorPositionsEqual(
selection1.cursorStartPosition,
selection2.cursorStartPosition
) && areCursorPositionsEqual(
selection1.cursorEndPosition,
selection2.cursorEndPosition
);
case "selection-in-multiple-blocks" /* SelectionInMultipleBlocks */:
return selection1.blockStartId === selection2.blockStartId && selection1.blockEndId === selection2.blockEndId && areCursorPositionsEqual(
selection1.cursorStartPosition,
selection2.cursorStartPosition
) && areCursorPositionsEqual(
selection1.cursorEndPosition,
selection2.cursorEndPosition
);
case "whole-block" /* WholeBlock */:
return selection1.blockId === selection2.blockId;
default:
return false;
}
}
function areCursorPositionsEqual(cursorPosition1, cursorPosition2) {
const isRelativePositionEqual = JSON.stringify(cursorPosition1.relativePosition) === JSON.stringify(cursorPosition2.relativePosition);
const isAbsoluteOffsetEqual = cursorPosition1.absoluteOffset === cursorPosition2.absoluteOffset;
return isRelativePositionEqual && isAbsoluteOffsetEqual;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SelectionType,
areSelectionsStatesEqual,
getSelectionState
});
//# sourceMappingURL=crdt-user-selections.cjs.map