刘光辉
12 小时以前 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
<script lang="ts" setup>
  import { nextTick, reactive, ref, toRefs, unref } from 'vue';
 
  import { IWorkbookData } from '@univerjs/core';
  import { SetWorksheetActiveOperation } from '@univerjs/sheets';
  import { Button as AButton, Modal as AModal } from 'ant-design-vue';
 
  import JnpfUniverDesign from '../Design/index.vue';
  import { DefaultPrintConfig } from '../Design/univer/utils/define';
  import JnpfUniverPrintForm from './Form/index.vue';
  import JnpfUniverPrintRender from './Render/index.vue';
 
  const jnpfUniverDesignPrintRef = ref();
  const jnpfUniverPrintRenderRef = ref();
 
  // 默认数据
  const defaultRenderProgressTip = ' '; // 加载文案
  const defaultFreezeStatus = {
    colHasFreeze: false,
    // 是否存在冻结项
    rowHasFreeze: false,
  };
 
  interface State {
    internalOpen: boolean;
 
    jnpfUniverAPI: any;
    loading: boolean;
 
    printRenderTip: string;
 
    sheetFreezeStatus: any;
  }
  const state = reactive<State>({
    internalOpen: false,
 
    jnpfUniverAPI: null,
    loading: false,
 
    printRenderTip: defaultRenderProgressTip,
 
    sheetFreezeStatus: { ...defaultFreezeStatus },
  });
  const { internalOpen, jnpfUniverAPI, loading, printRenderTip, sheetFreezeStatus } = toRefs(state);
 
  // 创建打印实例
  function handleCreatePrintUnit(data: { defaultActiveSheetId?: string; snapshot: IWorkbookData }) {
    state.internalOpen = true;
 
    state.printRenderTip = defaultRenderProgressTip;
    state.loading = true;
 
    const { defaultActiveSheetId, snapshot } = data ?? {};
 
    nextTick(() => {
      const { jnpfUniverAPI: jnpfUniverAPICache } = unref(jnpfUniverDesignPrintRef)?.handleCreateDesignUnit({
        defaultActiveSheetId,
        loading: false,
        mode: 'print',
        snapshot,
      });
      state.jnpfUniverAPI = jnpfUniverAPICache ?? null;
 
      // ??? 这个生命周期待测试,看能不能提前
      jnpfUniverAPI.value?.getHooks()?.onSteady(() => {
        // 获取激活的工作表id
        const subUnitId = defaultActiveSheetId ?? jnpfUniverAPI.value?.getActiveWorkbook()?.getActiveSheet()?.getSheetId();
 
        // 判断行列的冻结状态
        const { xSplit = 0, ySplit = 0 } = snapshot?.sheets?.[subUnitId]?.freeze ?? {};
        const colHasFreeze = xSplit > 0; // 列冻结(冻结左侧的列)
        const rowHasFreeze = ySplit > 0; // 行冻结(冻结上方的行)
        state.sheetFreezeStatus = {
          colHasFreeze, // 列冻结
          rowHasFreeze, // 行冻结
        };
 
        console.log('☕️ The underlying univer has been rendered, and the print preview page is being rendered.');
        unref(jnpfUniverPrintRenderRef)?.handlePrintRender(JSON.parse(JSON.stringify(DefaultPrintConfig)));
      });
    });
  }
 
  // 变更打印配置
  function handleChangePrintConfig(config: any) {
    state.printRenderTip = defaultRenderProgressTip;
    state.loading = true;
 
    console.log('✋ You have switched the print configuration.');
    unref(jnpfUniverPrintRenderRef)?.handlePrintRender(JSON.parse(JSON.stringify(config)));
  }
 
  // 获取打印预览渲染进度
  function getPreviewRenderProgress(res: any) {
    const { rendered, total } = res ?? {};
    state.printRenderTip = `共${total}页,已加载${rendered}页`;
 
    if (rendered === total) {
      console.log('🎆 The preview page has been fully rendered.');
      state.loading = false;
 
      /**
       * 下面的内容主要是为了实现打印全部工作表
       * 必须如此处理,不然打印所有工作表会出现缺失
       */
      // 获取工作簿id
      const activeWorkbook = jnpfUniverAPI.value?.getActiveWorkbook();
      const unitId = activeWorkbook?.getId();
 
      // 获取到所有的工作表
      const sheets = activeWorkbook.save().sheets;
      const sheetKeys = Object.keys(sheets);
      const activeSubUnitId = activeWorkbook?.getActiveSheet()?.getSheetId();
 
      // 自动缓存所有页签
      function autoRenderAllSheets(index: number) {
        if (index >= sheetKeys?.length) {
          // 切换到最初激活的工作表
          jnpfUniverAPI.value?.executeCommand(SetWorksheetActiveOperation.id, {
            subUnitId: activeSubUnitId,
            unitId,
          });
          return;
        }
 
        const sheetKey = sheetKeys[index];
 
        jnpfUniverAPI.value?.executeCommand(SetWorksheetActiveOperation.id, {
          subUnitId: sheetKey,
          unitId,
        });
 
        setTimeout(() => {
          autoRenderAllSheets(index + 1);
        }, 100); // 加延迟才能切换命令执行无效
      }
 
      autoRenderAllSheets(0);
    }
  }
 
  // 执行浏览器打印
  function handleBrowserPrint() {
    state.printRenderTip = defaultRenderProgressTip;
    state.loading = true;
 
    unref(jnpfUniverPrintRenderRef)?.handleBrowserPrint();
  }
 
  // 获取打印浏览器渲染进度
  function getPrintRenderProgress(res: any) {
    const { rendered, total } = res ?? {};
 
    state.printRenderTip = `共${total}页,已准备${rendered}页`;
 
    if (rendered === total) {
      state.printRenderTip = '整理中...';
    }
  }
 
  // 打印准备好了
  function handleBrowserShow() {
    state.loading = false;
  }
 
  // 关闭弹窗(建议有个时间缓存周期再关闭弹窗,要销毁设计器)
  function handleCancel() {
    resetUniverState();
    unref(jnpfUniverDesignPrintRef)?.handleDisposeUnit();
  }
 
  // 重置实例状态
  function resetUniverState() {
    state.jnpfUniverAPI = null;
    state.loading = false;
    state.printRenderTip = defaultRenderProgressTip;
    state.sheetFreezeStatus = JSON.parse(JSON.stringify(defaultFreezeStatus));
 
    unref(jnpfUniverPrintRenderRef)?.resetState();
  }
 
  defineExpose({ handleCreatePrintUnit });
</script>
 
<template>
  <AModal v-model:open="internalOpen" title="打印预览" width="100%" wrap-class-name="print-full-modal" @cancel="handleCancel">
    <div class="content-main" v-loading="loading" :loading-tip="printRenderTip">
      <!--    测试按钮,随时可以删除掉    -->
      <div class="buttons-wrap">
        <AButton type="primary" @click="handleBrowserPrint">浏览器打印</AButton>
      </div>
 
      <div class="print-container">
        <!--        设计器被隐藏-->
        <div class="print-design-content">
          <JnpfUniverDesign v-if="internalOpen" ref="jnpfUniverDesignPrintRef" />
        </div>
 
        <div class="print-main-container">
          <!--        视图效果-->
          <JnpfUniverPrintRender
            ref="jnpfUniverPrintRenderRef"
            :jnpf-univer-api="jnpfUniverAPI"
            @preview-render-progress="getPreviewRenderProgress"
            @print-render-progress="getPrintRenderProgress"
            @browser-print-show="handleBrowserShow" />
 
          <!--          配置表单-->
          <JnpfUniverPrintForm
            v-if="internalOpen"
            :default-print-config="DefaultPrintConfig"
            :sheet-freeze-status="sheetFreezeStatus"
            @change="handleChangePrintConfig" />
        </div>
      </div>
    </div>
  </AModal>
</template>
 
<style lang="scss">
  .print-full-modal {
    .ant-modal {
      top: 0;
      max-width: 100%;
      padding-bottom: 0;
      margin: 0;
    }
 
    .ant-modal-content {
      display: flex;
      flex-direction: column;
      height: calc(100vh);
      padding: 20px 10px 0;
    }
 
    .ant-modal-body {
      display: flex;
      flex: 1;
 
      .content-main {
        display: flex;
        flex: 1;
        flex-direction: column;
      }
 
      .print-container {
        position: relative;
        flex: 1;
 
        .print-design-content {
          position: absolute;
          inset: 0;
          z-index: 2;
          display: flex;
 
          .jnpf-univer-design-content {
            opacity: 0;
          }
        }
 
        .print-main-container {
          position: absolute;
          inset: 0;
          z-index: 99;
          display: flex;
          overflow: hidden;
          background: #f2f3f4;
        }
      }
    }
 
    .ant-modal-footer {
      display: none;
    }
  }
</style>