# Delta [](http://travis-ci.org/quilljs/delta) [](https://coveralls.io/r/quilljs/delta)
Deltas are a simple, yet expressive format that can be used to describe contents and changes. The format is JSON based, and is human readable, yet easily parsible by machines. Deltas can describe any rich text document, includes all text and formatting information, without the ambiguity and complexity of HTML.
A Delta is made up of an [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) of Operations, which describe changes 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.
Don’t be confused by its name Delta—Deltas represents both documents and changes to documents. If you think of Deltas as the instructions from going from one document to another, the way Deltas represent a document is by expressing the instructions starting from an empty document.
## Quick Example
```js
// Document with text "Gandalf the Grey"
// with "Gandalf" bolded, and "Grey" in grey
var delta = new Delta([
{ insert: 'Gandalf', attributes: { bold: true } },
{ insert: ' the ' },
{ insert: 'Grey', attributes: { color: '#ccc' } }
]);
// Change intended to be applied to above:
// 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' });
// {
// ops: [
// { retain: 12 },
// { delete: 4 },
// { insert: 'White', attributes: { color: '#fff' } }
// ]
// }
// Applying the above:
var restored = delta.compose(death);
// {
// ops: [
// { insert: 'Gandalf ', attributes: { bold: true } },
// { insert: 'the ' },
// { insert: 'White', attributes: { color: '#fff' } }
// ]
// }
```
This README describes Deltas in its general form and API functionality. Additional information on the way Quill specifically uses Deltas can be found on its own [Delta docs](http://quilljs.com/docs/delta/). A walkthough of the motivation and design thinking behind Deltas are on [Designing the Delta Format](http://quilljs.com/guides/designing-the-delta-format/).
This format is suitable for [Operational Transform](https://en.wikipedia.org/wiki/Operational_transformation) and defines several functions to support this use case.
## Contents
#### Operations
- [`insert`](#insert-operation)
- [`delete`](#delete-operation)
- [`retain`](#retain-operation)
#### Construction
- [`constructor`](#constructor)
- [`insert`](#insert)
- [`delete`](#delete)
- [`retain`](#retain)
#### Documents
These methods called on or with non-document Deltas will result in undefined behavior.
- [`concat`](#concat)
- [`diff`](#diff)
- [`eachLine`](#eachline)
#### Utility
- [`filter`](#filter)
- [`forEach`](#foreach)
- [`length`](#length)
- [`map`](#map)
- [`partition`](#partition)
- [`reduce`](#reduce)
- [`slice`](#slice)
#### Operational Transform
- [`compose`](#compose)
- [`transform`](#transform)
- [`transformPosition`](#transformposition)
## Operations
### Insert Operation
Insert operations have an `insert` key defined. A String value represents inserting text. Any other type represents inserting an embed (however only one level of object comparison will be performed for equality).
In both cases of text and embeds, an optional `attributes` key can be defined with an Object to describe additonal formatting information. Formats can be changed by the [retain](#retain) operation.
```js
// Insert a bolded "Text"
{ insert: "Text", attributes: { bold: true } }
// Insert a link
{ insert: "Google", attributes: { link: 'https://www.google.com' } }
// Insert an embed
{
insert: { image: 'https://octodex.github.com/images/labtocat.png' },
attributes: { alt: "Lab Octocat" }
}
// Insert another embed
{
insert: { video: 'https://www.youtube.com/watch?v=dMH0bHeiRNg' },
attributes: {
width: 420,
height: 315
}
}
```
### Delete Operation
Delete operations have a Number `delete` key defined representing the number of characters to delete. All embeds have a length of 1.
```js
// Delete the next 10 characters
{ delete: 10 }
```
### Retain Operation
Retain operations have a Number `retain` key defined representing the number of characters to keep (other libraries might use the name keep or skip). An optional `attributes` key can be defined with an Object to describe formatting changes to the character range. A value of `null` in the `attributes` Object represents removal of that key.
*Note: It is not necessary to retain the last characters of a document as this is implied.*
```js
// Keep the next 5 characters
{ retain: 5 }
// Keep and bold the next 5 characters
{ retain: 5, attributes: { bold: true } }
// Keep and unbold the next 5 characters
// More specifically, remove the bold key in the attributes Object
// in the next 5 characters
{ retain: 5, attributes: { bold: null } }
```
## Construction
### 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 from 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` - Object representing embed type to insert
- `attributes` - Optional attributes to apply
#### Example
```js
delta.insert('Text', { bold: true, color: '#ccc' });
delta.insert({ image: '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' });
```
## Documents
### 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. Optionally, accepts a suggested index where change took place, often representing a cursor position *before* change.
#### Methods
- `diff(other)`
- `diff(other, index)`
#### Parameters
- `other` - Document Delta to diff against
- `index` - Suggested index where change took place
#### Returns
- `Delta` - difference between the two documents
#### Example
```js
var a
没有合适的资源?快使用搜索试试~ 我知道了~
StrayAnimals:自己的毕设作品,是一个前后端分离的项目,分为前台门户系统和后台管理系统。前端基于Vue.js、Elem...

共2000个文件
js:976个
java:329个
jpg:184个


温馨提示
自己的毕设作品,是一个前后端分离的项目,分为前台门户系统和后台管理系统。前端基于Vue.js、Element UI、Axios等实现页面的构建和请求的发送,后端基于SpringBoot、MyBatis、Redis、Nginx实现。系统实现了流浪动物救助帖子的发布、回复、评论,以及宠物领养、宠物寻找、宠物咨询信息分享等功能,后台管理系统可以对用户、帖子等信息实现批量管理、审核、修改等操作。 集成了百度的UEditor富文本框,实现对帖子的发布;使用JWT实现对身份信息的验证;集成腾讯QQ邮箱处理邮件服务;使用Redis缓存部分热点数据和注册码。
资源详情
资源评论
资源推荐
收起资源包目录





































































































共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20














格式:zip 资源大小:300.0KB









格式:zip 资源大小:3.3MB


格式:doc 资源大小:19.5KB 页数:1






善音
- 粉丝: 33
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 房建装修工程监理技术标标书.doc
- 置地北京公司建筑工程施工阶段管理规定.doc
- 河北2012建筑、装饰工程预算定额变化及计算规则说明.doc
- 敏感性分析例题.ppt
- 单层钢结构工业厂房毕业实习报告.docx
- 建设工程监理规范用表.doc
- 微信小程序微商城(仿拼多多).zip
- 工程造价控制的新思路.ppt
- 土壤源热泵的应用培训讲义.doc
- 纠正、预防措施记录表3.doc
- 普定县某住宅楼岩土工程勘察报告.doc
- 变风量空调末端装置控制分类.doc
- 微信小程序学习.zip
- 合同预算部部门经理个人工作总结.doc
- 微信小程序点餐+SpringBoot(1).zip
- 北京市某220kv变电所第三电源工程施工组织设计.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论1