Skip to content

Commit f74f67f

Browse files
committed
dev: prevent some circular dependency
1 parent cb738da commit f74f67f

File tree

9 files changed

+112
-96
lines changed

9 files changed

+112
-96
lines changed

layout/tags.pug

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ block content
1111
if theme.useTagCloud
1212
.autopagerize_page_element: .content
1313
.tag_box
14-
script(type="text/javascript", charset="utf-8", src="/js/tagcloud.js")
1514
script(type="text/javascript", charset="utf-8", src="/js/tagcanvas.js")
1615
#myCanvasContainer.widget.tagcloud(align="center")
1716
canvas#resCanvas

source/js/tagcanvas.js

+56
Original file line numberDiff line numberDiff line change
@@ -2553,4 +2553,60 @@
25532553
},
25542554
window,
25552555
);
2556+
2557+
function addLoadEvent(func) {
2558+
var oldonload = window.onload;
2559+
if (typeof window.onload != 'function') {
2560+
window.onload = func;
2561+
} else {
2562+
window.onload = function () {
2563+
oldonload();
2564+
func();
2565+
};
2566+
}
2567+
}
2568+
2569+
addLoadEvent(function () {
2570+
console.log('tag cloud plugin rock and roll!');
2571+
try {
2572+
var wid = document.body.clientWidth;
2573+
var textHeight = wid / 18;
2574+
if (textHeight > 40) {
2575+
textHeight = 40;
2576+
}
2577+
var wid2 = parseInt(document.body.clientWidth * 0.45);
2578+
if (wid2 < 320) {
2579+
wid2 = 320;
2580+
}
2581+
document.getElementById('resCanvas').width = wid2;
2582+
document.getElementById('resCanvas').height = wid2;
2583+
2584+
TagCanvas.textFont = 'PingHei, PingFang SC, Helvetica Neue, Microsoft YaHei';
2585+
TagCanvas.textColour = '#333';
2586+
TagCanvas.textHeight = textHeight;
2587+
TagCanvas.outlineColour = '#E2E1D1';
2588+
TagCanvas.maxSpeed = 0.07;
2589+
TagCanvas.freezeActive = true;
2590+
TagCanvas.outlineMethod = 'block';
2591+
TagCanvas.minBrightness = 0.2;
2592+
TagCanvas.depth = 0.92;
2593+
TagCanvas.pulsateTo = 0.6;
2594+
TagCanvas.initial = [0.1, -0.1];
2595+
TagCanvas.decel = 0.98;
2596+
TagCanvas.reverse = true;
2597+
TagCanvas.hideTags = false;
2598+
TagCanvas.shadow = '#ccf';
2599+
TagCanvas.shadowBlur = 3;
2600+
TagCanvas.weight = false;
2601+
TagCanvas.imageScale = null;
2602+
TagCanvas.fadeIn = 1000;
2603+
TagCanvas.clickToFront = 600;
2604+
TagCanvas.lock = false;
2605+
TagCanvas.Start('resCanvas');
2606+
TagCanvas.tc['resCanvas'].Wheel(true);
2607+
} catch (e) {
2608+
console.log(e);
2609+
document.getElementById('myCanvasContainer').style.display = 'none';
2610+
}
2611+
});
25562612
})();

source/js/tagcloud.js

-55
This file was deleted.

src/anatolo/comment.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CommentConfig } from '@/types/comment';
22
import { Anatolo } from './anatolo';
3+
import { router } from './router';
34

45
let config: CommentConfig | null = null;
56

@@ -49,6 +50,8 @@ export async function load(retry = 3) {
4950
}
5051
}
5152

53+
router.onPageChange(load);
54+
5255
export function setConfig(conf: CommentConfig) {
5356
config = conf;
5457
}

src/anatolo/router.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { make_friends_list } from '@/utils/main';
22
import { AnatoloRef } from './ref';
33
import { site } from './site';
4-
import { load as loadComment } from './comment';
54

65
type RouterState = { url: string; body: string; title: string; scrollY?: number };
76

87
export class AnatoloRouter {
8+
pageChangeFns: (() => void)[] = [];
99
routerStates = new Map<string, RouterState>();
1010
__animating = new AnatoloRef(false);
1111
__loading = new AnatoloRef(false);
@@ -212,14 +212,25 @@ export class AnatoloRouter {
212212

213213
handlePage() {
214214
this.scrollToHash();
215-
loadComment().catch(() => {});
216-
make_friends_list();
215+
this.pageChangeFns.forEach((fn) => {
216+
try {
217+
fn();
218+
} catch (error) {
219+
console.error(error);
220+
}
221+
});
217222
}
218223

219224
onPopState(event: PopStateEvent) {
220225
event.preventDefault();
221226
this.routeTo(window.location.href, false);
222227
}
228+
229+
onPageChange(fn: () => void) {
230+
this.pageChangeFns.push(fn);
231+
}
223232
}
224233

225234
export const router = new AnatoloRouter();
235+
236+
router.onPageChange(make_friends_list);

src/utils/copy-to-clipboard.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { success } from '@/components/success';
2+
3+
export async function copyToClipboard(text: string) {
4+
try {
5+
await window.navigator.clipboard.writeText(text);
6+
} catch (err) {
7+
// fallback
8+
const t = document.createElement('textarea');
9+
t.value = text;
10+
t.style.width = '0';
11+
t.style.position = 'fixed';
12+
t.style.left = '-999px';
13+
t.style.top = '10px';
14+
t.setAttribute('readonly', 'readonly');
15+
document.body.appendChild(t);
16+
t.select();
17+
document.execCommand('copy');
18+
document.body.removeChild(t);
19+
} finally {
20+
success();
21+
}
22+
}

src/utils/escape-html.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function escapeHTML(str: any) {
2+
return String(str).replace(
3+
/[&<>'"]/g,
4+
(tag) =>
5+
({
6+
'&': '&amp;',
7+
'<': '&lt;',
8+
'>': '&gt;',
9+
"'": '&#39;',
10+
'"': '&quot;',
11+
})[tag] || tag,
12+
);
13+
}

src/utils/friend-link.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { escapeHTML, h } from './main';
1+
import { escapeHTML } from './escape-html';
2+
import { h } from './html-helper';
23

34
export function make_friends_list() {
45
try {

src/utils/main.ts

+2-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { success } from '@/components/success';
21
export { h } from './html-helper';
32

43
export function nextTick(fn: () => void) {
@@ -7,39 +6,6 @@ export function nextTick(fn: () => void) {
76
}, 10);
87
}
98

10-
export function escapeHTML(str: any) {
11-
return String(str).replace(
12-
/[&<>'"]/g,
13-
(tag) =>
14-
({
15-
'&': '&amp;',
16-
'<': '&lt;',
17-
'>': '&gt;',
18-
"'": '&#39;',
19-
'"': '&quot;',
20-
})[tag] || tag,
21-
);
22-
}
23-
9+
export { escapeHTML } from './escape-html';
2410
export { make_friends_list } from './friend-link';
25-
26-
export async function copyToClipboard(text: string) {
27-
try {
28-
await window.navigator.clipboard.writeText(text);
29-
} catch (err) {
30-
// fallback
31-
const t = document.createElement('textarea');
32-
t.value = text;
33-
t.style.width = '0';
34-
t.style.position = 'fixed';
35-
t.style.left = '-999px';
36-
t.style.top = '10px';
37-
t.setAttribute('readonly', 'readonly');
38-
document.body.appendChild(t);
39-
t.select();
40-
document.execCommand('copy');
41-
document.body.removeChild(t);
42-
} finally {
43-
success();
44-
}
45-
}
11+
export { copyToClipboard } from './copy-to-clipboard';

0 commit comments

Comments
 (0)