CodeMirror 6,如何获取 input/update/change 事件的编辑器值?

CodeMirror 6, how to get editor value on input/update/change event?

我希望 CodeMirror 6 编辑器实例 的内容/值反映在外部变量中,当文本更改时变量更改/同步。

如何在 CodeMirror 6 中做到这一点?

伪代码

let sync_val = "";

import {EditorView} from "@codemirror/view"
import {EditorState} from "@codemirror/state"

let myView = new EditorView({
  state: EditorState.create({doc: "hello"}),
  parent: document.body
})

myView.onChange(function(val) {
  sync_val = val;
});

以下作品(使用 updateListener 扩展名):

let sync_val = "";

import {EditorView} from "@codemirror/view"
import {EditorState} from "@codemirror/state"

let myView = new EditorView({
    state: EditorState.create({
        doc: "hello",
        extensions: [
            EditorView.updateListener.of(function(e) {
                sync_val = e.state.doc.toString();
            })
        ]
    }),
    parent: document.body
})