@bbki.ng/bb-msg-history 0.1.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +174 -19
- package/dist/component.d.ts +35 -0
- package/dist/component.js +141 -0
- package/dist/const/authors.d.ts +10 -0
- package/dist/const/authors.js +29 -0
- package/dist/const/styles.d.ts +12 -0
- package/dist/const/styles.js +275 -0
- package/dist/const/theme.d.ts +6 -0
- package/dist/const/theme.js +32 -0
- package/dist/index.d.ts +3 -14
- package/dist/index.dev.js +12 -0
- package/dist/index.js +1 -1
- package/dist/types/index.d.ts +33 -0
- package/dist/types/index.js +4 -0
- package/dist/utils/author-resolver.d.ts +11 -0
- package/dist/utils/author-resolver.js +71 -0
- package/dist/utils/avatar.d.ts +4 -0
- package/dist/utils/avatar.js +18 -0
- package/dist/utils/html.d.ts +12 -0
- package/dist/utils/html.js +33 -0
- package/dist/utils/message-builder.d.ts +13 -0
- package/dist/utils/message-builder.js +49 -0
- package/dist/utils/message-parser.d.ts +6 -0
- package/dist/utils/message-parser.js +22 -0
- package/dist/utils/registration.d.ts +8 -0
- package/dist/utils/registration.js +29 -0
- package/dist/utils/tooltip.d.ts +5 -0
- package/dist/utils/tooltip.js +17 -0
- package/package.json +7 -6
- package/src/component.ts +172 -0
- package/src/const/authors.ts +32 -0
- package/src/const/styles.ts +278 -0
- package/src/const/theme.ts +34 -0
- package/src/index.ts +10 -478
- package/src/types/index.ts +37 -0
- package/src/utils/author-resolver.ts +81 -0
- package/src/utils/avatar.ts +19 -0
- package/src/utils/html.ts +35 -0
- package/src/utils/message-builder.ts +58 -0
- package/src/utils/message-parser.ts +27 -0
- package/src/utils/registration.ts +35 -0
- package/src/utils/tooltip.ts +16 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { AuthorConfig } from '../types/index.js';
|
|
2
|
+
import { escapeHtml } from './html.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Build avatar HTML string
|
|
6
|
+
*/
|
|
7
|
+
export function buildAvatarHtml(author: string, config: AuthorConfig, showAvatar: boolean): string {
|
|
8
|
+
return `
|
|
9
|
+
<div class="avatar-wrapper ${showAvatar ? '' : 'avatar-wrapper--hidden'}"
|
|
10
|
+
data-author="${escapeHtml(author)}">
|
|
11
|
+
<div class="avatar">${config.avatar}</div>
|
|
12
|
+
<div class="avatar-tooltip">${escapeHtml(author)}</div>
|
|
13
|
+
</div>
|
|
14
|
+
`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Build a single message row HTML string
|
|
19
|
+
*/
|
|
20
|
+
export function buildMessageRowHtml(
|
|
21
|
+
author: string,
|
|
22
|
+
text: string,
|
|
23
|
+
config: AuthorConfig,
|
|
24
|
+
isSubsequent: boolean
|
|
25
|
+
): string {
|
|
26
|
+
const showAvatar = !isSubsequent;
|
|
27
|
+
const side = config.side;
|
|
28
|
+
const avatarHtml = buildAvatarHtml(author, config, showAvatar);
|
|
29
|
+
|
|
30
|
+
return `
|
|
31
|
+
<div class="msg-row msg-row--${side} ${isSubsequent ? 'msg-row--subsequent' : 'msg-row--new-author'}">
|
|
32
|
+
${side === 'left' ? avatarHtml : ''}
|
|
33
|
+
|
|
34
|
+
<div class="msg-content">
|
|
35
|
+
<div class="msg-bubble msg-bubble--${side}"
|
|
36
|
+
style="background-color: ${config.bubbleColor}; color: ${config.textColor};">
|
|
37
|
+
${escapeHtml(text)}
|
|
38
|
+
</div>
|
|
39
|
+
</div>
|
|
40
|
+
|
|
41
|
+
${side === 'right' ? avatarHtml : ''}
|
|
42
|
+
</div>
|
|
43
|
+
`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Setup tooltip for a single avatar wrapper element
|
|
48
|
+
*/
|
|
49
|
+
export function setupTooltipForElement(wrapper: Element): void {
|
|
50
|
+
wrapper.addEventListener('mouseenter', () => {
|
|
51
|
+
const tooltip = wrapper.querySelector('.avatar-tooltip') as HTMLElement;
|
|
52
|
+
if (!tooltip) return;
|
|
53
|
+
const rect = wrapper.getBoundingClientRect();
|
|
54
|
+
const tooltipRect = tooltip.getBoundingClientRect();
|
|
55
|
+
tooltip.style.left = `${rect.left + rect.width / 2 - tooltipRect.width / 2}px`;
|
|
56
|
+
tooltip.style.top = `${rect.top - tooltipRect.height - 8}px`;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Message } from '../types/index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse text content into message array
|
|
5
|
+
* Format: `author: text` (one message per line)
|
|
6
|
+
*/
|
|
7
|
+
export function parseMessages(textContent: string | null): Message[] {
|
|
8
|
+
const raw = textContent || '';
|
|
9
|
+
const messages: Message[] = [];
|
|
10
|
+
|
|
11
|
+
for (const line of raw.split('\n')) {
|
|
12
|
+
const trimmed = line.trim();
|
|
13
|
+
if (!trimmed) continue;
|
|
14
|
+
|
|
15
|
+
const colonIdx = trimmed.indexOf(':');
|
|
16
|
+
if (colonIdx <= 0) continue;
|
|
17
|
+
|
|
18
|
+
const author = trimmed.slice(0, colonIdx).trim();
|
|
19
|
+
const text = trimmed.slice(colonIdx + 1).trim();
|
|
20
|
+
|
|
21
|
+
if (author && text) {
|
|
22
|
+
messages.push({ author, text });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return messages;
|
|
27
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { THEME } from '../const/theme.js';
|
|
2
|
+
import { FALLBACK_STYLES } from '../const/styles.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Define the custom element
|
|
6
|
+
*/
|
|
7
|
+
export function define(
|
|
8
|
+
BBMsgHistoryClass: CustomElementConstructor,
|
|
9
|
+
tagName = 'bb-msg-history'
|
|
10
|
+
): void {
|
|
11
|
+
if (!customElements.get(tagName)) {
|
|
12
|
+
customElements.define(tagName, tagName === 'bb-msg-history'
|
|
13
|
+
? BBMsgHistoryClass
|
|
14
|
+
: class extends BBMsgHistoryClass {}
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Initialize with fallback for unsupported browsers
|
|
21
|
+
*/
|
|
22
|
+
export function initBBMsgHistory(BBMsgHistoryClass: CustomElementConstructor): void {
|
|
23
|
+
try {
|
|
24
|
+
define(BBMsgHistoryClass);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.warn('BBMsgHistory registration failed, falling back to plain text:', error);
|
|
27
|
+
|
|
28
|
+
document.querySelectorAll('bb-msg-history').forEach(el => {
|
|
29
|
+
const pre = document.createElement('pre');
|
|
30
|
+
pre.style.cssText = FALLBACK_STYLES;
|
|
31
|
+
pre.textContent = el.textContent || '';
|
|
32
|
+
el.replaceWith(pre);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Setup dynamic tooltip positioning
|
|
3
|
+
* Tooltips are positioned fixed to avoid overflow clipping from parent containers
|
|
4
|
+
*/
|
|
5
|
+
export function setupTooltips(shadowRoot: ShadowRoot): void {
|
|
6
|
+
shadowRoot.querySelectorAll('.avatar-wrapper').forEach(wrapper => {
|
|
7
|
+
wrapper.addEventListener('mouseenter', () => {
|
|
8
|
+
const tooltip = wrapper.querySelector('.avatar-tooltip') as HTMLElement;
|
|
9
|
+
if (!tooltip) return;
|
|
10
|
+
const rect = wrapper.getBoundingClientRect();
|
|
11
|
+
const tooltipRect = tooltip.getBoundingClientRect();
|
|
12
|
+
tooltip.style.left = `${rect.left + rect.width / 2 - tooltipRect.width / 2}px`;
|
|
13
|
+
tooltip.style.top = `${rect.top - tooltipRect.height - 8}px`;
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
}
|