Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

refactor(canvas): share the text font style helpers

Josh Corbettea9b45172846
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/TextEditPortal.tsx b/invokeai/frontend/webv2/src/workbench/widgets/canvas/TextEditPortal.tsx
index 533711eda8..555c564689 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/TextEditPortal.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/TextEditPortal.tsx
@@ -3,9 +3,14 @@ import type { TextEditSession } from '@workbench/canvas-engine/api';
import type { CanvasEngineHandle } from '@workbench/widgets/canvas/useCanvasEngine';
import type { CSSProperties, KeyboardEvent as ReactKeyboardEvent } from 'react';
-import { useMountEffect } from '@platform/react/useMountEffect';
import { useNotify } from '@workbench/useNotify';
import { useTextEditSession } from '@workbench/widgets/canvas/engineStoreHooks';
+import {
+ TextFontReadiness,
+ textFontKey,
+ textFontVariationSettings,
+ useResolvedTextFontFamily,
+} from '@workbench/widgets/canvas/textFontStyle';
import { reportStructuralCommit } from '@workbench/widgets/canvas/useStructuralCommit';
import { useCallback, useRef, useSyncExternalStore } from 'react';
import { useTranslation } from 'react-i18next';
@@ -13,12 +18,6 @@ import { useTranslation } from 'react-i18next';
type TextEditEngine = Pick<CanvasEngineHandle, 'interaction' | 'layers' | 'viewport'> &
Partial<Pick<CanvasEngineHandle, 'fonts'>>;
-const textFontVariationSettings = (source: TextEditSession['source']): string =>
- Object.entries(source.fontVariations ?? {})
- .sort(([left], [right]) => left.localeCompare(right))
- .map(([tag, value]) => `"${tag}" ${value}`)
- .join(', ');
-
/**
* The text-editing portal: a positioned `contenteditable` div, rendered over the
* canvas whenever a text-edit session is active, that IS the text while editing
@@ -55,45 +54,6 @@ const useViewportTick = (engine: TextEditEngine): string => {
return '';
};
-/** Re-renders when the account-scoped runtime registers or evicts a custom face. */
-const useResolvedFontFamily = (engine: TextEditEngine, source: TextEditSession['source']): string => {
- const fonts = engine.fonts;
- const subscribe = useCallback((onChange: () => void) => fonts?.subscribe(onChange) ?? (() => {}), [fonts]);
- const getSnapshot = useCallback(() => fonts?.resolveFamily(source) ?? source.fontFamily, [fonts, source]);
- return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
-};
-
-const textFontKey = (source: TextEditSession['source']): string =>
- JSON.stringify([
- source.fontRef?.id ?? null,
- source.fontRef?.contentHash ?? null,
- source.fontFamily,
- source.fontStyle ?? 'normal',
- source.fontWeight,
- Object.entries(source.fontVariations ?? {})
- .sort(([left], [right]) => left.localeCompare(right))
- .map(([tag, value]) => [tag, value]),
- ]);
-
-/** Keeps the active edit source loading until it is replaced or the portal unmounts. */
-const TextFontReadiness = ({
- fonts,
- source,
-}: {
- fonts: TextEditEngine['fonts'];
- source: TextEditSession['source'];
-}) => {
- useMountEffect(() => {
- if (!fonts || typeof fonts.ensurePreview !== 'function') {
- return;
- }
- const controller = new AbortController();
- void fonts.ensurePreview(source, controller.signal).catch(() => undefined);
- return () => controller.abort();
- });
- return null;
-};
-
/** Reads the editable's text with manual line breaks preserved (`\n` per visual line). */
const readEditableText = (el: HTMLElement): string => el.innerText;
@@ -144,7 +104,7 @@ const TextEditable = ({ engine, session }: TextEditableProps) => {
useViewportTick(engine);
const viewport = engine.viewport.getViewport();
const { source, transform } = session;
- const resolvedFontFamily = useResolvedFontFamily(engine, source);
+ const resolvedFontFamily = useResolvedTextFontFamily(engine.fonts, source);
const ignoreNextBlur = useRef(false);
// Seeds content + focus once when the element mounts, and registers a live-
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/textFontStyle.tsx b/invokeai/frontend/webv2/src/workbench/widgets/canvas/textFontStyle.tsx
new file mode 100644
index 0000000000..74ca6f9326
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/textFontStyle.tsx
@@ -0,0 +1,50 @@
+import type { CanvasFontCapability, CanvasLayerSourceContract } from '@workbench/canvas-engine/api';
+
+import { useMountEffect } from '@platform/react/useMountEffect';
+import { useCallback, useSyncExternalStore } from 'react';
+
+export type TextSource = Extract<CanvasLayerSourceContract, { type: 'text' }>;
+
+export const textFontVariationSettings = (source: Pick<TextSource, 'fontVariations'>): string =>
+ Object.entries(source.fontVariations ?? {})
+ .sort(([left], [right]) => left.localeCompare(right))
+ .map(([tag, value]) => `"${tag}" ${value}`)
+ .join(', ');
+
+export const textFontKey = (source: TextSource): string =>
+ JSON.stringify([
+ source.fontRef?.id ?? null,
+ source.fontRef?.contentHash ?? null,
+ source.fontFamily,
+ source.fontStyle ?? 'normal',
+ source.fontWeight,
+ Object.entries(source.fontVariations ?? {})
+ .sort(([left], [right]) => left.localeCompare(right))
+ .map(([tag, value]) => [tag, value]),
+ ]);
+
+/** Re-renders when the account-scoped runtime registers or evicts a custom face. */
+export const useResolvedTextFontFamily = (fonts: CanvasFontCapability | undefined, source: TextSource): string => {
+ const subscribe = useCallback((onChange: () => void) => fonts?.subscribe(onChange) ?? (() => {}), [fonts]);
+ const getSnapshot = useCallback(() => fonts?.resolveFamily(source) ?? source.fontFamily, [fonts, source]);
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
+};
+
+/** Keeps `source`'s face loading until it is replaced (key by {@link textFontKey}) or the owner unmounts. */
+export const TextFontReadiness = ({
+ fonts,
+ source,
+}: {
+ fonts: CanvasFontCapability | undefined;
+ source: TextSource;
+}) => {
+ useMountEffect(() => {
+ if (!fonts || typeof fonts.ensurePreview !== 'function') {
+ return;
+ }
+ const controller = new AbortController();
+ void fonts.ensurePreview(source, controller.signal).catch(() => undefined);
+ return () => controller.abort();
+ });
+ return null;
+};