1 | import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2 | import _extends from "@babel/runtime/helpers/esm/extends";
|
3 | import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
|
4 | import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
5 | import _createClass from "@babel/runtime/helpers/esm/createClass";
|
6 | import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
|
7 | import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
|
8 | import _inherits from "@babel/runtime/helpers/esm/inherits";
|
9 | import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
|
10 | import { createElement } from "@wordpress/element";
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | import classnames from 'classnames';
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | import { __, _x } from '@wordpress/i18n';
|
21 | import { Component, Fragment } from '@wordpress/element';
|
22 | import { PanelBody, ToggleControl, Toolbar, withFallbackStyles } from '@wordpress/components';
|
23 | import { withColors, AlignmentToolbar, BlockControls, ContrastChecker, FontSizePicker, InspectorControls, PanelColorSettings, RichText, withFontSizes } from '@wordpress/block-editor';
|
24 | import { createBlock } from '@wordpress/blocks';
|
25 | import { compose } from '@wordpress/compose';
|
26 | import { withSelect } from '@wordpress/data';
|
27 | var _window = window,
|
28 | getComputedStyle = _window.getComputedStyle;
|
29 | var name = 'core/paragraph';
|
30 | var 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"]');
|
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 |
|
46 | var ParagraphBlock =
|
47 |
|
48 | function (_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 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
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 |
|
119 |
|
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 |
|
133 | onReplace([]);
|
134 | } else if (content !== before) {
|
135 |
|
136 |
|
137 |
|
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 |
|
255 | var 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);
|
265 | export default ParagraphEdit;
|
266 |
|
\ | No newline at end of file |