codesnippet.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  4. For licensing, see LICENSE.md or http://ckeditor.com/license
  5. -->
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Code Snippet &mdash; CKEditor Sample</title>
  10. <script src="../../../ckeditor.js"></script>
  11. <link href="../../../samples/sample.css" rel="stylesheet">
  12. <link href="../../../plugins/codesnippet/lib/highlight/styles/monokai_sublime.css" rel="stylesheet">
  13. <meta name="ckeditor-sample-name" content="Code Snippet plugin">
  14. <meta name="ckeditor-sample-group" content="Plugins">
  15. <meta name="ckeditor-sample-description" content="View and modify code using the Code Snippet plugin.">
  16. <meta name="ckeditor-sample-isnew" content="1">
  17. <style>
  18. #editable
  19. {
  20. padding: 10px 20px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h1 class="samples">
  26. <a href="../../../samples/index.html">CKEditor Samples</a> &raquo; Code Snippet Plugin
  27. </h1>
  28. <div class="description">
  29. <p>
  30. This editor is using the <strong><a href="http://ckeditor.com/addon/codesnippet">Code Snippet</a></strong> plugin which introduces beautiful code snippets.
  31. By default the <code>codesnippet</code> plugin depends on the built-in client-side syntax highlighting
  32. library <a href="http://highlightjs.org">highlight.js</a>.
  33. </p>
  34. <p>
  35. You can adjust the appearance of code snippets using the <code><a href="http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-codeSnippet_theme">codeSnippet_theme</a></code> configuration variable
  36. (see <a href="http://highlightjs.org/static/test.html">available themes</a>).
  37. </p>
  38. <p>
  39. Select theme: <select id="select"></select>
  40. </p>
  41. <p>
  42. The CKEditor instance below was created by using the following configuration settings:
  43. </p>
  44. <pre class="samples" id="sampleConfig">
  45. CKEDITOR.replace( 'editor1', {
  46. <strong>extraPlugins: 'codesnippet',</strong>
  47. codeSnippet_theme: 'monokai_sublime'
  48. } );
  49. </pre>
  50. <p id="ie8-warning">
  51. Please note that this plugin is not compatible with Internet Explorer 8.
  52. </p>
  53. </div>
  54. <textarea id="editor1">
  55. &lt;p&gt;JavaScript code:&lt;/p&gt;
  56. &lt;pre&gt;
  57. &lt;code class="language-javascript"&gt;function isEmpty( object ) {
  58. for ( var i in object ) {
  59. if ( object.hasOwnProperty( i ) )
  60. return false;
  61. }
  62. return true;
  63. }&lt;/code&gt;&lt;/pre&gt;
  64. &lt;p&gt;SQL query:&lt;/p&gt;
  65. &lt;pre&gt;
  66. &lt;code class="language-sql"&gt;SELECT cust.id FROM cust LEFT JOIN loc ON ( cust.loc_id = loc.id ) WHERE cust.type IN ( 1, 2 );&lt;/code&gt;&lt;/pre&gt;
  67. &lt;p&gt;Unknown markup:&lt;/p&gt;
  68. &lt;pre&gt;
  69. &lt;code&gt; ________________
  70. / \
  71. | How about moo? | ^__^
  72. \________________/ (oo)\_______
  73. \ (__)\ )\/\
  74. ||----w |
  75. || ||
  76. &lt;/code&gt;&lt;/pre&gt;
  77. </textarea>
  78. <h2>Inline editor</h2>
  79. <div class="description">
  80. <p>
  81. The following sample shows the <strong>Code Snippet</strong> plugin running inside
  82. an inline CKEditor instance. The CKEditor instance below was created by using the following configuration settings:
  83. </p>
  84. <pre class="samples">
  85. CKEDITOR.inline( 'editable', {
  86. <strong>extraPlugins: 'codesnippet'</strong>
  87. } );
  88. </pre>
  89. <p>
  90. <strong>Note</strong>: The <a href="http://highlightjs.org/static/test.html">highlight.js themes</a>
  91. must be loaded manually to be applied inside an inline editor instance, as the
  92. <code>codeSnippet_theme</code> setting will not work in that case.
  93. You need to include the stylesheet in the <code>&lt;head&gt;</code> section of the page, for example:
  94. </p>
  95. <pre class="samples">
  96. &lt;head&gt;
  97. ...
  98. &lt;link href="path/to/highlight.js/styles/monokai_sublime.css" rel="stylesheet"&gt;
  99. &lt;/head&gt;
  100. </pre>
  101. </div>
  102. <div id="editable" contenteditable="true">
  103. <p>JavaScript code:</p>
  104. <pre><code class="language-javascript">function isEmpty( object ) {
  105. for ( var i in object ) {
  106. if ( object.hasOwnProperty( i ) )
  107. return false;
  108. }
  109. return true;
  110. }</code></pre>
  111. <p>SQL query:</p>
  112. <pre><code class="language-sql">SELECT cust.id, cust.name, loc.city FROM cust LEFT JOIN loc ON ( cust.loc_id = loc.id ) WHERE cust.type IN ( 1, 2 );</code></pre>
  113. <p>Unknown markup:</p>
  114. <pre><code> ________________
  115. / \
  116. | How about moo? | ^__^
  117. \________________/ (oo)\_______
  118. \ (__)\ )\/\
  119. ||----w |
  120. || ||
  121. </code></pre>
  122. </div>
  123. <h2>Server-side Highlighting and Custom Highlighting Engines</h2>
  124. <p>
  125. The <a href="http://ckeditor.com/addon/codesnippetgeshi"><strong>Code Snippet GeSHi</strong></a> plugin is an
  126. extension of the <strong>Code Snippet</strong> plugin which uses a server-side highligter.
  127. </p>
  128. <p>
  129. It also is possible to replace the default highlighter with any library using
  130. the <a href="http://docs.ckeditor.com/#!/api/CKEDITOR.plugins.codesnippet.highlighter">Highlighter API</a>
  131. and the <a href="http://docs.ckeditor.com/#!/api/CKEDITOR.plugins.codesnippet-method-setHighlighter"><code>editor.plugins.codesnippet.setHighlighter()</code></a> method.
  132. </p>
  133. <script>
  134. ( function() {
  135. CKEDITOR.disableAutoInline = true;
  136. var config = {
  137. extraPlugins: 'codesnippet',
  138. toolbar: [
  139. [ 'Source' ], [ 'Undo', 'Redo' ], [ 'Bold', 'Italic', 'Underline' ], [ 'CodeSnippet' ]
  140. ],
  141. codeSnippet_theme: 'monokai_sublime',
  142. height: 400
  143. };
  144. CKEDITOR.replace( 'editor1', config );
  145. CKEDITOR.inline( 'editable', CKEDITOR.tools.extend( {}, config, {
  146. extraPlugins: 'codesnippet'
  147. }, true ) );
  148. initThemeChange();
  149. function initThemeChange() {
  150. var templates = [
  151. 'monokai_sublime', 'default', 'arta', 'ascetic', 'atelier-dune.dark', 'atelier-dune.light', 'atelier-forest.dark', 'atelier-forest.light', 'atelier-heath.dark', 'atelier-heath.light', 'atelier-lakeside.dark', 'atelier-lakeside.light', 'atelier-seaside.dark', 'atelier-seaside.light', 'brown_paper', 'dark', 'docco', 'far', 'foundation', 'github', 'googlecode', 'idea', 'ir_black', 'magula', 'mono-blue', 'monokai', 'obsidian', 'paraiso.dark', 'paraiso.light', 'pojoaque', 'railscasts', 'rainbow', 'school_book', 'solarized_dark', 'solarized_light', 'sunburst', 'tomorrow-night-blue', 'tomorrow-night-bright', 'tomorrow-night-eighties', 'tomorrow-night', 'tomorrow', 'vs', 'xcode', 'zenburn'
  152. ],
  153. pre = CKEDITOR.document.getById( 'sampleConfig' ),
  154. select = CKEDITOR.document.getById( 'select' ),
  155. codeTemplate = 'CKEDITOR.replace( \'editor1\', {\n' +
  156. ' <strong>extraPlugins: \'codesnippet\',</strong>\n' +
  157. ' codeSnippet_theme: \'{tpl}\'\n' +
  158. '} );',
  159. name, option;
  160. while ( ( name = templates.shift() ) ) {
  161. option = CKEDITOR.document.createElement( 'option', {
  162. attributes: {
  163. value: name
  164. }
  165. } );
  166. option.setText( name );
  167. select.append( option );
  168. }
  169. select.$.onchange = function() {
  170. if ( CKEDITOR.instances.editor1 )
  171. CKEDITOR.instances.editor1.destroy();
  172. CKEDITOR.replace( 'editor1', CKEDITOR.tools.extend( {}, config, {
  173. codeSnippet_theme: this.value
  174. }, true ) );
  175. pre.setHtml( codeTemplate.replace( '{tpl}', this.value ) );
  176. };
  177. }
  178. if ( CKEDITOR.env.ie && CKEDITOR.env.version == 8 )
  179. CKEDITOR.document.getById( 'ie8-warning' ).addClass( 'warning' );
  180. }() );
  181. </script>
  182. <div id="footer">
  183. <hr>
  184. <p>
  185. CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>
  186. </p>
  187. <p id="copy">
  188. Copyright &copy; 2003-2015, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
  189. Knabben. All rights reserved.
  190. </p>
  191. </div>
  192. </body>
  193. </html>