Skip to content

Commit

Permalink
Add button to convert to pdf in web editor
Browse files Browse the repository at this point in the history
  • Loading branch information
svenslaggare committed Jan 27, 2025
1 parent a326e18 commit 79cec56
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/web_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub async fn launch(config: WebEditorConfig, input: WebEditorInput) -> EditorOut
.route("/api/add-resource", post(add_resource))
.route("/local/{*path}", get(get_local_file))
.route("/resource/{*path}", get(get_resource_file))
.route("/convert-to-pdf", get(convert_to_pdf))
.with_state(state.clone())
.layer(DefaultBodyLimit::max(10 * 1024 * 1024))
;
Expand Down Expand Up @@ -255,7 +256,10 @@ struct SaveContent {
content: String
}

async fn save_content(State(state): State<Arc<WebServerState>>, Json(input): Json<SaveContent>) -> WebServerResult<Response> {
async fn save_content(
State(state): State<Arc<WebServerState>>,
Json(input): Json<SaveContent>
) -> WebServerResult<Response> {
if state.access_mode == AccessMode::ReadWrite {
std::fs::write(&input.path, input.content)?;
println!("Saved content for '{}'.", input.path.to_str().unwrap());
Expand All @@ -275,7 +279,10 @@ struct RunSnippet {
content: String
}

async fn run_snippet(State(state): State<Arc<WebServerState>>, Json(input): Json<RunSnippet>) -> WebServerResult<Response> {
async fn run_snippet(
State(state): State<Arc<WebServerState>>,
Json(input): Json<RunSnippet>
) -> WebServerResult<Response> {
let arena = markdown::storage();

let mut snippet_output = String::new();
Expand Down Expand Up @@ -348,6 +355,18 @@ async fn get_resource_file(
}
}

async fn convert_to_pdf(headers: HeaderMap, Query(parameters): Query<HashMap<String, String>>) -> WebServerResult<Response> {
let path = parameters.get("path").ok_or_else(|| WebServerError::ExpectedQueryParameter("path".to_owned()))?;

let output_path = tempfile::Builder::new()
.suffix(".pdf")
.tempfile()?
.path().to_path_buf();

markdown::convert(Path::new(&path), &output_path).unwrap();
Ok(serve_file(headers, &output_path).await)
}

async fn serve_file(headers: HeaderMap, path: &Path) -> Response {
let mut request = Request::new(Body::empty());
*request.headers_mut() = headers;
Expand Down
Binary file added test.pdf
Binary file not shown.
7 changes: 6 additions & 1 deletion webeditor/scripts/webeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ var WebEditorMain = /** @class */ (function (_super) {
react_1.default.createElement("button", { type: "button", className: "btn btn-primary", onClick: function () { _this.runSnippet(); } }, "Run snippet"),
!this.state.isReadOnly ?
react_1.default.createElement("button", { type: "button", className: "btn btn-primary", onClick: function () { _this.showAddResourceModel(); } }, "Add resource")
: null));
: null,
react_1.default.createElement("button", { type: "button", className: "btn btn-primary", onClick: function () { _this.convertToPDF(); } }, "Convert to PDF")));
};
WebEditorMain.prototype.renderText = function () {
var _this = this;
Expand Down Expand Up @@ -353,6 +354,10 @@ var WebEditorMain = /** @class */ (function (_super) {
react_1.default.createElement("br", null),
react_1.default.createElement("button", { type: "button", className: "btn btn-primary", onClick: function () { _this.addResource(); } }, "Upload"))))));
};
WebEditorMain.prototype.convertToPDF = function () {
var url = "/convert-to-pdf?path=".concat(this.props.filePath);
window.open(url, "_blank");
};
WebEditorMain.prototype.showAddLinkModel = function () {
// @ts-ignore
this.addLinkModal = new bootstrap.Modal(document.getElementById("addLinkModal"));
Expand Down
6 changes: 6 additions & 0 deletions webeditor/scripts/webeditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class WebEditorMain extends React.Component<WebEditorMainProps, WebEditorMainSta
<button type="button" className="btn btn-primary" onClick={() => { this.showAddResourceModel(); }}>Add resource</button>
: null
}
<button type="button" className="btn btn-primary" onClick={() => { this.convertToPDF(); }}>Convert to PDF</button>
</span>
);
}
Expand Down Expand Up @@ -472,6 +473,11 @@ class WebEditorMain extends React.Component<WebEditorMainProps, WebEditorMainSta
);
}

convertToPDF() {
let url = `/convert-to-pdf?path=${this.props.filePath}`
window.open(url, "_blank");
}

showAddLinkModel() {
// @ts-ignore
this.addLinkModal = new bootstrap.Modal(document.getElementById("addLinkModal"));
Expand Down

0 comments on commit 79cec56

Please sign in to comment.