Advertisement
Guest User

Untitled

a guest
Aug 17th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. Hi Ryan,
  2.  
  3. I have figured it out :D
  4.  
  5. I'll leave this here for anyone else trying to do the same thing. It's a tiny bit quirky but it absolutely functions as it should. Most of the anomalies are visual but those could be related to how I'm using the editor rather than it being a product of how the editor is initalised.
  6.  
  7. Anyway, here's how I got JCE working with JS. You [b]do[/b] need to use Joomla functions still.
  8.  
  9. First of all we need to set up the editor scripts via PHP like Ryan does
  10.  
  11.  
  12. [code type="php"]
  13. require_once JPATH_ADMINISTRATOR . '/components/com_jce/includes/base.php';
  14.  
  15. // create editor
  16. $editor = new WFEditor();
  17.  
  18. $editor->init();
  19.  
  20. foreach ($editor->getScripts() as $script) {
  21. $document->addScript($script);
  22. }
  23.  
  24. foreach ($editor->getStyleSheets() as $style) {
  25. $document->addStylesheet($style);
  26. }
  27.  
  28. $document->addScriptDeclaration(implode("\n", $editor->getScriptDeclaration()));
  29. [/code]
  30.  
  31. You will also need to generate a session token so that the JCE popups still work (link/media/file). I created a hidden input that appears on the page, so I can grab the token value via JS when needed
  32.  
  33. [code type="php"]
  34. $html .= '<input type="hidden" name="sessionToken" id="sessionToken" value="'. JSession::getFormToken() .'" />';
  35. [/code]
  36.  
  37. With all this on the page, we can use WFEditor.init(); to create a JCE instance, but the options it requires are quite specific. Based on this thread
  38.  
  39. https://www.joomlacontenteditor.net/support/forum/98135-error-in-creating-editor-via-javascript-uncaught-typeerror-cannot-read-property-css-of-undefined-in-editor_template-js
  40.  
  41. What I found was missing was a parameter in the init options. This held things like the profile_id to use, the session token, and also a context - no idea what this is but it was set to "22" for all my other JCE instances so I hard coded the 22. Here's my settings object:
  42.  
  43. [code type="javascript"]
  44. var token = document.getElementById('sessionToken').value,
  45. editorOpts = {
  46. skin: 'o2k7',
  47. skin_variant: 'silver',
  48. mode: 'specific_textareas',
  49. theme_advanced_toolbar_location: 'top',
  50. theme_advanced_buttons1: 'bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull,formatselect,bullist,numlist,outdent,indent',
  51. theme_advanced_buttons2: 'link,unlink,anchor,image,separator,undo,redo,cleanup,code,separator,sub,sup,charmap',
  52. theme_advanced_buttons3: '',
  53. height: 350,
  54. width: 700,
  55. max_width: 700,
  56. base_url: 'https://website.com/',
  57. selector: '#editor-text-popup',
  58. language_load: 0,
  59. query: {
  60. context: 22,
  61. profile_id: 1
  62. }
  63. };
  64.  
  65. editorOpts.query[token] = 1;
  66.  
  67. WFEditor.init(editorOpts);
  68. [/code]
  69.  
  70. This creates a JCE instance that functions as expected, initialised via JS.
  71.  
  72. After this, the WFEditor object doesn't behave the same as tinymce, and so I found to do things like destroy the created instance, I had to use
  73.  
  74. [code type="javascript"]
  75. tinymce.execCommand('mceRemoveControl', true, 'editor-text-popup');
  76. [/code]
  77.  
  78. Rather than a function on WFEditor. However, WFEditor is where you call things like getContent() to get the html.
  79.  
  80. I hope this helps others. Thank you for your help Ryan.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement