-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistries.ts
More file actions
149 lines (134 loc) · 4.59 KB
/
registries.ts
File metadata and controls
149 lines (134 loc) · 4.59 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
140
141
142
143
144
145
146
147
148
149
import type { OverTypeInstance } from 'overtype'
import OverType from 'overtype'
import type {
CommentEnhancer,
CommentEvent,
CommentEventType,
CommentSpot,
StrippedLocation,
} from './enhancer'
import { CommentEnhancerMissing } from './enhancers/CommentEnhancerMissing'
import { GitHubEditEnhancer } from './enhancers/github/GitHubEditEnhancer'
import { GitHubIssueAppendEnhancer } from './enhancers/github/GitHubIssueAppendEnhancer'
import { GitHubIssueCreateEnhancer } from './enhancers/github/GitHubIssueCreateEnhancer'
import { GitHubPrAppendEnhancer } from './enhancers/github/GitHubPrAppendEnhancer'
import { GitHubPrCreateEnhancer } from './enhancers/github/GitHubPrCreateEnhancer'
export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
textarea: HTMLTextAreaElement
spot: T
enhancer: CommentEnhancer<T>
overtype: OverTypeInstance
}
export class EnhancerRegistry {
private enhancers = new Set<CommentEnhancer>()
byType = new Map<string, CommentEnhancer>()
constructor() {
// Register all available handlers
this.register(new GitHubEditEnhancer())
this.register(new GitHubIssueAppendEnhancer())
this.register(new GitHubIssueCreateEnhancer())
this.register(new GitHubPrAppendEnhancer())
this.register(new GitHubPrCreateEnhancer())
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, location: StrippedLocation): EnhancedTextarea | null {
for (const enhancer of this.enhancers) {
try {
const spot = enhancer.tryToEnhance(textarea, location)
if (spot) {
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()
}, 800)
}
getEnhancerCount(): number {
return this.enhancers.size
}
}
export class TextareaRegistry {
private textareas = new Map<HTMLTextAreaElement, EnhancedTextarea>()
private eventSender: (event: CommentEvent) => void = () => {}
setCommentEventSender(sendEvent: (event: CommentEvent) => void): void {
this.eventSender = sendEvent
}
private sendEvent(eventType: CommentEventType, enhanced: EnhancedTextarea): void {
this.eventSender({
draft: enhanced.textarea.value,
spot: enhanced.spot,
type: eventType,
})
}
register<T extends CommentSpot>(enhanced: EnhancedTextarea<T>): void {
this.textareas.set(enhanced.textarea, enhanced)
enhanced.textarea.addEventListener('blur', () => {
this.sendEvent('LOST_FOCUS', enhanced)
})
this.sendEvent('ENHANCED', enhanced)
}
unregisterDueToModification(textarea: HTMLTextAreaElement): void {
const enhanced = this.textareas.get(textarea)
if (enhanced) {
this.sendEvent('DESTROYED', enhanced)
this.textareas.delete(textarea)
}
}
get(textarea: HTMLTextAreaElement): EnhancedTextarea | undefined {
return this.textareas.get(textarea)
}
tabLostFocus(): void {
for (const enhanced of this.textareas.values()) {
this.sendEvent('LOST_FOCUS', enhanced)
}
}
}