Compare commits
3 Commits
200f5fd0aa
...
9e3c1c554f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e3c1c554f | ||
|
|
01ece27e75 | ||
|
|
da505305a3 |
90
client-protocol/ChatAttachment.ts
Normal file
90
client-protocol/ChatAttachment.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import { ApiCallbackMessage } from 'lingchair-internal-shared'
|
||||
import BaseClientObject from './BaseClientObject.ts'
|
||||
import CallbackError from './CallbackError.ts'
|
||||
import LingChairClient from './LingChairClient.ts'
|
||||
|
||||
export default class ChatAttachment extends BaseClientObject {
|
||||
declare file_hash: string
|
||||
declare file_name: string
|
||||
constructor(client: LingChairClient, {
|
||||
file_hash,
|
||||
file_name
|
||||
}: {
|
||||
file_hash: string,
|
||||
file_name: string
|
||||
}) {
|
||||
super(client)
|
||||
this.file_name = file_name
|
||||
this.file_hash = file_hash
|
||||
}
|
||||
async blob() {
|
||||
try {
|
||||
return await this.blobOrThrow()
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
fetch(init?: RequestInit) {
|
||||
const url = this.client.getUrlForFileByHash(this.file_hash)
|
||||
return fetch(url!, init)
|
||||
}
|
||||
async blobOrThrow() {
|
||||
const re = await this.fetch()
|
||||
const blob = await re.blob()
|
||||
if (!re.ok) throw new CallbackError({
|
||||
msg: await blob.text(),
|
||||
code: re.status,
|
||||
} as ApiCallbackMessage)
|
||||
return blob
|
||||
}
|
||||
async getMimeType() {
|
||||
try {
|
||||
return await this.getMimeTypeOrThrow()
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
async getMimeTypeOrThrow() {
|
||||
const re = await this.fetch({
|
||||
method: 'HEAD'
|
||||
})
|
||||
if (re.ok) {
|
||||
const t = re.headers.get('content-type')
|
||||
if (t)
|
||||
return t
|
||||
throw new Error("Unable to get Content-Type")
|
||||
}
|
||||
throw new CallbackError({
|
||||
msg: await re.text(),
|
||||
code: re.status,
|
||||
} as ApiCallbackMessage)
|
||||
}
|
||||
async getLength() {
|
||||
try {
|
||||
return await this.getLengthOrThrow()
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
async getLengthOrThrow() {
|
||||
const re = await this.fetch({
|
||||
method: 'HEAD'
|
||||
})
|
||||
if (re.ok) {
|
||||
const contentLength = re.headers.get('content-length')
|
||||
if (contentLength)
|
||||
return parseInt(contentLength)
|
||||
throw new Error("Unable to get Content-Length")
|
||||
}
|
||||
throw new CallbackError({
|
||||
msg: await re.text(),
|
||||
code: re.status,
|
||||
} as ApiCallbackMessage)
|
||||
}
|
||||
getFileHash() {
|
||||
return this.file_hash
|
||||
}
|
||||
getFileName() {
|
||||
return this.file_name
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,7 @@ import MessageBean from "./bean/MessageBean.ts"
|
||||
import LingChairClient from "./LingChairClient.ts"
|
||||
import Chat from "./Chat.ts"
|
||||
import User from "./User.ts"
|
||||
import CallbackError from "./CallbackError.ts"
|
||||
import ApiCallbackMessage from "./ApiCallbackMessage.ts"
|
||||
import ChatAttachment from "./ChatAttachment.ts"
|
||||
|
||||
import * as marked from 'marked'
|
||||
|
||||
@@ -37,93 +36,18 @@ export class ChatMention extends BaseClientObject {
|
||||
}
|
||||
}
|
||||
|
||||
type FileType = 'Video' | 'Image' | 'File'
|
||||
type MentionType = 'ChatMention' | 'UserMention'
|
||||
type ChatMentionType = 'ChatMention' | 'UserMention'
|
||||
type ChatFileType = 'Video' | 'Image' | 'File'
|
||||
|
||||
export class ChatAttachment extends BaseClientObject {
|
||||
declare file_hash: string
|
||||
declare file_name: string
|
||||
constructor(client: LingChairClient, {
|
||||
file_hash,
|
||||
file_name
|
||||
}: {
|
||||
file_hash: string,
|
||||
file_name: string
|
||||
}) {
|
||||
super(client)
|
||||
this.file_name = file_name
|
||||
this.file_hash = file_hash
|
||||
}
|
||||
async blob() {
|
||||
try {
|
||||
return await this.blobOrThrow()
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
fetch(init?: RequestInit) {
|
||||
const url = this.client.getUrlForFileByHash(this.file_hash)
|
||||
return fetch(url!, init)
|
||||
}
|
||||
async blobOrThrow() {
|
||||
const re = await this.fetch()
|
||||
const blob = await re.blob()
|
||||
if (!re.ok) throw new CallbackError({
|
||||
msg: await blob.text(),
|
||||
code: re.status,
|
||||
} as ApiCallbackMessage)
|
||||
return blob
|
||||
}
|
||||
async getMimeType() {
|
||||
try {
|
||||
return await this.getMimeTypeOrThrow()
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
async getMimeTypeOrThrow() {
|
||||
const re = await this.fetch({
|
||||
method: 'HEAD'
|
||||
})
|
||||
if (re.ok) {
|
||||
const t = re.headers.get('content-type')
|
||||
if (t)
|
||||
return t
|
||||
throw new Error("Unable to get Content-Type")
|
||||
}
|
||||
throw new CallbackError({
|
||||
msg: await re.text(),
|
||||
code: re.status,
|
||||
} as ApiCallbackMessage)
|
||||
}
|
||||
async getLength() {
|
||||
try {
|
||||
return await this.getLengthOrThrow()
|
||||
} catch (_) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
async getLengthOrThrow() {
|
||||
const re = await this.fetch({
|
||||
method: 'HEAD'
|
||||
})
|
||||
if (re.ok) {
|
||||
const contentLength = re.headers.get('content-length')
|
||||
if (contentLength)
|
||||
return parseInt(contentLength)
|
||||
throw new Error("Unable to get Content-Length")
|
||||
}
|
||||
throw new CallbackError({
|
||||
msg: await re.text(),
|
||||
code: re.status,
|
||||
} as ApiCallbackMessage)
|
||||
}
|
||||
getFileHash() {
|
||||
return this.file_hash
|
||||
}
|
||||
getFileName() {
|
||||
return this.file_name
|
||||
}
|
||||
type ChatParserTransformers = {
|
||||
attachment?: ({ text, fileType, attachment }: { text: string, fileType: ChatFileType, attachment: ChatAttachment }) => string,
|
||||
mention?: ({ text, mentionType, mention }: { text: string, mentionType: ChatMentionType, mention: ChatMention }) => string,
|
||||
}
|
||||
|
||||
export type {
|
||||
ChatMentionType,
|
||||
ChatFileType,
|
||||
ChatParserTransformers,
|
||||
}
|
||||
|
||||
export default class Message extends BaseClientObject {
|
||||
@@ -152,10 +76,7 @@ export default class Message extends BaseClientObject {
|
||||
parseWithTransformers({
|
||||
attachment,
|
||||
mention,
|
||||
}: {
|
||||
attachment?: ({ text, fileType, attachment }: { text: string, fileType: FileType, attachment: ChatAttachment }) => string,
|
||||
mention?: ({ text, mentionType, mention }: { text: string, mentionType: MentionType, mention: ChatMention }) => string,
|
||||
}) {
|
||||
}: ChatParserTransformers) {
|
||||
return new marked.Marked({
|
||||
async: false,
|
||||
extensions: [
|
||||
@@ -178,8 +99,8 @@ export default class Message extends BaseClientObject {
|
||||
{
|
||||
name: 'image',
|
||||
renderer: ({ text, href }) => {
|
||||
const mentionType = /^(UserMention|ChatMention)=.*/.exec(text)?.[1] as MentionType
|
||||
const fileType = (/^(Video|File|Image)=.*/.exec(text)?.[1] || 'Image') as FileType
|
||||
const mentionType = /^(UserMention|ChatMention)=.*/.exec(text)?.[1] as ChatMentionType
|
||||
const fileType = (/^(Video|File|Image)=.*/.exec(text)?.[1] || 'Image') as ChatFileType
|
||||
|
||||
if (fileType != null && /tws:\/\/file\?hash=[A-Za-z0-9]+$/.test(href)) {
|
||||
const file_hash = /^tws:\/\/file\?hash=(.*)/.exec(href)?.[1]!
|
||||
|
||||
@@ -7,10 +7,16 @@ import GroupSettingsBean from "./bean/GroupSettingsBean.ts"
|
||||
import JoinRequestBean from "./bean/JoinRequestBean.ts"
|
||||
import MessageBean from "./bean/MessageBean.ts"
|
||||
import RecentChatBean from "./bean/RecentChatBean.ts"
|
||||
import Message, { ChatAttachment, ChatMention } from "./Message.ts"
|
||||
import Message, {
|
||||
ChatMention,
|
||||
ChatParserTransformers,
|
||||
ChatMentionType,
|
||||
ChatFileType,
|
||||
} from "./Message.ts"
|
||||
|
||||
import LingChairClient from "./LingChairClient.ts"
|
||||
import CallbackError from "./CallbackError.ts"
|
||||
import ChatAttachment from "./ChatAttachment.ts"
|
||||
|
||||
export {
|
||||
LingChairClient,
|
||||
@@ -19,6 +25,7 @@ export {
|
||||
Chat,
|
||||
User,
|
||||
UserMySelf,
|
||||
|
||||
Message,
|
||||
ChatAttachment,
|
||||
ChatMention,
|
||||
@@ -29,4 +36,10 @@ export {
|
||||
RecentChatBean,
|
||||
JoinRequestBean,
|
||||
}
|
||||
export type { GroupSettingsBean }
|
||||
export type {
|
||||
ChatParserTransformers,
|
||||
ChatMentionType,
|
||||
ChatFileType,
|
||||
|
||||
GroupSettingsBean,
|
||||
}
|
||||
|
||||
5
client-protocol/type/ChatParserTransformers.ts
Normal file
5
client-protocol/type/ChatParserTransformers.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import ChatAttachment from '../ChatAttachment.ts'
|
||||
import { ChatMention } from '../Message.ts'
|
||||
import ChatFileType from './ChatFileType.ts'
|
||||
|
||||
|
||||
3
client/env.d.ts
vendored
3
client/env.d.ts
vendored
@@ -6,8 +6,9 @@ declare global {
|
||||
namespace React {
|
||||
namespace JSX {
|
||||
interface IntrinsicElements {
|
||||
'input-element': {
|
||||
'mdui-patched-textarea': {
|
||||
'value'?: string
|
||||
insertHtml: (html: string) => void
|
||||
} & React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,9 @@ export default class MduiPatchedTextAreaElement extends HTMLElement {
|
||||
this._lastValue = this.value || ''
|
||||
this.dispatchEvent(new Event('change', { bubbles: true }))
|
||||
}
|
||||
// 消除 <br> 对 placeholder 的影响
|
||||
if (this.value == '')
|
||||
this.value = ''
|
||||
})
|
||||
this.inputDiv.addEventListener('paste', (e: ClipboardEvent) => {
|
||||
e.preventDefault()
|
||||
@@ -101,6 +104,11 @@ export default class MduiPatchedTextAreaElement extends HTMLElement {
|
||||
set value(v) {
|
||||
this.inputDiv && (this.inputDiv.textContent = v)
|
||||
}
|
||||
insertHtml(html: string) {
|
||||
this.inputDiv?.focus()
|
||||
|
||||
document.execCommand('insertHTML', false, html)
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define('mdui-patched-textarea', MduiPatchedTextAreaElement)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Message } from "lingchair-client-protocol"
|
||||
import { ChatParserTransformers, Message } from "lingchair-client-protocol"
|
||||
import isMobileUI from "../../utils/isMobileUI"
|
||||
import useAsyncEffect from "../../utils/useAsyncEffect"
|
||||
import ClientCache from "../../ClientCache"
|
||||
@@ -94,35 +94,7 @@ const sanitizeConfig = {
|
||||
],
|
||||
}
|
||||
|
||||
export default function ChatMessage({ message, noUserDisplay, avatarMenuItems, messageMenuItems }: { message: Message, noUserDisplay?: boolean, avatarMenuItems?: globalThis.React.JSX.IntrinsicElements['mdui-menu-item'][], messageMenuItems?: globalThis.React.JSX.IntrinsicElements['mdui-menu-item'][] }) {
|
||||
const AppState = React.useContext(AppStateContext)
|
||||
|
||||
const [isAtRight, setAtRight] = React.useState(false)
|
||||
|
||||
const messageDropDownRef = React.useRef<Dropdown>()
|
||||
const [isMessageDropDownOpen, setMessageDropDownOpen] = React.useState(false)
|
||||
useEventListener(messageDropDownRef, 'closed', () => {
|
||||
setMessageDropDownOpen(false)
|
||||
})
|
||||
|
||||
const avatarDropDownRef = React.useRef<Dropdown>()
|
||||
const [isAvatarDropDownOpen, setAvatarDropDownOpen] = React.useState(false)
|
||||
useEventListener(avatarDropDownRef, 'closed', () => {
|
||||
setAvatarDropDownOpen(false)
|
||||
})
|
||||
|
||||
const [nickName, setNickName] = React.useState('')
|
||||
const [avatarUrl, setAvatarUrl] = React.useState('')
|
||||
useAsyncEffect(async () => {
|
||||
const user = await ClientCache.getUser(message.getUserId()!)
|
||||
setAtRight(await ClientCache.getMySelf().then((re) => re?.getId()) == user?.getId())
|
||||
setNickName(user?.getNickName() || '')
|
||||
setAvatarUrl(getClient().getUrlForFileByHash(user?.getAvatarFileHash() || '') || '')
|
||||
}, [message])
|
||||
|
||||
const messageInnerRef = React.useRef<HTMLSpanElement>(null)
|
||||
React.useEffect(() => {
|
||||
messageInnerRef.current!.innerHTML = prettyFlatParsedMessage(DOMPurify.sanitize(message.parseWithTransformers({
|
||||
const transformers: ChatParserTransformers = {
|
||||
attachment({ fileType, attachment }) {
|
||||
const url = getClient().getUrlForFileByHash(attachment.getFileHash())
|
||||
return ({
|
||||
@@ -139,7 +111,40 @@ export default function ChatMessage({ message, noUserDisplay, avatarMenuItems, m
|
||||
return `<chat-mention chat-id="${mention.chat_id}" text="${mention.text}">[对话提及]</chat-mention>`
|
||||
}
|
||||
},
|
||||
}), sanitizeConfig))
|
||||
}
|
||||
|
||||
export default function ChatMessage({ message, noUserDisplay, avatarMenuItems, messageMenuItems }: { message: Message, noUserDisplay?: boolean, avatarMenuItems?: globalThis.React.JSX.IntrinsicElements['mdui-menu-item'][], messageMenuItems?: globalThis.React.JSX.IntrinsicElements['mdui-menu-item'][] }) {
|
||||
const AppState = React.useContext(AppStateContext)
|
||||
|
||||
const [show, setShown] = React.useState(false)
|
||||
|
||||
const [isAtRight, setAtRight] = React.useState(false)
|
||||
|
||||
const messageDropDownRef = React.useRef<Dropdown>()
|
||||
const [isMessageDropDownOpen, setMessageDropDownOpen] = React.useState(false)
|
||||
useEventListener(messageDropDownRef, 'closed', () => {
|
||||
setMessageDropDownOpen(false)
|
||||
})
|
||||
|
||||
const avatarDropDownRef = React.useRef<Dropdown>()
|
||||
const [isAvatarDropDownOpen, setAvatarDropDownOpen] = React.useState(false)
|
||||
useEventListener(avatarDropDownRef, 'closed', () => {
|
||||
setAvatarDropDownOpen(false)
|
||||
})
|
||||
|
||||
const [nickName, setNickName] = React.useState(message.getUserId()! || 'System')
|
||||
const [avatarUrl, setAvatarUrl] = React.useState('')
|
||||
useAsyncEffect(async () => {
|
||||
const user = await ClientCache.getUser(message.getUserId()!)
|
||||
setAtRight(await ClientCache.getMySelf().then((re) => re?.getId()) == user?.getId())
|
||||
setNickName(user?.getNickName() || '')
|
||||
setAvatarUrl(getClient().getUrlForFileByHash(user?.getAvatarFileHash() || '') || '')
|
||||
setShown(true)
|
||||
}, [message])
|
||||
|
||||
const messageInnerRef = React.useRef<HTMLSpanElement>(null)
|
||||
React.useEffect(() => {
|
||||
messageInnerRef.current!.innerHTML = prettyFlatParsedMessage(DOMPurify.sanitize(message.parseWithTransformers(transformers), sanitizeConfig))
|
||||
|
||||
// 没有办法的办法 (笑)
|
||||
// 姐姐, 谁让您不是 React 组件呢
|
||||
@@ -150,7 +155,11 @@ export default function ChatMessage({ message, noUserDisplay, avatarMenuItems, m
|
||||
})
|
||||
}, [message])
|
||||
|
||||
return (
|
||||
return <>
|
||||
<div style={{
|
||||
display: show ? 'none' : undefined,
|
||||
padding: '5px',
|
||||
}}>加载中...</div>
|
||||
<div
|
||||
slot="trigger"
|
||||
onContextMenu={(e) => {
|
||||
@@ -165,7 +174,7 @@ export default function ChatMessage({ message, noUserDisplay, avatarMenuItems, m
|
||||
}}
|
||||
style={{
|
||||
width: "100%",
|
||||
display: "flex",
|
||||
display: show ? 'flex' : 'none',
|
||||
justifyContent: isAtRight ? "flex-end" : "flex-start",
|
||||
flexDirection: "column"
|
||||
}}>
|
||||
@@ -258,5 +267,5 @@ export default function ChatMessage({ message, noUserDisplay, avatarMenuItems, m
|
||||
</mdui-card>
|
||||
|
||||
</div>
|
||||
)
|
||||
</>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user