1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-08 19:47:21 +00:00

Improve attachment upload methods (#30513)

* Use dropzone to handle file uploading for all cases, including pasting
and dragging
* Merge duplicate code, use consistent behavior for link generating

Close #20130

---------

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Tyrone Yeh
2024-06-27 17:31:49 +08:00
committed by GitHub
parent 00fc29aee1
commit 9bc5552c11
10 changed files with 169 additions and 97 deletions

View File

@ -262,18 +262,6 @@ export function isElemVisible(element) {
return Boolean(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
}
// extract text and images from "paste" event
export function getPastedContent(e) {
const images = [];
for (const item of e.clipboardData?.items ?? []) {
if (item.type?.startsWith('image/')) {
images.push(item.getAsFile());
}
}
const text = e.clipboardData?.getData?.('text') ?? '';
return {text, images};
}
// replace selected text in a textarea while preserving editor history, e.g. CTRL-Z works after this
export function replaceTextareaSelection(textarea, text) {
const before = textarea.value.slice(0, textarea.selectionStart ?? undefined);

View File

@ -19,11 +19,10 @@ export async function pngChunks(blob) {
return chunks;
}
// decode a image and try to obtain width and dppx. If will never throw but instead
// decode a image and try to obtain width and dppx. It will never throw but instead
// return default values.
export async function imageInfo(blob) {
let width = 0; // 0 means no width could be determined
let dppx = 1; // 1 dot per pixel for non-HiDPI screens
let width = 0, dppx = 1; // dppx: 1 dot per pixel for non-HiDPI screens
if (blob.type === 'image/png') { // only png is supported currently
try {
@ -41,6 +40,8 @@ export async function imageInfo(blob) {
}
}
} catch {}
} else {
return {}; // no image info for non-image files
}
return {width, dppx};

View File

@ -26,4 +26,5 @@ test('imageInfo', async () => {
expect(await imageInfo(await dataUriToBlob(pngNoPhys))).toEqual({width: 1, dppx: 1});
expect(await imageInfo(await dataUriToBlob(pngPhys))).toEqual({width: 2, dppx: 2});
expect(await imageInfo(await dataUriToBlob(pngEmpty))).toEqual({width: 0, dppx: 1});
expect(await imageInfo(await dataUriToBlob(`data:image/gif;base64,`))).toEqual({});
});