You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
I am creating a TeX editor using a decorator. When I add an equation before another one, the new equation take the value from the second one :
React.App.-.Google.Chrome.2022-09-01.20-25-34.mp4
I think the problem come from the state inside the Equation component which is not initialized for each Equation. It is probably an expected behaviour but I don't know how to do it correctly.
I precise that I don't want to use Block like in the documentation example because I want my equation to be inline.
The only solution I tought about would be to move the Equation edition feature outside of the decorator.
I did a simplified version of the code that replicate the bug :
import{useState}from'react'import{Editor,EditorState,CompositeDecorator,Modifier,}from'draft-js'import'draft-js/dist/Draft.css'functionfindEquationEntities(contentBlock,callback,contentState){contentBlock.findEntityRanges((character)=>{constentityKey=character.getEntity()return(entityKey!==null&&contentState.getEntity(entityKey).getType()==='EQUATION')},callback)}functionEquation({ contentState, entityKey }){const{math: initialMath}=contentState.getEntity(entityKey).getData()const[math,setMath]=useState(initialMath||'')//This following state is not initialized for each Equation. For the equation containing "b" we have the following console.log result ://console.log(initialMath) will be "b"//console.log(math) will be "c"return<b>{math}</b>}functiongetSelectedText(editorState){constselectionState=editorState.getSelection()constanchorKey=selectionState.getAnchorKey()constcurrentContent=editorState.getCurrentContent()constcurrentContentBlock=currentContent.getBlockForKey(anchorKey)conststart=selectionState.getStartOffset()constend=selectionState.getEndOffset()constselectedText=currentContentBlock.getText().slice(start,end)returnselectedText}functionaddEntity(type,mutability,data,editorState,setEditorState){constselectionState=editorState.getSelection()constcontentState=editorState.getCurrentContent()constcontentStateWithEntity=contentState.createEntity(type,mutability,data)constentityKey=contentStateWithEntity.getLastCreatedEntityKey()constcontentStateWithHelp=Modifier.applyEntity(contentStateWithEntity,selectionState,entityKey)constnewEditorState=EditorState.set(editorState,{currentContent: contentStateWithHelp,})returnsetEditorState(newEditorState)}functionApp(){constdecorator=newCompositeDecorator([{strategy: findEquationEntities,component: Equation,},])const[editorState,setEditorState]=useState(()=>EditorState.createEmpty(decorator))functionturnIntoEquation(){constmath=getSelectedText(editorState)addEntity('EQUATION','IMMUTABLE',{ math },editorState,setEditorState)}return(<><EditoreditorState={editorState}onChange={setEditorState}/><buttononClick={turnIntoEquation}>Turn into equation</button></>)}exportdefaultApp
Fun behaviours :
It does not occur without text before the equation :
React.App.-.Google.Chrome.2022-09-01.20-44-46.mp4
The behaviour disappears if I create a third equation before :
React.App.-.Google.Chrome.2022-09-01.20-44-29.mp4
The text was updated successfully, but these errors were encountered:
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I am creating a TeX editor using a decorator. When I add an equation before another one, the new equation take the value from the second one :
React.App.-.Google.Chrome.2022-09-01.20-25-34.mp4
I think the problem come from the state inside the Equation component which is not initialized for each Equation. It is probably an expected behaviour but I don't know how to do it correctly.
I precise that I don't want to use Block like in the documentation example because I want my equation to be inline.
The only solution I tought about would be to move the Equation edition feature outside of the decorator.
I did a simplified version of the code that replicate the bug :
Fun behaviours :
It does not occur without text before the equation :
React.App.-.Google.Chrome.2022-09-01.20-44-46.mp4
The behaviour disappears if I create a third equation before :
React.App.-.Google.Chrome.2022-09-01.20-44-29.mp4
The text was updated successfully, but these errors were encountered: