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 Edit in Playground #72

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"lint": "run-s prettier gts stylelint tsc",
"lint:ci": "run-s prettier:ci gts:ci stylelint:ci tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"checklinks:internal": "npx --yes --package=hyperlink -- 'hyperlink --canonicalroot https://sass-lang.com/ --root _site/ -r -p -i --skip \"fragment-redirect\" --skip \"types.Map\" _site/index.html'",
"checklinks:internal": "npx --yes --package=hyperlink -- 'hyperlink --canonicalroot https://sass-lang.com/ --root _site/ -r -p -i --skip \"fragment-redirect\" --skip \"types.Map\" --skip \"/playground#\" _site/index.html'",
"checklinks:external": "npx --yes --package=broken-link-checker -- 'blc -ro --exclude=twitter.com --exclude=mvnrepository.com --exclude=drupal.org --exclude=web.archive.org https://sass-lang.com'"
},
"devDependencies": {
Expand Down
40 changes: 24 additions & 16 deletions source/_includes/code_examples/code_example.liquid
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
<div class="code-example ui-tabs{% if code.canSplit %} can-split{% endif %}"{% if code.splitLocation %} style="--split-location: {{ code.splitLocation }}%"{% endif %}>
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix">
<div>
{%- if code.scss.size == 1 -%}
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
<a href="/playground#{{ playgroundLinks.scss }}" class="sl-c-to-playground" target="_blank" data-syntax="scss"><span class="sl-c-to-playground__highlight">Edit on</span> Playground</a>
{%- endif -%}
{%- if code.sass.size == 1 -%}
<a href="/playground#{{ playgroundLinks.sass }}" class="sl-c-to-playground" target="_blank" data-syntax="sass"><span class="sl-c-to-playground__highlight">Edit on</span> Playground</a>
{%- endif -%}
<div class="code-example ui-tabs{% if code.canSplit %} can-split{% endif %}"{% if code.splitLocation %} style="--split-location: {{ code.splitLocation }}%"{% endif %}>
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix">
{%- if code.scss.size -%}
{%- render 'code_examples/tab', name: 'SCSS', syntax: 'scss', id: exampleName, enabled: true -%}
{%- endif -%}
{%- if code.sass.size -%}
{%- assign enabled = code.scss.size == 0 -%}
{%- render 'code_examples/tab', name: 'Sass', syntax: 'sass', id: exampleName, enabled: enabled -%}
{%- endif -%}
{%- if code.css.size -%}
{%- render 'code_examples/tab', name: 'CSS', syntax: 'css', id: exampleName, enabled: false -%}
{%- endif -%}
</ul>
{%- if code.scss.size -%}
{%- render 'code_examples/tab', name: 'SCSS', syntax: 'scss', id: exampleName, enabled: true -%}
{%- render 'code_examples/panel', name: 'SCSS Syntax', syntax: 'scss', id: exampleName, examples: code.scss, paddings: code.scssPaddings, enabled: true -%}
{%- endif -%}
{%- if code.sass.size -%}
{%- assign enabled = code.scss.size == 0 -%}
{%- render 'code_examples/tab', name: 'Sass', syntax: 'sass', id: exampleName, enabled: enabled -%}
{%- render 'code_examples/panel', name: 'Sass Syntax', syntax: 'sass', id: exampleName, examples: code.sass, paddings: code.sassPaddings, enabled: enabled -%}
{%- endif -%}
{%- if code.css.size -%}
{%- render 'code_examples/tab', name: 'CSS', syntax: 'css', id: exampleName, enabled: false -%}
{%- render 'code_examples/panel', name: 'CSS Output', syntax: 'css', id: exampleName, examples: code.css, paddings: code.cssPaddings, enabled: false -%}
{%- endif -%}
jgerigmeyer marked this conversation as resolved.
Show resolved Hide resolved
</ul>
{%- if code.scss.size -%}
{%- render 'code_examples/panel', name: 'SCSS Syntax', syntax: 'scss', id: exampleName, examples: code.scss, paddings: code.scssPaddings, enabled: true -%}
{%- endif -%}
{%- if code.sass.size -%}
{%- assign enabled = code.scss.size == 0 -%}
{%- render 'code_examples/panel', name: 'Sass Syntax', syntax: 'sass', id: exampleName, examples: code.sass, paddings: code.sassPaddings, enabled: enabled -%}
{%- endif -%}
{%- if code.css.size -%}
{%- render 'code_examples/panel', name: 'CSS Output', syntax: 'css', id: exampleName, examples: code.css, paddings: code.cssPaddings, enabled: false -%}
{%- endif -%}
</div>
</div>
2 changes: 1 addition & 1 deletion source/_includes/code_examples/tab.liquid
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<li class="ui-tabs-tab{% if syntax == 'css' %} css-tab{% endif %}{% if enabled %} ui-tabs-active{% endif %}">
<li class="ui-tabs-tab{% if syntax == 'css' %} css-tab{% endif %}{% if enabled %} ui-tabs-active{% endif %}" data-syntax="{{ syntax }}">
<a href="#example-{{ id }}-{{ syntax }}" class="ui-tabs-anchor">{{ name }}</a>
</li>
4 changes: 4 additions & 0 deletions source/assets/js/components/sass-syntax-switcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ $(() => {
if (!noRecursion) {
noRecursion = true;
jqASass.not(this).trigger('click');
$('body').attr('data-selected-syntax', 'sass');
noRecursion = false;
}
}),
jqASCSS = jqA.filter(":contains('SCSS')").on('click', function () {
if (!noRecursion) {
noRecursion = true;
jqASCSS.not(this).trigger('click');
$('body').attr('data-selected-syntax', 'scss');
noRecursion = false;
}
});
// Set initial value
$('body').attr('data-selected-syntax', 'scss');
jgerigmeyer marked this conversation as resolved.
Show resolved Hide resolved
});
5 changes: 4 additions & 1 deletion source/assets/js/playground/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export function serializeState(state: PlaygroundState): string {
* - `inputFormat`: 0=indented 1=scss
* - `outputFormat`: 0=compressed 1=expanded
*/
function serializeStateContents(state: PlaygroundState): string {
export function serializeStateContents(
state: Partial<PlaygroundState> &
Pick<PlaygroundState, 'inputFormat' | 'outputFormat' | 'inputValue'>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be simplified?

Suggested change
state: Partial<PlaygroundState> &
Pick<PlaygroundState, 'inputFormat' | 'outputFormat' | 'inputValue'>
state: Pick<PlaygroundState, 'inputFormat' | 'outputFormat' | 'inputValue'>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that was so the function could be called with the whole state or a subset, but I can't track down where the whole state was being passed, so I took your suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, makes sense. I think the entire state is passed in earlier in this same file, but I also think TS allows extra properties on objects as long as the specified ones match.

): string {
const inputFormatChar = state.inputFormat === 'scss' ? 1 : 0;
const outputFormatChar = state.outputFormat === 'expanded' ? 1 : 0;
const persistedState = `${inputFormatChar}${outputFormatChar}${state.inputValue}`;
Expand Down
27 changes: 27 additions & 0 deletions source/assets/sass/components/_playground.scss
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,30 @@
text-align: right;
}
}

// Edit on Playground Link
.sl-c-to-playground {
border: 0;
display: var(--to-playground-display, none);
float: inline-end;
line-height: 1.2;
margin-block-start: var(--sl-gutter--quarter-negative);
padding: var(--sl-gutter--quarter);
text-align: end;
position: relative;
z-index: 1;
}

$syntaxes: ('scss', 'sass');

@each $syntax in $syntaxes {
[data-selected-syntax='#{$syntax}']
.sl-c-to-playground[data-syntax='#{$syntax}'] {
--to-playground-display: grid;
}
}

.sl-c-to-playground__highlight {
font-weight: var(--sl-font-weight--bold);
text-transform: uppercase;
}
33 changes: 33 additions & 0 deletions source/helpers/components/codeExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import sass from 'sass';

import {liquidEngine} from '../engines';
import {stripIndent} from '../type';
import {serializeStateContents} from '../../assets/js/playground/utils';

/**
* Renders a code example.
Expand Down Expand Up @@ -57,9 +58,14 @@ export default async function codeExample(
throw new Error('`{% codeExample %}` tags require a unique name.');
}
const code = generateCodeExample(contents, autogenCSS, syntax);
const playgroundLinks = generatePlaygroundLinks({
scss: code.scss,
sass: code.sass,
});
return liquidEngine.renderFile('code_examples/code_example', {
code,
exampleName,
playgroundLinks,
});
}

Expand Down Expand Up @@ -287,3 +293,30 @@ const getCanSplit = (
maxCSSWidth,
};
};

// Create links to editable example in the playground. Returns false for
// multiple sections/files, which are not supported by the playground.
function generatePlaygroundLinks(
{scss, sass} = {scss: <Array<string>>[], sass: <Array<string>>[]}
jamesnw marked this conversation as resolved.
Show resolved Hide resolved
) {
const sassLink =
sass.length === 1
? serializeStateContents({
inputFormat: 'indented',
outputFormat: 'expanded',
inputValue: sass[0],
})
: false;
const scssLink =
scss.length === 1
? serializeStateContents({
inputFormat: 'scss',
outputFormat: 'expanded',
inputValue: scss[0],
})
: false;
return {
sass: sassLink,
scss: scssLink,
};
}