-
Notifications
You must be signed in to change notification settings - Fork 112
[Angular] Pass DOM Element in the config object instead of editor constructor in CKEditor >= 48 #553
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
Open
Mati365
wants to merge
9
commits into
master
Choose a base branch
from
ck/550
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Angular] Pass DOM Element in the config object instead of editor constructor in CKEditor >= 48 #553
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dcaff04
Improve v48 compatibility.
Mati365 1376c27
Improve asseration.
Mati365 7af1dbe
Simplify code.
Mati365 625ea30
Fix broken initialization.
Mati365 26bb9ca
Simplify code.
Mati365 696c8a6
Fix tests.
Mati365 bfd7edf
Fix tests.
Mati365 3463835
bump ckeditor integrations common
Mati365 263a974
fix imports
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
186 changes: 186 additions & 0 deletions
186
src/ckeditor/compatibility/assignElementToEditorConfig.spec.ts
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,186 @@ | ||
| /** | ||
| * @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 { beforeEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| import { assignElementToEditorConfig } from './assignElementToEditorConfig.js'; | ||
| import type { EditorConstructor } from '../types.js'; | ||
|
|
||
| function createEditorConstructor( editorName?: string ): EditorConstructor { | ||
| return { editorName } as unknown as EditorConstructor; | ||
| } | ||
|
|
||
| describe( 'assignElementToEditorConfig', () => { | ||
| let element: HTMLElement; | ||
|
|
||
| beforeEach( () => { | ||
| element = document.createElement( 'div' ); | ||
| } ); | ||
|
|
||
| describe( 'ClassicEditor (attachTo)', () => { | ||
| it( 'should assign element to attachTo when editorName is "ClassicEditor"', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'ClassicEditor' ), | ||
| element, | ||
| {} | ||
| ); | ||
|
|
||
| expect( config.attachTo ).toBe( element ); | ||
| } ); | ||
|
|
||
| it( 'should assign element to attachTo when editorName is undefined', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( undefined ), | ||
| element, | ||
| {} | ||
| ); | ||
|
|
||
| expect( config.attachTo ).toBe( element ); | ||
| } ); | ||
|
|
||
| it( 'should preserve other config properties', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'ClassicEditor' ), | ||
| element, | ||
| { language: 'pl' } | ||
| ) as any; | ||
|
|
||
| expect( config.language ).toBe( 'pl' ); | ||
| expect( config.attachTo ).toBe( element ); | ||
| } ); | ||
|
|
||
| it( 'should override existing attachTo with the new element', () => { | ||
| const oldElement = document.createElement( 'div' ); | ||
|
|
||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'ClassicEditor' ), | ||
| element, | ||
| { attachTo: oldElement } | ||
| ); | ||
|
|
||
| expect( config.attachTo ).toBe( element ); | ||
| } ); | ||
|
|
||
| it( 'should not set roots.main.element', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'ClassicEditor' ), | ||
| element, | ||
| {} | ||
| ) as any; | ||
|
|
||
| expect( config.roots?.main?.element ).toBeUndefined(); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'non-ClassicEditor (roots.main.element)', () => { | ||
| it( 'should assign element to roots.main.element for DecoupledEditor', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'DecoupledEditor' ), | ||
| element, | ||
| {} | ||
| ) as any; | ||
|
|
||
| expect( config.roots.main.element ).toBe( element ); | ||
| } ); | ||
|
|
||
| it( 'should assign element to roots.main.element for InlineEditor', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'InlineEditor' ), | ||
| element, | ||
| {} | ||
| ) as any; | ||
|
|
||
| expect( config.roots.main.element ).toBe( element ); | ||
| } ); | ||
|
|
||
| it( 'should not set attachTo', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'DecoupledEditor' ), | ||
| element, | ||
| {} | ||
| ); | ||
|
|
||
| expect( config.attachTo ).toBeUndefined(); | ||
| } ); | ||
|
|
||
| it( 'should delete config.root after mapping', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'DecoupledEditor' ), | ||
| element, | ||
| { root: { initialData: 'hello' } } | ||
| ) as any; | ||
|
|
||
| expect( config.root ).toBeUndefined(); | ||
| } ); | ||
|
|
||
| it( 'should merge config.root into roots.main', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'DecoupledEditor' ), | ||
| element, | ||
| { root: { initialData: 'hello' } } | ||
| ) as any; | ||
|
|
||
| expect( config.roots.main.initialData ).toBe( 'hello' ); | ||
| expect( config.roots.main.element ).toBe( element ); | ||
| } ); | ||
|
|
||
| it( 'should merge existing config.roots.main into result', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'DecoupledEditor' ), | ||
| element, | ||
| { roots: { main: { initialData: 'from-roots' } } } | ||
| ) as any; | ||
|
|
||
| expect( config.roots.main.initialData ).toBe( 'from-roots' ); | ||
| expect( config.roots.main.element ).toBe( element ); | ||
| } ); | ||
|
|
||
| it( 'should let config.roots.main take precedence over config.root', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'DecoupledEditor' ), | ||
| element, | ||
| { | ||
| root: { initialData: 'from-root' }, | ||
| roots: { main: { initialData: 'from-roots-main' } } | ||
| } | ||
| ) as any; | ||
|
|
||
| expect( config.roots.main.initialData ).toBe( 'from-roots-main' ); | ||
| } ); | ||
|
|
||
| it( 'should preserve other roots alongside main', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'DecoupledEditor' ), | ||
| element, | ||
| { roots: { outro: { initialData: 'outro' } } } | ||
| ) as any; | ||
|
|
||
| expect( config.roots ).toHaveProperty( 'outro' ); | ||
| expect( config.roots ).toHaveProperty( 'main' ); | ||
| } ); | ||
|
|
||
| it( 'should preserve other top-level config properties', () => { | ||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'DecoupledEditor' ), | ||
| element, | ||
| { language: 'pl' } | ||
| ) as any; | ||
|
|
||
| expect( config.language ).toBe( 'pl' ); | ||
| } ); | ||
|
|
||
| it( 'should override existing roots.main.element with the new element', () => { | ||
| const oldElement = document.createElement( 'span' ); | ||
|
|
||
| const config = assignElementToEditorConfig( | ||
| createEditorConstructor( 'DecoupledEditor' ), | ||
| element, | ||
| { roots: { main: { element: oldElement } } } | ||
| ) as any; | ||
|
|
||
| expect( config.roots.main.element ).toBe( element ); | ||
| } ); | ||
| } ); | ||
| } ); |
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,43 @@ | ||
| /** | ||
| * @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 { EditorConstructor, EditorRelaxedConfig } from '../types.js'; | ||
|
|
||
| /** | ||
| * Assigns a DOM element to the editor configuration in a way that is compatible with the specific editor type. | ||
| * | ||
| * @param Editor Constructor of the editor used to determine the location of element config entry. | ||
| * @param element Element to be assigned to config. | ||
| * @param config Config of the editor. | ||
| * @returns The updated configuration object. | ||
| */ | ||
| export function assignElementToEditorConfig( | ||
| Editor: EditorConstructor, | ||
| element: HTMLElement, | ||
| config: EditorRelaxedConfig | ||
| ): EditorRelaxedConfig { | ||
| if ( !Editor.editorName || Editor.editorName === 'ClassicEditor' ) { | ||
| return { | ||
| ...config, | ||
| attachTo: element | ||
| }; | ||
| } | ||
|
|
||
| const mappedConfig: EditorRelaxedConfig = { | ||
| ...config, | ||
| roots: { | ||
| ...config.roots, | ||
| main: { | ||
| ...config.root, | ||
| ...config.roots?.main, | ||
| element | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| delete mappedConfig.root; | ||
|
|
||
| return mappedConfig; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.