Tiny MCE - How to enable pagebreak plugin

Hi
My TinyMCE editor works great but the page-break button only inserts a comment (!–pagebreak–>) rather than the proper CSS to generate a page break.

As far as I recall, this is a standard issue with TinyMCE, which is resolved by enabling the pagebreak plugin.

Does anyone have any idea how to do this in Scriptcase?

At the moment, I’ve just given my users a CSS hack which they have to enter in the <> code edit area!

I REALLY don’t want to have to initialise my own version of TinyMCE just to get this fixed.

On a related note, there seems to be no way to prevent a paste into the field, which allows the users to paste a picture which could be too large for the field, which basically makes the field uneditable from there on in.

I think this points to a wider need for Scriptcase to rethink their implementation of TinyMCE and at least give us the tools to customise key features WITHOUT the need to implement our own version.

I needed to solve this relatively quickly so I have investigated further and created a workaround based on a nice bit of code produced by @GunterEibl
here: https://asdw.de/en/scriptcase-und-tinymce-mit-benutzerdefinierten-init-parametern/

This code is a little old so I had some problems getting it to work at first because Scriptcase have made some subsequent changes (as have tinyMCE).

The TinyMCE help pages are very good at guiding you through it.

Here is my revised version: (I’ve left some messy echo and comment lines in there as it might help you if you want to reimplement… I needed them to follow the thread and debug!)

I also set the code up as functions as I intend to put them in a lib once I’m finished debugging

Notes

  • TinyMCE is just a javascript library that effectively hijacks any field and overlays the editor (Which is very configurable)
  • The Scriptcase file location is different from when Gunther did the original (see $tinyFolder line below)
  • Unfortunately this workaround needs to be applied to EVERY field that uses the editor, so its a bit messy and so the REAL solution needs Scriptcase to allow for a bit of extra configuration in their Editor HTML setup (2 lines see final paragraph)

—First Function----

TinyMCEInitialise(); // This just sets up the TinyMCE based on the one in the SC libs

function TinyMCEInitialise(){
//--------------------------	
// get TinyMCE lib location
//--------------------------	
$prodFolder = $_SESSION['scriptcase']['tinyMCE_Test']['glo_nm_path_prod'];
$tinyFolder = $prodFolder."/third/tinymce/js/tinymce/";
// Declare tinyMCE - the one in Scriptcase
$msg = "<script src='".$tinyFolder."tinymce.min.js'></script>";
//echo "<br>-----<br>";
//echo htmlspecialchars($msg); 
//echo "<br>-----<br>";
echo ($msg); 
}

Then every field you want as an editor you need to say which field to hijack and initialise the editor

BasicEditor("#id_sc_field_tester"); // this is the field to hijack… found using google console on the form page.

— Second Function----
function BasicEditor($element){

//--------------------------	
// Install tinyMCE with plugins
//--------------------------
$msg = "<script>";
$msg.= "tinymce.init({ ";
$msg.= "selector: '".$element."',";
$msg.= "pagebreak_separator: '<div style= page-break-before: avoid !important;></div>',";
$msg.= "paste_data_images: false,";   // STOP paste of Images into the html

$msg.= "menubar : 'file edit insert view format table tools ',";
$msg.= "plugins : 'advlist,autolink,link,image,lists,charmap,print,preview,hr,anchor,pagebreak,searchreplace,wordcount,visualblocks,visualchars,code,fullscreen,insertdatetime,media,nonbreaking,table,directionality,emoticons,template,paste,textcolor,colorpicker,textpattern,contextmenu',";
$msg.= 'toolbar1: "undo,redo,separator,styleselect,separator,bold,italic,separator,alignleft,aligncenter,alignright,alignjustify,separator,bullist,numlist,outdent,indent,separator,fullpage,link,image,template",';
$msg .="})";
//$msg.="content_css: 'css/content.css'})";
$msg.= "</script>";
 
//echo htmlspecialchars($msg);  
echo($msg);
	}

Warning
I did find the coding to be a bit fiddly and errors in the Javascript (Echod above) cause it not to works. Best advice is to just get a basic editor working first by following the TinyMCE help pages… then add the extras.

For the record, the only 2 lines Scriptcase would need to add to their own TinyMCE init function would be:
$msg.= “pagebreak_separator: ‘<div style= page-break-before: avoid !important;>’,”;
$msg.= “paste_data_images: false,”; // STOP paste of Images into the html

to fix both the pasting of pictures and correcting the pagebreak comment nonsense. (This is a TinyMCE bit of nonsense)

Good luck.

Further update:

I got the above code to work exactly as I wanted in a control. BUT… in a form it doesn’t save the editor content to the field. I’ve fiddled for some hours without success in making it work. in a form.

There are console errors which MAY point to the culprit saying:
[Violation] Permissions policy violation: unload is not allowed in this document
which may be part to the problem as it is the adding of the event listener by tinyMCE causing the error report. BUT, this also appears on the Scriptcase editor. I think its just a chrome warning that unload is being depricated

I ALSO NOTICE THAT THE VERSION IN PROD IS 2022 SO VERY OLD… 5.10.7 - 2022-12-06 versus a latest vesion around 7.7

Everything else works fine, but I cant find a way of simply getting the editor content into the field.

If anyone has any ideas I’m all ears!

The core solution would be for Striptcase to give an option (probably by editor) about whether to allow paste or not (It currently assumes every editor allows it. )
They could also very simply add a single line for the pagebreak. (I think the pagebreak plugin is already added, but if not, that would simply require a minor change to the plugins directive)

It would need to update the code generation so that the file is changed here:

In the meantime I took the easy way out for the pagebreak problem.

I simply replace the incorrect editor line with a line that will actually create a pagebreak in the onValidateSuccess event:
image

Its still a pain as you have to do this for every editor field in every application.

I’m still working through the picture paste breaking the field. (Which is why I went the original direction to try and prevent it at source)

“THE VERSION IN PROD IS 2022 SO VERY OLD”

They definitely should provide a more recent version of TinyMCE and also allow for customization of its parameters.

Whenever I need a good editor, I load the latest version of TinyMCE in a blank app and handle all db operations via ajax.
But that’s the (relatively) easy part.
The nightmare is that if in the same app I need other kinds of db fields I have to write custom code to generate and handle them.

Final (Hopefully) comment on this… I added some crude size checking on all Editor fields.

Ths should really be a standard input length check like any other field… the editor should be no different.

Again the downside is you have to add this logic to every app that uses an editor.

I hope this helps anyone else.