Customizing a tinyMCE button with a unique icon

I'm attempting to insert a Font-Awsome icon into a button that I included in tinyMCE using the following code:

 ed.addButton('youtube', {
     title: 'Add Video' ,
     icon: 'icon-youtube',
     onclick: function () {
     //do stuff here...
 }

Despite following the documentation's advice to use an image, I have been unsuccessful in getting this to function properly. Does anyone have any suggestions for troubleshooting?

Answer №1

Here is an effective solution using CSS:

.mce-i-[FONT-AWESOME-CLASSNAME]:before {   
    content: "[FONT-AWESOME-CONTENT]";       
    font-family: FontAwesome;
    font-style: normal;
    font-weight: normal;
    text-decoration: inherit;
    color: #000;
    font-size: 1.5em;
    padding-right: 0.5em;
    position: absolute;
    top: 15%;
    left: 0;
}

This approach was inspired by matt-royal's response in this specific WordPress forum thread.

Answer №2

Although this information may be dated, I wanted to share my solution for those who might find it useful. In my case, I am utilizing TinyMCE 4 and made the following CSS adjustment:

.mce-ico.mce-i-fa {
    display: inline-block;
    font: normal normal normal 14px/1 FontAwesome;
    font-size: inherit;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

When assigning icons to buttons, you can simply do the following:

editor.addButton('adjust', {
    tooltip: 'Adjust Layout',
    icon: 'fa fa-adjust',
    onclick: function () {
        dialogLayout(editor, url, settings);
    }
});

This approach allows for the easy incorporation of any Font Awesome icons without the need for distinct class layouts per icon.

I hope this proves helpful to someone in their development efforts.

Answer №3

It seems like you are looking to include a button with an image in the list of icons within tinyMCE.

 tinymce.PluginManager.add("video", function (editor) {   
    editor.addButton('video', {
        tooltip: 'Insert video',
        image: tinymce.baseURL + '/plugins/video/icons/video.gif',
        onclick: function() {

        }
    }); 
});

To achieve this, create a new folder (e.g., "video") and within it, create another folder named "icons" where you can store your image. Next, create a file called video.js under the video folder.

Answer №4

In the latest version of TinyMCE, known as TinyMCE v5, a new functionality called tinymce.editor.ui.Registry has been introduced. This method, named addIcon, allows users to register a new SVG icon with the editor instance it was configured for. The purpose is to provide customizable icons for various Ui components.

The registered SVG icon can be referenced by name and utilized by different TinyMCE 5 Ui elements that support icon display. It remains exclusive to the specific editor instance it was assigned to.


Parameters of the Method:

addIcon(name: String, svgData: String)
  • name (String) - A unique identifier for the newly added icon.
  • svgData (String) - The SVG data string used by the browser to render the SVG icon.

Implementation of the Method:

// Example code to add a basic triangle icon:
editor.ui.registry.addIcon('triangleUp', '<svg height="24" width="24"><path d="M12 0 L24 24 L0 24 Z" /></svg>');

Subsequently, you can reference this unique identifier in creating custom buttons like so:

editor.ui.registry.addButton('youtube', {
  text: 'Add Video' ,
  icon: 'triangleUp',
  onAction: () => {
    // Perform desired actions here...
  }
});

Result:

Answer №5

In the case of tinymce 5, an example code snippet that can be utilized is as follows:

editor.ui.registry.addButton('customButton1', {
    text: '<span class="fa fa-youtube"></span>',
    //icon: '<span class="icon-youtube"></span>',
    onAction: () => alert('Button clicked!')
});

Answer №6

This easy-to-use stylesheet was successful for me:

i.mce-i-[FONT-AWESOME-CLASSNAME]:before {   // Customize FONT-AWESOME-CLASSNAME like "icon-youtube"
    content: [FONT-AWESOME-CONTENT];         // Update FONT-AWESOME-CONTENT such as "\f166"
    font-family: FontAwesome;
}

It's suitable for both toolbar buttons and menu items.

ed.addButton('youtube', {
     title: 'Insert Video' ,
     icon: '[FONT-AWESOME-CLASSNAME]',
     onclick: function () { /* perform magic here */ }
}

ed.addMenuItem('youtube', {
     text: 'Insert Video' ,
     icon: '[FONT-AWESOME-CLASSNAME]',
     onclick: function () { /* perform magic here */ },
     context: 'view'
}

Avoid using position: absolute to prevent disrupting the layout in the menu.

Answer №7

If you are using TinyMCE 4 and FontAwesome 5, the CSS code below will help you achieve your desired results:

.mce-ico.mce-i-fas {
    display: inline-block;
    font-family: 'Font Awesome 5 Free';
    font-weight: 900;
    text-rendering: auto;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale; }

Answer №8

I've built upon Ricardo's response and devised a method to incorporate font awesome icons into your project. Follow these steps:

1. Visit the font awesome website and locate the SVG of the desired icon:

https://fontawesome.com/icons/sitemap?f=classic&s=solid

2. Embed the SVG icon into tinyMCE:

editor.ui.registry.addIcon('iconName', '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M208 80c0-26.5 21.5-48 48-48h64c26.5 0 48 21.5 48 48v64c0 26.5-21.5 48-48 48h-8v40H464c30.9 0 56 25.1 56 56v32h8c26.5 0 48 21.5 48 48v64c0 26.5-21.5 48-48 48H464c-26.5 0-48-21.5-48-48V368c0-26.5 21.5-48 48-48h8V288c0-4.4-3.6-8-8-8H312v40h8c26.5 0 48 21.5 48 48v64c0 26.5-21.5 48-48 48H256c-26.5 0-48-21.5-48-48V368c0-26.5 21.5-48 48-48h8V280H112c-4.4 0-8 3.6-8 8v32h8c26.5 0 48 21.5 48 48v64c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V368c0-26.5 21.5-48 48-48h8V288c0-30.9 25.1-56 56-56H264V192h-8c-26.5 0-48-21.5-48-48V80z</path></svg>');

3. Associate the icon with the button:

                    editor.ui.registry.addButton('btn', {
                        text: 'Workflow Field',
                        icon: "iconName"
                    });

Result:

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

Wait for Axios Request Interceptor to complete before sending another ajax call

One feature I have added is a request interceptor for all axios calls. This interceptor checks the JWT token and automatically refreshes it if necessary. axios.interceptors.request.use((config) =>{ const currentState = store.getState(); // get upd ...

Looking to incorporate content from an external website onto my own site

I have experimented with various types of HTML tags such as iframe, embed, and object to display external websites. Some of them load successfully, while others do not. After researching my issue on Google, I discovered that "For security reasons, some si ...

Utilizing ng-route to parse the URL and identify the corresponding controller by its name

I am implementing ng-click and ng-view to navigate my div id= "listings" in order to display various mobile listings including iphone 4, iphone 5, nexus 4, nexus 5, nexus 6, lg g3, and more. You can check out my code on Plunkr. <div ng-repeat = ' ...

Determine if the JSON lacks an array within it

Utilizing an API to fetch results, the response received looks something like the following: {"currentPage":1,"numberOfPages":1,"totalResults":1,"data":[{"id":"Sf8xxo","name":"The Bear Reader Huckleberry Oatmeal Stout","nameDisplay":"The Bear Reader Huckl ...

Update JSON data in ng-blur event using AngularJS

Could you offer some guidance on how to push the content from a text box into JSON when I click outside of the box? Below is the code for my text box: <input type="text" name="treatmentCost" class="form-control" ng-model="addTemplate" /> And here i ...

Finding solutions using a controller class

I have a component that uses a controller class and accepts an input parameter through the use of a bindings object. In the controller, I am able to access the parameter after the constructor runs, but I cannot pass it as a constructor parameter. While wo ...

"Typescript: Unraveling the Depths of Nested

Having trouble looping through nested arrays in a function that returns a statement. selectInputFilter(enteredText, filter) { if (this.searchType === 3) { return (enteredText['actors'][0]['surname'].toLocaleLowerCase().ind ...

JavaScript if else statement with 3 different scenarios

I'm having trouble figuring out why this code isn't working. It seems like there might be a conflict between the testRed and testGreen functions. If that's the case, could someone please suggest a better approach to solving this issue? Code: ...

Display a compilation either in the backend or the frontend

I'm fairly new to NodeJS and I could really use some advice. I currently have a webpage that displays a list of users, which is retrieved from a collection using Mongoose. I am aware of two different ways to display this list: 1) One option is to que ...

Choosing the fourth cell in every row of a table and converting the information

Currently, I am working on a function that is supposed to take the fourth column in each row of a specific table, manipulate the data using a function, and return different data for the cell. This function takes an integer representing total minutes as i ...

Error thrown when attempting to access properties of null values (Uncaught TypeError: Cannot read properties of undefined (reading 'map'))

import React, { useState, useEffect } from "react"; import { TaskLists } from "./TaskLists"; import { Daycard } from "./daycard"; import { getTasks, deleteTask } from "../api/task.api"; export function TaskManager() { const [tasks, setTasks] = useState( ...

What is the most effective way to extract content values from different divs for calculation using jQuery?

I am working on creating a function that retrieves the content values from the <div class="rowtabela"> div and reads the nodes of <div class="item v_...">. Check out my code below: <div class="adicionados" id=& ...

Enhancing Next.js Images with Custom CSS Styles

I am working with a Next.js component: import styles from '../styles/Navbar.module.css' import Image from 'next/image' export const Navbar = () => { <div> <Image className={styles["navbar-home-icon"]} ...

Mastering the art of utilizing drag and drop features for both columns and rows in a React Table within ReactJS

I have managed to create a React Table with columns and rows, but now I'm looking to incorporate drag and drop functionality for both. Does anyone know how I can achieve this? Feel free to check out my CodeSandbox Sample here - https://codesandbox.io ...

React-native horizontal sliding plugin

Can anyone recommend a reliable horizontal slider plugin for react-native? I've come across some options, but they haven't been working as smoothly as I'd hoped. ...

Using Rails Devise as a backend to validate users in an Ionic application

Is anyone experienced with setting up authentication for an Ionic app connected to a Rails API using Devise? If so, I'd love to hear about your implementation and any helpful tutorials you've used. Please share your insights. ...

The functionality for tabbed content seems to be malfunctioning on Chrome and Firefox, however it works perfectly

In my index.js file, I have various functions set up like so: // a and b is placed at index.jsp $("#a").click(function (){ //this works on index.jsp and display.jsp(where the servlets forwards it). $("#b").load('servletA.html?action=dis ...

The CORS policy is preventing the AJAX request from functioning properly on localhost

Recently, I have been working on an Angular application that requires interaction with an API. To test it out, I set up an API on my localhost and made a request using AJAX. However, I encountered the dreaded CORS error. Despite trying various solutions fo ...

Step-by-step guide on incorporating Google Fonts and Material Icons into Vue 3 custom web components

While developing custom web components with Vue 3 for use in various web applications, I encountered an issue related to importing fonts and Google material icons due to the shadow-root. Currently, my workaround involves adding the stylesheet link tag to t ...

Incorporating z-index into weekly rows within the FullCalendar interface

I'm facing an issue where my detail dropdowns on events are being cropped by the following row. Is there a solution to adjust the z-index of each week row (.fc-row) in the monthly view, arranging them in descending order? For example, setting the z-i ...