/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// main.ts
var main_exports = {};
__export(main_exports, {
  default: () => FrenchTypos
});
module.exports = __toCommonJS(main_exports);
var import_obsidian = require("obsidian");
var DEFAULT_SETTINGS = {
  apostrophe: true,
  quotationmarks: true,
  emdashes: true,
  twoenters: true,
  desactivatelinks: true,
  hyphenate: true,
  emptytlines: "normal"
};
var FrenchTypos = class extends import_obsidian.Plugin {
  constructor() {
    super(...arguments);
    this.openQuote = true;
  }
  // Variable pour suivre l'état des guillemets
  async onload() {
    this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
    this.addSettingTab(new FrenchTyposSettingTab(this.app, this));
    this.addCommand({
      id: "Apostrophes",
      name: "Apostrophes",
      editorCallback: (editor, view) => {
        this.updateApostrophes(editor);
      }
    });
    this.registerDomEvent(document, "keydown", (event) => {
      const activeState = this.app.workspace.getLeaf().getViewState().state;
      const activeView = this.app.workspace.getActiveViewOfType(import_obsidian.MarkdownView);
      if (activeView && activeView.getMode() === "source" && activeState["source"] === false) {
        const editor = activeView.editor;
        const cursor = editor.getCursor();
        if (event.key === "'" && this.settings.apostrophe) {
          event.preventDefault();
          const selection = editor.getSelection();
          if (selection.length > 0) {
            editor.replaceSelection("\u2019");
          } else {
            editor.replaceRange("\u2019", cursor);
            editor.setCursor({ line: cursor.line, ch: cursor.ch + 1 });
          }
        } else if (event.key === '"' && this.settings.quotationmarks) {
          event.preventDefault();
          if (this.openQuote) {
            editor.replaceRange("\xAB\xA0", cursor);
          } else {
            editor.replaceRange("\xA0\xBB", cursor);
          }
          editor.setCursor({ line: cursor.line, ch: cursor.ch + 2 });
          this.openQuote = !this.openQuote;
        } else if (event.key === " " && editor.getRange({ line: cursor.line, ch: cursor.ch - 2 }, cursor) === "--" && this.settings.emdashes) {
          event.preventDefault();
          editor.replaceRange("\u2014\xA0", { line: cursor.line, ch: cursor.ch - 2 }, { line: cursor.line, ch: cursor.ch });
        } else if (event.key === "Enter" && this.settings.twoenters) {
          event.preventDefault();
          editor.replaceRange("\n\n", cursor);
          editor.setCursor({ line: cursor.line + 2, ch: cursor.ch });
        }
      }
    });
    this.registerDomEvent(document, "click", (event) => {
      var _a, _b;
      const target = event.target;
      const parent = target.parentNode;
      const ancertor = parent.parentNode;
      const activeView = this.app.workspace.getActiveViewOfType(import_obsidian.MarkdownView);
      const activeState = this.app.workspace.getLeaf().getViewState().state;
      if (activeView && activeView.getMode() === "source" && activeState["source"] === false && parent.classList.contains("cm-link") && this.settings.desactivatelinks) {
        event.preventDefault();
        event.stopPropagation();
        const editor = activeView.editor;
        const linktext = (_a = target.textContent) != null ? _a : "";
        const longtext = (_b = ancertor == null ? void 0 : ancertor.textContent) != null ? _b : "";
        this.MoveCursor(editor, longtext, linktext);
      }
    }, true);
    if (this.settings.emptytlines == "invisible") {
      await this.injectCSS(this.nobrcss());
    } else if (this.settings.emptytlines == "small") {
      await this.injectCSS(this.small_interline());
    }
    if (this.settings.hyphenate) {
      await this.setLanguage();
      await this.injectCSS(this.hyphenscss());
    }
  }
  updateApostrophes(editor) {
    const cursor = editor.getCursor();
    const text = editor.getValue();
    const updatedText = text.replace(/'/g, "\u2019");
    editor.setValue(updatedText);
    editor.setCursor(cursor);
  }
  MoveCursor(editor, longtext, linktext) {
    const markdownContent = editor.getValue();
    const lines = markdownContent.split("\n");
    for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
      const nolink = this.noLinks(lines[lineIndex]);
      const lIndex = nolink.indexOf(this.noLinks(longtext));
      if (lIndex !== -1) {
        const search = "[" + linktext + "]";
        let localIndex = lines[lineIndex].indexOf(search);
        if (localIndex !== -1) {
          const coords = { line: lineIndex, ch: localIndex };
          editor.setCursor(coords);
        }
      }
    }
  }
  noLinks(makdown) {
    return makdown.replace(/\[(.*?)\]\(.*?\)/g, "$1");
  }
  async setLanguage() {
    document.documentElement.setAttribute("lang", "fr");
  }
  async loadSettings() {
    this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
  }
  async saveSettings() {
    await this.saveData(this.settings);
  }
  // /Users/thierrycrouzet/Documents/ObsidianDev/.obsidian/plugins/obsidian-french-typos/nobr.css
  async injectCSS(css) {
    const styleEl = document.createElement("style");
    styleEl.innerHTML = css;
    document.head.appendChild(styleEl);
  }
  nobrcss() {
    return `
		.markdown-source-view.mod-cm6.is-live-preview .HyperMD-header{
			text-indent: 0rem !important;
		}
		
		.markdown-source-view.mod-cm6.is-live-preview .cm-line {
			text-indent: 2rem;
		}
		
		.markdown-source-view.mod-cm6.is-live-preview .HyperMD-header-1{
			margin-bottom: 2rem !important;
		}
		
		.markdown-source-view.mod-cm6.is-live-preview .HyperMD-header-2{
			margin-top: 1rem !important;
		}
		
		.markdown-source-view.mod-cm6.is-live-preview .HyperMD-header-3{
			margin-top: 1rem !important;
		}
		
		.markdown-source-view.mod-cm6.is-live-preview .cm-line:has(> br) {
			display: none;
		}
		
		.markdown-source-view.mod-cm6.is-live-preview .cm-active:has(> br) {
			display: inline !important;
		}
		
		.markdown-source-view.mod-cm6.is-live-preview .cm-line.HyperMD-list-line {
			margin: 0.25rem 0;
		}
		`;
  }
  hyphenscss() {
    return `
		.markdown-preview-view p {
			text-indent: 3rem;
			text-align: justify;
			margin-top: 0;
			margin-bottom: 0;
			hyphens: auto;
			word-wrap: break-word;
			hyphenate-character: auto;
		    hyphenate-limit-chars: 6 3 3;
		}
		`;
  }
  small_interline() {
    return `
		.markdown-source-view.mod-cm6.is-live-preview .HyperMD-header{
			text-indent: 0rem !important;
		}

		.markdown-source-view.mod-cm6.is-live-preview .cm-line {
			text-indent: 2rem;
		}

		.markdown-source-view.mod-cm6.is-live-preview .cm-line:has(> br) {
			height: 0.7rem;text-indent: 2rem;
		}
	`;
  }
};
var FrenchTyposSettingTab = class extends import_obsidian.PluginSettingTab {
  constructor(app, plugin) {
    super(app, plugin);
    this.plugin = plugin;
  }
  display() {
    const { containerEl } = this;
    containerEl.empty();
    const titleEl = containerEl.createEl("h2", { text: "French Typos settings" });
    const desEl = containerEl.createEl("p", { text: "Works mainly in Live Preview mode." });
    new import_obsidian.Setting(containerEl).setName("Apostrophe").setDesc("Activate typographic apostrophe").addToggle((toggle) => toggle.setValue(this.plugin.settings.apostrophe).onChange(async (value) => {
      this.plugin.settings.apostrophe = value;
      await this.plugin.saveSettings();
    }));
    new import_obsidian.Setting(containerEl).setName("French quotation marks and hard spaces").setDesc("Convert standard quotation marks").addToggle((toggle) => toggle.setValue(this.plugin.settings.quotationmarks).onChange(async (value) => {
      this.plugin.settings.quotationmarks = value;
      await this.plugin.saveSettings();
    }));
    new import_obsidian.Setting(containerEl).setName("Em dashes").setDesc('Convert "-- " into em dashes').addToggle((toggle) => toggle.setValue(this.plugin.settings.emdashes).onChange(async (value) => {
      this.plugin.settings.emdashes = value;
      await this.plugin.saveSettings();
    }));
    new import_obsidian.Setting(containerEl).setName("Two Enters for one").setDesc("One Enter create a Markdown paragraph").addToggle((toggle) => toggle.setValue(this.plugin.settings.twoenters).onChange(async (value) => {
      this.plugin.settings.twoenters = value;
      await this.plugin.saveSettings();
    }));
    new import_obsidian.Setting(containerEl).setName("Simulate Shift+Clic on links").setDesc("Display the URL instead of opening").addToggle((toggle) => toggle.setValue(this.plugin.settings.desactivatelinks).onChange(async (value) => {
      this.plugin.settings.desactivatelinks = value;
      await this.plugin.saveSettings();
    }));
    new import_obsidian.Setting(containerEl).setName("Empty lines").setDesc("Reload your Vault to process\u2026").addDropdown((dropdown) => {
      dropdown.addOptions({
        "normal": "Normal",
        "small": "Small",
        "invisible": "Invisible"
      });
      dropdown.setValue(this.plugin.settings.emptytlines);
      dropdown.onChange(async (value) => {
        this.plugin.settings.emptytlines = value;
        await this.plugin.saveSettings();
      });
    });
    new import_obsidian.Setting(containerEl).setName("Hyphenate French rules").setDesc("Only in Reading view (reload your Vault to process)").addToggle((toggle) => toggle.setValue(this.plugin.settings.hyphenate).onChange(async (value) => {
      this.plugin.settings.hyphenate = value;
      await this.plugin.saveSettings();
    }));
  }
};
//# sourceMappingURL=data:application/json;base64,[encoded data, 22012 characters]
