Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/builder/app/builder/shared/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import {
deleteInstanceMutable,
extractWebstudioFragment,
insertWebstudioFragmentAt,
insertWebstudioFragmentCopy,
updateWebstudioData,
} from "~/shared/instance-utils";
Expand All @@ -48,6 +49,8 @@ import {
isRichTextContent,
isTreeSatisfyingContentModel,
} from "~/shared/content-model";
import { generateFragmentFromHtml } from "~/shared/html";
import { generateFragmentFromTailwind } from "~/shared/tailwind/tailwind";

export const $styleSourceInputElement = atom<HTMLInputElement | undefined>();

Expand Down Expand Up @@ -519,6 +522,16 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({
handler: () => unwrap(),
},

{
name: "pasteHtmlWithTailwindClasses",
handler: async () => {
const html = await navigator.clipboard.readText();
let fragment = generateFragmentFromHtml(html);
fragment = await generateFragmentFromTailwind(fragment);
return insertWebstudioFragmentAt(fragment);
},
},

// history

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe("parseStyleInput", () => {
test("parses custom property", () => {
const result = parseStyleInput("--custom-color");
expect(result).toEqual(
new Map([["--custom-color", { type: "keyword", value: "unset" }]])
new Map([["--custom-color", { type: "unparsed", value: "" }]])
);
});

Expand Down Expand Up @@ -43,7 +43,7 @@ describe("parseStyleInput", () => {
test("converts unknown property to custom property assuming user forgot to add --", () => {
const result = parseStyleInput("notaproperty");
expect(result).toEqual(
new Map([["--notaproperty", { type: "keyword", value: "unset" }]])
new Map([["--notaproperty", { type: "unparsed", value: "" }]])
);
});

Expand Down
21 changes: 13 additions & 8 deletions apps/builder/app/shared/html.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,20 @@ test("generate style attribute as local styles", () => {
);
});

test("paste svg as html embed", () => {
test("optionally paste svg as html embed", () => {
expect(
generateFragmentFromHtml(`
<div>
<svg viewBox="0 0 20 20">
<rect x="5" y="5" width="10" height="10" />
</svg>
</div>
`)
generateFragmentFromHtml(
`
<div>
<svg viewBox="0 0 20 20">
<rect x="5" y="5" width="10" height="10" />
</svg>
</div>
`,
{
unknownTags: true,
}
)
).toEqual(
renderTemplate(
<ws.element ws:tag="div">
Expand Down
11 changes: 9 additions & 2 deletions apps/builder/app/shared/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ const findContentTags = (element: ElementNode, tags = new Set<string>()) => {
return tags;
};

export const generateFragmentFromHtml = (html: string): WebstudioFragment => {
export const generateFragmentFromHtml = (
html: string,
options?: { unknownTags?: boolean }
): WebstudioFragment => {
const attributeTypes = getAttributeTypes();
const instances = new Map<Instance["id"], Instance>();
const styleSourceSelections: StyleSourceSelection[] = [];
Expand Down Expand Up @@ -112,7 +115,11 @@ export const generateFragmentFromHtml = (html: string): WebstudioFragment => {
};

const convertElementToInstance = (node: ElementNode) => {
if (node.tagName === "svg" && node.sourceCodeLocation) {
if (
node.tagName === "svg" &&
node.sourceCodeLocation &&
options?.unknownTags
) {
const { startCol, startOffset, endOffset } = node.sourceCodeLocation;
const indent = startCol - 1;
const htmlFragment = html
Expand Down
29 changes: 29 additions & 0 deletions apps/builder/app/shared/style-object-model.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,35 @@ test("support custom properties in unparsed values", () => {
});
});

test("support empty custom properties", () => {
const model = createModel({
css: `
bodyLocal {
--inset: ;
box-shadow: var(--inset) red;
}
`,
jsx: <$.Body ws:id="body" class="bodyLocal"></$.Body>,
});
expect(
getComputedStyleDecl({
model,
instanceSelector: ["body"],
property: "--inset",
}).computedValue
).toEqual({ type: "unparsed", value: "" });
expect(
getComputedStyleDecl({
model,
instanceSelector: ["body"],
property: "box-shadow",
}).computedValue
).toEqual({
type: "layers",
value: [{ type: "unparsed", value: "red" }],
});
});

test("use fallback value when custom property does not exist", () => {
const model = createModel({
css: `
Expand Down
Loading