diff --git a/core/block/editor/clipboard/clipboard_test.go b/core/block/editor/clipboard/clipboard_test.go index 6309af6b1..077cfe3c5 100644 --- a/core/block/editor/clipboard/clipboard_test.go +++ b/core/block/editor/clipboard/clipboard_test.go @@ -141,6 +141,13 @@ func TestCommonSmart_pasteAny(t *testing.T) { checkBlockText(t, sb, []string{"11111", "22222", "aaaaa", "bbbbb", "55555"}) }) + t.Run("9. Save id of focused block", func(t *testing.T) { + sb := createPage(t, createBlocks([]string{}, []string{"11111", "22222", "33333", "44444", "55555"}, emptyMarks)) + pasteAny(t, sb, "4", model.Range{}, []string{}, createBlocks([]string{"new1", "new2"}, []string{"aaaaa", "bbbbb"}, emptyMarks)) + checkBlockText(t, sb, []string{"11111", "22222", "33333", "aaaaa", "bbbbb", "44444", "55555"}) + assert.Equal(t, sb.Blocks()[5].Id, "4") + }) + } func TestCommonSmart_splitMarks(t *testing.T) { diff --git a/core/block/editor/clipboard/paste.go b/core/block/editor/clipboard/paste.go index b544b3506..29e12c5ed 100644 --- a/core/block/editor/clipboard/paste.go +++ b/core/block/editor/clipboard/paste.go @@ -3,6 +3,8 @@ package clipboard import ( "strings" + "github.com/samber/lo" + "github.com/anytypeio/go-anytype-middleware/core/block/editor/state" "github.com/anytypeio/go-anytype-middleware/core/block/editor/template" "github.com/anytypeio/go-anytype-middleware/core/block/simple" @@ -66,7 +68,7 @@ func (p *pasteCtrl) Exec(req *pb.RpcBlockPasteRequest) (err error) { p.normalize() if p.mode.singleRange && req.FocusedBlockId != "" { - p.setTargetIdForLastBlock(req.FocusedBlockId) + p.restoreFocusedBlockId(req.FocusedBlockId) } p.processFiles() @@ -345,13 +347,33 @@ func (p *pasteCtrl) intoCodeBlock() (err error) { return err } -func (p *pasteCtrl) setTargetIdForLastBlock(target string) { - root := p.s.Get(p.s.RootId()) - rootChildren := root.Model().ChildrenIds - lastBlockId := rootChildren[len(rootChildren)-1] - root.Model().ChildrenIds = append(rootChildren[:len(rootChildren)-1], target) +// TODO: GO-1394 Changing id of new block to old one conflicts the idea of changes and multiplatform. Needs redesign +func (p *pasteCtrl) restoreFocusedBlockId(target string) { + isTargetFound := false + p.s.Iterate(func(b simple.Block) (isContinue bool) { + if b.Model().Id == target { + isTargetFound = true + return false + } + return true + }) - lastBlock := p.s.Get(lastBlockId) + if isTargetFound { + return + } + + lastPasteText := p.getLastPasteText() + p.caretPos = int32(len(lastPasteText.GetText())) + lastPasteTextId := lastPasteText.Model().Id + lastBlock := p.s.Get(lastPasteTextId) lastBlock.Model().Id = target p.s.Set(lastBlock) + + p.s.Iterate(func(b simple.Block) (isContinue bool) { + if lo.Contains(b.Model().ChildrenIds, lastPasteTextId) { + b.Model().ChildrenIds = lo.Replace(b.Model().ChildrenIds, lastPasteTextId, target, 1) + return false + } + return true + }) } diff --git a/core/block/editor/clipboard/paste_test.go b/core/block/editor/clipboard/paste_test.go deleted file mode 100644 index 84c31e8cf..000000000 --- a/core/block/editor/clipboard/paste_test.go +++ /dev/null @@ -1,66 +0,0 @@ -package clipboard - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/anytypeio/go-anytype-middleware/core/block/editor/state" - "github.com/anytypeio/go-anytype-middleware/core/block/simple" - "github.com/anytypeio/go-anytype-middleware/core/block/simple/base" - "github.com/anytypeio/go-anytype-middleware/core/block/simple/text" - "github.com/anytypeio/go-anytype-middleware/pb" - "github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model" -) - -const newText = "brand new text" - -func TestPasteCtrl_Exec(t *testing.T) { - t.Run("Single range. Last block has target id", func(t *testing.T) { - - s := state.NewDoc("root1", map[string]simple.Block{ - "root1": base.NewBase(&model.Block{ - Id: "root1", - ChildrenIds: []string{"1"}, - }), - "1": text.NewText(&model.Block{ - Id: "1", - Content: &model.BlockContentOfText{ - Text: &model.BlockContentText{ - Text: "", - }, - }, - }), - }).NewState() - ps := state.NewDoc("root2", map[string]simple.Block{}).NewState() - ps.Add(base.NewBase(&model.Block{ - Id: "root2", - ChildrenIds: []string{"2"}, - })) - ps.Add(text.NewText(&model.Block{ - Id: "2", - Content: &model.BlockContentOfText{ - Text: &model.BlockContentText{ - Text: newText, - }, - }, - })) - ctrl := pasteCtrl{ - s: s, - ps: ps, - } - - assert.NoError(t, ctrl.Exec(&pb.RpcBlockPasteRequest{ - FocusedBlockId: "1", - IsPartOfBlock: false, - TextSlot: newText, - })) - - b := s.Get("1") - assert.NotNil(t, b) - - txt, _ := b.Model().Content.(*model.BlockContentOfText) - assert.NotNil(t, txt) - assert.Equal(t, txt.Text.Text, newText) - }) -}