Introducing pangu.js to the Hugo Blog

Words: 362

I used to often struggle with typography when mixing Chinese and English, so I introduced pangu.js to improve the typesetting effect.

Official repository: vinta/pangu.js: Paranoid text spacing in JavaScript

Calling pangu.autoSpacingPage() converts the page into the beautified result.

Since loading and processing JS takes time, writing it directly like this means users will first see the unprocessed page, then after a short pause it refreshes to the new content, which makes the experience feel strange.

As a solution, you can consider hiding the page first, waiting for DOM to finish loading, running pangu.js processing first, and then showing the page again after it is done.

To avoid a long first load, you can also add a Loading effect.

Note that the autoSpacingPage function processes asynchronously; you can switch to the blocking version spacingPage.

Reference Code

html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<div id="loadingOverlay">  
    <div class="loader"></div>  
    <p>加载中...</p>  
</div>
<style>  
    #loadingOverlay {  
        position: fixed;  
        top: 0;  
        left: 0;  
        width: 100%;  
        height: 100%;  
        background: rgba(255, 255, 255, 0.9);  
        z-index: 9999;  
        display: none;  
        justify-content: center;  
        align-items: center;  
        flex-direction: column;  
    }  
  
    .loader {  
        border: 8px solid #f3f3f3;  
        border-top: 8px solid #3498db;  
        border-radius: 50%;  
        width: 60px;  
        height: 60px;  
        animation: spin 1s linear infinite;  
    }  
  
    @keyframes spin {  
        0% {  
            transform: rotate(0deg);  
        }  
        100% {  
            transform: rotate(360deg);  
        }  
    }  
</style>

<style>  
    /* 初始隐藏页面内容,避免显示未经处理的文本 */    .main-container {  
        display: none;  
    }  
</style>  
  
<script>  
    let isShouldLoad = true;  
    setTimeout(() => {  
        if (isShouldLoad) {  
            const loadingOverlay = document.getElementById('loadingOverlay');  
            if (loadingOverlay) {  
                loadingOverlay.style.display = 'flex';  
            }  
        }  
    }, 200); // 0.2秒如果还没有加载完成就开启Loading  
</script>  
  
<script type="module">  
    import pangu from 'https://jspm.dev/pangu';  
    document.addEventListener('DOMContentLoaded', () => {  
        isShouldLoad = false  
        pangu.spacingPage();  
        const elements = document.getElementsByClassName("main-container")  
        for (let i = 0; i < elements.length; i++) {  
            elements[i].style.display = 'flex';  
        }  
        const loadingOverlay = document.getElementById('loadingOverlay');  
        if (loadingOverlay) {  
            loadingOverlay.style.display = 'none';  
        }  
    });  
</script>
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...