Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add prop surrounds with range #5729

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/small-carrots-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate': patch
---

feat: add prop surrounds with range
4 changes: 4 additions & 0 deletions docs/api/locations/range.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions packages/slate/src/interfaces/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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 (
Expand Down
Loading