diff --git a/lang/en/phrases/richtext.xml b/lang/en/phrases/richtext.xml new file mode 100644 index 0000000..0712559 --- /dev/null +++ b/lang/en/phrases/richtext.xml @@ -0,0 +1,6 @@ + + + + +
+
diff --git a/plugins/EPrints/MetaField/Richtext.pm b/plugins/EPrints/MetaField/Richtext.pm index 9288fd9..fc1abc0 100644 --- a/plugins/EPrints/MetaField/Richtext.pm +++ b/plugins/EPrints/MetaField/Richtext.pm @@ -51,7 +51,17 @@ sub render_input_field_actual #$frag->appendChild( $session->make_element( "script", src => "//code.jquery.com/jquery-1.12.4.js" ) ); $frag->appendChild( $session->make_element( "script", src=> "/javascript/tinymce.min.js" ) ); - $frag->appendChild( $session->make_javascript( 'jQuery( document ).ready(function($){ initTinyMCE("#' . $basename .'"); } );' ) ); + + if( $self->get_property( "multiple" ) ) + { + # This method will call initTinyMCE for each textarea rendered + # not in a document.ready as document.ready doesn't call in AJAX contexts + $frag->appendChild( $session->make_javascript( 'initMultipleTinyMCEs("' . $basename . '");' ) ); + } + else + { + $frag->appendChild( $session->make_javascript( 'jQuery( document ).ready(function($){ initTinyMCE("#' . $basename .'"); } );' ) ); + } return $frag; } @@ -60,16 +70,27 @@ sub render_single_value { my( $self, $session, $value, $obj ) = @_; - if( !defined $value ) { return $session->make_doc_fragment; } + if( !defined $value ) { return $session->make_doc_fragment; } + + my $body = eval + { + my $dom = XML::LibXML->load_html( + string => $value + ); - my $dom = XML::LibXML->load_html( - string => $value - ); + my @nodelist = $dom->getElementsByTagName("body"); + return $nodelist[0]; + }; - my @nodelist = $dom->getElementsByTagName("body"); - my $body = $nodelist[0]; + # If you have non-richtext encoded characters like & here, can cause LibXML to fail and a 500 error + # so catch this and just render plaintext + if( $@ ) + { + print STDERR "Exception trying to render richtext for $value: $@\n"; + return $session->make_text( $value ); + } - return $body; + return $body; } ###################################################################### diff --git a/static/javascript/auto/60_richtext.js b/static/javascript/auto/60_richtext.js index 29d51f2..bf8fc17 100644 --- a/static/javascript/auto/60_richtext.js +++ b/static/javascript/auto/60_richtext.js @@ -1,4 +1,7 @@ var initTinyMCE = function(id){ + // Remove any old tinymce instance with this id (e.g. in AJAX context) + tinymce.remove(id); + tinymce.init({ selector: id, height: 500, @@ -13,3 +16,9 @@ var initTinyMCE = function(id){ }); }; +// For multiple fields +function initMultipleTinyMCEs(basename) { + for( var i = 1; i <= document.getElementById(basename + '_spaces').value; i++ ) { + initTinyMCE('#' + basename + '_' + i); + } +}