Page MenuHomeSealhub

jdd-table-paste.stimulus.ts
No OneTemporary

jdd-table-paste.stimulus.ts

/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/consistent-type-assertions */
/* eslint-disable no-await-in-loop */
/* eslint-disable @typescript-eslint/no-misused-promises */
import { Controller } from "stimulus";
import TurndownService from "turndown";
import objectPath from "object-path";
const turndownService = new TurndownService();
export default class extends Controller<HTMLButtonElement> {
declare argpathValue: string;
static values = {
argpath: String,
};
connect() {
this.element.addEventListener("click", async () => {
const clipboardContents = await navigator.clipboard.read();
for (const item of clipboardContents) {
if (!item.types.includes("text/html")) {
alert(
"Nie można skopiować tabeli - nie znaleziono danych HTML"
);
return;
}
const blob = await item.getType("text/html");
const html = await blob.text();
const div = document.createElement("div");
div.innerHTML = html;
const table = div.querySelector("table");
if (!table) {
alert("No table in clipboard");
}
const rows = Array.from(
table?.querySelectorAll("tr") || []
).map((tr) => {
const cells = Array.from(tr.querySelectorAll("td"));
return {
type: "row",
cells: cells.map((cell) =>
turndownService.turndown(cell)
),
};
});
const form = this.element.closest("form");
const json_textarea = document.getElementById(
"component-debugger-json-textarea"
) as HTMLTextAreaElement;
if (!json_textarea) {
console.error(
"Could not find json textarea with the jdd state"
);
return;
}
if (!form) {
throw new Error("Not inside a form");
}
const handler = () => {
document.removeEventListener("turbo:morph", handler);
const state = JSON.parse(json_textarea.value) as unknown;
objectPath.set(state as object, this.argpathValue, {
rows,
});
json_textarea.childNodes[0]!.textContent =
JSON.stringify(state);
setTimeout(() => {
// async in order to give a chance for UI state to update after changing the iframe value
const submitter_button =
document.createElement("button");
submitter_button.setAttribute(
"formaction",
"./?action=replace_state&action_args=W10="
);
form.appendChild(submitter_button);
form.requestSubmit(submitter_button);
submitter_button.remove();
}, 0);
};
document.addEventListener("turbo:morph", handler);
form.requestSubmit(); // to get the newest json first, there may be some
// unsent changes
}
});
}
}

File Metadata

Mime Type
text/x-java
Expires
Sat, Sep 20, 14:26 (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
931123
Default Alt Text
jdd-table-paste.stimulus.ts (2 KB)

Event Timeline