接口定义

能够对于文字、段落乃至任何元素的精准定位 并做出增删改查,都是在开发一款富文本编辑器时一项最基本也是最重要的功能之一。让我们先来看看Slate中对于如何在文档树中定位元素是怎么定义的[源码]

/**
* The `Location` interface is a union of the ways to refer to a specific
* location in a Slate document: paths, points or ranges.
*
* Methods will often accept a `Location` instead of requiring only a `Path`,
* `Point` or `Range`. This eliminates the need for developers to manage
* converting between the different interfaces in their own code base.
*/
export type Location = Path | Point | Range

Location是一个包含了PathPointRange的联合类型,代指了Slate中所有关于“定位”的定位,同时也方便了开发。例如在几乎所有的Transforms方法中,都可以通过传递Location参数来决定Transforms方法需要应用到文档中的哪些位置上[链接]

All transforms support a parameter options. This includes options specific to the transform, and general NodeOptions to specify which Nodes in the document that the transform is applied to.

interface NodeOptions {
at?: Location
match?: (node: Node, path: Location) => boolean
mode?: 'highest' | 'lowest'
voids?: boolean
}

Path

Path是三个中最基本的概念,也是唯一一个不可拓展的类型。

/**
* `Path` arrays are a list of indexes that describe a node's exact position in
* a Slate node tree. Although they are usually relative to the root `Editor`
* object, they can be relative to any `Node` object.
*/
export type Path = number[]

Path类型就是一个数组,用来表示Slate文档树中自根节点root到指定node的绝对路径。我们以下边的示例来演示下各个node所代表的路径:

const initialValue: Descendant[] = [
// path: [0]
{
type: 'paragraph',
children: [
{ text: 'This is editable ' }, // path: [0, 0]
{ text: 'rich text!', bold: true } // path: [0, 1]
]
},
// path: [1]
{
type: 'paragraph',
children: [
{ text: 'It\' so cool.' } // path: [1, 0]
]
}
]

虽然Path所代表的路径通常是以顶层Editor作为root节点的,但也会有其他情况,比如由Node提供的get方法中传入的Path参数则代表的是相对路径[源码]

/**
* Get the descendant node referred to by a specific path. If the path is an
* empty array, it refers to the root node itself.
*/
get(root: Node, path: Path): Node {
let node = root for (let i = 0; i < path.length; i++) {
const p = path[i] if (Text.isText(node) || !node.children[p]) {
throw new Error(
`Cannot find a descendant at path [${path}] in node: ${Scrubber.stringify(
root
)}`
)
} node = node.children[p]
} return node
}

Point

Point是在Path的基础上封装而来的概念:

/**
* `Point` objects refer to a specific location in a text node in a Slate
* document. Its path refers to the location of the node in the tree, and its
* offset refers to the distance into the node's string of text. Points can
* only refer to `Text` nodes.
*/
export interface BasePoint {
path: Path
offset: number
} export type Point = ExtendedType<'Point', BasePoint>

用于定位单个字符在文档中的位置;先用Path定位到字符所属的Node,再根据offset字段信息精确到字符是在该Nodetext文本中的偏移量。

我们仍然以前面的示例做说明,如果想要将光标位置定位到第一句话"This is editable rich text!"的感叹号之后,其Point值为:

const initialValue: Descendant[] = [
// path: [0]
{
type: 'paragraph',
children: [
{ text: 'This is editable ' }, // path: [0, 0]
{ text: 'rich text!', bold: true } // { path: [0, 1], offset: 10 }
]
},
// path: [1]
{
type: 'paragraph',
children: [
{ text: 'It\' so cool.' } // path: [1, 0]
]
}
]

Range

最后一个Range则是再在Point基础上延伸封装而来的概念:

/**
* `Range` objects are a set of points that refer to a specific span of a Slate
* document. They can define a span inside a single node or a can span across
* multiple nodes.
*/
export interface BaseRange {
anchor: Point
focus: Point
} export type Range = ExtendedType<'Range', BaseRange>

它代表的是一段文本的集合;包含有两个Point类型的字段anchorfocus。看到这,应该能发现Slate中Range的概念其实与DOM中的Selection对象是一样的,anchorfocus分别对应原生Selection中的锚点anchorNode和焦点focusNode;这正是Slate对于原生Selection做的抽象,使之在自身API中更方便地通过光标选区来获取文档树中的内容。

我们在上一篇文章中有提到过的Editor.selection是一个Selections类型,其本身就是一个Range类型,专门用来指定编辑区域中的光标位置[源码]

export type BaseSelection = Range | null

export type Selection = ExtendedType<'Selection', BaseSelection>

Refs

当我们需要长期追踪某些Node时,可以通过获取对应NodePath/Point/Range值并保存下来以达到目的。但这种方式存在的问题是,在Slate文档树经过insertremove等操作后,原先的Path/Point/Range可能会产生变动或者直接作废掉。

Refs的出现就是为了解决上述问题。

三者的ref的定义分别在slate/src/interfaces/下的path-ref.tspoint-ref.tsrange-ref.ts文件中:

/**
* `PathRef` objects keep a specific path in a document synced over time as new
* operations are applied to the editor. You can access their `current` property
* at any time for the up-to-date path value.
*/
export interface PathRef {
current: Path | null
affinity: 'forward' | 'backward' | null
unref(): Path | null
} /**
* `PointRef` objects keep a specific point in a document synced over time as new
* operations are applied to the editor. You can access their `current` property
* at any time for the up-to-date point value.
*/
export interface PointRef {
current: Point | null
affinity: TextDirection | null
unref(): Point | null
} /**
* `RangeRef` objects keep a specific range in a document synced over time as new
* operations are applied to the editor. You can access their `current` property
* at any time for the up-to-date range value.
*/
export interface RangeRef {
current: Range | null
affinity: 'forward' | 'backward' | 'outward' | 'inward' | null
unref(): Range | null
}

都包含以下三个字段:

  • current:同React ref用法一样,用current字段存储最新值
  • affinity:当前定位所代表的节点 在文档树变动时如果受到影响的话,所采取的调整策略
  • unref:卸载方法;彻底删除当前的ref确保能够被引擎GC掉

另外我们先来看下各种RefsSlate存储的方式,跳到slate/src/utils/weak-maps.ts[源码]

export const PATH_REFS: WeakMap<Editor, Set<PathRef>> = new WeakMap()
export const POINT_REFS: WeakMap<Editor, Set<PointRef>> = new WeakMap()
export const RANGE_REFS: WeakMap<Editor, Set<RangeRef>> = new WeakMap()

可以看到Refs的存储区在一个Set数据结构中的;而对于不同EditorSet<xxxRef>的存储则是放在哈希表WeakMap中的,使其不会影响到GC(WeakMap同Map原理一样,都是ES6之后新出的哈希数据结构,与Map不同点在于其持有的引用算作弱引用)。

生成三种Ref以及获取相应Refs的方法定义在EditorInterface接口上[源码]

export interface EditorInterface {
// ...
pathRef: (
editor: Editor,
path: Path,
options?: EditorPathRefOptions
) => PathRef pointRef: (
editor: Editor,
point: Point,
options?: EditorPointRefOptions
) => PointRef rangeRef: (
editor: Editor,
range: Range,
options?: EditorRangeRefOptions
) => RangeRef pathRefs: (editor: Editor) => Set<PathRef> pointRefs: (editor: Editor) => Set<PointRef> rangeRefs: (editor: Editor) => Set<RangeRef>
}

PathPointRange三者的实现逻辑都差不多,下面就以Path为例作介绍。

pathRef

  /**
* Create a mutable ref for a `Point` object, which will stay in sync as new
* operations are applied to the editor.
*/
pointRef(
editor: Editor,
point: Point,
options: EditorPointRefOptions = {}
): PointRef {
const { affinity = 'forward' } = options
const ref: PointRef = {
current: point,
affinity,
unref() {
const { current } = ref
const pointRefs = Editor.pointRefs(editor)
pointRefs.delete(ref)
ref.current = null
return current
},
} const refs = Editor.pointRefs(editor)
refs.add(ref)
return ref
}

实现逻辑非常简单,就是根据传入的参数放入ref对象中,并添加卸载方法unref。然后通过pathRefs拿到对应的Set,讲当前的ref对象添加进去。unref方法中实现的则是相反的操作:通过pathRefs拿到对应的Set后,将当前ref对象移除掉,然后再把ref.current的值置空。

pathRefs

  /**
* Get the set of currently tracked path refs of the editor.
*/
pathRefs(editor: Editor): Set<PathRef> {
let refs = PATH_REFS.get(editor) if (!refs) {
refs = new Set()
PATH_REFS.set(editor, refs)
} return refs
}

代码非常简短,类似懒加载的方式做Set的初始化,然后调用get方法获取集合后返回。

Ref同步

前一篇文章我们提到过,用于修改内容的单个Transform方法会包含有多个OperationOperation则是Slate中的原子化操作。而在Slate文档树更新之后,解决Ref同步更新的方式就是:在执行了任意Operation之后,对所有的Ref根据执行的Operation类型做相应的调整。

看到create-editor.ts中的apply方法,该方法是所有Operation执行的入口[源码]

apply: (op: Operation) => {
for (const ref of Editor.pathRefs(editor)) {
PathRef.transform(ref, op)
} for (const ref of Editor.pointRefs(editor)) {
PointRef.transform(ref, op)
} for (const ref of Editor.rangeRefs(editor)) {
RangeRef.transform(ref, op)
} // ...
}

apply方法的最开头就是三组for of循环,对所有的Ref执行对应的Ref.transform方法并传入当前执行的Operation

同样以Path为例,看下path-ref.ts中的PathRef.transform方法[源码]

export const PathRef: PathRefInterface = {
/**
* Transform the path ref's current value by an operation.
*/
transform(ref: PathRef, op: Operation): void {
const { current, affinity } = ref if (current == null) {
return
} const path = Path.transform(current, op, { affinity })
ref.current = path if (path == null) {
ref.unref()
}
}
}

将当前ref中的数据和Operation作为参数,传递给对应的Path.transform,返回更新后的path值并赋值给ref.current。如果path为空,则说明当前ref指代的位置已经失效,调用卸载方法unref

至于Path.transform的细节在本篇就不展开了,我们留到后续的Transform篇再统一讲解: )

slate源码解析(三)- 定位的更多相关文章

  1. Celery 源码解析三: Task 对象的实现

    Task 的实现在 Celery 中你会发现有两处,一处位于 celery/app/task.py,这是第一个:第二个位于 celery/task/base.py 中,这是第二个.他们之间是有关系的, ...

  2. Mybatis源码解析(三) —— Mapper代理类的生成

    Mybatis源码解析(三) -- Mapper代理类的生成   在本系列第一篇文章已经讲述过在Mybatis-Spring项目中,是通过 MapperFactoryBean 的 getObject( ...

  3. ReactiveCocoa源码解析(三) Signal代码的基本实现

    上篇博客我们详细的聊了ReactiveSwift源码中的Bag容器,详情请参见<ReactiveSwift源码解析之Bag容器>.本篇博客我们就来聊一下信号量,也就是Signal的的几种状 ...

  4. ReactiveSwift源码解析(三) Signal代码的基本实现

    上篇博客我们详细的聊了ReactiveSwift源码中的Bag容器,详情请参见<ReactiveSwift源码解析之Bag容器>.本篇博客我们就来聊一下信号量,也就是Signal的的几种状 ...

  5. React的React.createRef()/forwardRef()源码解析(三)

    1.refs三种使用用法 1.字符串 1.1 dom节点上使用 获取真实的dom节点 //使用步骤: 1. <input ref="stringRef" /> 2. t ...

  6. slate源码解析(一)- 序言

    笔者从大学时期就开始接触的前端,在刚去实习的时候就被导师安排去做内网的一个小富文本工具.之后从毕业后干的第一份工作游戏客户端,到现在做着可视化相关的前端工作,都有在做富文本相关的内容.可以说是和富文本 ...

  7. slate源码解析(二)- 基本框架与数据模型

    源码架构 首先来看下最核心的slate包下的目录: 可以看到,作为一个开源富文本库,其源码是相当之少.在第一篇文章中说过,Slate没有任何开箱即用的功能,只提供给开发者用于构建富文本所需的最基本的一 ...

  8. Sentinel源码解析三(滑动窗口流量统计)

    前言 Sentinel的核心功能之一是流量统计,例如我们常用的指标QPS,当前线程数等.上一篇文章中我们已经大致提到了提供数据统计功能的Slot(StatisticSlot),StatisticSlo ...

  9. Spring源码解析三:IOC容器的依赖注入

    一般情况下,依赖注入的过程是发生在用户第一次向容器索要Bean是触发的,而触发依赖注入的地方就是BeanFactory的getBean方法. 这里以DefaultListableBeanFactory ...

  10. jQuery 源码解析(三) pushStack方法 详解

    该函数用于创建一个新的jQuery对象,然后将一个DOM元素集合加入到jQuery栈中,最后返回该jQuery对象,有三个参数,如下: elems Array类型 将要压入 jQuery 栈的数组元素 ...

随机推荐

  1. 云上使用 Stable Diffusion ,模型数据如何共享和存储

    随着人工智能技术的爆发,内容生成式人工智能(AIGC)成为了当下热门领域.除了 ChatGPT 之外,文本生成图像技术更令人惊艳. Stable Diffusion,是一款开源的深度学习模型.与 Mi ...

  2. java后端接入微信小程序登录功能

    前言 此文章是Java后端接入微信登录功能,由于项目需要,舍弃了解密用户信息的session_key,只保留openid用于检索用户信息 后端框架:spring boot 小程序框架:uniapp 流 ...

  3. 洛谷 P8179 Tyres

    滴叉题/se/se 题意 直接复制了 有 \(n\) 套轮胎,滴叉需要用这些轮胎跑 \(m\) 圈.使用第 \(i\) 套轮胎跑的第 \(j\) 圈(对每套轮胎单独计数)需要 \(a_i+b_i(j- ...

  4. 如何使用libavfilter库给pcm音频采样数据添加音频滤镜?

    一.初始化音频滤镜 初始化音频滤镜的方法基本上和初始化视频滤镜的方法相同,不懂的可以看上篇博客,这里直接给出代码: //audio_filter_core.cpp #define INPUT_SAMP ...

  5. ChatGPT「代码解释器」正式开放,图片转视频仅需30秒!十大令人震惊的魔法揭秘

    经过超过三个月的等待,ChatGPT「代码解释器」终于全面开放.这是一大波神奇魔法的高潮. OpenAI的科学家Karpathy对这个强大的代码解释器测试版赞不绝口.他把它比作你的个人数据分析师,可以 ...

  6. 基于GPT搭建私有知识库聊天机器人(五)函数调用

    文章链接: 基于GPT搭建私有知识库聊天机器人(一)实现原理 基于GPT搭建私有知识库聊天机器人(二)环境安装 基于GPT搭建私有知识库聊天机器人(三)向量数据训练 基于GPT搭建私有知识库聊天机器人 ...

  7. 2023-07-17:给定一个数组arr,长度为n, 再给定一个数字k,表示一定要将arr划分成k个集合, 每个数字只能进一个集合。 返回每个集合内部的平均值都累加起来最小的值。 平均值向下取整。 1

    2023-07-17:给定一个数组arr,长度为n, 再给定一个数字k,表示一定要将arr划分成k个集合, 每个数字只能进一个集合. 返回每个集合内部的平均值都累加起来最小的值. 平均值向下取整. 1 ...

  8. linux_mint_21 vim配置:剪贴板支持和输入法自动切换

    一.vim的剪贴板支持 有的vim版本不支持系统剪切板,也就是说在vim编辑器外面复制的内容,不能够粘贴到vim中;在vim中通过yy.d.c复制剪切的内容也不能粘贴在vim编辑器外面. 这实在是有点 ...

  9. JSGRID loaddata显示超级多空行

    这个逼问题困扰了我两天了 作为一个主后端的程序员 初体验前端技术栈真的麻之又麻 以防万一 请先确认 是不是和我一个情况 如果是 请往下看 首先 我们需要念一段咒语 json是json string是s ...

  10. 一:wince 开发环境

    1:下载相关文件,vs2008 可以自行搜索安装 链接:https://pan.baidu.com/s/1b2shwCqmc1o9x-zsy8CmeA 提取码:qing