markdown-text-editor 1.9.2 → 1.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2433,6 +2433,14 @@ var On = Dn(), Z = class {
2433
2433
  constructor(e, t) {
2434
2434
  this.editor = e, this.defaultText = `${t} text`, this.button = this.createButton(), this.title = t;
2435
2435
  }
2436
+ trimSelection() {
2437
+ let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n, value: r } = e;
2438
+ if (t === n) return;
2439
+ let i = t, a = n;
2440
+ for (; i < a && /\s/.test(r[i]);) i++;
2441
+ for (; a > i && /\s/.test(r[a - 1]);) a--;
2442
+ i !== a && (i !== t || a !== n) && e.setSelectionRange(i, a);
2443
+ }
2436
2444
  createButton(e) {
2437
2445
  let t = document.createElement("button"), n = this.title ? `${this.title.replace(/ /g, "-")}-btn`.toLowerCase() : "";
2438
2446
  t.innerHTML = e;
@@ -2448,6 +2456,7 @@ var On = Dn(), Z = class {
2448
2456
  super(e, "Bold"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M7 5h6a3.5 3.5 0 0 1 0 7h-6l0 -7\" /><path d=\"M13 12h1a3.5 3.5 0 0 1 0 7h-7v-7\" /></svg>\n ");
2449
2457
  }
2450
2458
  applySyntax() {
2459
+ this.trimSelection();
2451
2460
  let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n } = e, r = e.value, i = r.substring(t, n), a = "", o = 0;
2452
2461
  if (i.startsWith("***") && i.endsWith("***")) a = "*" + i.slice(3, -3) + "*";
2453
2462
  else if (i.startsWith("**") && i.endsWith("**")) a = i.slice(2, -2);
@@ -2465,6 +2474,7 @@ var On = Dn(), Z = class {
2465
2474
  super(e, "Italic"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M11 5l6 0\" /><path d=\"M7 19l6 0\" /><path d=\"M14 5l-4 14\" /></svg>\n ");
2466
2475
  }
2467
2476
  applySyntax() {
2477
+ this.trimSelection();
2468
2478
  let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n } = e, r = e.value, i = r.substring(t, n), a = "", o = 0;
2469
2479
  if (i.startsWith("***") && i.endsWith("***")) a = "**" + i.slice(3, -3) + "**";
2470
2480
  else if (i.startsWith("*") && i.endsWith("*") && !i.startsWith("**")) a = i.slice(1, -1);
@@ -2482,6 +2492,7 @@ var On = Dn(), Z = class {
2482
2492
  super(e, "Strikethrough"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M5 12l14 0\" /><path d=\"M16 6.5a4 2 0 0 0 -4 -1.5h-1a3.5 3.5 0 0 0 0 7h2a3.5 3.5 0 0 1 0 7h-1.5a4 2 0 0 1 -4 -1.5\" /></svg>\n ");
2483
2493
  }
2484
2494
  applySyntax() {
2495
+ this.trimSelection();
2485
2496
  let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n } = e, r = e.value, i = r.substring(t, n), a = "", o = 0;
2486
2497
  if (i.startsWith("~~") && i.endsWith("~~")) a = i.slice(2, -2);
2487
2498
  else if (r.substring(t - 2, t) === "~~" && r.substring(n, n + 2) === "~~") {
@@ -2601,6 +2612,7 @@ var $ = class extends Z {
2601
2612
  super(e, "Link"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M9 15l6 -6\" /><path d=\"M11 6l.463 -.536a5 5 0 0 1 7.071 7.072l-.534 .464\" /><path d=\"M13 18l-.397 .534a5.068 5.068 0 0 1 -7.127 0a4.972 4.972 0 0 1 0 -7.071l.524 -.463\" /></svg>\n ");
2602
2613
  }
2603
2614
  applySyntax(e) {
2615
+ this.trimSelection();
2604
2616
  let t = this.editor, n = t.usertextarea, { selectionStart: r, selectionEnd: i } = n, a = n.value, o = a.substring(0, r), s = a.substring(i);
2605
2617
  if (o.endsWith("[") && /^\]\([^)]+\)/.test(s)) {
2606
2618
  let e = s.match(/^\]\(([^)]+)\)/);
@@ -2790,6 +2802,7 @@ var zn = class extends Z {
2790
2802
  super(e, "Code"), this.button = this.createButton("\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"20\" height=\"20\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><path stroke=\"none\" d=\"M0 0h24v24H0z\" fill=\"none\"/><path d=\"M7 8l-4 4l4 4\" /><path d=\"M17 8l4 4l-4 4\" /><path d=\"M14 4l-4 16\" /></svg>\n ");
2791
2803
  }
2792
2804
  applySyntax() {
2805
+ this.trimSelection();
2793
2806
  let e = this.editor.usertextarea, { selectionStart: t, selectionEnd: n } = e, r = e.value, i = r.substring(t, n);
2794
2807
  if (i.startsWith("`") && i.endsWith("`")) {
2795
2808
  this.editor.insertText(i.slice(1, -1), 0, 0);
@@ -3637,7 +3650,7 @@ var Or = class {
3637
3650
  }
3638
3651
  }, kr = class {
3639
3652
  constructor(e) {
3640
- this.editor = e, this._matches = [], this._currentIndex = -1, this._caseSensitive = !1, this._panel = null, this._findInput = null, this._replaceInput = null, this._countEl = null, this._replaceRow = null, this._caseSensitiveBtn = null, this._highlightEl = null, this._scrollHandler = null, this._taHandler = this._onTextareaKeyDown.bind(this), e.usertextarea.addEventListener("keydown", this._taHandler);
3653
+ this.editor = e, this._matches = [], this._currentIndex = -1, this._caseSensitive = !1, this._panel = null, this._findInput = null, this._replaceInput = null, this._countEl = null, this._replaceRow = null, this._caseSensitiveBtn = null, this._highlightEl = null, this._matchTop = null, this._matchHeight = 0, this._scrollHandler = null, this._taHandler = this._onTextareaKeyDown.bind(this), e.usertextarea.addEventListener("keydown", this._taHandler);
3641
3654
  }
3642
3655
  _onTextareaKeyDown(e) {
3643
3656
  let t = e.ctrlKey || e.metaKey;
@@ -3720,12 +3733,13 @@ var Or = class {
3720
3733
  this._clearMatchHighlight();
3721
3734
  return;
3722
3735
  }
3723
- let { start: e } = this._matches[this._currentIndex];
3724
- this._scrollToMatch(this.editor.usertextarea, e), this._showMatchHighlight();
3736
+ this._showMatchHighlight(), this._scrollMatchIntoView();
3725
3737
  }
3726
- _scrollToMatch(e, t) {
3727
- let n = parseInt(window.getComputedStyle(e).lineHeight) || 20, r = e.value.substring(0, t).split("\n").length - 1, i = Math.floor(e.clientHeight / n);
3728
- e.scrollTop = Math.max(0, (r - Math.floor(i / 2)) * n);
3738
+ _scrollMatchIntoView() {
3739
+ let e = this.editor.usertextarea;
3740
+ if (this._matchTop === null || this._matchHeight <= 0) return;
3741
+ let t = e.clientHeight, n = e.scrollTop, r = this._matchHeight, i = n;
3742
+ this._matchTop - r < n ? i = this._matchTop - r : this._matchTop + this._matchHeight + r > n + t && (i = this._matchTop + this._matchHeight + r - t), i !== n && (e.scrollTop = Math.max(0, i), this._highlightEl && (this._highlightEl.style.top = this._matchTop - e.scrollTop + "px"));
3729
3743
  }
3730
3744
  _showMatchHighlight() {
3731
3745
  if (this._clearMatchHighlight(), this._currentIndex === -1 || !this._matches.length) return;
@@ -3736,7 +3750,7 @@ var Or = class {
3736
3750
  pointerEvents: "none",
3737
3751
  top: "0",
3738
3752
  left: "0",
3739
- right: "0",
3753
+ width: n.clientWidth + "px",
3740
3754
  whiteSpace: "pre-wrap",
3741
3755
  wordBreak: "break-word",
3742
3756
  overflowWrap: "break-word",
@@ -3751,7 +3765,9 @@ var Or = class {
3751
3765
  let o = document.createElement("span");
3752
3766
  o.textContent = n.value.substring(e, t) || "​", a.appendChild(document.createTextNode(n.value.substring(0, e))), a.appendChild(o), a.appendChild(document.createTextNode(n.value.substring(t))), i.appendChild(a);
3753
3767
  try {
3754
- let e = i.getBoundingClientRect(), t = o.getBoundingClientRect(), r = document.createElement("div");
3768
+ let e = i.getBoundingClientRect(), t = o.getBoundingClientRect();
3769
+ this._matchTop = t.top - e.top, this._matchHeight = t.height;
3770
+ let r = document.createElement("div");
3755
3771
  Object.assign(r.style, {
3756
3772
  position: "absolute",
3757
3773
  top: t.top - e.top - n.scrollTop + "px",
@@ -3768,7 +3784,7 @@ var Or = class {
3768
3784
  }
3769
3785
  }
3770
3786
  _clearMatchHighlight() {
3771
- this._highlightEl?.remove(), this._highlightEl = null;
3787
+ this._highlightEl?.remove(), this._highlightEl = null, this._matchTop = null;
3772
3788
  }
3773
3789
  _updateCount() {
3774
3790
  this._countEl && (this._matches.length === 0 ? this._countEl.textContent = this._findInput?.value ? "No results" : "" : this._countEl.textContent = `${this._currentIndex + 1} of ${this._matches.length}`);
@@ -3905,20 +3921,23 @@ var Or = class {
3905
3921
  this._autoGrow(), this.renderHybrid(), this.debouncedPreview(), this.notifyChange();
3906
3922
  }, this._scrollHandler = () => {
3907
3923
  this._scrollRaf ||= requestAnimationFrame(() => {
3908
- if (this._scrollRaf = null, this.mode === "hybrid" && (this.displayLayer.scrollTop = this.usertextarea.scrollTop, this.displayLayer.scrollLeft = this.usertextarea.scrollLeft), this.preview && this.previewContent) {
3909
- let e = this.usertextarea, t = e.scrollTop / (e.scrollHeight - e.clientHeight);
3910
- this.previewContent.scrollTop = t * (this.previewContent.scrollHeight - this.previewContent.clientHeight);
3911
- }
3924
+ this._scrollRaf = null, this.mode === "hybrid" && (this.displayLayer.scrollTop = this.usertextarea.scrollTop, this.displayLayer.scrollLeft = this.usertextarea.scrollLeft), this.preview && this.previewContent && this._syncPreviewScroll();
3912
3925
  });
3913
3926
  }, this.usertextarea.addEventListener("input", this._inputHandler), this.usertextarea.addEventListener("scroll", this._scrollHandler);
3914
3927
  }
3915
3928
  debouncedPreview() {
3916
3929
  this.preview && (clearTimeout(this.previewTimer), this.previewTimer = setTimeout(() => {
3917
- this.previewContent.innerHTML = this._renderMarkdown(this.usertextarea.value), this.wirePreviewCheckboxes();
3918
- let e = this.usertextarea, t = e.scrollTop / (e.scrollHeight - e.clientHeight);
3919
- this.previewContent.scrollTop = t * (this.previewContent.scrollHeight - this.previewContent.clientHeight);
3930
+ let e = this.previewContent.scrollTop;
3931
+ this.previewContent.innerHTML = this._renderMarkdown(this.usertextarea.value), this.wirePreviewCheckboxes(), this._syncPreviewScroll() || (this.previewContent.scrollTop = e);
3920
3932
  }, 150));
3921
3933
  }
3934
+ _syncPreviewScroll() {
3935
+ if (!this.preview || !this.previewContent) return !1;
3936
+ let e = this.usertextarea, t = e.scrollHeight - e.clientHeight;
3937
+ if (t <= 0) return !1;
3938
+ let n = e.scrollTop / t, r = this.previewContent.scrollHeight - this.previewContent.clientHeight;
3939
+ return this.previewContent.scrollTop = n * r, !0;
3940
+ }
3922
3941
  renderMarkdown(e) {
3923
3942
  return this._renderMarkdown(e);
3924
3943
  }