@gechiui/block-editor
Version:
288 lines (252 loc) • 9.26 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.OPTION_TAKE_VIDEO = exports.OPTION_TAKE_PHOTO_OR_VIDEO = exports.OPTION_TAKE_PHOTO = exports.OPTION_INSERT_FROM_URL = exports.MediaUpload = exports.MEDIA_TYPE_VIDEO = exports.MEDIA_TYPE_IMAGE = exports.MEDIA_TYPE_AUDIO = exports.MEDIA_TYPE_ANY = void 0;
var _element = require("@gechiui/element");
var _reactNative = require("react-native");
var _lodash = require("lodash");
var _reactNativePromptAndroid = _interopRequireDefault(require("react-native-prompt-android"));
var _i18n = require("@gechiui/i18n");
var _components = require("@gechiui/components");
var _reactNativeBridge = require("@gechiui/react-native-bridge");
var _icons = require("@gechiui/icons");
var _blockEditor = require("@gechiui/block-editor");
var _compose = require("@gechiui/compose");
var _data = require("@gechiui/data");
/**
* External dependencies
*/
/**
* GeChiUI dependencies
*/
const MEDIA_TYPE_IMAGE = 'image';
exports.MEDIA_TYPE_IMAGE = MEDIA_TYPE_IMAGE;
const MEDIA_TYPE_VIDEO = 'video';
exports.MEDIA_TYPE_VIDEO = MEDIA_TYPE_VIDEO;
const MEDIA_TYPE_AUDIO = 'audio';
exports.MEDIA_TYPE_AUDIO = MEDIA_TYPE_AUDIO;
const MEDIA_TYPE_ANY = 'any';
exports.MEDIA_TYPE_ANY = MEDIA_TYPE_ANY;
const OPTION_TAKE_VIDEO = (0, _i18n.__)('Take a Video');
exports.OPTION_TAKE_VIDEO = OPTION_TAKE_VIDEO;
const OPTION_TAKE_PHOTO = (0, _i18n.__)('Take a Photo');
exports.OPTION_TAKE_PHOTO = OPTION_TAKE_PHOTO;
const OPTION_TAKE_PHOTO_OR_VIDEO = (0, _i18n.__)('Take a Photo or Video');
exports.OPTION_TAKE_PHOTO_OR_VIDEO = OPTION_TAKE_PHOTO_OR_VIDEO;
const OPTION_INSERT_FROM_URL = (0, _i18n.__)('从URL插入');
exports.OPTION_INSERT_FROM_URL = OPTION_INSERT_FROM_URL;
const URL_MEDIA_SOURCE = 'URL';
const PICKER_OPENING_DELAY = 200;
class MediaUpload extends _element.Component {
constructor(props) {
super(props);
this.onPickerPresent = this.onPickerPresent.bind(this);
this.onPickerSelect = this.onPickerSelect.bind(this);
this.getAllSources = this.getAllSources.bind(this);
this.state = {
otherMediaOptions: []
};
}
componentDidMount() {
const {
allowedTypes = [],
autoOpen
} = this.props;
(0, _reactNativeBridge.getOtherMediaOptions)(allowedTypes, otherMediaOptions => {
const otherMediaOptionsWithIcons = otherMediaOptions.map(option => {
return { ...option,
requiresModal: true,
types: allowedTypes,
id: option.value
};
});
this.setState({
otherMediaOptions: otherMediaOptionsWithIcons
});
});
if (autoOpen) {
this.onPickerPresent();
}
}
getAllSources() {
const cameraImageSource = {
id: _reactNativeBridge.mediaSources.deviceCamera,
// ID is the value sent to native
value: _reactNativeBridge.mediaSources.deviceCamera + '-IMAGE',
// This is needed to diferenciate image-camera from video-camera sources.
label: (0, _i18n.__)('Take a Photo'),
requiresModal: true,
types: [MEDIA_TYPE_IMAGE],
icon: _icons.capturePhoto
};
const cameraVideoSource = {
id: _reactNativeBridge.mediaSources.deviceCamera,
value: _reactNativeBridge.mediaSources.deviceCamera,
label: (0, _i18n.__)('Take a Video'),
requiresModal: true,
types: [MEDIA_TYPE_VIDEO],
icon: _icons.captureVideo
};
const deviceLibrarySource = {
id: _reactNativeBridge.mediaSources.deviceLibrary,
value: _reactNativeBridge.mediaSources.deviceLibrary,
label: (0, _i18n.__)('Choose from device'),
requiresModal: true,
types: [MEDIA_TYPE_IMAGE, MEDIA_TYPE_VIDEO],
icon: _icons.image
};
const siteLibrarySource = {
id: _reactNativeBridge.mediaSources.siteMediaLibrary,
value: _reactNativeBridge.mediaSources.siteMediaLibrary,
label: (0, _i18n.__)('GeChiUI Media Library'),
requiresModal: true,
types: [MEDIA_TYPE_IMAGE, MEDIA_TYPE_VIDEO, MEDIA_TYPE_AUDIO, MEDIA_TYPE_ANY],
icon: _icons.gechiui,
mediaLibrary: true
};
const urlSource = {
id: URL_MEDIA_SOURCE,
value: URL_MEDIA_SOURCE,
label: (0, _i18n.__)('从URL插入'),
types: [MEDIA_TYPE_AUDIO],
icon: _icons.globe
};
const internalSources = [deviceLibrarySource, cameraImageSource, cameraVideoSource, siteLibrarySource, urlSource];
return internalSources.concat(this.state.otherMediaOptions);
}
getMediaOptionsItems() {
const {
allowedTypes = [],
__experimentalOnlyMediaLibrary,
isAudioBlockMediaUploadEnabled
} = this.props;
return this.getAllSources().filter(source => {
if (__experimentalOnlyMediaLibrary) {
return source.mediaLibrary;
} else if (allowedTypes.every(allowedType => allowedType === MEDIA_TYPE_AUDIO && source.types.includes(allowedType)) && source.id !== URL_MEDIA_SOURCE) {
return isAudioBlockMediaUploadEnabled === true;
}
return allowedTypes.some(allowedType => source.types.includes(allowedType));
}).map(source => {
return { ...source,
icon: source.icon || this.getChooseFromDeviceIcon()
};
});
}
getChooseFromDeviceIcon() {
return _icons.mobile;
}
onPickerPresent() {
const {
autoOpen
} = this.props;
const isIOS = _reactNative.Platform.OS === 'ios';
if (this.picker) {
// the delay below is required because on iOS this action sheet gets dismissed by the close event of the Inserter
// so this delay allows the Inserter to be closed fully before presenting action sheet.
if (autoOpen && isIOS) {
(0, _lodash.delay)(() => this.picker.presentPicker(), PICKER_OPENING_DELAY);
} else {
this.picker.presentPicker();
}
}
}
onPickerSelect(value) {
const {
allowedTypes = [],
onSelect,
onSelectURL,
multiple = false
} = this.props;
if (value === URL_MEDIA_SOURCE) {
(0, _reactNativePromptAndroid.default)((0, _i18n.__)('Type a URL'), // title
undefined, // message
[{
text: (0, _i18n.__)('取消'),
style: 'cancel'
}, {
text: (0, _i18n.__)('应用'),
onPress: onSelectURL
}], // buttons
'plain-text', // type
undefined, // defaultValue
'url' // keyboardType
);
return;
}
const mediaSource = this.getAllSources().filter(source => source.value === value).shift();
const types = allowedTypes.filter(type => mediaSource.types.includes(type));
(0, _reactNativeBridge.requestMediaPicker)(mediaSource.id, types, multiple, media => {
if (multiple && media || media && media.id) {
onSelect(media);
}
});
}
render() {
const {
allowedTypes = [],
isReplacingMedia,
multiple
} = this.props;
const isOneType = allowedTypes.length === 1;
const isImage = isOneType && allowedTypes.includes(MEDIA_TYPE_IMAGE);
const isVideo = isOneType && allowedTypes.includes(MEDIA_TYPE_VIDEO);
const isAudio = isOneType && allowedTypes.includes(MEDIA_TYPE_AUDIO);
const isAnyType = isOneType && allowedTypes.includes(MEDIA_TYPE_ANY);
const isImageOrVideo = allowedTypes.length === 2 && allowedTypes.includes(MEDIA_TYPE_IMAGE) && allowedTypes.includes(MEDIA_TYPE_VIDEO);
let pickerTitle;
if (isImage) {
if (isReplacingMedia) {
pickerTitle = (0, _i18n.__)('更换图片');
} else {
pickerTitle = multiple ? (0, _i18n.__)('Choose images') : (0, _i18n.__)('选择图片');
}
} else if (isVideo) {
if (isReplacingMedia) {
pickerTitle = (0, _i18n.__)('替换视频');
} else {
pickerTitle = (0, _i18n.__)('选择视频');
}
} else if (isImageOrVideo) {
if (isReplacingMedia) {
pickerTitle = (0, _i18n.__)('Replace image or video');
} else {
pickerTitle = (0, _i18n.__)('Choose image or video');
}
} else if (isAudio) {
if (isReplacingMedia) {
pickerTitle = (0, _i18n.__)('替换音频');
} else {
pickerTitle = (0, _i18n.__)('选择音频');
}
} else if (isAnyType) {
pickerTitle = (0, _i18n.__)('选择文件');
if (isReplacingMedia) {
pickerTitle = (0, _i18n.__)('Replace file');
} else {
pickerTitle = (0, _i18n.__)('选择文件');
}
}
const getMediaOptions = () => (0, _element.createElement)(_components.Picker, {
title: pickerTitle,
hideCancelButton: true,
ref: instance => this.picker = instance,
options: this.getMediaOptionsItems(),
onChange: this.onPickerSelect
});
return this.props.render({
open: this.onPickerPresent,
getMediaOptions
});
}
}
exports.MediaUpload = MediaUpload;
var _default = (0, _compose.compose)([(0, _data.withSelect)(select => {
return {
isAudioBlockMediaUploadEnabled: select(_blockEditor.store).getSettings('capabilities').isAudioBlockMediaUploadEnabled === true
};
})])(MediaUpload);
exports.default = _default;
//# sourceMappingURL=index.native.js.map