import { } from "@fluidframework/core-interfaces" ; import { ICombiningOp , IMergeTreeInsertMsg , IMergeTreeRemoveMsg , IRelativePosition , ISegment , Marker , MergeTreeTextHelper , PropertySet , ReferencePosition , ReferenceType , TextSegment , } from "@fluidframework/merge-tree" ; import { IFluidDataStoreRuntime , IChannelAttributes } from "@fluidframework/datastore-definitions" ; import { SharedSegmentSequence } from "./sequence" ; import { SharedStringFactory } from "./sequenceFactory" ; /**
 * Fluid object interface describing access methods on a SharedString
 */ export interface ISharedString extends SharedSegmentSequence < SharedStringSegment > { param pos param text param props insertText ( pos , text , props ? ) : void ; param pos param refType param props insertMarker ( pos , refType , props ? ) : IMergeTreeInsertMsg ; /**
     * {@inheritDoc SharedSegmentSequence.posFromRelativePos}
     */ posFromRelativePos ( relativePos ) : number ; } export type SharedStringSegment = TextSegment | Marker ; /**
 * The Shared String is a specialized data structure for handling collaborative
 * text. It is based on a more general Sequence data structure but has
 * additional features that make working with text easier.
 *
 * In addition to text, a Shared String can also contain markers. Markers can be
 * used to store metadata at positions within the text, like the details of an
 * image or Fluid object that should be rendered with the text.
 *
 */ export class SharedString extends SharedSegmentSequence < SharedStringSegment > implements ISharedString { param runtime param id returns public static create ( runtime , id ? ) { return runtime . createChannel ( id , SharedStringFactory . Type ) as SharedString ; } returns public static getFactory ( ) { return new SharedStringFactory ( ) ; } public get ISharedString ( ) : ISharedString { return this ; } private readonly mergeTreeTextHelper : MergeTreeTextHelper ; constructor ( document , public id , attributes ) { super ( document , id , attributes , SharedStringFactory . segmentFromSpec ) ; this . mergeTreeTextHelper = this . client . createTextHelper ( ) ; } param relativePos1 param refType param props public insertMarkerRelative ( relativePos1 , refType , props ? ) { const segment = new Marker ( refType ) ; if ( props ) { segment . addProperties ( props ) ; } const pos = this . posFromRelativePos ( relativePos1 ) ; const insertOp = this . client . insertSegmentLocal ( pos , segment ) ; if ( insertOp ) { this . submitSequenceMessage ( insertOp ) ; } } /**
     * {@inheritDoc ISharedString.insertMarker}
     */ public insertMarker ( pos , refType , props ? ) { const segment = new Marker ( refType ) ; if ( props ) { segment . addProperties ( props ) ; } const insertOp = this . client . insertSegmentLocal ( pos , segment ) ; if ( insertOp ) { this . submitSequenceMessage ( insertOp ) ; } return insertOp ; } param relativePos1 param text param props public insertTextRelative ( relativePos1 , text , props ? ) { const segment = new TextSegment ( text ) ; if ( props ) { segment . addProperties ( props ) ; } const pos = this . posFromRelativePos ( relativePos1 ) ; const insertOp = this . client . insertSegmentLocal ( pos , segment ) ; if ( insertOp ) { this . submitSequenceMessage ( insertOp ) ; } } /**
     * {@inheritDoc ISharedString.insertText}
     */ public insertText ( pos , text , props ? ) { const segment = new TextSegment ( text ) ; if ( props ) { segment . addProperties ( props ) ; } const insertOp = this . client . insertSegmentLocal ( pos , segment ) ; if ( insertOp ) { this . submitSequenceMessage ( insertOp ) ; } } param start param end param text param props public replaceText ( start , end , text , props ? ) { this . replaceRange ( start , end , TextSegment . make ( text , props ) ) ; } param start param end returns public removeText ( start , end ) { return this . removeRange ( start , end ) ; } param marker param props param consensusCallback public annotateMarkerNotifyConsensus ( marker , props , callback ) { const annotateOp = this . client . annotateMarkerNotifyConsensus ( marker , props , callback ) ; if ( annotateOp ) { this . submitSequenceMessage ( annotateOp ) ; } } param marker param props param combiningOp public annotateMarker ( marker , props , combiningOp ? ) { const annotateOp = this . client . annotateMarker ( marker , props , combiningOp ) ; if ( annotateOp ) { this . submitSequenceMessage ( annotateOp ) ; } } public findTile ( startPos , tileLabel , preceding = true ) { return this . client . findTile ( startPos , tileLabel , preceding ) ; } public getTextAndMarkers ( label ) { const segmentWindow = this . client . getCollabWindow ( ) ; return this . mergeTreeTextHelper . getTextAndMarkers ( segmentWindow . currentSeq , segmentWindow . clientId , label ) ; } param start param end returns public getText ( start ? , end ? ) { const segmentWindow = this . client . getCollabWindow ( ) ; return this . mergeTreeTextHelper . getText ( segmentWindow . currentSeq , segmentWindow . clientId , "" , start , end ) ; } /**
     * Adds spaces for markers and handles, so that position calculations account for them.
     */ public getTextWithPlaceholders ( ) { const segmentWindow = this . client . getCollabWindow ( ) ; return this . mergeTreeTextHelper . getText ( segmentWindow . currentSeq , segmentWindow . clientId , " " ) ; } public getTextRangeWithPlaceholders ( start , end ) { const segmentWindow = this . client . getCollabWindow ( ) ; return this . mergeTreeTextHelper . getText ( segmentWindow . currentSeq , segmentWindow . clientId , " " , start , end ) ; } public getTextRangeWithMarkers ( start , end ) { const segmentWindow = this . client . getCollabWindow ( ) ; return this . mergeTreeTextHelper . getText ( segmentWindow . currentSeq , segmentWindow . clientId , "*" , start , end ) ; } public getMarkerFromId ( id ) { return this . client . getMarkerFromId ( id ) ; } }