PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.4.0
AlphaListing v4.4.0
trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / scripts / blocks / index.js
alphalisting / scripts / blocks Last commit date
attributes.json 1 month ago constants.js 1 month ago edit.js 1 month ago index.js 1 month ago shortcode-attributes.js 1 month ago shortcode-upgrader.js 1 month ago
index.js
159 lines
1 /**
2 * Registers a new block provided a unique name and an object defining its behavior.
3 *
4 * @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
5 */
6 import { __ } from '@wordpress/i18n';
7
8 import { createBlock, registerBlockType } from '@wordpress/blocks';
9 import { createReduxStore, register } from '@wordpress/data';
10 import domReady from '@wordpress/dom-ready';
11 import { applyFilters } from '@wordpress/hooks';
12 import { postList as icon } from '@wordpress/icons';
13
14 import edit from './edit';
15 import globalAttributes from './attributes.json';
16 import DisplayOptions from '../components/DisplayOptions';
17 import ItemSelection from '../components/ItemSelection';
18 import Extensions from '../components/Extensions';
19 import AZInspectorControls from '../components/AZInspectorControls';
20
21 import shortcodeUpgrader from './shortcode-upgrader';
22 import { parseShortcodeAttributes } from './shortcode-attributes';
23
24 const createBlockFromShortcodeText = ( shortcodeText ) => {
25 if ( typeof shortcodeText !== 'string' ) {
26 return createBlock( 'alphalisting/block' );
27 }
28
29 let normalizedShortcode = shortcodeText.trim();
30
31 if ( ! normalizedShortcode.startsWith( '[alphalisting' ) ) {
32 normalizedShortcode = `[alphalisting${normalizedShortcode}`;
33 }
34
35 if ( ! normalizedShortcode.endsWith( ']' ) ) {
36 normalizedShortcode = `${normalizedShortcode}]`;
37 }
38
39 const attributes = parseShortcodeAttributes( normalizedShortcode );
40
41 return createBlock( 'alphalisting/block', attributes );
42 };
43
44 const createBlockFromPrefixContent = ( prefixContent ) => {
45 if ( typeof prefixContent !== 'string' ) {
46 return createBlock( 'alphalisting/block' );
47 }
48
49 const shouldInsertSpace = prefixContent !== '' && ! /^[\s\]]/.test( prefixContent );
50 const normalizedShortcode = `[alphalisting${shouldInsertSpace ? ' ' : ''}${prefixContent}`;
51
52 return createBlockFromShortcodeText( normalizedShortcode );
53 };
54
55 const store = createReduxStore( 'alphalisting/slotfills', {
56 reducer( state = {} ) {
57 return state;
58 },
59 selectors: {
60 getDisplayOptions() {
61 return DisplayOptions;
62 },
63 getItemSelection() {
64 return ItemSelection;
65 },
66 getExtensions() {
67 return Extensions;
68 },
69 getInspectorControls() {
70 return AZInspectorControls;
71 },
72 },
73 } );
74 register( store );
75
76 domReady( () => {
77 const attributes = applyFilters( 'alphalisting_attributes', globalAttributes );
78
79 /**
80 * Every block starts by registering a new block type definition.
81 *
82 * @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
83 */
84 registerBlockType( 'alphalisting/block', {
85 apiVersion: 3,
86
87 /**
88 * This is the display title for your block, which can be translated with `i18n` functions.
89 * The block inserter will show this name.
90 */
91 title: __( 'AlphaListing', 'alphalisting' ),
92
93 /**
94 * This is a short description for your block, can be translated with `i18n` functions.
95 * It will be shown in the Block Tab in the Settings Sidebar.
96 */
97 description: __(
98 'Show your posts in an alphabetically-ordered rolodex-style list',
99 'alphalisting'
100 ),
101
102 /**
103 * Blocks are grouped into categories to help users browse and discover them.
104 * The categories provided by core are `common`, `embed`, `formatting`, `layout` and `widgets`.
105 */
106 category: 'widgets',
107
108 /**
109 * An icon property should be specified to make it easier to identify a block.
110 * These can be any of WordPress’ Dashicons, or a custom svg element.
111 */
112 icon,
113
114 /**
115 * Optional block extended support features.
116 */
117 supports: {
118 align: true,
119 html: false,
120 },
121
122 attributes,
123
124 edit,
125
126 save: () => null,
127
128 transforms: {
129 from: [
130 {
131 type: 'prefix',
132 prefix: '[alphalisting',
133 transform( content ) {
134 return createBlockFromPrefixContent( content );
135 },
136 },
137 {
138 type: 'prefix',
139 prefix: '[alphalisting]',
140 transform( content ) {
141 return createBlockFromPrefixContent( content );
142 },
143 },
144 {
145 type: 'raw',
146 isMatch: ( node ) =>
147 node.nodeName === 'P' &&
148 /^\s*\[alphalisting.*\]\s*$/.test( node.textContent ),
149 transform( node ) {
150 return createBlockFromShortcodeText( node.textContent );
151 },
152 },
153 ],
154 },
155 } );
156
157 shortcodeUpgrader();
158 } );
159