You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.0KB

  1. /* Prismatic - Highlight.js Block */
  2. var
  3. el = wp.element.createElement,
  4. Fragment = wp.element.Fragment,
  5. registerBlockType = wp.blocks.registerBlockType,
  6. RichText = wp.editor.RichText,
  7. PlainText = wp.editor.PlainText,
  8. RawHTML = wp.editor.RawHTML,
  9. InspectorControls = wp.editor.InspectorControls,
  10. SelectControl = wp.components.SelectControl,
  11. __ = wp.i18n.__;
  12. registerBlockType('prismatic/blocks', {
  13. title : 'Prismatic',
  14. icon : 'editor-code',
  15. category : 'formatting',
  16. keywords : [
  17. __('code', 'prismatic'),
  18. __('pre', 'prismatic'),
  19. __('prism', 'prismatic'),
  20. __('highlight', 'prismatic'),
  21. __('prismatic', 'prismatic')
  22. ],
  23. attributes : {
  24. content : {
  25. type : 'string',
  26. source : 'text',
  27. selector : 'pre code',
  28. },
  29. language : {
  30. type : 'string',
  31. default : '',
  32. },
  33. backgroundColor : {
  34. type : 'string',
  35. default : '#f7f7f7',
  36. },
  37. textColor : {
  38. type : 'string',
  39. default : '#373737',
  40. },
  41. },
  42. edit : function(props) {
  43. var
  44. content = props.attributes.content,
  45. language = props.attributes.language,
  46. backgroundColor = props.attributes.backgroundColor,
  47. textColor = props.attributes.textColor,
  48. className = props.className;
  49. function onChangeContent(newValue) {
  50. props.setAttributes({ content: newValue });
  51. }
  52. function onChangelanguage(newValue) {
  53. props.setAttributes({ language: newValue });
  54. }
  55. return (
  56. el(
  57. Fragment,
  58. null,
  59. el(
  60. InspectorControls,
  61. null,
  62. el(
  63. SelectControl,
  64. {
  65. label : __('Select Language for Highlight.js', 'prismatic'),
  66. value : language,
  67. onChange : onChangelanguage,
  68. options : [
  69. { label : 'Language..', value : '' },
  70. { label : 'Apache', value : 'apache' },
  71. { label : 'AppleScript', value : 'applescript' },
  72. { label : 'Arduino', value : 'arduino' },
  73. { label : 'Bash', value : 'bash' },
  74. { label : 'C', value : 'c' },
  75. { label : 'C#', value : 'cs' },
  76. { label : 'C++', value : 'cpp' },
  77. { label : 'CSS', value : 'css' },
  78. { label : 'CoffeeScript', value : 'coffeescript' },
  79. { label : 'D', value : 'd' },
  80. { label : 'Dart', value : 'dart' },
  81. { label : 'Diff', value : 'diff' },
  82. { label : 'Elixir', value : 'elixir' },
  83. { label : 'G-code', value : 'gcode' },
  84. { label : 'GML', value : 'gml' },
  85. { label : 'Go', value : 'go' },
  86. { label : 'Groovy', value : 'groovy' },
  87. { label : 'HTML/XML', value : 'xml' },
  88. { label : 'HTTP', value : 'http' },
  89. { label : 'Ini/TOML', value : 'ini' },
  90. { label : 'JSON', value : 'json' },
  91. { label : 'Java', value : 'java' },
  92. { label : 'JavaScript', value : 'javascript' },
  93. { label : 'Kotlin', value : 'kotlin' },
  94. { label : 'Less', value : 'less' },
  95. { label : 'Lua', value : 'lua' },
  96. { label : 'Makefile', value : 'makefile' },
  97. { label : 'Markdown', value : 'markdown' },
  98. { label : 'Nginx', value : 'nginx' },
  99. { label : 'Objective-C', value : 'objectivec' },
  100. { label : 'PHP', value : 'php' },
  101. { label : 'Perl', value : 'perl' },
  102. { label : 'Plaintext', value : 'plaintext' },
  103. { label : 'PowerShell', value : 'powershell' },
  104. { label : 'Properties', value : 'properties' },
  105. { label : 'Python', value : 'python' },
  106. { label : 'Python REPL', value : 'python-repl' },
  107. { label : 'R', value : 'r' },
  108. { label : 'Ruby', value : 'ruby' },
  109. { label : 'Rust', value : 'rust' },
  110. { label : 'Scala', value : 'scala' },
  111. { label : 'SCSS', value : 'scss' },
  112. { label : 'Shell Session', value : 'shell' },
  113. { label : 'SQL', value : 'sql' },
  114. { label : 'Swift', value : 'swift' },
  115. { label : 'TypeScript', value : 'typescript' },
  116. { label : 'VB.Net', value : 'vbnet' },
  117. { label : 'YAML', value : 'yaml' },
  118. ]
  119. }
  120. )
  121. ),
  122. el(
  123. PlainText,
  124. {
  125. tagName : 'pre',
  126. key : 'editable',
  127. placeholder : __('Add code..', 'prismatic'),
  128. className : className,
  129. onChange : onChangeContent,
  130. style : { backgroundColor : backgroundColor, color : textColor },
  131. value : content,
  132. }
  133. )
  134. )
  135. );
  136. },
  137. save : function(props) {
  138. var
  139. content = props.attributes.content,
  140. language = props.attributes.language;
  141. return el('pre', null, el('code', { className: 'language-'+ language }, content));
  142. },
  143. });