Skip to main content
Version: Fegg 5.x

Rich text (FeggEditor)

Rich text fields use FeggEditor, an editor built on Tiptap (MIT). Existing rich text opens in it as it is (images, YouTube videos, tables).

Using it in a CMS screen​

Mark the textarea with data-fegg-editor and load the editor in the screen's html_head.tpl. Every textarea[data-fegg-editor] becomes an editor when the page is ready; no script of your own is needed.

site/template/cms/tpl/news/form.tpl
<textarea name="content" data-fegg-editor="full">{{ $page.data.content }}</textarea>
<textarea name="summary" data-fegg-editor="simple">{{ $page.data.summary }}</textarea>
site/template/cms/tpl/news/html_head.tpl
<link rel="stylesheet" type="text/css" href="{{ $site.cms_assets }}/js/editor.css" />
<script type="text/javascript" src="{{ $site.cms_assets }}/js/modal.js"></script>
<script type="text/javascript" src="{{ $site.cms_assets }}/js/modal/image.js"></script>
<script type="text/javascript" src="{{ $site.cms_assets }}/js/modal/file.js"></script>
<script type="text/javascript" src="{{ $site.cms_assets }}/js/editor.js"></script>
  • full: every block. simple: inline formatting and links only.
  • Several fields on one page are independent editors (their own toolbar, undo and draft).
  • For a textarea added to the page later, call FeggEditor.attach(textarea, { toolbar: 'full' }).
  • While text is typed, a draft is kept in the browser. When the page opens with a draft different from the saved text, the editor offers to restore it.

Saving and showing it​

The textarea receives the HTML when the form is sent. Always clean each field on the server before saving it:

site/src/Controller/Cms/News.php
use Fegg\Cms\HtmlSanitizer;

$data['content'] = HtmlSanitizer::clean($this->in('content'));

HtmlSanitizer::clean() keeps only the tags, attributes, classes and URLs the editor writes (below). Everything else is removed: scripts, event handlers, javascript: URLs, other iframes, other classes. The saved HTML can then be printed as it is, inside .c-entry-block:

site/template/main/tpl/news/detail.tpl
<div class="c-entry-block">
{{ $page.data.content|noescape }}
</div>
danger

Never print a rich text field with noescape unless it was saved through HtmlSanitizer::clean(). Text saved before the project used FeggEditor, or written by another tool, is cleaned only when it is saved again: until then, the controller can clean it before showing it ($row['content'] = HtmlSanitizer::clean($row['content']);).

What the editor writes​

BlockHTMLClasses
Image<figure class="fegg-image aligncenter" style="width: 50%"><a href><img></a><figcaption>alignleft / aligncenter / alignright; width 1–100 %; is-zoom: shown large on a click (no link then)
Columns<div class="fegg-columns is-1-2"><div class="fegg-column">…</div>…</div>is-1-1, is-1-2, is-2-1, is-1-1-1
Button link<div class="fegg-button is-primary is-center"><a href>Text</a></div>is-primary (filled) / is-secondary (outlined), is-center
Gallery<div class="fegg-gallery is-cols-3 is-sp-cols-1 is-1-1 is-contain is-zoom"><figure><img></figure>…</div>is-cols-2 … is-cols-5; on a phone (body under 480px) is-sp-cols-1 / is-sp-cols-3 (2 without); frame is-16-9 / is-1-1 / is-3-4 (4:3 without); is-contain: whole images (filled and cut without); is-zoom
Google Maps<div class="fegg-map is-16-9 aligncenter" style="width: 50%"><iframe src="https://www.google.com/maps/embed?pb=…" loading="lazy"></iframe></div>ratio is-16-9 / is-1-1 (4:3 without), aligncenter / alignright; width 10–100 %
Link to a file<a class="fegg-file is-pdf" href>Name(PDF・1.2 MB)</a>is-pdf, is-word, is-excel, is-powerpoint, is-zip, is-other
YouTube<div data-youtube-video class="fegg-video is-9-16"><iframe src="https://www.youtube-nocookie.com/embed/…"></iframe></div>ratio is-9-16 (a short; 16:9 without), alignment and width as maps
Table<table><thead>…</thead><tbody>…</tbody></table>merged cells: colspan / rowspan

Plus paragraphs, headings (h2–h4, text-align), lists, quotes, lines, bold, italic, underline, strike, mark and links. The class names are written by the editor, so they are not FLOCSS names.

List items and table cells hold their text in a paragraph (<li><p>Item</p></li>, <td><p>1</p></td>), as Tiptap makes lists and tables. It is valid HTML; the public styles remove the paragraph margins there, so styles of your own for lists and cells count on li > p and td > p.

YouTube and maps outside the text​

A CMS field of its own that holds a YouTube URL or a Google map is printed with the same block as the editor (arguments and Fegg\Tool\Embed in PHP: see Embed):

site/template/main/tpl/news/detail.tpl
<div class="c-entry-block">
{{ $page.data.youtube_url|feggYoutube|noescape }}
{{ $page.data.map|feggMap|noescape }}
</div>

Its look on the public site​

The public styles are in the site's scss/object/component/_wysiwyg.scss (htdocs/_fegg/site/template/main/scss/..., .c-entry-block). Change them there for a project. Sites share no SCSS: another site that shows rich text copies it.

  • Blocks respond to the body's own width (container queries on .c-entry-block), not to the project's breakpoints: columns stack under 600px. Under 480px images lose their width and do not float (large ones fill the body, small ones keep their own size), maps and videos fill the body, and galleries use 2 columns (1 or 3 with is-sp-cols-1 / is-sp-cols-3). The editor uses the same values and says so next to the width of images, maps and videos.
  • Images and galleries with is-zoom open the image large in a modal (<dialog class="c-lightbox">) on a click or Enter, with its caption, and arrows in a gallery. The script is site/template/main/js/common/lightbox.js (loaded by siteframe.tpl), the styles site/template/main/scss/object/component/_lightbox.scss. Keep both when the project replaces the skeleton's frame; another site copies both if it shows galleries.