Skip to content

Commit

Permalink
electron utils, missing paths modified for electron
Browse files Browse the repository at this point in the history
  • Loading branch information
DNLRQ committed Dec 3, 2024
1 parent dedc41b commit 9256e9b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
20 changes: 1 addition & 19 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,7 @@ import LandingPage from "./pages/LandingPage";
import SettingsContextProvider from "./context/SettingsContext";
import { useSettings } from "./hooks";
import NotFound from "./pages/NotFound";

function isElectron() {
if (
typeof window !== "undefined" &&
typeof window.process === "object" &&
window.process.type === "renderer"
) {
return true;
}
if (
typeof navigator === "object" &&
typeof navigator.userAgent === "string" &&
navigator.userAgent.indexOf("Electron") >= 0
) {
return true;
}

return false;
}
import {isElectron} from "./utils/electronUtils"

const Router = isElectron() ? HashRouter : BrowserRouter;

Expand Down
30 changes: 25 additions & 5 deletions src/components/EditorHeader/ControlPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { jsonToMermaid } from "../../utils/exportAs/mermaid";
import { isRtl } from "../../i18n/utils/rtl";
import { jsonToDocumentation } from "../../utils/exportAs/documentation";
import { IdContext } from "../Workspace";
import { isElectron } from "../../utils/electronUtils";

export default function ControlPanel({
diagramId,
Expand Down Expand Up @@ -1333,21 +1334,40 @@ export default function ControlPanel({
},
help: {
shortcuts: {
function: () => window.open("/shortcuts", "_blank"),
function: () => isElectron()
? (window.location.href = "#/shortcuts")
: window.open("/shortcuts", "_blank"),
shortcut: "Ctrl+H",
},
ask_on_discord: {
function: () => window.open("https://discord.gg/BrjZgNrmR6", "_blank"),
function: () => {
const discordAppUrl = "discord://invite/BrjZgNrmR6";
const fallbackUrl = "https://discord.gg/BrjZgNrmR6";
try {
if (isElectron()) {
window.electron(discordAppUrl);
} else {
window.open(fallbackUrl, "_blank");
}
} catch (error) {
window.open(fallbackUrl, "_blank");
}
},
},
report_bug: {
function: () => window.open("/bug-report", "_blank"),
function: () => isElectron()
? (window.location.href = "#/bug-report")
: window.open("/bug-report", "_blank"),
},
feedback: {
function: () => window.open("/survey", "_blank"),
function: () => isElectron()
? (window.location.href = "#/survey")
: window.open("/survey", "_blank"),
},
},
};


useHotkeys("ctrl+i, meta+i", fileImport, { preventDefault: true });
useHotkeys("ctrl+z, meta+z", undo, { preventDefault: true });
useHotkeys("ctrl+y, meta+y", redo, { preventDefault: true });
Expand Down Expand Up @@ -1387,7 +1407,7 @@ export default function ControlPanel({
style={isRtl(i18n.language) ? { direction: "rtl" } : {}}
>
{header()}
{window.name.split(" ")[0] !== "t" && (
{!isElectron() && window.name.split(" ")[0] !== "t" && (
<Button
type="primary"
className="text-base me-2 pe-6 ps-5 py-[18px] rounded-md"
Expand Down
11 changes: 11 additions & 0 deletions src/pages/Templates.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useLiveQuery } from "dexie-react-hooks";
import Thumbnail from "../components/Thumbnail";
import logo_light from "../assets/logo_light_160.png";
import template_screenshot from "../assets/template_screenshot.png";
import { isElectron } from "../utils/electronUtils";

export default function Templates() {
const defaultTemplates = useLiveQuery(() =>
Expand All @@ -22,13 +23,23 @@ export default function Templates() {
};

const editTemplate = (id) => {
if(isElectron()){
const newWindow = window.open("#/editor", "_blank");
newWindow.name = "t " + id;
}else{
const newWindow = window.open("/editor", "_blank");
newWindow.name = "t " + id;
}
};

const forkTemplate = (id) => {
if(isElectron()){
const newWindow = window.open("#/editor", "_blank");
newWindow.name = "lt " + id;
}else{
const newWindow = window.open("/editor", "_blank");
newWindow.name = "lt " + id;
}
};

useEffect(() => {
Expand Down
20 changes: 20 additions & 0 deletions src/utils/electronUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

export function isElectron() {
if (
typeof window !== "undefined" &&
typeof window.process === "object" &&
window.process.type === "renderer"
) {
return true;
}
if (
typeof navigator === "object" &&
typeof navigator.userAgent === "string" &&
navigator.userAgent.indexOf("Electron") >= 0
) {
return true;
}

return false;
}

0 comments on commit 9256e9b

Please sign in to comment.