GetmeUK / ContentTools

A JS library for building WYSIWYG editors for HTML content.
http://getcontenttools.com
MIT License
3.95k stars 395 forks source link

How to delete only selected text instead of deleting whole region #422

Closed mallikarjuna909 closed 7 years ago

mallikarjuna909 commented 7 years ago

Hi, When i selected text, if click on Remove( Delete) icon then whole editable region is removing but i want to delete only selected text in that region. And I've gone through the code. In switch case, for element.type() == 'Text' removing whole region like element.parent().detach(element); . So, in Switch-case we can handle for 'Text' as well.

Thanks.

anthonyjb commented 7 years ago

Hi @mallikarjuna909,

So you'd need to modify the tool so that instead of detaching the element it removes the current selection from it's content string, like so:

tip = element.content.substring(0, selection.get()[0])
tail = element.content.substring(selection.get()[1])
element.content = tip.concat(tail.content)
elemenrt.updateInnerHTML()
mallikarjuna909 commented 7 years ago

Hi @anthonyjb , Thanks for your help. Your snippet working like charm after removing typos. element.content = tip.concat(tail); element.updateInnerHTML() .

That's cool and focus is not presenting at current position rather it is going to next or previous elements. So i've made some changes to get it work.

var from, to, _ref, cursor; _ref = selection.get(), from = _ref[0], to = _ref[1], cursor = from; / Remove selected text from element if there is selected text / if (from != to) { tip = element.content.substring(0, selection.get()[0]); tail = element.content.substring(selection.get()[1]); element.content = tip.concat(tail); element.updateInnerHTML(); element.taint(); selection.set(cursor, cursor); element.selection(selection); element.focus(); } else { / Remove whole element if there is no selected region / element.parent().detach(element); }