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

Implement sidebars on react pages #1957

Closed
Closed
Changes from 22 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0dfa261
initial commit: got sidebar to appear
marcustyphoon Aug 11, 2020
834f667
add rough css for react
marcustyphoon Aug 11, 2020
9bcb203
add svg for "carrot" indicator
marcustyphoon Aug 18, 2020
7b6c2a2
cleaned up construct_react code
marcustyphoon Aug 12, 2020
1cc9ee5
move updated sidebar to XKit.interface.react.sidebar and revert old one
marcustyphoon Aug 12, 2020
e62ce1a
fix sidebar selector for /blog/ page
marcustyphoon Aug 12, 2020
0883ecf
place sidebar below navigation on "channel" pages
marcustyphoon Aug 13, 2020
9a56a97
cleaned up css
marcustyphoon Aug 13, 2020
981c8c0
removed legacy fallback code from sidebar.react
marcustyphoon Aug 14, 2020
c0ae88c
behavior fixes
marcustyphoon Aug 14, 2020
ee64114
moved css variable
marcustyphoon Aug 14, 2020
64ee3a5
add sticky sidebar
marcustyphoon Aug 18, 2020
c561b4c
fix the race condition with a MutationObserver
marcustyphoon Aug 18, 2020
6b7a45d
version bump
marcustyphoon Aug 18, 2020
1614a85
Merge remote-tracking branch 'upstream/master' into update-interface-…
marcustyphoon Aug 19, 2020
40953cd
version bump again
marcustyphoon Aug 19, 2020
356b329
fixes for legacy sidebars showing up and async-init-fires-multiple-times
marcustyphoon Aug 22, 2020
395bfdb
oops. better fix for async behavior that doesn't break everything
marcustyphoon Aug 22, 2020
82a29f9
move sticky version into seperate PR
marcustyphoon Aug 22, 2020
a2da7b8
reduce variable scope
marcustyphoon Sep 1, 2020
5046172
Merge remote-tracking branch 'upstream/master' into update-interface-…
marcustyphoon Sep 10, 2020
32253a0
version bump
marcustyphoon Sep 10, 2020
c544af2
fix indentation
marcustyphoon Sep 18, 2020
46a50e0
version bump
marcustyphoon Sep 19, 2020
8775607
empty commit to trigger new CI
marcustyphoon Nov 19, 2020
86e4207
Merge branch 'master' into update-interface-dot-sidebar
marcustyphoon Nov 19, 2020
8bd9a24
Merge branch 'master' into update-interface-dot-sidebar
marcustyphoon Mar 31, 2021
b8b548b
version bump
marcustyphoon Mar 31, 2021
7664cb3
update css for tumblr color change
marcustyphoon Apr 2, 2021
ef8ad72
Merge remote-tracking branch 'upstream/master' into update-interface-…
marcustyphoon Jun 23, 2021
39a4224
version bump
marcustyphoon Jun 23, 2021
cb11ebd
Merge branch 'master' into update-interface-dot-sidebar
marcustyphoon Sep 15, 2022
eef8acb
version bump
marcustyphoon Sep 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
180 changes: 178 additions & 2 deletions Extensions/xkit_patches.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//* TITLE XKit Patches **//
//* VERSION 7.4.10 **//
//* VERSION 7.4.11 **//
//* DESCRIPTION Patches framework **//
//* DEVELOPER new-xkit **//

Expand Down Expand Up @@ -500,10 +500,12 @@ XKit.extensions.xkit_patches = new Object({
`.post_brick .post_controls .post_controls_inner {
white-space: nowrap;
}`,
"xkit_patches");
"xkit_patches");

XKit.interface.sidebar = {
init: function() {
if (XKit.page.react) { return; }

const html = `<div id="xkit_sidebar"></div>`;
const priority = [
$(".small_links"),
Expand Down Expand Up @@ -1118,6 +1120,180 @@ XKit.extensions.xkit_patches = new Object({
},
};

XKit.interface.react.sidebar = {

css_added: false,

react_sidebar_css: `
.xkit--react .controls_section li {
list-style-type: none;
}

.xkit--react .controls_section {
padding: 0;
margin-bottom: 38px;
}

.xkit--react .controls_section .controls_section_item {
border-top: 1px solid var(--transparent-white-7);
position: relative;
color: var(--transparent-white-65);
line-height: 30px;
font-weight: 700;
}

.xkit--react .controls_section .controls_section_item:hover {
background-color: var(--transparent-white-7);
}

.xkit--react .controls_section .hide_overflow {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 88%;
padding-left: 10px;
}

.xkit--react .controls_section a {
text-decoration: none;
}

.xkit--react .controls_section a .count {
position: absolute;
top: 0;
right: 10px;
font-size: 13px;
font-weight: 400;
display: flex;
}

.xkit--react .small_links {
border-top: 1px solid var(--transparent-white-7);
overflow: hidden;
}

.xkit--react .small_links a {
padding: 11px 13px 0;
color: var(--transparent-white-65);
font-size: 11px;
}

.xkit--react .small_links a:first-child { float: left; }
.xkit--react .small_links a:nth-child(2) { float: right; }
`,
marcustyphoon marked this conversation as resolved.
Show resolved Hide resolved

init: async function() {
await XKit.css_map.getCssMap();

if ($("#xkit_react_sidebar").length) { return; }
//everything after this check is synchronous

if (!this.css_added) {
XKit.tools.add_css(this.react_sidebar_css, "xkit_patches");
this.css_added = true;
}

const html = `<div id="xkit_react_sidebar"></div>`;

//inject the xkit sidebar after the navigation on tumblr.com/blog/myblogname pages
const $navSidebarItem = $(XKit.css_map.keyToCss("sideBar")).first().parent();
if ($navSidebarItem.length) {
$navSidebarItem.after(html);
return;
}

//otherwise, inject the xkit sidebar at the top
const $sidebarContainer = $(XKit.css_map.keyToCss("sidebar")).find("> aside");
$sidebarContainer.prepend(html);


//fix for "after navigation" race condition:
//detect if we loaded before the sidebar navigation and wait for it
const $loadingPlaceholder = $(XKit.css_map.descendantSelector("sidebar", "loadingPlaceholder"));
if ($loadingPlaceholder.length) {

const loadingObserver = new MutationObserver(function(mutations) {
const $newNavSidebarItem = $(XKit.css_map.keyToCss("sideBar")).first().parent();
if ($newNavSidebarItem.length) {
$newNavSidebarItem.after($("#xkit_react_sidebar"));
loadingObserver.disconnect();
}
});
loadingObserver.observe($sidebarContainer.get(0), {childList: true, subtree: true});
}
this.running = false;
},

/**
* Constructs HTML to add to the sidebar with react-specific CSS.
* Primarily used by add, but can be used directly for custom positioning.
* @param {Object} section
* @param {String} section.id - The element ID for the whole sidebar section
* @param {String} [section.title] - Visible header text of the sidebar section
* @param {Object[]} [section.items] - Array of objects containing button data
* @param {String} section.items[].id - Button element ID
* @param {String} section.items[].text - Visible button text
* @param {Number/String} [section.items[].count] - Text to be displayed as a counter on the button
Copy link

Choose a reason for hiding this comment

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

Another nitpick that doesn't require changing: Number|String is more commonly used. Technically in a type annotation like this it would be number|string since Number and String refer to the boxed versions of the primitive number and string types.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, all of the JSDoc—actually, most of the green code here—was copy-paste :D That did look odd though.

* @param {Boolean} [section.items[].carrot] - Whether to put a right-facing arrow on the button (shouldn't be combined with count)
* @param {Object[]} [section.small] - Array of objects containing small link data (shouldn't contain more than two)
* @param {String} section.small[].id - Button element ID
* @param {String} section.small[].text - Visible button text
* @return {String} Plug-ready sidebar controls section HTML
*/
construct: async function(section) {
section.items = section.items || [];
section.small = section.small || [];

let html = `<ul id="${section.id}" class="controls_section">`;
if (section.title) {
await XKit.css_map.getCssMap();
const sidebarTitleClasses = XKit.css_map.keyToClasses("sidebarTitle").join(" ");

html += `<li class="${sidebarTitleClasses}">${section.title}</li>`;
}

const carrot = '<svg height="30px" fill="var(--transparent-white-65)" viewBox="0 -16 13 52.1"><path d="M0,2.9l7.2,7.2l-7.1,7.1L3,20.1l7.1-7.1l2.9-2.9L2.9,0L0,2.9"></path></svg>';

for (let item of section.items) {
html += `
<li class="controls_section_item">
<a id="${item.id}" class="control-item control-anchor" style="cursor:pointer">
<div class="hide_overflow">
${item.text}
</div>
<span class="count">${item.count || ""}${(item.carrot ? carrot : "")}</span>
</a>
</li>`;
}

if (section.small.length !== 0) {
html += '<div class="small_links">';
for (let item of section.small) {
html += `<a id="${item.id}" style="cursor:pointer">${item.text}</a>`;
}
html += "</div>";
}
html += "</ul>";

return html;
},

/**
* Shortcut command for constructing and applying controls sections
* @param {Object} section - see construct's documentation
*/
add: async function(section) {
if (!XKit.page.react) { return; }
if (!$("#xkit_react_sidebar").length) {
await this.init();
}

$("#xkit_react_sidebar").append(await this.construct(section));
},

remove: id => $(`#${id}, #${id} + .small_links`).remove()
};

XKit.interface.async_form_key = async function() {
const request = await fetch('https://www.tumblr.com/settings/dashboard');
const meta_tag = (await request.text()).match(
Expand Down