fontsettings.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. require(['gitbook', 'jquery'], function(gitbook, $) {
  2. // Configuration
  3. var MAX_SIZE = 4,
  4. MIN_SIZE = 0,
  5. BUTTON_ID;
  6. // Current fontsettings state
  7. var fontState;
  8. // Default themes
  9. var THEMES = [
  10. {
  11. config: 'white',
  12. text: 'White',
  13. id: 0
  14. },
  15. {
  16. config: 'sepia',
  17. text: 'Sepia',
  18. id: 1
  19. },
  20. {
  21. config: 'night',
  22. text: 'Night',
  23. id: 2
  24. }
  25. ];
  26. // Default font families
  27. var FAMILIES = [
  28. {
  29. config: 'serif',
  30. text: 'Serif',
  31. id: 0
  32. },
  33. {
  34. config: 'sans',
  35. text: 'Sans',
  36. id: 1
  37. }
  38. ];
  39. // Return configured themes
  40. function getThemes() {
  41. return THEMES;
  42. }
  43. // Modify configured themes
  44. function setThemes(themes) {
  45. THEMES = themes;
  46. updateButtons();
  47. }
  48. // Return configured font families
  49. function getFamilies() {
  50. return FAMILIES;
  51. }
  52. // Modify configured font families
  53. function setFamilies(families) {
  54. FAMILIES = families;
  55. updateButtons();
  56. }
  57. // Save current font settings
  58. function saveFontSettings() {
  59. gitbook.storage.set('fontState', fontState);
  60. update();
  61. }
  62. // Increase font size
  63. function enlargeFontSize(e) {
  64. e.preventDefault();
  65. if (fontState.size >= MAX_SIZE) return;
  66. fontState.size++;
  67. saveFontSettings();
  68. }
  69. // Decrease font size
  70. function reduceFontSize(e) {
  71. e.preventDefault();
  72. if (fontState.size <= MIN_SIZE) return;
  73. fontState.size--;
  74. saveFontSettings();
  75. }
  76. // Change font family
  77. function changeFontFamily(configName, e) {
  78. if (e && e instanceof Event) {
  79. e.preventDefault();
  80. }
  81. var familyId = getFontFamilyId(configName);
  82. fontState.family = familyId;
  83. saveFontSettings();
  84. }
  85. // Change type of color theme
  86. function changeColorTheme(configName, e) {
  87. if (e && e instanceof Event) {
  88. e.preventDefault();
  89. }
  90. var $book = gitbook.state.$book;
  91. var $header = $('.book-body > .book-header');
  92. // Remove currently applied color theme
  93. if (fontState.theme !== 0) {
  94. $book.removeClass('color-theme-'+fontState.theme);
  95. if ($header.length !== 0) {
  96. $header.removeClass('color-theme-'+fontState.theme);
  97. }
  98. }
  99. // Set new color theme
  100. var themeId = getThemeId(configName);
  101. fontState.theme = themeId;
  102. if (fontState.theme !== 0) {
  103. $book.addClass('color-theme-'+fontState.theme);
  104. if ($header.length !== 0) {
  105. $header.addClass('color-theme-'+fontState.theme);
  106. }
  107. }
  108. saveFontSettings();
  109. }
  110. // Return the correct id for a font-family config key
  111. // Default to first font-family
  112. function getFontFamilyId(configName) {
  113. // Search for plugin configured font family
  114. var configFamily = $.grep(FAMILIES, function(family) {
  115. return family.config == configName;
  116. })[0];
  117. // Fallback to default font family
  118. return (!!configFamily)? configFamily.id : 0;
  119. }
  120. // Return the correct id for a theme config key
  121. // Default to first theme
  122. function getThemeId(configName) {
  123. // Search for plugin configured theme
  124. var configTheme = $.grep(THEMES, function(theme) {
  125. return theme.config == configName;
  126. })[0];
  127. // Fallback to default theme
  128. return (!!configTheme)? configTheme.id : 0;
  129. }
  130. function update() {
  131. var $book = gitbook.state.$book;
  132. $('.font-settings .font-family-list li').removeClass('active');
  133. $('.font-settings .font-family-list li:nth-child('+(fontState.family+1)+')').addClass('active');
  134. $book[0].className = $book[0].className.replace(/\bfont-\S+/g, '');
  135. $book.addClass('font-size-'+fontState.size);
  136. $book.addClass('font-family-'+fontState.family);
  137. if(fontState.theme !== 0) {
  138. $book[0].className = $book[0].className.replace(/\bcolor-theme-\S+/g, '');
  139. $book.addClass('color-theme-'+fontState.theme);
  140. }
  141. }
  142. function init(config) {
  143. // Search for plugin configured font family
  144. var configFamily = getFontFamilyId(config.family),
  145. configTheme = getThemeId(config.theme);
  146. // Instantiate font state object
  147. fontState = gitbook.storage.get('fontState', {
  148. size: config.size || 2,
  149. family: configFamily,
  150. theme: configTheme
  151. });
  152. update();
  153. }
  154. function updateButtons() {
  155. // Remove existing fontsettings buttons
  156. if (!!BUTTON_ID) {
  157. gitbook.toolbar.removeButton(BUTTON_ID);
  158. }
  159. // Create buttons in toolbar
  160. BUTTON_ID = gitbook.toolbar.createButton({
  161. icon: 'fa fa-font',
  162. label: 'Font Settings',
  163. className: 'font-settings',
  164. dropdown: [
  165. [
  166. {
  167. text: 'A',
  168. className: 'font-reduce',
  169. onClick: reduceFontSize
  170. },
  171. {
  172. text: 'A',
  173. className: 'font-enlarge',
  174. onClick: enlargeFontSize
  175. }
  176. ],
  177. $.map(FAMILIES, function(family) {
  178. family.onClick = function(e) {
  179. return changeFontFamily(family.config, e);
  180. };
  181. return family;
  182. }),
  183. $.map(THEMES, function(theme) {
  184. theme.onClick = function(e) {
  185. return changeColorTheme(theme.config, e);
  186. };
  187. return theme;
  188. })
  189. ]
  190. });
  191. }
  192. // Init configuration at start
  193. gitbook.events.bind('start', function(e, config) {
  194. var opts = config.fontsettings;
  195. // Generate buttons at start
  196. updateButtons();
  197. // Init current settings
  198. init(opts);
  199. });
  200. // Expose API
  201. gitbook.fontsettings = {
  202. enlargeFontSize: enlargeFontSize,
  203. reduceFontSize: reduceFontSize,
  204. setTheme: changeColorTheme,
  205. setFamily: changeFontFamily,
  206. getThemes: getThemes,
  207. setThemes: setThemes,
  208. getFamilies: getFamilies,
  209. setFamilies: setFamilies
  210. };
  211. });