parent
14432d0d4b
commit
ee0d7478fd
|
@ -41,7 +41,7 @@
|
||||||
return link;
|
return link;
|
||||||
};
|
};
|
||||||
|
|
||||||
const setUpTocItem = function(item) {
|
const setUpTocItem = function (item) {
|
||||||
const unique = item.attr("id");
|
const unique = item.attr("id");
|
||||||
const text = item.text();
|
const text = item.text();
|
||||||
|
|
||||||
|
@ -59,15 +59,15 @@
|
||||||
return tocItem;
|
return tocItem;
|
||||||
};
|
};
|
||||||
|
|
||||||
(function(dToc) {
|
(function (dToc) {
|
||||||
dToc($, window);
|
dToc($, window);
|
||||||
$.widget("discourse.dToc", {
|
$.widget("discourse.dToc", {
|
||||||
_create: function() {
|
_create: function () {
|
||||||
this.generateDtoc();
|
this.generateDtoc();
|
||||||
this.setEventHandlers();
|
this.setEventHandlers();
|
||||||
},
|
},
|
||||||
|
|
||||||
generateDtoc: function() {
|
generateDtoc: function () {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
const primaryHeadings = $(this.options.cooked).find(
|
const primaryHeadings = $(this.options.cooked).find(
|
||||||
|
@ -76,7 +76,7 @@
|
||||||
|
|
||||||
self.element.addClass("d-toc");
|
self.element.addClass("d-toc");
|
||||||
|
|
||||||
primaryHeadings.each(function(index) {
|
primaryHeadings.each(function (index) {
|
||||||
const selectors = self.options.selectors,
|
const selectors = self.options.selectors,
|
||||||
ul = $("<ul/>", {
|
ul = $("<ul/>", {
|
||||||
id: `d-toc-top-heading-${index}`,
|
id: `d-toc-top-heading-${index}`,
|
||||||
|
@ -88,26 +88,24 @@
|
||||||
|
|
||||||
$(this)
|
$(this)
|
||||||
.nextUntil(this.nodeName.toLowerCase())
|
.nextUntil(this.nodeName.toLowerCase())
|
||||||
.each(function() {
|
.each(function () {
|
||||||
const headings = $(this).find(selectors).length
|
const headings = $(this).find(selectors).length
|
||||||
? $(this).find(selectors)
|
? $(this).find(selectors)
|
||||||
: $(this).filter(selectors);
|
: $(this).filter(selectors);
|
||||||
|
|
||||||
headings.each(function() {
|
headings.each(function () {
|
||||||
self.nestTocItem.call(this, self, ul);
|
self.nestTocItem.call(this, self, ul);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
nestTocItem: function(self, ul) {
|
nestTocItem: function (self, ul) {
|
||||||
const index = $(this).index(self.options.selectors);
|
const index = $(this).index(self.options.selectors);
|
||||||
const previousHeader = $(self.options.selectors).eq(index - 1);
|
const previousHeader = $(self.options.selectors).eq(index - 1);
|
||||||
const previousTagName = previousHeader.prop("tagName").charAt(1);
|
const previousTagName = previousHeader.prop("tagName").charAt(1);
|
||||||
|
|
||||||
const currentTagName = $(this)
|
const currentTagName = $(this).prop("tagName").charAt(1);
|
||||||
.prop("tagName")
|
|
||||||
.charAt(1);
|
|
||||||
|
|
||||||
if (currentTagName < previousTagName) {
|
if (currentTagName < previousTagName) {
|
||||||
self.element
|
self.element
|
||||||
|
@ -132,14 +130,14 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
setEventHandlers: function() {
|
setEventHandlers: function () {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
const dtocMobile = () => {
|
const dtocMobile = () => {
|
||||||
$(".d-toc").toggleClass("d-toc-mobile");
|
$(".d-toc").toggleClass("d-toc-mobile");
|
||||||
};
|
};
|
||||||
|
|
||||||
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) {
|
||||||
|
@ -166,16 +164,16 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
highlightItemsOnScroll: self => {
|
highlightItemsOnScroll: self => {
|
||||||
$('html, body')
|
$("html, body")
|
||||||
.promise()
|
.promise()
|
||||||
.done(function() {
|
.done(function () {
|
||||||
const winScrollTop = $(window).scrollTop();
|
const winScrollTop = $(window).scrollTop();
|
||||||
const anchors = $(self.options.cooked).find("[data-d-toc]");
|
const anchors = $(self.options.cooked).find("[data-d-toc]");
|
||||||
|
|
||||||
let closestAnchorDistance = null;
|
let closestAnchorDistance = null;
|
||||||
let closestAnchorIdx = null;
|
let closestAnchorIdx = null;
|
||||||
|
|
||||||
anchors.each(function(idx) {
|
anchors.each(function (idx) {
|
||||||
const distance = Math.abs(
|
const distance = Math.abs(
|
||||||
$(this).offset().top - minimumOffset() - winScrollTop
|
$(this).offset().top - minimumOffset() - winScrollTop
|
||||||
);
|
);
|
||||||
|
@ -204,7 +202,7 @@
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
triggerShowHide: function(elem) {
|
triggerShowHide: function (elem) {
|
||||||
if (
|
if (
|
||||||
elem.parent().is(".d-toc-heading") ||
|
elem.parent().is(".d-toc-heading") ||
|
||||||
elem.next().is(".d-toc-subheading")
|
elem.next().is(".d-toc-subheading")
|
||||||
|
@ -215,11 +213,11 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
showHide: function(elem) {
|
showHide: function (elem) {
|
||||||
return elem.is(":visible") ? this.hide(elem) : this.show(elem);
|
return elem.is(":visible") ? this.hide(elem) : this.show(elem);
|
||||||
},
|
},
|
||||||
|
|
||||||
hide: function(elem) {
|
hide: function (elem) {
|
||||||
const target = $(".d-toc-subheading")
|
const target = $(".d-toc-subheading")
|
||||||
.not(elem)
|
.not(elem)
|
||||||
.not(elem.parents(".d-toc-subheading:has(.d-toc-active)"));
|
.not(elem.parents(".d-toc-subheading:has(.d-toc-active)"));
|
||||||
|
@ -227,14 +225,14 @@
|
||||||
return target.slideUp(TOC_ANIMATION_SPEED);
|
return target.slideUp(TOC_ANIMATION_SPEED);
|
||||||
},
|
},
|
||||||
|
|
||||||
show: function(elem) {
|
show: function (elem) {
|
||||||
return elem.slideDown(TOC_ANIMATION_SPEED);
|
return elem.slideDown(TOC_ANIMATION_SPEED);
|
||||||
},
|
},
|
||||||
|
|
||||||
scrollTo: function(elem) {
|
scrollTo: function (elem) {
|
||||||
const currentDiv = $(`[data-d-toc="${elem.attr("data-d-toc")}"]`);
|
const currentDiv = $(`[data-d-toc="${elem.attr("data-d-toc")}"]`);
|
||||||
|
|
||||||
$('html, body').animate(
|
$("html, body").animate(
|
||||||
{
|
{
|
||||||
scrollTop: `${currentDiv.offset().top - minimumOffset()}`
|
scrollTop: `${currentDiv.offset().top - minimumOffset()}`
|
||||||
},
|
},
|
||||||
|
@ -250,92 +248,95 @@
|
||||||
});
|
});
|
||||||
})(() => {});
|
})(() => {});
|
||||||
|
|
||||||
api.decorateCooked($elem => {
|
api.decorateCooked(
|
||||||
run.scheduleOnce("actions", () => {
|
$elem => {
|
||||||
if ($elem.hasClass("d-editor-preview")) return;
|
run.scheduleOnce("actions", () => {
|
||||||
if (!$elem.parents("article#post_1").length) return;
|
if ($elem.hasClass("d-editor-preview")) 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, details").each(function() {
|
body.find("div, aside, blockquote, article, details").each(function () {
|
||||||
$(this)
|
$(this)
|
||||||
.children("h1,h2,h3,h4,h5,h6")
|
.children("h1,h2,h3,h4,h5,h6")
|
||||||
.each(function() {
|
.each(function () {
|
||||||
$(this).replaceWith(
|
$(this).replaceWith(
|
||||||
`<div class="d-toc-ignore">${$(this).html()}</div>`
|
`<div class="d-toc-ignore">${$(this).html()}</div>`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
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) {
|
||||||
dTocHeadingSelectors = "h3,h4,h5,h6";
|
dTocHeadingSelectors = "h3,h4,h5,h6";
|
||||||
if (!body.has(">h3").length) {
|
if (!body.has(">h3").length) {
|
||||||
dTocHeadingSelectors = "h4,h5,h6";
|
dTocHeadingSelectors = "h4,h5,h6";
|
||||||
if (!body.has(">h4").length) {
|
if (!body.has(">h4").length) {
|
||||||
dTocHeadingSelectors = "h5,h6";
|
dTocHeadingSelectors = "h5,h6";
|
||||||
if (!body.has(">h5").length) {
|
if (!body.has(">h5").length) {
|
||||||
dTocHeadingSelectors = "h6";
|
dTocHeadingSelectors = "h6";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
body.find(dTocHeadingSelectors).each(function() {
|
body.find(dTocHeadingSelectors).each(function () {
|
||||||
if ($(this).hasClass("d-toc-ignore")) return;
|
if ($(this).hasClass("d-toc-ignore")) return;
|
||||||
const heading = $(this);
|
const heading = $(this);
|
||||||
|
|
||||||
let id = heading.attr("id") || "";
|
let id = heading.attr("id") || "";
|
||||||
|
|
||||||
if (!id.length) {
|
if (!id.length) {
|
||||||
id = cleanUp(heading.text());
|
id = cleanUp(heading.text());
|
||||||
}
|
}
|
||||||
|
|
||||||
heading
|
heading
|
||||||
.attr({
|
.attr({
|
||||||
id: id,
|
id: id,
|
||||||
"data-d-toc": id
|
"data-d-toc": id
|
||||||
})
|
})
|
||||||
.append(createAnchors(id))
|
.append(createAnchors(id))
|
||||||
.addClass("d-toc-post-heading");
|
.addClass("d-toc-post-heading");
|
||||||
});
|
});
|
||||||
|
|
||||||
body
|
body
|
||||||
.addClass("d-toc-cooked")
|
.addClass("d-toc-cooked")
|
||||||
.prepend(
|
.prepend(
|
||||||
`<span class="d-toc-toggle">
|
`<span class="d-toc-toggle">
|
||||||
${dtocIcon} ${I18n.t(themePrefix("table_of_contents"))}
|
${dtocIcon} ${I18n.t(themePrefix("table_of_contents"))}
|
||||||
</span>`
|
</span>`
|
||||||
)
|
)
|
||||||
.parents(".regular")
|
.parents(".regular")
|
||||||
.addClass("d-toc-regular")
|
.addClass("d-toc-regular")
|
||||||
.parents("article")
|
.parents("article")
|
||||||
.addClass("d-toc-article")
|
.addClass("d-toc-article")
|
||||||
.append(
|
.append(
|
||||||
`<ul id="d-toc">
|
`<ul id="d-toc">
|
||||||
<div class="d-toc-close-wrapper">
|
<div class="d-toc-close-wrapper">
|
||||||
<div class="d-toc-close">
|
<div class="d-toc-close">
|
||||||
${closeIcon}
|
${closeIcon}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</ul>`
|
||||||
</ul>`
|
)
|
||||||
)
|
.parents(".topic-post")
|
||||||
.parents(".topic-post")
|
.addClass("d-toc-post")
|
||||||
.addClass("d-toc-post")
|
.parents("body")
|
||||||
.parents("body")
|
.addClass("d-toc-timeline");
|
||||||
.addClass("d-toc-timeline");
|
|
||||||
|
|
||||||
$("#d-toc").dToc({
|
$("#d-toc").dToc({
|
||||||
cooked: body,
|
cooked: body,
|
||||||
selectors: dTocHeadingSelectors
|
selectors: dTocHeadingSelectors
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
},
|
||||||
}, {id: "disco-toc"});
|
{ id: "disco-toc" }
|
||||||
|
);
|
||||||
|
|
||||||
api.cleanupStream(() => {
|
api.cleanupStream(() => {
|
||||||
$(window).off("scroll.d-toc");
|
$(window).off("scroll.d-toc");
|
||||||
|
@ -362,7 +363,6 @@
|
||||||
}
|
}
|
||||||
I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = " ";
|
I18n.translations[I18n.currentLocale()].js.composer.contains_dtoc = " ";
|
||||||
|
|
||||||
|
|
||||||
api.addToolbarPopupMenuOptionsCallback(() => {
|
api.addToolbarPopupMenuOptionsCallback(() => {
|
||||||
const composerController = api.container.lookup("controller:composer");
|
const composerController = api.container.lookup("controller:composer");
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Reference in New Issue