Skip to main content
Version: v1

SharedString Class

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.

Signature​

export declare class SharedString extends SharedSegmentSequence<SharedStringSegment> implements ISharedString

Extends: SharedSegmentSequence<SharedStringSegment

Implements: ISharedString

Constructors​

ConstructorDescription
(constructor)(document, id, attributes)Constructs a new instance of the SharedString class

Static Methods​

MethodReturn TypeDescription
create(runtime, id)SharedStringCreate a new shared string.
getFactory()SharedStringFactoryGet a factory for SharedString to register with the data store.

Properties​

PropertyTypeDescription
idstring
ISharedStringISharedString

Methods​

MethodReturn TypeDescription
annotateMarker(marker, props, combiningOp)voidAnnotates the marker with the provided properties.
annotateMarkerNotifyConsensus(marker, props, callback)voidAnnotates the marker with the provided properties and calls the callback on consensus.
findTile(startPos, tileLabel, preceding){ tile: ReferencePosition; pos: number; }
getMarkerFromId(id)ISegment
getText(start, end)stringRetrieve text from the SharedString in string format.
getTextAndMarkers(label){ parallelText: string[]; parallelMarkers: Marker[]; }
getTextRangeWithMarkers(start, end)string
getTextRangeWithPlaceholders(start, end)string
getTextWithPlaceholders()stringAdds spaces for markers and handles, so that position calculations account for them.
insertMarker(pos, refType, props)IMergeTreeInsertMsgInserts a marker at the position.
insertMarkerRelative(relativePos1, refType, props)voidInserts a marker at a relative position.
insertText(pos, text, props)voidInserts the text at the position.
insertTextRelative(relativePos1, text, props)voidInserts the text at the position.
removeText(start, end)IMergeTreeRemoveMsgRemoves the text in the given range.
replaceText(start, end, text, props)voidReplaces a range with the provided text.

Constructor Details​

(constructor)​

Constructs a new instance of the SharedString class

Signature​

constructor(document: IFluidDataStoreRuntime, id: string, attributes: IChannelAttributes);

Parameters​

ParameterTypeDescription
documentIFluidDataStoreRuntime
idstring
attributesIChannelAttributes

Property Details​

id​

Signature​

id: string;

Type: string

ISharedString​

Signature​

get ISharedString(): ISharedString;

Type: ISharedString

Method Details​

annotateMarker​

Annotates the marker with the provided properties.

Signature​

annotateMarker(marker: Marker, props: PropertySet, combiningOp?: ICombiningOp): void;

Parameters​

ParameterModifiersTypeDescription
markerMarkerThe marker to annotate
propsPropertySetThe properties to annotate the marker with
combiningOpoptionalICombiningOpOptional. Specifies how to combine values for the property, such as "incr" for increment.

annotateMarkerNotifyConsensus​

Annotates the marker with the provided properties and calls the callback on consensus.

Signature​

annotateMarkerNotifyConsensus(marker: Marker, props: PropertySet, callback: (m: Marker) => void): void;

Parameters​

ParameterTypeDescription
markerMarkerThe marker to annotate
propsPropertySetThe properties to annotate the marker with
callback(m: Marker) => void

create​

Create a new shared string.

Signature​

static create(runtime: IFluidDataStoreRuntime, id?: string): SharedString;

Parameters​

ParameterModifiersTypeDescription
runtimeIFluidDataStoreRuntimedata store runtime the new shared string belongs to
idoptionalstringoptional name of the shared string

Returns​

newly create shared string (but not attached yet)

Return type: SharedString

findTile​

Signature​

findTile(startPos: number | undefined, tileLabel: string, preceding?: boolean): {
tile: ReferencePosition;
pos: number;
};

Parameters​

ParameterModifiersTypeDescription
startPosnumber | undefined
tileLabelstring
precedingoptionalboolean

Returns​

Return type: { tile: ReferencePosition; pos: number; }

getFactory​

Get a factory for SharedString to register with the data store.

Signature​

static getFactory(): SharedStringFactory;

Returns​

a factory that creates and load SharedString

Return type: SharedStringFactory

getMarkerFromId​

Signature​

getMarkerFromId(id: string): ISegment;

Parameters​

ParameterTypeDescription
idstring

Returns​

Return type: ISegment

getText​

Retrieve text from the SharedString in string format.

Signature​

getText(start?: number, end?: number): string;

Parameters​

ParameterModifiersTypeDescription
startoptionalnumberThe starting index of the text to retrieve, or 0 if omitted.
endoptionalnumberThe ending index of the text to retrieve, or the end of the string if omitted

Returns​

The requested text content as a string.

Return type: string

getTextAndMarkers​

Signature​

getTextAndMarkers(label: string): {
parallelText: string[];
parallelMarkers: Marker[];
};

Parameters​

ParameterTypeDescription
labelstring

Returns​

Return type: { parallelText: string[]; parallelMarkers: Marker[]; }

getTextRangeWithMarkers​

Signature​

getTextRangeWithMarkers(start: number, end: number): string;

Parameters​

ParameterTypeDescription
startnumber
endnumber

Returns​

Return type: string

getTextRangeWithPlaceholders​

Signature​

getTextRangeWithPlaceholders(start: number, end: number): string;

Parameters​

ParameterTypeDescription
startnumber
endnumber

Returns​

Return type: string

getTextWithPlaceholders​

Adds spaces for markers and handles, so that position calculations account for them.

Signature​

getTextWithPlaceholders(): string;

Returns​

Return type: string

insertMarker​

Inserts a marker at the position.

Signature​

insertMarker(pos: number, refType: ReferenceType, props?: PropertySet): IMergeTreeInsertMsg;

Parameters​

ParameterModifiersTypeDescription
posnumberThe position to insert the marker at
refTypeReferenceTypeThe reference type of the marker
propsoptionalPropertySetThe properties of the marker

Returns​

Return type: IMergeTreeInsertMsg

insertMarkerRelative​

Inserts a marker at a relative position.

Signature​

insertMarkerRelative(relativePos1: IRelativePosition, refType: ReferenceType, props?: PropertySet): void;

Parameters​

ParameterModifiersTypeDescription
relativePos1IRelativePositionThe relative position to insert the marker at
refTypeReferenceTypeThe reference type of the marker
propsoptionalPropertySetThe properties of the marker

insertText​

Inserts the text at the position.

Signature​

insertText(pos: number, text: string, props?: PropertySet): void;

Parameters​

ParameterModifiersTypeDescription
posnumberThe position to insert the text at
textstringThe text to insert
propsoptionalPropertySetThe properties of the text

insertTextRelative​

Inserts the text at the position.

Signature​

insertTextRelative(relativePos1: IRelativePosition, text: string, props?: PropertySet): void;

Parameters​

ParameterModifiersTypeDescription
relativePos1IRelativePositionThe relative position to insert the text at
textstringThe text to insert
propsoptionalPropertySetThe properties of text

removeText​

Removes the text in the given range.

Signature​

removeText(start: number, end: number): IMergeTreeRemoveMsg;

Parameters​

ParameterTypeDescription
startnumberThe inclusive start of the range to remove
endnumberThe exclusive end of the range to replace

Returns​

the message sent.

Return type: IMergeTreeRemoveMsg

replaceText​

Replaces a range with the provided text.

Signature​

replaceText(start: number, end: number, text: string, props?: PropertySet): void;

Parameters​

ParameterModifiersTypeDescription
startnumberThe inclusive start of the range to replace
endnumberThe exclusive end of the range to replace
textstringThe text to replace the range with
propsoptionalPropertySetOptional. The properties of the replacement text