UNPKG

collaborative-ui

Version:

React component library for building real-time collaborative editing applications.

107 lines (106 loc) 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonCrdtLogState = void 0; const rxjs_1 = require("rxjs"); const util_1 = require("sonic-forest/lib/util"); const JsonCrdtPatchState_1 = require("../JsonCrdtPatch/JsonCrdtPatchState"); const JsonCrdtModelState_1 = require("../JsonCrdtModel/JsonCrdtModelState"); class JsonCrdtLogState { constructor(log, { view = 'timeline' } = {}) { this.log = log; this.pinned$ = new rxjs_1.BehaviorSubject(null); this.pinnedModel$ = new rxjs_1.BehaviorSubject(null); this.timelineScroll$ = new rxjs_1.BehaviorSubject(1); this.readonlyEnforced$ = new rxjs_1.BehaviorSubject(0); this.patchState = new JsonCrdtPatchState_1.JsonCrdtPatchState(); this.modelState = new JsonCrdtModelState_1.JsonCrdtModelState(); this.setView = (view) => { this.view$.next(view); }; this.pin = (patch) => { if (!patch) { this.pinned$.next(null); this.pinnedModel$.next(null); return; } let model; if (patch === 'start') { this.pinned$.next('start'); model = this.log.start(); } else { const id = patch?.getId(); if (!id) return; this.pinned$.next(patch); model = this.log.replayTo(id); } const makeReadOnly = (model) => { const unsubscribe = model.api.onBeforeLocalChange.listen(() => { unsubscribe(); this.readonlyEnforced$.next(this.readonlyEnforced$.getValue() + 1); const model = patch === 'start' ? this.log.start() : this.log.replayTo(patch.getId()); makeReadOnly(model); this.pinnedModel$.next(model); }); }; makeReadOnly(model); this.pinnedModel$.next(model); }; this.next = () => { const pinned = this.pinned$.getValue(); if (pinned === 'start') { const node = this.log.patches.first(); if (node) this.pin(node.v); return; } const pinnedId = pinned?.getId(); if (!pinnedId) return; const node = this.log.patches.find(pinnedId); if (!node) return; const prevNode = (0, util_1.next)(node); if (prevNode) this.pin(prevNode.v); }; this.prev = () => { const pinned = this.pinned$.getValue(); if (pinned === 'start') return; const pinnedId = pinned?.getId(); if (!pinnedId) return; const node = this.log.patches.find(pinnedId); if (!node) return; const prevNode = (0, util_1.prev)(node); if (prevNode) this.pin(prevNode.v); else this.pin('start'); }; this.pause = () => { const node = (0, util_1.last)(this.log.patches.root); if (node) this.pin(node.v); }; this.play = () => { this.pin(null); this.setTimelineScroll(1); }; this.togglePlay = () => { const pinnedModel = this.pinnedModel$.getValue(); if (pinnedModel) this.play(); else this.pause(); }; this.setTimelineScroll = (scroll) => { this.timelineScroll$.next(scroll); }; this.view$ = new rxjs_1.BehaviorSubject(view); } } exports.JsonCrdtLogState = JsonCrdtLogState;