Skip to content

Commit

Permalink
webapp: get rid of innerHTML, use DOM API instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyagr committed Mar 14, 2024
1 parent 820bee9 commit 53b13af
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 42 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion webapp/dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Diffedit3</title>
<script type="module" crossorigin src="/assets/index-D3Bm5jCz.js"></script>
<script type="module" crossorigin src="/assets/index-Ca1uhQEV.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-lMSNPRYN.css">
</head>

Expand Down
6 changes: 2 additions & 4 deletions webapp/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
exit_user_abandoned_merge,
} from "./backend_interactions";
import { MergeState } from "./merge_state";
import { unreachable } from "./utils";
import { replaceElementByIdWithNewEmptyDiv, unreachable } from "./utils";

// Error handling
function show_error_to_user(e: any) {
Expand Down Expand Up @@ -38,9 +38,7 @@ async function run_and_show_any_errors_to_user<T>(f: {

import { listen } from "@tauri-apps/api/event";
window.addEventListener("DOMContentLoaded", async () => {
const loading_elt = document.getElementById("loading_message")!;
// TODO: Try the until directive?
loading_elt.innerHTML = "";
const loading_elt = replaceElementByIdWithNewEmptyDiv("loading_message")!;
lit_html_render(
html`
<h2>Loading...</h2>
Expand Down
16 changes: 4 additions & 12 deletions webapp/src/merge_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SingleFileMergeInput,
to_text,
} from "./backend_interactions";
import { unreachable } from "./utils";
import { replaceElementByIdWithNewEmptyDiv, unreachable } from "./utils";

export class MergeState {
protected merge_views: Record<string, MergeView>;
Expand Down Expand Up @@ -111,8 +111,7 @@ export class MergeState {
}
}

const target_element = document.getElementById(unique_id)!;
target_element.innerHTML = ""; // TODO: Should use replaceWith or something
let target_element = replaceElementByIdWithNewEmptyDiv(unique_id)!;
lit_html_render(html`${templates}`, target_element);

const merge_state = new MergeState();
Expand Down Expand Up @@ -245,16 +244,9 @@ export class MergeState {
return;
}
let dom_id = this.dom_ids[filename];
const codemirror_dom_id = `cm_${dom_id}`;


const current_state = this.getSingleMergeState(filename);

const new_codemirror_element = document.createElement("div");
document
.getElementById(codemirror_dom_id)
?.replaceWith(new_codemirror_element);
new_codemirror_element.id = codemirror_dom_id;

replaceElementByIdWithNewEmptyDiv(`cm_${dom_id}`);
this.createCodeMirrorMergeWidget(
dom_id,
filename,
Expand Down
11 changes: 11 additions & 0 deletions webapp/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ export function unreachable(): never {
throw new Error(
"this statement is unreachable; this check exists to make TS happy"
);
}

export function replaceElementByIdWithNewEmptyDiv(id: string) {
const old_element = document.getElementById(id);
if (old_element == null) {
return;
}
const new_element = document.createElement("div");
new_element.id = id;
old_element.replaceWith(new_element);
return new_element;
}

0 comments on commit 53b13af

Please sign in to comment.