diff options
| author | Melissa Zhang <mzhang19096@gmail.com> | 2020-07-14 22:22:47 -0700 |
|---|---|---|
| committer | Melissa Zhang <mzhang19096@gmail.com> | 2020-07-14 22:22:47 -0700 |
| commit | 610ddaf1425018b4b00eb1fae83930f20777bad7 (patch) | |
| tree | 102c20334b54e2200030f7d36ce11cd7b8987a43 /src/client/apis/hypothesis/HypothesisApiUtils.ts | |
| parent | eb6e1b6705560d6fe94cb5787839b9138ab4b979 (diff) | |
deleting a link in a document removes its corresponding hyperlink in annotation (with some regex bugs)
Diffstat (limited to 'src/client/apis/hypothesis/HypothesisApiUtils.ts')
| -rw-r--r-- | src/client/apis/hypothesis/HypothesisApiUtils.ts | 51 |
1 files changed, 34 insertions, 17 deletions
diff --git a/src/client/apis/hypothesis/HypothesisApiUtils.ts b/src/client/apis/hypothesis/HypothesisApiUtils.ts index 83881cd89..2bffdb530 100644 --- a/src/client/apis/hypothesis/HypothesisApiUtils.ts +++ b/src/client/apis/hypothesis/HypothesisApiUtils.ts @@ -43,27 +43,44 @@ export namespace Hypothesis { } }; - // Find the most recent placeholder annotation created, and return 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; + export const editAnnotation = async (annotationId: string, newText: string) => { + console.log("DASH dispatching editRequest"); + const credentials = await getCredentials(); + document.dispatchEvent(new CustomEvent<{ newText: string, id: string, apiKey: string }>("editRequest", { + detail: { newText: newText, id: annotationId, apiKey: credentials.apiKey }, + bubbles: true + })); }; - // Send request to Hypothes.is client to modify a placeholder annotation into a hyperlink to Dash - export const dispatchLinkRequest = async (title: string, url: string, annotationId: string) => { - const credentials = await getCredentials(); + /** + * Edit an annotation with ID @param annotationId to add a hyperlink to a Dash document, which needs to be + * written in the format [@param title](@param url) + */ + export const makeLink = async (title: string, url: string, annotationId: string) => { const oldAnnotation = await fetchAnnotation(annotationId); const oldText = StrCast(oldAnnotation.text); const newHyperlink = `[${title}\n](${url})`; - const newText = oldText === "placeholder" ? newHyperlink : oldText + '\n\n' + newHyperlink; + const newText = oldText === "placeholder" ? newHyperlink : oldText + '\n\n' + newHyperlink; // if this is not the first link in the annotation, add link on new line + await editAnnotation(annotationId, newText); + }; - console.log("DASH dispatching linkRequest"); - document.dispatchEvent(new CustomEvent<{ newText: string, id: string, apiKey: string }>("linkRequest", { - detail: { newText: newText, id: annotationId, apiKey: credentials.apiKey }, - bubbles: true - })); + /** + * Edit an annotation with ID @param annotationId to delete a hyperlink to the Dash document with URL @param linkUrl + */ + export const deleteLink = async (annotationId: string, linkUrl: string) => { + const annotation = await fetchAnnotation(annotationId); + const regex = new RegExp(`(\[[^[]*)\(${linkUrl.replace('/', '\/')}\)`); + const target = regex.exec(annotation.text); // use regex to extract the link to be deleted, which is written in [title](hyperlink) format + target && console.log(target); + // target && editAnnotation(annotationId, annotation.text.replace(target[0], '')); + }; + + // 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 @@ -74,7 +91,7 @@ export namespace Hypothesis { // Extract username from Hypothe.is's userId format export const extractUsername = (userid: string) => { - const exp: RegExp = /(?<=\:)(.*?)(?=\@)/; - return exp.exec(userid)![0]; + const regex = new RegExp('(?<=\:)(.*?)(?=\@)/'); + return regex.exec(userid)![0]; }; }
\ No newline at end of file |
