codeediting.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module basic-styles/code/codeediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import AttributeCommand from '../attributecommand';
  10. const CODE = 'code';
  11. /**
  12. * The code editing feature.
  13. *
  14. * It registers the `'code'` command and introduces the `code` attribute in the model which renders to the view
  15. * as a `<code>` element.
  16. *
  17. * @extends module:core/plugin~Plugin
  18. */
  19. export default class CodeEditing extends Plugin {
  20. /**
  21. * @inheritDoc
  22. */
  23. static get pluginName() {
  24. return 'CodeEditing';
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. init() {
  30. const editor = this.editor;
  31. // Allow code attribute on text nodes.
  32. editor.model.schema.extend( '$text', { allowAttributes: CODE } );
  33. editor.model.schema.setAttributeProperties( CODE, {
  34. isFormatting: true,
  35. copyOnEnter: true
  36. } );
  37. editor.conversion.attributeToElement( {
  38. model: CODE,
  39. view: 'code',
  40. upcastAlso: {
  41. styles: {
  42. 'word-wrap': 'break-word'
  43. }
  44. }
  45. } );
  46. // Create code command.
  47. editor.commands.add( CODE, new AttributeCommand( editor, CODE ) );
  48. }
  49. }