Merge pull request '[v8.0/forgejo] Improve textarea paste (part of gitea#31948) #5269' (#5301) from fnetx/bp-textarea-paste into v8.0/forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5301
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
Otto 2024-09-12 13:00:38 +00:00
commit be8097df23
3 changed files with 66 additions and 32 deletions

View file

@ -1,31 +0,0 @@
// @ts-check
import {test, expect} from '@playwright/test';
import {login_user, load_logged_in_context} from './utils_e2e.js';
test.beforeAll(async ({browser}, workerInfo) => {
await login_user(browser, workerInfo, 'user2');
});
test('Always focus edit tab first on edit', async ({browser}, workerInfo) => {
const context = await load_logged_in_context(browser, workerInfo, 'user2');
const page = await context.newPage();
const response = await page.goto('/user2/repo1/issues/1');
await expect(response?.status()).toBe(200);
// Switch to preview tab and save
await page.click('#issue-1 .comment-container .context-menu');
await page.click('#issue-1 .comment-container .menu>.edit-content');
await page.locator('#issue-1 .comment-container a[data-tab-for=markdown-previewer]').click();
await page.click('#issue-1 .comment-container .save');
await page.waitForLoadState('networkidle');
// Edit again and assert that edit tab should be active (and not preview tab)
await page.click('#issue-1 .comment-container .context-menu');
await page.click('#issue-1 .comment-container .menu>.edit-content');
const editTab = page.locator('#issue-1 .comment-container a[data-tab-for=markdown-writer]');
const previewTab = page.locator('#issue-1 .comment-container a[data-tab-for=markdown-previewer]');
await expect(editTab).toHaveClass(/active/);
await expect(previewTab).not.toHaveClass(/active/);
});

View file

@ -0,0 +1,65 @@
// @ts-check
import {expect, test} from '@playwright/test';
import {login_user, load_logged_in_context} from './utils_e2e.js';
test.beforeAll(async ({browser}, workerInfo) => {
await login_user(browser, workerInfo, 'user2');
});
test('Hyperlink paste behaviour', async ({browser}, workerInfo) => {
test.skip(['Mobile Safari', 'Mobile Chrome', 'webkit'].includes(workerInfo.project.name), 'Mobile clients seem to have very weird behaviour with this test, which I cannot confirm with real usage');
const context = await load_logged_in_context(browser, workerInfo, 'user2');
const page = await context.newPage();
await page.goto('/user2/repo1/issues/new');
await page.locator('textarea').click();
// same URL
await page.locator('textarea').fill('https://codeberg.org/forgejo/forgejo#some-anchor');
await page.locator('textarea').press('Shift+Home');
await page.locator('textarea').press('ControlOrMeta+c');
await page.locator('textarea').press('ControlOrMeta+v');
await expect(page.locator('textarea')).toHaveValue('https://codeberg.org/forgejo/forgejo#some-anchor');
// other text
await page.locator('textarea').fill('Some other text');
await page.locator('textarea').press('ControlOrMeta+a');
await page.locator('textarea').press('ControlOrMeta+v');
await expect(page.locator('textarea')).toHaveValue('[Some other text](https://codeberg.org/forgejo/forgejo#some-anchor)');
// subset of URL
await page.locator('textarea').fill('https://codeberg.org/forgejo/forgejo#some');
await page.locator('textarea').press('ControlOrMeta+a');
await page.locator('textarea').press('ControlOrMeta+v');
await expect(page.locator('textarea')).toHaveValue('https://codeberg.org/forgejo/forgejo#some-anchor');
// superset of URL
await page.locator('textarea').fill('https://codeberg.org/forgejo/forgejo#some-anchor-on-the-page');
await page.locator('textarea').press('ControlOrMeta+a');
await page.locator('textarea').press('ControlOrMeta+v');
await expect(page.locator('textarea')).toHaveValue('https://codeberg.org/forgejo/forgejo#some-anchor');
// completely separate URL
await page.locator('textarea').fill('http://example.com');
await page.locator('textarea').press('ControlOrMeta+a');
await page.locator('textarea').press('ControlOrMeta+v');
await expect(page.locator('textarea')).toHaveValue('https://codeberg.org/forgejo/forgejo#some-anchor');
});
test('Always focus edit tab first on edit', async ({browser}, workerInfo) => {
const context = await load_logged_in_context(browser, workerInfo, 'user2');
const page = await context.newPage();
const response = await page.goto('/user2/repo1/issues/1');
await expect(response?.status()).toBe(200);
// Switch to preview tab and save
await page.click('#issue-1 .comment-container .context-menu');
await page.click('#issue-1 .comment-container .menu>.edit-content');
await page.locator('#issue-1 .comment-container a[data-tab-for=markdown-previewer]').click();
await page.click('#issue-1 .comment-container .save');
await page.waitForLoadState('networkidle');
// Edit again and assert that edit tab should be active (and not preview tab)
await page.click('#issue-1 .comment-container .context-menu');
await page.click('#issue-1 .comment-container .menu>.edit-content');
const editTab = page.locator('#issue-1 .comment-container a[data-tab-for=markdown-writer]');
const previewTab = page.locator('#issue-1 .comment-container a[data-tab-for=markdown-previewer]');
await expect(editTab).toHaveClass(/active/);
await expect(previewTab).not.toHaveClass(/active/);
});

View file

@ -116,7 +116,7 @@ function handleClipboardText(textarea, text, e) {
if (_shiftDown) return; if (_shiftDown) return;
const selectedText = value.substring(selectionStart, selectionEnd); const selectedText = value.substring(selectionStart, selectionEnd);
const trimmedText = text.trim(); const trimmedText = text.trim();
if (selectedText && isUrl(trimmedText)) { if (selectedText && isUrl(trimmedText) && !isUrl(selectedText)) {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
replaceTextareaSelection(textarea, `[${selectedText}](${trimmedText})`); replaceTextareaSelection(textarea, `[${selectedText}](${trimmedText})`);