1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00

🚀 Link improvements (#601)

no issue
- Adds a few improvements for link insertion.
  - Sanitises links
  - Toggles a link so that if there are existing links in the selected text it removes them.
This commit is contained in:
Ryan McCarvill 2017-03-24 22:40:21 +13:00 committed by Kevin Ansfield
parent 517111ea55
commit fade388899

View file

@ -3,7 +3,7 @@ import computed from 'ember-computed';
import run from 'ember-runloop';
import $ from 'jquery';
import layout from '../templates/components/koenig-toolbar';
import cajaSanitizers from '../lib/caja-sanitizers';
import Tools from '../options/default-tools';
export default Component.extend({
@ -13,6 +13,7 @@ export default Component.extend({
isVisible: false,
tools: [],
hasRendered: false,
activeTags: null,
isLink: computed({
get() {
return this._isLink;
@ -90,10 +91,14 @@ export default Component.extend({
linkKeyPress(event) {
// if enter run link
if (event.keyCode === 13) {
let url = event.target.value;
if (!cajaSanitizers.url(url)) {
url = `http://${url}`;
}
this.send('closeLink');
this.set('isVisible', false);
this.editor.run((postEditor) => {
let markup = postEditor.builder.createMarkup('a', {href: event.target.value});
let markup = postEditor.builder.createMarkup('a', {href: url});
postEditor.addMarkupToRange(this.get('linkRange'), markup);
});
@ -102,6 +107,17 @@ export default Component.extend({
}
},
doLink(range) {
// if a link is already selected then we remove the links from within the range.
let currentLinks = this.get('activeTags').filter((element) => element.tagName === 'a');
if (currentLinks.length) {
this.get('editor').run((postEditor) => {
currentLinks.forEach((link) => {
postEditor.removeMarkupFromRange(range, link);
});
});
return;
}
this.set('isLink', true);
this.set('linkRange', range);
run.schedule('afterRender', this,
@ -126,6 +142,11 @@ function updateToolbarToRange(self, $holder, $editor, isMouseDown) {
}
return;
}
// set the active markups and sections
let sectionTagName = editor.activeSection.tagName === 'li' ? editor.activeSection.parent.tagName : editor.activeSection.tagName;
self.set('activeTags', editor.activeMarkups.concat([{tagName: sectionTagName}]));
self.propertyWillChange('toolbar');
self.propertyWillChange('toolbarBlocks');