UNPKG

@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.

65 lines (61 loc) 2.31 kB
'use strict'; var slate = require('slate'); var isWhitespaceCharacter = require('./is-whitespace-character.cjs'); const defaultOptions = { direction: "before", allowConsecutiveWhitespace: true }; function getMatchRange(editor, at, terminators = [" "], options = defaultOptions) { const { include, direction, ignoreTerminator, allowConsecutiveWhitespace } = { ...defaultOptions, ...options }; let [start, end] = slate.Range.edges(at); let point = start; let previousCharacterWasWhitespace = false; function move(direction2) { const nextPoint = direction2 === "after" ? slate.Editor.after(editor, point, { unit: "character" }) : slate.Editor.before(editor, point, { unit: "character" }); if (!nextPoint || slate.Path.compare(nextPoint.path, point.path) !== 0) { return false; } const nextCharacter = nextPoint && slate.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.isWhitespaceCharacter(lastCharacter)) { return false; } if (nextPoint && lastCharacter && (!terminators.includes(lastCharacter) || ignoreTerminator?.(lastCharacter, nextPoint, direction2))) { previousCharacterWasWhitespace = isWhitespaceCharacter.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" ? slate.Editor.before(editor, start, { unit: "offset" }) ?? start : start, focus: direction === "after" || direction === "both" ? slate.Editor.after(editor, end, { unit: "offset" }) ?? end : end }; } return { anchor: start, focus: end }; } exports.getMatchRange = getMatchRange; //# sourceMappingURL=get-match-range.cjs.map