@easy-cms/plugin-social 1.35.1 → 1.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin.js +41 -30
- package/dist/index.js +102 -2
- package/package.json +4 -4
package/dist/admin.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var x="/social/channels",
|
|
1
|
+
var x="/social/channels",f="/social/post",w="/social/ai",A="/social/share";var C=["value","path","doc","collection","id","uiLocale","readOnly","api"],$={en:{pick:"Choose a channel",gone:"{id} (not connected)",none:"No channel is connected yet: add one in Settings \u2192 Integrations.",chars:"{n} characters",charsMax:"{n} / {max} characters",over:"Too long for {channel}",saveFirst:"Save the post first, then post it.",savedOnly:"Posts what is saved: save your changes first.",postNow:"Post now",confirmNow:"Post to {n} channels now? Posts can\u2019t be taken back from here.",schedule:"Schedule",when:"Post at",scheduledFor:"Scheduled for {time}",cancel:"Cancel the schedule",retry:"Retry",waiting:"Not posted",scheduled:"Scheduled",sending:"Sending\u2026",retrying:"Trying again at {time}",posted:"Posted",view:"View post",failed:"Failed",noRight:"You may write this post; someone who may post sends it.",share:"Share on social media",shareSave:"Save first, then share.",sharing:"Creating a post\u2026",writeAi:"Write with AI",writingAi:"Writing\u2026",spentAi:"Today's AI requests are used up",replaceAi:"Replace this text with one written by AI?",writtenAi:"Written by AI: check it before posting.",undo:"Undo"},th:{pick:"\u0E40\u0E25\u0E37\u0E2D\u0E01\u0E0A\u0E48\u0E2D\u0E07\u0E17\u0E32\u0E07",gone:"{id} (\u0E44\u0E21\u0E48\u0E44\u0E14\u0E49\u0E40\u0E0A\u0E37\u0E48\u0E2D\u0E21\u0E15\u0E48\u0E2D\u0E41\u0E25\u0E49\u0E27)",none:"\u0E22\u0E31\u0E07\u0E44\u0E21\u0E48\u0E21\u0E35\u0E0A\u0E48\u0E2D\u0E07\u0E17\u0E32\u0E07\u0E17\u0E35\u0E48\u0E40\u0E0A\u0E37\u0E48\u0E2D\u0E21\u0E15\u0E48\u0E2D: \u0E40\u0E1E\u0E34\u0E48\u0E21\u0E44\u0E14\u0E49\u0E43\u0E19 Settings \u2192 \u0E01\u0E32\u0E23\u0E40\u0E0A\u0E37\u0E48\u0E2D\u0E21\u0E15\u0E48\u0E2D",chars:"{n} \u0E15\u0E31\u0E27\u0E2D\u0E31\u0E01\u0E29\u0E23",charsMax:"{n} / {max} \u0E15\u0E31\u0E27\u0E2D\u0E31\u0E01\u0E29\u0E23",over:"\u0E22\u0E32\u0E27\u0E40\u0E01\u0E34\u0E19\u0E2A\u0E33\u0E2B\u0E23\u0E31\u0E1A {channel}",saveFirst:"\u0E1A\u0E31\u0E19\u0E17\u0E36\u0E01\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E01\u0E48\u0E2D\u0E19 \u0E41\u0E25\u0E49\u0E27\u0E04\u0E48\u0E2D\u0E22\u0E42\u0E1E\u0E2A\u0E15\u0E4C",savedOnly:"\u0E08\u0E30\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E15\u0E32\u0E21\u0E17\u0E35\u0E48\u0E1A\u0E31\u0E19\u0E17\u0E36\u0E01\u0E44\u0E27\u0E49: \u0E1A\u0E31\u0E19\u0E17\u0E36\u0E01\u0E01\u0E32\u0E23\u0E41\u0E01\u0E49\u0E44\u0E02\u0E01\u0E48\u0E2D\u0E19",postNow:"\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E40\u0E25\u0E22",confirmNow:"\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E44\u0E1B {n} \u0E0A\u0E48\u0E2D\u0E07\u0E17\u0E32\u0E07\u0E40\u0E25\u0E22\u0E44\u0E2B\u0E21? \u0E22\u0E01\u0E40\u0E25\u0E34\u0E01\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E08\u0E32\u0E01\u0E17\u0E35\u0E48\u0E19\u0E35\u0E48\u0E44\u0E21\u0E48\u0E44\u0E14\u0E49",schedule:"\u0E15\u0E31\u0E49\u0E07\u0E40\u0E27\u0E25\u0E32",when:"\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E40\u0E27\u0E25\u0E32",scheduledFor:"\u0E15\u0E31\u0E49\u0E07\u0E40\u0E27\u0E25\u0E32\u0E44\u0E27\u0E49 {time}",cancel:"\u0E22\u0E01\u0E40\u0E25\u0E34\u0E01\u0E01\u0E32\u0E23\u0E15\u0E31\u0E49\u0E07\u0E40\u0E27\u0E25\u0E32",retry:"\u0E25\u0E2D\u0E07\u0E2D\u0E35\u0E01\u0E04\u0E23\u0E31\u0E49\u0E07",waiting:"\u0E22\u0E31\u0E07\u0E44\u0E21\u0E48\u0E42\u0E1E\u0E2A\u0E15\u0E4C",scheduled:"\u0E15\u0E31\u0E49\u0E07\u0E40\u0E27\u0E25\u0E32\u0E44\u0E27\u0E49",sending:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E2A\u0E48\u0E07\u2026",retrying:"\u0E08\u0E30\u0E25\u0E2D\u0E07\u0E43\u0E2B\u0E21\u0E48\u0E40\u0E27\u0E25\u0E32 {time}",posted:"\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E41\u0E25\u0E49\u0E27",view:"\u0E14\u0E39\u0E42\u0E1E\u0E2A\u0E15\u0E4C",failed:"\u0E44\u0E21\u0E48\u0E2A\u0E33\u0E40\u0E23\u0E47\u0E08",noRight:"\u0E04\u0E38\u0E13\u0E40\u0E02\u0E35\u0E22\u0E19\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E19\u0E35\u0E49\u0E44\u0E14\u0E49 \u0E2A\u0E48\u0E27\u0E19\u0E01\u0E32\u0E23\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E15\u0E49\u0E2D\u0E07\u0E43\u0E2B\u0E49\u0E1C\u0E39\u0E49\u0E17\u0E35\u0E48\u0E21\u0E35\u0E2A\u0E34\u0E17\u0E18\u0E34\u0E4C\u0E40\u0E1B\u0E47\u0E19\u0E04\u0E19\u0E01\u0E14",share:"\u0E41\u0E0A\u0E23\u0E4C\u0E44\u0E1B\u0E42\u0E0B\u0E40\u0E0A\u0E35\u0E22\u0E25",shareSave:"\u0E1A\u0E31\u0E19\u0E17\u0E36\u0E01\u0E01\u0E48\u0E2D\u0E19 \u0E41\u0E25\u0E49\u0E27\u0E04\u0E48\u0E2D\u0E22\u0E41\u0E0A\u0E23\u0E4C",sharing:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E2A\u0E23\u0E49\u0E32\u0E07\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u2026",writeAi:"\u0E40\u0E02\u0E35\u0E22\u0E19\u0E14\u0E49\u0E27\u0E22 AI",writingAi:"\u0E01\u0E33\u0E25\u0E31\u0E07\u0E40\u0E02\u0E35\u0E22\u0E19\u2026",spentAi:"\u0E43\u0E0A\u0E49\u0E04\u0E33\u0E02\u0E2D AI \u0E02\u0E2D\u0E07\u0E27\u0E31\u0E19\u0E19\u0E35\u0E49\u0E04\u0E23\u0E1A\u0E41\u0E25\u0E49\u0E27",replaceAi:"\u0E41\u0E17\u0E19\u0E17\u0E35\u0E48\u0E02\u0E49\u0E2D\u0E04\u0E27\u0E32\u0E21\u0E19\u0E35\u0E49\u0E14\u0E49\u0E27\u0E22\u0E02\u0E49\u0E2D\u0E04\u0E27\u0E32\u0E21\u0E17\u0E35\u0E48 AI \u0E40\u0E02\u0E35\u0E22\u0E19\u0E44\u0E2B\u0E21?",writtenAi:"\u0E40\u0E02\u0E35\u0E22\u0E19\u0E42\u0E14\u0E22 AI: \u0E15\u0E23\u0E27\u0E08\u0E14\u0E39\u0E01\u0E48\u0E2D\u0E19\u0E42\u0E1E\u0E2A\u0E15\u0E4C",undo:"\u0E40\u0E25\u0E34\u0E01\u0E17\u0E33"}},s=o=>o.replace(/[&<>"']/g,t=>({"&":"&","<":"<",">":">",'"':""","'":"'"})[t]);function L(o){if(typeof Intl.Segmenter!="function")return[...o].length;let t=0;for(let e of new Intl.Segmenter(void 0,{granularity:"grapheme"}).segment(o))t++;return t}var k=new WeakMap;function O(o){let t=k.get(o);if(t)return t;let e=o("GET",x).then(n=>n).catch(()=>({channels:[],canPost:!1}));return k.set(o,e),e}var E=new WeakMap;function S(o,t=!1){let e=E.get(o);if(e&&!t)return e;let n=o("GET","/admin/ai/status").then(r=>{let i=r;return{ready:i.connected&&i.features.includes("social"),spent:i.used>=i.limit}}).catch(()=>({ready:!1,spent:!1}));return E.set(o,n),n}var P=`
|
|
2
2
|
:host { display: block; font: inherit; color: var(--text); }
|
|
3
3
|
.row { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
|
|
4
4
|
.hint { margin: 0; font-size: 0.8rem; color: var(--text-muted); }
|
|
@@ -18,13 +18,24 @@ var x="/social/channels",m="/social/post",w="/social/share";var S=["value","path
|
|
|
18
18
|
border-radius: var(--radius-sm, 8px); background: var(--surface); color: var(--text); font: inherit;
|
|
19
19
|
}
|
|
20
20
|
a { color: var(--accent-ink); }
|
|
21
|
-
`,p=class o extends HTMLElement{ctx={value:void 0,path:void 0,doc:{},collection:void 0,id:null,uiLocale:"en",readOnly:!1,api:void 0};channels=[];canPost=!1;asked=!1;queued=!1;html="";constructor(){super();let t=this.attachShadow({mode:"open"});t.addEventListener("click",e=>{let
|
|
22
|
-
<select data-action="pick" aria-label="${
|
|
23
|
-
${t?"":`<option value="">${
|
|
24
|
-
${!
|
|
25
|
-
${this.channels.map(
|
|
21
|
+
`,p=class o extends HTMLElement{ctx={value:void 0,path:void 0,doc:{},collection:void 0,id:null,uiLocale:"en",readOnly:!1,api:void 0};channels=[];canPost=!1;asked=!1;queued=!1;html="";constructor(){super();let t=this.attachShadow({mode:"open"});t.addEventListener("click",e=>{let n=e.target.closest("[data-action]"),r=n?.getAttribute("data-action");r&&n?.tagName!=="SELECT"&&this.onAction(r,n)}),t.addEventListener("change",e=>this.onChange(e.target))}static{for(let t of C)Object.defineProperty(o.prototype,t,{get(){return this.ctx[t]},set(e){this.ctx[t]=e,this.update()}})}connectedCallback(){this.update()}t(t,e={}){return(this.ctx.uiLocale==="th"?$.th:$.en)[t].replace(/\{(\w+)\}/g,(r,i)=>String(e[i]??""))}time(t){return new Date(t).toLocaleString(this.ctx.uiLocale==="th"?"th-TH":"en-GB",{dateStyle:"medium",timeStyle:"short"})}labelOf(t){return this.channels.find(e=>e.id===t)?.label??t}update(){!this.asked&&this.ctx.api&&(this.asked=!0,O(this.ctx.api).then(({channels:t,canPost:e})=>{this.channels=t,this.canPost=e,this.update()})),!this.queued&&(this.queued=!0,queueMicrotask(()=>{this.queued=!1;let t=this.shadowRoot;if(!t)return;let e=`<style>${P}${this.css()}</style>${this.render()}`;if(e===this.html)return;let n=t.activeElement?.getAttribute("data-action");this.html=e,t.innerHTML=e,n&&t.querySelector(`[data-action="${n}"]`)?.focus()}))}onAction(t,e){}onChange(t){}css(){return""}},m=class extends p{render(){let t=typeof this.ctx.value=="string"?this.ctx.value:"",e=this.ctx.doc.delivery??null,n=!!t&&e?.channels?.[t]?.state==="posted";if(this.channels.length===0&&!t)return`<p class="hint">${s(this.t("none"))}</p>`;let r=this.channels.some(i=>i.id===t);return`
|
|
22
|
+
<select data-action="pick" aria-label="${s(this.t("pick"))}" ${this.ctx.readOnly||n?"disabled":""}>
|
|
23
|
+
${t?"":`<option value="">${s(this.t("pick"))}</option>`}
|
|
24
|
+
${!r&&t?`<option value="${s(t)}" selected>${s(this.t("gone",{id:t}))}</option>`:""}
|
|
25
|
+
${this.channels.map(i=>`<option value="${s(i.id)}" ${i.id===t?"selected":""}>${s(i.label)}</option>`).join("")}
|
|
26
26
|
</select>
|
|
27
|
-
`}css(){return"select { width: 100%; }"}onChange(t){t.getAttribute("data-action")==="pick"&&this.dispatchEvent(new CustomEvent("change",{detail:t.value}))}},v=class extends p{
|
|
27
|
+
`}css(){return"select { width: 100%; }"}onChange(t){t.getAttribute("data-action")==="pick"&&this.dispatchEvent(new CustomEvent("change",{detail:t.value}))}},v=class extends p{ai={ready:!1,spent:!1};aiAsked=!1;busy=!1;beforeAi=null;notice=null;css(){return`
|
|
28
|
+
.bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.25rem 0.75rem; }
|
|
29
|
+
.ai { color: var(--accent-ink); font-weight: 600; border-color: transparent; background: transparent; }
|
|
30
|
+
.written { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin: 0.25rem 0 0; font-size: 0.8rem; color: var(--accent-ink); }
|
|
31
|
+
`}render(){!this.aiAsked&&this.ctx.api&&(this.aiAsked=!0,S(this.ctx.api).then(e=>{this.ai=e,this.update()}));let t=this.ai.ready&&!this.ctx.readOnly?`<button type="button" class="ai" data-action="ai" ${this.busy||this.ai.spent?"disabled":""}>
|
|
32
|
+
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3l1.9 5.1L19 10l-5.1 1.9L12 17l-1.9-5.1L5 10l5.1-1.9z"/></svg>
|
|
33
|
+
${s(this.t(this.busy?"writingAi":this.ai.spent?"spentAi":"writeAi"))}
|
|
34
|
+
</button>`:"";return`
|
|
35
|
+
<div class="bar">${this.count()}${t}</div>
|
|
36
|
+
${this.beforeAi?`<p class="written" role="status">${s(this.t("writtenAi"))} <button type="button" data-action="undo-ai">${s(this.t("undo"))}</button></p>`:""}
|
|
37
|
+
${this.notice?`<p class="error" role="alert">${s(this.notice)}</p>`:""}
|
|
38
|
+
`}async onAction(t){let{api:e,path:n,id:r,doc:i}=this.ctx;if(!e||!n)return;let l=c=>this.dispatchEvent(new CustomEvent("set-field",{detail:{path:n,value:c}}));if(t==="undo-ai"&&this.beforeAi){l(this.beforeAi.value),this.beforeAi=null,this.update();return}if(t!=="ai"||this.busy||(typeof this.ctx.value=="string"?this.ctx.value.trim():"")&&!window.confirm(this.t("replaceAi")))return;let u=/^channels\.(\d+)\.text$/.exec(n),a=Array.isArray(i.channels)?i.channels:[],d=u?a[Number(u[1])]?.connection:void 0;this.busy=!0,this.notice=null,this.update();try{let c=await e("POST",w,{post:r,doc:{title:i.title,text:i.text,source:i.source??null},...typeof d=="string"?{channel:d}:{}});this.beforeAi??={value:this.ctx.value??null},l(c.text)}catch(c){this.notice=c.message}finally{this.busy=!1,this.ai=await S(e,!0),this.update()}}count(){let t=typeof this.ctx.value=="string"?this.ctx.value:"",e=L(t.trim()),n=Array.isArray(this.ctx.doc.channels)?this.ctx.doc.channels:[],r=/^channels\.(\d+)\.text$/.exec(this.ctx.path??""),h=(r?n.filter((a,d)=>d===Number(r[1])):n.filter(a=>!(typeof a.text=="string"&&a.text.trim()))).map(a=>this.channels.find(d=>d.id===a.connection)).filter(a=>!!a?.maxLength).sort((a,d)=>(a.maxLength??0)-(d.maxLength??0))[0];if(!h)return`<span class="hint">${s(this.t("chars",{n:e}))}</span>`;let u=e>(h.maxLength??Number.POSITIVE_INFINITY);return`<span class="${u?"error":"hint"}">${s(this.t("charsMax",{n:e,max:h.maxLength??0}))}${u?` \xB7 ${s(this.t("over",{channel:h.label}))}`:""}</span>`}},y=class extends p{live=null;busy=!1;notice=null;problems=[];checkedFor=null;timer;disconnectedCallback(){clearTimeout(this.timer)}delivery(){return this.live??this.ctx.value??{channels:{}}}css(){return`
|
|
28
39
|
.panel { display: flex; flex-direction: column; gap: 0.75rem; }
|
|
29
40
|
ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
|
|
30
41
|
li { display: flex; flex-direction: column; gap: 0.2rem; padding: 0.55rem 0.65rem; border: 1px solid var(--border); border-radius: var(--radius-sm, 8px); background: var(--surface); }
|
|
@@ -36,31 +47,31 @@ var x="/social/channels",m="/social/post",w="/social/share";var S=["value","path
|
|
|
36
47
|
.schedule { display: flex; gap: 0.4rem; flex-wrap: wrap; }
|
|
37
48
|
.schedule input { flex: 1 1 10rem; }
|
|
38
49
|
.warning { margin: 0; padding: 0.5rem 0.65rem; border-radius: var(--radius-sm, 8px); background: var(--warning-soft); color: var(--warning-text); font-size: 0.8rem; }
|
|
39
|
-
`}check(){let{api:t,id:e}=this.ctx,{image:
|
|
40
|
-
<li class="${
|
|
41
|
-
<span class="name">${
|
|
42
|
-
<span class="state">${
|
|
43
|
-
${
|
|
44
|
-
${
|
|
45
|
-
${
|
|
46
|
-
</li>`}).join(""),u=this.canPost?
|
|
47
|
-
<div class="row"><button type="button" class="primary" data-action="now" ${this.busy?"disabled":""}>${
|
|
48
|
-
${
|
|
49
|
-
<div class="row"><button type="button" data-action="cancel" ${this.busy?"disabled":""}>${
|
|
50
|
-
<input type="datetime-local" data-field="when" aria-label="${
|
|
51
|
-
<button type="button" data-action="schedule" ${this.busy?"disabled":""}>${
|
|
50
|
+
`}check(){let{api:t,id:e}=this.ctx,{image:n=null,source:r=null}=this.ctx.doc,i=JSON.stringify([e,n,r]);!t||e===null||i===this.checkedFor||(this.checkedFor=i,t("POST",f,{post:e,action:"check",image:n,source:r}).then(l=>{this.problems=l.problems??[],this.update()}).catch(()=>{}))}render(){if(this.ctx.id===null)return`<p class="hint">${s(this.t("saveFirst"))}</p>`;this.check();let e=this.delivery(),n=(Array.isArray(this.ctx.doc.channels)?this.ctx.doc.channels:[]).filter(a=>typeof a.connection=="string"&&a.connection),r=a=>e.channels?.[a]??{state:"waiting",attempts:0},i=n.filter(a=>["waiting","scheduled"].includes(r(a.connection).state)),l=n.some(a=>r(a.connection).state==="scheduled"),h=n.map(a=>{let d=a.connection,c=r(d),T=c.state==="sending"&&c.attempts>0&&c.nextAttemptAt?this.t("retrying",{time:this.time(c.nextAttemptAt)}):this.t(c.state);return`
|
|
51
|
+
<li class="${c.state}" data-channel="${s(d)}">
|
|
52
|
+
<span class="name">${s(this.labelOf(d))}</span>
|
|
53
|
+
<span class="state">${s(T)}</span>
|
|
54
|
+
${c.state==="posted"&&c.url?`<a href="${s(c.url)}" target="_blank" rel="noopener">${s(this.t("view"))}</a>`:""}
|
|
55
|
+
${c.error&&c.state!=="posted"?`<p class="error">${s(c.error)}</p>`:""}
|
|
56
|
+
${c.state==="failed"&&this.canPost?`<div class="row"><button type="button" data-action="retry" data-channel="${s(d)}" ${this.busy?"disabled":""}>${s(this.t("retry"))}</button></div>`:""}
|
|
57
|
+
</li>`}).join(""),u=this.canPost?i.length===0?"":`
|
|
58
|
+
<div class="row"><button type="button" class="primary" data-action="now" ${this.busy?"disabled":""}>${s(this.t("postNow"))}</button></div>
|
|
59
|
+
${l&&e.postAt?`<p class="hint">${s(this.t("scheduledFor",{time:this.time(e.postAt)}))}</p>
|
|
60
|
+
<div class="row"><button type="button" data-action="cancel" ${this.busy?"disabled":""}>${s(this.t("cancel"))}</button></div>`:`<div class="schedule">
|
|
61
|
+
<input type="datetime-local" data-field="when" aria-label="${s(this.t("when"))}" />
|
|
62
|
+
<button type="button" data-action="schedule" ${this.busy?"disabled":""}>${s(this.t("schedule"))}</button>
|
|
52
63
|
</div>`}
|
|
53
|
-
<p class="hint">${
|
|
64
|
+
<p class="hint">${s(this.t("savedOnly"))}</p>`:`<p class="hint">${s(this.t("noRight"))}</p>`;return`
|
|
54
65
|
<div class="panel">
|
|
55
|
-
${
|
|
56
|
-
${this.problems.map(
|
|
57
|
-
${
|
|
58
|
-
${this.notice?`<p class="error" role="alert">${
|
|
66
|
+
${n.length?`<ul>${h}</ul>`:`<p class="hint">${s(this.t("none"))}</p>`}
|
|
67
|
+
${this.problems.map(a=>`<p class="warning">${s(a)}</p>`).join("")}
|
|
68
|
+
${n.length?u:""}
|
|
69
|
+
${this.notice?`<p class="error" role="alert">${s(this.notice)}</p>`:""}
|
|
59
70
|
</div>
|
|
60
|
-
`}async onAction(t,e){let{api:
|
|
71
|
+
`}async onAction(t,e){let{api:n,id:r}=this.ctx;if(!n||r===null||this.busy)return;let i={post:r,action:t};if(t==="retry"&&(i.channel=e.getAttribute("data-channel")),t==="schedule"){let l=this.shadowRoot?.querySelector('[data-field="when"]');if(!l?.value){l?.focus();return}i.postAt=new Date(l.value).toISOString()}if(t==="now"){let h=(this.ctx.doc.channels??[]).filter(u=>this.delivery().channels?.[String(u.connection)]?.state!=="posted").length;if(!window.confirm(this.t("confirmNow",{n:h})))return}this.busy=!0,this.notice=null,this.update();try{let l=await n("POST",f,i);this.live=l.delivery,this.watch()}catch(l){this.notice=l.message}finally{this.busy=!1,this.update()}}watch(){clearTimeout(this.timer),!(!Object.values(this.delivery().channels??{}).some(e=>e.state==="sending")||!this.ctx.api||!this.ctx.collection||this.ctx.id===null)&&(this.timer=setTimeout(async()=>{try{let e=await this.ctx.api?.("GET",`/${this.ctx.collection}/${encodeURIComponent(String(this.ctx.id))}?depth=0`);this.live=e.delivery??null,this.update()}catch{}this.watch()},5e3))}},b=class extends p{busy=!1;notice=null;render(){let t=this.ctx.id!==null;return`
|
|
61
72
|
<div class="row">
|
|
62
|
-
<button type="button" data-action="share" ${!t||this.busy?"disabled":""}>${
|
|
73
|
+
<button type="button" data-action="share" ${!t||this.busy?"disabled":""}>${s(this.t(this.busy?"sharing":"share"))}</button>
|
|
63
74
|
</div>
|
|
64
|
-
${t?"":`<p class="hint">${
|
|
65
|
-
${this.notice?`<p class="error" role="alert">${
|
|
66
|
-
`}async onAction(t){let{api:e,id:
|
|
75
|
+
${t?"":`<p class="hint">${s(this.t("shareSave"))}</p>`}
|
|
76
|
+
${this.notice?`<p class="error" role="alert">${s(this.notice)}</p>`:""}
|
|
77
|
+
`}async onAction(t){let{api:e,id:n,collection:r}=this.ctx;if(!(t!=="share"||!e||n===null||this.busy)){this.busy=!0,this.notice=null,this.update();try{let i=await e("POST",A,{collection:r,id:n});this.dispatchEvent(new CustomEvent("navigate",{detail:i.path}))}catch(i){this.notice=i.message}finally{this.busy=!1,this.update()}}}},g=(o,t)=>{customElements.get(o)||customElements.define(o,t)};g("ecms-social-channel",m);g("ecms-social-count",v);g("ecms-social-panel",y);g("ecms-social-share",b);
|
package/dist/index.js
CHANGED
|
@@ -18,12 +18,13 @@ var INFO = {
|
|
|
18
18
|
name: "@easy-cms/plugin-social",
|
|
19
19
|
// The plugin API it is written for (`PLUGIN_API_VERSION` of @easy-cms/core).
|
|
20
20
|
apiVersion: 1,
|
|
21
|
-
...true ? { version: "1.
|
|
21
|
+
...true ? { version: "1.37.0" } : {}
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
// src/shared.ts
|
|
25
25
|
var CHANNELS_PATH = "/social/channels";
|
|
26
26
|
var ACTION_PATH = "/social/post";
|
|
27
|
+
var AI_PATH = "/social/ai";
|
|
27
28
|
var SHARE_PATH = "/social/share";
|
|
28
29
|
|
|
29
30
|
// src/webhook.ts
|
|
@@ -506,6 +507,67 @@ function socialPlugin(options = {}) {
|
|
|
506
507
|
return json(400, "action must be now, schedule, cancel or retry");
|
|
507
508
|
}
|
|
508
509
|
};
|
|
510
|
+
const aiEndpoint = {
|
|
511
|
+
path: AI_PATH,
|
|
512
|
+
method: "post",
|
|
513
|
+
handler: async ({ json: body, user, cms }) => {
|
|
514
|
+
if (!user) return json(401, "Log in to write with AI");
|
|
515
|
+
const ai = cms.ai;
|
|
516
|
+
if (!ai) return json(404, "No AI plugin");
|
|
517
|
+
const input = await body();
|
|
518
|
+
const id = typeof input.post === "string" || typeof input.post === "number" ? input.post : null;
|
|
519
|
+
const allowed = id !== null ? (await cms.documentPermissions(slug, String(id), user)).update : await cms.roles.allows(user, { collection: slug }, "create") !== false;
|
|
520
|
+
if (!allowed) return json(403, "You may not change this post");
|
|
521
|
+
const form = typeof input.doc === "object" && input.doc !== null ? input.doc : {};
|
|
522
|
+
const parts = [];
|
|
523
|
+
const ref = sourceOf(form);
|
|
524
|
+
if (ref) {
|
|
525
|
+
const config2 = cms.config.collections.find((c) => c.slug === ref.collection);
|
|
526
|
+
const source2 = await cms.findById(ref.collection, ref.id, {
|
|
527
|
+
depth: 0,
|
|
528
|
+
draft: true,
|
|
529
|
+
user,
|
|
530
|
+
overrideAccess: false
|
|
531
|
+
});
|
|
532
|
+
if (source2 && config2) parts.push(contentText(config2, source2));
|
|
533
|
+
}
|
|
534
|
+
if (parts.length === 0) {
|
|
535
|
+
for (const key of ["title", "text"])
|
|
536
|
+
if (typeof form[key] === "string" && form[key].trim())
|
|
537
|
+
parts.push(form[key]);
|
|
538
|
+
}
|
|
539
|
+
const source = parts.join("\n\n").trim();
|
|
540
|
+
if (!source) return json(400, "Nothing to write from yet: share a document or add a title");
|
|
541
|
+
const channel = typeof input.channel === "string" ? input.channel : null;
|
|
542
|
+
const adapter = channel ? channelOf(channel) : void 0;
|
|
543
|
+
const max = adapter?.maxLength ?? 500;
|
|
544
|
+
const where = channel ? `a post on ${channel.slice(0, channel.indexOf(":")) || "social media"}` : "a social media post";
|
|
545
|
+
try {
|
|
546
|
+
const result = await ai.generate(
|
|
547
|
+
{
|
|
548
|
+
feature: "social",
|
|
549
|
+
system: [
|
|
550
|
+
`You write ${where} that makes people want to read the page it shares.`,
|
|
551
|
+
`At most ${max} characters, plain text, no hashtags unless they fit, no link (it is added`,
|
|
552
|
+
`separately). Write it in ${ai.languageOf(null)}. Answer with the post only.`
|
|
553
|
+
].join(" "),
|
|
554
|
+
prompt: `The page:
|
|
555
|
+
|
|
556
|
+
${source.slice(0, 6e3)}`,
|
|
557
|
+
maxTokens: 400
|
|
558
|
+
},
|
|
559
|
+
{ user, ...id !== null ? { about: { collection: slug, id } } : {} }
|
|
560
|
+
);
|
|
561
|
+
let text = result.text.trim().replace(/^["“]+|["”]+$/g, "");
|
|
562
|
+
if ([...text].length > max)
|
|
563
|
+
text = [...text].slice(0, max - 1).join("").trimEnd() + "\u2026";
|
|
564
|
+
return { text };
|
|
565
|
+
} catch (e) {
|
|
566
|
+
const status = e.status;
|
|
567
|
+
return json(typeof status === "number" ? status : 400, e.message);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
};
|
|
509
571
|
const shareEndpoint = {
|
|
510
572
|
path: SHARE_PATH,
|
|
511
573
|
method: "post",
|
|
@@ -575,7 +637,25 @@ function socialPlugin(options = {}) {
|
|
|
575
637
|
...config,
|
|
576
638
|
collections: [...(config.collections ?? []).map(withShare), posts],
|
|
577
639
|
integrations: [...config.integrations ?? [], ...integrations],
|
|
578
|
-
endpoints: [
|
|
640
|
+
endpoints: [
|
|
641
|
+
...config.endpoints ?? [],
|
|
642
|
+
channelsEndpoint,
|
|
643
|
+
actionEndpoint,
|
|
644
|
+
shareEndpoint,
|
|
645
|
+
aiEndpoint
|
|
646
|
+
],
|
|
647
|
+
// Used only with an AI plugin (`ai` in the config): Write with AI on a post's text.
|
|
648
|
+
aiFeatures: [
|
|
649
|
+
...config.aiFeatures ?? [],
|
|
650
|
+
{
|
|
651
|
+
id: "social",
|
|
652
|
+
label: { en: "Social posts", th: "\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E42\u0E0B\u0E40\u0E0A\u0E35\u0E22\u0E25" },
|
|
653
|
+
description: {
|
|
654
|
+
en: "Writes a social post from the document it shares",
|
|
655
|
+
th: "\u0E40\u0E02\u0E35\u0E22\u0E19\u0E42\u0E1E\u0E2A\u0E15\u0E4C\u0E42\u0E0B\u0E40\u0E0A\u0E35\u0E22\u0E25\u0E08\u0E32\u0E01\u0E40\u0E2D\u0E01\u0E2A\u0E32\u0E23\u0E17\u0E35\u0E48\u0E41\u0E0A\u0E23\u0E4C"
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
],
|
|
579
659
|
jobs: [...config.jobs ?? [], job],
|
|
580
660
|
events: [.../* @__PURE__ */ new Set([...config.events ?? [], "social.posted", "social.failed"])],
|
|
581
661
|
admin: {
|
|
@@ -585,6 +665,26 @@ function socialPlugin(options = {}) {
|
|
|
585
665
|
};
|
|
586
666
|
}, INFO);
|
|
587
667
|
}
|
|
668
|
+
function contentText(collection, doc) {
|
|
669
|
+
const parts = [];
|
|
670
|
+
const titleField = collection.useAsTitle;
|
|
671
|
+
if (titleField && typeof doc[titleField] === "string") parts.push(doc[titleField]);
|
|
672
|
+
const words = (node) => {
|
|
673
|
+
if (!node || typeof node !== "object") return "";
|
|
674
|
+
const n = node;
|
|
675
|
+
const own = typeof n.text === "string" ? n.text : "";
|
|
676
|
+
const kids = Array.isArray(n.content) ? n.content.map(words).join(n.text === void 0 ? " " : "") : "";
|
|
677
|
+
return `${own}${kids}`;
|
|
678
|
+
};
|
|
679
|
+
for (const field of collection.fields) {
|
|
680
|
+
if (!("name" in field) || field.name === titleField) continue;
|
|
681
|
+
const value = doc[field.name];
|
|
682
|
+
if ((field.type === "text" || field.type === "textarea") && typeof value === "string")
|
|
683
|
+
parts.push(value);
|
|
684
|
+
else if (field.type === "richText") parts.push(words(value).replace(/\s+/g, " "));
|
|
685
|
+
}
|
|
686
|
+
return parts.map((p) => p.trim()).filter(Boolean).join("\n\n");
|
|
687
|
+
}
|
|
588
688
|
function defaults(collection, doc) {
|
|
589
689
|
const fields = collection?.fields ?? [];
|
|
590
690
|
const titleField = collection?.useAsTitle;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easy-cms/plugin-social",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.37.0",
|
|
4
4
|
"description": "Social posts for Easy CMS: write a post once and send it to several channels (webhooks to Make, n8n or Zapier first), with schedules, retries and a status per channel",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -17,12 +17,12 @@
|
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@easy-cms/core": "^1.
|
|
20
|
+
"@easy-cms/core": "^1.37.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"happy-dom": "^21.0.4",
|
|
24
|
-
"@easy-cms/core": "^1.
|
|
25
|
-
"@easy-cms/db-sqlite": "^1.
|
|
24
|
+
"@easy-cms/core": "^1.37.0",
|
|
25
|
+
"@easy-cms/db-sqlite": "^1.37.0"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
28
|
"node": ">=22.12"
|