Skip to content

fix: replace v-bind="$attrs" with v-bind="editorAttrs" for improved a…#4853

Merged
wxg0103 merged 1 commit intov1from
pr@v1@fix_attrs
Mar 10, 2026
Merged

fix: replace v-bind="$attrs" with v-bind="editorAttrs" for improved a…#4853
wxg0103 merged 1 commit intov1from
pr@v1@fix_attrs

Conversation

@liuruibin
Copy link
Member

…ttribute handling

What this PR does / why we need it?

Summary of your change

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

Copilot AI review requested due to automatic review settings March 10, 2026 02:08
@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Mar 10, 2026

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Mar 10, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

const previewAttrs = useAttrs() as any
const { user } = useStore()
const language = computed(() => user.getLanguage() || getBrowserLang() || '')
config({
Copy link
Contributor

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:

  1. 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.

  2. Consistency with Attribute Syntax: The v-on: syntax should be used consistently. In this case, you can remove the unnecessary : before @.

  3. Variable Naming: Use descriptive variable names where appropriate to improve readability.

  4. 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.


defineExpose({ validate_rules: validate_rules })
</script>
<style lang="scss">
Copy link
Contributor

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:

  1. Attribute Binding: The v-bind="$attrs" has been replaced with v-bind="editor_attrs". This is fine, but it might be useful to include @keydown.enter.prevent if you want to handle Enter key press events separately.

  2. 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.

  3. Use of Composition API: Instead of importing individual functions (computed, ref) directly from 'vue', consider using useComputed, useRef functions 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.

const editorAttrs = useAttrs() as any

const props = defineProps<{
title: String
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  1. Imports ref, computed, watch, and useAttrs from Vue,
  2. Import Codemirror and its Python syntax (lang-python) and one-dark theme.
    3 Defines a setup for the Codemirror editor which uses these components,
  3. Sets some basic props like title,
  4. Uses v-bind="$attrs" and assigns it to a new variable editorAttrs.

If you have additional requirements or need further optimizations, let me know!

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates several Vue wrapper components to forward attributes via an explicit useAttrs() binding (e.g., v-bind="editorAttrs") instead of referencing $attrs directly in templates, aiming to standardize/improve attribute forwarding.

Changes:

  • Replace v-bind="$attrs" with v-bind="editorAttrs"/v-bind="previewAttrs" in several components.
  • Import useAttrs from vue and expose the attrs object for template binding.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
ui/src/components/markdown/MdPreview.vue Switches to useAttrs() + previewAttrs for forwarding attributes to MdPreview.
ui/src/components/markdown/MdEditor.vue Switches to useAttrs() + editorAttrs for forwarding attributes to MdEditor.
ui/src/components/dynamics-form/items/JsonInput.vue Switches Codemirror attribute forwarding to editorAttrs sourced from useAttrs().
ui/src/components/codemirror-editor/index.vue Switches Codemirror attribute forwarding to editorAttrs sourced from useAttrs().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

import ZH_TW from '@vavt/cm-extension/dist/locale/zh-TW'

defineOptions({ name: 'MdPreview' })
const previewAttrs = useAttrs() as any
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

useAttrs() already has a useful type (Readonly<Record<string, unknown>>), so casting to any unnecessarily drops type-safety and can hide mistakes when forwarding attrs. Prefer keeping the inferred type (or casting to a narrower record type if needed) instead of as any.

Suggested change
const previewAttrs = useAttrs() as any
const previewAttrs = useAttrs()

Copilot uses AI. Check for mistakes.
import ZH_TW from '@vavt/cm-extension/dist/locale/zh-TW'

defineOptions({ name: 'MdEditor' })
const editorAttrs = useAttrs() as any
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

useAttrs() is being cast to any, which removes type-safety for forwarded attributes. Prefer keeping the inferred Readonly<Record<string, unknown>> (or a more specific record type) instead of as any.

Suggested change
const editorAttrs = useAttrs() as any
const editorAttrs = useAttrs()

Copilot uses AI. Check for mistakes.
return true
}

const editorAttrs = useAttrs() as any
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

Casting useAttrs() to any removes type-safety for forwarded attributes and may mask invalid props/listeners being passed through. Prefer keeping the inferred Readonly<Record<string, unknown>> (or casting to a safer record type) instead of as any.

Suggested change
const editorAttrs = useAttrs() as any
const editorAttrs = useAttrs()

Copilot uses AI. Check for mistakes.
import FunctionApi from '@/api/function-lib'

defineOptions({ name: 'CodemirrorEditor' })
const editorAttrs = useAttrs() as any
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

useAttrs() is cast to any, which drops type checking for forwarded attrs. Prefer keeping the inferred type (or casting to Record<string, unknown>/a more specific type) rather than as any so vue-tsc can catch invalid bindings.

Suggested change
const editorAttrs = useAttrs() as any
const editorAttrs = useAttrs()

Copilot uses AI. Check for mistakes.
@wxg0103 wxg0103 merged commit c6b9583 into v1 Mar 10, 2026
10 of 12 checks passed
@wxg0103 wxg0103 deleted the pr@v1@fix_attrs branch March 10, 2026 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants