Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gather urls while initializing extension #448

Merged
merged 3 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For simplicity I tweaked the click-handler according to my needs. As mentioned above, it will go away soon.

}
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];
}
}
});
Copy link
Member Author

@stefanbuck stefanbuck Feb 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used data-attributes to hold a reference to the plugin and other plugin related informations. Now we pass the plugin itself down which makes things a lot easier.

}

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