Skip to content

Commit 850801f

Browse files
author
Stefan Buck
committed
Gather urls while initializing extension
1 parent 03cf6a2 commit 850801f

File tree

43 files changed

+169
-500
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+169
-500
lines changed

packages/core/__tests__/click-handler.test.js

Lines changed: 0 additions & 153 deletions
This file was deleted.

packages/core/click-handler.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const RESOLVED = 'Redirecting 🚀';
99

1010
const LINK_SELECTOR = '.octolinker-link';
1111
const $body = $('body');
12-
let pluginManager;
12+
let matches;
1313

1414
function openUrl(url, newWindow = false, newWindowActive = true) {
1515
if (!url) {
@@ -29,25 +29,10 @@ function openUrl(url, newWindow = false, newWindowActive = true) {
2929
}
3030
}
3131

32-
function getResolverUrls(dataAttr) {
32+
function getResolverUrls(urls) {
3333
const BASE_URL = 'https://github.com';
34-
const { pluginName } = dataAttr;
35-
const resolver = pluginManager.getResolver(pluginName);
3634

37-
if (!resolver) {
38-
return [];
39-
}
40-
41-
chrome.runtime.sendMessage({
42-
type: 'track',
43-
payload: {
44-
category: 'resolver',
45-
action: 'invoke',
46-
label: pluginName,
47-
},
48-
});
49-
50-
return [].concat(resolver(dataAttr)).map(url => {
35+
return [].concat(urls).map(url => {
5136
if (!url) {
5237
return null;
5338
}
@@ -85,12 +70,16 @@ async function onClick(event) {
8570
return;
8671
}
8772

88-
const dataAttr = event.currentTarget.dataset;
73+
const found = matches.find(item => item.link === event.currentTarget);
74+
if (!found) {
75+
return;
76+
}
77+
8978
const $tooltipTarget = $('span', event.currentTarget);
9079

9180
showTooltip($tooltipTarget, PROCESS);
9281

93-
const urls = getResolverUrls(dataAttr);
82+
const urls = getResolverUrls(found.urls);
9483

9584
if (!urls.length) {
9685
return;
@@ -127,12 +116,12 @@ async function onClick(event) {
127116
}
128117
}
129118

130-
export default function(_pluginManager) {
119+
export default function(_matches) {
131120
$body.undelegate(LINK_SELECTOR, 'click', onClick);
132121
$body.undelegate(LINK_SELECTOR, 'mouseup', onClick);
133122

134123
$body.delegate(LINK_SELECTOR, 'click', onClick);
135124
$body.delegate(LINK_SELECTOR, 'mouseup', onClick);
136125

137-
pluginManager = _pluginManager;
126+
matches = _matches;
138127
}

packages/core/octo-linker.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@ function initialize(self) {
1414

1515
self._blobReader = new BlobReader();
1616
self._pluginManager = new Plugins(loadPlugins);
17-
clickHandler(self._pluginManager);
18-
}
19-
20-
function insertDataAttr(matches) {
21-
matches.forEach(item => {
22-
if (!item) {
23-
return;
24-
}
25-
26-
const { data, link } = item;
27-
for (const key in data) {
28-
if (data.hasOwnProperty(key)) {
29-
link.dataset[key] = data[key];
30-
}
31-
}
32-
});
3317
}
3418

3519
function run(self) {
@@ -52,19 +36,13 @@ function run(self) {
5236
matches = matches.concat(plugin.parseBlob(blob));
5337
} else if (plugin.getLinkRegexes) {
5438
[].concat(plugin.getLinkRegexes(blob)).forEach(regex => {
55-
matches = matches.concat(
56-
insertLink(blob.el, regex, {
57-
pluginName: plugin.name,
58-
target: '$1',
59-
path: blob.path,
60-
}),
61-
);
39+
matches = matches.concat(insertLink(blob, regex, plugin));
6240
});
6341
}
6442
});
65-
66-
insertDataAttr(matches);
6743
});
44+
45+
clickHandler(matches);
6846
}
6947

7048
export default class OctoLinkerCore {

packages/helper-insert-link/__tests__/__snapshots__/index.js.snap

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,21 @@ exports[`insert-link does not remove closing parentheses from commented out requ
1818
</div>
1919
`;
2020

21-
exports[`insert-link returns adds the given data-* attributes 1`] = `
22-
Object {
23-
"bar": "baz",
24-
"value": "foo",
25-
}
26-
`;
27-
28-
exports[`insert-link returns replace placeholder with capture group value 1`] = `
29-
Object {
30-
"value": "go/foo.txt",
31-
}
32-
`;
33-
3421
exports[`insert-link returns returns an array 1`] = `
3522
Array [
3623
Object {
37-
"data": Object {},
3824
"link": <a
3925
class="octolinker-link"
4026
>
4127
<span>
4228
bar
4329
</span>
4430
</a>,
31+
"urls": "urlsToResolve",
4532
},
4633
]
4734
`;
4835

49-
exports[`insert-link returns wraps the second regex match 1`] = `
50-
Object {
51-
"value": "baz",
52-
"xx": "yy",
53-
}
54-
`;
55-
5636
exports[`insert-link wraps a nested element 1`] = `
5737
<div>
5838
@@ -265,32 +245,3 @@ exports[`insert-link wraps the parent element when keyword is divided 1`] = `
265245
</a>
266246
</div>
267247
`;
268-
269-
exports[`insert-link wraps the second regex match 1`] = `
270-
<div>
271-
272-
foo
273-
<i>
274-
"
275-
</i>
276-
bar
277-
<i>
278-
"
279-
</i>
280-
281-
<i>
282-
"
283-
</i>
284-
<a
285-
class="octolinker-link"
286-
>
287-
<span>
288-
baz
289-
</span>
290-
</a>
291-
<i>
292-
"
293-
294-
</i>
295-
</div>
296-
`;

0 commit comments

Comments
 (0)