1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 09:35:00 +09:00

GO-1265 Fix search of focused block

This commit is contained in:
kirillston 2023-04-25 18:17:59 +02:00 committed by Mikhail Iudin
parent ff4953f0ca
commit 384d9bea4f
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
3 changed files with 36 additions and 73 deletions

View file

@ -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) {

View file

@ -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
})
}

View file

@ -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)
})
}