File: plugin-fontsettings.js

package info (click to toggle)
r-cran-bookdown 0.42%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,752 kB
  • sloc: javascript: 11,343; makefile: 21; sh: 20
file content (175 lines) | stat: -rw-r--r-- 4,894 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
gitbook.require(["gitbook", "lodash", "jQuery"], function(gitbook, _, $) {
    var fontState;

    var THEMES = {
        "white": 0,
        "sepia": 1,
        "night": 2
    };

    var FAMILY = {
        "serif": 0,
        "sans": 1
    };

    // Save current font settings
    function saveFontSettings() {
        gitbook.storage.set("fontState", fontState);
        update();
    }

    // Increase font size
    function enlargeFontSize(e) {
        e.preventDefault();
        if (fontState.size >= 4) return;

        fontState.size++;
        saveFontSettings();
    };

    // Decrease font size
    function reduceFontSize(e) {
        e.preventDefault();
        if (fontState.size <= 0) return;

        fontState.size--;
        saveFontSettings();
    };

    // Change font family
    function changeFontFamily(index, e) {
        e.preventDefault();

        fontState.family = index;
        saveFontSettings();
    };

    // Change type of color
    function changeColorTheme(index, e) {
        e.preventDefault();

        var $book = $(".book");

        if (fontState.theme !== 0)
            $book.removeClass("color-theme-"+fontState.theme);

        fontState.theme = index;
        if (fontState.theme !== 0)
            $book.addClass("color-theme-"+fontState.theme);

        saveFontSettings();
    };

    // Increase or decrease line spacing
    function changeSpacing(e, inc = true) {
        e.preventDefault();
        inc ? fontState.spacing++ : (fontState.spacing > 10 && fontState.spacing--);
        saveFontSettings();
    }

    function update() {
        var $book = gitbook.state.$book;

        $(".font-settings .font-family-list li").removeClass("active");
        $(".font-settings .font-family-list li:nth-child("+(fontState.family+1)+")").addClass("active");

        $book[0].className = $book[0].className.replace(/\bfont-\S+/g, '');
        $book.addClass("font-size-"+fontState.size);
        $book.addClass("font-family-"+fontState.family);

        if(fontState.theme !== 0) {
            $book[0].className = $book[0].className.replace(/\bcolor-theme-\S+/g, '');
            $book.addClass("color-theme-"+fontState.theme);
        }
        var lineHeight = fontState.spacing / 10;
        $book.find('section').css('line-height', lineHeight);
        $('.font-settings .spacing-reduce').prop('disabled', lineHeight <= 1);
    };

    function init(config) {
        var $bookBody, $book;

        //Find DOM elements.
        $book = gitbook.state.$book;
        $bookBody = $book.find(".book-body");

        // Instantiate font state object
        fontState = gitbook.storage.get("fontState", {
            size: config.size || 2,
            family: FAMILY[config.family || "sans"],
            theme: THEMES[config.theme || "white"],
            spacing: config.spacing || 17,
        });

        update();
    };


    gitbook.events.bind("start", function(e, config) {
        var opts = config.fontsettings;
        if (!opts) return;
        
        // Create buttons in toolbar
        gitbook.toolbar.createButton({
            icon: 'fa fa-font',
            label: 'Font Settings',
            className: 'font-settings',
            dropdown: [
                [
                    {
                        text: 'A',
                        className: 'font-reduce',
                        onClick: reduceFontSize
                    },
                    {
                        text: 'A',
                        className: 'font-enlarge',
                        onClick: enlargeFontSize
                    }
                ],
                [
                    {
                        text: 'Serif',
                        onClick: _.partial(changeFontFamily, 0)
                    },
                    {
                        text: 'Sans',
                        onClick: _.partial(changeFontFamily, 1)
                    }
                ],
                [
                    {
                        text: 'White',
                        onClick: _.partial(changeColorTheme, 0)
                    },
                    {
                        text: 'Sepia',
                        onClick: _.partial(changeColorTheme, 1)
                    },
                    {
                        text: 'Night',
                        onClick: _.partial(changeColorTheme, 2)
                    }
                
                ],
                [
                    {
                    className: 'spacing-reduce',
                    text: 'Spacing -',
                    onClick: e => changeSpacing(e, false)
                    },
                    {
                    text: 'Spacing +',
                    onClick: changeSpacing
                    }
                ]
            ]
        });


        // Init current settings
        init(opts);
    });
});