Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CodeMirror on Admin Submissions Diff tab #2204

Merged
merged 31 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
372de18
Add `diff-to-document` helper for parsing diffs into document objects
VasylMarchuk Sep 6, 2024
285c809
Increase CodeMirror's `collapseUnchanged` margin to 3 lines
VasylMarchuk Sep 6, 2024
2eb72ff
Explicitly enable CodeMirror's `highlightChanges` and `syntaxHighligh…
VasylMarchuk Sep 6, 2024
6a4d2ad
Inherit `codeCraftersDark` CodeMirror theme from `codeCraftersLight` …
VasylMarchuk Sep 6, 2024
7ec9c19
Add svg icons for "Expand unchanged lines" styling
VasylMarchuk Sep 8, 2024
a6df2c5
Better styling for CodeMirror component on the Demo page
VasylMarchuk Sep 10, 2024
f8e1a40
Improved CodeMirror themes for Merge View + Dark Mode
VasylMarchuk Sep 10, 2024
a9698c6
Use CodeMirror on Admin Submissions Diff page instead of `SyntaxHighl…
VasylMarchuk Sep 10, 2024
071d50b
Don't show line numbers in `DiffContainer`
VasylMarchuk Sep 10, 2024
b803587
Disable CodeMirror's inline changes highlighting
VasylMarchuk Sep 14, 2024
06e1b53
Make "Expand unchanged lines" line extend over the gutter
VasylMarchuk Sep 19, 2024
c43f3dd
Remove gutter border in Dark Mode
VasylMarchuk Sep 19, 2024
9852bdd
Add diff-related elements to `codeMirror` page object
VasylMarchuk Sep 20, 2024
48d8286
Update `submissions-page` page object to use `codeMirror`
VasylMarchuk Sep 20, 2024
09dd314
Rewrite "expandable chunks has the correct number of lines" test
VasylMarchuk Sep 20, 2024
63a6b32
Better test for `diff-to-document` to also test deleted lines
VasylMarchuk Sep 20, 2024
61a0c67
Rewrite "expandable chunks display the hidden code when clicked" test
VasylMarchuk Sep 20, 2024
e4855e7
Delete irrelevant diff tests
VasylMarchuk Sep 20, 2024
4e1e3e1
Combine two `view-diffs` tests into one that tests all conditions
VasylMarchuk Sep 20, 2024
4f4fdbd
Better diff to test the middle unchanged lines placeholder as well
VasylMarchuk Sep 22, 2024
981f9fe
Don't highlight trailing whitespace in DiffContainer's CodeMirror
VasylMarchuk Sep 22, 2024
fb71942
Use a more realistic diff for `view-diffs` tests
VasylMarchuk Sep 27, 2024
284de19
Move svg icons for "Expand unchanged lines" styling to `images/codemi…
VasylMarchuk Sep 27, 2024
27350e6
Mark `BASE_STYLE` as `dark: true` when using it in a dark theme
VasylMarchuk Sep 27, 2024
6c6f9c5
Use Tailwind's color contants for the light CodeMirror theme
VasylMarchuk Sep 28, 2024
95c436b
Use Tailwind's color contants for the dark CodeMirror theme (mostly)
VasylMarchuk Sep 28, 2024
85d8655
Add a margin to CodeMirror's collapsed lines bar on admin submissions…
VasylMarchuk Sep 29, 2024
ab0d6fc
Use Tailwind's color constants for the dark CodeMirror theme
VasylMarchuk Oct 6, 2024
1984535
Extract `blendColors` function into an util, with unit tests
VasylMarchuk Oct 6, 2024
3451f02
Add an extra test for `diff-to-document`
VasylMarchuk Oct 6, 2024
9fe2f7b
Fix margin for collapsed lines bar on admin submissions diff page
VasylMarchuk Oct 17, 2024
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
4 changes: 3 additions & 1 deletion app/components/code-mirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ const OPTION_HANDLERS: OptionHandlersSignature = {
unifiedMergeView({
original: originalDocument,
mergeControls: !!mergeControls,
collapseUnchanged: collapseUnchanged ? { margin: 2, minSize: 4 } : undefined,
collapseUnchanged: collapseUnchanged ? { margin: 3, minSize: 4 } : undefined,
highlightChanges: false,
syntaxHighlightDeletions: true,
}),
]
: [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
<div ...attributes>
<div class="course-admin-submission-diff-container" ...attributes>
{{#if @submission.hasChangedFiles}}
{{#each @submission.changedFiles key="filename" as |changedFile index|}}
<div
class="{{if (not-eq index (sub @submission.changedFiles.length 1)) 'mb-4'}}
bg-white shadow-sm rounded border border-gray-300 overflow-y-hidden"
data-test-changed-file
VasylMarchuk marked this conversation as resolved.
Show resolved Hide resolved
>
<div class="bg-gray-100 rounded-t py-1 px-4 border-b border-gray-200 shadow-sm">
<span class="font-mono text-xs text-gray-600 bold">{{changedFile.filename}}</span>
</div>

<div class="text-sm leading-6">
{{! #if ensures that language is typed and not null }}
{{#if @submission.repository.language}}
<SyntaxHighlightedDiff
@comments={{(array)}}
@code={{changedFile.diff}}
@language={{@submission.repository.language.slug}}
@shouldCollapseUnchangedLines={{true}}
/>
{{/if}}
</div>
{{#let (diff-to-document changedFile.diff) as |document|}}
<CodeMirror
@document={{document.current}}
@originalDocument={{document.original}}
@language={{@submission.repository.language.slug}}
@filename={{changedFile.filename}}
@collapseUnchanged={{true}}
@allowMultipleSelections={{true}}
@bracketMatching={{true}}
@crosshairCursor={{true}}
@drawSelection={{true}}
@rectangularSelection={{true}}
@readOnly={{true}}
@highlightSelectionMatches={{true}}
@highlightSpecialChars={{true}}
@theme={{this.codeMirrorTheme}}
class="block text-sm"
/>
{{/let}}
VasylMarchuk marked this conversation as resolved.
Show resolved Hide resolved
</div>
{{/each}}
{{else}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Component from '@glimmer/component';
import { service } from '@ember/service';
import type DarkModeService from 'codecrafters-frontend/services/dark-mode';
import type SubmissionModel from 'codecrafters-frontend/models/submission';

import { codeCraftersDark, codeCraftersLight } from 'codecrafters-frontend/utils/code-mirror-themes';
interface Signature {
Element: HTMLDivElement;

Expand All @@ -9,7 +11,13 @@ interface Signature {
};
}

export default class DiffContainerComponent extends Component<Signature> {}
export default class DiffContainerComponent extends Component<Signature> {
@service declare darkMode: DarkModeService;

get codeMirrorTheme() {
return this.darkMode.isEnabled ? codeCraftersDark : codeCraftersLight;
}
}

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
Expand Down
32 changes: 32 additions & 0 deletions app/helpers/diff-to-document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { helper } from '@ember/component/helper';

const diffToDocument = helper(function diffToDocument([diff = '']: [string | undefined]) {
const diffLines = diff.split('\n');

const current = [];
const original = [];

for (const line of diffLines) {
if (line.startsWith('-')) {
original.push(line.substring(1));
} else if (line.startsWith('+')) {
current.push(line.substring(1));
} else {
original.push(line.substring(1));
current.push(line.substring(1));
}
}

return {
current: current.join('\n'),
original: original.join('\n'),
};
});

export default diffToDocument;

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
'diff-to-document': typeof diffToDocument;
}
}
1 change: 1 addition & 0 deletions app/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@import 'pages/affiliate-link-page.scss';
@import 'pages/course-stage-solution-page.scss';
@import 'pages/code-walkthrough-page.scss';
@import 'pages/course-admin-submission-diff.scss';

/* purgecss start ignore */
@import '@typeform/embed/build/css/popup.css';
Expand Down
3 changes: 3 additions & 0 deletions app/styles/pages/course-admin-submission-diff.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.course-admin-submission-diff-container code-mirror .cm-collapsedLines {
margin-left: -24px;
}
2 changes: 1 addition & 1 deletion app/templates/demo/code-mirror.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
@scrollPastEnd={{this.scrollPastEnd}}
@syntaxHighlighting={{this.syntaxHighlighting}}
{{! Element classes }}
class="block overflow-auto mt-4 h-72 {{if this.outline 'outline-2 outline-dashed outline-green-500'}}"
class="block overflow-auto mt-4 max-h-96 {{if this.outline 'outline outline-dotted outline-gray-200 dark:outline-gray-700'}}"
/>

<div class="my-2 text-right">
Expand Down
11 changes: 11 additions & 0 deletions app/utils/blend-colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import hexRgb from 'hex-rgb';
import rgbHex from 'rgb-hex';

export default function blendColors(fgColor = '#ffffff', opacity = 1, bgColor = '#ffffff') {
const fgRgb = hexRgb(fgColor, { format: 'array' });
const bgRgb = hexRgb(bgColor, { format: 'array' });

const blendedRgb = fgRgb.slice(0, 3).map<number>((colFg, idx) => opacity * colFg + (1 - opacity) * (bgRgb[idx] || 255)) as [number, number, number];

return `#${rgbHex(...blendedRgb)}`;
}
Loading
Loading