@nocobase/plugin-text-copy
Version:
Add copy button to text fields
10 lines (9 loc) • 12.2 kB
JavaScript
/**
* 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-i18next"),require("@nocobase/client-v2"),require("react"),require("antd"),require("@nocobase/client"),require("@formily/react"),require("@nocobase/flow-engine"),require("lodash")):"function"==typeof define&&define.amd?define("@nocobase/plugin-text-copy",["react-i18next","@nocobase/client-v2","react","antd","@nocobase/client","@formily/react","@nocobase/flow-engine","lodash"],t):"object"==typeof exports?exports["@nocobase/plugin-text-copy"]=t(require("react-i18next"),require("@nocobase/client-v2"),require("react"),require("antd"),require("@nocobase/client"),require("@formily/react"),require("@nocobase/flow-engine"),require("lodash")):e["@nocobase/plugin-text-copy"]=t(e["react-i18next"],e["@nocobase/client-v2"],e.react,e.antd,e["@nocobase/client"],e["@formily/react"],e["@nocobase/flow-engine"],e.lodash)}(self,function(e,t,n,r,o,i,u,a){return function(){"use strict";var c={230:function(e){e.exports=i},342:function(e){e.exports=o},485:function(e){e.exports=t},694:function(e){e.exports=u},59:function(e){e.exports=r},773:function(e){e.exports=a},155:function(e){e.exports=n},953:function(t){t.exports=e}},l={};function p(e){var t=l[e];if(void 0!==t)return t.exports;var n=l[e]={exports:{}};return c[e](n,n.exports,p),n.exports}p.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return p.d(t,{a:t}),t},p.d=function(e,t){for(var n in t)p.o(t,n)&&!p.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},p.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||Function("return this")()}catch(e){if("object"==typeof window)return window}}(),p.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},p.r=function(e){"u">typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},p.g.importScripts&&(s=p.g.location+"");var s,f=p.g.document;if(!s&&f&&(f.currentScript&&"SCRIPT"===f.currentScript.tagName.toUpperCase()&&(s=f.currentScript.src),!s)){var y=f.getElementsByTagName("script");if(y.length)for(var d=y.length-1;d>-1&&(!s||!/^http(s?):/.test(s));)s=y[d--].src}if(!s)throw Error("Automatic publicPath is not supported in this browser");p.p=s.replace(/^blob:/,"").replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/");var b={};return!function(){var e="",t="u">typeof document?document.currentScript:null;if(t&&t.src){var n=t.src.replace(/^blob:/,"").replace(/#.*$/,"").replace(/\?.*$/,"");n.indexOf("/static/plugins/@nocobase/plugin-text-copy/dist/client/")>=0&&(e=n.replace(/\/[^\/]+$/,"/"))}if(!e){var r=window.__webpack_public_path__||"";r&&("/"!==r.charAt(r.length-1)&&(r+="/"),e=r+"static/plugins/@nocobase/plugin-text-copy/dist/client/")}if(!e){var o=window.__nocobase_modern_client_prefix__||"v",i="/"+(o=String(o).replace(/^\/+|\/+$/g,"")||"v")+"/";if(!(e=window.__nocobase_public_path__||"")&&window.location&&window.location.pathname){var u=window.location.pathname||"/",a=u.indexOf(i);e=a>=0?u.slice(0,a+1):"/"}e&&(e=e.replace(RegExp("/"+o+"/?$"),"/")),e||(e="/"),"/"!==e.charAt(e.length-1)&&(e+="/"),e+="static/plugins/@nocobase/plugin-text-copy/dist/client/"}p.p=e}(),!function(){p.r(b),p.d(b,{default:function(){return E}});var e=p(230),t=p(342),n=p(59),r=p(773),o=p.n(r),i=p(155),u=p.n(i),a=p(953),c=p(485),l=p(694);function s(e){return(0,l.tExpr)(e,{ns:["@nocobase/plugin-text-copy","text-copy","client"]})}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}var y=Symbol.for("nocobase.plugin-text-copy.DisplayTextFieldModel.patched"),d=function(e){var t,r=e.children,o=e.copyText,i=function(e){if(Array.isArray(e))return e}(t=u().useState(!1))||function(e){var t,n,r=null==e?null:"u">typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var o=[],i=!0,u=!1;try{for(r=r.call(e);!(i=(t=r.next()).done)&&(o.push(t.value),2!==o.length);i=!0);}catch(e){u=!0,n=e}finally{try{i||null==r.return||r.return()}finally{if(u)throw n}}return o}}(t)||function(e){if(e){if("string"==typeof e)return f(e,2);var t=Object.prototype.toString.call(e).slice(8,-1);if("Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t)return Array.from(t);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return f(e,2)}}(t)||function(){throw TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(),a=i[0],c=i[1];return u().createElement("span",{onMouseEnter:function(){return c(!0)},onMouseLeave:function(){return c(!1)}},u().createElement(n.Space,{size:"small"},r,a?u().createElement(n.Typography.Text,{copyable:{text:o}}):null))};function v(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 h(e,t,n,r,o,i,u){try{var a=e[i](u),c=a.value}catch(e){n(e);return}a.done?t(c):Promise.resolve(c).then(r,o)}function m(e){return function(){var t=this,n=arguments;return new Promise(function(r,o){var i=e.apply(t,n);function u(e){h(i,r,o,u,a,"next",e)}function a(e){h(i,r,o,u,a,"throw",e)}u(void 0)})}}function g(e,t,n){return(g=O()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&w(o,n.prototype),o}).apply(null,arguments)}function x(e){return(x=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function w(e,t){return(w=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function S(e){var t="function"==typeof Map?new Map:void 0;return(S=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 g(e,arguments,x(this).constructor)}return n.prototype=Object.create(e.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),w(n,e)})(e)}function O(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(O=function(){return!!e})()}function _(e,t){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype),a=Object.defineProperty;return a(u,"next",{value:c(0)}),a(u,"throw",{value:c(1)}),a(u,"return",{value:c(2)}),"function"==typeof Symbol&&a(u,Symbol.iterator,{value:function(){return this}}),u;function c(a){return function(c){var l=[a,c];if(n)throw TypeError("Generator is already executing.");for(;u&&(u=0,l[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&l[0]?r.return:l[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,l[1])).done)return o;switch(r=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return i.label++,{value:l[1],done:!1};case 5:i.label++,r=l[1],l=[0];continue;case 7:l=i.ops.pop(),i.trys.pop();continue;default:if(!(o=(o=i.trys).length>0&&o[o.length-1])&&(6===l[0]||2===l[0])){i=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){i.label=l[1];break}if(6===l[0]&&i.label<o[1]){i.label=o[1],o=l;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(l);break}o[2]&&i.ops.pop(),i.trys.pop();continue}l=t.call(e,i)}catch(e){l=[6,e],r=0}finally{n=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}}}var j=(0,e.observer)(function(){var r,o=(0,e.useField)(),a=(0,t.useToken)().token,c=(0,i.useRef)(null),l=function(e){if(Array.isArray(e))return e}(r=u().useState(!1))||function(e){var t,n,r=null==e?null:"u">typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var o=[],i=!0,u=!1;try{for(r=r.call(e);!(i=(t=r.next()).done)&&(o.push(t.value),2!==o.length);i=!0);}catch(e){u=!0,n=e}finally{try{i||null==r.return||r.return()}finally{if(u)throw n}}return o}}(r)||function(e){if(e){if("string"==typeof e)return v(e,2);var t=Object.prototype.toString.call(e).slice(8,-1);if("Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t)return Array.from(t);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return v(e,2)}}(r)||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,i.useEffect)(function(){if(o.value&&o.readPretty&&c.current){var e,t,n=c.current,r=function(e){s(!0)},i=function(e){s(!1)};return null==n||null==(e=n.parentElement)||e.addEventListener("mouseover",r),null==n||null==(t=n.parentElement)||t.addEventListener("mouseout",i),function(){var e,t;null==n||null==(e=n.parentElement)||e.removeEventListener("mouseover",r),null==n||null==(t=n.parentElement)||t.removeEventListener("mouseout",i)}}},[o.readPretty,o.value]);var f=o.readPretty&&(!o.value||!p);return u().createElement(n.Typography.Text,{ref:c,copyable:{text:o.value},style:{marginLeft:o.readPretty?a.marginXXS:0,opacity:+!f}})},{displayName:"TextCopyButton"}),E=function(n){var r;if("function"!=typeof n&&null!==n)throw TypeError("Super expression must either be null or a function");function i(){var e,t;if(!(this instanceof i))throw TypeError("Cannot call a class as a function");return e=i,t=arguments,e=x(e),function(e,t){var n;if(t&&("object"==((n=t)&&"u">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,O()?Reflect.construct(e,t||[],x(this).constructor):e.apply(this,t))}return i.prototype=Object.create(n&&n.prototype,{constructor:{value:i,writable:!0,configurable:!0}}),n&&w(i,n),r=[{key:"load",value:function(){return m(function(){var n;return _(this,function(r){c.DisplayTextFieldModel.registerFlow({key:"textCopySettings",title:s("Display Field settings"),sort:210,steps:{displayCopyButton:{title:s("Display copy button"),uiMode:{type:"switch",key:"displayCopyButton"},defaultParams:{displayCopyButton:!1},handler:function(e,t){e.model.setProps({displayCopyButton:t.displayCopyButton})}}}});var i=c.DisplayTextFieldModel.prototype;if(!i[y]){var l=i.renderComponent;i.renderComponent=function(e,t){var n,r=l.call(this,e,t),o=this.t(e);return(null==(n=this.props)?void 0:n.displayCopyButton)&&null!=o&&""!==o?u().createElement(d,{copyText:String(o)},r):r},i[y]=!0}return n=u().createElement(j,null),this.app.addScopes({TextCopyButton:n}),this.app.schemaSettingsManager.addItem("fieldSettings:component:Input","enableCopier",{type:"switch",useComponentProps:function(){var r,i=(0,a.useTranslation)("text-copy").t,u=(0,t.useColumnSchema)().fieldSchema,c=(0,e.useFieldSchema)(),l=(0,e.useField)(),p=(0,t.useDesignable)().dn,s=u||c;return{title:i("Display copy button"),checked:!!(null==(r=s["x-component-props"])?void 0:r.addonAfter),onChange:function(e){return m(function(){return _(this,function(t){switch(t.label){case 0:return e?(l.componentProps.addonAfter=n,o().set(s,"x-component-props.addonAfter","{{TextCopyButton}}")):(l.componentProps.addonAfter=null,o().unset(s,"x-component-props.addonAfter")),[4,p.emit("patch",{schema:{"x-uid":s["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}({},s["x-component-props"])}})];case 1:return t.sent(),[2]}})})()}}}}),[2]})}).call(this)}}],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)}}(i.prototype,r),i}(S(t.Plugin))}(),b}()});