cm-md-editor 2.2.0 → 2.3.0
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/index.html +1 -1
- package/package.json +1 -1
- package/src/MdEditor.js +17 -11
package/index.html
CHANGED
|
@@ -35,7 +35,7 @@ description: Some example text for the editor
|
|
|
35
35
|
|
|
36
36
|
### Heading 3
|
|
37
37
|
|
|
38
|
-
This is a minimal **markdown editor** with _italic_ and **_bold italic_** text. You can also use ~~strikethrough~~ for deleted text.
|
|
38
|
+
This is a minimal **markdown editor** with _italic_ and *also italic* and **_bold italic_** text. You can also use ~~strikethrough~~ for deleted text.
|
|
39
39
|
|
|
40
40
|
<!-- This is a comment -->
|
|
41
41
|
|
package/package.json
CHANGED
package/src/MdEditor.js
CHANGED
|
@@ -102,7 +102,7 @@ export class MdEditor {
|
|
|
102
102
|
// Copy textarea computed styles to backdrop
|
|
103
103
|
const cs = window.getComputedStyle(this.element)
|
|
104
104
|
this.backdrop.style.cssText = `position:absolute;overflow:hidden;pointer-events:none;z-index:1;opacity:${this.props.syntaxHighlight};`
|
|
105
|
-
this.highlightLayer.style.cssText = `white-space:pre-wrap;word-wrap:break-word;overflow-wrap:break-word
|
|
105
|
+
this.highlightLayer.style.cssText = `white-space:pre-wrap;word-wrap:break-word;overflow-wrap:break-word;`
|
|
106
106
|
|
|
107
107
|
const syncStyles = () => {
|
|
108
108
|
const cs = window.getComputedStyle(this.element)
|
|
@@ -125,13 +125,16 @@ export class MdEditor {
|
|
|
125
125
|
this.element.style.overscrollBehavior = 'none'
|
|
126
126
|
this.element.style.background = 'transparent'
|
|
127
127
|
this.element.style.position = 'relative'
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
this.
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
128
|
+
const originalColor = cs.color
|
|
129
|
+
this.element.style.caretColor = originalColor
|
|
130
|
+
this.element.style.color = 'transparent'
|
|
131
|
+
this.highlightLayer.style.color = originalColor
|
|
132
|
+
|
|
133
|
+
// Sync scroll via transform — no visible jitter since all text is in the highlight layer
|
|
134
|
+
const syncScroll = () => {
|
|
135
|
+
this.highlightLayer.style.transform = `translate(${-this.element.scrollLeft}px, ${-this.element.scrollTop}px)`
|
|
136
|
+
}
|
|
137
|
+
this.element.addEventListener('scroll', syncScroll)
|
|
135
138
|
|
|
136
139
|
// Update on input
|
|
137
140
|
this.element.addEventListener('input', () => this.updateHighlight())
|
|
@@ -312,8 +315,10 @@ export class MdEditor {
|
|
|
312
315
|
result = result.replace(/(\*\*)(.*?)(\*\*)/g, (_, p1, p2, p3) =>
|
|
313
316
|
this.colorSpan('colorBold', p1) + this.colorSpan('colorBold', p2) + this.colorSpan('colorBold', p3))
|
|
314
317
|
|
|
315
|
-
// Italic _text_
|
|
316
|
-
result = result.replace(/((?:^|[
|
|
318
|
+
// Italic _text_ or *text* (single asterisk, after bold has been handled)
|
|
319
|
+
result = result.replace(/((?:^|[^\\*]))(\_)(.*?[^\\])(\_)/g, (_, pre, p1, p2, p3) =>
|
|
320
|
+
pre + this.colorSpan('colorItalic', p1) + this.colorSpan('colorItalic', p2) + this.colorSpan('colorItalic', p3))
|
|
321
|
+
result = result.replace(/((?:^|[^\\*]))(\*)((?!\*).+?[^\\])(\*)/g, (_, pre, p1, p2, p3) =>
|
|
317
322
|
pre + this.colorSpan('colorItalic', p1) + this.colorSpan('colorItalic', p2) + this.colorSpan('colorItalic', p3))
|
|
318
323
|
|
|
319
324
|
// HTML tags
|
|
@@ -341,7 +346,7 @@ export class MdEditor {
|
|
|
341
346
|
getCurrentLineInfo() {
|
|
342
347
|
const start = this.element.selectionStart
|
|
343
348
|
const text = this.element.value
|
|
344
|
-
const lineStart = text.lastIndexOf('\n', start - 1) + 1
|
|
349
|
+
const lineStart = start === 0 ? 0 : text.lastIndexOf('\n', start - 1) + 1
|
|
345
350
|
let lineEnd = text.indexOf('\n', start)
|
|
346
351
|
if (lineEnd === -1) lineEnd = text.length
|
|
347
352
|
const line = text.substring(lineStart, lineEnd)
|
|
@@ -354,6 +359,7 @@ export class MdEditor {
|
|
|
354
359
|
}
|
|
355
360
|
|
|
356
361
|
toggleHeading(level) {
|
|
362
|
+
this.element.focus()
|
|
357
363
|
const {lineStart, lineEnd, line} = this.getCurrentLineInfo()
|
|
358
364
|
const prefix = '#'.repeat(level) + ' '
|
|
359
365
|
const headingMatch = line.match(/^(#{1,6}) /)
|