|
|
@ -30,10 +30,7 @@ const actions = { |
|
|
style: 'font-size:1.4rem;padding:0 4px 2px;', |
|
|
style: 'font-size:1.4rem;padding:0 4px 2px;', |
|
|
title: 'Link', |
|
|
title: 'Link', |
|
|
}, |
|
|
}, |
|
|
result: (rte) => { |
|
|
result: (rte) => rte.insertHTML(`<a class="link" href="">${rte.selection()}</a>`) |
|
|
const url = window.prompt('Enter the link URL') |
|
|
|
|
|
if (url) rte.exec('createLink', url)//class="link"
|
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -148,7 +145,7 @@ export default class RichTextEditor { |
|
|
* @return {Selection} |
|
|
* @return {Selection} |
|
|
*/ |
|
|
*/ |
|
|
selection() { |
|
|
selection() { |
|
|
this.doc.getSelection() |
|
|
return this.doc.getSelection() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -174,21 +171,23 @@ export default class RichTextEditor { |
|
|
* @param {string} value HTML string |
|
|
* @param {string} value HTML string |
|
|
*/ |
|
|
*/ |
|
|
insertHTML(value) { |
|
|
insertHTML(value) { |
|
|
|
|
|
let lastNode; |
|
|
const doc = this.doc; |
|
|
const doc = this.doc; |
|
|
const sel = doc.getSelection(); |
|
|
const sel = doc.getSelection(); |
|
|
|
|
|
|
|
|
if (sel && sel.rangeCount) { |
|
|
if (sel && sel.rangeCount) { |
|
|
const node = doc.createElement('div'); |
|
|
const node = doc.createElement('div'); |
|
|
const range = sel.getRangeAt(0); |
|
|
const range = sel.getRangeAt(0); |
|
|
range.collapse(true); |
|
|
range.deleteContents(); |
|
|
node.outerHTML = value.replace('${content}', sel); |
|
|
node.innerHTML = value; |
|
|
range.insertNode(node); |
|
|
Array.prototype.slice.call(node.childNodes).forEach(nd => { |
|
|
|
|
|
range.insertNode(nd); |
|
|
|
|
|
lastNode = nd; |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
// Move the caret immediately after the inserted node
|
|
|
|
|
|
range.setStartAfter(node); |
|
|
|
|
|
range.collapse(true); |
|
|
|
|
|
sel.removeAllRanges(); |
|
|
sel.removeAllRanges(); |
|
|
sel.addRange(range); |
|
|
sel.addRange(range); |
|
|
|
|
|
this.el.focus(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|