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

Prepare master for v5 release. #551

Merged
merged 15 commits into from
Jun 1, 2019
Prev Previous commit
Next Next commit
Rerun octolinker after progressive-container has loaded
  • Loading branch information
Stefan Buck committed Jun 1, 2019
commit 0a4ec76c1041bf70872348faa12e76c96e6db394
21 changes: 17 additions & 4 deletions packages/blob-reader/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ function getBlobCodeInner(el) {
return [].slice.call(el.getElementsByClassName('blob-code-inner'));
}

function getBlobWrapper() {
let ret = [].slice.call(document.getElementsByClassName('blob-wrapper'));
function getBlobWrapper(rootElement = document) {
let ret = [].slice.call(
rootElement.getElementsByClassName('js-blob-wrapper'),
);

if (!ret.length) {
ret = [].slice.call(document.getElementsByClassName('highlight'));
ret = [].slice.call(rootElement.getElementsByClassName('highlight'));
}

return ret;
Expand Down Expand Up @@ -50,7 +52,18 @@ function getParentSha() {

function getPath(el) {
// When current page is a diff view get path from "View" button
let ret = $('.file-actions a', el.parentElement.parentElement)
let rootSelector = el.parentElement.parentElement.querySelectorAll(
'.file-actions a',
);

// When current diff blob is loaded on-demand get path from "View" button
if (!rootSelector.length) {
rootSelector = el.parentElement.parentElement.parentElement.querySelectorAll(
'.file-actions a',
);
}

let ret = $(rootSelector)
.filter(function() {
return (
$(this)
Expand Down
14 changes: 6 additions & 8 deletions packages/blob-reader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import Blob from './blob';
import { getBlobWrapper } from './helper';

export default class BlobReader {
hasBlobs() {
return !!getBlobWrapper().length;
constructor() {
this._blobs = [];
}

read() {
this._blobs = getBlobWrapper().map(el => new Blob(el));

return this;
hasBlobs() {
return !!getBlobWrapper(document).length;
}

getBlobs() {
return this._blobs;
read(rootElement) {
return getBlobWrapper(rootElement).map(el => new Blob(el));
}
}
99 changes: 94 additions & 5 deletions packages/core/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,96 @@
import injection from 'github-injection';
import BlobReader from '@octolinker/blob-reader';
import insertLink from '@octolinker/helper-insert-link';
import * as storage from '@octolinker/helper-settings';
import OctoLinker from './octo-linker.js';
import helperSortUrls from '@octolinker/helper-sort-urls';
import normaliseResolverResults from '@octolinker/helper-normalise-resolver-results';
import notification from './notification';
import Plugins from './plugin-manager.js';
import debugMode from './debug-mode.js';
import loader from './loader.js';
import * as loadPlugins from './load-plugins';

storage.load().then(() => {
const octoLinker = new OctoLinker();
octoLinker.init();
});
const blobReader = new BlobReader();
const pluginManager = new Plugins(loadPlugins);

async function run(rootElement) {
const blobs = blobReader.read(rootElement);

let matches = [];
for (const blob of blobs) {
const plugins = pluginManager.get(blob.path, blob.el.classList);

if (plugins.length) {
for (const plugin of plugins) {
if (plugin.needsContext && blob.isDiff) {
await blob.fetchBlob(); // eslint-disable-line no-await-in-loop
await blob.fetchParentBlob(); // eslint-disable-line no-await-in-loop
}

if (plugin.parseBlob) {
matches = matches.concat(plugin.parseBlob(blob));
} else if (plugin.getLinkRegexes) {
for (const regex of [].concat(plugin.getLinkRegexes(blob))) {
matches = matches.concat(insertLink(blob, regex, plugin));
}
}
}
}
}

matches = matches
.filter(result => result !== undefined)
.map(({ link, urls }) => {
const urlsSorted = helperSortUrls(urls, link.innerText);

return {
link,
urls: normaliseResolverResults(urlsSorted),
};
});

// Prefetch live resolver results in background
requestIdleCallback(() => {
loader(matches);
});
}

function watch(viewSpy) {
const elements = [
...document.getElementsByClassName('js-diff-load-container'),
...document.getElementsByClassName('js-diff-progressive-container'),
];

elements.forEach(element => {
viewSpy.observe(element, {
childList: true,
});
});
}

function init() {
debugMode(storage.get('debugMode'));
notification();

injection(() => {
if (!blobReader.hasBlobs()) {
return false;
}

run(document);
});

const viewSpy = new MutationObserver(mutationRecords => {
console.log('callback'); // check how often this gets invoked
mutationRecords.forEach(mutationRecord => {
if (mutationRecord.addedNodes.length > 0) {
run(this, mutationRecord.target);
watch(viewSpy);
}
});
});

watch(viewSpy);
}

storage.load().then(init);
90 changes: 0 additions & 90 deletions packages/core/octo-linker.js

This file was deleted.