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:

https://i.sstatic.net/YvtTk.png

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

Changing HTML elements dynamically within an ng-repeat using AngularJS directives

I have devised an angular directive where I execute an ng-repeat. The fundamental purpose of this directive is to interchange itself with a distinct directive that depends on a value forwarded into the original directive: <content-type-directive type=" ...

The Angular Material Datepicker lacks any selected date

I am currently in the process of developing a web application using Angular and incorporating Angular Material for certain UI components. I have encountered an issue that I am unable to resolve. When attempting to use the datepicker as outlined on https:// ...

Exploring the world of Javascript: The significance of variable scope and its

Encountered a unique challenge while attempting to execute an ajax call and confine the function's actions to itself. Below is the code snippet: $(document).on('click', 'input.action', function(event) { var self = this; ...

Expansive Carousel Feature with Ng Bootstrap

In my Angular 5 application, I am utilizing the Carousel component from "@ng-bootstrap/ng-bootstrap": "^1.1.2". I am trying to display pictures in full screen but when I press F11, the image appears like this. I am unsure of which CSS properties to apply ...

"Server request with ajax did not yield a response in JSON format

http://jsfiddle.net/0cp2v9od/ Can anyone help me figure out what's wrong with my code? I'm unable to see my data in console.log, even though the network tab in Chrome shows that my data has been successfully retrieved. Here is my code snippet: ...

Vue event manager, accessible for all components

I have created a new Vue instance and assigned it to the window object, thinking that it would be accessible throughout all components. I expected this setup to allow me access to events emitted anywhere within my application. However, it seems like this ...

Are you ensuring compliance with licensing in your Webpack bundles?

Can webpack be used to verify license compliance? I'm looking for a way to ensure that the license headers from all modules built by webpack are included in the final output file. How can we confirm this is happening? Furthermore, I am also intereste ...

Switch between using the useState hook by toggling it with the MUI Switch

Looking to implement a custom Dark Mode feature for a specific element on my create-react-app website using MUI. I have successfully implemented the Switch to change the state on toggle, but I am having trouble figuring out how to toggle it back and fort ...

Tips for incorporating an outside model into vue.js with babylon js

Struggling with importing a gltf file into vue.js using babylon.js to create a 3D view on a webpage. The documentation online isn't very clear, and I've tried the following steps in my Hello.vue file: <div> <h1> Hello </h1> < ...

IE8 - "object does not exist or is undefined" error

Below is the HTML code snippet: <td style="vertical-align: bottom;"><div id="resultCount">n.v.</div></td> Accompanied by this JavaScript code: function processResultCount(data) { $("#resultCount").html(formatNumber(data.res ...

Adding miscellaneous PHP scripts

When a user clicks on the sample button, my PHP code gets appended. It works fine, but I want to clean up my process. After some research, I learned that using jQuery AJAX is the way to go. The only problem is, I'm not sure how to implement AJAX. I&ap ...

Exploring the option of showcasing multiple json_encode data on a pie chart

Hey there! I'm currently utilizing Chart.js to generate a pie chart by retrieving data from the database: <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script> <script> var ctx = document.getE ...

Issue with VueJS: Cannot modify a component property within a watcher function

I am currently developing a Vue 2 Webpack application that utilizes Vuex. My aim is to update the local state of a component by observing a computed property which retrieves data from the Vuex store. Here's an excerpt from the <script></scrip ...

Creating a text field in an input box using JavaScript or jQuery to input data

At work, I need to use a web application that requires me to fill in two input fields. However, when I try to do so using JavaScript commands, the strings appear but the "Add" button remains disabled. Additionally, even if I enable the button, the data can ...

Is there a way to customize the color of the bar displaying my poll results?

My poll features two results bars that are currently both blue. I attempted to change the color of these bars but was unsuccessful. I've searched for solutions on stack overflow, specifically How can I change the color of a progress bar using javascr ...

Errors are not displayed or validated when a FormControl is disabled in Angular 4

My FormControl is connected to an input element. <input matInput [formControl]="nameControl"> This setup looks like the following during initialization: this.nameControl = new FormControl({value: initValue, disabled: true}, [Validators.required, U ...

Change not accepted

I am a beginner in Angular and still grappling with the fundamentals. On my menu, I have a cart icon with an initial value of 0 upon first load. In my product list, each product has an 'AddToCart' button. What I aim to achieve is- I want to dy ...

Guide on making API calls in AngularJS using query strings

I am new to learning about AngularJS and recently came across a helpful article on connecting to an API and using its data in our app. The article specifically focuses on displaying weather information with AngularJS. The only downside is that the weather ...

The connection to MongoDB is failing due to an incorrect URI

I tried setting up mongoDB on my node server and referred to the official MongoDB documentation. Here are the details of my setup: MongoDB version: 4.4.3 Node.js version: v15.7.0 I copied the starter code from MongoDB and here is what I used: const { Mon ...

Unexpected bug encountered while implementing redux

I received a warning from eslint while working with create-react-app. ./src/components/auth.js Line 24: Unexpected labeled statement no-labels Line 24: 'authenticated:' is defined but never used ...