electron Ace.js编辑器.getValue()输出有额外的空白

ujv3wf0j  于 2022-12-08  发布在  Electron
关注(0)|答案(1)|浏览(210)

我正在尝试使用Ace.js和Electron.js构建一个代码编辑器。到目前为止,我已经设置了这个编辑器,并且可以在其中编写代码。当我尝试将代码保存到文件中时,问题出现了。我的app.js文件中有以下代码:

editor.setTheme("ace/theme/monokai")
editor.session.setMode("ace/mode/arduino")

editor.setOptions({
    fontFamily: "Fira Code",
    fontSize: "10pt",
    enableBasicAutocompletion: true,
    enableSnippets: true,
    showPrintMargin: false,
    UseWrapMode: true,
    UseSoftTabs: true,
})

beautify.beautify(editor.session)

code = editor.getValue()

save_file.addEventListener('click', () => {
    window.eapi.saveFile('text.ino', code)
})

在我的index.js文件中,我有:

fs.writeFile(fname, code, (err) => {
      if (err) {
        console.log(err)
      }
      else {
        console.log('Save succesful')
      }
    })

输出为

在编辑器中是这样的:

如有任何帮助,我们将不胜感激。

nbysray5

nbysray51#

好了,我明白了。问题是code变量包含了编辑器中的代码,它是在按钮单击之前设置的。所以这是在HTML文件中设置的默认代码。这是我的一个愚蠢的错误。下面是编辑后的app.js文件

editor.setTheme("ace/theme/monokai")
editor.session.setMode("ace/mode/arduino")

editor.setOptions({
    fontFamily: "Fira Code",
    fontSize: "10pt",
    enableBasicAutocompletion: true,
    enableSnippets: true,
    showPrintMargin: false,
    UseWrapMode: true,
    UseSoftTabs: true,
})

beautify.beautify(editor.session)

// Removed from here

save_file.addEventListener('click', () => {
    code = editor.getValue() // and put here
    window.eapi.saveFile('text.ino', code)
})

相关问题