/*
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: () => HeadingIndent
});
var import_obsidian2 = __toModule(require("obsidian"));

// src/preview_indenting.ts
var containerSelector = ".workspace-leaf.mod-active .markdown-reading-view .markdown-preview-section";
var arrClassesHeadings = {
  1: "heading_h1",
  2: "heading_h2",
  3: "heading_h3",
  4: "heading_h4",
  5: "heading_h5",
  6: "heading_h6"
};
var arrClassesData = {
  0: "data_no-heading",
  1: "data_h1",
  2: "data_h2",
  3: "data_h3",
  4: "data_h4",
  5: "data_h5",
  6: "data_h6"
};
function setObserverToActiveLeaf(plugin) {
  if (plugin.previewObserver !== void 0) {
    plugin.previewObserver.disconnect();
  }
  const targetNode = activeDocument.querySelector(containerSelector);
  if (targetNode == null) {
    console.log("target node is NULL");
    return;
  }
  const config = { childList: true };
  const callback = (mutationList, observer) => {
    for (const mutation of mutationList) {
      if (mutation.type === "childList") {
        wrapperIndentPreview(plugin, 100, true);
      }
    }
  };
  plugin.previewObserver = new MutationObserver(callback);
  plugin.previewObserver.observe(targetNode, config);
}
function wrapperIndentPreview(plugin, timeout, flag) {
  timeout = timeout || 100;
  if (flag) {
    if (plugin.flagExecute == void 0)
      plugin.flagExecute = 1;
    if (plugin.flagExecute == 1) {
      plugin.flagExecute = 2;
      setTimeout(function() {
        return __async(this, null, function* () {
          indentPreview(plugin);
        });
      }, timeout);
      setTimeout(() => {
        plugin.flagExecute = 1;
      }, timeout + 50);
    }
  } else {
    setTimeout(function() {
      return __async(this, null, function* () {
        indentPreview(plugin);
      });
    }, timeout);
  }
}
function indentPreview(plugin) {
  const settings = plugin.settings;
  const divsNodeList = activeDocument.querySelectorAll(containerSelector + " > div");
  if (!divsNodeList) {
    return;
  }
  const arrDivs = Array.from(divsNodeList);
  const excludedClassNames = ["mod-header", "mod-footer", "markdown-preview-pusher"];
  cleanSectionModifications(arrDivs);
  const arrMargins = {
    0: 0,
    1: parseInt(settings.h1) || 0,
    2: parseInt(settings.h2) || 0,
    3: parseInt(settings.h3) || 0,
    4: parseInt(settings.h4) || 0,
    5: parseInt(settings.h5) || 0,
    6: parseInt(settings.h6) || 0
  };
  let hNumber = 0;
  suck:
    for (const div of arrDivs) {
      if (excludedClassNames.some((className) => div.classList.contains(className))) {
        continue suck;
      }
      let headingNodeList = div.querySelectorAll("h1, h2, h3, h4, h5, h6"), currentDivIsHeading = headingNodeList.length > 0;
      if (currentDivIsHeading) {
        let hTag = headingNodeList[0].tagName.toLowerCase();
        hNumber = parseInt(hTag.replace(/^\D+/g, ""));
        div.style.marginLeft = arrMargins[hNumber - 1] + "px";
        div.classList.add(arrClassesHeadings[hNumber]);
      } else {
        div.style.marginLeft = arrMargins[hNumber] + "px";
        div.classList.add(arrClassesData[hNumber]);
      }
    }
}
function cleanSectionModifications(arrDivs) {
  for (const div of arrDivs) {
    div.style.marginLeft = null;
    div.classList.forEach((item) => {
      if (item.startsWith("data_") || item.startsWith("heading_")) {
        div.classList.remove(item);
      }
    });
  }
}

// src/settings.ts
var import_obsidian = __toModule(require("obsidian"));
var DEFAULT_SETTINGS = {
  h1: "30",
  h2: "50",
  h3: "70",
  h4: "90",
  h5: "110",
  h6: "130"
};
var IndentSettingTab = class extends import_obsidian.PluginSettingTab {
  constructor(app, plugin) {
    super(app, plugin);
    this.plugin = plugin;
  }
  display() {
    const { containerEl } = this;
    containerEl.empty();
    containerEl.createEl("h2", { text: "Set identations for each heading (in pixels)" });
    new import_obsidian.Setting(containerEl).setName("H1 Identation").addText((number) => number.setPlaceholder("").setValue(this.plugin.settings.h1).onChange((value) => __async(this, null, function* () {
      this.plugin.settings.h1 = value;
      yield this.plugin.saveSettings();
    })));
    new import_obsidian.Setting(containerEl).setName("H2 Identation").addText((text) => text.setPlaceholder("").setValue(this.plugin.settings.h2).onChange((value) => __async(this, null, function* () {
      this.plugin.settings.h2 = value;
      yield this.plugin.saveSettings();
    })));
    new import_obsidian.Setting(containerEl).setName("H3 Identation").addText((text) => text.setPlaceholder("").setValue(this.plugin.settings.h3).onChange((value) => __async(this, null, function* () {
      this.plugin.settings.h3 = value;
      yield this.plugin.saveSettings();
    })));
    new import_obsidian.Setting(containerEl).setName("H4 Identation").addText((text) => text.setPlaceholder("").setValue(this.plugin.settings.h4).onChange((value) => __async(this, null, function* () {
      this.plugin.settings.h4 = value;
      yield this.plugin.saveSettings();
    })));
    new import_obsidian.Setting(containerEl).setName("H5 Identation").addText((text) => text.setPlaceholder("").setValue(this.plugin.settings.h5).onChange((value) => __async(this, null, function* () {
      this.plugin.settings.h5 = value;
      yield this.plugin.saveSettings();
    })));
    new import_obsidian.Setting(containerEl).setName("H6 Identation").addText((text) => text.setPlaceholder("").setValue(this.plugin.settings.h6).onChange((value) => __async(this, null, function* () {
      this.plugin.settings.h6 = value;
      yield this.plugin.saveSettings();
    })));
  }
};

// src/main.ts
var HeadingIndent = class extends import_obsidian2.Plugin {
  onload() {
    return __async(this, null, function* () {
      yield this.loadSettings();
      console.log("plugin Heading Indent Settings loaded");
      this.addSettingTab(new IndentSettingTab(this.app, this));
      this.app.workspace.onLayoutReady(() => {
        wrapperIndentPreview(this, 100, false);
        wrapperIndentPreview(this, 300, false);
        wrapperIndentPreview(this, 1e3, false);
        setObserverToActiveLeaf(this);
      });
      this.registerEvent(this.app.workspace.on("active-leaf-change", () => {
        indentPreview(this);
        wrapperIndentPreview(this, 100, false);
        setObserverToActiveLeaf(this);
      }));
      this.registerEvent(this.app.workspace.on("layout-change", () => {
        setObserverToActiveLeaf(this);
      }));
    });
  }
  onunload() {
    this.previewObserver && this.previewObserver.disconnect();
  }
  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);
      indentPreview(this);
    });
  }
};
//# sourceMappingURL=data:application/json;base64,[encoded data, 25476 characters]
