From 90d5abaf3c32b53f34f6bd89e854c57c250937b0 Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Wed, 15 Jul 2020 22:59:21 -0700 Subject: set up listening for event from extension to link an existing annotation --- src/client/views/nodes/DocumentLinksButton.tsx | 56 +++++++++++++++++--------- 1 file changed, 36 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/client/views/nodes/DocumentLinksButton.tsx b/src/client/views/nodes/DocumentLinksButton.tsx index 6d439e379..d753edbeb 100644 --- a/src/client/views/nodes/DocumentLinksButton.tsx +++ b/src/client/views/nodes/DocumentLinksButton.tsx @@ -38,18 +38,32 @@ export class DocumentLinksButton extends React.Component { - if (e.origin === "http://localhost:1050" && e.data.message === "annotation created") { - console.log("DASH received message: annotation created"); - const response = await Hypothesis.getPlaceholderId("placeholder"); // delete once eventListening between client & Dash works - const source = SelectionManager.SelectedDocuments()[0]; - response && runInAction(() => { - DocumentLinksButton.AnnotationId = response.id; - DocumentLinksButton.AnnotationUri = response.uri; - DocumentLinksButton.StartLink = source; - }); - } - }); + window.addEventListener("message", this.newAnnotation); // listen for a new Hypothes.is annotation from an iframe inside Dash + document.addEventListener("linkAnnotationToDash", this.linkAnnotation); // listen for event from Hypothes.is extension to link an existing annotation + } + + // start link from new Hypothes.is annotation + // TODO: pass in placeholderId directly from client + @action + newAnnotation = async (e: any) => { + if (e.origin === "http://localhost:1050" && e.data.message === "annotation created") { + console.log("DASH received message: annotation created"); + window.removeEventListener("message", this.newAnnotation); + const response = await Hypothesis.getPlaceholderId("placeholder"); + const source = SelectionManager.SelectedDocuments()[0]; + response && runInAction(() => { + DocumentLinksButton.AnnotationId = response.id; + DocumentLinksButton.AnnotationUri = response.uri; + DocumentLinksButton.StartLink = source; + }); + } + } + + @action + linkAnnotation = async (e: any) => { // event used by hypothes.is plugin to tell Dash which annotation to link from + const annotationId = e.detail.id; + const sourceUrl = e.detail.uri; + console.log(annotationId, sourceUrl); } @action @@ -119,14 +133,14 @@ export class DocumentLinksButton extends React.Component { @@ -151,6 +165,8 @@ export class DocumentLinksButton extends React.Component) => { // Doc.UnBrushDoc(this.props.View.Document); // }); -- cgit v1.2.3-70-g09d2