-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistries.ts
More file actions
139 lines (125 loc) · 4.44 KB
/
registries.ts
File metadata and controls
139 lines (125 loc) · 4.44 KB
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
import type { OverTypeInstance } from 'overtype'
import OverType from 'overtype'
import type { CommentEnhancer, CommentSpot } from './enhancer'
import { CommentEnhancerMissing } from './enhancers/CommentEnhancerMissing'
import { GitHubIssueAddCommentEnhancer } from './enhancers/github/githubIssueAddComment'
import { GitHubIssueNewCommentEnhancer } from './enhancers/github/githubIssueNewComment'
import { GitHubPRAddCommentEnhancer } from './enhancers/github/githubPRAddComment'
import { GitHubPRNewCommentEnhancer } from './enhancers/github/githubPRNewComment'
export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
textarea: HTMLTextAreaElement
spot: T
enhancer: CommentEnhancer<T>
overtype: OverTypeInstance
}
export class EnhancerRegistry {
private enhancers = new Set<CommentEnhancer>()
private preparedEnhancers = new Set<CommentEnhancer>()
byType = new Map<string, CommentEnhancer>()
constructor() {
// Register all available handlers
this.register(new GitHubIssueAddCommentEnhancer())
this.register(new GitHubIssueNewCommentEnhancer())
this.register(new GitHubPRAddCommentEnhancer())
this.register(new GitHubPRNewCommentEnhancer())
const textColor = 'rgb(31, 35, 40)'
const headingColor = 'rgb(174, 52, 151)'
OverType.setTheme({
colors: {
blockquote: 'rgb(89, 99, 110)',
code: '#59636e',
codeBg: '#f6f8fa',
cursor: '#f95738',
em: 'rgb(126, 123, 255)',
h1: headingColor,
h2: headingColor,
h3: headingColor,
hr: '#5a7a9b',
link: 'rgb(9, 105, 218)',
selection: 'rgba(244, 211, 94, 0.4)',
strong: 'rgb(45, 1, 142)',
syntaxMarker: textColor,
text: textColor,
},
name: 'custom-github',
})
}
private register<T extends CommentSpot>(enhancer: CommentEnhancer<T>): void {
this.enhancers.add(enhancer)
for (const spotType of enhancer.forSpotTypes()) {
this.byType.set(spotType, enhancer)
}
}
enhancerFor<T extends CommentSpot>(spot: T): CommentEnhancer<T> {
return (this.byType.get(spot.type) || new CommentEnhancerMissing()) as CommentEnhancer<T>
}
tryToEnhance(textarea: HTMLTextAreaElement): EnhancedTextarea | null {
for (const enhancer of this.enhancers) {
try {
const spot = enhancer.tryToEnhance(textarea)
if (spot) {
// Prepare enhancer on first use
if (!this.preparedEnhancers.has(enhancer)) {
enhancer.prepareForFirstEnhancement()
this.preparedEnhancers.add(enhancer)
}
const overtype = enhancer.enhance(textarea, spot)
this.handleDelayedValueInjection(overtype)
return { enhancer, overtype, spot, textarea }
}
} catch (error) {
console.warn('Handler failed to identify textarea:', error)
}
}
return null
}
private handleDelayedValueInjection(overtype: OverTypeInstance): void {
// GitHub sometimes injects textarea content after a delay
// We need to trigger OverType to update its preview after such injections
// https://github.com/diffplug/gitcasso/issues/46
setTimeout(() => {
overtype.updatePreview()
}, 100)
setTimeout(() => {
overtype.updatePreview()
}, 200)
setTimeout(() => {
overtype.updatePreview()
}, 400)
setTimeout(() => {
overtype.updatePreview()
}, 8000)
}
getEnhancerCount(): number {
return this.enhancers.size
}
}
export class TextareaRegistry {
private textareas = new Map<HTMLTextAreaElement, EnhancedTextarea>()
private onEnhanced?: (spot: CommentSpot) => void
private onDestroyed?: (spot: CommentSpot) => void
setEventHandlers(
onEnhanced: (spot: CommentSpot) => void,
onDestroyed: (spot: CommentSpot) => void,
): void {
this.onEnhanced = onEnhanced
this.onDestroyed = onDestroyed
}
register<T extends CommentSpot>(textareaInfo: EnhancedTextarea<T>): void {
this.textareas.set(textareaInfo.textarea, textareaInfo)
this.onEnhanced?.(textareaInfo.spot)
}
unregisterDueToModification(textarea: HTMLTextAreaElement): void {
const textareaInfo = this.textareas.get(textarea)
if (textareaInfo) {
this.onDestroyed?.(textareaInfo.spot)
this.textareas.delete(textarea)
}
}
get(textarea: HTMLTextAreaElement): EnhancedTextarea | undefined {
return this.textareas.get(textarea)
}
getAllEnhanced(): EnhancedTextarea[] {
return Array.from(this.textareas.values())
}
}