UNPKG

5.53 kBJavaScriptView Raw
1import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
2import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3import _createClass from "@babel/runtime/helpers/esm/createClass";
4import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
5import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
6import _inherits from "@babel/runtime/helpers/esm/inherits";
7import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
8import { createElement } from "@wordpress/element";
9
10/**
11 * External dependencies
12 */
13import { View } from 'react-native';
14/**
15 * WordPress dependencies
16 */
17
18import { __ } from '@wordpress/i18n';
19import { Component } from '@wordpress/element';
20import { createBlock } from '@wordpress/blocks';
21import { RichText } from '@wordpress/block-editor';
22/**
23 * Internal dependencies
24 */
25
26import styles from './style.scss';
27var name = 'core/paragraph';
28
29var ParagraphEdit =
30/*#__PURE__*/
31function (_Component) {
32 _inherits(ParagraphEdit, _Component);
33
34 function ParagraphEdit(props) {
35 var _this;
36
37 _classCallCheck(this, ParagraphEdit);
38
39 _this = _possibleConstructorReturn(this, _getPrototypeOf(ParagraphEdit).call(this, props));
40 _this.splitBlock = _this.splitBlock.bind(_assertThisInitialized(_assertThisInitialized(_this)));
41 _this.onReplace = _this.onReplace.bind(_assertThisInitialized(_assertThisInitialized(_this)));
42 _this.state = {
43 aztecHeight: 0
44 };
45 return _this;
46 }
47 /**
48 * Split handler for RichText value, namely when content is pasted or the
49 * user presses the Enter key.
50 *
51 * @param {?Array} before Optional before value, to be used as content
52 * in place of what exists currently for the
53 * block. If undefined, the block is deleted.
54 * @param {?Array} after Optional after value, to be appended in a new
55 * paragraph block to the set of blocks passed
56 * as spread.
57 * @param {...WPBlock} blocks Optional blocks inserted between the before
58 * and after value blocks.
59 */
60
61
62 _createClass(ParagraphEdit, [{
63 key: "splitBlock",
64 value: function splitBlock(before, after) {
65 var _this$props = this.props,
66 attributes = _this$props.attributes,
67 insertBlocksAfter = _this$props.insertBlocksAfter,
68 setAttributes = _this$props.setAttributes,
69 onReplace = _this$props.onReplace;
70
71 for (var _len = arguments.length, blocks = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
72 blocks[_key - 2] = arguments[_key];
73 }
74
75 if (after !== null) {
76 // Append "After" content as a new paragraph block to the end of
77 // any other blocks being inserted after the current paragraph.
78 var newBlock = createBlock(name, {
79 content: after
80 });
81 blocks.push(newBlock);
82 }
83
84 if (blocks.length && insertBlocksAfter) {
85 insertBlocksAfter(blocks);
86 }
87
88 var content = attributes.content;
89
90 if (before === null) {
91 onReplace([]);
92 } else if (content !== before) {
93 // Only update content if it has in-fact changed. In case that user
94 // has created a new paragraph at end of an existing one, the value
95 // of before will be strictly equal to the current content.
96 setAttributes({
97 content: before
98 });
99 }
100 }
101 }, {
102 key: "onReplace",
103 value: function onReplace(blocks) {
104 var _this$props2 = this.props,
105 attributes = _this$props2.attributes,
106 onReplace = _this$props2.onReplace;
107 onReplace(blocks.map(function (block, index) {
108 return index === 0 && block.name === name ? _objectSpread({}, block, {
109 attributes: _objectSpread({}, attributes, block.attributes)
110 }) : block;
111 }));
112 }
113 }, {
114 key: "render",
115 value: function render() {
116 var _this2 = this;
117
118 var _this$props3 = this.props,
119 attributes = _this$props3.attributes,
120 setAttributes = _this$props3.setAttributes,
121 mergeBlocks = _this$props3.mergeBlocks,
122 style = _this$props3.style;
123 var placeholder = attributes.placeholder,
124 content = attributes.content;
125 var minHeight = styles.blockText.minHeight;
126 return createElement(View, null, createElement(RichText, {
127 tagName: "p",
128 value: content,
129 isSelected: this.props.isSelected,
130 onFocus: this.props.onFocus // always assign onFocus as a props
131 ,
132 onBlur: this.props.onBlur // always assign onBlur as a props
133 ,
134 onCaretVerticalPositionChange: this.props.onCaretVerticalPositionChange,
135 style: _objectSpread({}, style, {
136 minHeight: Math.max(minHeight, this.state.aztecHeight)
137 }),
138 onChange: function onChange(nextContent) {
139 setAttributes({
140 content: nextContent
141 });
142 },
143 onSplit: this.splitBlock,
144 onMerge: mergeBlocks,
145 onReplace: this.onReplace,
146 onContentSizeChange: function onContentSizeChange(event) {
147 _this2.setState({
148 aztecHeight: event.aztecHeight
149 });
150 },
151 placeholder: placeholder || __('Start writing…')
152 }));
153 }
154 }]);
155
156 return ParagraphEdit;
157}(Component);
158
159export default ParagraphEdit;
160//# sourceMappingURL=edit.native.js.map
\No newline at end of file