-
Notifications
You must be signed in to change notification settings - Fork 3
feat: file viewer #87
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
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c1f2124
feat: file viewer
kemuru c1d83a4
feat: feedback
kemuru e2990a4
feat(file-viewer): block unsafe URL schemes, add pdf worker override
kemuru 0fc501c
fix(file-viewer): decode data: URIs per RFC 2397
kemuru 888e50e
feat(file-viewer): translucent pdf toolbar with per-theme tint
kemuru 8e451d4
feat(attachment-display): wire image checker color into file viewer s…
kemuru d17f8a9
style(file-viewer): trim image preview vertical padding
kemuru e9eb308
chore: delete vertical padding on png viewer altogether
kemuru 1f54562
feat: feedback
kemuru 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,3 +25,6 @@ yarn-debug.log* | |
| yarn-error.log* | ||
|
|
||
| *storybook.log | ||
|
|
||
| # yarn pack output (local-test artifact) | ||
| *.tgz | ||
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,76 @@ | ||
| import React, { useMemo } from "react"; | ||
| import DocViewer, { | ||
| DocViewerRenderers, | ||
| type IConfig, | ||
| } from "@cyntler/react-doc-viewer"; | ||
|
|
||
| import { cn } from "../../utils"; | ||
| import MarkdownDocRenderer from "./markdown-viewer"; | ||
| import "@cyntler/react-doc-viewer/dist/index.css"; | ||
| import "./styles.css"; | ||
|
|
||
| interface FileViewerProps { | ||
| /** URL of the file to display. Supports URLs, blob URLs, data URIs, and IPFS URIs. */ | ||
| url: string; | ||
| /** Optional file name override (used for download). Useful for IPFS URIs that lack one. */ | ||
| fileName?: string; | ||
| /** Override the DocViewer config. Merged shallowly over the defaults. */ | ||
| config?: IConfig; | ||
| /** Class applied to the outer wrapper. */ | ||
| className?: string; | ||
| } | ||
|
|
||
| const defaultConfig: IConfig = { | ||
| header: { | ||
| disableHeader: true, | ||
| disableFileName: true, | ||
| }, | ||
| pdfZoom: { | ||
| defaultZoom: 0.8, | ||
| zoomJump: 0.1, | ||
| }, | ||
| pdfVerticalScrollByDefault: true, | ||
| }; | ||
|
|
||
| /** | ||
| * In-app viewer for policies, evidence attachments, and arbitrary file-URI | ||
|
kemuru marked this conversation as resolved.
Outdated
|
||
| * links. Supports PDFs, images, markdown, plaintext, and common document | ||
| * formats via `@cyntler/react-doc-viewer`. | ||
|
kemuru marked this conversation as resolved.
Outdated
|
||
| */ | ||
| function FileViewer({ | ||
| url, | ||
| fileName, | ||
| config, | ||
| className, | ||
| }: Readonly<FileViewerProps>) { | ||
| const docs = useMemo(() => [{ uri: url, fileName }], [url, fileName]); | ||
|
|
||
| const pluginRenderers = useMemo( | ||
| () => [...DocViewerRenderers, MarkdownDocRenderer], | ||
| [], | ||
| ); | ||
|
|
||
| const mergedConfig = useMemo<IConfig>( | ||
| () => ({ ...defaultConfig, ...config }), | ||
| [config], | ||
| ); | ||
|
|
||
| return ( | ||
| <div | ||
| className={cn( | ||
| "bg-klerosUIComponentsWhiteBackground shadow-default", | ||
| "max-h-[80vh] overflow-auto rounded-[3px]", | ||
|
kemuru marked this conversation as resolved.
Outdated
|
||
| className, | ||
| )} | ||
| > | ||
| <DocViewer | ||
| documents={docs} | ||
| pluginRenderers={pluginRenderers} | ||
| config={mergedConfig} | ||
| className="kleros-file-viewer-doc" | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default FileViewer; | ||
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,29 @@ | ||
| import React from "react"; | ||
| import { type DocRenderer } from "@cyntler/react-doc-viewer"; | ||
| import ReactMarkdown from "react-markdown"; | ||
|
|
||
| const MarkdownDocRenderer: DocRenderer = ({ | ||
| mainState: { currentDocument }, | ||
| }) => { | ||
| if (!currentDocument) return null; | ||
|
|
||
| const fileData = currentDocument.fileData as string; | ||
| const base64String = fileData.includes(",") | ||
| ? fileData.split(",")[1] | ||
| : fileData; | ||
| const decodedData = atob(base64String); | ||
|
|
||
|
kemuru marked this conversation as resolved.
Outdated
|
||
| return ( | ||
| <div | ||
| id="md-renderer" | ||
| className="text-klerosUIComponentsPrimaryText [&_code]:text-klerosUIComponentsSecondaryText p-4 [&_a]:text-base" | ||
| > | ||
| <ReactMarkdown>{decodedData}</ReactMarkdown> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| MarkdownDocRenderer.fileTypes = ["md", "text/plain"]; | ||
| MarkdownDocRenderer.weight = 1; | ||
|
|
||
| export default MarkdownDocRenderer; | ||
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,11 @@ | ||
| .kleros-file-viewer-doc { | ||
|
kemuru marked this conversation as resolved.
Outdated
|
||
| background-color: var(--klerosUIComponentsWhiteBackground) !important; | ||
| } | ||
|
|
||
| .kleros-file-viewer-doc #pdf-controls { | ||
| z-index: 3; | ||
| } | ||
|
|
||
| .kleros-file-viewer-doc [class*="--loading"] { | ||
| color: var(--klerosUIComponentsSecondaryText); | ||
| } | ||
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,35 @@ | ||
| import type { Meta, StoryObj } from "@storybook/react"; | ||
|
|
||
| import { IPreviewArgs } from "./utils"; | ||
|
|
||
| import FileViewerComponent from "../lib/file-viewer"; | ||
|
|
||
| const meta = { | ||
| component: FileViewerComponent, | ||
| title: "File Viewer", | ||
| tags: ["autodocs"], | ||
| } satisfies Meta<typeof FileViewerComponent>; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof meta> & IPreviewArgs; | ||
|
|
||
| export const FileViewer: Story = { | ||
| args: { | ||
| themeUI: "light", | ||
| backgroundUI: "light", | ||
| className: "w-[800px]", | ||
| url: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", | ||
| }, | ||
| }; | ||
|
|
||
| export const Image: Story = { | ||
| args: { | ||
| themeUI: "light", | ||
| backgroundUI: "light", | ||
| className: "w-[800px]", | ||
| url: | ||
| "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/" + | ||
| "PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png", | ||
| }, | ||
| }; |
Oops, something went wrong.
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.