Skip to content

Commit

Permalink
Gather urls while initializing extension (OctoLinker#448)
Browse files Browse the repository at this point in the history
* Gather urls while initializing extension

* Update Manifest plugins

* Update all other plugins
  • Loading branch information
stefanbuck authored Mar 5, 2018
1 parent 83d3c67 commit 6a5afda
Show file tree
Hide file tree
Showing 43 changed files with 169 additions and 500 deletions.
153 changes: 0 additions & 153 deletions packages/core/__tests__/click-handler.test.js

This file was deleted.

33 changes: 11 additions & 22 deletions packages/core/click-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const RESOLVED = 'Redirecting 🚀';

const LINK_SELECTOR = '.octolinker-link';
const $body = $('body');
let pluginManager;
let matches;

function openUrl(url, newWindow = false, newWindowActive = true) {
if (!url) {
Expand All @@ -29,25 +29,10 @@ function openUrl(url, newWindow = false, newWindowActive = true) {
}
}

function getResolverUrls(dataAttr) {
function getResolverUrls(urls) {
const BASE_URL = 'https://github.com';
const { pluginName } = dataAttr;
const resolver = pluginManager.getResolver(pluginName);

if (!resolver) {
return [];
}

chrome.runtime.sendMessage({
type: 'track',
payload: {
category: 'resolver',
action: 'invoke',
label: pluginName,
},
});

return [].concat(resolver(dataAttr)).map(url => {
return [].concat(urls).map(url => {
if (!url) {
return null;
}
Expand Down Expand Up @@ -85,12 +70,16 @@ async function onClick(event) {
return;
}

const dataAttr = event.currentTarget.dataset;
const found = matches.find(item => item.link === event.currentTarget);
if (!found) {
return;
}

const $tooltipTarget = $('span', event.currentTarget);

showTooltip($tooltipTarget, PROCESS);

const urls = getResolverUrls(dataAttr);
const urls = getResolverUrls(found.urls);

if (!urls.length) {
return;
Expand Down Expand Up @@ -127,12 +116,12 @@ async function onClick(event) {
}
}

export default function(_pluginManager) {
export default function(_matches) {
$body.undelegate(LINK_SELECTOR, 'click', onClick);
$body.undelegate(LINK_SELECTOR, 'mouseup', onClick);

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

pluginManager = _pluginManager;
matches = _matches;
}
28 changes: 3 additions & 25 deletions packages/core/octo-linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ function initialize(self) {

self._blobReader = new BlobReader();
self._pluginManager = new Plugins(loadPlugins);
clickHandler(self._pluginManager);
}

function insertDataAttr(matches) {
matches.forEach(item => {
if (!item) {
return;
}

const { data, link } = item;
for (const key in data) {
if (data.hasOwnProperty(key)) {
link.dataset[key] = data[key];
}
}
});
}

function run(self) {
Expand All @@ -52,19 +36,13 @@ function run(self) {
matches = matches.concat(plugin.parseBlob(blob));
} else if (plugin.getLinkRegexes) {
[].concat(plugin.getLinkRegexes(blob)).forEach(regex => {
matches = matches.concat(
insertLink(blob.el, regex, {
pluginName: plugin.name,
target: '$1',
path: blob.path,
}),
);
matches = matches.concat(insertLink(blob, regex, plugin));
});
}
});

insertDataAttr(matches);
});

clickHandler(matches);
}

export default class OctoLinkerCore {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,21 @@ exports[`insert-link does not remove closing parentheses from commented out requ
</div>
`;

exports[`insert-link returns adds the given data-* attributes 1`] = `
Object {
"bar": "baz",
"value": "foo",
}
`;

exports[`insert-link returns replace placeholder with capture group value 1`] = `
Object {
"value": "go/foo.txt",
}
`;

exports[`insert-link returns returns an array 1`] = `
Array [
Object {
"data": Object {},
"link": <a
class="octolinker-link"
>
<span>
bar
</span>
</a>,
"urls": "urlsToResolve",
},
]
`;

exports[`insert-link returns wraps the second regex match 1`] = `
Object {
"value": "baz",
"xx": "yy",
}
`;

exports[`insert-link wraps a nested element 1`] = `
<div>
Expand Down Expand Up @@ -265,32 +245,3 @@ exports[`insert-link wraps the parent element when keyword is divided 1`] = `
</a>
</div>
`;

exports[`insert-link wraps the second regex match 1`] = `
<div>
foo
<i>
"
</i>
bar
<i>
"
</i>
<i>
"
</i>
<a
class="octolinker-link"
>
<span>
baz
</span>
</a>
<i>
"
</i>
</div>
`;
Loading

0 comments on commit 6a5afda

Please sign in to comment.