Skip to content

Commit

Permalink
docs: add customize color button (NG-ZORRO#1192)
Browse files Browse the repository at this point in the history
* docs: add customize color button

* resolve  ngx-color/sketch path
  • Loading branch information
cipchk authored and vthinkxie committed Apr 4, 2018
1 parent 4a6d9a3 commit 27dd074
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "An enterprise-class UI components based on Ant Design and Angular",
"scripts": {
"site:start": "node site_scripts/generate-site init && ng serve --port 0 --open",
"site:init": "node site_scripts/generate-site init",
"site:init": "node site_scripts/generate-site init && node site_scripts/generateColorLess",
"site": "node site_scripts/generate-site",
"ng": "ng",
"start": "ng serve",
Expand Down Expand Up @@ -78,6 +78,7 @@
"karma-cli": "~1.0.1",
"marked": "^0.3.6",
"node-prismjs": "^0.1.1",
"ngx-color": "^1.2.1",
"prismjs": "^1.10.0",
"remark": "^8.0.0",
"rollup": "^0.49.2",
Expand Down
10 changes: 10 additions & 0 deletions site_scripts/_site/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ <h2>
<a target="_blank" rel="noopener noreferrer" href="https://eggjs.org/">Egg</a>
<span> - </span><span>{{language==='zh'?'企业级 Node 开发框架':'Enterprise Node Framework'}}</span>
</div>
<div style="margin-top: 20px;">
<nz-popover [nzTrigger]="'click'" nzOverlayClassName="theme-color-content">
<div class="theme-color" nz-popover>
<div class="theme-color-value" [ngStyle]="{'background': color}"></div>
</div>
<ng-template #nzTemplate>
<color-sketch [color]="color" (onChangeComplete)="changeColor($event)"></color-sketch>
</ng-template>
</nz-popover>
</div>
</div>
</div>
</div>
Expand Down
53 changes: 51 additions & 2 deletions site_scripts/_site/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { NavigationEnd, Router } from '@angular/router';
import { en_US, zh_CN, NzI18nService } from 'ng-zorro-antd';
import { en_US, zh_CN, NzI18nService, NzMessageService } from 'ng-zorro-antd';
import { ROUTER_LIST } from './router';
import { environment } from '../environments/environment';

Expand Down Expand Up @@ -33,7 +33,7 @@ export class AppComponent implements OnInit {
this.hide = !this.hide;
}

constructor(private router: Router, private title: Title, private nzI18nService: NzI18nService) {
constructor(private router: Router, private title: Title, private nzI18nService: NzI18nService, private msg: NzMessageService) {

}

Expand Down Expand Up @@ -76,6 +76,55 @@ export class AppComponent implements OnInit {
}
}
});
this.initColor();
}

// region: color
color = `#1890ff`;
initColor() {
const node = document.createElement('link');
node.rel = 'stylesheet/less';
node.type = 'text/css';
node.href = '/assets/color.less';
document.getElementsByTagName('head')[0].appendChild(node);
}
lessLoaded = false;
changeColor(res: any) {
const changeColor = () => {
(window as any).less.modifyVars({
'@primary-color': res.color.hex
}).then(() => {
this.msg.success(`应用成功`);
this.color = res.color.hex;
window.scrollTo(0, 0);
});
};

const lessUrl = 'https://cdnjs.cloudflare.com/ajax/libs/less.js/2.7.2/less.min.js';

if (this.lessLoaded) {
changeColor();
} else {
(window as any).less = {
async: true
};
this.loadScript(lessUrl).then(() => {
this.lessLoaded = true;
changeColor();
});
}
}

loadScript(src: string) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
// endregion

}
3 changes: 3 additions & 0 deletions site_scripts/_site/src/app/share/share.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
NgZorroAntdModule
} from 'ng-zorro-antd';
import { InfiniteScrollModule } from 'ngx-infinite-scroll';
import { ColorSketchModule } from 'ngx-color/sketch';

import { NzCodeBoxModule } from './nz-codebox/nz-codebox.module';
import { NzHighlightModule } from './nz-highlight/nz-highlight.module';
Expand All @@ -25,6 +26,7 @@ import { NzCopyIconModule } from './nz-copy-icon/nz-copy-icon.module';
NzCopyIconModule,
// third libs
InfiniteScrollModule,
ColorSketchModule,
],
exports: [
CommonModule,
Expand All @@ -38,6 +40,7 @@ import { NzCopyIconModule } from './nz-copy-icon/nz-copy-icon.module';
NzCopyIconModule,
// third libs
InfiniteScrollModule,
ColorSketchModule,
]
})
export class ShareModule { }
21 changes: 21 additions & 0 deletions site_scripts/_site/src/style/footer.less
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,24 @@ footer {
}
}
}

.theme-color {
padding: 4px;
background: rgb(255, 255, 255);
border-radius: 2px;
box-shadow: rgba(0, 0, 0, 0.1) 0 0 0 1px;
display: inline-block;
cursor: pointer;
vertical-align: middle;
&-value {
width: 80px;
height: 16px;
border-radius: 2px;
}
&-content {
.ant-popover-inner-content {
width: 200px;
padding: 0;
}
}
}
3 changes: 2 additions & 1 deletion site_scripts/_site/src/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"node"
],
"paths": {
"ng-zorro-antd": [ "../../components/ng-zorro-antd.module.ts" ]
"ng-zorro-antd": [ "../../components/ng-zorro-antd.module.ts" ],
"ngx-color/sketch": [ "../../node_modules/ngx-color/sketch" ]
}
},
"exclude": [
Expand Down
92 changes: 92 additions & 0 deletions site_scripts/generateColorLess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const glob = require('glob');
const postcss = require('postcss');
const less = require('less');

const COLOR_MAP = {
'#e6f7ff': 'color(~`colorPalette("@{primary-color}", 1)`)', // @primary-1
'#bae7ff': 'color(~`colorPalette("@{primary-color}", 2)`)', // @primary-2
'#40a9ff': 'color(~`colorPalette("@{primary-color}", 5)`)', // @primary-5
'#1890ff': '@primary-color',
'#096dd9': 'color(~`colorPalette("@{primary-color}", 7)`)', // @primary-7
'#46a6ff': 'tint(@primary-color, 20%)',
'#8cc8ff': 'tint(@primary-color, 50%)',
'rgba\\(24, 144, 255, 0.2\\)': 'fade(@primary-color, 20%)',
};

const reducePlugin = postcss.plugin('reducePlugin', () => {
const cleanRule = (rule) => {
if (rule.selector.startsWith('.main-color .palatte-')) {
rule.remove();
return;
}
let removeRule = true;
rule.walkDecls((decl) => {
if (
!decl.prop.includes('color') &&
!decl.prop.includes('background') &&
!decl.prop.includes('border') &&
!decl.prop.includes('box-shadow')
) {
decl.remove();
} else {
removeRule = false;
}
});
if (removeRule) {
rule.remove();
}
};
return (css) => {
css.walkAtRules((atRule) => {
atRule.remove();
});

css.walkRules(cleanRule);

css.walkComments(c => c.remove());
};
});

const antd = path.resolve(__dirname, '../');
const entry = path.join(antd, 'components/style/index.less');
let content = fs.readFileSync(entry).toString();
const styles = glob.sync(path.join(antd, 'components/*/style/index.less'));
content += '\n';
styles.forEach((style) => {
content += `@import "${style}";\n`;
});
content += `@import "${path.join(antd, 'site/src/styles.less')}";\n`;

less.render.call(less, content, {
paths: [path.join(antd, 'components/style')],
}).then(({ css }) => {
return postcss([
reducePlugin,
]).process(css, { parser: less.parser, from: entry });
}).then(({ css }) => {
Object.keys(COLOR_MAP).forEach((key) => {
css = css.replace(new RegExp(key, 'g'), COLOR_MAP[key]);
});

const bezierEasing = fs.readFileSync(path.join(antd, 'components/style/color/bezierEasing.less')).toString();
const tinyColor = fs.readFileSync(path.join(antd, 'components/style/color/tinyColor.less')).toString();
const colorPalette = fs.readFileSync(path.join(antd, 'components/style/color/colorPalette.less'))
.toString()
.replace('@import "bezierEasing";', '')
.replace('@import "tinyColor";', '');

css = `${colorPalette}\n${css}`;
css = `${tinyColor}\n${css}`;
css = `${bezierEasing}\n${css}`;
css = `@primary-color: #1890ff;\n${css}`;

const siteDir = path.resolve(__dirname, '../site');
if (!fs.existsSync(siteDir)) {
fs.mkdirSync(siteDir);
}
fs.writeFileSync(path.resolve(__dirname, '../site/src/assets/color.less'), css);
});

0 comments on commit 27dd074

Please sign in to comment.