| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- /**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /**
- * @module mention/mentionediting
- */
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- import MentionCommand from './mentioncommand';
- /**
- * The mention editing feature.
- *
- * @extends module:core/plugin~Plugin
- */
- export default class MentionEditing extends Plugin {
- /**
- * @inheritDoc
- */
- static get pluginName() {
- return 'MentionEditing';
- }
- /**
- * @inheritDoc
- */
- init() {
- const editor = this.editor;
- // Allow mention attribute on text nodes.
- editor.model.schema.extend( '$text', { allowAttributes: 'mention' } );
- editor.conversion.for( 'upcast' ).elementToAttribute( {
- view: {
- name: 'span',
- key: 'data-mention',
- classes: 'mention'
- },
- model: {
- key: 'mention',
- value: viewItem => {
- return viewItem.getAttribute( 'data-mention' );
- }
- }
- } );
- editor.conversion.for( 'downcast' ).attributeToElement( {
- model: 'mention',
- view: ( modelAttributeValue, viewWriter ) => {
- return viewWriter.createAttributeElement( 'span', {
- class: 'mention',
- 'data-mention': modelAttributeValue
- } );
- }
- } );
- // Remove mention attribute if text was edited.
- editor.model.document.registerPostFixer( writer => {
- const changes = editor.model.document.differ.getChanges();
- let wasChanged = false;
- for ( const change of changes ) {
- if ( change.type == 'insert' || change.type == 'remove' ) {
- const textNode = change.position.textNode;
- if ( change.name == '$text' && textNode && textNode.hasAttribute( 'mention' ) ) {
- writer.removeAttribute( 'mention', textNode );
- wasChanged = true;
- }
- }
- if ( change.type == 'remove' ) {
- const nodeBefore = change.position.nodeBefore;
- if ( nodeBefore && nodeBefore.hasAttribute( 'mention' ) ) {
- const text = nodeBefore.data;
- if ( text != nodeBefore.getAttribute( 'mention' ) ) {
- writer.removeAttribute( 'mention', nodeBefore );
- wasChanged = true;
- }
- }
- }
- }
- return wasChanged;
- } );
- editor.model.document.registerPostFixer( writer => {
- const selection = editor.model.document.selection;
- const focus = selection.focus;
- if ( selection.hasAttribute( 'mention' ) && selection.isCollapsed && focus.nodeBefore && focus.nodeBefore.is( 'text' ) ) {
- writer.removeSelectionAttribute( 'mention' );
- return true;
- }
- } );
- editor.commands.add( 'mention', new MentionCommand( editor ) );
- }
- }
|