UNPKG

@wordpress/components

Version:
128 lines (106 loc) 3.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.MediaEdit = exports.MEDIA_EDITOR = exports.MEDIA_TYPE_IMAGE = void 0; var _element = require("@wordpress/element"); var _lodash = require("lodash"); var _i18n = require("@wordpress/i18n"); var _components = require("@wordpress/components"); var _reactNativeBridge = require("@wordpress/react-native-bridge"); /** * External dependencies */ /** * WordPress dependencies */ const MEDIA_TYPE_IMAGE = 'image'; exports.MEDIA_TYPE_IMAGE = MEDIA_TYPE_IMAGE; const MEDIA_EDITOR = 'MEDIA_EDITOR'; exports.MEDIA_EDITOR = MEDIA_EDITOR; const editOption = { id: MEDIA_EDITOR, value: MEDIA_EDITOR, label: (0, _i18n.__)('Edit'), requiresModal: true, types: [MEDIA_TYPE_IMAGE] }; const replaceOption = { id: _reactNativeBridge.mediaSources.deviceLibrary, value: _reactNativeBridge.mediaSources.deviceLibrary, label: (0, _i18n.__)('Replace'), types: [MEDIA_TYPE_IMAGE] }; class MediaEdit extends _element.Component { constructor(props) { super(props); this.onPickerPresent = this.onPickerPresent.bind(this); this.onPickerSelect = this.onPickerSelect.bind(this); this.getMediaOptionsItems = this.getMediaOptionsItems.bind(this); this.getDestructiveButtonIndex = this.getDestructiveButtonIndex.bind(this); } getMediaOptionsItems() { const { pickerOptions, openReplaceMediaOptions, source } = this.props; return (0, _lodash.compact)([(source === null || source === void 0 ? void 0 : source.uri) && editOption, openReplaceMediaOptions && replaceOption, ...(pickerOptions ? pickerOptions : [])]); } getDestructiveButtonIndex() { const options = this.getMediaOptionsItems(); const destructiveButtonIndex = options.findIndex(option => option === null || option === void 0 ? void 0 : option.destructiveButton); return destructiveButtonIndex !== -1 ? destructiveButtonIndex + 1 : undefined; } onPickerPresent() { if (this.picker) { this.picker.presentPicker(); } } onPickerSelect(value) { const { onSelect, pickerOptions, multiple = false, openReplaceMediaOptions } = this.props; switch (value) { case MEDIA_EDITOR: (0, _reactNativeBridge.requestMediaEditor)(this.props.source.uri, media => { if (multiple && media || media && media.id) { onSelect(media); } }); break; default: const optionSelected = pickerOptions && pickerOptions.find(option => option.id === value); if (optionSelected && optionSelected.onPress) { optionSelected.onPress(); return; } if (openReplaceMediaOptions) { openReplaceMediaOptions(); } } } render() { const mediaOptions = () => (0, _element.createElement)(_components.Picker, { hideCancelButton: true, ref: instance => this.picker = instance, options: this.getMediaOptionsItems(), leftAlign: true, onChange: this.onPickerSelect // translators: %s: block title e.g: "Paragraph". , title: (0, _i18n.__)('Media options'), destructiveButtonIndex: this.getDestructiveButtonIndex() }); return this.props.render({ open: this.onPickerPresent, mediaOptions }); } } exports.MediaEdit = MediaEdit; var _default = MediaEdit; exports.default = _default; //# sourceMappingURL=index.native.js.map