Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix editing text inside button #1739

Merged
merged 3 commits into from
Jun 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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"]>;
Expand Down
5 changes: 5 additions & 0 deletions apps/builder/app/shared/instance-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
kof marked this conversation as resolved.
Show resolved Hide resolved
) {
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 }
);
};
Loading