Skip to content

Commit

Permalink
Merge pull request #2163 from tvdeyen/fix-tinymce-init-for-ingredients
Browse files Browse the repository at this point in the history
(Re)-init Tinymce for elements with ingredients
  • Loading branch information
tvdeyen authored Aug 6, 2021
2 parents 08a6ce8 + df4e4a2 commit c841676
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/controllers/alchemy/admin/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def update
end

def destroy
@richtext_ids = @element.richtext_contents_ids + @element.richtext_ingredients_ids
@element.destroy
@notice = Alchemy.t("Successfully deleted element") % { element: @element.display_name }
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/alchemy/admin/elements/create.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

Alchemy.growl('<%= Alchemy.t(:successfully_added_element) %>');
Alchemy.closeCurrentDialog();
Alchemy.Tinymce.init(<%= @element.richtext_contents_ids.to_json %>);
Alchemy.Tinymce.init(<%= (@element.richtext_contents_ids + @element.richtext_ingredients_ids).to_json %>);
Alchemy.PreviewWindow.refresh(function() {
Alchemy.ElementEditors.focusElementPreview(<%= @element.id %>);
});
Expand Down
4 changes: 1 addition & 3 deletions app/views/alchemy/admin/elements/destroy.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ $('#element_<%= @element.id %>').hide(200, function() {
Alchemy.growl('<%= j @notice %>');
$('#element_area .sortable-elements').sortable('refresh');
Alchemy.PreviewWindow.refresh();
<% @element.richtext_contents_ids.each do |id| %>
tinymce.get('tinymce_<%= id %>').remove();
<% end %>
Alchemy.Tinymce.remove(<%= @richtext_ids.to_json %>);
<% if @element.fixed? %>
Alchemy.FixedElements.removeTab(<%= @element.id %>);
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/alchemy/admin/elements/fold.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

<% if @element.folded? -%>

Alchemy.Tinymce.remove(<%= @element.richtext_contents_ids.to_json %>);
Alchemy.Tinymce.remove(<%= (@element.richtext_contents_ids + @element.richtext_ingredients_ids).to_json %>);

<% else -%>

$el.trigger('FocusElementEditor.Alchemy');
Alchemy.Tinymce.init(<%= @element.richtext_contents_ids.to_json %>);
Alchemy.Tinymce.init(<%= (@element.richtext_contents_ids + @element.richtext_ingredients_ids).to_json %>);
Alchemy.GUI.initElement($el);
Alchemy.SortableElements(
<%= @page.id %>,
Expand Down
52 changes: 52 additions & 0 deletions spec/requests/alchemy/admin/elements_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Alchemy::Admin::ElementsController do
before do
authorize_user(:as_admin)
end

describe "#create" do
let(:page_version) { create(:alchemy_page_version) }

context "element with ingredients" do
it "inits Tinymce for richtext ingredients" do
post admin_elements_path(element: { page_version_id: page_version.id, name: "element_with_ingredients" }, format: :js)
element = Alchemy::Element.last
expect(response.body).to include("Alchemy.Tinymce.init([#{element.ingredient_by_role(:text).id}]);")
end
end
end

describe "#fold" do
context "expanded element with ingredients" do
let(:element) { create(:alchemy_element, :with_ingredients) }

it "removes Tinymce for richtext ingredients" do
post fold_admin_element_path(id: element.id, format: :js)
expect(response.body).to include("Alchemy.Tinymce.remove([#{element.ingredient_by_role(:text).id}]);")
end
end

context "folded element with ingredients" do
let(:element) { create(:alchemy_element, :with_ingredients, folded: true) }

it "inits Tinymce for richtext ingredients" do
post fold_admin_element_path(id: element.id, format: :js)
expect(response.body).to include("Alchemy.Tinymce.init([#{element.ingredient_by_role(:text).id}]);")
end
end
end

describe "#destroy" do
context "element with ingredients" do
let(:element) { create(:alchemy_element, :with_ingredients) }

it "removes Tinymce for richtext ingredients" do
delete admin_element_path(id: element.id, format: :js)
expect(response.body).to include("Alchemy.Tinymce.remove([#{element.ingredient_by_role(:text).id}]);")
end
end
end
end

0 comments on commit c841676

Please sign in to comment.