Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
feat: rewrite helper framework
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazeyu committed Feb 8, 2018
1 parent ffc980a commit fc6ec6b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 96 deletions.
33 changes: 4 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,14 @@ live2d:
# Live2D
## https://github.com/EYHN/hexo-helper-live2d
live2d:
model: z16 # The model that you are willing to show. default: z16
width: 150 # The width of your model. default: 150
height: 300 # The height of your model. default: 300
scaling: 1 # The scaling of the resolution. default: 2
opacityDefault: 0.7 # The default opacity. default: 0.7
opacityHover: 1 # The opacity when hover. default: 1
mobileShow: true # Whether to show on mobile devices. default: true
mobileScaling: 0.5 # The scaling on mobile. default: 0.5
position: right # Which side the model is shown at. default: right
horizontalOffset: 0 # The horizontal offset. default: 0
verticalOffset: -20 # The offset of the bottom. default: -20
id: live2dcanvas # The ID of the live2d element. default: live2dcanvas
deviceJsSource: local # The source of the current-device script. default: local
enable: true
jsPath: local # 'local'||'jsdelivr'||'unpkg'||{Your own path, String}
model:
use: live2d-widget-model-miku
```
> To use settings in Chinese, please have a look at [Chinese document](./README.zh-CN.md).
|OPTION|default value|Optional value|description|
|:-----|:-----------:|:------------:|:----------|
|model|`z16`|*limited string* See below|The model that you are willing to show.|
|width|`150`|*real*|The width of your model.|
|height|`300`|*real*|The height of your model.|
|scaling|`2`|*real 1 = 100%*|The scaling of the resolution. The greater the value is setted, less mosaic it will be. **Now have bug, see #32, not until the bug is solved, please set this value to 1.**|
|opacityDefault|`0.7`|*real 0 - 1*|The default opacity. *(Beta)*|
|opacityHover|`1`|*real 0 - 1*|The opacity when hover. *(Beta)*|
|mobileShow|`true`|*`true` / `false`*|Whether to show on mobile devices.|
|mobileScaling|`0.5`|*real 1 = 100%*|The scaling on mobile.|
|position|`right`|*`left` / `right`*|`left` or `right` side the model is shown at.|
|horizontalOffset|`0`|*real*|The horizontal offset. <br>Change this variable to adjust the position of model.|
|verticalOffset|`-20`|*real*|The offset of the bottom. <br>Change this variable to adjust the position of model.|
|id|`live2dcanvas`|*string*|The ID of the `<canvas>` element.|
|deviceJsSource|`local`|*`local` / `official` / `string`*|The source of the current-device script.<br>We have three options to choose:<br>`local`: **Default, highly recommended.** Use the local version of script. Compressed using webpack.<br>`official`: Use the official link. [https://unpkg.com/current-device/umd/current-device.min.js](https://unpkg.com/current-device/umd/current-device.min.js)<br>`(your CDN url path)`: put your own CDN path here, must contain `.js`.|

<details><summary>Current supported models:</summary><br>
- `chitose`
Expand Down
129 changes: 72 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,37 @@ const fs = require('hexo-fs'),
path = require('path'),
url = require('url'),
_ = require('lodash'),
jsOnLocalPath = '/live2d/lib/',
clJs = require.resolve('live2d-widget/lib/manifest'),
localJsPath = '/live2d/lib/',
coreJsList = require('live2d-widget/lib/manifest'),
defaultConfig = require('live2d-widget/src/config/defaultConfig');

let fileArr = new Array(),
modelPath = null,
modelJsonPath,
modelPath,
jsPath,
modelJsonPath,
config = _.defaultsDeep(hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);

function getModelJson(pathName){
var fileName = path.parse(pathName).name.split('.');
if(fileName[1] === 'model'){
modelJsonPath = pathName;
}
}

function addFile(destPath, sourceFile){
fileArr.push({
path: destPath,
data: () => fs.createReadStream(sourceFile),
});
}

function addDir(destPath, sourceDir) {
let lsDir = fs.readdirSync(sourceDir)
lsDir.forEach(function (file) {
addFile(destPath + file, path.resolve(sourceDir, file));
}, this);
}

// Check if enabled
if(_.hasIn(config, 'enable')){
if(!config.enable){
Expand All @@ -27,7 +48,8 @@ if(_.hasIn(config, 'enable')){
_.unset(config, 'enable');
}

// Generate jsPath for injecting
// Preprocess jsPath for injecting and file copying
// Copy file and apply real_jsPath only if jsPath === jsOnLocalPath
if(_.hasIn(config, 'jsPath')){
switch(config.jsPath){
case 'local':
Expand All @@ -47,33 +69,60 @@ if(_.hasIn(config, 'jsPath')){
jsPath = jsOnLocalPath;
}

if(_.hasIn(config, 'model.path')){
modelPath = config.model.path;
_.unset(config, '.path');
// Preprocess modelPath for injecting and file copying
// 1. Unset model.jsonPath
if(_.hasIn(config, 'model.jsonPath')){
_.unset(config, 'model.jsonPath');
}
// 2. Process config.model.use
// Set modelPath and config.model.use in some case
// Copy file and apply config.model.jsonPath only if !_.hasIn(config, 'model.jsonPath')
if(_.hasIn(config, 'model.use')){
// 2.a is a npm-module
try(){
modelPath = require.resolve(config.model.use);
}catch(e){
// 2.b is in live2d_models/ folder
let tryPath = path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model.use));
fs.exists(tryPath, function(exists){
if(exists){
// 2.b continue
}else{
// 2.c maybe an url or something, just apply it.
// 3.c Apply config.model.jsonPath
config.model.jsonPath = config.model.use;
}
})
}


function getModelJson(pathName){
var fileName = path.parse(pathName).name.split('.');
if(fileName[1] === 'model'){
modelJsonPath = pathName;
}
_.unset(config, 'model.use');
}else{
// 2.d doesn't have config.model.use use default
// 3.d Apply config.model.jsonPath
config.model.jsonPath = defaultConfig.model.jsonPath;
}

function addFile(destPath, sourceFile){
fileArr.push({
path: destPath,
data: () => fs.createReadStream(sourceFile),
});
// Process config.model.jsonPath
// and copy files
if(!_.hasIn(config, 'model.jsonPath')){

}

function addDir(destPath, sourceDir) {
let lsDir = fs.readdirSync(sourceDir)
lsDir.forEach(function (file) {
addFile(destPath + file, path.resolve(sourceDir, file));
}, this);

// Process jsPath with real_jsPath(not a varible)
// and copy files
if(jsPath === jsOnLocalPath){
// a. is local
// copy coreJs

// apply jsPath

}else{
// b. is a CDN or url, let it go ~
}


/**
* Deprecated version support
* since 3.0
Expand All @@ -84,39 +133,6 @@ hexo.extend.helper.register('live2d', function(){
console.warn('hexo-helper-live2d: live2d tag was deprecated since 3.0. See #36. PLEASE REMOVE live2d TAG IN YOUR TEMPLATE FILE.');
});

// Copy Live2D models
// a. no user defined config, use default config, copy nothing
if(modelPath !== null){
// b. in /live2d_models/ folder, find modelJson, copy
let tryPath = path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model.path));
fs.exists(tryPath, function(exists){
if(exists){
// copy
// find modelJson
// apply modelJson
}else{
// c. in npm_modules or built-in models, find modelJson, copy

// find module
// copy
// find modelJson
// apply modelJson

// d. doesn't found, may be a url, apply to config, copy nothing
// // apply modelJson
}
})
}

if(jsPath === jsOnLocalPath){
// a. local, copy clJs, apply jsOnLocalPath
// copy clJs
// apply jsOnLocalPath
}else{
// b. is a CDN or url, apply jsOnLocalPath
config.model.jsonPath = jsPath;
}

// injector borrowed form here:
// https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
hexo.extend.filter.register('after_render:html', function(htmlContent){
Expand All @@ -128,4 +144,3 @@ hexo.extend.filter.register('after_render:html', function(htmlContent){
}
return htmlContent;
});

20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc6ec6b

Please sign in to comment.