8
0

mentioncommand.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module mention/mentioncommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
  10. /**
  11. * The mention command.
  12. *
  13. * @extends module:core/command~Command
  14. */
  15. export default class MentionCommand extends Command {
  16. /**
  17. * @inheritDoc
  18. */
  19. refresh() {
  20. // @todo implement refresh
  21. this.isEnabled = true;
  22. }
  23. /**
  24. * Executes the command.
  25. *
  26. * @protected
  27. * @param {Object} [options] Options for the executed command.
  28. * @param {String} [options.marker='@'] The mention marker.
  29. * @param {String} options.mention.
  30. * @param {String} [options.range].
  31. * @fires execute
  32. */
  33. execute( options = {} ) {
  34. const model = this.editor.model;
  35. const document = model.document;
  36. const selection = document.selection;
  37. const marker = options.marker || '@';
  38. const mention = options.mention;
  39. const range = options.range || selection.getFirstRange();
  40. const name = mention.name || mention;
  41. model.change( writer => {
  42. writer.remove( range );
  43. const selectionAttributes = toMap( selection.getAttributes() );
  44. const attributes = new Map( selectionAttributes.entries() );
  45. attributes.set( 'mention', mention );
  46. writer.insertText( `${ marker }${ name }`, attributes, range.start );
  47. writer.insertText( ' ', selectionAttributes, model.document.selection.focus );
  48. } );
  49. }
  50. }