Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Fix editing text inside button (#1739)

Bogdan Chadkin891a96e0c9a1
diff --git a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
index 24b0c2d894..c4984f7503 100644
--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx
@@ -49,13 +49,24 @@ const ContentEditable = ({
const ref = useCallback(
(rootElement: null | HTMLElement) => {
+ // button with contentEditable does not let to press space
+ // so add span inside and use it as editor element in lexical
+ if (rootElement?.tagName === "BUTTON") {
+ const span = document.createElement("span");
+ span.contentEditable = "true";
+ rootElement.appendChild(span);
+ rootElement = span;
+ }
+ if (rootElement) {
+ rootElement.contentEditable = "true";
+ }
editor.setRootElement(rootElement);
elementRef.current = rootElement ?? null;
},
[editor, elementRef]
);
- return <Component ref={ref} {...props} contentEditable={true} />;
+ return <Component ref={ref} {...props} />;
};
type UserProps = Record<Prop["name"], Prop["value"]>;
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 92a7641499..7d93e664e7 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -334,7 +334,12 @@ export const deleteInstance = (instanceSelector: InstanceSelector) => {
};
export const deleteSelectedInstance = () => {
+ const textEditingInstanceSelector = textEditingInstanceSelectorStore.get();
const selectedInstanceSelector = selectedInstanceSelectorStore.get();
+ // cannot delete instance while editing
+ if (textEditingInstanceSelector) {
+ return;
+ }
// @todo tell user they can't delete root
if (
selectedInstanceSelector === undefined ||
diff --git a/packages/prisma-client/prisma/migrations/20230610111903_button_children/migration.ts b/packages/prisma-client/prisma/migrations/20230610111903_button_children/migration.ts
new file mode 100644
index 0000000000..8ec2e668cc
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230610111903_button_children/migration.ts
@@ -0,0 +1,131 @@
+import { PrismaClient } from "./client";
+
+type Text = {
+ type: "text";
+ value: string;
+};
+
+type Id = {
+ type: "id";
+ value: string;
+};
+
+export type Instance = {
+ type: "instance";
+ id: string;
+ component: string;
+ label?: string;
+ children: Array<Id | Text>;
+};
+
+type InstancesList = Instance[];
+
+type BaseProp = {
+ id: string;
+ instanceId: string;
+ name: string;
+ required?: boolean;
+};
+
+type Prop = BaseProp &
+ (
+ | { type: "number"; value: number }
+ | { type: "string"; value: string }
+ | { type: "boolean"; value: boolean }
+ | { type: "asset"; value: string }
+ | {
+ type: "page";
+ value: string | { pageId: string; instanceId: string };
+ }
+ | { type: "string[]"; value: string[] }
+ );
+
+type PropsList = Prop[];
+
+export default async () => {
+ const client = new PrismaClient({
+ // Uncomment to see the queries in console as the migration runs
+ // log: ["query", "info", "warn", "error"],
+ });
+
+ await client.$transaction(
+ async (prisma) => {
+ const chunkSize = 1000;
+ let cursor: undefined | string = undefined;
+ let hasNext = true;
+ while (hasNext) {
+ const builds = await prisma.build.findMany({
+ take: chunkSize,
+ orderBy: {
+ id: "asc",
+ },
+ ...(cursor
+ ? {
+ skip: 1, // Skip the cursor
+ cursor: { id: cursor },
+ }
+ : null),
+ });
+ cursor = builds.at(-1)?.id;
+ hasNext = builds.length === chunkSize;
+
+ for (const build of builds) {
+ const buildId = build.id;
+ try {
+ const instancesList: InstancesList = JSON.parse(build.instances);
+ const propsList: PropsList = JSON.parse(build.props);
+ const newPropsList: PropsList = [];
+ const buttonInstanceIds = new Set<string>();
+ const buttonInnerTexts = new Map<string, string>();
+
+ for (const instance of instancesList) {
+ if (
+ instance.component === "Button" ||
+ instance.component === "VimeoPlayButton"
+ ) {
+ buttonInstanceIds.add(instance.id);
+ }
+ }
+
+ for (const prop of propsList) {
+ if (
+ buttonInstanceIds.has(prop.instanceId) &&
+ prop.name === "innerText"
+ ) {
+ if (prop.type === "string") {
+ buttonInnerTexts.set(prop.instanceId, prop.value);
+ }
+ continue;
+ }
+ newPropsList.push(prop);
+ }
+
+ for (const instance of instancesList) {
+ if (
+ instance.component === "Button" ||
+ instance.component === "VimeoPlayButton"
+ ) {
+ const innerText = buttonInnerTexts.get(instance.id);
+ if (instance.children.length !== 0 || innerText === undefined) {
+ continue;
+ }
+ instance.children.push({ type: "text", value: innerText });
+ }
+ }
+
+ build.instances = JSON.stringify(instancesList);
+ build.props = JSON.stringify(newPropsList);
+ } catch {
+ console.info(`build ${buildId} cannot be converted`);
+ }
+ }
+ await Promise.all(
+ builds.map(({ id, instances, props }) =>
+ prisma.build.update({ where: { id }, data: { instances, props } })
+ )
+ );
+ }
+ },
+ { timeout: 1000 * 60 * 8 }
+ );
+};
diff --git a/packages/prisma-client/prisma/migrations/20230610111903_button_children/schema.prisma b/packages/prisma-client/prisma/migrations/20230610111903_button_children/schema.prisma
new file mode 100644
index 0000000000..9ead9c752b
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230610111903_button_children/schema.prisma
@@ -0,0 +1,232 @@
+// DO NOT EDIT THIS FILE!
+// This is a copy of your schema.prisma that corresponds to the state of the database
+// when all migrations up until this one are applied.
+// It's used to generate a Prisma Client for the migration.
+
+// This is your Prisma schema file,
+// learn more about it in the docs: https://pris.ly/d/prisma-schema
+
+generator client {
+ provider = "prisma-client-js"
+ previewFeatures = ["views", "clientExtensions", "jsonProtocol"]
+ // see commands.ts
+ output = "client"
+}
+
+datasource db {
+ provider = "postgres"
+ url = env("DATABASE_URL")
+}
+
+model Team {
+ id String @id @default(uuid())
+ users User[]
+}
+
+enum UploadStatus {
+ UPLOADING
+ UPLOADED
+}
+
+model File {
+ name String @id
+ format String
+ size Int
+ description String?
+ createdAt DateTime @default(now())
+ updatedAt DateTime @default(now()) @updatedAt
+ meta String @default("{}")
+ status UploadStatus @default(UPLOADING)
+ isDeleted Boolean @default(false)
+ uploaderProject Project? @relation(fields: [uploaderProjectId], references: [id])
+ uploaderProjectId String?
+ assets Asset[]
+}
+
+model Asset {
+ id String @default(uuid()) // not unique!
+ projectId String
+ file File @relation(fields: [name], references: [name])
+ name String
+
+ @@id([id, projectId])
+}
+
+model User {
+ id String @id @default(uuid())
+ email String? @unique
+ provider String?
+ image String?
+ username String?
+ createdAt DateTime @default(now())
+ team Team? @relation(fields: [teamId], references: [id])
+ teamId String?
+ projects Project[]
+}
+
+model Project {
+ id String @id @default(uuid())
+ createdAt DateTime @default(now())
+ title String
+ domain String @unique
+ user User? @relation(fields: [userId], references: [id])
+ userId String?
+ build Build[]
+ isDeleted Boolean @default(false)
+ files File[]
+ projectDomain ProjectDomain[]
+ latestBuild LatestBuildPerProject?
+
+ @@unique([id, isDeleted])
+ @@unique([domain, isDeleted])
+ @@unique([id, domain])
+}
+
+enum PublishStatus {
+ PENDING
+ PUBLISHED
+ FAILED
+}
+
+model Build {
+ id String @unique @default(uuid())
+ version Int @default(0)
+ createdAt DateTime @default(now())
+ updatedAt DateTime @default(now()) @updatedAt
+ pages String
+
+ project Project @relation(fields: [projectId], references: [id])
+ projectId String
+
+ breakpoints String @default("[]")
+ styles String @default("[]")
+ styleSources String @default("[]")
+ styleSourceSelections String @default("[]")
+ props String @default("[]")
+ instances String @default("[]")
+
+ deployment String?
+ publishStatus PublishStatus @default(PENDING)
+
+ @@id([id, projectId])
+}
+
+enum AuthorizationRelation {
+ viewers
+ editors
+ builders
+}
+
+model AuthorizationToken {
+ token String @default(uuid())
+ // No relation to Project, as the Authorization system is not tied to a project
+ projectId String
+ name String @default("")
+ relation AuthorizationRelation @default(viewers)
+ createdAt DateTime @default(now())
+
+ @@id([token, projectId])
+}
+
+enum DomainStatus {
+ INITIALIZING
+ ACTIVE
+ ERROR
+ PENDING
+}
+
+// Domains + last known status and last known txtRecord
+// In the future we can update this table using queue, n8n or temporal workflows.
+// As of now updates are done during UI interactions
+model Domain {
+ id String @id @default(uuid())
+ domain String @unique
+ createdAt DateTime @default(now())
+ updatedAt DateTime @default(now()) @updatedAt
+
+ ProjectDomain ProjectDomain[]
+ // Last known txtRecord of the domain (to check domain ownership)
+ txtRecord String?
+ // create, init, pending, active, error
+ status DomainStatus @default(INITIALIZING)
+ // In case of status="error", this will contain the error message
+ error String?
+
+ projectWithDomain ProjectWithDomain[]
+}
+
+model ProjectDomain {
+ projectId String
+ project Project @relation(fields: [projectId], references: [id])
+ domainId String
+ domain Domain @relation(fields: [domainId], references: [id])
+ createdAt DateTime @default(now())
+ // Generated txt record to check domain ownership
+ txtRecord String @unique @default(uuid())
+
+ // CNAME record to point to the domain
+ cname String
+
+ @@id([projectId, domainId])
+ @@index([domainId])
+}
+
+view ProjectWithDomain {
+ projectId String
+
+ domainId String
+ domain Domain @relation(fields: [domainId], references: [id])
+ txtRecord String
+
+ createdAt DateTime
+
+ // CNAME record to point to the domain
+ cname String
+
+ verified Boolean
+ // To count statistics per user
+ userId String?
+
+ // We can deploy on per domain basis, here for each project domain we have latest build
+ latestBuid LatestBuildPerProjectDomain?
+
+ @@id([projectId, domainId])
+}
+
+view LatestBuildPerProjectDomain {
+ domainId String
+ buildId String
+ projectId String
+ projectWithDomain ProjectWithDomain @relation(fields: [projectId, domainId], references: [projectId, domainId])
+
+ isLatestBuild Boolean
+ publishStatus PublishStatus
+ updatedAt DateTime
+
+ @@id([projectId, domainId])
+}
+
+view LatestBuildPerProject {
+ buildId String
+
+ projectId String
+ domain String
+ project Project @relation(fields: [projectId, domain], references: [id, domain])
+
+ isLatestBuild Boolean
+ publishStatus PublishStatus
+ updatedAt DateTime
+
+ @@id([projectId, domain])
+}
+
+// Dashboard
+view DashboardProject {
+ id String @id @default(uuid())
+ createdAt DateTime @default(now())
+ title String
+ domain String
+ userId String?
+ isDeleted Boolean @default(false)
+ isPublished Boolean
+}
diff --git a/packages/sdk-components-react/src/__generated__/button.props.ts b/packages/sdk-components-react/src/__generated__/button.props.ts
index 83ed7072ae..c13a7d3ec1 100644
--- a/packages/sdk-components-react/src/__generated__/button.props.ts
+++ b/packages/sdk-components-react/src/__generated__/button.props.ts
@@ -456,10 +456,4 @@ export const props: Record<string, PropMeta> = {
control: "text",
type: "string",
},
- innerText: {
- required: false,
- control: "text",
- type: "string",
- defaultValue: "Edit Inner Text in Properties",
- },
};
diff --git a/packages/sdk-components-react/src/__generated__/vimeo-play-button.props.ts b/packages/sdk-components-react/src/__generated__/vimeo-play-button.props.ts
index 34983b6ff7..2ffbb2c54c 100644
--- a/packages/sdk-components-react/src/__generated__/vimeo-play-button.props.ts
+++ b/packages/sdk-components-react/src/__generated__/vimeo-play-button.props.ts
@@ -455,5 +455,4 @@ export const props: Record<string, PropMeta> = {
control: "text",
type: "string",
},
- innerText: { required: false, control: "text", type: "string" },
};
diff --git a/packages/sdk-components-react/src/button.tsx b/packages/sdk-components-react/src/button.tsx
index 5c1f4b7e53..4d8d1b0b34 100644
--- a/packages/sdk-components-react/src/button.tsx
+++ b/packages/sdk-components-react/src/button.tsx
@@ -2,20 +2,12 @@ import { forwardRef, type ElementRef, type ComponentProps } from "react";
export const defaultTag = "button";
-type ButtonProps = ComponentProps<typeof defaultTag> & { innerText?: string };
+type ButtonProps = ComponentProps<typeof defaultTag>;
export const Button = forwardRef<ElementRef<typeof defaultTag>, ButtonProps>(
- (
- {
- type = "submit",
- innerText = "Edit Inner Text in Properties",
- children,
- ...props
- },
- ref
- ) => (
+ ({ type = "submit", children, ...props }, ref) => (
<button type={type} {...props} ref={ref}>
- {children ? children : innerText}
+ {children}
</button>
)
);
diff --git a/packages/sdk-components-react/src/button.ws.tsx b/packages/sdk-components-react/src/button.ws.tsx
index d76de4eb55..8748abb5ec 100644
--- a/packages/sdk-components-react/src/button.ws.tsx
+++ b/packages/sdk-components-react/src/button.ws.tsx
@@ -26,9 +26,16 @@ export const meta: WsComponentMeta = {
{ selector: ":enabled", label: "Enabled" },
],
order: 1,
+ template: [
+ {
+ type: "instance",
+ component: "Button",
+ children: [{ type: "text", value: "Button you can edit" }],
+ },
+ ],
};
export const propsMeta: WsComponentPropsMeta = {
props,
- initialProps: ["type", "innerText", "aria-label"],
+ initialProps: ["type", "aria-label"],
};