-
Notifications
You must be signed in to change notification settings - Fork 0
Add export to word #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add export to word #146
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
259a9e7
Added custom docx generation
MatejFrnka c69a1c8
Added UI, updated download button
MatejFrnka 7e5941a
Changed font to Aptos
MatejFrnka 2ad681e
Fix uv run ruff check
MatejFrnka 6ad3106
Use same icon as Document section
MatejFrnka ea6dee3
Switched to calibri
MatejFrnka 8d3e617
Fix formatting
MatejFrnka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| .root { | ||
| position: relative; | ||
| } | ||
|
|
||
| .caret { | ||
| margin-left: 0.5rem; | ||
| font-size: 0.6rem; | ||
| vertical-align: middle; | ||
| } | ||
|
|
||
| .menu { | ||
| position: absolute; | ||
| top: calc(100% + 0.35rem); | ||
| right: 0; | ||
| z-index: 20; | ||
| min-width: 11rem; | ||
| display: grid; | ||
| overflow: hidden; | ||
| border: 1px solid var(--ai-doc-color-slate-200); | ||
| border-radius: var(--bs-border-radius); | ||
| background: var(--ai-doc-color-white); | ||
| box-shadow: | ||
| 0 10px 30px rgba(15, 23, 42, 0.08), | ||
| 0 2px 8px rgba(15, 23, 42, 0.05); | ||
| } | ||
|
|
||
| .item { | ||
| display: flex; | ||
| align-items: center; | ||
| gap: 0.6rem; | ||
| width: 100%; | ||
| padding: 0.6rem 0.9rem; | ||
| border: 0; | ||
| background: transparent; | ||
| color: var(--ai-doc-color-slate-900); | ||
| text-align: left; | ||
| line-height: 1.35; | ||
| transition: background-color 120ms ease; | ||
| } | ||
|
|
||
| .item:hover:not(:disabled) { | ||
| background: var(--ai-doc-color-blue-50); | ||
| } | ||
|
|
||
| .item:disabled { | ||
| opacity: 0.65; | ||
| cursor: not-allowed; | ||
| } | ||
|
|
||
| .itemIcon { | ||
| width: 1rem; | ||
| font-size: 0.85rem; | ||
| color: var(--ai-doc-color-slate-500); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { useEffect, useRef, useState } from 'react' | ||
|
MatejFrnka marked this conversation as resolved.
|
||
| import { toast } from 'sonner' | ||
|
|
||
| import { exportPipelineResultAsDocx } from '@/client' | ||
| import styles from '@/components/DownloadDropdown.module.css' | ||
|
|
||
| const triggerBlobDownload = (blob: Blob, fileName: string) => { | ||
| const url = URL.createObjectURL(blob) | ||
| const link = document.createElement('a') | ||
| link.href = url | ||
| link.download = fileName | ||
| document.body.appendChild(link) | ||
| link.click() | ||
| link.remove() | ||
| URL.revokeObjectURL(url) | ||
| } | ||
|
|
||
| type DownloadDropdownProps = { | ||
| markdown: string | ||
| runId: string | null | ||
| baseName: string | ||
| } | ||
|
|
||
| /** | ||
| * Download menu for a pipeline result, offering the raw markdown or a Word export. | ||
| * | ||
| * The markdown passed in is the editor's current text, so unsaved edits are downloaded too. | ||
| */ | ||
| export function DownloadDropdown({ markdown, runId, baseName }: DownloadDropdownProps) { | ||
| const [isOpen, setIsOpen] = useState(false) | ||
| const [isDownloadingDocx, setIsDownloadingDocx] = useState(false) | ||
| const rootRef = useRef<HTMLDivElement | null>(null) | ||
|
|
||
| const fileBaseName = baseName || 'pipeline-output' | ||
|
|
||
| useEffect(() => { | ||
| if (!isOpen) { | ||
| return | ||
| } | ||
|
|
||
| const handlePointerDown = (event: MouseEvent) => { | ||
| if (!rootRef.current?.contains(event.target as Node)) { | ||
| setIsOpen(false) | ||
| } | ||
| } | ||
|
|
||
| const handleEscape = (event: KeyboardEvent) => { | ||
| if (event.key === 'Escape') { | ||
| setIsOpen(false) | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener('mousedown', handlePointerDown) | ||
| document.addEventListener('keydown', handleEscape) | ||
|
|
||
| return () => { | ||
| document.removeEventListener('mousedown', handlePointerDown) | ||
| document.removeEventListener('keydown', handleEscape) | ||
| } | ||
| }, [isOpen]) | ||
|
|
||
| const onDownloadMarkdown = () => { | ||
| setIsOpen(false) | ||
|
|
||
| if (!markdown) { | ||
| return | ||
| } | ||
|
|
||
| const blob = new Blob([markdown], { type: 'text/markdown;charset=utf-8' }) | ||
| triggerBlobDownload(blob, `${fileBaseName}.md`) | ||
| toast.success('Markdown download has started.') | ||
| } | ||
|
|
||
| const onDownloadDocx = async () => { | ||
| setIsOpen(false) | ||
|
|
||
| if (!markdown || !runId) { | ||
| toast.error('There is no pipeline result to export yet.') | ||
| return | ||
| } | ||
|
|
||
| setIsDownloadingDocx(true) | ||
| try { | ||
| const blob = await exportPipelineResultAsDocx(runId, markdown) | ||
| triggerBlobDownload(blob, `${fileBaseName}.docx`) | ||
| toast.success('Word download has started.') | ||
| } catch (error) { | ||
| toast.error( | ||
| error instanceof Error | ||
| ? error.message | ||
| : 'Failed to export the result as a Word document.', | ||
| ) | ||
| } finally { | ||
| setIsDownloadingDocx(false) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div className={styles.root} ref={rootRef}> | ||
| <button | ||
| type="button" | ||
| onClick={() => setIsOpen((currentValue) => !currentValue)} | ||
| disabled={isDownloadingDocx} | ||
| className="btn btn-outline-secondary" | ||
| aria-expanded={isOpen} | ||
| aria-haspopup="menu" | ||
| > | ||
| {isDownloadingDocx ? 'Preparing...' : 'Download'} | ||
| <span className={styles.caret} aria-hidden="true"> | ||
| ▼ | ||
| </span> | ||
| </button> | ||
|
|
||
| {isOpen ? ( | ||
| <div className={styles.menu} role="menu"> | ||
| <button | ||
| type="button" | ||
| role="menuitem" | ||
| onClick={onDownloadMarkdown} | ||
| className={styles.item} | ||
| > | ||
| <i className={`fa far fa-file-alt ${styles.itemIcon}`} aria-hidden="true" /> | ||
| Markdown | ||
| </button> | ||
|
|
||
| <button | ||
| type="button" | ||
| role="menuitem" | ||
| onClick={() => void onDownloadDocx()} | ||
| disabled={!runId} | ||
| className={styles.item} | ||
| > | ||
| <i | ||
| className={`fa far fa-file-word ${styles.itemIcon}`} | ||
| aria-hidden="true" | ||
| /> | ||
| MS Word | ||
| </button> | ||
| </div> | ||
| ) : null} | ||
| </div> | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.