UNPKG

@gitlab/ui

Version:
293 lines (239 loc) • 8.32 kB
import _isString from 'lodash/isString'; import _has from 'lodash/has'; import __vue_normalize__ from 'vue-runtime-helpers/dist/normalize-component.js'; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread2(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var PREFIX = '%{'; var SUFFIX = '}'; var START_SUFFIX = 'Start'; var END_SUFFIX = 'End'; var PLACE_HOLDER_REGEX = new RegExp("(".concat(PREFIX, "[a-z]+[\\w-]*[a-z0-9]+").concat(SUFFIX, ")"), 'gi'); function groupPlaceholdersByStartTag() { var placeholders = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return Object.entries(placeholders).reduce(function (acc, _ref) { var _ref2 = _slicedToArray(_ref, 2), slotName = _ref2[0], _ref2$ = _slicedToArray(_ref2[1], 2), startTag = _ref2$[0], endTag = _ref2$[1]; acc[startTag] = { slotName: slotName, endTag: endTag }; return acc; }, {}); } function getPlaceholderDefinition(chunk, placeholdersByStartTag) { var tagName = chunk.slice(PREFIX.length, -SUFFIX.length); if (_has(placeholdersByStartTag, tagName)) { // Use provided custom placeholder definition return _objectSpread2(_objectSpread2({}, placeholdersByStartTag[tagName]), {}, { tagName: tagName }); } if (tagName.endsWith(START_SUFFIX)) { // Tag conforms to default start/end tag naming convention var slotName = tagName.slice(0, -START_SUFFIX.length); return { slotName: slotName, endTag: "".concat(slotName).concat(END_SUFFIX), tagName: tagName }; } return { slotName: tagName, endTag: undefined, tagName: tagName }; } var script = { functional: true, props: { message: { type: String, required: true }, placeholders: { type: Object, required: false, default: undefined, validator: function validator(value) { return Object.values(value).every(function (tagPair) { return Array.isArray(tagPair) && tagPair.length === 2 && tagPair.every(_isString); }); } } }, /** * While a functional style is generally preferred, an imperative style is * used here, as it lends itself better to the message parsing algorithm. * This approach is also more performant, as it minimizes (relatively) object * creation/garbage collection, which is important given how frequently this * code may run on a given page. */ render: function render(createElement, context) { var i = 0; var vnodes = []; var slots = context.scopedSlots; var chunks = context.props.message.split(PLACE_HOLDER_REGEX); var placeholdersByStartTag = groupPlaceholdersByStartTag(context.props.placeholders); while (i < chunks.length) { var chunk = chunks[i]; // Skip past this chunk now we have it i += 1; if (!PLACE_HOLDER_REGEX.test(chunk)) { // Not a placeholder, so pass through as-is vnodes.push(chunk); continue; } var _getPlaceholderDefini = getPlaceholderDefinition(chunk, placeholdersByStartTag), slotName = _getPlaceholderDefini.slotName, endTag = _getPlaceholderDefini.endTag, tagName = _getPlaceholderDefini.tagName; if (endTag) { // Peek ahead to find end placeholder, if any var indexOfEnd = chunks.indexOf("".concat(PREFIX).concat(endTag).concat(SUFFIX), i); if (indexOfEnd > -1) { // We have a valid start/end placeholder pair! Extract the content // between them and skip past the end placeholder var content = chunks.slice(i, indexOfEnd); i = indexOfEnd + 1; if (!_has(slots, slotName)) { // Slot hasn't been provided; return placeholders and content as-is vnodes.push.apply(vnodes, [chunk].concat(_toConsumableArray(content), [chunks[indexOfEnd]])); continue; } // Provide content to provided scoped slot vnodes.push(slots[slotName]({ content: content.join('') })); continue; } } // By process of elimination, chunk must be a plain placeholder vnodes.push(_has(slots, tagName) ? slots[tagName]() : chunk); continue; } return vnodes; } }; /* script */ const __vue_script__ = script; /* template */ /* style */ const __vue_inject_styles__ = undefined; /* scoped */ const __vue_scope_id__ = undefined; /* module identifier */ const __vue_module_identifier__ = undefined; /* functional template */ const __vue_is_functional_template__ = undefined; /* style inject */ /* style inject SSR */ /* style inject shadow dom */ const __vue_component__ = __vue_normalize__( {}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, false, undefined, undefined, undefined ); export default __vue_component__;