mirror of
https://github.com/marktext/marktext.git
synced 2025-05-04 08:42:03 +08:00
Respect existing image title if no source is specified (#599)
This commit is contained in:
parent
a77b9ab6f1
commit
638f65e4ea
2
.github/CHANGELOG.md
vendored
2
.github/CHANGELOG.md
vendored
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
**:butterfly:Optimization**
|
**:butterfly:Optimization**
|
||||||
|
|
||||||
|
- Respect existing image title if no source is specified (#562)
|
||||||
|
|
||||||
**:beetle:Bug fix**
|
**:beetle:Bug fix**
|
||||||
|
|
||||||
- Fix dark preview box background color (#587)
|
- Fix dark preview box background color (#587)
|
||||||
|
@ -176,6 +176,7 @@ const formatCtrl = ContentState => {
|
|||||||
|
|
||||||
ContentState.prototype.insertImage = function (url) {
|
ContentState.prototype.insertImage = function (url) {
|
||||||
const title = /\/?([^./]+)\.[a-z]+$/.exec(url)[1] || ''
|
const title = /\/?([^./]+)\.[a-z]+$/.exec(url)[1] || ''
|
||||||
|
const encodeUrl = encodeURI(url)
|
||||||
const { start, end } = this.cursor
|
const { start, end } = this.cursor
|
||||||
const { formats } = this.selectionFormats({ start, end })
|
const { formats } = this.selectionFormats({ start, end })
|
||||||
const { key, offset: startOffset } = start
|
const { key, offset: startOffset } = start
|
||||||
@ -185,29 +186,38 @@ const formatCtrl = ContentState => {
|
|||||||
const imageFormat = formats.filter(f => f.type === 'image')
|
const imageFormat = formats.filter(f => f.type === 'image')
|
||||||
|
|
||||||
if (imageFormat.length === 1) {
|
if (imageFormat.length === 1) {
|
||||||
// replace pre image
|
// Replace already existing image
|
||||||
|
let imageTitle = title
|
||||||
|
|
||||||
|
// Extract title from image if there isn't an image source already (GH#562). E.g: ![old-title]()
|
||||||
|
if (imageFormat[0].alt && !imageFormat[0].src) {
|
||||||
|
imageTitle = imageFormat[0].alt
|
||||||
|
}
|
||||||
|
|
||||||
const { start, end } = imageFormat[0].range
|
const { start, end } = imageFormat[0].range
|
||||||
block.text = text.substring(0, start) +
|
block.text = text.substring(0, start) +
|
||||||
`` +
|
`` +
|
||||||
text.substring(end)
|
text.substring(end)
|
||||||
|
|
||||||
this.cursor = {
|
this.cursor = {
|
||||||
start: { key, offset: start + 2 },
|
start: { key, offset: start + 2 },
|
||||||
end: { key, offset: start + 2 + title.length }
|
end: { key, offset: start + 2 + imageTitle.length }
|
||||||
}
|
}
|
||||||
} else if (key !== end.key) {
|
} else if (key !== end.key) {
|
||||||
|
// Replace multi-line text
|
||||||
const endBlock = this.getBlock(end.key)
|
const endBlock = this.getBlock(end.key)
|
||||||
const { text } = endBlock
|
const { text } = endBlock
|
||||||
endBlock.text = text.substring(0, endOffset) + `` + text.substring(endOffset)
|
endBlock.text = text.substring(0, endOffset) + `` + text.substring(endOffset)
|
||||||
const offset = endOffset + 2
|
const offset = endOffset + 2
|
||||||
this.cursor = {
|
this.cursor = {
|
||||||
start: { key: end.key, offset },
|
start: { key: end.key, offset },
|
||||||
end: { key: end.key, offset: offset + title.length }
|
end: { key: end.key, offset: offset + title.length }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Replace single-line text
|
||||||
const imageTitle = startOffset !== endOffset ? text.substring(startOffset, endOffset) : title
|
const imageTitle = startOffset !== endOffset ? text.substring(startOffset, endOffset) : title
|
||||||
block.text = text.substring(0, start.offset) +
|
block.text = text.substring(0, start.offset) +
|
||||||
`` +
|
`` +
|
||||||
text.substring(end.offset)
|
text.substring(end.offset)
|
||||||
|
|
||||||
this.cursor = {
|
this.cursor = {
|
||||||
|
Loading…
Reference in New Issue
Block a user