FEATURE: Automatically add http:// when adding links without them via composer

This commit is contained in:
Sam Saffron 2016-03-10 18:25:02 +11:00
parent aa001fcfc4
commit f3efe657fa
2 changed files with 21 additions and 3 deletions

View File

@ -516,18 +516,26 @@ export default Ember.Component.extend({
const link = this.get('link');
const sel = this._lastSel;
const autoHttp = function(l){
if (l.indexOf("://") === -1) {
return "http://" + l;
} else {
return l;
}
};
if (Ember.isEmpty(link)) { return; }
const m = / "([^"]+)"/.exec(link);
if (m && m.length === 2) {
const description = m[1];
const remaining = link.replace(m[0], '');
this._addText(sel, `[${description}](${remaining})`);
this._addText(sel, `[${description}](${autoHttp(remaining)})`);
} else {
if (sel.value) {
this._addText(sel, `[${sel.value}](${link})`);
this._addText(sel, `[${sel.value}](${autoHttp(link)})`);
} else {
const desc = I18n.t('composer.link_description');
this._addText(sel, `[${desc}](${link})`);
this._addText(sel, `[${desc}](${autoHttp(link)})`);
this._selectText(sel.start + 1, desc.length);
}
}

View File

@ -207,6 +207,16 @@ testCase('link modal (simple link)', function(assert, textarea) {
});
});
testCase('link modal auto http addition', function(assert) {
click('button.link');
fillIn('.insert-link input', 'sam.com');
click('.insert-link button.btn-primary');
const desc = I18n.t('composer.link_description');
andThen(() => {
assert.equal(this.get('value'), `hello world.[${desc}](http://sam.com)`);
});
});
testCase('link modal (simple link) with selected text', function(assert, textarea) {
textarea.selectionStart = 0;
textarea.selectionEnd = 12;