Start trial
PricingContact Us
Log InStart For Free

How to set up source code editing with TinyMCE

April 6th, 2023

4 min read

The words Advanced code editor surrounded by symbols for code editing.

Written by

Joe Robinson

Category

How-to Use TinyMCE

Power users can swap between writing in a text editor, and editing the HTML that supports their writing. But are there enough power users out there to justify including the ability to edit their HTML? It may end up costing you too much time to develop. 

An alternative: you can rely on the rich text editor in your app to enable source code editing. 

Finding and configuring a rich text editor that can provide easy-to-implement source code editing saves you time and energy learning how to seamlessly switch from the WYSIWYG view to the HTML view. Instead, take a look into TinyMCE.

TinyMCE not only provides a great source code editing plugin, but now offers several upgrades. This article explains how to configure the plugins for source code editing. 

What is a source code editor?

A source code editor in an application allows you to edit the code that makes up the content. The most common case is manually editing the HTML tags and the tag attributes that the rich text editor translates into the content seen on the screen. Different customers make use of source code editors, like power users editing the HTML behind emails, or  Web developers fixing broken code in a CMS.

The term “source code editor” has other definitions that relate to different contexts. For example, a source code editor can also be described as a text editor that specialized for writing software.

How to configure source code editing with TinyMCE

Setting up TinyMCE

To get started configuring a source code editor, you need an API key. Navigate to the Get-tiny sign up page to get your FREE WYSIWYG editor API key, which comes with 14 days access to Premium plugins.

You can use Google or GitHub account details to log into your TinyMCE account, and get an API key.

With that ready, the try out the source code editor:

  1. Create an index.html file in your environment, and include HTML to get started:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
</body>
</html>
  1. Open the index.html file, and in the head section, add script tags, and reference TinyMCE Cloud through a CDN link within the script tags. This is what the link looks like:

<script
  src="https://cdn.tiny.cloud/1/your-api-key/tinymce/6/tinymce.min.js"
  referrerpolicy="origin"
></script>;

Note: For the Tiny Cloud connection, creating an account with TinyMCE gets you a FREE API key. When you use this key, you get access to Premium TinyMCE plugins for 14 days, as well as no warning messages concerning API keys in the text area.

  1. Add another pair of script tags, add the following JavaScript, which is the tinymce.init method, which is where the source code editor configurations are set:

<script>
   tinymce.init({
   selector: '#editor',
   plugins: 'powerpaste casechange searchreplace autolink directionality visualblocks visualchars image link media mediaembed codesample table charmap pagebreak nonbreaking anchor tableofcontents insertdatetime advlist lists checklist wordcount tinymcespellchecker editimage help formatpainter permanentpen charmap linkchecker emoticons advtable export autosave',
   toolbar: 'undo redo print spellcheckdialog formatpainter | blocks fontfamily fontsize | bold italic underline forecolor backcolor | link image | alignleft aligncenter alignright alignjustify',
   height: '600px'
       });
</script>
  1. Add initial HTML content, and the CSS selector class to some textarea tags. The selector is set as an id with the value “editor” in the TinyMCE init script:

<body>
   <h1>TinyMCE Quick Start Guide</h1> 
  <form id="post">
       <textarea id="editor">Hello, World!</textarea> 
  </form>
</body>;

 

  1. Test run the index.html file by opening it in your browser, or use a local server command with Python or with the PHP command:

TinyMCE demo working in the browser

 

Configuring the source code editor

With the TinyMCE init script ready, configure the source code editor by adding the following plugins:

  1. Include the advanced plugin, and the fullscreen plugins:

plugins: 'powerpaste casechange searchreplace autolink directionality visualblocks visualchars image link media mediaembed codesample table charmap pagebreak nonbreaking anchor tableofcontents insertdatetime advlist lists checklist wordcount tinymcespellchecker editimage help formatpainter permanentpen charmap linkchecker emoticons advtable export autosave advcode fullscreen',
  1. Include the Advanced Code inline option, and set it to “true”:

            tinymce.init({
                selector: '#editor',
…
                advcode_inline: true,
            });
  1. Add the Source Code editor button to the TinyMCE toolbar:

toolbar: "undo redo print spellcheckdialog formatpainter | blocks fontfamily fontsize | bold italic underline forecolor backcolor | link image | alignleft aligncenter alignright alignjustify | code",
  1. With these adjustments made, reload the demo, and press the Source Code button to open the source code editor:

The source code editor open and running in the browser

Controlling the source code editor

With the source code editor configured, you can make use of the interface options:

  1. Change to full screen if needed.
    1. This requires the Full Screen plugin included along with the Advanced Code plugin. 
    2. You also need to enable the Advanced Code Inline option (set it to “true”) to make use of the full screen option.
  2. Switch to Dark or Light mode
  3. Grow or shrink down the font size.
    1. The font size cannot decrease lower than 5px.
  4. Copy the code to the clipboard.

Making use of the copy code button

To test out the ability to copy code, the following demo makes use of the TinyMCE DMS solution content:

  1. Open the Source Code Editor
  2. Click on the Copy Code button

The dark mode view active in the source code editor

  1. With the HTML now on your clipboard, paste the HTML code where it is needed

TinyMCE source code editing view again

Source code editors support your customers

Different customers bring different experience levels with them when they use your app to write their content. And it shouldn’t be a significant investment of your time to be able to give them  control over their content, by modifying the source code (if they want to).

By following this article’s demo, you can configure the TinyMCE Advanced Code plugin and show how an enterprise-grade editor can be set up rapidly.

You can contact us if you need more information on source code editing to support your customers’ needs. 

TinyMCEDevelopersTiny ReleasesPlugins
byJoe Robinson

Technical and creative writer, editor, and a TinyMCE advocate. An enthusiast for teamwork, open source software projects, and baking. Can often be found puzzling over obscure history, cryptic words, and lucid writing.

Related Articles

  • How-to Use TinyMCENov 21st, 2024

    What are Line Breaks? Learn How to Add and Remove Them

Join 100,000+ developers who get regular tips & updates from the Tiny team.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Tiny logo

Stay Connected

SOC2 compliance badge

Products

TinyMCEDriveMoxieManager
© Copyright 2024 Tiny Technologies Inc.

TinyMCE® and Tiny® are registered trademarks of Tiny Technologies, Inc.