-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: replace v-bind="$attrs" with v-bind="editorAttrs" for improved a… #4853
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 |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| :style="codemirrorStyle" | ||
| :tab-size="4" | ||
| :autofocus="true" | ||
| v-bind="$attrs" | ||
| v-bind="editorAttrs" | ||
| /> | ||
|
|
||
| <div class="codemirror-editor__footer"> | ||
|
|
@@ -39,14 +39,15 @@ | |
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { ref, computed, watch } from 'vue' | ||
| import { ref, computed, watch, useAttrs } from 'vue' | ||
| import { Codemirror } from 'vue-codemirror' | ||
| import { python } from '@codemirror/lang-python' | ||
| import { oneDark } from '@codemirror/theme-one-dark' | ||
| import { linter, type Diagnostic } from '@codemirror/lint' | ||
| import FunctionApi from '@/api/function-lib' | ||
|
|
||
| defineOptions({ name: 'CodemirrorEditor' }) | ||
| const editorAttrs = useAttrs() as any | ||
|
|
||
| const props = defineProps<{ | ||
| title: String | ||
|
Contributor
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. There is no apparent issue with the given Vue code snippet. It appears to be setting up a CodeMirror component on a Vue template, using various plugins such as Python language support, One Dark theme, linting tools, and an API call function for functions. The code bindings are correctly defined within the template and script sections. Here's a concise overview of what this code snippet does:
If you have additional requirements or need further optimizations, let me know! |
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||
| <template> | ||||||
| <div style="width: 100%" class="function-CodemirrorEditor"> | ||||||
| <Codemirror | ||||||
| v-bind="$attrs" | ||||||
| v-bind="editorAttrs" | ||||||
| ref="cmRef" | ||||||
| v-model="model_value" | ||||||
| :extensions="extensions" | ||||||
|
|
@@ -51,7 +51,7 @@ import { json, jsonParseLinter } from '@codemirror/lang-json' | |||||
| import { oneDark } from '@codemirror/theme-one-dark' | ||||||
| import { Codemirror } from 'vue-codemirror' | ||||||
| import { linter } from '@codemirror/lint' | ||||||
| import { computed, ref } from 'vue' | ||||||
| import { computed, ref, useAttrs } from 'vue' | ||||||
| import { t } from '@/locales' | ||||||
| const props = withDefaults(defineProps<{ modelValue?: any }>(), { modelValue: () => {} }) | ||||||
| const emit = defineEmits(['update:modelValue']) | ||||||
|
|
@@ -124,6 +124,8 @@ const validate_rules = (rule: any, value: any, callback: any) => { | |||||
| return true | ||||||
| } | ||||||
|
|
||||||
| const editorAttrs = useAttrs() as any | ||||||
|
||||||
| const editorAttrs = useAttrs() as any | |
| const editorAttrs = useAttrs() |
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.
The provided code seems to be for a Vue component with a CodeMirror editor using the 'vue-codemirror' library. Here are some minor adjustments and suggestions:
-
Attribute Binding: The
v-bind="$attrs"has been replaced withv-bind="editor_attrs". This is fine, but it might be useful to include@keydown.enter.preventif you want to handle Enter key press events separately. -
Component Name Spaces: Ensure that all components like
linter,oneDark,$attrs, etc., are properly namespace qualified (e.g.,@codemirror/lint) as these could depend on environment-specific module resolution. -
Use of Composition API: Instead of importing individual functions (
computed,ref) directly from'vue', consider usinguseComputed,useReffunctions when necessary. However, all dependencies are imported at the top which is good practice.
Overall, the structure and functionality appear correct for managing a CodeMirror instance within Vue.js.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,20 +1,21 @@ | ||||||
| <template> | ||||||
| <MdEditor :language="language" noIconfont noPrettier v-bind="$attrs"> | ||||||
| <MdEditor :language="language" noIconfont noPrettier v-bind="editorAttrs"> | ||||||
| <template #defFooters> | ||||||
| <slot name="defFooters"> </slot> | ||||||
| </template> | ||||||
| </MdEditor> | ||||||
| </template> | ||||||
|
|
||||||
| <script setup lang="ts"> | ||||||
| import { computed } from 'vue' | ||||||
| import { computed, useAttrs } from 'vue' | ||||||
| import { MdEditor, config } from 'md-editor-v3' | ||||||
| import { getBrowserLang } from '@/locales/index' | ||||||
| import './assets/markdown-iconfont.js' | ||||||
| // 引入公共库中的语言配置 | ||||||
| import ZH_TW from '@vavt/cm-extension/dist/locale/zh-TW' | ||||||
|
|
||||||
| defineOptions({ name: 'MdEditor' }) | ||||||
| const editorAttrs = useAttrs() as any | ||||||
|
||||||
| const editorAttrs = useAttrs() as any | |
| const editorAttrs = useAttrs() |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,16 +1,23 @@ | ||||||
| <template> | ||||||
| <MdPreview :language="language" noIconfont noPrettier :codeFoldable="false" v-bind="$attrs" /> | ||||||
| <MdPreview | ||||||
| :language="language" | ||||||
| noIconfont | ||||||
| noPrettier | ||||||
| :codeFoldable="false" | ||||||
| v-bind="previewAttrs" | ||||||
| /> | ||||||
| </template> | ||||||
|
|
||||||
| <script setup lang="ts"> | ||||||
| import { computed } from 'vue' | ||||||
| import { computed, useAttrs } from 'vue' | ||||||
| import { MdPreview, config } from 'md-editor-v3' | ||||||
| import { getBrowserLang } from '@/locales/index' | ||||||
| import useStore from '@/stores' | ||||||
| // 引入公共库中的语言配置 | ||||||
| import ZH_TW from '@vavt/cm-extension/dist/locale/zh-TW' | ||||||
|
|
||||||
| defineOptions({ name: 'MdPreview' }) | ||||||
| const previewAttrs = useAttrs() as any | ||||||
|
||||||
| const previewAttrs = useAttrs() as any | |
| const previewAttrs = useAttrs() |
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.
The provided code is mostly correct and well-documented. However, there are a few suggestions for improvement:
-
Attribute Binding: Instead of binding
v-bind="$attrs"directly to a component that doesn't have built-in support (<MdPreview>), it might be beneficial to apply these attributes manually if needed. -
Consistency with Attribute Syntax: The
v-on:syntax should be used consistently. In this case, you can remove the unnecessary:before@. -
Variable Naming: Use descriptive variable names where appropriate to improve readability.
-
Comments: Ensure comments are clear and concise. If additional documentation is required, consider updating it.
Here's an optimized version of the code considering these points:
<!-- Correcting attribute binding -->
<template>
<MdPreview
:language="computedLanguage"
noIconfont
noPrettier
:codeFoldable="false"
v-on="$attrs" <!-- Consistent attribute binding using @ instead of : -->
/>
</template>
<script setup lang="ts">
import { computed, useAttrs } from 'vue'
import { MdPreview, config } from 'md-editor-v3'
import { getBrowserLang } from '@/locales/index'
import useStore from '@/stores'
// Using useAttrs to access any extra props passed to the component
const previewAttrs = useAttrs() as Record<string, any>
// Importing Chinese Traditional Locale configuration for Vue Tiptap extension
import ZH_TW from '@vavt/cm-extension/dist/locale/zh-TW'
defineOptions({ name: 'MdPreview' })
// Setting up global editor configuration
config({
locale: {
...ZH_TW,
// Additional configuration settings if needed
},
})
// Computed property to determine the active language or default to browser language
const computedLanguage = computed(() => [
user.getLanguage(),
browserLang(), // Assuming browserLang is defined somewhere else in your application
].find(lang => !!lang) || '')
/**
* Fetches the current browser language based on navigator.locale.
*/
function browserLang(): string | null {
return navigator.language.split('-')[0]; // Example function definition
}
</script>Key Changes:
- Used
'@'prefix for event bindings to adhere to modern JavaScript practices. - Ensured consistent variable naming for better readability (
computedLanguage,browserLang). - Provided a commented-out example showing how to fetch and set the browser language.
- Removed unused imports and variables.
These changes make the code more readable and maintainable while leveraging existing Vue features effectively.
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.
useAttrs()is cast toany, which drops type checking for forwarded attrs. Prefer keeping the inferred type (or casting toRecord<string, unknown>/a more specific type) rather thanas anysovue-tsccan catch invalid bindings.