transform

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2023 License: AGPL-3.0 Imports: 3 Imported by: 2

Documentation

Overview

Package transform implements document transforms, which are used by the editor to treat changes as first-class values, which can be saved, shared, and reasoned about.

Index

Constants

This section is empty.

Variables

View Source
var EmptyStepMap = NewStepMap(nil)

EmptyStepMap is an empty StepMap.

Functions

This section is empty.

Types

type AddMarkStep

type AddMarkStep struct {
	From int
	To   int
	Mark *model.Mark
}

AddMarkStep adds a mark to all inline content between two positions.

func NewAddMarkStep

func NewAddMarkStep(from, to int, mark *model.Mark) *AddMarkStep

NewAddMarkStep is the constructor for AddMarkStep.

func (*AddMarkStep) Apply

func (s *AddMarkStep) Apply(doc *model.Node) StepResult

Apply is a method of the Step interface.

func (*AddMarkStep) GetMap

func (s *AddMarkStep) GetMap() *StepMap

GetMap is a method of the Step interface.

func (*AddMarkStep) Invert

func (s *AddMarkStep) Invert(doc *model.Node) Step

Invert is a method of the Step interface.

func (*AddMarkStep) Map

func (s *AddMarkStep) Map(mapping Mappable) Step

Map is a method of the Step interface.

func (*AddMarkStep) Merge

func (s *AddMarkStep) Merge(other Step) (Step, bool)

Merge is a method of the Step interface.

func (*AddMarkStep) ToJSON added in v0.2.0

func (s *AddMarkStep) ToJSON() map[string]interface{}

ToJSON is a method of the Step interface.

type MapResult

type MapResult struct {
	// The mapped version of the position.
	Pos int
	// Tells you whether the position was deleted, that is, whether the step
	// removed its surroundings from the document.
	Deleted bool
}

MapResult is an object representing a mapped position with extra information.

func NewMapResult

func NewMapResult(pos int, deleted ...bool) *MapResult

NewMapResult is the constructor for MapResult

type Mappable

type Mappable interface {
	// Map a position through this object. When given, assoc (should be -1 or
	// 1, defaults to 1) determines with which side the position is associated,
	// which determines in which direction to move when a chunk of content is
	// inserted at the mapped position.
	Map(pos int, assoc ...int) int

	// MapResult maps a position, and returns an object containing additional
	// information about the mapping. The result's deleted field tells you
	// whether the position was deleted (completely enclosed in a replaced
	// range) during the mapping. When content on only one side is deleted, the
	// position itself is only considered deleted when assoc points in the
	// direction of the deleted content.
	MapResult(pos int, assoc ...int) *MapResult
}

Mappable is an interface. There are several things that positions can be mapped through. Such objects conform to this interface.

type RemoveMarkStep

type RemoveMarkStep struct {
	From int
	To   int
	Mark *model.Mark
}

RemoveMarkStep adds a mark to all inline content between two positions.

func NewRemoveMarkStep

func NewRemoveMarkStep(from, to int, mark *model.Mark) *RemoveMarkStep

NewRemoveMarkStep is the constructor for RemoveMarkStep.

func (*RemoveMarkStep) Apply

func (s *RemoveMarkStep) Apply(doc *model.Node) StepResult

Apply is a method of the Step interface.

func (*RemoveMarkStep) GetMap

func (s *RemoveMarkStep) GetMap() *StepMap

GetMap is a method of the Step interface.

func (*RemoveMarkStep) Invert

func (s *RemoveMarkStep) Invert(doc *model.Node) Step

Invert is a method of the Step interface.

func (*RemoveMarkStep) Map

func (s *RemoveMarkStep) Map(mapping Mappable) Step

Map is a method of the Step interface.

func (*RemoveMarkStep) Merge

func (s *RemoveMarkStep) Merge(other Step) (Step, bool)

Merge is a method of the Step interface.

func (*RemoveMarkStep) ToJSON added in v0.2.0

func (s *RemoveMarkStep) ToJSON() map[string]interface{}

ToJSON is a method of the Step interface.

type ReplaceAroundStep

type ReplaceAroundStep struct {
	From      int
	To        int
	GapFrom   int
	GapTo     int
	Slice     *model.Slice
	Insert    int
	Structure bool
}

ReplaceAroundStep replaces a part of the document with a slice of content, but preserve a range of the replaced content by moving it into the slice.

func NewReplaceAroundStep

func NewReplaceAroundStep(from, to, gapFrom, gapTo int, slice *model.Slice, insert int, structure bool) *ReplaceAroundStep

NewReplaceAroundStep creates a replace-around step with the given range and gap. insert should be the point in the slice into which the content of the gap should be moved. structure has the same meaning as it has in the ReplaceStep class.

func (*ReplaceAroundStep) Apply

func (s *ReplaceAroundStep) Apply(doc *model.Node) StepResult

Apply is a method of the Step interface.

func (*ReplaceAroundStep) GetMap

func (s *ReplaceAroundStep) GetMap() *StepMap

GetMap is a method of the Step interface.

func (*ReplaceAroundStep) Invert

func (s *ReplaceAroundStep) Invert(doc *model.Node) Step

Invert is a method of the Step interface.

func (*ReplaceAroundStep) Map

func (s *ReplaceAroundStep) Map(mapping Mappable) Step

Map is a method of the Step interface.

func (*ReplaceAroundStep) Merge

func (s *ReplaceAroundStep) Merge(other Step) (Step, bool)

Merge is a method of the Step interface.

func (*ReplaceAroundStep) ToJSON added in v0.2.0

func (s *ReplaceAroundStep) ToJSON() map[string]interface{}

ToJSON is a method of the Step interface.

type ReplaceStep

type ReplaceStep struct {
	From      int
	To        int
	Slice     *model.Slice
	Structure bool
}

ReplaceStep replaces a part of the document with a slice of new content.

func NewReplaceStep

func NewReplaceStep(from, to int, slice *model.Slice, structure ...bool) *ReplaceStep

NewReplaceStep is the constructor of ReplaceStep.

The given slice should fit the 'gap' between from and to—the depths must line up, and the surrounding nodes must be able to be joined with the open sides of the slice. When structure is true, the step will fail if the content between from and to is not just a sequence of closing and then opening tokens (this is to guard against rebased replace steps overwriting something they weren't supposed to).

func (*ReplaceStep) Apply

func (s *ReplaceStep) Apply(doc *model.Node) StepResult

Apply is a method of the Step interface.

func (*ReplaceStep) GetMap

func (s *ReplaceStep) GetMap() *StepMap

GetMap is a method of the Step interface.

func (*ReplaceStep) Invert

func (s *ReplaceStep) Invert(doc *model.Node) Step

Invert is a method of the Step interface.

func (*ReplaceStep) Map

func (s *ReplaceStep) Map(mapping Mappable) Step

Map is a method of the Step interface.

func (*ReplaceStep) Merge

func (s *ReplaceStep) Merge(other Step) (Step, bool)

Merge is a method of the Step interface.

func (*ReplaceStep) ToJSON added in v0.2.0

func (s *ReplaceStep) ToJSON() map[string]interface{}

ToJSON is a method of the Step interface.

type SetAttrsStep added in v0.4.10

type SetAttrsStep struct {
	Pos   int
	Attrs map[string]interface{}
}

SetAttrsStep can be used to change the attributes of a node.

For more context, see: - https://discuss.prosemirror.net/t/preventing-image-placeholder-replacement-from-being-undone/1394/1 - https://bitbucket.org/atlassian/atlassian-frontend-mirror/src/master/editor/adf-schema/src/steps/set-attrs.tsx

func NewSetAttrsStep added in v0.4.10

func NewSetAttrsStep(pos int, attrs map[string]interface{}) *SetAttrsStep

NewSetAttrsStep is a constructor for SetAttrsStep

func (*SetAttrsStep) Apply added in v0.4.10

func (s *SetAttrsStep) Apply(doc *model.Node) StepResult

Apply is a method of the Step interface.

func (*SetAttrsStep) GetMap added in v0.4.10

func (s *SetAttrsStep) GetMap() *StepMap

GetMap is a method of the Step interface.

func (*SetAttrsStep) Invert added in v0.4.10

func (s *SetAttrsStep) Invert(doc *model.Node) Step

Invert is a method of the Step interface.

func (*SetAttrsStep) Map added in v0.4.10

func (s *SetAttrsStep) Map(mapping Mappable) Step

Map is a method of the Step interface.

func (*SetAttrsStep) Merge added in v0.4.10

func (s *SetAttrsStep) Merge(other Step) (Step, bool)

Merge is a method of the Step interface.

func (*SetAttrsStep) ToJSON added in v0.4.10

func (s *SetAttrsStep) ToJSON() map[string]interface{}

ToJSON is a method of the Step interface.

type Step

type Step interface {
	// Applies this step to the given document, returning a result
	// object that either indicates failure, if the step can not be
	// applied to this document, or indicates success by containing a
	// transformed document.
	Apply(doc *model.Node) StepResult

	// GetMap gets the step map that represents the changes made by this step,
	// and which can be used to transform between positions in the old and the
	// new document.
	GetMap() *StepMap

	// Invert creates an inverted version of this step. Needs the document as
	// it was before the step as argument.
	Invert(doc *model.Node) Step

	// Map this step through a mappable thing, returning either a version of
	// that step with its positions adjusted, or null if the step was entirely
	// deleted by the mapping.
	Map(mapping Mappable) Step

	// Merge tries to merge this step with another one, to be applied directly
	// after it. Returns the merged step when possible, null if the steps can't
	// be merged.
	Merge(other Step) (Step, bool)

	// ToJSON creates a JSON-serializeable representation of this step. When
	// defining this for a custom subclass, make sure the result object
	// includes the step type's JSON id under the stepType property.
	ToJSON() map[string]interface{}
}

Step objects represent an atomic change. It generally applies only to the document it was created for, since the positions stored in it will only make sense for that document.

New steps are defined by creating classes that extend Step, overriding the apply, invert, map, getMap and fromJSON methods, and registering your class with a unique JSON-serialization identifier using Step.jsonID.

func AddMarkStepFromJSON added in v0.2.0

func AddMarkStepFromJSON(schema *model.Schema, obj map[string]interface{}) (Step, error)

AddMarkStepFromJSON builds an AddMarkStep from a JSON representation.

func RemoveMarkStepFromJSON added in v0.2.0

func RemoveMarkStepFromJSON(schema *model.Schema, obj map[string]interface{}) (Step, error)

RemoveMarkStepFromJSON builds an RemoveMarkStep from a JSON representation.

func ReplaceAroundStepFromJSON added in v0.2.0

func ReplaceAroundStepFromJSON(schema *model.Schema, obj map[string]interface{}) (Step, error)

ReplaceAroundStepFromJSON builds an RemoveMarkStep from a JSON representation.

func ReplaceStepFromJSON added in v0.2.0

func ReplaceStepFromJSON(schema *model.Schema, obj map[string]interface{}) (Step, error)

ReplaceStepFromJSON builds an RemoveMarkStep from a JSON representation.

func SetAttrsStepFromJSON added in v0.4.10

func SetAttrsStepFromJSON(schema *model.Schema, obj map[string]interface{}) (Step, error)

SetAttrsStepFromJSON builds an SetAttrsStep from a JSON representation.

func StepFromJSON added in v0.2.0

func StepFromJSON(schema *model.Schema, obj map[string]interface{}) (Step, error)

StepFromJSON deserializes a step from its JSON representation. Will call through to the step class' own implementation of this method.

func TableSortStepFromJSON added in v0.4.11

func TableSortStepFromJSON(schema *model.Schema, obj map[string]interface{}) (Step, error)

TableSortStepFromJSON builds a step that does nothing. It is used by atlaskit. Cf https://bitbucket.org/atlassian/atlaskit-mk-2/src/master/packages/editor/editor-core/src/plugins/table/utils/sort-step.ts

type StepMap

type StepMap struct {
	Ranges   []int
	Inverted bool
}

StepMap is a map describing the deletions and insertions made by a step, which can be used to find the correspondence between positions in the pre-step version of a document and the same position in the post-step version.

func NewStepMap

func NewStepMap(ranges []int, inverted ...bool) *StepMap

NewStepMap creates a position map. The modifications to the document are represented as an array of numbers, in which each group of three represents a modified chunk as [start, oldSize, newSize].

func (*StepMap) Invert

func (sm *StepMap) Invert() *StepMap

Invert creates an inverted version of this map. The result can be used to map positions in the post-step document to the pre-step document.

func (*StepMap) Map

func (sm *StepMap) Map(pos int, assoc ...int) int

Map is part of the Mappable interface.

func (*StepMap) MapResult

func (sm *StepMap) MapResult(pos int, assoc ...int) *MapResult

MapResult is part of the Mappable interface.

func (*StepMap) String

func (sm *StepMap) String() string

String returns a string representation of this StepMap.

type StepResult

type StepResult struct {
	// :: ?Node The transformed document.
	Doc *model.Node
	// :: ?string Text providing information about a failed step.
	Failed string
}

StepResult is the result of applying a step. Contains either a new document or a failure value.

func Fail

func Fail(message string) StepResult

Fail creates a failed step result.

func FromReplace

func FromReplace(doc *model.Node, from, to int, slice *model.Slice) StepResult

FromReplace calls Node.replace with the given arguments. Create a successful result if it succeeds, and a failed one if it throws a `ReplaceError`.

func OK

func OK(doc *model.Node) StepResult

OK creates a successful step result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL