Skip to content

Commit

Permalink
Don't show 'run snippet' in standalone mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
svenslaggare committed Dec 14, 2023
1 parent 68bf61d commit 38c0e15
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn run(input_command: InputCommand, main_input_command: MainInputCommand) -> Res
let mut config = WebEditorConfig::default();
config.port = port;
config.is_read_only = is_read_only;
config.is_standalone = true;
web_editor::launch_sync(config, &path);
Ok(())
}
Expand Down
12 changes: 10 additions & 2 deletions src/web_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct WebEditorConfig {
pub port: u16,
pub launch_web_view: bool,
pub is_read_only: bool,
pub is_standalone: bool,
pub repository_path: Option<PathBuf>,
pub snippet_config: Option<SnippetFileConfig>
}
Expand All @@ -44,6 +45,7 @@ impl Default for WebEditorConfig {
port: 9000,
launch_web_view: default_launch_web_view(),
is_read_only: false,
is_standalone: false,
repository_path: None,
snippet_config: None
}
Expand All @@ -69,6 +71,7 @@ pub async fn launch(config: WebEditorConfig, path: &Path) {
let state = Arc::new(WebServerState::new(
path.to_owned(),
config.is_read_only,
config.is_standalone,
config.repository_path.clone(),
SnippetRunnerManger::from_config(config.snippet_config.as_ref()).unwrap()
));
Expand Down Expand Up @@ -143,19 +146,22 @@ struct WebServerState {
path: PathBuf,
notify: Notify,
is_read_only: bool,
is_standalone: bool,
repository_path: Option<PathBuf>,
snippet_runner_manager: SnippetRunnerManger
}

impl WebServerState {
pub fn new(path: PathBuf,
is_read_only: bool,
is_standalone: bool,
repository_path: Option<PathBuf>,
snippet_runner_manager: SnippetRunnerManger) -> WebServerState {
WebServerState {
path,
notify: Notify::new(),
is_read_only,
is_standalone,
repository_path,
snippet_runner_manager
}
Expand Down Expand Up @@ -198,14 +204,16 @@ impl IntoResponse for WebServerError {
struct AppTemplate {
time: i64,
file_path: String,
is_read_only: bool
is_read_only: bool,
is_standalone: bool
}

async fn index(State(state): State<Arc<WebServerState>>) -> Response {
let template = AppTemplate {
time: Local::now().timestamp(),
file_path: state.path.to_str().unwrap().to_owned(),
is_read_only: state.is_read_only
is_read_only: state.is_read_only,
is_standalone: state.is_standalone
};

Html(template.render().unwrap()).into_response()
Expand Down
1 change: 1 addition & 0 deletions templates/webEditor.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<input type="hidden" id="file_path" value="{{ file_path }}" />
<input type="hidden" id="is_read_only" value="{{ is_read_only }}" />
<input type="hidden" id="is_standalone" value="{{ is_standalone }}" />
<script src="/content/webeditor.js?time={{ time }}"></script>
</body>
</html>
5 changes: 3 additions & 2 deletions webeditor/scripts/webeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ var WebEditorMain = /** @class */ (function (_super) {
_this.state = {
content: "",
isReadOnly: _this.props.isReadOnly,
isStandalone: _this.props.isStandalone,
showCode: !_this.props.isReadOnly,
showRendered: true,
success: null,
Expand All @@ -79,7 +80,7 @@ var WebEditorMain = /** @class */ (function (_super) {
react_1.default.createElement("div", { className: "row", style: { "padding": "7px" } },
react_1.default.createElement("div", { className: "col-9" },
!this.state.isReadOnly ? react_1.default.createElement("button", { type: "button", className: "btn btn-success", onClick: function () { _this.saveContent(); } }, "Save") : null,
react_1.default.createElement("button", { type: "button", className: "btn btn-primary", onClick: function () { _this.runSnippet(); } }, "Run snippet"),
!this.state.isStandalone ? react_1.default.createElement("button", { type: "button", className: "btn btn-primary", onClick: function () { _this.runSnippet(); } }, "Run snippet") : null,
!this.state.isReadOnly ? react_1.default.createElement("button", { type: "button", className: "btn btn-primary", onClick: function () { _this.saveContentAndExit(); } }, "Save & exit") : null,
react_1.default.createElement("button", { type: "button", className: "btn btn-danger", onClick: function () { _this.exit(); } }, "Exit")),
react_1.default.createElement("div", { className: "col-3" },
Expand Down Expand Up @@ -291,4 +292,4 @@ function sendMessageToServer(cmd) {
}
throw new Error('Failed to locate webkit external handler');
}
react_dom_1.default.render(react_1.default.createElement(WebEditorMain, { filePath: document.getElementById("file_path").value, isReadOnly: document.getElementById("is_read_only").value == "true" }), document.getElementById("root"));
react_dom_1.default.render(react_1.default.createElement(WebEditorMain, { filePath: document.getElementById("file_path").value, isReadOnly: document.getElementById("is_read_only").value == "true", isStandalone: document.getElementById("is_standalone").value == "true" }), document.getElementById("root"));
7 changes: 5 additions & 2 deletions webeditor/scripts/webeditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import axios from "axios";
class WebEditorMainProps {
filePath: string;
isReadOnly: boolean;
isStandalone: boolean;
}

interface WebEditorMainState {
content: string;
isReadOnly: boolean;
isStandalone: boolean;
showCode: boolean;
showRendered: boolean;

Expand All @@ -38,6 +40,7 @@ class WebEditorMain extends React.Component<WebEditorMainProps, WebEditorMainSta
this.state = {
content: "",
isReadOnly: this.props.isReadOnly,
isStandalone: this.props.isStandalone,
showCode: !this.props.isReadOnly,
showRendered: true,
success: null,
Expand All @@ -57,7 +60,7 @@ class WebEditorMain extends React.Component<WebEditorMainProps, WebEditorMainSta
<div className="row" style={{ "padding": "7px" }}>
<div className="col-9">
{ !this.state.isReadOnly ? <button type="button" className="btn btn-success" onClick={() => { this.saveContent(); }}>Save</button> : null }
<button type="button" className="btn btn-primary" onClick={() => { this.runSnippet(); }}>Run snippet</button>
{ !this.state.isStandalone ? <button type="button" className="btn btn-primary" onClick={() => { this.runSnippet(); }}>Run snippet</button> : null }
{ !this.state.isReadOnly ? <button type="button" className="btn btn-primary" onClick={() => { this.saveContentAndExit(); }}>Save & exit</button> : null }
<button type="button" className="btn btn-danger" onClick={() => { this.exit(); }}>Exit</button>
</div>
Expand All @@ -73,7 +76,6 @@ class WebEditorMain extends React.Component<WebEditorMainProps, WebEditorMainSta
onChange={event => { this.changeRendered(event); }} />
<label className="form-check-label" htmlFor="showRenderedheckbox">Rendered</label>
</div>

</div>
</div>
{this.renderSuccess()}
Expand Down Expand Up @@ -352,6 +354,7 @@ ReactDOM.render(
<WebEditorMain
filePath={(document.getElementById("file_path") as HTMLInputElement).value}
isReadOnly={(document.getElementById("is_read_only") as HTMLInputElement).value == "true"}
isStandalone={(document.getElementById("is_standalone") as HTMLInputElement).value == "true"}
/>,
document.getElementById("root")
);

0 comments on commit 38c0e15

Please sign in to comment.