Skip to content

Commit

Permalink
fix js initializer (#132)
Browse files Browse the repository at this point in the history
* fix js initializer

* update document
  • Loading branch information
ElderJames authored Jan 21, 2024
1 parent 47524ea commit ed13fda
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
7 changes: 0 additions & 7 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@
$ dotnet add package AntDesign.Charts
```

- 在 `wwwroot/index.html`(WebAssembly) 或 `Pages/_Host.razor`(Server) 中引入静态文件:

```html
<script src="https://unpkg.com/@antv/g2plot@latest/dist/g2plot.min.js"></script>
<script src="_content/AntDesign.Charts/ant-design-charts-blazor.js"></script>
```

- 在 `_Imports.razor` 中加入命名空间

```csharp
Expand Down
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ English | [简体中文](README-zh_CN.md)
$ dotnet add package AntDesign.Charts
```

- Link the static files in `wwwroot/index.html` (WebAssembly) or `Pages/_Host.razor` (Server)

```html
<script src="https://unpkg.com/@antv/g2plot@latest/dist/g2plot.min.js"></script>
<script src="_content/AntDesign.Charts/ant-design-charts-blazor.js"></script>
```

- Add namespace in `_Imports.razor`

```csharp
Expand Down Expand Up @@ -54,12 +47,12 @@ English | [简体中文](README-zh_CN.md)
title = new Title()
{
visible = true,
text = "曲线折线图",
text = "Line",
},
description = new Description()
{
visible = true,
text = "用平滑的曲线代替折线。",
text = "description",
},
padding = "auto",
forceFit = true,
Expand Down
27 changes: 12 additions & 15 deletions src/AntDesign.Charts/wwwroot/AntDesign.Charts.lib.module.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
var beforeStartCalled = false;
var afterStartedCalled = false;

export function beforeWebStart() {
export function beforeWebStart() {
loadScriptAndStyle();
}

export function beforeStart(options, extensions) {
loadScriptAndStyle();
loadScriptAndStyle();
}

function loadScriptAndStyle() {
if (beforeStartCalled) {
return;
}

beforeStartCalled = true;

const interopJS = "_content/AntDesign.Charts/ant-design-charts-blazor.js";
const cdnJS = "https://unpkg.com/@antv/[email protected]/dist/g2plot.min.js";
const localJS = "_content/AntDesign.Charts/g2plot.min.js";
const cdnFlag = document.querySelector('[use-ant-design-charts-cdn]');

if (!document.querySelector(`[src="${interopJS}"]`)) {
var chartJS = cdnFlag ? cdnJS : localJS;
var chartScript = document.createElement('script');
const chartJS = cdnFlag ? cdnJS : localJS;
const chartScript = document.createElement('script');
chartScript.setAttribute('src', chartJS);

document.body.insertBefore(chartScript, document.body.querySelector("script"));
const jsMark = document.querySelector("script");
if (jsMark) {
jsMark.before(chartScript);
}
else {
document.body.appendChild(chartScript);
}

var interopScript = document.createElement('script');
const interopScript = document.createElement('script');
interopScript.setAttribute('src', interopJS);
chartScript.after(interopScript);
}
Expand Down

0 comments on commit ed13fda

Please sign in to comment.