简体   繁体   中英

How to get the textarea name from within SCeditor?

I am initializing SCEditor like this:

var mytextarea = $('textarea[name="one"])[0];

sceditor.create(mytextarea, 
{
    format: 'xhtml',
    style: '../default.min.css',
    // ...
});

Now I need to get the textarea's name from within SCEditor. My goal: After the user entered text into SCEditor or used a custom plugin , then the textarea name/id is passed with the new content to an external HTML preview field with the same ID. (There are several editors on the page.)

I haven't found any way how to get the name of the textarea ( "one" ) from within SCEditor. Here is a code snippet of a plugin that gives access to the editor variable:

// Insert-Formula Command
sceditor.command.set('insertformula', {
    // wysiswyg mode 
    exec: function(caller) {
       var editor = this;
       console.log(editor);  // cannot find the textarea's name here

The API does not reveal such a function: https://www.sceditor.com/api/sceditor/

I have tried to understand the source code (the textarea is passed as original in the init() function) but I could not find any way of how to return the textarea/original: https://github.com/samclarke/SCEditor/blob/master/src/lib/SCEditor.js

Then I thought I could bind the name after init with the editor's create() and access it later on, eg:

sceditor.textarea_name = "one";

But this does not work either.

Here is a JSFiddle for testing: http://jsfiddle.net/kai_noack/0gLhtabq/10/

--

Update:

I can see that there is [[Scopes]] in the console output (jsfiddle) that holds the name. Maybe it is possible to access it?

控制台输出 sceditor

I need to get the textarea#one part.

Alright, I found a nice solution:

Pass the textarea name as the id in the create() method:

sceditor.create(mytextarea, 
{
    format: 'xhtml',
    style: '../default.min.css',
    // ...
    id: mytextarea_id,
});

And read it within the Sceditor plugin using editor.opts.id :

// Insert-Formula Command
sceditor.command.set('insertformula', {
    // wysiswyg mode 
    exec: function(caller) {
       var editor = this;
       console.log(editor.opts.id);  // cannot find the textarea's name here

Done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM