-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Modified superscript and subscript text styles to be mutually exclusive.
#20116
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
martnpaneq
wants to merge
10
commits into
master
Choose a base branch
from
ck/20000
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7df06c0
Modified `superscript` and `subscript` text styles to be mutually exc…
martnpaneq 4fa113b
Updated docs.
martnpaneq cc7aede
Fixed coverage.
martnpaneq 705a5a2
Fixed validation errors.
martnpaneq 44ef9f6
Updated lock.
martnpaneq 3717685
Unified `SubscriptCommand` and `SuperscriptCommand` classes.
martnpaneq 518e4a0
Using `basicStyles` namespace for `superscript` and `subscript` options.
martnpaneq b921abc
Extracted `SUPERSCRIPT` and `SUBSCRIPT` variables to a common place.
martnpaneq 32a625e
Renamed changeset file.
martnpaneq 71425c2
Updated the changeset content.
martnpaneq 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| type: Fix | ||
| scope: | ||
| - ckeditor5-basic-styles | ||
| --- | ||
|
|
||
| The `superscript` and `subscript` text styles are now mutually exclusive by default - applying one to text that already has the other replaces it. The previous behavior, where both attributes could coexist on the same text, can be restored by setting `config.basicStyles.superscript.allowNesting` or `config.basicStyles.subscript.allowNesting` to `true`. |
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
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,49 @@ | ||
| /** | ||
| * @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 | ||
| */ | ||
|
|
||
| /** | ||
| * @module basic-styles/basicstylesconfig | ||
| */ | ||
|
|
||
| import type { BasicStyleSubscriptConfig } from './subscriptconfig.js'; | ||
| import type { BasicStyleSuperscriptConfig } from './superscriptconfig.js'; | ||
|
|
||
| /** | ||
| * The configuration of the basic styles features (`Bold`, `Italic`, `Subscript`, `Superscript`, etc.). | ||
| * | ||
| * ```ts | ||
| * ClassicEditor | ||
| * .create( editorElement, { | ||
| * basicStyles: { | ||
| * superscript: { | ||
| * allowNesting: true | ||
| * }, | ||
| * subscript: { | ||
| * allowNesting: true | ||
| * } | ||
| * } | ||
| * } ) | ||
| * .then( ... ) | ||
| * .catch( ... ); | ||
| * ``` | ||
| * | ||
| * See {@link module:core/editor/editorconfig~EditorConfig all editor options}. | ||
| */ | ||
| export interface BasicStylesConfig { | ||
|
|
||
| /** | ||
| * The configuration of the {@link module:basic-styles/superscript~Superscript superscript feature}. | ||
| * | ||
| * Read more in {@link module:basic-styles/superscriptconfig~BasicStyleSuperscriptConfig}. | ||
| */ | ||
| superscript?: BasicStyleSuperscriptConfig; | ||
|
|
||
| /** | ||
| * The configuration of the {@link module:basic-styles/subscript~Subscript subscript feature}. | ||
| * | ||
| * Read more in {@link module:basic-styles/subscriptconfig~BasicStyleSubscriptConfig}. | ||
| */ | ||
| subscript?: BasicStyleSubscriptConfig; | ||
| } |
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,22 @@ | ||
| /** | ||
| * @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 | ||
| */ | ||
|
|
||
| /** | ||
| * @module basic-styles/constants | ||
| */ | ||
|
|
||
| /** | ||
| * The model attribute key for the `superscript` text style. | ||
| * | ||
| * @internal | ||
| */ | ||
| export const SUPERSCRIPT = 'superscript'; | ||
|
|
||
| /** | ||
| * The model attribute key for the `subscript` text style. | ||
| * | ||
| * @internal | ||
| */ | ||
| export const SUBSCRIPT = 'subscript'; |
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
93 changes: 93 additions & 0 deletions
93
packages/ckeditor5-basic-styles/src/mutuallyexclusiveattributecommand.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,93 @@ | ||
| /** | ||
| * @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 | ||
| */ | ||
|
|
||
| /** | ||
| * @module basic-styles/mutuallyexclusiveattributecommand | ||
| */ | ||
|
|
||
| import type { Editor } from '@ckeditor/ckeditor5-core'; | ||
| import { ModelDocumentSelection, type ModelElement, type ModelRange } from '@ckeditor/ckeditor5-engine'; | ||
|
|
||
| import { AttributeCommand } from './attributecommand.js'; | ||
|
|
||
| /** | ||
| * An {@link module:basic-styles/attributecommand~AttributeCommand} variant that removes a configured | ||
| * opposite attribute from the affected ranges whenever the command turns its own attribute on. | ||
| * | ||
| * Used by the `superscript` and `subscript` commands to enforce their mutual exclusion. The opposite | ||
| * attribute is removed in the same model change as the parent's toggle, so the operation is a single | ||
| * undo step. | ||
| * | ||
| * The mutual exclusion can be disabled by setting either | ||
| * `config.basicStyles.superscript.allowNesting` or `config.basicStyles.subscript.allowNesting` to `true`. | ||
| * In that case, the command behaves exactly the same as the plain | ||
| * {@link module:basic-styles/attributecommand~AttributeCommand}. | ||
| * | ||
| * The exclusion only applies to command execution. Content set through the data pipeline | ||
| * (for example `editor.setData( '<sub><sup>x</sup></sub>' )`) is not modified by this command. | ||
| * | ||
| * @internal | ||
| */ | ||
| export class MutuallyExclusiveAttributeCommand extends AttributeCommand { | ||
| private readonly _oppositeAttributeKey: string; | ||
|
|
||
| constructor( editor: Editor, attributeKey: string, oppositeAttributeKey: string ) { | ||
| super( editor, attributeKey ); | ||
|
|
||
| this._oppositeAttributeKey = oppositeAttributeKey; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public override execute( options: { forceValue?: boolean } = {} ): void { | ||
| const editor = this.editor; | ||
| const model = editor.model; | ||
| const value = ( options.forceValue === undefined ) ? !this.value : options.forceValue; | ||
|
|
||
| if ( !value || _isNestingAllowed( editor ) ) { | ||
| super.execute( options ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const oppositeKey = this._oppositeAttributeKey; | ||
|
|
||
| model.change( writer => { | ||
| super.execute( options ); | ||
|
|
||
| const selection = model.document.selection; | ||
|
|
||
| if ( selection.isCollapsed ) { | ||
| writer.removeSelectionAttribute( oppositeKey ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const ranges = model.schema.getValidRanges( selection.getRanges(), oppositeKey, { | ||
| includeEmptyRanges: true | ||
| } ); | ||
|
|
||
| for ( const range of ranges ) { | ||
| let itemOrRange: ModelRange | ModelElement = range; | ||
| let attributeKey = oppositeKey; | ||
|
|
||
| if ( range.isCollapsed ) { | ||
| itemOrRange = range.start.parent as ModelElement; | ||
| attributeKey = ModelDocumentSelection._getStoreAttributeKey( oppositeKey ); | ||
| } | ||
|
|
||
| writer.removeAttribute( attributeKey, itemOrRange ); | ||
| } | ||
| } ); | ||
| } | ||
| } | ||
|
|
||
| function _isNestingAllowed( editor: Editor ): boolean { | ||
| return Boolean( | ||
| editor.config.get( 'basicStyles.superscript.allowNesting' ) || | ||
| editor.config.get( 'basicStyles.subscript.allowNesting' ) | ||
| ); | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * @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 | ||
| */ | ||
|
|
||
| /** | ||
| * @module basic-styles/subscriptconfig | ||
| */ | ||
|
|
||
| /** | ||
| * The configuration of the {@link module:basic-styles/subscript~Subscript subscript feature}. | ||
| * Nested under {@link module:basic-styles/basicstylesconfig~BasicStylesConfig#subscript `config.basicStyles.subscript`}. | ||
| * | ||
| * ```ts | ||
| * ClassicEditor | ||
| * .create( editorElement, { | ||
| * basicStyles: { | ||
| * subscript: { | ||
| * allowNesting: true | ||
| * } | ||
| * } | ||
| * } ) | ||
| * .then( ... ) | ||
| * .catch( ... ); | ||
| * ``` | ||
| * | ||
| * See {@link module:core/editor/editorconfig~EditorConfig all editor options}. | ||
| */ | ||
| export interface BasicStyleSubscriptConfig { | ||
|
|
||
| /** | ||
| * Whether `subscript` and `superscript` attributes are allowed to coexist on the same text. | ||
| * | ||
| * By default this is `false`: applying subscript to text that is already superscript removes the | ||
| * superscript attribute (and vice versa), matching the behavior of common word processors. | ||
| * | ||
| * Set to `true` to restore the historical behavior where both attributes can be applied to the same | ||
| * text. This is useful for content such as isotope notation (`¹⁴₆C`) or tensor indices (`T^i_j`). | ||
| * | ||
| * The flag is symmetric with | ||
| * {@link module:basic-styles/superscriptconfig~BasicStyleSuperscriptConfig#allowNesting `config.basicStyles.superscript.allowNesting`}: | ||
| * if either is set to `true`, both commands skip the mutual-exclusion step. | ||
| * | ||
| * The flag only affects command execution. Content set through the data pipeline (for example | ||
| * `editor.setData( '<sub><sup>x</sup></sub>' )`) keeps both attributes regardless of this option. | ||
| * | ||
| * @default false | ||
| */ | ||
| allowNesting?: boolean; | ||
| } |
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
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.