forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-dotcom-markdown.js
More file actions
129 lines (112 loc) · 3.95 KB
/
github-dotcom-markdown.js
File metadata and controls
129 lines (112 loc) · 3.95 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
import {CompositeDisposable, Disposable} from 'event-kit';
import React from 'react';
import ReactDom from 'react-dom';
import PropTypes from 'prop-types';
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 {autobind} from '../helpers';
export class BareGithubDotcomMarkdown extends React.Component {
static propTypes = {
relayEnvironment: PropTypes.object.isRequired,
html: PropTypes.string.isRequired,
switchToIssueish: PropTypes.func.isRequired,
handleClickEvent: PropTypes.func,
openIssueishLinkInNewTab: PropTypes.func,
openLinkInBrowser: PropTypes.func,
}
static defaultProps = {
handleClickEvent,
openIssueishLinkInNewTab,
openLinkInBrowser,
}
constructor(props) {
super(props);
autobind(this, 'handleClick', 'openLinkInNewTab', 'openLinkInThisTab', '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"
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 {
render() {
return (
<RelayEnvironment.Consumer>
{relayEnvironment => <BareGithubDotcomMarkdown relayEnvironment={relayEnvironment} {...this.props} />}
</RelayEnvironment.Consumer>
);
}
}