CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/editor/string/doc.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { CompressedPatch, Document } from "../generic/types";
7
import { apply_patch, make_patch } from "../generic/util";
8
9
// Immutable string document that satisfies our spec.
10
export class StringDocument implements Document {
11
private value: string;
12
13
constructor(value = "") {
14
this.value = value;
15
}
16
17
public to_str(): string {
18
return this.value;
19
}
20
21
public is_equal(other?: StringDocument): boolean {
22
return this.value === (other != null ? other.value : undefined);
23
}
24
25
public apply_patch(patch: CompressedPatch): StringDocument {
26
return new StringDocument(apply_patch(patch, this.value)[0]);
27
}
28
29
public make_patch(other: StringDocument): CompressedPatch {
30
return make_patch(this.value, other.value);
31
}
32
33
public set(x: any): StringDocument {
34
if (typeof x === "string") {
35
return new StringDocument(x);
36
}
37
throw Error("x must be a string");
38
}
39
40
public get(_?: any): any {
41
throw Error("get queries on strings don't have meaning");
42
}
43
44
public get_one(_?: any): any {
45
throw Error("get_one queries on strings don't have meaning");
46
}
47
48
public delete(_?: any): StringDocument {
49
throw Error("delete on strings doesn't have meaning");
50
}
51
52
public changes(_?: StringDocument): any {
53
// no-op (this is useful for other things, e.g., db-doc)
54
return;
55
}
56
57
public count(): number {
58
return this.value.length;
59
}
60
}
61
62