tiny-markdown-editor 0.1.29 → 0.1.31

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.
package/lib/TinyMDE.js CHANGED
@@ -44,27 +44,32 @@ class Editor {
44
44
  if (this.textarea) {
45
45
  this.textarea.style.display = "none";
46
46
  }
47
- this.createEditorElement(element);
47
+ this.createEditorElement(element, props);
48
48
  this.setContent(typeof props.content === "string" ? props.content : this.textarea ? this.textarea.value : "# Hello TinyMDE!\nEdit **here**");
49
49
  }
50
50
 
51
51
  /**
52
52
  * Creates the editor element inside the target element of the DOM tree
53
53
  * @param element The target element of the DOM tree
54
+ * @param props options, passed from constructor's props
54
55
  */
55
- createEditorElement(element) {
56
- this.e = document.createElement("div");
57
- this.e.className = "TinyMDE";
56
+ createEditorElement(element, props) {
57
+ if (props && props.editor !== undefined) {
58
+ if (props.editor.tagName) this.e = props.editor;else this.e = document.getElementById(props.editor);
59
+ } else this.e = document.createElement("div");
60
+ this.e.classList.add("TinyMDE");
58
61
  this.e.contentEditable = true;
59
62
  // The following is important for formatting purposes, but also since otherwise the browser replaces subsequent spaces with    
60
63
  // That breaks a lot of stuff, so we do this here and not in CSS—therefore, you don't have to remember to put this in the CSS file
61
64
  this.e.style.whiteSpace = "pre-wrap";
62
65
  // Avoid formatting (B / I / U) popping up on iOS
63
66
  this.e.style.webkitUserModify = "read-write-plaintext-only";
64
- if (this.textarea && this.textarea.parentNode == element && this.textarea.nextSibling) {
65
- element.insertBefore(this.e, this.textarea.nextSibling);
66
- } else {
67
- element.appendChild(this.e);
67
+ if (props.editor === undefined) {
68
+ if (this.textarea && this.textarea.parentNode == element && this.textarea.nextSibling) {
69
+ element.insertBefore(this.e, this.textarea.nextSibling);
70
+ } else {
71
+ element.appendChild(this.e);
72
+ }
68
73
  }
69
74
  this.e.addEventListener("input", e => this.handleInputEvent(e));
70
75
  this.e.addEventListener("compositionend", e => this.handleInputEvent(e));
@@ -165,7 +170,7 @@ class Editor {
165
170
  /**
166
171
  * Determines line types for all lines based on the line / block grammar. Captures the results of the respective line
167
172
  * grammar regular expressions.
168
- * Updates this.lineTypes, this.lineCaptures, and this.lineReplacements.
173
+ * Updates this.lineTypes, this.lineCaptures, and this.lineReplacements, as well as this.lineDirty.
169
174
  */
170
175
  updateLineTypes() {
171
176
  let codeBlockType = false;
@@ -921,13 +926,13 @@ class Editor {
921
926
  let firstChangedLine = 0;
922
927
  while (firstChangedLine <= this.lines.length && firstChangedLine <= this.lineElements.length && this.lineElements[firstChangedLine] &&
923
928
  // Check that the line element hasn't been deleted
924
- this.lines[firstChangedLine] == this.lineElements[firstChangedLine].textContent) {
929
+ this.lines[firstChangedLine] == this.lineElements[firstChangedLine].textContent && this.lineTypes[firstChangedLine] == this.lineElements[firstChangedLine].className) {
925
930
  firstChangedLine++;
926
931
  }
927
932
 
928
933
  // End also from the end
929
934
  let lastChangedLine = -1;
930
- while (-lastChangedLine < this.lines.length && -lastChangedLine < this.lineElements.length && this.lines[this.lines.length + lastChangedLine] == this.lineElements[this.lineElements.length + lastChangedLine].textContent) {
935
+ while (-lastChangedLine < this.lines.length && -lastChangedLine < this.lineElements.length && this.lines[this.lines.length + lastChangedLine] == this.lineElements[this.lineElements.length + lastChangedLine].textContent && this.lineTypes[this.lines.length + lastChangedLine] == this.lineElements[this.lineElements.length + lastChangedLine].className) {
931
936
  lastChangedLine--;
932
937
  }
933
938
  let linesToDelete = this.lines.length + lastChangedLine + 1 - firstChangedLine;
@@ -943,9 +948,10 @@ class Editor {
943
948
  for (let line = 0; line < this.lineElements.length; line++) {
944
949
  let e = this.lineElements[line];
945
950
  let ct = e.textContent;
946
- if (this.lines[line] !== ct) {
951
+ if (this.lines[line] !== ct || this.lineTypes[line] !== e.className) {
947
952
  // Line changed, update it
948
953
  this.lines[line] = ct;
954
+ this.lineTypes[line] = e.className;
949
955
  this.lineDirty[line] = true;
950
956
  }
951
957
  }
@@ -977,14 +983,12 @@ class Editor {
977
983
  break;
978
984
  }
979
985
  let lines = this.lines[sel.row].replace(/\n\n$/, "\n").split(/(?:\r\n|\n|\r)/);
980
- if (lines.length == 1) {
981
- // No new line
982
- this.updateFormatting();
983
- return;
986
+ if (lines.length > 1) {
987
+ // New line
988
+ this.spliceLines(sel.row, 1, lines, true);
989
+ sel.row++;
990
+ sel.col = 0;
984
991
  }
985
- this.spliceLines(sel.row, 1, lines, true);
986
- sel.row++;
987
- sel.col = 0;
988
992
  if (continuableType) {
989
993
  // Check if the previous line was non-empty
990
994
  let capture = _grammar.lineGrammar[continuableType].regexp.exec(this.lines[sel.row - 1]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-markdown-editor",
3
- "version": "0.1.29",
3
+ "version": "0.1.31",
4
4
  "description": "TinyMDE: A tiny, ultra low dependency, embeddable HTML/JavaScript Markdown editor.",
5
5
  "main": "lib/index.js",
6
6
  "files": [