LinkPreview: restructure to make it easier to keep old link data
This commit is contained in:
parent
73f28ffabe
commit
acdb9ac1e5
@ -62,82 +62,86 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
var LinkPreview = {
|
||||||
* Find URL links from the source text that may be interesting.
|
links: [],
|
||||||
*
|
|
||||||
* @param {String} text
|
|
||||||
* @return {Array} list of URLs
|
|
||||||
*/
|
|
||||||
function findLinks(text)
|
|
||||||
{
|
|
||||||
// @fixme match this to core code
|
|
||||||
var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
|
|
||||||
var links = [];
|
|
||||||
var matches;
|
|
||||||
while ((matches = re.exec(text)) !== null) {
|
|
||||||
links.push(matches[1]);
|
|
||||||
}
|
|
||||||
return links;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start looking up info for a link preview...
|
* Find URL links from the source text that may be interesting.
|
||||||
* May start async data loads.
|
*
|
||||||
*
|
* @param {String} text
|
||||||
* @param {String} id
|
* @return {Array} list of URLs
|
||||||
* @param {String} url
|
*/
|
||||||
*/
|
findLinks: function (text)
|
||||||
function prepLinkPreview(id, url)
|
{
|
||||||
{
|
// @fixme match this to core code
|
||||||
oEmbed.lookup(url, function(data) {
|
var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/mg;
|
||||||
var thumb = null;
|
var links = [];
|
||||||
var width = 100;
|
var matches;
|
||||||
if (typeof data.thumbnail_url == "string") {
|
while ((matches = re.exec(text)) !== null) {
|
||||||
thumb = data.thumbnail_url;
|
links.push(matches[1]);
|
||||||
if (typeof data.thumbnail_width !== "undefined") {
|
}
|
||||||
if (data.thumbnail_width < width) {
|
return links;
|
||||||
width = data.thumbnail_width;
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start looking up info for a link preview...
|
||||||
|
* May start async data loads.
|
||||||
|
*
|
||||||
|
* @param {String} id
|
||||||
|
* @param {String} url
|
||||||
|
*/
|
||||||
|
prepLinkPreview: function(id, url)
|
||||||
|
{
|
||||||
|
oEmbed.lookup(url, function(data) {
|
||||||
|
var thumb = null;
|
||||||
|
var width = 100;
|
||||||
|
if (typeof data.thumbnail_url == "string") {
|
||||||
|
thumb = data.thumbnail_url;
|
||||||
|
if (typeof data.thumbnail_width !== "undefined") {
|
||||||
|
if (data.thumbnail_width < width) {
|
||||||
|
width = data.thumbnail_width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (data.type == 'photo' && typeof data.url == "string") {
|
||||||
|
thumb = data.url;
|
||||||
|
if (typeof data.width !== "undefined") {
|
||||||
|
if (data.width < width) {
|
||||||
|
width = data.width;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (data.type == 'photo' && typeof data.url == "string") {
|
if (thumb) {
|
||||||
thumb = data.url;
|
var link = $('<span class="inline-attachment"><a><img/></a></span>');
|
||||||
if (typeof data.width !== "undefined") {
|
link.find('a')
|
||||||
if (data.width < width) {
|
.attr('href', url)
|
||||||
width = data.width;
|
.attr('target', '_blank')
|
||||||
}
|
.last()
|
||||||
|
.find('img')
|
||||||
|
.attr('src', thumb)
|
||||||
|
.attr('width', width)
|
||||||
|
.attr('title', data.title || data.url || url);
|
||||||
|
$('#' + id).append(link);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
if (thumb) {
|
},
|
||||||
var link = $('<span class="inline-attachment"><a><img/></a></span>');
|
|
||||||
link.find('a')
|
|
||||||
.attr('href', url)
|
|
||||||
.attr('target', '_blank')
|
|
||||||
.last()
|
|
||||||
.find('img')
|
|
||||||
.attr('src', thumb)
|
|
||||||
.attr('width', width)
|
|
||||||
.attr('title', data.title || data.url || url);
|
|
||||||
$('#' + id).append(link);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the live preview section with links found in the given text.
|
* Update the live preview section with links found in the given text.
|
||||||
* May start async data loads.
|
* May start async data loads.
|
||||||
*
|
*
|
||||||
* @param {String} text: free-form input text
|
* @param {String} text: free-form input text
|
||||||
*/
|
*/
|
||||||
function previewLinks(text)
|
previewLinks: function(text)
|
||||||
{
|
{
|
||||||
var links = findLinks(text);
|
var links = LinkPreview.findLinks(text);
|
||||||
$('#link-preview').html('');
|
$('#link-preview').html('');
|
||||||
for (var i = 0; i < links.length; i++) {
|
for (var i = 0; i < links.length; i++) {
|
||||||
var id = 'link-preview-' + i;
|
var id = 'link-preview-' + i;
|
||||||
$('#link-preview').append('<span id="' + id + '"></span>');
|
$('#link-preview').append('<span id="' + id + '"></span>');
|
||||||
prepLinkPreview(id, links[i]);
|
LinkPreview.prepLinkPreview(id, links[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
SN.Init.LinkPreview = function(params) {
|
SN.Init.LinkPreview = function(params) {
|
||||||
if (params.api) oEmbed.api = params.api;
|
if (params.api) oEmbed.api = params.api;
|
||||||
@ -149,7 +153,7 @@
|
|||||||
// Piggyback on the counter update...
|
// Piggyback on the counter update...
|
||||||
var origCounter = SN.U.Counter;
|
var origCounter = SN.U.Counter;
|
||||||
SN.U.Counter = function(form) {
|
SN.U.Counter = function(form) {
|
||||||
previewLinks($('#notice_data-text').val());
|
LinkPreview.previewLinks($('#notice_data-text').val());
|
||||||
return origCounter(form);
|
return origCounter(form);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user