Skip to content

ot

import "github.com/alimasry/go-collab-editor/ot"

Index

func Apply

func Apply(doc string, op Operation) (string, error)

Apply applies the operation to a document string.

type Component

Component is a single step in an OT operation. Exactly one field should be set.

type Component struct {
    Retain int    `json:"retain,omitempty"` // keep N chars unchanged
    Insert string `json:"insert,omitempty"` // insert text at cursor
    Delete int    `json:"delete,omitempty"` // remove N chars at cursor
}

func (Component) IsDelete

func (c Component) IsDelete() bool

func (Component) IsInsert

func (c Component) IsInsert() bool

func (Component) IsRetain

func (c Component) IsRetain() bool

type Document

Document represents a collaborative document with its full operation history.

type Document struct {
    Content string
    Version int
    History []Operation
}

func NewDocument

func NewDocument(content string) *Document

NewDocument creates a new document with the given initial content.

func (*Document) Apply

func (d *Document) Apply(op Operation) error

Apply applies an operation to the document, appending it to history.

type Engine

Engine abstracts the OT collaboration algorithm. Different algorithms (Jupiter, Wave, etc.) implement this interface.

type Engine interface {
    // TransformIncoming transforms a client operation (created at the given
    // revision) against all operations in the history since that revision.
    // Returns the operation transformed to apply at the current server state.
    TransformIncoming(op Operation, revision int, history []Operation) (Operation, error)
}

type JupiterEngine

JupiterEngine implements the Jupiter OT algorithm. It sequentially transforms the incoming operation against each server operation the client hasn't seen.

type JupiterEngine struct{}

func (*JupiterEngine) TransformIncoming

func (e *JupiterEngine) TransformIncoming(op Operation, revision int, history []Operation) (Operation, error)

type Operation

Operation is a sequence of components that transforms a document. Components are applied left-to-right, advancing a cursor through the input.

type Operation struct {
    Ops []Component `json:"ops"`
}

func NewDelete

func NewDelete(pos, count, docLen int) Operation

NewDelete creates an operation that deletes count chars at pos in a document of docLen.

func NewInsert

func NewInsert(pos int, text string, docLen int) Operation

NewInsert creates an operation that inserts text at pos in a document of docLen.

func Transform

func Transform(a, b Operation) (aPrime, bPrime Operation, err error)

Transform takes two concurrent operations a and b (both applied to the same document state) and returns aPrime and bPrime such that:

Apply(Apply(doc, a), bPrime) == Apply(Apply(doc, b), aPrime)

func (Operation) BaseLen

func (op Operation) BaseLen() int

BaseLen returns the expected input document length.

func (Operation) IsNoop

func (op Operation) IsNoop() bool

IsNoop returns true if the operation makes no changes.

func (Operation) TargetLen

func (op Operation) TargetLen() int

TargetLen returns the document length after the operation is applied.

Generated by gomarkdoc