Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lexical does not render images when setting editorState #57

Open
mmolinahn opened this issue Feb 16, 2024 · 4 comments
Open

Lexical does not render images when setting editorState #57

mmolinahn opened this issue Feb 16, 2024 · 4 comments

Comments

@mmolinahn
Copy link

I currently have this code to render the LexicalView:

        let theme = Theme()
        let editorConfig = EditorConfig(theme: theme, plugins: [InlineImagePlugin()])
        let lexicalView = LexicalView(editorConfig: editorConfig, featureFlags: FeatureFlags())
        
        let editor = lexicalView.editor
        
       if let newEditorState = try? EditorState.fromJSON(json: jsonString, editor: editor) {
            try? editor.setEditorState(newEditorState)
        }

the value for jsonString used above is: "{\"root\":{\"children\":[{\"children\":[{\"src\":\"https://t4.ftcdn.net/jpg/01/43/42/83/360_F_143428338_gcxw3Jcd0tJpkvvb53pfEztwtU9sxsgT.jpg\",\"maxWidth\":\"100%\",\"maxHeight\":800,\"width\":10,\"height\":20,\"type\":\"image\",\"version\":1}],\"direction\":null,\"format\":\"\",\"indent\":0,\"type\":\"paragraph\",\"version\":1}],\"direction\":null,\"format\":\"\",\"indent\":0,\"type\":\"root\",\"version\":1}}"

I expect the image to render in the lexical view, but it just shows empty.

In order to render any image in LexicalView I have to run this:

        try? editor.update {
            let imageNode = ImageNode(url: "https://t4.ftcdn.net/jpg/01/43/42/83/360_F_143428338_gcxw3Jcd0tJpkvvb53pfEztwtU9sxsgT.jpg", size: CGSize(width: 300, height: 300), sourceID: "")
              if let selection = try getSelection() {
                _ = try selection.insertNodes(nodes: [imageNode], selectStart: false)
              }
        }

The problem with this approach is that it renders the inline image functionality worthless when I have to manually insert images.

Is this expected behavior? Or am I using it wrong?

For reference this is the entire class (it's a SwiftUI project so it is in a UIViewRepresentable):

import Foundation
import Lexical
import SwiftUI
import LexicalInlineImagePlugin

struct LexicalReadOnlyViewRepresentable: UIViewRepresentable {
    @State var jsonString: String
    
    func updateUIView(_ uiView: UIViewType, context: Context) {
    }
    
    func makeUIView(context: Context) -> some UIView {
        let theme = Theme()
        let editorConfig = EditorConfig(theme: theme, plugins: [InlineImagePlugin()])
        let lexicalView = LexicalView(editorConfig: editorConfig, featureFlags: FeatureFlags())
        
        let editor = lexicalView.editor
        
       if let newEditorState = try? EditorState.fromJSON(json: jsonString, editor: editor) {
            try? editor.setEditorState(newEditorState)
        }
        
       // have to write this code for any images to show up
        try? editor.update {
           
            let imageNode = ImageNode(url: "https://t4.ftcdn.net/jpg/01/43/42/83/360_F_143428338_gcxw3Jcd0tJpkvvb53pfEztwtU9sxsgT.jpg", size: CGSize(width: 300, height: 300), sourceID: "")
              if let selection = try getSelection() {
                _ = try selection.insertNodes(nodes: [imageNode], selectStart: false)
              }
        }
        
        
        return lexicalView
    }
}
@mmolinahn
Copy link
Author

mmolinahn commented Feb 19, 2024

I mostly want to clarify if my expectation of loading the state should render images is correct or not.
@amyworrall any help appreciated!

@mmolinahn
Copy link
Author

It looks like the url is not being decoded from the EditorState we pass. url (which is a different key than what the JS version uses, src) is not one of the CodingKeys for ImageNode and it is not accounted for in the decode method.

Is this intentional? Treating the ImageNode different than other nodes?

@amyworrall
Copy link
Contributor

amyworrall commented Feb 22, 2024 via email

@mmolinahn
Copy link
Author

Thanks Amy! I'll work on the PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants