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

Add delete highlight support #664

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
29 changes: 28 additions & 1 deletion src/components/ContentNode/CodeListing.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
<!-- Do not add newlines in <pre>, as they'll appear in the rendered HTML. -->
<pre><CodeBlock><span
v-for="(line, index) in syntaxHighlightedLines"
:class="['code-line-container',{ highlighted: isHighlighted(index) }]"
:class="['code-line-container',
{ highlighted: !isLastCodeListing && isHighlighted(index),
deleteHighlighted: isLastCodeListing && isDeleteHighlighted(index)
}]"
:key="index"
><span
v-show="showLineNumbers" class="code-number"
Expand Down Expand Up @@ -75,16 +78,27 @@ export default {
type: Array,
default: () => [],
},
deleteHighlights: {
type: Array,
default: () => [],
},
showLineNumbers: {
type: Boolean,
default: () => false,
},
isLastCodeListing: {
type: Boolean,
default: () => false,
},
},
computed: {
escapedContent: ({ content }) => content.map(escapeHtml),
highlightedLineNumbers() {
return new Set(this.highlights.map(({ line }) => line));
},
deleteHighlightedLineNumbers() {
return new Set(this.deleteHighlights.map(({ line }) => line));
},
syntaxNameNormalized() {
// `occ` is a legacy naming convention
const fallbackMap = { occ: Language.objectiveC.key.url };
Expand All @@ -101,6 +115,9 @@ export default {
isHighlighted(index) {
return this.highlightedLineNumbers.has(this.lineNumberFor(index));
},
isDeleteHighlighted(index) {
return this.deleteHighlightedLineNumbers.has(this.lineNumberFor(index));
},
// Returns the line number for the line at the given index in `content`.
lineNumberFor(index) {
return this.startLineNumber + index;
Expand Down Expand Up @@ -152,6 +169,16 @@ export default {
}
}

.deleteHighlighted {
text-decoration-line: line-through;
background: var(--line-delete-highlight, var(--color-code-line-delete-highlight));
border-left: $highlighted-border-width solid var(--color-code-line-delete-highlight-border);

.code-number {
padding-left: $code-number-padding-left - $highlighted-border-width;
}
}

pre {
padding: $code-listing-with-numbers-padding;
display: flex;
Expand Down
10 changes: 10 additions & 0 deletions src/components/Tutorial/CodePreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<div :class="['code-preview', { ide: isTargetIDE }]">
<CodeTheme>
<CodeListing v-if="code" v-bind="codeProps" showLineNumbers />
<CodeListing v-if="lastCode" v-bind="lastCodeProps" showLineNumbers isLastCodeListing />
</CodeTheme>
<div
class="runtime-preview"
Expand Down Expand Up @@ -89,6 +90,10 @@ export default {
type: String,
required: true,
},
lastCode: {
type: String,
required: false,
},
preview: {
type: String,
required: false,
Expand Down Expand Up @@ -157,6 +162,11 @@ export default {
codeProps() {
return this.references[this.code];
},
lastCodeProps() {
const ref = this.references[this.lastCode];
ref.deleteHighlights = this.codeProps.deleteHighlights;
return ref;
},
runtimePreviewClasses() {
return {
collapsed: !this.shouldDisplayHideLabel,
Expand Down
1 change: 1 addition & 0 deletions src/components/Tutorial/CodeTheme.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default {
'--text': codeColors.text,
'--background': codeColors.background,
'--line-highlight': codeColors.lineHighlight,
'--line-delete-highlight': codeColors.lineDeleteHighlight,
'--url': codeColors.commentURL,
'--syntax-comment': codeColors.comment,
'--syntax-quote': codeColors.comment,
Expand Down
11 changes: 11 additions & 0 deletions src/components/Tutorial/SectionSteps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<CodePreview
v-if="visibleAsset.code"
:code="visibleAsset.code"
:lastCode="visibleAsset.lastCode"
:preview="visibleAsset.runtimePreview"
:isRuntimePreviewVisible="isRuntimePreviewVisible"
@runtime-preview-toggle="onRuntimePreviewToggle"
Expand Down Expand Up @@ -99,11 +100,13 @@ export default {
runtimePreview,
} = this.content[firstStepIndex] || {};

const lastCode = '';
return {
tutorialState: this.store.state,
visibleAsset: {
media,
code,
lastCode,
runtimePreview,
},
activeStep: firstStepIndex,
Expand Down Expand Up @@ -190,9 +193,17 @@ export default {
runtimePreview,
} = this.content[key];

let lastCode;
if (key > 0) {
lastCode = this.content[key - 1].code;
} else {
lastCode = '';
}

this.activeStep = key;
this.visibleAsset = {
code,
lastCode,
media,
runtimePreview,
};
Expand Down
1 change: 1 addition & 0 deletions src/styles/core/colors/_dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
--color-badge-default: var(--color-badge-dark-default);
--color-button-background-active: #{dark-color(fill-blue)};
--color-code-line-highlight: #{change-color(dark-color(figure-blue), $alpha: 0.08)};
--color-code-line-delete-highlight: #{change-color(dark-color(figure-red), $alpha: 0.08)};
--color-dropdown-background: var(--color-dropdown-dark-background);
--color-dropdown-border: var(--color-dropdown-dark-border);
--color-dropdown-option-text: var(--color-dropdown-dark-option-text);
Expand Down
2 changes: 2 additions & 0 deletions src/styles/core/colors/_light.scss
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
--color-code-collapsible-text: var(--color-figure-gray-secondary-alt);
--color-code-line-highlight: #{change-color(light-color(figure-blue), $alpha: 0.08)};
--color-code-line-highlight-border: var(--color-figure-blue);
--color-code-line-delete-highlight: #{change-color(light-color(figure-red), $alpha: 0.08)};
--color-code-line-delete-highlight-border: var(--color-figure-red);
--color-code-plain: var(--color-figure-gray);
--color-dropdown-background: #{transparentize(light-color(fill), 0.2)};
--color-dropdown-border: #{light-color(fill-gray)};
Expand Down
4 changes: 2 additions & 2 deletions src/utils/syntax-highlight.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021 Apple Inc. and the Swift project authors
* Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -192,7 +192,7 @@ function duplicateMultilineNode(element) {

// wrap each new line with the current element's class
const result = getLines(element.innerHTML)
.reduce((all, lineText) => `${all}<span class="${className}">${lineText || '\n\n'}</span>\n`, '');
.reduce((all, lineText) => `${all}<span class="${className}">${lineText || ''}</span>\n`, '');

// return a list of newly wrapped HTML elements
return htmlToElements(result.trim());
Expand Down
12 changes: 3 additions & 9 deletions tests/unit/utils/syntax-highlight.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,11 @@ describe("syntax-highlight", () => {
expect(sanitizedCode).toMatchInlineSnapshot(`
<span class="syntax-keyword">let</span> multiline = <span class="syntax-string">""</span><span class="syntax-string">"</span>
<span class="syntax-string">Needs</span>
<span class="syntax-string">

</span>
<span class="syntax-string"></span>
<span class="syntax-string">Spaces</span>
<span class="syntax-string">

</span>
<span class="syntax-string"></span>
<span class="syntax-string">Between</span>
<span class="syntax-string">

</span>
<span class="syntax-string"></span>
<span class="syntax-string">Lines</span>
<span class="syntax-string">"</span><span class="syntax-string">""</span>
`);
Expand Down