-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9751 from ckeditor/i/4665
Feature (mention): Keyboard shortcuts to accept mentions can be customized using the [`config.mention.commitKeys`](https://ckeditor.com/docs/ckeditor5/latest/api/module_mention_mention-MentionConfig.html#member-commitKeys) configuration option. Closes #4665.
- Loading branch information
Showing
6 changed files
with
279 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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
8 changes: 8 additions & 0 deletions
8
packages/ckeditor5-mention/tests/manual/mention-custom-commitkeys.html
This file contains 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,8 @@ | ||
<div id="editor"> | ||
<p>Hello <span class="mention" data-mention="@Ted">@Ted</span>.</p> | ||
|
||
<figure class="image"> | ||
<img src="sample.jpg" /> | ||
<figcaption>CKEditor logo - caption</figcaption> | ||
</figure> | ||
</div> |
142 changes: 142 additions & 0 deletions
142
packages/ckeditor5-mention/tests/manual/mention-custom-commitkeys.js
This file contains 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,142 @@ | ||
/** | ||
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/* global console, window */ | ||
|
||
import global from '@ckeditor/ckeditor5-utils/src/dom/global'; | ||
|
||
import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'; | ||
import Mention from '../../src/mention'; | ||
import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline'; | ||
import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset'; | ||
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; | ||
|
||
import { toWidget, viewToModelPositionOutsideModelElement } from '@ckeditor/ckeditor5-widget/src/utils'; | ||
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview'; | ||
|
||
import { keyCodes } from 'ckeditor5/src/utils'; | ||
|
||
class InlineWidget extends Plugin { | ||
constructor( editor ) { | ||
super( editor ); | ||
|
||
editor.model.schema.register( 'placeholder', { | ||
allowWhere: '$text', | ||
isObject: true, | ||
isInline: true, | ||
allowAttributes: [ 'type' ] | ||
} ); | ||
|
||
editor.conversion.for( 'editingDowncast' ).elementToElement( { | ||
model: 'placeholder', | ||
view: ( modelItem, conversionApi ) => { | ||
const widgetElement = createPlaceholderView( modelItem, conversionApi ); | ||
|
||
return toWidget( widgetElement, conversionApi.writer ); | ||
} | ||
} ); | ||
|
||
editor.conversion.for( 'dataDowncast' ).elementToElement( { | ||
model: 'placeholder', | ||
view: createPlaceholderView | ||
} ); | ||
|
||
editor.conversion.for( 'upcast' ).elementToElement( { | ||
view: 'placeholder', | ||
model: ( viewElement, { writer } ) => { | ||
let type = 'general'; | ||
|
||
if ( viewElement.childCount ) { | ||
const text = viewElement.getChild( 0 ); | ||
|
||
if ( text.is( '$text' ) ) { | ||
type = text.data.slice( 1, -1 ); | ||
} | ||
} | ||
|
||
return writer.createElement( 'placeholder', { type } ); | ||
} | ||
} ); | ||
|
||
editor.editing.mapper.on( | ||
'viewToModelPosition', | ||
viewToModelPositionOutsideModelElement( editor.model, viewElement => viewElement.name == 'placeholder' ) | ||
); | ||
|
||
this._createToolbarButton(); | ||
|
||
function createPlaceholderView( modelItem, { writer } ) { | ||
const widgetElement = writer.createContainerElement( 'placeholder' ); | ||
const viewText = writer.createText( '{' + modelItem.getAttribute( 'type' ) + '}' ); | ||
|
||
writer.insert( writer.createPositionAt( widgetElement, 0 ), viewText ); | ||
|
||
return widgetElement; | ||
} | ||
} | ||
|
||
_createToolbarButton() { | ||
const editor = this.editor; | ||
const t = editor.t; | ||
|
||
editor.ui.componentFactory.add( 'placeholder', locale => { | ||
const buttonView = new ButtonView( locale ); | ||
|
||
buttonView.set( { | ||
label: t( 'Insert placeholder' ), | ||
tooltip: true, | ||
withText: true | ||
} ); | ||
|
||
this.listenTo( buttonView, 'execute', () => { | ||
const model = editor.model; | ||
|
||
model.change( writer => { | ||
const placeholder = writer.createElement( 'placeholder', { type: 'placeholder' } ); | ||
|
||
model.insertContent( placeholder ); | ||
|
||
writer.setSelection( placeholder, 'on' ); | ||
} ); | ||
} ); | ||
|
||
return buttonView; | ||
} ); | ||
} | ||
} | ||
|
||
ClassicEditor | ||
.create( global.document.querySelector( '#editor' ), { | ||
plugins: [ ArticlePluginSet, Underline, Mention, InlineWidget ], | ||
toolbar: [ | ||
'heading', | ||
'|', 'bulletedList', 'numberedList', 'blockQuote', | ||
'|', 'bold', 'italic', 'underline', 'link', | ||
'|', 'insertTable', 'placeholder', | ||
'|', 'undo', 'redo' | ||
], | ||
image: { | ||
toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ] | ||
}, | ||
table: { | ||
contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ], | ||
tableToolbar: [ 'bold', 'italic' ] | ||
}, | ||
mention: { | ||
commitKeys: [ keyCodes.a, keyCodes.space ], | ||
feeds: [ | ||
{ | ||
marker: '@', | ||
feed: [ '@Barney', '@Lily', '@Marshall', '@Robin', '@Ted' ] | ||
} | ||
] | ||
} | ||
} ) | ||
.then( editor => { | ||
window.editor = editor; | ||
} ) | ||
.catch( err => { | ||
console.error( err.stack ); | ||
} ); |
13 changes: 13 additions & 0 deletions
13
packages/ckeditor5-mention/tests/manual/mention-custom-commitkeys.md
This file contains 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,13 @@ | ||
## Mention | ||
|
||
The mention configuration with a custom `config.mention.commitKeys` configuration and a static list of autocomplete feed: | ||
|
||
### Configuration | ||
|
||
Type "@" to display the list of available mentions. | ||
|
||
### Interaction | ||
|
||
- Use **<kbd>arrowup</kbd>** to select previous item | ||
- Use **<kbd>arrowdown</kbd>** to select next item | ||
- Use **<kbd>space</kbd>** or **<kbd>a</kbd>** keys to insert a mention into the documentation. |
This file contains 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