mop/.obsidian/plugins/obsidian-dialogue-plugin/main.js

341 lines
13 KiB
JavaScript

/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, desc) => {
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && key !== "default")
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
}
return target;
};
var __toModule = (module2) => {
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/main.ts
__export(exports, {
default: () => DialoguePlugin
});
var import_obsidian2 = __toModule(require("obsidian"));
// src/types/dialogueTitleMode.ts
var DialogueTitleMode;
(function(DialogueTitleMode2) {
DialogueTitleMode2["Disabled"] = "disabled";
DialogueTitleMode2["First"] = "first";
DialogueTitleMode2["All"] = "all";
})(DialogueTitleMode || (DialogueTitleMode = {}));
// src/constants/classes.ts
var CLASSES = class {
};
CLASSES.DIALOGUE_WRAPPER = "dialogue-plugin-wrapper";
CLASSES.BLOCK_WRAPPER = "dialogue-plugin-block-wrapper";
CLASSES.MESSAGE_WRAPPER_LEFT = "dialogue-plugin-message-wrapper-left";
CLASSES.MESSAGE_WRAPPER_RIGHT = "dialogue-plugin-message-wrapper-right";
CLASSES.MESSAGE = "dialogue-plugin-message";
CLASSES.MESSAGE_TITLE = "dialogue-plugin-message-title";
CLASSES.MESSAGE_CONTENT = "dialogue-plugin-message-content";
CLASSES.DELIMITER_WRAPPER = "dialogue-plugin-delimiter-wrapper";
CLASSES.DELIMITER = "dialogue-plugin-delimiter";
CLASSES.DELIMITER_DOT = "dialogue-plugin-delimiter-dot";
CLASSES.COMMENT_WRAPPER = "dialogue-plugin-comment-wrapper";
CLASSES.COMMENT = "dialogue-plugin-comment";
// src/components/message.ts
var SIDES = class {
};
SIDES.LEFT = "left";
SIDES.RIGHT = "right";
var Message = class {
constructor(content, side, dialogueSettings) {
this.content = content;
this.side = side;
this.dialogueSettings = dialogueSettings;
this.participant = this.side == SIDES.LEFT ? this.dialogueSettings.leftParticipant : this.dialogueSettings.rightParticipant;
this.renderMessage();
}
renderMessage() {
const messageEl = this.createMessageEl();
if (this.titleShouldRender()) {
messageEl.createDiv({ cls: CLASSES.MESSAGE_TITLE, text: this.participant.title });
}
messageEl.createDiv({ cls: CLASSES.MESSAGE_CONTENT, text: this.content });
}
createMessageEl() {
var _a;
const sideClass = this.side == SIDES.LEFT ? CLASSES.MESSAGE_WRAPPER_LEFT : CLASSES.MESSAGE_WRAPPER_RIGHT;
const messageWrapperEl = this.dialogueSettings.parent.createDiv({
cls: `${CLASSES.BLOCK_WRAPPER} ${sideClass}`
});
return messageWrapperEl.createDiv({
cls: CLASSES.MESSAGE,
attr: {
style: `max-width: ${this.dialogueSettings.messageMaxWidth};`,
"data-participant-name": this.participant.title,
"data-participant-id": (_a = this.participant.enforcedId) != null ? _a : this.dialogueSettings.participants.get(this.participant.title)
}
});
}
titleShouldRender() {
if (this.participant.title.length < 1)
return false;
switch (this.dialogueSettings.titleMode) {
case DialogueTitleMode.Disabled:
return false;
case DialogueTitleMode.All:
return true;
case DialogueTitleMode.First: {
if (this.participant.renderedOnce)
return false;
this.participant.renderedOnce = true;
return true;
}
default:
return false;
}
}
};
// src/components/delimiter.ts
var Delimiter = class {
constructor(dialogueSettings) {
this.dialogueSettings = dialogueSettings;
this.renderDelimiter();
}
renderDelimiter() {
const delimiterWrapperEl = this.dialogueSettings.parent.createDiv({
cls: `${CLASSES.BLOCK_WRAPPER} ${CLASSES.DELIMITER_WRAPPER}`
});
const delimiterEl = delimiterWrapperEl.createDiv({ cls: CLASSES.DELIMITER });
delimiterEl.createEl("div", { cls: CLASSES.DELIMITER_DOT });
delimiterEl.createEl("div", { cls: CLASSES.DELIMITER_DOT });
delimiterEl.createEl("div", { cls: CLASSES.DELIMITER_DOT });
}
};
// src/components/comment.ts
var Comment = class {
constructor(content, dialogueSettings) {
this.content = content;
this.dialogueSettings = dialogueSettings;
this.renderComment();
}
renderComment() {
const commentEl = this.dialogueSettings.parent.createDiv({
cls: `${CLASSES.BLOCK_WRAPPER} ${CLASSES.COMMENT_WRAPPER}`
});
return commentEl.createDiv({
cls: CLASSES.COMMENT,
text: this.content,
attr: {
style: `max-width: ${this.dialogueSettings.commentMaxWidth};`
}
});
}
};
// src/dialogue.ts
var KEYWORDS = class {
};
KEYWORDS.LEFT_PATTERN = /^l(?:eft)?(?:-(\d+))?:/i;
KEYWORDS.RIGHT_PATTERN = /^r(?:ight)?(?:-(\d+))?:/i;
KEYWORDS.TITLE_MODE = "titleMode:";
KEYWORDS.MESSAGE_MAX_WIDTH = "messageMaxWidth:";
KEYWORDS.COMMENT_MAX_WIDTH = "commentMaxWidth:";
KEYWORDS.DELIMITER = /^-|delimiter/;
KEYWORDS.COMMENT = "#";
KEYWORDS.MESSAGE_LEFT = "<";
KEYWORDS.MESSAGE_RIGHT = ">";
var DialogueRenderer = class {
constructor(src, parent, settings) {
this.src = src;
this.dialogueWrapperEl = parent.createDiv({ cls: CLASSES.DIALOGUE_WRAPPER });
this.dialogueSettings = {
parent: this.dialogueWrapperEl,
leftParticipant: {
title: settings.defaultLeftTitle,
renderedOnce: false,
enforcedId: null
},
rightParticipant: {
title: settings.defaultRightTitle,
renderedOnce: false,
enforcedId: null
},
titleMode: settings.defaultTitleMode,
messageMaxWidth: settings.defaultMessageMaxWidth,
commentMaxWidth: settings.defaultCommentMaxWidth,
participants: new Map()
};
this.renderDialogue();
}
registerParticipant(participant) {
if (!this.dialogueSettings.participants.has(participant)) {
this.dialogueSettings.participants.set(participant, this.dialogueSettings.participants.size + 1);
}
}
getEnforcedId(pattern, line) {
let enforcedId = null;
const result = pattern.exec(line);
if (result != null && result.length > 1) {
enforcedId = result[1];
}
return enforcedId;
}
renderDialogue() {
const lines = this.src.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
for (const line of lines) {
if (KEYWORDS.LEFT_PATTERN.test(line)) {
this.dialogueSettings.leftParticipant.title = line.split(":").splice(1).join(":").trim();
this.dialogueSettings.leftParticipant.renderedOnce = false;
this.dialogueSettings.leftParticipant.enforcedId = this.getEnforcedId(KEYWORDS.LEFT_PATTERN, line);
} else if (KEYWORDS.RIGHT_PATTERN.test(line)) {
this.dialogueSettings.rightParticipant.title = line.split(":").splice(1).join(":").trim();
this.dialogueSettings.rightParticipant.renderedOnce = false;
this.dialogueSettings.rightParticipant.enforcedId = this.getEnforcedId(KEYWORDS.RIGHT_PATTERN, line);
} else if (line.startsWith(KEYWORDS.TITLE_MODE)) {
const modeName = line.substr(KEYWORDS.TITLE_MODE.length).trim().toLowerCase();
if (Object.values(DialogueTitleMode).some((mode) => mode == modeName)) {
this.dialogueSettings.titleMode = modeName;
}
} else if (line.startsWith(KEYWORDS.MESSAGE_MAX_WIDTH)) {
this.dialogueSettings.messageMaxWidth = line.substr(KEYWORDS.MESSAGE_MAX_WIDTH.length).trim();
} else if (line.startsWith(KEYWORDS.COMMENT_MAX_WIDTH)) {
this.dialogueSettings.commentMaxWidth = line.substr(KEYWORDS.COMMENT_MAX_WIDTH.length).trim();
} else if (KEYWORDS.DELIMITER.test(line)) {
new Delimiter(this.dialogueSettings);
} else if (line.startsWith(KEYWORDS.COMMENT)) {
const content = line.substr(KEYWORDS.COMMENT.length);
new Comment(content, this.dialogueSettings);
} else if (line.startsWith(KEYWORDS.MESSAGE_LEFT)) {
const content = line.substr(KEYWORDS.MESSAGE_LEFT.length);
this.registerParticipant(this.dialogueSettings.leftParticipant.title);
new Message(content, SIDES.LEFT, this.dialogueSettings);
} else if (line.startsWith(KEYWORDS.MESSAGE_RIGHT)) {
const content = line.substr(KEYWORDS.MESSAGE_RIGHT.length);
this.registerParticipant(this.dialogueSettings.rightParticipant.title);
new Message(content, SIDES.RIGHT, this.dialogueSettings);
}
}
}
};
// src/settings.ts
var import_obsidian = __toModule(require("obsidian"));
var DEFAULT_SETTINGS = {
defaultLeftTitle: "",
defaultRightTitle: "",
defaultTitleMode: DialogueTitleMode.First,
defaultMessageMaxWidth: "60%",
defaultCommentMaxWidth: "60%"
};
var DialogueSettingTab = class extends import_obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Dialogue Settings" });
const coffeeEl = containerEl.createEl("div", {
attr: {
style: "text-align: center; margin-bottom: 10px;"
}
});
const coffeeLinkEl = coffeeEl.createEl("a", { href: "https://www.buymeacoffee.com/holubj" });
coffeeLinkEl.createEl("img", {
attr: {
src: "https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png",
alt: "Buy Me A Coffee",
style: "height: 60px; width: 217px;"
}
});
new import_obsidian.Setting(containerEl).setName("Default left title").setDesc("Default value for left title in all dialogues.").addText((text) => text.setPlaceholder("Enter default left title").setValue(this.plugin.settings.defaultLeftTitle).onChange((value) => __async(this, null, function* () {
this.plugin.settings.defaultLeftTitle = value;
yield this.plugin.saveSettings();
})));
new import_obsidian.Setting(containerEl).setName("Default right title").setDesc("Default value for right title in all dialogues.").addText((text) => text.setPlaceholder("Enter default right title").setValue(this.plugin.settings.defaultRightTitle).onChange((value) => __async(this, null, function* () {
this.plugin.settings.defaultRightTitle = value;
yield this.plugin.saveSettings();
})));
new import_obsidian.Setting(containerEl).setName("Default title mode").setDesc("Default title mode in all dialogues.").addDropdown((cb) => {
Object.values(DialogueTitleMode).forEach((titleMode) => {
const mode = titleMode.toString();
cb.addOption(mode, mode.charAt(0).toUpperCase() + mode.slice(1));
});
cb.setValue(this.plugin.settings.defaultTitleMode).onChange((value) => __async(this, null, function* () {
this.plugin.settings.defaultTitleMode = value;
yield this.plugin.saveSettings();
}));
});
new import_obsidian.Setting(containerEl).setName("Default max message width").setDesc("Default max message width in all dialogues.").addText((text) => text.setPlaceholder("Enter default max message width").setValue(this.plugin.settings.defaultMessageMaxWidth).onChange((value) => __async(this, null, function* () {
this.plugin.settings.defaultMessageMaxWidth = value;
yield this.plugin.saveSettings();
})));
new import_obsidian.Setting(containerEl).setName("Default max comment width").setDesc("Default max comment width in all dialogues.").addText((text) => text.setPlaceholder("Enter default max comment width").setValue(this.plugin.settings.defaultCommentMaxWidth).onChange((value) => __async(this, null, function* () {
this.plugin.settings.defaultCommentMaxWidth = value;
yield this.plugin.saveSettings();
})));
}
};
// src/main.ts
var DialoguePlugin = class extends import_obsidian2.Plugin {
onload() {
return __async(this, null, function* () {
yield this.loadSettings();
this.registerMarkdownCodeBlockProcessor(`dialogue`, (src, el, ctx) => {
new DialogueRenderer(src, el, this.settings);
});
this.addSettingTab(new DialogueSettingTab(this.app, this));
});
}
loadSettings() {
return __async(this, null, function* () {
this.settings = Object.assign({}, DEFAULT_SETTINGS, yield this.loadData());
});
}
saveSettings() {
return __async(this, null, function* () {
yield this.saveData(this.settings);
});
}
};