Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add tinymce editor to element object instead of selector

I have a custom-element (Aurelia equivelent of a web component) that creates a tinymce editor. There is no way to select the textarea by using a selector (because there can exist any number of these custom-elements on a page). I need some way of initializing the tinymce instance by passing it the element object. Is there such a possibility? I haven't been able to find this functionality anywhere...

Thanks in advance.

like image 303
pQuestions123 Avatar asked Jun 08 '16 17:06

pQuestions123


People also ask

How do I get TinyMCE editor value?

getContent() method. to add the TinyMCE script and text area, then we can get the value when the editor is initialized by writing: tinymce. init({ selector: '#mytextarea', setup(editor) { editor.


2 Answers

Sorry that I'm a bit late. I had this exact same problem. I used an Angular directive, and I wanted to initialize TinyMCE on $element. It turns out you can use this syntax:

var element = getYourHTMLElementSomehow();

//...

tinymce.init({
  target: element
});

So you don't use selector at all, and instead use target.

I had to look in the source code for this, because it doesn't seem to be explicitly documented anywhere.

like image 189
jens1101 Avatar answered Oct 19 '22 14:10

jens1101


Since TinyMCE seems to require you to use a selector and won't let you simply pass an element instance (and the developer doesn't seem to grasp the utility of this use-case, based on his forum responses), your best bet would be to do something like this:

View

<template>
  <textarea id.one-time="uniqueId" ...other bindings go here...></textarea>
</template>

ViewModel

export class TinyMceCustomElement {
  constructor() {
    this.uniqueId = generateUUID();
  }

  attached() {
    tinymce.init({
      selector: `#${this.uniqueId}`,
      inline: true,
      menubar: false,
      toolbar: 'undo redo'
    });
  }
}

function generateUUID() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
      return v.toString(16);
  });
}

My UUID function comes from here: Create GUID / UUID in JavaScript?

like image 36
Ashley Grant Avatar answered Oct 19 '22 13:10

Ashley Grant