From f7b237828e3eaf76d64ee14c7d2930ff3718534a Mon Sep 17 00:00:00 2001 From: Rex Su Date: Tue, 24 Oct 2017 15:10:54 -0400 Subject: [PATCH] Added SimpleMDE field and specs. --- .../javascripts/rails_admin/ra.widgets.coffee | 25 ++++++++++++++ .../rails_admin/main/_form_simple_mde.haml | 8 +++++ lib/rails_admin/config/fields/types/all.rb | 1 + .../config/fields/types/simple_mde.rb | 33 +++++++++++++++++++ .../edit/rails_admin_config_edit_spec.rb | 12 +++++++ .../config/fields/types/simple_mde_spec.rb | 32 ++++++++++++++++++ 6 files changed, 111 insertions(+) create mode 100644 app/views/rails_admin/main/_form_simple_mde.haml create mode 100644 lib/rails_admin/config/fields/types/simple_mde.rb create mode 100644 spec/rails_admin/config/fields/types/simple_mde_spec.rb diff --git a/app/assets/javascripts/rails_admin/ra.widgets.coffee b/app/assets/javascripts/rails_admin/ra.widgets.coffee index 4cc0962fda..40d2c626bc 100644 --- a/app/assets/javascripts/rails_admin/ra.widgets.coffee +++ b/app/assets/javascripts/rails_admin/ra.widgets.coffee @@ -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('') + $.getScript options['js_location'], (script, textStatus, jqXHR) -> + goSimpleMDEs() + else + goSimpleMDEs() + # ckeditor goCkeditors = -> diff --git a/app/views/rails_admin/main/_form_simple_mde.haml b/app/views/rails_admin/main/_form_simple_mde.haml new file mode 100644 index 0000000000..99cd82d98c --- /dev/null +++ b/app/views/rails_admin/main/_form_simple_mde.haml @@ -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 }) diff --git a/lib/rails_admin/config/fields/types/all.rb b/lib/rails_admin/config/fields/types/all.rb index 0099d8a142..daab8072f8 100644 --- a/lib/rails_admin/config/fields/types/all.rb +++ b/lib/rails_admin/config/fields/types/all.rb @@ -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' diff --git a/lib/rails_admin/config/fields/types/simple_mde.rb b/lib/rails_admin/config/fields/types/simple_mde.rb new file mode 100644 index 0000000000..3d2fa726c7 --- /dev/null +++ b/lib/rails_admin/config/fields/types/simple_mde.rb @@ -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 diff --git a/spec/integration/config/edit/rails_admin_config_edit_spec.rb b/spec/integration/config/edit/rails_admin_config_edit_spec.rb index 935a8ceb2e..21891bae99 100644 --- a/spec/integration/config/edit/rails_admin_config_edit_spec.rb +++ b/spec/integration/config/edit/rails_admin_config_edit_spec.rb @@ -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 diff --git a/spec/rails_admin/config/fields/types/simple_mde_spec.rb b/spec/rails_admin/config/fields/types/simple_mde_spec.rb new file mode 100644 index 0000000000..3d39539991 --- /dev/null +++ b/spec/rails_admin/config/fields/types/simple_mde_spec.rb @@ -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