-
-
Notifications
You must be signed in to change notification settings - Fork 16
Feat: Auto switch code tab to user preferred programming language #111
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,26 @@ | ||||||||||||||||||||
| <script setup> | ||||||||||||||||||||
| import { ref, nextTick } from 'vue' | ||||||||||||||||||||
| <script> | ||||||||||||||||||||
| import { ref, watch, onMounted, nextTick } from 'vue' | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const GLOBAL_LANG_KEY = 'java' | ||||||||||||||||||||
| const globalActiveLanguage = ref('') | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // initialize from localStorage if available | ||||||||||||||||||||
| if (typeof window !== 'undefined') { | ||||||||||||||||||||
| const stored = localStorage.getItem(GLOBAL_LANG_KEY) | ||||||||||||||||||||
| if (stored) { | ||||||||||||||||||||
| globalActiveLanguage.value = stored | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Watch for changes and save to localStorage | ||||||||||||||||||||
| watch(globalActiveLanguage, (newLang) => { | ||||||||||||||||||||
| if (typeof window !== 'undefined' && newLang) { | ||||||||||||||||||||
| localStorage.setItem(GLOBAL_LANG_KEY, newLang) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| </script> | ||||||||||||||||||||
|
|
||||||||||||||||||||
| <script setup> | ||||||||||||||||||||
| const props = defineProps({ | ||||||||||||||||||||
| languages: { | ||||||||||||||||||||
| type: Array, | ||||||||||||||||||||
|
|
@@ -12,6 +32,34 @@ const activeTab = ref(0) | |||||||||||||||||||
| const isFullscreen = ref(false) | ||||||||||||||||||||
| const copiedIndex = ref(null) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| onMounted(() => { | ||||||||||||||||||||
| if (globalActiveLanguage.value) { | ||||||||||||||||||||
| const index = props.languages.findIndex( | ||||||||||||||||||||
| (lang) => lang.name === globalActiveLanguage.value, | ||||||||||||||||||||
| ) | ||||||||||||||||||||
| if (index !== -1) { | ||||||||||||||||||||
| activeTab.value = index | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } else if (props.languages.length > 0) { | ||||||||||||||||||||
| // Note: If no global language is set, use the first tab's language as default | ||||||||||||||||||||
| globalActiveLanguage.value = props.languages[0].name | ||||||||||||||||||||
| } | ||||||||||||||||||||
| }) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| watch(globalActiveLanguage, (newLang) => { | ||||||||||||||||||||
| if (newLang) { | ||||||||||||||||||||
| const index = props.languages.findIndex((lang) => lang.name === newLang) | ||||||||||||||||||||
| if (index !== -1) { | ||||||||||||||||||||
| activeTab.value = index | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| }) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const setActiveTab = (index) => { | ||||||||||||||||||||
| activeTab.value = index | ||||||||||||||||||||
| globalActiveLanguage.value = props.languages[index].name | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
Comment on lines
+58
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing bounds check before accessing
🛡️ Proposed fix const setActiveTab = (index) => {
+ if (index < 0 || index >= props.languages.length) return
activeTab.value = index
globalActiveLanguage.value = props.languages[index].name
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| const toggleFullscreen = () => { | ||||||||||||||||||||
| isFullscreen.value = !isFullscreen.value | ||||||||||||||||||||
| document.body.style.overflow = isFullscreen.value ? 'hidden' : '' | ||||||||||||||||||||
|
|
@@ -58,7 +106,7 @@ const langIcon = (lang) => { | |||||||||||||||||||
| v-for="(lang, index) in languages" | ||||||||||||||||||||
| :key="index" | ||||||||||||||||||||
| :class="['tab-button', { active: activeTab === index }]" | ||||||||||||||||||||
| @click="activeTab = index" | ||||||||||||||||||||
| @click="setActiveTab(index)" | ||||||||||||||||||||
| > | ||||||||||||||||||||
| <span class="lang-icon">{{ langIcon(lang.name) }}</span> | ||||||||||||||||||||
| {{ lang.name }} | ||||||||||||||||||||
|
|
||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GLOBAL_LANG_KEYholds a language name instead of a storage key identifier.The constant value
'java'is a language name, not a storage key. This means all preferences are stored underlocalStorage.key = 'java'(e.g.,localStorage.setItem('java', 'C++')when C++ is selected). The nameGLOBAL_LANG_KEYstrongly implies it should hold a descriptive key identifier like'preferred-language'.🐛 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents