blocks-highlight.js 5.07 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/* Prismatic - Highlight.js Block */

var 
	el                = wp.element.createElement,
	Fragment          = wp.element.Fragment,
	registerBlockType = wp.blocks.registerBlockType,
	RichText          = wp.editor.RichText,
	PlainText         = wp.editor.PlainText,
	RawHTML           = wp.editor.RawHTML,
	InspectorControls = wp.editor.InspectorControls,
	SelectControl     = wp.components.SelectControl,
	__                = wp.i18n.__;

registerBlockType('prismatic/blocks', {

	title    : 'Prismatic',
	icon     : 'editor-code',
	category : 'formatting',
	keywords : [ 
		__('code',      'prismatic'), 
		__('pre',       'prismatic'), 
		__('prism',     'prismatic'), 
		__('highlight', 'prismatic'), 
		__('prismatic', 'prismatic') 
	],
	attributes : {
		content : {
			type     : 'string',
			source   : 'text',
			selector : 'pre code',
		},
		language : {
			type    : 'string',
			default : '',
		},
		backgroundColor : {
			type    : 'string',
			default : '#f7f7f7',
		},
		textColor : {
			type    : 'string',
			default : '#373737',
		},
	},

	edit : function(props) {
		
		var 
			content         = props.attributes.content,
			language        = props.attributes.language,
			backgroundColor = props.attributes.backgroundColor,
			textColor       = props.attributes.textColor,
			className       = props.className;
		
		function onChangeContent(newValue) {
			props.setAttributes({ content: newValue });
		}
		
		function onChangelanguage(newValue) {
			props.setAttributes({ language: newValue });
		}
		
		return (
			el(
				Fragment,
				null,
				el(
					InspectorControls,
					null,
					el(
						SelectControl,
						{
							label    : __('Select Language for Highlight.js', 'prismatic'),
							value    : language,
							onChange : onChangelanguage,
							options  : [
								{ label : 'Language..',    value : '' },
								
								{ label : 'Apache',        value : 'apache' },
								{ label : 'AppleScript',   value : 'applescript' },
								{ label : 'Arduino',       value : 'arduino' },
								{ label : 'AVR Assembly',  value : 'avr' },
								{ label : 'Bash',          value : 'bash' },
								{ label : 'C',             value : 'c' },
								{ label : 'C#',            value : 'cs' },
								{ label : 'C++',           value : 'cpp' },
								{ label : 'CSS',           value : 'css' },
								{ label : 'CoffeeScript',  value : 'coffeescript' },
								
								{ label : 'D',             value : 'd' },
								{ label : 'Dart',          value : 'dart' },
								{ label : 'Diff',          value : 'diff' },
								{ label : 'Elixir',        value : 'elixir' },
								{ label : 'G-code',        value : 'gcode' },
								{ label : 'GML',           value : 'gml' },
								{ label : 'Go',            value : 'go' },
								{ label : 'Groovy',        value : 'groovy' },
								{ label : 'HTML/XML',      value : 'xml' },
								{ label : 'HTTP',          value : 'http' },
								
								{ label : 'Ini/TOML',      value : 'ini' },
								{ label : 'JSON',          value : 'json' },
								{ label : 'Java',          value : 'java' },
								{ label : 'JavaScript',    value : 'javascript' },
								{ label : 'Kotlin',        value : 'kotlin' },
								{ label : 'Less',          value : 'less' },
								{ label : 'Lua',           value : 'lua' },
								{ label : 'Makefile',      value : 'makefile' },
								{ label : 'Markdown',      value : 'markdown' },
								{ label : 'Nginx',         value : 'nginx' },
								
								{ label : 'Objective-C',   value : 'objectivec' },
								{ label : 'PHP',           value : 'php' },
								{ label : 'Perl',          value : 'perl' },
								{ label : 'Plaintext',     value : 'plaintext' },
								{ label : 'PowerShell',    value : 'powershell' },
								{ label : 'Properties',    value : 'properties' },
								{ label : 'Python',        value : 'python' },
								{ label : 'Python REPL',   value : 'python-repl' },
								{ label : 'R',             value : 'r' },
								{ label : 'Ruby',          value : 'ruby' },
								
								{ label : 'Rust',          value : 'rust' },
								{ label : 'SAS',           value : 'sas' },
								{ label : 'Scala',         value : 'scala' },
								{ label : 'SCSS',          value : 'scss' },
								{ label : 'Shell Session', value : 'shell' },
								{ label : 'SQL',           value : 'sql' },
								{ label : 'Swift',         value : 'swift' },
								{ label : 'TypeScript',    value : 'typescript' },
								{ label : 'VB.Net',        value : 'vbnet' },
								{ label : 'YAML',          value : 'yaml' },
							]
						}
					)
				),
				el(
					PlainText,
					{
						tagName     : 'pre',
						key         : 'editable',
						placeholder : __('Add code..', 'prismatic'),
						className   : className,
						onChange    : onChangeContent,
						style       : { backgroundColor : backgroundColor, color : textColor },
						value       : content,
					}
				)
			)
		);
	},
	
	save : function(props) {
		
		var 
			content  = props.attributes.content,
			language = props.attributes.language;
		
		return el('pre', null, el('code', { className: 'language-'+ language }, content));
		
	},
});