Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for #1 and #2 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lang/en/phrases/richtext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>
<!DOCTYPE phrases SYSTEM "entities.dtd">

<epp:phrases xmlns="http://www.w3.org/1999/xhtml" xmlns:epp="http://eprints.org/ep3/phrase" xmlns:epc="http://eprints.org/ep3/control">
<epp:phrase id="lib/metafield:join_richtext"><br/></epp:phrase>
</epp:phrases>
37 changes: 29 additions & 8 deletions plugins/EPrints/MetaField/Richtext.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

######################################################################
Expand Down
9 changes: 9 additions & 0 deletions static/javascript/auto/60_richtext.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
}
}