From 7157310f1b4d7a9f409c46f35c10c8db4a66b0af Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Tue, 28 Jul 2020 19:00:02 -0700 Subject: scroll to annotations within dash, handle annotation editing in client --- src/client/apis/hypothesis/HypothesisApiUtils.ts | 22 +++++++++++----------- src/client/views/MainView.tsx | 19 +++---------------- src/client/views/linking/LinkMenuItem.tsx | 4 ++-- 3 files changed, 16 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/client/apis/hypothesis/HypothesisApiUtils.ts b/src/client/apis/hypothesis/HypothesisApiUtils.ts index 1158dcc11..e2fb91af9 100644 --- a/src/client/apis/hypothesis/HypothesisApiUtils.ts +++ b/src/client/apis/hypothesis/HypothesisApiUtils.ts @@ -5,6 +5,7 @@ import { action } from "mobx"; import { Doc } from "../../../fields/Doc"; import { DocumentType } from "../../documents/DocumentTypes"; import { WebField } from "../../../fields/URLField"; +import { DocumentManager } from "../../util/DocumentManager"; export namespace Hypothesis { @@ -49,9 +50,9 @@ export namespace Hypothesis { }; export const editAnnotation = async (annotationId: string, newText: string) => { - console.log("DASH dispatching editRequest"); + console.log("DASH dispatching editAnnotation"); const credentials = await getCredentials(); - document.dispatchEvent(new CustomEvent<{ newText: string, id: string, apiKey: string }>("editRequest", { + document.dispatchEvent(new CustomEvent<{ newText: string, id: string, apiKey: string }>("editAnnotation", { detail: { newText: newText, id: annotationId, apiKey: credentials.apiKey }, bubbles: true })); @@ -76,14 +77,6 @@ export namespace Hypothesis { editAnnotation(annotationId, out); }; - // Finds the most recent placeholder annotation created and returns its ID - export const getPlaceholderId = async (searchKeyWord: string) => { - const getResponse = await Hypothesis.searchAnnotation(searchKeyWord); - const id = getResponse.rows.length > 0 ? getResponse.rows[0].id : undefined; - const uri = getResponse.rows.length > 0 ? getResponse.rows[0].uri : undefined; - return id ? { id, uri } : undefined; - }; - // Construct an URL which will scroll the web page to a specific annotation's position export const makeAnnotationUrl = (annotationId: string, baseUrl: string) => { return `https://hyp.is/${annotationId}/${baseUrl}`; // embeds the generic version of Hypothes.is client, not the Dash version @@ -111,6 +104,13 @@ export namespace Hypothesis { })); // TODO: open & return new Web doc with given uri if no matching Web docs are found - return results.length ? results[0] : undefined; + return results.length ? DocumentManager.Instance.getFirstDocumentView(results[0]) : undefined; + }; + + export const scrollToAnnotation = (annotationId: string) => { + document.dispatchEvent(new CustomEvent("scrollToAnnotation", { + detail: annotationId, + bubbles: true + })); }; } \ No newline at end of file diff --git a/src/client/views/MainView.tsx b/src/client/views/MainView.tsx index 605b81506..64538d015 100644 --- a/src/client/views/MainView.tsx +++ b/src/client/views/MainView.tsx @@ -115,27 +115,14 @@ export class MainView extends React.Component { } }); }); - window.addEventListener("message", async (e: any) => { // listen for a new Hypothes.is annotation from an iframe inside Dash - // start link from new Hypothes.is annotation - // TODO: pass in placeholderId directly from client, move - if (e.origin === "http://localhost:1050" && e.data.message === "annotation created") { - console.log("DASH received message: annotation created"); - const response = await Hypothesis.getPlaceholderId("placeholder"); - const source = SelectionManager.SelectedDocuments()[0]; - response && runInAction(() => { - DocumentLinksButton.AnnotationId = response.id; - DocumentLinksButton.AnnotationUri = response.uri; - DocumentLinksButton.StartLink = source; - }); - } - }); - document.addEventListener("linkAnnotationToDash", async (e: any) => { // listen for event from Hypothes.is plugin to to link an existing annotation to Dash + document.addEventListener("linkAnnotationToDash", async (e: any) => { // listen for event from Hypothes.is plugin to link an annotation to Dash const annotationId = e.detail.id; const annotationUri = e.detail.uri; const sourceDoc = await Hypothesis.getSourceWebDoc(annotationUri); console.log("sourceDoc: ", sourceDoc ? sourceDoc.title : "not found"); - const source = SelectionManager.SelectedDocuments()[0]; // TO BE FIXED, currently link just starts from whichever doc is selected + // TO BE FIXED, currently cannot start links from new webpages that don't exist in Dash + const source = sourceDoc || SelectionManager.SelectedDocuments()[0]; runInAction(() => { DocumentLinksButton.AnnotationId = annotationId; DocumentLinksButton.AnnotationUri = annotationUri; diff --git a/src/client/views/linking/LinkMenuItem.tsx b/src/client/views/linking/LinkMenuItem.tsx index cae90cd0c..8f7a2481d 100644 --- a/src/client/views/linking/LinkMenuItem.tsx +++ b/src/client/views/linking/LinkMenuItem.tsx @@ -154,14 +154,14 @@ export class LinkMenuItem extends React.Component { DocumentLinksButton.EditLink = undefined; LinkDocPreview.LinkInfo = undefined; - // this.props.linkDoc.linksToAnnotation && (Doc.GetProto(this.props.destinationDoc).data = new WebField(StrCast(this.props.linkDoc.annotationUrl))); // if destination is a Hypothes.is annotation, redirect website to the annotation's URL to scroll to the annotation if (this.props.linkDoc.followLinkLocation && this.props.linkDoc.followLinkLocation !== "Default") { this.props.addDocTab(this.props.destinationDoc, StrCast(this.props.linkDoc.followLinkLocation)); } else { DocumentManager.Instance.FollowLink(this.props.linkDoc, this.props.sourceDoc, doc => this.props.addDocTab(doc, "onRight"), false); } - this.props.linkDoc.linksToAnnotation && setTimeout(() => window.open(StrCast(this.props.linkDoc.annotationUrl), '_blank'), 1000); // open external page if hypothes.is annotation + // this.props.linkDoc.linksToAnnotation && setTimeout(() => window.open(StrCast(this.props.linkDoc.annotationUrl), '_blank'), 1000); // open external page if hypothes.is annotation + this.props.linkDoc.linksToAnnotation && Hypothesis.scrollToAnnotation(StrCast(this.props.linkDoc.annotationId)); } @undoBatch -- cgit v1.2.3-70-g09d2