如何展平Quill delta输出?

问题描述

quill似乎将数据输出显示为增量,以逐步建立文档。这对于“撤消”目的或在编辑文档时很有用。但是在保存文档之后,此增量输出根本没有帮助,存储开销高并且难以在结构上遍历文档。

例如,单个代码块的内容为:

while (1) {
 i++;
 j++;
}

和增量输出

ops: Array(8)
0: {insert: "↵while (1) {"}
1: {attributes: {…},insert: "↵"}
2: {insert: " i++;"}
3: {attributes: {…},insert: "↵"}
4: {insert: " j++;"}
5: {attributes: {…},insert: "↵"}
6: {insert: "}"}
7: {attributes: {…},insert: "↵"}
length: 8

我如何将增量输出展平为一个更简单的输出,以结构形式显示文档。在这种情况下,具有“代码块”属性的单个插入?

解决方法

您可以使用Delta.compose方法来平整更改。

这是文档中的一个示例:

const a = new Delta().insert('abc');
const b = new Delta().retain(1).delete(1);

const composed = a.compose(b);  // composed == new Delta().insert('ac');

Autosave playground sample中还有另一个使用此示例的示例。