From 51cd44b8e93457ef298c1342581c8236901787d9 Mon Sep 17 00:00:00 2001 From: xion Date: Sun, 20 Oct 2024 02:23:53 +0800 Subject: [PATCH] fix: fix urlsBegin bugs --- package.json | 2 +- src/container.ts | 37 +++++++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 2750bef..b25d068 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/container", - "version": "0.0.2", + "version": "0.0.2-alpha.1", "description": "", "main": "dist/index.js", "publishConfig": { diff --git a/src/container.ts b/src/container.ts index 9af6470..73b6763 100644 --- a/src/container.ts +++ b/src/container.ts @@ -82,7 +82,7 @@ export class CodeUrlManager { export const codeUrlManager = new CodeUrlManager(); const handleOneCode = async (data: RenderData) => { const { code } = data; - const urlsBegin = ['http', 'https', 'blob', '/']; + const urlsBegin = ['http', 'https', 'blob', '//:']; if (typeof code === 'string' && code && urlsBegin.find((item) => code.startsWith(item))) { const importContent = await import(/* @vite-ignore */ code); const { render, unmount, ...rest } = importContent || {}; @@ -174,6 +174,11 @@ export class Container { } } async loadData(data: RenderData[], key: string = '') { + if (data.length === 0) { + this.data = data; + this.loaded = true; + return; + } this.loading = true; this.data = await handleCode(data); this.loading = false; @@ -229,6 +234,7 @@ export class Container { renderChildren(cid: string, parentElement?: any, pid?: any): void | HTMLDivElement { const data = this.data; + console.log('renderChildren', cid, data); const globalCss = this.globalCss; if (!parentElement) { parentElement = this.root; @@ -264,17 +270,18 @@ export class Container { el.classList.add(cid, 'kv-container'); const { render, unmount } = (code as RenderCode) || {}; let renderRoot = node.shadowRoot ? shadowRoot : root; + const ctx = { + root: root, + shadowRoot, + renderRoot, + event, + container: this, + code: code as RenderCode, + data: node, + css: shadowRoot ? css : globalCss, + }; if (render) { - render({ - root: root, - shadowRoot, - renderRoot, - event, - container: this, - code: code as RenderCode, - data: node, - css: shadowRoot ? css : globalCss, - }); + render(ctx); } else { // no render, so no insert } @@ -283,13 +290,13 @@ export class Container { const destroy = (id: string) => { if (id) { if (id === cid || node?.parents?.find?.((item) => item === id)) { - unmount?.(); // 销毁父亲元素有这个元素的 + unmount?.(ctx); // 销毁父亲元素有这个元素的 event.off('destroy', destroy); // 移除监听 } else { // console.warn('destroy id not found, and not find parentIds', id, 'currentid', cid); } } else if (!id) { - unmount?.(); // 所有的都销毁 + unmount?.(ctx); // 所有的都销毁 event.off('destroy', destroy); } // 不需要销毁子元素 @@ -397,10 +404,12 @@ export class ContainerOne extends Container { super(opts); } async renderOne({ code: RenderCode }) { - const newData = { id: 'test-render-one', code: RenderCode }; + const newData = { codeId: 'test-reneder-one-code-id', id: 'test-render-one', code: RenderCode }; + console.log('loading', this.loading); if (this.loading) { return; } + console.log('renderOne', newData); if (this.data.length === 0) { await this.loadData([newData]); } else {