CodeEditSourceEditor icon indicating copy to clipboard operation
CodeEditSourceEditor copied to clipboard

Using CodeEditSourceEditor does not display text

Open wisepmlin opened this issue 1 year ago • 2 comments

Description

截屏2024-07-08 11 48 23

To Reproduce

struct SerialReception: View { @Binding var document: String @State private var language: CodeLanguage = .default @State private var theme: EditorTheme = .wise @State private var font: NSFont = NSFont.monospacedSystemFont(ofSize: 12, weight: .regular) @AppStorage("wrapLines") private var wrapLines: Bool = true @State private var cursorPositions: [CursorPosition] = [] var body: some View { CodeEditSourceEditor( $document, language: language, theme: theme, font: font, tabWidth: 4, lineHeight: 1.2, wrapLines: wrapLines, cursorPositions: $cursorPositions ) } }

Expected Behavior

截屏2024-07-08 11 54 30

Version Information

CodeEditSourceEditor: [e.g. 0.x.y] macOS: [e.g. 15.0] Xcode: [e.g. 16.0 beta]

Additional Context

No response

Screenshots

No response

wisepmlin avatar Jul 08 '24 03:07 wisepmlin

@ViewBuilder func reception() -> some View { if !viewModel.isObject { TextEditor(text: $viewModel.receivedData) } else { SerialReception(document: $viewModel.receivedData) .id(viewModel.receivedData) } } .id(viewModel.receivedData) An id binding is required to update the UI

wisepmlin avatar Jul 08 '24 04:07 wisepmlin

Could you provide more context as to what the problem is?

thecoolwinter avatar Apr 14 '25 17:04 thecoolwinter

I think I understand, the text binding is not a two-way binding. Updating it from outside the view will not update the editor. You'll have to either use an NSTextStorage instead of a Binding<String> and update the storage object, or use the TextCoordinator API.

thecoolwinter avatar Jun 19 '25 21:06 thecoolwinter