Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/sync/editor/string/doc.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { CompressedPatch, Document } from "../generic/types";6import { apply_patch, make_patch } from "../generic/util";78// Immutable string document that satisfies our spec.9export class StringDocument implements Document {10private value: string;1112constructor(value = "") {13this.value = value;14}1516public to_str(): string {17return this.value;18}1920public is_equal(other?: StringDocument): boolean {21return this.value === (other != null ? other.value : undefined);22}2324public apply_patch(patch: CompressedPatch): StringDocument {25return new StringDocument(apply_patch(patch, this.value)[0]);26}2728public make_patch(other: StringDocument): CompressedPatch {29return make_patch(this.value, other.value);30}3132public set(x: any): StringDocument {33if (typeof x === "string") {34return new StringDocument(x);35}36throw Error("x must be a string");37}3839public get(_?: any): any {40throw Error("get queries on strings don't have meaning");41}4243public get_one(_?: any): any {44throw Error("get_one queries on strings don't have meaning");45}4647public delete(_?: any): StringDocument {48throw Error("delete on strings doesn't have meaning");49}5051public changes(_?: StringDocument): any {52// no-op (this is useful for other things, e.g., db-doc)53return;54}5556public count(): number {57return this.value.length;58}59}606162