游戏模式
This commit is contained in:
parent
6a244555d6
commit
e844cbd910
45
src/chat.js
45
src/chat.js
@ -1,5 +1,5 @@
|
|||||||
import { get, set, del, update, createStore, values } from 'idb-keyval'
|
import { get, set, del, update, createStore, values } from 'idb-keyval'
|
||||||
import { Span, Button, List, ListItem, TextArea, createElement } from './weigets.js'
|
import { Span, Button, List, ListItem, TextArea, createElement, Input } from './weigets.js'
|
||||||
|
|
||||||
export default class Chat {
|
export default class Chat {
|
||||||
constructor({ name, EventListeners = {}, onsend, onexit }) {
|
constructor({ name, EventListeners = {}, onsend, onexit }) {
|
||||||
@ -7,29 +7,43 @@ export default class Chat {
|
|||||||
this.store = createStore(`db-chat-${name}`, `store-chat-${name}`)
|
this.store = createStore(`db-chat-${name}`, `store-chat-${name}`)
|
||||||
this.ul = List({ classList: ['chat-list'] })
|
this.ul = List({ classList: ['chat-list'] })
|
||||||
this.EventListeners = EventListeners
|
this.EventListeners = EventListeners
|
||||||
document.body.appendChild(createElement({
|
this.element = createElement({
|
||||||
|
style: {
|
||||||
|
position: 'fixed', bottom: 0, left: '50%',
|
||||||
|
maxWidth: '64rem', width: '100%',
|
||||||
|
transform: 'translate(-50%, 0)',
|
||||||
|
display: 'none',
|
||||||
|
},
|
||||||
children: [
|
children: [
|
||||||
this.ul,
|
this.ul,
|
||||||
TextArea({
|
createElement({
|
||||||
|
style: {
|
||||||
|
display: 'flex',
|
||||||
|
margin: '1rem 2rem',
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
Input({
|
||||||
type: 'text',
|
type: 'text',
|
||||||
placeholder: '输入聊天内容',
|
placeholder: '输入聊天内容',
|
||||||
style: {
|
style: {
|
||||||
height: '5rem',
|
flex: 1,
|
||||||
margin: '1rem 2rem',
|
height: '3rem',
|
||||||
padding: '1rem',
|
padding: '0 1rem',
|
||||||
boxSizing: 'border-box',
|
boxSizing: 'border-box',
|
||||||
boxShadow: '0 0 1rem #eee',
|
boxShadow: '0 0 1rem #eee',
|
||||||
|
border: 'none',
|
||||||
|
outline: 'none',
|
||||||
},
|
},
|
||||||
onkeydown: event => {
|
onkeydown: event => {
|
||||||
const text = event.target.value.trim()
|
const text = event.target.value.trim()
|
||||||
if (text && event.ctrlKey && event.key === 'Enter') {
|
if (text && event.key === 'Enter') {
|
||||||
this.发送消息(text)
|
this.发送消息(text)
|
||||||
event.target.value = ''
|
event.target.value = ''
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
Button({
|
Button({
|
||||||
textContent: '发送(ctrl+Enter)',
|
textContent: '发送(Enter)',
|
||||||
onclick: event => {
|
onclick: event => {
|
||||||
const text = event.target.previousSibling.value.trim()
|
const text = event.target.previousSibling.value.trim()
|
||||||
if (text) {
|
if (text) {
|
||||||
@ -38,7 +52,6 @@ export default class Chat {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
style: {
|
style: {
|
||||||
margin: '1rem 2rem',
|
|
||||||
padding: '.5rem 1rem',
|
padding: '.5rem 1rem',
|
||||||
boxSizing: 'border-box',
|
boxSizing: 'border-box',
|
||||||
boxShadow: '0 0 1rem #eee',
|
boxShadow: '0 0 1rem #eee',
|
||||||
@ -46,7 +59,10 @@ export default class Chat {
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
]
|
]
|
||||||
}))
|
})
|
||||||
|
]
|
||||||
|
})
|
||||||
|
document.body.appendChild(this.element)
|
||||||
document.head.appendChild(createElement({
|
document.head.appendChild(createElement({
|
||||||
innerText: `
|
innerText: `
|
||||||
ul.chat-list {
|
ul.chat-list {
|
||||||
@ -69,6 +85,15 @@ export default class Chat {
|
|||||||
}`
|
}`
|
||||||
}, 'style'))
|
}, 'style'))
|
||||||
this.载入消息()
|
this.载入消息()
|
||||||
|
this.挂载全局快捷键()
|
||||||
|
}
|
||||||
|
挂载全局快捷键() {
|
||||||
|
document.addEventListener('keydown', event => {
|
||||||
|
if (event.key === 'Enter' || event.key === '/') {
|
||||||
|
this.element.style.display = 'block'
|
||||||
|
this.element.querySelector('input').focus()
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
// 收到应答(对方确认消息已被接收)
|
// 收到应答(对方确认消息已被接收)
|
||||||
answer(data) {
|
answer(data) {
|
||||||
|
Loading…
Reference in New Issue
Block a user