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.

172 lines
5.6KB

  1. /* Prismatic - Prism.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 Prism.js', 'prismatic'),
  66. value : language,
  67. onChange : onChangelanguage,
  68. options : [
  69. { label : 'Language..', value : '' },
  70. { label : 'Apache', value : 'apacheconf' },
  71. { label : 'AppleScript', value : 'applescript' },
  72. { label : 'Arduino', value : 'arduino' },
  73. { label : 'Bash', value : 'bash' },
  74. { label : 'Batch', value : 'batch' },
  75. { label : 'C', value : 'c' },
  76. { label : 'C#', value : 'csharp' },
  77. { label : 'C++', value : 'cpp' },
  78. { label : 'C-like', value : 'clike' },
  79. { label : 'CoffeeScript', value : 'coffeescript' },
  80. { label : 'CSS', value : 'css' },
  81. { label : 'D', value : 'd' },
  82. { label : 'Dart', value : 'dart' },
  83. { label : 'Diff', value : 'diff' },
  84. { label : 'Elixir', value : 'elixir' },
  85. { label : 'G-code', value : 'gcode' },
  86. { label : 'Git', value : 'git' },
  87. { label : 'Go', value : 'go' },
  88. { label : 'GraphQL', value : 'graphql' },
  89. { label : 'Groovy', value : 'groovy' },
  90. { label : 'HCL', value : 'hcl' },
  91. { label : 'HTML', value : 'markup' },
  92. { label : 'HTTP', value : 'http' },
  93. { label : 'Ini', value : 'ini' },
  94. { label : 'Java', value : 'java' },
  95. { label : 'JavaScript', value : 'javascript' },
  96. { label : 'JSON', value : 'json' },
  97. { label : 'JSX', value : 'jsx' },
  98. { label : 'Kotlin', value : 'kotlin' },
  99. { label : 'LaTeX', value : 'latex' },
  100. { label : 'Liquid', value : 'liquid' },
  101. { label : 'Lua', value : 'lua' },
  102. { label : 'Makefile', value : 'makefile' },
  103. { label : 'Markdown', value : 'markdown' },
  104. { label : 'Markup', value : 'markup' },
  105. { label : 'NGINX', value : 'nginx' },
  106. { label : 'Objective-C', value : 'objectivec' },
  107. { label : 'Pascal', value : 'pascal' },
  108. { label : 'Perl', value : 'perl' },
  109. { label : 'PHP', value : 'php' },
  110. { label : 'PowerQuery', value : 'powerquery' },
  111. { label : 'PowerShell', value : 'powershell' },
  112. { label : 'Python', value : 'python' },
  113. { label : 'R', value : 'r' },
  114. { label : 'Ruby', value : 'ruby' },
  115. { label : 'Rust', value : 'rust' },
  116. { label : 'SASS', value : 'sass' },
  117. { label : 'Scala', value : 'scala' },
  118. { label : 'SCSS', value : 'scss' },
  119. { label : 'Shell Session', value : 'shell-session' },
  120. { label : 'Solidity', value : 'solidity' },
  121. { label : 'Splunk SPL', value : 'splunk-spl' },
  122. { label : 'SQL', value : 'sql' },
  123. { label : 'Swift', value : 'swift' },
  124. { label : 'TSX', value : 'tsx' },
  125. { label : 'Twig', value : 'twig' },
  126. { label : 'TypeScript', value : 'typescript' },
  127. { label : 'Visual Basic', value : 'visual-basic' },
  128. { label : 'YAML', value : 'yaml' },
  129. ]
  130. }
  131. )
  132. ),
  133. el(
  134. PlainText,
  135. {
  136. tagName : 'pre',
  137. key : 'editable',
  138. placeholder : __('Add code..', 'prismatic'),
  139. className : className,
  140. onChange : onChangeContent,
  141. style : { backgroundColor : backgroundColor, color : textColor },
  142. value : content,
  143. }
  144. )
  145. )
  146. );
  147. },
  148. save : function(props) {
  149. var
  150. content = props.attributes.content,
  151. language = props.attributes.language;
  152. return el('pre', null, el('code', { className: 'language-'+ language }, content));
  153. },
  154. });