UNPKG

10.7 kBJavaScriptView Raw
1import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2import _extends from "@babel/runtime/helpers/esm/extends";
3import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
4import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
5import _createClass from "@babel/runtime/helpers/esm/createClass";
6import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
7import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
8import _inherits from "@babel/runtime/helpers/esm/inherits";
9import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
10import { createElement } from "@wordpress/element";
11
12/**
13 * External dependencies
14 */
15import classnames from 'classnames';
16/**
17 * WordPress dependencies
18 */
19
20import { __, _x } from '@wordpress/i18n';
21import { Component, Fragment } from '@wordpress/element';
22import { PanelBody, ToggleControl, Toolbar, withFallbackStyles } from '@wordpress/components';
23import { withColors, AlignmentToolbar, BlockControls, ContrastChecker, FontSizePicker, InspectorControls, PanelColorSettings, RichText, withFontSizes } from '@wordpress/block-editor';
24import { createBlock } from '@wordpress/blocks';
25import { compose } from '@wordpress/compose';
26import { withSelect } from '@wordpress/data';
27var _window = window,
28 getComputedStyle = _window.getComputedStyle;
29var name = 'core/paragraph';
30var applyFallbackStyles = withFallbackStyles(function (node, ownProps) {
31 var _ownProps$attributes = ownProps.attributes,
32 textColor = _ownProps$attributes.textColor,
33 backgroundColor = _ownProps$attributes.backgroundColor,
34 fontSize = _ownProps$attributes.fontSize,
35 customFontSize = _ownProps$attributes.customFontSize;
36 var editableNode = node.querySelector('[contenteditable="true"]'); //verify if editableNode is available, before using getComputedStyle.
37
38 var computedStyles = editableNode ? getComputedStyle(editableNode) : null;
39 return {
40 fallbackBackgroundColor: backgroundColor || !computedStyles ? undefined : computedStyles.backgroundColor,
41 fallbackTextColor: textColor || !computedStyles ? undefined : computedStyles.color,
42 fallbackFontSize: fontSize || customFontSize || !computedStyles ? undefined : parseInt(computedStyles.fontSize) || undefined
43 };
44});
45
46var ParagraphBlock =
47/*#__PURE__*/
48function (_Component) {
49 _inherits(ParagraphBlock, _Component);
50
51 function ParagraphBlock() {
52 var _this;
53
54 _classCallCheck(this, ParagraphBlock);
55
56 _this = _possibleConstructorReturn(this, _getPrototypeOf(ParagraphBlock).apply(this, arguments));
57 _this.onReplace = _this.onReplace.bind(_assertThisInitialized(_assertThisInitialized(_this)));
58 _this.toggleDropCap = _this.toggleDropCap.bind(_assertThisInitialized(_assertThisInitialized(_this)));
59 _this.splitBlock = _this.splitBlock.bind(_assertThisInitialized(_assertThisInitialized(_this)));
60 return _this;
61 }
62
63 _createClass(ParagraphBlock, [{
64 key: "onReplace",
65 value: function onReplace(blocks) {
66 var _this$props = this.props,
67 attributes = _this$props.attributes,
68 onReplace = _this$props.onReplace;
69 onReplace(blocks.map(function (block, index) {
70 return index === 0 && block.name === name ? _objectSpread({}, block, {
71 attributes: _objectSpread({}, attributes, block.attributes)
72 }) : block;
73 }));
74 }
75 }, {
76 key: "toggleDropCap",
77 value: function toggleDropCap() {
78 var _this$props2 = this.props,
79 attributes = _this$props2.attributes,
80 setAttributes = _this$props2.setAttributes;
81 setAttributes({
82 dropCap: !attributes.dropCap
83 });
84 }
85 }, {
86 key: "getDropCapHelp",
87 value: function getDropCapHelp(checked) {
88 return checked ? __('Showing large initial letter.') : __('Toggle to show a large initial letter.');
89 }
90 /**
91 * Split handler for RichText value, namely when content is pasted or the
92 * user presses the Enter key.
93 *
94 * @param {?Array} before Optional before value, to be used as content
95 * in place of what exists currently for the
96 * block. If undefined, the block is deleted.
97 * @param {?Array} after Optional after value, to be appended in a new
98 * paragraph block to the set of blocks passed
99 * as spread.
100 * @param {...WPBlock} blocks Optional blocks inserted between the before
101 * and after value blocks.
102 */
103
104 }, {
105 key: "splitBlock",
106 value: function splitBlock(before, after) {
107 var _this$props3 = this.props,
108 attributes = _this$props3.attributes,
109 insertBlocksAfter = _this$props3.insertBlocksAfter,
110 setAttributes = _this$props3.setAttributes,
111 onReplace = _this$props3.onReplace;
112
113 for (var _len = arguments.length, blocks = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
114 blocks[_key - 2] = arguments[_key];
115 }
116
117 if (after !== null) {
118 // Append "After" content as a new paragraph block to the end of
119 // any other blocks being inserted after the current paragraph.
120 blocks.push(createBlock(name, {
121 content: after
122 }));
123 }
124
125 if (blocks.length && insertBlocksAfter) {
126 insertBlocksAfter(blocks);
127 }
128
129 var content = attributes.content;
130
131 if (before === null) {
132 // If before content is omitted, treat as intent to delete block.
133 onReplace([]);
134 } else if (content !== before) {
135 // Only update content if it has in-fact changed. In case that user
136 // has created a new paragraph at end of an existing one, the value
137 // of before will be strictly equal to the current content.
138 setAttributes({
139 content: before
140 });
141 }
142 }
143 }, {
144 key: "render",
145 value: function render() {
146 var _classnames;
147
148 var _this$props4 = this.props,
149 attributes = _this$props4.attributes,
150 setAttributes = _this$props4.setAttributes,
151 mergeBlocks = _this$props4.mergeBlocks,
152 onReplace = _this$props4.onReplace,
153 className = _this$props4.className,
154 backgroundColor = _this$props4.backgroundColor,
155 textColor = _this$props4.textColor,
156 setBackgroundColor = _this$props4.setBackgroundColor,
157 setTextColor = _this$props4.setTextColor,
158 fallbackBackgroundColor = _this$props4.fallbackBackgroundColor,
159 fallbackTextColor = _this$props4.fallbackTextColor,
160 fallbackFontSize = _this$props4.fallbackFontSize,
161 fontSize = _this$props4.fontSize,
162 setFontSize = _this$props4.setFontSize,
163 isRTL = _this$props4.isRTL;
164 var align = attributes.align,
165 content = attributes.content,
166 dropCap = attributes.dropCap,
167 placeholder = attributes.placeholder,
168 direction = attributes.direction;
169 return createElement(Fragment, null, createElement(BlockControls, null, createElement(AlignmentToolbar, {
170 value: align,
171 onChange: function onChange(nextAlign) {
172 setAttributes({
173 align: nextAlign
174 });
175 }
176 }), isRTL && createElement(Toolbar, {
177 controls: [{
178 icon: 'editor-ltr',
179 title: _x('Left to right', 'editor button'),
180 isActive: direction === 'ltr',
181 onClick: function onClick() {
182 var nextDirection = direction === 'ltr' ? undefined : 'ltr';
183 setAttributes({
184 direction: nextDirection
185 });
186 }
187 }]
188 })), createElement(InspectorControls, null, createElement(PanelBody, {
189 title: __('Text Settings'),
190 className: "blocks-font-size"
191 }, createElement(FontSizePicker, {
192 fallbackFontSize: fallbackFontSize,
193 value: fontSize.size,
194 onChange: setFontSize
195 }), createElement(ToggleControl, {
196 label: __('Drop Cap'),
197 checked: !!dropCap,
198 onChange: this.toggleDropCap,
199 help: this.getDropCapHelp
200 })), createElement(PanelColorSettings, {
201 title: __('Color Settings'),
202 initialOpen: false,
203 colorSettings: [{
204 value: backgroundColor.color,
205 onChange: setBackgroundColor,
206 label: __('Background Color')
207 }, {
208 value: textColor.color,
209 onChange: setTextColor,
210 label: __('Text Color')
211 }]
212 }, createElement(ContrastChecker, _extends({
213 textColor: textColor.color,
214 backgroundColor: backgroundColor.color,
215 fallbackTextColor: fallbackTextColor,
216 fallbackBackgroundColor: fallbackBackgroundColor
217 }, {
218 fontSize: fontSize.size
219 })))), createElement(RichText, {
220 identifier: "content",
221 tagName: "p",
222 className: classnames('wp-block-paragraph', className, (_classnames = {
223 'has-text-color': textColor.color,
224 'has-background': backgroundColor.color,
225 'has-drop-cap': dropCap
226 }, _defineProperty(_classnames, backgroundColor.class, backgroundColor.class), _defineProperty(_classnames, textColor.class, textColor.class), _defineProperty(_classnames, fontSize.class, fontSize.class), _classnames)),
227 style: {
228 backgroundColor: backgroundColor.color,
229 color: textColor.color,
230 fontSize: fontSize.size ? fontSize.size + 'px' : undefined,
231 textAlign: align,
232 direction: direction
233 },
234 value: content,
235 onChange: function onChange(nextContent) {
236 setAttributes({
237 content: nextContent
238 });
239 },
240 unstableOnSplit: this.splitBlock,
241 onMerge: mergeBlocks,
242 onReplace: this.onReplace,
243 onRemove: function onRemove() {
244 return onReplace([]);
245 },
246 "aria-label": content ? __('Paragraph block') : __('Empty block; start writing or type forward slash to choose a block'),
247 placeholder: placeholder || __('Start writing or type / to choose a block')
248 }));
249 }
250 }]);
251
252 return ParagraphBlock;
253}(Component);
254
255var ParagraphEdit = compose([withColors('backgroundColor', {
256 textColor: 'color'
257}), withFontSizes('fontSize'), applyFallbackStyles, withSelect(function (select) {
258 var _select = select('core/block-editor'),
259 getSettings = _select.getSettings;
260
261 return {
262 isRTL: getSettings().isRTL
263 };
264})])(ParagraphBlock);
265export default ParagraphEdit;
266//# sourceMappingURL=edit.js.map
\No newline at end of file