codeblock.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 code-block/codeblock
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import CodeBlockEditing from './codeblockediting';
  10. import CodeBlockUI from './codeblockui';
  11. /**
  12. * The code block plugin.
  13. *
  14. * For more information about this feature check the package page.
  15. *
  16. * This is a "glue" plugin which loads the {@link module:code-block/codeblockediting~CodeBlockEditing code block editing feature}
  17. * and {@link module:code-block/codeblockui~CodeBlockUI code block UI feature}.
  18. *
  19. * @extends module:core/plugin~Plugin
  20. */
  21. export default class CodeBlock extends Plugin {
  22. /**
  23. * @inheritDoc
  24. */
  25. static get requires() {
  26. return [ CodeBlockEditing, CodeBlockUI ];
  27. }
  28. /**
  29. * @inheritDoc
  30. */
  31. static get pluginName() {
  32. return 'CodeBlock';
  33. }
  34. }
  35. /**
  36. * The configuration of the {@link module:code-block/codeblock~CodeBlock} feature.
  37. *
  38. * Read more in {@link module:code-block/codeblock~CodeBlockConfig}.
  39. *
  40. * @member {module:code-block/codeblock~CodeBlockConfig} module:core/editor/editorconfig~EditorConfig#codeBlock
  41. */
  42. /**
  43. * The configuration of the {@link module:code-block/codeblock~CodeBlock code block feature}.
  44. *
  45. * ClassicEditor
  46. * .create( editorElement, {
  47. * codeBlock: ... // Code block feature configuration.
  48. * } )
  49. * .then( ... )
  50. * .catch( ... );
  51. *
  52. * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
  53. *
  54. * @interface CodeBlockConfig
  55. */
  56. /**
  57. * The code block language descriptor. See {@link module:code-block/codeblock~CodeBlockConfig#languages} to learn more.
  58. *
  59. * {
  60. * language: 'javascript',
  61. * label: 'JavaScript'
  62. * }
  63. *
  64. * @typedef {Object} module:code-block/codeblock~CodeBlockLanguageDefinition
  65. * @property {String} language The name of the language that will be stored in the model attribute. Also, when `class`
  66. * is not specified, it will also be used to create the CSS class associated with the language (prefixed by "language-").
  67. * @property {String} label The human–readable label associated with the language and displayed in the UI.
  68. * @property {String} [class] The CSS class associated with the language. When not specified the `language`
  69. * property is used to create a class prefixed by "language-".
  70. */
  71. /**
  72. * The list of code languages available in the user interface to choose for a particular code block.
  73. *
  74. * The language of the code block is represented as a CSS class (by default prefixed by "language-") set on the
  75. * `<code>` element, both when editing and in the editor data. The CSS class associated with the language
  76. * can be used by third–party code syntax highlighters to detect and apply the correct highlighting.
  77. *
  78. * For instance, this language configuration:
  79. *
  80. * ClassicEditor
  81. * .create( editorElement, {
  82. * codeBlock: {
  83. * languages: [
  84. * // ...
  85. * { language: 'javascript', label: 'JavaScript' },
  86. * // ...
  87. * ]
  88. * }
  89. * } )
  90. * .then( ... )
  91. * .catch( ... );
  92. *
  93. * will result in the following structure of JavaScript code blocks in the editor editing and data:
  94. *
  95. * <pre><code class="language-javascript">window.alert( 'Hello world!' )</code></pre>
  96. *
  97. * You can customize the CSS class by specifying an optional `class` property in a language definition:
  98. *
  99. * ClassicEditor
  100. * .create( editorElement, {
  101. * codeBlock: {
  102. * languages: [
  103. * // Do not render CSS class for the plain text code blocks.
  104. * { language: 'plaintext', label: 'Plain text', class: '' },
  105. *
  106. * // Use the "php-code" class for PHP code blocks.
  107. * { language: 'php', label: 'PHP', class: 'php-code' },
  108. *
  109. * // Use the "js" class for JavaScript code blocks.
  110. * { language: 'javascript', label: 'JavaScript', class: 'js' },
  111. *
  112. * // Python code blocks will have the default "language-python" CSS class.
  113. * { language: 'python', label: 'Python' }
  114. * ]
  115. * }
  116. * } )
  117. * .then( ... )
  118. * .catch( ... );
  119. *
  120. * The default value of the language configuration is as follows:
  121. *
  122. * languages: [
  123. * { language: 'plaintext', label: 'Plain text' }, // The default language.
  124. * { language: 'c', label: 'C' },
  125. * { language: 'cs', label: 'C#' },
  126. * { language: 'cpp', label: 'C++' },
  127. * { language: 'css', label: 'CSS' },
  128. * { language: 'diff', label: 'Diff' },
  129. * { language: 'xml', label: 'HTML/XML' },
  130. * { language: 'java', label: 'Java' },
  131. * { language: 'javascript', label: 'JavaScript' },
  132. * { language: 'php', label: 'PHP' },
  133. * { language: 'python', label: 'Python' },
  134. * { language: 'ruby', label: 'Ruby' },
  135. * { language: 'typescript', label: 'TypeScript' },
  136. * ]
  137. *
  138. * **Note**: The first language defined in the configuration is considered the default one. This means it will be
  139. * applied to code blocks loaded from data that have no CSS `class` specified (or no matching `class` in the config).
  140. * It will also be used when creating new code blocks using the main UI button. By default it is "Plain text".
  141. *
  142. * @member {Array.<module:code-block/codeblock~CodeBlockLanguageDefinition>} module:code-block/codeblock~CodeBlockConfig#languages
  143. */
  144. /**
  145. * A sequence of characters inserted or removed from the code block lines when its indentation
  146. * is changed by the user, for instance, using <kbd>Tab</kbd> and <kbd>Shift</kbd>+<kbd>Tab</kbd> keys.
  147. *
  148. * The default value is a single tab character (" ", `\u0009` in Unicode).
  149. *
  150. * This configuration is used by `indentCodeBlock` and `outdentCodeBlock` commands (instances of
  151. * {@link module:code-block/indentcodeblockcommand~IndentCodeBlockCommand}).
  152. *
  153. * **Note**: Setting this configuration to `false` will disable the code block indentation commands
  154. * and associated keystrokes.
  155. *
  156. * @member {String} module:code-block/codeblock~CodeBlockConfig#indentSequence
  157. */