Skip to content

Commit

Permalink
#8 add route to receive PatchSets via link
Browse files Browse the repository at this point in the history
  • Loading branch information
Max.-F.Helm committed Apr 9, 2024
1 parent 63b2b57 commit 61423d2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
26 changes: 23 additions & 3 deletions src/FileProcessorWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type {ErrorCallback, Proposal} from "@/processing/file-processor";
import {FileProcessor} from "@/processing/file-processor";
import type {Proposal} from "@/processing/file-processor";
import type {ErrorCallback} from "@/processing/file-processor";
import type {Identity} from "@/processing/identity-processor";
import {IllegalStateException} from "@/processing/exceptions";
import type Author from "@/processing/model/Author";
import type BufferReader from "@/processing/buffer-reader";
import BufferReader from "@/processing/buffer-reader";
import type {Buffer} from "buffer";
import {ref} from "vue";

Expand All @@ -18,6 +17,7 @@ export default class FileProcessorWrapper {
private identity: Identity | null = null;
private readonly listeners: Listener[] = [];
private readonly errListeners: ErrorCallback[] = [];
private readonly toBeLoadedPatches: Buffer[] = [];

/**
* the name of the Proposal with which it is stored in BrowserStorage
Expand Down Expand Up @@ -45,6 +45,18 @@ export default class FileProcessorWrapper {
this.errListeners.slice(idx, 1);
}

addToBeLoadedPatch(data: Buffer) {
this.toBeLoadedPatches.push(data);
}

getToBeLoadedPatches(): Buffer[] {
return this.toBeLoadedPatches;
}

clearToBeLoadedPatches() {
this.toBeLoadedPatches.splice(0);
}

setIdentity(identity: Identity) {
this.identity = identity;
}
Expand Down Expand Up @@ -85,6 +97,8 @@ export default class FileProcessorWrapper {
throw new IllegalStateException("not initialized");

await this.fileProcessor.loadFile(data);
await this.importPendingPatches();

this.onUpdated();
}

Expand Down Expand Up @@ -140,6 +154,12 @@ export default class FileProcessorWrapper {
return this.fileProcessor.getChangesCount();
}

private async importPendingPatches() {
for(const patch of this.toBeLoadedPatches) {
await this.fileProcessor!.importPatchSet(new BufferReader(patch));
}
}

private onErr(msg: string) {
this.errListeners.forEach(l => l(msg));
}
Expand Down
8 changes: 7 additions & 1 deletion src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createRouter, createWebHistory } from 'vue-router';
import {createRouter, createWebHistory} from 'vue-router';
import Document from '@/ui/Document.vue';
import PatchFromLink from "@/ui/PatchFromLink.vue";

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
Expand All @@ -9,6 +10,11 @@ const router = createRouter({
name: 'document',
component: Document
},
{
path: '/importPatchset',
name: 'importPatchset',
component: PatchFromLink
},
]
});

Expand Down
57 changes: 57 additions & 0 deletions src/ui/PatchFromLink.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<template>

</template>

<script setup lang="ts">
import {onBeforeMount} from "vue";
import {useToast} from "primevue/usetoast";
import libsodium from "libsodium-wrappers-sumo";
import {Buffer} from "buffer";
import FileProcessorWrapper from "@/FileProcessorWrapper";
import {useRouter} from "vue-router";
const toast = useToast();
const router = useRouter();
onBeforeMount(() => {
let dataStr = location.hash;
if(dataStr === "" || dataStr === "#") {
toast.add({
severity: "error",
summary: "No data was given to be imported as a Patchset",
life: 5000
});
navigateToDocumentView();
return;
}
try {
dataStr = dataStr.substring(1);// remove '#'
const data = new Buffer(libsodium.from_base64(dataStr, libsodium.base64_variants.URLSAFE));
FileProcessorWrapper.INSTANCE.addToBeLoadedPatch(data);
toast.add({
severity: "success",
summary: "The Patchset will be imported as soon as you opened the document",
life: 3000
});
navigateToDocumentView();
} catch(e) {
console.error("error while reading PatchSet", e);
toast.add({
severity: "error",
summary: "Unable to import Patchset",
life: 5000
});
navigateToDocumentView();
}
});
function navigateToDocumentView() {
router.push({ name: "document" });
}
</script>

<style scoped lang="scss">
</style>
9 changes: 9 additions & 0 deletions src/ui/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function basePath(): string {
const baseUrlStr = import.meta.env.BASE_URL;
const baseUrl = new URL(baseUrlStr, location.href);
const path = baseUrl.pathname;

if(path.endsWith("/"))
return path.substring(0, path.length - 1);
return path;
}

0 comments on commit 61423d2

Please sign in to comment.