惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
B
Blog
月光博客
月光博客
博客园 - 【当耐特】
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Jina AI
Jina AI
博客园 - Franky
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Last Week in AI
Last Week in AI
B
Blog RSS Feed
H
Help Net Security

时间的朋友

Windows 命令行密码重置 Anaconda安装 typescript 注解解读1 Konvajs Shape加载自定义图片 sshpass 使用 why-is-node-running webgl笔记 SharedArrayBuffer is not defined blender 常用快捷键 | 时间的朋友 vue -- v3.4commit提交记录2 vue2 升级vue3报错问题整理 着色器 expressjs 源码 hyper-V arch linux 网络配置 element input数字格式化 three 拼接货架 WebAudio笔记 Windows nginx重启bat脚本 vue -- v3.4commit提交记录 URI malformed vue3 -- Class 对象在组件中使用范例 | 时间的朋友 ruby 安装和升级 element-plus 老版本cascader使用卡死问题 vue3 内置Transition组件 | 时间的朋友 前端memo的实现 | 时间的朋友 Vue -- vue-class-component源码 | 时间的朋友 linux 优化脚本 typescript 装饰器 | 时间的朋友 microbundle 源码 | 时间的朋友 WSL2问题解决WslRegisterDistribution failed with error: 0x800701bc
slate.js 富文本编辑器 | 时间的朋友
2022-07-24 · via 时间的朋友

Published: · LastMod: July 27, 2022 · 776 words

slate.js 🔗

v0.47

slate是开源社区最新出的一个比较火的可定制化的富文本编辑器

https://github.com/ianstormtaylor/slate

slate 是一个底层核心库,如果要使用,还需要配合slate-react等进行UI部分展示

安装 🔗

yarn add slate slate-react

使用 🔗

slate使用的是基于js对象,通过slate内核进行转换后,用slate-react最终映射到真实dom上

slate传入的初始化的值格式为

1
2
3
4
5
6
[
  {
    type: string,
    children: []
  }
]

最顶层的type就是paragraph, 表示段落

段落的子元素又是一个个对象,可以用不同的type进行区分,其中children中也可以写入一个text对象,表示当前就是文本节点

基础渲染 🔗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import React, { useMemo, useCallback } from 'react'
import { Editor, BaseEditor } from "slate";
import {
  Slate,
  Editable,
  withReact,
  ReactEditor
} from "slate-react";

const initialValue: Descendant[] = [
  {
    type: "paragraph",
    children: [
      {
        text: "With Slate you can build complex block types that have their own embedded content and behaviors, like rendering checkboxes inside check list items!",
      },  
    ],
  },
];

export const App = ({ p }: any) => {
 
  const editor: ReactEditor = useMemo(
    () => withHistory(withReact(createEditor())),
    []
  );

  return (
    <Slate editor={editor} value={initialValue}>
      <Editable
        placeholder="Get to work…"
      />
    </Slate>
  );
};

添加监听事件 🔗

slate-react封装的Editable组件就是类似于contenteditable, 其中暴露了一系列的监听事件可以对外使用

1
2
3
4
5
6
7
8
9
<Editable
    onKeyDown={event => {
      if (event.key === '&') {
        // 可以监听到对应的值
        event.preventDefault()
        editor.insertText('and')
      }
    }}
/>

自定义渲染元素 🔗

https://docs.slatejs.org/walkthroughs/03-defining-custom-elements

实际在使用过程中富文本编辑器中需要渲染其他效果,比如说高亮,文字加颜色等

Editable组件留下了一个渲染入口,可以根据不同的type去渲染不同的组件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
const renderElement = useCallback((props: any) => <Element {...props} />, []);



const Element = (
  props: JSX.IntrinsicAttributes & {
    attributes: any;
    children: any;
    element: any;
  }
) => {
  const { attributes, children, element } = props;
	// 根据不同的type类型渲染不同的元素
  switch (element.type) {
    case "check-list-item":
      return <CheckListItemElement {...props} />;
    case "pinyin":
      return <RenderHanzi {...props} />;
    default:
      return <DefaultElement {...attributes}>{children}</DefaultElement>;
  }
};

最后在Editable上使用

1
2
3
4
 <Editable
   			// ....
        renderElement={renderElement}
  />