forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-dotcom-markdown.js
156 lines (134 loc) · 4.46 KB
/
github-dotcom-markdown.js
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
import React from 'react';
import ReactDom from 'react-dom';
import PropTypes from 'prop-types';
import {CompositeDisposable, Disposable} from 'event-kit';
import {handleClickEvent, openIssueishLinkInNewTab, openLinkInBrowser, getDataFromGithubUrl} from './issueish-link';
import UserMentionTooltipItem from '../items/user-mention-tooltip-item';
import IssueishTooltipItem from '../items/issueish-tooltip-item';
import RelayEnvironment from './relay-environment';
import {renderMarkdown} from '../helpers';
export class BareGithubDotcomMarkdown extends React.Component {
static propTypes = {
relayEnvironment: PropTypes.object.isRequired,
className: PropTypes.string,
html: PropTypes.string.isRequired,
switchToIssueish: PropTypes.func.isRequired,
handleClickEvent: PropTypes.func,
openIssueishLinkInNewTab: PropTypes.func,
openLinkInBrowser: PropTypes.func,
}
static defaultProps = {
className: '',
handleClickEvent,
openIssueishLinkInNewTab,
openLinkInBrowser,
}
componentDidMount() {
this.commandSubscriptions = atom.commands.add(ReactDom.findDOMNode(this), {
'github:open-link-in-new-tab': this.openLinkInNewTab,
'github:open-link-in-browser': this.openLinkInBrowser,
'github:open-link-in-this-tab': this.openLinkInThisTab,
});
this.setupComponentHandlers();
this.setupTooltipHandlers();
}
componentDidUpdate() {
this.setupTooltipHandlers();
}
setupComponentHandlers() {
this.component.addEventListener('click', this.handleClick);
this.componentHandlers = new Disposable(() => {
this.component.removeEventListener('click', this.handleClick);
});
}
setupTooltipHandlers() {
if (this.tooltipSubscriptions) {
this.tooltipSubscriptions.dispose();
}
this.tooltipSubscriptions = new CompositeDisposable();
this.component.querySelectorAll('.user-mention').forEach(node => {
const item = new UserMentionTooltipItem(node.textContent, this.props.relayEnvironment);
this.tooltipSubscriptions.add(atom.tooltips.add(node, {
trigger: 'hover',
delay: 0,
class: 'github-Popover',
item,
}));
this.tooltipSubscriptions.add(new Disposable(() => item.destroy()));
});
this.component.querySelectorAll('.issue-link').forEach(node => {
const item = new IssueishTooltipItem(node.getAttribute('href'), this.props.relayEnvironment);
this.tooltipSubscriptions.add(atom.tooltips.add(node, {
trigger: 'hover',
delay: 0,
class: 'github-Popover',
item,
}));
this.tooltipSubscriptions.add(new Disposable(() => item.destroy()));
});
}
componentWillUnmount() {
this.commandSubscriptions.dispose();
this.componentHandlers.dispose();
this.tooltipSubscriptions && this.tooltipSubscriptions.dispose();
}
render() {
return (
<div
className={`github-DotComMarkdownHtml native-key-bindings ${this.props.className}`}
tabIndex="-1"
ref={c => { this.component = c; }}
dangerouslySetInnerHTML={{__html: this.props.html}}
/>
);
}
handleClick = event => {
if (event.target.dataset.url) {
return this.props.handleClickEvent(event, event.target.dataset.url);
} else {
return null;
}
}
openLinkInNewTab = event => {
return this.props.openIssueishLinkInNewTab(event.target.dataset.url);
}
openLinkInThisTab = event => {
const {repoOwner, repoName, issueishNumber} = getDataFromGithubUrl(event.target.dataset.url);
this.props.switchToIssueish(repoOwner, repoName, issueishNumber);
}
openLinkInBrowser = event => {
return this.props.openLinkInBrowser(event.target.getAttribute('href'));
}
}
export default class GithubDotcomMarkdown extends React.Component {
static propTypes = {
markdown: PropTypes.string,
html: PropTypes.string,
}
state = {
lastMarkdown: null,
html: null,
}
static getDerivedStateFromProps(props, state) {
if (props.html) {
return {html: props.html};
}
if (props.markdown && props.markdown !== state.lastMarkdown) {
return {html: renderMarkdown(props.markdown), lastMarkdown: props.markdown};
}
return null;
}
render() {
return (
<RelayEnvironment.Consumer>
{relayEnvironment => (
<BareGithubDotcomMarkdown
relayEnvironment={relayEnvironment}
{...this.props}
html={this.state.html}
/>
)}
</RelayEnvironment.Consumer>
);
}
}