Gathering detailed insights and metrics for quill-delta
Gathering detailed insights and metrics for quill-delta
Gathering detailed insights and metrics for quill-delta
Gathering detailed insights and metrics for quill-delta
Simple and expressive JSON format for describing rich-text content and their changes
npm install quill-delta
98.9
Supply Chain
100
Quality
78
Maintenance
100
Vulnerability
98.9
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
920 Stars
274 Commits
132 Forks
18 Watching
3 Branches
16 Contributors
Updated on 26 Nov 2024
Minified
Minified + Gzipped
TypeScript (99.89%)
JavaScript (0.11%)
Cumulative downloads
Total Downloads
Last day
-6.2%
391,381
Compared to previous day
Last week
3%
2,126,768
Compared to previous week
Last month
8.3%
9,029,838
Compared to previous month
Last year
12.8%
92,155,506
Compared to previous year
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 of Operations, which describe changes to a document. They can be an insert
, delete
or retain
. 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.
1// Document with text "Gandalf the Grey" 2// with "Gandalf" bolded, and "Grey" in grey 3const delta = new Delta([ 4 { insert: 'Gandalf', attributes: { bold: true } }, 5 { insert: ' the ' }, 6 { insert: 'Grey', attributes: { color: '#ccc' } } 7]); 8 9// Change intended to be applied to above: 10// Keep the first 12 characters, insert a white 'White' 11// and delete the next four characters ('Grey') 12const death = new Delta().retain(12) 13 .insert('White', { color: '#fff' }) 14 .delete(4); 15// { 16// ops: [ 17// { retain: 12 }, 18// { insert: 'White', attributes: { color: '#fff' } }, 19// { delete: 4 } 20// ] 21// } 22 23// Applying the above: 24const restored = delta.compose(death); 25// { 26// ops: [ 27// { insert: 'Gandalf', attributes: { bold: true } }, 28// { insert: ' the ' }, 29// { insert: 'White', attributes: { color: '#fff' } } 30// ] 31// } 32
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. A walkthough of the motivation and design thinking behind Deltas are on Designing the Delta Format.
This format is suitable for Operational Transform and defines several functions to support this use case.
These methods called on or with non-document Deltas will result in undefined behavior.
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 operation.
1// Insert a bolded "Text" 2{ insert: "Text", attributes: { bold: true } } 3 4// Insert a link 5{ insert: "Google", attributes: { link: 'https://www.google.com' } } 6 7// Insert an embed 8{ 9 insert: { image: 'https://octodex.github.com/images/labtocat.png' }, 10 attributes: { alt: "Lab Octocat" } 11} 12 13// Insert another embed 14{ 15 insert: { video: 'https://www.youtube.com/watch?v=dMH0bHeiRNg' }, 16 attributes: { 17 width: 420, 18 height: 315 19 } 20}
Delete operations have a Number delete
key defined representing the number of characters to delete. All embeds have a length of 1.
1// Delete the next 10 characters 2{ delete: 10 }
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.
1// Keep the next 5 characters 2{ retain: 5 } 3 4// Keep and bold the next 5 characters 5{ retain: 5, attributes: { bold: true } } 6 7// Keep and unbold the next 5 characters 8// More specifically, remove the bold key in the attributes Object 9// in the next 5 characters 10{ retain: 5, attributes: { bold: null } }
Creates a new Delta object.
new Delta()
new Delta(ops)
new Delta(delta)
ops
- Array of operationsdelta
- Object with an ops
key set to an array of operationsNote: 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.
1const delta = new Delta([ 2 { insert: 'Hello World' }, 3 { insert: '!', attributes: { bold: true }} 4]); 5 6const packet = JSON.stringify(delta); 7 8const other = new Delta(JSON.parse(packet)); 9 10const chained = new Delta().insert('Hello World').insert('!', { bold: true });
Appends an insert operation. Returns this
for chainability.
insert(text, attributes)
insert(embed, attributes)
text
- String representing text to insertembed
- Object representing embed type to insertattributes
- Optional attributes to apply1delta.insert('Text', { bold: true, color: '#ccc' }); 2delta.insert({ image: 'https://octodex.github.com/images/labtocat.png' });
Appends a delete operation. Returns this
for chainability.
delete(length)
length
- Number of characters to delete1delta.delete(5);
Appends a retain operation. Returns this
for chainability.
retain(length, attributes)
length
- Number of characters to retainattributes
- Optional attributes to apply1delta.retain(4).retain(5, { color: '#0c6' });
Returns a new Delta representing the concatenation of this and another document Delta's operations.
concat(other)
other
- Document Delta to concatenateDelta
- Concatenated document Delta1const a = new Delta().insert('Hello'); 2const b = new Delta().insert('!', { bold: true }); 3 4 5// { 6// ops: [ 7// { insert: 'Hello' }, 8// { insert: '!', attributes: { bold: true } } 9// ] 10// } 11const concat = a.concat(b);
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.
diff(other)
diff(other, index)
other
- Document Delta to diff againstindex
- Suggested index where change took placeDelta
- difference between the two documents1const a = new Delta().insert('Hello'); 2const b = new Delta().insert('Hello!'); 3 4const diff = a.diff(b); // { ops: [{ retain: 5 }, { insert: '!' }] } 5 // a.compose(diff) == b 6
Iterates through document Delta, calling a given function with a Delta and attributes object, representing the line segment.
eachLine(predicate, newline)
predicate
- function to call on each line groupnewline
- newline character, defaults to \n
1const delta = new Delta().insert('Hello\n\n') 2 .insert('World') 3 .insert({ image: 'octocat.png' }) 4 .insert('\n', { align: 'right' }) 5 .insert('!'); 6 7delta.eachLine((line, attributes, i) => { 8 console.log(line, attributes, i); 9 // Can return false to exit loop early 10}); 11// Should log: 12// { ops: [{ insert: 'Hello' }] }, {}, 0 13// { ops: [] }, {}, 1 14// { ops: [{ insert: 'World' }, { insert: { image: 'octocat.png' } }] }, { align: 'right' }, 2 15// { ops: [{ insert: '!' }] }, {}, 3
Returned an inverted delta that has the opposite effect of against a base document delta. That is base.compose(delta).compose(inverted) === base
.
invert(base)
base
- Document delta to invert againstDelta
- inverted delta against the base delta1const base = new Delta().insert('Hello\n') 2 .insert('World'); 3const delta = new Delta().retain(6, { bold: true }).insert('!').delete(5); 4 5const inverted = delta.invert(base); // { ops: [ 6 // { retain: 6, attributes: { bold: null } }, 7 // { insert: 'World' }, 8 // { delete: 1 } 9 // ]} 10 // base.compose(delta).compose(inverted) === base
Returns an array of operations that passes a given function.
filter(predicate)
predicate
- Function to test each operation against. Return true
to keep the operation, false
otherwise.Array
- Filtered resulting array1const delta = new Delta().insert('Hello', { bold: true }) 2 .insert({ image: 'https://octodex.github.com/images/labtocat.png' }) 3 .insert('World!'); 4 5const text = delta 6 .filter((op) => typeof op.insert === 'string') 7 .map((op) => op.insert) 8 .join('');
Iterates through operations, calling the provided function for each operation.
forEach(predicate)
predicate
- Function to call during iteration, passing in the current operation.1delta.forEach((op) => { 2 console.log(op); 3});
Returns length of a Delta, which is the sum of the lengths of its operations.
length()
1new Delta().insert('Hello').length(); // Returns 5 2 3new Delta().insert('A').retain(2).delete(1).length(); // Returns 4
Returns a new array with the results of calling provided function on each operation.
map(predicate)
predicate
- Function to call, passing in the current operation, returning an element of the new array to be returnedArray
- A new array with each element being the result of the given function.1const delta = new Delta().insert('Hello', { bold: true }) 2 .insert({ image: 'https://octodex.github.com/images/labtocat.png' }) 3 .insert('World!'); 4 5const text = delta 6 .map((op) => { 7 if (typeof op.insert === 'string') { 8 return op.insert; 9 } else { 10 return ''; 11 } 12 }) 13 .join('');
Create an array of two arrays, the first with operations that pass the given function, the other that failed.
partition(predicate)
predicate
- Function to call, passing in the current operation, returning whether that operation passedArray
- A new array of two Arrays, the first with passed operations, the other with failed operations1const delta = new Delta().insert('Hello', { bold: true }) 2 .insert({ image: 'https://octodex.github.com/images/labtocat.png' }) 3 .insert('World!'); 4 5const results = delta.partition((op) => typeof op.insert === 'string'); 6const passed = results[0]; // [{ insert: 'Hello', attributes: { bold: true }}, 7 // { insert: 'World'}] 8const failed = results[1]; // [{ insert: { image: 'https://octodex.github.com/images/labtocat.png' }}]
Applies given function against an accumulator and each operation to reduce to a single value.
reduce(predicate, initialValue)
predicate
- Function to call per iteration, returning an accumulated valueinitialValue
- Initial value to pass to first call to predicateany
- the accumulated value1const delta = new Delta().insert('Hello', { bold: true }) 2 .insert({ image: 'https://octodex.github.com/images/labtocat.png' }) 3 .insert('World!'); 4 5const length = delta.reduce((length, op) => ( 6 length + (op.insert.length || 1); 7), 0);
Returns copy of delta with subset of operations.
slice()
slice(start)
slice(start, end)
start
- Start index of subset, defaults to 0end
- End index of subset, defaults to rest of operations1const delta = new Delta().insert('Hello', { bold: true }).insert(' World'); 2 3// { 4// ops: [ 5// { insert: 'Hello', attributes: { bold: true } }, 6// { insert: ' World' } 7// ] 8// } 9const copy = delta.slice(); 10 11// { ops: [{ insert: 'World' }] } 12const world = delta.slice(6); 13 14// { ops: [{ insert: ' ' }] } 15const space = delta.slice(5, 6);
Returns a Delta that is equivalent to applying the operations of own Delta, followed by another Delta.
compose(other)
other
- Delta to compose1const a = new Delta().insert('abc'); 2const b = new Delta().retain(1).delete(1); 3 4const composed = a.compose(b); // composed == new Delta().insert('ac'); 5
Transform given Delta against own operations.
transform(other, priority = false)
transform(index, priority = false)
- Alias for transformPosition
other
- Delta to transformpriority
- Boolean used to break ties. If true
, then this
takes priority
over other
, that is, its actions are considered to happen "first."Delta
- transformed Delta1const a = new Delta().insert('a'); 2const b = new Delta().insert('b').retain(5).insert('c'); 3 4a.transform(b, true); // new Delta().retain(1).insert('b').retain(5).insert('c'); 5a.transform(b, false); // new Delta().insert('b').retain(6).insert('c');
Transform an index against the delta. Useful for representing cursor/selection positions.
transformPosition(index, priority = false)
index
- index to transformNumber
- transformed index1const delta = new Delta().retain(5).insert('a'); 2delta.transformPosition(4); // 4 3delta.transformPosition(5); // 6
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
Found 5/13 approved changesets -- score normalized to 3
Reason
9 existing vulnerabilities detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More