CKEditor
in package
CKEditor 5 helper for Tsugi applications.
Provides reusable configuration, styles, and utilities for CKEditor 5 so multiple controllers (Pages, Announcements, etc.) can use it DRY.
Usage: CKEditor::renderScriptTag(); CKEditor::renderConfigScript(['pagesBase' => $url, 'appHome' => $url]); CKEditor::renderStyles(['includeLinkPicker' => true]);
Pages uses a newer modular build (font, text color, styled rules) via renderPagesEditorAssets() and renderPagesEditorModule(). The module imports ckeditor.js by its full URL. Other tools stay on the classic build from CDN_URL.
Table of Contents
Constants
- CDN_URL : mixed = 'https://cdn.ckeditor.com/ckeditor5/16.0.0/clas...
- Default CDN URL for CKEditor 5 Classic build.
- DEFAULT_TOOLBAR : mixed = ['heading', '|', 'bold', 'italic', 'link', 'bul...
- Default toolbar items for rich text editing.
- PAGES_TOOLBAR : mixed = ['heading', '|', 'fontFamily', 'fontSize', 'fon...
- Pages toolbar: the classic items plus font, text color, and styled rules.
- PAGES_VERSION : mixed = '49.1.0'
- Modular CKEditor 5 used by the Pages editor (font, color, styled rules).
Methods
- renderConfigScript() : mixed
- Output JavaScript that sets ClassicEditor.defaultConfig.
- renderLinkPickerModal() : mixed
- Output the standard link picker modal HTML.
- renderLinkPickerScript() : mixed
- Output the Insert-link picker JavaScript (fetch, expandos, modal, toolbar button).
- renderPagesEditorAssets() : mixed
- Stylesheet for the Pages editor.
- renderPagesEditorModule() : mixed
- Module that creates the Pages editor: fonts, text color, and styled rules.
- renderScriptTag() : mixed
- Output the script tag to load CKEditor from CDN.
- renderStyles() : mixed
- Output CSS styles for CKEditor and optional link picker modal.
- pagesColors() : array<int, array{color: string, label: string, hasBorder?: bool}>
- Text colors shared by the font color dropdown and the line picker.
- pagesEditorBase() : mixed
- Absolute URL of the Pages CKEditor folder, with a trailing slash.
- pagesFontSizes() : array<int, int|string>
- Pixel sizes. Inline font-size survives outside the editor.
- pagesRuleThicknesses() : array<int, array{label: string, value: string}>
- Line thicknesses. The value is the rendered bar height.
Constants
CDN_URL
Default CDN URL for CKEditor 5 Classic build.
public
mixed
CDN_URL
= 'https://cdn.ckeditor.com/ckeditor5/16.0.0/classic/ckeditor.js'
DEFAULT_TOOLBAR
Default toolbar items for rich text editing.
public
mixed
DEFAULT_TOOLBAR
= ['heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote', 'insertTable', 'mediaEmbed', 'undo', 'redo']
PAGES_TOOLBAR
Pages toolbar: the classic items plus font, text color, and styled rules.
public
mixed
PAGES_TOOLBAR
= ['heading', '|', 'fontFamily', 'fontSize', 'fontColor', 'highlight', '|', 'bold', 'italic', 'link', '|', 'alignment', 'bulletedList', 'numberedList', '|', 'blockQuote', 'codeBlock', 'styledHorizontalLine', 'insertTable', 'mediaEmbed', '|', 'undo', 'redo']
PAGES_VERSION
Modular CKEditor 5 used by the Pages editor (font, color, styled rules).
public
mixed
PAGES_VERSION
= '49.1.0'
Methods
renderConfigScript()
Output JavaScript that sets ClassicEditor.defaultConfig.
public
static renderConfigScript([array<string|int, mixed> $options = [] ]) : mixed
The link decorator callback uses pagesBase and appHome from the caller's scope - ensure those variables are defined before this runs.
Parameters
- $options : array<string|int, mixed> = []
-
Optional overrides:
- toolbar: array of toolbar item names (default: DEFAULT_TOOLBAR)
- automaticExternalBlank: bool, auto target=_blank on external URLs (default: true). Set false when the author must be able to clear target=_blank; the automatic decorator puts it back on getData() even if the manual switch is off.
renderLinkPickerModal()
Output the standard link picker modal HTML.
public
static renderLinkPickerModal([string $modalTitle = 'Insert link' ]) : mixed
Uses id="page-link-modal" and id="page-link-list" for the modal and list container. Pair with renderLinkPickerScript() to populate the list.
Parameters
- $modalTitle : string = 'Insert link'
-
Optional title (default: 'Insert link')
renderLinkPickerScript()
Output the Insert-link picker JavaScript (fetch, expandos, modal, toolbar button).
public
static renderLinkPickerScript() : mixed
Caller must define before this runs: pagesJsonUrl, lessonsJsonUrl, pagesBase, appHome, currentPageId Optional: filesJsonUrl, filesBase
Expects #editor_body, #page_form, and the modal from renderLinkPickerModal().
renderPagesEditorAssets()
Stylesheet for the Pages editor.
public
static renderPagesEditorAssets() : mixed
The files are a self-hosted GPL build of CKEditor 5 (PAGES_VERSION) at lib/src/Controllers/static/Pages/. The CDN build is the commercial "cloud" channel and opens read-only when licenseKey is GPL.
The editor script imports that file by URL. An import map cannot be added here: the page footer already loads other modules, and a map that appears after those is ignored.
Call this before renderPagesEditorModule(). Quiz, catalog, and site keep renderScriptTag() and the classic build.
renderPagesEditorModule()
Module that creates the Pages editor: fonts, text color, and styled rules.
public
static renderPagesEditorModule() : mixed
The caller sets window.tsugiPagesEditor and includes renderConfigScript() plus renderLinkPickerScript() first, so this can call tsugiBindEditor().
renderScriptTag()
Output the script tag to load CKEditor from CDN.
public
static renderScriptTag([string|null $url = null ]) : mixed
Parameters
- $url : string|null = null
-
Optional override for CDN URL
renderStyles()
Output CSS styles for CKEditor and optional link picker modal.
public
static renderStyles([array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string|int, mixed> = []
-
Optional:
- includeLinkPicker: bool, include modal/expando styles (default: false)
- includeLinkUnderline: bool, underline links in editor and content (default: true)
- extraStyles: string, additional CSS to append (e.g. .ckeditor-container { min-height: 400px; })
pagesColors()
Text colors shared by the font color dropdown and the line picker.
private
static pagesColors() : array<int, array{color: string, label: string, hasBorder?: bool}>
Return values
array<int, array{color: string, label: string, hasBorder?: bool}>pagesEditorBase()
Absolute URL of the Pages CKEditor folder, with a trailing slash.
private
static pagesEditorBase() : mixed
pagesFontSizes()
Pixel sizes. Inline font-size survives outside the editor.
private
static pagesFontSizes() : array<int, int|string>
Return values
array<int, int|string>pagesRuleThicknesses()
Line thicknesses. The value is the rendered bar height.
private
static pagesRuleThicknesses() : array<int, array{label: string, value: string}>