Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
吴浩麟 committed Mar 17, 2019
0 parents commit 5c608f7
Show file tree
Hide file tree
Showing 180 changed files with 96,742 additions and 0 deletions.
Binary file added 1-3安装与使用.zip
Binary file not shown.
Binary file added 1-4使用Loader.zip
Binary file not shown.
Binary file added 1-5使用Plugin.zip
Binary file not shown.
Binary file added 1-6使用DevServer.zip
Binary file not shown.
1,397 changes: 1,397 additions & 0 deletions 1入门/1-1前端的发展.html

Large diffs are not rendered by default.

1,430 changes: 1,430 additions & 0 deletions 1入门/1-2常见的构建工具及对比.html

Large diffs are not rendered by default.

1,315 changes: 1,315 additions & 0 deletions 1入门/1-3安装与使用.html

Large diffs are not rendered by default.

1,292 changes: 1,292 additions & 0 deletions 1入门/1-4使用Loader.html

Large diffs are not rendered by default.

1,267 changes: 1,267 additions & 0 deletions 1入门/1-5使用Plugin.html

Large diffs are not rendered by default.

1,275 changes: 1,275 additions & 0 deletions 1入门/1-6使用DevServer.html

Large diffs are not rendered by default.

1,239 changes: 1,239 additions & 0 deletions 1入门/1-7核心概念.html

Large diffs are not rendered by default.

Binary file added 1入门/img/1-2webpack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 1入门/img/1-6source-map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,235 changes: 1,235 additions & 0 deletions 1入门/index.html

Large diffs are not rendered by default.

Binary file added 2-9多种配置类型.zip
Binary file not shown.
1,231 changes: 1,231 additions & 0 deletions 2配置/2-10配置总结.html

Large diffs are not rendered by default.

1,287 changes: 1,287 additions & 0 deletions 2配置/2-1Entry.html

Large diffs are not rendered by default.

1,384 changes: 1,384 additions & 0 deletions 2配置/2-2Output.html

Large diffs are not rendered by default.

1,333 changes: 1,333 additions & 0 deletions 2配置/2-3Module.html

Large diffs are not rendered by default.

1,286 changes: 1,286 additions & 0 deletions 2配置/2-4Resolve.html

Large diffs are not rendered by default.

1,238 changes: 1,238 additions & 0 deletions 2配置/2-5Plugins.html

Large diffs are not rendered by default.

1,327 changes: 1,327 additions & 0 deletions 2配置/2-6DevServer.html

Large diffs are not rendered by default.

1,331 changes: 1,331 additions & 0 deletions 2配置/2-7其它配置项.html

Large diffs are not rendered by default.

1,401 changes: 1,401 additions & 0 deletions 2配置/2-8整体配置结构.html

Large diffs are not rendered by default.

1,298 changes: 1,298 additions & 0 deletions 2配置/2-9多种配置类型.html

Large diffs are not rendered by default.

1,247 changes: 1,247 additions & 0 deletions 2配置/index.html

Large diffs are not rendered by default.

Binary file added 3-10管理多个单页应用.zip
Binary file not shown.
Binary file added 3-11构建同构应用.zip
Binary file not shown.
Binary file added 3-12构建Electron应用.zip
Binary file not shown.
Binary file added 3-13构建Npm库.zip
Binary file not shown.
Binary file added 3-14构建离线应用.zip
Binary file not shown.
Binary file added 3-15搭配NpmScript.zip
Binary file not shown.
Binary file added 3-17通过Node.jsAPI启动Webpack.zip
Binary file not shown.
Binary file added 3-18使用WebpackDevMiddleware.zip
Binary file not shown.
Binary file added 3-19加载图片file-loader.zip
Binary file not shown.
Binary file added 3-19加载图片url-loader.zip
Binary file not shown.
Binary file added 3-1使用ES6语言.zip
Binary file not shown.
Binary file added 3-20加载SVG-raw-loader.zip
Binary file not shown.
Binary file added 3-20加载SVG-svg-inline-loader.zip
Binary file not shown.
Binary file added 3-2使用TypeScript语言.zip
Binary file not shown.
Binary file added 3-3使用Flow检查器.zip
Binary file not shown.
Binary file added 3-4使用SCSS语言.zip
Binary file not shown.
Binary file added 3-5使用PostCSS.zip
Binary file not shown.
Binary file added 3-6使用React框架Babel.zip
Binary file not shown.
Binary file added 3-6使用React框架TypeScript.zip
Binary file not shown.
Binary file added 3-7使用Vue框架Babel.zip
Binary file not shown.
Binary file added 3-7使用Vue框架TypeScript.zip
Binary file not shown.
Binary file added 3-8使用Angular2框架.zip
Binary file not shown.
Binary file added 3-9为单页应用生成HTML.zip
Binary file not shown.
1,385 changes: 1,385 additions & 0 deletions 3实战/3-10管理多个单页应用.html

Large diffs are not rendered by default.

1,379 changes: 1,379 additions & 0 deletions 3实战/3-11构建同构应用.html

Large diffs are not rendered by default.

1,339 changes: 1,339 additions & 0 deletions 3实战/3-12构建Electron应用.html

Large diffs are not rendered by default.

1,446 changes: 1,446 additions & 0 deletions 3实战/3-13构建Npm模块.html

Large diffs are not rendered by default.

1,491 changes: 1,491 additions & 0 deletions 3实战/3-14构建离线应用.html

Large diffs are not rendered by default.

1,274 changes: 1,274 additions & 0 deletions 3实战/3-15搭配NpmScript.html

Large diffs are not rendered by default.

1,385 changes: 1,385 additions & 0 deletions 3实战/3-16检查代码.html

Large diffs are not rendered by default.

1,279 changes: 1,279 additions & 0 deletions 3实战/3-17通过Node.jsAPI启动Webpack.html

Large diffs are not rendered by default.

1,361 changes: 1,361 additions & 0 deletions 3实战/3-18使用WebpackDevMiddleware.html

Large diffs are not rendered by default.

1,310 changes: 1,310 additions & 0 deletions 3实战/3-19加载图片.html

Large diffs are not rendered by default.

1,338 changes: 1,338 additions & 0 deletions 3实战/3-1使用ES6语言.html

Large diffs are not rendered by default.

1,313 changes: 1,313 additions & 0 deletions 3实战/3-20加载SVG.html

Large diffs are not rendered by default.

1,322 changes: 1,322 additions & 0 deletions 3实战/3-21加载SourceMap.html

Large diffs are not rendered by default.

1,236 changes: 1,236 additions & 0 deletions 3实战/3-22实战总结.html

Large diffs are not rendered by default.

1,312 changes: 1,312 additions & 0 deletions 3实战/3-2使用TypeScript语言.html

Large diffs are not rendered by default.

1,295 changes: 1,295 additions & 0 deletions 3实战/3-3使用Flow检查器.html

Large diffs are not rendered by default.

1,280 changes: 1,280 additions & 0 deletions 3实战/3-4使用SCSS语言.html

Large diffs are not rendered by default.

1,300 changes: 1,300 additions & 0 deletions 3实战/3-5使用PostCSS.html

Large diffs are not rendered by default.

1,327 changes: 1,327 additions & 0 deletions 3实战/3-6使用React框架.html

Large diffs are not rendered by default.

1,375 changes: 1,375 additions & 0 deletions 3实战/3-7使用Vue框架.html

Large diffs are not rendered by default.

1,330 changes: 1,330 additions & 0 deletions 3实战/3-8使用Angular2框架.html

Large diffs are not rendered by default.

1,389 changes: 1,389 additions & 0 deletions 3实战/3-9为单页应用生成HTML.html

Large diffs are not rendered by default.

Binary file added 3实战/img/3-11server-render.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3实战/img/3-12electron-app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3实战/img/3-12electron-arch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3实战/img/3-14cache-storage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3实战/img/3-14service-workers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3实战/img/3-14sw-offline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3实战/img/3-1presets-es.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3实战/img/3-1presets-stage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,272 changes: 1,272 additions & 0 deletions 3实战/index.html

Large diffs are not rendered by default.

Binary file added 4-10使用TreeShaking.zip
Binary file not shown.
Binary file added 4-11提取公共代码.zip
Binary file not shown.
Binary file added 4-12分割代码按需加载.zip
Binary file not shown.
Binary file added 4-12分割代码按需加载ReactRouter.zip
Binary file not shown.
Binary file added 4-13提升代码性能Prepack.zip
Binary file not shown.
Binary file added 4-14开启ScopeHoisting.zip
Binary file not shown.
Binary file added 4-15输出分析.zip
Binary file not shown.
Binary file added 4-16优化总结.zip
Binary file not shown.
Binary file added 4-1缩小文件搜索范围.zip
Binary file not shown.
Binary file added 4-2使用DllPlugin.zip
Binary file not shown.
Binary file added 4-3使用HappyPack.zip
Binary file not shown.
Binary file added 4-4使用ParallelUglifyPlugin.zip
Binary file not shown.
Binary file added 4-5使用自动刷新.zip
Binary file not shown.
Binary file added 4-6开启模块热替换.zip
Binary file not shown.
Binary file added 4-7区分环境.zip
Binary file not shown.
Binary file added 4-8压缩代码-CSS.zip
Binary file not shown.
Binary file added 4-8压缩代码-ES5.zip
Binary file not shown.
Binary file added 4-8压缩代码-ES6.zip
Binary file not shown.
Binary file added 4-9CDN加速.zip
Binary file not shown.
1,335 changes: 1,335 additions & 0 deletions 4优化/4-10使用TreeShaking.html

Large diffs are not rendered by default.

1,325 changes: 1,325 additions & 0 deletions 4优化/4-11提取公共代码.html

Large diffs are not rendered by default.

1,379 changes: 1,379 additions & 0 deletions 4优化/4-12按需加载.html

Large diffs are not rendered by default.

1,273 changes: 1,273 additions & 0 deletions 4优化/4-13使用Prepack.html

Large diffs are not rendered by default.

1,305 changes: 1,305 additions & 0 deletions 4优化/4-14开启ScopeHoisting.html

Large diffs are not rendered by default.

1,290 changes: 1,290 additions & 0 deletions 4优化/4-15输出分析.html

Large diffs are not rendered by default.

1,473 changes: 1,473 additions & 0 deletions 4优化/4-16优化总结.html

Large diffs are not rendered by default.

1,378 changes: 1,378 additions & 0 deletions 4优化/4-1缩小文件搜索范围.html

Large diffs are not rendered by default.

1,416 changes: 1,416 additions & 0 deletions 4优化/4-2使用DllPlugin.html

Large diffs are not rendered by default.

1,330 changes: 1,330 additions & 0 deletions 4优化/4-3使用HappyPack.html

Large diffs are not rendered by default.

1,286 changes: 1,286 additions & 0 deletions 4优化/4-4使用ParallelUglifyPlugin.html

Large diffs are not rendered by default.

1,378 changes: 1,378 additions & 0 deletions 4优化/4-5使用自动刷新.html

Large diffs are not rendered by default.

1,360 changes: 1,360 additions & 0 deletions 4优化/4-6开启模块热替换.html

Large diffs are not rendered by default.

1,305 changes: 1,305 additions & 0 deletions 4优化/4-7区分环境.html

Large diffs are not rendered by default.

1,374 changes: 1,374 additions & 0 deletions 4优化/4-8压缩代码.html

Large diffs are not rendered by default.

1,374 changes: 1,374 additions & 0 deletions 4优化/4-9CDN加速.html

Large diffs are not rendered by default.

Binary file added 4优化/img/4-11提取公共代码.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-15webpack-analyse-assets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-15webpack-analyse-chunks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-15webpack-analyse-dialog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-15webpack-analyse-hints.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-15webpack-analyse-home.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-15webpack-analyse-modules.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-15webpack-bundle-analyzer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-5iframe-reload.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-5inline-client-websocket.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-6hmr-log-named.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-6hmr-log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-6hot-patch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4优化/img/4-9cdn-arch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,266 changes: 1,266 additions & 0 deletions 4优化/index.html

Large diffs are not rendered by default.

Binary file added 5-1工作原理概括.zip
Binary file not shown.
Binary file added 5-2输出文件分析.zip
Binary file not shown.
1,393 changes: 1,393 additions & 0 deletions 5原理/5-1工作原理概括.html

Large diffs are not rendered by default.

1,469 changes: 1,469 additions & 0 deletions 5原理/5-2输出文件分析.html

Large diffs are not rendered by default.

1,459 changes: 1,459 additions & 0 deletions 5原理/5-3编写Loader.html

Large diffs are not rendered by default.

1,456 changes: 1,456 additions & 0 deletions 5原理/5-4编写Plugin.html

Large diffs are not rendered by default.

1,249 changes: 1,249 additions & 0 deletions 5原理/5-5调试Webpack.html

Large diffs are not rendered by default.

1,222 changes: 1,222 additions & 0 deletions 5原理/5-6原理总结.html

Large diffs are not rendered by default.

Binary file added 5原理/img/5-1监听模式的构建流程.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 5原理/img/5-5启动Webpack.png
Binary file added 5原理/img/5-5执行到断点.png
Binary file added 5原理/img/5-5设置断点.png
Binary file added 5原理/img/5-5配置执行入口.png
1,238 changes: 1,238 additions & 0 deletions 5原理/index.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions CNAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
webpack.wuhaolin.cn
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# [在线阅读](http://webpack.wuhaolin.cn)《深入浅出 Webpack》

<p align='center'>
<a href='http://webpack.wuhaolin.cn/'>
<img src='http://p0.meituan.net/scarlett/df16c51ffb95186df6f75d8c0e22b965842464.png' width='306px' height='411px'/>
</a>
</p>

## 内容简介
本书是国内第一本系统全面讲解 Webpack 的图书,涵盖了 Webpack 的入门、配置、实战、优化、原理。

- [第1章](http://webpack.wuhaolin.cn/1%E5%85%A5%E9%97%A8/)教你从0开始学会使用 Webpack;
- [第2章](http://webpack.wuhaolin.cn/2%E9%85%8D%E7%BD%AE/)详细的讲解了 Webpack 提供的常用配置项;
- [第3章](http://webpack.wuhaolin.cn/3%E5%AE%9E%E6%88%98/)结合实际项目中常见的场景给出最佳实践;
- [第4章](http://webpack.wuhaolin.cn/4%E4%BC%98%E5%8C%96/)罗列出了各种优化 Webpack 的手段;
- [第5章](http://webpack.wuhaolin.cn/5%E5%8E%9F%E7%90%86/)剖析了 Webpack 原理以及如何开发 Plugin 和 Loader。

除了深入讲解 Webpack,本书还附带介绍了 ES6、TypeScript、PostCSS、Prepack、离线缓存、单页应用、CDN 等 Web 开发相关的技能。

无论是对 Webpack 一无所知的初学者,还是经验丰富的工程师,相信都能通过本书进一步提高对 Webpack 的理解,让你在 Web 开发中更加熟练的运用 Webpack。

## 评价
> Webpack 凭借强大的功能与良好的使用体验,已经成为目前最流行,社区最活跃的打包工具,是现代 Web 开发必须掌握的技能之一。作者结合自身的实战经验,介绍了 Webpack 的使用与常见优化方法、并深入讲解了 Webpack 原理与架构,相信各阶段的 Webpack 用户都能通过本书得到启发。
>
> -- [LeanCloud](https://leancloud.cn/?source=00YQRYTC) 联合创始人/CEO 江宏

> 本书的内容包含多个主题,Webpack 的用法、配置、使用场景等都有涉及,并且提供所有示例的源码,可以补充 Webpack 官方文档。
>
> -- 阮一峰([著名技术博客](http://www.ruanyifeng.com/blog/)[《ES6 标准入门》](http://union-click.jd.com/jdc?d=Vaj3NC)的作者)

> 我手边需要这样一本书,内容涵盖Webpack,或者说涵盖现代前端技术基础、构建与优化的方方面面。如果你已经上手 Webpack,那么本书将带领你进一步学习,真正掌握 Webpack!
>
> -- 陆金所前端架构师、[前端外刊评论](https://qianduan.group/)站长 寸志
## 版权许可
电子工业出版社持有本书全媒体形式的出版发行权利。

[吴浩麟](https://github.com/gwuhaolin)拥有本书的著作权。

**其它人不能将本书用于商用用途,不能转载,不能以任何形式发行,违者将追究法律责任**

Binary file added gitbook/fonts/fontawesome/FontAwesome.otf
Binary file not shown.
Binary file added gitbook/fonts/fontawesome/fontawesome-webfont.eot
Binary file not shown.
685 changes: 685 additions & 0 deletions gitbook/fonts/fontawesome/fontawesome-webfont.svg

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
60 changes: 60 additions & 0 deletions gitbook/gitbook-plugin-adsense/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-env browser */

require(['gitbook'], function (gitbook) {
var adInsertPoint = '.page-inner section'
, adPosition = 'bottom'
, ad;

gitbook.events.bind('start', function (e, cnf) {
const config = cnf.adsense;

// Use custom insert point if defined
if (config.element) {
adInsertPoint = config.element;
}

// Use custom position if defined
if (config.position) {
adPosition = config.position;
}

// Inject script to head.
var adsByGoogleScript = document.createElement('script');
adsByGoogleScript.src = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
adsByGoogleScript.setAttribute('async', true);
document.body.appendChild(adsByGoogleScript);

// Generate <ins> (ad)
ad = document.createElement('ins');
ad.style = 'display: block';
ad.className = 'adsbygoogle';

ad.setAttribute('data-ad-client', config.client);
ad.setAttribute('data-ad-slot', config.slot);
ad.setAttribute('data-ad-format', config.format);

// Add the ad to the DOM
var element = document.querySelector(adInsertPoint);

if (adPosition === 'top') {
element.insertBefore(ad, element.firstChild);
} else {
element.appendChild(ad);
}

(adsbygoogle = window.adsbygoogle || []).push({}); // eslint-disable-line
});

// I insert ad again when switching pages
gitbook.events.bind('page.change', function () {
if (ad) {
var element = document.querySelector(adInsertPoint);

if (adPosition === 'top') {
element.insertBefore(ad, element.firstChild);
} else {
element.appendChild(ad);
}
}
});
});
240 changes: 240 additions & 0 deletions gitbook/gitbook-plugin-fontsettings/fontsettings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
require(['gitbook', 'jquery'], function(gitbook, $) {
// Configuration
var MAX_SIZE = 4,
MIN_SIZE = 0,
BUTTON_ID;

// Current fontsettings state
var fontState;

// Default themes
var THEMES = [
{
config: 'white',
text: 'White',
id: 0
},
{
config: 'sepia',
text: 'Sepia',
id: 1
},
{
config: 'night',
text: 'Night',
id: 2
}
];

// Default font families
var FAMILIES = [
{
config: 'serif',
text: 'Serif',
id: 0
},
{
config: 'sans',
text: 'Sans',
id: 1
}
];

// Return configured themes
function getThemes() {
return THEMES;
}

// Modify configured themes
function setThemes(themes) {
THEMES = themes;
updateButtons();
}

// Return configured font families
function getFamilies() {
return FAMILIES;
}

// Modify configured font families
function setFamilies(families) {
FAMILIES = families;
updateButtons();
}

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

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

fontState.size++;
saveFontSettings();
}

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

fontState.size--;
saveFontSettings();
}

// Change font family
function changeFontFamily(configName, e) {
if (e && e instanceof Event) {
e.preventDefault();
}

var familyId = getFontFamilyId(configName);
fontState.family = familyId;
saveFontSettings();
}

// Change type of color theme
function changeColorTheme(configName, e) {
if (e && e instanceof Event) {
e.preventDefault();
}

var $book = gitbook.state.$book;

// Remove currently applied color theme
if (fontState.theme !== 0)
$book.removeClass('color-theme-'+fontState.theme);

// Set new color theme
var themeId = getThemeId(configName);
fontState.theme = themeId;
if (fontState.theme !== 0)
$book.addClass('color-theme-'+fontState.theme);

saveFontSettings();
}

// Return the correct id for a font-family config key
// Default to first font-family
function getFontFamilyId(configName) {
// Search for plugin configured font family
var configFamily = $.grep(FAMILIES, function(family) {
return family.config == configName;
})[0];
// Fallback to default font family
return (!!configFamily)? configFamily.id : 0;
}

// Return the correct id for a theme config key
// Default to first theme
function getThemeId(configName) {
// Search for plugin configured theme
var configTheme = $.grep(THEMES, function(theme) {
return theme.config == configName;
})[0];
// Fallback to default theme
return (!!configTheme)? configTheme.id : 0;
}

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);
}
}

function init(config) {
// Search for plugin configured font family
var configFamily = getFontFamilyId(config.family),
configTheme = getThemeId(config.theme);

// Instantiate font state object
fontState = gitbook.storage.get('fontState', {
size: config.size || 2,
family: configFamily,
theme: configTheme
});

update();
}

function updateButtons() {
// Remove existing fontsettings buttons
if (!!BUTTON_ID) {
gitbook.toolbar.removeButton(BUTTON_ID);
}

// Create buttons in toolbar
BUTTON_ID = 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
}
],
$.map(FAMILIES, function(family) {
family.onClick = function(e) {
return changeFontFamily(family.config, e);
};

return family;
}),
$.map(THEMES, function(theme) {
theme.onClick = function(e) {
return changeColorTheme(theme.config, e);
};

return theme;
})
]
});
}

// Init configuration at start
gitbook.events.bind('start', function(e, config) {
var opts = config.fontsettings;

// Generate buttons at start
updateButtons();

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

// Expose API
gitbook.fontsettings = {
enlargeFontSize: enlargeFontSize,
reduceFontSize: reduceFontSize,
setTheme: changeColorTheme,
setFamily: changeFontFamily,
getThemes: getThemes,
setThemes: setThemes,
getFamilies: getFamilies,
setFamilies: setFamilies
};
});


Loading

0 comments on commit 5c608f7

Please sign in to comment.