From 23616d301f56db9a8cd6095e6e1196db4bc92720 Mon Sep 17 00:00:00 2001 From: zbycz Date: Wed, 3 Feb 2016 15:37:10 +0100 Subject: [PATCH 1/3] favorites panel --- page.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/page.js b/page.js index 8f00d0d..1f64468 100644 --- a/page.js +++ b/page.js @@ -5,3 +5,75 @@ $(document).on('pjax:end', function () { return document.dispatchEvent(e); }); + + +var panel = $('div.gm_panel'); + +if (panel.length === 0) { + panel = $('
').html( + "add this page" + + "Favorites" + + "" + + "My events viewer" + + "" + ); + $('body').append(panel); + + setInterval(gm_refresh_favorites, 2000); + gm_refresh_favorites(); + + panel.on('click', 'a.add', function () { + var path = location.pathname.split('/'); // /winsite/iprace_fe/issues/779 + var text = prompt('Edit title:', path[2] + ' ' + document.title); + + if (!text) return; + + var items = gm_get_favorites(); + items.push({ + loc: location.toString(), + title: document.title, + text: text + }); + gm_set_favorites(items); + gm_refresh_favorites(); + + return false; + }); + + panel.on('click', 'span.rem', function () { + var items = gm_get_favorites(); + items.splice($(this).attr('data-i'), 1); + gm_set_favorites(items); + gm_refresh_favorites(); + }); +} + + +function gm_refresh_favorites() { + var ul = ''; + var items = gm_get_favorites(); + for (var i in items) { + var item = items[i]; + //var text = decodeURIComponent(item.loc.replace(/https?:\/\/[^\/]+\//, '')).replace('+', ' '); + ul += '
  • × ' + item.text + '
  • ' + } + panel.find('ul').html(ul); +} + + +function gm_get_favorites() { + return JSON.parse(localStorage.getItem('gm_panel')) || []; +} +function gm_set_favorites(items) { + localStorage.setItem('gm_panel', JSON.stringify(items)); +} From d2d002f7c09944c71adceb59fbe449d8e62c279f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Zbytovsky=CC=81?= Date: Fri, 1 Apr 2016 21:56:18 +0200 Subject: [PATCH 2/3] favorites panel - edit instead of remove --- page.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/page.js b/page.js index 1f64468..04a80c1 100644 --- a/page.js +++ b/page.js @@ -13,17 +13,17 @@ if (panel.length === 0) { panel = $('
    ').html( "add this page" + "Favorites" + - "" + - "My events viewer" + + "" + "" ); @@ -50,29 +50,38 @@ if (panel.length === 0) { return false; }); - panel.on('click', 'span.rem', function () { + panel.on('click', 'span.edit', function () { var items = gm_get_favorites(); - items.splice($(this).attr('data-i'), 1); + var i = $(this).attr('data-i'); + + var pr = window.prompt('Set new title OR set blank to delete', items[i].text); + if (pr === null) return; + else if (pr === '') items.splice(i, 1); + else items[i].text = pr; + gm_set_favorites(items); gm_refresh_favorites(); }); } - +var lastString = ''; function gm_refresh_favorites() { var ul = ''; var items = gm_get_favorites(); for (var i in items) { var item = items[i]; //var text = decodeURIComponent(item.loc.replace(/https?:\/\/[^\/]+\//, '')).replace('+', ' '); - ul += '
  • × ' + item.text + '
  • ' + ul += '
  • ' + item.text + '
  • ' + } + if (ul != lastString) { + lastString = ul; + panel.find('ul.favorites').html(ul); } - panel.find('ul').html(ul); } function gm_get_favorites() { - return JSON.parse(localStorage.getItem('gm_panel')) || []; + return JSON.parse(localStorage.getItem('gm_panel')) || [{loc: 'https://rawgit.com/zbycz/github-events-viewer/master/github-event-viewer.html', text: 'My GH events', title:'default entry'}]; } function gm_set_favorites(items) { localStorage.setItem('gm_panel', JSON.stringify(items)); From 934cddaffb773ff20707bbc470508fec260d93db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Zbytovsky=CC=81?= Date: Fri, 1 Apr 2016 21:58:46 +0200 Subject: [PATCH 3/3] Show markdown outline in side panel --- page.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/page.js b/page.js index 04a80c1..4ceadef 100644 --- a/page.js +++ b/page.js @@ -3,10 +3,14 @@ $(document).on('pjax:end', function () { var e = document.createEvent('Events'); e.initEvent('_pjax:end', !'bubbles', !'cancelable'); + updateOutlinesInPanel() + return document.dispatchEvent(e); }); +// Sidebar panel originaly by github.com/zbycz + var panel = $('div.gm_panel'); if (panel.length === 0) { @@ -25,7 +29,9 @@ if (panel.length === 0) { ".gm_panel li { margin: .4em 0 0 1em; }" + ".gm_panel li span.edit { display: none; position: absolute; right:5px; cursor: pointer; }" + ".gm_panel li:hover span { display: inline; }" + - "" + "" + + "
    " + ); $('body').append(panel); @@ -86,3 +92,78 @@ function gm_get_favorites() { function gm_set_favorites(items) { localStorage.setItem('gm_panel', JSON.stringify(items)); } + + + + +// Copyright (c) 2015 Dan Kaplun +// The MIT License (MIT) +// from https://github.com/dbkaplun/github-markdown-outline-extension/blob/master/index.js +// very hackily edited by zbycz to add it in panel + +getHeaderLevel.REGEXP = /h(\d)/i +function getHeaderLevel ($h) { + var level = Number(((($h || {}).tagName || '').match(getHeaderLevel.REGEXP) || [])[1]) + return isNaN(level) ? undefined : level +} + +var headerSels = [] +for (var l = 1; l <= 6; l++) headerSels.push('h'+l) +var headerSel = headerSels.join(', ') +var anchorSel = 'a[id]' + +function updateOutlinesInPanel() { + var $container = document.createElement('div') + $container.classList.add('gm_outline') + + Array.from(document.querySelectorAll('.markdown-body')).forEach($md => { + var $headers = Array.from($md.querySelectorAll(headerSel)) + + + var $b = document.createElement('b') + $b.innerText = "Outline" + $container.appendChild($b) + + var $outline = document.createElement('ul') + //$outline.classList.add('__github-markdown-outline') + $container.appendChild($outline) + + // generate outline from headers + $headers.forEach($h => { + var level = getHeaderLevel($h) + if (!level) return + var $ul = $outline, $li, $child + for (var l = 1; l < level; l++) { + $li = $ul.lastChild || $ul.appendChild(document.createElement('li')) + $child = $li.lastChild || {} + $ul = $child.tagName === 'UL' + ? $child + : $li.appendChild(document.createElement('ul')) + } + var $topic = $ul + .appendChild(document.createElement('li')) + .appendChild(document.createElement('a')) + $topic.innerText = $h.innerText + $topic.href = `#${$h.querySelector(anchorSel).id.replace(/^user-content-/, '')}` + }) + + // find all sublists with one item and replace with contents + Array.from($container.querySelectorAll('ul')).forEach($ul => { + var $parent = $ul.parentNode + var $li = $ul.firstChild + var $child = $li.firstChild + if ($li !== $ul.lastChild || $child.tagName !== 'UL') return + while ($child) { + $parent.insertBefore($child, $ul.nextSibling) // inserts to end of list if $ul.nextSibling is null + $child = $child.nextSibling + } + $parent.removeChild($ul) + }) + + }) + + //$md.insertBefore($container, $md.firstChild) + panel.find('.gm_outline').replaceWith($container); +} + +updateOutlinesInPanel(); \ No newline at end of file