| 1 |
/** |
| 2 |
* JavaScript code for the TablePress table block in the block editor. |
| 3 |
* |
| 4 |
* @package TablePress |
| 5 |
* @subpackage Blocks |
| 6 |
* @author Tobias Bäthge |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/* globals tp */ |
| 11 |
|
| 12 |
/** |
| 13 |
* WordPress dependencies. |
| 14 |
*/ |
| 15 |
import { createBlock } from '@wordpress/blocks'; |
| 16 |
import shortcode from '@wordpress/shortcode'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Get the block name from the block.json. |
| 20 |
*/ |
| 21 |
import block from '../block.json'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Internal dependencies. |
| 25 |
*/ |
| 26 |
import { shortcode_attrs_to_string } from './common/functions'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Converts a textual Shortcode to a TablePress table block. |
| 30 |
* |
| 31 |
* @param {string} content The Shortcode as a text string. |
| 32 |
* @return {Object} TablePress table block. |
| 33 |
*/ |
| 34 |
const convertShortcodeTextToBlock = function ( content ) { |
| 35 |
let shortcodeAttrs = shortcode.next( tp.table.shortcode, content ).shortcode.attrs; |
| 36 |
shortcodeAttrs = { named: { ...shortcodeAttrs.named }, numeric: [ ...shortcodeAttrs.numeric ] }; // Use object destructuring to get a clone of the object. |
| 37 |
const id = shortcodeAttrs.named.id; |
| 38 |
delete shortcodeAttrs.named.id; |
| 39 |
let parameters = shortcode_attrs_to_string( shortcodeAttrs ); |
| 40 |
parameters = parameters.replace( /=“([^”]*)”/g, '="$1"' ); // Replace curly quotation marks around a value with normal ones. |
| 41 |
return createBlock( block.name, { id, parameters } ); |
| 42 |
}; |
| 43 |
|
| 44 |
const transforms = { |
| 45 |
from: [ |
| 46 |
// Detect table Shortcodes that are pasted into the block editor. |
| 47 |
{ |
| 48 |
type: 'shortcode', |
| 49 |
tag: tp.table.shortcode, |
| 50 |
attributes: { |
| 51 |
id: { |
| 52 |
type: 'string', |
| 53 |
shortcode: ( { named: { id = '' } } ) => { |
| 54 |
return id; |
| 55 |
}, |
| 56 |
}, |
| 57 |
parameters: { |
| 58 |
type: 'string', |
| 59 |
shortcode: ( shortcodeAttrs ) => { |
| 60 |
shortcodeAttrs = { named: { ...shortcodeAttrs.named }, numeric: [ ...shortcodeAttrs.numeric ] }; // Use object destructuring to get a clone of the object. |
| 61 |
delete shortcodeAttrs.named.id; |
| 62 |
return shortcode_attrs_to_string( shortcodeAttrs ); |
| 63 |
}, |
| 64 |
}, |
| 65 |
}, |
| 66 |
}, |
| 67 |
|
| 68 |
// Detect table Shortcodes that are typed into the block editor. |
| 69 |
{ |
| 70 |
type: 'enter', |
| 71 |
regExp: shortcode.regexp( tp.table.shortcode ), |
| 72 |
transform: ( { content } ) => convertShortcodeTextToBlock( content ), |
| 73 |
}, |
| 74 |
|
| 75 |
// Add conversion option from "Shortcode" to "TablePress table" block. |
| 76 |
{ |
| 77 |
type: 'block', |
| 78 |
blocks: [ 'core/shortcode' ], |
| 79 |
transform: ( { text: content } ) => convertShortcodeTextToBlock( content ), |
| 80 |
isMatch: ( { text } ) => { |
| 81 |
return ( undefined !== shortcode.next( tp.table.shortcode, text ) ); |
| 82 |
}, |
| 83 |
isMultiBlock: false, |
| 84 |
}, |
| 85 |
], |
| 86 |
|
| 87 |
to: [ |
| 88 |
// Add conversion option from "TablePress table" to "Shortcode" block. |
| 89 |
{ |
| 90 |
type: 'block', |
| 91 |
blocks: [ 'core/shortcode' ], |
| 92 |
transform: ( { id, parameters } ) => { |
| 93 |
parameters = parameters.trim(); |
| 94 |
if ( '' !== parameters ) { |
| 95 |
parameters += ' '; |
| 96 |
} |
| 97 |
const text = `[${ tp.table.shortcode } id=${ id } ${ parameters }/]`; |
| 98 |
return createBlock( 'core/shortcode', { text } ); |
| 99 |
}, |
| 100 |
}, |
| 101 |
], |
| 102 |
}; |
| 103 |
|
| 104 |
export default transforms; |
| 105 |
|