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:
parent
fa5a06a27c
commit
2f10b0fb64
1 changed files with 23 additions and 2 deletions
|
@ -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');
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue