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(docs): [anchor] Optimize the anchor document. #2495

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 2 deletions examples/sites/demos/pc/app/anchor/change-composition-api.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<script setup>
import { ref } from 'vue'
import { TinyAnchor, TinyModal } from '@opentiny/vue'
import { TinyAnchor } from '@opentiny/vue'

const links = ref([
{
Expand Down Expand Up @@ -42,6 +42,6 @@ const links = ref([
])

function handleChange(link) {
TinyModal.message({ message: `change${link}`, status: 'info' })
console.log(`当前锚点${link}`)
}
</script>
12 changes: 8 additions & 4 deletions examples/sites/demos/pc/app/anchor/change.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ test('change事件', async ({ page }) => {
const anchor = page.locator('.tiny-anchor')
const link1 = anchor.getByRole('link', { name: '演示' })
const link2 = anchor.getByRole('link', { name: 'change 事件' })
const modal = page.locator('.tiny-modal')

const values = [] as string[]
page.on('console', async (msg) => {
for (const arg of msg.args()) {
values.push(await arg.jsonValue())
}
})
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider improving console message handling robustness.

The current implementation could benefit from several improvements:

  1. Add cleanup by removing the console listener after the test
  2. Add error handling for JSON conversion
  3. Consider clearing the array between clicks to avoid memory buildup

Consider this improved implementation:

  const values = [] as string[]
+ const consoleListener = async (msg) => {
+   try {
+     for (const arg of msg.args()) {
+       values.push(await arg.jsonValue())
+     }
+   } catch (error) {
+     console.error('Failed to process console message:', error)
+   }
+ }
- page.on('console', async (msg) => {
-   for (const arg of msg.args()) {
-     values.push(await arg.jsonValue())
-   }
- })
+ page.on('console', consoleListener)
+ test.afterEach(() => {
+   values.length = 0
+   page.removeListener('console', consoleListener)
+ })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const values = [] as string[]
page.on('console', async (msg) => {
for (const arg of msg.args()) {
values.push(await arg.jsonValue())
}
})
const values = [] as string[]
const consoleListener = async (msg) => {
try {
for (const arg of msg.args()) {
values.push(await arg.jsonValue())
}
} catch (error) {
console.error('Failed to process console message:', error)
}
}
page.on('console', consoleListener)
test.afterEach(() => {
values.length = 0
page.removeListener('console', consoleListener)
})

await link1.click()
await expect(modal).toHaveCount(1)
await link2.click()
await expect(modal).toHaveCount(2)
await expect(modal.last()).toHaveText(/#change/)
expect(values.length).toBe(2)
expect(values[0].startsWith('当前锚点#demonstrate')).toBeTruthy()
})
4 changes: 2 additions & 2 deletions examples/sites/demos/pc/app/anchor/change.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</template>

<script>
import { TinyAnchor, TinyModal } from '@opentiny/vue'
import { TinyAnchor } from '@opentiny/vue'

export default {
components: {
Expand Down Expand Up @@ -49,7 +49,7 @@ export default {
},
methods: {
handleChange(link) {
TinyModal.message({ message: `change${link}`, status: 'info' })
console.log(`当前锚点${link}`)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,29 @@ function handleClick(e, link) {
.scroll-container {
height: 40vh;
overflow: auto;
text-align: center;
font-size: 24px;
}
.scroll-container > div {
padding-top: 20%;
}
.sec-1 {
height: 50vh;
background: rgba(135, 206, 235, 0.1);
background: #b6d4f2;
}
.sec-2 {
height: 50vh;
background: rgba(135, 206, 235, 0.3);
background: #c2c2c2;
}
.sec-3 {
background: rgba(135, 206, 235, 0.6);
padding-top: 20vh;
background: #fff4e8;
}
.sec-3-1 {
height: 50vh;
background: rgba(135, 206, 235, 0.8);
background: #b9e683;
}
.sec-3-2 {
height: 50vh;
background: rgba(135, 206, 235, 1);
background: #f2c5c2;
}
</style>
16 changes: 10 additions & 6 deletions examples/sites/demos/pc/app/anchor/set-container.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,29 @@ export default {
.scroll-container {
height: 40vh;
overflow: auto;
text-align: center;
font-size: 24px;
}
.scroll-container > div {
padding-top: 20%;
}
.sec-1 {
height: 50vh;
background: rgba(135, 206, 235, 0.1);
background: #b6d4f2;
}
.sec-2 {
height: 50vh;
background: rgba(135, 206, 235, 0.3);
background: #c2c2c2;
}
.sec-3 {
background: rgba(135, 206, 235, 0.6);
padding-top: 20vh;
background: #fff4e8;
}
.sec-3-1 {
height: 50vh;
background: rgba(135, 206, 235, 0.8);
background: #b9e683;
}
.sec-3-2 {
height: 50vh;
background: rgba(135, 206, 235, 1);
background: #f2c5c2;
}
</style>
Loading