Skip to content

Commit

Permalink
extract menu additions to functions, remove ifs (#153)
Browse files Browse the repository at this point in the history
* extract to function, remove ifs

* swap CopyAsPng | CopyAsSvg shortcuts

* 1.10.14
  • Loading branch information
mdroidian authored Sep 11, 2023
1 parent 33b1b51 commit f5ec606
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 60 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "query-builder",
"version": "1.10.13",
"version": "1.10.14",
"description": "Introduces new user interfaces for building queries in Roam",
"main": "./build/main.js",
"author": {
Expand Down
110 changes: 53 additions & 57 deletions src/components/TldrawCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,57 @@ const TldrawCanvas = ({ title }: Props) => {
);
};
}, [appRef, allNodes]);

// Menu Overrides
const addFullScreenToggle = (mainMenu: MenuGroup) => {
const viewSubMenu = mainMenu.children.find(
(m): m is SubMenu => m.type === "submenu" && m.id === "view"
);
const viewActionsGroup = viewSubMenu?.children.find(
(m): m is MenuGroup => m.type === "group" && m.id === "view-actions"
);
if (!viewActionsGroup) return;
viewActionsGroup.children.push({
type: "item",
readonlyOk: true,
id: "toggle-full-screen",
disabled: false,
checked: maximized,
actionItem: {
id: "toggle-full-screen",
label: "action.toggle-full-screen" as TLTranslationKey,
kbd: "!3",
onSelect: () => {
setMaximized(!maximized);
},
readonlyOk: true,
},
});
};
const editCopyAsShortcuts = (mainMenu: MenuGroup) => {
const editSubMenu = mainMenu.children.find(
(m): m is SubMenu => m.type === "submenu" && m.id === "edit"
);
const conversionsGroup = editSubMenu?.children.find(
(m): m is MenuGroup => m.type === "group" && m.id === "conversions"
);
const copyAsSubMenu = conversionsGroup?.children.find(
(m): m is SubMenu => m.type === "submenu" && m.id === "copy-as"
);
const copyAsGroup = copyAsSubMenu?.children.find(
(m): m is MenuGroup => m.type === "group" && m.id === "copy-as-group"
);
const copyAsPngItem = copyAsGroup?.children.find(
(m): m is MenuItem => m.type === "item" && m.id === "copy-as-png"
);
const copyAsSvgItem = copyAsGroup?.children.find(
(m): m is MenuItem => m.type === "item" && m.id === "copy-as-svg"
);
if (!copyAsPngItem || !copyAsSvgItem) return;
copyAsPngItem.actionItem.kbd = "$!C";
copyAsSvgItem.actionItem.kbd = "$!X";
};

return (
<div
className={`border border-gray-300 rounded-md bg-white h-full w-full z-10 overflow-hidden ${
Expand Down Expand Up @@ -1574,63 +1625,8 @@ const TldrawCanvas = ({ title }: Props) => {
(m): m is MenuGroup => m.type === "group" && m.id === "menu"
);
if (mainMenu) {
const viewSubMenu = mainMenu.children.find(
(m): m is SubMenu => m.type === "submenu" && m.id === "view"
);
if (viewSubMenu) {
const viewActionsGroup = viewSubMenu.children.find(
(m): m is MenuGroup =>
m.type === "group" && m.id === "view-actions"
);
if (viewActionsGroup) {
viewActionsGroup.children.push({
type: "item",
readonlyOk: true,
id: "toggle-full-screen",
disabled: false,
checked: maximized,
actionItem: {
id: "toggle-full-screen",
label: "action.toggle-full-screen" as TLTranslationKey,
kbd: "!3",
onSelect: () => {
setMaximized(!maximized);
},
readonlyOk: true,
},
});
}
}
const editSubMenu = mainMenu.children.find(
(m): m is SubMenu => m.type === "submenu" && m.id === "edit"
);
if (editSubMenu) {
const conversionsGroup = editSubMenu.children.find(
(m): m is MenuGroup =>
m.type === "group" && m.id === "conversions"
);
if (conversionsGroup) {
const copyAsSubMenu = conversionsGroup.children.find(
(m): m is MenuGroup =>
m.type === "submenu" && m.id === "copy-as"
);
if (copyAsSubMenu) {
const copyAsGroup = copyAsSubMenu.children.find(
(m): m is MenuGroup =>
m.type === "group" && m.id === "copy-as-group"
);
if (copyAsGroup) {
const copyAsPngItem = copyAsGroup.children.find(
(m): m is MenuItem =>
m.type === "item" && m.id === "copy-as-png"
);
if (copyAsPngItem) {
copyAsPngItem.actionItem.kbd = "$!X";
}
}
}
}
}
addFullScreenToggle(mainMenu);
editCopyAsShortcuts(mainMenu);
}
return menu;
},
Expand Down

0 comments on commit f5ec606

Please sign in to comment.