使用 PHP (& MySQL) 处理 Monaco Editor 生成的换行符

Handle linebreak generated by Monaco Editor with PHP (& MySQL)

当使用 PHP (Laravel) 处理时,我无法在摩纳哥编辑器上处理 line-break/new-line/carriage-return。此后我想将代码存储到 MySQL 。 最终,存储的代码无论如何都会显示在摩纳哥编辑器上。

最佳做法是什么?

这不能解决问题。标签已损坏

// comes from the textarea
$code = preg_replace('/\r\n|\r|\n/', '\n', request()->custom_script); 

$this->storeToMySQL($code);

blade:

<label for="">Custom Script</label>
<div class="monaco-editor-container" style="height: 250px; border: 1px solid rgb(238, 238, 238);"></div>
<textarea name="custom_script" id="monaco_editor_textarea" style="display: none; white-space: pre-line"></textarea>
require(["vs/editor/editor.main"], function () {
    const monacoEditor = monaco.editor.create(document.querySelector('.monaco-editor-container'), {
        value: `{!! $monacoValue !!}`,
        language: 'html',
        base: 'vs',
        fontSize: "14px",
        minimap: { enabled: false },
    });

    $('#monaco_editor_textarea').val(monacoEditor.getModel().getValue())

    monacoEditor.onKeyDown(debounce(e => {
        $('#monaco_editor_textarea').val(monacoEditor.getModel().getValue())
    }, 500))
});

我想我解决了这个问题。我一直遇到的问题是因为对象的 value 道具无法呈现 <script> 标签

这是解决这个问题的控制器更新代码:

$code = preg_replace('/<\/script>/', '<\/script>', request()-> custom_script);

$this->storeToMySQL($code);

它现在能够存储和显示更复杂的代码,例如我尝试过的 HTML、JS、XML