fix: fix urlsBegin bugs

This commit is contained in:
xion 2024-10-20 02:23:53 +08:00
parent eeb78110db
commit 51cd44b8e9
2 changed files with 24 additions and 15 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@kevisual/container", "name": "@kevisual/container",
"version": "0.0.2", "version": "0.0.2-alpha.1",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"publishConfig": { "publishConfig": {

View File

@ -82,7 +82,7 @@ export class CodeUrlManager {
export const codeUrlManager = new CodeUrlManager(); export const codeUrlManager = new CodeUrlManager();
const handleOneCode = async (data: RenderData) => { const handleOneCode = async (data: RenderData) => {
const { code } = data; const { code } = data;
const urlsBegin = ['http', 'https', 'blob', '/']; const urlsBegin = ['http', 'https', 'blob', '//:'];
if (typeof code === 'string' && code && urlsBegin.find((item) => code.startsWith(item))) { if (typeof code === 'string' && code && urlsBegin.find((item) => code.startsWith(item))) {
const importContent = await import(/* @vite-ignore */ code); const importContent = await import(/* @vite-ignore */ code);
const { render, unmount, ...rest } = importContent || {}; const { render, unmount, ...rest } = importContent || {};
@ -174,6 +174,11 @@ export class Container {
} }
} }
async loadData(data: RenderData[], key: string = '') { async loadData(data: RenderData[], key: string = '') {
if (data.length === 0) {
this.data = data;
this.loaded = true;
return;
}
this.loading = true; this.loading = true;
this.data = await handleCode(data); this.data = await handleCode(data);
this.loading = false; this.loading = false;
@ -229,6 +234,7 @@ export class Container {
renderChildren(cid: string, parentElement?: any, pid?: any): void | HTMLDivElement { renderChildren(cid: string, parentElement?: any, pid?: any): void | HTMLDivElement {
const data = this.data; const data = this.data;
console.log('renderChildren', cid, data);
const globalCss = this.globalCss; const globalCss = this.globalCss;
if (!parentElement) { if (!parentElement) {
parentElement = this.root; parentElement = this.root;
@ -264,17 +270,18 @@ export class Container {
el.classList.add(cid, 'kv-container'); el.classList.add(cid, 'kv-container');
const { render, unmount } = (code as RenderCode) || {}; const { render, unmount } = (code as RenderCode) || {};
let renderRoot = node.shadowRoot ? shadowRoot : root; 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) { if (render) {
render({ render(ctx);
root: root,
shadowRoot,
renderRoot,
event,
container: this,
code: code as RenderCode,
data: node,
css: shadowRoot ? css : globalCss,
});
} else { } else {
// no render, so no insert // no render, so no insert
} }
@ -283,13 +290,13 @@ export class Container {
const destroy = (id: string) => { const destroy = (id: string) => {
if (id) { if (id) {
if (id === cid || node?.parents?.find?.((item) => item === id)) { if (id === cid || node?.parents?.find?.((item) => item === id)) {
unmount?.(); // 销毁父亲元素有这个元素的 unmount?.(ctx); // 销毁父亲元素有这个元素的
event.off('destroy', destroy); // 移除监听 event.off('destroy', destroy); // 移除监听
} else { } else {
// console.warn('destroy id not found, and not find parentIds', id, 'currentid', cid); // console.warn('destroy id not found, and not find parentIds', id, 'currentid', cid);
} }
} else if (!id) { } else if (!id) {
unmount?.(); // 所有的都销毁 unmount?.(ctx); // 所有的都销毁
event.off('destroy', destroy); event.off('destroy', destroy);
} }
// 不需要销毁子元素 // 不需要销毁子元素
@ -397,10 +404,12 @@ export class ContainerOne extends Container {
super(opts); super(opts);
} }
async renderOne({ code: RenderCode }) { 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) { if (this.loading) {
return; return;
} }
console.log('renderOne', newData);
if (this.data.length === 0) { if (this.data.length === 0) {
await this.loadData([newData]); await this.loadData([newData]);
} else { } else {