@kgdata/annotation
Version:
annotation
196 lines (162 loc) • 7.17 kB
JavaScript
;
var React = require('react');
var poplarAnnotation = require('poplar-annotation');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for (_i = _i.call(arr); !(_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 _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 styleInject(css, ref) {
if ( ref === void 0 ) ref = {};
var insertAt = ref.insertAt;
if (!css || typeof document === 'undefined') { return; }
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild);
} else {
head.appendChild(style);
}
} else {
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
var css_248z = "svg {\n width: 100%;\n}\n#annotation-container .poplar-annotation-connection-line {\n stroke: rgba(0, 0, 0, 0.65);\n}\n#annotation-container .poplar-annotation-connection-line.hover {\n stroke: #4a90e2;\n}\n.poplar-annotation-content,\n.poplar-annotation-label,\n.poplar-annotation-connection {\n font-family: 'PingFang SC', serif;\n}\n.poplar-annotation-content {\n font-size: 16px;\n}\n.poplar-annotation-label {\n font-size: 12px;\n}\n.poplar-annotation-connection {\n font-size: 12px;\n}\n";
styleInject(css_248z);
var Annotation = function Annotation(props) {
var annotationData = props.annotationData,
action = props.action,
onTextSelected = props.onTextSelected,
onLabelDoubleClicked = props.onLabelDoubleClicked,
onLabelRightClicked = props.onLabelRightClicked,
onTwoLabelsClicked = props.onTwoLabelsClicked,
onConnectionDoubleClicked = props.onConnectionDoubleClicked,
onConnectionRightClicked = props.onConnectionRightClicked,
onAnnotatorUpdate = props.onAnnotatorUpdate;
var annotator = React.useRef(null);
var _useState = React.useState(null),
_useState2 = _slicedToArray(_useState, 2),
store = _useState2[0],
setStore = _useState2[1];
var actionEvent = React.useCallback(function () {
if (annotator.current) {
annotator.current.on('textSelected', function (startIndex, endIndex) {
// 输出用户选取的索引
if (onTextSelected) onTextSelected(startIndex, endIndex);
});
annotator.current.on('labelDoubleClicked', function (id, e) {
// 输出用户双击的label的ID, 点击时鼠标的event
if (onLabelDoubleClicked) onLabelDoubleClicked(id, e);
});
annotator.current.on('labelRightClicked', function (id, e) {
// 输出用户点击的label的ID, 点击时鼠标的event
if (onLabelRightClicked) onLabelRightClicked(id, e);
});
annotator.current.on('twoLabelsClicked', function (first, second) {
// 输出用户选取的两个label的ID
if (onTwoLabelsClicked) onTwoLabelsClicked(first, second);
});
annotator.current.on('connectionDoubleClicked', function (id, e) {
// 输出用户点击的Connection的ID, 点击鼠标的event
if (onConnectionDoubleClicked) onConnectionDoubleClicked(id, e);
});
annotator.current.on('connectionRightClicked', function (id, e) {
// 输出用户点击的Connection的ID, 点击鼠标的event
if (onConnectionRightClicked) onConnectionRightClicked(id, e);
});
}
}, [onTextSelected, onLabelDoubleClicked, onLabelRightClicked, onTwoLabelsClicked, onConnectionDoubleClicked, onConnectionRightClicked]);
React.useEffect(function () {
if (annotationData && !annotator.current) {
annotator.current = new poplarAnnotation.Annotator(annotationData, document.getElementById('annotation-container'), {
contentEditable: false
});
actionEvent();
}
}, [actionEvent, annotationData]);
React.useEffect(function () {
if (action === null || action === void 0 ? void 0 : action.type) {
var _action$type = action.type,
type = _action$type === void 0 ? '' : _action$type,
data = action.data;
switch (type) {
case 'LABELCREATE':
annotator.current.applyAction(poplarAnnotation.Action.Label.Create(data.categoryId, data.startIndex, data.endIndex));
break;
case 'LABELUPDATE':
annotator.current.applyAction(poplarAnnotation.Action.Label.Update(data.labelId, data.categoryId));
break;
case 'LABELDELETE':
annotator.current.applyAction(poplarAnnotation.Action.Label.Delete(data.labelId));
break;
case 'CONNECTIONCREATE':
annotator.current.applyAction(poplarAnnotation.Action.Connection.Create(data.categoryId, data.fromId, data.toId));
break;
case 'CONNECTIONUPDATE':
annotator.current.applyAction(poplarAnnotation.Action.Connection.Update(data.connectionId, data.categoryId));
break;
case 'CONNECTIONDELETE':
annotator.current.applyAction(poplarAnnotation.Action.Connection.Delete(data.connectionId));
break;
}
setStore(annotator.current.store.json);
}
}, [action]);
React.useEffect(function () {
if (store && onAnnotatorUpdate) onAnnotatorUpdate(store);
}, [store, onAnnotatorUpdate]);
return /*#__PURE__*/React__default['default'].createElement("div", {
id: "annotation-container"
});
};
module.exports = Annotation;