diff options
author | Melissa Zhang <mzhang19096@gmail.com> | 2020-07-08 21:11:35 -0700 |
---|---|---|
committer | Melissa Zhang <mzhang19096@gmail.com> | 2020-07-08 21:11:35 -0700 |
commit | 467ddce06a4cf9e3b61abf733f20c60b257c94db (patch) | |
tree | 6a719948e8cb7b930f7a40a9f52547f9f7dafbec /src | |
parent | 876437db1f7347787badbdd48ac751dba111a752 (diff) |
implemented much more link functionality, redirects to annotation URL when link followed
Diffstat (limited to 'src')
-rw-r--r-- | src/client/apis/hypothesis/HypothesisApiUtils.ts | 28 | ||||
-rw-r--r-- | src/client/documents/Documents.ts | 19 | ||||
-rw-r--r-- | src/client/views/linking/LinkMenuItem.tsx | 3 | ||||
-rw-r--r-- | src/client/views/nodes/DocumentLinksButton.tsx | 61 | ||||
-rw-r--r-- | src/client/views/nodes/DocumentView.tsx | 2 | ||||
-rw-r--r-- | src/server/database.ts | 2 |
6 files changed, 72 insertions, 43 deletions
diff --git a/src/client/apis/hypothesis/HypothesisApiUtils.ts b/src/client/apis/hypothesis/HypothesisApiUtils.ts index dc7e1f988..bf9f4ea99 100644 --- a/src/client/apis/hypothesis/HypothesisApiUtils.ts +++ b/src/client/apis/hypothesis/HypothesisApiUtils.ts @@ -1,7 +1,9 @@ +import { StrCast } from "../../../fields/Types"; + export namespace Hypothesis { - export const getAnnotation = async (username: String, searchParam: String) => { + export const getAnnotation = async (username: String, searchKeyWord: String) => { const base = 'https://api.hypothes.is/api/search'; - const request = base + `?user=acct:${username}@hypothes.is&text=${searchParam}`; + const request = base + `?user=acct:${username}@hypothes.is&text=${searchKeyWord}`; console.log("DASH Querying " + request); const response = await fetch(request); if (response.ok) { @@ -11,7 +13,29 @@ export namespace Hypothesis { } }; + export const getPlaceholderId = async (username: String, searchKeyWord: String) => { + const getResponse = await Hypothesis.getAnnotation(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) => { + console.log("DASH dispatching linkRequest"); + document.dispatchEvent(new CustomEvent<{ url: string, title: string, id: string }>("linkRequest", { + detail: { url: url, title: title, id: annotationId }, + bubbles: true + })); + }; + + // 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}`; }; + + // export const checkValidApiKey = async (apiKey: string) => { + // const response = await fetch("https://api.hypothes.is/api/profile", { + + // }); + // }; }
\ No newline at end of file diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index 763321a85..7b85d2e46 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -925,25 +925,6 @@ export namespace DocUtils { return linkDoc; } - export function MakeHypothesisLink(source: { doc: Doc }, target: { doc: Doc }, linkRelationship: string = "", sourceAnnotationId: string, id?: string) { - const sv = DocumentManager.Instance.getDocumentView(source.doc); - if (sv && sv.props.ContainingCollectionDoc === target.doc) return; - if (target.doc === Doc.UserDoc()) return undefined; - - const linkDoc = Docs.Create.LinkDocument(source, target, { linkRelationship, layoutKey: "layout_linkView" }, id); - linkDoc.layout_linkView = Cast(Cast(Doc.UserDoc()["template-button-link"], Doc, null).dragFactory, Doc, null); - Doc.GetProto(linkDoc).title = ComputedField.MakeFunction('self.anchor1?.title +" (" + (self.linkRelationship||"to") +") " + self.anchor2?.title'); - - const sourceUrl = StrCast(source.doc.data.url); // The URL of the annotation's source web page - console.log("sourceAnnotationId, url", sourceAnnotationId, sourceUrl); - Doc.GetProto(linkDoc).annotationUrl = Hypothesis.makeAnnotationUrl(sourceAnnotationId, sourceUrl); - - Doc.GetProto(source.doc).links = ComputedField.MakeFunction("links(self)"); - Doc.GetProto(target.doc).links = ComputedField.MakeFunction("links(self)"); - - return linkDoc; - } - export function DocumentFromField(target: Doc, fieldKey: string, proto?: Doc, options?: DocumentOptions): Doc | undefined { let created: Doc | undefined; let layout: ((fieldKey: string) => string) | undefined; diff --git a/src/client/views/linking/LinkMenuItem.tsx b/src/client/views/linking/LinkMenuItem.tsx index 76f802c0c..9c21bca35 100644 --- a/src/client/views/linking/LinkMenuItem.tsx +++ b/src/client/views/linking/LinkMenuItem.tsx @@ -153,8 +153,7 @@ export class LinkMenuItem extends React.Component<LinkMenuItemProps> { LinkDocPreview.LinkInfo = undefined; const redirectUrl = StrCast(this.props.linkDoc.annotationUrl, null); - redirectUrl && (this.props.destinationDoc.data = new WebField(redirectUrl)); // If the link is to an annotation, go to annotation - console.log(redirectUrl, "redirectUrl"); + 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 if (this.props.linkDoc.follow) { if (this.props.linkDoc.follow === "Default") { diff --git a/src/client/views/nodes/DocumentLinksButton.tsx b/src/client/views/nodes/DocumentLinksButton.tsx index 61143a6af..839f83f78 100644 --- a/src/client/views/nodes/DocumentLinksButton.tsx +++ b/src/client/views/nodes/DocumentLinksButton.tsx @@ -1,7 +1,7 @@ import { action, computed, observable, runInAction } from "mobx"; import { observer } from "mobx-react"; import { Doc, DocListCast } from "../../../fields/Doc"; -import { emptyFunction, setupMoveUpEvents, returnFalse } from "../../../Utils"; +import { emptyFunction, setupMoveUpEvents, returnFalse, Utils } from "../../../Utils"; import { DragManager } from "../../util/DragManager"; import { UndoManager } from "../../util/UndoManager"; import './DocumentLinksButton.scss'; @@ -17,6 +17,7 @@ import { StrCast } from "../../../fields/Types"; import { LinkDescriptionPopup } from "./LinkDescriptionPopup"; import { LinkManager } from "../../util/LinkManager"; +import { Hypothesis } from "../../apis/hypothesis/HypothesisApiUtils"; const higflyout = require("@hig/flyout"); export const { anchorPoints } = higflyout; export const Flyout = higflyout.default; @@ -35,19 +36,25 @@ export class DocumentLinksButton extends React.Component<DocumentLinksButtonProp @observable public static AnnotationId: string | undefined; componentDidMount() { - // window.addEventListener("linkStarted", (e: any) => { // event used by Hypothes.is plugin to tell Dash when an unlinked annotation has been created - // const annotatedUrl = e.details; - // SelectionManager.SelectedDocuments().forEach(action((element: DocumentView) => { - // DocumentLinksButton.StartLink = element; - // })); + // window.addEventListener("annotationCreated", (e: any) => { // event used by Hypothes.is plugin to tell Dash when an unlinked annotation has been created + // const id = e.details; + // const source = SelectionManager.SelectedDocuments()[0]; + // runInAction(() => { + // DocumentLinksButton.AnnotationId = id; + // DocumentLinksButton.StartLink = source; + // }); // }); - window.addEventListener("fakeLinkStarted", action((e: any) => { // event used by Hypothes.is plugin to tell Dash when an unlinked annotation has been created - console.log("Helo fake started link"); - const id = e.detail; + console.log("window", window); + window.addEventListener("fakeAnnotationCreated", async (e: any) => { // event used by Hypothes.is plugin to tell Dash when an unlinked annotation has been created + console.log("Helo fake annotation make"); + // const id = e.detail; + const id = await Hypothesis.getPlaceholderId("melissaz", "placeholder"); // delete once eventListening between client & Dash works const source = SelectionManager.SelectedDocuments()[0]; - DocumentLinksButton.AnnotationId = id; - DocumentLinksButton.StartLink = source; - })); + runInAction(() => { + DocumentLinksButton.AnnotationId = id; + DocumentLinksButton.StartLink = source; + }); + }); } @action @@ -114,9 +121,17 @@ export class DocumentLinksButton extends React.Component<DocumentLinksButtonProp // }); } else { if (DocumentLinksButton.StartLink && DocumentLinksButton.StartLink !== this.props.View) { - const linkDoc = DocumentLinksButton.AnnotationId ? - DocUtils.MakeHypothesisLink({ doc: DocumentLinksButton.StartLink.props.Document }, { doc: this.props.View.props.Document }, "hypothesis annotation", DocumentLinksButton.AnnotationId) : - DocUtils.MakeLink({ doc: DocumentLinksButton.StartLink.props.Document }, { doc: this.props.View.props.Document }, "long drag"); + const sourceDoc = DocumentLinksButton.StartLink.props.Document; + const targetDoc = this.props.View.props.Document; + const linkDoc = DocUtils.MakeLink({ doc: sourceDoc }, { doc: targetDoc }, DocumentLinksButton.AnnotationId ? "hypothes.is annotation" : "long drag"); + + // if the link's source is a Hypothes.is annotation + if (DocumentLinksButton.AnnotationId) { + const sourceUrl = StrCast(sourceDoc.data.url); // the URL of the annotation's source web page + Doc.GetProto(linkDoc as Doc).annotationUrl = Hypothesis.makeAnnotationUrl(DocumentLinksButton.AnnotationId, sourceUrl); // redirect web doc to this URL when following link + Hypothesis.dispatchLinkRequest(StrCast(targetDoc.title), Utils.prepend("/doc/" + targetDoc.Id), DocumentLinksButton.AnnotationId); // update and link placeholder annotation + } + LinkManager.currentLink = linkDoc; runInAction(() => { LinkCreatedBox.popupX = e.screenX; @@ -139,15 +154,25 @@ export class DocumentLinksButton extends React.Component<DocumentLinksButtonProp finishLinkClick = (e: React.MouseEvent) => { if (DocumentLinksButton.StartLink === this.props.View) { DocumentLinksButton.StartLink = undefined; + DocumentLinksButton.AnnotationId = undefined; console.log("reset to undefined (finisheLinkClick)"); // action((e: React.PointerEvent<HTMLDivElement>) => { // Doc.UnBrushDoc(this.props.View.Document); // }); } else { if (DocumentLinksButton.StartLink && DocumentLinksButton.StartLink !== this.props.View) { - const linkDoc = DocumentLinksButton.AnnotationId ? - DocUtils.MakeHypothesisLink({ doc: DocumentLinksButton.StartLink.props.Document }, { doc: this.props.View.props.Document }, "hypothesis annotation", DocumentLinksButton.AnnotationId) : - DocUtils.MakeLink({ doc: DocumentLinksButton.StartLink.props.Document }, { doc: this.props.View.props.Document }, "long drag"); + const sourceDoc = DocumentLinksButton.StartLink.props.Document; + const targetDoc = this.props.View.props.Document; + const linkDoc = DocUtils.MakeLink({ doc: sourceDoc }, { doc: targetDoc }, DocumentLinksButton.AnnotationId ? "hypothes.is annotation" : "long drag"); + + // if the link is to a Hypothes.is annotation + if (DocumentLinksButton.AnnotationId) { + const sourceUrl = StrCast(sourceDoc.data.url); // the URL of the annotation's source web page + console.log("sourceAnnotationId, url", DocumentLinksButton.AnnotationId, sourceUrl); + Doc.GetProto(linkDoc as Doc).annotationUrl = Hypothesis.makeAnnotationUrl(DocumentLinksButton.AnnotationId, sourceUrl); // redirect web doc to this URL when following link + Hypothesis.dispatchLinkRequest(StrCast(targetDoc.title), Utils.prepend("/doc/" + targetDoc.Id), DocumentLinksButton.AnnotationId); // update and link placeholder annotation + } + LinkManager.currentLink = linkDoc; runInAction(() => { LinkCreatedBox.popupX = e.screenX; diff --git a/src/client/views/nodes/DocumentView.tsx b/src/client/views/nodes/DocumentView.tsx index c67267860..41308b3a4 100644 --- a/src/client/views/nodes/DocumentView.tsx +++ b/src/client/views/nodes/DocumentView.tsx @@ -793,7 +793,7 @@ export class DocumentView extends DocComponent<DocumentViewProps, Document>(Docu cm.addItem({ description: "pretend we made an annotation", event: () => { - document.dispatchEvent(new CustomEvent("fakeLinkStarted", { + document.dispatchEvent(new CustomEvent("fakeAnnotationCreated", { detail: "fakefakefakeid", bubbles: true })); diff --git a/src/server/database.ts b/src/server/database.ts index 7fbab357b..767d38350 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -417,7 +417,7 @@ export namespace Database { } /** - * Writes the @param enrichedCredentials to the database, associated + * Writes the @param hypothesisApiKey to the database, associated * with @param userId for later retrieval and updating. */ export const Write = async (userId: string, hypothesisApiKey: string) => { |