AI-native design editor. Open-source Figma alternative.
fix(vue): start CanvasKit once CanvasSurface provides the canvas (#823)
Danila Poyarkov7fff3de1ba68
diff --git a/.storybook/main.ts b/.storybook/main.tsindex de11eeb79..5ccf8d63d 100644--- a/.storybook/main.ts+++ b/.storybook/main.ts@@ -18,11 +18,7 @@ const config: StorybookConfig = {options: { docgen: false }},viteFinal(config) {- const excludedPluginPrefixes = [- 'copy-canvaskit-wasm',- 'open-pencil-automation',- 'vite-plugin-pwa'- ]+ const excludedPluginPrefixes = ['open-pencil-automation', 'vite-plugin-pwa']config.plugins = flattenPlugins(config.plugins ?? []).filter((plugin) => {if (!plugin || typeof plugin !== 'object' || !('name' in plugin)) return truediff --git a/CHANGELOG.md b/CHANGELOG.mdindex 878385d73..1ece8f51c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -34,6 +34,7 @@### Fixed+- Render the canvas with the Vue SDK's `CanvasRoot` and `CanvasSurface`; CanvasKit never started there and the canvas stayed blank.- Keep the view centered on what you were looking at when zooming to 100% or another fixed level, instead of jumping elsewhere whenever the zoom changes.- Draw collaborators' names on their cursors with proper letter spacing and fallback fonts, and end long names with an ellipsis.- Keep line breaks in multi-line text when exporting OpenPencil JSX, so `get_jsx` output and `openpencil export -f jsx` render back to the same text instead of joining the lines with spaces.diff --git a/packages/vue/src/canvas/CanvasRoot.stories.ts b/packages/vue/src/canvas/CanvasRoot.stories.tsnew file mode 100644index 000000000..f488c2e59--- /dev/null+++ b/packages/vue/src/canvas/CanvasRoot.stories.ts@@ -0,0 +1,21 @@+import type { Meta, StoryObj } from '@storybook/vue3-vite'++import CanvasRootExample from './examples/CanvasRootExample.vue'++const meta = {+ title: 'Vue SDK/Canvas/Canvas Root',+ component: CanvasRootExample,+ parameters: {+ docs: {+ description: {+ component:+ 'CanvasRoot connects the injected editor to the CanvasSurface inside it and renders the current page with CanvasKit.'+ }+ }+ }+} satisfies Meta<typeof CanvasRootExample>++export default meta+type Story = StoryObj<typeof meta>++export const Default: Story = {}diff --git a/packages/vue/src/canvas/examples/CanvasRootExample.vue b/packages/vue/src/canvas/examples/CanvasRootExample.vuenew file mode 100644index 000000000..12ac0075f--- /dev/null+++ b/packages/vue/src/canvas/examples/CanvasRootExample.vue@@ -0,0 +1,25 @@+<script setup lang="ts">+import { createEditor } from '@open-pencil/core/editor'+import { parseColor } from '@open-pencil/scene-graph/color'++import CanvasRoot from '#vue/canvas/CanvasRoot.vue'+import CanvasSurface from '#vue/canvas/CanvasSurface.vue'+import { provideEditor } from '#vue/editor/context'++const editor = createEditor()+editor.graph.createNode('RECTANGLE', editor.state.currentPageId, {+ name: 'Card',+ x: 220,+ y: 150,+ width: 160,+ height: 100,+ fills: [{ type: 'SOLID', color: parseColor('#338cf2'), opacity: 1, visible: true }]+})+provideEditor(editor)+</script>++<template>+ <CanvasRoot v-slot="{ ready }" :show-rulers="false">+ <CanvasSurface class="block h-[400px] w-[600px]" :data-ready="ready || undefined" />+ </CanvasRoot>+</template>diff --git a/packages/vue/src/canvas/surface/kit-loader.ts b/packages/vue/src/canvas/surface/kit-loader.tsindex 830067429..e145b2374 100644--- a/packages/vue/src/canvas/surface/kit-loader.ts+++ b/packages/vue/src/canvas/surface/kit-loader.ts@@ -1,5 +1,6 @@+import { whenever } from '@vueuse/core'import type { CanvasKit } from 'canvaskit-wasm'-import { onMounted, onScopeDispose } from 'vue'+import { onScopeDispose } from 'vue'import type { Ref } from 'vue'import { getCanvasKit } from '@open-pencil/core/canvaskit'@@ -42,9 +43,9 @@ export function useCanvasKitLoader({onReady?.()}- onMounted(() => {- void init()- })+ // Start once there is a canvas: a CanvasSurface child hands its element over after the+ // root has mounted, so waiting for mount alone would find none.+ whenever(canvasRef, () => void init(), { once: true, flush: 'post', immediate: true })onScopeDispose(() => {lifecycle.destroyed = truediff --git a/tests/e2e/storybook/canvas-root.spec.ts b/tests/e2e/storybook/canvas-root.spec.tsnew file mode 100644index 000000000..bf085b46f--- /dev/null+++ b/tests/e2e/storybook/canvas-root.spec.ts@@ -0,0 +1,9 @@+import { expect, test } from '@playwright/test'++test('CanvasRoot starts CanvasKit for the CanvasSurface inside it', async ({ page }) => {+ await page.goto('/iframe.html?id=vue-sdk-canvas-canvas-root--default&viewMode=story')+ const canvas = page.locator('canvas')+ await expect(canvas).toHaveAttribute('data-ready', 'true')+ // A canvas keeps its 300×150 default until CanvasKit creates a surface at its CSS size.+ expect(await canvas.evaluate((element: HTMLCanvasElement) => element.width)).toBe(600)+})