From 0ac77145ae58c96f4ca582ee3c8b788c268b903a Mon Sep 17 00:00:00 2001 From: AnastasiaShemyakinskaya Date: Tue, 25 Apr 2023 18:17:59 +0200 Subject: [PATCH] GO-1367: migration for fixing imported notion pages --- core/block/editor/page.go | 43 ++++++++++++++++++++++++++------ core/block/editor/state/state.go | 8 +++--- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/core/block/editor/page.go b/core/block/editor/page.go index c43121921..44d1cbb49 100644 --- a/core/block/editor/page.go +++ b/core/block/editor/page.go @@ -14,8 +14,8 @@ import ( "github.com/anytypeio/go-anytype-middleware/core/block/editor/table" "github.com/anytypeio/go-anytype-middleware/core/block/editor/template" "github.com/anytypeio/go-anytype-middleware/core/block/migration" - "github.com/anytypeio/go-anytype-middleware/core/files" - "github.com/anytypeio/go-anytype-middleware/core/relation" + "github.com/anytypeio/go-anytype-middleware/core/block/simple" + relation2 "github.com/anytypeio/go-anytype-middleware/core/relation" "github.com/anytypeio/go-anytype-middleware/pkg/lib/bundle" "github.com/anytypeio/go-anytype-middleware/pkg/lib/core" "github.com/anytypeio/go-anytype-middleware/pkg/lib/localstore/objectstore" @@ -39,23 +39,22 @@ type Page struct { } func NewPage( - sb smartblock.SmartBlock, objectStore objectstore.ObjectStore, anytype core.Service, fileBlockService file.BlockService, bookmarkBlockService bookmark.BlockService, bookmarkService bookmark.BookmarkService, - relationService relation.Service, + relationService relation2.Service, tempDirProvider core.TempDirProvider, sbtProvider typeprovider.SmartBlockTypeProvider, layoutConverter converter.LayoutConverter, - fileService files.Service, ) *Page { + sb := smartblock.New() f := file.NewFile( sb, fileBlockService, + anytype, tempDirProvider, - fileService, ) return &Page{ SmartBlock: sb, @@ -69,9 +68,9 @@ func NewPage( Clipboard: clipboard.NewClipboard( sb, f, + anytype, tempDirProvider, relationService, - fileService, ), Bookmark: bookmark.NewBookmark( sb, @@ -166,7 +165,35 @@ func (p *Page) CreationStateMigration(ctx *smartblock.InitContext) migration.Mig } func (p *Page) StateMigrations() migration.Migrations { - return migration.MakeMigrations(nil) + blockMigration := cleanupEmptyBlockMigration() + return migration.MakeMigrations([]migration.Migration{blockMigration}) +} + +// cleanupEmptyBlockMigration is fixing existing pages, imported from Notion +func cleanupEmptyBlockMigration() migration.Migration { + return migration.Migration{ + Version: 2, + Proc: func(s *state.State) { + if s.Exists(s.RootId()) { + block := s.Get(s.RootId()) + if isBlockEmpty(block) { + newBlock := block.Copy() + newBlock.Model().Content = &model.BlockContentOfSmartblock{ + Smartblock: &model.BlockContentSmartblock{}, + } + s.Set(newBlock) + } + } + }, + } +} + +func isBlockEmpty(block simple.Block) bool { + if block.Model().Content == nil { + return true + } + smartBlock := block.Model().Content.(*model.BlockContentOfSmartblock) + return smartBlock == nil || smartBlock.Smartblock == nil } func GetDefaultViewRelations(rels []*model.Relation) []*model.BlockContentDataviewRelation { diff --git a/core/block/editor/state/state.go b/core/block/editor/state/state.go index eaf74b140..b242f42ef 100644 --- a/core/block/editor/state/state.go +++ b/core/block/editor/state/state.go @@ -559,14 +559,14 @@ func (s *State) apply(fast, one, withLayouts bool) (msgs []simple.EventMessage, s.fillChanges(msgs) // apply to parent - for _, id := range toRemove { - if s.parent != nil { + if s.parent != nil { + for _, id := range toRemove { action.Remove = append(action.Remove, s.PickOrigin(id).Copy()) delete(s.parent.blocks, id) } } - for _, b := range s.blocks { - if s.parent != nil { + if s.parent != nil { + for _, b := range s.blocks { id := b.Model().Id if _, ok := inUse[id]; ok { s.parent.blocks[id] = b