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

// src/ScrollOffsetCM6.ts
var import_view = __toModule(require("@codemirror/view"));
var import_state = __toModule(require("@codemirror/state"));
var eventHandlers = {
  mousedown(event, view) {
    this.switch = false;
  },
  keydown(event, view) {
    this.switch = true;
  }
};
function generateScrollOffsetCM6Plugin(calcRequiredOffset) {
  return import_state.Prec.highest(import_view.ViewPlugin.fromClass(class {
    constructor(_view) {
      this.margin = 200;
      this.switch = true;
    }
    update(_update) {
      if (!_update.selectionSet)
        return;
      const view = _update.view;
      view.requestMeasure({
        read: () => {
          return {
            cursor: view.coordsAtPos(view.state.selection.main.head)
          };
        },
        write: ({ cursor }) => {
          if (!cursor)
            return;
          if (this.switch) {
            const cursorHeight = cursor.bottom - cursor.top + 5;
            const requiredOffset = calcRequiredOffset(view.dom, cursorHeight);
            this.margin = requiredOffset;
          } else {
            this.margin = 0;
          }
        }
      });
    }
  }, {
    eventHandlers,
    provide: (plugin) => import_view.EditorView.scrollMargins.of((view) => {
      const value = view.plugin(plugin);
      return {
        top: value.margin,
        bottom: value.margin
      };
    })
  }));
}
var ScrollOffsetCM6_default = generateScrollOffsetCM6Plugin;

// src/main.ts
var DEFAULT_SETTINGS = {
  percentageMode: true,
  offset: "25"
};
var ScrollOffset = class extends import_obsidian.Plugin {
  constructor() {
    super(...arguments);
    this.clickSwitch = true;
    this.mouseDownHandler = () => {
      this.clickSwitch = false;
    };
    this.cursorActiveHandler = (cm) => {
      if (!this.clickSwitch) {
        this.clickSwitch = true;
        return;
      }
      this.scrollLaunch(cm);
    };
    this.calcRequiredOffset = (container, cursorHeight) => {
      const { settings } = this;
      const maxOffset = (container.offsetHeight - cursorHeight) / 2;
      let requiredOffset = settings.percentageMode ? container.offsetHeight * +settings.offset / 100 : +settings.offset;
      requiredOffset = Math.min(requiredOffset, maxOffset);
      return requiredOffset;
    };
    this.scrollLaunch = (cm) => {
      const cursor = cm.charCoords(cm.getCursor());
      const cursorHeight = cursor.bottom - cursor.top + 5;
      const container = cm.getWrapperElement();
      const requiredOffset = this.calcRequiredOffset(container, cursorHeight);
      cm.scrollIntoView(null, requiredOffset);
    };
    this.enableScrollOffset = () => {
      this.registerCodeMirror((cm) => {
        cm.on("mousedown", this.mouseDownHandler);
        cm.on("cursorActivity", this.cursorActiveHandler);
      });
      this.registerEditorExtension(ScrollOffsetCM6_default(this.calcRequiredOffset));
    };
    this.disableScrollOffset = () => {
      this.app.workspace.iterateCodeMirrors((cm) => {
        cm.off("mousedown", this.mouseDownHandler);
        cm.off("cursorActivity", this.cursorActiveHandler);
      });
    };
  }
  onload() {
    return __async(this, null, function* () {
      console.log("Loading Scroll Offset.");
      yield this.loadSettings();
      this.addSettingTab(new ScrollOffsetSettingTab(this.app, this));
      this.enableScrollOffset();
    });
  }
  onunload() {
    this.disableScrollOffset();
    console.log("Scroll Offset unloaded.");
  }
  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);
    });
  }
};
var ScrollOffsetSettingTab = class extends import_obsidian.PluginSettingTab {
  constructor(app, plugin) {
    super(app, plugin);
    this.plugin = plugin;
  }
  display() {
    const { containerEl } = this;
    containerEl.empty();
    containerEl.createEl("h2", { text: "Scroll Offset Settings." });
    new import_obsidian.Setting(containerEl).setName("Use percentage offset").setDesc("Use percentage offset, or use fixed number of distance instead").addToggle((comp) => comp.setValue(this.plugin.settings.percentageMode).onChange((value) => {
      this.plugin.settings.percentageMode = value;
      this.plugin.saveSettings();
    }));
    new import_obsidian.Setting(containerEl).setName("Distance").setDesc('unit in "px", or "%" if using percentage offset, 0 to disable this plugin').addText((text) => text.setValue(this.plugin.settings.offset).onChange((value) => {
      this.plugin.settings.offset = value;
      this.plugin.saveSettings();
    }));
  }
};


/* nosourcemap */