刘光辉
11 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<script lang="ts" setup>
  import { onMounted, reactive, ref, toRefs, unref } from 'vue';
 
  import JnpfUniverDesign from '@/components/Report/Design/index.vue';
  import {
    designMockFloatEchartData,
    designMockFloatImageData,
    designMockSnapshotData,
    mockUpdateFloatEchartOption,
    mockUpdateFloatImageOption,
  } from '@/components/Report/Design/mock';
  import { DefaultWatermarkConfig } from '@/components/Report/Design/univer/utils/define';
  import JnpfUniverPreview from '@/components/Report/Preview/index.vue';
  import JnpfUniverPrint from '@/components/Report/Print/index.vue';
  import { IWorkbookData } from '@univerjs/core';
  import { Button as AButton } from 'ant-design-vue';
 
  defineOptions({ name: 'App' });
 
  const jnpfUniverDesignRef = ref();
  const jnpfUniverPreviewRef = ref();
  const jnpfUniverPrintRef = ref();
 
  const state = reactive<{
    focusedCell: any;
 
    focusedFloatEchartId: any;
    focusedFloatImageId: any;
    jnpfUniverAPI: any;
  }>({
    focusedCell: {
      startColumn: null,
      startRow: null,
    },
 
    focusedFloatEchartId: null,
    focusedFloatImageId: null,
    jnpfUniverAPI: null,
  });
  const { focusedCell, focusedFloatEchartId, focusedFloatImageId } = toRefs(state);
 
  // 创建设计器实例
  function initCreate() {
    const { jnpfUniverAPI } = unref(jnpfUniverDesignRef)?.handleCreateDesignUnit(
      {} || {
        floatEcharts: {} || designMockFloatEchartData,
        floatImages: {} || designMockFloatImageData,
        snapshot: designMockSnapshotData,
      },
    );
 
    state.jnpfUniverAPI = jnpfUniverAPI ?? null;
  }
 
  // 缓存悬浮图表的画布id
  function handleFocusFloatEchart(data: any) {
    state.focusedFloatEchartId = data.drawingId;
  }
 
  // 【模拟】 -  更新悬浮图表的配置
  function handleUpdateFloatEchart() {
    unref(jnpfUniverDesignRef)?.updateFloatEchartConfig({
      drawingId: focusedFloatEchartId.value,
 
      echartType: 'line',
      option: mockUpdateFloatEchartOption,
    });
  }
 
  // 缓存悬浮图片的画布id
  function handleFocusFloatImage(data: any) {
    state.focusedFloatImageId = data.drawingId;
  }
 
  //  【模拟】 -  更新悬浮图片的配置
  function handleUpdateFloatImage() {
    unref(jnpfUniverDesignRef)?.updateFloatImageConfig({
      drawingId: focusedFloatImageId.value,
      ...mockUpdateFloatImageOption,
    });
  }
 
  // 缓存选中单元格的行列坐标
  function handleChangeCellFromSelection(cellInfo: any) {
    const { startColumn, startRow } = cellInfo ?? {};
 
    state.focusedCell = {
      startColumn,
      startRow,
    };
  }
 
  //  【模拟】 -  更新静态文本
  function setCellToText() {
    const updateCellsData = [
      {
        ...focusedCell.value,
        cellData: {
          custom: {
            type: 'text',
          },
          t: 1,
          v: '静态文本',
        },
      },
    ];
 
    unref(jnpfUniverDesignRef)?.updateCellsData(updateCellsData);
  }
 
  //  【模拟】 -  更新数据集
  function setCellToDataSource(fillDirection: string) {
    const updateCellsData = [
      {
        ...focusedCell.value,
        cellData: {
          custom: {
            fillDirection,
            leftParentCellCustomColName: '1',
            leftParentCellCustomRowName: 'A',
            leftParentCellType: 'custom',
            topParentCellCustomColName: '1',
            topParentCellCustomRowName: 'B',
            topParentCellType: 'custom',
            type: 'dataSource',
          },
          t: 1,
          v: 'userSex',
        },
      },
    ];
 
    unref(jnpfUniverDesignRef)?.updateCellsData(updateCellsData);
  }
 
  //  【模拟】 -  单元格图表
  function setCellToEchart(echartType: string) {
    const updateCellsData = [
      {
        ...focusedCell.value,
        cellData: {
          custom: {
            echartType,
            type: 'cellEchart',
          },
          echartOption: {
            series: [
              {
                data: [120, 200, 150, 80, 70, 110, 130],
                type: 'bar',
              },
            ],
            xAxis: {
              data: ['周一', '周二', '周三', '周四', '周五', '周六', '周天'],
              type: 'category',
            },
            yAxis: {
              type: 'value',
            },
          },
        },
      },
    ];
 
    unref(jnpfUniverDesignRef)?.updateCellsData(updateCellsData);
  }
 
  //  【模拟】 -  二维码
  function setCellToQrCode() {
    const updateCellsData = [
      {
        ...focusedCell.value,
        cellData: {
          custom: {
            type: 'qrCode',
          },
          qrCodeOption: {
            color: {
              dark: '#000000', // 前景色
              light: '#f2f3f4', // 背景色
            },
            errorCorrectionLevel: 'L',
            margin: 4,
            type: 'static', // 这里预留动态
          },
          t: 1,
          v: 'https://www.baidu.com',
        },
      },
    ];
 
    unref(jnpfUniverDesignRef)?.updateCellsData(updateCellsData);
  }
 
  //  【模拟】 -  条形码
  function setCellToJsbarcode() {
    const updateCellsData = [
      {
        ...focusedCell.value,
        cellData: {
          custom: {
            type: 'jsbarcode',
          },
          jsbarcodeOption: {
            background: '#f2f3f4', // 设置背景色为浅灰色
            displayValue: true, // 显示数字
            font: 'Arial', // 设置字体
            fontSize: 18, // 设置字体大小
            format: 'CODE128', // 设置条形码类型为 EAN13
            lineColor: '#000000', // 设置线条为蓝色
            margin: 15, // 设置条形码四周边距
            textAlign: 'center', // 设置文本居中
            textPosition: 'bottom', // 设置文本在条形码底部
            type: 'static', // 这里预留动态
            width: 3, // 设置条形码宽度
          },
          t: 1,
          v: '1234567890',
        },
      },
    ];
 
    unref(jnpfUniverDesignRef)?.updateCellsData(updateCellsData);
  }
 
  // 打开页面弹窗去选单元格
  function openCellByDialogSelect() {
    unref(jnpfUniverDesignRef)?.getCellFromDialogSelect(focusedCell.value);
  }
 
  // 获取弹窗选择的结果
  function getCellFromDialogSelect(res: any) {
    console.log(`关联的单元格坐标为:${res}`);
  }
 
  // 设置水印
  function handleSetWatermark() {
    // 水印配置
    // x: number;
    // y: number;
    // repeat: boolean;
    // spacingX: number;
    // spacingY: number;
    // rotate: number;
    // opacity: number;
    // content?: string;
    // fontSize: number;
    // color: string;
    // bold: boolean;
    // italic: boolean;
    // direction: 'ltr' | 'rtl' | 'inherit';
    unref(jnpfUniverDesignRef)?.setWatermark(JSON.parse(JSON.stringify(DefaultWatermarkConfig)));
  }
 
  // 清除水印
  function handleClearWatermark() {
    unref(jnpfUniverDesignRef)?.clearWatermark();
  }
 
  // 执行预览
  async function handlePreview() {
    /**
     * 保存时做的事情
     */
    await unref(jnpfUniverDesignRef)?.setCellEditVisible();
    const designWorkbookData = unref(jnpfUniverDesignRef)?.getDesignWorkbookData() ?? {};
 
    /**
     * 预览时要做的事情
     * 这里要把floatEcharts, cellEcharts, floatImages的真实数据对接成功
     */
    // @ts-ignore 下面的常量没用到飘红真令人讨厌
    const { cellEcharts = {}, customs = [], floatEcharts = {}, floatImages = {}, snapshot } = designWorkbookData;
    const activeSubUnitId = unref(jnpfUniverDesignRef)?.getActiveWorksheetId(); // 获取到当前激活的工作表,这样预览的时候,可以更直观的看到当前设计器更改的内容
    console.log(snapshot);
    unref(jnpfUniverPreviewRef)?.handleCreatePreviewUnit({
      cellEcharts,
      defaultActiveSheetId: activeSubUnitId,
      floatEcharts,
      floatImages,
      snapshot,
    });
  }
 
  // 执行打印
  function handlePrint(data: { activeWorksheetId?: string; snapshot: IWorkbookData }) {
    unref(jnpfUniverPrintRef)?.handleCreatePrintUnit(data);
  }
 
  // 销毁实例
  function handleDispose() {
    unref(jnpfUniverDesignRef)?.handleDisposeUnit();
  }
 
  onMounted(() => {
    initCreate();
  });
</script>
 
<template>
  <div class="jnpf-univer-module">
    <!--    测试按钮,随时可以删除掉    -->
    <div class="buttons-wrap">
      <AButton type="primary" style="background: orangered" @click="handlePreview">设计预览</AButton>
      <AButton type="primary" @click="setCellToText">设置纯文本</AButton>
      <AButton type="primary" @click="setCellToDataSource('landscape')">设置数据集</AButton>
      <AButton type="primary" @click="openCellByDialogSelect">选择数据集的父格</AButton>
      <AButton type="primary" @click="setCellToEchart('line')">设置单元格图表</AButton>
      <AButton type="primary" @click="setCellToQrCode">设置单元格二维码</AButton>
      <AButton type="primary" @click="setCellToJsbarcode">设置单元格条形码</AButton>
      <AButton type="primary" style="background: blueviolet" @click="handleUpdateFloatEchart">更新悬浮图表配置</AButton>
      <AButton type="primary" style="background: purple" @click="handleUpdateFloatImage">更新悬浮图片配置</AButton>
      <AButton type="primary" style="background: palevioletred" @click="handleSetWatermark">添加水印</AButton>
      <AButton type="primary" style="background: green" @click="handleClearWatermark">清除水印</AButton>
      <AButton @click="handleDispose">销毁工作簿</AButton>
    </div>
 
    <!--    设计器模块-->
    <JnpfUniverDesign
      ref="jnpfUniverDesignRef"
      @change-cell="handleChangeCellFromSelection"
      @focus-float-echart="handleFocusFloatEchart"
      @focus-float-image="handleFocusFloatImage"
      @change-dialog-select-cell="getCellFromDialogSelect"
      @preview="handlePreview" />
    <!--    设计预览模块-->
    <JnpfUniverPreview ref="jnpfUniverPreviewRef" @print="handlePrint" />
    <!--    打印预览模块-->
    <JnpfUniverPrint ref="jnpfUniverPrintRef" />
  </div>
</template>
 
<style>
  * {
    padding: 0;
    margin: 0;
  }
 
  html {
    height: 100%;
  }
 
  body {
    height: calc(100%);
  }
 
  #app {
    height: calc(100%);
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
 
  .jnpf-univer-module {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: calc(100%);
 
    .ant-btn {
      margin-right: 10px;
    }
  }
 
  .buttons-wrap {
    display: flex;
    align-items: center;
    height: 60px;
    padding: 0 10px;
    background: #ddd;
  }
</style>