UNPKG

5.75 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.getLevelFromHeadingNodeName = getLevelFromHeadingNodeName;
9exports.settings = exports.name = void 0;
10
11var _element = require("@wordpress/element");
12
13var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
14
15var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
16
17var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
18
19var _lodash = require("lodash");
20
21var _i18n = require("@wordpress/i18n");
22
23var _blocks = require("@wordpress/blocks");
24
25var _blockEditor = require("@wordpress/block-editor");
26
27var _components = require("@wordpress/components");
28
29var _edit = _interopRequireDefault(require("./edit"));
30
31/**
32 * External dependencies
33 */
34
35/**
36 * WordPress dependencies
37 */
38
39/**
40 * Internal dependencies
41 */
42
43/**
44 * Given a node name string for a heading node, returns its numeric level.
45 *
46 * @param {string} nodeName Heading node name.
47 *
48 * @return {number} Heading level.
49 */
50function getLevelFromHeadingNodeName(nodeName) {
51 return Number(nodeName.substr(1));
52}
53
54var supports = {
55 className: false,
56 anchor: true
57};
58var schema = {
59 content: {
60 type: 'string',
61 source: 'html',
62 selector: 'h1,h2,h3,h4,h5,h6',
63 default: ''
64 },
65 level: {
66 type: 'number',
67 default: 2
68 },
69 align: {
70 type: 'string'
71 },
72 placeholder: {
73 type: 'string'
74 }
75};
76var name = 'core/heading';
77exports.name = name;
78var settings = {
79 title: (0, _i18n.__)('Heading'),
80 description: (0, _i18n.__)('Introduce new sections and organize content to help visitors (and search engines) understand the structure of your content.'),
81 icon: (0, _element.createElement)(_components.SVG, {
82 xmlns: "http://www.w3.org/2000/svg",
83 viewBox: "0 0 24 24"
84 }, (0, _element.createElement)(_components.Path, {
85 d: "M5 4v3h5.5v12h3V7H19V4z"
86 }), (0, _element.createElement)(_components.Path, {
87 fill: "none",
88 d: "M0 0h24v24H0V0z"
89 })),
90 category: 'common',
91 keywords: [(0, _i18n.__)('title'), (0, _i18n.__)('subtitle')],
92 supports: supports,
93 attributes: schema,
94 transforms: {
95 from: [{
96 type: 'block',
97 blocks: ['core/paragraph'],
98 transform: function transform(_ref) {
99 var content = _ref.content;
100 return (0, _blocks.createBlock)('core/heading', {
101 content: content
102 });
103 }
104 }, {
105 type: 'raw',
106 selector: 'h1,h2,h3,h4,h5,h6',
107 schema: {
108 h1: {
109 children: (0, _blocks.getPhrasingContentSchema)()
110 },
111 h2: {
112 children: (0, _blocks.getPhrasingContentSchema)()
113 },
114 h3: {
115 children: (0, _blocks.getPhrasingContentSchema)()
116 },
117 h4: {
118 children: (0, _blocks.getPhrasingContentSchema)()
119 },
120 h5: {
121 children: (0, _blocks.getPhrasingContentSchema)()
122 },
123 h6: {
124 children: (0, _blocks.getPhrasingContentSchema)()
125 }
126 },
127 transform: function transform(node) {
128 return (0, _blocks.createBlock)('core/heading', (0, _objectSpread2.default)({}, (0, _blocks.getBlockAttributes)('core/heading', node.outerHTML), {
129 level: getLevelFromHeadingNodeName(node.nodeName)
130 }));
131 }
132 }].concat((0, _toConsumableArray2.default)([2, 3, 4, 5, 6].map(function (level) {
133 return {
134 type: 'prefix',
135 prefix: Array(level + 1).join('#'),
136 transform: function transform(content) {
137 return (0, _blocks.createBlock)('core/heading', {
138 level: level,
139 content: content
140 });
141 }
142 };
143 }))),
144 to: [{
145 type: 'block',
146 blocks: ['core/paragraph'],
147 transform: function transform(_ref2) {
148 var content = _ref2.content;
149 return (0, _blocks.createBlock)('core/paragraph', {
150 content: content
151 });
152 }
153 }]
154 },
155 deprecated: [{
156 supports: supports,
157 attributes: (0, _objectSpread2.default)({}, (0, _lodash.omit)(schema, ['level']), {
158 nodeName: {
159 type: 'string',
160 source: 'property',
161 selector: 'h1,h2,h3,h4,h5,h6',
162 property: 'nodeName',
163 default: 'H2'
164 }
165 }),
166 migrate: function migrate(attributes) {
167 var nodeName = attributes.nodeName,
168 migratedAttributes = (0, _objectWithoutProperties2.default)(attributes, ["nodeName"]);
169 return (0, _objectSpread2.default)({}, migratedAttributes, {
170 level: getLevelFromHeadingNodeName(nodeName)
171 });
172 },
173 save: function save(_ref3) {
174 var attributes = _ref3.attributes;
175 var align = attributes.align,
176 nodeName = attributes.nodeName,
177 content = attributes.content;
178 return (0, _element.createElement)(_blockEditor.RichText.Content, {
179 tagName: nodeName.toLowerCase(),
180 style: {
181 textAlign: align
182 },
183 value: content
184 });
185 }
186 }],
187 merge: function merge(attributes, attributesToMerge) {
188 return {
189 content: (attributes.content || '') + (attributesToMerge.content || '')
190 };
191 },
192 edit: _edit.default,
193 save: function save(_ref4) {
194 var attributes = _ref4.attributes;
195 var align = attributes.align,
196 level = attributes.level,
197 content = attributes.content;
198 var tagName = 'h' + level;
199 return (0, _element.createElement)(_blockEditor.RichText.Content, {
200 tagName: tagName,
201 style: {
202 textAlign: align
203 },
204 value: content
205 });
206 }
207};
208exports.settings = settings;
209//# sourceMappingURL=index.js.map
\No newline at end of file