Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions webapp/src/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@

class TwigExtension extends AbstractExtension implements GlobalsInterface
{
/**
* @param array<int, bool> $renderedSources
*/
public function __construct(
protected readonly DOMJudgeService $dj,
protected readonly ConfigurationService $config,
Expand All @@ -55,7 +58,8 @@ public function __construct(
protected readonly AuthorizationCheckerInterface $authorizationChecker,
protected readonly RouterInterface $router,
#[Autowire('%kernel.project_dir%')]
protected readonly string $projectDir
protected readonly string $projectDir,
protected array $renderedSources = []
) {}

public function getFunctions(): array
Expand Down Expand Up @@ -917,23 +921,47 @@ public function codeEditor(
sprintf($editor, $code, $editable ? 'false' : 'true', $mode, $extraForEdit));
}

/**
* Gets the JavaScript to get a Monaco model instance for the submission file.
* Renders the source code of the file as Monaco model, if not already rendered.
* @return string The JavaScript source assignable to a model variable.
*/
public function getMonacoModel(SubmissionFile $file): string
{
if (array_key_exists($file->getSubmitfileid(), $this->renderedSources)) {
return sprintf(
<<<JS
monaco.editor.getModel(monaco.Uri.parse("diff/%d/%s"));
JS,
$file->getSubmitfileid(),
$file->getFilename(),
);
}
$this->renderedSources[$file->getSubmitfileid()] = true;

return sprintf(
<<<JS
monaco.editor.createModel(
"%s",
undefined,
monaco.Uri.parse("diff/%d/%s")
);
JS,
$this->twig->getRuntime(EscaperRuntime::class)->escape($file->getSourcecode(), 'js'),
$file->getSubmitfileid(),
$file->getFilename(),
);
}

public function showDiff(string $id, SubmissionFile $newFile, SubmissionFile $oldFile): string
{
$editor = <<<HTML
<div class="editor" id="__EDITOR__"></div>
<script>
$(function() {
require(['vs/editor/editor.main'], function () {
const originalModel = monaco.editor.createModel(
"%s",
undefined,
monaco.Uri.parse("diff-old/%s")
);
const modifiedModel = monaco.editor.createModel(
"%s",
undefined,
monaco.Uri.parse("diff-new/%s")
);
const originalModel = %s
const modifiedModel = %s

const initialDiffMode = getDiffMode();
const radios = $("#diffselect-__EDITOR__ > input[name='__EDITOR__-mode']");
Expand Down Expand Up @@ -988,10 +1016,8 @@ public function showDiff(string $id, SubmissionFile $newFile, SubmissionFile $ol

return sprintf(
str_replace('__EDITOR__', $id, $editor),
$this->twig->getRuntime(EscaperRuntime::class)->escape($oldFile->getSourcecode(), 'js'),
$oldFile->getFilename(),
$this->twig->getRuntime(EscaperRuntime::class)->escape($newFile->getSourcecode(), 'js'),
$newFile->getFilename(),
$this->getMonacoModel($oldFile),
$this->getMonacoModel($newFile),
);
}

Expand Down
Loading