UNPKG

@nocobase/plugin-text-copy

Version:

Add copy button to text fields

10 lines (9 loc) 8.36 kB
/** * This file is part of the NocoBase (R) project. * Copyright (c) 2020-2024 NocoBase Co., Ltd. * Authors: NocoBase Team. * * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. * For more information, please refer to: https://www.nocobase.com/agreement. */ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react"),require("@formily/react"),require("@nocobase/client"),require("lodash"),require("antd"),require("react-i18next")):"function"==typeof define&&define.amd?define("@nocobase/plugin-text-copy",["react","@formily/react","@nocobase/client","lodash","antd","react-i18next"],t):"object"==typeof exports?exports["@nocobase/plugin-text-copy"]=t(require("react"),require("@formily/react"),require("@nocobase/client"),require("lodash"),require("antd"),require("react-i18next")):e["@nocobase/plugin-text-copy"]=t(e.react,e["@formily/react"],e["@nocobase/client"],e.lodash,e.antd,e["react-i18next"])}(self,function(e,t,n,r,o,u){return function(){"use strict";var i={505:function(e){e.exports=t},772:function(e){e.exports=n},721:function(e){e.exports=o},467:function(e){e.exports=r},156:function(t){t.exports=e},238:function(e){e.exports=u}},a={};function c(e){var t=a[e];if(void 0!==t)return t.exports;var n=a[e]={exports:{}};return i[e](n,n.exports,c),n.exports}c.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return c.d(t,{a:t}),t},c.d=function(e,t){for(var n in t)c.o(t,n)&&!c.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},c.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},c.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var l={};return!function(){c.r(l);var e=c(505),t=c(772),n=c(721),r=c(467),o=c.n(r),u=c(156),i=c.n(u),a=c(238);function f(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function p(e,t,n,r,o,u,i){try{var a=e[u](i),c=a.value}catch(e){n(e);return}a.done?t(c):Promise.resolve(c).then(r,o)}function s(e){return function(){var t=this,n=arguments;return new Promise(function(r,o){var u=e.apply(t,n);function i(e){p(u,r,o,i,a,"next",e)}function a(e){p(u,r,o,i,a,"throw",e)}i(void 0)})}}function y(e,t,n){return(y=m()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&b(o,n.prototype),o}).apply(null,arguments)}function d(e){return(d=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function b(e,t){return(b=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function v(e){var t="function"==typeof Map?new Map:void 0;return(v=function(e){if(null===e||-1===Function.toString.call(e).indexOf("[native code]"))return e;if("function"!=typeof e)throw TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,n)}function n(){return y(e,arguments,d(this).constructor)}return n.prototype=Object.create(e.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),b(n,e)})(e)}function m(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(m=function(){return!!e})()}function h(e,t){var n,r,o,u,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return u={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function a(u){return function(a){var c=[u,a];if(n)throw TypeError("Generator is already executing.");for(;i;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,r=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(o=(o=i.trys).length>0&&o[o.length-1])&&(6===c[0]||2===c[0])){i=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){i.label=c[1];break}if(6===c[0]&&i.label<o[1]){i.label=o[1],o=c;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(c);break}o[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}catch(e){c=[6,e],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}}}var x=(0,e.observer)(function(){var r,o=(0,e.useField)(),a=(0,t.useToken)().token,c=(0,u.useRef)(null),l=(r=i().useState(!1),function(e){if(Array.isArray(e))return e}(r)||function(e,t){var n,r,o=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=o){var u=[],i=!0,a=!1;try{for(o=o.call(e);!(i=(n=o.next()).done)&&(u.push(n.value),u.length!==t);i=!0);}catch(e){a=!0,r=e}finally{try{i||null==o.return||o.return()}finally{if(a)throw r}}return u}}(r,2)||function(e,t){if(e){if("string"==typeof e)return f(e,2);var n=Object.prototype.toString.call(e).slice(8,-1);if("Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n)return Array.from(n);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return f(e,t)}}(r,2)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),p=l[0],s=l[1];(0,u.useEffect)(function(){if(o.value&&o.readPretty&&c.current){var e,t,n=c.current,r=function(e){s(!0)},u=function(e){s(!1)};return null==n||null==(e=n.parentElement)||e.addEventListener("mouseover",r),null==n||null==(t=n.parentElement)||t.addEventListener("mouseout",u),function(){var e,t;null==n||null==(e=n.parentElement)||e.removeEventListener("mouseover",r),null==n||null==(t=n.parentElement)||t.removeEventListener("mouseout",u)}}},[o.readPretty,o.value]);var y=o.readPretty&&(!o.value||!p);return i().createElement(n.Typography.Text,{ref:c,copyable:{text:o.value},style:{marginLeft:o.readPretty?a.marginXXS:0,opacity:+!y}})},{displayName:"TextCopyButton"});l.default=function(n){var r;if("function"!=typeof n&&null!==n)throw TypeError("Super expression must either be null or a function");function u(){var e,t;if(!(this instanceof u))throw TypeError("Cannot call a class as a function");return e=u,t=arguments,e=d(e),function(e,t){var n;if(t&&("object"==((n=t)&&"undefined"!=typeof Symbol&&n.constructor===Symbol?"symbol":typeof n)||"function"==typeof t))return t;if(void 0===e)throw ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(this,m()?Reflect.construct(e,t||[],d(this).constructor):e.apply(this,t))}return u.prototype=Object.create(n&&n.prototype,{constructor:{value:u,writable:!0,configurable:!0}}),n&&b(u,n),r=[{key:"load",value:function(){var n=this;return s(function(){var r;return h(this,function(u){return r=i().createElement(x,null),n.app.addScopes({TextCopyButton:r}),n.app.schemaSettingsManager.addItem("fieldSettings:component:Input","enableCopier",{type:"switch",useComponentProps:function(){var n,u,i=(0,a.useTranslation)("text-copy").t,c=(0,t.useColumnSchema)().fieldSchema,l=(0,e.useFieldSchema)(),f=(0,e.useField)(),p=(0,t.useDesignable)().dn,y=c||l;return{title:i("Display copy button"),checked:!!(null==(u=y["x-component-props"])?void 0:u.addonAfter),onChange:(n=s(function(e){return h(this,function(t){switch(t.label){case 0:return e?(f.componentProps.addonAfter=r,o().set(y,"x-component-props.addonAfter","{{TextCopyButton}}")):(f.componentProps.addonAfter=null,o().unset(y,"x-component-props.addonAfter")),[4,p.emit("patch",{schema:{"x-uid":y["x-uid"],"x-component-props":function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){var r;r=n[t],t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r})}return e}({},y["x-component-props"])}})];case 1:return t.sent(),[2]}})}),function(e){return n.apply(this,arguments)})}}}),[2]})})()}}],function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(u.prototype,r),u}(v(t.Plugin))}(),l}()});