@ckeditor/ckeditor5-editor-classic
Version:
Classic editor implementation for CKEditor 5.
420 lines (415 loc) • 20.5 kB
JavaScript
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import { EditorUI, _initMenuBar, DialogView, normalizeToolbarConfig, BoxedEditorUIView, StickyPanelView, ToolbarView, MenuBarView, InlineEditableUIView } from '@ckeditor/ckeditor5-ui/dist/index.js';
import { enablePlaceholder } from '@ckeditor/ckeditor5-engine/dist/index.js';
import { Rect, ElementReplacer, CKEditorError, getDataFromElement } from '@ckeditor/ckeditor5-utils/dist/index.js';
import { ElementApiMixin, Editor, attachToForm, Context } from '@ckeditor/ckeditor5-core/dist/index.js';
import { EditorWatchdog, ContextWatchdog } from '@ckeditor/ckeditor5-watchdog/dist/index.js';
import { isElement as isElement$1 } from 'lodash-es';
class ClassicEditorUI extends EditorUI {
/**
* @inheritDoc
*/ get element() {
return this.view.element;
}
/**
* Initializes the UI.
*
* @param replacementElement The DOM element that will be the source for the created editor.
*/ init(replacementElement) {
const editor = this.editor;
const view = this.view;
const editingView = editor.editing.view;
const editable = view.editable;
const editingRoot = editingView.document.getRoot();
// The editable UI and editing root should share the same name. Then name is used
// to recognize the particular editable, for instance in ARIA attributes.
editable.name = editingRoot.rootName;
view.render();
// The editable UI element in DOM is available for sure only after the editor UI view has been rendered.
// But it can be available earlier if a DOM element has been passed to BalloonEditor.create().
const editableElement = editable.element;
// Register the editable UI view in the editor. A single editor instance can aggregate multiple
// editable areas (roots) but the classic editor has only one.
this.setEditableElement(editable.name, editableElement);
// Let the editable UI element respond to the changes in the global editor focus
// tracker. It has been added to the same tracker a few lines above but, in reality, there are
// many focusable areas in the editor, like balloons, toolbars or dropdowns and as long
// as they have focus, the editable should act like it is focused too (although technically
// it isn't), e.g. by setting the proper CSS class, visually announcing focus to the user.
// Doing otherwise will result in editable focus styles disappearing, once e.g. the
// toolbar gets focused.
view.editable.bind('isFocused').to(this.focusTracker);
// Bind the editable UI element to the editing view, making it an end– and entry–point
// of the editor's engine. This is where the engine meets the UI.
editingView.attachDomRoot(editableElement);
// If an element containing the initial data of the editor was provided, replace it with
// an editor instance's UI in DOM until the editor is destroyed. For instance, a <textarea>
// can be such element.
if (replacementElement) {
this._elementReplacer.replace(replacementElement, this.element);
}
this._initPlaceholder();
this._initToolbar();
if (view.menuBarView) {
_initMenuBar(editor, view.menuBarView);
}
this._initDialogPluginIntegration();
this.fire('ready');
}
/**
* @inheritDoc
*/ destroy() {
super.destroy();
const view = this.view;
const editingView = this.editor.editing.view;
this._elementReplacer.restore();
editingView.detachDomRoot(view.editable.name);
view.destroy();
}
/**
* Initializes the editor toolbar.
*/ _initToolbar() {
const view = this.view;
// Set–up the sticky panel with toolbar.
view.stickyPanel.bind('isActive').to(this.focusTracker, 'isFocused');
view.stickyPanel.limiterElement = view.element;
view.stickyPanel.bind('viewportTopOffset').to(this, 'viewportOffset', ({ top })=>top || 0);
view.toolbar.fillFromConfig(this._toolbarConfig, this.componentFactory);
// Register the toolbar so it becomes available for Alt+F10 and Esc navigation.
this.addToolbar(view.toolbar);
}
/**
* Enable the placeholder text on the editing root.
*/ _initPlaceholder() {
const editor = this.editor;
const editingView = editor.editing.view;
const editingRoot = editingView.document.getRoot();
const sourceElement = editor.sourceElement;
let placeholderText;
const placeholder = editor.config.get('placeholder');
if (placeholder) {
placeholderText = typeof placeholder === 'string' ? placeholder : placeholder[this.view.editable.name];
}
if (!placeholderText && sourceElement && sourceElement.tagName.toLowerCase() === 'textarea') {
placeholderText = sourceElement.getAttribute('placeholder');
}
if (placeholderText) {
editingRoot.placeholder = placeholderText;
}
enablePlaceholder({
view: editingView,
element: editingRoot,
isDirectHost: false,
keepOnFocus: true
});
}
/**
* Provides an integration between the sticky toolbar and {@link module:utils/dom/scroll~scrollViewportToShowTarget}.
* It allows the UI-agnostic engine method to consider the geometry of the
* {@link module:editor-classic/classiceditoruiview~ClassicEditorUIView#stickyPanel} that pins to the
* edge of the viewport and can obscure the user caret after scrolling the window.
*
* @param evt The `scrollToTheSelection` event info.
* @param data The payload carried by the `scrollToTheSelection` event.
* @param originalArgs The original arguments passed to `scrollViewportToShowTarget()` method (see implementation to learn more).
*/ _handleScrollToTheSelectionWithStickyPanel(evt, data, originalArgs) {
const stickyPanel = this.view.stickyPanel;
if (stickyPanel.isSticky) {
const stickyPanelHeight = new Rect(stickyPanel.element).height;
data.viewportOffset.top += stickyPanelHeight;
} else {
const scrollViewportOnPanelGettingSticky = ()=>{
this.editor.editing.view.scrollToTheSelection(originalArgs);
};
this.listenTo(stickyPanel, 'change:isSticky', scrollViewportOnPanelGettingSticky);
// This works as a post-scroll-fixer because it's impossible predict whether the panel will be sticky after scrolling or not.
// Listen for a short period of time only and if the toolbar does not become sticky very soon, cancel the listener.
setTimeout(()=>{
this.stopListening(stickyPanel, 'change:isSticky', scrollViewportOnPanelGettingSticky);
}, 20);
}
}
/**
* Provides an integration between the sticky toolbar and {@link module:ui/dialog/dialog the Dialog plugin}.
*
* It moves the dialog down to ensure that the
* {@link module:editor-classic/classiceditoruiview~ClassicEditorUIView#stickyPanel sticky panel}
* used by the editor UI will not get obscured by the dialog when the dialog uses one of its automatic positions.
*/ _initDialogPluginIntegration() {
if (!this.editor.plugins.has('Dialog')) {
return;
}
const stickyPanel = this.view.stickyPanel;
const dialogPlugin = this.editor.plugins.get('Dialog');
dialogPlugin.on('show', ()=>{
const dialogView = dialogPlugin.view;
dialogView.on('moveTo', (evt, data)=>{
// Engage only when the panel is sticky, and the dialog is using one of default positions.
if (!stickyPanel.isSticky || dialogView.wasMoved) {
return;
}
const stickyPanelContentRect = new Rect(stickyPanel.contentPanelElement);
if (data[1] < stickyPanelContentRect.bottom + DialogView.defaultOffset) {
data[1] = stickyPanelContentRect.bottom + DialogView.defaultOffset;
}
}, {
priority: 'high'
});
}, {
priority: 'low'
});
}
/**
* Creates an instance of the classic editor UI class.
*
* @param editor The editor instance.
* @param view The view of the UI.
*/ constructor(editor, view){
super(editor);
this.view = view;
this._toolbarConfig = normalizeToolbarConfig(editor.config.get('toolbar'));
this._elementReplacer = new ElementReplacer();
this.listenTo(editor.editing.view, 'scrollToTheSelection', this._handleScrollToTheSelectionWithStickyPanel.bind(this));
}
}
class ClassicEditorUIView extends BoxedEditorUIView {
/**
* @inheritDoc
*/ render() {
super.render();
if (this.menuBarView) {
// Set toolbar as a child of a stickyPanel and makes toolbar sticky.
this.stickyPanel.content.addMany([
this.menuBarView,
this.toolbar
]);
} else {
this.stickyPanel.content.add(this.toolbar);
}
this.top.add(this.stickyPanel);
this.main.add(this.editable);
}
/**
* Creates an instance of the classic editor UI view.
*
* @param locale The {@link module:core/editor/editor~Editor#locale} instance.
* @param editingView The editing view instance this view is related to.
* @param options Configuration options for the view instance.
* @param options.shouldToolbarGroupWhenFull When set `true` enables automatic items grouping
* in the main {@link module:editor-classic/classiceditoruiview~ClassicEditorUIView#toolbar toolbar}.
* See {@link module:ui/toolbar/toolbarview~ToolbarOptions#shouldGroupWhenFull} to learn more.
*/ constructor(locale, editingView, options = {}){
super(locale);
this.stickyPanel = new StickyPanelView(locale);
this.toolbar = new ToolbarView(locale, {
shouldGroupWhenFull: options.shouldToolbarGroupWhenFull
});
if (options.useMenuBar) {
this.menuBarView = new MenuBarView(locale);
}
this.editable = new InlineEditableUIView(locale, editingView);
}
}
/**
* The {@glink installation/getting-started/predefined-builds#classic-editor classic editor} implementation.
* It uses an inline editable and a sticky toolbar, all enclosed in a boxed UI.
* See the {@glink examples/builds/classic-editor demo}.
*
* In order to create a classic editor instance, use the static
* {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method.
*
* # Classic editor and classic build
*
* The classic editor can be used directly from source (if you installed the
* [`@ckeditor/ckeditor5-editor-classic`](https://www.npmjs.com/package/@ckeditor/ckeditor5-editor-classic) package)
* but it is also available in the {@glink installation/getting-started/predefined-builds#classic-editor classic build}.
*
* {@glink installation/getting-started/predefined-builds Builds}
* are ready-to-use editors with plugins bundled in. When using the editor from
* source you need to take care of loading all plugins by yourself
* (through the {@link module:core/editor/editorconfig~EditorConfig#plugins `config.plugins`} option).
* Using the editor from source gives much better flexibility and allows easier customization.
*
* Read more about initializing the editor from source or as a build in
* {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}.
*/ class ClassicEditor extends ElementApiMixin(Editor) {
/**
* Destroys the editor instance, releasing all resources used by it.
*
* Updates the original editor element with the data if the
* {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy `updateSourceElementOnDestroy`}
* configuration option is set to `true`.
*/ destroy() {
if (this.sourceElement) {
this.updateSourceElement();
}
this.ui.destroy();
return super.destroy();
}
/**
* Creates a new classic editor instance.
*
* There are three ways how the editor can be initialized.
*
* # Replacing a DOM element (and loading data from it)
*
* You can initialize the editor using an existing DOM element:
*
* ```ts
* ClassicEditor
* .create( document.querySelector( '#editor' ) )
* .then( editor => {
* console.log( 'Editor was initialized', editor );
* } )
* .catch( err => {
* console.error( err.stack );
* } );
* ```
*
* The element's content will be used as the editor data and the element will be replaced by the editor UI.
*
* # Creating a detached editor
*
* Alternatively, you can initialize the editor by passing the initial data directly as a string.
* In this case, the editor will render an element that must be inserted into the DOM:
*
* ```ts
* ClassicEditor
* .create( '<p>Hello world!</p>' )
* .then( editor => {
* console.log( 'Editor was initialized', editor );
*
* // Initial data was provided so the editor UI element needs to be added manually to the DOM.
* document.body.appendChild( editor.ui.element );
* } )
* .catch( err => {
* console.error( err.stack );
* } );
* ```
*
* This lets you dynamically append the editor to your web page whenever it is convenient for you. You may use this method if your
* web page content is generated on the client side and the DOM structure is not ready at the moment when you initialize the editor.
*
* # Replacing a DOM element (and data provided in `config.initialData`)
*
* You can also mix these two ways by providing a DOM element to be used and passing the initial data through the configuration:
*
* ```ts
* ClassicEditor
* .create( document.querySelector( '#editor' ), {
* initialData: '<h2>Initial data</h2><p>Foo bar.</p>'
* } )
* .then( editor => {
* console.log( 'Editor was initialized', editor );
* } )
* .catch( err => {
* console.error( err.stack );
* } );
* ```
*
* This method can be used to initialize the editor on an existing element with the specified content in case if your integration
* makes it difficult to set the content of the source element.
*
* Note that an error will be thrown if you pass the initial data both as the first parameter and also in the configuration.
*
* # Configuring the editor
*
* See the {@link module:core/editor/editorconfig~EditorConfig editor configuration documentation} to learn more about
* customizing plugins, toolbar and more.
*
* # Using the editor from source
*
* The code samples listed in the previous sections of this documentation assume that you are using an
* {@glink installation/getting-started/predefined-builds editor build} (for example – `@ckeditor/ckeditor5-build-classic`).
*
* If you want to use the classic editor from source (`@ckeditor/ckeditor5-editor-classic/src/classiceditor`),
* you need to define the list of
* {@link module:core/editor/editorconfig~EditorConfig#plugins plugins to be initialized} and
* {@link module:core/editor/editorconfig~EditorConfig#toolbar toolbar items}. Read more about using the editor from
* source in the {@glink installation/advanced/alternative-setups/integrating-from-source-webpack dedicated guide}.
*
* @param sourceElementOrData The DOM element that will be the source for the created editor
* or the editor's initial data.
*
* If a DOM element is passed, its content will be automatically loaded to the editor upon initialization
* and the {@link module:editor-classic/classiceditorui~ClassicEditorUI#element editor element} will replace the passed element
* in the DOM (the original one will be hidden and the editor will be injected next to it).
*
* If the {@link module:core/editor/editorconfig~EditorConfig#updateSourceElementOnDestroy updateSourceElementOnDestroy}
* option is set to `true`, the editor data will be set back to the original element once the editor is destroyed and when a form,
* in which this element is contained, is submitted (if the original element is a `<textarea>`). This ensures seamless integration
* with native web forms.
*
* If the initial data is passed, a detached editor will be created. In this case you need to insert it into the DOM manually.
* It is available under the {@link module:editor-classic/classiceditorui~ClassicEditorUI#element `editor.ui.element`} property.
*
* @param config The editor configuration.
* @returns A promise resolved once the editor is ready. The promise resolves with the created editor instance.
*/ static create(sourceElementOrData, config = {}) {
return new Promise((resolve)=>{
const editor = new this(sourceElementOrData, config);
resolve(editor.initPlugins().then(()=>editor.ui.init(isElement(sourceElementOrData) ? sourceElementOrData : null)).then(()=>editor.data.init(editor.config.get('initialData'))).then(()=>editor.fire('ready')).then(()=>editor));
});
}
/**
* Creates an instance of the classic editor.
*
* **Note:** do not use the constructor to create editor instances. Use the static
* {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`} method instead.
*
* @param sourceElementOrData The DOM element that will be the source for the created editor
* or the editor's initial data. For more information see
* {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}.
* @param config The editor configuration.
*/ constructor(sourceElementOrData, config = {}){
// If both `config.initialData` is set and initial data is passed as the constructor parameter, then throw.
if (!isElement(sourceElementOrData) && config.initialData !== undefined) {
// Documented in core/editor/editorconfig.jsdoc.
// eslint-disable-next-line ckeditor5-rules/ckeditor-error-message
throw new CKEditorError('editor-create-initial-data', null);
}
super(config);
this.config.define('menuBar.isVisible', false);
if (this.config.get('initialData') === undefined) {
this.config.set('initialData', getInitialData(sourceElementOrData));
}
if (isElement(sourceElementOrData)) {
this.sourceElement = sourceElementOrData;
}
this.model.document.createRoot();
const shouldToolbarGroupWhenFull = !this.config.get('toolbar.shouldNotGroupWhenFull');
const menuBarConfig = this.config.get('menuBar');
const view = new ClassicEditorUIView(this.locale, this.editing.view, {
shouldToolbarGroupWhenFull,
useMenuBar: menuBarConfig.isVisible
});
this.ui = new ClassicEditorUI(this, view);
attachToForm(this);
}
}
/**
* The {@link module:core/context~Context} class.
*
* Exposed as static editor field for easier access in editor builds.
*/ ClassicEditor.Context = Context;
/**
* The {@link module:watchdog/editorwatchdog~EditorWatchdog} class.
*
* Exposed as static editor field for easier access in editor builds.
*/ ClassicEditor.EditorWatchdog = EditorWatchdog;
/**
* The {@link module:watchdog/contextwatchdog~ContextWatchdog} class.
*
* Exposed as static editor field for easier access in editor builds.
*/ ClassicEditor.ContextWatchdog = ContextWatchdog;
function getInitialData(sourceElementOrData) {
return isElement(sourceElementOrData) ? getDataFromElement(sourceElementOrData) : sourceElementOrData;
}
function isElement(value) {
return isElement$1(value);
}
export { ClassicEditor };
//# sourceMappingURL=index.js.map