From 1e988c7c6d927630059645ebee05261619aba7b4 Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Wed, 8 Jul 2020 23:13:11 -0700 Subject: added multiple links capability, linking bug fixes --- src/client/apis/hypothesis/HypothesisApiUtils.ts | 33 +++++++++++++++++++----- src/client/views/linking/LinkMenuItem.tsx | 5 ++-- src/client/views/nodes/DocumentLinksButton.tsx | 7 +++-- src/client/views/nodes/DocumentView.tsx | 17 ------------ 4 files changed, 34 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/client/apis/hypothesis/HypothesisApiUtils.ts b/src/client/apis/hypothesis/HypothesisApiUtils.ts index bf9f4ea99..fe35f5831 100644 --- a/src/client/apis/hypothesis/HypothesisApiUtils.ts +++ b/src/client/apis/hypothesis/HypothesisApiUtils.ts @@ -1,7 +1,20 @@ import { StrCast } from "../../../fields/Types"; export namespace Hypothesis { - export const getAnnotation = async (username: String, searchKeyWord: String) => { + export const fetchAnnotation = async (annotationId: string) => { + const response = await fetch(`https://api.hypothes.is/api/annotations/${annotationId}`); + if (response.ok) { + return response.json(); + } else { + throw new Error('DASH: Error in fetchAnnotation GET request'); + } + }; + + /** + * Searches for annotations made by @param username that + * contain @param searchKeyWord + */ + export const searchAnnotation = async (username: String, searchKeyWord: String) => { const base = 'https://api.hypothes.is/api/search'; const request = base + `?user=acct:${username}@hypothes.is&text=${searchKeyWord}`; console.log("DASH Querying " + request); @@ -9,21 +22,29 @@ export namespace Hypothesis { if (response.ok) { return response.json(); } else { - throw new Error('DASH: Error in GET request'); + throw new Error('DASH: Error in searchAnnotation GET request'); } }; + // Find the most recent placeholder annotation created, and return its ID export const getPlaceholderId = async (username: String, searchKeyWord: String) => { - const getResponse = await Hypothesis.getAnnotation(username, searchKeyWord); + const getResponse = await Hypothesis.searchAnnotation(username, searchKeyWord); const id = getResponse.rows.length > 0 ? getResponse.rows[0].id : undefined; return StrCast(id); }; // Send request to Hypothes.is client to modify a placeholder annotation into a hyperlink to Dash - export const dispatchLinkRequest = (title: string, url: string, annotationId: string) => { + export const dispatchLinkRequest = async (title: string, url: string, annotationId: string) => { + const apiKey = "6879-GHmtDG_P2kmWNKM3hcHptEUZX3VMOUePkamCaOrJbSw"; + + const oldAnnotation = await fetchAnnotation(annotationId); + const oldText = StrCast(oldAnnotation.text); + const newHyperlink = `[${title}\n](${url})`; + const newText = oldText === "placeholder" ? newHyperlink : oldText + '\n\n' + newHyperlink; + console.log("DASH dispatching linkRequest"); - document.dispatchEvent(new CustomEvent<{ url: string, title: string, id: string }>("linkRequest", { - detail: { url: url, title: title, id: annotationId }, + document.dispatchEvent(new CustomEvent<{ newText: string, id: string, apiKey: string }>("linkRequest", { + detail: { newText: newText, id: annotationId, apiKey: apiKey }, bubbles: true })); }; diff --git a/src/client/views/linking/LinkMenuItem.tsx b/src/client/views/linking/LinkMenuItem.tsx index 9c21bca35..ad3c12122 100644 --- a/src/client/views/linking/LinkMenuItem.tsx +++ b/src/client/views/linking/LinkMenuItem.tsx @@ -152,8 +152,7 @@ export class LinkMenuItem extends React.Component { DocumentLinksButton.EditLink = undefined; LinkDocPreview.LinkInfo = undefined; - const redirectUrl = StrCast(this.props.linkDoc.annotationUrl, null); - redirectUrl && (Doc.GetProto(this.props.destinationDoc).data = new WebField(redirectUrl)); // if destination is a Hypothes.is annotation, redirect website to the annotation's URL to scroll to the annotation + 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.follow) { if (this.props.linkDoc.follow === "Default") { @@ -196,7 +195,7 @@ export class LinkMenuItem extends React.Component {

- {StrCast(this.props.destinationDoc.title)}

+ {(this.props.linkDoc.linksToAnnotation ? "Annotation in " : "") + StrCast(this.props.destinationDoc.title)}

{this.props.linkDoc.description !== "" ?

{StrCast(this.props.linkDoc.description)}

: null}
diff --git a/src/client/views/nodes/DocumentLinksButton.tsx b/src/client/views/nodes/DocumentLinksButton.tsx index 839f83f78..32c344304 100644 --- a/src/client/views/nodes/DocumentLinksButton.tsx +++ b/src/client/views/nodes/DocumentLinksButton.tsx @@ -18,6 +18,7 @@ import { StrCast } from "../../../fields/Types"; import { LinkDescriptionPopup } from "./LinkDescriptionPopup"; import { LinkManager } from "../../util/LinkManager"; import { Hypothesis } from "../../apis/hypothesis/HypothesisApiUtils"; +import { Id } from "../../../fields/FieldSymbols"; const higflyout = require("@hig/flyout"); export const { anchorPoints } = higflyout; export const Flyout = higflyout.default; @@ -128,8 +129,9 @@ export class DocumentLinksButton extends React.Component(Docu const cm = ContextMenu.Instance; if (!cm) return; - cm.addItem({ - description: "make hypothesis link", event: async () => { - const docUrl = Utils.prepend("/doc/" + this.props.Document[Id]); - const docTitle = StrCast(this.layoutDoc.title); - const getResponse = await Hypothesis.getAnnotation("melissaz", "placeholder"); - if (getResponse && getResponse.rows.length > 0) { - const annotationId = getResponse.rows[0].id; - document.dispatchEvent(new CustomEvent<{ url: string, title: string, id: string }>("linkRequest", { - detail: { url: docUrl, title: docTitle, id: annotationId }, - bubbles: true - })); - } else { - console.log("no placeholder annotation found"); - } - }, icon: "eye" - }); - cm.addItem({ description: "pretend we made an annotation", event: () => { document.dispatchEvent(new CustomEvent("fakeAnnotationCreated", { -- cgit v1.2.3-70-g09d2