Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mail): 保证深色渐变背景上的正文可读

Dash10b9d3e16596
diff --git a/apps/desktop/scripts/fixtures/mail-reading/fixture.tsx b/apps/desktop/scripts/fixtures/mail-reading/fixture.tsx
index 1abd4c6ad0..7c55fe9ddb 100644
--- a/apps/desktop/scripts/fixtures/mail-reading/fixture.tsx
+++ b/apps/desktop/scripts/fixtures/mail-reading/fixture.tsx
@@ -23,6 +23,7 @@ body{margin:0;background:white;font-family:Arial,sans-serif}
Example City
</p></div>
<p style="text-align:center;padding:20px;color:black">Keep this ticket for your visit.</p>
+<p class="gradient" style="padding:20px;background:linear-gradient(#fff,#eee);color:black">Read this notice on a gradient.</p>
</td></tr></table></body></html>`;
void mountPage({ render: ctx => <main className="min-h-screen p-3">
diff --git a/apps/desktop/scripts/fixtures/mail-reading/ui-page.mjs b/apps/desktop/scripts/fixtures/mail-reading/ui-page.mjs
index 049d6e66e3..9018024bb5 100644
--- a/apps/desktop/scripts/fixtures/mail-reading/ui-page.mjs
+++ b/apps/desktop/scripts/fixtures/mail-reading/ui-page.mjs
@@ -25,6 +25,7 @@ export default {
heights: paragraphs.map(el => el.getBoundingClientRect().height),
runs: doc.querySelectorAll('[data-cindy-mail-run]').length,
padding: css(doc.querySelector('.address')).paddingLeft,
+ gradient: css(doc.querySelector('.gradient')).backgroundImage,
};
});
if (ui.case === 'fixed-width') {
@@ -33,6 +34,7 @@ export default {
return;
}
if (result.background !== (ui.theme === 'dark' ? 'rgb(129, 71, 14)' : 'rgb(249, 154, 60)')) throw new Error('Mail background lost its intended hue/brightness');
+ if (ui.theme === 'dark' ? result.gradient !== 'none' : !result.gradient.includes('linear-gradient')) throw new Error('Gradient decoration must clear only in dark mode');
if (ui.theme === 'light' && result.color !== 'rgb(0, 0, 0)') throw new Error('Authored light text changed');
if (ui.theme === 'dark' && result.color === 'rgb(0, 0, 0)') throw new Error('Text was not adapted to the dark background');
if (result.runs || result.heights.some(height => height !== 28)) throw new Error('Source indentation changed the rendered line spacing');
diff --git a/apps/desktop/src/renderer/components/chat/__tests__/MailHtmlPreview.test.tsx b/apps/desktop/src/renderer/components/chat/__tests__/MailHtmlPreview.test.tsx
index 757d966a95..d2ccb37f86 100644
--- a/apps/desktop/src/renderer/components/chat/__tests__/MailHtmlPreview.test.tsx
+++ b/apps/desktop/src/renderer/components/chat/__tests__/MailHtmlPreview.test.tsx
@@ -230,7 +230,7 @@ it('adapts the reading palette without changing text structure or accumulating d
root.classList.add('dark');
root.style.setProperty('--surface', '#181818');
root.style.setProperty('--text-primary', '#eeeeee');
- const source = '<body style="background:#f99a3c;color:black"><div style="background:white;border:5px solid white"><p style="margin:0;line-height:28px">\n Alex\n </p></div><img src="cid:logo"></body>';
+ const source = '<body style="background:#f99a3c;color:black"><div style="background:white;border:5px solid white"><p style="margin:0;line-height:28px">\n Alex\n </p></div><section style="background:linear-gradient(#fff,#eee);color:#000">Gradient</section><img src="cid:logo"></body>';
const view = render(<MailHtmlPreview value={source} reading />);
const frame = screen.getByTitle('chat.ghostCall.mailPreview') as HTMLIFrameElement, doc = frame.contentDocument!;
doc.open(); doc.write(frame.srcdoc); doc.close(); fireEvent.load(frame);
@@ -242,6 +242,9 @@ it('adapts the reading palette without changing text structure or accumulating d
expect(doc.querySelector('div')!.style.backgroundColor).toBe(dark ? 'rgb(24, 24, 24)' : 'white');
expect(doc.querySelector('div')!.style.borderTopColor).toBe(dark ? 'rgb(24, 24, 24)' : 'white');
expect(doc.querySelector('img')!.getAttribute('style')).toBeNull();
+ // jsdom miscomputes !important longhands beside background shorthand;
+ // the Electron fixture separately checks the real computed background.
+ expect(doc.querySelector('section')!.style.backgroundImage).toBe(dark ? 'none' : 'linear-gradient(rgb(255, 255, 255), rgb(238, 238, 238))');
expect(doc.querySelector('[data-cindy-mail-run]')).toBeNull();
expect(doc.querySelector('p')).toBe(paragraph);
expect(doc.body.contentEditable).toBe('false');
@@ -259,7 +262,7 @@ it('uses the same reading palette in native injection and restores the original
const frame = document.createElement('iframe'); document.body.append(frame);
try {
const doc = frame.contentDocument!;
- doc.body.innerHTML = '<p style="background:rgb(249,154,60);color:black;border:5px solid white">Ticket</p><img src="data:image/png;base64,YQ==">';
+ doc.body.innerHTML = '<p style="background:rgb(249,154,60);color:black;border:5px solid white">Ticket</p><section style="background:linear-gradient(#fff,#eee);color:#000">Gradient</section><img src="data:image/png;base64,YQ==">';
const original = doc.body.innerHTML;
const palette = { surface: '#181818', text: '#eeeeee' };
const apply = (dark: boolean) => new Function('document', 'getComputedStyle', mailReadingThemeScript(dark, palette))(doc, doc.defaultView!.getComputedStyle.bind(doc.defaultView));
@@ -269,6 +272,7 @@ it('uses the same reading palette in native injection and restores the original
expect(mailReadingColor('rgb(249, 154, 60)', 'background', palette)).toBe('rgba(129, 71, 14, 1)');
expect(doc.querySelector('p')!.style.color).toBe('rgb(238, 238, 238)');
expect(doc.querySelector('img')!.getAttribute('style')).toBeNull();
+ expect(doc.querySelector('section')!.style.backgroundImage).toBe('none');
apply(true);
expect(doc.body.innerHTML).toBe(painted);
apply(false);
diff --git a/apps/desktop/src/renderer/components/chat/mailPreviewAppearance.ts b/apps/desktop/src/renderer/components/chat/mailPreviewAppearance.ts
index e4ff83827a..3b347e2dc5 100644
--- a/apps/desktop/src/renderer/components/chat/mailPreviewAppearance.ts
+++ b/apps/desktop/src/renderer/components/chat/mailPreviewAppearance.ts
@@ -94,6 +94,9 @@ export function followMailPreviewTheme(doc: Document, host: Document, reading =
const set = (name: string, value: string) => el.style.setProperty(name, value, 'important');
if (reading) {
const palette = { surface, text };
+ // Gradients can remain bright even when backgroundColor is transparent.
+ // Remove CSS decoration before making its foreground light, as in compose.
+ if (entry.image && entry.image !== 'none') set('background-image', 'none');
set('background-color', mailReadingColor(entry.background, 'background', palette));
set('color', mailReadingColor(entry.color, 'text', palette));
['top', 'right', 'bottom', 'left'].forEach((side, index) =>
diff --git a/apps/mobile/src/__tests__/pluginCardActions.test.tsx b/apps/mobile/src/__tests__/pluginCardActions.test.tsx
index 1941bba730..09b7209e78 100644
--- a/apps/mobile/src/__tests__/pluginCardActions.test.tsx
+++ b/apps/mobile/src/__tests__/pluginCardActions.test.tsx
@@ -193,7 +193,7 @@ describe("native plugin card deliberate actions", () => {
it('repaints native reading colors on theme changes without replacing the document or changing mail', async () => {
const container = document.createElement('div'), root = createRoot(container);
unmount = () => act(() => root.unmount());
- const html = '<p style="background:rgb(249,154,60);color:black">Ticket</p>';
+ const html = '<p style="background:rgb(249,154,60);color:black">Ticket</p><section style="background:linear-gradient(#fff,#eee);color:#000">Gradient</section>';
const render = () => act(async () => root.render(createElement(MobileMailHtmlPreview, { html, deviceId: 'device' })));
await render();
const source = h.web.source;
@@ -207,6 +207,7 @@ describe("native plugin card deliberate actions", () => {
await render();
expect(h.web.source).toBe(source);
expect(doc.querySelector('p')!.style.backgroundColor).toBe(mode === 'dark' ? 'rgb(129, 71, 14)' : 'rgb(249, 154, 60)');
+ expect(doc.querySelector('section')!.style.backgroundImage).toBe(mode === 'dark' ? 'none' : 'linear-gradient(rgb(255, 255, 255), rgb(238, 238, 238))');
expect(doc.querySelector('[data-cindy-mail-run]')).toBeNull();
}
expect(h.invoke).not.toHaveBeenCalled();
diff --git a/packages/maker-shared/src/mailReadingColors.ts b/packages/maker-shared/src/mailReadingColors.ts
index 2cac69c027..e49334a363 100644
--- a/packages/maker-shared/src/mailReadingColors.ts
+++ b/packages/maker-shared/src/mailReadingColors.ts
@@ -62,6 +62,8 @@ export function mailReadingThemeScript(dark: boolean, palette: MailReadingPalett
var authored=nodes.map(function(el){var css=getComputedStyle(el);return {el:el,style:el.getAttribute('style'),background:css.backgroundColor,color:css.color,borders:['Top','Right','Bottom','Left'].map(function(side){return css['border'+side+'Color']})}});
authored.forEach(function(item){
var el=item.el;el.setAttribute(marker,JSON.stringify(item.style));
+ // CSS gradients must not remain bright behind the adapted light text.
+ el.style.setProperty('background-image','none','important');
el.style.setProperty('background-color',recolor(item.background,'background',palette),'important');
el.style.setProperty('color',recolor(item.color,'text',palette),'important');
['top','right','bottom','left'].forEach(function(side,i){el.style.setProperty('border-'+side+'-color',recolor(item.borders[i],'background',palette),'important')});