draft.js_如何使用快捷方式在Draft.js中创建有序列表和无序列表

draft.js

by Andrey Semin

通过安德烈·塞米(Andrey Semin)

如何使用快捷方式在Draft.js中创建有序列表和无序列表 (How to create ordered and unordered lists in Draft.js with a shortcut)

We at Propeller have encountered many differences between Draft.js and popular text editors. We also found some issues like controlling list depth and multiline items in lists. The biggest difference is the inability to use shortcuts to start a list by default. Surprisingly enough you need to implement this logic yourself.

Propeller的我们在Draft.js和流行的文本编辑器之间遇到了许多差异。 我们还发现了一些问题,例如控制列表深度和列表中的多行项目。 最大的区别是默认情况下无法使用快捷方式启动列表。 令人惊讶的是,您需要自己实现此逻辑。

As always, there is a plugin available to add support for the shortcuts you use. I also want to refer to draft-js-autolist-plugin as a source of inspiration. For some reason, this plugin didn’t work when we tried it. So we’ve come up with our own solution which is now presented in this post.

与往常一样,有一个插件可用于添加对您所使用快捷方式的支持。 我也想参考draft-js-autolist-plugin作为灵感来源。 出于某种原因,当我们尝试该插件时无法使用。 因此,我们提出了自己的解决方案,该解决方案现在将在本文中介绍。

问题 (The problem)

Open Google Docs, Word365, or whatever editor you use. Try to type * and then type space. Boom! You’ve started an unordered list. Nice feature to have, right?

打开Goog​​le文档,Word365或您使用的任何编辑器。 尝试键入* ,然后键入space 。 繁荣! 您已经开始了无序列表。 不错的功能,对不对?

If we try the exact same trick with the default Draft.js configuration, we will get nothing but plain text.

如果我们使用默认的Draft.js配置尝试完全相同的技巧,则只会得到纯文本。

Let’s change it!

让我们改变它!

(Solution)

To implement this feature, we need to keep track of the last three pressed buttons. Why three? Well, that’s because the longest character combination we need to support is 1. + space which is exactly three presses.

要实现此功能,我们需要跟踪最后按下的三个按钮。 为什么是三个? 好吧,这是因为我们需要支持的最长字符组合是1. + space ,正好是三按。

To start, let’s implement the logic to store these presses. Here we would use a simple array named history. This array will store the value of the key that was pressed. We definitely don’t want to process any key presses with modifiers like shift, alt and so on. We can use Draft.js built-inKeyBindingUtil.hasCommandModifier function to perform the check for any modifier.

首先,让我们实现存储这些印刷机的逻辑。 在这里,我们将使用一个名为history的简单数组。 该数组将存储所按下键的值。 我们绝对不希望使用shiftalt等修饰符来处理任何按键。 我们可以使用Draft.js内置的KeyBindingUtil.hasCommandModifier函数执行对任何修饰符的检查。

Draft.js exposes a keyDown event for us in the keyBindingFn prop function. We are going to check if we need to start a list here. If so, we need to return a so calledDraftEditorCommand, which is a string. Also, to benefit from OS-level commands we need to add a getDefaultKeyBinding function call as a fall-through case.

Draft.js在keyBindingFn prop函数中为我们公开了一个keyDown事件。 我们将检查是否需要在此处开始列表。 如果是这样,我们需要返回一个所谓的DraftEditorCommand ,它是一个字符串。 另外,要从操作系统级别的命令中受益,我们需要添加getDefaultKeyBinding函数调用作为getDefaultKeyBinding案例。

We need to check if the currently pressed key is a space. If so we would run our checks against the history array. We check if we have a suitable set of previously pressed keys — * for an unordered list and 1. for an ordered one. If we find a match, we return a command(string) to be processed later.

我们需要检查当前按下的键是否为space 。 如果是这样,我们将对history数组进行检查。 我们检查是否有一组合适的先前按下的键- *表示无序列表, 1. .表示有序键。 如果找到匹配项,则返回一个命令(字符串),以便稍后处理。

Now we need to implement the handleKeyCommand prop function and pass it to the editor. The logic is pretty simple. If we get one of our custom commands, we check if we should start a list on the current block. So here is a skeleton of the handleKeyCommand function.

现在,我们需要实现handleKeyCommand prop函数并将其传递给编辑器。 逻辑很简单。 如果得到自定义命令之一,则检查是否应在当前块上启动列表。 因此,这是handleKeyCommand函数的框架。

To check if we are good to start a list, we check if the currently selected block satisfies all three of the following rules:

要检查是否可以很好地启动列表,请检查当前选定的块是否满足以下所有三个规则:

  • The block type is unstyled

    块类型是无unstyled

  • The block has a depth of 0

    块的depth为0

  • the block has * or 1. as a text

    该块具有*1.作为文本

Let’s wrap it up with the code:

让我们用代码包装一下:

Now we’re able to catch the exact case where Draft.js needs to start a list! Now it’s a time to implement the startList function.

现在,我们可以捕捉到Draft.js需要启动列表的确切情况! 现在是时候实现startList函数了。

First of all, we need to map our custom commands to a particular list style. This means we need to start an unordered list for the start-unoredered-list command.

首先,我们需要将自定义命令映射到特定的列表样式。 这意味着我们需要为start-unoredered-list命令启动一个无序列表。

We start an ordered list for the start-ordered-list command. Next, we need to update the styling of the block to the selected type. To do it we would use the toggleBlockType function of RichUtils module, which comes as a part of Draft.js.

我们为start-ordered-list命令启动一个有序列表。 接下来,我们需要将块的样式更新为选定的类型。 为此,我们将使用RichUtils模块的toggleBlockType函数,该函数是RichUtils的一部分。

Next we need to replace the shortcut text we’ve entered with an empty string. To do it we need to call the replaceText method of the Modifier module. This method requires a selection range to determine what should be replaced. We need to get the selection out of the block and update it to have focusOffset value equal to block length. This combination means we want to replace the whole text we’ve entered.

接下来,我们需要将输入的快捷方式文本替换为空字符串。 为此,我们需要调用Modifier模块的replaceText方法。 此方法需要一个选择范围来确定应替换的内容。 我们需要从块中取出选择并进行更新,以focusOffset值等于块长度。 这种组合意味着我们要替换输入的整个文本。

Great! Now we need to update our local editor state with the new state we get from the startList function. So let’s bring it all together!

大! 现在,我们需要使用从startList函数获得的新状态来更新本地编辑器状态。 因此,让我们将它们放在一起!

OK! We’re almost done! But there is one more moment we need to handle. In some cases when one of our custom commands fire, we should not start a list based on the output of the shouldStartList function. We need to process the insertion of the space manually.

好! 我们快完成了! 但是,我们还有另外一刻需要处理。 在某些情况下,当我们的自定义命令之一触发时,我们不应基于shouldStartList函数的输出来启动列表。 我们需要手动处理空间的插入。

For implementation details of the getSelectedBlock method, check out my previous post on this Draft.js topic!

有关getSelectedBlock方法的实现细节,请查看我以前关于此Draft.js主题的文章!

To do this we may want to use a method called insertText of the Modifier module. Obviously enough, it is used to build a new content state with the provided text inserted into it. As always, we need to provide the current content state, current selection state and the text we want to insert (a single space in our case).

为此,我们可能需要使用Modifier模块的一个名为insertText的方法。 显然,它用于通过插入所提供的文本来构建新的内容状态。 与往常一样,我们需要提供当前的内容状态,当前的选择状态和我们要插入的文本(在本例中为单个空格)。

We need to add a call to this function to our handleKeyCommand function. So here is the final version of it:

我们需要在handleKeyCommand函数中添加对此函数的调用。 所以这是它的最终版本:

If you’ve read this post all the way through, you may also want to check out my previous post about Draft.js enchantment. You may want to apply it to your project as well.

如果您已经阅读了所有文章,那么您可能还想看看我以前有关Draft.js附魔的文章。 您可能还希望将其应用于您的项目。

翻译自: https://www.freecodecamp.org/news/how-to-create-ordered-and-unordered-lists-in-draft-js-with-a-shortcut-5de34a1a570f/

draft.js

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393158.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

当javaScript从入门到提高前需要注意的细节:变量部分

到了HTML5的时代,对javaScript的要求不是降低了,而是更提高了。javaScript语言的入门非常简单,如果你有java、C#等C风格的结构化语言的基础,那javaScript你最多半天就可以写点什么了。但是javaScript是一种动态语言,这…

PAT乙级 1003. 我要通过!

题目: “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。 得到“答案正确”的条件是: 1. 字符串中必须仅有…

电台复活节_如何通过在控制台中隐藏复活节彩蛋使您的应用程序用户惊讶

电台复活节by Ethan Ryan由伊桑瑞安(Ethan Ryan) 如何通过在控制台中隐藏复活节彩蛋使您的应用程序用户惊讶 (How to surprise your app’s users by hiding Easter eggs in the console) I love console.logging(“stuff”).我喜欢console.logging(“ stuff”)。 I do it th…

leetcode1267. 统计参与通信的服务器(dfs)

这里有一幅服务器分布图,服务器的位置标识在 m * n 的整数矩阵网格 grid 中,1 表示单元格上有服务器,0 表示没有。 如果两台服务器位于同一行或者同一列,我们就认为它们之间可以进行通信。 请你统计并返回能够与至少一台其他服务…

System类入门学习

第三阶段 JAVA常见对象的学习 System类 System类包含一些有用的字段和方法,他不能被实例化 //用于垃圾回收 public static void gc()//终止正在运行的java虚拟机。参数用作状态码,根据惯例,非0表示异常终止 public static void exit(int stat…

gulpfile php,Laravel利用gulp如何构建前端资源详解

什么是gulp?gulp是新一代的前端项目构建工具,你可以使用gulp及其插件对你的项目代码(less,sass)进行编译,还可以压缩你的js和css代码,甚至压缩你的图片,gulp仅有少量的API,所以非常容易学习。 gulp 使用 st…

ios jenkins_如何使用Jenkins和Fastlane制作iOS点播构建系统

ios jenkinsby Agam Mahajan通过Agam Mahajan 如何使用Jenkins和Fastlane制作iOS点播构建系统 (How to make an iOS on-demand build system with Jenkins and Fastlane) This article is about creating iOS builds through Jenkins BOT, remotely, without the need of a de…

菜鸟也学hadoop(1)_搭建单节点的hadoop

其实跟官方的教程一样 只是 我想写下来 避免自己搞忘记了,,,,好记性不如烂笔头 首先确认自己是否安装了 java, ssh 以及 rsync 没有装的直接就 apt-get install 了嘛,,,java的不一定…

SP703 SERVICE - Mobile Service[DP]

题意翻译 Description   一个公司有三个移动服务员。如果某个地方有一个请求,某个员工必须赶到那个地方去(那个地方没有其他员工),某一时刻只有一个员工能移动。只有被请求后,他才能移动,不允许在同样的位…

CF758 D. Ability To Convert 细节处理字符串

link 题意:给定进制数n及一串数字,问在此进制下这串数能看成最小的数(10进制)是多少(如HEX下 1|13|11 475) 思路:此题要仔细思考细节。首先要想使数最小那么必定有个想法是使低位的数尽可能大即位数尽可能…

java 可能尚未初始化变量,java - 局部变量“变量”可能尚未初始化-Java - 堆栈内存溢出...

我得到这个错误。线程“主”中的异常java.lang.Error:未解决的编译问题:rgb2无法解析为变量它总是导致错误的rgb2数组。 如何解决这个问题呢?BufferedImage img1 ImageIO.read(file1);BufferedImage img2 ImageIO.read(file2);int w img1.…

leetcode1249. 移除无效的括号(栈)

给你一个由 ‘(’、’)’ 和小写字母组成的字符串 s。 你需要从字符串中删除最少数目的 ‘(’ 或者 ‘)’ (可以删除任意位置的括号),使得剩下的「括号字符串」有效。 请返回任意一个合法字符串。 有效「括号字符串」应当符合以下 任意一条 要求&…

软件工程——个人课程总结

软件工程,我就是冲着软件这两个字来的,开始我觉得我们大多数人也是这样的,能开发一款属于自己的软件应该是我们人生中的第一个小目标八,在上学期学完java语言后,我们自认为自己已经具备了开发一款小软件的能力&#xf…

规则网络_实用的网络可访问性规则

规则网络by Tiago Romero Garcia蒂亚戈罗梅罗加西亚(Tiago Romero Garcia) 实用的网络可访问性规则 (Pragmatic rules of web accessibility that will stick to your mind) I first started to work with web accessibility back in 2015, at an American retail giant. It h…

8-python自动化-day08-进程、线程、协程篇

本节内容 主机管理之paramiko模块学习 进程、与线程区别python GIL全局解释器锁线程语法join线程锁之Lock\Rlock\信号量将线程变为守护进程Event事件 queue队列生产者消费者模型Queue队列开发一个线程池进程语法进程间通讯进程池 转载:  http://www.cnblogs.co…

部署HDFS HA的环境

> 环境架构部署规划: bigdata1 NameNode ResourceManager Zookeeper JournalNode failOverController bigdata2 NameNode ResourceManager Zookeeper JournalNode failOverController bigdata3 DataNode NodeManager Zookeeper bigdata4 DataNode NodeManager &g…

php layui 框架,Thinkphp5+Layui高颜值内容管理框架

Thinkphp5Layui高颜值内容管理框架TP5Layui高颜值内容管理框架,新增API模块Thinkphp5Layui响应式后台权限管理系统专注打造好用的框架,极速开发,高效灵活,从架构上兼顾系统复杂度的迭代与需求多变。代码结构清晰,接口开…

leetcode657. 机器人能否返回原点

在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。 移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L&#xff…

在Angular专家Dan Wahlin的免费33部分课程中学习Angular

According to the Stack Overflow developer survey 2018, Angular is one of the most popular frameworks/libraries among professional developers. So learning it increases your chances of getting a job as a web developer significantly.根据2018年Stack Overflow开…

select查询语句执行顺序

查询中用到的关键词主要包含六个,并且他们的顺序依次为 select--from--where--group by--having--order by 其中select和from是必须的,其他关键词是可选的,这六个关键词的执行顺序 与sql语句的书写顺序并不是一样的,而是按照下面的…