-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlockMetadata.ts
More file actions
69 lines (62 loc) · 1.87 KB
/
Copy pathBlockMetadata.ts
File metadata and controls
69 lines (62 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type {Transaction} from "@codemirror/state";
export type Range = {from: number; to: number};
export class BlockMetadata {
public readonly id: string;
public readonly name: string;
public readonly output: Range | null;
public readonly source: Range;
public attributes: Record<string, unknown>;
public error: boolean;
/**
* Create a new `BlockMetadata` instance.
* @param id a unique identifier for this block
* @param name a descriptive name of this block
* @param output the range of the output region
* @param source the range of the source region
* @param attributes any user-customized attributes of this block
* @param error whether this block has an error
*/
public constructor(
id: string,
name: string,
output: Range | null,
source: Range,
attributes: Record<string, unknown> = {},
error: boolean = false,
) {
this.id = id;
this.name = name;
this.output = output;
this.source = source;
this.attributes = attributes;
this.error = error;
}
/**
* Get the start position (inclusive) of this block.
*/
get from() {
return this.output?.from ?? this.source.from;
}
/**
* Get the end position (exclusive) of this block.
*/
get to() {
return this.source.to;
}
public map(tr: Transaction): BlockMetadata {
// If no changes were made to the document, return the current instance.
if (!tr.docChanged) return this;
// Otherwise, map the output and source ranges.
const output = this.output
? {
from: tr.changes.mapPos(this.output.from, -1),
to: tr.changes.mapPos(this.output.to, 1),
}
: null;
const source = {
from: tr.changes.mapPos(this.source.from, 1),
to: tr.changes.mapPos(this.source.to, 1),
};
return new BlockMetadata(this.id, this.name, output, source, this.attributes, this.error);
}
}