UNPKG

@wordpress/editor

Version:
64 lines (58 loc) 2.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useExistingTemplateParts = exports.getUniqueTemplatePartTitle = exports.getCleanTemplatePartSlug = void 0; var _changeCase = require("change-case"); var _data = require("@wordpress/data"); var _coreData = require("@wordpress/core-data"); var _constants = require("../../store/constants"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ const useExistingTemplateParts = () => { return (0, _data.useSelect)(select => select(_coreData.store).getEntityRecords('postType', _constants.TEMPLATE_PART_POST_TYPE, { per_page: -1 }), []); }; /** * Return a unique template part title based on * the given title and existing template parts. * * @param {string} title The original template part title. * @param {Object} templateParts The array of template part entities. * @return {string} A unique template part title. */ exports.useExistingTemplateParts = useExistingTemplateParts; const getUniqueTemplatePartTitle = (title, templateParts) => { const lowercaseTitle = title.toLowerCase(); const existingTitles = templateParts.map(templatePart => templatePart.title.rendered.toLowerCase()); if (!existingTitles.includes(lowercaseTitle)) { return title; } let suffix = 2; while (existingTitles.includes(`${lowercaseTitle} ${suffix}`)) { suffix++; } return `${title} ${suffix}`; }; /** * Get a valid slug for a template part. * Currently template parts only allow latin chars. * The fallback slug will receive suffix by default. * * @param {string} title The template part title. * @return {string} A valid template part slug. */ exports.getUniqueTemplatePartTitle = getUniqueTemplatePartTitle; const getCleanTemplatePartSlug = title => { return (0, _changeCase.paramCase)(title).replace(/[^\w-]+/g, '') || 'wp-custom-part'; }; exports.getCleanTemplatePartSlug = getCleanTemplatePartSlug; //# sourceMappingURL=utils.js.map