Skip to content

Commit

Permalink
Merge pull request #1420 from anyproto/go-3788-cross-spce-file-dnd
Browse files Browse the repository at this point in the history
GO-3788 Create FileObject on DnD file to other space
  • Loading branch information
KirillSto authored Jul 29, 2024
2 parents 44581c7 + ff0042b commit 000f61a
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 69 deletions.
45 changes: 37 additions & 8 deletions core/block/editor/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
relationblock "github.com/anyproto/anytype-heart/core/block/simple/relation"
"github.com/anyproto/anytype-heart/core/block/simple/text"
"github.com/anyproto/anytype-heart/core/domain"
"github.com/anyproto/anytype-heart/core/domain/objectorigin"
"github.com/anyproto/anytype-heart/core/files/fileobject"
"github.com/anyproto/anytype-heart/core/session"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
Expand Down Expand Up @@ -103,19 +105,22 @@ func NewBasic(
sb smartblock.SmartBlock,
objectStore objectstore.ObjectStore,
layoutConverter converter.LayoutConverter,
fileObjectService fileobject.Service,
) AllOperations {
return &basic{
SmartBlock: sb,
objectStore: objectStore,
layoutConverter: layoutConverter,
SmartBlock: sb,
objectStore: objectStore,
layoutConverter: layoutConverter,
fileObjectService: fileObjectService,
}
}

type basic struct {
smartblock.SmartBlock

objectStore objectstore.ObjectStore
layoutConverter converter.LayoutConverter
objectStore objectstore.ObjectStore
layoutConverter converter.LayoutConverter
fileObjectService fileobject.Service
}

func (bs *basic) CreateBlock(s *state.State, req pb.RpcBlockCreateRequest) (id string, err error) {
Expand Down Expand Up @@ -149,7 +154,7 @@ func (bs *basic) CreateBlock(s *state.State, req pb.RpcBlockCreateRequest) (id s
func (bs *basic) Duplicate(srcState, destState *state.State, targetBlockId string, position model.BlockPosition, blockIds []string) (newIds []string, err error) {
blockIds = srcState.SelectRoots(blockIds)
for _, id := range blockIds {
copyId, e := copyBlocks(srcState, destState, id)
copyId, e := bs.copyBlocks(srcState, destState, id)
if e != nil {
return nil, e
}
Expand All @@ -169,7 +174,7 @@ type duplicatable interface {
Duplicate(s *state.State) (newId string, visitedIds []string, blocks []simple.Block, err error)
}

func copyBlocks(srcState, destState *state.State, sourceId string) (id string, err error) {
func (bs *basic) copyBlocks(srcState, destState *state.State, sourceId string) (id string, err error) {
b := srcState.Pick(sourceId)
if b == nil {
return "", smartblock.ErrSimpleBlockNotFound
Expand All @@ -190,13 +195,37 @@ func copyBlocks(srcState, destState *state.State, sourceId string) (id string, e
result := simple.New(m)
destState.Add(result)
for i, childrenId := range result.Model().ChildrenIds {
if result.Model().ChildrenIds[i], err = copyBlocks(srcState, destState, childrenId); err != nil {
if result.Model().ChildrenIds[i], err = bs.copyBlocks(srcState, destState, childrenId); err != nil {
return
}
}

if f, ok := result.Model().Content.(*model.BlockContentOfFile); ok && srcState.SpaceID() != destState.SpaceID() {
bs.processFileBlock(f, destState.SpaceID())
}

return result.Model().Id, nil
}

func (bs *basic) processFileBlock(f *model.BlockContentOfFile, spaceId string) {
fileId, err := bs.fileObjectService.GetFileIdFromObject(f.File.TargetObjectId)
if err != nil {
log.Errorf("failed to get fileId: %v", err)
return
}

objectId, err := bs.fileObjectService.CreateFromImport(
domain.FullFileId{SpaceId: spaceId, FileId: fileId.FileId},
objectorigin.ObjectOrigin{Origin: model.ObjectOrigin_clipboard},
)
if err != nil {
log.Errorf("failed to create file object: %v", err)
return
}

f.File.TargetObjectId = objectId
}

func (bs *basic) Unlink(ctx session.Context, ids ...string) (err error) {
s := bs.NewStateCtx(ctx)

Expand Down
Loading

0 comments on commit 000f61a

Please sign in to comment.