kevin1207 commited on
Commit
c65ce5d
·
verified ·
1 Parent(s): 5fbc36c

Update custom.js

Browse files
Files changed (1) hide show
  1. custom.js +1 -120
custom.js CHANGED
@@ -1,120 +1 @@
1
- (function () {
2
- function checkIsEditPage() {
3
- return window.location.href.includes("/functions");
4
- }
5
- let isCurrentlyEditPage = checkIsEditPage();
6
- function onRouteChange() {
7
- isCurrentlyEditPage = checkIsEditPage();
8
- if (isCurrentlyEditPage) {
9
- if (mutationObserverActive) {
10
- mutationObserver.disconnect();
11
- mutationObserverActive = false;
12
- }
13
- } else {
14
- initializeAllCodeBlocks();
15
- if (!mutationObserverActive) {
16
- mutationObserver.observe(document.body, {
17
- childList: true,
18
- subtree: true,
19
- });
20
- mutationObserverActive = true;
21
- }
22
- }
23
- }
24
- const originalPushState = history.pushState;
25
- history.pushState = function (state, title, url) {
26
- originalPushState.apply(history, arguments);
27
- onRouteChange();
28
- };
29
- window.addEventListener("popstate", onRouteChange);
30
- const observedCodeBlocks = new WeakSet();
31
- const resizeObserver = new ResizeObserver((entries) => {
32
- if (isCurrentlyEditPage) return;
33
- for (const entry of entries) {
34
- const editorRoot = entry.target;
35
- if (!editorRoot.classList.contains("cm-editor")) continue;
36
- updateCodeBlock(editorRoot);
37
- }
38
- });
39
- function updateCodeBlock(editorRoot) {
40
- if (editorRoot.querySelector(".code-expand-btn")) return;
41
- const height = editorRoot.scrollHeight;
42
- if (height > 400) {
43
- editorRoot.id = "collapsed";
44
- const expandBtn = document.createElement("button");
45
- expandBtn.className = "code-expand-btn";
46
- expandBtn.id = "collapsed";
47
- editorRoot.appendChild(expandBtn);
48
- editorRoot.style.height = "400px";
49
- }
50
- }
51
- function initializeCodeBlock(editorRoot) {
52
- if (observedCodeBlocks.has(editorRoot)) return;
53
- observedCodeBlocks.add(editorRoot);
54
- resizeObserver.observe(editorRoot);
55
- updateCodeBlock(editorRoot);
56
- }
57
- function initializeAllCodeBlocks() {
58
- if (isCurrentlyEditPage) return;
59
- document.querySelectorAll(".cm-editor").forEach(initializeCodeBlock);
60
- }
61
- const mutationObserver = new MutationObserver((mutations) => {
62
- if (isCurrentlyEditPage) return;
63
- let hasNewCodeBlocks = false;
64
- mutations.forEach((mutation) => {
65
- mutation.addedNodes.forEach((node) => {
66
- if (node.nodeType !== 1) return;
67
- if (node.classList?.contains("cm-editor")) {
68
- initializeCodeBlock(node);
69
- hasNewCodeBlocks = true;
70
- } else {
71
- const matches = node.querySelectorAll?.(".cm-editor") || [];
72
- matches.forEach((el) => {
73
- initializeCodeBlock(el);
74
- hasNewCodeBlocks = true;
75
- });
76
- }
77
- });
78
- });
79
- if (hasNewCodeBlocks) requestAnimationFrame(initializeAllCodeBlocks);
80
- });
81
- let mutationObserverActive = false;
82
- document.addEventListener("click", function (evt) {
83
- if (!evt.target.classList.contains("code-expand-btn")) return;
84
- const editorRoot = evt.target.closest(".cm-editor");
85
- if (!editorRoot) return;
86
- const isCollapsed = editorRoot.id === "collapsed";
87
- requestAnimationFrame(() => {
88
- if (isCollapsed) {
89
- const scroller = editorRoot.querySelector(".cm-scroller");
90
- editorRoot.style.height = `${scroller.scrollHeight}px`;
91
- editorRoot.id = "expanded";
92
- evt.target.id = "expanded";
93
- } else {
94
- editorRoot.style.height = "400px";
95
- editorRoot.id = "collapsed";
96
- evt.target.id = "collapsed";
97
- const scrollTarget =
98
- editorRoot.closest(".relative.my-2")?.parentElement;
99
- scrollTarget?.scrollIntoView({ behavior: "smooth", block: "start" });
100
- }
101
- });
102
- });
103
- function init() {
104
- isCurrentlyEditPage = checkIsEditPage();
105
- if (!isCurrentlyEditPage) initializeAllCodeBlocks();
106
- mutationObserver.observe(document.body, { childList: true, subtree: true });
107
- mutationObserverActive = true;
108
- }
109
- if (document.readyState === "loading") {
110
- document.addEventListener("DOMContentLoaded", init);
111
- } else {
112
- init();
113
- }
114
- window.addEventListener("error", (error) => {
115
- console.error("Code block error:", error);
116
- });
117
- window.addEventListener("unhandledrejection", (event) => {
118
- console.error("Unhandled rejection:", event.reason);
119
- });
120
- })();
 
1
+ (function () { function checkIsEditPage() { return window.location.href.includes('/functions'); } let isCurrentlyEditPage = checkIsEditPage(); function onRouteChange() { isCurrentlyEditPage = checkIsEditPage(); if (isCurrentlyEditPage) { if (mutationObserverActive) { mutationObserver.disconnect(); mutationObserverActive = false; } } else { initializeAllCodeBlocks(); if (!mutationObserverActive) { mutationObserver.observe(document.body, { childList: true, subtree: true }); mutationObserverActive = true; } } } const originalPushState = history.pushState; history.pushState = function (state, title, url) { originalPushState.apply(history, arguments); onRouteChange(); }; window.addEventListener('popstate', onRouteChange); const observedCodeBlocks = new WeakSet(); const resizeObserver = new ResizeObserver((entries) => { if (isCurrentlyEditPage) return; for (const entry of entries) { const editorRoot = entry.target; if (!editorRoot.classList.contains('cm-editor')) continue; updateCodeBlock(editorRoot); } }); function updateCodeBlock(editorRoot) { if (editorRoot.querySelector('.code-expand-btn')) return; const height = editorRoot.scrollHeight; if (height > 400) { editorRoot.id = 'collapsed'; const expandBtn = document.createElement('button'); expandBtn.className = 'code-expand-btn'; expandBtn.id = 'collapsed'; editorRoot.appendChild(expandBtn); editorRoot.style.height = '400px'; } } function initializeCodeBlock(editorRoot) { if (observedCodeBlocks.has(editorRoot)) return; observedCodeBlocks.add(editorRoot); resizeObserver.observe(editorRoot); updateCodeBlock(editorRoot); } function initializeAllCodeBlocks() { if (isCurrentlyEditPage) return; document.querySelectorAll('.cm-editor').forEach(initializeCodeBlock); } const mutationObserver = new MutationObserver((mutations) => { if (isCurrentlyEditPage) return; let hasNewCodeBlocks = false; mutations.forEach((mutation) => { mutation.addedNodes.forEach((node) => { if (node.nodeType !== 1) return; if (node.classList?.contains('cm-editor')) { initializeCodeBlock(node); hasNewCodeBlocks = true; } else { const matches = node.querySelectorAll?.('.cm-editor') || []; matches.forEach((el) => { initializeCodeBlock(el); hasNewCodeBlocks = true; }); } }); }); if (hasNewCodeBlocks) requestAnimationFrame(initializeAllCodeBlocks); }); let mutationObserverActive = false; document.addEventListener('click', function (evt) { if (!evt.target.classList.contains('code-expand-btn')) return; const editorRoot = evt.target.closest('.cm-editor'); if (!editorRoot) return; const isCollapsed = editorRoot.id === 'collapsed'; requestAnimationFrame(() => { if (isCollapsed) { const scroller = editorRoot.querySelector('.cm-scroller'); editorRoot.style.height = `${scroller.scrollHeight}px`; editorRoot.id = 'expanded'; evt.target.id = 'expanded'; } else { editorRoot.style.height = '400px'; editorRoot.id = 'collapsed'; evt.target.id = 'collapsed'; const scrollTarget = editorRoot.closest('.relative.my-2')?.parentElement; scrollTarget?.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); function init() { isCurrentlyEditPage = checkIsEditPage(); if (!isCurrentlyEditPage) initializeAllCodeBlocks(); mutationObserver.observe(document.body, { childList: true, subtree: true }); mutationObserverActive = true; } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } window.addEventListener('error', (error) => { console.error('Code block error:', error); }); window.addEventListener('unhandledrejection', (event) => { console.error('Unhandled rejection:', event.reason); }); })();