0.0.26 • Published 6 months ago

zeort-slate-editor v0.0.26

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

slate-editor

Usage

import RichTextEditor from "@zhiyou/slate-editor";
import { useState } from "react";

const App = () {
    const [text] = useState([
        type: "paragraph",
        textAlign: "left",
        lineHeight: "1.25",
        children: [
          {
            text: "A line of text in a paragraph.",
            fontName: "Arial",
            fontSize: "16px",
            fontColor: "#000000",
          }
        ]
    ])

    return <RichTextEditor initialValue={text} />
};

export default App;

API

参数说明类型
readonly是否为只读属性boolean
placeholder没有值时默认填充的文本string
initialValue初始文本内容any[]
linkToolZIndex链接弹框的 z-indexnumber
hoverLinkToolBar修改/删除链接时的弹框ReactNode
extraComponent额外扩展组件ExtraComponentType[]
onCompactState当选中内容的 state 发生变化时,调用此函数function (state: Record<string, StateType>)
onChange当编辑器发生变化时,调用此函数function (value: Descendant[])
onKeyDown当编辑器按下键盘时,调用此函数function (event: React.KeyboardEvent)

Ref

名称说明
editor对象存储 Slate 编辑器的所有状态,拿到这个才可以对编辑器内容进行修改操作
getInnerHTML获取编辑器生成的 HTML 代码

1. Utils

组件提供的一些可供使用的函数

import RichTextEditor, { SlateEditor, utils } from "@zhiyou/slate-editor";
import { useRef } from "react";

const App = () {
    const richTextRef = useRef<SlateEditor.EditorRefProps>(null)

    const onBtnClick = () => {
        if (!richTextRef.current) return
        const { editor } = richTextRef.current

        utils.updateEditor(editor, [
            {
                type: 'paragraph',
                textAlign: 'left',
                lineHeight: '1.25',
                children: [{ text: 'A line of text in a paragraph.' }]
            }
        ])
    }

     return (<div>
       <RichTextEditor ref={richTextRef} />
       <button onClick={onBtnClick}></button>
     </div>)
};

export default App;
名称说明类型
alignmentMap对齐方式的映射Record<string, TextAlignType>
withExtraEditor扩展组件在 Editor 中设置类型规则function(editor: Editor): Editor
isBooleanMarkType是否 mark 为 boolean 类型的 markfunction(type: any): boolean
isStringMarkType是否 mark 为 string 类型的 markfunction(type: any): boolean
isAlignType是否为对齐方式的类型function(type: any): boolean
focusSelectAll编辑器全选function(editor: Editor): void
resetFocusRange重新设置焦点function(editor: Editor): void
updateEditor更新富文本编辑器function(editor: Editor, value: any[]): void
EditorCommand编辑器操作命令EditorCommandType

EditorCommand (编辑器操作命令)

集成了所有操作命令,包含状态获取,插入,删除,更新

import RichTextEditor, { utils } from "@zhiyou/slate-editor";
...
//ref 获取省略(参考上边代码)
const { editor } = richTextRef.current
const { EditorCommand } = utils
const command = new EditorCommand(editor)

// 状态
const linkState = command.state('link')
console.log(linkState)
// 插入
command.insert('link', {url: 'http://www.xxx.com'})
// 修改
command.update('link', {url: 'http://www.yyy.com'})
// 删除链接
command.remove('link')
...

基本组件:

  1. BlockQuote(缩进)
    state(editor: Editor): { blockQuote: StateType }
    
    insert(editor: Editor): void
    
    update(editor: Editor): void
    
    remove(editor: Editor): void
  2. BulletedList(无序列表)
    state(editor: Editor): { unOrderedList: StateType }
    
    insert(editor: Editor): void
    
    remove(editor: Editor): void
  3. NumberedList(有序列表)
    state(editor: Editor): { orderedList: StateType }
    
    insert(editor: Editor): void
    
    remove(editor: Editor): void
  4. Divider(分割线)
    state(editor: Editor): { divider: StateType }
    
    insert(editor: Editor, props: { color: string }): void
    
    update(editor: Editor, props: { color: string }): void
  5. ItemList(list 列表 \)
    update(editor: Editor, props: { textAlign?: string, lineHeight?: string | number, fontSize?: string, fontColor?: string, fontName?: string, bold?: boolean, italic?: boolean }, options?: any): void
  6. Leaf(文本节点)
    state(editor: Editor): { bold: StateType, italic: StateType, underline: StateType, strikethrough: StateType, fontName: StateType, fontColor: StateType, fontSize: StateType }
    
    insert(editor: Editor, props: { bold?: boolean, italic?: boolean, underline?: boolean, strikethrough?: boolean }): void
    
    update(editor: Editor, props: { fontName?: string, fontColor?: string, fontSize?: string }): void
    
    //特殊 清空所有格式 props: { all: true }
    remove(editor: Editor, props: { bold?: boolean, italic?: boolean, underline?: boolean, strikethrough?: boolean, all: boolean }): void
  7. Link(链接)
    state(editor: Editor): { link: StateType }
    
    insert(editor: Editor, props: { url: string, fontColor?: string }): void
    
    update(editor: Editor, props: { url: string }): void
    
    remove(editor: Editor): void
  8. Paragraph(段落)
    state(editor: Editor): { lineHeight: StateType, justifyCenter: StateType, justifyFull: StateType, justifyLeft: StateType, justifyRight: StateType }
    
    update(editor: Editor, props: { lineHeight: string | number, textAlign: string }): void

2. ShortcutKey

导出部分常用的快捷键指令

3. SlateEditor

编辑器中的类型声明

4. slate

Introduction - Slate (slatejs.org)

5.slateReact

Slate React - Slate (slatejs.org)

0.0.26

6 months ago

0.0.25

6 months ago

0.0.24

6 months ago

0.0.23

6 months ago

0.0.22

6 months ago

0.0.21

6 months ago

0.0.20

6 months ago

0.0.19

6 months ago

0.0.18

6 months ago

0.0.17

6 months ago

0.0.16

6 months ago

0.0.15

6 months ago

0.0.14

6 months ago

0.0.13

6 months ago

0.0.12

6 months ago

0.0.11

6 months ago

0.0.10

6 months ago

0.0.9

6 months ago

0.0.8

6 months ago

0.0.7

6 months ago

0.0.6

6 months ago

0.0.5

6 months ago

0.0.4

6 months ago

0.0.3

6 months ago

0.0.2

6 months ago

0.0.1

6 months ago

0.0.0

6 months ago