This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
helpers.ts
112 lines (103 loc) · 4.11 KB
/
helpers.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
import { highlight, highlightAuto } from 'highlight.js/lib/highlight'
import marked from 'marked'
import * as React from 'react'
import sanitize from 'sanitize-html'
import { MarkupContent } from 'vscode-languageserver-types'
import { HoverOverlayProps, isJumpURL } from './HoverOverlay'
import { HoverMerged } from './types'
/**
* Returns true if `val` is not `null` or `undefined`
*/
export const isDefined = <T>(val: T): val is NonNullable<T> => val !== undefined && val !== null
/**
* Returns a function that returns `true` if the given `key` of the object is not `null` or `undefined`.
*
* I ❤️ TypeScript.
*/
export const propertyIsDefined = <T extends object, K extends keyof T>(key: K) => (
val: T
): val is K extends any ? ({ [k in Exclude<keyof T, K>]: T[k] } & { [k in K]: NonNullable<T[k]> }) : never =>
isDefined(val[key])
export const isEmptyHover = (hover: HoverMerged | null): boolean =>
!hover ||
!hover.contents ||
(Array.isArray(hover.contents) && hover.contents.length === 0) ||
(MarkupContent.is(hover.contents) && !hover.contents.value)
/**
* Returns true if the HoverOverlay would have anything to show according to the given hover and definition states.
*/
export const overlayUIHasContent = (state: Pick<HoverOverlayProps, 'hoverOrError' | 'definitionURLOrError'>): boolean =>
(!!state.hoverOrError && !(HoverMerged.is(state.hoverOrError) && isEmptyHover(state.hoverOrError))) ||
isJumpURL(state.definitionURLOrError)
/**
* Scrolls an element to the center if it is out of view.
* Does nothing if the element is in view.
*
* @param container The scrollable container (that has `overflow: auto`)
* @param content The content child that is being scrolled
* @param target The element that should be scrolled into view
*/
export const scrollIntoCenterIfNeeded = (container: HTMLElement, content: HTMLElement, target: HTMLElement): void => {
const containerRect = container.getBoundingClientRect()
const rowRect = target.getBoundingClientRect()
if (rowRect.top <= containerRect.top || rowRect.bottom >= containerRect.bottom) {
const containerRect = container.getBoundingClientRect()
const contentRect = content.getBoundingClientRect()
const rowRect = target.getBoundingClientRect()
const scrollTop = rowRect.top - contentRect.top - containerRect.height / 2 + rowRect.height / 2
container.scrollTop = scrollTop
}
}
/**
* Escapes HTML by replacing characters like `<` with their HTML escape sequences like `<`
*/
const escapeHTML = (html: string): string => {
const span = document.createElement('span')
span.textContent = html
return span.innerHTML
}
/**
* Attempts to syntax-highlight the given code.
* If the language is not given, it is auto-detected.
* If an error occurs, the code is returned as plain text with escaped HTML entities
*
* @param code The code to highlight
* @param language The language of the code, if known
* @return Safe HTML
*/
export const highlightCodeSafe = (code: string, language?: string): string => {
try {
if (language === 'plaintext' || language === 'text') {
return escapeHTML(code)
}
if (language) {
return highlight(language, code, true).value
}
return highlightAuto(code).value
} catch (err) {
console.warn('Error syntax-highlighting hover markdown code block', err)
return escapeHTML(code)
}
}
/**
* Renders the given markdown to HTML, highlighting code and sanitizing dangerous HTML.
* Can throw an exception on parse errors.
*/
export const renderMarkdown = (markdown: string): string =>
sanitize(
marked(markdown, {
gfm: true,
breaks: true,
sanitize: false,
highlight: (code, language) => '<code>' + highlightCodeSafe(code, language) + '</code>',
})
)
/**
* Converts a synthetic React event to a persisted, native Event object.
*
* @param event The synthetic React event object
*/
export const toNativeEvent = <E extends React.SyntheticEvent<T>, T>(event: E): E['nativeEvent'] => {
event.persist()
return event.nativeEvent
}