@dtu-olp-2024/rich-block-nocobase
Version:
A NocoBase plugin for rich block rendering, supporting custom block types, dynamic block rendering, and block-based workflows. Built for the DTU GreenHope project.
11 lines (9 loc) • 7.73 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(i,e){typeof exports=="object"&&typeof module!="undefined"?e(exports,require("@nocobase/client"),require("@formily/react"),require("react/jsx-runtime"),require("react"),require("@formily/shared"),require("@ant-design/icons")):typeof define=="function"&&define.amd?define(["exports","@nocobase/client","@formily/react","react/jsx-runtime","react","@formily/shared","@ant-design/icons"],e):(i=typeof globalThis!="undefined"?globalThis:i||self,e(i["@dtu-olp-2024/rich-block-nocobase"]={},i["@nocobase/client"],i["@formily/react"],i.jsxRuntime,i.react,i["@formily/shared"],i["@ant-design/icons"]))})(this,function(i,e,r,f,S,k,v){"use strict";var q=Object.defineProperty,E=Object.defineProperties;var L=Object.getOwnPropertyDescriptors;var D=Object.getOwnPropertySymbols;var $=Object.prototype.hasOwnProperty,U=Object.prototype.propertyIsEnumerable;var H=(i,e,r)=>e in i?q(i,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):i[e]=r,u=(i,e)=>{for(var r in e||(e={}))$.call(e,r)&&H(i,r,e[r]);if(D)for(var r of D(e))U.call(e,r)&&H(i,r,e[r]);return i},x=(i,e)=>E(i,L(e));var y=(i,e,r)=>new Promise((f,S)=>{var k=s=>{try{l(r.next(s))}catch(b){S(b)}},v=s=>{try{l(r.throw(s))}catch(b){S(b)}},l=s=>s.done?f(s.value):Promise.resolve(s.value).then(k,v);l((r=r.apply(i,e)).next())});const l="Rich",s=l.toLowerCase(),N={name:"@dtu-olp-2024/rich-block-nocobase",version:"1.0.1",license:"GPL-3.0",main:"dist/server/index.js",dependencies:{"@formily/react":"^2.3.2"},peerDependencies:{"@nocobase/client":"1.x","@nocobase/server":"1.x","@nocobase/test":"1.x"},publishConfig:{access:"public",registry:"https://registry.npmjs.org/"},repository:{type:"git",url:"git+https://github.com/olp-dtu-2024/DTU-GreenHope"},files:["dist","dist/node_modules","README.md","LICENSE","package.json","server.js","server.d.ts","client.js","client.d.ts","CHANGELOG.md"],keywords:["plugin","nocobase","dtu","greenhope","rich-block"],description:"A NocoBase plugin for rich block rendering, supporting custom block types, dynamic block rendering, and block-based workflows. Built for the DTU GreenHope project.",displayName:"Rich Block Plugin - DTU GreenHope 2024"};function C(){const o=e.useApp();return t=>o.i18n.t(t,{ns:[N.name,"client"]})}const j={name:"height",type:"actionModal",useComponentProps(){var c,d;const o=r.useFieldSchema(),{deepMerge:t}=e.useDesignable(),n=C();return console.log(">>>>",o["x-decorator-props"]),{title:n("Edit Height"),schema:{type:"object",title:n("Edit Height"),properties:{height:{title:n("Height"),type:"number",default:(d=(c=o["x-decorator-props"])==null?void 0:c[s])==null?void 0:d.height,"x-decorator":"FormItem","x-component":"InputNumber"}}},onSubmit({height:p}){var a;t({"x-uid":o.parent["x-uid"],"x-decorator-props":x(u({},o["x-decorator-props"]),{[s]:x(u({},(a=o["x-decorator-props"])==null?void 0:a[s]),{height:p})})})}}}},I=new e.SchemaSettings({name:`blockSettings:${s}`,items:[{name:e.SchemaSettingsDataScope.name,Component:e.SchemaSettingsDataScope,useComponentProps:()=>{var p,a;const o=e.useCollection(),t=e.useAPIClient(),n=r.useFieldSchema(),c=((a=(p=n==null?void 0:n["x-decorator-props"])==null?void 0:p.params)==null?void 0:a.filter)||{},{deepMerge:d}=e.useDesignable();return{collectionName:o.name,defaultFilter:c,onSubmit:m=>y(this,null,function*(){const h={"x-uid":n["x-uid"],"x-decorator-props":x(u({},n["x-decorator-props"]),{params:{filter:u(u({},c),m.filter)}})};yield t.request({url:"/uiSchemas:patch",method:"POST",data:h}).then(g=>{g.status===200&&d({"x-decorator-props":x(u({},n["x-decorator-props"]),{params:{filter:u(u({},c),m.filter)}})})})})}}},{type:"remove",name:"remove",componentProps:{removeParentsIfNoChildren:!0,breakRemoveOn:{"x-component":"Grid"}}},j]});function P(){const o=e.useCollection(),{data:t,loading:n}=e.useDataBlockRequest();return{collectionName:o.name,data:t==null?void 0:t.data,loading:n}}function w(){var t,n;return(n=(t=r.useFieldSchema().parent)==null?void 0:t["x-decorator-props"])==null?void 0:n[s]}function G({dataSource:o="main",collection:t}){return{type:"void","x-decorator":"DataBlockProvider","x-decorator-props":{[s]:{},dataSource:o,collection:t,action:"list"},"x-component":"CardItem","x-toolbar":"BlockSchemaToolbar","x-settings":I.name,"x-component-props":{useConfigureFields:!0},"x-use-decorator-props":"useBlockScopeDecoratorProps","x-use-component-props":"useRichBlockProps",properties:{[s]:{type:"void","x-component":`${l}Block`,"x-use-component-props":"useRichBlockProps",properties:{fields:{type:"void","x-component":"Grid","x-initializer":"richText:configureFields"}}}}}}const T=new e.SchemaSettings({name:"fieldSettings:component:richText",items:[{type:"remove",name:"remove",componentProps:{removeParentsIfNoChildren:!0,breakRemoveOn:{"x-component":"Grid"}}},e.createSelectSchemaSettingsItem({name:"richText",title:"Rich Text",schemaKey:"x-component-props.richText",defaultValue:"h1",useOptions(){return[{label:"Heading 1",value:"h1"},{label:"Heading 2",value:"h2"},{label:"Heading 3",value:"h3"},{label:"Heading 4",value:"h4"},{label:"Heading 5",value:"h5"},{label:"Heading 6",value:"h6"},{label:"Paragragh",value:"p"}]}})]}),R=({name:o})=>{var m;const{data:t,loading:n}=P(),c=r.useFieldSchema(),d=((m=c==null?void 0:c["x-component-props"])==null?void 0:m.richText)||"h1",p=S.useMemo(()=>{if(!t)return"";if(Array.isArray(t)){const h=t.find(g=>g[o]);return h==null?void 0:h[o]}return t[o]},[t,o]),a=d;return n?f.jsx(a,{children:"Loading..."}):f.jsx(a,{children:p||"N/A"})};function M({name:o}){return{type:"void","x-collection-field":o,"x-component":"Grid.Row",properties:{[k.uid()]:{type:"void","x-component":"Grid.Col",properties:{[o]:{type:"void","x-component":"InfoField","x-component-props":{name:o,richText:"h1"},"x-settings":T.name,"x-decorator":"FormItem"}}}}}}function z(o){var h;const{collectionField:t,schema:n,remove:c,insert:d}=o,p=((h=t.uiSchema)==null?void 0:h.title)||t.name,a=t.name,m=Object.values(n.properties||{}).find(g=>g["x-collection-field"]===a);return{name:a,title:p,type:"switch",checked:!!m,onClick(){if(m){c(m);return}d(M({name:a}))}}}const A=new e.SchemaInitializer({name:"richText:configureFields",icon:"SettingOutlined",title:"Configure fields",items:[{type:"itemGroup",name:"fields",title:"Display fields",useChildren(){const o=e.useCollection(),{insert:t}=e.useSchemaInitializer(),{remove:n}=e.useDesignable(),c=r.useFieldSchema();return o.getFields().map(p=>z({collectionField:p,schema:c,remove:n,insert:t}))}}]}),F={type:"item",name:s,Component:"DataBlockInitializer",useComponentProps(){const{insert:o}=e.useSchemaInitializer();return{title:C()(`${l} block`),icon:f.jsx(v.CodeOutlined,{}),componentType:l,useTranslationHooks:C,onCreateBlockSchema({item:n}){o(G({dataSource:n.dataSource,collection:n.name}))}}}},O=e.withDynamicSchemaProps(o=>{const{height:t,children:n}=o;return f.jsx("div",{style:{height:t},children:n})},{displayName:l});class B extends e.Plugin{afterAdd(){return y(this,null,function*(){})}beforeLoad(){return y(this,null,function*(){})}load(){return y(this,null,function*(){this.app.addComponents({InfoField:R,RichBlock:O}),this.app.schemaInitializerManager.add(A),this.app.addScopes({useHeaderPickerProps:P,useRichBlockProps:w}),this.app.schemaSettingsManager.add(I,T),this.app.schemaInitializerManager.addItem("page:addBlock",`dataBlocks.${F.name}`,F)})}}i.HeaderPickerNocobaseClient=B,i.default=B,Object.defineProperties(i,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});