Skip to content

Commit

Permalink
Merge pull request #14926 from ckeditor/ck/14923
Browse files Browse the repository at this point in the history
Fix (mention): Fixes typing of Mention#toMentionAttribute method. Closes #14923.
  • Loading branch information
arkflpc authored Sep 6, 2023
2 parents 5880220 + 381867d commit 1f6bf52
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
27 changes: 22 additions & 5 deletions packages/ckeditor5-mention/src/mention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import '../theme/mention.css';
*/
export default class Mention extends Plugin {
/**
* Creates a mention attribute value from the provided view element and optional data.
* Creates a mention attribute value from the provided view element and additional data.
*
* ```ts
* editor.plugins.get( 'Mention' ).toMentionAttribute( viewElement, { userId: '1234' } );
Expand All @@ -32,10 +32,27 @@ export default class Mention extends Plugin {
* // { id: '@joe', userId: '1234', uid: '7a7bc7...', _text: '@John Doe' }
* ```
*
* @param viewElement
* @param data Additional data to be stored in the mention attribute.
*/
public toMentionAttribute( viewElement: Element, data?: MentionAttribute ): MentionAttribute | undefined {
public toMentionAttribute<MentionData extends Record<string, unknown>>(
viewElement: Element,
data: MentionData
): ( MentionAttribute & MentionData ) | undefined;

/**
* Creates a mention attribute value from the provided view element.
*
* ```ts
* editor.plugins.get( 'Mention' ).toMentionAttribute( viewElement );
*
* // For a view element: <span data-mention="@joe">@John Doe</span>
* // it will return:
* // { id: '@joe', uid: '7a7bc7...', _text: '@John Doe' }
* ```
*/
public toMentionAttribute( viewElement: Element ): MentionAttribute | undefined;

public toMentionAttribute( viewElement: Element, data?: Record<string, unknown> ): MentionAttribute | undefined {
return _toMentionAttribute( viewElement, data );
}

Expand Down Expand Up @@ -71,13 +88,13 @@ export type MentionAttribute = {
* A unique ID of this mention instance. Should be passed as an `option.id` when using
* {@link module:engine/view/downcastwriter~DowncastWriter#createAttributeElement writer.createAttributeElement()}.
*/
uid?: string;
uid: string;

/**
* Helper property that stores the text of the inserted mention. Used for detecting a broken mention
* in the editing area.
*
* @internal
*/
_text?: string;
_text: string;
};
8 changes: 6 additions & 2 deletions packages/ckeditor5-mention/src/mentioncommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type { Range } from 'ckeditor5/src/engine';
import { CKEditorError, toMap } from 'ckeditor5/src/utils';

import { _addMentionAttributes } from './mentionediting';
import type { MentionAttribute } from './mention';

/**
* The mention command.
Expand Down Expand Up @@ -84,7 +83,12 @@ export default class MentionCommand extends Command {
* Note that the replaced range might be shorter than the inserted text with the mention attribute.
* @fires execute
*/
public override execute( options: { mention: string | MentionAttribute; marker: string; text?: string; range?: Range } ): void {
public override execute( options: {
mention: string | { id: string; [ key: string ]: unknown };
marker: string;
text?: string;
range?: Range;
} ): void {
const model = this.editor.model;
const document = model.document;
const selection = document.selection;
Expand Down
6 changes: 3 additions & 3 deletions packages/ckeditor5-mention/src/mentionediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export default class MentionEditing extends Plugin {
* @internal
*/
export function _addMentionAttributes(
baseMentionData: MentionAttribute,
data?: MentionAttribute
baseMentionData: { id: string; _text: string },
data?: Record<string, unknown>
): MentionAttribute {
return Object.assign( { uid: uid() }, baseMentionData, data || {} );
}
Expand All @@ -100,7 +100,7 @@ export function _addMentionAttributes(
*/
export function _toMentionAttribute(
viewElementOrMention: Element,
data?: MentionAttribute
data?: Record<string, unknown>
): MentionAttribute | undefined {
const dataMention = viewElementOrMention.getAttribute( 'data-mention' ) as string;

Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-mention/src/ui/domwrapperview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @module mention/ui/domwrapperview
*/

import { View, type Template } from 'ckeditor5/src/ui';
import { View } from 'ckeditor5/src/ui';
import type { Locale } from 'ckeditor5/src/utils';

/**
Expand Down

0 comments on commit 1f6bf52

Please sign in to comment.