From f571e720969db8c8574a6f3fd9a0e55a72d140c0 Mon Sep 17 00:00:00 2001 From: joonhyukchoi Date: Tue, 1 Aug 2023 11:07:27 +0900 Subject: [PATCH] Remove Select operation from Text (#589) * Remove Select operation from Text * Retaining the order of protobuf message --- api/converter/converter_test.go | 3 +- api/converter/from_pb.go | 27 --------- api/converter/to_pb.go | 13 ----- pkg/document/crdt/rga_tree_split.go | 15 ----- pkg/document/crdt/text.go | 13 ----- pkg/document/json/text.go | 26 --------- pkg/document/operations/select.go | 91 ----------------------------- 7 files changed, 1 insertion(+), 187 deletions(-) delete mode 100644 pkg/document/operations/select.go diff --git a/api/converter/converter_test.go b/api/converter/converter_test.go index c92b047bc..f7477adb0 100644 --- a/api/converter/converter_test.go +++ b/api/converter/converter_test.go @@ -173,8 +173,7 @@ func TestConverter(t *testing.T) { Edit(0, 1, "한"). Edit(0, 1, "하"). Edit(1, 1, "느"). - Edit(1, 2, "늘"). - Select(1, 2) + Edit(1, 2, "늘") // rich text root.SetNewText("k3"). diff --git a/api/converter/from_pb.go b/api/converter/from_pb.go index 7295103be..ac4d6effa 100644 --- a/api/converter/from_pb.go +++ b/api/converter/from_pb.go @@ -260,8 +260,6 @@ func FromOperations(pbOps []*api.Operation) ([]operations.Operation, error) { op, err = fromRemove(decoded.Remove) case *api.Operation_Edit_: op, err = fromEdit(decoded.Edit) - case *api.Operation_Select_: - op, err = fromSelect(decoded.Select) case *api.Operation_Style_: op, err = fromStyle(decoded.Style) case *api.Operation_Increase_: @@ -421,31 +419,6 @@ func fromRemove(pbRemove *api.Operation_Remove) (*operations.Remove, error) { ), nil } -func fromSelect(pbSelect *api.Operation_Select) (*operations.Select, error) { - parentCreatedAt, err := fromTimeTicket(pbSelect.ParentCreatedAt) - if err != nil { - return nil, err - } - from, err := fromTextNodePos(pbSelect.From) - if err != nil { - return nil, err - } - to, err := fromTextNodePos(pbSelect.To) - if err != nil { - return nil, err - } - executedAt, err := fromTimeTicket(pbSelect.ExecutedAt) - if err != nil { - return nil, err - } - return operations.NewSelect( - parentCreatedAt, - from, - to, - executedAt, - ), nil -} - func fromEdit(pbEdit *api.Operation_Edit) (*operations.Edit, error) { parentCreatedAt, err := fromTimeTicket(pbEdit.ParentCreatedAt) if err != nil { diff --git a/api/converter/to_pb.go b/api/converter/to_pb.go index 0867437e5..48362c6a1 100644 --- a/api/converter/to_pb.go +++ b/api/converter/to_pb.go @@ -245,8 +245,6 @@ func ToOperations(ops []operations.Operation) ([]*api.Operation, error) { pbOperation.Body, err = toRemove(op) case *operations.Edit: pbOperation.Body, err = toEdit(op) - case *operations.Select: - pbOperation.Body, err = toSelect(op) case *operations.Style: pbOperation.Body, err = toStyle(op) case *operations.Increase: @@ -368,17 +366,6 @@ func toEdit(e *operations.Edit) (*api.Operation_Edit_, error) { }, nil } -func toSelect(s *operations.Select) (*api.Operation_Select_, error) { - return &api.Operation_Select_{ - Select: &api.Operation_Select{ - ParentCreatedAt: ToTimeTicket(s.ParentCreatedAt()), - From: toTextNodePos(s.From()), - To: toTextNodePos(s.To()), - ExecutedAt: ToTimeTicket(s.ExecutedAt()), - }, - }, nil -} - func toStyle(style *operations.Style) (*api.Operation_Style_, error) { return &api.Operation_Style_{ Style: &api.Operation_Style{ diff --git a/pkg/document/crdt/rga_tree_split.go b/pkg/document/crdt/rga_tree_split.go index 640e0a33c..cabb68b0d 100644 --- a/pkg/document/crdt/rga_tree_split.go +++ b/pkg/document/crdt/rga_tree_split.go @@ -144,21 +144,6 @@ func (pos *RGATreeSplitNodePos) Equal(other *RGATreeSplitNodePos) bool { return pos.relativeOffset == other.relativeOffset } -// Selection represents the selection of text range in the editor. -type Selection struct { - from *RGATreeSplitNodePos - to *RGATreeSplitNodePos - updatedAt *time.Ticket -} - -func newSelection(from, to *RGATreeSplitNodePos, updatedAt *time.Ticket) *Selection { - return &Selection{ - from, - to, - updatedAt, - } -} - // RGATreeSplitNode is a node of RGATreeSplit. type RGATreeSplitNode[V RGATreeSplitValue] struct { id *RGATreeSplitNodeID diff --git a/pkg/document/crdt/text.go b/pkg/document/crdt/text.go index 45caab38a..148f3d959 100644 --- a/pkg/document/crdt/text.go +++ b/pkg/document/crdt/text.go @@ -116,7 +116,6 @@ func InitialTextNode() *RGATreeSplitNode[*TextValue] { // Text is an extended data type for the contents of a text editor. type Text struct { rgaTreeSplit *RGATreeSplit[*TextValue] - selectionMap map[string]*Selection createdAt *time.Ticket movedAt *time.Ticket removedAt *time.Ticket @@ -126,7 +125,6 @@ type Text struct { func NewText(elements *RGATreeSplit[*TextValue], createdAt *time.Ticket) *Text { return &Text{ rgaTreeSplit: elements, - selectionMap: make(map[string]*Selection), createdAt: createdAt, } } @@ -276,17 +274,6 @@ func (t *Text) Style( return nil } -// Select stores that the given range has been selected. -func (t *Text) Select( - from *RGATreeSplitNodePos, - to *RGATreeSplitNodePos, - executedAt *time.Ticket, -) { - if prev, ok := t.selectionMap[executedAt.ActorIDHex()]; !ok || executedAt.After(prev.updatedAt) { - t.selectionMap[executedAt.ActorIDHex()] = newSelection(from, to, executedAt) - } -} - // Nodes returns the internal nodes of this Text. func (t *Text) Nodes() []*RGATreeSplitNode[*TextValue] { return t.rgaTreeSplit.nodes() diff --git a/pkg/document/json/text.go b/pkg/document/json/text.go index 7129ba82c..abac1becc 100644 --- a/pkg/document/json/text.go +++ b/pkg/document/json/text.go @@ -122,29 +122,3 @@ func (p *Text) Style(from, to int, attributes map[string]string) *Text { return p } - -// Select stores that the given range has been selected. -func (p *Text) Select(from, to int) *Text { - if from > to { - panic("from should be less than or equal to to") - } - fromPos, toPos, err := p.Text.CreateRange(from, to) - if err != nil { - panic(err) - } - - ticket := p.context.IssueTimeTicket() - p.Text.Select( - fromPos, - toPos, - ticket, - ) - - p.context.Push(operations.NewSelect( - p.CreatedAt(), - fromPos, - toPos, - ticket, - )) - return p -} diff --git a/pkg/document/operations/select.go b/pkg/document/operations/select.go deleted file mode 100644 index 0825dcbd1..000000000 --- a/pkg/document/operations/select.go +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2020 The Yorkie Authors. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package operations - -import ( - "github.com/yorkie-team/yorkie/pkg/document/crdt" - "github.com/yorkie-team/yorkie/pkg/document/time" -) - -// Select represents an operation that selects an area in the text. -type Select struct { - // parentCreatedAt is the creation time of the Text that executes Select. - parentCreatedAt *time.Ticket - - // from represents the start point of the selection. - from *crdt.RGATreeSplitNodePos - - // to represents the end point of the selection. - to *crdt.RGATreeSplitNodePos - - // executedAt is the time the operation was executed. - executedAt *time.Ticket -} - -// NewSelect creates a new instance of Select. -func NewSelect( - parentCreatedAt *time.Ticket, - from *crdt.RGATreeSplitNodePos, - to *crdt.RGATreeSplitNodePos, - executedAt *time.Ticket, -) *Select { - return &Select{ - parentCreatedAt: parentCreatedAt, - from: from, - to: to, - executedAt: executedAt, - } -} - -// Execute executes this operation on the given document(`root`). -func (s *Select) Execute(root *crdt.Root) error { - parent := root.FindByCreatedAt(s.parentCreatedAt) - - switch obj := parent.(type) { - case *crdt.Text: - obj.Select(s.from, s.to, s.executedAt) - default: - return ErrNotApplicableDataType - } - - return nil -} - -// From returns the start point of the selection. -func (s *Select) From() *crdt.RGATreeSplitNodePos { - return s.from -} - -// To returns the end point of the selection. -func (s *Select) To() *crdt.RGATreeSplitNodePos { - return s.to -} - -// ExecutedAt returns execution time of this operation. -func (s *Select) ExecutedAt() *time.Ticket { - return s.executedAt -} - -// SetActor sets the given actor to this operation. -func (s *Select) SetActor(actorID *time.ActorID) { - s.executedAt = s.executedAt.SetActorID(actorID) -} - -// ParentCreatedAt returns the creation time of the Text. -func (s *Select) ParentCreatedAt() *time.Ticket { - return s.parentCreatedAt -}