diff --git a/AGENTS.md b/AGENTS.md index cf6e6bbd..53556573 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -59,6 +59,10 @@ For broad tasks, identify the primary implementation path first. Expand discover Parallel tool calls are useful for latency but must not be used to load large amounts of speculative context simultaneously. +## UI Notifications + +Use the shared `PlotDirector.toast` service for transient feedback. See `Docs/Codex/Notifications.md` for the API, partial-view usage, and messages that must remain inline. Do not introduce another notification library or page-specific toast host. + ## Build And Test Normal build gate: diff --git a/Docs/Codex/Notifications.md b/Docs/Codex/Notifications.md new file mode 100644 index 00000000..648c247f --- /dev/null +++ b/Docs/Codex/Notifications.md @@ -0,0 +1,42 @@ +# Shared UI notifications + +The application shell loads `wwwroot/js/notifications.js` and `css/notifications.css` once, outside replaceable page/panel content. No additional library is required. + +```js +PlotDirector.toast.success('Changes saved'); +PlotDirector.toast.info('Export completed'); +PlotDirector.toast.warning('Word connection interrupted'); +PlotDirector.toast.error('Unable to save changes'); +``` + +Use success for completed user actions, info for meaningful background completion, warning for attention required, and error for unsuccessful operations. Routine successful background sync, paragraph changes and navigation should stay silent. Keep progress indicators, field validation, destructive confirmations, persistent connection state, and substantial recovery instructions in their existing workflow. + +Success/info dismiss after six seconds. Warning/error remain until dismissed. An optional second argument `{ duration: 10000 }` overrides the duration; zero means manual dismissal. The returned function dismisses that toast. Hover/focus pauses auto dismissal; leaving restarts the duration. Close buttons work with the keyboard and Escape dismisses the focused toast. Notifications never take focus. Status/alert roles announce messages; labels convey categories without relying on colour. + +Identical category/text is suppressed while visible and for five seconds after its last accepted display. At most four toasts appear; the oldest is removed on overflow. This is transient feedback, never the sole record of an ongoing problem. + +Pages and dynamically loaded partial scripts use the same API without creating hosts or subscriptions. For server flash messages, retain the existing consuming TempData read and emit Razor-encoded text: + +```cshtml +@if (TempData["SaveMessage"] is string message) +{ + @message +} +``` + +The singleton observer consumes and removes markers, including newly inserted partials. Do not use `TempData.Peek`/`Keep` for these messages or cache/reinsert consumed flash fragments. Messages are plain text, never HTML. The service and CSS use the same shell in browser and installed PWA, including viewport/safe-area sizing and theme variables. + +## Bounded migration inventory + +- Shared archive/import information; book archive/manuscript information; chapter archive/update messages. +- Character image success, relationship/update information; location list/detail success; floor plan and metric messages; collaborator success; native review save confirmation. +- Word: one interruption warning after a connected session, one recovery info message; explicit sync-request errors also surface as toasts, retaining detailed inline feedback. The separately hosted Office Companion loads the same service and reports a sync failure once until recovery. No initial-offline warning and no routine successful sync/navigation toast. +- Retained: archive/location/image/collaboration errors and recovery instructions; Word persistent status and sync progress/review instructions; onboarding and import-result workflows; scenario warnings; all validation and confirmation dialogs. + +Other feature-specific/admin banners and scripts remain future cleanup opportunities. Do not broaden migration without reviewing their workflow semantics. + +## Focused verification + +Run `node PlotLine.Tests/Browser/shared-notifications.cjs` from the repository root. It uses production assets in isolated Chromium documents with no application/database writes, testing all categories, plain-text rendering, focus preservation, bounded display, dismissal, duplicate suppression, partial markers and repeated script loading, and native Word interruption/recovery events. Both ordinary and simulated standalone presentation are covered; physical installed-PWA and Word acceptance are separate from this check. + +Implementation validation (2026-11-21): solution build and the full executable regression harness passed; focused browser checks passed. Word transport, debounce, capture, paragraph identity, navigation, and native Windows components were not changed. diff --git a/PlotLine.Tests/Browser/shared-notifications.cjs b/PlotLine.Tests/Browser/shared-notifications.cjs new file mode 100644 index 00000000..355f8a49 --- /dev/null +++ b/PlotLine.Tests/Browser/shared-notifications.cjs @@ -0,0 +1,57 @@ +// Isolated production assets: no database or manuscript activity. +const { chromium } = require('playwright'); +const assert = require('node:assert/strict'); +const path = require('node:path'); +const assets = path.resolve(__dirname, '../../PlotLine/wwwroot'); +(async () => { + const browser = await chromium.launch(); + try { + for (const standalone of [false, true]) { + const page = await browser.newPage({ viewport: { width: 390, height: 844 } }); + await page.setContent('
'); + await page.evaluate(value => Object.defineProperty(navigator, 'standalone', { value }), standalone); + await page.addStyleTag({ path: assets + '/css/notifications.css' }); + await page.addScriptTag({ path: assets + '/js/notifications.js' }); + await page.locator('#editor').focus(); + await page.evaluate(() => { for (const type of ['success','info','warning','error']) PlotDirector.toast[type]('' + type + ''); }); + assert.equal(await page.locator('.pd-toast').count(), 4); + assert.equal(await page.locator('.pd-toast b').count(), 0, 'text is never HTML'); + assert.equal(await page.locator('[role=alert]').count(), 2); + assert.equal(await page.locator('[role=status]').count(), 2); + assert(await page.locator('#editor').evaluate(n => n === document.activeElement)); + await page.evaluate(() => PlotDirector.toast.error('error')); + assert.equal(await page.locator('.pd-toast').count(), 4); + await page.evaluate(() => PlotDirector.toast.info('overflow')); + assert.equal(await page.locator('.pd-toast').count(), 4); + assert.equal(await page.getByText('success', { exact: true }).count(), 0); + await page.locator('.pd-toast button').first().focus(); await page.keyboard.press('Escape'); + assert.equal(await page.locator('.pd-toast').count(), 3); + await page.locator('.pd-toast button').first().click(); + await page.evaluate(() => document.querySelectorAll('.pd-toast button').forEach(b => b.click())); + await page.mouse.move(0, 800); + await page.locator('#editor').focus(); + await page.evaluate(() => PlotDirector.toast.success('short', { duration: 80 })); + await page.waitForTimeout(180); assert.equal(await page.locator('.pd-toast').count(), 0); + await page.evaluate(() => document.querySelector('#panel').innerHTML = 'Saved partial'); + await page.locator('.pd-toast').filter({ hasText: 'Saved partial' }).waitFor(); + await page.addScriptTag({ path: assets + '/js/notifications.js' }); + await page.evaluate(() => document.querySelector('#panel').innerHTML = 'Saved partial'); + await page.waitForTimeout(50); + assert.equal(await page.locator('[data-toast-host]').count(), 1); + assert.equal(await page.locator('.pd-toast').count(), 1); + assert.equal(await page.locator('[data-toast-message]').count(), 0); + await page.addScriptTag({ path: assets + '/js/word-notifications.js' }); + await page.addScriptTag({ path: assets + '/js/word-notifications.js' }); + const presence = value => page.evaluate(value => dispatchEvent(new CustomEvent('native-companion-presence', { detail: value ? [{ wordAvailable: true }] : [] })), value); + await presence(false); assert.equal(await page.locator('.pd-toast--warning').count(), 0); + await presence(true); await presence(true); await presence(false); await presence(false); + assert.equal(await page.locator('.pd-toast--warning').count(), 1); + await presence(true); assert.equal(await page.locator('.pd-toast--warning').count(), 0); + assert.equal(await page.locator('.pd-toast--info').count(), 1); + const bounds = await page.locator('[data-toast-host]').boundingBox(); + assert(bounds.x >= 0 && bounds.x + bounds.width <= 390); + console.log('PASS shared notifications: categories, safe text, focus, bounds, dedup, dismissal, partials, singleton, Word transitions; standalone=' + standalone); + await page.close(); + } + } finally { await browser.close(); } +})().catch(e => { console.error(e); process.exitCode = 1; }); diff --git a/PlotLine/Views/Books/Details.cshtml b/PlotLine/Views/Books/Details.cshtml index ae721d09..96dcd1e9 100644 --- a/PlotLine/Views/Books/Details.cshtml +++ b/PlotLine/Views/Books/Details.cshtml @@ -16,7 +16,7 @@ @if (TempData["ArchiveMessage"] is string archiveMessage) { -AI scene suggestions · Word remains authoritative. Edit, approve or reject below. Saving decisions does not import scenes, change prose or overwrite existing story data.
- @if(TempData["NativeReviewSaved"] is string message){