Socket
Socket
Sign inDemoInstall

rich-text

Package Overview
Dependencies
26
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.2 to 3.1.0

19

lib/type.js

@@ -1,3 +0,2 @@

var Delta = require('./delta');
var pkg = require('../package.json');
var Delta = require('quill-delta');

@@ -38,4 +37,20 @@

return delta2.transform(delta1, side === 'left');
},
transformCursor: function(cursor, delta, isOwnOp) {
return delta.transformPosition(cursor, !isOwnOp);
},
normalize: function(delta) {
return delta; // quill-delta is already canonical
},
serialize: function(delta) {
return delta.ops;
},
deserialize: function(ops) {
return new Delta(ops);
}
}
};

19

package.json
{
"name": "rich-text",
"version": "3.0.2",
"description": "Format for representing rich text documents and changes.",
"version": "3.1.0",
"description": "OT type for rich text",
"author": "Jason Chen <jhchen7@gmail.com>",

@@ -9,13 +9,8 @@ "homepage": "https://github.com/ottypes/rich-text",

"dependencies": {
"deep-equal": "^1.0.1",
"extend": "^3.0.0",
"fast-diff": "^1.0.1"
"quill-delta": "^3.2.0"
},
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"grunt": "~1.0.1",
"istanbul": "~0.4.3",
"lodash": "^4.10.0",
"mocha": "^2.4.5",
"lodash": "^4.16.4",
"mocha": "^3.1.2",
"ot-fuzzer": "^1.0.0"

@@ -28,3 +23,3 @@ },

"scripts": {
"test": "grunt test"
"test": "mocha test/fuzzer.js"
},

@@ -42,4 +37,4 @@ "repository": {

"operational transform",
"delta"
"sharejs"
]
}

@@ -1,70 +0,15 @@

# Rich Text [![Build Status](https://travis-ci.org/ottypes/rich-text.svg?branch=master)](http://travis-ci.org/ottypes/rich-text) [![Coverage Status](https://img.shields.io/coveralls/ottypes/rich-text.svg)](https://coveralls.io/r/ottypes/rich-text)
# Rich Text OT Type [![Build Status](https://travis-ci.org/ottypes/rich-text.svg?branch=master)](https://travis-ci.org/ottypes/rich-text)
A format for representing rich text documents and changes. It aimes to be intuitive and human readable with the ability to express any change necessary to deal with rich text. A document can also be expressed with this format--as the change from an empty document.
An OT Type for rich text documents.
## Quick Example
For documentation on the spec this type implements, see [ottypes/docs](https://github.com/ottypes/docs). Rich Text does not implement the optional `invert`, but does implement `normalize`, tranformCursor, `serialize`, and `deserialize`. Please refer to [ottypes/docs](https://github.com/ottypes/docs) for documentation.
```js
var delta = new Delta([
{ insert: 'Gandalf', attributes: { bold: true } },
{ insert: ' the ' },
{ insert: 'Grey', attributes: { color: '#ccc' } }
]);
Rich Text uses [quill-delta](https://github.com/quilljs/delta) on the back end.
// Keep the first 12 characters, delete the next 4, and insert a white 'White'
var death = new Delta().retain(12)
.delete(4)
.insert('White', { color: '#fff' });
// this produces:
// {
// ops: [
// { retain: 12 },
// { delete: '4 ' },
// { insert: 'White', attributes: { color: '#fff' } }
// ]
// }
var restored = delta.compose(death);
// restored is:
// {
// ops: [
// { insert: 'Gandalf ', attributes: { bold: true } },
// { insert: 'the ' },
// { insert: 'White', attributes: { color: '#fff' } }
// ]
// }
## Operations
```
Operations are an Array of changes, each operation describing a singular change to a document. They can be an [`insert`](#insert-operation), [`delete`](#delete-operation) or [`retain`](#retain-operation). Note operations do not take an index. They always describe the change at the current index. Use retains to "keep" or "skip" certain parts of the document.
This format is suitable for [Operational Transform](https://en.wikipedia.org/wiki/Operational_transformation) and defines several functions ([`compose`](#compose), [`transform`](#transform), [`diff`](#diff)) to support this use case.
## Contents
#### [Operations](#operations-1)
- [insert](#insert-operation)
- [delete](#delete-operation)
- [retain](#retain-operation)
#### [Deltas](#deltas-1)
- [`constructor`](#constructor)
- [`insert`](#insert)
- [`delete`](#delete)
- [`retain`](#retain)
- [`length`](#length)
- [`slice`](#slice)
- [`compose`](#compose)
- [`transform`](#transform)
- [`transformPosition`](#transformposition)
#### [Documents](#documents-1)
- [`concat`](#concat)
- [`diff`](#diff)
## Operations
Operations describe a singular change to a document. They can be an [`insert`](#insert-operation), [`delete`](#delete-operation) or [`retain`](#retain-operation). Note operations do not take an index. They always describe the change at the current index. Use retains to "keep" or "skip" certain parts of the document.
### Insert Operation

@@ -128,306 +73,8 @@

## Deltas
## Commentary
A Delta is made up of an array of operations. All methods maintain the property that Deltas are represented in the most compact form. For example two consecutive insert operations of plain text will be merged into one. Thus a vanilla deep Object/Array comparison can be used to determine Delta equality.
This library was originally implemented as part of a full fledged Google Docs like product called Stypi. Eventually, parts were open sourced--the editor became [Quill](https://github.com/quilljs/quill), the realtime engine became [tandem](https://github.com/tandem/tandem) and the document type became [tandem-core](https://github.com/tandem/tandem-core).
---
[ShareJS](https://github.com/josephg/ShareJS) was a more established open source realtime collaboration engine, so `tandem` and `tandem-core` were deprecated to unify support under one project. `tandem-core` was rewritten as `rich-text`, to adhere to ShareJS's [OT Type specification](https://github.com/ottypes/docs).
### constructor
Creates a new Delta object.
#### Methods
- `new Delta()`
- `new Delta(ops)`
- `new Delta(delta)`
#### Parameters
- `ops` - Array of operations
- `delta` - Object with an `ops` key set to an array of operations
*Note: No validity/sanity check is performed when constructed with ops or delta. The new delta's internal ops array will also be assigned to ops or delta.ops without deep copying.*
#### Example
```js
var delta = new Delta([
{ insert: 'Hello World' },
{ insert: '!', attributes: { bold: true }}
]);
var packet = JSON.stringify(delta);
var other = new Delta(JSON.parse(packet));
var chained = new Delta().insert('Hello World').insert('!', { bold: true });
```
---
### insert()
Appends an insert operation. Returns `this` for chainability.
#### Methods
- `insert(text, attributes)`
- `insert(embed, attributes)`
#### Parameters
- `text` - String representing text to insert
- `embed` - Number representing embed type to insert
- `attributes` - Optional attributes to apply
#### Example
```js
delta.insert('Text', { bold: true, color: '#ccc' });
delta.insert(1, { src: 'https://octodex.github.com/images/labtocat.png' });
```
---
### delete()
Appends a delete operation. Returns `this` for chainability.
#### Methods
- `delete(length)`
#### Parameters
- `length` - Number of characters to delete
#### Example
```js
delta.delete(5);
```
---
### retain()
Appends a retain operation. Returns `this` for chainability.
#### Methods
- `retain(length, attributes)`
#### Parameters
- `length` - Number of characters to retain
- `attributes` - Optional attributes to apply
#### Example
```js
delta.retain(4).retain(5, { color: '#0c6' });
```
---
### length()
Returns length of a Delta, which is the sum of the lengths of its operations.
#### Methods
- `length()`
#### Example
```js
new Delta().insert('Hello').length(); // Returns 5
new Delta().insert('A').retain(2).delete(1) // Returns 4
```
---
### slice()
Returns copy of delta with subset of operations.
#### Methods
- `slice()`
- `slice(start)`
- `slice(start, end)`
#### Parameters
- `start` - Start index of subset, defaults to 0
- `end` - End index of subset, defaults to rest of operations
#### Example
```js
var delta = new Delta().insert('Hello', { bold: true }).insert(' World');
// {
// ops: [
// { insert: 'Hello', attributes: { bold: true } },
// { insert: ' World' }
// ]
// }
var copy = delta.slice();
// { ops: [{ insert: 'World' }] }
var world = delta.slice(6);
// { ops: [{ insert: ' ' }] }
var space = delta.slice(5, 6);
```
---
### compose()
Returns a Delta that is equivalent to applying the operations of own Delta, followed by another Delta.
#### Methods
- `compose(other)`
#### Parameters
- `other` - Delta to compose
#### Example
```js
var a = new Delta().insert('abc');
var b = new Delta().retain(1).delete(1);
var composed = a.compose(b); // composed == new Delta().insert('ac');
```
---
### transform()
Transform given Delta against own operations.
#### Methods
- `transform(other, priority)`
- `transform(index)` - Alias for [`transformPosition`](#tranformposition)
#### Parameters
- `other` - Delta to transform
- `priority` - Boolean used to break ties
#### Returns
- `Delta` - transformed Delta
#### Example
```js
var a = new Delta().insert('a');
var b = new Delta().insert('b');
b = a.transform(b, true); // new Delta().retain(1).insert('b');
```
---
### transformPosition()
Transform an index against the delta. Useful for representing cursor/selection positions.
#### Methods
- `transformPosition(index)`
#### Parameters
- `index` - index to transform
#### Returns
- `Number` - transformed index
#### Example
```js
var index = 12;
var transformedIndex = delta.transformPosition(index);
```
## Documents
A Delta with only insert operations can be used to represent a rich text document. This can be thought of as a Delta applied to an empty document.
The following methods are supported only for Deltas that represent a document (i.e. they only contain inserts). There are no guarantees on the behavior on non-document Deltas.
---
### concat()
Returns a new Delta representing the concatenation of this and another document Delta's operations.
#### Methods
- `concat(other)`
#### Parameters
- `other` - Document Delta to concatenate
#### Returns
- `Delta` - Concatenated document Delta
#### Example
```js
var a = new Delta().insert('Hello');
var b = new Delta().insert('!', { bold: true });
// {
// ops: [
// { insert: 'Hello' },
// { insert: '!', attributes: { bold: true } }
// ]
// }
var concat = a.concat(b);
```
---
### diff()
Returns a Delta representing the difference between two documents.
#### Methods
- `diff(other)`
#### Parameters
- `other` - Document Delta to diff against
#### Returns
- `Delta` - difference between the two documents
#### Example
```js
var a = new Delta().insert('Hello');
var b = new Delta().insert('Hello!');
var diff = a.diff(b); // { ops: [{ retain: 5 }, { insert: '!' }] }
// a.compose(diff) == b
```
The needs of a realtime rich text document type was formerly a superset of a generalized rich text document type. As Quill has evolved, the reverse is becoming true. This `rich-text` library today provides the interface to use with ShareJS, but the underlying type and fuctionality is implemented in [`quill-delta`](https://github.com/quilljs/delta).

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc