75 lines
2.4 KiB
TypeScript
75 lines
2.4 KiB
TypeScript
import { App, Modal } from "obsidian";
|
|
|
|
export class ThreadModal extends Modal {
|
|
private tweets: string[];
|
|
|
|
constructor(app: App, tweets: string[]) {
|
|
super(app);
|
|
this.tweets = tweets;
|
|
}
|
|
|
|
onOpen(): void {
|
|
const { contentEl } = this;
|
|
this.modalEl.addClass("unicode-thread-modal");
|
|
this.modalEl.style.width = "700px";
|
|
this.modalEl.style.maxWidth = "85vw";
|
|
contentEl.empty();
|
|
|
|
const headingText = this.tweets.length === 1
|
|
? "X post"
|
|
: `X thread (${this.tweets.length} posts)`;
|
|
contentEl.createEl("h3", { text: headingText });
|
|
|
|
const list = contentEl.createDiv({ cls: "thread-tweet-list" });
|
|
|
|
for (let i = 0; i < this.tweets.length; i++) {
|
|
const tweet = this.tweets[i]!;
|
|
const item = list.createDiv({ cls: "thread-tweet-item" });
|
|
|
|
const header = item.createDiv({ cls: "thread-tweet-header" });
|
|
if (this.tweets.length > 1) {
|
|
header.createSpan({
|
|
text: `Post ${i + 1}/${this.tweets.length}`,
|
|
cls: "thread-tweet-label",
|
|
});
|
|
}
|
|
|
|
item.createEl("textarea", {
|
|
text: tweet,
|
|
attr: { readonly: "true" },
|
|
cls: "thread-tweet-textarea",
|
|
});
|
|
|
|
const copyBtn = item.createEl("button", {
|
|
text: "Copy",
|
|
cls: "thread-copy-button",
|
|
});
|
|
copyBtn.addEventListener("click", () => {
|
|
navigator.clipboard.writeText(tweet).then(() => {
|
|
copyBtn.setText("Copied");
|
|
setTimeout(() => copyBtn.setText("Copy"), 2000);
|
|
}).catch(() => {});
|
|
});
|
|
}
|
|
|
|
if (this.tweets.length > 1) {
|
|
const copyAllBtn = contentEl.createEl("button", {
|
|
text: "Copy all",
|
|
cls: "thread-copy-all-button",
|
|
});
|
|
copyAllBtn.addEventListener("click", () => {
|
|
const all = this.tweets.join("\n\n");
|
|
navigator.clipboard.writeText(all).then(() => {
|
|
copyAllBtn.setText("Copied");
|
|
setTimeout(() => copyAllBtn.setText("Copy all"), 2000);
|
|
}).catch(() => {});
|
|
});
|
|
}
|
|
}
|
|
|
|
onClose(): void {
|
|
const { contentEl } = this;
|
|
contentEl.empty();
|
|
}
|
|
}
|