@@ -55,7 +55,8 @@ public function __construct(
55
55
protected readonly AuthorizationCheckerInterface $ authorizationChecker ,
56
56
protected readonly RouterInterface $ router ,
57
57
#[Autowire('%kernel.project_dir% ' )]
58
- protected readonly string $ projectDir
58
+ protected readonly string $ projectDir ,
59
+ protected array $ renderedSources = []
59
60
) {}
60
61
61
62
public function getFunctions (): array
@@ -917,23 +918,48 @@ public function codeEditor(
917
918
sprintf ($ editor , $ code , $ editable ? 'false ' : 'true ' , $ mode , $ extraForEdit ));
918
919
}
919
920
921
+ /**
922
+ * Gets the JavaScript to get a Monaco model instance for the submission file.
923
+ * Renders the source code of the file as Monaco model, if not already rendered.
924
+ * @param SubmissionFile $file The submission file to render
925
+ * @return string The JavaScript source assignable to a model variable.
926
+ */
927
+ public function getMonacoModel (SubmissionFile $ file ): string
928
+ {
929
+ if (array_key_exists ($ file ->getSubmitfileid (), $ this ->renderedSources )) {
930
+ return sprintf (
931
+ <<<JS
932
+ monaco.editor.getModel(monaco.Uri.parse("diff/%d/%s"));
933
+ JS ,
934
+ $ file ->getSubmitfileid (),
935
+ $ file ->getFilename (),
936
+ );
937
+ }
938
+ $ this ->renderedSources [$ file ->getSubmitfileid ()] = true ;
939
+
940
+ return sprintf (
941
+ <<<JS
942
+ monaco.editor.createModel(
943
+ "%s",
944
+ undefined,
945
+ monaco.Uri.parse("diff/%d/%s")
946
+ );
947
+ JS ,
948
+ $ this ->twig ->getRuntime (EscaperRuntime::class)->escape ($ file ->getSourcecode (), 'js ' ),
949
+ $ file ->getSubmitfileid (),
950
+ $ file ->getFilename (),
951
+ );
952
+ }
953
+
920
954
public function showDiff (string $ id , SubmissionFile $ newFile , SubmissionFile $ oldFile ): string
921
955
{
922
956
$ editor = <<<HTML
923
957
<div class="editor" id="__EDITOR__"></div>
924
958
<script>
925
959
$(function() {
926
960
require(['vs/editor/editor.main'], function () {
927
- const originalModel = monaco.editor.createModel(
928
- "%s",
929
- undefined,
930
- monaco.Uri.parse("diff-old/%s")
931
- );
932
- const modifiedModel = monaco.editor.createModel(
933
- "%s",
934
- undefined,
935
- monaco.Uri.parse("diff-new/%s")
936
- );
961
+ const originalModel = %s
962
+ const modifiedModel = %s
937
963
938
964
const initialDiffMode = getDiffMode();
939
965
const radios = $("#diffselect-__EDITOR__ > input[name='__EDITOR__-mode']");
@@ -988,10 +1014,8 @@ public function showDiff(string $id, SubmissionFile $newFile, SubmissionFile $ol
988
1014
989
1015
return sprintf (
990
1016
str_replace ('__EDITOR__ ' , $ id , $ editor ),
991
- $ this ->twig ->getRuntime (EscaperRuntime::class)->escape ($ oldFile ->getSourcecode (), 'js ' ),
992
- $ oldFile ->getFilename (),
993
- $ this ->twig ->getRuntime (EscaperRuntime::class)->escape ($ newFile ->getSourcecode (), 'js ' ),
994
- $ newFile ->getFilename (),
1017
+ $ this ->getMonacoModel ($ oldFile ),
1018
+ $ this ->getMonacoModel ($ newFile ),
995
1019
);
996
1020
}
997
1021
0 commit comments