From 30e605ef7ea68794dbb5f4ec93c0c5e9882ab3ab Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Wed, 3 Apr 2024 15:56:56 +0100 Subject: [PATCH] Font size adjustment migration (#3338) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR adds migrations for font size adjustment. ### Change Type - [x] `sdk` — Changes the tldraw SDK - [x] `improvement` — Improving existing features --- packages/tldraw/api-report.md | 2 +- packages/tldraw/api/api.json | 2 +- packages/tlschema/api-report.md | 2 +- packages/tlschema/api/api.json | 2 +- packages/tlschema/src/migrations.test.ts | 14 ++++++++++- packages/tlschema/src/shapes/TLNoteShape.ts | 26 ++++++++++++++------- 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/tldraw/api-report.md b/packages/tldraw/api-report.md index 9cf3984a4..770394c90 100644 --- a/packages/tldraw/api-report.md +++ b/packages/tldraw/api-report.md @@ -1174,7 +1174,7 @@ export class NoteShapeUtil extends ShapeUtil { static props: { color: EnumStyleProp<"black" | "blue" | "green" | "grey" | "light-blue" | "light-green" | "light-red" | "light-violet" | "orange" | "red" | "violet" | "white" | "yellow">; size: EnumStyleProp<"l" | "m" | "s" | "xl">; - fontSizeAdjustment: Validator; + fontSizeAdjustment: Validator; font: EnumStyleProp<"draw" | "mono" | "sans" | "serif">; align: EnumStyleProp<"end-legacy" | "end" | "middle-legacy" | "middle" | "start-legacy" | "start">; verticalAlign: EnumStyleProp<"end" | "middle" | "start">; diff --git a/packages/tldraw/api/api.json b/packages/tldraw/api/api.json index 91f4b479c..9b9418aa2 100644 --- a/packages/tldraw/api/api.json +++ b/packages/tldraw/api/api.json @@ -13517,7 +13517,7 @@ }, { "kind": "Content", - "text": ";\n font: import(\"@tldraw/editor\")." + "text": ";\n font: import(\"@tldraw/editor\")." }, { "kind": "Reference", diff --git a/packages/tlschema/api-report.md b/packages/tlschema/api-report.md index d349e8299..14a8cdd5b 100644 --- a/packages/tlschema/api-report.md +++ b/packages/tlschema/api-report.md @@ -705,7 +705,7 @@ export const noteShapeMigrations: Migrations; export const noteShapeProps: { color: EnumStyleProp<"black" | "blue" | "green" | "grey" | "light-blue" | "light-green" | "light-red" | "light-violet" | "orange" | "red" | "violet" | "white" | "yellow">; size: EnumStyleProp<"l" | "m" | "s" | "xl">; - fontSizeAdjustment: T.Validator; + fontSizeAdjustment: T.Validator; font: EnumStyleProp<"draw" | "mono" | "sans" | "serif">; align: EnumStyleProp<"end-legacy" | "end" | "middle-legacy" | "middle" | "start-legacy" | "start">; verticalAlign: EnumStyleProp<"end" | "middle" | "start">; diff --git a/packages/tlschema/api/api.json b/packages/tlschema/api/api.json index 81a30b7d9..47ba1f52a 100644 --- a/packages/tlschema/api/api.json +++ b/packages/tlschema/api/api.json @@ -2909,7 +2909,7 @@ }, { "kind": "Content", - "text": ";\n font: import(\"..\")." + "text": ";\n font: import(\"..\")." }, { "kind": "Reference", diff --git a/packages/tlschema/src/migrations.test.ts b/packages/tlschema/src/migrations.test.ts index 377920f2d..6156e38fa 100644 --- a/packages/tlschema/src/migrations.test.ts +++ b/packages/tlschema/src/migrations.test.ts @@ -20,7 +20,7 @@ import { embedShapeMigrations } from './shapes/TLEmbedShape' import { GeoShapeVersions, geoShapeMigrations } from './shapes/TLGeoShape' import { imageShapeMigrations } from './shapes/TLImageShape' import { lineShapeMigrations, lineShapeVersions } from './shapes/TLLineShape' -import { noteShapeMigrations } from './shapes/TLNoteShape' +import { noteShapeMigrations, noteShapeVersions } from './shapes/TLNoteShape' import { textShapeMigrations } from './shapes/TLTextShape' import { videoShapeMigrations } from './shapes/TLVideoShape' import { storeMigrations, storeVersions } from './store-migrations' @@ -2030,6 +2030,18 @@ describe('Fractional indexing for line points', () => { }) }) +describe('Add font size adjustment to notes', () => { + const { up, down } = noteShapeMigrations.migrators[noteShapeVersions.AddFontSizeAdjustment] + + test('up works as expected', () => { + expect(up({ props: {} })).toEqual({ props: { fontSizeAdjustment: 0 } }) + }) + + test('down works as expected', () => { + expect(down({ props: { fontSizeAdjustment: 0 } })).toEqual({ props: {} }) + }) +}) + /* --- PUT YOUR MIGRATIONS TESTS ABOVE HERE --- */ for (const migrator of allMigrators) { diff --git a/packages/tlschema/src/shapes/TLNoteShape.ts b/packages/tlschema/src/shapes/TLNoteShape.ts index fcfa0e9f5..972aeca55 100644 --- a/packages/tlschema/src/shapes/TLNoteShape.ts +++ b/packages/tlschema/src/shapes/TLNoteShape.ts @@ -14,7 +14,7 @@ import { ShapePropsType, TLBaseShape } from './TLBaseShape' export const noteShapeProps = { color: DefaultColorStyle, size: DefaultSizeStyle, - fontSizeAdjustment: T.optional(T.positiveNumber), + fontSizeAdjustment: T.positiveNumber, font: DefaultFontStyle, align: DefaultHorizontalAlignStyle, verticalAlign: DefaultVerticalAlignStyle, @@ -29,19 +29,20 @@ export type TLNoteShapeProps = ShapePropsType /** @public */ export type TLNoteShape = TLBaseShape<'note', TLNoteShapeProps> -const Versions = { +export const noteShapeVersions = { AddUrlProp: 1, RemoveJustify: 2, MigrateLegacyAlign: 3, AddVerticalAlign: 4, MakeUrlsValid: 5, + AddFontSizeAdjustment: 6, } as const /** @internal */ export const noteShapeMigrations = defineMigrations({ - currentVersion: Versions.MakeUrlsValid, + currentVersion: noteShapeVersions.AddFontSizeAdjustment, migrators: { - [Versions.AddUrlProp]: { + [noteShapeVersions.AddUrlProp]: { up: (shape) => { return { ...shape, props: { ...shape.props, url: '' } } }, @@ -50,7 +51,7 @@ export const noteShapeMigrations = defineMigrations({ return { ...shape, props } }, }, - [Versions.RemoveJustify]: { + [noteShapeVersions.RemoveJustify]: { up: (shape) => { let newAlign = shape.props.align if (newAlign === 'justify') { @@ -70,7 +71,7 @@ export const noteShapeMigrations = defineMigrations({ }, }, - [Versions.MigrateLegacyAlign]: { + [noteShapeVersions.MigrateLegacyAlign]: { up: (shape) => { let newAlign: TLDefaultHorizontalAlignStyle switch (shape.props.align) { @@ -116,7 +117,7 @@ export const noteShapeMigrations = defineMigrations({ } }, }, - [Versions.AddVerticalAlign]: { + [noteShapeVersions.AddVerticalAlign]: { up: (shape) => { return { ...shape, @@ -135,7 +136,7 @@ export const noteShapeMigrations = defineMigrations({ } }, }, - [Versions.MakeUrlsValid]: { + [noteShapeVersions.MakeUrlsValid]: { up: (shape) => { const url = shape.props.url if (url !== '' && !T.linkUrl.isValid(shape.props.url)) { @@ -145,5 +146,14 @@ export const noteShapeMigrations = defineMigrations({ }, down: (shape) => shape, }, + [noteShapeVersions.AddFontSizeAdjustment]: { + up: (shape) => { + return { ...shape, props: { ...shape.props, fontSizeAdjustment: 0 } } + }, + down: (shape) => { + const { fontSizeAdjustment: _, ...props } = shape.props + return { ...shape, props } + }, + }, }, })