Include the insertion button in the Tiny MCE Editor

Currently, I am in the process of developing my own plugin to integrate into the list of TinyMCE v4 plugins. So far, I have successfully added a button to the menu that opens a pop-up when clicked. In this pop-up, users can input data which is then added to the TinyMCE editor. However, I am encountering some issues when trying to edit this information. I have attempted to add additional scripts but they have not resolved the issue.

Below is a snippet of my source code:

Note 1: The onclick function works for adding a new button.

Note 2: The onpostrender function is intended for editing the button.


tinymce.PluginManager.add('buttonlink', function(editor, url) {
    // Add a button that opens a window
    editor.addButton('buttonlink', {
        text: 'Insert Button',
        tooltip: "Insert/edit Button link",
        icon: false,
        onclick: function() {
            // Open window
            editor.windowManager.open({
                title: 'Button',
                body: [
                    {type: 'textbox', name: 'title', label: 'Title'},
                    {type: 'textbox', name: 'link', label: 'Link'},
                    {type: 'listbox', name: 'colorBtn', label: 'Button Color',values:
                                [...]
                    },
                    {type: 'listbox', name: 'colorText', label: 'Text Color',values:
                                [...]
                    },
                ],
                onsubmit: function(e) {
                    if(e.data.title != null && e.data.title != "") {
                        editor.insertContent('<a href="' + e.data.link + '" target="_blank" id="btn-link-plugin" class="btn" style="color: #' + e.data.colorText + '; background: #' + e.data.colorBtn + '">' + e.data.title + '</a>');
                    }
                }
            });
        },
        onpostrender: function (buttonApi) {
            var btn = this;
            var editorEventCallback = function (e) {
                [...]
            };
            editor.on('NodeChange', editorEventCallback);
            return function (buttonApi) {
                console.log("off");
                editor.off('NodeChange', editorEventCallback);
            }
        }
    });

    return {
        getMetadata: function () {
            return  {
                name: "Button Link plugin",
                url: "https://capoffshore.com"
            };
        }
    };
});

This is an example of the pop-up used for creating a new button:

Answer №1

This code successfully resolved the issue at hand:

tinymce.PluginManager.add('buttonlink', function(editor, url) {
// Implementation of a button for opening a new window
var settings = [];
editor.addButton('buttonlink', {
    text: 'Insert Button',
    tooltip: "Add/edit Button link",
    icon: false,
    onclick: function(e) {
        // Open the window
        var btn = this;

        var text = "";
        var link = "";
        var backgroundTxt = "0000FF";
        var colorTxt = "FFFFFF";

        if (typeof settings['text'] !== 'undefined') {
            text = settings['text'];
        }
        if (typeof settings['link'] !== 'undefined') {
            link = settings['link'];
        }
        if (typeof settings['backgroundTxt'] !== 'undefined') {
            backgroundTxt = settings['backgroundTxt'];
        }
        if (typeof settings['colorTxt'] !== 'undefined') {
            colorTxt = settings['colorTxt'];
        }

        editor.windowManager.open({
            title: 'Button',
            body: [
                {type: 'textbox', name: 'title', label: 'Title', value: text},
                {type: 'textbox', name: 'link', label: 'Link', value: link},
                {type: 'listbox', name: 'colorBtn', label: 'Button Color',values:
                        [
                            {value:"0000FF", text:"Blue"},
                            {value:"008000", text:"Green"}
                        ],
                    onPostRender: function() {
                        this.value(backgroundTxt);
                    }
                },
                {type: 'listbox', name: 'colorText', label: 'Text Color',values:
                        [
                            {value:"FFFFFF", text:"White"},
                            {value:"000000", text:"Black"}
                        ],
                    onPostRender: function() {
                        this.value(colorTxt);
                    }
                },
            ],
            onsubmit: function(e) {
                // Insert content upon form submission
                if(e.data.title != null && e.data.title != "") {

                    var link = e.data.link;
                    var title = e.data.title;
                    var colorText = e.data.colorText;
                    var colorBtn = e.data.colorBtn;

                    if (e.data.link == null || e.data.link == "" || typeof e.data.link === 'undefined') {
                        link = "#";
                    }

                    tinymce.activeEditor.dom.remove(tinymce.activeEditor.dom.get("btn-link-plugin"));

                    editor.insertContent('<a href="' + link+ '" target="_blank" id="btn-link-plugin" class="btn" style="color: #' + colorText + '; background: #' + colorBtn + '">' + title + '</a>');
                }
            }
        });
    },
    onpostrender: function (buttonApi) {
        var btn = this;
        var editorEventCallback = function (e) {
            var IDElement = e.element.getAttribute('id');
            if (btn._id == "mceu_22" && IDElement == "btn-link-plugin") {
                btn.active(true);
                var link = e.element.getAttribute('data-mce-href');
                var style = e.element.getAttribute('style');
                var text = e.element.text;

                var res = style.split(";");
                var colorStyle = res[0].split(":");
                var backgroundStyle = res[1].split(":");
                colorStyle[1] = colorStyle[1].replace(/\s+/g, '');
                backgroundStyle[1] = backgroundStyle[1].replace(/\s+/g, '');

                var colorTxt = colorStyle[1].substr(1);
                var backgroundTxt = backgroundStyle[1].substr(1);

                settings['link'] = link;
                settings['text'] = text;
                settings['colorTxt'] = colorTxt;
                settings['backgroundTxt'] = backgroundTxt;
            }
        };
        editor.on('NodeChange', editorEventCallback);
    }
});

return {
    getMetadata: function () {
        return  {
            name: "Button Link plugin",
            url: "https://capoffshore.com"
        };
    }
};
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The ajaxStart() and ajaxStop() methods are not being triggered

I'm currently working on a Q/A platform where users can click on specific questions to be redirected to a page dedicated for answers. However, when a user tries to answer a question by clicking the "Answer" link, certain background processes such as ...

A guide on transferring variables to sessions instead of passing them through the URL in PHP

<a class='okok' id='$file' href='" . $_SERVER['PHP_SELF'] . "?file=" . $file . "'>$file</a> The given code snippet represents a hyperlink that passes the filename to the 'file' variable, which ...

Error in form action when generated through an ajax partial in Ruby

I am facing an issue with a form that is loaded via an ajax partial. The problem arises when the form loads through ajax as it targets the wrong controller/url instead of the intended one. Despite my efforts to set the target controller correctly, it keeps ...

Is it possible to implement pagination using 'useSWR' in combination with the contentful-client?

I am currently working on implementing pagination in a Next.js project using the useSWR hook. My approach seems to be functioning correctly, but I have a concern about caching due to the key parameter not being a unique string as recommended in the documen ...

rating-widget not displaying when performing an ajax request

Having an issue with the star rating plugin () while using an ajax function for searching and filtering. The star rating displays correctly when the page initially loads https://i.stack.imgur.com/eaOmu.png However, when utilizing the filter and search fu ...

Identify the Google Maps Marker currently displayed on the screen

Is there a way to generate a list of markers within the zoom range for Google Maps, similar to the functionality on this site I'm curious if I can achieve this using jQuery or if there is a built-in function for Google Maps v3? Thank you! ...

The functionality of Javascript is being compromised when utilizing ng-repeat

Just recently diving into the world of AngularJs while developing a website. I've successfully retrieved data from Rest services on a page, and used ng-repeat to display it. The issue arises when I have a regular javascript element on the page that i ...

Determining the page's coordinates in ColdFusion

Whenever I use iframes or frames on older websites, I implement an additional security measure using a JavaScript function: <SCRIPT LANGUAGE="JavaScript1.1"> if (top == self) self.location.href = "../index.cfm"; </SCRIPT> I also include an ...

The function Firebase.database() is unavailable in the Sails Service

I have developed a service named Firebase.js, and I am attempting to utilize it from my Controllers by using Firebase.database. However, I am encountering an error stating that Firebase.database() is not a function services/Firebase.js var admin = requir ...

Angular 1 and Javascript offer a different approach than using lodash omit and the delete operator

I am facing an issue with a child component where I need to remove properties from an object. Normally, using Lodash, it should work with the following code snippet: this.current.obj = omit(this.current.obj, ['sellerSupportWeb', 'sellerSup ...

Show a condensed version of a lengthy string in a div using React TS

I've been tackling a React component that takes in a lengthy string and a number as props. The goal of the component is to show a truncated version of the string based on the specified number, while also featuring "show more" and "show less" buttons. ...

Is there a way to retrieve the field names from a JSON array within a for loop?

Here is the structure of my Json array: var data = { "categories": { "category1": { "Name": "Maps", "Id": 3, "orderInList": 1 }, "category2": { "Name": "B ...

Is it possible to incorporate an additional value into the jQuery widget 'Autocomplete' by using a second variable?

For several years, I have been utilizing the jQuery 'Autocomplete Widget' in my projects. This plugin allows me to pass a value labeled as 'term' to the PHP SQL code that works like this: $( "#cs1" ).autocomplete({ aut ...

What is the best way to use scrollIntoView() to display an additional item at the top or bottom of the visible area

When implementing scrollIntoView() with navigation buttons (up and down), I aim to display two items at a time to signal to the user that there are more items to navigate. However, the first and last items should retain their default behavior so the user u ...

Require help with personalizing a jQuery horizontal menu

I recently downloaded this amazing menu for my first website project By clicking the download source link, you can access the code Now, I need your kind help with two issues: Issue 1: The menu seems to be getting hidden under other elements on the page ...

What is the best way to resize an image to fit its surroundings within a nested box?

Have you ever heard of a website called SPAM? It has a unique homepage that I want to replicate using JavaScript, jQuery, and some CSS. My main challenge is figuring out how to adjust the image size to match the center box on the page. I want to create th ...

Leveraging AngularJS for a Windows store app

After attempting to integrate AngularJS into my Windows store application, I came across a few recommended solutions: Unfortunately, these solutions did not work as expected. While I didn't encounter the Unable to add dynamic content error, AngularJS ...

Link scripts can sometimes cause issues with node.js

Greetings! I have successfully created a client-side SPA using vanilla-router. However, my Node.js server sometimes encounters an error when attempting to load a linked script. Uncaught SyntaxError: Unexpected token '<' This error only oc ...

Tips for minimizing the transfer time of large arrays using ajax

https://i.stack.imgur.com/IP0oe.pngDescription I am currently working on transferring a JSON object from the server to the client using PHP and JavaScript via AJAX. The JSON object contains a large array (200x200) of integers. The server is running on lo ...

Activate the Bootstrap Jquery/Ajax inline editing feature by simply clicking on the Edit button

Seeking recommendations for a way to implement inline editing. When the edit button is clicked, I want the label's content to be replaced with an input text field that can be updated in my MySQL database. This is what my code looks like: <label s ...