Merge pull request #5904 from Raven24/JesseWeinstein-patch-1
prefill bookmarklet with markdown-formatted content
This commit is contained in:
commit
2570d88cc3
8 changed files with 138 additions and 52 deletions
|
|
@ -6,6 +6,7 @@ This release recommends using Ruby 2.2, while retaining Ruby 2.1 as an officiall
|
||||||
Ruby 2.0 is no longer officially supported.
|
Ruby 2.0 is no longer officially supported.
|
||||||
|
|
||||||
## Refactor
|
## Refactor
|
||||||
|
* Improve bookmarklet [#5904](https://github.com/diaspora/diaspora/pull/5904)
|
||||||
|
|
||||||
## Bug fixes
|
## Bug fixes
|
||||||
* Destroy Participation when removing interactions with a post [#5852](https://github.com/diaspora/diaspora/pull/5852)
|
* Destroy Participation when removing interactions with a post [#5852](https://github.com/diaspora/diaspora/pull/5852)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
|
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
|
||||||
|
|
||||||
app.views.Bookmarklet = Backbone.View.extend({
|
app.views.Bookmarklet = Backbone.View.extend({
|
||||||
separator: ' - ',
|
separator: "\n\n",
|
||||||
|
|
||||||
initialize: function(opts) {
|
initialize: function(opts) {
|
||||||
// init a standalone publisher
|
// init a standalone publisher
|
||||||
|
|
@ -25,8 +25,12 @@ app.views.Bookmarklet = Backbone.View.extend({
|
||||||
var p = this.param_contents;
|
var p = this.param_contents;
|
||||||
if( p.content ) return p.content;
|
if( p.content ) return p.content;
|
||||||
|
|
||||||
var contents = p.title + this.separator + p.url;
|
var contents = "### " + p.title + this.separator;
|
||||||
if( p.notes ) contents += this.separator + p.notes;
|
if( p.notes ) {
|
||||||
|
var notes = p.notes.toString().replace(/(?:\r\n|\r|\n)/g, "\n> ");
|
||||||
|
contents += "> " + notes + this.separator;
|
||||||
|
}
|
||||||
|
contents += p.url;
|
||||||
return contents;
|
return contents;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
45
app/assets/javascripts/bookmarklet.js
Normal file
45
app/assets/javascripts/bookmarklet.js
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
|
||||||
|
|
||||||
|
var bookmarklet = function(url, width, height, opts) {
|
||||||
|
// calculate popup dimensions & placement
|
||||||
|
var dim = function() {
|
||||||
|
var w = window,
|
||||||
|
winTop = (w.screenTop ? w.screenTop : w.screenY),
|
||||||
|
winLeft = (w.screenLeft ? w.screenLeft : w.screenX),
|
||||||
|
top = (winTop + (w.innerHeight / 2) - (height / 2)),
|
||||||
|
left = (winLeft + (w.innerWidth / 2) - (width / 2));
|
||||||
|
return "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left;
|
||||||
|
};
|
||||||
|
|
||||||
|
// prepare url parameters
|
||||||
|
var params = function() {
|
||||||
|
var w = window,
|
||||||
|
d = document,
|
||||||
|
href = w.location.href,
|
||||||
|
title = d.title,
|
||||||
|
sel = w.getSelection ? w.getSelection() :
|
||||||
|
d.getSelection ? d.getSelection() :
|
||||||
|
d.selection.createRange().text,
|
||||||
|
notes = sel.toString();
|
||||||
|
return "url=" + encodeURIComponent(href) +
|
||||||
|
"&title=" + encodeURIComponent(title) +
|
||||||
|
"¬es=" + encodeURIComponent(notes);
|
||||||
|
};
|
||||||
|
|
||||||
|
// popup (or redirect) action
|
||||||
|
var act = function() {
|
||||||
|
var popupOpts = (opts || "location=yes,links=no,scrollbars=yes,toolbar=no"),
|
||||||
|
jumpUrl = url + "?jump=yes";
|
||||||
|
|
||||||
|
(window.open(url + "?" + params(), "diaspora_bookmarklet", popupOpts + "," + dim()) ||
|
||||||
|
(window.location.href = jumpUrl + "&" + params()));
|
||||||
|
};
|
||||||
|
|
||||||
|
if( /Firefox/.test(navigator.userAgent) ) {
|
||||||
|
setTimeout(act, 0);
|
||||||
|
} else {
|
||||||
|
act();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// @license-end
|
||||||
|
|
@ -25,8 +25,10 @@ module ApplicationHelper
|
||||||
timeago_tag(time, options.merge(:class => 'timeago', :title => time.iso8601, :force => true)) if time
|
timeago_tag(time, options.merge(:class => 'timeago', :title => time.iso8601, :force => true)) if time
|
||||||
end
|
end
|
||||||
|
|
||||||
def bookmarklet_url( height = 400, width = 620)
|
def bookmarklet_code(height=400, width=620)
|
||||||
"javascript:(function(){f='#{AppConfig.pod_uri.to_s}bookmarklet?url='+encodeURIComponent(window.location.href)+'&title='+encodeURIComponent(document.title)+'¬es='+encodeURIComponent(''+(window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text))+'&v=1&';a=function(){if(!window.open(f+'noui=1&jump=doclose','diasporav1','location=yes,links=no,scrollbars=yes,toolbar=no,width=#{width},height=#{height}'))location.href=f+'jump=yes'};if(/Firefox/.test(navigator.userAgent)){setTimeout(a,0)}else{a()}})()"
|
"javascript:" +
|
||||||
|
BookmarkletRenderer.body +
|
||||||
|
"bookmarklet('#{bookmarklet_url}', #{width}, #{height});"
|
||||||
end
|
end
|
||||||
|
|
||||||
def contacts_link
|
def contacts_link
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@
|
||||||
%i.entypo.bookmark
|
%i.entypo.bookmark
|
||||||
= t('bookmarklet.heading')
|
= t('bookmarklet.heading')
|
||||||
.content
|
.content
|
||||||
!= t('bookmarklet.explanation', :link => link_to(t('bookmarklet.post_something'), bookmarklet_url))
|
!= t('bookmarklet.explanation', :link => link_to(t('bookmarklet.post_something'), bookmarklet_code))
|
||||||
|
|
||||||
- if AppConfig.settings.paypal_donations.enable? || AppConfig.bitcoin_donation_address
|
- if AppConfig.settings.paypal_donations.enable? || AppConfig.bitcoin_donation_address
|
||||||
.section
|
.section
|
||||||
|
|
|
||||||
27
lib/bookmarklet_renderer.rb
Normal file
27
lib/bookmarklet_renderer.rb
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
|
||||||
|
class BookmarkletRenderer
|
||||||
|
class << self
|
||||||
|
def cached_name
|
||||||
|
@cached ||= File.join(Rails.application.config.paths["tmp"].first, "cache", "bookmarklet.cached")
|
||||||
|
end
|
||||||
|
|
||||||
|
def source_name
|
||||||
|
@source ||= Rails.application.assets["bookmarklet.js"].pathname.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def body
|
||||||
|
if !File.exist?(cached_name) && Rails.env.production?
|
||||||
|
raise "please run the Rake task to compile the bookmarklet: `bundle exec rake assets:uglify_bookmarklet`"
|
||||||
|
end
|
||||||
|
|
||||||
|
compile unless Rails.env.production? # don't make me re-run rake in development
|
||||||
|
@body ||= File.read(cached_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def compile
|
||||||
|
src = File.read(source_name)
|
||||||
|
@body = Uglifier.compile(src)
|
||||||
|
File.open(cached_name, "w") {|f| f.write(@body) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
47
lib/error_page_renderer.rb
Normal file
47
lib/error_page_renderer.rb
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
# Inspired by https://github.com/route/errgent/blob/master/lib/errgent/renderer.rb
|
||||||
|
class ErrorPageRenderer
|
||||||
|
def initialize options={}
|
||||||
|
@codes = options.fetch :codes, [404, 500]
|
||||||
|
@output = options.fetch :output, "public/%s.html"
|
||||||
|
@vars = options.fetch :vars, {}
|
||||||
|
@template = options.fetch :template, "errors/error_%s"
|
||||||
|
@layout = options.fetch :layout, "layouts/error_page"
|
||||||
|
end
|
||||||
|
|
||||||
|
def render
|
||||||
|
@codes.each do |code|
|
||||||
|
view = build_action_view
|
||||||
|
view.assign @vars.merge(code: code)
|
||||||
|
path = Rails.root.join(@output % code)
|
||||||
|
File.write path, view.render(template: @template % code, layout: @layout)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def helpers(&block)
|
||||||
|
@helpers = block
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def build_action_view
|
||||||
|
paths = ::ActionController::Base.view_paths
|
||||||
|
::ActionView::Base.new(paths).tap do |view|
|
||||||
|
view.class_eval do
|
||||||
|
include Rails.application.helpers
|
||||||
|
include Rails.application.routes.url_helpers
|
||||||
|
end
|
||||||
|
view.assets_manifest = build_manifest(Rails.application)
|
||||||
|
view.class_eval(&@helpers) if @helpers
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Internal API from the sprocket-rails railtie, if somebody finds a way to
|
||||||
|
# call it, please replace it. Might need to be updated on sprocket-rails
|
||||||
|
# updates.
|
||||||
|
def build_manifest(app)
|
||||||
|
config = app.config
|
||||||
|
path = File.join(config.paths['public'].first, config.assets.prefix)
|
||||||
|
Sprockets::Manifest.new(app.assets, path, config.assets.manifest)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -1,49 +1,3 @@
|
||||||
# Inspired by https://github.com/route/errgent/blob/master/lib/errgent/renderer.rb
|
|
||||||
class ErrorPageRenderer
|
|
||||||
def initialize options={}
|
|
||||||
@codes = options.fetch :codes, [404, 500]
|
|
||||||
@output = options.fetch :output, "public/%s.html"
|
|
||||||
@vars = options.fetch :vars, {}
|
|
||||||
@template = options.fetch :template, "errors/error_%s"
|
|
||||||
@layout = options.fetch :layout, "layouts/error_page"
|
|
||||||
end
|
|
||||||
|
|
||||||
def render
|
|
||||||
@codes.each do |code|
|
|
||||||
view = build_action_view
|
|
||||||
view.assign @vars.merge(code: code)
|
|
||||||
path = Rails.root.join(@output % code)
|
|
||||||
File.write path, view.render(template: @template % code, layout: @layout)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def helpers(&block)
|
|
||||||
@helpers = block
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def build_action_view
|
|
||||||
paths = ::ActionController::Base.view_paths
|
|
||||||
::ActionView::Base.new(paths).tap do |view|
|
|
||||||
view.class_eval do
|
|
||||||
include Rails.application.helpers
|
|
||||||
include Rails.application.routes.url_helpers
|
|
||||||
end
|
|
||||||
view.assets_manifest = build_manifest(Rails.application)
|
|
||||||
view.class_eval(&@helpers) if @helpers
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Internal API from the sprocket-rails railtie, if somebody finds a way to
|
|
||||||
# call it, please replace it. Might need to be updated on sprocket-rails
|
|
||||||
# updates.
|
|
||||||
def build_manifest(app)
|
|
||||||
config = app.config
|
|
||||||
path = File.join(config.paths['public'].first, config.assets.prefix)
|
|
||||||
Sprockets::Manifest.new(app.assets, path, config.assets.manifest)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
namespace :assets do
|
namespace :assets do
|
||||||
desc "Generate error pages"
|
desc "Generate error pages"
|
||||||
|
|
@ -52,8 +6,14 @@ namespace :assets do
|
||||||
renderer.render
|
renderer.render
|
||||||
end
|
end
|
||||||
|
|
||||||
|
desc "Uglify bookmarklet snippet"
|
||||||
|
task :uglify_bookmarklet => :environment do
|
||||||
|
BookmarkletRenderer.compile
|
||||||
|
end
|
||||||
|
|
||||||
# Augment precompile with error page generation
|
# Augment precompile with error page generation
|
||||||
task :precompile do
|
task :precompile do
|
||||||
Rake::Task['assets:generate_error_pages'].invoke
|
Rake::Task['assets:generate_error_pages'].invoke
|
||||||
|
Rake::Task['assets:uglify_bookmarklet'].invoke
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue