-
Notifications
You must be signed in to change notification settings - Fork 106
Pass DOM Element in the config object instead of editor constructor in CKEditor >= 48 #663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7ad90ad
Move multiroot hooks to `multiroot/` folder.
Mati365 67f6950
Add jsdocs
Mati365 f855b5e
More modifications.
Mati365 7115d7d
Split tests.
Mati365 bd1f589
fix `onError` handling regression
Mati365 171d5c2
Make tests run on v47 and v48 version of the editor.
Mati365 e510cd2
Add new attributes format support.
Mati365 ba95b28
More backport fixes
Mati365 cd1e055
Fix minor crashes.
Mati365 815ad77
Fix comments.
Mati365 1b23f4f
Strict attribute checks.
Mati365 ac9de18
Fix tests.
Mati365 4341d5c
Simplify code.
Mati365 4312de4
bump integrations common to `2.3.0`
Mati365 f85cdfd
fix react imports
Mati365 849eedf
fix import
Mati365 ac49d59
fix tests
Mati365 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** | ||
| * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. | ||
| * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options | ||
| */ | ||
|
|
||
| import type { Editor, ContextWatchdog, EditorConfig, ContextWatchdogItemConfiguration } from 'ckeditor5'; | ||
|
|
||
| import { uid } from '@ckeditor/ckeditor5-integrations-common'; | ||
|
|
||
| /** | ||
| * An adapter aligning the context watchdog API to the editor watchdog API for easier usage. | ||
| */ | ||
| export class EditorWatchdogAdapter<TEditor extends Editor> { | ||
| /** | ||
| * The context watchdog instance that will be wrapped into editor watchdog API. | ||
| */ | ||
| private readonly _contextWatchdog: ContextWatchdog; | ||
|
|
||
| /** | ||
| * A unique id for the adapter to distinguish editor items when using the context watchdog API. | ||
| */ | ||
| private readonly _id: string; | ||
|
|
||
| /** | ||
| * A watchdog's editor creator function. | ||
| */ | ||
| private _creator?: AdapterEditorCreatorFunction<TEditor>; | ||
|
|
||
| /** | ||
| * @param contextWatchdog The context watchdog instance that will be wrapped into editor watchdog API. | ||
| */ | ||
| constructor( contextWatchdog: ContextWatchdog ) { | ||
| this._contextWatchdog = contextWatchdog; | ||
| this._id = uid(); | ||
| } | ||
|
|
||
| /** | ||
| * @param creator A watchdog's editor creator function. | ||
| */ | ||
| public setCreator( creator: AdapterEditorCreatorFunction<TEditor> ): void { | ||
| this._creator = creator; | ||
| } | ||
|
|
||
| /** | ||
| * Adds an editor configuration to the context watchdog registry. Creates an instance of it. | ||
| * | ||
| * @param sourceElementOrData A source element or data for the new editor. | ||
| * @param config CKEditor 5 editor config. | ||
| */ | ||
| public create( sourceElementOrData: HTMLElement | string, config: EditorConfig ): Promise<unknown>; | ||
|
|
||
| public create( config: EditorConfig ): Promise<unknown>; | ||
|
|
||
| public create( sourceElementOrDataOrConfig: HTMLElement | string | EditorConfig, config?: EditorConfig ): Promise<unknown> { | ||
| let watchdogItemConfiguration: Record<string, any> = { | ||
| creator: this._creator!, | ||
| id: this._id, | ||
| type: 'editor' | ||
| }; | ||
|
|
||
| // Newer versions of the editor deprecated passing both source element and config at the same time. | ||
| // So, if the second argument (config) is present, the older version of the editor is being initialized. | ||
| /* istanbul ignore else -- @preserve */ | ||
| if ( config ) { | ||
| // <= 47 legacy config approach to watchdog configuration. | ||
| watchdogItemConfiguration = { | ||
| ...watchdogItemConfiguration, | ||
| sourceElementOrData: sourceElementOrDataOrConfig, | ||
| config | ||
| }; | ||
| } else { | ||
| // >= 48 single config approach to watchdog item configuration. | ||
| watchdogItemConfiguration = { | ||
| ...watchdogItemConfiguration, | ||
| config: sourceElementOrDataOrConfig | ||
| }; | ||
| } | ||
|
|
||
| return this._contextWatchdog.add( watchdogItemConfiguration as ContextWatchdogItemConfiguration ); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a listener that is attached to context watchdog's item and run when the context watchdog fires. | ||
| * Currently works only for the `error` event. | ||
| */ | ||
| public on( _: string, callback: ( _: null, data: { error: Error; causesRestart?: boolean } ) => void ): void { | ||
| // Assume that the event name was error. | ||
| this._contextWatchdog.on( 'itemError', ( _, { itemId, error } ) => { | ||
| if ( itemId === this._id ) { | ||
| callback( null, { error, causesRestart: undefined } ); | ||
| } | ||
| } ); | ||
| } | ||
|
|
||
| public destroy(): Promise<unknown> { | ||
| // Destroying an editor instance after destroying the Context is handled in the `ContextWatchdog` class. | ||
| // As `EditorWatchdogAdapter` is an adapter, we should not destroy the editor manually. | ||
| // Otherwise, it causes that the editor is destroyed twice. However, there is a case, when the editor | ||
| // needs to be removed from the context, without destroying the context itself. We may assume the following | ||
| // relations with `ContextWatchdog#state`: | ||
| // | ||
| // a) `ContextWatchdog#state` === 'ready' - context is not destroyed; it's safe to destroy the editor manually. | ||
| // b) `ContextWatchdog#state` === 'destroyed' - context is destroyed; let `ContextWatchdog` handle the whole process. | ||
| // | ||
| // See #354 for more information. | ||
| if ( this._contextWatchdog.state === 'ready' ) { | ||
| return this._contextWatchdog.remove( this._id ); | ||
| } | ||
|
|
||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| /** | ||
| * An editor instance. | ||
| */ | ||
| public get editor(): TEditor { | ||
| return this._contextWatchdog.getItem( this._id ) as TEditor; | ||
| } | ||
| } | ||
|
|
||
| type AdapterEditorCreatorFunction<TEditor = Editor> = | ||
| | ( ( config: EditorConfig ) => Promise<TEditor> ) | ||
| | ( ( | ||
| elementOrData: HTMLElement | string | Record<string, string> | Record<string, HTMLElement>, | ||
| config: EditorConfig | ||
| ) => Promise<TEditor> ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to add unit tests for this adapter? Something that would be focused on the adapter behavior, like
ests/EditorWatchdogAdapter.test.tsx.