@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
151 lines (149 loc) • 4.95 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/post-text-editor/utils.js
var utils_exports = {};
__export(utils_exports, {
adjustPosition: () => adjustPosition,
getAdjustedCursorPosition: () => getAdjustedCursorPosition,
getDiff: () => getDiff
});
module.exports = __toCommonJS(utils_exports);
var import_character = require("diff/lib/diff/character.js");
var import_line = require("diff/lib/diff/line.js");
var STRING_TOO_LARGE_THRESHOLD = 1e3;
function getDiff(oldValue, newValue) {
const maxStringLength = Math.max(oldValue.length, newValue.length);
if (maxStringLength <= STRING_TOO_LARGE_THRESHOLD) {
return (0, import_character.diffChars)(oldValue, newValue);
}
let start = 0;
const minStringLength = Math.min(oldValue.length, newValue.length);
while (start < minStringLength && oldValue[start] === newValue[start]) {
start++;
}
let oldEnd = oldValue.length;
let newEnd = newValue.length;
while (oldEnd > start && newEnd > start && oldValue[oldEnd - 1] === newValue[newEnd - 1]) {
oldEnd--;
newEnd--;
}
const oldChangedValue = oldValue.slice(start, oldEnd);
const newChangedValue = newValue.slice(start, newEnd);
const maxChangedStringLength = Math.max(
oldChangedValue.length,
newChangedValue.length
);
let changes;
if (maxChangedStringLength <= STRING_TOO_LARGE_THRESHOLD) {
changes = (0, import_character.diffChars)(oldChangedValue, newChangedValue);
} else {
changes = (0, import_line.diffLines)(oldChangedValue, newChangedValue);
}
if (start > 0) {
changes.unshift({ value: oldValue.slice(0, start) });
}
if (oldEnd < oldValue.length) {
changes.push({ value: oldValue.slice(oldEnd) });
}
return changes;
}
function clampCursorPosition(position, value) {
return Math.max(0, Math.min(value.length, position));
}
function getCursorOffsetFromCommonTextBeforeCursor(oldValue, newValue, position) {
const oldValueBeforeCursor = oldValue.slice(0, position);
let low = 1;
let high = oldValueBeforeCursor.length;
let cursorOffset;
while (low <= high) {
const length = Math.floor((low + high) / 2);
const index = newValue.indexOf(
oldValueBeforeCursor.slice(oldValueBeforeCursor.length - length)
);
if (index === -1) {
high = length - 1;
} else {
cursorOffset = index + length;
low = length + 1;
}
}
return cursorOffset;
}
function adjustPosition(position, changes, oldValue, newValue) {
let oldIndex = 0;
let newIndex = 0;
let adjustedPosition = position;
for (let i = 0; i < changes.length; i++) {
const change = changes[i];
if (!change.added && !change.removed) {
oldIndex += change.value.length;
newIndex += change.value.length;
continue;
}
const oldStart = oldIndex;
const newStart = newIndex;
let oldLength = 0;
let newLength = 0;
while (i < changes.length && (changes[i].added || changes[i].removed)) {
if (changes[i].removed) {
oldLength += changes[i].value.length;
} else {
newLength += changes[i].value.length;
}
i++;
}
i--;
const oldEnd = oldStart + oldLength;
const newEnd = newStart + newLength;
if (position < oldStart) {
break;
}
if (oldLength === 0) {
adjustedPosition += newLength;
} else if (position <= oldEnd) {
const cursorOffset = getCursorOffsetFromCommonTextBeforeCursor(
oldValue.slice(oldStart, oldEnd),
newValue.slice(newStart, newEnd),
position - oldStart
);
adjustedPosition = cursorOffset === void 0 ? newEnd : newStart + cursorOffset;
break;
} else {
adjustedPosition += newLength - oldLength;
}
oldIndex += oldLength;
newIndex += newLength;
}
return clampCursorPosition(adjustedPosition, newValue);
}
function getAdjustedCursorPosition(position, oldValue, newValue) {
return adjustPosition(
position,
getDiff(oldValue, newValue),
oldValue,
newValue
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
adjustPosition,
getAdjustedCursorPosition,
getDiff
});
//# sourceMappingURL=utils.cjs.map