Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions .context/TECHSTACK.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,15 @@ data-doop-webfonts>` for the roster families the frame names and does not load i
custom properties, as one `<style data-doop-carried>`, every rule scoped through a
`data-doop-carried-scope` mark on the moved root so the destination's own layers keep their
look); copy/paste across frames carries it the same way. Tests: `layerTransfer.test.ts`, `layersDragAcross.test.tsx`, `styleCarry.test.ts`.
- Frame paper (`shared/framePaper.ts`): the Frame tool's empty document is white through a
`<style data-doop-paper>:where(body){background:#ffffff}</style>` rule in its head, which has
no specificity, so a design that styles its own body wins. It used to be an inline
`background` on the body, which beat the design's stylesheet: a dark design put into a drawn
frame sat on white, and the white showed as a light line along the frame's edge on a dark
canvas. `withoutPaperOverride` drops that inline white from documents saved before, only where
one of the document's own stylesheets gives the body a background, wherever `withFontFaces`
runs (`actions` on every write, `FrameView`'s save path), so a frame is healed on its next
save. Tests: `framePaper.test.ts`, the paper case in `drawing.test.ts`.
- The pen snaps to the nearest anchor on its unfinished path within eight screen pixels,
highlights the target, and previews a closed curve when approaching the first anchor.
Alt bypasses snapping. Double-clicking a native path opens `PathEditor.tsx`: anchors and
Expand Down
3 changes: 2 additions & 1 deletion server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
type TokenResult,
type TokenType,
} from '../shared/designTokens.ts'
import { withoutPaperOverride } from '../shared/framePaper.ts'
import { emitCanvasEvent } from './events.ts'
import type {
Actor,
Expand Down Expand Up @@ -1716,7 +1717,7 @@ function withLibraryFaces(canvasId: string, html: string): string {
const library = fontsForCanvas(canvas)
/* tokens first: a family named only through a font token is then in the
document for the font scan to find */
const withTokens = withTokenBlock(html, canvas.tokens ?? [], canvas.tokenThemes ?? [])
const withTokens = withTokenBlock(withoutPaperOverride(html), canvas.tokens ?? [], canvas.tokenThemes ?? [])
return withWebFontLinks(withFontFaces(withTokens, library), library)
}

Expand Down
48 changes: 48 additions & 0 deletions shared/framePaper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* The paper a drawn frame starts on. The Frame tool makes an empty document
white, and that white has to give way the moment a design brings a
background of its own: written inline on the body it won instead, so a dark
design put into a drawn frame sat on white, and the white showed as a light
line along the frame's edge wherever the design's edge pixel was blended
(a dark design on a dark canvas). As a `:where()` rule it has no
specificity, so any `body { background }` in the design beats it. */

export const FRAME_PAPER_ATTR = 'data-doop-paper'
export const FRAME_PAPER_CSS = ':where(body){background:#ffffff}'
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
export const FRAME_PAPER_STYLE = `<style ${FRAME_PAPER_ATTR}>${FRAME_PAPER_CSS}</style>`

const BODY_TAG = /<body\b[^>]*>/i
const STYLE_ATTR = /\bstyle="([^"]*)"/i
/* the white as the Frame tool wrote it before, in the forms a style
attribute serializes it to */
const INLINE_WHITE = /(^|;)\s*background(?:-color)?:\s*(?:rgb\(255,\s*255,\s*255\)|#fff(?:fff)?)\s*(?:;|$)/i
const STYLE_ELEMENT = /<style\b([^>]*)>([\s\S]*?)<\/style>/gi
const CSS_COMMENT = /\/\*[\s\S]*?\*\//g
/* a rule whose subject is the body (`body`, `html, body`, `body.dark`), not
one that only starts there (`body > main`), giving it a background */
const BODY_BACKGROUND =
/(?:^|[{}\s,])body(?:[.:#[][^\s{},>+~]*)?\s*(?:,[^{}]*)?\{[^{}]*\bbackground(?:-color|-image)?\s*:/i
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

/** Whether one of the document's own stylesheets gives the body a background. */
export function stylesBodyBackground(html: string): boolean {
for (const [, attrs = '', css = ''] of html.matchAll(STYLE_ELEMENT)) {
if (attrs.includes(FRAME_PAPER_ATTR)) continue
if (BODY_BACKGROUND.test(css.replace(CSS_COMMENT, ''))) return true
}
return false
}

/** A frame document without the inline white an earlier Frame tool wrote on
* its body, where the design's own stylesheet gives the body a background
* that the inline white was overriding. Every other document comes back as
* it is, and only the body's style attribute is ever touched. */
export function withoutPaperOverride(html: string): string {
const body = BODY_TAG.exec(html)
if (!body) return html
const style = STYLE_ATTR.exec(body[0])
const declarations = style?.[1]
if (!style || declarations === undefined || !INLINE_WHITE.test(declarations)) return html
if (!stylesBodyBackground(html)) return html
const kept = declarations.replace(INLINE_WHITE, (_match, lead: string) => lead).trim()
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
const tag = body[0].replace(style[0], `style="${kept}"`)
return html.slice(0, body.index) + tag + html.slice(body.index + body[0].length)
}
7 changes: 6 additions & 1 deletion src/components/FrameView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { sizeLabel, type ResizeHandle } from '../lib/elementResize'
import { withFontFaces } from '../../shared/fonts'
import { withWebFontLinks } from '../../shared/webFonts'
import { withTokenBlock } from '../../shared/designTokens'
import { withoutPaperOverride } from '../../shared/framePaper'
import { canvasFonts } from '../lib/customFonts'
import { t as translate, useT } from '../lib/i18n'

Expand Down Expand Up @@ -1411,7 +1412,11 @@ export const FrameView = memo(function FrameView({
const library = canvasFonts()
const canvas = useStore.getState().canvas
/* tokens first, so a family named through a font token is seen by the font scan */
const withTokens = withTokenBlock(ev.data.html, canvas?.tokens ?? [], canvas?.tokenThemes ?? [])
const withTokens = withTokenBlock(
withoutPaperOverride(ev.data.html),
canvas?.tokens ?? [],
canvas?.tokenThemes ?? [],
)
const html = withWebFontLinks(withFontFaces(withTokens, library), library)
const before = useStore.getState().canvas?.frames.find((f) => f.id === frame.id)?.html
useStore.getState().patchFrameLocal(frame.id, { html })
Expand Down
10 changes: 8 additions & 2 deletions src/lib/drawing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { currentPaper } from './paper'
import type { ResolvedTheme } from './theme'
import type { DrawingTool } from './tools'
import { paddedBounds, subpathsData } from './svgPath'
import { FRAME_PAPER_ATTR, FRAME_PAPER_CSS } from '../../shared/framePaper'

export interface Point {
x: number
Expand Down Expand Up @@ -170,8 +171,13 @@ export function drawingDocument(drawing: Drawing, ink = DRAWING_INK): string {
const doc = new DOMParser().parseFromString('<!doctype html><html><head></head><body></body></html>', 'text/html')
doc.documentElement.style.cssText = 'width:100%;height:100%;margin:0;'
doc.body.style.cssText = 'width:100%;height:100%;margin:0;'
if (drawing.tool === 'frame') doc.body.style.background = '#ffffff'
else {
if (drawing.tool === 'frame') {
/* white until a design brings a background of its own (framePaper) */
const paper = doc.createElement('style')
paper.setAttribute(FRAME_PAPER_ATTR, '')
paper.textContent = FRAME_PAPER_CSS
doc.head.append(paper)
} else {
doc.body.setAttribute('data-doop-canvas-element', 'true')
doc.body.append(drawingElement(doc, drawing, true, ink))
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/drawingMove.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Frame } from '../../shared/types'
import { framePageId } from '../../shared/pages'
import { FRAME_PAPER_ATTR } from '../../shared/framePaper'
import { api, ApiError } from './api'
import { drawingDocument, isCanvasElement, queueDrawingSave, type Point } from './drawing'
import { recordCreate, recordDrawingTransfer, recordUpdates } from './history'
Expand Down Expand Up @@ -209,7 +210,7 @@ export function moveDrawing(
placeDrawing(el, rect, target, hit?.geometry)
const destinationDoc = target ? parse(target.html) : parse(drawingDocument({ tool: 'frame', ...rect }))
if (!target) {
destinationDoc.body.style.background = ''
destinationDoc.querySelector(`style[${FRAME_PAPER_ATTR}]`)?.remove()
destinationDoc.body.setAttribute('data-doop-canvas-element', 'true')
}
/* an element with an id of its own keeps it (its carried rules name it)
Expand Down
6 changes: 6 additions & 0 deletions tests/drawing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ describe('drawing geometry and markup', () => {
expect(isCanvasElement(drawingDocument({ ...box, tool: 'frame' }))).toBe(false)
})

it('gives a drawn frame its white as a rule any design background beats, not inline on the body', () => {
const frame = doc(drawingDocument({ ...box, tool: 'frame' }))
expect(frame.body.style.background).toBe('')
expect(frame.head.querySelector('style[data-doop-paper]')?.textContent).toBe(':where(body){background:#ffffff}')
})

it('inverts the ink of text and strokes on bare canvas in the dark theme, never inside a frame', async () => {
expect(drawingInk(target, 'light')).toBe(DRAWING_INK)
expect(drawingInk(target, 'dark')).toBe(DRAWING_INK_ON_DARK)
Expand Down
71 changes: 71 additions & 0 deletions tests/framePaper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, expect, it } from 'vitest'
import { FRAME_PAPER_STYLE, stylesBodyBackground, withoutPaperOverride } from '../shared/framePaper'

const SHELL = 'width: 100%; height: 100%; margin: 0px;'
const WHITE = 'background: rgb(255, 255, 255);'
const legacy = (content: string, bodyStyle = `${SHELL} ${WHITE}`) =>
`<!doctype html>\n<html style="${SHELL}"><head></head><body style="${bodyStyle}">${content}</body></html>`
const DARK_DESIGN = '<meta charset="utf-8"><style>*{margin:0}body{background:#101111;color:#fff}</style><main></main>'

describe('stylesBodyBackground', () => {
const styled = (css: string) => `<html><head><style>${css}</style></head><body></body></html>`

it('finds a rule whose subject is the body', () => {
for (const css of [
'body{background:#101111}',
'html, body { background-color: black }',
'*{margin:0}body.dark{background-image:linear-gradient(#000,#111)}',
'@media (min-width:1px){ body { background: red } }',
])
expect(stylesBodyBackground(styled(css)), css).toBe(true)
})

it('does not count the Frame tool paper itself', () => {
expect(stylesBodyBackground(`<html><head>${FRAME_PAPER_STYLE}</head><body></body></html>`)).toBe(false)
})

it('passes over rules that are not the body getting a background', () => {
for (const css of [
'body{color:red}',
'body > main{background:#000}',
'tbody{background:#000}',
'.body{background:#000}',
'/* body{background:#000} */ main{background:#000}',
])
expect(stylesBodyBackground(styled(css)), css).toBe(false)
})
})

describe('withoutPaperOverride', () => {
it('drops the inline white where the design styles the body itself', () => {
const html = withoutPaperOverride(legacy(DARK_DESIGN))
expect(html).toBe(legacy(DARK_DESIGN, SHELL))
})

it('reads the white in the forms a style attribute holds it', () => {
for (const white of ['background: #ffffff;', 'background:#fff', 'background-color: rgb(255,255,255);'])
expect(withoutPaperOverride(legacy(DARK_DESIGN, `${SHELL} ${white}`))).toBe(legacy(DARK_DESIGN, SHELL))
})

it('keeps the white of a drawn frame nothing has styled yet', () => {
const empty = legacy('')
expect(withoutPaperOverride(empty)).toBe(empty)
const content = legacy('<style>main{background:#101111}</style><main></main>')
expect(withoutPaperOverride(content)).toBe(content)
})

it('keeps a body background that is not the Frame tool white', () => {
const tinted = legacy(DARK_DESIGN, `${SHELL} background: rgb(250, 240, 230);`)
expect(withoutPaperOverride(tinted)).toBe(tinted)
})

it('leaves documents without a styled body, or without a body, alone', () => {
for (const html of ['<main>fragment</main>', '<html><body><p>plain</p></body></html>', DARK_DESIGN])
expect(withoutPaperOverride(html)).toBe(html)
})

it('touches nothing but the body tag', () => {
const html = legacy(`${DARK_DESIGN}<div style="${WHITE}">card</div>`)
expect(withoutPaperOverride(html)).toBe(legacy(`${DARK_DESIGN}<div style="${WHITE}">card</div>`, SHELL))
})
})
Loading