Skip to content

Commit

Permalink
Block nav-away from page during highlight creation
Browse files Browse the repository at this point in the history
- covers tooltip and KB shortcut methods
  • Loading branch information
poltak committed May 30, 2024
1 parent d3b187a commit 2753aa8
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/content-scripts/content_script/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ import { HIGHLIGHT_COLORS_DEFAULT } from '@worldbrain/memex-common/lib/common-ui
import { PKMSyncBackgroundModule } from 'src/pkm-integrations/background'
import { injectTelegramCustomUI } from './injectionUtils/telegram'
import { renderSpacesBar } from './injectionUtils/utils'
import { getTimestampedNoteWithAIsummaryForYoutubeNotes } from './injectionUtils/youtube'
import {
injectTwitterProfileUI,
trackTwitterMessageList,
Expand All @@ -113,7 +112,6 @@ import type { InPageUIComponent } from 'src/in-page-ui/shared-state/types'
import type { RemoteCopyPasterInterface } from 'src/copy-paster/background/types'
import type { HighlightColor } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/types'
import type { InPageUIInterface } from 'src/in-page-ui/background/types'
import type { Storage } from 'webextension-polyfill'
import type { PseudoSelection } from '@worldbrain/memex-common/lib/in-page-ui/types'
import {
cloneSelectionAsPseudoObject,
Expand All @@ -125,6 +123,7 @@ import {
} from '@worldbrain/memex-common/lib/subscriptions/constants'
import type { RemoteSearchInterface } from 'src/search/background/types'
import * as anchoring from '@worldbrain/memex-common/lib/annotations'
import type { TaskState } from 'ui-logic-core/lib/types'

// Content Scripts are separate bundles of javascript code that can be loaded
// on demand by the browser, as needed. This main function manages the initialisation
Expand Down Expand Up @@ -578,7 +577,17 @@ export async function main(
quality: 100,
})

let highlightCreateState: TaskState = 'pristine'
// Block nav away from current page while highlight being created
window.addEventListener('beforeunload', (e) => {
let shouldBlockUnload = highlightCreateState === 'running'
if (shouldBlockUnload) {
e.preventDefault()
}
})

const annotationsFunctions = {
// TODO: Simplify and move this logic away from here
createHighlight: (
analyticsEvent?: AnalyticsEvent<'Highlights'>,
) => async (
Expand All @@ -589,6 +598,7 @@ export async function main(
highlightColorSetting?: HighlightColor,
preventHideTooltip?: boolean,
) => {
highlightCreateState = 'running'
let anchor: Anchor
let quote: string

Expand All @@ -603,6 +613,7 @@ export async function main(
anchor = { quote, descriptor }
}
if (quote?.length === 0 && !drawRectangle) {
highlightCreateState = 'success'
return
}

Expand All @@ -618,7 +629,7 @@ export async function main(
sidebarEvents.emit('showPowerUpModal', {
limitReachedNotif: 'Bookmarks',
})

highlightCreateState = 'error'
return
}

Expand Down Expand Up @@ -647,6 +658,7 @@ export async function main(
screenshotGrabResult == null ||
screenshotGrabResult.anchor == null
) {
highlightCreateState = 'success'
return
}

Expand Down Expand Up @@ -722,8 +734,10 @@ export async function main(
}
}

highlightCreateState = 'success'
return annotationId
},
// TODO: Simplify and move this logic away from here
createAnnotation: (
analyticsEvent?: AnalyticsEvent<'Annotations'>,
) => async (
Expand All @@ -734,8 +748,10 @@ export async function main(
commentText?: string,
highlightColorSetting?: HighlightColor,
) => {
highlightCreateState = 'running'
const selectionEmpty = !selection?.toString().length
if (selectionEmpty) {
highlightCreateState = 'success'
return
}

Expand All @@ -761,6 +777,7 @@ export async function main(
sidebarEvents.emit('showPowerUpModal', {
limitReachedNotif: 'Bookmarks',
})
highlightCreateState = 'error'
return
}

Expand All @@ -787,6 +804,7 @@ export async function main(
screenshotGrabResult == null ||
screenshotGrabResult.anchor == null
) {
highlightCreateState = 'success'
return
}

Expand Down Expand Up @@ -868,6 +886,7 @@ export async function main(
)
}
}
highlightCreateState = 'success'
},
askAI: () => (
highlightedText: string,
Expand Down

0 comments on commit 2753aa8

Please sign in to comment.