I'm saving the text of a TextEditor
to a text file. I start by creating the file with the first line of the TextEditor
as the filename, and then subsequent updates are saved on that file. The code lives on a .onChange
action. This presents a challenge since I'm creating a file for each character the use type on the first line of the TextEditor
.
Is there a way to detect when the user stopped typing, or the component is idle, and then create the file and save the text? This is on macOS Big Sur.
I haven't been able to find any action I can use. Code follows for the view:
@EnvironmentObject private var data: DataModel
var note: NoteItem
@State var text: String
var body: some View {
HStack {
VStack(alignment: .leading) {
TextEditor(text: $text)
//.onTapGesture {
// tried with this....
// print("stopped typing")
//}
.onChange(of: text, perform: { value in
guard let index = data.notes.firstIndex(of: note) else { return }
data.notes[index].text = value
data.notes[index].date = Date()
data.notes[index].filename = value.components(separatedBy: NSCharacterSet.newlines).first!
saveFile(filename: data.notes[index].filename, contents: data.notes[index].text)
})
}
}