This is an automated email from the ASF dual-hosted git repository.

ovilia pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/echarts-custom-series.git


The following commit(s) were added to refs/heads/main by this push:
     new 26770cc  chore: update create template
26770cc is described below

commit 26770cc8a2a42c17faa98b0c3273e3aba41bd819
Author: Ovilia <[email protected]>
AuthorDate: Wed Sep 11 10:13:02 2024 +0800

    chore: update create template
---
 scripts/create.js                 | 39 ++++++++++++++++++++++++++++++++++++++-
 scripts/template/package.json     |  2 +-
 scripts/template/rollup.config.js |  7 ++-----
 scripts/template/src/index.ts     | 20 ++++++++++++++------
 scripts/template/test/index.html  |  7 +++++++
 5 files changed, 62 insertions(+), 13 deletions(-)

diff --git a/scripts/create.js b/scripts/create.js
index 5f6feb8..dbf079d 100644
--- a/scripts/create.js
+++ b/scripts/create.js
@@ -1,3 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 const fs = require('fs');
 const path = require('path');
 
@@ -16,7 +35,9 @@ function create(name) {
   fs.mkdirSync(seriesPath);
   copyDirectory(templatePath, seriesPath);
 
-  // Replace $$$ in package.json with <name>
+  // Replace `$CUSTOM_SERIES_NAME$` in all files under seriesPath with <name>
+  replaceCustomSeriesName(seriesPath, name);
+
   const packageJsonPath = path.join(seriesPath, 'package.json');
   const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
   packageJson.name = `echarts-${name}`;
@@ -30,6 +51,22 @@ function create(name) {
   console.log(`Run "npm run build ${name}" to build the custom series`);
 }
 
+/**
+ * Recursively replace the custom series name in the file
+ */
+function replaceCustomSeriesName(filePath, name) {
+  if (fs.statSync(filePath).isDirectory()) {
+    const items = fs.readdirSync(filePath);
+    items.forEach((item) => {
+      replaceCustomSeriesName(path.join(filePath, item), name);
+    });
+  } else {
+    const content = fs.readFileSync(filePath, 'utf-8');
+    const newContent = content.replace(/\$CUSTOM_SERIES_NAME\$/g, name);
+    fs.writeFileSync(filePath, newContent);
+  }
+}
+
 function copyDirectory(src, dest) {
   // Check if source directory exists
   if (!fs.existsSync(src)) {
diff --git a/scripts/template/package.json b/scripts/template/package.json
index 0e4c6f8..6001e14 100644
--- a/scripts/template/package.json
+++ b/scripts/template/package.json
@@ -1,5 +1,5 @@
 {
-  "name": "echarts-$$$",
+  "name": "echarts-template",
   "version": "0.0.1",
   "description": "",
   "main": "dist/index.js",
diff --git a/scripts/template/rollup.config.js 
b/scripts/template/rollup.config.js
index fb381b8..44ccfb5 100644
--- a/scripts/template/rollup.config.js
+++ b/scripts/template/rollup.config.js
@@ -11,7 +11,7 @@ module.exports = {
   },
   plugins: [
     {
-      name: 'add-window-assignment',
+      name: 'add-license',
       renderChunk(code) {
         return `/*
 * Licensed to the Apache Software Foundation (ASF) under one
@@ -31,10 +31,7 @@ module.exports = {
 * specific language governing permissions and limitations
 * under the License.
 */
-${code}
-if (typeof window !== 'undefined') {
-  window.$CUSTOM_SERIES_NAME$CustomSeriesInstaller = 
violinCustomSeriesInstaller;
-}`;
+${code}`;
       },
     },
   ],
diff --git a/scripts/template/src/index.ts b/scripts/template/src/index.ts
index 154f721..cd848f8 100644
--- a/scripts/template/src/index.ts
+++ b/scripts/template/src/index.ts
@@ -17,8 +17,12 @@
  * under the License.
  */
 
-import echarts, { CustomSeriesRenderItem } from 'echarts';
-import type { CustomRootElementOption } from 
'echarts/types/src/chart/custom/CustomSeries.d.ts';
+import echarts from 'echarts';
+import type {
+  CustomRootElementOption,
+  CustomSeriesRenderItem,
+} from 'echarts/types/src/chart/custom/CustomSeries.d.ts';
+import type { EChartsExtensionInstallRegisters } from 
'echarts/src/extension.ts';
 
 const renderItem = (
   params: echarts.CustomSeriesRenderItemParams,
@@ -30,7 +34,11 @@ const renderItem = (
   } as CustomRootElementOption;
 };
 
-echarts.registerCustomSeries(
-  'violin',
-  renderItem as unknown as CustomSeriesRenderItem
-);
+export default {
+  install(registers: EChartsExtensionInstallRegisters) {
+    registers.registerCustomSeries(
+      '$CUSTOM_SERIES_NAME$',
+      renderItem as unknown as CustomSeriesRenderItem
+    );
+  },
+};
diff --git a/scripts/template/test/index.html b/scripts/template/test/index.html
index cdd1b16..b55ae08 100644
--- a/scripts/template/test/index.html
+++ b/scripts/template/test/index.html
@@ -13,9 +13,16 @@
     <script src="../node_modules/echarts/dist/echarts.js"></script>
     <script src="../dist/index.js"></script>
     <script>
+        echarts.use(window.$CUSTOM_SERIES_NAME$CustomSeriesInstaller);
         const chart = echarts.init(document.getElementById('main'));
 
         option = {
+            series: {
+                type: 'custom',
+                renderItem: '$CUSTOM_SERIES_NAME$',
+                itemPayload: {
+                }
+            }
         };
 
         chart.setOption(option);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to