lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Feature: According to the socket push data to achieve incremental document update

Open wuyxp opened this issue 4 years ago • 2 comments

I'm using lexical as our document component. Documents carry large amounts of data, and we support collaborative editing of documents. So, we think we can capitalize on the ability of reconcile, maximizing the performance of reducing the data network transfer overhead and lexical redraw.

Let's think about it in two parts

  1. In terms of data storage, neither html string storage nor json structure obtained by editor.getEditorState() can be used. image

The above two storage methods will store data in a complete data structure, which is not suitable for partial transmission when incremental push. So what we want to save is the following structure. store the list structure with the key in the _nodeMap to the server so that it can be stored incrementally and also contains the key information to reduce dom updates when they need to be updated.

const obj = [...editor?.getEditorState()._nodeMap.entries()].reduce((obj, [key, value]) => (obj[key] = value, obj), {})
setEditorContentJSON(JSON.stringify(obj))
image
  1. We can do this when partial data is pushed through the socket.
     const editorState = editor?.getEditorState()
      const nodeMap = editorState?._nodeMap
// First declare the default map data to be updated
      const dirtyMap = new Map()

// 先将当前 state 的所有 map 进行获取并且设置
      nodeMap?.forEach((value) => {
        value && dirtyMap.set(value.__key, value)
      })

// Obtain and set all maps of the current state
      dirtyArr.forEach((value) => {
        value && dirtyMap.set(value.__key, value)
      })

// Finally, all data is mounted to the corresponding prototype method by type.
              Object.entries(editorState).forEach(([key, value]) => {
                switch (value.__type) {
                  case 'root':
                    dirtyMap.set(key,Object.assign($createRootNode(),value))
                    break;
                  case 'paragraph':
                    dirtyMap.set(key,Object.assign($createParagraphNode(),value))
                    break;
                  case 'text':
                    dirtyMap.set(key,Object.assign($createTextNode(),value))
                    break;
                }
              })

// Then the map is generated to generate the state of the letter, and the set is updated
              const newState = new EditorState(dirtyMap)
              editor?.setEditorState(newState)
// TODO: The problem here is that EditorState isn't exposed in the source code, so it needs to be

Thank you so much for reading this: My English is not good, the above are all used Google Translate, if the sentence is not smooth, please forgive me. The above solution is temporarily decided based on the capability of lexical supply. Of course, lexical supply will provide the function of local update as well as the function of shard data storage. If there are other solutions, please reply in time to improve our documentation performance and code robustness.

wuyxp avatar Nov 30 '22 02:11 wuyxp

Commented on in PR #3458

thegreatercurve avatar Dec 01 '22 10:12 thegreatercurve

Exposing the internal implementation would mean that we can't change it without causing a major breaking version change. The details of the EditorState are intended to be private and thus why we don't expose them. You can use editorState.read() to get the contents using the public API.

trueadm avatar Dec 01 '22 10:12 trueadm