Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(edit): 避免链接输入框与原文重叠

longmeidaocdc042acb0bb
diff --git a/frontend/e2e/copy-editor.test.ts b/frontend/e2e/copy-editor.test.ts
index e384c7b88..36a34982a 100644
--- a/frontend/e2e/copy-editor.test.ts
+++ b/frontend/e2e/copy-editor.test.ts
@@ -14,6 +14,35 @@ describe('原位改字', () => {
before(async () => { browser = await launch(); });
after(async () => { await browser?.close(); });
for (const viewport of VIEWPORTS) {
+ it(`链接和按钮输入框独占文字位置并恢复原文(${viewport.name})`, async () => {
+ const context = await browser.newContext({ viewport });
+ try {
+ const page = await context.newPage();
+ await page.setContent('<meta charset="utf-8"><style>[data-copy-highlight]{pointer-events:none}[data-copy-input]{padding:2px 6px;border:0;background:#141518;color:white}</style><header style="position:fixed;top:8px;left:20px;font:13px/20.8px sans-serif"><a id="link" href="#download">下载最新版 →</a></header><button id="button" style="margin:90px 20px">操作名称</button>');
+ await page.addScriptTag({ content: await readFile(new URL('../src/copy-editor-core.js', import.meta.url), 'utf8') });
+ await page.evaluate(() => (globalThis as any).PeachCopyEditorCore.create({save(_edit: unknown, text: string) {
+ if (text === '保存失败') throw Error('临时失败');
+ return {};
+ }}));
+ for (const id of ['link', 'button']) {
+ const target = page.locator('#' + id), before = await target.boundingBox(), original = await target.innerText();
+ await target.click();
+ const input = page.locator('[data-copy-input]'); await input.waitFor();
+ assert.equal(await target.locator('[data-copy-editor]').evaluate(node => getComputedStyle(node).visibility), 'hidden');
+ assert.deepEqual(await target.boundingBox(), before, '隐藏原文仍保留控件布局');
+ const rect = await input.boundingBox(); assert.ok(rect && rect.y >= 2);
+ await input.fill('取消修改'); await input.press('Escape');
+ assert.equal(await target.innerText(), original);
+ assert.equal(await target.locator('[data-copy-editor]').count(), 0);
+ await target.click(); await input.fill('保存失败'); await input.press('Enter');
+ assert.equal(await target.innerText(), original);
+ assert.equal(await target.locator('[data-copy-editor]').count(), 0);
+ await target.click(); await input.fill('已修改'); await input.press('Enter');
+ assert.equal(await target.innerText(), '已修改');
+ assert.equal(await target.locator('[data-copy-editor]').count(), 0);
+ }
+ } finally { await context.close(); }
+ });
it(`连续行内文字整段编辑、取消与保存失败保留样式(${viewport.name})`, async () => {
const context = await browser.newContext({ viewport });
try {
@@ -68,7 +97,8 @@ describe('原位改字', () => {
await input.waitFor();
await page.evaluate(() => scrollTo(0, 120));
const rect = await input.boundingBox();
- assert.ok(rect && rect.x >= 16 && rect.y >= 16 && rect.x + rect.width <= viewport.width - 15);
+ assert.ok(rect && rect.x >= 16 && rect.y >= 2 && rect.x + rect.width <= viewport.width - 15
+ && rect.y + rect.height <= viewport.height - 1);
await input.fill('切换文字'); await input.press('Enter');
assert.equal(await page.locator('#tab').innerText(), '切换文字');
await page.locator('#command').click({ position: { x: 5, y: 8 } });
diff --git a/frontend/src/copy-editor-core.js b/frontend/src/copy-editor-core.js
index bf77af0bb..0afa4ccd7 100644
--- a/frontend/src/copy-editor-core.js
+++ b/frontend/src/copy-editor-core.js
@@ -126,8 +126,9 @@
const viewport = window.visualViewport, width = viewport?.width ?? innerWidth, height = viewport?.height ?? innerHeight;
const x = viewport?.offsetLeft ?? 0, y = viewport?.offsetTop ?? 0;
const inputWidth = Math.min(Math.max(140, rect.width + 60), Math.max(1, width - 32));
+ const inputHeight = editing.box.getBoundingClientRect().height;
Object.assign(editing.box.style, { left: Math.max(x + 16, Math.min(rect.left - 6, x + width - inputWidth - 16)) + 'px',
- top: Math.max(y + 16, Math.min(rect.top - 2, y + height - editing.box.getBoundingClientRect().height - 16)) + 'px', width: inputWidth + 'px' });
+ top: Math.max(y + 2, Math.min(rect.top + (rect.height - inputHeight) / 2, y + height - inputHeight - 2)) + 'px', width: inputWidth + 'px' });
}
async function begin(node, target) {
const before = target.element ? target.element.textContent : node.data, originalText = before.trim();
@@ -147,7 +148,11 @@
box.value = ed.core;
const style = getComputedStyle(node.parentElement);
Object.assign(box.style, { position: 'fixed', boxSizing: 'border-box', font: style.font, letterSpacing: style.letterSpacing });
- (node.parentElement.closest('dialog[open]') || document.body).append(box);
+ const host = node.parentElement.closest('dialog[open]') || document.body;
+ ed.placeholder = document.createElement('span'); ed.placeholder.dataset.copyEditor = '';
+ ed.placeholder.style.visibility = 'hidden';
+ node.replaceWith(ed.placeholder); ed.placeholder.append(node);
+ host.append(box);
} else {
box.dataset.copyInline = '';
if (options.inlineClass) box.className = options.inlineClass;
@@ -177,7 +182,7 @@
finally { setBusy(false); }
}
function putBack(ed) {
- if (ed.box.tagName === 'INPUT') { ed.box.remove(); return; }
+ if (ed.box.tagName === 'INPUT') { ed.placeholder.replaceWith(ed.node); ed.box.remove(); return; }
if (ed.element) { ed.box.replaceWith(ed.fragment); return; }
const a = ed.box.previousSibling, c = ed.box.nextSibling;
if (ed.lead && a?.nodeType === 3) a.remove();