Skip to content

Commit ded7f69

Browse files
committed
docs: add some jsdoc
1 parent 864cf54 commit ded7f69

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

packages/core/src/locations/location.ts

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ import {
55
} from "../api/getBlockInfoFromPos.js";
66
import { getNodeById } from "../api/nodeUtil.js";
77
import type { BlockId, Location, PMLocation, Point, Range } from "./types.js";
8-
import { isBlockId, isBlockIdentifier, isPoint, isRange } from "./utils.js";
8+
import {
9+
getBlockRange,
10+
isBlockId,
11+
isBlockIdentifier,
12+
isPoint,
13+
isRange,
14+
} from "./utils.js";
915

10-
export function resolveLocation(
11-
doc: Node,
12-
location: Location,
13-
// TODO
14-
// opts: {
15-
// /**
16-
// * Whether to include child blocks as part of the response
17-
// * @default true
18-
// */
19-
// includeChildren: boolean;
20-
// },
21-
): PMLocation {
16+
/**
17+
* Resolves a {@link Location} to a {@link PMLocation}.
18+
*/
19+
export function resolveLocation(doc: Node, location: Location): PMLocation {
2220
if (isBlockId(location)) {
2321
return resolveBlockToPM(doc, location);
2422
}
@@ -68,7 +66,7 @@ export function resolvePointToPM(doc: Node, point: Point): PMLocation {
6866
if (head > block.head) {
6967
// TODO should this just clamp?
7068
throw new Error(
71-
`Offset ${point.offset} exceeds block length ${block.head - block.anchor}`,
69+
`Invalid offset: ${point.offset} exceeds block length ${block.head - block.anchor}`,
7270
);
7371
}
7472

@@ -112,6 +110,9 @@ export function resolveRangeToPM(doc: Node, range: Range): PMLocation {
112110
}
113111
}
114112

113+
/**
114+
* Resolves a {@link PMLocation} to a {@link Point} or {@link Range}, depending on if the {@link PMLocation} points to a single block or a range of blocks.
115+
*/
115116
export function resolvePMToLocation(
116117
doc: Node,
117118
pmLocation: PMLocation,
@@ -167,3 +168,34 @@ export function resolvePMToLocation(
167168
},
168169
};
169170
}
171+
172+
/**
173+
* Returns the block's {@link PMLocation} at the given {@link Location}
174+
*/
175+
export function getBlockPosAt(doc: Node, location: Location): PMLocation {
176+
const [blockId, otherBlockId] = getBlockRange(location);
177+
if (blockId !== otherBlockId) {
178+
throw new Error(
179+
"Block is ambiguous, given a range of blocks to get the start of: " +
180+
JSON.stringify(location),
181+
{ cause: { location } },
182+
);
183+
}
184+
return resolveBlockToPM(doc, blockId);
185+
}
186+
187+
/**
188+
* Returns the block's start position at the given {@link Location}
189+
*/
190+
export function getBlockStartPos(doc: Node, location: Location): number {
191+
const block = getBlockPosAt(doc, location);
192+
return block.anchor;
193+
}
194+
195+
/**
196+
* Returns the block's end position at the given {@link Location}
197+
*/
198+
export function getBlockEndPos(doc: Node, location: Location): number {
199+
const block = getBlockPosAt(doc, location);
200+
return block.head;
201+
}

0 commit comments

Comments
 (0)