@@ -105,9 +105,9 @@ function handleRootRequest() {
105
105
$(document).ready(function() {
106
106
let originalImageURL = '';
107
107
108
- // 初始化文件上传
108
+ // 初始化文件上传
109
109
initFileInput();
110
-
110
+
111
111
// 文件上传初始化函数
112
112
function initFileInput() {
113
113
$("#fileInput").fileinput({
@@ -127,8 +127,8 @@ $(document).ready(function() {
127
127
}).on('filebatchselected', handleFileSelection)
128
128
.on('fileclear', handleFileClear);
129
129
}
130
-
131
- // 处理接口选择器变更事件
130
+
131
+ // 处理接口选择器变更事件
132
132
$('#interfaceSelector').change(function() {
133
133
const selectedInterface = $(this).val();
134
134
let acceptTypes = '';
@@ -140,12 +140,12 @@ $(document).ready(function() {
140
140
}
141
141
$('#fileInput').attr('accept', acceptTypes);
142
142
}).trigger('change');
143
-
144
- // 处理文件选择事件
143
+
144
+ // 处理文件选择事件
145
145
async function handleFileSelection() {
146
146
const selectedInterface = $('#interfaceSelector').val();
147
147
const file = $('#fileInput')[0].files[0];
148
-
148
+
149
149
if (file && file.size > 5 * 1024 * 1024) {
150
150
if (file.type === 'image/gif') {
151
151
toastr.error('GIF 文件必须≤5MB');
@@ -155,13 +155,13 @@ $(document).ready(function() {
155
155
}
156
156
return;
157
157
}
158
-
158
+
159
159
if (file && (file.type === 'image/gif' || file.type.includes('image/'))) {
160
160
await uploadFile(file);
161
161
}
162
162
}
163
-
164
- // 处理上传文件函数
163
+
164
+ // 处理上传文件函数
165
165
async function uploadFile(file) {
166
166
try {
167
167
$('#uploadingText').show();
@@ -179,7 +179,42 @@ $(document).ready(function() {
179
179
$('#uploadingText').hide();
180
180
}
181
181
}
182
-
182
+
183
+ //处理图片压缩事件
184
+ async function compressImage(file, quality = 0.6, maxResolution = 20000000) {
185
+ $('#compressingText').show();
186
+
187
+ return new Promise((resolve) => {
188
+ const image = new Image();
189
+ image.onload = () => {
190
+ const width = image.width;
191
+ const height = image.height;
192
+ const resolution = width * height;
193
+ let scale = 1;
194
+ if (resolution > maxResolution) {
195
+ scale = Math.sqrt(maxResolution / resolution);
196
+ }
197
+ const targetWidth = Math.round(width * scale);
198
+ const targetHeight = Math.round(height * scale);
199
+ const canvas = document.createElement('canvas');
200
+ const ctx = canvas.getContext('2d');
201
+ canvas.width = targetWidth;
202
+ canvas.height = targetHeight;
203
+ ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
204
+ canvas.toBlob((blob) => {
205
+ const compressedFile = new File([blob], file.name, { type: 'image/jpeg' });
206
+ $('#compressingText').hide();
207
+ resolve(compressedFile);
208
+ }, 'image/jpeg', quality);
209
+ };
210
+ const reader = new FileReader();
211
+ reader.onload = (event) => {
212
+ image.src = event.target.result;
213
+ };
214
+ reader.readAsDataURL(file);
215
+ });
216
+ }
217
+
183
218
// 处理上传响应函数
184
219
async function handleUploadResponse(response) {
185
220
if (response.ok) {
@@ -189,48 +224,45 @@ $(document).ready(function() {
189
224
return '文件上传失败!';
190
225
}
191
226
}
192
-
193
- // 处理按钮点击事件
227
+
228
+ // 处理按钮点击事件
194
229
$('#urlBtn, #bbcodeBtn, #markdownBtn').on('click', function() {
195
230
const fileLink = originalImageURL.trim();
196
231
if (fileLink !== '') {
197
232
let formattedLink;
198
233
switch ($(this).attr('id')) {
199
234
case 'urlBtn':
200
235
formattedLink = fileLink;
201
- break
202
- case 'urlBtn':
203
- formattedLink = fileLink;
204
- break;
205
- case 'bbcodeBtn':
206
- formattedLink = '[img]' + fileLink + '[/img]';
207
- break;
208
- case 'markdownBtn':
209
- formattedLink = '';
210
- break;
211
- default:
212
- formattedLink = fileLink;
236
+ break;
237
+ case 'bbcodeBtn':
238
+ formattedLink = '[img]' + fileLink + '[/img]';
239
+ break;
240
+ case 'markdownBtn':
241
+ formattedLink = '';
242
+ break;
243
+ default:
244
+ formattedLink = fileLink;
213
245
}
214
246
$('#fileLink').val(formattedLink);
215
247
adjustTextareaHeight($('#fileLink')[0]);
216
248
copyToClipboardWithToastr(formattedLink);
217
249
}
218
250
});
219
-
220
- // 处理移除按钮点击事件
251
+
252
+ // 处理移除按钮点击事件
221
253
function handleFileClear(event) {
222
254
$('#fileLink').val('');
223
255
adjustTextareaHeight($('#fileLink')[0]);
224
256
hideButtonsAndTextarea();
225
257
}
226
-
258
+
227
259
// 自动调整文本框高度函数
228
260
function adjustTextareaHeight(textarea) {
229
261
textarea.style.height = '1px';
230
262
textarea.style.height = (textarea.scrollHeight) + 'px';
231
263
}
232
-
233
- // 复制文本到剪贴板,并显示 toastr 提示框
264
+
265
+ // 复制文本到剪贴板,并显示 toastr 提示框
234
266
function copyToClipboardWithToastr(text) {
235
267
const input = document.createElement('input');
236
268
input.setAttribute('value', text);
@@ -240,53 +272,12 @@ $(document).ready(function() {
240
272
document.body.removeChild(input);
241
273
toastr.success('已复制到剪贴板', '', { timeOut: 300 });
242
274
}
243
-
275
+
244
276
// 隐藏按钮和文本框
245
277
function hideButtonsAndTextarea() {
246
278
$('#urlBtn, #bbcodeBtn, #markdownBtn, #fileLink').parent('.form-group').hide();
247
279
}
248
-
249
- // 处理图片压缩事件
250
- async function compressImage(file, quality = 0.6, maxResolution = 20000000) {
251
- $('#compressingText').show();
252
-
253
- return new Promise((resolve) => {
254
- const image = new Image();
255
- image.onload = () => {
256
- const width = image.width;
257
- const height = image.height;
258
-
259
- const resolution = width * height;
260
-
261
- let scale = 1;
262
- if (resolution > maxResolution) {
263
- scale = Math.sqrt(maxResolution / resolution);
264
- }
265
-
266
- const targetWidth = Math.round(width * scale);
267
- const targetHeight = Math.round(height * scale);
268
-
269
- const canvas = document.createElement('canvas');
270
- const ctx = canvas.getContext('2d');
271
-
272
- canvas.width = targetWidth;
273
- canvas.height = targetHeight;
274
- ctx.drawImage(image, 0, 0, targetWidth, targetHeight);
275
-
276
- canvas.toBlob((blob) => {
277
- const compressedFile = new File([blob], file.name, { type: 'image/jpeg' });
278
- $('#compressingText').hide();
279
- resolve(compressedFile);
280
- }, 'image/jpeg', quality);
281
- };
282
-
283
- const reader = new FileReader();
284
- reader.onload = (event) => {
285
- image.src = event.target.result;
286
- };
287
- reader.readAsDataURL(file);
288
- });
289
- }
280
+
290
281
});
291
282
292
283
</script>
0 commit comments