Skip to content

Commit

Permalink
Fix synchronisation
Browse files Browse the repository at this point in the history
  • Loading branch information
istarkov committed Dec 20, 2024
1 parent ee09d46 commit 3ce0904
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { useEffectEvent } from "~/shared/hook-utils/effect-event";
import type { InstanceSelector } from "~/shared/tree-utils";
import type { Instance } from "@webstudio-is/sdk";
import { shallowEqual } from "shallow-equal";
import { emitCommand } from "~/builder/shared/commands";

const TriggerButton = styled("button", {
position: "absolute",
Expand Down Expand Up @@ -84,7 +83,6 @@ const Menu = ({
(templateSelector: InstanceSelector) => {
const insertBefore = modifierKeys.altKey;
insertTemplateAt(templateSelector, anchor, insertBefore);
emitCommand("newInstanceText");
},
[anchor, modifierKeys.altKey]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { shallowEqual } from "shallow-equal";
import { selectInstance } from "~/shared/awareness";
import {
extractWebstudioFragment,
findAllEditableInstanceSelector,
findAvailableDataSources,
getWebstudioData,
insertInstanceChildrenMutable,
Expand All @@ -12,6 +13,8 @@ import {
} from "~/shared/instance-utils";
import {
$instances,
$registeredComponentMetas,
$textEditingInstanceSelector,
findBlockChildSelector,
findBlockSelector,
} from "~/shared/nano-states";
Expand Down Expand Up @@ -111,8 +114,32 @@ export const insertTemplateAt = (
const children: Instance["children"] = [
{ type: "id", value: newRootInstanceId },
];

insertInstanceChildrenMutable(data, children, target);

const selectedInstanceSelector = [
newRootInstanceId,
...target.parentSelector,
];

const selectors: InstanceSelector[] = [];

findAllEditableInstanceSelector(
selectedInstanceSelector,
data.instances,
$registeredComponentMetas.get(),
selectors
);

const editableInstanceSelector = selectors[0];

if (editableInstanceSelector) {
$textEditingInstanceSelector.set({
selector: editableInstanceSelector,
reason: "new",
});
}

selectInstance([newRootInstanceId, ...target.parentSelector]);
});
};
1 change: 0 additions & 1 deletion apps/builder/app/builder/shared/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({
source: "builder",
externalCommands: [
"editInstanceText",
"newInstanceText",
"formatBold",
"formatItalic",
"formatSuperscript",
Expand Down
2 changes: 0 additions & 2 deletions apps/builder/app/canvas/features/text-editor/text-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ import {
} from "~/shared/awareness";
import { shallowEqual } from "shallow-equal";
import { insertTemplateAt } from "~/builder/features/workspace/canvas-tools/outline/block-utils";
import { emitCommand } from "~/canvas/shared/commands";

const BindInstanceToNodePlugin = ({
refs,
Expand Down Expand Up @@ -1127,7 +1126,6 @@ const ContextMenuPluginInternal = ({
*/

insertTemplateAt(templateSelector, rootInstanceSelector, false);
emitCommand("newInstanceText");

event.preventDefault();
return true;
Expand Down
52 changes: 0 additions & 52 deletions apps/builder/app/canvas/shared/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,6 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({
source: "canvas",
externalCommands: ["clickCanvas"],
commands: [
{
name: "newInstanceText",
hidden: true,
disableHotkeyOutsideApp: true,
handler: () => {
const selectedInstanceSelector = $selectedInstanceSelector.get();
if (selectedInstanceSelector === undefined) {
return;
}

if (
isDescendantOrSelf(
$textEditingInstanceSelector.get()?.selector ?? [],
selectedInstanceSelector
)
) {
// already in text editing mode
return;
}

const selectors: InstanceSelector[] = [];

findAllEditableInstanceSelector(
selectedInstanceSelector,
$instances.get(),
$registeredComponentMetas.get(),
selectors
);

if (selectors.length === 0) {
$textEditingInstanceSelector.set(undefined);
return;
}

const editableInstanceSelector = selectors[0];

const element = getElementByInstanceSelector(editableInstanceSelector);
if (element === undefined) {
return;
}
// When an event is triggered from the Builder,
// the canvas element may be unfocused, so it's important to focus the element on the canvas.
element.focus();
selectInstance(editableInstanceSelector);

$textEditingInstanceSelector.set({
selector: editableInstanceSelector,
reason: "new",
});
},
},

{
name: "editInstanceText",
hidden: true,
Expand Down

0 comments on commit 3ce0904

Please sign in to comment.