美文网首页
ckeditor5/engine:Indexes and off

ckeditor5/engine:Indexes and off

作者: videring | 来源:发表于2020-10-04 13:57 被阅读0次

<paragraph>有两个文本节点:"Foo "和"bar"。如果你知道native DOM Ranges的巩固走原理,你可能会问:“但是,如果所选内容位于两个文本节点的边界,那么它是锚定在左侧,右侧还是包含在元素中?”
这个确实是另一个跟DOM APIs相关的问题。不仅元素外部和内部的位置在视觉上都是相同的,而且还可以将它们锚定在文本节点的内部或外部(如果位置在文本节点边界处)。在实现editing algorithms时,这会造成极大的复杂性。
为了避免此类麻烦,并使真正的协作编辑(collaborative editing)成为可能,CKEditor 5使用索引(indexes)和偏移量(offsets)的概念。索引与节点(元素和文本节点)有关,而偏移与位置有关。例如,在以下结构中:

据说内部<paragraph>有两个文本节点:"Foo ""bar"。如果您知道本机DOM Ranges的工作原理,那么您可能会问:“但是,如果所选内容位于两个文本节点的边界,那么它是锚定在左侧,右侧还是包含在元素中?”

确实,这是DOM API的另一个问题。不仅元素外部和内部的位置在视觉上都是相同的,而且还可以将它们锚定在文本节点的内部或外部(如果位置在文本节点边界处)。在实施编辑算法时,所有这些都会带来极大的复杂性。

为了避免此类麻烦,并使真正的协作编辑成为可能,CKEditor 5使用索引偏移量的概念。索引与节点(元素和文本节点)有关,而偏移与位置有关。例如,在以下结构中:

<paragraph>
    "Foo "
    <image></image>
    "bar"
</paragraph>

"Foo "文本节点是其父节点(paragraph标签)的下标(index)为0<image></image>的下标(index)为1"bar" 的下标为2。另一方面,父节点<paragraph>中,偏移量x为:

Offset Position Node
0 <paragraph>^Foo <image></image>bar</paragraph> "Foo "
1 <paragraph>F^oo <image></image>bar</paragraph> "Foo "
4 <paragraph>Foo ^<image></image>bar</paragraph> <image>
6 <paragraph>Foo <image></image>b^ar</paragraph>

注:其中文本节点偏移量是文本的长度,非文本节点偏移量为1。


Positions, ranges and selections

ckeditor5的edting engine还定义了三个操作(operate)偏移量(offsets)的级别:

ROOT
 |- P            before: [ 0 ]         after: [ 1 ]
 |- UL           before: [ 1 ]         after: [ 2 ]
    |- LI        before: [ 1, 0 ]      after: [ 1, 1 ]
    |  |- foo    before: [ 1, 0, 0 ]   after: [ 1, 0, 3 ]
    |- LI        before: [ 1, 1 ]      after: [ 1, 2 ]
       |- bar    before: [ 1, 1, 0 ]   after: [ 1, 1, 3 ]
ROOT
 |- P
 |- UL
    |- LI
    |  |- f^o|o  ^ has path: [ 1, 0, 1 ]   | has path: [ 1, 0, 2 ]
    |- LI
       |- b^a|r  ^ has path: [ 1, 1, 1 ]   | has path: [ 1, 1, 2 ]
  • 一个Range包含两个positionstartend
  • 最后,有一个Selection包含一个或多个range、属性,并具有方向(direction,无论是从左到右还是从右到左完成)的。您可以根据需要创建任意数量的实例,并且可以随时随意对其进行修改。此外,还有一个DocumentSelection。它代表文档的选择(document’s selection),并且只能通过模型编写器model writer)方式进行更改。更改文档结构后,它将自动更新。

参考:
官网:indexes-and-offsets
ckeditor5/engine:model tree的几个概念

相关文章

网友评论

      本文标题:ckeditor5/engine:Indexes and off

      本文链接:https://www.haomeiwen.com/subject/byrnuktx.html