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

chore: allow preselecting playground file #1209

Merged
merged 1 commit into from
May 17, 2024
Merged
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
9 changes: 8 additions & 1 deletion docs/src/components/sd-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ class SdPlayground extends LitElement {
outputFiles: {
state: true,
},
defaultSelected: {
type: String,
attribute: 'default-selected',
reflect: true,
},
};
}

Expand All @@ -166,6 +171,7 @@ class SdPlayground extends LitElement {
declare config: string;
declare script: string;
declare output: string;
declare defaultSelected: Files;
declare outputFiles: string[];
declare _currentFile: Files;
declare editor: any;
Expand All @@ -179,6 +185,7 @@ class SdPlayground extends LitElement {
this.config = '{}';
this.script = '{}';
this.output = '{}';
this.defaultSelected = 'config';
this.outputFiles = [];
this.editor = undefined;
this.hasInitialized = new Promise((resolve) => {
Expand Down Expand Up @@ -261,7 +268,7 @@ class SdPlayground extends LitElement {
await this.initMonaco();
await this.initData();
this.hasInitializedResolve();
this.currentFile = 'config';
this.currentFile = this.defaultSelected;
}

async initMonaco() {
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import tokens from '/public/demo-tokens.json';

Below is a showcase of how a set of DTCG tokens would be exported to CSS:

<sd-playground tokens={JSON.stringify({ value: JSON.stringify(tokens, null, 2), lang: 'json'})} id="main-demo">
<sd-playground tokens={JSON.stringify({ value: JSON.stringify(tokens, null, 2), lang: 'json'})} default-selected="tokens" id="main-demo">

<div style="height: 100%" slot="monaco-editor"></div>
</sd-playground>
52 changes: 32 additions & 20 deletions docs/src/remark-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,35 @@ const paragraphVisitor = (
let configData = serialize({});
let scriptData = serialize({});
let skipAmount = 1;
let defaultSelected;

for (const child of parent.children.slice(index + 1, index + 4)) {
if (child.type !== 'code') break;
switch (child.meta) {
case 'tokens':
tokensData = serialize({
value: child.value,
lang: child.lang ?? 'text',
});
break;
case 'script':
scriptData = serialize({
value: child.value,
lang: child.lang ?? 'text',
});
break;
case 'config':
configData = serialize({
value: child.value,
lang: child.lang ?? 'text',
});
break;
if (child.meta) {
const metas = child.meta?.split(' ');
switch (metas[0]) {
case 'tokens':
tokensData = serialize({
value: child.value,
lang: child.lang ?? 'text',
});
break;
case 'script':
scriptData = serialize({
value: child.value,
lang: child.lang ?? 'text',
});
break;
case 'config':
configData = serialize({
value: child.value,
lang: child.lang ?? 'text',
});
break;
}
if (metas[1] === 'selected') {
defaultSelected = metas[0];
}
}

skipAmount++;
Expand All @@ -51,7 +58,12 @@ const paragraphVisitor = (
value: `<sd-playground
tokens="${tokensData}"
config="${configData}"
script="${scriptData}"
script="${scriptData}"${
defaultSelected
? `
default-selected="${defaultSelected}"`
: ''
}
><div style="height: 100%" slot="monaco-editor"></div></sd-playground>`,
} as Html;
parent.children.splice(index, skipAmount, newNode);
Expand Down