From 3b2991dba1008878189a20a85d31d450a6d329e8 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Thu, 12 Oct 2017 02:27:27 +0200 Subject: [PATCH] Update insertHTML in RichTextEditor --- src/rich_text_editor/index.js | 8 -------- src/rich_text_editor/model/RichTextEditor.js | 21 ++++++++++---------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/rich_text_editor/index.js b/src/rich_text_editor/index.js index 08267d12d..041477472 100644 --- a/src/rich_text_editor/index.js +++ b/src/rich_text_editor/index.js @@ -68,14 +68,6 @@ module.exports = () => { //Avoid closing on toolbar clicking on(toolbar, 'mousedown', e => e.stopPropagation()); - - /* - commands = new CommandButtons(c.commands); - toolbar = new CommandButtonsView({ - collection: commands, - config: c, - }); - */ return this; }, diff --git a/src/rich_text_editor/model/RichTextEditor.js b/src/rich_text_editor/model/RichTextEditor.js index e17df20fb..00e816cb4 100644 --- a/src/rich_text_editor/model/RichTextEditor.js +++ b/src/rich_text_editor/model/RichTextEditor.js @@ -30,10 +30,7 @@ const actions = { style: 'font-size:1.4rem;padding:0 4px 2px;', title: 'Link', }, - result: (rte) => { - const url = window.prompt('Enter the link URL') - if (url) rte.exec('createLink', url)//class="link" - } + result: (rte) => rte.insertHTML(`${rte.selection()}`) }, } @@ -148,7 +145,7 @@ export default class RichTextEditor { * @return {Selection} */ selection() { - this.doc.getSelection() + return this.doc.getSelection() } /** @@ -174,21 +171,23 @@ export default class RichTextEditor { * @param {string} value HTML string */ insertHTML(value) { + let lastNode; const doc = this.doc; const sel = doc.getSelection(); if (sel && sel.rangeCount) { const node = doc.createElement('div'); const range = sel.getRangeAt(0); - range.collapse(true); - node.outerHTML = value.replace('${content}', sel); - range.insertNode(node); + range.deleteContents(); + node.innerHTML = value; + 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.addRange(range); + this.el.focus(); } } }