diff options
Diffstat (limited to 'src/client/util/HypothesisUtils.ts')
-rw-r--r-- | src/client/util/HypothesisUtils.ts | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/src/client/util/HypothesisUtils.ts b/src/client/util/HypothesisUtils.ts index c5f307f44..5a6522dc8 100644 --- a/src/client/util/HypothesisUtils.ts +++ b/src/client/util/HypothesisUtils.ts @@ -1,15 +1,11 @@ import { action, runInAction } from 'mobx'; -import { simulateMouseClick } from '../../Utils'; +import { simulateMouseClick } from '../../ClientUtils'; import { Doc, Opt } from '../../fields/Doc'; import { Cast, StrCast } from '../../fields/Types'; import { WebField } from '../../fields/URLField'; -import { DocumentType } from '../documents/DocumentTypes'; import { Docs } from '../documents/Documents'; import { DocumentLinksButton } from '../views/nodes/DocumentLinksButton'; import { DocumentView } from '../views/nodes/DocumentView'; -import { DocumentManager } from './DocumentManager'; -import { SearchUtil } from './SearchUtil'; -import { SelectionManager } from './SelectionManager'; export namespace Hypothesis { /** @@ -17,6 +13,7 @@ export namespace Hypothesis { * If none exist, create and return a new WebDocument. */ export const getSourceWebDoc = async (uri: string) => { + // eslint-disable-next-line no-use-before-define const result = await findWebDoc(uri); console.log(result ? 'existing doc found' : 'existing doc NOT found'); return result || Docs.Create.WebDocument(uri, { title: uri, _nativeWidth: 850, _height: 512, _width: 400, data_useCors: true }); // create and return a new Web doc with given uri if no matching docs are found @@ -26,21 +23,21 @@ export namespace Hypothesis { * Search for a WebDocument whose url field matches the given uri, return undefined if not found */ export const findWebDoc = async (uri: string) => { - const currentDoc = SelectionManager.Docs.lastElement(); + const currentDoc = DocumentView.Selected().lastElement()?.Document; if (currentDoc && Cast(currentDoc.data, WebField)?.url.href === uri) return currentDoc; // always check first whether the currently selected doc is the annotation's source, only use Search otherwise const results: Doc[] = []; // await SearchUtil.Search('web', true).then( // action(async (res: SearchUtil.DocSearchResult) => { // const docs = res.docs; - // const filteredDocs = docs.filter(doc => doc.author === Doc.CurrentUserEmail && doc.type === DocumentType.WEB && doc.data); + // const filteredDocs = docs.filter(doc => doc.author === ClientUtils.CurrentUserEmail() && doc.type === DocumentType.WEB && doc.data); // filteredDocs.forEach(doc => { // uri === Cast(doc.data, WebField)?.url.href && results.push(doc); // TODO check visited sites history? // }); // }) // ); - const onScreenResults = results.filter(doc => DocumentManager.Instance.getFirstDocumentView(doc)); + const onScreenResults = results.filter(doc => DocumentView.getFirstDocumentView(doc)); return onScreenResults.length ? onScreenResults[0] : results.length ? results[0] : undefined; // prioritize results that are currently on the screen }; @@ -66,7 +63,7 @@ export namespace Hypothesis { DocumentLinksButton.AnnotationId = annotationId; DocumentLinksButton.AnnotationUri = annotationUri; }); - const endLinkView = DocumentManager.Instance.getFirstDocumentView(sourceDoc); + const endLinkView = DocumentView.getFirstDocumentView(sourceDoc); const rect = document.body.getBoundingClientRect(); const x = rect.x + rect.width / 2; const y = 250; @@ -77,17 +74,19 @@ export namespace Hypothesis { /** * Send message to Hypothes.is client to edit an annotation to add a Dash hyperlink */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars export const makeLink = async (title: string, url: string, annotationId: string, annotationSourceDoc: Doc) => { // if the annotation's source webpage isn't currently loaded in Dash, we're not able to access and edit the annotation from the client // so we're loading the webpage and its annotations invisibly in a WebBox in MainView.tsx, until the editing is done - //!DocumentManager.Instance.getFirstDocumentView(annotationSourceDoc) && runInAction(() => DocumentLinksButton.invisibleWebDoc = annotationSourceDoc); + //! DocumentManager.Instance.getFirstDocumentView(annotationSourceDoc) && runInAction(() => DocumentLinksButton.invisibleWebDoc = annotationSourceDoc); - var success = false; + let success = false; const onSuccess = action(() => { console.log('Edit success!!'); success = true; + // eslint-disable-next-line no-use-before-define clearTimeout(interval); - //DocumentLinksButton.invisibleWebDoc = undefined; + // DocumentLinksButton.invisibleWebDoc = undefined; document.removeEventListener('editSuccess', onSuccess); }); @@ -109,7 +108,7 @@ export namespace Hypothesis { action(() => { if (!success) { clearInterval(interval); - //DocumentLinksButton.invisibleWebDoc = undefined; + // DocumentLinksButton.invisibleWebDoc = undefined; } }), 10000 @@ -123,12 +122,13 @@ export namespace Hypothesis { export const deleteLink = async (linkDoc: Doc, sourceDoc: Doc, destinationDoc: Doc) => { if (Cast(destinationDoc.data, WebField)?.url.href !== StrCast(linkDoc.annotationUri)) return; // check that the destinationDoc is a WebDocument containing the target annotation - //!DocumentManager.Instance.getFirstDocumentView(destinationDoc) && runInAction(() => DocumentLinksButton.invisibleWebDoc = destinationDoc); // see note in makeLink + //! DocumentManager.Instance.getFirstDocumentView(destinationDoc) && runInAction(() => DocumentLinksButton.invisibleWebDoc = destinationDoc); // see note in makeLink - var success = false; + let success = false; const onSuccess = action(() => { console.log('Edit success!'); success = true; + // eslint-disable-next-line no-use-before-define clearTimeout(interval); // DocumentLinksButton.invisibleWebDoc = undefined; document.removeEventListener('editSuccess', onSuccess); @@ -151,7 +151,7 @@ export namespace Hypothesis { action(() => { if (!success) { clearInterval(interval); - //DocumentLinksButton.invisibleWebDoc = undefined; + // DocumentLinksButton.invisibleWebDoc = undefined; } }), 10000 @@ -163,10 +163,11 @@ export namespace Hypothesis { * Send message to Hypothes.is client to scroll to an annotation when it loads */ export const scrollToAnnotation = (annotationId: string, target: Doc) => { - var success = false; + let success = false; const onSuccess = () => { console.log('Scroll success!!'); document.removeEventListener('scrollSuccess', onSuccess); + // eslint-disable-next-line no-use-before-define clearInterval(interval); success = true; }; @@ -179,7 +180,7 @@ export namespace Hypothesis { bubbles: true, }) ); - const targetView: Opt<DocumentView> = DocumentManager.Instance.getFirstDocumentView(target); + const targetView: Opt<DocumentView> = DocumentView.getFirstDocumentView(target); const position = targetView?.screenToViewTransform().inverse().transformPoint(0, 0); targetView && position && simulateMouseClick(targetView.ContentDiv!, position[0], position[1], position[0], position[1], false); }, 300); |