Add plain text document state
This commit is contained in:
parent
fefbdbd764
commit
43dbdd8d68
61
data/plaintextdocstate.go
Normal file
61
data/plaintextdocstate.go
Normal file
@ -0,0 +1,61 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/data/pb"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
)
|
||||
|
||||
type PlainTextDocumentState struct {
|
||||
LastChangeId string
|
||||
Text string
|
||||
}
|
||||
|
||||
func NewPlainTextDocumentState(text string, id string) *PlainTextDocumentState {
|
||||
return &PlainTextDocumentState{
|
||||
LastChangeId: id,
|
||||
Text: text,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PlainTextDocumentState) ApplyChange(change []byte, id string) (DocumentState, error) {
|
||||
var changesData pb.PlainTextChangeData
|
||||
err := proto.Unmarshal(change, &changesData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, content := range changesData.GetContent() {
|
||||
err = p.applyChange(content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
p.LastChangeId = id
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *PlainTextDocumentState) applyChange(ch *pb.PlainTextChangeContent) error {
|
||||
switch {
|
||||
case ch.GetTextAppend() != nil:
|
||||
text := ch.GetTextAppend().GetText()
|
||||
p.Text += text
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PlainTextDocumentStateProvider struct{}
|
||||
|
||||
func (p *PlainTextDocumentStateProvider) ProvideFromInitialChange(change []byte, id string) (DocumentState, error) {
|
||||
var changesData pb.PlainTextChangeData
|
||||
err := proto.Unmarshal(change, &changesData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if changesData.GetSnapshot() == nil {
|
||||
return nil, fmt.Errorf("could not create state from empty snapshot")
|
||||
}
|
||||
return NewPlainTextDocumentState(changesData.GetSnapshot().GetText(), id), nil
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user