@@ -862,16 +862,17 @@ public function codeEditor(
862862 ?string $ filename = null
863863 ): string {
864864 $ editor = <<<HTML
865- <div class="editor" id="__EDITOR__">%s </div>
865+ <div class="editor" id="__EDITOR__"></div>
866866<script>
867867$(function() {
868868 require(['vs/editor/editor.main'], function () {
869869 const element = document.getElementById('__EDITOR__');
870- const content = element.textContent;
871- element.textContent = '';
870+ const content = %s;
871+ const filePath = %s;
872+ const uri = filePath ? monaco.Uri.file(filePath) : monaco.Uri.parse("editor-__EDITOR__");
873+ const model = monaco.editor.createModel(content, undefined, uri);
872874
873875 const editor = monaco.editor.create(element, {
874- value: content,
875876 scrollbar: {
876877 alwaysConsumeMouseWheel: false,
877878 vertical: 'auto',
@@ -882,6 +883,7 @@ public function codeEditor(
882883 readOnly: %s,
883884 theme: getCurrentEditorTheme(),
884885 });
886+ editor.setModel(model);
885887 %s
886888 %s
887889 });
@@ -890,7 +892,7 @@ public function codeEditor(
890892HTML ;
891893 $ rank = $ index ;
892894 $ id = sprintf ('editor%s ' , $ rank );
893- $ code = htmlspecialchars ($ code) ;
895+ $ source = mb_check_encoding ($ code, ' UTF-8 ' ) ? $ code : " Could not display binary file " ;
894896 if ($ elementToUpdate ) {
895897 $ extraForEdit = <<<JS
896898editor.getModel().onDidChangeContent(() => {
@@ -905,22 +907,21 @@ public function codeEditor(
905907
906908 if ($ language !== null ) {
907909 $ mode = <<<JS
908- const model = editor.getModel();
909910model.setLanguage(" $ language");
910911JS ;
911- } elseif ($ filename !== null ) {
912- $ modeTemplate = <<<JS
913- const filePath = "%s";
914- const model = monaco.editor.createModel(content, undefined, monaco.Uri.file(filePath));
915- editor.setModel(model);
916- JS ;
917- $ mode = sprintf ($ modeTemplate , htmlspecialchars ($ filename ));
918912 } else {
919913 $ mode = '' ;
920914 }
921915
922916 return str_replace ('__EDITOR__ ' , $ id ,
923- sprintf ($ editor , $ code , $ editable ? 'false ' : 'true ' , $ mode , $ extraForEdit ));
917+ sprintf (
918+ $ editor ,
919+ $ this ->serializer ->serialize ($ source , 'json ' ),
920+ $ this ->serializer ->serialize ($ filename , 'json ' ),
921+ $ editable ? 'false ' : 'true ' ,
922+ $ mode ,
923+ $ extraForEdit
924+ ));
924925 }
925926
926927 /**
0 commit comments