pdf-js-viewer.stories.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import PdfJsViewer from '../js/features/pdf-preview/components/pdf-js-viewer'
  2. import useFetchMock from './hooks/use-fetch-mock'
  3. import examplePdf from './fixtures/storybook-example.pdf'
  4. import { Button } from 'react-bootstrap'
  5. import { useCallback } from 'react'
  6. import { withContextRoot } from './utils/with-context-root'
  7. import { setupContext } from './fixtures/context'
  8. import useScopeValue from '../js/shared/hooks/use-scope-value'
  9. setupContext()
  10. export default {
  11. title: 'PDF Viewer',
  12. component: PdfJsViewer,
  13. }
  14. const project = {
  15. _id: 'story-project',
  16. }
  17. const mockHighlights = [
  18. {
  19. page: 1,
  20. h: 85.03936,
  21. v: 509.999878,
  22. width: 441.921265,
  23. height: 8.855677,
  24. },
  25. {
  26. page: 1,
  27. h: 85.03936,
  28. v: 486.089539,
  29. width: 441.921265,
  30. height: 8.855677,
  31. },
  32. {
  33. page: 1,
  34. h: 85.03936,
  35. v: 498.044708,
  36. width: 441.921265,
  37. height: 8.855677,
  38. },
  39. {
  40. page: 1,
  41. h: 85.03936,
  42. v: 521.955078,
  43. width: 441.921265,
  44. height: 8.855677,
  45. },
  46. ]
  47. export const Interactive = () => {
  48. useFetchMock(fetchMock => {
  49. fetchMock.get(
  50. 'express:/build/output.pdf',
  51. (url, options, request) => {
  52. return new Promise(resolve => {
  53. const xhr = new XMLHttpRequest()
  54. xhr.addEventListener('load', () => {
  55. resolve({
  56. status: 200,
  57. headers: {
  58. 'Content-Length': xhr.getResponseHeader('Content-Length'),
  59. 'Content-Type': xhr.getResponseHeader('Content-Type'),
  60. 'Accept-Ranges': 'bytes',
  61. },
  62. body: xhr.response,
  63. })
  64. })
  65. xhr.open('GET', examplePdf)
  66. xhr.responseType = 'arraybuffer'
  67. xhr.send()
  68. })
  69. },
  70. { sendAsJson: false }
  71. )
  72. })
  73. const Inner = () => {
  74. const [, setHighlights] = useScopeValue('pdf.highlights')
  75. const dispatchSyncFromCode = useCallback(() => {
  76. setHighlights([])
  77. window.setTimeout(() => {
  78. setHighlights(mockHighlights)
  79. }, 0)
  80. }, [setHighlights])
  81. return (
  82. <div
  83. style={{
  84. zIndex: 10,
  85. position: 'absolute',
  86. top: 20,
  87. right: 40,
  88. }}
  89. >
  90. <div style={{ display: 'flex', gap: 20 }}>
  91. <Button onClick={dispatchSyncFromCode}>
  92. sync position from editor
  93. </Button>
  94. </div>
  95. </div>
  96. )
  97. }
  98. return withContextRoot(
  99. <div className="pdf-viewer">
  100. <PdfJsViewer url="/build/output.pdf" />
  101. <Inner />
  102. </div>,
  103. { project }
  104. )
  105. }