From 4c10531e549520aaf00d3a099150c4e2fcd7496f Mon Sep 17 00:00:00 2001 From: Kevin Goedecke Date: Tue, 10 Nov 2026 13:32:01 -0800 Subject: [PATCH 1/2] fix(editor): copy and paste properties carry bold, italic and underline runs Co-Authored-By: Claude Opus 5.5 --- src/lib/actions/edits.ts | 34 +++++++++++------------ src/lib/frameBridge.ts | 18 ++++++++++--- src/lib/frameRuntime.ts | 7 ++--- src/lib/frameTextRuntime.ts | 35 +++++++++++++++++++----- src/lib/richText.ts | 17 ++++++++++++ tests/richTextBrowser.test.ts | 51 +++++++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+), 31 deletions(-) diff --git a/src/lib/actions/edits.ts b/src/lib/actions/edits.ts index 4d8558cbe..30fc5d33d 100644 --- a/src/lib/actions/edits.ts +++ b/src/lib/actions/edits.ts @@ -12,6 +12,7 @@ import { api } from '../api' import { t } from '../i18n' import { canEditOpenCanvas, pickedSelectors, useStore } from '../store' import { currentPageCanvas } from '../pages' +import { LAYER_TEXT_PROPERTIES } from '../richText' import { recordUpdates, trackSave } from '../history' import { isCanvasElement } from '../drawing' import { @@ -511,19 +512,6 @@ const VISUAL_PROPERTIES = [ 'mix-blend-mode', ] as const -const TEXT_PROPERTIES = [ - 'color', - 'font-family', - 'font-size', - 'font-weight', - 'font-style', - 'line-height', - 'letter-spacing', - 'text-transform', - 'text-decoration-line', - 'text-align', -] as const - const STYLE_CLIP_KEY = 'doop:style-clip' interface StyleClip { @@ -538,12 +526,13 @@ export async function copyProperties(): Promise { const s = useStore.getState() const picked = s.selectedElement if (!picked) return false - const [visual, info] = await Promise.all([ - computedStyles(picked.frameId, picked.selector, [...VISUAL_PROPERTIES, ...TEXT_PROPERTIES]), + const [visual, type, info] = await Promise.all([ + computedStyles(picked.frameId, picked.selector, VISUAL_PROPERTIES), + computedStyles(picked.frameId, picked.selector, LAYER_TEXT_PROPERTIES, { fromTextRun: true }), inspectElement(picked.frameId, picked.selector), ]) - if (!visual || !info) return false - const text = info.hasText ? Object.fromEntries(TEXT_PROPERTIES.map((k) => [k, visual[k] ?? ''])) : null + if (!visual || !type || !info) return false + const text = info.hasText ? Object.fromEntries(LAYER_TEXT_PROPERTIES.map((k) => [k, type[k] ?? ''])) : null const clip: StyleClip = { visual: Object.fromEntries(VISUAL_PROPERTIES.map((k) => [k, visual[k] ?? ''])), text } try { localStorage.setItem(STYLE_CLIP_KEY, JSON.stringify(clip)) @@ -569,7 +558,16 @@ export const hasStyleClip = () => !!styleClip() export function pasteProperties(): boolean { const clip = styleClip() if (!clip) return false - return styleEach((info) => ({ ...clip.visual, ...(clip.text && info.hasText ? clip.text : {}) })) + const p = pickedLayers() + if (!p || p.frame.locked) return false + for (const selector of p.selectors) { + void inspectElement(p.frame.id, selector).then((info) => { + if (!info) return + const text = clip.text && info.hasText ? clip.text : null + void styleElement(p.frame.id, selector, { ...clip.visual, ...text }, { uniformText: !!text }) + }) + } + return true } /* ---- pick a colour from anywhere on screen ---- */ diff --git a/src/lib/frameBridge.ts b/src/lib/frameBridge.ts index dc19d25b6..ef1450804 100644 --- a/src/lib/frameBridge.ts +++ b/src/lib/frameBridge.ts @@ -302,8 +302,15 @@ export async function inspectElement(frameId: string, selector: string): Promise /** Write inline styles onto the element. The runtime saves the edited * document through the frame's normal edit path and answers with the * element's fresh properties. */ -export async function styleElement(frameId: string, selector: string, styles: StylePatch): Promise { - return infoOf(await requestFrame(frameId, 'doop:style', { selector, styles })) +/** `uniformText` makes the patch's type the whole layer's: the inline runs + * under it drop their own values, and / / runs their formatting. */ +export async function styleElement( + frameId: string, + selector: string, + styles: StylePatch, + { uniformText = false }: { uniformText?: boolean } = {}, +): Promise { + return infoOf(await requestFrame(frameId, 'doop:style', { selector, styles, uniformText })) } /** Replace image content through the same live edit/undo path as styles. */ @@ -355,13 +362,16 @@ export async function formatElementText( } /** The element's computed values for `properties`, or null when the selector - * no longer resolves or the frame is not mounted. */ + * no longer resolves or the frame is not mounted. `fromTextRun` reads them + * off the element's first text run instead, where a bold or italic run's + * formatting shows. */ export async function computedStyles( frameId: string, selector: string, properties: readonly string[], + { fromTextRun = false }: { fromTextRun?: boolean } = {}, ): Promise | null> { - const reply = await requestFrame(frameId, 'doop:computed', { selector, properties: [...properties] }) + const reply = await requestFrame(frameId, 'doop:computed', { selector, properties: [...properties], fromTextRun }) return reply && reply.values && typeof reply.values === 'object' ? (reply.values as Record) : null } diff --git a/src/lib/frameRuntime.ts b/src/lib/frameRuntime.ts index 1f6bbee83..073340d8d 100644 --- a/src/lib/frameRuntime.ts +++ b/src/lib/frameRuntime.ts @@ -1252,7 +1252,7 @@ export const FRAME_BOOTSTRAP = ` } else if (Object.keys(edit.touched).length || edit.runs.length || edit.created.length || edit.renamed.length) postEdited(true) return true } - function applyStyle(selector, styles) { + function applyStyle(selector, styles, uniformText) { var el = styleTarget(selector) if (!el || isLocked(el)) return false var textPatch = isTextPatch(styles) @@ -1291,6 +1291,7 @@ export const FRAME_BOOTSTRAP = ` return true } if (textPatch) noteRuns(clearTextRuns(el, styles)) + else if (uniformText) noteRuns(clearTextRuns(el, layerTextPatch(styles))) if (styleEdit && styleEdit.selector === selector) { Object.assign(styleEdit.styles, styles) previewStyle(el, styles) @@ -1319,7 +1320,7 @@ export const FRAME_BOOTSTRAP = ` var computedEl = styleTarget(d.selector) var values = null if (computedEl) { - var cs = getComputedStyle(computedEl) + var cs = getComputedStyle(d.fromTextRun === true ? firstTextRun(computedEl) : computedEl) values = {} d.properties.forEach(function (name) { values[name] = cs.getPropertyValue(name) }) } @@ -1334,7 +1335,7 @@ export const FRAME_BOOTSTRAP = ` parent.postMessage({ type: 'doop:settle-result', reqId: d.reqId }, '*') } if (d.type === 'doop:style' && d.styles && typeof d.styles === 'object') { - var applied = applyStyle(d.selector, d.styles) + var applied = applyStyle(d.selector, d.styles, d.uniformText === true) parent.postMessage({ type: 'doop:style-result', reqId: d.reqId, ok: applied, info: applied ? inspect(d.selector) : null }, '*') } if (d.type === 'doop:replace-image') { diff --git a/src/lib/frameTextRuntime.ts b/src/lib/frameTextRuntime.ts index 817b51300..a857b9c33 100644 --- a/src/lib/frameTextRuntime.ts +++ b/src/lib/frameTextRuntime.ts @@ -1,9 +1,10 @@ -import { TEXT_FORMAT_COMMANDS, TEXT_STYLE_PROPERTIES } from './richText' +import { LAYER_TEXT_PROPERTIES, TEXT_FORMAT_COMMANDS, TEXT_STYLE_PROPERTIES } from './richText' /** Runs in FRAME_BOOTSTRAP's closure, sharing its active element and save path. */ export const FRAME_TEXT_RUNTIME = ` var textCommands = ${JSON.stringify(TEXT_FORMAT_COMMANDS)} var textProps = ${JSON.stringify(TEXT_STYLE_PROPERTIES)} + var layerTextProps = ${JSON.stringify(LAYER_TEXT_PROPERTIES)} var TEXT_RUN_MARK = 'doop-run' var textRange = null var lastTextSelection = '' @@ -260,11 +261,30 @@ export const FRAME_TEXT_RUNTIME = ` run.remove() } + /* The element whose computed type the layer's text shows: its first text + run's holder (a made by ⌘B, say), else the layer itself. */ + function firstTextRun(el) { + var node = textNodesIn(el, null)[0] + return node && node.parentElement ? node.parentElement : el + } + + /* The type properties of a patch, for clearing a layer's runs under it. */ + function layerTextPatch(styles) { + var text = {} + for (var key in styles) if (layerTextProps.indexOf(key) !== -1) text[key] = styles[key] + return text + } + + /* The formatting tags a layer-wide value replaces: a weight set over + runs takes their boldness with it, a style their italics, a decoration + their underline or strike. */ + var FORMAT_TAGS = { 'font-weight': /^(B|STRONG)$/, 'font-style': /^(I|EM)$/, 'text-decoration-line': /^(U|S|STRIKE)$/ } + /* A value set on the whole layer makes it uniform: the inline runs under it - drop their own copy of the property, and a weight set over / - runs takes their boldness with it (the tag becomes a span with the same - attributes), as Figma applies a layer-wide value over its mixed runs. - Returns what a cancelled gesture must undo. */ + drop their own copy of the property, and the formatting tags it replaces + (FORMAT_TAGS) become spans with the same attributes, as Figma applies a + layer-wide value over its mixed runs. Returns what a cancelled gesture + must undo. */ function clearTextRuns(el, styles) { var before = [] var renamed = [] @@ -276,7 +296,10 @@ export const FRAME_TEXT_RUNTIME = ` before.push({ el: run, cssText: cssText }) if (!run.getAttribute('style')) run.removeAttribute('style') } - if (!Object.prototype.hasOwnProperty.call(styles, 'font-weight') || !/^(B|STRONG)$/.test(run.tagName)) return + var replaced = Object.keys(FORMAT_TAGS).some(function (key) { + return Object.prototype.hasOwnProperty.call(styles, key) && FORMAT_TAGS[key].test(run.tagName) + }) + if (!replaced) return var span = document.createElement('span') Array.prototype.forEach.call(run.attributes, function (attr) { span.setAttribute(attr.name, attr.value) }) while (run.firstChild) span.appendChild(run.firstChild) diff --git a/src/lib/richText.ts b/src/lib/richText.ts index 865564509..b253ccbaa 100644 --- a/src/lib/richText.ts +++ b/src/lib/richText.ts @@ -14,3 +14,20 @@ export type TextFormatState = Partial> /** Design panel text properties a selection inside an edited element scopes * to its own runs; set on the whole layer, they override its runs. */ export const TEXT_STYLE_PROPERTIES = ['font-family', 'font-size', 'font-weight', 'color'] as const + +/** The type a pasted look (⌥⌘V) carries from one text layer to another. It + * is read off the source's first text run, since ⌘B / ⌘I / ⌘U format a + * layer by wrapping its text in / / , and it replaces the + * target's runs' own values, so the pasted type is what shows. */ +export const LAYER_TEXT_PROPERTIES = [ + 'color', + 'font-family', + 'font-size', + 'font-weight', + 'font-style', + 'line-height', + 'letter-spacing', + 'text-transform', + 'text-decoration-line', + 'text-align', +] as const diff --git a/tests/richTextBrowser.test.ts b/tests/richTextBrowser.test.ts index 5a81d8424..f9893a764 100644 --- a/tests/richTextBrowser.test.ts +++ b/tests/richTextBrowser.test.ts @@ -303,6 +303,57 @@ describe.skipIf(!findBrowserPath())('rich text editing', { timeout: 30_000 }, () expect((await inspect())?.textMixed).toEqual({}) }) + it('copies the type of a layer formatted with ⌘B / ⌘I and pastes it over the formatting of another', async () => { + await send({ + type: 'doop:html', + html: + '

Bold

' + + '

Plain and marked

', + reqId: 'render', + }) + await frame.waitForSelector('#source') + const reqId = `computed-${++nextReq}` + await send({ + type: 'doop:computed', + selector: '#source', + properties: ['font-weight', 'font-style', 'font-size'], + fromTextRun: true, + reqId, + }) + await page.waitForFunction( + (reqId) => (window as unknown as Fixture).messages.some((m) => m.reqId === reqId), + {}, + reqId, + ) + const copied = await page.evaluate( + (reqId) => + ( + (window as unknown as Fixture).messages.find((m) => m.reqId === reqId) as unknown as { + values: Record + } + ).values, + reqId, + ) + expect(copied).toEqual({ 'font-weight': '700', 'font-style': 'italic', 'font-size': '24px' }) + + await ask('doop:style', { + selector: '#text', + styles: { + 'background-color': 'blue', + 'font-weight': '400', + 'font-style': 'italic', + 'text-decoration-line': 'none', + }, + uniformText: true, + }) + expect(await frame.$eval('#text', (el) => el.innerHTML)).toBe('Plain and marked') + expect(await frame.$$eval('#text span', (runs) => runs.map((r) => getComputedStyle(r).fontWeight))).toEqual([ + '400', + '400', + ]) + expect(await frame.$eval('#text', (el) => getComputedStyle(el).backgroundColor)).toBe('rgb(0, 0, 255)') + }) + it('restores a cancelled size scrub on selected words and saves a committed one once', async () => { await render('

Hello world

') await select(0, 5) From 50e74f40ed5b16c9cfe37655de7dafcc9b5ede32 Mon Sep 17 00:00:00 2001 From: Kevin Goedecke Date: Tue, 10 Nov 2026 13:36:10 -0800 Subject: [PATCH 2/2] fix(editor): copy a run's decoration together with the layer's above it Co-Authored-By: Claude Opus 5.5 --- src/lib/frameRuntime.ts | 7 +++++-- src/lib/frameTextRuntime.ts | 14 ++++++++++++++ tests/richTextBrowser.test.ts | 12 +++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/lib/frameRuntime.ts b/src/lib/frameRuntime.ts index 073340d8d..6f92ea9c6 100644 --- a/src/lib/frameRuntime.ts +++ b/src/lib/frameRuntime.ts @@ -1320,9 +1320,12 @@ export const FRAME_BOOTSTRAP = ` var computedEl = styleTarget(d.selector) var values = null if (computedEl) { - var cs = getComputedStyle(d.fromTextRun === true ? firstTextRun(computedEl) : computedEl) + var run = d.fromTextRun === true ? firstTextRun(computedEl) : computedEl + var cs = getComputedStyle(run) values = {} - d.properties.forEach(function (name) { values[name] = cs.getPropertyValue(name) }) + d.properties.forEach(function (name) { + values[name] = name === 'text-decoration-line' && run !== computedEl ? runDecoration(run, computedEl) : cs.getPropertyValue(name) + }) } parent.postMessage({ type: 'doop:computed-result', reqId: d.reqId, values: values }, '*') } diff --git a/src/lib/frameTextRuntime.ts b/src/lib/frameTextRuntime.ts index a857b9c33..f6fe4bf15 100644 --- a/src/lib/frameTextRuntime.ts +++ b/src/lib/frameTextRuntime.ts @@ -268,6 +268,20 @@ export const FRAME_TEXT_RUNTIME = ` return node && node.parentElement ? node.parentElement : el } + /* The lines a run's text is drawn with: text-decoration is not inherited + but painted across descendants, so an underline on the layer shows on its + run though the run's own computed value is none. Every line set from + the run up to the layer counts. */ + function runDecoration(run, layer) { + var lines = [] + for (var el = run; el; el = el === layer ? null : el.parentElement) { + getComputedStyle(el).textDecorationLine.split(/\\s+/).forEach(function (line) { + if (line && line !== 'none' && lines.indexOf(line) === -1) lines.push(line) + }) + } + return lines.length ? lines.join(' ') : 'none' + } + /* The type properties of a patch, for clearing a layer's runs under it. */ function layerTextPatch(styles) { var text = {} diff --git a/tests/richTextBrowser.test.ts b/tests/richTextBrowser.test.ts index f9893a764..e851bade9 100644 --- a/tests/richTextBrowser.test.ts +++ b/tests/richTextBrowser.test.ts @@ -307,7 +307,7 @@ describe.skipIf(!findBrowserPath())('rich text editing', { timeout: 30_000 }, () await send({ type: 'doop:html', html: - '

Bold

' + + '

Bold

' + '

Plain and marked

', reqId: 'render', }) @@ -316,7 +316,7 @@ describe.skipIf(!findBrowserPath())('rich text editing', { timeout: 30_000 }, () await send({ type: 'doop:computed', selector: '#source', - properties: ['font-weight', 'font-style', 'font-size'], + properties: ['font-weight', 'font-style', 'font-size', 'text-decoration-line'], fromTextRun: true, reqId, }) @@ -334,7 +334,13 @@ describe.skipIf(!findBrowserPath())('rich text editing', { timeout: 30_000 }, () ).values, reqId, ) - expect(copied).toEqual({ 'font-weight': '700', 'font-style': 'italic', 'font-size': '24px' }) + // The paragraph's underline is painted on the run too, though not inherited. + expect(copied).toEqual({ + 'font-weight': '700', + 'font-style': 'italic', + 'font-size': '24px', + 'text-decoration-line': 'line-through underline', + }) await ask('doop:style', { selector: '#text',