codeediting.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. init() {
  24. const editor = this.editor;
  25. // Allow code attribute on text nodes.
  26. editor.model.schema.extend( '$text', { allowAttributes: CODE } );
  27. editor.model.schema.setAttributeProperties( CODE, { isFormatting: true } );
  28. editor.conversion.attributeToElement( {
  29. model: CODE,
  30. view: 'code',
  31. upcastAlso: {
  32. styles: {
  33. 'word-wrap': 'break-word'
  34. }
  35. }
  36. } );
  37. // Create code command.
  38. editor.commands.add( CODE, new AttributeCommand( editor, CODE ) );
  39. }
  40. }