slidev-addon-infographic 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,17 +2,30 @@
2
2
 
3
3
  [@antv/infographic](https://infographic.antv.vision/) component for slidev
4
4
 
5
+ ## Usage Process
6
+
5
7
  ``` sh
6
8
  pnpm i slidev-addon-infographic --save
7
9
  ```
8
10
 
11
+ Add the following to your `package.json`:
12
+
13
+ ``` json
14
+ ...
15
+ "slidev": {
16
+ "addons": [
17
+ "slidev-addon-infographic"
18
+ ]
19
+ }
20
+ ...
21
+ ```
22
+
9
23
  Add the following to your `slides.md`:
10
24
 
11
25
  ``` md
12
26
  ---
13
- layout: center
14
27
  addons:
15
- - slidev-addon-infographic
28
+ - slidev-addon-infographic
16
29
  ---
17
30
 
18
31
  <RenderWhen
@@ -31,13 +44,33 @@ addons:
31
44
  </RenderWhen>
32
45
  ```
33
46
 
34
- - **Infographic component must be used with RenderWhen component to show infographic in slidev**
35
- - **The Infographic component must occupy the entire page or be contained within elements with dimensions**
36
- - **Temporarily supports exporting in PPTX and PNG formats**
47
+ [RenderWhen](https://sli.dev/builtin/components#renderwhen)
48
+
49
+ - **Infographic component must be used with RenderWhen component to show infographic in slidev.**
50
+ - **If you want to export PDF format, please remove `visible` in `RenderWhen` component's `context` attribute.**
51
+
52
+ 1. **Infographic组件必须与RenderWhen组件一起使用,才能在幻灯片中显示信息图。**
53
+ 2. **如需导出 PDF 格式,请将 `RenderWhen` 组件的 `context` 属性中的 `visible` 去除。**
54
+
55
+ ## Parameter Description
56
+
57
+ | Parameter | Description | Type | Default | Version |
58
+ | --------- | ----------- | ---- | ------- | ------- |
59
+ | data | Infographic data | string | - | 1.0.0 |
60
+ | click | Whether to respond to [Slidev click animation](https://sli.dev/guide/animations#click-animation), If set to `true`, you need to add a [`clicks`](https://sli.dev/guide/animations#total) field in the `frontmatter` with the value being the number of clicks. | boolean | false | 1.0.4 |
61
+
62
+ - **Note: `click` is primarily used for demonstrating effects, and it is recommended to turn it off when exporting.**
63
+ - **Note: After setting `click` to `true` and adding the `clicks` count, you need to click yourself to confirm the effect.**
64
+
65
+ | 参数 | 描述 | 类型 | 默认值 | 版本 |
66
+ | ---- | ---- | ---- | ------ | ---- |
67
+ | data | 信息图数据 | string | - | 1.0.0 |
68
+ | click | 是否响应[Slidev点击动画](https://cn.sli.dev/guide/animations#click-animation),如果设置为 `true`,需要在 `frontmatter` 中添加 [`clicks`](https://cn.sli.dev/guide/animations#total) 字段,值为点击次数。 | boolean | false | 1.0.4 |
69
+
70
+ 1. **注意:`click` 主要用于演示效果,导出时建议关闭。**
71
+ 2. **注意:设置 `click` 为 `true` 后,并添加 `clicks` 点击次数之后的效果需要自行点击确认。**
37
72
 
38
- 1. **Infographic组件必须与RenderWhen组件一起使用,才能在幻灯片中显示信息图**
39
- 2. **Infographic组件必须占用整个页面或被包含在带尺寸的元素中**
40
- 3. **暂时支持导出 PPTX, PNG 格式**
73
+ ## Slidev
41
74
 
42
75
  Edit the [slides.md](./slides.md) to see the changes.
43
76
 
@@ -3,48 +3,170 @@
3
3
  </template>
4
4
 
5
5
  <script setup>
6
- import { useDarkMode } from '@slidev/client'
6
+ import { useDarkMode, useNav } from '@slidev/client'
7
7
  import {ref, watch, onMounted, onUnmounted} from 'vue'
8
8
  import {Infographic} from '@antv/infographic'
9
9
 
10
- const props = defineProps(['data'])
10
+ const props = defineProps({
11
+ data: {
12
+ type: String,
13
+ default: '',
14
+ },
15
+ click: {
16
+ type: Boolean,
17
+ default: false,
18
+ }
19
+ })
11
20
 
12
21
  const containerRef = ref(null)
13
- let infographic = null
22
+ let infographic = ref(null)
14
23
  const { isDark } = useDarkMode()
24
+ const nav = useNav()
25
+ const parseStringRef = ref(null)
15
26
 
16
- function initInfographic(data) {
17
- if (infographic) infographic.destroy()
18
- infographic = new Infographic({
27
+ function initInfographic() {
28
+ if (infographic.value) infographic.value.destroy()
29
+ infographic.value = new Infographic({
19
30
  container: containerRef.value,
20
31
  width: '100%',
21
32
  height: '100%',
22
33
  })
23
- data && infographic.render(data)
34
+ if (!props.data) return
35
+ if (props.click) {
36
+ if (!parseStringRef.value) parseStringRef.value = parseString(props.data)
37
+ const clickNum = nav.currentRoute.value.query.clicks || 0
38
+ infographic.value.render(`${parseStringRef.value.strNoData}
39
+ ${parseStringRef.value.strDataNoItems}
40
+ items
41
+ ${parseStringRef.value.itemsArray.slice(0, Math.min(clickNum * 1 + 1, parseStringRef.value.itemsArray.length)).join('\n')}`)
42
+ } else {
43
+ infographic.value.render(props.data)
44
+ }
24
45
  }
25
46
 
26
47
  onMounted(() => {
27
- initInfographic(props.data)
48
+ initInfographic()
28
49
  })
29
50
 
30
51
  onUnmounted(() => {
31
- if (infographic) {
32
- infographic.destroy()
52
+ if (infographic.value) {
53
+ infographic.value.destroy()
33
54
  }
34
55
  })
35
56
 
36
57
  watch(() => props.data, (newData) => {
37
- newData && initInfographic(newData)
58
+ if (newData) {
59
+ parseStringRef.value = null
60
+ initInfographic()
61
+ }
62
+ })
63
+
64
+ watch(() => props.click, (val) => {
65
+ parseStringRef.value = null
66
+ initInfographic()
38
67
  })
68
+
69
+ watch(() => nav.currentRoute.value.query.clicks, () => {
70
+ initInfographic()
71
+ })
72
+
39
73
  watch(() => isDark.value, (newDark) => {
40
74
  if (newDark) {
41
- infographic.update({
75
+ infographic.value.update({
42
76
  theme: 'dark',
43
77
  })
44
78
  } else {
45
- infographic.update({
79
+ infographic.value.update({
46
80
  theme: 'light',
47
81
  })
48
82
  }
49
83
  })
84
+
85
+ /**
86
+ * 精准拆分字符串为指定的三个部分
87
+ * @param {string} str - 原始字符串
88
+ * @returns {object} 包含strNoData、strDataNoItems、itemsArray三个结果
89
+ */
90
+ function parseString(str) {
91
+ const lines = str.split('\n')
92
+ let strNoDataLines = [] // 行集合(非data内容)
93
+ let strDataNoItemsLines = [] // 行集合(data内除items外)
94
+ let itemsLines = [] // items的原始行集合
95
+ let inDataBlock = false
96
+ let inItemsBlock = false
97
+ let currentItemBuffer = []
98
+ let dataLineSpaceNum = 0
99
+ let itemsLineSpaceNum = 0
100
+
101
+ lines.forEach(line => {
102
+ if (line.trim() === 'data') {
103
+ inDataBlock = true
104
+ dataLineSpaceNum = line.search(/\S/)
105
+ strDataNoItemsLines.push(line)
106
+ return
107
+ }
108
+
109
+ if (!inDataBlock) {
110
+ strNoDataLines.push(line)
111
+ return
112
+ }
113
+
114
+ if (inDataBlock) {
115
+ if (line.trim() === 'items') {
116
+ itemsLineSpaceNum = line.search(/\S/)
117
+ inItemsBlock = true
118
+ return
119
+ }
120
+
121
+ if (!inItemsBlock) {
122
+ strDataNoItemsLines.push(line)
123
+ return
124
+ }
125
+
126
+ const curLineSpaceNum = line.search(/\S/)
127
+ if (curLineSpaceNum <= itemsLineSpaceNum) {
128
+ inItemsBlock = false
129
+ if (curLineSpaceNum <= dataLineSpaceNum) {
130
+ inDataBlock = false
131
+ strNoDataLines.push(line)
132
+ } else {
133
+ strDataNoItemsLines.push(line)
134
+ }
135
+ return
136
+ }
137
+
138
+ // 处理items区块内的内容
139
+ if (inItemsBlock) {
140
+ itemsLines.push(line)
141
+ return
142
+ }
143
+ }
144
+ })
145
+
146
+ const itemsArray = []
147
+ itemsLines.forEach(line => {
148
+ const curLineSpaceNum = line.search(/\S/)
149
+ if (line.trim().startsWith('- label') && curLineSpaceNum === itemsLineSpaceNum + 2) {
150
+ if (currentItemBuffer.length > 0) {
151
+ itemsArray.push(currentItemBuffer.join('\n'))
152
+ currentItemBuffer = []
153
+ }
154
+ }
155
+ if (line.trim() !== '') {
156
+ currentItemBuffer.push(line)
157
+ }
158
+ })
159
+ if (currentItemBuffer.length > 0) {
160
+ itemsArray.push(currentItemBuffer.join('\n'))
161
+ }
162
+
163
+ const strNoData = strNoDataLines.join('\n')
164
+ const strDataNoItems = strDataNoItemsLines.join('\n')
165
+
166
+ return {
167
+ strNoData,
168
+ strDataNoItems,
169
+ itemsArray
170
+ }
171
+ }
50
172
  </script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slidev-addon-infographic",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "type": "module",
5
5
  "author": "fxss5201",
6
6
  "description": "@antv/infographic component for slidev",