Skip to content

Commit

Permalink
Remove select operation from text (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
blurfx authored Aug 21, 2023
1 parent 5523983 commit 808d1f7
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 244 deletions.
31 changes: 5 additions & 26 deletions src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { AddOperation } from '@yorkie-js-sdk/src/document/operation/add_operatio
import { MoveOperation } from '@yorkie-js-sdk/src/document/operation/move_operation';
import { RemoveOperation } from '@yorkie-js-sdk/src/document/operation/remove_operation';
import { EditOperation } from '@yorkie-js-sdk/src/document/operation/edit_operation';
import { SelectOperation } from '@yorkie-js-sdk/src/document/operation/select_operation';
import { StyleOperation } from '@yorkie-js-sdk/src/document/operation/style_operation';
import { TreeEditOperation } from '@yorkie-js-sdk/src/document/operation/tree_edit_operation';
import { ChangeID } from '@yorkie-js-sdk/src/document/change/change_id';
Expand Down Expand Up @@ -62,24 +61,24 @@ import {
ChangeID as PbChangeID,
ChangePack as PbChangePack,
Checkpoint as PbCheckpoint,
Presence as PbPresence,
PresenceChange as PbPresenceChange,
Snapshot as PbSnapshot,
JSONElement as PbJSONElement,
JSONElementSimple as PbJSONElementSimple,
NodeAttr as PbNodeAttr,
Operation as PbOperation,
Presence as PbPresence,
PresenceChange as PbPresenceChange,
RGANode as PbRGANode,
RHTNode as PbRHTNode,
Snapshot as PbSnapshot,
TextNode as PbTextNode,
TextNodeID as PbTextNodeID,
TextNodePos as PbTextNodePos,
NodeAttr as PbNodeAttr,
TimeTicket as PbTimeTicket,
ValueType as PbValueType,
TreeNode as PbTreeNode,
TreeNodes as PbTreeNodes,
TreePos as PbTreePos,
TreeNodeID as PbTreeNodeID,
ValueType as PbValueType,
} from '@yorkie-js-sdk/src/api/yorkie/v1/resources_pb';
import { IncreaseOperation } from '@yorkie-js-sdk/src/document/operation/increase_operation';
import {
Expand Down Expand Up @@ -350,18 +349,6 @@ function toOperation(operation: Operation): PbOperation {
}
pbEditOperation.setExecutedAt(toTimeTicket(editOperation.getExecutedAt()));
pbOperation.setEdit(pbEditOperation);
} else if (operation instanceof SelectOperation) {
const selectOperation = operation as SelectOperation;
const pbSelectOperation = new PbOperation.Select();
pbSelectOperation.setParentCreatedAt(
toTimeTicket(selectOperation.getParentCreatedAt()),
);
pbSelectOperation.setFrom(toTextNodePos(selectOperation.getFromPos()));
pbSelectOperation.setTo(toTextNodePos(selectOperation.getToPos()));
pbSelectOperation.setExecutedAt(
toTimeTicket(selectOperation.getExecutedAt()),
);
pbOperation.setSelect(pbSelectOperation);
} else if (operation instanceof StyleOperation) {
const styleOperation = operation as StyleOperation;
const pbStyleOperation = new PbOperation.Style();
Expand Down Expand Up @@ -1065,14 +1052,6 @@ function fromOperations(pbOperations: Array<PbOperation>): Array<Operation> {
fromTimeTicket(pbRemoveOperation!.getCreatedAt())!,
fromTimeTicket(pbRemoveOperation!.getExecutedAt())!,
);
} else if (pbOperation.hasSelect()) {
const pbSelectOperation = pbOperation.getSelect();
operation = SelectOperation.create(
fromTimeTicket(pbSelectOperation!.getParentCreatedAt())!,
fromTextNodePos(pbSelectOperation!.getFrom()!),
fromTextNodePos(pbSelectOperation!.getTo()!),
fromTimeTicket(pbSelectOperation!.getExecutedAt())!,
);
} else if (pbOperation.hasEdit()) {
const pbEditOperation = pbOperation.getEdit();
const createdAtMapByActor = new Map();
Expand Down
37 changes: 1 addition & 36 deletions src/document/crdt/rga_tree_split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface ValueChange<T> {

interface RGATreeSplitValue {
length: number;

substring(indexStart: number, indexEnd?: number): RGATreeSplitValue;
}

Expand Down Expand Up @@ -1036,39 +1037,3 @@ export class RGATreeSplit<T extends RGATreeSplitValue> {
node.setInsNext(undefined);
}
}

/**
* `Selection` represents the selection of text range in the editor.
*/
export class Selection {
private from: RGATreeSplitPos;
private to: RGATreeSplitPos;
private updatedAt: TimeTicket;

constructor(
from: RGATreeSplitPos,
to: RGATreeSplitPos,
updatedAt: TimeTicket,
) {
this.from = from;
this.to = to;
this.updatedAt = updatedAt;
}

/**
* `of` creates a new instance of Selection.
*/
public static of(
range: RGATreeSplitPosRange,
updatedAt: TimeTicket,
): Selection {
return new Selection(range[0], range[1], updatedAt);
}

/**
* `getUpdatedAt` returns update time of this selection.
*/
public getUpdatedAt(): TimeTicket {
return this.updatedAt;
}
}
39 changes: 1 addition & 38 deletions src/document/crdt/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { CRDTGCElement } from '@yorkie-js-sdk/src/document/crdt/element';
import {
RGATreeSplit,
RGATreeSplitPosRange,
Selection,
ValueChange,
} from '@yorkie-js-sdk/src/document/crdt/rga_tree_split';
import { escapeString } from '@yorkie-js-sdk/src/document/json/strings';
Expand All @@ -34,7 +33,6 @@ import { parseObjectValues } from '@yorkie-js-sdk/src/util/object';
*/
enum TextChangeType {
Content = 'content',
Selection = 'selection',
Style = 'style',
}

Expand All @@ -49,7 +47,7 @@ export interface TextValueType<A> {

/**
* `TextChange` represents the changes to the text
* when executing the edit, setstyle, and select methods.
* when executing the edit, setstyle methods.
*/
interface TextChange<A = Indexable> extends ValueChange<TextValueType<A>> {
type: TextChangeType;
Expand Down Expand Up @@ -162,15 +160,13 @@ export class CRDTTextValue {
*/
export class CRDTText<A extends Indexable = Indexable> extends CRDTGCElement {
private rgaTreeSplit: RGATreeSplit<CRDTTextValue>;
private selectionMap: Map<string, Selection>;

constructor(
rgaTreeSplit: RGATreeSplit<CRDTTextValue>,
createdAt: TimeTicket,
) {
super(createdAt);
this.rgaTreeSplit = rgaTreeSplit;
this.selectionMap = new Map();
}

/**
Expand Down Expand Up @@ -277,18 +273,6 @@ export class CRDTText<A extends Indexable = Indexable> extends CRDTGCElement {
return changes;
}

/**
* `select` stores that the given range has been selected.
*
* @internal
*/
public select(
range: RGATreeSplitPosRange,
updatedAt: TimeTicket,
): TextChange<A> | undefined {
return this.selectPriv(range, updatedAt);
}

/**
* `indexRangeToPosRange` returns the position range of the given index range.
*/
Expand Down Expand Up @@ -418,25 +402,4 @@ export class CRDTText<A extends Indexable = Indexable> extends CRDTGCElement {
public findIndexesFromRange(range: RGATreeSplitPosRange): [number, number] {
return this.rgaTreeSplit.findIndexesFromRange(range);
}

private selectPriv(
range: RGATreeSplitPosRange,
updatedAt: TimeTicket,
): TextChange<A> | undefined {
const prevSelection = this.selectionMap.get(updatedAt.getActorID()!);
if (!prevSelection || updatedAt.after(prevSelection!.getUpdatedAt())) {
this.selectionMap.set(
updatedAt.getActorID()!,
Selection.of(range, updatedAt),
);

const [from, to] = this.rgaTreeSplit.findIndexesFromRange(range);
return {
type: TextChangeType.Selection,
actor: updatedAt.getActorID()!,
from,
to,
};
}
}
}
28 changes: 1 addition & 27 deletions src/document/json/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import {
} from '@yorkie-js-sdk/src/document/time/ticket';
import { ChangeContext } from '@yorkie-js-sdk/src/document/change/context';
import {
RGATreeSplitPosRange,
RGATreeSplitPos,
RGATreeSplitPosRange,
} from '@yorkie-js-sdk/src/document/crdt/rga_tree_split';
import { CRDTText, TextValueType } from '@yorkie-js-sdk/src/document/crdt/text';
import { EditOperation } from '@yorkie-js-sdk/src/document/operation/edit_operation';
import { StyleOperation } from '@yorkie-js-sdk/src/document/operation/style_operation';
import { SelectOperation } from '@yorkie-js-sdk/src/document/operation/select_operation';
import { stringifyObjectValues } from '@yorkie-js-sdk/src/util/object';

/**
Expand Down Expand Up @@ -181,31 +180,6 @@ export class Text<A extends Indexable = Indexable> {
return true;
}

/**
* `select` selects the given range.
*/
select(fromIdx: number, toIdx: number): boolean {
if (!this.context || !this.text) {
logger.fatal('it is not initialized yet');
return false;
}

const range = this.text.indexRangeToPosRange(fromIdx, toIdx);
if (logger.isEnabled(LogLevel.Debug)) {
logger.debug(
`SELT: f:${fromIdx}->${range[0].toTestString()}, t:${toIdx}->${range[1].toTestString()}`,
);
}
const ticket = this.context.issueTimeTicket();
this.text.select(range, ticket);

this.context.push(
new SelectOperation(this.text.getCreatedAt(), range[0], range[1], ticket),
);

return true;
}

/**
* `indexRangeToPosRange` returns TextRangeStruct of the given index range.
*/
Expand Down
117 changes: 0 additions & 117 deletions src/document/operation/select_operation.ts

This file was deleted.

0 comments on commit 808d1f7

Please sign in to comment.