GithubHelp home page GithubHelp logo

anthrax3 / prosemirror-changeset Goto Github PK

View Code? Open in Web Editor NEW

This project forked from prosemirror/prosemirror-changeset

0.0 1.0 0.0 106 KB

Distills a series of editing steps into deleted and added ranges

License: MIT License

JavaScript 100.00%

prosemirror-changeset's Introduction

prosemirror-changeset

This is a helper module that can turn a sequence of document changes into a set of insertions and deletions, for example to display them in a change-tracking interface. Such a set can be built up incrementally, in order to do such change tracking in a halfway performant way during live editing.

This code is licensed under an MIT licence.

Programming interface

Insertions and deletions are represented as ‘spans’—ranges in the document. The deleted spans refer to the original document, whereas the inserted ones point into the current document.

It is possible to associate arbitrary data values with such spans, for example to track the user that made the change, the timestamp at which it was made, or the step data necessary to invert it again.

class Change

A replaced range with metadata associated with it.

  • fromA: number
    The start of the range deleted/replaced in the old document.

  • toA: number
    The end of the range in the old document.

  • fromB: number
    The start of the range inserted in the new document.

  • toB: number
    The end of the range in the new document.

  • deleted: [Span]
    Data associated with the deleted content. The length of these spans adds up to this.toA - this.fromA.

  • inserted: [Span]
    Data associated with the inserted content. Length adds up to this.toB - this.toA.

class Span

Stores metadata for a part of a change.

  • length: number

  • data: any

class ChangeSet

A change set tracks the changes to a document from a given point in the past. It condenses a number of step maps down to a flat sequence of replacements, and simplifies replacements that partially undo themselves by comparing their content.

  • changes: [Change]
    Replaced regions.

  • addSteps(newDoc: Node, maps: [StepMap], data: [any] | any) → ChangeSet
    Computes a new changeset by adding the given step maps and metadata (either as an array, per-map, or as a single value to be associated with all maps) to the current set. Will not mutate the old set.

    Note that due to simplification that happens after each add, incrementally adding steps might create a different final set than adding all those changes at once, since different document tokens might be matched during simplification depending on the boundaries of the current changed ranges.

  • startDoc: Node
    The starting document of the change set.

  • map(f: fn(range: Change) → any) → ChangeSet
    Map the span's data values in the given set through a function and construct a new set with the resulting data.

  • changedRange(b: ChangeSet, maps: ?[StepMap]) → ?{from: number, to: number}
    Compare two changesets and return the range in which they are changed, if any. If the document changed between the maps, pass the maps for the steps that changed it as second argument, and make sure the method is called on the old set and passed the new set. The returned positions will be in new document coordinates.

  • static create(doc: Node, combine: ?fn(a: any, b: any) → any) → ChangeSet
    Create a changeset with the given base object and configuration. The combine function is used to compare and combine metadata—it should return null when metadata isn't compatible, and a combined version for a merged range when it is.

  • simplifyChanges(changes: [Change], doc: Node) → [Change]
    Simplifies a set of changes for presentation. This makes the assumption that having both insertions and deletions within a word is confusing, and, when such changes occur without a word boundary between them, they should be expanded to cover the entire set of words (in the new document) they touch. An exception is made for single-character replacements.

prosemirror-changeset's People

Contributors

marijnh avatar floorish avatar lpellegr avatar saranrapjs avatar jgravois avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.