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

Fix: language dropdown state selection #1664

Merged
merged 3 commits into from
Feb 12, 2025
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
4 changes: 4 additions & 0 deletions src/lib/layouts/DocsArticle.svelte
Original file line number Diff line number Diff line change
@@ -17,12 +17,16 @@
import { writable } from 'svelte/store';
import { Feedback } from '$lib/components';
import { scrollToTop } from '$lib/actions/scrollToTop';
import type { Language } from '$lib/utils/code';

export let title: string;
export let toc: Array<TocItem>;
export let back: string | undefined = undefined;
export let date: string | undefined = undefined;

// Shared writable store for a selected language.
setContext('language-context', writable<Language>());

const reducedArticleSize = setContext('articleHasNumericBadge', writable(false));
</script>

20 changes: 20 additions & 0 deletions src/markdoc/tags/MultiCode.svelte
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@
content: writable('')
});

const languageContext = getContext<Writable<string>>('language-context');

const { snippets, selected, content } = getContext<CodeContext>('multi-code');

snippets.subscribe((n) => {
@@ -29,6 +31,24 @@
}
});

selected.subscribe((language) => {
// apply if exists in snippets
if (language && $snippets.has(language as Language)) {
languageContext?.set(language);
}
});

languageContext?.subscribe((language) => {
if (
language &&
language !== $selected &&
// apply if exists in snippets
$snippets.has(language as Language)
) {
selected.set(language);
}
});

enum CopyStatus {
Copy = 'Copy',
Copied = 'Copied!'