Skip to content

feat(helper): add Message class to handle message #417

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

Merged
merged 7 commits into from
Mar 28, 2025
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: 4 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export default vuepress(
],
'@typescript-eslint/no-dynamic-delete': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-empty-function': [
'error',
{ allow: ['private-constructors'] },
],
'@typescript-eslint/no-floating-promises': [
'error',
{
Expand Down
58 changes: 58 additions & 0 deletions tools/helper/src/client/styles/message.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@keyframes message-move-in {
0% {
opacity: 0;
transform: translateY(-100%);
}

100% {
opacity: 1;
transform: translateY(0);
}
}

@keyframes message-move-out {
0% {
opacity: 1;
transform: translateY(0);
}

100% {
opacity: 0;
transform: translateY(-100%);
}
}

#message-container {
position: fixed;
inset: calc(var(--message-offset, 3.6rem) + 1rem) 0 auto;
z-index: 75;
text-align: center;
}

.message-item {
display: inline-block;

padding: 8px 10px;
border-radius: 3px;

background: var(--vp-c-bg);
color: var(--vp-c-text);
box-shadow: 0 0 10px 0 var(--vp-c-shadow);

font-size: 14px;

&.move-in {
animation: message-move-in 0.3s ease-in-out;
}

&.move-out {
animation: message-move-out 0.3s ease-in-out;
animation-fill-mode: forwards;
}

svg {
position: relative;
bottom: -0.125em;
margin-inline-end: 5px;
}
}
1 change: 1 addition & 0 deletions tools/helper/src/client/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './getHeaders.js'
export * from './isFocusingTextControl.js'
export * from './isKeyMatched.js'
export * from './hasGlobalComponent.js'
export * from './message.js'
export * from './wait.js'
63 changes: 63 additions & 0 deletions tools/helper/src/client/utils/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { keys } from '../../shared/index.js'

const containerId = 'message-container'

export class Message {
private elements: Record<number, HTMLDivElement> = {}

private constructor() {}

public static get containerElement(): HTMLElement {
let containerElement = document.getElementById(containerId)

if (containerElement) return containerElement

containerElement = document.createElement('div')

containerElement.id = containerId
document.body.appendChild(containerElement)

return containerElement
}

public pop(html: string, duration = 2000): number {
const messageElement = document.createElement('div')
const messageId = Date.now()
Copy link
Preview

Copilot AI Mar 28, 2025

Choose a reason for hiding this comment

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

Using Date.now() to generate a messageId may lead to collisions if multiple messages are created in rapid succession. Consider using a more robust unique ID generator.

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

@pengzhanbo I think the AI is right about this, any suggestions with it?


messageElement.className = 'message-item move-in'
messageElement.innerHTML = html
Message.containerElement.appendChild(messageElement)
this.elements[messageId] = messageElement

if (duration > 0)
setTimeout(() => {
this.close(messageId)
}, duration)

return messageId
}

public close(messageId?: number): void {
if (messageId) {
const messageElement = this.elements[messageId]

messageElement.classList.remove('move-in')
messageElement.classList.add('move-out')
messageElement.addEventListener('animationend', () => {
messageElement.remove()
delete this.elements[messageId]
})
} else {
keys(this.elements).forEach((id) => {
this.close(Number(id))
})
}
}

public destroy(): void {
const containerElement = document.getElementById(containerId)

if (containerElement) document.body.removeChild(containerElement)
this.elements = {}
}
}
Loading