Skip to content

Instantly share code, notes, and snippets.

@n8finch
Last active January 28, 2022 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8finch/2c32945dc40845affdfd41ba1330e4e5 to your computer and use it in GitHub Desktop.
Save n8finch/2c32945dc40845affdfd41ba1330e4e5 to your computer and use it in GitHub Desktop.
/* 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,
applyFilters = wp.hooks.applyFilters,
__ = 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;
languages = [
{ 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' },
];
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 : applyFilters( 'highlightJsLanguages', languages )
}
)
),
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));
},
});
/* Prismatic - Prism.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,
applyFilters = wp.hooks.applyFilters,
__ = 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,
languages = [
{ label : 'Language..', value : '' },
{ label : 'Apache', value : 'apacheconf' },
{ label : 'AppleScript', value : 'applescript' },
{ label : 'Arduino', value : 'arduino' },
{ label : 'Bash', value : 'bash' },
{ label : 'Batch', value : 'batch' },
{ label : 'C', value : 'c' },
{ label : 'C#', value : 'csharp' },
{ label : 'C++', value : 'cpp' },
{ label : 'C-like', value : 'clike' },
{ label : 'CoffeeScript', value : 'coffeescript' },
{ label : 'CSS', value : 'css' },
{ label : 'D', value : 'd' },
{ label : 'Dart', value : 'dart' },
{ label : 'Diff', value : 'diff' },
{ label : 'Elixir', value : 'elixir' },
{ label : 'G-code', value : 'gcode' },
{ label : 'Git', value : 'git' },
{ label : 'Go', value : 'go' },
{ label : 'GraphQL', value : 'graphql' },
{ label : 'Groovy', value : 'groovy' },
{ label : 'HCL', value : 'hcl' },
{ label : 'HTML', value : 'markup' },
{ label : 'HTTP', value : 'http' },
{ label : 'Ini', value : 'ini' },
{ label : 'Java', value : 'java' },
{ label : 'JavaScript', value : 'javascript' },
{ label : 'JSON', value : 'json' },
{ label : 'JSX', value : 'jsx' },
{ label : 'Kotlin', value : 'kotlin' },
{ label : 'LaTeX', value : 'latex' },
{ label : 'Liquid', value : 'liquid' },
{ label : 'Lua', value : 'lua' },
{ label : 'Makefile', value : 'makefile' },
{ label : 'Markdown', value : 'markdown' },
{ label : 'Markup', value : 'markup' },
{ label : 'NGINX', value : 'nginx' },
{ label : 'Objective-C', value : 'objectivec' },
{ label : 'Pascal', value : 'pascal' },
{ label : 'Perl', value : 'perl' },
{ label : 'PHP', value : 'php' },
{ label : 'PowerQuery', value : 'powerquery' },
{ label : 'PowerShell', value : 'powershell' },
{ label : 'Python', value : 'python' },
{ label : 'R', value : 'r' },
{ label : 'Ruby', value : 'ruby' },
{ label : 'Rust', value : 'rust' },
{ label : 'SASS', value : 'sass' },
{ label : 'Scala', value : 'scala' },
{ label : 'SCSS', value : 'scss' },
{ label : 'Shell Session', value : 'shell-session' },
{ label : 'Solidity', value : 'solidity' },
{ label : 'Splunk SPL', value : 'splunk-spl' },
{ label : 'SQL', value : 'sql' },
{ label : 'Swift', value : 'swift' },
{ label : 'TSX', value : 'tsx' },
{ label : 'Twig', value : 'twig' },
{ label : 'TypeScript', value : 'typescript' },
{ label : 'Visual Basic', value : 'visual-basic' },
{ label : 'YAML', value : 'yaml' },
];
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 Prism.js', 'prismatic'),
value : language,
onChange : onChangelanguage,
options : applyFilters( 'prismJsLanguages', languages )
}
)
),
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));
},
});
// Enqueue an override script (in functions.php)
wp_enqueue_script('strattic-prismatic', get_stylesheet_directory_uri() . '/js/prismatic-override.js', array( 'wp-hooks', 'wp-i18n', 'wp-blocks', 'wp-dom' ), $theme->get('Version'), true );
===
// This is the prismatic-override.js file
wp.hooks.addFilter('prismJsLanguages', 'prismatic/blocks', function(languages){
return [
{ label : 'Language..', value : '' },
{ label : 'PHP', value : 'php' },
{ label : 'JavaScript', value : 'javascript' },
{ label : 'HTML', value : 'markup' },
{ label : 'JSON', value : 'json' },
{ label : 'YAML', value : 'yaml' }
];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment