Skip to content

AI-native design editor. Open-source Figma alternative.

fix: style every character of a text from the design panel (#1667)

Danila Poyarkovf1c19d2e7adb
diff --git a/changes/1667/fixed.md b/changes/1667/fixed.md
new file mode 100644
index 000000000..5cd867138
--- /dev/null
+++ b/changes/1667/fixed.md
@@ -0,0 +1 @@
+- Font size, weight, colour and other text styles set from the design panel apply to every character of a text whose characters were styled in ranges, and the panel shows the style the text is drawn in.
diff --git a/packages/core/src/editor/create.ts b/packages/core/src/editor/create.ts
index ca956f08a..6a2965eac 100644
--- a/packages/core/src/editor/create.ts
+++ b/packages/core/src/editor/create.ts
@@ -486,6 +486,7 @@ export function createEditor(options?: EditorOptions) {
nodes.updateNodeWithUndo(target, next, label)
),
getTextSelectionStyle: textSelectionStyle.selectionStyle,
+ layerChanges: textSelectionStyle.layerChanges,
// Alignment (align, flip, rotate)
...alignment,
diff --git a/packages/core/src/editor/text/selection-style.ts b/packages/core/src/editor/text/selection-style.ts
index 983353658..94be3d01d 100644
--- a/packages/core/src/editor/text/selection-style.ts
+++ b/packages/core/src/editor/text/selection-style.ts
@@ -6,6 +6,8 @@ import type { CharacterStyleOverride, SceneNode, StyleRun } from '@open-pencil/s
import type { EditorContext } from '#core/editor/types'
import {
characterStyles,
+ layerTextStyleChanges,
+ shownTextStyle,
styleRunsWithPatch,
TEXT_STYLE_KEYS,
type TextStyleKey
@@ -109,15 +111,27 @@ export function createTextSelectionStyle(
/**
* The style of the selected characters as the layer's own properties, for the panel to show;
- * characters styled differently show the first one's.
+ * characters styled differently show the first one's. A text not being edited shows its first
+ * character's, so a style its ranges set shows instead of one they hide.
*/
function selectionStyle(node: SceneNode): Partial<SceneNode> | null {
const range = selectedRange(node.id)
- if (!range) return null
+ if (!range) return shownTextStyle(node)
const [style] = characterStyles(node, range[0], range[0] + 1)
// Links, styles, and bindings of characters are not the layer's own properties.
return pick(style, TEXT_STYLE_KEYS)
}
- return { updateNode, updateNodeWithUndo, commitNodeUpdate, selectionStyle }
+ /**
+ * A change the panel makes to a layer: on a text not being edited, a style it sets applies to
+ * every character, so a range styled on its own no longer hides it. While text is edited the
+ * change goes to the selected characters, as `updateNode` sends it.
+ */
+ function layerChanges(id: string, changes: Partial<SceneNode>): Partial<SceneNode> {
+ const node = ctx.graph.getNode(id)
+ if (!node || ctx.state.editingTextId === id) return changes
+ return layerTextStyleChanges(node, changes)
+ }
+
+ return { updateNode, updateNodeWithUndo, commitNodeUpdate, selectionStyle, layerChanges }
}
diff --git a/packages/core/src/text/character-style.ts b/packages/core/src/text/character-style.ts
index 777e79a1a..9a1c85d92 100644
--- a/packages/core/src/text/character-style.ts
+++ b/packages/core/src/text/character-style.ts
@@ -1,5 +1,5 @@
-import { omit, pick } from 'es-toolkit/object'
-import { isEqual } from 'es-toolkit/predicate'
+import { omit, omitBy, pick } from 'es-toolkit/object'
+import { isEmptyObject, isEqual, isUndefined } from 'es-toolkit/predicate'
import {
TEXT_BINDABLE_FIELDS,
@@ -140,12 +140,11 @@ function withoutRedundantOverrides(node: SceneNode, runs: StyleRun[]): StyleRun[
const base = nodeStyle(node)
const result: StyleRun[] = []
for (const run of runs) {
- const style = omit(
- run.style,
- CHARACTER_STYLE_KEYS.filter(
- (key) => run.style[key] !== undefined && isEqual(run.style[key], base[key])
- )
+ const repeated = CHARACTER_STYLE_KEYS.filter(
+ (key) => run.style[key] !== undefined && isEqual(run.style[key], base[key])
)
+ // A key a copy left undefined overrides nothing either.
+ const style = omitBy(omit(run.style, repeated), isUndefined)
if (Object.keys(style).length === 0) continue
const previous = result.at(-1)
if (
@@ -262,3 +261,26 @@ export function textStyleChanges(
styleRuns: styleRunsWithPatch({ ...updated, styleRuns }, 0, node.text.length, sized)
}
}
+
+/**
+ * A change to a text layer as a whole, such as from the design panel: a style it sets applies to
+ * every character, as Figma's node setters do, so no range keeps its own value of it.
+ */
+export function layerTextStyleChanges(
+ node: SceneNode,
+ changes: Partial<SceneNode>
+): Partial<SceneNode> {
+ const style = pick(changes, TEXT_STYLE_KEYS)
+ if (node.type !== 'TEXT' || node.styleRuns.length === 0 || isEmptyObject(style)) return changes
+ return { ...changes, ...textStyleChanges(node, style) }
+}
+
+/**
+ * The style a text layer shows as its own: its first character's, which a range may set; the
+ * panel shows it, as it shows the first selected character's while text is edited.
+ */
+export function shownTextStyle(node: SceneNode): Partial<SceneNode> | null {
+ if (node.type !== 'TEXT' || node.styleRuns.length === 0 || node.text.length === 0) return null
+ const [style] = characterStyles(node, 0, 1)
+ return pick(style, TEXT_STYLE_KEYS)
+}
diff --git a/packages/core/src/text/index.ts b/packages/core/src/text/index.ts
index c4f5e5b59..d282b4cb7 100644
--- a/packages/core/src/text/index.ts
+++ b/packages/core/src/text/index.ts
@@ -25,3 +25,4 @@ export {
type GlyphOutlineProbe,
type OutlineCommand
} from './font/glyphs'
+export { layerTextStyleChanges, shownTextStyle } from './character-style'
diff --git a/packages/core/tests/editor/text/selection-style.test.ts b/packages/core/tests/editor/text/selection-style.test.ts
index 9bc611ceb..306fd5806 100644
--- a/packages/core/tests/editor/text/selection-style.test.ts
+++ b/packages/core/tests/editor/text/selection-style.test.ts
@@ -109,3 +109,63 @@ describe('text case while text is edited', () => {
expect(Object.keys(style)).not.toContain('hyperlink')
})
})
+
+describe('panel edits to a text layer whose characters a range styled', () => {
+ const BLUE: Fill = { ...RED, color: { r: 0, g: 0, b: 1, a: 1 } }
+
+ /** "Hello" drawn at 40 and red by a range over all of it, over the layer's own 14. */
+ function rangeStyled() {
+ const editor = createEditor()
+ cleanups.push(() => editor.dispose())
+ const text = editor.graph.createNode('TEXT', editor.state.currentPageId, {
+ text: 'Hello',
+ fontSize: 14,
+ styleRuns: [{ start: 0, length: 5, style: { fontSize: 40, fills: [RED] } }]
+ })
+ const node = () => expectDefined(editor.graph.getNode(text.id), 'text')
+ return { editor, id: text.id, node }
+ }
+
+ test('show the style its characters are drawn in', () => {
+ const { editor, node } = rangeStyled()
+ expect(editor.getTextSelectionStyle(node())).toMatchObject({ fontSize: 40, fills: [RED] })
+ })
+
+ test('style every character, as Figma does, and undo to the range', () => {
+ const { editor, id, node } = rangeStyled()
+ const runs = node().styleRuns
+
+ editor.updateNodeWithUndo(id, editor.layerChanges(id, { fontSize: 24 }), 'Change fontSize')
+ expect(node().fontSize).toBe(24)
+ expect(node().styleRuns).toEqual([{ start: 0, length: 5, style: { fills: [RED] } }])
+
+ editor.updateNodeWithUndo(id, editor.layerChanges(id, { fills: [BLUE] }), 'Change fills')
+ expect(node().fills).toEqual([BLUE])
+ expect(node().styleRuns).toEqual([])
+
+ editor.undoAction()
+ editor.undoAction()
+ expect(node().fontSize).toBe(14)
+ expect(node().styleRuns).toEqual(runs)
+ })
+
+ test('leave no empty range on a copy once every character takes the style', () => {
+ const { editor, id } = rangeStyled()
+ editor.select([id])
+ editor.duplicateSelected()
+ const [copy] = editor.state.selectedIds
+ editor.updateNodeWithUndo(
+ copy,
+ editor.layerChanges(copy, { fontSize: 24, fills: [BLUE] }),
+ 'Change style'
+ )
+ expect(editor.graph.getNode(copy)?.styleRuns).toEqual([])
+ })
+
+ test('leave a change that styles nothing as it is', () => {
+ const { editor, id } = rangeStyled()
+ expect(editor.layerChanges(id, { textAlignHorizontal: 'CENTER' })).toEqual({
+ textAlignHorizontal: 'CENTER'
+ })
+ })
+})
diff --git a/packages/vue/src/controls/property-list/use.ts b/packages/vue/src/controls/property-list/use.ts
index bddc2fc51..ebf899901 100644
--- a/packages/vue/src/controls/property-list/use.ts
+++ b/packages/vue/src/controls/property-list/use.ts
@@ -69,8 +69,9 @@ export function useEditorPropertyList<K extends PropertyListKey>(propKey: K) {
return own.length === 0 && !isMixed.value ? items.value : own
}
+ /** A text's fills colour every character, as a layer's own fills do in Figma. */
function updateArray(node: SceneNode, value: PropertyListItemFor<K>[], label: string) {
- editor.updateNodeWithUndo(node.id, { [propKey]: value }, label)
+ editor.updateNodeWithUndo(node.id, editor.layerChanges(node.id, { [propKey]: value }), label)
}
function add(item: PropertyListItemFor<K>) {
diff --git a/packages/vue/src/controls/typography/actions.ts b/packages/vue/src/controls/typography/actions.ts
index 641954476..4a63235f8 100644
--- a/packages/vue/src/controls/typography/actions.ts
+++ b/packages/vue/src/controls/typography/actions.ts
@@ -146,7 +146,10 @@ export function createTypographyActions({
// A dragged field's previews belong to the step its commit records, so they are not edits of
// their own that keep the document unsaved after that step is undone.
const batch = useUndoBatch(editor.undo, () => editor.beginInteractiveEdit())
- type Preview = { value: SceneNode[keyof SceneNode]; textStyleId: string | null }
+ /** A layer's own values before a preview, with the ranges a preview over the whole text clears. */
+ type Preview = Pick<SceneNode, 'textStyleId' | 'styleRuns'> & {
+ value: SceneNode[keyof SceneNode]
+ }
let previewKey: keyof SceneNode | undefined
const beforePreview = new Map<string, Preview>()
@@ -158,7 +161,8 @@ export function createTypographyActions({
) {
if (targets.length === 0) return
editor.undo.runBatch(label, () => {
- for (const target of targets) editor.updateNodeWithUndo(target.id, changes(target), label)
+ for (const target of targets)
+ editor.updateNodeWithUndo(target.id, editor.layerChanges(target.id, changes(target)), label)
})
}
@@ -403,9 +407,13 @@ export function createTypographyActions({
beforePreview.clear()
}
for (const target of nodes.value) {
- if (!beforePreview.has(target.id))
- beforePreview.set(target.id, { value: target[key], textStyleId: target.textStyleId })
- editor.updateNode(target.id, { [key]: value })
+ // The panel shows a text's characters' style, so what it replaces is read from the layer.
+ const own = editor.graph.getNode(target.id) ?? target
+ if (!beforePreview.has(target.id)) {
+ const { textStyleId, styleRuns } = own
+ beforePreview.set(target.id, { value: own[key], textStyleId, styleRuns })
+ }
+ editor.updateNode(target.id, editor.layerChanges(target.id, { [key]: value }))
}
}
@@ -416,11 +424,17 @@ export function createTypographyActions({
editor.undo.runBatch(`Change ${String(key)}`, () => {
for (const target of targets) {
const snapshot = snapshots.get(target.id)
+ // While text is edited, the editor records the selected characters' ranges itself.
+ const ranges =
+ snapshot && editor.state.editingTextId !== target.id
+ ? { styleRuns: snapshot.styleRuns }
+ : {}
editor.commitNodeUpdate(
target.id,
{
[key]: snapshot ? snapshot.value : previous,
- ...(snapshot ? { textStyleId: snapshot.textStyleId } : {})
+ ...(snapshot ? { textStyleId: snapshot.textStyleId } : {}),
+ ...ranges
},
`Change ${String(key)}`
)
@@ -434,8 +448,8 @@ export function createTypographyActions({
/** Puts back what a cancelled field previewed, as `cancelAxis` does for a font axis. */
function cancelProp(key: keyof SceneNode) {
if (previewKey === key) {
- for (const [id, before] of beforePreview)
- editor.updateNode(id, { [key]: before.value, textStyleId: before.textStyleId })
+ for (const [id, { value, textStyleId, styleRuns }] of beforePreview)
+ editor.updateNode(id, { [key]: value, textStyleId, styleRuns })
}
previewKey = undefined
beforePreview.clear()
diff --git a/packages/vue/src/controls/typography/axes.ts b/packages/vue/src/controls/typography/axes.ts
index 1ea514432..4054c7073 100644
--- a/packages/vue/src/controls/typography/axes.ts
+++ b/packages/vue/src/controls/typography/axes.ts
@@ -1,7 +1,8 @@
+import { omit } from 'es-toolkit/object'
import { computed, type ComputedRef } from 'vue'
import type { Editor } from '@open-pencil/core'
-import { drawnFontAxes, type DrawnFontAxis } from '@open-pencil/core/text'
+import { drawnFontAxes, layerTextStyleChanges, type DrawnFontAxis } from '@open-pencil/core/text'
import type { FontVariation, SceneNode } from '@open-pencil/scene-graph'
import { MIXED, type MixedValue } from '#vue/controls/node-props/use'
@@ -34,14 +35,40 @@ export function styleFontVariations(node: SceneNode): FontVariation[] {
return node.fontVariations.filter(({ axis }) => axis === OPTICAL_SIZE)
}
-/** The text's axes with `axis` set to `value`; an optical size set by hand no longer follows the size. */
-function axisChanges(node: SceneNode, axis: string, value: number): Partial<SceneNode> {
+/** A change to one axis: how it rewrites a list of axes, and what else it sets on the text. */
+interface AxisEdit {
+ variations: (variations: FontVariation[]) => FontVariation[]
+ changes?: Pick<Partial<SceneNode>, 'opticalSizeFromFontSize'>
+}
+
+/** `axis` set to `value`; an optical size set by hand no longer follows the size. */
+function setAxisEdit(axis: string, value: number): AxisEdit {
return {
- fontVariations: [
- ...node.fontVariations.filter((variation) => variation.axis !== axis),
+ variations: (variations) => [
+ ...variations.filter((variation) => variation.axis !== axis),
{ axis, value }
],
- ...(axis === OPTICAL_SIZE ? { opticalSizeFromFontSize: false } : {})
+ ...(axis === OPTICAL_SIZE ? { changes: { opticalSizeFromFontSize: false } } : {})
+ }
+}
+
+/**
+ * An axis edit on a whole text: on its own axes and on each range's, so a range keeps the other
+ * axes it sets, as it keeps its other styles.
+ */
+function wholeTextAxisChanges(node: SceneNode, edit: AxisEdit): Partial<SceneNode> {
+ const styleRuns = node.styleRuns.map((run) =>
+ run.style.fontVariations
+ ? {
+ ...run,
+ style: { ...run.style, fontVariations: edit.variations(run.style.fontVariations) }
+ }
+ : run
+ )
+ return {
+ styleRuns,
+ ...layerTextStyleChanges({ ...node, styleRuns }, edit.changes ?? {}),
+ fontVariations: edit.variations(node.fontVariations)
}
}
@@ -80,26 +107,51 @@ export function createTypographyAxes(editor: Editor, nodes: ComputedRef<SceneNod
// A drag or a typed value is previewed, then committed as one undo step from the values it
// started at, as the other typography fields are. While text is edited, the editor applies
// both to the selected characters, and records the step with what was typed.
- type AxisValues = Pick<SceneNode, 'fontVariations' | 'opticalSizeFromFontSize'>
+ type AxisValues = Pick<
+ SceneNode,
+ 'fontVariations' | 'opticalSizeFromFontSize' | 'styleRuns' | 'textStyleId'
+ >
const beforePreview = new Map<string, AxisValues>()
// The previews belong to the step the commit records, so undoing it leaves nothing unsaved.
const batch = useUndoBatch(editor.undo, () => editor.beginInteractiveEdit())
- function previewEach(changes: (node: SceneNode) => Partial<SceneNode>) {
+ /**
+ * Previews `edit` on every selected text: while text is edited the editor applies it to the
+ * selected characters, whose style the panel shows; otherwise to the whole text.
+ */
+ function previewEach(edit: (shown: SceneNode) => AxisEdit) {
batch.ensure('typography:axes', 'Change font axes')
for (const target of nodes.value) {
+ // The panel shows a text's characters' style, so what it replaces is read from the layer.
+ const own = editor.graph.getNode(target.id) ?? target
if (!beforePreview.has(target.id)) {
- const { fontVariations, opticalSizeFromFontSize } = target
- beforePreview.set(target.id, { fontVariations, opticalSizeFromFontSize })
+ const { fontVariations, opticalSizeFromFontSize, styleRuns, textStyleId } = own
+ beforePreview.set(target.id, {
+ fontVariations,
+ opticalSizeFromFontSize,
+ styleRuns,
+ textStyleId
+ })
}
- editor.updateNode(target.id, changes(target))
+ const axisEdit = edit(target)
+ editor.updateNode(
+ target.id,
+ editor.state.editingTextId === target.id
+ ? { fontVariations: axisEdit.variations(target.fontVariations), ...axisEdit.changes }
+ : wholeTextAxisChanges(own, axisEdit)
+ )
}
editor.requestRender()
}
function commitPreview(label: string) {
editor.undo.runBatch(label, () => {
- for (const [id, before] of beforePreview) editor.commitNodeUpdate(id, before, label)
+ // While text is edited, the editor records the selected characters' ranges itself.
+ for (const [id, before] of beforePreview) {
+ const values = omit(before, ['textStyleId'])
+ const edited = editor.state.editingTextId === id
+ editor.commitNodeUpdate(id, edited ? omit(values, ['styleRuns']) : values, label)
+ }
})
beforePreview.clear()
batch.flush()
@@ -107,10 +159,13 @@ export function createTypographyAxes(editor: Editor, nodes: ComputedRef<SceneNod
/** Draws the text at `value` on `axis` while it is dragged or typed. */
function previewAxis(axis: string, value: number) {
- previewEach((node) => axisChanges(node, axis, value))
+ previewEach(() => setAxisEdit(axis, value))
}
- /** Puts back the axes a cancelled edit previewed. */
+ /**
+ * Puts back the axes and ranges a cancelled edit previewed. The text style it names goes back
+ * too, so the editor restores the layer rather than styling selected characters with it.
+ */
function cancelAxis() {
for (const [id, before] of beforePreview) editor.updateNode(id, before)
beforePreview.clear()
@@ -129,12 +184,15 @@ export function createTypographyAxes(editor: Editor, nodes: ComputedRef<SceneNod
* Figma's "Set optical size automatically" does.
*/
function setOpticalSizeAuto(auto: boolean) {
- previewEach((node) => {
- const drawn = drawnAxes(node)?.find((axis) => axis.tag === OPTICAL_SIZE)
- const rest = node.fontVariations.filter(({ axis }) => axis !== OPTICAL_SIZE)
- const fontVariations =
- auto || !drawn ? rest : [...rest, { axis: OPTICAL_SIZE, value: drawn.value }]
- return { opticalSizeFromFontSize: auto, fontVariations }
+ previewEach((shown) => {
+ const drawn = drawnAxes(shown)?.find((axis) => axis.tag === OPTICAL_SIZE)
+ return {
+ variations: (variations) => {
+ const rest = variations.filter(({ axis }) => axis !== OPTICAL_SIZE)
+ return auto || !drawn ? rest : [...rest, { axis: OPTICAL_SIZE, value: drawn.value }]
+ },
+ changes: { opticalSizeFromFontSize: auto }
+ }
})
commitPreview('Change optical size')
}
diff --git a/packages/vue/src/editor/selection-state/nodes.ts b/packages/vue/src/editor/selection-state/nodes.ts
index 952988b2f..f860f2c7e 100644
--- a/packages/vue/src/editor/selection-state/nodes.ts
+++ b/packages/vue/src/editor/selection-state/nodes.ts
@@ -15,8 +15,8 @@ export interface SelectedNodeState {
}
/**
- * The selected layers as the panel shows them: text being edited has the style of its selected
- * characters, which the panel then edits.
+ * The selected layers as the panel shows them: text has the style of its first character, or of
+ * its selected characters while it is edited, which the panel then edits.
*/
export function selectedNodesAsShown(editor: Editor): SceneNode[] {
if (editor.state.editingTextId) void editor.state.textSelectionVersion
diff --git a/packages/vue/tests/controls/typography.test.ts b/packages/vue/tests/controls/typography.test.ts
index ee680151d..17e956a95 100644
--- a/packages/vue/tests/controls/typography.test.ts
+++ b/packages/vue/tests/controls/typography.test.ts
@@ -409,3 +409,154 @@ test('an axis set while text is edited styles the selected characters, and undoe
renderer.destroy()
}
})
+
+/** "Hello" drawn at 40 by a range over all of it, over the layer's own 14, and its actions. */
+function rangeSized() {
+ const editor = createEditor()
+ const text = editor.graph.createNode('TEXT', editor.state.currentPageId, {
+ text: 'Hello',
+ fontSize: 14,
+ styleRuns: [{ start: 0, length: 5, style: { fontSize: 40 } }]
+ })
+ const actions = createTypographyActions({
+ editor,
+ nodes: computed(() => [text]),
+ activeFormatting: computed(() => []),
+ supports: () => true,
+ options: {}
+ })
+ const node = () => editor.graph.getNode(text.id)
+ return { editor, actions, node }
+}
+
+test('a size set on a layer a range sized draws every character at it, and undoes', () => {
+ const { editor, actions, node } = rangeSized()
+ actions.updateProp('fontSize', 30)
+ actions.commitProp('fontSize', 30, 40)
+ expect(node()?.fontSize).toBe(30)
+ expect(node()?.styleRuns).toEqual([])
+ editor.undoAction()
+ expect(node()?.fontSize).toBe(14)
+ expect(node()?.styleRuns).toEqual([{ start: 0, length: 5, style: { fontSize: 40 } }])
+})
+
+test('a cancelled size on a layer a range sized puts the range back', () => {
+ const { actions, node } = rangeSized()
+ actions.updateProp('fontSize', 30)
+ expect(node()?.styleRuns).toEqual([])
+ actions.cancelProp('fontSize')
+ expect(node()?.fontSize).toBe(14)
+ expect(node()?.styleRuns).toEqual([{ start: 0, length: 5, style: { fontSize: 40 } }])
+})
+
+test('a cancelled axis edit on selected characters puts back their own axes', async () => {
+ await fontManager.loadFont('Inter', 'Regular')
+ const ck = await initCanvasKit()
+ const surface = ck.MakeSurface(200, 200)
+ if (!surface) throw new Error('Expected a surface')
+ const renderer = new SkiaRenderer(ck, surface)
+ const editor = createEditor()
+ editor.setCanvasKit(ck, renderer)
+ await renderer.loadFonts()
+ const range = { start: 6, length: 5, style: { fontVariations: [{ axis: 'wght', value: 700 }] } }
+ const text = editor.graph.createNode('TEXT', editor.state.currentPageId, {
+ text: 'Hello world',
+ fontFamily: 'Inter',
+ width: 200,
+ height: 20,
+ styleRuns: [range]
+ })
+ editor.startTextEditing(text.id)
+ const state = editor.textEditor?.state
+ if (!state) throw new Error('Expected a text editor')
+ state.selectionAnchor = 6
+ state.cursor = 11
+ const scope = effectScope()
+ const axes = scope.run(() => createTypographyAxes(editor, computed(() => [text])))
+ if (!axes) throw new Error('Expected the axes')
+ try {
+ axes.previewAxis('wght', 500)
+ axes.cancelAxis()
+ expect(editor.graph.getNode(text.id)?.styleRuns).toEqual([range])
+ } finally {
+ scope.stop()
+ editor.dispose()
+ renderer.destroy()
+ }
+})
+
+test('an axis set on a whole text keeps the other axes a range sets', () => {
+ const editor = createEditor()
+ const width = { start: 0, length: 5, style: { fontVariations: [{ axis: 'wdth', value: 80 }] } }
+ const text = editor.graph.createNode('TEXT', editor.state.currentPageId, {
+ text: 'Hello',
+ styleRuns: [width]
+ })
+ const scope = effectScope()
+ const axes = scope.run(() => createTypographyAxes(editor, computed(() => [text])))
+ if (!axes) throw new Error('Expected the axes')
+ const node = () => editor.graph.getNode(text.id)
+ try {
+ axes.setAxis('wght', 600)
+ expect(node()?.fontVariations).toEqual([{ axis: 'wght', value: 600 }])
+ expect(node()?.styleRuns).toEqual([
+ {
+ start: 0,
+ length: 5,
+ style: {
+ fontVariations: [
+ { axis: 'wdth', value: 80 },
+ { axis: 'wght', value: 600 }
+ ]
+ }
+ }
+ ])
+ editor.undoAction()
+ expect(node()?.fontVariations).toEqual([])
+ expect(node()?.styleRuns).toEqual([width])
+ } finally {
+ scope.stop()
+ editor.dispose()
+ }
+})
+
+test('a cancelled axis preview that outlived text editing puts back the layer and its ranges', async () => {
+ await fontManager.loadFont('Inter', 'Regular')
+ const ck = await initCanvasKit()
+ const surface = ck.MakeSurface(200, 200)
+ if (!surface) throw new Error('Expected a surface')
+ const renderer = new SkiaRenderer(ck, surface)
+ const editor = createEditor()
+ editor.setCanvasKit(ck, renderer)
+ await renderer.loadFonts()
+ const range = { start: 6, length: 5, style: { fontSize: 30 } }
+ const text = editor.graph.createNode('TEXT', editor.state.currentPageId, {
+ text: 'Hello world',
+ fontFamily: 'Inter',
+ width: 200,
+ height: 20,
+ styleRuns: [range]
+ })
+ editor.startTextEditing(text.id)
+ const state = editor.textEditor?.state
+ if (!state) throw new Error('Expected a text editor')
+ state.selectionAnchor = 6
+ state.cursor = 11
+ const scope = effectScope()
+ const axes = scope.run(() => createTypographyAxes(editor, computed(() => [text])))
+ if (!axes) throw new Error('Expected the axes')
+ const node = () => editor.graph.getNode(text.id)
+ try {
+ axes.previewAxis('wght', 500)
+ editor.commitTextEdit()
+ axes.previewAxis('wght', 600)
+ expect(node()?.fontVariations).toEqual([{ axis: 'wght', value: 600 }])
+ axes.cancelAxis()
+ expect(node()?.fontVariations).toEqual([])
+ expect(node()?.styleRuns).toEqual([range])
+ } finally {
+ scope.stop()
+ editor.dispose()
+ renderer.destroy()
+ }
+})