-
Notifications
You must be signed in to change notification settings - Fork 58
Description
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
}
}