Skip to content

Commit

Permalink
Merge pull request #32 from anyproto/go-1300-turn-into-object-to-note…
Browse files Browse the repository at this point in the history
…-converts-duplicates

GO-1300 Fix creating note with duplicates
  • Loading branch information
requilence authored Jun 6, 2023
2 parents 5007122 + a56b16c commit 313c2ba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
29 changes: 20 additions & 9 deletions core/block/editor/basic/extract_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,11 @@ func (bs *basic) ExtractBlocksToObjects(ctx *session.Context, s ObjectCreator, r
}
objState.Set(simple.New(rootBlock))

fields := map[string]*types.Value{
bundle.RelationKeyName.String(): pbtypes.String(root.Model().GetText().Text),
}
if req.ObjectType != "" {
fields[bundle.RelationKeyType.String()] = pbtypes.String(req.ObjectType)
}
det := &types.Struct{Fields: fields}
layout, _ := st.Layout()
details := extractDetailsFields(req.ObjectType, root.Model().GetText().Text, layout)

s.InjectWorkspaceID(det, req.ContextId)
objectID, _, err := s.CreateSmartBlockFromState(context.TODO(), coresb.SmartBlockTypePage, det, objState)
s.InjectWorkspaceID(details, req.ContextId)
objectID, _, err := s.CreateSmartBlockFromState(context.TODO(), coresb.SmartBlockTypePage, details, objState)
if err != nil {
return nil, fmt.Errorf("create child object: %w", err)
}
Expand All @@ -99,6 +94,22 @@ func (bs *basic) ExtractBlocksToObjects(ctx *session.Context, s ObjectCreator, r
return linkIds, bs.Apply(st)
}

func extractDetailsFields(objectType string, nameText string, layout model.ObjectTypeLayout) *types.Struct {
fields := map[string]*types.Value{}

// Without this check title will be duplicated in template.WithNameToFirstBlock
if layout != model.ObjectType_note {
fields[bundle.RelationKeyName.String()] = pbtypes.String(nameText)
}

if objectType != "" {
fields[bundle.RelationKeyType.String()] = pbtypes.String(objectType)
}

details := &types.Struct{Fields: fields}
return details
}

// reassignSubtreeIds makes a copy of a subtree of blocks and assign a new id for each block
func reassignSubtreeIds(rootId string, blocks []simple.Block) (string, []simple.Block) {
res := make([]simple.Block, 0, len(blocks))
Expand Down
12 changes: 12 additions & 0 deletions core/block/editor/basic/extract_objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,16 @@ func TestExtractObjects(t *testing.T) {

})
}

t.Run("do not add relation name - when creating note", func(t *testing.T) {
fields := extractDetailsFields("whatever type", "whatever name", model.ObjectType_note).Fields

assert.NotContains(t, fields, bundle.RelationKeyName.String())
})

t.Run("add relation name - when creating not note", func(t *testing.T) {
fields := extractDetailsFields("whatever type", "whatever name", model.ObjectType_basic).Fields

assert.Contains(t, fields, bundle.RelationKeyName.String())
})
}

0 comments on commit 313c2ba

Please sign in to comment.