Skip to content

Commit

Permalink
feat: Add link-based sharing for collaboration
Browse files Browse the repository at this point in the history
  • Loading branch information
code0xff committed Jul 17, 2024
1 parent a3fd7b4 commit 56ee83c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
31 changes: 12 additions & 19 deletions src/lib/editor/editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,14 @@
let _workspace: string;
onMount(async () => {
const searchParams = new URLSearchParams(window.location.search);
if (searchParams.has('endpoint') && searchParams.has('workspace')) {
const endpoint = searchParams.get('endpoint');
const workspace = searchParams.get('workspace');
localStorage.setItem('sharing', JSON.stringify({ endpoint, workspace }));
localStorage.setItem('shared', JSON.stringify({ endpoint, workspace }));
}
const sharing = localStorage.getItem('sharing');
const searchParams = new URLSearchParams(location.search);
let extensions: Extensions;
if (sharing) {
if (searchParams.has('endpoint') && searchParams.has('workspace')) {
try {
const { endpoint, workspace } = JSON.parse(sharing);
const endpoint = searchParams.get('endpoint');
const workspace = searchParams.get('workspace');
if (!endpoint) {
throw new Error('Invalid endpoint', { cause: 'InvalidMetadata' });
}
Expand All @@ -97,23 +89,24 @@
},
onClose() {
window.alert(`Failed to connect with ${endpoint}/${workspace}`);
localStorage.removeItem('sharing');
location.reload();
location.replace(`${location.protocol}//${location.host}${location.pathname}`);
},
connect: false
});
await provider.connect();
localStorage.setItem('shared', JSON.stringify({ endpoint, workspace }));
extensions = getExtensionsOnSharing(provider, bubbleMenu);
} catch (e: any) {
if (e instanceof Error && e.cause === 'InvalidMetadata') {
window.alert(`Failed to start sharing with ${sharing}: ${e.toString()}`);
window.alert(`Failed to start sharing with ${location.search}: ${e.toString()}`);
} else {
window.alert(`Failed to start sharing with ${sharing}`);
window.alert(`Failed to start sharing with ${location.search}`);
}
console.error(e);
localStorage.removeItem('sharing');
location.reload();
location.replace(`${location.protocol}//${location.host}${location.pathname}`);
}
} else {
try {
Expand Down
1 change: 0 additions & 1 deletion src/lib/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export function startSharing(endpoint: string, workspace: string) {
}

export function endSharing(provider: HocuspocusProvider) {
localStorage.removeItem('sharing');
if (provider) {
window.alert('Disconnecting...');
location.replace(`${location.protocol}//${location.host}${location.pathname}`);
Expand Down

0 comments on commit 56ee83c

Please sign in to comment.