diff --git a/Readme.md b/Readme.md index 84fa455..04f7ab4 100644 --- a/Readme.md +++ b/Readme.md @@ -13,11 +13,11 @@ - @bd/core 针对`web components`的核心封装库, 以数据驱动, 可以更方便的开发 wc 组件 -### 开发进度 && 计划 (33/54) +### 开发进度 && 计划 (34/54) - [x] `wc-card` 卡片组件 - [x] `wc-space` 间隔组件 -- [ ] `wc-avatar` 头像组件 +- [x] `wc-avatar` 头像组件 - [x] `wc-badge` 徽标组件 - [x] `wc-drawer` 抽屉组件 - [x] `wc-collapse` 折叠组件 diff --git a/package.json b/package.json index 932d283..980e3fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bd/ui", - "version": "0.1.5", + "version": "0.1.6", "description": "", "files": [ "dist/*" diff --git a/src/avatar/index.js b/src/avatar/index.js new file mode 100644 index 0000000..c7ec559 --- /dev/null +++ b/src/avatar/index.js @@ -0,0 +1,108 @@ +/** + * {} + * @author yutent + * @date 2023/04/25 09:27:25 + */ +import { html, raw, css, Component, nextTick } from '@bd/core' +import '../icon/index.js' + +class Avatar extends Component { + static props = { + src: { + type: String, + default: null + } + } + + static styles = [ + css` + :host { + display: inline-flex; + border-radius: 4px; + } + + .avatar { + display: flex; + align-items: center; + justify-content: center; + width: 100%; + height: 100%; + padding: 2px; + border-radius: inherit; + text-align: center; + background: var(--color-grey-3); + color: #fff; + user-select: none; + + img { + width: 100%; + height: 100%; + border-radius: inherit; + -webkit-user-drag: none; + } + } + + :host([src]) { + .avatar { + padding: 0; + } + } + :host([round]) { + border-radius: 50%; + } + `, + // 尺寸 + css` + @use 'sass:map'; + $sizes: ( + s: ( + h: 20px, + f: 12px + ), + m: ( + h: 24px, + f: 12px + ), + l: ( + h: 32px, + f: 14px + ), + xl: ( + h: 36px, + f: 14px + ), + xxl: ( + h: 44px, + f: 14px + ) + ); + + @loop $s, $v in $sizes { + :host([size='#{$s}']) { + width: map.get($v, 'h'); + height: map.get($v, 'h'); + font-size: map.get($v, 'f'); + --size: #{map.get($v, 'h') - 8px}; + } + } + ` + ] + + mounted() { + if (this.textContent.trim().length > 4) { + this.textContent = this.textContent.trim().slice(0, 4) + } + } + + render() { + return html` +
+ ${this.src + ? html`` + : html``} +
+ ` + } +} + +Avatar.reg('avatar') diff --git a/src/code/colorful.js b/src/code/colorful.js index 29de7ff..91d2468 100644 --- a/src/code/colorful.js +++ b/src/code/colorful.js @@ -195,15 +195,19 @@ export function colorHtml(code) { export function colorCss(code) { code = code .replace(STR, '$1$2$1') - .replace(/(\$[a-zA-Z\d_]+)/g, '$1') + .replace(/(\$[a-zA-Z\d_\-]+)/g, '$1') .replace(/(\-\-[a-z\d]+\-[a-z\d]+)/g, '$1') .replace( /:(hover|after|active|last\-child|first\-child|nth\-child)/g, ':$1' ) .replace( - /(?=\s)?([ ][\.#])([\w\-]+)/g, - '$1$2' + /^((?:[ ])*)([\.#])([\w\-]+)/gm, + '$1$2$3' + ) + .replace( + /:(root|host)/g, + ':$1' ) .replace(/([a-zA-Z\-]+):(\s?[^\n]+)/g, (m, k, v) => { if (v.trim() !== '(' && !v.endsWith(';')) { diff --git a/test.md b/test.md deleted file mode 100644 index 0dc3efa..0000000 --- a/test.md +++ /dev/null @@ -1,133 +0,0 @@ -```html -
-
-
-
${this.lang}
- -
-
- ${this.#code.map(s => html`${raw(s)}`)} -
-
-``` - - -```js -import { html, raw, css, Component, nextTick } from '@bd/core' -import { colorHtml, colorJs, colorCss, colorMd } from './colorful.js' -import '../icon/index.js' -import '../layer/index.js' - -function trim(str) { - return str - .trim() - .replace(/</g, '<') - .replace(/>/g, '>') - .replace(/&/g, '&') -} - -class Code extends Component { - static props = { - code: { - type: String, - default: '', - attribute: false, - observer(v) { - this.setCode(v.trim()) - } - }, - lang: '' - } - - - - #code = [] - - setCode(txt, a) { - let lang = this.lang - - switch (lang) { - case 'js': - case 'javascript': - case 'ts': - case 'typescript': - txt = colorJs(txt) - break - - case 'html': - txt = colorHtml(txt) - break - - case 'css': - case 'scss': - case 'less': - txt = colorCss(txt) - break - - case 'md': - case 'markdown': - txt = colorMd(txt) - break - } - - this.#code = txt.split('\n') - } - - copyCode() { - navigator.clipboard.writeText(this.code) - layer.toast('复制到粘贴板成功', 'success') - } - - mounted() { - var txt = this.innerHTML || this.textContent - txt = txt.trim().replace(/^[\r\n]|\s{2,}$/g, '') - - if (txt.startsWith('') && txt.endsWith('')) { - txt = txt.slice(5, -6).trim() - } else if (this.firstElementChild?.tagName === 'TEXTAREA') { - txt = this.firstElementChild.value.trim() - } else if (txt.startsWith('
') && txt.endsWith('
')) { - txt = trim(txt.slice(5, -6)) - } else { - txt = trim(txt) - } - - this.textContent = '' - if (txt) { - nextTick(_ => { - this.code = txt - }) - } - } - - render() { - let foo = html`
${this.#code.map(s => html`${s}`)}
` - - return html` -
-
-
-
${this.lang}
- -
-
- ${this.#code.map(s => html`${raw(s)}`)} -
-
- ` - } -} - -Code.reg('code') - -``` \ No newline at end of file