@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
161 lines (159 loc) • 5.26 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-utils.ts
var crdt_utils_exports = {};
__export(crdt_utils_exports, {
asHtmlStringIndex: () => asHtmlStringIndex,
asRichTextOffset: () => asRichTextOffset,
createYMap: () => createYMap,
findBlockByClientIdInDoc: () => findBlockByClientIdInDoc,
getRootMap: () => getRootMap,
getYTextByAttributeKey: () => getYTextByAttributeKey,
htmlIndexToRichTextOffset: () => htmlIndexToRichTextOffset,
isYMap: () => isYMap,
richTextOffsetToHtmlIndex: () => richTextOffsetToHtmlIndex
});
module.exports = __toCommonJS(crdt_utils_exports);
var import_sync = require("@wordpress/sync");
var import_rich_text = require("@wordpress/rich-text");
var import_sync2 = require("../sync.cjs");
function getRootMap(doc, key) {
return doc.getMap(key);
}
function createYMap(partial = {}) {
return new import_sync.Y.Map(Object.entries(partial));
}
function isYMap(value) {
return value instanceof import_sync.Y.Map;
}
function asRichTextOffset(offset) {
return offset;
}
function asHtmlStringIndex(index) {
return index;
}
function getYTextByAttributeKey(attributes, attributeKey) {
const directValue = attributes.get(attributeKey);
if (directValue instanceof import_sync.Y.Text) {
return directValue;
}
let value = attributes;
for (const pathPart of attributeKey.split(".")) {
if (value instanceof import_sync.Y.Map) {
value = value.get(pathPart);
} else if (value instanceof import_sync.Y.Array) {
const index = Number.parseInt(pathPart, 10);
if (!Number.isSafeInteger(index) || index < 0 || index.toString() !== pathPart) {
return null;
}
value = value.get(index);
} else {
return null;
}
}
return value instanceof import_sync.Y.Text ? value : null;
}
function findBlockByClientIdInDoc(blockId, ydoc) {
const ymap = getRootMap(ydoc, import_sync2.CRDT_RECORD_MAP_KEY);
const blocks = ymap.get("blocks");
if (!(blocks instanceof import_sync.Y.Array)) {
return null;
}
return findBlockByClientIdInBlocks(blockId, blocks);
}
var MARKER_START = 57344;
function pickMarker(text) {
const tryCount = 16;
for (let code = MARKER_START; code < MARKER_START + tryCount; code++) {
const candidate = String.fromCharCode(code);
if (!text.includes(candidate)) {
return candidate;
}
}
return null;
}
function htmlIndexToRichTextOffset(html, htmlIndex) {
if (!html.includes("<") && !html.includes("&")) {
return asRichTextOffset(htmlIndex);
}
const marker = pickMarker(html);
if (!marker) {
return asRichTextOffset(htmlIndex);
}
const withMarker = html.slice(0, htmlIndex) + marker + html.slice(htmlIndex);
const value = (0, import_rich_text.create)({ html: withMarker });
const markerPos = value.text.indexOf(marker);
return asRichTextOffset(markerPos === -1 ? htmlIndex : markerPos);
}
function richTextOffsetToHtmlIndex(html, richTextOffset) {
if (!html.includes("<") && !html.includes("&")) {
return asHtmlStringIndex(richTextOffset);
}
const marker = pickMarker(html);
if (!marker) {
return asHtmlStringIndex(richTextOffset);
}
const value = (0, import_rich_text.create)({ html });
const markerValue = (0, import_rich_text.create)({ text: marker });
if (value.formats[richTextOffset]) {
markerValue.formats[0] = value.formats[richTextOffset];
}
const withMarker = (0, import_rich_text.insert)(
value,
markerValue,
richTextOffset,
richTextOffset
);
const htmlWithMarker = (0, import_rich_text.toHTMLString)({ value: withMarker });
const markerIndex = htmlWithMarker.indexOf(marker);
return asHtmlStringIndex(
markerIndex === -1 ? richTextOffset : markerIndex
);
}
function findBlockByClientIdInBlocks(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 = findBlockByClientIdInBlocks(
blockId,
innerBlocks
);
if (innerBlock) {
return innerBlock;
}
}
}
return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
asHtmlStringIndex,
asRichTextOffset,
createYMap,
findBlockByClientIdInDoc,
getRootMap,
getYTextByAttributeKey,
htmlIndexToRichTextOffset,
isYMap,
richTextOffsetToHtmlIndex
});
//# sourceMappingURL=crdt-utils.cjs.map