generated from yuminn-k/yuminnk-nextjs-template
-
Notifications
You must be signed in to change notification settings - Fork 4
PDFビュアー機能の実装 #44
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
PDFビュアー機能の実装 #44
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19f36a9
🔨 chore : Add react-pdf library
d74d5f6
✨feat: Add PDF viewer feature in prompt page
faf3676
Merge branch 'main' into feat/class-prompt-page
686b11f
💄 style: Modify className in page.tsx
a9a264f
💄style : fix global layout errors
042c6df
🔨 chore: Add comments in codes
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
webpack: config => { | ||
config.resolve.alias.canvas = false; | ||
|
||
return config; | ||
}, | ||
}; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React, {FC, ReactNode} from 'react'; | ||
|
||
interface LayoutProps { | ||
children: ReactNode; | ||
} | ||
|
||
const Layout: FC<LayoutProps> = ({children}: LayoutProps) => ( | ||
<div className="flex flex-col items-center space-y-4">{children}</div> | ||
); | ||
|
||
export default Layout; | ||
Regulus0811 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,117 @@ | ||
'use client'; | ||
|
||
import React, {useState, useRef, useEffect} from 'react'; | ||
import {Document, Page, pdfjs} from 'react-pdf'; | ||
import 'react-pdf/dist/Page/TextLayer.css'; | ||
import 'react-pdf/dist/Page/AnnotationLayer.css'; | ||
|
||
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`; | ||
|
||
const MyPdfViewer = () => { | ||
const [file, setFile] = useState<File | null>(null); | ||
const [numPages, setNumPages] = useState<number | null>(null); | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const [pageNumber, setPageNumber] = useState<number>(1); | ||
const [inputValue, setInputValue] = useState<string>('1'); | ||
const pageRefs = useRef<(React.RefObject<HTMLDivElement> | null)[]>([]); | ||
|
||
// PDFファイルが読み込まれたときの処理 | ||
function onDocumentLoadSuccess({numPages}: {numPages: number}) { | ||
setNumPages(numPages); | ||
pageRefs.current = Array(numPages) | ||
.fill(null) | ||
.map(() => React.createRef()); | ||
} | ||
|
||
// ファイルを選択したときの処理 | ||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target.files?.[0]; | ||
if (file) { | ||
const reader = new FileReader(); | ||
reader.onload = () => { | ||
setFile(file); | ||
}; | ||
reader.readAsDataURL(file); | ||
} | ||
}; | ||
|
||
// ページが表示されたときの処理 | ||
const observePage = (pageIndex: number) => { | ||
const observer = new IntersectionObserver( | ||
entries => { | ||
if (entries[0].isIntersecting) { | ||
setPageNumber(pageIndex + 1); | ||
setInputValue((pageIndex + 1).toString()); | ||
} | ||
}, | ||
{threshold: 0.5} | ||
); | ||
|
||
// ページが表示されたときにページ番号を更新する | ||
const pageElement = pageRefs.current[pageIndex]?.current; | ||
if (pageElement) { | ||
observer.observe(pageElement); | ||
} | ||
return () => { | ||
if (pageElement) { | ||
observer.unobserve(pageElement); | ||
} | ||
}; | ||
}; | ||
|
||
// ページ番号を指定してページに移動する | ||
const goToPage = (page: number) => { | ||
const pageElement = pageRefs.current[page - 1]?.current; | ||
if (pageElement) { | ||
pageElement.scrollIntoView(); | ||
} | ||
}; | ||
|
||
// ページ数が変わったときにページの表示を監視する | ||
useEffect(() => { | ||
const cleanupFunctions = Array(numPages || 0) | ||
.fill(null) | ||
.map((_, index) => observePage(index)); | ||
return () => { | ||
cleanupFunctions.forEach(cleanup => cleanup()); | ||
}; | ||
}, [numPages]); | ||
|
||
return ( | ||
<div> | ||
<input type="file" onChange={handleChange} /> | ||
{file && ( | ||
<div> | ||
<div> | ||
<label>Page: </label> | ||
<input | ||
type="text" | ||
value={inputValue} | ||
onChange={e => setInputValue(e.target.value)} | ||
onKeyDown={e => { | ||
if (e.key === 'Enter') { | ||
goToPage(Number(inputValue)); | ||
} | ||
}} | ||
className="border rounded p-1 w-10 text-center" | ||
/> | ||
/ {numPages} | ||
</div> | ||
<div className="h-[80vh] overflow-auto"> | ||
<Document file={file as File} onLoadSuccess={onDocumentLoadSuccess}> | ||
{Array.from(new Array(numPages || 0), (el, index) => ( | ||
<Page | ||
key={`page_${index + 1}`} | ||
pageNumber={index + 1} | ||
inputRef={pageRefs.current[index]} | ||
/> | ||
))} | ||
</Document> | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default MyPdfViewer; | ||
Regulus0811 marked this conversation as resolved.
Show resolved
Hide resolved
Regulus0811 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.