0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05: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 fa5a06a27c
commit 2f10b0fb64

View file

@ -3,7 +3,7 @@ import computed from 'ember-computed';
import run from 'ember-runloop'; import run from 'ember-runloop';
import $ from 'jquery'; import $ from 'jquery';
import layout from '../templates/components/koenig-toolbar'; import layout from '../templates/components/koenig-toolbar';
import cajaSanitizers from '../lib/caja-sanitizers';
import Tools from '../options/default-tools'; import Tools from '../options/default-tools';
export default Component.extend({ export default Component.extend({
@ -13,6 +13,7 @@ export default Component.extend({
isVisible: false, isVisible: false,
tools: [], tools: [],
hasRendered: false, hasRendered: false,
activeTags: null,
isLink: computed({ isLink: computed({
get() { get() {
return this._isLink; return this._isLink;
@ -90,10 +91,14 @@ export default Component.extend({
linkKeyPress(event) { linkKeyPress(event) {
// if enter run link // if enter run link
if (event.keyCode === 13) { if (event.keyCode === 13) {
let url = event.target.value;
if (!cajaSanitizers.url(url)) {
url = `http://${url}`;
}
this.send('closeLink'); this.send('closeLink');
this.set('isVisible', false); this.set('isVisible', false);
this.editor.run((postEditor) => { 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); postEditor.addMarkupToRange(this.get('linkRange'), markup);
}); });
@ -102,6 +107,17 @@ export default Component.extend({
} }
}, },
doLink(range) { 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('isLink', true);
this.set('linkRange', range); this.set('linkRange', range);
run.schedule('afterRender', this, run.schedule('afterRender', this,
@ -126,6 +142,11 @@ function updateToolbarToRange(self, $holder, $editor, isMouseDown) {
} }
return; 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('toolbar');
self.propertyWillChange('toolbarBlocks'); self.propertyWillChange('toolbarBlocks');