Skip to content
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

fix: Pass proper file id in all scenarios #29

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/Controller/WhiteboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public function show(int $fileId): DataResponse {
$userFolder = $this->rootFolder->getUserFolder($user?->getUID());
$file = $userFolder->getById($fileId)[0];

$data = json_decode($file->getContent(), true, 512, JSON_THROW_ON_ERROR);
$fileContent = $file->getContent();
if ($fileContent === '') {
$fileContent = '{"elements":[],"scrollToContent":true}';
}
$data = json_decode($fileContent, true, 512, JSON_THROW_ON_ERROR);

return new DataResponse([
'data' => $data,
Expand Down
16 changes: 9 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ const COMMENT_ICON_DIMENSION = 32
const COMMENT_INPUT_HEIGHT = 50
const COMMENT_INPUT_WIDTH = 150

/**
*
*/
export default function App() {
interface WhiteboardAppProps {
fileId: number;
isEmbedded: boolean;
}

export default function App({ fileId, isEmbedded }: WhiteboardAppProps) {
const darkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
const appRef = useRef<any>(null)
const [viewModeEnabled, setViewModeEnabled] = useState(false)
const [zenModeEnabled, setZenModeEnabled] = useState(false)
const [viewModeEnabled, setViewModeEnabled] = useState(isEmbedded)
const [zenModeEnabled, setZenModeEnabled] = useState(isEmbedded)
const [gridModeEnabled, setGridModeEnabled] = useState(false)
const [blobUrl, setBlobUrl] = useState<string>('')
const [canvasUrl, setCanvasUrl] = useState<string>('')
Expand All @@ -84,7 +86,7 @@ export default function App() {
] = useState<ExcalidrawImperativeAPI | null>(null)
const [collab, setCollab] = useState<Collab | null>(null)

if (excalidrawAPI && !collab) setCollab(new Collab(excalidrawAPI))
if (excalidrawAPI && !collab) setCollab(new Collab(excalidrawAPI, fileId))
if (collab && !collab.portal.socket) collab.startCollab()

useHandleLibrary({excalidrawAPI})
Expand Down
11 changes: 2 additions & 9 deletions src/collaboration/collab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@ export class Collab {
lastBroadcastedOrReceivedSceneVersion: number = -1
private collaborators = new Map<string, Collaborator>()

constructor(excalidrawAPI: ExcalidrawImperativeAPI) {
constructor(excalidrawAPI: ExcalidrawImperativeAPI, fileId: number) {
this.excalidrawAPI = excalidrawAPI
const url = window.location.href
const fileIdMatch = url.match(/\/files\/(\d+)\?/)

if (fileIdMatch) {
this.portal = new Portal(fileIdMatch[1], '1', this)
} else {
throw new Error('No FileId found in URL')
}
this.portal = new Portal(String(fileId), '1', this)
}

startCollab() {
Expand Down
12 changes: 9 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ const Component = {
name: 'Whiteboard',
render(createElement: (arg0: string, arg1: { attrs: { id: string } }, arg2: string) => any) {
this.$emit('update:loaded', true)
const randomId = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10);
this.$nextTick(() => {
const rootElement = document.getElementById('whiteboard')
const rootElement = document.getElementById('whiteboard-' + randomId)
const root = createRoot(rootElement)

root.render(
<StrictMode>
<App />
<App fileId={this.fileid} isEmbedded={this.isEmbedded}/>
</StrictMode>,
)
})
return createElement('div', {
attrs: {
id: 'whiteboard',
id: 'whiteboard-' + randomId,
},
class: ['whiteboard', { 'whiteboard-viewer__embedding': this.isEmbedded }],
}, 'Hello whiteboard')
},
props: {
Expand All @@ -54,6 +56,10 @@ const Component = {
type: Number,
default: null,
},
isEmbedded: {
type: Boolean,
default: false,
},
},
data() {
return {}
Expand Down
Empty file added src/register.ts
Empty file.
7 changes: 6 additions & 1 deletion src/viewer.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#whiteboard {
.whiteboard {
width: 100%;
height: calc(100% + 50px);
position: absolute;
top: 0;
}

.whiteboard-viewer__embedding {
min-height: min(200px, 50vh);
position: relative;
}