Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions dsh-reader/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ const READER_CSS = `
.rd-editor .rd-input{width:100%;margin-top:6px}
.rd-editor{max-height:min(64vh,520px)}
.rd-editor textarea{width:100%;min-height:64px;margin-top:8px;resize:vertical;border:1px solid var(--dsw-alias-border-l1);border-radius:9px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:8px 10px;font-size:12.5px;line-height:1.6;font-family:inherit}
.rd-annotation-editor textarea{min-height:96px;max-height:min(34vh,260px);overflow:auto;overscroll-behavior:contain}
.rd-card .rd-actions.rd-sticky-actions{position:sticky;bottom:-1px;z-index:2;background:var(--dsw-alias-bg-overlay);padding:8px 0 2px;margin-top:10px;border-top:1px solid var(--dsw-alias-border-l1)}
.rd-trans-card{position:fixed;z-index:95;background:var(--dsw-alias-bg-overlay);border:1px solid var(--dsw-alias-border-l1);border-radius:12px;box-shadow:0 12px 36px rgba(0,0,0,.22);padding:12px 13px;overflow:auto}
.rd-trans{margin-top:6px;padding-left:10px;border-left:2px solid var(--dsw-alias-border-l1);color:var(--dsw-alias-label-secondary);font-size:.92em;line-height:1.6}
Expand Down Expand Up @@ -868,7 +869,7 @@ const READER_CSS = `
.rd-pdf-hl{position:absolute;background:color-mix(in srgb,var(--hl,#f2c94c) 30%,transparent);pointer-events:none;border-radius:2px}
.rd-note-mask{position:fixed;inset:0;z-index:100;background:rgba(0,0,0,.28);display:flex;align-items:center;justify-content:center}
.rd-note-box{width:min(420px,90vw);background:var(--dsw-alias-bg-overlay);border:1px solid var(--dsw-alias-border-l1);border-radius:14px;padding:16px;box-shadow:0 16px 48px rgba(0,0,0,.28)}
.rd-note-box textarea{width:100%;min-height:110px;resize:vertical;border:1px solid var(--dsw-alias-border-l1);border-radius:9px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:9px 10px;font-size:13px;line-height:1.6;font-family:inherit}
.rd-note-box textarea{width:100%;min-height:110px;overscroll-behavior:contain;resize:vertical;border:1px solid var(--dsw-alias-border-l1);border-radius:9px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:9px 10px;font-size:13px;line-height:1.6;font-family:inherit}
.rd-note-box .rd-actions{display:flex;justify-content:flex-end;gap:8px;margin-top:12px}
.rd-input{height:28px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:0 9px;font-size:12px;font-family:inherit}
.rd-select{height:28px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-label-primary);font-size:12px;padding:0 6px}
Expand Down Expand Up @@ -1981,6 +1982,15 @@ function asElement(target) {
return target && typeof target.closest === 'function' ? target : null
}

// Overlays float above the reading surface and must not be dismissed by
// scrolling inside themselves: a note field or a long card scrolling its own
// content is not the page moving under the anchor.
const OWN_SCROLL_SURFACE = '.rd-mini, .rd-card, .rd-trans-card'
// Editable fields scroll as the caret moves (and while an IME composes), which
// happens constantly while typing; closing the panel on that would make the
// editor unusable.
const EDITABLE_TARGET = 'textarea, input, select, [contenteditable="true"]'

/** True when the pointer landed on the selection toolbar itself. */
function isToolbarTarget(target) {
const element = asElement(target)
Expand All @@ -1994,6 +2004,18 @@ function shouldDismissOnPointerDown(target) {
return !element.closest('.rd-mini, .rd-card')
}

/**
* True when a scroll should close the floating panels. A window-level event
* (no element target) always counts as the reading surface moving; a scroll
* that originates inside a panel or an editable field does not.
*/
function shouldDismissOnScroll(target) {
const element = asElement(target)
if (!element) return true
if (element.closest(EDITABLE_TARGET)) return false
return !element.closest(OWN_SCROLL_SURFACE)
}

/** Drop the current text selection without touching the document. */
function dismissSelection(win) {
try {
Expand Down Expand Up @@ -4043,7 +4065,11 @@ function ReaderView(props) {
setCards((current) => (current.some((entry) => entry.pinned) ? [] : current))
dismissSelection(window)
}
const onScroll = () => {
// The listener sits on window in the capture phase, so it also receives
// scroll events from the annotation editor's own textarea and from any
// scrolling panel. Those must not close the panel the user is typing in.
const onScroll = (event) => {
if (!shouldDismissOnScroll(event.target)) return
setMenu(null)
setChildMenu(null)
setEditing(null)
Expand Down Expand Up @@ -6555,7 +6581,7 @@ function ReaderView(props) {
return h(
'div',
{
className: 'rd-card rd-editor',
className: 'rd-card rd-editor rd-annotation-editor',
style: { ...style, width: style.width, maxHeight: style.maxHeight },
onMouseEnter: () => clearTimeout(hideTimer.current),
onClick: (event) => event.stopPropagation(),
Expand Down Expand Up @@ -6588,7 +6614,7 @@ function ReaderView(props) {
}),
h(
'div',
{ className: 'rd-actions' },
{ className: 'rd-actions rd-sticky-actions' },
h('button', { className: 'rd-btn', onClick: saveEditing }, t('action.save')),
h('button', { className: 'rd-btn', onClick: deleteEditing }, t('action.delete')),
h('button', { className: 'rd-btn ghost', onClick: () => setEditing(null) }, t('action.close')),
Expand Down
45 changes: 45 additions & 0 deletions dsh-reader/eval/visual.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,47 @@ async function main() {
)
const quoteVisible = await page.$eval('.rd-editor .rd-quote', (node) => node.textContent.trim().length > 0).catch(() => false)
record('annotation editor shows the quote', quoteVisible, '')

// The "已收下" toast lives four seconds; wait it out so these shots record the
// editor and the note box instead of whatever transient banner a previous
// step raised.
await page.waitForFunction(() => !document.querySelector('.rd-toast'), { timeout: 8000 })
await shot('03d-annotation-editor')
// A long note scrolls inside its own field. That scroll must not be read as
// the page moving under the anchor, or the editor closes and the note is lost.
await page.click('.rd-annotation-editor textarea')
for (let line = 0; line < 8; line += 1) {
await page.keyboard.type(`line ${line + 1}`)
await page.keyboard.press('Enter')
}
const noteScroll = await page.evaluate(async () => {
const field = document.querySelector('.rd-annotation-editor textarea')
if (!field) return null
const scrollable = field.scrollHeight > field.clientHeight
field.scrollTop = field.scrollHeight
await new Promise((resolve) => setTimeout(resolve, 250))
return { scrollable, kept: Boolean(document.querySelector('.rd-annotation-editor')), scrollTop: field.scrollTop }
})
record('annotation note field scrolls on its own', noteScroll?.scrollable === true, noteScroll ? `scrollTop ${Math.round(noteScroll.scrollTop)}` : 'field missing')
record('scrolling inside the note keeps the editor open', noteScroll?.kept === true, noteScroll ? '' : 'field missing')
const dismissedByPageScroll = await page.evaluate(async () => {
const main = document.querySelector('.rd-main')
if (!main) return null
const before = main.scrollTop
// Jump instantly: the panes use smooth scrolling, and an animated restore
// would leave the rest of the walkthrough on a half-scrolled page.
const previousBehavior = main.style.scrollBehavior
main.style.scrollBehavior = 'auto'
main.scrollTop = before + 40
await new Promise((resolve) => setTimeout(resolve, 250))
const closed = !document.querySelector('.rd-annotation-editor')
// Put the reading surface back where the rest of the walkthrough expects it.
main.scrollTop = before
main.style.scrollBehavior = previousBehavior
await new Promise((resolve) => setTimeout(resolve, 250))
return closed
})
record('scrolling the page still closes the annotation editor', dismissedByPageScroll === true, '')
await page.keyboard.press('Escape')
await new Promise((resolve) => setTimeout(resolve, 300))

Expand Down Expand Up @@ -825,6 +865,11 @@ async function main() {
record('note box fits viewport', selected && fitsViewport(noteBox), `box ${Math.round(noteBox.left)},${Math.round(noteBox.top)}`)
record('note box no overflow', noteBox.scrollWidth <= noteBox.clientWidth + 1, `${noteBox.scrollWidth}/${noteBox.clientWidth}`)
record('note textarea fits box', noteTextarea.right <= noteBox.parentRight + 1, `${Math.round(noteTextarea.right)} <= ${Math.round(noteBox.parentRight)}`)

// The "已收下" toast lives four seconds; wait it out so these shots record the
// editor and the note box instead of whatever transient banner a previous
// step raised.
await page.waitForFunction(() => !document.querySelector('.rd-toast'), { timeout: 8000 })
await shot('03e-note-box')
await clickAny('.rd-note-box button', ['取消', 'Cancel'])
await new Promise((resolve) => setTimeout(resolve, 300))
Expand Down
Binary file modified dsh-reader/eval/visual/baseline/03d-annotation-editor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified dsh-reader/eval/visual/baseline/03e-note-box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions dsh-reader/src/client/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,11 @@ export function ReaderView(props) {
setCards((current) => (current.some((entry) => entry.pinned) ? [] : current))
dismissSelection(window)
}
const onScroll = () => {
// The listener sits on window in the capture phase, so it also receives
// scroll events from the annotation editor's own textarea and from any
// scrolling panel. Those must not close the panel the user is typing in.
const onScroll = (event) => {
if (!shouldDismissOnScroll(event.target)) return
setMenu(null)
setChildMenu(null)
setEditing(null)
Expand Down Expand Up @@ -3303,7 +3307,7 @@ export function ReaderView(props) {
return h(
'div',
{
className: 'rd-card rd-editor',
className: 'rd-card rd-editor rd-annotation-editor',
style: { ...style, width: style.width, maxHeight: style.maxHeight },
onMouseEnter: () => clearTimeout(hideTimer.current),
onClick: (event) => event.stopPropagation(),
Expand Down Expand Up @@ -3336,7 +3340,7 @@ export function ReaderView(props) {
}),
h(
'div',
{ className: 'rd-actions' },
{ className: 'rd-actions rd-sticky-actions' },
h('button', { className: 'rd-btn', onClick: saveEditing }, t('action.save')),
h('button', { className: 'rd-btn', onClick: deleteEditing }, t('action.delete')),
h('button', { className: 'rd-btn ghost', onClick: () => setEditing(null) }, t('action.close')),
Expand Down
21 changes: 21 additions & 0 deletions dsh-reader/src/client/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ function asElement(target) {
return target && typeof target.closest === 'function' ? target : null
}

// Overlays float above the reading surface and must not be dismissed by
// scrolling inside themselves: a note field or a long card scrolling its own
// content is not the page moving under the anchor.
const OWN_SCROLL_SURFACE = '.rd-mini, .rd-card, .rd-trans-card'
// Editable fields scroll as the caret moves (and while an IME composes), which
// happens constantly while typing; closing the panel on that would make the
// editor unusable.
const EDITABLE_TARGET = 'textarea, input, select, [contenteditable="true"]'

/** True when the pointer landed on the selection toolbar itself. */
export function isToolbarTarget(target) {
const element = asElement(target)
Expand All @@ -15,6 +24,18 @@ export function shouldDismissOnPointerDown(target) {
return !element.closest('.rd-mini, .rd-card')
}

/**
* True when a scroll should close the floating panels. A window-level event
* (no element target) always counts as the reading surface moving; a scroll
* that originates inside a panel or an editable field does not.
*/
export function shouldDismissOnScroll(target) {
const element = asElement(target)
if (!element) return true
if (element.closest(EDITABLE_TARGET)) return false
return !element.closest(OWN_SCROLL_SURFACE)
}

/** Drop the current text selection without touching the document. */
export function dismissSelection(win) {
try {
Expand Down
3 changes: 2 additions & 1 deletion dsh-reader/src/client/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const READER_CSS = `
.rd-editor .rd-input{width:100%;margin-top:6px}
.rd-editor{max-height:min(64vh,520px)}
.rd-editor textarea{width:100%;min-height:64px;margin-top:8px;resize:vertical;border:1px solid var(--dsw-alias-border-l1);border-radius:9px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:8px 10px;font-size:12.5px;line-height:1.6;font-family:inherit}
.rd-annotation-editor textarea{min-height:96px;max-height:min(34vh,260px);overflow:auto;overscroll-behavior:contain}
.rd-card .rd-actions.rd-sticky-actions{position:sticky;bottom:-1px;z-index:2;background:var(--dsw-alias-bg-overlay);padding:8px 0 2px;margin-top:10px;border-top:1px solid var(--dsw-alias-border-l1)}
.rd-trans-card{position:fixed;z-index:95;background:var(--dsw-alias-bg-overlay);border:1px solid var(--dsw-alias-border-l1);border-radius:12px;box-shadow:0 12px 36px rgba(0,0,0,.22);padding:12px 13px;overflow:auto}
.rd-trans{margin-top:6px;padding-left:10px;border-left:2px solid var(--dsw-alias-border-l1);color:var(--dsw-alias-label-secondary);font-size:.92em;line-height:1.6}
Expand Down Expand Up @@ -162,7 +163,7 @@ export const READER_CSS = `
.rd-pdf-hl{position:absolute;background:color-mix(in srgb,var(--hl,#f2c94c) 30%,transparent);pointer-events:none;border-radius:2px}
.rd-note-mask{position:fixed;inset:0;z-index:100;background:rgba(0,0,0,.28);display:flex;align-items:center;justify-content:center}
.rd-note-box{width:min(420px,90vw);background:var(--dsw-alias-bg-overlay);border:1px solid var(--dsw-alias-border-l1);border-radius:14px;padding:16px;box-shadow:0 16px 48px rgba(0,0,0,.28)}
.rd-note-box textarea{width:100%;min-height:110px;resize:vertical;border:1px solid var(--dsw-alias-border-l1);border-radius:9px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:9px 10px;font-size:13px;line-height:1.6;font-family:inherit}
.rd-note-box textarea{width:100%;min-height:110px;overscroll-behavior:contain;resize:vertical;border:1px solid var(--dsw-alias-border-l1);border-radius:9px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:9px 10px;font-size:13px;line-height:1.6;font-family:inherit}
.rd-note-box .rd-actions{display:flex;justify-content:flex-end;gap:8px;margin-top:12px}
.rd-input{height:28px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:0 9px;font-size:12px;font-family:inherit}
.rd-select{height:28px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-label-primary);font-size:12px;padding:0 6px}
Expand Down
Loading
Loading