Add flatten-text/plugin.js

This commit is contained in:
Marsn3 2025-03-31 04:03:32 +02:00
parent e4293e2c1d
commit 3ef660c884

1
flatten-text/plugin.js Normal file
View file

@ -0,0 +1 @@
penpot.ui.open("Flatten Text",`?theme=${penpot.theme}`,{width:400,height:400});penpot.on("themechange",t=>{penpot.ui.sendMessage({type:"theme",content:t})});penpot.ui.onMessage(async t=>{if(t.type==="init"){const e=penpot.selection.filter(n=>n.type==="text");if(e.length>0){const n=e[0];penpot.ui.sendMessage({type:"text",text:n.characters,fontFamily:n.fontFamily,fontSize:n.fontSize,fontId:n.fontId,fontWeight:n.fontWeight,fontStyle:n.fontStyle,width:n.width,height:n.height,lineHeight:n.lineHeight,letterSpacing:n.letterSpacing,textTransform:n.textTransform,textColor:n.fills!=="mixed"?n.fills[0].fillColor:"#000000",textDecoration:n.textDecoration,multiSelection:e.length>1,totalSelected:e.length,hasSelection:!0})}else penpot.ui.sendMessage({type:"noselection"})}if(t.type==="svg"){const e=penpot.selection.filter(o=>o.type==="text");if(e.length===0)return;const n=penpot.history.undoBlockBegin(),l=e[0],i=penpot.createShapeFromSvg(t.svg);if(!i)return;const r=i==null?void 0:i.children.filter(o=>o!==void 0&&o.name!=="base-background");if(r!=null&&r.length){const o=penpot.group(r);if(!o)return;if(penpot.alignVertical(i==null?void 0:i.children,"center"),i.rotation=l.rotation,i.x=l.x,i.y=l.y,t.asSvg||penpot.flatten([o]),o.strokes=l.strokes||[],o.fills=l.fills||[],o.blur=l.blur||void 0,o.shadows=l.shadows||[],o.opacity=l.opacity?l.opacity:1,o.blendMode=l.blendMode||"normal",o.name=l.name+" Paths",t.asSvg&&(i.name=l.name+" SVG"),o.parent&&o.parent.type==="group"){const c=i==null?void 0:i.children.filter(f=>f!==void 0&&f.name==="base-background");c!=null&&c.length&&c[0]&&!t.asSvg&&c[0].remove(),t.asSvg||penpot.ungroup(o.parent)}penpot.selection=[t.asSvg?i:o],penpot.history.undoBlockFinish(n)}}});penpot.on("selectionchange",()=>{const s=penpot.selection.filter(e=>e.type==="text");if(s.length>0){const e=s[0];penpot.ui.sendMessage({type:"text",text:e.characters,fontFamily:e.fontFamily,fontSize:e.fontSize,fontId:e.fontId,fontWeight:e.fontWeight,fontStyle:e.fontStyle,width:e.width,height:e.height,lineHeight:e.lineHeight,letterSpacing:e.letterSpacing,textTransform:e.textTransform,textColor:e.fills!=="mixed"?e.fills[0].fillColor:"#000000",textDecoration:e.textDecoration})}else penpot.ui.sendMessage({type:"noselection"})});penpot.on("shapechange",t=>{if(t.type==="text"){const s=t.fontFamily,e=t.fontSize,n=t.fontId,l=t.fontWeight,i=t.fontStyle,r=t.characters;penpot.ui.sendMessage({type:"text",text:r,fontFamily:s,fontSize:e,fontId:n,fontWeight:l,fontStyle:i,width:t.width,height:t.height,lineHeight:t.lineHeight,letterSpacing:t.letterSpacing,textTransform:t.textTransform,textColor:t.fills!=="mixed"?t.fills[0].fillColor:"#000000",textDecoration:t.textDecoration})}},{shapeId:penpot.selection.length>0?penpot.selection[0].id:null});