From c1c9b4d19991d10e904721c39276f87d719e635a Mon Sep 17 00:00:00 2001 From: EPrints Date: Fri, 11 Aug 2023 12:35:50 +0000 Subject: [PATCH 1/2] #1 Multiple field support --- lang/en/phrases/richtext.xml | 6 ++++++ plugins/EPrints/MetaField/Richtext.pm | 12 +++++++++++- static/javascript/auto/60_richtext.js | 9 +++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 lang/en/phrases/richtext.xml 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..387fb43 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; } 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); + } +} From 94c4d65d393770bae0cd26867a6a817263a04566 Mon Sep 17 00:00:00 2001 From: EPrints Date: Fri, 11 Aug 2023 12:49:35 +0000 Subject: [PATCH 2/2] \#2 Catch encoding issues on render --- plugins/EPrints/MetaField/Richtext.pm | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/plugins/EPrints/MetaField/Richtext.pm b/plugins/EPrints/MetaField/Richtext.pm index 387fb43..fc1abc0 100644 --- a/plugins/EPrints/MetaField/Richtext.pm +++ b/plugins/EPrints/MetaField/Richtext.pm @@ -70,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 $dom = XML::LibXML->load_html( - string => $value - ); + my $body = eval + { + 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; } ######################################################################