anentrypoint-design 0.0.28 → 0.0.29

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.
@@ -0,0 +1,61 @@
1
+ import { register } from './debug.js';
2
+
3
+ let _marked = null;
4
+ let _purify = null;
5
+ let _stats = { renders: 0, sanitizedTags: 0 };
6
+
7
+ async function loadMarked() {
8
+ if (_marked) return _marked;
9
+ const mod = await import('https://cdn.jsdelivr.net/npm/marked@15.0.7/lib/marked.esm.js');
10
+ _marked = mod.marked;
11
+ _marked.setOptions({ breaks: true, gfm: true });
12
+ return _marked;
13
+ }
14
+
15
+ async function loadPurify() {
16
+ if (_purify) return _purify;
17
+ const mod = await import('https://cdn.jsdelivr.net/npm/dompurify@3.4.1/+esm');
18
+ _purify = mod.default || mod;
19
+ if (!_purify.sanitize) throw new Error('dompurify did not load a sanitize fn');
20
+ _purify.addHook('uponSanitizeElement', (node, data) => {
21
+ if (data.tagName && data.allowedTags && !data.allowedTags[data.tagName]) _stats.sanitizedTags += 1;
22
+ });
23
+ return _purify;
24
+ }
25
+
26
+ let _ready = null;
27
+ export function ensureReady() {
28
+ if (_ready) return _ready;
29
+ _ready = Promise.all([loadMarked(), loadPurify()]).then(() => true);
30
+ return _ready;
31
+ }
32
+
33
+ export async function renderMarkdown(text) {
34
+ const [marked, purify] = await Promise.all([loadMarked(), loadPurify()]);
35
+ const dirty = marked.parse(String(text || ''));
36
+ const clean = purify.sanitize(dirty, {
37
+ FORBID_TAGS: ['script', 'iframe', 'object', 'embed', 'form'],
38
+ FORBID_ATTR: ['onerror', 'onclick', 'onload', 'onmouseover'],
39
+ ADD_ATTR: ['target', 'rel']
40
+ });
41
+ _stats.renders += 1;
42
+ return clean;
43
+ }
44
+
45
+ export function renderMarkdownSync(text) {
46
+ if (!_marked || !_purify) return null;
47
+ const dirty = _marked.parse(String(text || ''));
48
+ const clean = _purify.sanitize(dirty, {
49
+ FORBID_TAGS: ['script', 'iframe', 'object', 'embed', 'form'],
50
+ FORBID_ATTR: ['onerror', 'onclick', 'onload', 'onmouseover'],
51
+ ADD_ATTR: ['target', 'rel']
52
+ });
53
+ _stats.renders += 1;
54
+ return clean;
55
+ }
56
+
57
+ register('markdown', () => ({
58
+ loaded: { marked: !!_marked, dompurify: !!_purify },
59
+ renders: _stats.renders,
60
+ sanitizedTags: _stats.sanitizedTags
61
+ }));
@@ -0,0 +1,75 @@
1
+ import * as webjsx from '../../vendor/webjsx/index.js';
2
+ import { Chat, ChatComposer } from '../components/chat.js';
3
+ import { register } from '../debug.js';
4
+
5
+ let _stats = { mounts: 0, sends: 0 };
6
+
7
+ class DsChat extends HTMLElement {
8
+ static get observedAttributes() { return ['title', 'sub', 'placeholder']; }
9
+
10
+ constructor() {
11
+ super();
12
+ this._messages = [];
13
+ this._draft = '';
14
+ this._title = 'chat';
15
+ this._sub = '';
16
+ this._placeholder = 'message…';
17
+ }
18
+
19
+ set messages(v) { this._messages = Array.isArray(v) ? v : []; this._render(); }
20
+ get messages() { return this._messages; }
21
+
22
+ connectedCallback() {
23
+ if (this.hasAttribute('messages')) {
24
+ try { this._messages = JSON.parse(this.getAttribute('messages')); } catch {}
25
+ }
26
+ this._title = this.getAttribute('title') || this._title;
27
+ this._sub = this.getAttribute('sub') || '';
28
+ this._placeholder = this.getAttribute('placeholder') || this._placeholder;
29
+ _stats.mounts += 1;
30
+ this._render();
31
+ }
32
+
33
+ attributeChangedCallback(name, _old, val) {
34
+ if (name === 'title') this._title = val || 'chat';
35
+ if (name === 'sub') this._sub = val || '';
36
+ if (name === 'placeholder') this._placeholder = val || 'message…';
37
+ this._render();
38
+ }
39
+
40
+ pushMessage(msg) {
41
+ this._messages = [...this._messages, msg];
42
+ this._render();
43
+ }
44
+
45
+ _render() {
46
+ const onSend = (text) => {
47
+ _stats.sends += 1;
48
+ this.dispatchEvent(new CustomEvent('send', { detail: { text }, bubbles: true, composed: true }));
49
+ };
50
+ const onInput = (v) => { this._draft = v; this._render(); };
51
+ const view = Chat({
52
+ title: this._title,
53
+ sub: this._sub,
54
+ messages: this._messages,
55
+ composer: ChatComposer({
56
+ value: this._draft,
57
+ placeholder: this._placeholder,
58
+ onInput,
59
+ onSend
60
+ })
61
+ });
62
+ webjsx.applyDiff(this, view);
63
+ }
64
+ }
65
+
66
+ let _registered = false;
67
+ export function registerChatElement() {
68
+ if (_registered) return;
69
+ if (!customElements.get('ds-chat')) customElements.define('ds-chat', DsChat);
70
+ _registered = true;
71
+ }
72
+
73
+ register('ds-chat', () => ({ registered: _registered, ..._stats, instances: document.querySelectorAll('ds-chat').length }));
74
+
75
+ export { DsChat };