-
-
Save 5ally/d659576342bc58d46ffa66cb34f1273f to your computer and use it in GitHub Desktop.
See https://wordpress.stackexchange.com/q/369904 for details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******/ (function(modules) { // webpackBootstrap | |
/******/ // The module cache | |
/******/ var installedModules = {}; | |
/******/ | |
/******/ // The require function | |
/******/ function __webpack_require__(moduleId) { | |
/******/ | |
/******/ // Check if module is in cache | |
/******/ if(installedModules[moduleId]) { | |
/******/ return installedModules[moduleId].exports; | |
/******/ } | |
/******/ // Create a new module (and put it into the cache) | |
/******/ var module = installedModules[moduleId] = { | |
/******/ i: moduleId, | |
/******/ l: false, | |
/******/ exports: {} | |
/******/ }; | |
/******/ | |
/******/ // Execute the module function | |
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | |
/******/ | |
/******/ // Flag the module as loaded | |
/******/ module.l = true; | |
/******/ | |
/******/ // Return the exports of the module | |
/******/ return module.exports; | |
/******/ } | |
/******/ | |
/******/ | |
/******/ // expose the modules object (__webpack_modules__) | |
/******/ __webpack_require__.m = modules; | |
/******/ | |
/******/ // expose the module cache | |
/******/ __webpack_require__.c = installedModules; | |
/******/ | |
/******/ // define getter function for harmony exports | |
/******/ __webpack_require__.d = function(exports, name, getter) { | |
/******/ if(!__webpack_require__.o(exports, name)) { | |
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); | |
/******/ } | |
/******/ }; | |
/******/ | |
/******/ // define __esModule on exports | |
/******/ __webpack_require__.r = function(exports) { | |
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { | |
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); | |
/******/ } | |
/******/ Object.defineProperty(exports, '__esModule', { value: true }); | |
/******/ }; | |
/******/ | |
/******/ // create a fake namespace object | |
/******/ // mode & 1: value is a module id, require it | |
/******/ // mode & 2: merge all properties of value into the ns | |
/******/ // mode & 4: return value when already ns object | |
/******/ // mode & 8|1: behave like require | |
/******/ __webpack_require__.t = function(value, mode) { | |
/******/ if(mode & 1) value = __webpack_require__(value); | |
/******/ if(mode & 8) return value; | |
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; | |
/******/ var ns = Object.create(null); | |
/******/ __webpack_require__.r(ns); | |
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); | |
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); | |
/******/ return ns; | |
/******/ }; | |
/******/ | |
/******/ // getDefaultExport function for compatibility with non-harmony modules | |
/******/ __webpack_require__.n = function(module) { | |
/******/ var getter = module && module.__esModule ? | |
/******/ function getDefault() { return module['default']; } : | |
/******/ function getModuleExports() { return module; }; | |
/******/ __webpack_require__.d(getter, 'a', getter); | |
/******/ return getter; | |
/******/ }; | |
/******/ | |
/******/ // Object.prototype.hasOwnProperty.call | |
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | |
/******/ | |
/******/ // __webpack_public_path__ | |
/******/ __webpack_require__.p = ""; | |
/******/ | |
/******/ | |
/******/ // Load entry module and return exports | |
/******/ return __webpack_require__(__webpack_require__.s = "./wpse-369904/src/index.js"); | |
/******/ }) | |
/************************************************************************/ | |
/******/ ({ | |
/***/ "./node_modules/@babel/runtime/helpers/defineProperty.js": | |
/*!***************************************************************!*\ | |
!*** ./node_modules/@babel/runtime/helpers/defineProperty.js ***! | |
\***************************************************************/ | |
/*! no static exports found */ | |
/***/ (function(module, exports) { | |
function _defineProperty(obj, key, value) { | |
if (key in obj) { | |
Object.defineProperty(obj, key, { | |
value: value, | |
enumerable: true, | |
configurable: true, | |
writable: true | |
}); | |
} else { | |
obj[key] = value; | |
} | |
return obj; | |
} | |
module.exports = _defineProperty; | |
/***/ }), | |
/***/ "./node_modules/classnames/dedupe.js": | |
/*!*******************************************!*\ | |
!*** ./node_modules/classnames/dedupe.js ***! | |
\*******************************************/ | |
/*! no static exports found */ | |
/***/ (function(module, exports, __webpack_require__) { | |
var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;/*! | |
Copyright (c) 2017 Jed Watson. | |
Licensed under the MIT License (MIT), see | |
http://jedwatson.github.io/classnames | |
*/ | |
/* global define */ | |
(function () { | |
'use strict'; | |
var classNames = (function () { | |
// don't inherit from Object so we can skip hasOwnProperty check later | |
// http://stackoverflow.com/questions/15518328/creating-js-object-with-object-createnull#answer-21079232 | |
function StorageObject() {} | |
StorageObject.prototype = Object.create(null); | |
function _parseArray (resultSet, array) { | |
var length = array.length; | |
for (var i = 0; i < length; ++i) { | |
_parse(resultSet, array[i]); | |
} | |
} | |
var hasOwn = {}.hasOwnProperty; | |
function _parseNumber (resultSet, num) { | |
resultSet[num] = true; | |
} | |
function _parseObject (resultSet, object) { | |
for (var k in object) { | |
if (hasOwn.call(object, k)) { | |
// set value to false instead of deleting it to avoid changing object structure | |
// https://www.smashingmagazine.com/2012/11/writing-fast-memory-efficient-javascript/#de-referencing-misconceptions | |
resultSet[k] = !!object[k]; | |
} | |
} | |
} | |
var SPACE = /\s+/; | |
function _parseString (resultSet, str) { | |
var array = str.split(SPACE); | |
var length = array.length; | |
for (var i = 0; i < length; ++i) { | |
resultSet[array[i]] = true; | |
} | |
} | |
function _parse (resultSet, arg) { | |
if (!arg) return; | |
var argType = typeof arg; | |
// 'foo bar' | |
if (argType === 'string') { | |
_parseString(resultSet, arg); | |
// ['foo', 'bar', ...] | |
} else if (Array.isArray(arg)) { | |
_parseArray(resultSet, arg); | |
// { 'foo': true, ... } | |
} else if (argType === 'object') { | |
_parseObject(resultSet, arg); | |
// '130' | |
} else if (argType === 'number') { | |
_parseNumber(resultSet, arg); | |
} | |
} | |
function _classNames () { | |
// don't leak arguments | |
// https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments | |
var len = arguments.length; | |
var args = Array(len); | |
for (var i = 0; i < len; i++) { | |
args[i] = arguments[i]; | |
} | |
var classSet = new StorageObject(); | |
_parseArray(classSet, args); | |
var list = []; | |
for (var k in classSet) { | |
if (classSet[k]) { | |
list.push(k) | |
} | |
} | |
return list.join(' '); | |
} | |
return _classNames; | |
})(); | |
if ( true && module.exports) { | |
classNames.default = classNames; | |
module.exports = classNames; | |
} else if (true) { | |
// register as 'classnames', consistent with npm package name | |
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = (function () { | |
return classNames; | |
}).apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), | |
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); | |
} else {} | |
}()); | |
/***/ }), | |
/***/ "./wpse-369904/src/index.js": | |
/*!**********************************!*\ | |
!*** ./wpse-369904/src/index.js ***! | |
\**********************************/ | |
/*! no exports provided */ | |
/***/ (function(module, __webpack_exports__, __webpack_require__) { | |
"use strict"; | |
__webpack_require__.r(__webpack_exports__); | |
/* harmony import */ var _babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @babel/runtime/helpers/defineProperty */ "./node_modules/@babel/runtime/helpers/defineProperty.js"); | |
/* harmony import */ var _babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_0__); | |
/* harmony import */ var _wordpress_element__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @wordpress/element */ "@wordpress/element"); | |
/* harmony import */ var _wordpress_element__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__); | |
/* harmony import */ var classnames_dedupe__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! classnames/dedupe */ "./node_modules/classnames/dedupe.js"); | |
/* harmony import */ var classnames_dedupe__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(classnames_dedupe__WEBPACK_IMPORTED_MODULE_2__); | |
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | |
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_0___default()(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | |
// These are my version. I hope it helps. :) | |
// You should fix the unused vars/constant - try linting your code. | |
var wp = window.wp; | |
var __ = wp.i18n.__; | |
var addFilter = wp.hooks.addFilter; | |
var assign = window.lodash.assign; | |
var createHigherOrderComponent = wp.compose.createHigherOrderComponent; | |
var Fragment = wp.element.Fragment; | |
var _wp$blockEditor = wp.blockEditor, | |
RichText = _wp$blockEditor.RichText, | |
InspectorControls = _wp$blockEditor.InspectorControls, | |
getColorClassName = _wp$blockEditor.getColorClassName; // not wp.editor | |
var _wp$components = wp.components, | |
PanelBody = _wp$components.PanelBody, | |
TextControl = _wp$components.TextControl, | |
ToggleControl = _wp$components.ToggleControl; | |
var filterBlocks = function filterBlocks(settings) { | |
//console.log(settings); | |
if (settings.name !== 'core/table') { | |
return settings; | |
} | |
var newSettings = _objectSpread(_objectSpread({}, settings), {}, { | |
attributes: _objectSpread(_objectSpread({}, settings.attributes), {}, { | |
// spread in old attributes so we don't lose them! | |
tableClass: { | |
// here is our new attribute | |
type: 'string', | |
default: 'table ' // should be 'table table-hover' | |
// Just my suggestion: | |
/* | |
source: 'attribute', | |
attribute: 'class', | |
selector: 'table', | |
*/ | |
}, | |
tableBordered: { | |
// here is our new attribute | |
type: 'boolean', | |
default: false | |
}, | |
tableStriped: { | |
// here is our new attribute | |
type: 'boolean', | |
default: false | |
}, | |
tableHover: { | |
// here is our new attribute | |
type: 'boolean', | |
default: true | |
} | |
}), | |
save: function save(_ref) { | |
var attributes = _ref.attributes; | |
var hasFixedLayout = attributes.hasFixedLayout, | |
head = attributes.head, | |
body = attributes.body, | |
foot = attributes.foot, | |
backgroundColor = attributes.backgroundColor, | |
caption = attributes.caption, | |
tableBordered = attributes.tableBordered, | |
tableStriped = attributes.tableStriped, | |
tableHover = attributes.tableHover, | |
tableClass = attributes.tableClass; | |
var isEmpty = !head.length && !body.length && !foot.length; | |
if (isEmpty) { | |
return null; | |
} | |
var classes = buildTableClasses(attributes); | |
var hasCaption = !RichText.isEmpty(caption); | |
var Section = function Section(_ref2) { | |
var type = _ref2.type, | |
rows = _ref2.rows; | |
if (!rows.length) { | |
return null; | |
} | |
var Tag = "t".concat(type); | |
return Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(Tag, null, rows.map(function (_ref3, rowIndex) { | |
var cells = _ref3.cells; | |
return Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])("tr", { | |
key: rowIndex | |
}, cells.map(function (_ref4, cellIndex) { | |
var content = _ref4.content, | |
tag = _ref4.tag, | |
scope = _ref4.scope, | |
align = _ref4.align; | |
var cellClasses = classnames_dedupe__WEBPACK_IMPORTED_MODULE_2___default()(_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_0___default()({}, "has-text-align-".concat(align), align)); | |
return Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(RichText.Content, { | |
className: cellClasses ? cellClasses : undefined, | |
"data-align": align, | |
tagName: tag, | |
value: content, | |
key: cellIndex, | |
scope: tag === 'th' ? scope : undefined | |
}); | |
})); | |
})); | |
}; | |
return Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])("figure", null, Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])("table", { | |
className: classes === '' ? undefined : classes | |
}, Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(Section, { | |
type: "head", | |
rows: head | |
}), Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(Section, { | |
type: "body", | |
rows: body | |
}), Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(Section, { | |
type: "foot", | |
rows: foot | |
})), hasCaption && Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(RichText.Content, { | |
tagName: "figcaption", | |
value: caption | |
})); | |
} | |
}); | |
return newSettings; | |
}; | |
addFilter('blocks.registerBlockType', 'example/filter-blocks', filterBlocks); | |
function buildTableClasses(attributes) { | |
var hasFixedLayout = attributes.hasFixedLayout, | |
backgroundColor = attributes.backgroundColor, | |
tableStriped = attributes.tableStriped, | |
tableBordered = attributes.tableBordered, | |
tableHover = attributes.tableHover, | |
tableClass = attributes.tableClass; | |
var backgroundClass = getColorClassName('background-color', backgroundColor); | |
var classes = classnames_dedupe__WEBPACK_IMPORTED_MODULE_2___default()(tableClass.split(' '), backgroundClass, { | |
table: true, | |
'has-fixed-layout': hasFixedLayout, | |
'has-background': !!backgroundClass, | |
'table-bordered': tableBordered, | |
'table-striped': tableStriped, | |
'table-hover': tableHover | |
}); | |
return classes; | |
} | |
var tableClassControl = createHigherOrderComponent(function (BlockEdit) { | |
return function (props) { | |
if ('core/table' !== props.name) { | |
return Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(BlockEdit, props); | |
} | |
var _props$attributes = props.attributes, | |
tableStriped = _props$attributes.tableStriped, | |
tableBordered = _props$attributes.tableBordered, | |
tableHover = _props$attributes.tableHover, | |
tableClass = _props$attributes.tableClass; | |
function onChangeTableHover() { | |
props.setAttributes({ | |
// Toggle the state. | |
tableHover: !tableHover, | |
// Then add/remove only the table-hover class. | |
tableClass: classnames_dedupe__WEBPACK_IMPORTED_MODULE_2___default()(tableClass, { | |
'table-hover': !tableHover | |
}) | |
}); | |
} | |
function onChangeTableBordered() { | |
props.setAttributes({ | |
tableBordered: !tableBordered, | |
tableClass: classnames_dedupe__WEBPACK_IMPORTED_MODULE_2___default()(tableClass, { | |
'table-bordered': !tableBordered | |
}) | |
}); | |
} | |
function onChangeTableStriped() { | |
props.setAttributes({ | |
tableStriped: !tableStriped, | |
tableClass: classnames_dedupe__WEBPACK_IMPORTED_MODULE_2___default()(tableClass, { | |
'table-striped': !tableStriped | |
}) | |
}); | |
} | |
function onChangeTableClass(value) { | |
// User likely typed a whitespace. | |
if (tableClass === value.trim()) { | |
props.setAttributes({ | |
tableClass: value | |
}); | |
return; | |
} | |
var list = value.split(/ +/); | |
props.setAttributes({ | |
// Update the value. | |
tableClass: classnames_dedupe__WEBPACK_IMPORTED_MODULE_2___default()(value), | |
// Then the toggles. | |
tableHover: list.indexOf('table-hover') >= 0, | |
tableBordered: list.indexOf('table-bordered') >= 0, | |
tableStriped: list.indexOf('table-striped') >= 0 | |
}); | |
} | |
return Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(Fragment, null, Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(BlockEdit, props), Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(InspectorControls, null, Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(PanelBody, { | |
title: __('Table classes'), | |
initialOpen: true | |
}, Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(ToggleControl, { | |
label: __('Striped'), | |
checked: !!tableStriped, | |
onChange: onChangeTableStriped | |
}), Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(ToggleControl, { | |
label: __('Bordered'), | |
checked: !!tableBordered, | |
onChange: onChangeTableBordered | |
}), Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(ToggleControl, { | |
label: __('Hover'), | |
checked: !!tableHover, | |
onChange: onChangeTableHover | |
}), Object(_wordpress_element__WEBPACK_IMPORTED_MODULE_1__["createElement"])(TextControl, { | |
label: __('<table> classes'), | |
type: "text", | |
value: tableClass, | |
onChange: onChangeTableClass | |
})))); | |
}; | |
}, 'tableClassControl'); | |
addFilter('editor.BlockEdit', 'extend-block-example/with-spacing-control', tableClassControl); | |
/***/ }), | |
/***/ "@wordpress/element": | |
/*!******************************************!*\ | |
!*** external {"this":["wp","element"]} ***! | |
\******************************************/ | |
/*! no static exports found */ | |
/***/ (function(module, exports) { | |
(function() { module.exports = this["wp"]["element"]; }()); | |
/***/ }) | |
/******/ }); | |
//# sourceMappingURL=index.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// These are my version. I hope it helps. :) | |
// You should fix the unused vars/constant - try linting your code. | |
import classnames from 'classnames/dedupe'; | |
const wp = window.wp; | |
const { __ } = wp.i18n; | |
const { addFilter } = wp.hooks; | |
const { assign } = window.lodash; | |
const { | |
createHigherOrderComponent, | |
} = wp.compose; | |
const { | |
Fragment, | |
} = wp.element; | |
const { | |
RichText, | |
InspectorControls, | |
getColorClassName, | |
} = wp.blockEditor; // not wp.editor | |
const { | |
PanelBody, | |
TextControl, | |
ToggleControl, | |
} = wp.components; | |
const filterBlocks = ( settings ) => { | |
//console.log( settings ); | |
if ( settings.name !== 'core/table' ) { | |
return settings; | |
} | |
const newSettings = { | |
...settings, | |
attributes: { | |
...settings.attributes, // spread in old attributes so we don't lose them! | |
tableClass: { // here is our new attribute | |
type: 'string', | |
default: 'table ', // should be 'table table-hover' | |
// Just my suggestion: | |
/* | |
source: 'attribute', | |
attribute: 'class', | |
selector: 'table', | |
*/ | |
}, | |
tableBordered: { // here is our new attribute | |
type: 'boolean', | |
default: false, | |
}, | |
tableStriped: { // here is our new attribute | |
type: 'boolean', | |
default: false, | |
}, | |
tableHover: { // here is our new attribute | |
type: 'boolean', | |
default: true, | |
}, | |
}, | |
save( { attributes } ) { | |
const { | |
hasFixedLayout, | |
head, | |
body, | |
foot, | |
backgroundColor, | |
caption, | |
tableBordered, | |
tableStriped, | |
tableHover, | |
tableClass, | |
} = attributes; | |
const isEmpty = ! head.length && ! body.length && ! foot.length; | |
if ( isEmpty ) { | |
return null; | |
} | |
const classes = buildTableClasses( attributes ); | |
const hasCaption = ! RichText.isEmpty( caption ); | |
const Section = ( { type, rows } ) => { | |
if ( ! rows.length ) { | |
return null; | |
} | |
const Tag = `t${ type }`; | |
return ( | |
<Tag> | |
{ rows.map( ( { cells }, rowIndex ) => ( | |
<tr key={ rowIndex }> | |
{ cells.map( | |
( { content, tag, scope, align }, cellIndex ) => { | |
const cellClasses = classnames( { | |
[ `has-text-align-${ align }` ]: align, | |
} ); | |
return ( | |
<RichText.Content | |
className={ | |
cellClasses ? | |
cellClasses : | |
undefined | |
} | |
data-align={ align } | |
tagName={ tag } | |
value={ content } | |
key={ cellIndex } | |
scope={ | |
tag === 'th' ? scope : undefined | |
} | |
/> | |
); | |
} | |
) } | |
</tr> | |
) ) } | |
</Tag> | |
); | |
}; | |
return ( | |
<figure> | |
<table className={ classes === '' ? undefined : classes }> | |
<Section type="head" rows={ head } /> | |
<Section type="body" rows={ body } /> | |
<Section type="foot" rows={ foot } /> | |
</table> | |
{ hasCaption && ( | |
<RichText.Content tagName="figcaption" value={ caption } /> | |
) } | |
</figure> | |
); | |
}, | |
}; | |
return newSettings; | |
}; | |
addFilter( | |
'blocks.registerBlockType', | |
'example/filter-blocks', | |
filterBlocks | |
); | |
function buildTableClasses( attributes ) { | |
const { | |
hasFixedLayout, | |
backgroundColor, | |
tableStriped, | |
tableBordered, | |
tableHover, | |
tableClass, | |
} = attributes; | |
const backgroundClass = getColorClassName( | |
'background-color', | |
backgroundColor | |
); | |
const classes = classnames( | |
tableClass.split( ' ' ), | |
backgroundClass, | |
{ | |
table: true, | |
'has-fixed-layout': hasFixedLayout, | |
'has-background': !! backgroundClass, | |
'table-bordered': tableBordered, | |
'table-striped': tableStriped, | |
'table-hover': tableHover, | |
} | |
); | |
return classes; | |
} | |
const tableClassControl = createHigherOrderComponent( ( BlockEdit ) => { | |
return ( props ) => { | |
if ( 'core/table' !== props.name ) { | |
return ( | |
<BlockEdit { ...props } /> | |
); | |
} | |
const { | |
tableStriped, | |
tableBordered, | |
tableHover, | |
tableClass, | |
} = props.attributes; | |
function onChangeTableHover() { | |
props.setAttributes( { | |
// Toggle the state. | |
tableHover: ! tableHover, | |
// Then add/remove only the table-hover class. | |
tableClass: classnames( tableClass, { | |
'table-hover': ! tableHover, | |
} ), | |
} ); | |
} | |
function onChangeTableBordered() { | |
props.setAttributes( { | |
tableBordered: ! tableBordered, | |
tableClass: classnames( tableClass, { | |
'table-bordered': ! tableBordered, | |
} ), | |
} ); | |
} | |
function onChangeTableStriped() { | |
props.setAttributes( { | |
tableStriped: ! tableStriped, | |
tableClass: classnames( tableClass, { | |
'table-striped': ! tableStriped, | |
} ), | |
} ); | |
} | |
function onChangeTableClass( value ) { | |
// User likely typed a whitespace. | |
if ( tableClass === value.trim() ) { | |
props.setAttributes( { tableClass: value } ); | |
return; | |
} | |
const list = value.split( / +/ ); | |
props.setAttributes( { | |
// Update the value. | |
tableClass: classnames( value ), | |
// Then the toggles. | |
tableHover: list.indexOf( 'table-hover' ) >= 0, | |
tableBordered: list.indexOf( 'table-bordered' ) >= 0, | |
tableStriped: list.indexOf( 'table-striped' ) >= 0, | |
} ); | |
} | |
return ( | |
<Fragment> | |
<BlockEdit { ...props } /> | |
<InspectorControls> | |
<PanelBody | |
title={ __( 'Table classes' ) } | |
initialOpen={ true } | |
> | |
<ToggleControl | |
label={ __( 'Striped' ) } | |
checked={ !! tableStriped } | |
onChange={ onChangeTableStriped } | |
/> | |
<ToggleControl | |
label={ __( 'Bordered' ) } | |
checked={ !! tableBordered } | |
onChange={ onChangeTableBordered } | |
/> | |
<ToggleControl | |
label={ __( 'Hover' ) } | |
checked={ !! tableHover } | |
onChange={ onChangeTableHover } | |
/> | |
<TextControl | |
label={ __( '<table> classes' ) } | |
type="text" | |
value={ tableClass } | |
onChange={ onChangeTableClass } | |
/> | |
</PanelBody> | |
</InspectorControls> | |
</Fragment> | |
); | |
}; | |
}, 'tableClassControl' ); | |
addFilter( 'editor.BlockEdit', 'extend-block-example/with-spacing-control', tableClassControl ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment