ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import { nextTick, reactive, ref, toRefs, unref, watch } from 'vue';
 
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
import { JnpfUniver, JnpfUniverPrint } from '@jnpf/univer';
 
import { usePreferences } from '@vben/preferences';
 
import { useDebounceFn } from '@vueuse/core';
 
import logoImg from '#/assets/images/jnpf.png';
import { $t } from '#/locales';
 
import ConfigForm from './ConfigForm.vue';
 
defineOptions({ name: 'ReportPrint' });
 
interface State {
  reportData: any;
  formState: any;
  jnpfUniverAPI: any;
  configOptions: any;
  sheetsOption: any;
  loading: boolean;
  printRenderTip: string;
  configLoading: boolean;
  internalOpen: boolean;
  activeSheetFreeze: any;
  watermarkCache: any;
}
const [registerModal, { closeModal }] = useModalInner(init);
const defaultRenderProgressTip = ' '; // 默认的加载文案
 
const state = reactive<State>({
  reportData: {},
  jnpfUniverAPI: null,
  formState: {},
  configOptions: {},
  sheetsOption: [],
  loading: false,
  printRenderTip: defaultRenderProgressTip,
  configLoading: false,
  internalOpen: false,
  activeSheetFreeze: {},
  watermarkCache: {},
});
const { jnpfUniverAPI, formState, configOptions, sheetsOption, loading, printRenderTip, configLoading, internalOpen, activeSheetFreeze } = toRefs(state);
const { isDark } = usePreferences();
const jnpfUniverRef = ref();
const jnpfUniverPrintRef = ref();
const printConfigFormRef = ref();
 
const debounceHandlePrintConfig = useDebounceFn(() => {
  handleChangePrintConfig(state.formState);
}, 300);
 
watch(
  () => state.formState,
  () => {
    state.loading = true;
    debounceHandlePrintConfig();
  },
  { deep: true },
);
 
async function init(data) {
  state.reportData = data;
  state.loading = true;
  state.internalOpen = true;
  state.configLoading = true;
  state.printRenderTip = defaultRenderProgressTip;
  state.watermarkCache = { ...data.watermark };
 
  const { snapshot, sheetId } = data ?? {};
  state.activeSheetFreeze = snapshot?.sheets?.[sheetId]?.freeze ?? {};
 
  handleCreate(data.snapshot);
}
// 创建报表实例
function handleCreate(snapshot) {
  const defaultActiveSheetId = state.reportData.sheetId ?? null;
 
  nextTick(() => {
    const res = unref(jnpfUniverRef)?.handleCreateDesignUnit({
      mode: 'print',
      snapshot,
      loading: false,
      defaultActiveSheetId,
      darkMode: isDark.value,
    });
    state.jnpfUniverAPI = res ? res?.jnpfUniverAPI : null;
 
    state.jnpfUniverAPI?.getHooks()?.onSteady(() => {
      const sheetsInfo = unref(jnpfUniverRef).getSheetsInfo() ?? {};
      const partSheets: any = [];
      state.sheetsOption = Object.values(sheetsInfo).map((item: any) => {
        partSheets.push(item.id);
 
        return {
          id: item.id,
          fullName: item.name,
        };
      });
 
      const configOptions = unref(jnpfUniverRef).getPrintConfigs() ?? {};
      configOptions.defaultConfigForm.partSheets = partSheets;
      state.configOptions = configOptions;
 
      state.formState = { ...state.configOptions.defaultConfigForm, workbookTitleText: state.reportData.fullName };
      state.configLoading = false;
 
      nextTick(() => {
        unref(printConfigFormRef).handleChangedPrintAreaType();
      });
    });
  });
}
// 销毁示例
function handleDisposeUnit() {
  unref(jnpfUniverRef)?.handleDisposeUnit();
  state.jnpfUniverAPI = null;
}
function handleCancel() {
  handleDisposeUnit();
 
  setTimeout(() => {
    closeModal();
  }, 500);
}
// 打印
function handlePrint() {
  unref(jnpfUniverPrintRef)?.handleBrowserPrint();
}
function handleChangePrintConfig(config: any) {
  state.printRenderTip = defaultRenderProgressTip;
 
  if (state.jnpfUniverAPI) {
    unref(jnpfUniverPrintRef)?.handlePrintRender(config, state.watermarkCache);
  }
}
// 获取打印预览渲染进度
function getPreviewRenderProgress(res: any) {
  const { total, rendered } = res ?? {};
 
  if (total >= 10) state.printRenderTip = `共${total}页,已加载${rendered}页`;
  if (rendered === total) {
    state.loading = false;
 
    /**
     * 下面的内容主要是为了实现打印全部工作表
     * 必须如此处理,不然打印所有工作表会出现缺失
     */
    // 获取工作簿id
    const activeWorkbook = state.jnpfUniverAPI?.getActiveWorkbook();
 
    // 获取到所有的工作表
    const sheets = activeWorkbook.save().sheets;
    const sheetKeys = Object.keys(sheets);
    const activeSubUnitId = activeWorkbook?.getActiveSheet()?.getSheetId();
 
    // 自动缓存所有页签
    function autoRenderAllSheets(index: number) {
      if (index >= sheetKeys?.length) {
        // 切换到最初激活的工作表
        unref(jnpfUniverRef)?.setWorksheetActiveOperation(activeSubUnitId);
        return;
      }
 
      const sheetKey = sheetKeys[index];
 
      unref(jnpfUniverRef)?.setWorksheetActiveOperation(sheetKey);
 
      setTimeout(() => {
        autoRenderAllSheets(index + 1);
      }, 100); // 加延迟才能切换命令执行无效
    }
 
    autoRenderAllSheets(0);
  }
}
// 获取打印浏览器渲染进度
function getPrintRenderProgress(res: any) {
  const { total, rendered } = res ?? {};
 
  if (total >= 10) {
    state.printRenderTip = `共${total}页,已准备${rendered}页`;
    if (rendered === total) state.printRenderTip = '整理中...';
  }
}
// 打印准备好了
function handleBrowserShow() {
  state.loading = false;
}
async function handleClose() {
  state.internalOpen = false;
  return true;
}
</script>
 
<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    default-fullscreen
    :footer="null"
    :closable="false"
    :keyboard="false"
    :close-func="handleClose"
    destroy-on-close
    class="jnpf-full-modal full-modal designer-modal jnpf-report-print-modal">
    <template #title>
      <div class="jnpf-full-modal-header">
        <div class="header-title">
          <img :src="logoImg" class="header-logo" />
          <span class="header-dot"></span>
          <p class="header-txt">报表打印预览</p>
        </div>
        <a-space class="options" :size="10">
          <a-button type="primary" :disabled="loading" @click="handlePrint()">{{ $t('common.printText') }}</a-button>
          <a-button @click="handleCancel()">{{ $t('common.cancelText') }}</a-button>
        </a-space>
      </div>
    </template>
    <div class="print-container" v-loading="loading" :loading-tip="printRenderTip">
      <div class="print-design-content">
        <JnpfUniver v-if="internalOpen" ref="jnpfUniverRef" />
      </div>
      <div class="print-main-container">
        <div class="main-warp">
          <JnpfUniverPrint
            ref="jnpfUniverPrintRef"
            :jnpf-univer-api="jnpfUniverAPI"
            @preview-render-progress="getPreviewRenderProgress"
            @print-render-progress="getPrintRenderProgress"
            @browser-print-show="handleBrowserShow" />
        </div>
        <div class="config-warp">
          <ConfigForm
            ref="printConfigFormRef"
            v-if="!configLoading"
            :form-state="formState"
            :config-options="configOptions"
            :sheets-option="sheetsOption"
            :active-sheet-freeze="activeSheetFreeze" />
        </div>
      </div>
    </div>
  </BasicModal>
</template>
 
<style lang="scss">
.jnpf-full-modal.jnpf-report-print-modal {
  .ant-modal-body > .scrollbar {
    padding: 0 !important;
  }
 
  .print-container {
    position: relative;
    z-index: 1000;
    flex: 1;
    height: 100%;
 
    .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;
 
      .main-warp {
        display: flex;
        flex: 1 0 0;
        height: 100%;
        overflow: auto;
      }
 
      .config-warp {
        flex: 270px 0 0;
        min-width: 270px;
        overflow: hidden auto;
 
        .ant-radio-button-wrapper {
          padding: 0 11px;
        }
      }
    }
  }
}
</style>