Skip to content

Commit 2ba88b7

Browse files
JessYan0913yanheng
andauthored
85 使用装饰器重构插件方法可用性判断 (#88)
* update: 新增EnableCheck装饰器 * update: 更新方法可用性检查装饰器 * update: 更新方法可用性检查装饰器 * update: 使用装饰器检查方法是否可被调用 * update: 删除ruler中的enabled属性 * update: 使用装饰器EnableCheck检查方法是否可用 * update: 更新日志逻辑 --------- Co-authored-by: yanheng <[email protected]>
1 parent cff2db1 commit 2ba88b7

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

packages/core/src/utils/svg.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { logger } from '@pictode/utils';
2+
13
export function generateSVG(shape: 'circle' | 'triangle' | 'diamond', padding: number, size: number, color: string) {
24
const svgNS = 'http://www.w3.org/2000/svg';
35

@@ -31,7 +33,7 @@ export function generateSVG(shape: 'circle' | 'triangle' | 'diamond', padding: n
3133
Z`;
3234
break;
3335
default:
34-
console.error('Unsupported shape');
36+
logger.error('Unsupported shape');
3537
return;
3638
}
3739

packages/utils/src/decorator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logger from './logger';
2+
13
type EnabledTarget = { enabled: boolean };
24

35
export function EnabledCheck<T extends EnabledTarget>(target: T, propertyKey: string, descriptor: PropertyDescriptor) {
@@ -7,7 +9,7 @@ export function EnabledCheck<T extends EnabledTarget>(target: T, propertyKey: st
79
if (this.enabled) {
810
return originalMethod.apply(this, args);
911
} else {
10-
console.warn(`Method ${propertyKey} is disabled.`);
12+
logger.warning(`Method {{${propertyKey}}} is disabled.`);
1113
// 可以选择抛出错误或执行其他操作
1214
}
1315
};

packages/utils/src/files.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EnvironmentError, FileSelectCancelError, IllegalFileError } from './error';
2+
import logger from './logger';
23

34
/**
45
* 获取文件后缀
@@ -115,7 +116,7 @@ export const readeFile = <T extends string | ArrayBuffer | null>(fileHandler: Fi
115116
const size = '(' + Math.floor(event.total / 1000) + ' KB)';
116117
const progress = Math.floor((event.loaded / event.total) * 100) + '%';
117118

118-
console.log(`Loading size: ${size} progress: ${progress}`);
119+
logger.info(`Loading size: ${size} progress: ${progress}`);
119120
});
120121

121122
reader.addEventListener('load', (event: FileReaderEventMap['load']) => {

packages/utils/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export * from './func';
2424

2525
export * from './decorator';
2626

27+
export { default as logger } from './logger';
28+
2729
export const toLine = (name: string = '') => name.replace(/\B([A-Z])/g, '-$1').toLowerCase();
2830

2931
export function replacePropertyWithValue(obj: Record<string | number | symbol, any>, value: any, newValue: any) {

packages/utils/src/logger.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
type LogType = 'warning' | 'info' | 'error';
2+
3+
interface LoggerOptions {
4+
warningTitleBgColor?: string;
5+
warningKeyColor?: string;
6+
infoTitleBgColor?: string;
7+
infoKeyColor?: string;
8+
errorTitleBgColor?: string;
9+
errorKeyColor?: string;
10+
}
11+
12+
interface LogStyles {
13+
title: string;
14+
text: string;
15+
key: string;
16+
end: string;
17+
}
18+
19+
export class Logger {
20+
private productName: string;
21+
private styles: Record<LogType, LogStyles>;
22+
23+
constructor(productName: string, options: LoggerOptions = {}) {
24+
this.productName = productName;
25+
26+
this.styles = {
27+
warning: {
28+
title: `background-color: ${
29+
options.warningTitleBgColor || 'orange'
30+
}; color: white; font-weight: bold; padding: 2px 4px; border-radius: 4px 0 0 4px;`,
31+
text: 'background-color: black; color: white; font-weight: bold; padding: 2px;',
32+
key: `background-color: black; color: ${options.warningKeyColor || 'yellow'}; font-weight: bold; padding: 2px;`,
33+
end: 'background-color: black; color: white; font-weight: bold; padding: 2px; border-radius: 0 4px 4px 0;',
34+
},
35+
info: {
36+
title: `background-color: ${
37+
options.infoTitleBgColor || 'green'
38+
}; color: white; font-weight: bold; padding: 2px 4px; border-radius: 4px 0 0 4px;`,
39+
text: 'background-color: black; color: white; padding: 2px;',
40+
key: `background-color: black; color: ${options.infoKeyColor || 'lightgreen'}; padding: 2px;`,
41+
end: 'background-color: black; color: white; padding: 2px; border-radius: 0 4px 4px 0;',
42+
},
43+
error: {
44+
title: `background-color: ${
45+
options.errorTitleBgColor || 'purple'
46+
}; color: white; font-weight: bold; padding: 2px 4px; border-radius: 4px 0 0 4px;`,
47+
text: 'background-color: black; color: white; padding: 2px;',
48+
key: `background-color: black; color: ${options.errorKeyColor || 'red'}; font-weight: bold; padding: 2px;`,
49+
end: 'background-color: black; color: white; padding: 2px; border-radius: 0 4px 4px 0;',
50+
},
51+
};
52+
}
53+
54+
private log(type: LogType, message: string): void {
55+
if (!this.styles[type]) {
56+
console.error(`Logger: Unknown log type "${type}".`);
57+
return;
58+
}
59+
60+
const style = this.styles[type];
61+
62+
// 查找所有使用模板语法的占位符,并分隔出普通文本和关键字
63+
const messageParts = message.split(/(\{{[^}]+}})/).filter(Boolean);
64+
const formattedMessage: string[] = [];
65+
const formattedStyles: string[] = [];
66+
67+
messageParts.forEach((part) => {
68+
if (part.startsWith('{{') && part.endsWith('}}')) {
69+
// 如果是关键字占位符,应用关键字样式
70+
formattedMessage.push(`%c${part.slice(2, -2)}`);
71+
formattedStyles.push(style.key);
72+
} else {
73+
// 否则,应用普通文本样式
74+
formattedMessage.push(`%c${part}`);
75+
formattedStyles.push(style.text);
76+
}
77+
});
78+
79+
console.log(
80+
`%c${this.productName} - ${type.toUpperCase()}%c ${formattedMessage.join('')}`,
81+
style.title,
82+
style.text,
83+
...formattedStyles
84+
);
85+
}
86+
87+
public warning(message: string): void {
88+
this.log('warning', message);
89+
}
90+
91+
public info(message: string): void {
92+
this.log('info', message);
93+
}
94+
95+
public error(message: string): void {
96+
this.log('error', message);
97+
}
98+
}
99+
100+
export default new Logger('Pictode');

0 commit comments

Comments
 (0)