AI-native design editor. Open-source Figma alternative.
Add text editing with Paragraph API and inline overlay
Danila Poyarkov7139546cb556
diff --git a/public/Inter-Regular.ttf b/public/Inter-Regular.ttfnew file mode 100644index 000000000..9a53a64e5Binary files /dev/null and b/public/Inter-Regular.ttf differdiff --git a/src/components/EditorCanvas.vue b/src/components/EditorCanvas.vueindex 6f1fb7c8f..85e887e6d 100644--- a/src/components/EditorCanvas.vue+++ b/src/components/EditorCanvas.vue@@ -16,19 +16,98 @@ const cursor = computed(() => {const tool = store.state.activeToolif (tool === 'HAND') return 'grab'if (tool === 'SELECT') return 'default'+ if (tool === 'TEXT') return 'text'return 'crosshair'})++const editingNode = computed(() => {+ if (!store.state.editingTextId) return null+ return store.graph.getNode(store.state.editingTextId) ?? null+})++const textOverlayStyle = computed(() => {+ const node = editingNode.value+ if (!node) return null+ const abs = store.graph.getAbsolutePosition(node.id)+ return {+ left: `${abs.x * store.state.zoom + store.state.panX}px`,+ top: `${abs.y * store.state.zoom + store.state.panY}px`,+ width: `${node.width * store.state.zoom}px`,+ minHeight: `${node.height * store.state.zoom}px`,+ fontSize: `${(node.fontSize || 14) * store.state.zoom}px`,+ fontFamily: node.fontFamily || 'Inter',+ lineHeight: node.lineHeight ? `${node.lineHeight * store.state.zoom}px` : 'normal',+ letterSpacing: `${(node.letterSpacing || 0) * store.state.zoom}px`,+ textAlign: (node.textAlignHorizontal || 'LEFT').toLowerCase() as 'left' | 'center' | 'right'+ }+})++function onTextInput(e: Event) {+ const node = editingNode.value+ if (!node) return+ const text = (e.target as HTMLTextAreaElement).value+ store.graph.updateNode(node.id, { text })+ store.requestRender()+}++function onTextBlur() {+ const node = editingNode.value+ if (!node) return+ store.commitTextEdit(node.id, node.text)+}++function onTextKeyDown(e: KeyboardEvent) {+ if (e.key === 'Escape') {+ onTextBlur()+ }+ e.stopPropagation()+}</script><template>- <canvas ref="canvasRef" :style="{ cursor }" class="editor-canvas" />+ <div class="canvas-container">+ <canvas ref="canvasRef" :style="{ cursor }" class="editor-canvas" />++ <!-- Inline text editor overlay -->+ <textarea+ v-if="editingNode"+ class="text-overlay"+ :style="textOverlayStyle!"+ :value="editingNode.text"+ @input="onTextInput"+ @blur="onTextBlur"+ @keydown="onTextKeyDown"+ autofocus+ />+ </div></template><style scoped>-.editor-canvas {+.canvas-container {flex: 1;- display: block;+ position: relative;min-width: 0;min-height: 0;+ overflow: hidden;+}++.editor-canvas {+ width: 100%;+ height: 100%;+ display: block;+}++.text-overlay {+ position: absolute;+ background: transparent;+ border: 1px solid var(--accent, #3b82f6);+ color: black;+ padding: 0;+ margin: 0;+ outline: none;+ resize: none;+ overflow: hidden;+ z-index: 10;+ font-weight: 400;}</style>diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.tsindex 353879b94..0dd5709d8 100644--- a/src/composables/use-canvas-input.ts+++ b/src/composables/use-canvas-input.ts@@ -624,6 +624,16 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:}}+ function onDblClick(e: MouseEvent) {+ const { cx, cy } = getCoords(e)+ const hit = store.graph.hitTest(cx, cy)+ if (hit && hit.type === 'TEXT') {+ store.select([hit.id])+ store.startTextEditing(hit.id)+ }+ }++ useEventListener(canvasRef, 'dblclick', onDblClick)useEventListener(canvasRef, 'mousedown', onMouseDown)useEventListener(canvasRef, 'mousemove', onMouseMove)useEventListener(canvasRef, 'mouseup', onMouseUp)diff --git a/src/composables/use-canvas.ts b/src/composables/use-canvas.tsindex 30e2060d9..ca4ea88c0 100644--- a/src/composables/use-canvas.ts+++ b/src/composables/use-canvas.ts@@ -42,6 +42,7 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Edito}renderer = new SkiaRenderer(ck, surface)+ renderer.loadFonts().then(() => render())render()}diff --git a/src/engine/renderer.ts b/src/engine/renderer.tsindex 678373b81..111d60cf7 100644--- a/src/engine/renderer.ts+++ b/src/engine/renderer.ts@@ -1,6 +1,14 @@import type { SceneNode, SceneGraph, Fill } from './scene-graph'import type { SnapGuide } from './snap'-import type { CanvasKit, Surface, Canvas, Paint, Font } from 'canvaskit-wasm'+import type {+ CanvasKit,+ Surface,+ Canvas,+ Paint,+ Font,+ FontMgr,+ TypefaceFontProvider+} from 'canvaskit-wasm'export interface RenderOverlays {marquee?: { x: number; y: number; width: number; height: number } | null@@ -17,6 +25,9 @@ export class SkiaRenderer {private selectionPaint: Paintprivate snapPaint: Paintprivate textFont: Font | null = null+ private fontMgr: FontMgr | null = null+ private fontProvider: TypefaceFontProvider | null = null+ private fontsLoaded = falsepanX = 0panY = 0@@ -50,6 +61,23 @@ export class SkiaRenderer {this.textFont = new ck.Font(null, 14)}+ async loadFonts(): Promise<void> {+ const response = await fetch('/Inter-Regular.ttf')+ const fontData = await response.arrayBuffer()++ this.fontProvider = this.ck.TypefaceFontProvider.Make()+ this.fontProvider.registerFont(fontData, 'Inter')++ const typeface = this.ck.Typeface.MakeFreeTypeFaceFromData(fontData)+ if (typeface) {+ this.textFont?.delete()+ this.textFont = new this.ck.Font(typeface, 14)+ }++ this.fontMgr = this.ck.FontMgr.FromData(fontData) ?? null+ this.fontsLoaded = true+ }+render(graph: SceneGraph, selectedIds: Set<string>, overlays: RenderOverlays = {}): void {const canvas = this.surface.getCanvas()canvas.clear(this.ck.Color4f(0.96, 0.96, 0.96, 1.0))@@ -466,11 +494,43 @@ export class SkiaRenderer {}private renderText(canvas: Canvas, node: SceneNode): void {- if (!this.textFont || !('text' in node)) return- const text = (node as SceneNode & { text?: string }).text ?? ''+ const text = node.textif (!text) return- canvas.drawText(text, 0, 14, this.fillPaint, this.textFont)+ if (this.fontsLoaded && this.fontProvider) {+ const paraStyle = new this.ck.ParagraphStyle({+ textAlign: this.getTextAlign(node.textAlignHorizontal),+ textStyle: {+ color: this.fillPaint.getColor(),+ fontFamilies: [node.fontFamily || 'Inter'],+ fontSize: node.fontSize || 14,+ letterSpacing: node.letterSpacing || 0,+ heightMultiplier: node.lineHeight ? node.lineHeight / (node.fontSize || 14) : undefined+ }+ })+ const builder = this.ck.ParagraphBuilder.MakeFromFontProvider(paraStyle, this.fontProvider)+ builder.addText(text)+ const paragraph = builder.build()+ paragraph.layout(node.width || 1e6)+ canvas.drawParagraph(paragraph, 0, 0)+ paragraph.delete()+ builder.delete()+ } else if (this.textFont) {+ canvas.drawText(text, 0, node.fontSize || 14, this.fillPaint, this.textFont)+ }+ }++ private getTextAlign(align: string) {+ switch (align) {+ case 'CENTER':+ return this.ck.TextAlign.Center+ case 'RIGHT':+ return this.ck.TextAlign.Right+ case 'JUSTIFIED':+ return this.ck.TextAlign.Justify+ default:+ return this.ck.TextAlign.Left+ }}private applyFill(fill: Fill): void {@@ -494,6 +554,8 @@ export class SkiaRenderer {this.selectionPaint.delete()this.snapPaint.delete()this.textFont?.delete()+ this.fontMgr?.delete()+ this.fontProvider?.delete()this.surface.delete()}}diff --git a/src/stores/editor.ts b/src/stores/editor.tsindex 36c2f9a8e..0e55c3eb5 100644--- a/src/stores/editor.ts+++ b/src/stores/editor.ts@@ -71,6 +71,7 @@ export function createEditorStore() {snapGuides: [] as SnapGuide[],rotationPreview: null as { nodeId: string; angle: number } | null,dropTargetId: null as string | null,+ editingTextId: null as string | null,panX: 0,panY: 0,zoom: 1,@@ -147,6 +148,16 @@ export function createEditorStore() {requestRender()}+ function startTextEditing(nodeId: string) {+ state.editingTextId = nodeId+ }++ function commitTextEdit(nodeId: string, text: string) {+ graph.updateNode(nodeId, { text })+ state.editingTextId = null+ requestRender()+ }+function updateNode(id: string, changes: Partial<SceneNode>) {graph.updateNode(id, changes)requestRender()@@ -312,6 +323,8 @@ export function createEditorStore() {setRotationPreview,setDropTarget,reparentNodes,+ startTextEditing,+ commitTextEdit,updateNode,createShape,duplicateSelected,