react-img-editor-en
Version:
Image Annotation Tool for React
119 lines • 5.89 kB
JavaScript
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
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 _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
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."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import PluginFactory from "./plugins/PluginFactory";
import Palette from "./components/Palette";
import React, { useEffect, useState } from "react";
import Toolbar from "./components/Toolbar";
import { EditorContext } from "./components/EditorContext";
export default function ReactImageEditor(props) {
var _useState = useState(null),
_useState2 = _slicedToArray(_useState, 2),
imageObj = _useState2[0],
setImageObj = _useState2[1];
var pluginFactory = new PluginFactory();
var plugins = [].concat(_toConsumableArray(pluginFactory.plugins), _toConsumableArray(props.plugins));
var defaultPlugin = null;
var defaultParamValue = {};
for (var i = 0; i < plugins.length; i++) {
if (props.defaultPluginName && props.toolbar && plugins[i].name === props.defaultPluginName) {
defaultPlugin = plugins[i];
if (defaultPlugin.defaultParamValue) {
defaultParamValue = defaultPlugin.defaultParamValue;
}
break;
}
}
var _useState3 = useState(defaultPlugin),
_useState4 = _slicedToArray(_useState3, 2),
currentPlugin = _useState4[0],
setCurrentPlugin = _useState4[1];
var _useState5 = useState(defaultParamValue),
_useState6 = _slicedToArray(_useState5, 2),
paramValue = _useState6[0],
setParamValue = _useState6[1];
// 生成默认 toolbarItemConfig
var config = {};
plugins.map(function (plugin) {
if (plugin.name === "repeal") {
config[plugin.name] = {
disable: true
};
} else {
config[plugin.name] = {
disable: false
};
}
});
var _useState7 = useState(config),
_useState8 = _slicedToArray(_useState7, 2),
toolbarItemConfig = _useState8[0],
setToolbarItemConfig = _useState8[1];
useEffect(function () {
var image = new Image();
image.onload = function () {
setImageObj(image);
};
if (props.crossOrigin !== undefined) {
image.crossOrigin = props.crossOrigin;
}
image.src = props.src;
}, [props.src, props.crossOrigin]);
function handlePluginChange(plugin) {
setCurrentPlugin(plugin);
plugin.defaultParamValue && setParamValue(plugin.defaultParamValue);
if (plugin.disappearImmediately === true) {
setTimeout(function () {
setCurrentPlugin(null);
});
}
}
function handlePluginParamValueChange(value) {
setParamValue(value);
}
function updateToolbarItemConfig(config) {
setToolbarItemConfig(config);
}
var style = _extends({
width: props.width + "px",
height: props.height + "px"
}, props.style);
return React.createElement(EditorContext.Provider, {
value: {
containerWidth: props.width,
containerHeight: props.height,
plugins: plugins,
toolbar: props.toolbar,
currentPlugin: currentPlugin,
paramValue: paramValue,
handlePluginChange: handlePluginChange,
handlePluginParamValueChange: handlePluginParamValueChange,
toolbarItemConfig: toolbarItemConfig,
updateToolbarItemConfig: updateToolbarItemConfig
}
}, React.createElement("div", {
className: "react-img-editor-en",
style: style
}, imageObj ? React.createElement(React.Fragment, null, React.createElement(Palette, {
height: props.height - 42,
imageObj: imageObj,
getStage: props.getStage
}), React.createElement(Toolbar, null)) : null));
}
ReactImageEditor.defaultProps = {
width: 700,
height: 500,
style: {},
plugins: [],
toolbar: {
items: ["pen", "eraser", "arrow", "rect", "circle", "mosaic", "text", "|", "repeal", "download", "crop"]
}
};