obsidian#stringifyYaml TypeScript Examples

The following examples show how to use obsidian#stringifyYaml. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: view.ts    From obsidian-fantasy-calendar with MIT License 4 votes vote down vote up
build() {
        this.contentEl.empty();
        this._app = new CalendarUI({
            target: this.contentEl,
            props: {
                calendar: this.helper,
                fullView: this.full,
                yearView: this.yearView,
                moons: this.moons,
                displayWeeks: this.helper.displayWeeks,
                displayDayNumber: this.dayNumber
            }
        });
        this._app.$on("day-click", (event: CustomEvent<DayHelper>) => {
            const day = event.detail;

            if (day.events.length) return;
            this.createEventForDay(day.date);
        });

        this._app.$on("day-doubleclick", (event: CustomEvent<DayHelper>) => {
            const day = event.detail;
            if (!day.events.length) return;

            this.helper.viewing.day = day.number;
            this.helper.viewing.month = day.month.number;
            this.helper.viewing.year = day.month.year;

            this.yearView = false;

            this._app.$set({ yearView: false });
            this._app.$set({ dayView: true });
            this.triggerHelperEvent("day-update", false);
        });

        this._app.$on(
            "day-context-menu",
            (event: CustomEvent<{ day: DayHelper; evt: MouseEvent }>) => {
                const { day, evt } = event.detail;

                const menu = new Menu(this.app);

                menu.setNoIcon();

                if (!this.full) {
                    menu.addItem((item) => {
                        item.setTitle("Open Day").onClick(() => {
                            this.openDay({
                                day: day.number,
                                month: this.helper.displayed.month,
                                year: this.helper.displayed.year
                            });
                        });
                    });
                }
                menu.addItem((item) => {
                    item.setTitle("Set as Today").onClick(() => {
                        this.calendar.current = day.date;

                        this.helper.current.day = day.number;

                        this.triggerHelperEvent("day-update");

                        this.saveCalendars();
                    });
                });
                menu.addItem((item) =>
                    item.setTitle("New Event").onClick(() => {
                        this.createEventForDay(day.date);
                    })
                );
                menu.showAtMouseEvent(evt);
            }
        );

        this._app.$on("settings", (event: CustomEvent<MouseEvent>) => {
            const evt = event.detail;
            const menu = new Menu(this.app);

            menu.setNoIcon();
            menu.addItem((item) => {
                item.setTitle(
                    `${this.calendar.displayWeeks ? "Hide" : "Show"} Weeks`
                ).onClick(() => {
                    this.calendar.displayWeeks = !this.calendar.displayWeeks;
                    this.helper.update(this.calendar);
                    this._app.$set({
                        displayWeeks: this.calendar.displayWeeks
                    });
                    this.saveCalendars();
                });
            });
            menu.addItem((item) => {
                item.setTitle(
                    `Open ${this.yearView ? "Month" : "Year"}`
                ).onClick(() => {
                    this.yearView = !this.yearView;
                    this._app.$set({ yearView: this.yearView });
                });
            });
            menu.addItem((item) => {
                item.setTitle(
                    this.moons ? "Hide Moons" : "Display Moons"
                ).onClick(() => {
                    this.toggleMoons();
                });
            });
            menu.addItem((item) => {
                item.setTitle(
                    this.dayNumber ? "Hide Day Number" : "Display Day Number"
                ).onClick(() => {
                    this.dayNumber = !this.dayNumber;
                    this.calendar.static.displayDayNumber = this.dayNumber;
                    this._app.$set({ displayDayNumber: this.dayNumber });
                    this.saveCalendars();
                });
            });
            menu.addItem((item) => {
                item.setTitle("View Day");

                item.onClick(() => {
                    this.openDate();
                });
            });
            menu.addItem((item) => {
                item.setTitle("Switch Calendars");
                item.setDisabled(this.plugin.data.calendars.length <= 1);
                item.onClick(() => {
                    const modal = new SwitchModal(this.plugin, this.calendar);

                    modal.onClose = () => {
                        if (!modal.confirmed) return;

                        this.setCurrentCalendar(modal.calendar);
                    };
                    modal.open();
                });
            });

            menu.showAtMouseEvent(evt);
        });

        this._app.$on(
            "event-click",
            (evt: CustomEvent<{ event: Event; modifier: boolean }>) => {
                const { event, modifier } = evt.detail;
                if (event.note) {
                    let leaves: WorkspaceLeaf[] = [];
                    this.app.workspace.iterateAllLeaves((leaf) => {
                        if (!(leaf.view instanceof MarkdownView)) return;
                        if (leaf.view.file.basename === event.note) {
                            leaves.push(leaf);
                        }
                    });
                    if (leaves.length) {
                        this.app.workspace.setActiveLeaf(leaves[0]);
                    } else {
                        this.app.workspace.openLinkText(
                            event.note,
                            "",
                            this.full || modifier
                        );
                    }
                } else {
                    const modal = new ViewEventModal(event, this.plugin);
                    modal.open();
                }
            }
        );

        this._app.$on(
            "event-mouseover",
            (evt: CustomEvent<{ target: HTMLElement; event: Event }>) => {
                if (!this.plugin.data.eventPreview) return;
                const { target, event } = evt.detail;
                if (event.note) {
                    this.app.workspace.trigger(
                        "link-hover",
                        this, //hover popover, but don't need
                        target, //targetEl
                        event.note, //linkText
                        "" //source
                    );
                }
            }
        );

        this._app.$on(
            "event-context",
            (custom: CustomEvent<{ evt: MouseEvent; event: Event }>) => {
                const { evt, event } = custom.detail;

                const menu = new Menu(this.app);

                menu.setNoIcon();

                if (!event.note) {
                    menu.addItem((item) => {
                        item.setTitle("Create Note").onClick(async () => {
                            const path =
                                this.app.workspace.getActiveFile()?.path;
                            const newFilePath = path
                                ? this.app.fileManager.getNewFileParent(path)
                                      ?.parent ?? "/"
                                : "/";

                            const date = `${event.date.year}-${
                                event.date.month + 1
                            }-${event.date.day}`;

                            let end: string;
                            if (event.end) {
                                end = `${event.end.year}-${
                                    event.end.month + 1
                                }-${event.end.day}`;
                            }

                            const content = {
                                "fc-calendar": this.calendar.name,
                                "fc-date": date,
                                ...(event.end ? { "fc-end": end } : {}),
                                ...(event.category
                                    ? {
                                          "fc-category":
                                              this.calendar.categories.find(
                                                  (cat) =>
                                                      cat.id == event.category
                                              )?.name
                                      }
                                    : {}),
                                "fc-display-name": event.name
                            };
                            event.note = normalizePath(
                                `${newFilePath}/${event.name}.md`
                            );

                            let file = this.app.vault.getAbstractFileByPath(
                                event.note
                            );
                            if (!file) {
                                file = await this.app.vault.create(
                                    event.note,
                                    `---\n${stringifyYaml(content)}\n---`
                                );
                            }
                            this.saveCalendars();

                            if (file instanceof TFile) {
                                const fileViews =
                                    this.app.workspace.getLeavesOfType(
                                        "markdown"
                                    );
                                const existing = fileViews.find((l) => {
                                    l.view instanceof FileView &&
                                        l.view.file.path == event.note;
                                });
                                if (existing) {
                                    this.app.workspace.setActiveLeaf(existing);
                                } else {
                                    await this.app.workspace
                                        .getUnpinnedLeaf()
                                        .openFile(file, {
                                            active: true
                                        });
                                }
                            }
                        });
                    });
                }

                menu.addItem((item) => {
                    item.setTitle("Edit Event").onClick(() => {
                        const modal = new CreateEventModal(
                            this.plugin,
                            this.calendar,
                            event
                        );

                        modal.onClose = () => {
                            if (!modal.saved) return;

                            const existing = this.calendar.events.find(
                                (e) => e.id == event.id
                            );

                            this.calendar.events.splice(
                                this.calendar.events.indexOf(existing),
                                1,
                                modal.event
                            );

                            this.helper.refreshMonth(
                                modal.event.date.month,
                                modal.event.date.year
                            );
                            if (
                                modal.event.date.month != existing.date.month ||
                                modal.event.date.year != existing.date.year
                            ) {
                                this.helper.refreshMonth(
                                    existing.date.month,
                                    existing.date.year
                                );
                            }

                            this.saveCalendars();

                            this._app.$set({
                                calendar: this.helper
                            });

                            this.triggerHelperEvent("day-update");
                        };

                        modal.open();
                    });
                });

                menu.addItem((item) => {
                    item.setTitle("Delete Event").onClick(async () => {
                        if (
                            !this.plugin.data.exit.event &&
                            !(await confirmEventDeletion(this.plugin))
                        )
                            return;
                        const existing = this.calendar.events.find(
                            (e) => e.id == event.id
                        );

                        this.calendar.events.splice(
                            this.calendar.events.indexOf(existing),
                            1
                        );

                        this.helper.refreshMonth(
                            existing.date.month,
                            existing.date.year
                        );

                        this.saveCalendars();

                        this._app.$set({
                            calendar: this.helper
                        });

                        this.triggerHelperEvent("day-update");
                    });
                });

                menu.showAtMouseEvent(evt);
            }
        );

        this._app.$on("event", (e: CustomEvent<CurrentCalendarData>) => {
            const date = e.detail;
            this.createEventForDay(date);
        });

        this._app.$on("reset", () => {
            this.helper.reset();

            this.yearView = false;

            this._app.$set({ yearView: false });
            this._app.$set({ dayView: true });
            this.triggerHelperEvent("day-update", false);
        });
    }