UNPKG

collaborative-ui

Version:

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

48 lines (47 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StrAdapterNative = void 0; const tslib_1 = require("tslib"); const React = tslib_1.__importStar(require("react")); const ReplicatedStr_1 = require("collaborative-editor/lib/replicated-str/ReplicatedStr"); const fanout_1 = require("thingies/lib/fanout"); class StrFacade { constructor(value = '') { this.value = value; this.onChange = new fanout_1.FanOut(); this.view = () => this.value; this.set = (value) => { if (value === this.value) return; this.value = value; this.onChange.emit(value); }; this.ins = (pos, text) => { const value = this.value; const next = value.slice(0, pos) + text + value.slice(pos); this.set(next); }; this.del = (pos, length) => { const value = this.value; const next = value.slice(0, pos) + value.slice(pos + length); this.set(next); }; this.subscribe = (listener) => { return this.onChange.listen(() => listener()); }; } } const StrAdapterNative = (props) => { // biome-ignore lint: manually managed dependency list const [facade, str] = React.useMemo(() => { const facade = new StrFacade(props.value); const str = new ReplicatedStr_1.ReplicatedStr(facade); facade.onChange.listen((value) => { props.onChange(value); }); return [facade, () => str]; }, []); facade.set(props.value); return props.children(str); }; exports.StrAdapterNative = StrAdapterNative;