template-replacement 3.3.3 → 3.4.0

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.
Files changed (56) hide show
  1. package/.editorconfig +8 -0
  2. package/.oxfmtrc.jsonc +7 -0
  3. package/.oxlintrc.json +3 -0
  4. package/README.md +39 -9
  5. package/core/base.ts +54 -55
  6. package/core/general.ts +37 -9
  7. package/core/sign.ts +43 -8
  8. package/dispatcher/general.ts +2 -1
  9. package/dispatcher/sign.ts +2 -1
  10. package/dispatcher/workerGeneral.ts +1 -1
  11. package/dispatcher/workerSign.ts +1 -1
  12. package/dist/base-DQz39fXI.js +249 -0
  13. package/dist/index-oILo_kXG.js +46 -0
  14. package/dist/main/general.js +1334 -1384
  15. package/dist/main/sign.js +1476 -1514
  16. package/dist/replace/general.js +283 -313
  17. package/dist/replace/sign.js +309 -327
  18. package/download/index.ts +13 -13
  19. package/download/stream.ts +29 -28
  20. package/eslint.config.ts +28 -0
  21. package/fileSystem/db/index.ts +25 -25
  22. package/fileSystem/db/indexedDBCache.ts +145 -143
  23. package/fileSystem/index.ts +5 -8
  24. package/fileSystem/interface.ts +4 -5
  25. package/fileSystem/opfs/index.ts +40 -36
  26. package/helper/index.ts +136 -125
  27. package/index.ts +6 -6
  28. package/office/zip.ts +106 -97
  29. package/package.json +14 -8
  30. package/replace/base.ts +203 -222
  31. package/replace/general.ts +44 -24
  32. package/replace/image.ts +88 -90
  33. package/replace/interface.ts +29 -24
  34. package/replace/paramsData.ts +107 -95
  35. package/replace/sign.ts +79 -52
  36. package/task/urlDownloadTask.ts +53 -55
  37. package/temp/index.ts +139 -124
  38. package/temp/interface.ts +8 -8
  39. package/tsconfig.json +1 -1
  40. package/vite.config.ts +11 -14
  41. package/worker/child/agency.ts +49 -41
  42. package/worker/child/base.ts +125 -89
  43. package/worker/child/general.ts +2 -3
  44. package/worker/child/sign.ts +4 -4
  45. package/worker/index.ts +52 -53
  46. package/worker/interface.ts +9 -6
  47. package/worker/main/general.ts +5 -5
  48. package/worker/main/index.ts +191 -66
  49. package/worker/main/sign.ts +5 -5
  50. package/worker/type.ts +16 -15
  51. package/dist/assets/template_replacement_core_wasm_bg.wasm +0 -0
  52. package/dist/assets/template_replacement_sign_core_wasm_bg.wasm +0 -0
  53. package/dist/base-CJv023nf.js +0 -284
  54. package/dist/general.d.ts +0 -1
  55. package/dist/index-tFDVIkZX.js +0 -46
  56. package/dist/sign.d.ts +0 -1
package/replace/image.ts CHANGED
@@ -1,25 +1,23 @@
1
- // import { add_media } from "template-replacement-core-wasm"
2
-
3
1
  export function pxToEMU(px: number): number {
4
- return px * (914400 / 96)
2
+ return px * (914400 / 96)
5
3
  }
6
4
 
7
5
  export function cmToEMU(cm: number): number {
8
- return cm * 914400
6
+ return cm * 914400
9
7
  }
10
8
 
11
9
  export enum textWrapTypes {
12
- embed = "Embed",//嵌入型
13
- belowText = "BelowText",//嵌于文字下方
14
- aboveText = "AboveText",//嵌于文字上方
10
+ embed = 'Embed', //嵌入型
11
+ belowText = 'BelowText', //嵌于文字下方
12
+ aboveText = 'AboveText', //嵌于文字上方
15
13
  }
16
14
 
17
15
  function getFileExtension(filename: string): string {
18
- const ext = filename.split('.').pop()
19
- if (ext === undefined) {
20
- return ''
21
- }
22
- return '.' + ext
16
+ const ext = filename.split('.').pop()
17
+ if (ext === undefined) {
18
+ return ''
19
+ }
20
+ return '.' + ext
23
21
  }
24
22
 
25
23
  // async function generateId(file: Blob): Promise<string> {
@@ -28,101 +26,101 @@ function getFileExtension(filename: string): string {
28
26
  // }
29
27
 
30
28
  export type extent = {
31
- cy: number,
32
- cx: number,
29
+ cy: number
30
+ cx: number
33
31
  }
34
32
 
35
33
  //图片替换
36
34
  export default class image {
37
- file: Blob
38
- relationship = 'image'
39
- id?: string
35
+ file: Blob
36
+ relationship = 'image'
37
+ id?: string
40
38
 
41
- wpExtent?: extent //图片宽高
42
- textWrap = textWrapTypes.embed //文字环绕
39
+ wpExtent?: extent //图片宽高
40
+ textWrap = textWrapTypes.embed //文字环绕
43
41
 
44
- awaitInitQueue?: Function[] = []
42
+ private _awaitInitQueue?: ((value?: unknown) => void)[] = []
45
43
 
46
- constructor(file: Blob) {
47
- if (file instanceof Blob) {
48
- this.file = file
49
- this.init()
50
- } else {
51
- throw new Error("不支持的数据类型");
52
- }
44
+ constructor(file: Blob) {
45
+ if (file instanceof Blob) {
46
+ this.file = file
47
+ this.init()
48
+ } else {
49
+ throw new Error('不支持的数据类型')
53
50
  }
51
+ }
54
52
 
55
- async init(): Promise<void> {
56
- this.awaitInitQueue = []
57
- await this.getExtent()
58
- for (const resolve of this.awaitInitQueue) {
59
- resolve()
60
- }
61
- delete this.awaitInitQueue
53
+ async init(): Promise<void> {
54
+ this._awaitInitQueue = []
55
+ await this.getExtent()
56
+ for (const resolve of this._awaitInitQueue) {
57
+ resolve()
62
58
  }
63
-
64
- // async generateId(): Promise<string> {
65
- // this.id = await generateId(this.file)
66
- // return this.id
67
- // }
68
-
69
- async awaitInit(): Promise<void> {
70
- if (this.awaitInitQueue) {
71
- await new Promise(resolve => {
72
- this.awaitInitQueue?.push(resolve)
73
- })
74
- }
59
+ this._awaitInitQueue = undefined
60
+ }
61
+
62
+ // async generateId(): Promise<string> {
63
+ // this.id = await generateId(this.file)
64
+ // return this.id
65
+ // }
66
+
67
+ async awaitInit(): Promise<void> {
68
+ if (this._awaitInitQueue) {
69
+ await new Promise((resolve) => {
70
+ this._awaitInitQueue?.push(resolve)
71
+ })
75
72
  }
76
-
77
- async getExtent(): Promise<extent> {
78
- if (!this.wpExtent) {
79
- if (this.file.size) {
80
- try {
81
- const bitmap = await createImageBitmap(this.file)
82
- if (this.wpExtent) {
83
- this.setPxExtent(bitmap.width, bitmap.height)
84
- }
85
- bitmap.close()
86
- return this.wpExtent!
87
- } catch (error) {
88
- console.error(error)
89
- }
90
- }
91
- if (!this.wpExtent) {
92
- this.setPxExtent(0, 0)
93
- }
73
+ }
74
+
75
+ async getExtent(): Promise<extent> {
76
+ if (!this.wpExtent) {
77
+ if (this.file.size) {
78
+ try {
79
+ const bitmap = await createImageBitmap(this.file)
80
+ if (this.wpExtent) {
81
+ this.setPxExtent(bitmap.width, bitmap.height)
82
+ }
83
+ bitmap.close()
84
+ return this.wpExtent!
85
+ } catch (error) {
86
+ console.error(error)
94
87
  }
95
- return this.wpExtent!
88
+ }
89
+ if (!this.wpExtent) {
90
+ this.setPxExtent(0, 0)
91
+ }
96
92
  }
97
-
98
- //设置图片范围(像素)
99
- setPxExtent(width: number, height: number): void {
100
- this.wpExtent = {
101
- cy: pxToEMU(height),
102
- cx: pxToEMU(width),
103
- }
93
+ return this.wpExtent!
94
+ }
95
+
96
+ //设置图片范围(像素)
97
+ setPxExtent(width: number, height: number): void {
98
+ this.wpExtent = {
99
+ cy: pxToEMU(height),
100
+ cx: pxToEMU(width),
104
101
  }
102
+ }
105
103
 
106
- //设置图片范围(厘米)
107
- setCmExtent(width: number, height: number): void {
108
- this.wpExtent = {
109
- cy: cmToEMU(height),
110
- cx: cmToEMU(width),
111
- }
104
+ //设置图片范围(厘米)
105
+ setCmExtent(width: number, height: number): void {
106
+ this.wpExtent = {
107
+ cy: cmToEMU(height),
108
+ cx: cmToEMU(width),
112
109
  }
110
+ }
113
111
 
114
- async outJson(): Promise<Record<string, any>> {
115
- await this.awaitInit()
116
- const data: Record<string, any> = {}
117
- for (const key in this) {
118
- data[key] = this[key]
119
- }
120
- return data
112
+ async outJson(): Promise<Record<string, unknown>> {
113
+ await this.awaitInit()
114
+ const data: Record<string, unknown> = {}
115
+ for (const key in this) {
116
+ data[key] = this[key]
121
117
  }
118
+ return data
119
+ }
122
120
 
123
- setPropertys(data: Record<string, any>) {
124
- for (const key in data) {
125
- this[key as keyof image] = data[key]
126
- }
121
+ setProperties(data: Record<string, unknown>) {
122
+ for (const key in data) {
123
+ this[key as keyof image] = data[key] as never;
127
124
  }
128
- }
125
+ }
126
+ }
@@ -1,37 +1,42 @@
1
- import paramsData from "./paramsData"
2
- import Temp from "../temp"
1
+ import paramsData from './paramsData'
2
+ import Temp from '../temp'
3
3
 
4
4
  export type media = {
5
- id: string,
6
- data: Uint8Array
5
+ id: string
6
+ data: Uint8Array
7
7
  }
8
8
 
9
9
  export default interface ReplaceInterface {
10
+ //添加模板文件
11
+ addTempFile(tempFile: Temp): void
10
12
 
11
- //添加模板文件
12
- addTempFile(tempFile: Temp): void
13
+ //清空文件和链接
14
+ clear(): void
13
15
 
14
- //清空文件和链接
15
- clear(): void
16
+ //提取变量
17
+ extractVariables(files: Temp[] | undefined): Promise<Record<string, string[]>>
16
18
 
17
- //提取变量
18
- extractVariables(files: Temp[] | undefined): Promise<Record<string, string[]>>
19
+ //提取媒体
20
+ extractMedias(files: Temp[] | undefined): Promise<Record<string, media[]>>
19
21
 
20
- //提取媒体
21
- extractMedias(files: Temp[] | undefined): Promise<Record<string, media[]>>
22
+ //签名方法
23
+ sign(data: unknown): Promise<string>
22
24
 
23
- //签名方法
24
- sign(data: any): Promise<string>
25
+ //执行替换任务
26
+ execute(
27
+ params: paramsData,
28
+ files: Temp[] | undefined,
29
+ ): Promise<Record<string, Uint8Array>>
25
30
 
26
- //执行替换任务
27
- execute(params: paramsData, files: Temp[] | undefined): Promise<Record<string, Uint8Array>>
31
+ //执行替换任务(多套参数)
32
+ executeMultipleParams(
33
+ params: paramsData[],
34
+ files: Temp[] | undefined,
35
+ ): Promise<Record<string, Uint8Array>[]>
28
36
 
29
- //执行替换任务(多套参数)
30
- executeMultipleParams(params: paramsData[], files: Temp[] | undefined): Promise<Record<string, Uint8Array>[]>
37
+ //文件加密
38
+ fileEncrypt(file: Uint8Array): Promise<Uint8Array>
31
39
 
32
- //文件加密
33
- fileEncrypt(file: Uint8Array): Promise<Uint8Array>
34
-
35
- //文件批量加密
36
- filesEncrypt(files: (Uint8Array)[]): Promise<(Uint8Array)[]>
37
- }
40
+ //文件批量加密
41
+ filesEncrypt(files: Uint8Array[]): Promise<Uint8Array[]>
42
+ }
@@ -1,116 +1,128 @@
1
1
  import image, { extent, textWrapTypes } from './image'
2
2
 
3
- export type textData = Record<string, string|image>
3
+ export type textData = Record<string, string | image>
4
4
  export type mediaData = Record<string, image>
5
5
 
6
6
  type imageValue = {
7
- id: String,
8
- index: Number,
9
- suffix: String,
10
- wp_extent: extent,
11
- text_wrap: textWrapTypes,
7
+ id: string
8
+ index: number
9
+ suffix: string
10
+ wp_extent: extent
11
+ text_wrap: textWrapTypes
12
12
  }
13
13
 
14
14
  type value = {
15
- Image?: imageValue,
16
- Text?: string
15
+ Image?: imageValue
16
+ Text?: string
17
17
  }
18
18
 
19
19
  export type replaceParams = {
20
- text: Record<string, value>
21
- media: Record<string, value>
20
+ text: Record<string, value>
21
+ media: Record<string, value>
22
22
  }
23
23
 
24
24
  export default class paramsData {
25
- textData: textData = {}
26
- mediaData: mediaData = {}
27
- add_media?: (file: Uint8Array) => Promise<string>
25
+ textData: textData = {}
26
+ mediaData: mediaData = {}
27
+ add_media?: (file: Uint8Array) => string
28
28
 
29
- constructor(text?: textData, media?: mediaData) {
30
- if (text && text.constructor === Object) {
31
- this.textData = text
32
- }
33
- if (media && media.constructor === Object) {
34
- this.mediaData = media
35
- }
29
+ constructor(text?: textData, media?: mediaData) {
30
+ if (text && text.constructor === Object) {
31
+ this.textData = text
36
32
  }
33
+ if (media && media.constructor === Object) {
34
+ this.mediaData = media
35
+ }
36
+ }
37
37
 
38
- //转为替换参数
39
- async toReplaceParams(mediaBuffers: Uint8Array[] = []): Promise<[replaceParams, Uint8Array[]]> {
40
- const text: Record<string, value> = {}
41
- const media: Record<string, value> = {}
42
- const tasks = []
43
- for (const key in this.textData) {
44
- tasks.push(new Promise<void>(async resolve => {
45
- const value = this.textData[key]
46
- if (value instanceof image) {
47
- let id = value.id ?? ''
48
- let index = 0
49
- if (!id) {
50
- const buffer = await value.file.arrayBuffer()
51
- const uint8Array = new Uint8Array(buffer)
52
- if(this.add_media) {
53
- id = await this.add_media(uint8Array)
54
- }else{
55
- index = mediaBuffers.push(uint8Array) - 1
56
- }
57
- }
58
- text[key] = {
59
- Image: {
60
- index,
61
- id,
62
- suffix: '',
63
- wp_extent: value.wpExtent ?? {cx: 0, cy: 0},
64
- text_wrap: value.textWrap
65
- }
66
- }
67
- } else {
68
- text[key] = {
69
- Text: String(value)
70
- }
71
- }
72
- resolve()
73
- }))
74
- }
75
-
76
- for (const key in this.mediaData) {
77
- tasks.push(new Promise<void>(async resolve => {
78
- const value = this.mediaData[key]
79
- if (value instanceof image) {
80
- let id = value.id ?? ''
81
- let index = 0
82
- if (!id) {
83
- const buffer = await value.file.arrayBuffer()
84
- const uint8Array = new Uint8Array(buffer)
38
+ //转为替换参数
39
+ async toReplaceParams(
40
+ mediaBuffers: Uint8Array[] = [],
41
+ ): Promise<[replaceParams, Uint8Array[]]> {
42
+ const text: Record<string, value> = {}
43
+ const media: Record<string, value> = {}
44
+ const tasks = []
45
+ for (const key in this.textData) {
46
+ tasks.push(
47
+ new Promise<void>(async (resolve) => {
48
+ const value = this.textData[key]
49
+ if (value instanceof image) {
50
+ let id = value.id ?? ''
51
+ let index = 0
52
+ if (!id) {
53
+ const buffer = await value.file.arrayBuffer()
54
+ const uint8Array = new Uint8Array(buffer)
55
+ if (this.add_media) {
56
+ id = this.add_media(uint8Array)
57
+ } else {
58
+ index = mediaBuffers.push(uint8Array) - 1
59
+ }
60
+ }
61
+ text[key] = {
62
+ Image: {
63
+ index,
64
+ id,
65
+ suffix: '',
66
+ wp_extent: value.wpExtent ?? { cx: 0, cy: 0 },
67
+ text_wrap: value.textWrap,
68
+ },
69
+ }
70
+ } else {
71
+ text[key] = {
72
+ Text: String(value),
73
+ }
74
+ }
75
+ resolve()
76
+ }),
77
+ )
78
+ }
85
79
 
86
- if(this.add_media) {
87
- id = await this.add_media(uint8Array)
88
- }else{
89
- index = mediaBuffers.push(uint8Array) - 1
90
- }
91
- }
92
- media[key] = {
93
- Image: {
94
- index,
95
- id,
96
- suffix: '',
97
- wp_extent: value.wpExtent ?? {cx: 0, cy: 0},
98
- text_wrap: value.textWrap
99
- }
100
- }
101
- }
102
- resolve()
103
- }))
104
- }
80
+ for (const key in this.mediaData) {
81
+ tasks.push(
82
+ new Promise<void>(async (resolve) => {
83
+ const value = this.mediaData[key]
84
+ if (value instanceof image) {
85
+ let id = value.id ?? ''
86
+ let index = 0
87
+ if (!id) {
88
+ const buffer = await value.file.arrayBuffer()
89
+ const uint8Array = new Uint8Array(buffer)
105
90
 
106
- await Promise.all(tasks)
107
- return [{
108
- text,
109
- media
110
- }, mediaBuffers]
91
+ if (this.add_media) {
92
+ id = this.add_media(uint8Array)
93
+ } else {
94
+ index = mediaBuffers.push(uint8Array) - 1
95
+ }
96
+ }
97
+ media[key] = {
98
+ Image: {
99
+ index,
100
+ id,
101
+ suffix: '',
102
+ wp_extent: value.wpExtent ?? { cx: 0, cy: 0 },
103
+ text_wrap: value.textWrap,
104
+ },
105
+ }
106
+ }
107
+ resolve()
108
+ }),
109
+ )
111
110
  }
112
111
 
113
- isEmpty() {
114
- return Object.keys(this.textData).length === 0 && Object.keys(this.mediaData).length === 0
115
- }
116
- }
112
+ await Promise.all(tasks)
113
+ return [
114
+ {
115
+ text,
116
+ media,
117
+ },
118
+ mediaBuffers,
119
+ ]
120
+ }
121
+
122
+ isEmpty() {
123
+ return (
124
+ Object.keys(this.textData).length === 0 &&
125
+ Object.keys(this.mediaData).length === 0
126
+ )
127
+ }
128
+ }
package/replace/sign.ts CHANGED
@@ -1,61 +1,88 @@
1
- import Base from './base';
2
- import core, { add_template, add_media, replace_batch_verify, replace_params_encode, replace_batch_verify_multiple_params, replace_params_encode_multiple_params } from '../core/sign'
3
- import paramsData, { replaceParams } from './paramsData';
1
+ import Base from './base'
2
+ import core, {
3
+ add_media,
4
+ add_template,
5
+ replace_batch,
6
+ replace_params_encode,
7
+ replace_batch_multiple_params,
8
+ replace_params_encode_multiple_params,
9
+ } from '../core/sign'
10
+ import paramsData, { replaceParams } from './paramsData'
4
11
 
5
12
  export default class Sign extends Base {
6
- constructor() {
7
- super(core())
8
- }
9
-
10
- async handle(paramsData: paramsData, files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
11
- paramsData.add_media = add_media
12
-
13
- const addFileTasks: Promise<number>[] = []
14
- for (const file of files) {
15
- addFileTasks.push(add_template(file, isDecode))
16
- }
17
- const [tempFiles, [variables]] = await Promise.all([
18
- Promise.all(addFileTasks),
19
- paramsData.toReplaceParams(),
20
- ])
21
- const encodeData = {
22
- files: tempFiles,
23
- variables
24
- }
13
+ constructor() {
14
+ super(core())
15
+ }
25
16
 
26
- const paramsEncode = await replace_params_encode(encodeData)
27
- const verifyCode = await this.sign(paramsEncode)
28
- return replace_batch_verify(String(verifyCode), paramsEncode.data)
17
+ async handle(
18
+ paramsData: paramsData,
19
+ files: Uint8Array[],
20
+ encode_files: Uint8Array[],
21
+ ): Promise<Uint8Array[]> {
22
+ paramsData.add_media = add_media
23
+ const addFileTasks: Promise<number>[] = []
24
+ for (const file of files) {
25
+ addFileTasks.push(add_template(file, false))
29
26
  }
27
+ for (const file of encode_files) {
28
+ addFileTasks.push(add_template(file, true))
29
+ }
30
+ const [tempFiles, [variables]] = await Promise.all([
31
+ Promise.all(addFileTasks),
32
+ paramsData.toReplaceParams(),
33
+ ])
34
+ const encodeData = {
35
+ files: tempFiles,
36
+ variables,
37
+ }
38
+ const paramsEncode = await replace_params_encode(encodeData)
39
+ const verifyCode = await this.sign(paramsEncode)
40
+ return replace_batch(verifyCode, paramsEncode.data)
41
+ }
30
42
 
31
- async handleMultipleParams(paramsList: paramsData[], files: Uint8Array[], isDecode: boolean = false): Promise<Uint8Array[]> {
32
- let variablesTasks: Promise<replaceParams>[] = []
33
- for (const paramsData of paramsList) {
34
- paramsData.add_media = add_media
35
- variablesTasks.push(new Promise((resolve, reject) => {
36
- paramsData.toReplaceParams().then(([params]) => {
37
- resolve(params)
38
- }).catch(reject)
39
- }))
40
- }
41
-
42
- const addFileTasks: Promise<number>[] = []
43
- for (const file of files) {
44
- addFileTasks.push(add_template(file, isDecode))
45
- }
43
+ async handleMultipleParams(
44
+ paramsList: paramsData[],
45
+ files: Uint8Array[],
46
+ encode_files: Uint8Array[],
47
+ ): Promise<Uint8Array[]> {
48
+ let variablesTasks: Promise<replaceParams>[] = []
49
+ for (const paramsData of paramsList) {
50
+ paramsData.add_media = add_media
51
+ variablesTasks.push(
52
+ new Promise((resolve, reject) => {
53
+ paramsData
54
+ .toReplaceParams()
55
+ .then(([params]) => {
56
+ resolve(params)
57
+ })
58
+ .catch(reject)
59
+ }),
60
+ )
61
+ }
46
62
 
47
- const [tempFiles, variables] = await Promise.all([
48
- Promise.all(addFileTasks),
49
- Promise.all(variablesTasks),
50
- ])
63
+ const addFileTasks: Promise<number>[] = []
64
+ for (const file of files) {
65
+ addFileTasks.push(add_template(file, false))
66
+ }
67
+ for (const file of encode_files) {
68
+ addFileTasks.push(add_template(file, true))
69
+ }
51
70
 
52
- const encodeData = {
53
- files: tempFiles,
54
- variables
55
- }
71
+ const [tempFiles, variables] = await Promise.all([
72
+ Promise.all(addFileTasks),
73
+ Promise.all(variablesTasks),
74
+ ])
56
75
 
57
- const paramsEncode = await replace_params_encode_multiple_params(encodeData)
58
- const verifyCode = await this.sign(paramsEncode)
59
- return replace_batch_verify_multiple_params(String(verifyCode), paramsEncode.data)
76
+ const encodeData = {
77
+ files: tempFiles,
78
+ variables,
60
79
  }
61
- }
80
+
81
+ const paramsEncode = await replace_params_encode_multiple_params(encodeData)
82
+ const verifyCode = await this.sign(paramsEncode)
83
+ return replace_batch_multiple_params(
84
+ verifyCode,
85
+ paramsEncode.data,
86
+ )
87
+ }
88
+ }