diff --git a/app/models/editable_element.rb b/app/models/editable_element.rb index f0c6d2a2..0111ad89 100644 --- a/app/models/editable_element.rb +++ b/app/models/editable_element.rb @@ -3,6 +3,7 @@ class EditableElement include Mongoid::Document ## fields ## + field :kind field :slug field :block field :content @@ -23,4 +24,8 @@ class EditableElement self.read_attribute(:content).blank? ? self.default_content : self.read_attribute(:content) end + def short_text?; self.kind == 'ShortText'; end + + def long_text?; self.kind == 'LongText'; end + end \ No newline at end of file diff --git a/app/models/extensions/page/parse.rb b/app/models/extensions/page/parse.rb index b1135007..f95c8073 100644 --- a/app/models/extensions/page/parse.rb +++ b/app/models/extensions/page/parse.rb @@ -101,7 +101,7 @@ module Models # puts "*** [#{self.fullpath}] _update_direct_template_descendants" direct_descendants = template_descendants.select do |page| # puts "*** \t\t[#{self.fullpath}] _update_direct_template_descendants (#{page.template_dependencies.inspect})" - ((page.template_dependencies || [])- (self.template_dependencies || [])).size == 1 + ((page.template_dependencies || []) - (self.template_dependencies || [])).size == 1 end # puts "*** [#{self.fullpath}] direct = #{direct_descendants.inspect}" diff --git a/app/views/admin/pages/_editable_elements.html.haml b/app/views/admin/pages/_editable_elements.html.haml index bd4d8214..5d5889f9 100644 --- a/app/views/admin/pages/_editable_elements.html.haml +++ b/app/views/admin/pages/_editable_elements.html.haml @@ -17,4 +17,7 @@ %ol - elements.each_with_index do |el, index| = f.fields_for 'editable_elements', el, :child_index => el._index do |g| - = g.input :content, :label => el.slug.humanize, :hint => el.hint \ No newline at end of file + - if el.short_text? + = g.input :content, :label => el.slug.humanize, :hint => el.hint + - elsif el.long_text? + = g.input :content, :label => el.slug.humanize, :hint => el.hint, :as => :text, :input_html => { :class => 'html' } \ No newline at end of file diff --git a/app/views/admin/pages/_form.html.haml b/app/views/admin/pages/_form.html.haml index a90c99a8..584e4499 100644 --- a/app/views/admin/pages/_form.html.haml +++ b/app/views/admin/pages/_form.html.haml @@ -1,5 +1,5 @@ - content_for :head do - = javascript_include_tag 'admin/plugins/codemirror/codemirror', 'admin/pages', 'admin/editable_elements' + = javascript_include_tag 'admin/plugins/tiny_mce/tinymce', 'admin/plugins/codemirror/codemirror', 'admin/pages', 'admin/editable_elements' = stylesheet_link_tag 'admin/editable_elements' = image_picker_include_tags diff --git a/doc/TODO b/doc/TODO index cfd553d3..aa6cd4d6 100644 --- a/doc/TODO +++ b/doc/TODO @@ -6,7 +6,8 @@ x bug editable_xxx disabled for nil block x display liquid errors x theme assets selector in page editor x saving page in ajax -- editable_long_text tag +x editable_long_text tag +- blocking issue when modifying the parent of 2 templates => one of the 2 children has reference of the first child - editable_file tag - refactor slugify method (use parameterize + create a module) diff --git a/lib/locomotive/liquid/tags/editable.rb b/lib/locomotive/liquid/tags/editable.rb new file mode 100644 index 00000000..dd58d977 --- /dev/null +++ b/lib/locomotive/liquid/tags/editable.rb @@ -0,0 +1,2 @@ +require 'locomotive/liquid/tags/editable/short_text' +require 'locomotive/liquid/tags/editable/long_text' \ No newline at end of file diff --git a/lib/locomotive/liquid/tags/editable/long_text.rb b/lib/locomotive/liquid/tags/editable/long_text.rb new file mode 100644 index 00000000..3b512db0 --- /dev/null +++ b/lib/locomotive/liquid/tags/editable/long_text.rb @@ -0,0 +1,12 @@ +module Locomotive + module Liquid + module Tags + module Editable + class LongText < ShortText + end + + ::Liquid::Template.register_tag('editable_long_text', LongText) + end + end + end +end diff --git a/lib/locomotive/liquid/tags/editable/short_text.rb b/lib/locomotive/liquid/tags/editable/short_text.rb new file mode 100644 index 00000000..a14b8097 --- /dev/null +++ b/lib/locomotive/liquid/tags/editable/short_text.rb @@ -0,0 +1,58 @@ +module Locomotive + module Liquid + module Tags + module Editable + class ShortText < ::Liquid::Block + + Syntax = /(#{::Liquid::QuotedFragment})(\s*,\s*#{::Liquid::Expression}+)?/ + + def initialize(tag_name, markup, tokens, context) + if markup =~ Syntax + @slug = $1.gsub('\'', '') + @options = {} + markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') } + else + raise ::Liquid::SyntaxError.new("Syntax Error in 'editable_short_text' - Valid syntax: editable_short_text (, )") + end + + super + end + + def end_tag + @context[:page].add_or_update_editable_element({ + :kind => self.kind, + :block => @context[:current_block].try(:name), + :slug => @slug, + :hint => @options[:hint], + :default_content => @nodelist.first.to_s, + :disabled => false, + :from_parent => false + }) + end + + def render(context) + current_page = context.registers[:page] + + element = current_page.find_editable_element(context['block'].try(:name), @slug) + + if element + element.content + else + Locomotive.logger "[editable short text] missing editable short text #{context[:block].name} / #{@slug}" + '' + end + end + + protected + + def kind + self.class.name.demodulize + end + + end + + ::Liquid::Template.register_tag('editable_short_text', ShortText) + end + end + end +end diff --git a/lib/locomotive/liquid/tags/editable_short_text.rb b/lib/locomotive/liquid/tags/editable_short_text.rb deleted file mode 100644 index abc998e9..00000000 --- a/lib/locomotive/liquid/tags/editable_short_text.rb +++ /dev/null @@ -1,50 +0,0 @@ -module Locomotive - module Liquid - module Tags - class EditableShortText < ::Liquid::Block - - Syntax = /(#{::Liquid::QuotedFragment})(\s*,\s*#{::Liquid::Expression}+)?/ - - def initialize(tag_name, markup, tokens, context) - if markup =~ Syntax - @slug = $1.gsub('\'', '') - @options = {} - markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') } - else - raise ::Liquid::SyntaxError.new("Syntax Error in 'editable_short_text' - Valid syntax: editable_short_text (, )") - end - - super - # puts "context = #{context.object_id} / #{@context[:page]}" - end - - def end_tag - @context[:page].add_or_update_editable_element({ - :block => @context[:current_block].try(:name), - :slug => @slug, - :hint => @options[:hint], - :default_content => @nodelist.first.to_s, - :disabled => false, - :from_parent => false - }) - end - - def render(context) - current_page = context.registers[:page] - # puts "[EditableShortText] rendering #{current_page.editable_elements.inspect}" - - element = current_page.find_editable_element(context['block'].try(:name), @slug) - - if element - element.content - else - Locomotive.logger "[editable short text] missing editable short text #{context[:block].name} / #{@slug}" - '' - end - end - - ::Liquid::Template.register_tag('editable_short_text', EditableShortText) - end - end - end -end diff --git a/lib/locomotive/liquid/tags/inherited_block.rb b/lib/locomotive/liquid/tags/inherited_block.rb index 789f1875..b71a374e 100644 --- a/lib/locomotive/liquid/tags/inherited_block.rb +++ b/lib/locomotive/liquid/tags/inherited_block.rb @@ -32,30 +32,9 @@ module Locomotive protected - # def register_current_block - # @context[:blocks] ||= {} - # - # block = @context[:blocks][@name] - # - # if block - # # needed for the block.super statement - # # puts "[BLOCK #{@name}|end_tag] nodelist #{@nodelist.inspect}" - # block.add_parent(@nodelist) - # - # @parent = block.parent - # @nodelist = block.nodelist - # - # # puts "[BLOCK #{@name}|end_tag] direct parent #{block.parent.inspect}" - # else - # # register it - # # puts "[BLOCK #{@name}|end_tag] register it" - # @context[:blocks][@name] = self - # end - # end - def contains_super?(nodelist) nodelist.any? do |node| - if node.is_a?(String) && node =~ /\{\{\s*block.super\s*\}\}/ + if node.is_a?(::Liquid::Variable) && node.name == 'block.super' true elsif node.respond_to?(:nodelist) && !node.is_a?(Locomotive::Liquid::Tags::InheritedBlock) contains_super?(node.nodelist) diff --git a/public/javascripts/admin/editable_elements.js b/public/javascripts/admin/editable_elements.js index 7dd25308..132e18c2 100644 --- a/public/javascripts/admin/editable_elements.js +++ b/public/javascripts/admin/editable_elements.js @@ -23,4 +23,6 @@ $(document).ready(function() { } }, []); + $('textarea.html').tinymce(TinyMceDefaultSettings); + });