Skip to content

fix: Deleting in the code editor causes the instance on the canvas to be removed. #4717

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

Merged
merged 3 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 3 additions & 6 deletions apps/builder/app/builder/shared/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const makeBreakpointCommand = <CommandName extends string>(

export const deleteSelectedInstance = () => {
if ($isPreviewMode.get()) {
builderApi.toast.info("Deleting is not allowed in preview mode.");
return;
}
const textEditingInstanceSelector = $textEditingInstanceSelector.get();
Expand Down Expand Up @@ -375,13 +374,11 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({
},
*/

// instances

{
name: "deleteInstance",
name: "deleteInstanceBuilder",
defaultHotkeys: ["backspace", "delete"],
// this disables hotkey for inputs on style panel
// but still work for input on canvas which call event.preventDefault() in keydown handler
// See "deleteInstanceCanvas" for details on why the command is separated for the canvas and builder.
disableHotkeyOutsideApp: true,
disableOnInputLikeControls: true,
handler: deleteSelectedInstance,
},
Expand Down
10 changes: 10 additions & 0 deletions apps/builder/app/canvas/shared/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ import {
} from "../features/text-editor/toolbar-connector";
import { selectInstance } from "~/shared/awareness";
import { isDescendantOrSelf, type InstanceSelector } from "~/shared/tree-utils";
import { deleteSelectedInstance } from "~/builder/shared/commands";

export const { emitCommand, subscribeCommands } = createCommandsEmitter({
source: "canvas",
externalCommands: ["clickCanvas"],
commands: [
{
name: "deleteInstanceCanvas",
defaultHotkeys: ["backspace", "delete"],
disableHotkeyOutsideApp: true,
// We are not disabling "Backspace" or "Delete" on the canvas. This is the main reason we have separate functions: deleteInstanceCanvas and deleteInstanceBuilder.
disableOnInputLikeControls: false,
handler: deleteSelectedInstance,
},

{
name: "editInstanceText",
hidden: true,
Expand Down
32 changes: 14 additions & 18 deletions apps/builder/app/shared/commands-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,22 @@ export const createCommandsEmitter = <CommandName extends string>({
) {
continue;
}
const element = event.target as HTMLElement;
const isOnInputLikeControl =
["input", "select", "textarea"].includes(
element.tagName.toLowerCase()
) ||
element.isContentEditable ||
// Detect Radix select, dropdown and co.
element.getAttribute("role") === "option";

const { disableOnInputLikeControls } = commandMeta;

// in some cases hotkey override default behavior
// on form tags and contentEditable
// though still proceed when default behavior is prevented
// this hack makes hotkeys work on canvas instances of input etc.
if (
isOnInputLikeControl &&
disableOnInputLikeControls &&
event.defaultPrevented === false
) {
continue;
if (disableOnInputLikeControls) {
const element = event.target as HTMLElement;
const isOnInputLikeControl =
["input", "select", "textarea"].includes(
element.tagName.toLowerCase()
) ||
element.isContentEditable ||
// Detect Radix select, dropdown and co.
element.getAttribute("role") === "option";

if (isOnInputLikeControl) {
continue;
}
}

emitted = true;
Expand Down
Loading