-
Notifications
You must be signed in to change notification settings - Fork 25
WIP: stub of Living Papers preview pane in TEE #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
Open
geoffreylitt
wants to merge
1
commit into
main
Choose a base branch
from
living-papers-preview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,79 @@ | ||
import { TinyEssayEditor } from "@/tee/components/TinyEssayEditor"; | ||
import { AutomergeUrl } from "@automerge/automerge-repo"; | ||
import { | ||
useDocument, | ||
useHandle, | ||
useRepo, | ||
} from "@automerge/automerge-repo-react-hooks"; | ||
import { LivingPapersDoc } from "../datatype"; | ||
import { PDFViewer } from "./PDFViewer"; | ||
import { useCallback, useEffect } from "react"; | ||
import { debounce } from "lodash"; | ||
import { Build } from "@/tee/lp-shared"; | ||
|
||
export const LivingPapersEditor = ({ docUrl }: { docUrl: AutomergeUrl }) => { | ||
const [doc, changeDoc] = useDocument<LivingPapersDoc>(docUrl); | ||
const handle = useHandle<LivingPapersDoc>(docUrl); | ||
const repo = useRepo(); | ||
const fetchUrl = useCallback( | ||
debounce(async () => { | ||
console.log("rebuild"); | ||
// Assuming there's a function to fetch data from a URL | ||
console.log(`Fetching data for docUrl: ${docUrl}`); | ||
|
||
const buildResult = await fetch(`http://localhost:8088/build/${docUrl}`); | ||
const amUrl = (await buildResult.text()) as AutomergeUrl; | ||
const buildDoc = (await repo.find(amUrl).doc()) as any; // Assuming BuildsDoc type is known | ||
|
||
const build = Object.entries(buildDoc.builds)[0][1] as Build; | ||
const result = build.result; | ||
if (result.ok === false) { | ||
console.error("Build failed", result.error); | ||
return; | ||
} | ||
const buildDirUrl = result.value.buildDirUrl; | ||
const buildDirDoc = (await repo.find(buildDirUrl).doc()) as any; | ||
|
||
console.log(buildDirDoc); | ||
|
||
const pdf = buildDirDoc["index.pdf"].contents; | ||
|
||
console.log("pdf", pdf); | ||
|
||
handle.change((d) => (d.pdfOutput = pdf)); | ||
}, 1000), | ||
[docUrl, repo] | ||
); | ||
|
||
useEffect(() => { | ||
if (doc?.content) { | ||
fetchUrl(); | ||
} | ||
|
||
// Cleanup function to cancel the debounce if the component unmounts or the doc changes | ||
return () => { | ||
fetchUrl.cancel(); | ||
}; | ||
}, [fetchUrl, doc.content]); | ||
|
||
return ( | ||
<div className="flex flex-col h-full"> | ||
<div className="bg-gray-100 p-2">Settings go here</div> | ||
<div className="flex-grow flex"> | ||
<div className="w-1/2 h-full border-r border-gray-200"> | ||
<div className="bg-gray-50 py-2 px-8 text-gray-500 font-bold text-xs"> | ||
Source | ||
</div> | ||
<TinyEssayEditor docUrl={docUrl} /> | ||
</div> | ||
<div className="w-1/2 h-full bg-gray-50"> | ||
<div className="bg-gray-50 py-2 px-8 text-gray-500 font-bold text-xs"> | ||
Preview | ||
</div> | ||
{doc.pdfOutput && <PDFViewer data={doc.pdfOutput} />} | ||
{!doc.pdfOutput && <div>No PDF output yet</div>} | ||
</div> | ||
</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useCallback, useMemo, useState } from "react"; | ||
import { useResizeObserver } from "@wojtekmaj/react-hooks"; | ||
import { pdfjs, Document, Page } from "react-pdf"; | ||
import "react-pdf/dist/esm/Page/AnnotationLayer.css"; | ||
import "react-pdf/dist/esm/Page/TextLayer.css"; | ||
|
||
// TODO: loading worker from global CDN because Vite import wasn't working, | ||
// fix this. | ||
|
||
// pdfjs.GlobalWorkerOptions.workerSrc = new URL( | ||
// "pdfjs-dist/build/pdf.worker.min.js", | ||
// import.meta.url | ||
// ).toString(); | ||
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clemens' universal-automerge-cache strategy could come in handy here. |
||
|
||
const options = { | ||
cMapUrl: "/cmaps/", | ||
standardFontDataUrl: "/standard_fonts/", | ||
}; | ||
|
||
const resizeObserverOptions = {}; | ||
|
||
const maxWidth = 800; | ||
|
||
export const PDFViewer = ({ data }: { data: Uint8Array }) => { | ||
const [numPages, setNumPages] = useState<number>(); | ||
const [containerRef, setContainerRef] = useState<HTMLElement | null>(null); | ||
const [containerWidth, setContainerWidth] = useState<number>(); | ||
|
||
const inputToViewer = useMemo(() => ({ data: data.slice(0) }), [data]); | ||
|
||
const onResize = useCallback<ResizeObserverCallback>((entries) => { | ||
const [entry] = entries; | ||
|
||
if (entry) { | ||
setContainerWidth(entry.contentRect.width); | ||
} | ||
}, []); | ||
|
||
useResizeObserver(containerRef, resizeObserverOptions, onResize); | ||
|
||
// todo: get TS to understand the expected type for this callback | ||
function onDocumentLoadSuccess(pdfDocumentProxy: any): void { | ||
setNumPages(pdfDocumentProxy.numPages); | ||
} | ||
|
||
return ( | ||
<div className="w-full max-w-[calc(100%-2em)] my-4" ref={setContainerRef}> | ||
<Document | ||
file={inputToViewer} | ||
onLoadSuccess={onDocumentLoadSuccess} | ||
options={options} | ||
className="flex flex-col items-center" | ||
> | ||
{Array.from(new Array(numPages), (el, index) => ( | ||
<Page | ||
key={`page_${index + 1}`} | ||
pageNumber={index + 1} | ||
width={ | ||
containerWidth ? Math.min(containerWidth, maxWidth) : maxWidth | ||
} | ||
className="border border-gray-200" | ||
/> | ||
))} | ||
</Document> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { MarkdownDoc } from "@/tee/schema"; | ||
import * as MarkdownDatatype from "@/tee/datatype"; | ||
|
||
import { BookIcon } from "lucide-react"; | ||
|
||
// Until Patchwork has better multi-doc versioning, we have to keep the | ||
// TEE Markdown content directly in this doc for basic usability. | ||
export type LivingPapersDoc = MarkdownDoc & { | ||
pdfOutput: Uint8Array; | ||
}; | ||
|
||
// When a copy of the document has been made, | ||
// update the title so it's more clear which one is the copy vs original. | ||
// (this mechanism needs to be thought out more...) | ||
export const markCopy = (doc: any) => { | ||
MarkdownDatatype.markCopy(doc); | ||
}; | ||
|
||
const getTitle = (doc: any) => { | ||
return MarkdownDatatype.getTitle(doc); | ||
}; | ||
|
||
export const init = (doc: any) => { | ||
const initContent = `--- | ||
title: Untitled Living Paper | ||
author: | ||
- name: The Living Papers Team | ||
org: University of Washington | ||
keywords: [all, about, my, article] | ||
output: | ||
latex: true | ||
---`; | ||
|
||
doc.content = initContent; | ||
}; | ||
|
||
export const LivingPapersDatatype = { | ||
id: "living-papers", | ||
name: "Living Paper", | ||
icon: BookIcon, | ||
init, | ||
getTitle, | ||
markCopy, // TODO: this shouldn't be here | ||
}; |
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,46 @@ | ||
import { isValidAutomergeUrl, Repo } from "@automerge/automerge-repo"; | ||
import { BroadcastChannelNetworkAdapter } from "@automerge/automerge-repo-network-broadcastchannel"; | ||
import { BrowserWebSocketClientAdapter } from "@automerge/automerge-repo-network-websocket"; | ||
|
||
import { IndexedDBStorageAdapter } from "@automerge/automerge-repo-storage-indexeddb"; | ||
import { next as Automerge } from "@automerge/automerge"; | ||
|
||
import { mount } from "./mount.js"; | ||
import "./index.css"; | ||
import { LivingPapersDoc } from "./datatype.js"; | ||
|
||
const SYNC_SERVER_URL = | ||
import.meta.env?.VITE_SYNC_SERVER_URL ?? "wss://sync.automerge.org"; | ||
|
||
const repo = new Repo({ | ||
network: [ | ||
new BroadcastChannelNetworkAdapter(), | ||
new BrowserWebSocketClientAdapter(SYNC_SERVER_URL), | ||
], | ||
storage: new IndexedDBStorageAdapter(), | ||
}); | ||
|
||
const rootDocUrl = `${document.location.hash.slice(1)}`; | ||
let handle; | ||
if (isValidAutomergeUrl(rootDocUrl)) { | ||
handle = repo.find(rootDocUrl); | ||
} else { | ||
handle = repo.create<LivingPapersDoc>(); | ||
const { init } = await import("./datatype.js"); | ||
handle.change(init); | ||
} | ||
|
||
// eslint-disable-next-line | ||
const docUrl = (document.location.hash = handle.url); | ||
|
||
// @ts-expect-error - adding property to window | ||
window.Automerge = Automerge; | ||
// @ts-expect-error - adding property to window | ||
window.repo = repo; | ||
// @ts-expect-error - adding property to window | ||
window.handle = handle; // we'll use this later for experimentation | ||
|
||
// @ts-expect-error - adding property to window | ||
window.logoImageUrl = "/assets/logo-favicon-310x310-transparent.png"; | ||
|
||
mount(document.getElementById("root"), { docUrl }); |
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,23 @@ | ||
import React from "react"; | ||
import ReactDom from "react-dom/client"; | ||
import { RepoContext } from "@automerge/automerge-repo-react-hooks"; | ||
import { LivingPapersEditor } from "./components/LivingPapersEditor"; | ||
|
||
export function mount(node, params) { | ||
// workaround different conventions for documentUrl | ||
if (!params.docUrl && params.documentUrl) { | ||
params.docUrl = params.documentUrl; | ||
} | ||
|
||
ReactDom.createRoot(node).render( | ||
// We get the Automerge Repo from the global window; | ||
// this is set by either our standalone entrypoint or trailrunner | ||
React.createElement( | ||
RepoContext.Provider, | ||
// eslint-disable-next-line no-undef | ||
// @ts-expect-error - repo is on window | ||
{ value: repo }, | ||
React.createElement(LivingPapersEditor, Object.assign({}, params)) | ||
) | ||
); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔