AI-native design editor. Open-source Figma alternative.
feat(dom-css): add HTML conversion helpers
Danila Poyarkov9d14250ed25f
diff --git a/packages/dom-css/src/convert.ts b/packages/dom-css/src/convert.tsnew file mode 100644index 000000000..a67aa6ba3--- /dev/null+++ b/packages/dom-css/src/convert.ts@@ -0,0 +1,59 @@+import type { SceneGraph } from '@open-pencil/core/scene-graph'++import { createCSSRuntime } from './runtime'+import { compileTailwindCSS, type CompileTailwindCSSOptions } from './tailwind'+import { designDocumentToSceneGraph, type ToSceneGraphOptions } from './to-scene-graph'+import type { CSSComputeOptions, CSSRuntime, DesignDocument } from './types'++export interface HTMLToDesignDocumentOptions {+ cssText?: string+ runtime?: CSSRuntime+ compute?: CSSComputeOptions+}++export interface HTMLToSceneGraphOptions extends HTMLToDesignDocumentOptions, ToSceneGraphOptions {}++export interface TailwindHTMLToDesignDocumentOptions+ extends Omit<HTMLToDesignDocumentOptions, 'cssText'>, CompileTailwindCSSOptions {}++export interface TailwindHTMLToSceneGraphOptions+ extends TailwindHTMLToDesignDocumentOptions, ToSceneGraphOptions {}++function runtimeForOptions(runtime: CSSRuntime | undefined) {+ return runtime ?? createCSSRuntime()+}++export async function htmlToDesignDocument(+ html: string,+ options: HTMLToDesignDocumentOptions = {}+): Promise<DesignDocument> {+ const runtime = runtimeForOptions(options.runtime)+ const document = runtime.parseHTML(html)+ return runtime.computeStyles(document, options.cssText, options.compute)+}++export async function htmlToSceneGraph(+ html: string,+ options: HTMLToSceneGraphOptions = {}+): Promise<SceneGraph> {+ const document = await htmlToDesignDocument(html, options)+ return designDocumentToSceneGraph(document, options)+}++export async function tailwindHTMLToDesignDocument(+ html: string,+ candidates: string | Iterable<string>,+ options: TailwindHTMLToDesignDocumentOptions = {}+): Promise<DesignDocument> {+ const cssText = await compileTailwindCSS(candidates, options)+ return htmlToDesignDocument(html, { ...options, cssText })+}++export async function tailwindHTMLToSceneGraph(+ html: string,+ candidates: string | Iterable<string>,+ options: TailwindHTMLToSceneGraphOptions = {}+): Promise<SceneGraph> {+ const document = await tailwindHTMLToDesignDocument(html, candidates, options)+ return designDocumentToSceneGraph(document, options)+}diff --git a/packages/dom-css/src/index.ts b/packages/dom-css/src/index.tsindex 15740b99b..3215e97c9 100644--- a/packages/dom-css/src/index.ts+++ b/packages/dom-css/src/index.ts@@ -1,8 +1,20 @@export { serializeHTML, serializeNode } from './serialize'export { createBrowserCSSRuntime, createCSSRuntime, createHeadlessCSSRuntime } from './runtime'+export {+ htmlToDesignDocument,+ htmlToSceneGraph,+ tailwindHTMLToDesignDocument,+ tailwindHTMLToSceneGraph+} from './convert'export { designDocumentToSceneGraph } from './to-scene-graph'export { sceneGraphToDesignDocument } from './from-scene-graph'export { compileTailwindCSS } from './tailwind'+export type {+ HTMLToDesignDocumentOptions,+ HTMLToSceneGraphOptions,+ TailwindHTMLToDesignDocumentOptions,+ TailwindHTMLToSceneGraphOptions+} from './convert'export type { ToDesignDocumentOptions } from './from-scene-graph'export type { BrowserCSSRuntimeOptions } from './runtime'export type { CompileTailwindCSSOptions } from './tailwind'diff --git a/tests/engine/dom-css/conversion.test.ts b/tests/engine/dom-css/conversion.test.tsindex 9bfa58687..0a49e72c0 100644--- a/tests/engine/dom-css/conversion.test.ts+++ b/tests/engine/dom-css/conversion.test.ts@@ -4,8 +4,11 @@ import {compileTailwindCSS,createHeadlessCSSRuntime,designDocumentToSceneGraph,+ htmlToDesignDocument,+ htmlToSceneGraph,sceneGraphToDesignDocument,- serializeHTML+ serializeHTML,+ tailwindHTMLToSceneGraph} from '@open-pencil/dom-css'import {@@ -16,6 +19,48 @@ import {} from '#tests/helpers/dom-css'describe('@open-pencil/dom-css conversion', () => {+ it('converts HTML and CSS to DesignDOM with one API call', async () => {+ const document = await htmlToDesignDocument(cssCardHTML, {+ cssText: cssCardCSS,+ runtime: createHeadlessCSSRuntime()+ })+ const card = document.children[0]++ expect(card?.type).toBe('element')+ if (card?.type !== 'element') return+ expect(card.computedStyle?.width).toBe('320px')+ expect(card.computedStyle?.['border-radius']).toBe('16px')+ })++ it('converts HTML and CSS to a scene graph with one API call', async () => {+ const graph = await htmlToSceneGraph(cssCardHTML, {+ cssText: cssCardCSS,+ runtime: createHeadlessCSSRuntime()+ })+ const page = graph.getPages()[0]+ const card = page ? graph.getChildren(page.id)[0] : undefined++ expect(card?.type).toBe('FRAME')+ expect(card?.width).toBe(320)+ expect(card?.layoutMode).toBe('VERTICAL')+ })++ it('converts Tailwind HTML through generated CSS to a scene graph', async () => {+ const classes = [...tailwindCardClasses]+ const graph = await tailwindHTMLToSceneGraph(+ `<article class="${classes.join(' ')}"><h1>OpenPencil</h1></article>`,+ classes,+ { runtime: createHeadlessCSSRuntime() }+ )+ const page = graph.getPages()[0]+ const card = page ? graph.getChildren(page.id)[0] : undefined++ expect(card?.type).toBe('FRAME')+ expect(card?.width).toBe(320)+ expect(card?.height).toBe(176)+ expect(card?.itemSpacing).toBe(12)+ })+it('projects a DesignDOM card into a scene graph', () => {const graph = designDocumentToSceneGraph(computedCardDocument)const page = graph.getPages()[0]