@hprint/core 0.0.1-alpha.3 → 0.0.1

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.
@@ -1,165 +1,165 @@
1
- import { v4 as uuid } from 'uuid';
2
- import { useClipboard, useFileDialog, useBase64 } from '@vueuse/core';
3
-
4
- /**
5
- * @description: 图片文件转字符串
6
- * @param {Blob|File} file 文件
7
- * @return {String}
8
- */
9
- export function getImgStr(file: File | Blob): Promise<FileReader['result']> {
10
- return useBase64(file).promise.value;
11
- }
12
-
13
- /**
14
- * @description: 选择文件
15
- * @param {Object} options accept = '', capture = '', multiple = false
16
- * @return {Promise}
17
- */
18
- export function selectFiles(options: {
19
- accept?: string;
20
- capture?: string;
21
- multiple?: boolean;
22
- }): Promise<FileList | null> {
23
- return new Promise((resolve) => {
24
- const { onChange, open } = useFileDialog(options);
25
- onChange((files) => {
26
- resolve(files);
27
- });
28
- open();
29
- });
30
- }
31
-
32
- /**
33
- * @description: 创建图片元素
34
- * @param {String} str 图片地址或者base64图片
35
- * @return {Promise} element 图片元素
36
- */
37
- export function insertImgFile(str: string) {
38
- return new Promise((resolve) => {
39
- const imgEl = document.createElement('img');
40
- imgEl.src = str;
41
- // 插入页面
42
- document.body.appendChild(imgEl);
43
- imgEl.onload = () => {
44
- resolve(imgEl);
45
- };
46
- });
47
- }
48
-
49
- /**
50
- * Copying text to the clipboard
51
- * @param source Copy source
52
- * @param options Copy options
53
- * @returns Promise that resolves when the text is copied successfully, or rejects when the copy fails.
54
- */
55
- export const clipboardText = (
56
- source: string,
57
- options?: Parameters<typeof useClipboard>[0]
58
- ) => {
59
- return useClipboard({ source, ...options }).copy();
60
- };
61
-
62
- export function downFile(fileStr: string, fileType: string) {
63
- const anchorEl = document.createElement('a');
64
- anchorEl.href = fileStr;
65
- anchorEl.download = `${uuid()}.${fileType}`;
66
- document.body.appendChild(anchorEl); // required for firefox
67
- anchorEl.click();
68
- anchorEl.remove();
69
- }
70
-
71
- export function drawImg(
72
- ctx: CanvasRenderingContext2D,
73
- left: number,
74
- top: number,
75
- img: HTMLImageElement,
76
- wSize: number,
77
- hSize: number,
78
- angle: number | undefined
79
- ) {
80
- if (angle === undefined) return;
81
- ctx.save();
82
- ctx.translate(left, top);
83
- ctx.rotate(angle);
84
- ctx.drawImage(img, -wSize / 2, -hSize / 2, wSize, hSize);
85
- ctx.restore();
86
- }
87
-
88
- export function shiftAngle(start: fabric.Point, end: fabric.Point) {
89
- const startX = start.x;
90
- const startY = start.y;
91
- const x2 = end.x - startX;
92
- const y2 = end.y - startY;
93
- const r = Math.sqrt(x2 * x2 + y2 * y2);
94
- let angle = (Math.atan2(y2, x2) / Math.PI) * 180;
95
- angle = ~~(((angle + 7.5) % 360) / 15) * 15;
96
-
97
- const cosx = r * Math.cos((angle * Math.PI) / 180);
98
- const sinx = r * Math.sin((angle * Math.PI) / 180);
99
-
100
- return {
101
- x: cosx + startX,
102
- y: sinx + startY,
103
- };
104
- }
105
-
106
- /**
107
- * 类型工具
108
- */
109
- export const isImage = (thing: unknown): thing is fabric.Image => {
110
- return thing instanceof fabric.Image;
111
- };
112
-
113
- export const isGroup = (thing: unknown): thing is fabric.Group => {
114
- return thing instanceof fabric.Group;
115
- };
116
-
117
- export const isIText = (thing: unknown): thing is fabric.IText => {
118
- return thing instanceof fabric.IText;
119
- };
120
-
121
- export const isActiveSelection = (
122
- thing: unknown
123
- ): thing is fabric.ActiveSelection => {
124
- return thing instanceof fabric.ActiveSelection;
125
- };
126
-
127
- export function blobToBase64(blob: Blob) {
128
- return new Promise((resolve) => {
129
- const reader = new FileReader();
130
- reader.addEventListener('load', () => {
131
- resolve(reader.result as string);
132
- });
133
- reader.readAsDataURL(blob);
134
- });
135
- }
136
-
137
- export function base64ToBlob(base64Data: string) {
138
- if (!base64Data) {
139
- return null;
140
- }
141
- const dataArr = base64Data.split(',');
142
- const imageType = dataArr[0].match(/:(.*?);/)[1];
143
- const textData = window.atob(dataArr[1]);
144
- const arrayBuffer = new ArrayBuffer(textData.length);
145
- const uint8Array = new Uint8Array(arrayBuffer);
146
- for (let i = 0; i < textData.length; i++) {
147
- uint8Array[i] = textData.charCodeAt(i);
148
- }
149
- return [new Blob([arrayBuffer], { type: imageType }), imageType.slice(6)];
150
- }
151
-
152
- export default {
153
- getImgStr,
154
- downFile,
155
- selectFiles,
156
- insertImgFile,
157
- clipboardText,
158
- drawImg,
159
- isImage,
160
- isGroup,
161
- isIText,
162
- isActiveSelection,
163
- blobToBase64,
164
- base64ToBlob,
165
- };
1
+ import { v4 as uuid } from 'uuid';
2
+ import { useClipboard, useFileDialog, useBase64 } from '@vueuse/core';
3
+
4
+ /**
5
+ * @description: 图片文件转字符串
6
+ * @param {Blob|File} file 文件
7
+ * @return {String}
8
+ */
9
+ export function getImgStr(file: File | Blob): Promise<FileReader['result']> {
10
+ return useBase64(file).promise.value;
11
+ }
12
+
13
+ /**
14
+ * @description: 选择文件
15
+ * @param {Object} options accept = '', capture = '', multiple = false
16
+ * @return {Promise}
17
+ */
18
+ export function selectFiles(options: {
19
+ accept?: string;
20
+ capture?: string;
21
+ multiple?: boolean;
22
+ }): Promise<FileList | null> {
23
+ return new Promise((resolve) => {
24
+ const { onChange, open } = useFileDialog(options);
25
+ onChange((files) => {
26
+ resolve(files);
27
+ });
28
+ open();
29
+ });
30
+ }
31
+
32
+ /**
33
+ * @description: 创建图片元素
34
+ * @param {String} str 图片地址或者base64图片
35
+ * @return {Promise} element 图片元素
36
+ */
37
+ export function insertImgFile(str: string) {
38
+ return new Promise((resolve) => {
39
+ const imgEl = document.createElement('img');
40
+ imgEl.src = str;
41
+ // 插入页面
42
+ document.body.appendChild(imgEl);
43
+ imgEl.onload = () => {
44
+ resolve(imgEl);
45
+ };
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Copying text to the clipboard
51
+ * @param source Copy source
52
+ * @param options Copy options
53
+ * @returns Promise that resolves when the text is copied successfully, or rejects when the copy fails.
54
+ */
55
+ export const clipboardText = (
56
+ source: string,
57
+ options?: Parameters<typeof useClipboard>[0]
58
+ ) => {
59
+ return useClipboard({ source, ...options }).copy();
60
+ };
61
+
62
+ export function downFile(fileStr: string, fileType: string) {
63
+ const anchorEl = document.createElement('a');
64
+ anchorEl.href = fileStr;
65
+ anchorEl.download = `${uuid()}.${fileType}`;
66
+ document.body.appendChild(anchorEl); // required for firefox
67
+ anchorEl.click();
68
+ anchorEl.remove();
69
+ }
70
+
71
+ export function drawImg(
72
+ ctx: CanvasRenderingContext2D,
73
+ left: number,
74
+ top: number,
75
+ img: HTMLImageElement,
76
+ wSize: number,
77
+ hSize: number,
78
+ angle: number | undefined
79
+ ) {
80
+ if (angle === undefined) return;
81
+ ctx.save();
82
+ ctx.translate(left, top);
83
+ ctx.rotate(angle);
84
+ ctx.drawImage(img, -wSize / 2, -hSize / 2, wSize, hSize);
85
+ ctx.restore();
86
+ }
87
+
88
+ export function shiftAngle(start: fabric.Point, end: fabric.Point) {
89
+ const startX = start.x;
90
+ const startY = start.y;
91
+ const x2 = end.x - startX;
92
+ const y2 = end.y - startY;
93
+ const r = Math.sqrt(x2 * x2 + y2 * y2);
94
+ let angle = (Math.atan2(y2, x2) / Math.PI) * 180;
95
+ angle = ~~(((angle + 7.5) % 360) / 15) * 15;
96
+
97
+ const cosx = r * Math.cos((angle * Math.PI) / 180);
98
+ const sinx = r * Math.sin((angle * Math.PI) / 180);
99
+
100
+ return {
101
+ x: cosx + startX,
102
+ y: sinx + startY,
103
+ };
104
+ }
105
+
106
+ /**
107
+ * 类型工具
108
+ */
109
+ export const isImage = (thing: unknown): thing is fabric.Image => {
110
+ return thing instanceof fabric.Image;
111
+ };
112
+
113
+ export const isGroup = (thing: unknown): thing is fabric.Group => {
114
+ return thing instanceof fabric.Group;
115
+ };
116
+
117
+ export const isIText = (thing: unknown): thing is fabric.IText => {
118
+ return thing instanceof fabric.IText;
119
+ };
120
+
121
+ export const isActiveSelection = (
122
+ thing: unknown
123
+ ): thing is fabric.ActiveSelection => {
124
+ return thing instanceof fabric.ActiveSelection;
125
+ };
126
+
127
+ export function blobToBase64(blob: Blob) {
128
+ return new Promise((resolve) => {
129
+ const reader = new FileReader();
130
+ reader.addEventListener('load', () => {
131
+ resolve(reader.result as string);
132
+ });
133
+ reader.readAsDataURL(blob);
134
+ });
135
+ }
136
+
137
+ export function base64ToBlob(base64Data: string) {
138
+ if (!base64Data) {
139
+ return null;
140
+ }
141
+ const dataArr = base64Data.split(',');
142
+ const imageType = dataArr[0].match(/:(.*?);/)[1];
143
+ const textData = window.atob(dataArr[1]);
144
+ const arrayBuffer = new ArrayBuffer(textData.length);
145
+ const uint8Array = new Uint8Array(arrayBuffer);
146
+ for (let i = 0; i < textData.length; i++) {
147
+ uint8Array[i] = textData.charCodeAt(i);
148
+ }
149
+ return [new Blob([arrayBuffer], { type: imageType }), imageType.slice(6)];
150
+ }
151
+
152
+ export default {
153
+ getImgStr,
154
+ downFile,
155
+ selectFiles,
156
+ insertImgFile,
157
+ clipboardText,
158
+ drawImg,
159
+ isImage,
160
+ isGroup,
161
+ isIText,
162
+ isActiveSelection,
163
+ blobToBase64,
164
+ base64ToBlob,
165
+ };
package/tsconfig.json CHANGED
@@ -1,10 +1,10 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src",
6
- "composite": true
7
- },
8
- "include": ["src/**/*"],
9
- "exclude": ["node_modules", "dist"]
10
- }
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src",
6
+ "composite": true
7
+ },
8
+ "include": ["src/**/*"],
9
+ "exclude": ["node_modules", "dist"]
10
+ }
package/vite.config.ts CHANGED
@@ -1,29 +1,29 @@
1
- import { defineConfig } from 'vite';
2
- import { resolve } from 'path';
3
- import { fileURLToPath } from 'url';
4
- import dts from 'vite-plugin-dts';
5
-
6
- const __dirname = fileURLToPath(new URL('.', import.meta.url));
7
-
8
- export default defineConfig({
9
- plugins: [
10
- dts({
11
- outDir: 'dist',
12
- include: ['src/**/*'],
13
- }),
14
- ],
15
- build: {
16
- lib: {
17
- entry: resolve(__dirname, 'src/index.ts'),
18
- name: '@hprint/core',
19
- fileName: 'index',
20
- formats: ['es', 'cjs'],
21
- },
22
- rollupOptions: {
23
- external: ['@hprint/shared'],
24
- output: {
25
- globals: {},
26
- },
27
- },
28
- },
29
- });
1
+ import { defineConfig } from 'vite';
2
+ import { resolve } from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import dts from 'vite-plugin-dts';
5
+
6
+ const __dirname = fileURLToPath(new URL('.', import.meta.url));
7
+
8
+ export default defineConfig({
9
+ plugins: [
10
+ dts({
11
+ outDir: 'dist',
12
+ include: ['src/**/*'],
13
+ }),
14
+ ],
15
+ build: {
16
+ lib: {
17
+ entry: resolve(__dirname, 'src/index.ts'),
18
+ name: '@hprint/core',
19
+ fileName: 'index',
20
+ formats: ['es', 'cjs'],
21
+ },
22
+ rollupOptions: {
23
+ external: ['@hprint/shared'],
24
+ output: {
25
+ globals: {},
26
+ },
27
+ },
28
+ },
29
+ });