Skip to content

Commit 0d5d149

Browse files
committed
fix(Core): Fix plugin event ready triggering before its HTML finishes rendering. (issue #591)
1 parent 2e4feb3 commit 0d5d149

File tree

6 files changed

+68
-23
lines changed

6 files changed

+68
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
English | [简体中文](./CHANGELOG_CN.md)
22

3-
## 3.16.0 (2023-05-??)
3+
## 3.15.1 (2023-05-??)
44

55
- `Feat(Netwrk)` Add new option `network.ignoreUrlRegExp` to skip some requests. (PR #623)
66
- `Fix(Core)` Fix prototype pollution in `vConsole.setOption()`. (issue #616 #621)
7+
- `Fix(Core)` Fix plugin event `ready` triggering before its HTML finishes rendering. (issue #591)
78
- `Fix(Log)` Reset group state when `console.clear()` is called. (issue #611)
89
- `Fix(Log)` Fix fatal error caused by iOS (less than 13.4) which is not support `ResizeObserver` interface. (issue #610)
910
- `Fix(Network)` Fix possible "Cannot read property" error by `sendBeacon`. (issue #615)

CHANGELOG_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- `Feat(Netwrk)` 新增配置项 `network.ignoreUrlRegExp` 以跳过一些请求。 (PR #623)
66
- `Fix(Core)` 修复 `vConsole.setOption()` 中可能存在的原型污染问题。 (issue #616 #621)
7+
- `Fix(Core)` 修复插件事件 `ready` 在插件完成渲染前就被触发的问题。 (issue #591)
78
- `Fix(Log)` 修复调用 `console.clear()` 时没有重置 group 层级的问题。 (issue #611)
89
- `Fix(Log)` 修复因 iOS(小于 13.4)不支持 `ResizeObserver` 接口导致的致命错误 (issue #610)
910
- `Fix(Network)` 修复可能由 `sendBeacon` 引发的 "Cannot read property" 错误。 (issue #615)

dev/plugin.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
.on('init', function() { console.log(this.id, 'init'); })
4040
.on('renderTab', function(cb) {
4141
console.log(this.id, 'renderTab');
42-
cb('<div>I am ' + this.id+'</div>');
42+
cb('<div id="tab1">I am ' + this.id+'</div>');
4343
})
44-
.on('ready', function() { console.log(this.id, 'ready'); })
44+
.on('ready', function() { console.log(this.id, 'ready', document.querySelector('#tab1')); })
4545
.on('show', function() { console.log(this.id, 'show'); })
4646
.on('hide', function() { console.log(this.id, 'hide'); })
4747
.on('showConsole', function() { console.log(this.id, 'showConsole'); })

src/core/core.svelte

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { onMount, onDestroy, createEventDispatcher } from 'svelte';
33
import * as tool from '../lib/tool';
44
import { default as SwitchButton } from './switchButton.svelte';
5+
import { default as PluginContent } from '../lib/pluginContent.svelte';
56
import { contentStore } from './core.model';
67
import Style from './core.less';
78
import type { IVConsoleTopbarOptions, IVConsoleToolbarOptions, IVConsoleTabOptions } from '../lib/plugin';
@@ -17,6 +18,7 @@
1718
tabOptions?: IVConsoleTabOptions;
1819
topbarList?: IVConsoleTopbarOptions[];
1920
toolbarList?: IVConsoleToolbarOptions[];
21+
content?: HTMLElement;
2022
}
2123
2224
export let theme = '';
@@ -368,12 +370,13 @@
368370
on:scroll={onContentScroll}
369371
>
370372
{#each Object.entries(pluginList) as [pluginId, plugin]}
371-
<div
372-
id="__vc_plug_{plugin.id}"
373-
class="vc-plugin-box"
374-
class:vc-fixed-height="{plugin.tabOptions?.fixedHeight}"
375-
class:vc-actived="{plugin.id === activedPluginId}"
376-
></div>
373+
<svelte:component
374+
this={PluginContent}
375+
pluginId={plugin.id}
376+
fixedHeight={plugin.tabOptions?.fixedHeight}
377+
actived={plugin.id === activedPluginId}
378+
content={plugin.content}
379+
/>
377380
{/each}
378381
</div>
379382

src/core/core.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ export class VConsole {
293293
tabOptions: undefined,
294294
topbarList: [],
295295
toolbarList: [],
296+
content: undefined,
297+
contentContainer: undefined,
296298
};
297299
this.compInstance.pluginList = this._reorderPluginList(this.compInstance.pluginList);
298300
// start init
@@ -305,20 +307,7 @@ export class VConsole {
305307
pluginInfo.tabOptions = options;
306308
// render tabbox
307309
if (!!tabboxHTML) {
308-
// when built-in plugins are initializing in the same time,
309-
// plugin's `.vc-plugin-box` element will be re-order by `pluginOrder` option,
310-
// so the innerHTML should be inserted with a delay
311-
// to make sure getting the right `.vc-plugin-box`. (issue #559)
312-
setTimeout(() => {
313-
const divContentInner = document.querySelector('#__vc_plug_' + plugin.id);
314-
if (tool.isString(tabboxHTML)) {
315-
divContentInner.innerHTML += tabboxHTML;
316-
} else if (tool.isFunction(tabboxHTML.appendTo)) {
317-
tabboxHTML.appendTo(divContentInner);
318-
} else if (tool.isElement(tabboxHTML)) {
319-
divContentInner.insertAdjacentElement('beforeend', tabboxHTML);
320-
}
321-
}, 0);
310+
this.compInstance.pluginList[plugin.id].content = tabboxHTML;
322311
}
323312
this.compInstance.pluginList = this.compInstance.pluginList;
324313
});

src/lib/pluginContent.svelte

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<script lang="ts">
2+
import { onMount } from 'svelte';
3+
import { isElement, isString } from './tool';
4+
5+
/*************************************
6+
* Public properties
7+
*************************************/
8+
9+
export let pluginId = '';
10+
export let fixedHeight = false;
11+
export let actived = false;
12+
export let content: HTMLElement | string = undefined;
13+
14+
15+
/*************************************
16+
* Inner properties
17+
*************************************/
18+
19+
let container: HTMLElement = undefined;
20+
let previousId = undefined;
21+
22+
$: {
23+
if (previousId !== pluginId && content && container) {
24+
// console.log('pluginContent onChange', pluginId, previousId)
25+
previousId = pluginId;
26+
container.innerHTML = '';
27+
if (isString(content)) {
28+
container.innerHTML = <string>content;
29+
} else if (isElement(content)) {
30+
container.appendChild(<HTMLElement>content);
31+
}
32+
}
33+
}
34+
35+
/*************************************
36+
* Lifecycle
37+
*************************************/
38+
39+
// onMount(() => {
40+
// console.log('pluginContent onMount', pluginId)
41+
// });
42+
</script>
43+
44+
<div
45+
bind:this={container}
46+
id="__vc_plug_{pluginId}"
47+
class="vc-plugin-box"
48+
class:vc-fixed-height="{fixedHeight}"
49+
class:vc-actived="{actived}"
50+
>
51+
</div>

0 commit comments

Comments
 (0)