Skip to content

Commit 68a31ce

Browse files
committed
add open with external editor logic
1 parent c885ca9 commit 68a31ce

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

api/server.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,21 @@ def create_sequence_file():
403403

404404
return {"ok": ok, "err": err}
405405

406-
406+
@app.route('/open_sequence_file', methods=['POST'])
407+
def open_sequence_file():
408+
body = request.get_json()
409+
sequence_name = body['sequence']
410+
sequence_file = sequences_dir + sequence_name
411+
try:
412+
command = body['editorBinary'].split(" ")
413+
command.append(sequence_file)
414+
subprocess.Popen(command) # run the command asyncronously
415+
ok = True
416+
err = "no error"
417+
except:
418+
err = traceback.format_exc()
419+
ok = False
420+
return {"ok": ok, "err": err}
407421

408422
@socketio.on("disconnect")
409423
def disconnected():

src/components/ToolBar.js

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
import { useState, useEffect } from "react";
1+
import { useEffect, useState } from "react";
22
import {
3+
Button,
34
Container,
5+
Form,
6+
Modal,
47
Nav,
5-
Navbar,
68
NavDropdown,
7-
Modal,
8-
Button,
9+
Navbar,
910
Tab,
10-
Tabs,
1111
Table,
12-
Form
12+
Tabs
1313
} from "react-bootstrap";
1414
import { DropdownSubmenu, NavDropdownMenu } from "react-bootstrap-submenu";
15-
import { FaToggleOn, FaToggleOff, FaInfoCircle } from "react-icons/fa";
16-
import { FcRadarPlot, FcScatterPlot, FcHeatMap } from "react-icons/fc";
1715
import "react-bootstrap-submenu/dist/index.css";
18-
import logo from "../static/img/logo.png";
19-
import { generateUUID } from "three/src/math/MathUtils";
20-
import { PORT } from "../static/js/constants";
16+
import { FaInfoCircle, FaToggleOff, FaToggleOn } from "react-icons/fa";
17+
import { FcHeatMap, FcRadarPlot, FcScatterPlot } from "react-icons/fc";
18+
import { LuEdit, LuPlay } from "react-icons/lu";
2119
import { ToastContainer, toast } from "react-toastify";
2220
import "react-toastify/dist/ReactToastify.css";
21+
import { generateUUID } from "three/src/math/MathUtils";
22+
import logo from "../static/img/logo.png";
23+
import { PORT } from "../static/js/constants";
24+
import { defaultSettings } from "../views/Settings";
2325

2426
function ToolBar({
2527
page,
@@ -287,9 +289,19 @@ function ToolBar({
287289
rows.push(
288290
<NavDropdown.Item
289291
key={generateUUID()}
290-
onClick={() => runSequence(seq_name)}
292+
style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}
291293
>
292-
{seq_name}
294+
<div>{seq_name}</div>
295+
&nbsp;
296+
<div className="form-group">
297+
<Button variant="outline-success"
298+
onClick={() => runSequence(seq_name)}
299+
><LuPlay /></Button>
300+
&nbsp;
301+
<Button variant="outline-outline"
302+
onClick={() => openFileWithEditor(seq_name)}
303+
><LuEdit /></Button>
304+
</div>
293305
</NavDropdown.Item>
294306
);
295307
});
@@ -316,13 +328,32 @@ function ToolBar({
316328
}
317329
).then((res) => res.json());
318330

319-
if (response.ok) toast.success(`"${name}" created successfully`);
320-
else toast.error(response.err);
331+
if (response.ok) {
332+
toast.success(`"${name}" created successfully`);
333+
openFileWithEditor(`${name}.py`);
334+
}
335+
else {
336+
toast.error(response.err);
337+
}
321338

322339
getSequences();
323340
setShowCreateMessage(false);
324341
};
325342

343+
const openFileWithEditor = async (sequenceName) => {
344+
const general_settings = parseLocalStorage("general_settings");
345+
const externalEditor =
346+
general_settings.externalEditor ?? defaultSettings["externalEditor"];
347+
const response = await fetch(`http://localhost:${PORT}/open_sequence_file`, {
348+
headers: {
349+
"Content-Type": "application/json",
350+
},
351+
method: "POST",
352+
body: JSON.stringify({ sequence: sequenceName, editorBinary: externalEditor }),
353+
}).then((res) => res.json());
354+
if (!response.ok) toast.error(response.err);
355+
}
356+
326357
const paramSearch = (query, dict) => {
327358
const value = query.target.value;
328359
setSearchQuery(value);

0 commit comments

Comments
 (0)