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.pswp.cn/news/393158.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393158.shtml
英文地址,請注明出處:http://en.pswp.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語句的書寫順序并不是一樣的,而是按照下面的…