Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markdown field support through SimpleMDE #2949

Merged
merged 1 commit into from
Dec 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/assets/javascripts/rails_admin/ra.widgets.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,31 @@ $(document).on 'rails_admin.dom_ready', (e, content) ->
html = html.add(option)
object_select.html(html)


# simplemde

goSimpleMDEs = ->
content.find('[data-richtext=simplemde]').not('.simplemded').each (index, domEle) ->
options = $(this).data('options')
instance_config = options.instance_config
new window.SimpleMDE($.extend(true, {
element: document.getElementById(this.id),
autosave: {
uniqueId: this.id
}
}, instance_config))
$(this).addClass('simplemded')

$editors = content.find('[data-richtext=simplemde]').not('.simplemded')
if $editors.length
if not window.SimpleMDE
options = $editors.first().data('options')
$('head').append('<link href="' + options['css_location'] + '" rel="stylesheet" media="all" type="text\/css">')
$.getScript options['js_location'], (script, textStatus, jqXHR) ->
goSimpleMDEs()
else
goSimpleMDEs()

# ckeditor

goCkeditors = ->
Expand Down
8 changes: 8 additions & 0 deletions app/views/rails_admin/main/_form_simple_mde.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:ruby
js_data = {
js_location: field.js_location,
css_location: field.css_location,
instance_config: field.instance_config
}

= form.text_area field.method_name, field.html_attributes.reverse_merge(data: { richtext: 'simplemde', options: js_data.to_json }).reverse_merge({ value: field.form_value })
1 change: 1 addition & 0 deletions lib/rails_admin/config/fields/types/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require 'rails_admin/config/fields/types/time'
require 'rails_admin/config/fields/types/timestamp'
require 'rails_admin/config/fields/types/color'
require 'rails_admin/config/fields/types/simple_mde'
require 'rails_admin/config/fields/types/ck_editor'
require 'rails_admin/config/fields/types/code_mirror'
require 'rails_admin/config/fields/types/wysihtml5'
Expand Down
33 changes: 33 additions & 0 deletions lib/rails_admin/config/fields/types/simple_mde.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'rails_admin/config/fields/base'

module RailsAdmin
module Config
module Fields
module Types
class SimpleMDE < RailsAdmin::Config::Fields::Types::Text
# Register field type for the type loader
RailsAdmin::Config::Fields::Types.register(self)

# If you want to have a different SimpleMDE config for each instance
# you can override this option with these values: https://github.com/sparksuite/simplemde-markdown-editor#configuration
register_instance_option :instance_config do
nil
end

# Use this if you want to point to a cloud instance of the base SimpleMDE
register_instance_option :js_location do
"#{Rails.application.config.assets.prefix}/simplemde.min.js"
end

register_instance_option :css_location do
"#{Rails.application.config.assets.prefix}/simplemde.min.css"
end

register_instance_option :partial do
:form_simple_mde
end
end
end
end
end
end
12 changes: 12 additions & 0 deletions spec/integration/config/edit/rails_admin_config_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,18 @@ class HelpTest < Tableless
end
end

describe 'SimpleMDE Support' do
it 'adds Javascript to enable SimpleMDE' do
RailsAdmin.config Draft do
edit do
field :notes, :simple_mde
end
end
visit new_path(model_name: 'draft')
is_expected.to have_selector('textarea#draft_notes[data-richtext="simplemde"]')
end
end

describe 'CKEditor Support' do
it 'adds Javascript to enable CKEditor' do
RailsAdmin.config Draft do
Expand Down
32 changes: 32 additions & 0 deletions spec/rails_admin/config/fields/types/simple_mde_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe RailsAdmin::Config::Fields::Types::SimpleMDE do
describe 'asset locations' do
before do
@custom_prefix = '/foo'
@default_prefix = Rails.application.config.assets.prefix
Rails.application.config.assets.prefix = @custom_prefix
RailsAdmin.config FieldTest do
field :text_field, :simple_mde
end
end

after do
Rails.application.config.assets.prefix = @default_prefix
end

it 'allows custom assets prefix for js' do
expect(
RailsAdmin.config(FieldTest).fields.detect { |f| f.name == :text_field }.with(object: FieldTest.new).js_location[0..(@custom_prefix.length - 1)],
).to eq @custom_prefix
end

it 'allows custom assets prefix for css' do
expect(
RailsAdmin.config(FieldTest).fields.detect { |f| f.name == :text_field }.with(object: FieldTest.new).css_location[0..(@custom_prefix.length - 1)],
).to eq @custom_prefix
end
end

it_behaves_like 'a generic field type', :text_field, :simple_mde
end