File size: 2,434 Bytes
6fecfbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { $el } from "../../scripts/ui.js";

export class ComfirmDialog {
    constructor() {
        this.element = $el("div.comfy-modal", { parent: document.body }, [
            $el("div.comfy-modal-content", [$el("p", { $: (p) => (this.textElement = p) }), this.createButtons()]),
        ]);
        this.element.style.color = "var(--input-text)";
    }

    createButtons() {
        this.yesButton = $el("button", {
            type: "button",
            textContent: "Yes",
            onclick: () => this.close(),
        });
        this.noButton = $el("button", {
            type: "button",
            textContent: "No",
            onclick: () => this.close(),
        });
        return $el("div", [this.yesButton, this.noButton]);
    }

    close() {
        this.element.style.display = "none";
    }

    show(html, onYes, onNo) {
        if (typeof html === "string") {
            this.textElement.innerHTML = html;
        } else {
            this.textElement.replaceChildren(html);
        }
        this.element.style.display = "flex";

        this.element.querySelector("button:nth-child(1)").onclick = () => {
            this.close();
            onYes();
        }
        this.element.querySelector("button:nth-child(2)").onclick = () => {
            this.close();
            onNo();
        }
        this.yesButton.focus();
    }
}

export function showWidgetDialog(pos, prompt, onEnter, onCancel) {
    if (!onEnter) {
        onEnter = () => { };
    }
    if (!onCancel) {
        onCancel = () => { };
    }
    const htmlElement = `<span class='name'>${prompt}</span><input autofocus type='text' class='value'/><button>OK</button>`
    let dialog = app.canvas.createDialog(
        htmlElement,
        { position: pos }
    );
    let input = dialog.querySelector("input");
    input.focus();
    const cancel = () => {
        onCancel();
        dialog.close();
    };
    const enter = () => {
        onEnter(input);
        dialog.close();
    };
    input.addEventListener("keydown", function (e) {
        if (e.keyCode == 27) { // ESC
            cancel();
        } else if (e.keyCode == 13) { // ENTER
            enter();
        } else if (e.keyCode != 13) {
            dialog.modified();
            return;
        }
        e.preventDefault();
        e.stopPropagation();
    });
    let button = dialog.querySelector("button");
    button.addEventListener("click", enter);
}