TinyMCE has a setup property which allows you to specify a callback that will be executed before the TinyMCE editor instance is rendered:
setup: function (editor) {
editor.on('click', function () {
console.log('Editor was clicked');
});
editor which you give in the setup function, allows you to add predefined component to toolbar. Unfortunately it doesn't support input or something like that, But you can add this components to toolbar. One of them is button. So it can be added to toolbar like this:
setup: function (editor) {
editor.ui.registry.addButton('customInsertButton', {
text: 'My Button',
onAction: function (_) {
editor.insertContent(' <strong>It\'s my button!</strong> ');
}
});
}
In addition you should configure toolbar with added button:
toolbar: 'customInsertButton'
Also you can access to editors body content and change styles with the following function:
editor.getBody().style
Based on this description, you can add 2 buttons to the toolbar for changing fontsize which one of them is increasing fontsize and another for decreasing fontsize:
setup: (editor) => {
editor.ui.registry.addButton("btnIncreaseFontSize", {
text: "A",
icon: "action-prev",
onAction: (api) => {
let fontSize = editor.getBody().style.fontSize;
if (!fontSize) fontSize = defaultFontSize;
let value = parseInt(fontSize.substring(0, 2));
fontSize = `${value + 1}px`;
editor.getBody().style.fontSize = fontSize;
}
});
editor.ui.registry.addButton("btnDecreaseFontSize", {
text: "A",
icon: "action-next",
onAction: (api) => {
let fontSize = editor.getBody().style.fontSize;
if (!fontSize) fontSize = defaultFontSize;
let value = parseInt(fontSize.substring(0, 2));
fontSize = `${value - 1}px`;
editor.getBody().style.fontSize = fontSize;
}
});
}
Note: for icon property had been used from TinyMCEs default icon pack
Continually configure the toolbar like:
toolbar:
"undo redo | formatselect | " +
"bold italic backcolor | alignleft aligncenter " +
"alignright alignjustify | bullist numlist outdent indent | " +
"removeformat | help | btnIncreaseFontSize | btnDecreaseFontSize",
content_style: `body { font-family:Helvetica,Arial,sans-serif; font-size:${defaultFontSize} }`,
And here's the defaultFontSize:
const defaultFontSize = "12px";
At the end here's the full code:
export default function App() {
const defaultFontSize = "12px";
return (
<>
<Editor
initialValue="<p>This is the initial content of the editor.</p>"
init={{
height: 500,
menubar: false,
plugins: [
"advlist autolink lists link image charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table paste code help wordcount"
],
toolbar:
"undo redo | formatselect | " +
"bold italic backcolor | alignleft aligncenter " +
"alignright alignjustify | bullist numlist outdent indent | " +
"removeformat | help | btnIncreaseFontSize | btnDecreaseFontSize",
content_style: `body { font-family:Helvetica,Arial,sans-serif; font-size:${defaultFontSize} }`,
setup: (editor) => {
editor.ui.registry.addButton("btnIncreaseFontSize", {
text: "A",
icon: "action-prev",
onAction: (api) => {
let fontSize = editor.getBody().style.fontSize;
if (!fontSize) fontSize = defaultFontSize;
let value = parseInt(fontSize.substring(0, 2));
fontSize = `${value + 1}px`;
editor.getBody().style.fontSize = fontSize;
}
});
editor.ui.registry.addButton("btnDecreaseFontSize", {
text: "A",
icon: "action-next",
onAction: (api) => {
let fontSize = editor.getBody().style.fontSize;
if (!fontSize) fontSize = defaultFontSize;
let value = parseInt(fontSize.substring(0, 2));
fontSize = `${value - 1}px`;
editor.getBody().style.fontSize = fontSize;
}
});
}
}}
/>
</>
);
}
