From 880e355396cfba5c1ac79f26cebf2cf139fcc141 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 6 Mar 2023 19:20:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 - src/constants.js | 11 -------- src/css.js | 40 +++++++++++++++++++++++++++ src/html.js | 32 +++++++++++++++++++++ src/index.js | 72 ++++++++++-------------------------------------- 5 files changed, 87 insertions(+), 69 deletions(-) create mode 100644 src/css.js create mode 100644 src/html.js diff --git a/.gitignore b/.gitignore index cec61b0..ac30e41 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ *.min.js -*.min.css .httpserver index.html test.js diff --git a/src/constants.js b/src/constants.js index 82365dd..1365312 100644 --- a/src/constants.js +++ b/src/constants.js @@ -5,14 +5,3 @@ */ export const finalize = Symbol('finalize') -export const RESET_CSS_STYLE = css` - * { - box-sizing: border-box; - margin: 0; - padding: 0; - } - ::before, - ::after { - box-sizing: border-box; - } -` diff --git a/src/css.js b/src/css.js new file mode 100644 index 0000000..f2b131a --- /dev/null +++ b/src/css.js @@ -0,0 +1,40 @@ +/** + * {} + * @author yutent + * @date 2023/03/06 16:27:49 + */ + +export const RESET_CSS_STYLE = css` + * { + box-sizing: border-box; + margin: 0; + padding: 0; + } + ::before, + ::after { + box-sizing: border-box; + } +` + +export function css(strs, ...args) { + let output = ` + + ` + let tmp = Array.from(strs) + while (tmp.length) { + output += tmp.shift() + (args.shift() || '') + } + return output +} + +export function adoptStyles(root, styles = '') { + let sheet = new CSSStyleSheet() + if (typeof styles === 'string') { + styles = [styles] + } else { + styles = styles.flat(Infinity) + } + styles = (RESET_CSS_STYLE + styles.join(' ')).trim() + sheet.replaceSync(styles) + root.adoptedStyleSheets.push(sheet) +} diff --git a/src/html.js b/src/html.js new file mode 100644 index 0000000..78d691c --- /dev/null +++ b/src/html.js @@ -0,0 +1,32 @@ +export function html(strs, ...args) { + let output = '' + let tmp = Array.from(strs) + // console.log(tmp, args) + + while (tmp.length) { + let _ = args.shift() + switch (typeof _) { + case 'function': + console.log([_], _.name) + _ = _.name + break + case 'object': + if (Array.isArray(_)) { + _ = _.join('') + } + break + } + output += tmp.shift() + (_ === void 0 ? '' : _) + } + return output +} + +function getTemplate(html) { + let template = document.createElement('template') + template.innerHTML = html + return template.content.cloneNode(true) +} + +export function renderRoot(root, html) { + root.appendChild(getTemplate(html)) +} diff --git a/src/index.js b/src/index.js index a77026b..6f321cd 100644 --- a/src/index.js +++ b/src/index.js @@ -4,65 +4,14 @@ * @date 2023/03/03 11:21:44 */ -import { finalize, RESET_CSS_STYLE } from './constants.js' +import { finalize } from './constants.js' +import { css, adoptStyles } from './css.js' +import { html, renderRoot } from './html.js' -export function css(strs, ...args) { - let output = ` - - ` - let tmp = Array.from(strs) - while (tmp.length) { - output += tmp.shift() + (args.shift() || '') - } - return output -} - -export function html(strs, ...args) { - let output = '' - let tmp = Array.from(strs) - // console.log(tmp, args) - - while (tmp.length) { - let _ = args.shift() - switch (typeof _) { - case 'function': - console.log([_], _.name) - _ = _.name - break - case 'object': - if (Array.isArray(_)) { - _ = _.join('') - } - break - } - output += tmp.shift() + (_ === void 0 ? '' : _) - } - return output -} - -function getTemplate(html) { - let template = document.createElement('template') - template.innerHTML = html - return template.content.cloneNode(true) -} - -function adoptStyles(root, styles = '') { - let sheet = new CSSStyleSheet() - if (typeof styles === 'string') { - styles = [styles] - } else { - styles = styles.flat(Infinity) - } - styles = (RESET_CSS_STYLE + styles.join(' ')).trim() - sheet.replaceSync(styles) - root.adoptedStyleSheets.push(sheet) -} - -function render(root, html) { - root.appendChild(getTemplate(html)) -} +export { css, html } export class Component extends HTMLElement { + // 声明要监听的属性 static get observedAttributes() { this[finalize]() @@ -78,6 +27,9 @@ export class Component extends HTMLElement { this.created && this.created() } + /** + * 预处理rpops, styles等 + */ static [finalize]() { if (this.finalized) { return @@ -87,6 +39,11 @@ export class Component extends HTMLElement { this.elemProps = new Map() + const t = Object.getPrototypeOf(this) + + console.log('>>>', t) + console.log('>>>', this) + for (let k in this.props) { let prop = Symbol(k) let options = this.props[k] @@ -107,6 +64,7 @@ export class Component extends HTMLElement { return '' } + // 组件节点被插入文档时触发的回调 connectedCallback() { Object.defineProperty(this, 'root', { value: this.shadowRoot || this.attachShadow({ mode: 'open' }), @@ -115,7 +73,7 @@ export class Component extends HTMLElement { adoptStyles(this.root, this.constructor.styles) - render(this.root, this.render()) + renderRoot(this.root, this.render()) this.mounted && this.mounted() }