From b1a1021612b504bbc75488d650dc4d3eb926f20c Mon Sep 17 00:00:00 2001 From: Electrolux <59329360+electroluxcode@users.noreply.github.com> Date: Thu, 26 Sep 2024 21:18:05 +0800 Subject: [PATCH] feat: add prop surrounds with range (#5729) * feat: add prop surrounds with range * Create small-carrots-jam.md * fix: docx about range.included --------- Co-authored-by: Dylan Schiemann --- .changeset/small-carrots-jam.md | 5 +++++ docs/api/locations/range.md | 4 ++++ packages/slate/src/interfaces/range.ts | 13 +++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 .changeset/small-carrots-jam.md diff --git a/.changeset/small-carrots-jam.md b/.changeset/small-carrots-jam.md new file mode 100644 index 0000000000..9349bede94 --- /dev/null +++ b/.changeset/small-carrots-jam.md @@ -0,0 +1,5 @@ +--- +'slate': patch +--- + +feat: add prop surrounds with range diff --git a/docs/api/locations/range.md b/docs/api/locations/range.md index fd024eef4a..6155f0095c 100644 --- a/docs/api/locations/range.md +++ b/docs/api/locations/range.md @@ -54,6 +54,10 @@ Check if a `range` includes a path, a point, or part of another range. For clarity the definition of `includes` can mean partially includes. Another way to describe this is if one Range intersects the other Range. +#### `Range.surrounds(range: Range, target: Range) => boolean` + +Check if a `range` includes another range. + #### `Range.isBackward(range: Range) => boolean` Check if a `range` is backward, meaning that its anchor point appears _after_ its focus point in the document. diff --git a/packages/slate/src/interfaces/range.ts b/packages/slate/src/interfaces/range.ts index 9933c35eaa..b2f797017d 100644 --- a/packages/slate/src/interfaces/range.ts +++ b/packages/slate/src/interfaces/range.ts @@ -46,6 +46,11 @@ export interface RangeInterface { */ includes: (range: Range, target: Path | Point | Range) => boolean + /** + * Check if a range includes another range. + */ + surrounds: (range: Range, target: Range) => boolean + /** * Get the intersection of a range with another. */ @@ -124,6 +129,14 @@ export const Range: RangeInterface = { ) }, + surrounds(range: Range, target: Range): boolean { + const intersectionRange = Range.intersection(range, target) + if (!intersectionRange) { + return false + } + return Range.equals(intersectionRange, target) + }, + includes(range: Range, target: Path | Point | Range): boolean { if (Range.isRange(target)) { if (