code formatting
This commit is contained in:
parent
6133da21d4
commit
1660dabc91
|
@ -21,17 +21,14 @@ const mobileView = $("html").hasClass("mobile-view"),
|
||||||
class: "d-toc-anchor-link",
|
class: "d-toc-anchor-link",
|
||||||
html: linkIcon
|
html: linkIcon
|
||||||
});
|
});
|
||||||
|
|
||||||
return link;
|
return link;
|
||||||
},
|
},
|
||||||
dtocMobile = () => {
|
dtocMobile = () => {
|
||||||
$(".d-toc").toggleClass("d-toc-mobile");
|
$(".d-toc").toggleClass("d-toc-mobile");
|
||||||
};
|
};
|
||||||
|
|
||||||
I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
themePrefix("topic_will_contain_a_table_of_contents")
|
themePrefix("topic_will_contain_a_table_of_contents")
|
||||||
);
|
);
|
||||||
|
|
||||||
(function(dToc) {
|
(function(dToc) {
|
||||||
dToc($, window);
|
dToc($, window);
|
||||||
$.widget("discourse.dToc", {
|
$.widget("discourse.dToc", {
|
||||||
|
@ -39,66 +36,51 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
this.generateDtoc();
|
this.generateDtoc();
|
||||||
this.setEventHandlers();
|
this.setEventHandlers();
|
||||||
},
|
},
|
||||||
|
|
||||||
generateDtoc: function() {
|
generateDtoc: function() {
|
||||||
let self = this,
|
let self = this,
|
||||||
primaryHeadings = $(this.options.cooked).find(
|
primaryHeadings = $(this.options.cooked).find(
|
||||||
this.options.selectors.substr(0, this.options.selectors.indexOf(","))
|
this.options.selectors.substr(0, this.options.selectors.indexOf(","))
|
||||||
);
|
);
|
||||||
|
|
||||||
self.element.addClass("d-toc");
|
self.element.addClass("d-toc");
|
||||||
|
|
||||||
primaryHeadings.each(function(index) {
|
primaryHeadings.each(function(index) {
|
||||||
let selectors = self.options.selectors,
|
let selectors = self.options.selectors,
|
||||||
ul = $("<ul/>", {
|
ul = $("<ul/>", {
|
||||||
id: `d-toc-top-heading-${index}`,
|
id: `d-toc-top-heading-${index}`,
|
||||||
class: "d-toc-heading"
|
class: "d-toc-heading"
|
||||||
});
|
});
|
||||||
|
|
||||||
ul.append(self.nestElements($(this), index));
|
ul.append(self.nestElements($(this), index));
|
||||||
|
|
||||||
self.element.append(ul);
|
self.element.append(ul);
|
||||||
|
|
||||||
$(this)
|
$(this)
|
||||||
.nextUntil(this.nodeName.toLowerCase())
|
.nextUntil(this.nodeName.toLowerCase())
|
||||||
.each(function() {
|
.each(function() {
|
||||||
headings = $(this).find(selectors).length
|
headings = $(this).find(selectors).length
|
||||||
? $(this).find(selectors)
|
? $(this).find(selectors)
|
||||||
: $(this).filter(selectors);
|
: $(this).filter(selectors);
|
||||||
|
|
||||||
headings.each(function() {
|
headings.each(function() {
|
||||||
self.subheadings.call(this, self, ul);
|
self.subheadings.call(this, self, ul);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
nestElements: function(self, index) {
|
nestElements: function(self, index) {
|
||||||
let unique = self.attr("id"),
|
let unique = self.attr("id"),
|
||||||
text = self.text(),
|
text = self.text(),
|
||||||
arr,
|
arr,
|
||||||
item;
|
item;
|
||||||
|
|
||||||
arr = $.grep(items, i => i === text);
|
arr = $.grep(items, i => i === text);
|
||||||
|
|
||||||
items.push(text);
|
items.push(text);
|
||||||
|
|
||||||
item = $("<li/>", {
|
item = $("<li/>", {
|
||||||
class: "d-toc-item",
|
class: "d-toc-item",
|
||||||
"data-d-toc": unique
|
"data-d-toc": unique
|
||||||
});
|
});
|
||||||
|
|
||||||
item.append(
|
item.append(
|
||||||
$("<a/>", {
|
$("<a/>", {
|
||||||
text: text
|
text: text
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
self.attr({ "data-d-toc": unique });
|
self.attr({ "data-d-toc": unique });
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
},
|
},
|
||||||
|
|
||||||
subheadings: function(self, ul) {
|
subheadings: function(self, ul) {
|
||||||
let index = $(this).index(self.options.selectors),
|
let index = $(this).index(self.options.selectors),
|
||||||
previousHeader = $(self.options.selectors).eq(index - 1),
|
previousHeader = $(self.options.selectors).eq(index - 1),
|
||||||
|
@ -106,7 +88,6 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
.prop("tagName")
|
.prop("tagName")
|
||||||
.charAt(1),
|
.charAt(1),
|
||||||
previousTagName = +previousHeader.prop("tagName").charAt(1);
|
previousTagName = +previousHeader.prop("tagName").charAt(1);
|
||||||
|
|
||||||
if (currentTagName < previousTagName) {
|
if (currentTagName < previousTagName) {
|
||||||
self.element
|
self.element
|
||||||
.find(`.d-toc-subheading[data-tag="${currentTagName}"]`)
|
.find(`.d-toc-subheading[data-tag="${currentTagName}"]`)
|
||||||
|
@ -129,29 +110,22 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
.append(self.nestElements($(this), index));
|
.append(self.nestElements($(this), index));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
setEventHandlers: function() {
|
setEventHandlers: function() {
|
||||||
let self = this,
|
let self = this,
|
||||||
scrollThrottle = mobileView ? 10 : 50,
|
scrollThrottle = mobileView ? 10 : 50,
|
||||||
dTocTimeout;
|
dTocTimeout;
|
||||||
|
|
||||||
this.element.on("click.d-toc", "li", function() {
|
this.element.on("click.d-toc", "li", function() {
|
||||||
self.element.find(".d-toc-active").removeClass("d-toc-active");
|
self.element.find(".d-toc-active").removeClass("d-toc-active");
|
||||||
|
|
||||||
$(this).addClass("d-toc-active");
|
$(this).addClass("d-toc-active");
|
||||||
|
|
||||||
if (!mobileView) {
|
if (!mobileView) {
|
||||||
let elem = $(`li[data-d-toc="${$(this).attr("data-d-toc")}"]`);
|
let elem = $(`li[data-d-toc="${$(this).attr("data-d-toc")}"]`);
|
||||||
self.triggerShow(elem);
|
self.triggerShow(elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.scrollTo($(this));
|
self.scrollTo($(this));
|
||||||
|
|
||||||
if (mobileView) {
|
if (mobileView) {
|
||||||
$("#d-toc").removeClass("d-toc-mobile");
|
$("#d-toc").removeClass("d-toc-mobile");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$(window).on("scroll.d-toc", function() {
|
$(window).on("scroll.d-toc", function() {
|
||||||
if (!dTocTimeout) {
|
if (!dTocTimeout) {
|
||||||
dTocTimeout = setTimeout(function() {
|
dTocTimeout = setTimeout(function() {
|
||||||
|
@ -160,12 +134,10 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
.done(function() {
|
.done(function() {
|
||||||
let winScrollTop = $(window).scrollTop(),
|
let winScrollTop = $(window).scrollTop(),
|
||||||
elem;
|
elem;
|
||||||
|
|
||||||
let closestAnchorDistance = null,
|
let closestAnchorDistance = null,
|
||||||
closestAnchorIdx = null,
|
closestAnchorIdx = null,
|
||||||
anchors = $(self.options.cooked).find("[data-d-toc]"),
|
anchors = $(self.options.cooked).find("[data-d-toc]"),
|
||||||
anchorText;
|
anchorText;
|
||||||
|
|
||||||
anchors.each(function(idx) {
|
anchors.each(function(idx) {
|
||||||
let distance = Math.abs($(this).offset().top - winScrollTop);
|
let distance = Math.abs($(this).offset().top - winScrollTop);
|
||||||
if (
|
if (
|
||||||
|
@ -178,19 +150,14 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
anchorText = $(anchors[closestAnchorIdx]).attr("data-d-toc");
|
anchorText = $(anchors[closestAnchorIdx]).attr("data-d-toc");
|
||||||
|
|
||||||
elem = $(`li[data-d-toc="${anchorText}"]`);
|
elem = $(`li[data-d-toc="${anchorText}"]`);
|
||||||
|
|
||||||
if (elem.length) {
|
if (elem.length) {
|
||||||
self.element
|
self.element
|
||||||
.find(".d-toc-active")
|
.find(".d-toc-active")
|
||||||
.removeClass("d-toc-active");
|
.removeClass("d-toc-active");
|
||||||
|
|
||||||
elem.addClass("d-toc-active");
|
elem.addClass("d-toc-active");
|
||||||
}
|
}
|
||||||
|
|
||||||
self.triggerShow(elem, true);
|
self.triggerShow(elem, true);
|
||||||
});
|
});
|
||||||
dTocTimeout = null;
|
dTocTimeout = null;
|
||||||
|
@ -198,7 +165,6 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
show: function(elem) {
|
show: function(elem) {
|
||||||
if (!elem.is(":visible")) {
|
if (!elem.is(":visible")) {
|
||||||
if (
|
if (
|
||||||
|
@ -215,13 +181,11 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
}
|
}
|
||||||
elem.slideDown("medium");
|
elem.slideDown("medium");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mobileView) {
|
if (!mobileView) {
|
||||||
if (elem.parent().is(".d-toc-heading")) {
|
if (elem.parent().is(".d-toc-heading")) {
|
||||||
$(".d-toc-subheading")
|
$(".d-toc-subheading")
|
||||||
.not(elem)
|
.not(elem)
|
||||||
.slideUp("medium");
|
.slideUp("medium");
|
||||||
|
|
||||||
$(".d-toc-subheading")
|
$(".d-toc-subheading")
|
||||||
.not(
|
.not(
|
||||||
elem
|
elem
|
||||||
|
@ -233,7 +197,6 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
triggerShow: function(elem, scroll) {
|
triggerShow: function(elem, scroll) {
|
||||||
if (
|
if (
|
||||||
elem.parent().is(".d-toc-heading") ||
|
elem.parent().is(".d-toc-heading") ||
|
||||||
|
@ -244,14 +207,11 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
this.show(elem.parent(), scroll);
|
this.show(elem.parent(), scroll);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
setOptions: function() {
|
setOptions: function() {
|
||||||
$.Widget.prototype._setOptions.apply(this, arguments);
|
$.Widget.prototype._setOptions.apply(this, arguments);
|
||||||
},
|
},
|
||||||
|
|
||||||
scrollTo: function(elem) {
|
scrollTo: function(elem) {
|
||||||
let currentDiv = $(`[data-d-toc="${elem.attr("data-d-toc")}"]`);
|
let currentDiv = $(`[data-d-toc="${elem.attr("data-d-toc")}"]`);
|
||||||
|
|
||||||
$("html").animate(
|
$("html").animate(
|
||||||
{
|
{
|
||||||
scrollTop: currentDiv.offset().top + "px"
|
scrollTop: currentDiv.offset().top + "px"
|
||||||
|
@ -263,20 +223,14 @@ I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = I18n.t(
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})(() => {});
|
})(() => {});
|
||||||
|
|
||||||
$.fn.dtoc = $elem => {
|
$.fn.dtoc = $elem => {
|
||||||
run.scheduleOnce("sync", () => {
|
run.scheduleOnce("sync", () => {
|
||||||
if ($elem.hasClass("d-editor-preview")) return;
|
if ($elem.hasClass("d-editor-preview")) return;
|
||||||
if (!$elem.parents("article#post_1").length) return
|
if (!$elem.parents("article#post_1").length) return;
|
||||||
const dToc = $elem.find(`[data-theme-toc="true"]`);
|
const dToc = $elem.find(`[data-theme-toc="true"]`);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (!dToc.length) return this;
|
if (!dToc.length) return this;
|
||||||
|
|
||||||
const body = $elem;
|
const body = $elem;
|
||||||
|
|
||||||
body.find("div, aside, blockquote, article").each(function() {
|
body.find("div, aside, blockquote, article").each(function() {
|
||||||
$(this)
|
$(this)
|
||||||
.children("h1,h2,h3,h4,h5,h6")
|
.children("h1,h2,h3,h4,h5,h6")
|
||||||
|
@ -286,9 +240,7 @@ $.fn.dtoc = $elem => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
let dTocHeadingSelectors = "h1,h2,h3,h4,h5,h6";
|
let dTocHeadingSelectors = "h1,h2,h3,h4,h5,h6";
|
||||||
|
|
||||||
if (!body.has(">h1").length) {
|
if (!body.has(">h1").length) {
|
||||||
dTocHeadingSelectors = "h2,h3,h4,h5,h6";
|
dTocHeadingSelectors = "h2,h3,h4,h5,h6";
|
||||||
if (!body.has(">h2").length) {
|
if (!body.has(">h2").length) {
|
||||||
|
@ -304,24 +256,19 @@ $.fn.dtoc = $elem => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
body.find(dTocHeadingSelectors).each(function() {
|
body.find(dTocHeadingSelectors).each(function() {
|
||||||
if ($(this).hasClass("d-toc-ignore")) return;
|
if ($(this).hasClass("d-toc-ignore")) return;
|
||||||
|
|
||||||
let heading = $(this),
|
let heading = $(this),
|
||||||
id = heading.attr("id") || "";
|
id = heading.attr("id") || "";
|
||||||
|
|
||||||
if (id.length) {
|
if (id.length) {
|
||||||
heading.attr("id", id);
|
heading.attr("id", id);
|
||||||
heading.append(createAnchors(id));
|
heading.append(createAnchors(id));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
id = cleanUp(heading.text());
|
id = cleanUp(heading.text());
|
||||||
heading.attr("id", id);
|
heading.attr("id", id);
|
||||||
heading.append(createAnchors(id));
|
heading.append(createAnchors(id));
|
||||||
});
|
});
|
||||||
|
|
||||||
body
|
body
|
||||||
.addClass("d-toc-cooked")
|
.addClass("d-toc-cooked")
|
||||||
.prepend(
|
.prepend(
|
||||||
|
@ -344,33 +291,27 @@ $.fn.dtoc = $elem => {
|
||||||
)
|
)
|
||||||
.parents(".topic-post")
|
.parents(".topic-post")
|
||||||
.addClass("d-toc-post");
|
.addClass("d-toc-post");
|
||||||
|
|
||||||
$("#d-toc").dToc({
|
$("#d-toc").dToc({
|
||||||
cooked: body,
|
cooked: body,
|
||||||
selectors: dTocHeadingSelectors
|
selectors: dTocHeadingSelectors
|
||||||
});
|
});
|
||||||
|
|
||||||
$(".d-toc-post").on(
|
$(".d-toc-post").on(
|
||||||
"click.toggleDtoc",
|
"click.toggleDtoc",
|
||||||
".d-toc-toggle, .d-toc-close",
|
".d-toc-toggle, .d-toc-close",
|
||||||
dtocMobile
|
dtocMobile
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
$(".selected-posts")
|
$(".selected-posts")
|
||||||
.next()
|
.next()
|
||||||
.addClass("d-toc-timeline");
|
.addClass("d-toc-timeline");
|
||||||
};
|
};
|
||||||
|
|
||||||
api.decorateCooked($elem => $elem.dtoc($elem));
|
api.decorateCooked($elem => $elem.dtoc($elem));
|
||||||
|
|
||||||
api.modifyClass("component:topic-timeline", {
|
api.modifyClass("component:topic-timeline", {
|
||||||
willDestroyElement() {
|
willDestroyElement() {
|
||||||
$(window).off("scroll.d-toc");
|
$(window).off("scroll.d-toc");
|
||||||
$(".d-toc-post").off("click.toggleDtoc");
|
$(".d-toc-post").off("click.toggleDtoc");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
api.onAppEvent("topic:current-post-changed", post => {
|
api.onAppEvent("topic:current-post-changed", post => {
|
||||||
if (!$(".d-toc-timeline").length) return;
|
if (!$(".d-toc-timeline").length) return;
|
||||||
if (post.post.post_number === 1) {
|
if (post.post.post_number === 1) {
|
||||||
|
@ -381,7 +322,6 @@ api.onAppEvent("topic:current-post-changed", post => {
|
||||||
$(".d-toc-toggle").fadeOut();
|
$(".d-toc-toggle").fadeOut();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
api.addToolbarPopupMenuOptionsCallback(() => {
|
api.addToolbarPopupMenuOptionsCallback(() => {
|
||||||
composerController = api.container.lookup("controller:composer");
|
composerController = api.container.lookup("controller:composer");
|
||||||
return {
|
return {
|
||||||
|
@ -391,7 +331,6 @@ api.addToolbarPopupMenuOptionsCallback(() => {
|
||||||
condition: !composerController.get("model.canCategorize")
|
condition: !composerController.get("model.canCategorize")
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
api.modifyClass("controller:composer", {
|
api.modifyClass("controller:composer", {
|
||||||
actions: {
|
actions: {
|
||||||
insertDtoc() {
|
insertDtoc() {
|
||||||
|
|
Loading…
Reference in New Issue