forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_script.ts
163 lines (154 loc) · 5.13 KB
/
content_script.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as Mousetrap from 'mousetrap'
import { bodyLoader } from '../util/loader'
import {
setupRPC,
insertTooltip,
userSelectedText,
removeTooltip,
} from './interactions'
import ToolbarNotifications from '../toolbar-notification/content_script'
import {
conditionallyShowOnboardingNotifications,
conditionallyRemoveSelectOption,
} from './onboarding-interactions'
import {
getTooltipState,
getKeyboardShortcutsState,
convertKeyboardEventToKeyString,
runOnScriptShutdown,
} from './utils'
import { remoteFunction } from 'src/util/webextensionRPC'
import {
highlightAnnotations,
removeHighlights,
} from 'src/sidebar-overlay/content_script/highlight-interactions'
import { STAGES } from 'src/overview/onboarding/constants'
import {
toggleSidebarOverlay,
createAnnotation as createAnnotationAct,
createAndCopyDirectLink,
createHighlight,
} from 'src/direct-linking/content_script/interactions'
import { KeyboardShortcuts } from './types'
export default async function init({
toolbarNotifications,
}: {
toolbarNotifications?: ToolbarNotifications
}) {
runOnScriptShutdown(() => removeTooltip())
// Set up the RPC calls even if the tooltip is enabled or not.
setupRPC({ toolbarNotifications })
await conditionallyShowOnboardingNotifications({
toolbarNotifications,
})
const isTooltipEnabled = await getTooltipState()
if (!isTooltipEnabled) {
const {
shortcutsEnabled,
...shortcuts
} = await getKeyboardShortcutsState()
Mousetrap.bind(
Object.values(shortcuts).map(val => val.shortcut),
handleKeyboardShortcuts(shortcuts),
)
return
}
await bodyLoader()
await insertTooltip({ toolbarNotifications })
}
let highlightsOn = false
const handleKeyboardShortcuts = ({
addComment,
addTag,
addToCollection,
createAnnotation,
createBookmark,
highlight,
link,
toggleHighlights,
toggleSidebar,
}: KeyboardShortcuts) => async e => {
const isTooltipEnabled = await getTooltipState()
if (!isTooltipEnabled) {
if (!userSelectedText()) {
switch (convertKeyboardEventToKeyString(e)) {
case toggleSidebar.shortcut:
toggleSidebar.enabled &&
toggleSidebarOverlay({
override: true,
openSidebar: true,
})
break
case toggleHighlights.shortcut:
toggleHighlights.enabled && toggleHighlightsAct()
break
case addTag.shortcut:
addTag.enabled &&
toggleSidebarOverlay({
override: true,
openToTags: true,
})
break
case addToCollection.shortcut:
addToCollection.enabled &&
toggleSidebarOverlay({
override: true,
openToCollections: true,
})
break
case addComment.shortcut:
addComment.enabled &&
toggleSidebarOverlay({
override: true,
openToComment: true,
})
break
case createBookmark.shortcut:
createBookmark.enabled &&
toggleSidebarOverlay({
override: true,
openToBookmark: true,
})
break
default:
}
} else {
switch (convertKeyboardEventToKeyString(e)) {
case link.shortcut:
link.enabled && (await createLink())
break
case highlight.shortcut:
if (highlight.enabled) {
await createHighlight()
toggleHighlightsAct()
}
break
case createAnnotation.shortcut:
createAnnotation.enabled && (await createNewAnnotation(e))
break
default:
}
}
}
}
const toggleHighlightsAct = () => {
highlightsOn ? removeHighlights() : fetchAndHighlightAnnotations()
highlightsOn = !highlightsOn
}
const fetchAndHighlightAnnotations = async () => {
const annotations = await remoteFunction('getAllAnnotationsByUrl')(
window.location.href,
)
const highlightables = annotations.filter(annotation => annotation.selector)
highlightAnnotations(highlightables, toggleSidebarOverlay)
}
const createNewAnnotation = async e => {
e.preventDefault()
e.stopPropagation()
await createAnnotationAct()
// Remove onboarding select option notification if it's present
await conditionallyRemoveSelectOption(STAGES.annotation.annotationCreated)
}
const createLink = async () => {
await createAndCopyDirectLink()
}