Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions docs-en/javascripts/examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Copy button on each problem example block. */
(function (root) {
function uiText(doc) {
var lang = ((doc.documentElement && doc.documentElement.lang) || "").toLowerCase();
if (lang.indexOf("zh") === 0) return { copy: "复制", copied: "已复制" };
return { copy: "Copy", copied: "Copied" };
}

function blockText(block) {
var clone = block.cloneNode(true);
var button = clone.querySelector(".lc-example__copy");
if (button) button.remove();
var text = clone.innerText || clone.textContent || "";
return text.replace(/\n+$/g, "").trim();
}

function copyIcon() {
return '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M19 21H8V7h11m0-2H8a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h11a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2m-3-4H4a2 2 0 0 0-2 2v14h2V3h12z"/></svg>';
}

function checkIcon() {
return '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M21 7 9 19l-5.5-5.5 1.41-1.41L9 16.17 19.59 5.59z"/></svg>';
}

function copyText(text) {
if (navigator.clipboard && navigator.clipboard.writeText) {
return navigator.clipboard.writeText(text).then(
function () {
return true;
},
function () {
return false;
}
);
}
return Promise.resolve(false);
}

function enhance(doc) {
var article = doc.querySelector("article.md-content__inner");
if (!article || article.dataset.lcExamples) return;
var nodes = article.querySelectorAll(":scope > pre, .example-block");
if (!nodes.length) return;
article.dataset.lcExamples = "1";
var text = uiText(doc);
for (var i = 0; i < nodes.length; i++) {
var block = nodes[i];
if (block.closest && block.closest(".lc-example")) continue;
var host = doc.createElement("div");
host.className = "lc-example";
block.parentNode.insertBefore(host, block);
host.appendChild(block);
var button = doc.createElement("button");
button.type = "button";
button.className = "lc-example__copy";
button.setAttribute("aria-label", text.copy);
button.innerHTML = copyIcon();
(function (target, btn) {
btn.addEventListener("click", function () {
copyText(blockText(target)).then(function (ok) {
if (!ok) return;
btn.classList.add("is-copied");
btn.setAttribute("aria-label", text.copied);
btn.innerHTML = checkIcon();
setTimeout(function () {
btn.classList.remove("is-copied");
btn.setAttribute("aria-label", text.copy);
btn.innerHTML = copyIcon();
}, 1200);
});
});
})(block, button);
host.appendChild(button);
}
}

function boot() {
enhance(document);
}

if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", boot);
} else {
boot();
}
if (root.document$ && root.document$.subscribe) {
root.document$.subscribe(boot);
}
})(typeof globalThis !== "undefined" ? globalThis : this);
45 changes: 45 additions & 0 deletions docs-en/stylesheets/extra.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,51 @@
background-color: var(--pre-bg);
border-radius: .1rem;
}
.md-typeset .lc-example {
position: relative;
}
.md-typeset .lc-example > pre {
margin: 0;
overflow-x: auto;
padding: .3em;
background-color: var(--pre-bg);
border-radius: .1rem;
}
.md-typeset .lc-example__copy {
position: absolute;
top: .35em;
right: .35em;
z-index: 1;
display: inline-flex;
align-items: center;
justify-content: center;
width: 1.5rem;
height: 1.5rem;
padding: 0;
border: 0;
border-radius: .1rem;
background: var(--example-block-bg);
color: var(--md-default-fg-color--light);
cursor: pointer;
opacity: 0;
pointer-events: none;
transition: color .25s, opacity .25s;
}
.md-typeset .lc-example:hover > .lc-example__copy,
.md-typeset .lc-example__copy:focus,
.md-typeset .lc-example__copy.is-copied {
opacity: 1;
pointer-events: auto;
}
.md-typeset .lc-example__copy:hover,
.md-typeset .lc-example__copy.is-copied {
color: var(--md-accent-fg-color);
}
.md-typeset .lc-example__copy svg {
width: 1.05rem;
height: 1.05rem;
fill: currentColor;
}

.md-typeset .admonition.thinking,
.md-typeset details.thinking {
Expand Down
89 changes: 89 additions & 0 deletions docs/javascripts/examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Copy button on each problem example block. */
(function (root) {
function uiText(doc) {
var lang = ((doc.documentElement && doc.documentElement.lang) || "").toLowerCase();
if (lang.indexOf("zh") === 0) return { copy: "复制", copied: "已复制" };
return { copy: "Copy", copied: "Copied" };
}

function blockText(block) {
var clone = block.cloneNode(true);
var button = clone.querySelector(".lc-example__copy");
if (button) button.remove();
var text = clone.innerText || clone.textContent || "";
return text.replace(/\n+$/g, "").trim();
}

function copyIcon() {
return '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M19 21H8V7h11m0-2H8a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h11a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2m-3-4H4a2 2 0 0 0-2 2v14h2V3h12z"/></svg>';
}

function checkIcon() {
return '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M21 7 9 19l-5.5-5.5 1.41-1.41L9 16.17 19.59 5.59z"/></svg>';
}

function copyText(text) {
if (navigator.clipboard && navigator.clipboard.writeText) {
return navigator.clipboard.writeText(text).then(
function () {
return true;
},
function () {
return false;
}
);
}
return Promise.resolve(false);
}

function enhance(doc) {
var article = doc.querySelector("article.md-content__inner");
if (!article || article.dataset.lcExamples) return;
var nodes = article.querySelectorAll(":scope > pre, .example-block");
if (!nodes.length) return;
article.dataset.lcExamples = "1";
var text = uiText(doc);
for (var i = 0; i < nodes.length; i++) {
var block = nodes[i];
if (block.closest && block.closest(".lc-example")) continue;
var host = doc.createElement("div");
host.className = "lc-example";
block.parentNode.insertBefore(host, block);
host.appendChild(block);
var button = doc.createElement("button");
button.type = "button";
button.className = "lc-example__copy";
button.setAttribute("aria-label", text.copy);
button.innerHTML = copyIcon();
(function (target, btn) {
btn.addEventListener("click", function () {
copyText(blockText(target)).then(function (ok) {
if (!ok) return;
btn.classList.add("is-copied");
btn.setAttribute("aria-label", text.copied);
btn.innerHTML = checkIcon();
setTimeout(function () {
btn.classList.remove("is-copied");
btn.setAttribute("aria-label", text.copy);
btn.innerHTML = copyIcon();
}, 1200);
});
});
})(block, button);
host.appendChild(button);
}
}

function boot() {
enhance(document);
}

if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", boot);
} else {
boot();
}
if (root.document$ && root.document$.subscribe) {
root.document$.subscribe(boot);
}
})(typeof globalThis !== "undefined" ? globalThis : this);
45 changes: 45 additions & 0 deletions docs/stylesheets/extra.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,51 @@
background-color: var(--pre-bg);
border-radius: .1rem;
}
.md-typeset .lc-example {
position: relative;
}
.md-typeset .lc-example > pre {
margin: 0;
overflow-x: auto;
padding: .3em;
background-color: var(--pre-bg);
border-radius: .1rem;
}
.md-typeset .lc-example__copy {
position: absolute;
top: .35em;
right: .35em;
z-index: 1;
display: inline-flex;
align-items: center;
justify-content: center;
width: 1.5rem;
height: 1.5rem;
padding: 0;
border: 0;
border-radius: .1rem;
background: var(--example-block-bg);
color: var(--md-default-fg-color--light);
cursor: pointer;
opacity: 0;
pointer-events: none;
transition: color .25s, opacity .25s;
}
.md-typeset .lc-example:hover > .lc-example__copy,
.md-typeset .lc-example__copy:focus,
.md-typeset .lc-example__copy.is-copied {
opacity: 1;
pointer-events: auto;
}
.md-typeset .lc-example__copy:hover,
.md-typeset .lc-example__copy.is-copied {
color: var(--md-accent-fg-color);
}
.md-typeset .lc-example__copy svg {
width: 1.05rem;
height: 1.05rem;
fill: currentColor;
}

.md-typeset .admonition.thinking,
.md-typeset details.thinking {
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ markdown_extensions:


extra_javascript:
- javascripts/examples.js
- javascripts/mathjax.js
- https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/mathjax@3/es5/tex-mml-chtml.js

Expand Down
Loading