template-replacement 3.3.3 → 3.5.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 +1911 -2031
  15. package/dist/main/sign.js +2141 -2252
  16. package/dist/replace/general.js +287 -390
  17. package/dist/replace/sign.js +307 -401
  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 +100 -92
  33. package/replace/interface.ts +29 -24
  34. package/replace/paramsData.ts +107 -95
  35. package/replace/sign.ts +72 -54
  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 +192 -67
  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,111 @@ 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
 
33
+ //允许设置的属性
34
+ const allowSetPropertyKeys = new Set([
35
+ 'file',
36
+ 'id',
37
+ 'wpExtent',
38
+ 'textWrap',
39
+ 'relationship',
40
+ ])
41
+
35
42
  //图片替换
36
43
  export default class image {
37
- file: Blob
38
- relationship = 'image'
39
- id?: string
44
+ file: Blob
45
+ relationship = 'image'
46
+ id?: string
40
47
 
41
- wpExtent?: extent //图片宽高
42
- textWrap = textWrapTypes.embed //文字环绕
48
+ wpExtent?: extent //图片宽高
49
+ textWrap = textWrapTypes.embed //文字环绕
43
50
 
44
- awaitInitQueue?: Function[] = []
51
+ private _awaitInitQueue?: ((value?: unknown) => void)[] = []
45
52
 
46
- constructor(file: Blob) {
47
- if (file instanceof Blob) {
48
- this.file = file
49
- this.init()
50
- } else {
51
- throw new Error("不支持的数据类型");
52
- }
53
+ constructor(file: Blob) {
54
+ if (file instanceof Blob) {
55
+ this.file = file
56
+ this.init()
57
+ } else {
58
+ throw new Error('不支持的数据类型')
53
59
  }
60
+ }
54
61
 
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
62
+ async init(): Promise<void> {
63
+ this._awaitInitQueue = []
64
+ await this.getExtent()
65
+ for (const resolve of this._awaitInitQueue) {
66
+ resolve()
62
67
  }
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
- }
68
+ this._awaitInitQueue = undefined
69
+ }
70
+
71
+ async awaitInit(): Promise<void> {
72
+ if (this._awaitInitQueue) {
73
+ await new Promise((resolve) => {
74
+ this._awaitInitQueue?.push(resolve)
75
+ })
75
76
  }
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
- }
77
+ }
78
+
79
+ async getExtent(): Promise<extent> {
80
+ if (!this.wpExtent) {
81
+ if (this.file.size) {
82
+ try {
83
+ const bitmap = await createImageBitmap(this.file)
84
+ if (!this.wpExtent) {
85
+ this.setPxExtent(bitmap.width, bitmap.height)
86
+ }
87
+ bitmap.close()
88
+ return this.wpExtent!
89
+ } catch (error) {
90
+ console.error(error)
94
91
  }
95
- return this.wpExtent!
92
+ }
93
+ if (!this.wpExtent) {
94
+ this.setPxExtent(0, 0)
95
+ }
96
96
  }
97
-
98
- //设置图片范围(像素)
99
- setPxExtent(width: number, height: number): void {
100
- this.wpExtent = {
101
- cy: pxToEMU(height),
102
- cx: pxToEMU(width),
103
- }
97
+ return this.wpExtent!
98
+ }
99
+
100
+ //设置图片范围(像素)
101
+ setPxExtent(width: number, height: number): void {
102
+ this.wpExtent = {
103
+ cy: pxToEMU(height),
104
+ cx: pxToEMU(width),
104
105
  }
106
+ }
105
107
 
106
- //设置图片范围(厘米)
107
- setCmExtent(width: number, height: number): void {
108
- this.wpExtent = {
109
- cy: cmToEMU(height),
110
- cx: cmToEMU(width),
111
- }
108
+ //设置图片范围(厘米)
109
+ setCmExtent(width: number, height: number): void {
110
+ this.wpExtent = {
111
+ cy: cmToEMU(height),
112
+ cx: cmToEMU(width),
112
113
  }
113
-
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
114
+ }
115
+
116
+ async outJson(): Promise<Record<string, unknown>> {
117
+ await this.awaitInit()
118
+ const data: Record<string, unknown> = {}
119
+ for (const key in this) {
120
+ if (!allowSetPropertyKeys.has(key)) {
121
+ continue
122
+ }
123
+ data[key] = this[key]
121
124
  }
122
-
123
- setPropertys(data: Record<string, any>) {
124
- for (const key in data) {
125
- this[key as keyof image] = data[key]
126
- }
125
+ return data
126
+ }
127
+
128
+ setProperties(data: Record<string, unknown>) {
129
+ for (const key in data) {
130
+ if (!allowSetPropertyKeys.has(key)) {
131
+ continue
132
+ }
133
+ this[key as keyof image] = data[key] as never;
127
134
  }
128
- }
135
+ }
136
+ }
@@ -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,79 @@
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 tempFiles = []
24
+ for (const file of files) {
25
+ tempFiles.push(add_template(file, false))
29
26
  }
27
+ for (const file of encode_files) {
28
+ tempFiles.push(add_template(file, true))
29
+ }
30
+ const [variables] = await paramsData.toReplaceParams()
31
+ const encodeData = {
32
+ files: tempFiles,
33
+ variables,
34
+ }
35
+ const paramsEncode = await replace_params_encode(encodeData)
36
+ const verifyCode = await this.sign(paramsEncode)
37
+ return replace_batch(verifyCode, paramsEncode.data)
38
+ }
30
39
 
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
- }
46
-
47
- const [tempFiles, variables] = await Promise.all([
48
- Promise.all(addFileTasks),
49
- Promise.all(variablesTasks),
50
- ])
51
-
52
- const encodeData = {
53
- files: tempFiles,
54
- variables
55
- }
40
+ async handleMultipleParams(
41
+ paramsList: paramsData[],
42
+ files: Uint8Array[],
43
+ encode_files: Uint8Array[],
44
+ ): Promise<Uint8Array[]> {
45
+ let variablesTasks: Promise<replaceParams>[] = []
46
+ for (const paramsData of paramsList) {
47
+ paramsData.add_media = add_media
48
+ variablesTasks.push(
49
+ new Promise((resolve, reject) => {
50
+ paramsData
51
+ .toReplaceParams()
52
+ .then(([params]) => {
53
+ resolve(params)
54
+ })
55
+ .catch(reject)
56
+ }),
57
+ )
58
+ }
59
+ const tempFiles = []
60
+ for (const file of files) {
61
+ tempFiles.push(add_template(file, false))
62
+ }
63
+ for (const file of encode_files) {
64
+ tempFiles.push(add_template(file, true))
65
+ }
56
66
 
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)
67
+ const encodeData = {
68
+ files: tempFiles,
69
+ variables: await Promise.all(variablesTasks),
60
70
  }
61
- }
71
+
72
+ const paramsEncode = await replace_params_encode_multiple_params(encodeData)
73
+ const verifyCode = await this.sign(paramsEncode)
74
+ return replace_batch_multiple_params(
75
+ verifyCode,
76
+ paramsEncode.data,
77
+ )
78
+ }
79
+ }