@liveblocks/react-ui
Version:
A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.
63 lines (60 loc) • 2.21 kB
JavaScript
import { Range, Editor, Path } from 'slate';
import { isWhitespaceCharacter } from './is-whitespace-character.js';
const defaultOptions = {
direction: "before",
allowConsecutiveWhitespace: true
};
function getMatchRange(editor, at, terminators = [" "], options = defaultOptions) {
const { include, direction, ignoreTerminator, allowConsecutiveWhitespace } = {
...defaultOptions,
...options
};
let [start, end] = Range.edges(at);
let point = start;
let previousCharacterWasWhitespace = false;
function move(direction2) {
const nextPoint = direction2 === "after" ? Editor.after(editor, point, { unit: "character" }) : Editor.before(editor, point, { unit: "character" });
if (!nextPoint || Path.compare(nextPoint.path, point.path) !== 0) {
return false;
}
const nextCharacter = nextPoint && Editor.string(
editor,
direction2 === "after" ? { anchor: point, focus: nextPoint } : { anchor: nextPoint, focus: point }
);
const lastCharacter = nextCharacter && nextCharacter[direction2 === "after" ? 0 : nextCharacter.length - 1];
if (!allowConsecutiveWhitespace && previousCharacterWasWhitespace && isWhitespaceCharacter(lastCharacter)) {
return false;
}
if (nextPoint && lastCharacter && (!terminators.includes(lastCharacter) || ignoreTerminator?.(lastCharacter, nextPoint, direction2))) {
previousCharacterWasWhitespace = isWhitespaceCharacter(lastCharacter);
point = nextPoint;
if (point.offset === 0) {
return false;
}
} else {
return false;
}
return true;
}
if (direction !== "before") {
point = end;
while (move("after"))
;
end = point;
}
if (direction !== "after") {
point = start;
while (move("before"))
;
start = point;
}
if (include) {
return {
anchor: direction === "before" || direction === "both" ? Editor.before(editor, start, { unit: "offset" }) ?? start : start,
focus: direction === "after" || direction === "both" ? Editor.after(editor, end, { unit: "offset" }) ?? end : end
};
}
return { anchor: start, focus: end };
}
export { getMatchRange };
//# sourceMappingURL=get-match-range.js.map