UNPKG

react-filerobot-image-editor

Version:

React component version of filerobot image editor (FIE).

1 lines 5.65 kB
import _toConsumableArray from"@babel/runtime/helpers/toConsumableArray";import _defineProperty from"@babel/runtime/helpers/defineProperty";import _objectWithoutProperties from"@babel/runtime/helpers/objectWithoutProperties";var _excluded=["textContent"];function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){_defineProperty(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}import{SET_SELECTED_TEXT_PART}from"../actions";import emitCustomEvent from"../utils/emitCustomEvent";import{EVENTS}from"../utils/constants";import useStore from"./useStore";import useEditableTextId from"./useEditableTextId";import useSetAnnotation from"./useSetAnnotation";var useTextAnnotationPartEditing=function(){var a=useStore(),b=a.dispatch,c=a.annotations,d=a.selectedTextPart,e=useEditableTextId(),f=useSetAnnotation();return{selectedTextPart:d,setCurrentSelectedText:function(a){b({type:SET_SELECTED_TEXT_PART,payload:a})},editableTextId:e,updateAnnotationTextSlice:function(a){var b=a.annotationId,d=a.searchValue,g=a.replaceValue,h=a.emitUpdateEvent,i=c[b]||{},j=i.defaultText||i.text;if(i){var k,l=Array.isArray(j)?j:[{textContent:j}];l=l.map(function(a){var b,c=a.textContent,e=_objectWithoutProperties(a,_excluded);if(!c)return e;var f=e.startIndex,h=e.endIndex,i=c.replace(d,g),j="undefined"==typeof f?void 0:null!==(b=k)&&void 0!==b?b:f;return k="undefined"==typeof j?h:j+i.length,_objectSpread(_objectSpread(_objectSpread({},e),"undefined"!=typeof j&&{startIndex:j,endIndex:k}),{},{textContent:i})}),f({id:b,text:l,tmpText:void 0}),(!(void 0!==h)||h)&&emitCustomEvent(EVENTS.TEXT_CONTENT_EDITED,{id:e,textContent:l,annotation:_objectSpread(_objectSpread({},i),{},{text:l})})}},updateAnnotationTextSlices:function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:[],d=2<arguments.length&&arguments[2]!==void 0?arguments[2]:{},g=d.annotationTextProperty,h=void 0===g?"defaultText":g,i=d.emitUpdateEvent,j=d.dismissHistory,k=void 0!==j&&j,l=c[a]||{},m=l[h]||l.text;if(l&&Array.isArray(b)&&0!==b.length){var n,o=Array.isArray(m)?m:[{textContent:m}];o=o.map(function(a){var c,d=_objectSpread({},a),e=d.textContent||"";b.forEach(function(a){var b,c=a.searchValue,d=a.replaceValue;e=null===(b=e)||void 0===b?void 0:b.replace(c,d)});var f="undefined"==typeof d.startIndex?void 0:null!==(c=n)&&void 0!==c?c:d.startIndex;return n="undefined"==typeof f?d.endIndex:f+e.length,_objectSpread(_objectSpread({},d),{},{textContent:e},"undefined"!=typeof f&&{startIndex:f,endIndex:n})}),f({id:a,text:o,tmpText:void 0,dismissHistory:k}),(!(void 0!==i)||i)&&emitCustomEvent(EVENTS.TEXT_CONTENT_EDITED,{id:e,textContent:o,annotation:_objectSpread(_objectSpread({},l),{},{text:o}),dismissHistory:k})}},updateAnnotationTextSliceUsingIndices:function(a){var b=a.annotationId,d=a.startIndex,g=a.endIndex,h=a.newTextContent,i=a.emitUpdateEvent,j=c[b]||{},k=j.defaultText||j.text;if(j){var l,m=Array.isArray(k)?k:[{textContent:k}];m=m.map(function(a){var b=a.startIndex,c=a.endIndex,e=null!==d&&void 0!==d?d:0,f=a.textContent||k||"",i=null!==g&&void 0!==g?g:f.length,j=null!==b&&void 0!==b?b:0,m=null!==c&&void 0!==c?c:f.length;if(e>=j&&i<=m){var n,o=f.slice(0,e-j)+h+f.slice(i-m||f.length),p="undefined"==typeof b?void 0:null!==(n=l)&&void 0!==n?n:b;return l="undefined"==typeof p?c:p+o.length,_objectSpread(_objectSpread(_objectSpread({},a),"undefined"!=typeof p&&{startIndex:p,endIndex:l}),{},{textContent:o})}return a}),f({id:b,text:m,tmpText:void 0}),(!(void 0!==i)||i)&&emitCustomEvent(EVENTS.TEXT_CONTENT_EDITED,{id:e,textContent:m,annotation:_objectSpread(_objectSpread({},j),{},{text:m})})}},updateAnnotationTextSlicesUsingIndices:function(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:[],d=2<arguments.length&&arguments[2]!==void 0?arguments[2]:{},g=d.annotationTextProperty,h=void 0===g?"defaultText":g,i=d.emitUpdateEvent,j=d.dismissHistory,k=void 0!==j&&j,l=c[a]||{},m=l[h]||l.text;if(l&&Array.isArray(b)&&0!==b.length){var n,o=Array.isArray(m)?m:[{textContent:m}];o=o.map(function(a){var c,d,e,f=a,g=0;_toConsumableArray(b).sort(function(c,a){var b,d;return(null!==(b=c.startIndex)&&void 0!==b?b:0)-(null!==(d=a.startIndex)&&void 0!==d?d:0)}).forEach(function(a){var b,c,d=a.startIndex,e=a.endIndex,h=a.newTextContent,i=f.textContent||m||"",j=null!==(b=f.startIndex)&&void 0!==b?b:0,k=null!==(c=f.endIndex)&&void 0!==c?c:i.length,l=null!==d&&void 0!==d?d:0,n=null!==e&&void 0!==e?e:i.length;if(l>=j&&n<=k){var o,p,q=i.slice(0,l-j+g)+h+i.slice(n-k||i.length);g+=null!==(o=q.length-(null!==(p=i.length)&&void 0!==p?p:0))&&void 0!==o?o:0,f=_objectSpread(_objectSpread({},f),{},{textContent:q})}});var h="undefined"==typeof f.startIndex?void 0:null!==(c=n)&&void 0!==c?c:f.startIndex;return n="undefined"==typeof h?f.endIndex:h+(null!==(d=null===(e=f.textContent)||void 0===e?void 0:e.length)&&void 0!==d?d:0),_objectSpread(_objectSpread({},f),"undefined"!=typeof h&&{startIndex:h,endIndex:n})}),f({id:a,text:o,tmpText:void 0,dismissHistory:k}),(!(void 0!==i)||i)&&emitCustomEvent(EVENTS.TEXT_CONTENT_EDITED,{id:e,textContent:o,annotation:_objectSpread(_objectSpread({},l),{},{text:o}),dismissHistory:k})}}}};export default useTextAnnotationPartEditing;