Skip to content

Commit

Permalink
fix: 修复a标签包裹img的 取消链接 问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqiaoling01 committed Jun 10, 2021
1 parent 4ed9c87 commit 23eb557
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 4 additions & 1 deletion examples/link.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<p>
wangEditor demo
</p>
<button onclick="insertLinkImg()">插入带有超链接的图片</button>
<div id="div1">
<p><br/></p>
<p><br/></p>
Expand Down Expand Up @@ -41,7 +42,9 @@
<script>
const E = window.wangEditor
const editor = new E('#div1')

function insertLinkImg () {
editor.cmd.do( 'insertHTML', '<a href="https://www.baidu.com/"><img src="https://user-images.githubusercontent.com/37204782/119621908-3ebe3c00-be39-11eb-924d-82308460d461.png" style="max-width:100%;"/></a>' )
        }
editor.create()
</script>
</body>
Expand Down
21 changes: 18 additions & 3 deletions src/menus/link/bind-event/tooltip-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,24 @@ function createShowHideFn(editor: Editor) {
editor.selection.createRangeByElem($link)
editor.selection.restoreSelection()

// 用文字,替换链接
const selectionText = $link.text()
editor.cmd.do('insertHTML', '<span>' + selectionText + '</span>')
const $childNodes = $link.childNodes()
// 如果链接是图片
if ($childNodes?.getNodeName() === 'IMG') {
// 获取选中的图片
const $selectIMG = editor.selection.getSelectionContainerElem()?.children()
?.elems[0].children[0]
// 插入图片
editor.cmd.do(
'insertHTML',
`<img
src=${$selectIMG?.getAttribute('src')}
style=${$selectIMG?.getAttribute('style')}>`
)
} else {
// 用文字,替换链接
const selectionText = $link.text()
editor.cmd.do('insertHTML', '<span>' + selectionText + '</span>')
}

// 返回 true,表示执行完之后,隐藏 tooltip。否则不隐藏。
return true
Expand Down

0 comments on commit 23eb557

Please sign in to comment.