In my React component I use draft-js library. In my text input there is onPaste event that pastes text with the type of 'ordered-list-item'. It results in the list being pasted with numerical order. However after I send the message it just sends text itself without numbers for each line. What can I do in order to send numbers as well?
Assistant
User
I have a plain text. How do I convert it to BlockMap using draft-js?
Assistant
To convert a plain text to a BlockMap using draft-js, you can create a ContentState and extract the necessary components from it. Here's an example of how you can achieve this:
To send the message along with the numbers for each line of the ordered list, you can extract the text/content from the Draft.js editor and manually prepend the numbers to each line. Here's an example of how you can achieve this:
jsx
import React, { useState } from 'react';
import { Editor, EditorState, convertToRaw, convertFromRaw } from 'draft-js';
const MyComponent = () => {
const [editorState, setEditorState] = useState(() =>
EditorState.createEmpty()
);
const handlePaste = (event) => {
const clipboardData = event.clipboardData || window.clipboardData;
const pastedText = clipboardData.getData('text/plain');
const orderedListText = addNumbersToList(pastedText);
const contentState = convertFromRaw({
entityMap: {},
blocks: [
{
text: orderedListText,
type: 'unstyled',
depth: 0,
inlineStyleRanges: [],
entityRanges: [],
},
],
});
const newEditorState = EditorState.push(
editorState,
contentState,
'insert-characters'
);
setEditorState(newEditorState);
event.preventDefault(); // Prevent default paste behavior
};
const addNumbersToList = (text) => {
const lines = text.split('\n');
const orderedListText = lines
.map((line, index) => `${index + 1}. ${line}`) // Prepend number to each line
.join('\n');
return orderedListText;
};
const handleEditorChange = (newEditorState) => {
setEditorState(newEditorState);
};
const handleClickSend = () => {
const contentState = editorState.getCurrentContent();
const text = contentState.getPlainText('\n');
console.log(text); // This will include numbers for each line
// Send message with numbers to server or perform other actions
};
return (
<div>
<Editor
editorState={editorState}
onChange={handleEditorChange}
onPaste={handlePaste}
/>
<button onClick={handleClickSend}>Send Message</button>
</div>
);
};
export default MyComponent;
In the code snippet above, the handlePaste function is invoked when the onPaste event occurs in the Editor component. It extracts the pasted plain text from the clipboard and uses the addNumbersToList helper function to prepend numbers to each line. The modified text is then converted back to Draft.js content state and merged into the existing EditorState using EditorState.push. The updated EditorState is stored using the setEditorState hook.
When you click the "Send Message" button, the message content is extracted using getCurrentContent().getPlainText('\n') and will contain the numbers for each line. You can then proceed to send this message to the server or perform other actions as needed.
In the code snippet above, the convertPlainTextToBlockMap function takes a plain text as input and splits it into separate lines using split('\n'). It then maps over each line to create an array of ContentBlock objects, with each line being converted into a block. The genKey function is used to assign a unique key to each block.
After creating the blocks, a ContentState is created using ContentState.createFromBlockArray(blocks). Finally, the getBlockMap() method is used to extract the BlockMap from the ContentState. The resulting BlockMap can be used in Draft.js components or for further processing/manipulation.