@elyracode/web-ui 0.9.58 → 1.0.1

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.
Files changed (95) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/components/SandboxedIframe.d.ts +15 -1
  3. package/dist/components/SandboxedIframe.d.ts.map +1 -1
  4. package/dist/components/SandboxedIframe.js +129 -67
  5. package/dist/components/SandboxedIframe.js.map +1 -1
  6. package/dist/components/sandbox/RuntimeMessageRouter.d.ts +12 -2
  7. package/dist/components/sandbox/RuntimeMessageRouter.d.ts.map +1 -1
  8. package/dist/components/sandbox/RuntimeMessageRouter.js +26 -2
  9. package/dist/components/sandbox/RuntimeMessageRouter.js.map +1 -1
  10. package/package.json +18 -8
  11. package/example/README.md +0 -61
  12. package/example/index.html +0 -13
  13. package/example/package.json +0 -25
  14. package/example/src/app.css +0 -1
  15. package/example/src/css-modules.d.ts +0 -3
  16. package/example/src/custom-messages.ts +0 -99
  17. package/example/src/main.ts +0 -421
  18. package/example/tsconfig.json +0 -23
  19. package/example/vite.config.ts +0 -6
  20. package/scripts/count-prompt-tokens.ts +0 -88
  21. package/src/ChatPanel.ts +0 -209
  22. package/src/app.css +0 -68
  23. package/src/components/AgentInterface.ts +0 -403
  24. package/src/components/AttachmentTile.ts +0 -107
  25. package/src/components/ConsoleBlock.ts +0 -72
  26. package/src/components/CustomProviderCard.ts +0 -100
  27. package/src/components/ExpandableSection.ts +0 -46
  28. package/src/components/Input.ts +0 -113
  29. package/src/components/MessageEditor.ts +0 -405
  30. package/src/components/MessageList.ts +0 -98
  31. package/src/components/Messages.ts +0 -383
  32. package/src/components/ProviderKeyInput.ts +0 -153
  33. package/src/components/SandboxedIframe.ts +0 -627
  34. package/src/components/StreamingMessageContainer.ts +0 -103
  35. package/src/components/ThinkingBlock.ts +0 -43
  36. package/src/components/message-renderer-registry.ts +0 -28
  37. package/src/components/sandbox/ArtifactsRuntimeProvider.ts +0 -226
  38. package/src/components/sandbox/AttachmentsRuntimeProvider.ts +0 -68
  39. package/src/components/sandbox/ConsoleRuntimeProvider.ts +0 -188
  40. package/src/components/sandbox/FileDownloadRuntimeProvider.ts +0 -114
  41. package/src/components/sandbox/RuntimeMessageBridge.ts +0 -82
  42. package/src/components/sandbox/RuntimeMessageRouter.ts +0 -215
  43. package/src/components/sandbox/SandboxRuntimeProvider.ts +0 -54
  44. package/src/components/sandbox/runtime-messages.ts +0 -146
  45. package/src/dialogs/ApiKeyPromptDialog.ts +0 -75
  46. package/src/dialogs/AttachmentOverlay.ts +0 -637
  47. package/src/dialogs/CustomProviderDialog.ts +0 -274
  48. package/src/dialogs/ModelSelector.ts +0 -370
  49. package/src/dialogs/PersistentStorageDialog.ts +0 -144
  50. package/src/dialogs/ProvidersModelsTab.ts +0 -212
  51. package/src/dialogs/SessionListDialog.ts +0 -150
  52. package/src/dialogs/SettingsDialog.ts +0 -218
  53. package/src/index.ts +0 -120
  54. package/src/prompts/prompts.ts +0 -282
  55. package/src/storage/app-storage.ts +0 -60
  56. package/src/storage/backends/indexeddb-storage-backend.ts +0 -193
  57. package/src/storage/store.ts +0 -33
  58. package/src/storage/stores/custom-providers-store.ts +0 -62
  59. package/src/storage/stores/provider-keys-store.ts +0 -33
  60. package/src/storage/stores/sessions-store.ts +0 -136
  61. package/src/storage/stores/settings-store.ts +0 -34
  62. package/src/storage/types.ts +0 -206
  63. package/src/tools/artifacts/ArtifactElement.ts +0 -14
  64. package/src/tools/artifacts/ArtifactPill.ts +0 -26
  65. package/src/tools/artifacts/Console.ts +0 -93
  66. package/src/tools/artifacts/DocxArtifact.ts +0 -213
  67. package/src/tools/artifacts/ExcelArtifact.ts +0 -231
  68. package/src/tools/artifacts/GenericArtifact.ts +0 -117
  69. package/src/tools/artifacts/HtmlArtifact.ts +0 -195
  70. package/src/tools/artifacts/ImageArtifact.ts +0 -116
  71. package/src/tools/artifacts/MarkdownArtifact.ts +0 -82
  72. package/src/tools/artifacts/PdfArtifact.ts +0 -202
  73. package/src/tools/artifacts/SvgArtifact.ts +0 -119
  74. package/src/tools/artifacts/TextArtifact.ts +0 -148
  75. package/src/tools/artifacts/artifacts-tool-renderer.ts +0 -310
  76. package/src/tools/artifacts/artifacts.ts +0 -713
  77. package/src/tools/artifacts/index.ts +0 -7
  78. package/src/tools/extract-document.ts +0 -275
  79. package/src/tools/index.ts +0 -46
  80. package/src/tools/javascript-repl.ts +0 -293
  81. package/src/tools/renderer-registry.ts +0 -130
  82. package/src/tools/renderers/BashRenderer.ts +0 -52
  83. package/src/tools/renderers/CalculateRenderer.ts +0 -58
  84. package/src/tools/renderers/DefaultRenderer.ts +0 -103
  85. package/src/tools/renderers/GetCurrentTimeRenderer.ts +0 -92
  86. package/src/tools/types.ts +0 -15
  87. package/src/utils/attachment-utils.ts +0 -479
  88. package/src/utils/auth-token.ts +0 -22
  89. package/src/utils/format.ts +0 -42
  90. package/src/utils/i18n.ts +0 -653
  91. package/src/utils/model-discovery.ts +0 -290
  92. package/src/utils/proxy-utils.ts +0 -139
  93. package/src/utils/test-sessions.ts +0 -2357
  94. package/tsconfig.build.json +0 -20
  95. package/tsconfig.json +0 -7
@@ -1,195 +0,0 @@
1
- import hljs from "highlight.js";
2
- import { html } from "lit";
3
- import { customElement, property, state } from "lit/decorators.js";
4
- import { createRef, type Ref, ref } from "lit/directives/ref.js";
5
- import { unsafeHTML } from "lit/directives/unsafe-html.js";
6
- import { RefreshCw } from "lucide";
7
- import type { SandboxIframe } from "../../components/SandboxedIframe.js";
8
- import { type MessageConsumer, RUNTIME_MESSAGE_ROUTER } from "../../components/sandbox/RuntimeMessageRouter.js";
9
- import type { SandboxRuntimeProvider } from "../../components/sandbox/SandboxRuntimeProvider.js";
10
- import { i18n } from "../../utils/i18n.js";
11
- import "../../components/SandboxedIframe.js";
12
- import { ArtifactElement } from "./ArtifactElement.js";
13
- import type { Console } from "./Console.js";
14
- import "./Console.js";
15
- import { icon } from "@elyracode/mini-lit";
16
- import { Button } from "@elyracode/mini-lit/dist/Button.js";
17
- import { CopyButton } from "@elyracode/mini-lit/dist/CopyButton.js";
18
- import { DownloadButton } from "@elyracode/mini-lit/dist/DownloadButton.js";
19
- import { PreviewCodeToggle } from "@elyracode/mini-lit/dist/PreviewCodeToggle.js";
20
-
21
- @customElement("html-artifact")
22
- export class HtmlArtifact extends ArtifactElement {
23
- @property() override filename = "";
24
- @property({ attribute: false }) runtimeProviders: SandboxRuntimeProvider[] = [];
25
- @property({ attribute: false }) sandboxUrlProvider?: () => string;
26
-
27
- private _content = "";
28
- private logs: Array<{ type: "log" | "error"; text: string }> = [];
29
-
30
- // Refs for DOM elements
31
- public sandboxIframeRef: Ref<SandboxIframe> = createRef();
32
- private consoleRef: Ref<Console> = createRef();
33
-
34
- @state() private viewMode: "preview" | "code" = "preview";
35
-
36
- private setViewMode(mode: "preview" | "code") {
37
- this.viewMode = mode;
38
- }
39
-
40
- public getHeaderButtons() {
41
- const toggle = new PreviewCodeToggle();
42
- toggle.mode = this.viewMode;
43
- toggle.addEventListener("mode-change", (e: Event) => {
44
- this.setViewMode((e as CustomEvent).detail);
45
- });
46
-
47
- const copyButton = new CopyButton();
48
- copyButton.text = this._content;
49
- copyButton.title = i18n("Copy HTML");
50
- copyButton.showText = false;
51
-
52
- // Generate standalone HTML with all runtime code injected for download
53
- const sandbox = this.sandboxIframeRef.value;
54
- const sandboxId = `artifact-${this.filename}`;
55
- const downloadContent =
56
- sandbox?.prepareHtmlDocument(sandboxId, this._content, this.runtimeProviders || [], {
57
- isHtmlArtifact: true,
58
- isStandalone: true, // Skip runtime bridge and navigation interceptor for standalone downloads
59
- }) || this._content;
60
-
61
- return html`
62
- <div class="flex items-center gap-2">
63
- ${toggle}
64
- ${Button({
65
- variant: "ghost",
66
- size: "sm",
67
- onClick: () => {
68
- this.logs = [];
69
- this.executeContent(this._content);
70
- },
71
- title: i18n("Reload HTML"),
72
- children: icon(RefreshCw, "sm"),
73
- })}
74
- ${copyButton}
75
- ${DownloadButton({ content: downloadContent, filename: this.filename, mimeType: "text/html", title: i18n("Download HTML") })}
76
- </div>
77
- `;
78
- }
79
-
80
- override set content(value: string) {
81
- const oldValue = this._content;
82
- this._content = value;
83
- if (oldValue !== value) {
84
- // Reset logs when content changes
85
- this.logs = [];
86
- this.requestUpdate();
87
- // Execute content in sandbox if it exists
88
- if (this.sandboxIframeRef.value && value) {
89
- this.executeContent(value);
90
- }
91
- }
92
- }
93
-
94
- public executeContent(html: string) {
95
- const sandbox = this.sandboxIframeRef.value;
96
- if (!sandbox) return;
97
-
98
- // Configure sandbox URL provider if provided (for browser extensions)
99
- if (this.sandboxUrlProvider) {
100
- sandbox.sandboxUrlProvider = this.sandboxUrlProvider;
101
- }
102
-
103
- const sandboxId = `artifact-${this.filename}`;
104
-
105
- // Create consumer for console messages
106
- const consumer: MessageConsumer = {
107
- handleMessage: async (message): Promise<void> => {
108
- if (message.type === "console") {
109
- // Create new array reference for Lit reactivity
110
- this.logs = [
111
- ...this.logs,
112
- {
113
- type: message.method === "error" ? "error" : "log",
114
- text: message.text,
115
- },
116
- ];
117
- this.requestUpdate(); // Re-render to show console
118
- }
119
- },
120
- };
121
-
122
- // Inject window.complete() call at the end of the HTML to signal when page is loaded
123
- // HTML artifacts don't time out - they call complete() when ready
124
- let modifiedHtml = html;
125
- if (modifiedHtml.includes("</html>")) {
126
- modifiedHtml = modifiedHtml.replace(
127
- "</html>",
128
- "<script>if (window.complete) window.complete();</script></html>",
129
- );
130
- } else {
131
- // If no closing </html> tag, append the script
132
- modifiedHtml += "<script>if (window.complete) window.complete();</script>";
133
- }
134
-
135
- // Load content - this handles sandbox registration, consumer registration, and iframe creation
136
- sandbox.loadContent(sandboxId, modifiedHtml, this.runtimeProviders, [consumer]);
137
- }
138
-
139
- override get content(): string {
140
- return this._content;
141
- }
142
-
143
- override disconnectedCallback() {
144
- super.disconnectedCallback();
145
- // Unregister sandbox when element is removed from DOM
146
- const sandboxId = `artifact-${this.filename}`;
147
- RUNTIME_MESSAGE_ROUTER.unregisterSandbox(sandboxId);
148
- }
149
-
150
- override firstUpdated() {
151
- // Execute initial content
152
- if (this._content && this.sandboxIframeRef.value) {
153
- this.executeContent(this._content);
154
- }
155
- }
156
-
157
- override updated(changedProperties: Map<string | number | symbol, unknown>) {
158
- super.updated(changedProperties);
159
- // If we have content but haven't executed yet (e.g., during reconstruction),
160
- // execute when the iframe ref becomes available
161
- if (this._content && this.sandboxIframeRef.value && this.logs.length === 0) {
162
- this.executeContent(this._content);
163
- }
164
- }
165
-
166
- public getLogs(): string {
167
- if (this.logs.length === 0) return i18n("No logs for {filename}").replace("{filename}", this.filename);
168
- return this.logs.map((l) => `[${l.type}] ${l.text}`).join("\n");
169
- }
170
-
171
- override render() {
172
- return html`
173
- <div class="h-full flex flex-col">
174
- <div class="flex-1 overflow-hidden relative">
175
- <!-- Preview container - always in DOM, just hidden when not active -->
176
- <div class="absolute inset-0 flex flex-col" style="display: ${this.viewMode === "preview" ? "flex" : "none"}">
177
- <sandbox-iframe class="flex-1" ${ref(this.sandboxIframeRef)}></sandbox-iframe>
178
- ${
179
- this.logs.length > 0
180
- ? html`<artifact-console .logs=${this.logs} ${ref(this.consoleRef)}></artifact-console>`
181
- : ""
182
- }
183
- </div>
184
-
185
- <!-- Code view - always in DOM, just hidden when not active -->
186
- <div class="absolute inset-0 overflow-auto bg-background" style="display: ${this.viewMode === "code" ? "block" : "none"}">
187
- <pre class="m-0 p-4 text-xs"><code class="hljs language-html">${unsafeHTML(
188
- hljs.highlight(this._content, { language: "html" }).value,
189
- )}</code></pre>
190
- </div>
191
- </div>
192
- </div>
193
- `;
194
- }
195
- }
@@ -1,116 +0,0 @@
1
- import { DownloadButton } from "@elyracode/mini-lit/dist/DownloadButton.js";
2
- import { html, type TemplateResult } from "lit";
3
- import { customElement, property } from "lit/decorators.js";
4
- import { i18n } from "../../utils/i18n.js";
5
- import { ArtifactElement } from "./ArtifactElement.js";
6
-
7
- @customElement("image-artifact")
8
- export class ImageArtifact extends ArtifactElement {
9
- @property({ type: String }) private _content = "";
10
-
11
- get content(): string {
12
- return this._content;
13
- }
14
-
15
- set content(value: string) {
16
- this._content = value;
17
- this.requestUpdate();
18
- }
19
-
20
- protected override createRenderRoot(): HTMLElement | DocumentFragment {
21
- return this;
22
- }
23
-
24
- override connectedCallback(): void {
25
- super.connectedCallback();
26
- this.style.display = "block";
27
- this.style.height = "100%";
28
- }
29
-
30
- private getMimeType(): string {
31
- const ext = this.filename.split(".").pop()?.toLowerCase();
32
- if (ext === "jpg" || ext === "jpeg") return "image/jpeg";
33
- if (ext === "gif") return "image/gif";
34
- if (ext === "webp") return "image/webp";
35
- if (ext === "svg") return "image/svg+xml";
36
- if (ext === "bmp") return "image/bmp";
37
- if (ext === "ico") return "image/x-icon";
38
- return "image/png";
39
- }
40
-
41
- private getImageUrl(): string {
42
- // If content is already a data URL, use it directly
43
- if (this._content.startsWith("data:")) {
44
- return this._content;
45
- }
46
- // Otherwise assume it's base64 and construct data URL
47
- return `data:${this.getMimeType()};base64,${this._content}`;
48
- }
49
-
50
- private decodeBase64(): Uint8Array {
51
- let base64Data: string;
52
-
53
- // If content is a data URL, extract the base64 part
54
- if (this._content.startsWith("data:")) {
55
- const base64Match = this._content.match(/base64,(.+)/);
56
- if (base64Match) {
57
- base64Data = base64Match[1];
58
- } else {
59
- // Not a base64 data URL, return empty
60
- return new Uint8Array(0);
61
- }
62
- } else {
63
- // Otherwise use content as-is
64
- base64Data = this._content;
65
- }
66
-
67
- // Decode base64 to binary string
68
- const binaryString = atob(base64Data);
69
-
70
- // Convert binary string to Uint8Array
71
- const bytes = new Uint8Array(binaryString.length);
72
- for (let i = 0; i < binaryString.length; i++) {
73
- bytes[i] = binaryString.charCodeAt(i);
74
- }
75
-
76
- return bytes;
77
- }
78
-
79
- public getHeaderButtons() {
80
- return html`
81
- <div class="flex items-center gap-1">
82
- ${DownloadButton({
83
- content: this.decodeBase64(),
84
- filename: this.filename,
85
- mimeType: this.getMimeType(),
86
- title: i18n("Download"),
87
- })}
88
- </div>
89
- `;
90
- }
91
-
92
- override render(): TemplateResult {
93
- return html`
94
- <div class="h-full flex flex-col bg-background overflow-auto">
95
- <div class="flex-1 flex items-center justify-center p-4">
96
- <img
97
- src="${this.getImageUrl()}"
98
- alt="${this.filename}"
99
- class="max-w-full max-h-full object-contain"
100
- @error=${(e: Event) => {
101
- const target = e.target as HTMLImageElement;
102
- target.src =
103
- "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ctext x='50' y='50' text-anchor='middle' dominant-baseline='middle' fill='%23999'%3EImage Error%3C/text%3E%3C/svg%3E";
104
- }}
105
- />
106
- </div>
107
- </div>
108
- `;
109
- }
110
- }
111
-
112
- declare global {
113
- interface HTMLElementTagNameMap {
114
- "image-artifact": ImageArtifact;
115
- }
116
- }
@@ -1,82 +0,0 @@
1
- import hljs from "highlight.js";
2
- import { html } from "lit";
3
- import { customElement, property, state } from "lit/decorators.js";
4
- import { unsafeHTML } from "lit/directives/unsafe-html.js";
5
- import { i18n } from "../../utils/i18n.js";
6
- import "@elyracode/mini-lit/dist/MarkdownBlock.js";
7
- import { CopyButton } from "@elyracode/mini-lit/dist/CopyButton.js";
8
- import { DownloadButton } from "@elyracode/mini-lit/dist/DownloadButton.js";
9
- import { PreviewCodeToggle } from "@elyracode/mini-lit/dist/PreviewCodeToggle.js";
10
- import { ArtifactElement } from "./ArtifactElement.js";
11
-
12
- @customElement("markdown-artifact")
13
- export class MarkdownArtifact extends ArtifactElement {
14
- @property() override filename = "";
15
-
16
- private _content = "";
17
- override get content(): string {
18
- return this._content;
19
- }
20
- override set content(value: string) {
21
- this._content = value;
22
- this.requestUpdate();
23
- }
24
-
25
- @state() private viewMode: "preview" | "code" = "preview";
26
-
27
- protected override createRenderRoot(): HTMLElement | DocumentFragment {
28
- return this; // light DOM
29
- }
30
-
31
- private setViewMode(mode: "preview" | "code") {
32
- this.viewMode = mode;
33
- }
34
-
35
- public getHeaderButtons() {
36
- const toggle = new PreviewCodeToggle();
37
- toggle.mode = this.viewMode;
38
- toggle.addEventListener("mode-change", (e: Event) => {
39
- this.setViewMode((e as CustomEvent).detail);
40
- });
41
-
42
- const copyButton = new CopyButton();
43
- copyButton.text = this._content;
44
- copyButton.title = i18n("Copy Markdown");
45
- copyButton.showText = false;
46
-
47
- return html`
48
- <div class="flex items-center gap-2">
49
- ${toggle}
50
- ${copyButton}
51
- ${DownloadButton({
52
- content: this._content,
53
- filename: this.filename,
54
- mimeType: "text/markdown",
55
- title: i18n("Download Markdown"),
56
- })}
57
- </div>
58
- `;
59
- }
60
-
61
- override render() {
62
- return html`
63
- <div class="h-full flex flex-col">
64
- <div class="flex-1 overflow-auto">
65
- ${
66
- this.viewMode === "preview"
67
- ? html`<div class="p-4"><markdown-block .content=${this.content}></markdown-block></div>`
68
- : html`<pre class="m-0 p-4 text-xs whitespace-pre-wrap break-words"><code class="hljs language-markdown">${unsafeHTML(
69
- hljs.highlight(this.content, { language: "markdown", ignoreIllegals: true }).value,
70
- )}</code></pre>`
71
- }
72
- </div>
73
- </div>
74
- `;
75
- }
76
- }
77
-
78
- declare global {
79
- interface HTMLElementTagNameMap {
80
- "markdown-artifact": MarkdownArtifact;
81
- }
82
- }
@@ -1,202 +0,0 @@
1
- import { DownloadButton } from "@elyracode/mini-lit/dist/DownloadButton.js";
2
- import { html, type TemplateResult } from "lit";
3
- import { customElement, property, state } from "lit/decorators.js";
4
- import type { PDFDocumentLoadingTask, PDFDocumentProxy } from "pdfjs-dist";
5
- import * as pdfjsLib from "pdfjs-dist";
6
- import { i18n } from "../../utils/i18n.js";
7
- import { ArtifactElement } from "./ArtifactElement.js";
8
-
9
- // Configure PDF.js worker
10
- pdfjsLib.GlobalWorkerOptions.workerSrc = new URL("pdfjs-dist/build/pdf.worker.min.mjs", import.meta.url).toString();
11
-
12
- @customElement("pdf-artifact")
13
- export class PdfArtifact extends ArtifactElement {
14
- @property({ type: String }) private _content = "";
15
- @state() private error: string | null = null;
16
- private currentLoadingTask: PDFDocumentLoadingTask | null = null;
17
-
18
- get content(): string {
19
- return this._content;
20
- }
21
-
22
- set content(value: string) {
23
- this._content = value;
24
- this.error = null;
25
- this.requestUpdate();
26
- }
27
-
28
- protected override createRenderRoot(): HTMLElement | DocumentFragment {
29
- return this;
30
- }
31
-
32
- override connectedCallback(): void {
33
- super.connectedCallback();
34
- this.style.display = "block";
35
- this.style.height = "100%";
36
- }
37
-
38
- override disconnectedCallback(): void {
39
- super.disconnectedCallback();
40
- this.cleanup();
41
- }
42
-
43
- private cleanup() {
44
- if (this.currentLoadingTask) {
45
- this.currentLoadingTask.destroy();
46
- this.currentLoadingTask = null;
47
- }
48
- }
49
-
50
- private base64ToArrayBuffer(base64: string): ArrayBuffer {
51
- // Remove data URL prefix if present
52
- let base64Data = base64;
53
- if (base64.startsWith("data:")) {
54
- const base64Match = base64.match(/base64,(.+)/);
55
- if (base64Match) {
56
- base64Data = base64Match[1];
57
- }
58
- }
59
-
60
- const binaryString = atob(base64Data);
61
- const bytes = new Uint8Array(binaryString.length);
62
- for (let i = 0; i < binaryString.length; i++) {
63
- bytes[i] = binaryString.charCodeAt(i);
64
- }
65
- return bytes.buffer;
66
- }
67
-
68
- private decodeBase64(): Uint8Array {
69
- let base64Data = this._content;
70
- if (this._content.startsWith("data:")) {
71
- const base64Match = this._content.match(/base64,(.+)/);
72
- if (base64Match) {
73
- base64Data = base64Match[1];
74
- }
75
- }
76
-
77
- const binaryString = atob(base64Data);
78
- const bytes = new Uint8Array(binaryString.length);
79
- for (let i = 0; i < binaryString.length; i++) {
80
- bytes[i] = binaryString.charCodeAt(i);
81
- }
82
- return bytes;
83
- }
84
-
85
- public getHeaderButtons() {
86
- return html`
87
- <div class="flex items-center gap-1">
88
- ${DownloadButton({
89
- content: this.decodeBase64(),
90
- filename: this.filename,
91
- mimeType: "application/pdf",
92
- title: i18n("Download"),
93
- })}
94
- </div>
95
- `;
96
- }
97
-
98
- override async updated(changedProperties: Map<string, any>) {
99
- super.updated(changedProperties);
100
-
101
- if (changedProperties.has("_content") && this._content && !this.error) {
102
- await this.renderPdf();
103
- }
104
- }
105
-
106
- private async renderPdf() {
107
- const container = this.querySelector("#pdf-container");
108
- if (!container || !this._content) return;
109
-
110
- let pdf: PDFDocumentProxy | null = null;
111
-
112
- try {
113
- const arrayBuffer = this.base64ToArrayBuffer(this._content);
114
-
115
- // Cancel any existing loading task
116
- if (this.currentLoadingTask) {
117
- this.currentLoadingTask.destroy();
118
- }
119
-
120
- // Load the PDF
121
- this.currentLoadingTask = pdfjsLib.getDocument({ data: arrayBuffer });
122
- pdf = await this.currentLoadingTask.promise;
123
- this.currentLoadingTask = null;
124
-
125
- // Clear container
126
- container.innerHTML = "";
127
- const wrapper = document.createElement("div");
128
- wrapper.className = "p-4";
129
- container.appendChild(wrapper);
130
-
131
- // Render all pages
132
- for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
133
- const page = await pdf.getPage(pageNum);
134
-
135
- const pageContainer = document.createElement("div");
136
- pageContainer.className = "mb-4 last:mb-0";
137
-
138
- const canvas = document.createElement("canvas");
139
- const context = canvas.getContext("2d");
140
-
141
- const viewport = page.getViewport({ scale: 1.5 });
142
- canvas.height = viewport.height;
143
- canvas.width = viewport.width;
144
-
145
- canvas.className = "w-full max-w-full h-auto block mx-auto bg-white rounded shadow-sm border border-border";
146
-
147
- if (context) {
148
- context.fillStyle = "white";
149
- context.fillRect(0, 0, canvas.width, canvas.height);
150
- }
151
-
152
- await page.render({
153
- canvasContext: context!,
154
- viewport: viewport,
155
- canvas: canvas,
156
- }).promise;
157
-
158
- pageContainer.appendChild(canvas);
159
-
160
- if (pageNum < pdf.numPages) {
161
- const separator = document.createElement("div");
162
- separator.className = "h-px bg-border my-4";
163
- pageContainer.appendChild(separator);
164
- }
165
-
166
- wrapper.appendChild(pageContainer);
167
- }
168
- } catch (error) {
169
- console.error("Error rendering PDF:", error);
170
- this.error = error instanceof Error ? error.message : i18n("Failed to load PDF");
171
- } finally {
172
- if (pdf) {
173
- pdf.destroy();
174
- }
175
- }
176
- }
177
-
178
- override render(): TemplateResult {
179
- if (this.error) {
180
- return html`
181
- <div class="h-full flex items-center justify-center bg-background p-4">
182
- <div class="bg-destructive/10 border border-destructive text-destructive p-4 rounded-lg max-w-2xl">
183
- <div class="font-medium mb-1">${i18n("Error loading PDF")}</div>
184
- <div class="text-sm opacity-90">${this.error}</div>
185
- </div>
186
- </div>
187
- `;
188
- }
189
-
190
- return html`
191
- <div class="h-full flex flex-col bg-background overflow-auto">
192
- <div id="pdf-container" class="flex-1 overflow-auto"></div>
193
- </div>
194
- `;
195
- }
196
- }
197
-
198
- declare global {
199
- interface HTMLElementTagNameMap {
200
- "pdf-artifact": PdfArtifact;
201
- }
202
- }