PPT2.vue 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794
  1. <!--
  2. * @Author: LiZhiWei
  3. * @Date: 2025-04-10 14:38:27
  4. * @LastEditors: LiZhiWei
  5. * @LastEditTime: 2025-04-25 08:43:54
  6. * @Description:
  7. -->
  8. <template>
  9. <div id="pptx-container" ref="pptxContainer" class="pptx-container"></div>
  10. </template>
  11. <script setup>
  12. import { ref, watch, onMounted } from "vue"
  13. const props = defineProps({
  14. pptxJson: Object,
  15. })
  16. const pptxContainer = ref(null)
  17. // 渲染幻灯片
  18. const renderSlides = () => {
  19. if (!pptxContainer.value || !props.pptxJson) return
  20. pptxContainer.value.innerHTML = ""
  21. const { size, slides } = props.pptxJson
  22. slides.forEach((slide, index) => {
  23. const slideElement = createSlideElement(size.width, size.height)
  24. applySlideBackground(slideElement, slide.fill)
  25. if (slide.layoutElements && Array.isArray(slide.layoutElements)) {
  26. processElements(slide.layoutElements, index + 1)
  27. slide.layoutElements.forEach((element) => {
  28. const el = createElementByType(element)
  29. if (!el) return
  30. slideElement.appendChild(el)
  31. })
  32. }
  33. // 渲染幻灯片元素
  34. if (slide.elements && Array.isArray(slide.elements)) {
  35. processElements(slide.elements, index + 1)
  36. slide.elements.forEach((element) => {
  37. const el = createElementByType(element)
  38. if (!el) return
  39. slideElement.appendChild(el)
  40. })
  41. }
  42. // 添加幻灯片到容器
  43. pptxContainer.value.appendChild(slideElement)
  44. })
  45. // 为所有p标签应用样式
  46. const pTags = pptxContainer.value.querySelectorAll("p")
  47. pTags.forEach((p) => {
  48. p.style.margin = "0px"
  49. p.style.padding = "0px"
  50. p.style.wordBreak = "break-word"
  51. p.style.lineHeight = "1"
  52. })
  53. const ulTags = pptxContainer.value.querySelectorAll("ul")
  54. ulTags.forEach((ul) => {
  55. ul.style.wordBreak = "break-word"
  56. ul.style.lineHeight = "1"
  57. ul.style.padding = "auto 0px"
  58. ul.style.margin = "0px"
  59. })
  60. const olTags = pptxContainer.value.querySelectorAll("ol")
  61. olTags.forEach((ol) => {
  62. ol.style.margin = "0px"
  63. ol.style.padding = "auto 0px"
  64. ol.style.wordBreak = "break-word"
  65. ol.style.lineHeight = "1"
  66. })
  67. }
  68. // 创建幻灯片元素
  69. const createSlideElement = (width, height) => {
  70. const slideElement = document.createElement("div")
  71. slideElement.className = "slide"
  72. slideElement.style.position = "relative"
  73. slideElement.style.width = width + "px"
  74. slideElement.style.height = height + "px"
  75. slideElement.style.border = "1px solid #ccc"
  76. slideElement.style.margin = "0px auto"
  77. slideElement.style.marginBottom = "20px"
  78. slideElement.style.overflow = "hidden"
  79. return slideElement
  80. }
  81. // 应用幻灯片背景
  82. const applySlideBackground = (slideElement, fill) => {
  83. if (!fill) return
  84. if (fill.type === "gradient") {
  85. const { colors, path, rot } = fill.value
  86. if (colors && colors.length >= 2) {
  87. const gradientType = path === "rect" ? "linear" : "radial"
  88. const gradientAngle =
  89. gradientType === "linear" ? (90 - (rot || 0)) % 360 : rot || 0
  90. let gradientString = `${gradientType}-gradient(`
  91. if (gradientType === "linear") {
  92. gradientString += `${gradientAngle}deg, `
  93. }
  94. colors.forEach((color, i) => {
  95. gradientString += `${color.color} ${color.pos}${
  96. i < colors.length - 1 ? ", " : ""
  97. }`
  98. })
  99. gradientString += ")"
  100. slideElement.style.background = gradientString
  101. }
  102. } else if (fill.type === "color") {
  103. slideElement.style.backgroundColor = fill.value || "#FFFFFF"
  104. } else if (fill.type === "image" && fill.value.picBase64) {
  105. // 设置背景图片
  106. slideElement.style.backgroundImage = `url(${fill.value.picBase64})`
  107. slideElement.style.backgroundSize = "cover"
  108. slideElement.style.backgroundPosition = "center"
  109. slideElement.style.backgroundRepeat = "no-repeat"
  110. // 如果有背景图片的混合模式
  111. if (fill.value.blendMode) {
  112. slideElement.style.backgroundBlendMode = fill.value.blendMode
  113. }
  114. // 如果有透明度设置
  115. if (fill.value.opacity !== undefined) {
  116. slideElement.style.opacity = fill.value.opacity
  117. }
  118. }
  119. }
  120. // 根据元素类型创建DOM元素
  121. const createElementByType = (element) => {
  122. switch (element.type) {
  123. case "image":
  124. return createImageElement(element)
  125. case "video":
  126. return createVideoElement(element)
  127. case "audio":
  128. return createAudioElement(element)
  129. case "shape":
  130. return createShapeElement(element)
  131. case "table":
  132. return createTableElement(element)
  133. case "chart":
  134. return createChartElement(element)
  135. case "diagram":
  136. return createDiagramElement(element)
  137. case "math":
  138. return createMathElement(element)
  139. case "group":
  140. return createGroupElement(element)
  141. default:
  142. return createTextElement(element)
  143. }
  144. }
  145. // 创建组合元素
  146. const createGroupElement = (element) => {
  147. // 创建组合容器
  148. const groupContainer = document.createElement("div")
  149. groupContainer.className = "group-element"
  150. groupContainer.style.position = "absolute"
  151. groupContainer.style.top = element.top + "px"
  152. groupContainer.style.left = element.left + "px"
  153. groupContainer.style.width = element.width + "px"
  154. groupContainer.style.height = element.height + "px"
  155. groupContainer.style.zIndex = element.order
  156. groupContainer.style.transformOrigin = "center center"
  157. // 创建内部容器来处理子元素
  158. const innerContainer = document.createElement("div")
  159. innerContainer.style.position = "relative"
  160. innerContainer.style.width = "100%"
  161. innerContainer.style.height = "100%"
  162. // 应用旋转变换
  163. if (element.rotate) {
  164. groupContainer.style.transform = `rotate(${element.rotate}deg)`
  165. }
  166. // 应用其他样式属性
  167. if (element.opacity !== undefined) {
  168. groupContainer.style.opacity = element.opacity
  169. }
  170. // 递归渲染组内的所有子元素
  171. if (element.elements && Array.isArray(element.elements)) {
  172. element.elements.forEach((childElement) => {
  173. const childCopy = JSON.parse(JSON.stringify(childElement))
  174. const childEl = createElementByType(childCopy)
  175. if (childEl) {
  176. innerContainer.appendChild(childEl)
  177. }
  178. })
  179. }
  180. groupContainer.appendChild(innerContainer)
  181. return groupContainer
  182. }
  183. // 创建图像元素
  184. const createImageElement = (element) => {
  185. const img = document.createElement("img")
  186. img.src = element.src
  187. img.style.width = element.width + "px"
  188. img.style.height = element.height + "px"
  189. img.style.objectFit = "cover" // 保持图像的宽高比并填充容器
  190. img.style.position = "absolute"
  191. img.style.top = `${element.top}px`
  192. img.style.left = `${element.left}px`
  193. img.style.zIndex = element.order
  194. return img
  195. }
  196. // 创建视频元素
  197. const createVideoElement = (element) => {
  198. const el = document.createElement("video")
  199. el.src = element.blob
  200. el.controls = true
  201. el.style.width = element.width + "px"
  202. el.style.height = element.height + "px"
  203. el.style.position = "absolute"
  204. el.style.top = element.top + "px"
  205. el.style.left = element.left + "px"
  206. el.style.zIndex = element.order
  207. return el
  208. }
  209. // 创建音频元素
  210. const createAudioElement = (element) => {
  211. const el = document.createElement("audio")
  212. el.src = element.blob
  213. el.controls = true
  214. el.style.height = element.height + "px"
  215. el.style.position = "absolute"
  216. el.style.top = element.top + "px"
  217. el.style.left = element.left + "px"
  218. el.style.zIndex = element.order
  219. return el
  220. }
  221. // 创建形状元素
  222. const createShapeElement = (element) => {
  223. const el = document.createElement("div")
  224. el.style.position = "absolute"
  225. el.style.top = element.top + "px"
  226. el.style.left = element.left + "px"
  227. el.style.width = element.width + "px"
  228. el.style.height = element.height + "px"
  229. el.style.zIndex = element.order
  230. el.style.overflow = "visible"
  231. if (element.height === 0) {
  232. // 如果高度为0,直接在div上绘制边框
  233. el.style.borderTop = `${element.borderWidth || 1}px ${
  234. element.borderType || "solid"
  235. } ${element.borderColor || "#000"}`
  236. }
  237. // 使用SVG绘制形状
  238. const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg")
  239. svg.setAttribute("width", "100%")
  240. svg.setAttribute("height", "100%")
  241. svg.setAttribute("viewBox", `0 0 ${element.width} ${element.height}`)
  242. svg.style.overflow = "visible"
  243. const setBackground = (element, shape) => {
  244. if (element.fill && element.fill.type) {
  245. // 设置填充色
  246. if (element.fill.type === "color") {
  247. shape.setAttribute("fill", element.fill.value || "transparent")
  248. } else if (element.fill.type === "gradient") {
  249. // 渐变填充
  250. const { colors, path, rot } = element.fill.value
  251. if (colors && colors.length >= 2) {
  252. const gradientType = path === "rect" ? "linear" : "radial"
  253. const gradientAngle =
  254. gradientType === "linear" ? (90 - (rot || 0)) % 360 : rot || 0
  255. let gradientString = `${gradientType}-gradient(`
  256. if (gradientType === "linear") {
  257. gradientString += `${gradientAngle}deg, `
  258. }
  259. colors.forEach((color, i) => {
  260. gradientString += `${color.color} ${color.pos}${
  261. i < colors.length - 1 ? ", " : ""
  262. }`
  263. })
  264. gradientString += ")"
  265. // 创建渐变定义
  266. const gradientDef = document.createElementNS(
  267. "http://www.w3.org/2000/svg",
  268. "defs"
  269. )
  270. const gradientEl = document.createElementNS(
  271. "http://www.w3.org/2000/svg",
  272. gradientType === "linear" ? "linearGradient" : "radialGradient"
  273. )
  274. const gradientId = `gradient-${Date.now()}-${Math.random()
  275. .toString(36)
  276. .substr(2, 9)}`
  277. gradientEl.setAttribute("id", gradientId)
  278. // 设置渐变属性
  279. if (gradientType === "linear") {
  280. gradientEl.setAttribute(
  281. "gradientTransform",
  282. `rotate(${gradientAngle})`
  283. )
  284. }
  285. // 添加渐变色标
  286. colors.forEach((color) => {
  287. const stop = document.createElementNS(
  288. "http://www.w3.org/2000/svg",
  289. "stop"
  290. )
  291. stop.setAttribute("offset", color.pos)
  292. stop.setAttribute("stop-color", color.color)
  293. gradientEl.appendChild(stop)
  294. })
  295. gradientDef.appendChild(gradientEl)
  296. svg.appendChild(gradientDef)
  297. // 应用渐变
  298. shape.setAttribute("fill", `url(#${gradientId})`)
  299. }
  300. } else if (element.fill.type === "image") {
  301. // 创建图案填充
  302. const pattern = document.createElementNS(
  303. "http://www.w3.org/2000/svg",
  304. "pattern"
  305. )
  306. const patternId = `pattern-${Date.now()}-${Math.random()
  307. .toString(36)
  308. .substr(2, 9)}`
  309. pattern.setAttribute("id", patternId)
  310. pattern.setAttribute("patternUnits", "userSpaceOnUse")
  311. pattern.setAttribute("width", "100%")
  312. pattern.setAttribute("height", "100%")
  313. // 创建图片元素
  314. const image = document.createElementNS(
  315. "http://www.w3.org/2000/svg",
  316. "image"
  317. )
  318. image.setAttribute("width", "100%")
  319. image.setAttribute("height", "100%")
  320. image.setAttribute("preserveAspectRatio", "xMidYMid slice")
  321. image.setAttributeNS(
  322. "http://www.w3.org/1999/xlink",
  323. "href",
  324. element.fill.value.picBase64
  325. )
  326. pattern.appendChild(image)
  327. // 添加pattern到defs
  328. const defs = document.createElementNS(
  329. "http://www.w3.org/2000/svg",
  330. "defs"
  331. )
  332. defs.appendChild(pattern)
  333. svg.appendChild(defs)
  334. // 应用图案填充
  335. shape.setAttribute("fill", `url(#${patternId})`)
  336. } else {
  337. shape.setAttribute("fill", "transparent")
  338. }
  339. } else {
  340. shape.setAttribute("fill", "#ffffff00")
  341. }
  342. }
  343. // 根据形状类型创建不同的SVG元素
  344. switch (element.shapType) {
  345. case "roundRect":
  346. const radius = Math.min(element.width, element.height) * 0.1
  347. const roundRect = document.createElementNS(
  348. "http://www.w3.org/2000/svg",
  349. "rect"
  350. )
  351. roundRect.setAttribute("x", 0)
  352. roundRect.setAttribute("y", 0)
  353. roundRect.setAttribute("width", element.width)
  354. roundRect.setAttribute("height", element.height)
  355. roundRect.setAttribute("rx", radius)
  356. roundRect.setAttribute("ry", radius)
  357. // 设置填充色
  358. setBackground(element, roundRect)
  359. // 设置边框
  360. if (element.borderWidth > 0) {
  361. roundRect.setAttribute("stroke", element.borderColor || "#000")
  362. roundRect.setAttribute("stroke-width", element.borderWidth || 1)
  363. roundRect.setAttribute("stroke-linejoin", "round") // 设置连接处为圆角
  364. roundRect.setAttribute("vector-effect", "non-scaling-stroke") // 防止边框宽度变形
  365. roundRect.setAttribute("shape-rendering", "geometricPrecision") // 提高渲染精度
  366. // 处理虚线边框
  367. if (
  368. element.borderType === "dotted" ||
  369. element.borderType === "dashed"
  370. ) {
  371. if (element.borderStrokeDasharray) {
  372. roundRect.setAttribute(
  373. "stroke-dasharray",
  374. element.borderStrokeDasharray
  375. )
  376. } else if (element.borderType === "dotted") {
  377. roundRect.setAttribute("stroke-dasharray", "1, 3")
  378. } else if (element.borderType === "dashed") {
  379. roundRect.setAttribute("stroke-dasharray", "5, 5")
  380. }
  381. }
  382. }
  383. svg.appendChild(roundRect)
  384. break
  385. case "snip1Rect":
  386. // 使用path元素绘制剪去单角的矩形
  387. const snip1Rect = document.createElementNS(
  388. "http://www.w3.org/2000/svg",
  389. "path"
  390. )
  391. // 计算剪角的大小,通常为矩形较短边的20%
  392. const snipSize = Math.min(element.width, element.height) * 0.35
  393. // 绘制路径:从左上角开始,顺时针方向,右上角被剪去
  394. snip1Rect.setAttribute(
  395. "d",
  396. `M0,0 ` +
  397. `L${element.width - snipSize},0 ` +
  398. `L${element.width},${snipSize} ` +
  399. `L${element.width},${element.height} ` +
  400. `L0,${element.height} ` +
  401. `Z`
  402. )
  403. // 设置填充色
  404. setBackground(element, snip1Rect)
  405. // 设置边框
  406. if (element.borderWidth > 0) {
  407. snip1Rect.setAttribute("stroke", element.borderColor || "#000")
  408. snip1Rect.setAttribute("stroke-width", element.borderWidth || 1)
  409. // 处理虚线边框
  410. if (
  411. element.borderType === "dotted" ||
  412. element.borderType === "dashed"
  413. ) {
  414. if (element.borderStrokeDasharray) {
  415. snip1Rect.setAttribute(
  416. "stroke-dasharray",
  417. element.borderStrokeDasharray
  418. )
  419. } else if (element.borderType === "dotted") {
  420. snip1Rect.setAttribute("stroke-dasharray", "1, 3")
  421. } else if (element.borderType === "dashed") {
  422. snip1Rect.setAttribute("stroke-dasharray", "5, 5")
  423. }
  424. }
  425. }
  426. svg.appendChild(snip1Rect)
  427. break
  428. case "snip2SameRect":
  429. // 使用path元素绘制剪去四个角的矩形
  430. const snip2SameRect = document.createElementNS(
  431. "http://www.w3.org/2000/svg",
  432. "path"
  433. )
  434. // 计算剪角的大小,通常为矩形较短边的20%
  435. const snip2Size = Math.min(element.width, element.height) * 0.35
  436. // 绘制路径:从左上角开始,顺时针方向,剪去四个角
  437. snip2SameRect.setAttribute(
  438. "d",
  439. `M${snip2Size},0 ` +
  440. `L${element.width - snip2Size},0 ` +
  441. `L${element.width},${snip2Size} ` +
  442. `L${element.width},${element.height - snip2Size} ` +
  443. `L${element.width - snip2Size},${element.height} ` +
  444. `L${snip2Size},${element.height} ` +
  445. `L0,${element.height - snip2Size} ` +
  446. `L0,${snip2Size} ` +
  447. `Z`
  448. )
  449. // 设置填充色
  450. setBackground(element, snip2SameRect)
  451. // 设置边框
  452. if (element.borderWidth > 0) {
  453. snip2SameRect.setAttribute("stroke", element.borderColor || "#000")
  454. snip2SameRect.setAttribute("stroke-width", element.borderWidth || 1)
  455. // 处理虚线边框
  456. if (
  457. element.borderType === "dotted" ||
  458. element.borderType === "dashed"
  459. ) {
  460. if (element.borderStrokeDasharray) {
  461. snip2SameRect.setAttribute(
  462. "stroke-dasharray",
  463. element.borderStrokeDasharray
  464. )
  465. } else if (element.borderType === "dotted") {
  466. snip2SameRect.setAttribute("stroke-dasharray", "2, 2")
  467. snip2SameRect.setAttribute("stroke-linecap", "round")
  468. } else if (element.borderType === "dashed") {
  469. snip2SameRect.setAttribute("stroke-dasharray", "6, 3")
  470. }
  471. }
  472. // 添加这些属性以确保边框正确渲染
  473. snip2SameRect.setAttribute("vector-effect", "non-scaling-stroke")
  474. snip2SameRect.setAttribute("shape-rendering", "geometricPrecision")
  475. }
  476. svg.appendChild(snip2SameRect)
  477. break
  478. case "line":
  479. // 创建直线连接符
  480. const line = document.createElementNS(
  481. "http://www.w3.org/2000/svg",
  482. "line"
  483. )
  484. // 设置SVG的最小高度为1px
  485. const svgHeight = element.height > 0 ? element.height : 1
  486. svg.setAttribute("height", svgHeight + "px")
  487. // 设置线条的起点和终点
  488. line.setAttribute("x1", 0)
  489. line.setAttribute("y1", 0)
  490. line.setAttribute("x2", element.width)
  491. line.setAttribute("y2", svgHeight)
  492. // 确保边框颜色和宽度被正确设置
  493. line.setAttribute("stroke", element.borderColor || "#000")
  494. line.setAttribute("stroke-width", element.borderWidth || 1)
  495. // 处理虚线样式
  496. if (element.borderType === "dotted" || element.borderType === "dashed") {
  497. if (element.borderStrokeDasharray) {
  498. line.setAttribute("stroke-dasharray", element.borderStrokeDasharray)
  499. } else if (element.borderType === "dotted") {
  500. line.setAttribute("stroke-dasharray", "2, 2")
  501. line.setAttribute("stroke-linecap", "round") // 添加圆角端点使点线更明显
  502. } else if (element.borderType === "dashed") {
  503. line.setAttribute("stroke-dasharray", "6, 3")
  504. }
  505. }
  506. // 确保线条正确渲染
  507. line.setAttribute("vector-effect", "non-scaling-stroke") // 防止缩放影响线条宽度
  508. svg.appendChild(line)
  509. break
  510. case "snip2DiagRect":
  511. // 使用path元素绘制剪去右上角和左下角的矩形
  512. const snip2DiagRect = document.createElementNS(
  513. "http://www.w3.org/2000/svg",
  514. "path"
  515. )
  516. // 计算剪角的大小,通常为矩形较短边的35%
  517. const snipDiagSize = Math.min(element.width, element.height) * 0.35
  518. // 绘制路径:从左上角开始,顺时针方向,右上角和左下角被剪去
  519. snip2DiagRect.setAttribute(
  520. "d",
  521. `M0,0 ` +
  522. `L${element.width - snipDiagSize},0 ` +
  523. `L${element.width},${snipDiagSize} ` +
  524. `L${element.width},${element.height} ` +
  525. `L${snipDiagSize},${element.height} ` +
  526. `L0,${element.height - snipDiagSize} ` +
  527. `Z`
  528. )
  529. // 设置填充色
  530. setBackground(element, snip2DiagRect)
  531. // 设置边框
  532. if (element.borderWidth > 0) {
  533. snip2DiagRect.setAttribute("stroke", element.borderColor || "#000")
  534. snip2DiagRect.setAttribute("stroke-width", element.borderWidth || 1)
  535. // 处理虚线边框
  536. if (
  537. element.borderType === "dotted" ||
  538. element.borderType === "dashed"
  539. ) {
  540. if (element.borderStrokeDasharray) {
  541. snip2DiagRect.setAttribute(
  542. "stroke-dasharray",
  543. element.borderStrokeDasharray
  544. )
  545. } else if (element.borderType === "dotted") {
  546. snip2DiagRect.setAttribute("stroke-dasharray", "2, 2")
  547. snip2DiagRect.setAttribute("stroke-linecap", "round")
  548. } else if (element.borderType === "dashed") {
  549. snip2DiagRect.setAttribute("stroke-dasharray", "6, 3")
  550. }
  551. }
  552. // 添加这些属性以确保边框正确渲染
  553. snip2DiagRect.setAttribute("vector-effect", "non-scaling-stroke")
  554. snip2DiagRect.setAttribute("shape-rendering", "geometricPrecision")
  555. }
  556. svg.appendChild(snip2DiagRect)
  557. break
  558. case "snipRoundRect":
  559. const snipRoundRect = document.createElementNS(
  560. "http://www.w3.org/2000/svg",
  561. "path"
  562. )
  563. // 计算剪切大小和圆角大小
  564. const snipSize2 = Math.min(element.width, element.height) * 0.4
  565. const roundSize = Math.min(element.width, element.height) * 0.5
  566. // 绘制路径:从左上角开始,顺时针方向,左上为圆角,右上为剪切角
  567. snipRoundRect.setAttribute(
  568. "d",
  569. `M${roundSize},0 ` +
  570. `L${element.width - snipSize2},0 ` +
  571. `L${element.width},${snipSize2} ` +
  572. `L${element.width},${element.height} ` +
  573. `L0,${element.height} ` +
  574. `L0,${roundSize} ` +
  575. `A${roundSize},${roundSize} 0 0,1 ${roundSize},0 ` +
  576. `Z`
  577. )
  578. // 设置填充色
  579. setBackground(element, snipRoundRect)
  580. // 设置边框
  581. if (element.borderWidth > 0) {
  582. snipRoundRect.setAttribute("stroke", element.borderColor || "#000")
  583. snipRoundRect.setAttribute("stroke-width", element.borderWidth || 1)
  584. // 处理虚线边框
  585. if (
  586. element.borderType === "dotted" ||
  587. element.borderType === "dashed"
  588. ) {
  589. if (element.borderStrokeDasharray) {
  590. snipRoundRect.setAttribute(
  591. "stroke-dasharray",
  592. element.borderStrokeDasharray
  593. )
  594. } else if (element.borderType === "dotted") {
  595. snipRoundRect.setAttribute("stroke-dasharray", "2, 2")
  596. snipRoundRect.setAttribute("stroke-linecap", "round")
  597. } else if (element.borderType === "dashed") {
  598. snipRoundRect.setAttribute("stroke-dasharray", "6, 3")
  599. }
  600. }
  601. snipRoundRect.setAttribute("vector-effect", "non-scaling-stroke")
  602. snipRoundRect.setAttribute("shape-rendering", "geometricPrecision")
  603. }
  604. svg.appendChild(snipRoundRect)
  605. break
  606. case "round1Rect":
  607. const round1Rect = document.createElementNS(
  608. "http://www.w3.org/2000/svg",
  609. "path"
  610. )
  611. // 计算圆角大小
  612. const round1Size = Math.min(element.width, element.height) * 0.5
  613. // 绘制路径:从左上角开始,顺时针方向,右上角为圆角
  614. round1Rect.setAttribute(
  615. "d",
  616. `M0,0 ` +
  617. `L${element.width - round1Size},0 ` +
  618. `Q${element.width},0 ${element.width},${round1Size} ` +
  619. `L${element.width},${element.height} ` +
  620. `L0,${element.height} ` +
  621. `Z`
  622. )
  623. // 设置填充色
  624. setBackground(element, round1Rect)
  625. // 设置边框
  626. if (element.borderWidth > 0) {
  627. round1Rect.setAttribute("stroke", element.borderColor || "#000")
  628. round1Rect.setAttribute("stroke-width", element.borderWidth || 1)
  629. // 处理虚线边框
  630. if (
  631. element.borderType === "dotted" ||
  632. element.borderType === "dashed"
  633. ) {
  634. if (element.borderStrokeDasharray) {
  635. round1Rect.setAttribute("stroke-dasharray", "40,15,5,15")
  636. } else if (element.borderType === "dotted") {
  637. round1Rect.setAttribute("stroke-dasharray", "2, 2")
  638. round1Rect.setAttribute("stroke-linecap", "round")
  639. } else if (element.borderType === "dashed") {
  640. round1Rect.setAttribute("stroke-dasharray", "6, 3")
  641. }
  642. }
  643. round1Rect.setAttribute("vector-effect", "non-scaling-stroke")
  644. round1Rect.setAttribute("shape-rendering", "geometricPrecision")
  645. }
  646. svg.appendChild(round1Rect)
  647. break
  648. case "ellipse":
  649. case "circle":
  650. const ellipse = document.createElementNS(
  651. "http://www.w3.org/2000/svg",
  652. "ellipse"
  653. )
  654. ellipse.setAttribute("cx", element.width / 2)
  655. ellipse.setAttribute("cy", element.height / 2)
  656. ellipse.setAttribute("rx", element.width / 2)
  657. ellipse.setAttribute("ry", element.height / 2)
  658. // 设置填充色
  659. setBackground(element, ellipse)
  660. // 设置边框
  661. if (element.borderWidth > 0) {
  662. ellipse.setAttribute("stroke", element.borderColor || "#000")
  663. ellipse.setAttribute("stroke-width", element.borderWidth || 1)
  664. // 处理虚线边框
  665. if (
  666. element.borderType === "dotted" ||
  667. element.borderType === "dashed"
  668. ) {
  669. if (element.borderStrokeDasharray) {
  670. ellipse.setAttribute(
  671. "stroke-dasharray",
  672. element.borderStrokeDasharray
  673. )
  674. } else if (element.borderType === "dotted") {
  675. ellipse.setAttribute("stroke-dasharray", "1, 3")
  676. } else if (element.borderType === "dashed") {
  677. ellipse.setAttribute("stroke-dasharray", "5, 5")
  678. }
  679. }
  680. }
  681. svg.appendChild(ellipse)
  682. break
  683. case "round2SameRect":
  684. const round2SameRect = document.createElementNS(
  685. "http://www.w3.org/2000/svg",
  686. "path"
  687. )
  688. // 计算圆角大小
  689. const round2Size = Math.min(element.width, element.height) * 0.5
  690. // 绘制路径:从左上角开始,顺时针方向,左上和右上为圆角
  691. round2SameRect.setAttribute(
  692. "d",
  693. `M${round2Size},0 ` +
  694. `L${element.width - round2Size},0 ` +
  695. `Q${element.width},0 ${element.width},${round2Size} ` +
  696. `L${element.width},${element.height} ` +
  697. `L0,${element.height} ` +
  698. `L0,${round2Size} ` +
  699. `Q0,0 ${round2Size},0 ` +
  700. `Z`
  701. )
  702. // 设置填充色
  703. setBackground(element, round2SameRect)
  704. // 设置边框
  705. if (element.borderWidth > 0) {
  706. round2SameRect.setAttribute("stroke", element.borderColor || "#000")
  707. round2SameRect.setAttribute("stroke-width", element.borderWidth || 1)
  708. // 处理虚线边框
  709. if (
  710. element.borderType === "dotted" ||
  711. element.borderType === "dashed"
  712. ) {
  713. if (element.borderStrokeDasharray) {
  714. round2SameRect.setAttribute(
  715. "stroke-dasharray",
  716. element.borderStrokeDasharray
  717. )
  718. } else if (element.borderType === "dotted") {
  719. round2SameRect.setAttribute("stroke-dasharray", "2, 2")
  720. round2SameRect.setAttribute("stroke-linecap", "round")
  721. } else if (element.borderType === "dashed") {
  722. round2SameRect.setAttribute("stroke-dasharray", "6, 3")
  723. }
  724. }
  725. round2SameRect.setAttribute("vector-effect", "non-scaling-stroke")
  726. round2SameRect.setAttribute("shape-rendering", "geometricPrecision")
  727. }
  728. svg.appendChild(round2SameRect)
  729. break
  730. case "round2DiagRect":
  731. const round2DiagRect = document.createElementNS(
  732. "http://www.w3.org/2000/svg",
  733. "path"
  734. )
  735. // 计算圆角大小
  736. const round2DiagSize = Math.min(element.width, element.height) * 0.5
  737. // 绘制路径:从左上角开始,顺时针方向,左上和右下为圆角
  738. round2DiagRect.setAttribute(
  739. "d",
  740. `M${round2DiagSize},0 ` +
  741. `L${element.width},0 ` +
  742. `L${element.width},${element.height - round2DiagSize} ` +
  743. `Q${element.width},${element.height} ${
  744. element.width - round2DiagSize
  745. },${element.height} ` +
  746. `L0,${element.height} ` +
  747. `L0,${round2DiagSize} ` +
  748. `Q0,0 ${round2DiagSize},0 ` +
  749. `Z`
  750. )
  751. // 设置填充色
  752. setBackground(element, round2DiagRect)
  753. // 设置边框
  754. if (element.borderWidth > 0) {
  755. round2DiagRect.setAttribute("stroke", element.borderColor || "#000")
  756. round2DiagRect.setAttribute("stroke-width", element.borderWidth || 1)
  757. // 处理虚线边框
  758. if (
  759. element.borderType === "dotted" ||
  760. element.borderType === "dashed"
  761. ) {
  762. if (element.borderStrokeDasharray) {
  763. round2DiagRect.setAttribute(
  764. "stroke-dasharray",
  765. element.borderStrokeDasharray
  766. )
  767. } else if (element.borderType === "dotted") {
  768. round2DiagRect.setAttribute("stroke-dasharray", "2, 2")
  769. round2DiagRect.setAttribute("stroke-linecap", "round")
  770. } else if (element.borderType === "dashed") {
  771. round2DiagRect.setAttribute("stroke-dasharray", "6, 3")
  772. }
  773. }
  774. round2DiagRect.setAttribute("vector-effect", "non-scaling-stroke")
  775. round2DiagRect.setAttribute("shape-rendering", "geometricPrecision")
  776. }
  777. svg.appendChild(round2DiagRect)
  778. break
  779. case "triangle":
  780. const triangle = document.createElementNS(
  781. "http://www.w3.org/2000/svg",
  782. "polygon"
  783. )
  784. triangle.setAttribute(
  785. "points",
  786. `${element.width / 2},0 ${element.width},${element.height} 0,${
  787. element.height
  788. }`
  789. )
  790. // 设置填充色
  791. setBackground(element, triangle)
  792. // 设置边框
  793. if (element.borderWidth > 0) {
  794. triangle.setAttribute("stroke", element.borderColor || "#000")
  795. triangle.setAttribute("stroke-width", element.borderWidth || 1)
  796. // 处理虚线边框
  797. if (
  798. element.borderType === "dotted" ||
  799. element.borderType === "dashed"
  800. ) {
  801. if (element.borderStrokeDasharray) {
  802. triangle.setAttribute(
  803. "stroke-dasharray",
  804. element.borderStrokeDasharray
  805. )
  806. } else if (element.borderType === "dotted") {
  807. triangle.setAttribute("stroke-dasharray", "1, 3")
  808. } else if (element.borderType === "dashed") {
  809. triangle.setAttribute("stroke-dasharray", "5, 5")
  810. }
  811. }
  812. }
  813. svg.appendChild(triangle)
  814. break
  815. case "arc":
  816. const arc = document.createElementNS("http://www.w3.org/2000/svg", "path")
  817. // 计算弧形的参数
  818. const arcRadius = Math.min(element.width, element.height) / 2
  819. // 绘制弧形路径
  820. arc.setAttribute(
  821. "d",
  822. `M${arcRadius},0 ` +
  823. `A${arcRadius},${arcRadius} 0 1 1 ${arcRadius * 0.15},${
  824. arcRadius * 0.5
  825. }`
  826. )
  827. // 设置填充色
  828. arc.setAttribute("fill", "transparent")
  829. // 设置边框
  830. if (element.borderWidth > 0) {
  831. arc.setAttribute("stroke", element.borderColor || "#000")
  832. arc.setAttribute("stroke-width", element.borderWidth || 0.5)
  833. // 处理虚线边框
  834. if (
  835. element.borderType === "dotted" ||
  836. element.borderType === "dashed"
  837. ) {
  838. if (element.borderStrokeDasharray) {
  839. arc.setAttribute("stroke-dasharray", element.borderStrokeDasharray)
  840. } else if (element.borderType === "dotted") {
  841. arc.setAttribute("stroke-dasharray", "2, 2")
  842. arc.setAttribute("stroke-linecap", "round")
  843. } else if (element.borderType === "dashed") {
  844. arc.setAttribute("stroke-dasharray", "6, 3")
  845. }
  846. }
  847. arc.setAttribute("vector-effect", "non-scaling-stroke")
  848. arc.setAttribute("shape-rendering", "geometricPrecision")
  849. } else {
  850. arc.setAttribute("stroke", element.borderColor || "#000")
  851. arc.setAttribute("stroke-width", 0.5)
  852. }
  853. svg.appendChild(arc)
  854. break
  855. case "custom":
  856. const customPath = document.createElementNS(
  857. "http://www.w3.org/2000/svg",
  858. "path"
  859. )
  860. // 直接使用传入的 path 数据
  861. customPath.setAttribute("d", element.path || "")
  862. // 设置填充色
  863. setBackground(element, customPath)
  864. // 设置边框
  865. if (element.borderWidth > 0) {
  866. customPath.setAttribute("stroke", element.borderColor || "#000")
  867. customPath.setAttribute("stroke-width", element.borderWidth || 1)
  868. // 处理虚线边框
  869. if (
  870. element.borderType === "dotted" ||
  871. element.borderType === "dashed"
  872. ) {
  873. if (element.borderStrokeDasharray) {
  874. customPath.setAttribute(
  875. "stroke-dasharray",
  876. element.borderStrokeDasharray
  877. )
  878. } else if (element.borderType === "dotted") {
  879. customPath.setAttribute("stroke-dasharray", "2, 2")
  880. customPath.setAttribute("stroke-linecap", "round")
  881. } else if (element.borderType === "dashed") {
  882. customPath.setAttribute("stroke-dasharray", "6, 3")
  883. }
  884. }
  885. customPath.setAttribute("vector-effect", "non-scaling-stroke")
  886. customPath.setAttribute("shape-rendering", "geometricPrecision")
  887. }
  888. svg.appendChild(customPath)
  889. break
  890. case "teardrop":
  891. const teardrop = document.createElementNS(
  892. "http://www.w3.org/2000/svg",
  893. "path"
  894. )
  895. // 计算泪珠形状的参数
  896. const tdCenterX = element.width / 2
  897. const tdCenterY = element.height / 2
  898. const tdRadiusX = element.width / 2
  899. const tdRadiusY = element.height / 2
  900. // 使用相对比例计算控制点
  901. teardrop.setAttribute(
  902. "d",
  903. `M${tdCenterX * 2} ${tdCenterY} ` +
  904. `A ${tdRadiusX} ${tdRadiusY} 0 1 1 ${tdCenterX} 0 ` +
  905. `Q${tdCenterX * 1.7},0 ${tdCenterX * 2.1},${-tdCenterY * 0.1} ` +
  906. `Q${tdCenterX * 2},${tdCenterY * 0.29} ${tdCenterX * 2},${tdCenterY}`
  907. )
  908. // 设置填充色
  909. setBackground(element, teardrop)
  910. // 设置边框样式
  911. if (element.borderWidth > 0) {
  912. teardrop.setAttribute("stroke", element.borderColor || "#000")
  913. teardrop.setAttribute("stroke-width", element.borderWidth || 1)
  914. teardrop.setAttribute("stroke-linejoin", "round")
  915. // 处理虚线边框
  916. if (
  917. element.borderType === "dotted" ||
  918. element.borderType === "dashed"
  919. ) {
  920. // ... 原有的虚线边框处理代码 ...
  921. }
  922. }
  923. svg.appendChild(teardrop)
  924. break
  925. case "decagon":
  926. const decagon = document.createElementNS(
  927. "http://www.w3.org/2000/svg",
  928. "polygon"
  929. )
  930. // 计算十边形的顶点
  931. let decagonPoints = ""
  932. for (let i = 0; i < 10; i++) {
  933. const angle = (i * 2 * Math.PI) / 10 - Math.PI / 2 // 从顶部开始
  934. const x = element.width / 2 + (element.width / 2) * Math.cos(angle)
  935. const y = element.height / 2 + (element.height / 2) * Math.sin(angle)
  936. decagonPoints += `${x},${y} `
  937. }
  938. decagon.setAttribute("points", decagonPoints.trim())
  939. // 设置填充色
  940. setBackground(element, decagon)
  941. // 设置边框
  942. if (element.borderWidth > 0) {
  943. decagon.setAttribute("stroke", element.borderColor || "#000")
  944. decagon.setAttribute("stroke-width", element.borderWidth || 1)
  945. // 处理虚线边框
  946. if (
  947. element.borderType === "dotted" ||
  948. element.borderType === "dashed"
  949. ) {
  950. if (element.borderStrokeDasharray) {
  951. decagon.setAttribute(
  952. "stroke-dasharray",
  953. element.borderStrokeDasharray
  954. )
  955. } else if (element.borderType === "dotted") {
  956. decagon.setAttribute("stroke-dasharray", "1, 3")
  957. } else if (element.borderType === "dashed") {
  958. decagon.setAttribute("stroke-dasharray", "5, 5")
  959. }
  960. }
  961. }
  962. svg.appendChild(decagon)
  963. break
  964. case "pentagon":
  965. const pentagon = document.createElementNS(
  966. "http://www.w3.org/2000/svg",
  967. "polygon"
  968. )
  969. pentagon.setAttribute(
  970. "points",
  971. `${element.width / 2},0 ${element.width},${element.height * 0.38} ` +
  972. `${element.width * 0.82},${element.height} ${element.width * 0.18},${
  973. element.height
  974. } ` +
  975. `0,${element.height * 0.38}`
  976. )
  977. // 设置填充色
  978. setBackground(element, pentagon)
  979. // 设置边框
  980. if (element.borderWidth > 0) {
  981. pentagon.setAttribute("stroke", element.borderColor || "#000")
  982. pentagon.setAttribute("stroke-width", element.borderWidth || 1)
  983. // 处理虚线边框
  984. if (
  985. element.borderType === "dotted" ||
  986. element.borderType === "dashed"
  987. ) {
  988. if (element.borderStrokeDasharray) {
  989. pentagon.setAttribute(
  990. "stroke-dasharray",
  991. element.borderStrokeDasharray
  992. )
  993. } else if (element.borderType === "dotted") {
  994. pentagon.setAttribute("stroke-dasharray", "1, 3")
  995. } else if (element.borderType === "dashed") {
  996. pentagon.setAttribute("stroke-dasharray", "5, 5")
  997. }
  998. }
  999. }
  1000. svg.appendChild(pentagon)
  1001. break
  1002. case "pie":
  1003. const pie = document.createElementNS("http://www.w3.org/2000/svg", "path")
  1004. // 使用提供的路径数据,但保留元素的宽高比例
  1005. const centerX = element.width / 2
  1006. const centerY = element.height / 2
  1007. const radius2 = Math.min(element.width, element.height) / 2
  1008. // 绘制饼图,使用类似提供的路径数据
  1009. pie.setAttribute(
  1010. "d",
  1011. `M${centerX},${centerY} ` +
  1012. `L${centerX * 1.85},${centerY * 1.52} ` +
  1013. `A${radius2},${radius2} 0 1,1 ${centerX * 1.56},${centerY * 0.17} ` +
  1014. `Z`
  1015. )
  1016. // 设置填充色
  1017. setBackground(element, pie)
  1018. // 设置边框
  1019. if (element.borderWidth > 0) {
  1020. pie.setAttribute("stroke", element.borderColor || "#000")
  1021. pie.setAttribute("stroke-width", element.borderWidth || 1)
  1022. // 处理虚线边框
  1023. if (
  1024. element.borderType === "dotted" ||
  1025. element.borderType === "dashed"
  1026. ) {
  1027. if (element.borderStrokeDasharray) {
  1028. pie.setAttribute("stroke-dasharray", element.borderStrokeDasharray)
  1029. } else if (element.borderType === "dotted") {
  1030. pie.setAttribute("stroke-dasharray", "2, 2")
  1031. pie.setAttribute("stroke-linecap", "round")
  1032. } else if (element.borderType === "dashed") {
  1033. pie.setAttribute("stroke-dasharray", "6, 3")
  1034. }
  1035. }
  1036. pie.setAttribute("vector-effect", "non-scaling-stroke")
  1037. pie.setAttribute("shape-rendering", "geometricPrecision")
  1038. }
  1039. svg.appendChild(pie)
  1040. break
  1041. case "chord":
  1042. const chord = document.createElementNS(
  1043. "http://www.w3.org/2000/svg",
  1044. "path"
  1045. )
  1046. // 计算弦形的参数
  1047. const chordRadiusX = element.width * 0.4
  1048. const chordRadiusY = element.height * 0.4
  1049. // 绘制弦形 - 使用更大的圆弧和不同的起始/结束点
  1050. chord.setAttribute(
  1051. "d",
  1052. `M${element.width * 0.4},${element.height * 0.9} ` +
  1053. `A ${chordRadiusX} ${chordRadiusY} 0 1 1 ${element.width * 0.8},${
  1054. element.height * 0.5
  1055. } ` +
  1056. `Z`
  1057. )
  1058. // 设置填充色
  1059. setBackground(element, chord)
  1060. // 设置边框
  1061. if (element.borderWidth > 0) {
  1062. chord.setAttribute("stroke", element.borderColor || "#000")
  1063. chord.setAttribute("stroke-width", element.borderWidth || 1)
  1064. // 处理虚线边框
  1065. if (
  1066. element.borderType === "dotted" ||
  1067. element.borderType === "dashed"
  1068. ) {
  1069. if (element.borderStrokeDasharray) {
  1070. chord.setAttribute(
  1071. "stroke-dasharray",
  1072. element.borderStrokeDasharray
  1073. )
  1074. } else if (element.borderType === "dotted") {
  1075. chord.setAttribute("stroke-dasharray", "2, 2")
  1076. chord.setAttribute("stroke-linecap", "round")
  1077. } else if (element.borderType === "dashed") {
  1078. chord.setAttribute("stroke-dasharray", "6, 3")
  1079. }
  1080. }
  1081. chord.setAttribute("vector-effect", "non-scaling-stroke")
  1082. chord.setAttribute("shape-rendering", "geometricPrecision")
  1083. }
  1084. svg.appendChild(chord)
  1085. break
  1086. case "heptagon":
  1087. const heptagon = document.createElementNS(
  1088. "http://www.w3.org/2000/svg",
  1089. "polygon"
  1090. )
  1091. // 计算七边形的顶点
  1092. let heptagonPoints = ""
  1093. for (let i = 0; i < 7; i++) {
  1094. const angle = (i * 2 * Math.PI) / 7 - Math.PI / 2 // 从顶部开始
  1095. const x = element.width / 2 + (element.width / 2) * Math.cos(angle)
  1096. const y = element.height / 2 + (element.height / 2) * Math.sin(angle)
  1097. heptagonPoints += `${x},${y} `
  1098. }
  1099. heptagon.setAttribute("points", heptagonPoints.trim())
  1100. // 设置填充色
  1101. setBackground(element, heptagon)
  1102. // 设置边框
  1103. if (element.borderWidth > 0) {
  1104. heptagon.setAttribute("stroke", element.borderColor || "#000")
  1105. heptagon.setAttribute("stroke-width", element.borderWidth || 1)
  1106. // 处理虚线边框
  1107. if (
  1108. element.borderType === "dotted" ||
  1109. element.borderType === "dashed"
  1110. ) {
  1111. if (element.borderStrokeDasharray) {
  1112. heptagon.setAttribute(
  1113. "stroke-dasharray",
  1114. element.borderStrokeDasharray
  1115. )
  1116. } else if (element.borderType === "dotted") {
  1117. heptagon.setAttribute("stroke-dasharray", "1, 3")
  1118. } else if (element.borderType === "dashed") {
  1119. heptagon.setAttribute("stroke-dasharray", "5, 5")
  1120. }
  1121. }
  1122. }
  1123. svg.appendChild(heptagon)
  1124. break
  1125. case "hexagon":
  1126. const hexagon = document.createElementNS(
  1127. "http://www.w3.org/2000/svg",
  1128. "polygon"
  1129. )
  1130. hexagon.setAttribute(
  1131. "points",
  1132. `${element.width * 0.25},0 ${element.width * 0.75},0 ${element.width},${
  1133. element.height * 0.5
  1134. } ` +
  1135. `${element.width * 0.75},${element.height} ${element.width * 0.25},${
  1136. element.height
  1137. } 0,${element.height * 0.5}`
  1138. )
  1139. // 设置填充色
  1140. setBackground(element, hexagon)
  1141. // 设置边框
  1142. if (element.borderWidth > 0) {
  1143. hexagon.setAttribute("stroke", element.borderColor || "#000")
  1144. hexagon.setAttribute("stroke-width", element.borderWidth || 1)
  1145. // 处理虚线边框
  1146. if (
  1147. element.borderType === "dotted" ||
  1148. element.borderType === "dashed"
  1149. ) {
  1150. if (element.borderStrokeDasharray) {
  1151. hexagon.setAttribute(
  1152. "stroke-dasharray",
  1153. element.borderStrokeDasharray
  1154. )
  1155. } else if (element.borderType === "dotted") {
  1156. hexagon.setAttribute("stroke-dasharray", "1, 3")
  1157. } else if (element.borderType === "dashed") {
  1158. hexagon.setAttribute("stroke-dasharray", "5, 5")
  1159. }
  1160. }
  1161. }
  1162. svg.appendChild(hexagon)
  1163. break
  1164. case "octagon":
  1165. const octagon = document.createElementNS(
  1166. "http://www.w3.org/2000/svg",
  1167. "polygon"
  1168. )
  1169. octagon.setAttribute(
  1170. "points",
  1171. `${element.width * 0.3},0 ${element.width * 0.7},0 ${element.width},${
  1172. element.height * 0.3
  1173. } ` +
  1174. `${element.width},${element.height * 0.7} ${element.width * 0.7},${
  1175. element.height
  1176. } ` +
  1177. `${element.width * 0.3},${element.height} 0,${
  1178. element.height * 0.7
  1179. } 0,${element.height * 0.3}`
  1180. )
  1181. // 设置填充色
  1182. setBackground(element, octagon)
  1183. // 设置边框
  1184. if (element.borderWidth > 0) {
  1185. octagon.setAttribute("stroke", element.borderColor || "#000")
  1186. octagon.setAttribute("stroke-width", element.borderWidth || 1)
  1187. // 处理虚线边框
  1188. if (
  1189. element.borderType === "dotted" ||
  1190. element.borderType === "dashed"
  1191. ) {
  1192. if (element.borderStrokeDasharray) {
  1193. octagon.setAttribute(
  1194. "stroke-dasharray",
  1195. element.borderStrokeDasharray
  1196. )
  1197. } else if (element.borderType === "dotted") {
  1198. octagon.setAttribute("stroke-dasharray", "1, 3")
  1199. } else if (element.borderType === "dashed") {
  1200. octagon.setAttribute("stroke-dasharray", "5, 5")
  1201. }
  1202. }
  1203. }
  1204. svg.appendChild(octagon)
  1205. break
  1206. case "trapezoid":
  1207. const trapezoid = document.createElementNS(
  1208. "http://www.w3.org/2000/svg",
  1209. "polygon"
  1210. )
  1211. trapezoid.setAttribute(
  1212. "points",
  1213. `${element.width * 0.2},0 ${element.width * 0.8},0 ${element.width},${
  1214. element.height
  1215. } 0,${element.height}`
  1216. )
  1217. // 设置填充色
  1218. setBackground(element, trapezoid)
  1219. // 设置边框
  1220. if (element.borderWidth > 0) {
  1221. trapezoid.setAttribute("stroke", element.borderColor || "#000")
  1222. trapezoid.setAttribute("stroke-width", element.borderWidth || 1)
  1223. // 处理虚线边框
  1224. if (
  1225. element.borderType === "dotted" ||
  1226. element.borderType === "dashed"
  1227. ) {
  1228. if (element.borderStrokeDasharray) {
  1229. trapezoid.setAttribute(
  1230. "stroke-dasharray",
  1231. element.borderStrokeDasharray
  1232. )
  1233. } else if (element.borderType === "dotted") {
  1234. trapezoid.setAttribute("stroke-dasharray", "1, 3")
  1235. } else if (element.borderType === "dashed") {
  1236. trapezoid.setAttribute("stroke-dasharray", "5, 5")
  1237. }
  1238. }
  1239. }
  1240. svg.appendChild(trapezoid)
  1241. break
  1242. case "diamond":
  1243. const diamond = document.createElementNS(
  1244. "http://www.w3.org/2000/svg",
  1245. "polygon"
  1246. )
  1247. diamond.setAttribute(
  1248. "points",
  1249. `${element.width / 2},0 ${element.width},${element.height / 2} ${
  1250. element.width / 2
  1251. },${element.height} 0,${element.height / 2}`
  1252. )
  1253. // 设置填充色
  1254. setBackground(element, diamond)
  1255. // 设置边框
  1256. if (element.borderWidth > 0) {
  1257. diamond.setAttribute("stroke", element.borderColor || "#000")
  1258. diamond.setAttribute("stroke-width", element.borderWidth || 1)
  1259. // 处理虚线边框
  1260. if (
  1261. element.borderType === "dotted" ||
  1262. element.borderType === "dashed"
  1263. ) {
  1264. if (element.borderStrokeDasharray) {
  1265. diamond.setAttribute(
  1266. "stroke-dasharray",
  1267. element.borderStrokeDasharray
  1268. )
  1269. } else if (element.borderType === "dotted") {
  1270. diamond.setAttribute("stroke-dasharray", "1, 3")
  1271. } else if (element.borderType === "dashed") {
  1272. diamond.setAttribute("stroke-dasharray", "5, 5")
  1273. }
  1274. }
  1275. }
  1276. svg.appendChild(diamond)
  1277. break
  1278. case "dodecagon":
  1279. const dodecagon = document.createElementNS(
  1280. "http://www.w3.org/2000/svg",
  1281. "polygon"
  1282. )
  1283. // 计算十二边形的顶点
  1284. let dodecagonPoints = ""
  1285. for (let i = 0; i < 12; i++) {
  1286. const angle = (i * 2 * Math.PI) / 12 - Math.PI / 2 // 从顶部开始
  1287. const x = element.width / 2 + (element.width / 2) * Math.cos(angle)
  1288. const y = element.height / 2 + (element.height / 2) * Math.sin(angle)
  1289. dodecagonPoints += `${x},${y} `
  1290. }
  1291. dodecagon.setAttribute("points", dodecagonPoints.trim())
  1292. // 设置填充色
  1293. setBackground(element, dodecagon)
  1294. // 设置边框
  1295. if (element.borderWidth > 0) {
  1296. dodecagon.setAttribute("stroke", element.borderColor || "#000")
  1297. dodecagon.setAttribute("stroke-width", element.borderWidth || 1)
  1298. // 处理虚线边框
  1299. if (
  1300. element.borderType === "dotted" ||
  1301. element.borderType === "dashed"
  1302. ) {
  1303. if (element.borderStrokeDasharray) {
  1304. dodecagon.setAttribute(
  1305. "stroke-dasharray",
  1306. element.borderStrokeDasharray
  1307. )
  1308. } else if (element.borderType === "dotted") {
  1309. dodecagon.setAttribute("stroke-dasharray", "1, 3")
  1310. } else if (element.borderType === "dashed") {
  1311. dodecagon.setAttribute("stroke-dasharray", "5, 5")
  1312. }
  1313. }
  1314. }
  1315. svg.appendChild(dodecagon)
  1316. break
  1317. case "halfFrame":
  1318. // 创建外框和内框
  1319. const outerRect2 = document.createElementNS(
  1320. "http://www.w3.org/2000/svg",
  1321. "path"
  1322. )
  1323. const innerRect2 = document.createElementNS(
  1324. "http://www.w3.org/2000/svg",
  1325. "path"
  1326. )
  1327. // 计算内框的边距
  1328. const frameWidth2 = element.width / 9
  1329. // 绘制外框的左边和上边
  1330. outerRect2.setAttribute(
  1331. "d",
  1332. `M0,${element.height} L0,0 L${element.width},0`
  1333. )
  1334. // 绘制内框的左边和上边,注意起点位置调整
  1335. innerRect2.setAttribute(
  1336. "d",
  1337. `M${frameWidth2},${element.height - frameWidth2} ` +
  1338. `L${frameWidth2},${frameWidth2} ` +
  1339. `L${element.width - frameWidth2},${frameWidth2}`
  1340. )
  1341. // 设置填充色
  1342. if (element.fill && element.fill.type === "color") {
  1343. // 创建一个填充用的路径
  1344. const fillPath = document.createElementNS(
  1345. "http://www.w3.org/2000/svg",
  1346. "path"
  1347. )
  1348. fillPath.setAttribute(
  1349. "d",
  1350. `M${frameWidth2},${frameWidth2} ` +
  1351. `L${element.width - frameWidth2},${frameWidth2} ` +
  1352. `L${element.width},0 ` +
  1353. `L0,0 ` +
  1354. `L0,${element.height} ` +
  1355. `L${frameWidth2},${element.height - frameWidth2} Z`
  1356. )
  1357. fillPath.setAttribute("fill", element.fill.value || "transparent")
  1358. svg.appendChild(fillPath)
  1359. outerRect2.setAttribute("fill", "none")
  1360. innerRect2.setAttribute("fill", "none")
  1361. } else {
  1362. outerRect2.setAttribute("fill", "none")
  1363. innerRect2.setAttribute("fill", "none")
  1364. }
  1365. // 设置边框
  1366. if (element.borderWidth > 0) {
  1367. const borderColor = element.borderColor || "#000"
  1368. const borderWidth = element.borderWidth || 1
  1369. outerRect2.setAttribute("stroke", borderColor)
  1370. outerRect2.setAttribute("stroke-width", borderWidth)
  1371. innerRect2.setAttribute("stroke", borderColor)
  1372. innerRect2.setAttribute("stroke-width", borderWidth)
  1373. // 处理虚线边框
  1374. if (
  1375. element.borderType === "dotted" ||
  1376. element.borderType === "dashed"
  1377. ) {
  1378. if (element.borderStrokeDasharray) {
  1379. const dashArray = element.borderStrokeDasharray
  1380. outerRect2.setAttribute("stroke-dasharray", dashArray)
  1381. innerRect2.setAttribute("stroke-dasharray", dashArray)
  1382. } else if (element.borderType === "dotted") {
  1383. outerRect2.setAttribute("stroke-dasharray", "1, 3")
  1384. innerRect2.setAttribute("stroke-dasharray", "1, 3")
  1385. } else if (element.borderType === "dashed") {
  1386. outerRect2.setAttribute("stroke-dasharray", "5, 5")
  1387. innerRect2.setAttribute("stroke-dasharray", "5, 5")
  1388. }
  1389. }
  1390. }
  1391. svg.appendChild(outerRect2)
  1392. svg.appendChild(innerRect2)
  1393. break
  1394. case "corner":
  1395. const corner = document.createElementNS(
  1396. "http://www.w3.org/2000/svg",
  1397. "path"
  1398. )
  1399. corner.setAttribute(
  1400. "d",
  1401. `M0,0 ` +
  1402. `L0,${element.height} ` +
  1403. `L${element.width},${element.height} ` +
  1404. `L${element.width},${element.height * 0.6} ` +
  1405. `L${element.width * 0.4},${element.height * 0.6} ` +
  1406. `L${element.width * 0.4},0 Z`
  1407. )
  1408. // 设置填充色
  1409. setBackground(element, corner)
  1410. // 设置边框
  1411. if (element.borderWidth > 0) {
  1412. corner.setAttribute("stroke", element.borderColor || "#000")
  1413. corner.setAttribute("stroke-width", element.borderWidth || 1)
  1414. // 处理虚线边框
  1415. if (
  1416. element.borderType === "dotted" ||
  1417. element.borderType === "dashed"
  1418. ) {
  1419. if (element.borderStrokeDasharray) {
  1420. corner.setAttribute(
  1421. "stroke-dasharray",
  1422. element.borderStrokeDasharray
  1423. )
  1424. } else if (element.borderType === "dotted") {
  1425. corner.setAttribute("stroke-dasharray", "1, 3")
  1426. } else if (element.borderType === "dashed") {
  1427. corner.setAttribute("stroke-dasharray", "5, 5")
  1428. }
  1429. }
  1430. }
  1431. svg.appendChild(corner)
  1432. break
  1433. case "diagStripe":
  1434. const diagStripe = document.createElementNS(
  1435. "http://www.w3.org/2000/svg",
  1436. "path"
  1437. )
  1438. // 绘制斜纹路径
  1439. diagStripe.setAttribute(
  1440. "d",
  1441. `M${element.width * 0.4},0 ` +
  1442. `L${element.width},0 ` +
  1443. `L0,${element.height} ` +
  1444. `L0,${element.height * 0.4} Z`
  1445. )
  1446. // 设置填充色
  1447. setBackground(element, diagStripe)
  1448. // 设置边框
  1449. if (element.borderWidth > 0) {
  1450. diagStripe.setAttribute("stroke", element.borderColor || "#000")
  1451. diagStripe.setAttribute("stroke-width", element.borderWidth || 1)
  1452. diagStripe.setAttribute("stroke-linejoin", "round")
  1453. // 处理虚线边框
  1454. if (
  1455. element.borderType === "dotted" ||
  1456. element.borderType === "dashed"
  1457. ) {
  1458. if (element.borderStrokeDasharray) {
  1459. diagStripe.setAttribute(
  1460. "stroke-dasharray",
  1461. element.borderStrokeDasharray
  1462. )
  1463. } else if (element.borderType === "dotted") {
  1464. diagStripe.setAttribute("stroke-dasharray", "1, 3")
  1465. } else if (element.borderType === "dashed") {
  1466. diagStripe.setAttribute("stroke-dasharray", "5, 5")
  1467. }
  1468. }
  1469. }
  1470. svg.appendChild(diagStripe)
  1471. break
  1472. case "plus":
  1473. const plus = document.createElementNS(
  1474. "http://www.w3.org/2000/svg",
  1475. "polygon"
  1476. )
  1477. plus.setAttribute(
  1478. "points",
  1479. `${element.width * 0.4},0 ` +
  1480. `${element.width * 0.6},0 ` +
  1481. `${element.width * 0.6},${element.height * 0.4} ` +
  1482. `${element.width},${element.height * 0.4} ` +
  1483. `${element.width},${element.height * 0.6} ` +
  1484. `${element.width * 0.6},${element.height * 0.6} ` +
  1485. `${element.width * 0.6},${element.height} ` +
  1486. `${element.width * 0.4},${element.height} ` +
  1487. `${element.width * 0.4},${element.height * 0.6} ` +
  1488. `0,${element.height * 0.6} ` +
  1489. `0,${element.height * 0.4} ` +
  1490. `${element.width * 0.4},${element.height * 0.4}`
  1491. )
  1492. // 设置填充色
  1493. setBackground(element, plus)
  1494. // 设置边框
  1495. if (element.borderWidth > 0) {
  1496. plus.setAttribute("stroke", element.borderColor || "#000")
  1497. plus.setAttribute("stroke-width", element.borderWidth || 1)
  1498. // 处理虚线边框
  1499. if (
  1500. element.borderType === "dotted" ||
  1501. element.borderType === "dashed"
  1502. ) {
  1503. if (element.borderStrokeDasharray) {
  1504. plus.setAttribute("stroke-dasharray", element.borderStrokeDasharray)
  1505. } else if (element.borderType === "dotted") {
  1506. plus.setAttribute("stroke-dasharray", "1, 3")
  1507. } else if (element.borderType === "dashed") {
  1508. plus.setAttribute("stroke-dasharray", "5, 5")
  1509. }
  1510. }
  1511. }
  1512. svg.appendChild(plus)
  1513. break
  1514. case "can":
  1515. const can = document.createElementNS("http://www.w3.org/2000/svg", "g")
  1516. // 创建圆柱体的主体部分
  1517. const cylinderBody = document.createElementNS(
  1518. "http://www.w3.org/2000/svg",
  1519. "path"
  1520. )
  1521. cylinderBody.setAttribute(
  1522. "d",
  1523. `M0,${element.height * 0.15} ` +
  1524. `A${element.width / 2},${element.height * 0.15} 0 0 1 ${
  1525. element.width
  1526. },${element.height * 0.15} ` +
  1527. `V${element.height * 0.85} ` +
  1528. `A${element.width / 2},${element.height * 0.15} 0 0 1 0,${
  1529. element.height * 0.85
  1530. } Z`
  1531. )
  1532. // 创建顶部椭圆
  1533. const topEllipse = document.createElementNS(
  1534. "http://www.w3.org/2000/svg",
  1535. "ellipse"
  1536. )
  1537. topEllipse.setAttribute("cx", element.width / 2)
  1538. topEllipse.setAttribute("cy", element.height * 0.15)
  1539. topEllipse.setAttribute("rx", element.width / 2)
  1540. topEllipse.setAttribute("ry", element.height * 0.15)
  1541. // 设置填充色
  1542. setBackground(element, cylinderBody)
  1543. setBackground(element, topEllipse)
  1544. // 设置边框
  1545. if (element.borderWidth > 0) {
  1546. const borderColor = element.borderColor || "#000"
  1547. const borderWidth = element.borderWidth || 1
  1548. cylinderBody.setAttribute("stroke", borderColor)
  1549. cylinderBody.setAttribute("stroke-width", borderWidth)
  1550. topEllipse.setAttribute("stroke", borderColor)
  1551. topEllipse.setAttribute("stroke-width", borderWidth)
  1552. // 处理虚线边框
  1553. if (
  1554. element.borderType === "dotted" ||
  1555. element.borderType === "dashed"
  1556. ) {
  1557. if (element.borderStrokeDasharray) {
  1558. const dashArray = element.borderStrokeDasharray
  1559. cylinderBody.setAttribute("stroke-dasharray", dashArray)
  1560. topEllipse.setAttribute("stroke-dasharray", dashArray)
  1561. } else if (element.borderType === "dotted") {
  1562. cylinderBody.setAttribute("stroke-dasharray", "1, 3")
  1563. topEllipse.setAttribute("stroke-dasharray", "1, 3")
  1564. } else if (element.borderType === "dashed") {
  1565. cylinderBody.setAttribute("stroke-dasharray", "5, 5")
  1566. topEllipse.setAttribute("stroke-dasharray", "5, 5")
  1567. }
  1568. }
  1569. }
  1570. can.appendChild(cylinderBody)
  1571. can.appendChild(topEllipse)
  1572. svg.appendChild(can)
  1573. break
  1574. case "cube":
  1575. const cube = document.createElementNS("http://www.w3.org/2000/svg", "g")
  1576. // 计算关键点坐标
  1577. const offset = element.width * 0.2
  1578. const frontX = offset
  1579. const frontY = offset
  1580. const frontW = element.width - offset
  1581. const frontH = element.height
  1582. // 设置立方体的三个面的路径
  1583. const frontFace = document.createElementNS(
  1584. "http://www.w3.org/2000/svg",
  1585. "path"
  1586. )
  1587. frontFace.setAttribute(
  1588. "d",
  1589. `M${frontX},${frontY} ` +
  1590. `L${frontW},${frontY} ` +
  1591. `L${frontW},${frontH} ` +
  1592. `L${frontX},${frontH} Z`
  1593. )
  1594. const rightFace = document.createElementNS(
  1595. "http://www.w3.org/2000/svg",
  1596. "path"
  1597. )
  1598. rightFace.setAttribute(
  1599. "d",
  1600. `M${frontW},${frontY} ` +
  1601. `L${element.width},0 ` +
  1602. `L${element.width},${element.height - offset} ` +
  1603. `L${frontW},${frontH} Z`
  1604. )
  1605. const topFace = document.createElementNS(
  1606. "http://www.w3.org/2000/svg",
  1607. "path"
  1608. )
  1609. topFace.setAttribute(
  1610. "d",
  1611. `M${frontX},${frontY} ` +
  1612. `L${frontW - offset - frontX},0 ` + // 修改这里:改为从(0,0)开始
  1613. `L${element.width - offset},0 ` + // 修改这里:使用 offset 来计算右上角的位置
  1614. `L${element.width},0 ` +
  1615. `L${frontW},${frontY} Z`
  1616. )
  1617. // 设置填充色
  1618. if (element.fill && element.fill.type === "color") {
  1619. const fillColor = element.fill.value || "transparent"
  1620. frontFace.setAttribute("fill", fillColor)
  1621. rightFace.setAttribute("fill", this.adjustBrightness(fillColor, 0.8))
  1622. topFace.setAttribute("fill", this.adjustBrightness(fillColor, 1.2))
  1623. } else {
  1624. frontFace.setAttribute("fill", "transparent")
  1625. rightFace.setAttribute("fill", "transparent")
  1626. topFace.setAttribute("fill", "transparent")
  1627. }
  1628. // 设置边框
  1629. if (element.borderWidth > 0) {
  1630. const borderColor = element.borderColor || "#000"
  1631. const borderWidth = element.borderWidth || 1
  1632. frontFace.setAttribute("stroke", borderColor)
  1633. frontFace.setAttribute("stroke-width", borderWidth)
  1634. rightFace.setAttribute("stroke", borderColor)
  1635. rightFace.setAttribute("stroke-width", borderWidth)
  1636. topFace.setAttribute("stroke", borderColor)
  1637. topFace.setAttribute("stroke-width", borderWidth)
  1638. if (
  1639. element.borderType === "dotted" ||
  1640. element.borderType === "dashed"
  1641. ) {
  1642. const dashArray =
  1643. element.borderStrokeDasharray ||
  1644. (element.borderType === "dotted" ? "1, 3" : "5, 5")
  1645. frontFace.setAttribute("stroke-dasharray", dashArray)
  1646. rightFace.setAttribute("stroke-dasharray", dashArray)
  1647. topFace.setAttribute("stroke-dasharray", dashArray)
  1648. }
  1649. }
  1650. // 按正确的顺序添加面(从后到前)
  1651. cube.appendChild(rightFace)
  1652. cube.appendChild(topFace)
  1653. cube.appendChild(frontFace)
  1654. svg.appendChild(cube)
  1655. break
  1656. case "bevel":
  1657. const bevel = document.createElementNS("http://www.w3.org/2000/svg", "g")
  1658. // 计算关键点坐标
  1659. const bevelOffset = element.width * 0.2
  1660. const smallRectX = bevelOffset
  1661. const smallRectY = bevelOffset
  1662. const smallRectW = element.width - 2 * bevelOffset
  1663. const smallRectH = element.height - 2 * bevelOffset
  1664. // 绘制顶部小矩形
  1665. const topRect = document.createElementNS(
  1666. "http://www.w3.org/2000/svg",
  1667. "rect"
  1668. )
  1669. topRect.setAttribute("x", smallRectX)
  1670. topRect.setAttribute("y", smallRectY)
  1671. topRect.setAttribute("width", smallRectW)
  1672. topRect.setAttribute("height", smallRectH)
  1673. // 绘制四个梯形
  1674. const topTrapezoid = document.createElementNS(
  1675. "http://www.w3.org/2000/svg",
  1676. "path"
  1677. )
  1678. topTrapezoid.setAttribute(
  1679. "d",
  1680. `M0,0 L${element.width},0 L${
  1681. element.width - bevelOffset
  1682. },${bevelOffset} L${bevelOffset},${bevelOffset} Z`
  1683. )
  1684. const bottomTrapezoid = document.createElementNS(
  1685. "http://www.w3.org/2000/svg",
  1686. "path"
  1687. )
  1688. bottomTrapezoid.setAttribute(
  1689. "d",
  1690. `M${bevelOffset},${element.height - bevelOffset} L${
  1691. element.width - bevelOffset
  1692. },${element.height - bevelOffset} L${element.width},${
  1693. element.height
  1694. } L0,${element.height} Z`
  1695. )
  1696. const leftTrapezoid = document.createElementNS(
  1697. "http://www.w3.org/2000/svg",
  1698. "path"
  1699. )
  1700. leftTrapezoid.setAttribute(
  1701. "d",
  1702. `M0,0 L${bevelOffset},${bevelOffset} L${bevelOffset},${
  1703. element.height - bevelOffset
  1704. } L0,${element.height} Z`
  1705. )
  1706. const rightTrapezoid = document.createElementNS(
  1707. "http://www.w3.org/2000/svg",
  1708. "path"
  1709. )
  1710. rightTrapezoid.setAttribute(
  1711. "d",
  1712. `M${element.width},0 L${element.width},${element.height} L${
  1713. element.width - bevelOffset
  1714. },${element.height - bevelOffset} L${
  1715. element.width - bevelOffset
  1716. },${bevelOffset} Z`
  1717. )
  1718. // 设置填充色
  1719. if (element.fill && element.fill.type === "color") {
  1720. const fillColor = element.fill.value || "transparent"
  1721. topRect.setAttribute("fill", fillColor)
  1722. topTrapezoid.setAttribute("fill", this.adjustBrightness(fillColor, 1.2))
  1723. bottomTrapezoid.setAttribute(
  1724. "fill",
  1725. this.adjustBrightness(fillColor, 0.8)
  1726. )
  1727. leftTrapezoid.setAttribute(
  1728. "fill",
  1729. this.adjustBrightness(fillColor, 0.9)
  1730. )
  1731. rightTrapezoid.setAttribute(
  1732. "fill",
  1733. this.adjustBrightness(fillColor, 0.7)
  1734. )
  1735. } else {
  1736. topRect.setAttribute("fill", "transparent")
  1737. topTrapezoid.setAttribute("fill", "transparent")
  1738. bottomTrapezoid.setAttribute("fill", "transparent")
  1739. leftTrapezoid.setAttribute("fill", "transparent")
  1740. rightTrapezoid.setAttribute("fill", "transparent")
  1741. }
  1742. // 设置边框
  1743. if (element.borderWidth > 0) {
  1744. const borderColor = element.borderColor || "#000"
  1745. const borderWidth = element.borderWidth || 1
  1746. const parts = [
  1747. topRect,
  1748. topTrapezoid,
  1749. bottomTrapezoid,
  1750. leftTrapezoid,
  1751. rightTrapezoid,
  1752. ]
  1753. parts.forEach((part) => {
  1754. part.setAttribute("stroke", borderColor)
  1755. part.setAttribute("stroke-width", borderWidth)
  1756. if (
  1757. element.borderType === "dotted" ||
  1758. element.borderType === "dashed"
  1759. ) {
  1760. const dashArray =
  1761. element.borderStrokeDasharray ||
  1762. (element.borderType === "dotted" ? "1, 3" : "5, 5")
  1763. part.setAttribute("stroke-dasharray", dashArray)
  1764. }
  1765. })
  1766. }
  1767. // 按正确的顺序添加面(从后到前)
  1768. bevel.appendChild(bottomTrapezoid)
  1769. bevel.appendChild(leftTrapezoid)
  1770. bevel.appendChild(rightTrapezoid)
  1771. bevel.appendChild(topTrapezoid)
  1772. bevel.appendChild(topRect)
  1773. svg.appendChild(bevel)
  1774. break
  1775. case "donut":
  1776. const donut = document.createElementNS("http://www.w3.org/2000/svg", "g")
  1777. // 外圆
  1778. const outerCircle = document.createElementNS(
  1779. "http://www.w3.org/2000/svg",
  1780. "circle"
  1781. )
  1782. outerCircle.setAttribute("cx", element.width / 2)
  1783. outerCircle.setAttribute("cy", element.height / 2)
  1784. outerCircle.setAttribute("r", Math.min(element.width, element.height) / 2)
  1785. // 内圆
  1786. const innerCircle = document.createElementNS(
  1787. "http://www.w3.org/2000/svg",
  1788. "circle"
  1789. )
  1790. innerCircle.setAttribute("cx", element.width / 2)
  1791. innerCircle.setAttribute("cy", element.height / 2)
  1792. innerCircle.setAttribute("r", Math.min(element.width, element.height) / 4)
  1793. // 设置填充色
  1794. setBackground(element, outerCircle)
  1795. innerCircle.setAttribute("fill", "white")
  1796. // 设置边框
  1797. if (element.borderWidth > 0) {
  1798. const borderColor = element.borderColor || "#000"
  1799. const borderWidth = element.borderWidth || 1
  1800. outerCircle.setAttribute("stroke", borderColor)
  1801. outerCircle.setAttribute("stroke-width", borderWidth)
  1802. innerCircle.setAttribute("stroke", borderColor)
  1803. innerCircle.setAttribute("stroke-width", borderWidth)
  1804. if (
  1805. element.borderType === "dotted" ||
  1806. element.borderType === "dashed"
  1807. ) {
  1808. const dashArray =
  1809. element.borderStrokeDasharray ||
  1810. (element.borderType === "dotted" ? "1, 3" : "5, 5")
  1811. outerCircle.setAttribute("stroke-dasharray", dashArray)
  1812. innerCircle.setAttribute("stroke-dasharray", dashArray)
  1813. }
  1814. }
  1815. donut.appendChild(outerCircle)
  1816. donut.appendChild(innerCircle)
  1817. svg.appendChild(donut)
  1818. break
  1819. case "noSmoking":
  1820. const noSmoking = document.createElementNS(
  1821. "http://www.w3.org/2000/svg",
  1822. "g"
  1823. )
  1824. // 创建路径
  1825. const path = document.createElementNS(
  1826. "http://www.w3.org/2000/svg",
  1827. "path"
  1828. )
  1829. path.setAttribute(
  1830. "d",
  1831. "M0,70 A77,70 0 1,1 0,71 Z M123.80284467265982,99.39738105155146 A57.5386,50.5386 0 0 0 45.160427644134444,27.90427466198293 Z M30.197155327340184,40.602618948448544 A57.5386,50.5386 0 0 0 108.83957235586556,112.09572533801708 Z"
  1832. )
  1833. path.setAttribute("fill", "rgba(255,217,102,1)")
  1834. path.setAttribute("stroke", "rgba(23,44,81,1)")
  1835. path.setAttribute("stroke-width", "1px")
  1836. path.setAttribute("stroke-dasharray", "")
  1837. path.setAttribute("stroke-linecap", "butt")
  1838. path.setAttribute("stroke-linejoin", "round")
  1839. noSmoking.appendChild(path)
  1840. svg.appendChild(noSmoking)
  1841. break
  1842. case "rightArrow":
  1843. const rightArrow = document.createElementNS(
  1844. "http://www.w3.org/2000/svg",
  1845. "polygon"
  1846. )
  1847. rightArrow.setAttribute(
  1848. "points",
  1849. `0,${element.height * 0.3} ${element.width * 0.7},${
  1850. element.height * 0.3
  1851. } ` +
  1852. `${element.width * 0.7},0 ${element.width},${element.height * 0.5} ` +
  1853. `${element.width * 0.7},${element.height} ${element.width * 0.7},${
  1854. element.height * 0.7
  1855. } ` +
  1856. `0,${element.height * 0.7}`
  1857. )
  1858. // 设置填充色
  1859. setBackground(element, rightArrow)
  1860. // 设置边框
  1861. if (element.borderWidth > 0) {
  1862. rightArrow.setAttribute("stroke", element.borderColor || "#000")
  1863. rightArrow.setAttribute("stroke-width", element.borderWidth || 1)
  1864. // 处理虚线边框
  1865. if (
  1866. element.borderType === "dotted" ||
  1867. element.borderType === "dashed"
  1868. ) {
  1869. if (element.borderStrokeDasharray) {
  1870. rightArrow.setAttribute(
  1871. "stroke-dasharray",
  1872. element.borderStrokeDasharray
  1873. )
  1874. } else if (element.borderType === "dotted") {
  1875. rightArrow.setAttribute("stroke-dasharray", "1, 3")
  1876. } else if (element.borderType === "dashed") {
  1877. rightArrow.setAttribute("stroke-dasharray", "5, 5")
  1878. }
  1879. }
  1880. }
  1881. svg.appendChild(rightArrow)
  1882. break
  1883. case "leftArrow":
  1884. const leftArrow = document.createElementNS(
  1885. "http://www.w3.org/2000/svg",
  1886. "polygon"
  1887. )
  1888. leftArrow.setAttribute(
  1889. "points",
  1890. `${element.width},${element.height * 0.3} ${element.width * 0.3},${
  1891. element.height * 0.3
  1892. } ` +
  1893. `${element.width * 0.3},0 0,${element.height * 0.5} ` +
  1894. `${element.width * 0.3},${element.height} ${element.width * 0.3},${
  1895. element.height * 0.7
  1896. } ` +
  1897. `${element.width},${element.height * 0.7}`
  1898. )
  1899. // 设置填充色
  1900. setBackground(element, leftArrow)
  1901. // 设置边框
  1902. if (element.borderWidth > 0) {
  1903. leftArrow.setAttribute("stroke", element.borderColor || "#000")
  1904. leftArrow.setAttribute("stroke-width", element.borderWidth || 1)
  1905. // 处理虚线边框
  1906. if (
  1907. element.borderType === "dotted" ||
  1908. element.borderType === "dashed"
  1909. ) {
  1910. if (element.borderStrokeDasharray) {
  1911. leftArrow.setAttribute(
  1912. "stroke-dasharray",
  1913. element.borderStrokeDasharray
  1914. )
  1915. } else if (element.borderType === "dotted") {
  1916. leftArrow.setAttribute("stroke-dasharray", "1, 3")
  1917. } else if (element.borderType === "dashed") {
  1918. leftArrow.setAttribute("stroke-dasharray", "5, 5")
  1919. }
  1920. }
  1921. }
  1922. svg.appendChild(leftArrow)
  1923. break
  1924. case "upArrow":
  1925. const upArrow = document.createElementNS(
  1926. "http://www.w3.org/2000/svg",
  1927. "polygon"
  1928. )
  1929. upArrow.setAttribute(
  1930. "points",
  1931. `${element.width * 0.3},${element.height} ${element.width * 0.3},${
  1932. element.height * 0.3
  1933. } ` +
  1934. `0,${element.height * 0.3} ${element.width * 0.5},0 ` +
  1935. `${element.width},${element.height * 0.3} ${element.width * 0.7},${
  1936. element.height * 0.3
  1937. } ` +
  1938. `${element.width * 0.7},${element.height}`
  1939. )
  1940. // 设置填充色
  1941. setBackground(element, upArrow)
  1942. // 设置边框
  1943. if (element.borderWidth > 0) {
  1944. upArrow.setAttribute("stroke", element.borderColor || "#000")
  1945. upArrow.setAttribute("stroke-width", element.borderWidth || 1)
  1946. // 处理虚线边框
  1947. if (
  1948. element.borderType === "dotted" ||
  1949. element.borderType === "dashed"
  1950. ) {
  1951. if (element.borderStrokeDasharray) {
  1952. upArrow.setAttribute(
  1953. "stroke-dasharray",
  1954. element.borderStrokeDasharray
  1955. )
  1956. } else if (element.borderType === "dotted") {
  1957. upArrow.setAttribute("stroke-dasharray", "1, 3")
  1958. } else if (element.borderType === "dashed") {
  1959. upArrow.setAttribute("stroke-dasharray", "5, 5")
  1960. }
  1961. }
  1962. }
  1963. svg.appendChild(upArrow)
  1964. break
  1965. case "downArrow":
  1966. const downArrow = document.createElementNS(
  1967. "http://www.w3.org/2000/svg",
  1968. "polygon"
  1969. )
  1970. downArrow.setAttribute(
  1971. "points",
  1972. `${element.width * 0.3},0 ${element.width * 0.3},${
  1973. element.height * 0.7
  1974. } ` +
  1975. `0,${element.height * 0.7} ${element.width * 0.5},${
  1976. element.height
  1977. } ` +
  1978. `${element.width},${element.height * 0.7} ${element.width * 0.7},${
  1979. element.height * 0.7
  1980. } ` +
  1981. `${element.width * 0.7},0`
  1982. )
  1983. // 设置填充色
  1984. setBackground(element, downArrow)
  1985. // 设置边框
  1986. if (element.borderWidth > 0) {
  1987. downArrow.setAttribute("stroke", element.borderColor || "#000")
  1988. downArrow.setAttribute("stroke-width", element.borderWidth || 1)
  1989. // 处理虚线边框
  1990. if (
  1991. element.borderType === "dotted" ||
  1992. element.borderType === "dashed"
  1993. ) {
  1994. if (element.borderStrokeDasharray) {
  1995. downArrow.setAttribute(
  1996. "stroke-dasharray",
  1997. element.borderStrokeDasharray
  1998. )
  1999. } else if (element.borderType === "dotted") {
  2000. downArrow.setAttribute("stroke-dasharray", "1, 3")
  2001. } else if (element.borderType === "dashed") {
  2002. downArrow.setAttribute("stroke-dasharray", "5, 5")
  2003. }
  2004. }
  2005. }
  2006. svg.appendChild(downArrow)
  2007. break
  2008. case "leftRightArrow":
  2009. const leftRightArrow = document.createElementNS(
  2010. "http://www.w3.org/2000/svg",
  2011. "polygon"
  2012. )
  2013. leftRightArrow.setAttribute(
  2014. "points",
  2015. `0,${element.height * 0.5} ${element.width * 0.2},${
  2016. element.height * 0.2
  2017. } ` +
  2018. `${element.width * 0.2},${element.height * 0.4} ${
  2019. element.width * 0.8
  2020. },${element.height * 0.4} ` +
  2021. `${element.width * 0.8},${element.height * 0.2} ${element.width},${
  2022. element.height * 0.5
  2023. } ` +
  2024. `${element.width * 0.8},${element.height * 0.8} ${
  2025. element.width * 0.8
  2026. },${element.height * 0.6} ` +
  2027. `${element.width * 0.2},${element.height * 0.6} ${
  2028. element.width * 0.2
  2029. },${element.height * 0.8}`
  2030. )
  2031. // 设置填充色
  2032. setBackground(element, leftRightArrow)
  2033. // 设置边框
  2034. if (element.borderWidth > 0) {
  2035. leftRightArrow.setAttribute("stroke", element.borderColor || "#000")
  2036. leftRightArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2037. // 处理虚线边框
  2038. if (
  2039. element.borderType === "dotted" ||
  2040. element.borderType === "dashed"
  2041. ) {
  2042. if (element.borderStrokeDasharray) {
  2043. leftRightArrow.setAttribute(
  2044. "stroke-dasharray",
  2045. element.borderStrokeDasharray
  2046. )
  2047. } else if (element.borderType === "dotted") {
  2048. leftRightArrow.setAttribute("stroke-dasharray", "1, 3")
  2049. } else if (element.borderType === "dashed") {
  2050. leftRightArrow.setAttribute("stroke-dasharray", "5, 5")
  2051. }
  2052. }
  2053. }
  2054. svg.appendChild(leftRightArrow)
  2055. break
  2056. case "upDownArrow":
  2057. const upDownArrow = document.createElementNS(
  2058. "http://www.w3.org/2000/svg",
  2059. "polygon"
  2060. )
  2061. upDownArrow.setAttribute(
  2062. "points",
  2063. `${element.width * 0.5},0 ${element.width * 0.3},${
  2064. element.height * 0.2
  2065. } ` +
  2066. `${element.width * 0.4},${element.height * 0.2} ${
  2067. element.width * 0.4
  2068. },${element.height * 0.8} ` +
  2069. `${element.width * 0.3},${element.height * 0.8} ${
  2070. element.width * 0.5
  2071. },${element.height} ` +
  2072. `${element.width * 0.7},${element.height * 0.8} ${
  2073. element.width * 0.6
  2074. },${element.height * 0.8} ` +
  2075. `${element.width * 0.6},${element.height * 0.2} ${
  2076. element.width * 0.7
  2077. },${element.height * 0.2}`
  2078. )
  2079. // 设置填充色
  2080. setBackground(element, upDownArrow)
  2081. // 设置边框
  2082. if (element.borderWidth > 0) {
  2083. upDownArrow.setAttribute("stroke", element.borderColor || "#000")
  2084. upDownArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2085. // 处理虚线边框
  2086. if (
  2087. element.borderType === "dotted" ||
  2088. element.borderType === "dashed"
  2089. ) {
  2090. if (element.borderStrokeDasharray) {
  2091. upDownArrow.setAttribute(
  2092. "stroke-dasharray",
  2093. element.borderStrokeDasharray
  2094. )
  2095. } else if (element.borderType === "dotted") {
  2096. upDownArrow.setAttribute("stroke-dasharray", "1, 3")
  2097. } else if (element.borderType === "dashed") {
  2098. upDownArrow.setAttribute("stroke-dasharray", "5, 5")
  2099. }
  2100. }
  2101. }
  2102. svg.appendChild(upDownArrow)
  2103. break
  2104. case "quadArrow":
  2105. const quadArrow = document.createElementNS(
  2106. "http://www.w3.org/2000/svg",
  2107. "path"
  2108. )
  2109. // 计算缩放比例
  2110. const scale = Math.min(element.width / 167, element.height / 121)
  2111. const offsetX = (element.width - 167 * scale) / 2
  2112. const offsetY = (element.height - 121 * scale) / 2
  2113. // 构建路径数据
  2114. const pathData = `
  2115. M${offsetX},${60.5 * scale + offsetY}
  2116. L${27.225 * scale + offsetX},${33.275 * scale + offsetY}
  2117. L${27.225 * scale + offsetX},${46.8875 * scale + offsetY}
  2118. L${69.8875 * scale + offsetX},${46.8875 * scale + offsetY}
  2119. L${69.8875 * scale + offsetX},${27.225 * scale + offsetY}
  2120. L${56.275 * scale + offsetX},${27.225 * scale + offsetY}
  2121. L${83.5 * scale + offsetX},${offsetY}
  2122. L${110.725 * scale + offsetX},${27.225 * scale + offsetY}
  2123. L${97.1125 * scale + offsetX},${27.225 * scale + offsetY}
  2124. L${97.1125 * scale + offsetX},${46.8875 * scale + offsetY}
  2125. L${139.775 * scale + offsetX},${46.8875 * scale + offsetY}
  2126. L${139.775 * scale + offsetX},${33.275 * scale + offsetY}
  2127. L${167 * scale + offsetX},${60.5 * scale + offsetY}
  2128. L${139.775 * scale + offsetX},${87.725 * scale + offsetY}
  2129. L${139.775 * scale + offsetX},${74.1125 * scale + offsetY}
  2130. L${97.1125 * scale + offsetX},${74.1125 * scale + offsetY}
  2131. L${97.1125 * scale + offsetX},${93.775 * scale + offsetY}
  2132. L${110.725 * scale + offsetX},${93.775 * scale + offsetY}
  2133. L${83.5 * scale + offsetX},${121 * scale + offsetY}
  2134. L${56.275 * scale + offsetX},${93.775 * scale + offsetY}
  2135. L${69.8875 * scale + offsetX},${93.775 * scale + offsetY}
  2136. L${69.8875 * scale + offsetX},${74.1125 * scale + offsetY}
  2137. L${27.225 * scale + offsetX},${74.1125 * scale + offsetY}
  2138. L${27.225 * scale + offsetX},${87.725 * scale + offsetY}
  2139. Z`
  2140. quadArrow.setAttribute("d", pathData)
  2141. // 设置填充色
  2142. setBackground(element, quadArrow)
  2143. // 设置边框
  2144. if (element.borderWidth > 0) {
  2145. quadArrow.setAttribute("stroke", element.borderColor || "#000")
  2146. quadArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2147. quadArrow.setAttribute("stroke-linecap", "butt")
  2148. quadArrow.setAttribute("stroke-linejoin", "round")
  2149. // 处理虚线边框
  2150. if (
  2151. element.borderType === "dotted" ||
  2152. element.borderType === "dashed"
  2153. ) {
  2154. if (element.borderStrokeDasharray) {
  2155. quadArrow.setAttribute(
  2156. "stroke-dasharray",
  2157. element.borderStrokeDasharray
  2158. )
  2159. } else if (element.borderType === "dotted") {
  2160. quadArrow.setAttribute("stroke-dasharray", "1, 3")
  2161. } else if (element.borderType === "dashed") {
  2162. quadArrow.setAttribute("stroke-dasharray", "5, 5")
  2163. }
  2164. }
  2165. }
  2166. svg.appendChild(quadArrow)
  2167. break
  2168. case "leftRightUpArrow":
  2169. const leftRightUpArrow = document.createElementNS(
  2170. "http://www.w3.org/2000/svg",
  2171. "path"
  2172. )
  2173. // 计算缩放比例
  2174. const scale2 = Math.min(element.width / 167, element.height / 121)
  2175. const offsetX2 = (element.width - 167 * scale2) / 2
  2176. const offsetY2 = (element.height - 121 * scale2) / 2
  2177. // 构建路径数据
  2178. const pathData2 = `
  2179. M${offsetX2},${60.5 * scale2 + offsetY2}
  2180. L${27.225 * scale2 + offsetX2},${33.275 * scale2 + offsetY2}
  2181. L${27.225 * scale2 + offsetX2},${46.8875 * scale2 + offsetY2}
  2182. L${69.8875 * scale2 + offsetX2},${46.8875 * scale2 + offsetY2}
  2183. L${69.8875 * scale2 + offsetX2},${27.225 * scale2 + offsetY2}
  2184. L${56.275 * scale2 + offsetX2},${27.225 * scale2 + offsetY2}
  2185. L${83.5 * scale2 + offsetX2},${offsetY2}
  2186. L${110.725 * scale2 + offsetX2},${27.225 * scale2 + offsetY2}
  2187. L${97.1125 * scale2 + offsetX2},${27.225 * scale2 + offsetY2}
  2188. L${97.1125 * scale2 + offsetX2},${46.8875 * scale2 + offsetY2}
  2189. L${139.775 * scale2 + offsetX2},${46.8875 * scale2 + offsetY2}
  2190. L${139.775 * scale2 + offsetX2},${33.275 * scale2 + offsetY2}
  2191. L${167 * scale2 + offsetX2},${60.5 * scale2 + offsetY2}
  2192. L${139.775 * scale2 + offsetX2},${87.725 * scale2 + offsetY2}
  2193. L${139.775 * scale2 + offsetX2},${74.1125 * scale2 + offsetY2}
  2194. L${97.1125 * scale2 + offsetX2},${74.1125 * scale2 + offsetY2}
  2195. L${69.8875 * scale2 + offsetX2},${74.1125 * scale2 + offsetY2}
  2196. L${27.225 * scale2 + offsetX2},${74.1125 * scale2 + offsetY2}
  2197. L${27.225 * scale2 + offsetX2},${87.725 * scale2 + offsetY2}
  2198. Z`
  2199. leftRightUpArrow.setAttribute("d", pathData2)
  2200. // 设置填充色
  2201. setBackground(element, leftRightUpArrow)
  2202. // 设置边框
  2203. if (element.borderWidth > 0) {
  2204. leftRightUpArrow.setAttribute("stroke", element.borderColor || "#000")
  2205. leftRightUpArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2206. leftRightUpArrow.setAttribute("stroke-linecap", "butt")
  2207. leftRightUpArrow.setAttribute("stroke-linejoin", "round")
  2208. // 处理虚线边框
  2209. if (
  2210. element.borderType === "dotted" ||
  2211. element.borderType === "dashed"
  2212. ) {
  2213. if (element.borderStrokeDasharray) {
  2214. leftRightUpArrow.setAttribute(
  2215. "stroke-dasharray",
  2216. element.borderStrokeDasharray
  2217. )
  2218. } else if (element.borderType === "dotted") {
  2219. leftRightUpArrow.setAttribute("stroke-dasharray", "1, 3")
  2220. } else if (element.borderType === "dashed") {
  2221. leftRightUpArrow.setAttribute("stroke-dasharray", "5, 5")
  2222. }
  2223. }
  2224. }
  2225. svg.appendChild(leftRightUpArrow)
  2226. break
  2227. case "bentArrow":
  2228. const bentArrow = document.createElementNS(
  2229. "http://www.w3.org/2000/svg",
  2230. "path"
  2231. )
  2232. // 构建路径数据
  2233. const pathData3 = `
  2234. M0,${element.height * 0.88}
  2235. L0,${element.height * 0.495}
  2236. A${element.width * 0.385} ${element.height * 0.385} 0 0 1 ${
  2237. element.width * 0.385
  2238. } ${element.height * 0.11}
  2239. L${element.width * 0.67},${element.height * 0.11}
  2240. L${element.width * 0.67},0
  2241. L${element.width * 0.89},${element.height * 0.22}
  2242. L${element.width * 0.67},${element.height * 0.44}
  2243. L${element.width * 0.67},${element.height * 0.33}
  2244. L${element.width * 0.385},${element.height * 0.33}
  2245. A${element.width * 0.165} ${element.height * 0.165} 0 0 0 ${
  2246. element.width * 0.22
  2247. } ${element.height * 0.495}
  2248. L${element.width * 0.22},${element.height * 0.88}
  2249. Z`
  2250. bentArrow.setAttribute("d", pathData3)
  2251. // 设置填充色
  2252. setBackground(element, bentArrow)
  2253. // 设置边框
  2254. if (element.borderWidth > 0) {
  2255. bentArrow.setAttribute("stroke", element.borderColor || "#000")
  2256. bentArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2257. bentArrow.setAttribute("stroke-linecap", "butt")
  2258. bentArrow.setAttribute("stroke-linejoin", "round")
  2259. // 处理虚线边框
  2260. if (
  2261. element.borderType === "dotted" ||
  2262. element.borderType === "dashed"
  2263. ) {
  2264. if (element.borderStrokeDasharray) {
  2265. bentArrow.setAttribute(
  2266. "stroke-dasharray",
  2267. element.borderStrokeDasharray
  2268. )
  2269. } else if (element.borderType === "dotted") {
  2270. bentArrow.setAttribute("stroke-dasharray", "1, 3")
  2271. } else if (element.borderType === "dashed") {
  2272. bentArrow.setAttribute("stroke-dasharray", "5, 5")
  2273. }
  2274. }
  2275. }
  2276. svg.appendChild(bentArrow)
  2277. break
  2278. case "parallelogram":
  2279. const parallelogram = document.createElementNS(
  2280. "http://www.w3.org/2000/svg",
  2281. "polygon"
  2282. )
  2283. parallelogram.setAttribute(
  2284. "points",
  2285. `${element.width * 0.25},0 ${element.width},0 ${element.width * 0.75},${
  2286. element.height
  2287. } 0,${element.height}`
  2288. )
  2289. // 设置填充色
  2290. setBackground(element, parallelogram)
  2291. // 设置边框
  2292. if (element.borderWidth > 0) {
  2293. parallelogram.setAttribute("stroke", element.borderColor || "#000")
  2294. parallelogram.setAttribute("stroke-width", element.borderWidth || 1)
  2295. // 处理虚线边框
  2296. if (
  2297. element.borderType === "dotted" ||
  2298. element.borderType === "dashed"
  2299. ) {
  2300. if (element.borderStrokeDasharray) {
  2301. parallelogram.setAttribute(
  2302. "stroke-dasharray",
  2303. element.borderStrokeDasharray
  2304. )
  2305. } else if (element.borderType === "dotted") {
  2306. parallelogram.setAttribute("stroke-dasharray", "1, 3")
  2307. } else if (element.borderType === "dashed") {
  2308. parallelogram.setAttribute("stroke-dasharray", "5, 5")
  2309. }
  2310. }
  2311. }
  2312. svg.appendChild(parallelogram)
  2313. break
  2314. case "uturnArrow":
  2315. const uturnArrow = document.createElementNS(
  2316. "http://www.w3.org/2000/svg",
  2317. "path"
  2318. )
  2319. // 构建路径数据
  2320. const pathDataUturn = `
  2321. M0,${element.height * 0.745}
  2322. L0,${element.height * 0.179375}
  2323. A${element.width * 0.179375} ${element.height * 0.179375} 0 0 1 ${
  2324. element.width * 0.179375
  2325. },0
  2326. L${element.width * 0.179375},0
  2327. A${element.width * 0.179375} ${element.height * 0.179375} 0 0 1 ${
  2328. element.width * 0.359375
  2329. },${element.height * 0.179375}
  2330. L${element.width * 0.359375},${element.height * 0.205}
  2331. L${element.width * 0.41},${element.height * 0.205}
  2332. L${element.width * 0.3075},${element.height * 0.3075}
  2333. L${element.width * 0.205},${element.height * 0.205}
  2334. L${element.width * 0.25625},${element.height * 0.205}
  2335. L${element.width * 0.25625},${element.height * 0.179375}
  2336. A${element.width * 0.076875} ${element.height * 0.076875} 0 0 0 ${
  2337. element.width * 0.179375
  2338. },${element.height * 0.1025}
  2339. L${element.width * 0.179375},${element.height * 0.1025}
  2340. A${element.width * 0.076875} ${element.height * 0.076875} 0 0 0 ${
  2341. element.width * 0.1025
  2342. },${element.height * 0.179375}
  2343. L${element.width * 0.1025},${element.height * 0.745}
  2344. Z`
  2345. uturnArrow.setAttribute("d", pathDataUturn)
  2346. // 设置填充色
  2347. setBackground(element, uturnArrow)
  2348. // 设置边框
  2349. if (element.borderWidth > 0) {
  2350. uturnArrow.setAttribute("stroke", element.borderColor || "#000")
  2351. uturnArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2352. uturnArrow.setAttribute("stroke-linecap", "butt")
  2353. uturnArrow.setAttribute("stroke-linejoin", "round")
  2354. // 处理虚线边框
  2355. if (
  2356. element.borderType === "dotted" ||
  2357. element.borderType === "dashed"
  2358. ) {
  2359. if (element.borderStrokeDasharray) {
  2360. uturnArrow.setAttribute(
  2361. "stroke-dasharray",
  2362. element.borderStrokeDasharray
  2363. )
  2364. } else if (element.borderType === "dotted") {
  2365. uturnArrow.setAttribute("stroke-dasharray", "1, 3")
  2366. } else if (element.borderType === "dashed") {
  2367. uturnArrow.setAttribute("stroke-dasharray", "5, 5")
  2368. }
  2369. }
  2370. }
  2371. svg.appendChild(uturnArrow)
  2372. break
  2373. case "leftUpArrow":
  2374. const leftUpArrow = document.createElementNS(
  2375. "http://www.w3.org/2000/svg",
  2376. "path"
  2377. )
  2378. // 使用提供的路径数据
  2379. const pathDataLeftUp = `
  2380. M0,${element.height * 0.65}
  2381. L${element.width * 0.15},${element.height * 0.4}
  2382. L${element.width * 0.15},${element.height * 0.55}
  2383. L${element.width * 0.5},${element.height * 0.55}
  2384. L${element.width * 0.5},${element.height * 0.1}
  2385. L${element.width * 0.35},${element.height * 0.1}
  2386. L${element.width * 0.65},0
  2387. L${element.width * 0.85},${element.height * 0.1}
  2388. L${element.width * 0.75},${element.height * 0.1}
  2389. L${element.width * 0.75},${element.height * 0.85}
  2390. L${element.width * 0.15},${element.height * 0.85}
  2391. L${element.width * 0.15},${element.height * 1}
  2392. Z`
  2393. leftUpArrow.setAttribute("d", pathDataLeftUp)
  2394. // 设置填充色
  2395. setBackground(element, leftUpArrow)
  2396. // 设置边框
  2397. if (element.borderWidth > 0) {
  2398. leftUpArrow.setAttribute("stroke", element.borderColor || "#000")
  2399. leftUpArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2400. leftUpArrow.setAttribute("stroke-linecap", "butt")
  2401. leftUpArrow.setAttribute("stroke-linejoin", "round")
  2402. // 处理虚线边框
  2403. if (
  2404. element.borderType === "dotted" ||
  2405. element.borderType === "dashed"
  2406. ) {
  2407. if (element.borderStrokeDasharray) {
  2408. leftUpArrow.setAttribute(
  2409. "stroke-dasharray",
  2410. element.borderStrokeDasharray
  2411. )
  2412. } else if (element.borderType === "dotted") {
  2413. leftUpArrow.setAttribute("stroke-dasharray", "1, 3")
  2414. } else if (element.borderType === "dashed") {
  2415. leftUpArrow.setAttribute("stroke-dasharray", "5, 5")
  2416. }
  2417. }
  2418. }
  2419. svg.appendChild(leftUpArrow)
  2420. break
  2421. case "bentUpArrow":
  2422. const bentUpArrow = document.createElementNS(
  2423. "http://www.w3.org/2000/svg",
  2424. "path"
  2425. )
  2426. // 构建路径数据,去掉左侧箭头
  2427. // 使用提供的路径数据
  2428. const pathDataBentUp = `
  2429. M0,${element.height * 0.89}
  2430. L0,${element.height * 0.7}
  2431. L${element.width * 0.575},${element.height * 0.7}
  2432. L${element.width * 0.575},${element.height * 0.2}
  2433. L${element.width * 0.46},${element.height * 0.2}
  2434. L${element.width * 0.69},0
  2435. L${element.width * 0.92},${element.height * 0.2}
  2436. L${element.width * 0.805},${element.height * 0.2}
  2437. L${element.width * 0.805},${element.height * 0.89}
  2438. Z`
  2439. bentUpArrow.setAttribute("d", pathDataBentUp)
  2440. // 设置填充色
  2441. setBackground(element, bentUpArrow)
  2442. // 设置边框
  2443. if (element.borderWidth > 0) {
  2444. bentUpArrow.setAttribute("stroke", element.borderColor || "#000")
  2445. bentUpArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2446. bentUpArrow.setAttribute("stroke-linecap", "butt")
  2447. bentUpArrow.setAttribute("stroke-linejoin", "round")
  2448. // 处理虚线边框
  2449. if (
  2450. element.borderType === "dotted" ||
  2451. element.borderType === "dashed"
  2452. ) {
  2453. if (element.borderStrokeDasharray) {
  2454. bentUpArrow.setAttribute(
  2455. "stroke-dasharray",
  2456. element.borderStrokeDasharray
  2457. )
  2458. } else if (element.borderType === "dotted") {
  2459. bentUpArrow.setAttribute("stroke-dasharray", "1, 3")
  2460. } else if (element.borderType === "dashed") {
  2461. bentUpArrow.setAttribute("stroke-dasharray", "5, 5")
  2462. }
  2463. }
  2464. }
  2465. svg.appendChild(bentUpArrow)
  2466. break
  2467. case "curvedRightArrow":
  2468. const curvedRightArrow = document.createElementNS(
  2469. "http://www.w3.org/2000/svg",
  2470. "path"
  2471. )
  2472. // 使用提供的路径数据
  2473. const pathDataCurvedRight = `
  2474. M${element.width},0
  2475. A${element.width} ${element.height * 0.4} 0 0 0 0 ${
  2476. element.height * 0.4
  2477. }
  2478. L0,${element.height * 0.5}
  2479. A${element.width} ${element.height * 0.4} 0 0 1 ${element.width} ${
  2480. element.height * 0.15
  2481. }
  2482. Z
  2483. M0,${element.height * 0.4}
  2484. A${element.width} ${element.height * 0.4} 0 0 0 ${
  2485. element.width * 0.8
  2486. } ${element.height * 0.8}
  2487. L${element.width * 0.8},${element.height * 0.75}
  2488. L${element.width},${element.height * 0.875}
  2489. L${element.width * 0.8},${element.height * 1}
  2490. L${element.width * 0.8},${element.height * 0.95}
  2491. A${element.width} ${element.height * 0.4} 0 0 1 0 ${
  2492. element.height * 0.5
  2493. }
  2494. Z`
  2495. curvedRightArrow.setAttribute("d", pathDataCurvedRight)
  2496. // 设置填充色
  2497. setBackground(element, curvedRightArrow)
  2498. // 设置边框
  2499. if (element.borderWidth > 0) {
  2500. curvedRightArrow.setAttribute("stroke", element.borderColor || "#000")
  2501. curvedRightArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2502. curvedRightArrow.setAttribute("stroke-linecap", "butt")
  2503. curvedRightArrow.setAttribute("stroke-linejoin", "round")
  2504. // 处理虚线边框
  2505. if (
  2506. element.borderType === "dotted" ||
  2507. element.borderType === "dashed"
  2508. ) {
  2509. if (element.borderStrokeDasharray) {
  2510. curvedRightArrow.setAttribute(
  2511. "stroke-dasharray",
  2512. element.borderStrokeDasharray
  2513. )
  2514. } else if (element.borderType === "dotted") {
  2515. curvedRightArrow.setAttribute("stroke-dasharray", "1, 3")
  2516. } else if (element.borderType === "dashed") {
  2517. curvedRightArrow.setAttribute("stroke-dasharray", "5, 5")
  2518. }
  2519. }
  2520. }
  2521. svg.appendChild(curvedRightArrow)
  2522. break
  2523. case "curvedLeftArrow":
  2524. const curvedLeftArrow = document.createElementNS(
  2525. "http://www.w3.org/2000/svg",
  2526. "path"
  2527. )
  2528. // 定义左弧形箭头的路径数据
  2529. const pathDataCurvedLeft = `
  2530. M0,0
  2531. A${element.width} ${element.height * 0.4} 0 0 1 ${element.width} ${
  2532. element.height * 0.4
  2533. }
  2534. L${element.width},${element.height * 0.55}
  2535. A${element.width} ${element.height * 0.4} 0 0 0 0 ${
  2536. element.height * 0.15
  2537. }
  2538. Z
  2539. M0,${element.height * 0.85}
  2540. L${element.width * 0.25},${element.height * 0.65}
  2541. L${element.width * 0.25},${element.height * 0.75}
  2542. A${element.width} ${element.height * 0.4} 0 0 0 ${element.width} ${
  2543. element.height * 0.4
  2544. }
  2545. L${element.width},${element.height * 0.5}
  2546. L${element.width},${element.height * 0.5}
  2547. A${element.width} ${element.height * 0.4} 0 0 1 ${
  2548. element.width * 0.25
  2549. } ${element.height * 0.9}
  2550. L${element.width * 0.25},${element.height}
  2551. Z`
  2552. curvedLeftArrow.setAttribute("d", pathDataCurvedLeft)
  2553. // 设置填充色
  2554. setBackground(element, curvedLeftArrow)
  2555. // 设置边框
  2556. if (element.borderWidth > 0) {
  2557. curvedLeftArrow.setAttribute("stroke", element.borderColor || "#000")
  2558. curvedLeftArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2559. curvedLeftArrow.setAttribute("stroke-linecap", "butt")
  2560. curvedLeftArrow.setAttribute("stroke-linejoin", "round")
  2561. // 处理虚线边框
  2562. if (
  2563. element.borderType === "dotted" ||
  2564. element.borderType === "dashed"
  2565. ) {
  2566. if (element.borderStrokeDasharray) {
  2567. curvedLeftArrow.setAttribute(
  2568. "stroke-dasharray",
  2569. element.borderStrokeDasharray
  2570. )
  2571. } else if (element.borderType === "dotted") {
  2572. curvedLeftArrow.setAttribute("stroke-dasharray", "1, 3")
  2573. } else if (element.borderType === "dashed") {
  2574. curvedLeftArrow.setAttribute("stroke-dasharray", "5, 5")
  2575. }
  2576. }
  2577. }
  2578. svg.appendChild(curvedLeftArrow)
  2579. break
  2580. case "curvedUpArrow":
  2581. const curvedUpArrow = document.createElementNS(
  2582. "http://www.w3.org/2000/svg",
  2583. "path"
  2584. )
  2585. // 定义上弧形箭头的路径数据
  2586. const pathDataCurvedUp = `
  2587. M${element.width * 0.905},0
  2588. L${element.width * 0.81},${element.height * 0.25}
  2589. L${element.width * 0.857},${element.height * 0.25}
  2590. A${element.width * 0.428} ${element.height} 0 0 1 ${
  2591. element.width * 0.428
  2592. },${element.height}
  2593. L${element.width * 0.522},${element.height}
  2594. A${element.width * 0.428} ${element.height} 0 0 0 ${
  2595. element.width * 0.952
  2596. },${element.height * 0.25}
  2597. L${element.width},${element.height * 0.25}
  2598. Z
  2599. M${element.width * 0.094},0
  2600. L0,0
  2601. A${element.width * 0.428} ${element.height} 0 0 0 ${
  2602. element.width * 0.428
  2603. },${element.height}
  2604. L${element.width * 0.522},${element.height}
  2605. A${element.width * 0.428} ${element.height} 0 0 1 ${
  2606. element.width * 0.094
  2607. },0
  2608. Z`
  2609. curvedUpArrow.setAttribute("d", pathDataCurvedUp)
  2610. // 设置填充色
  2611. setBackground(element, curvedUpArrow)
  2612. // 设置边框
  2613. if (element.borderWidth > 0) {
  2614. curvedUpArrow.setAttribute("stroke", element.borderColor || "#000")
  2615. curvedUpArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2616. curvedUpArrow.setAttribute("stroke-linecap", "butt")
  2617. curvedUpArrow.setAttribute("stroke-linejoin", "round")
  2618. // 处理虚线边框
  2619. if (
  2620. element.borderType === "dotted" ||
  2621. element.borderType === "dashed"
  2622. ) {
  2623. if (element.borderStrokeDasharray) {
  2624. curvedUpArrow.setAttribute(
  2625. "stroke-dasharray",
  2626. element.borderStrokeDasharray
  2627. )
  2628. } else if (element.borderType === "dotted") {
  2629. curvedUpArrow.setAttribute("stroke-dasharray", "1, 3")
  2630. } else if (element.borderType === "dashed") {
  2631. curvedUpArrow.setAttribute("stroke-dasharray", "5, 5")
  2632. }
  2633. }
  2634. }
  2635. svg.appendChild(curvedUpArrow)
  2636. break
  2637. case "curvedDownArrow":
  2638. const curvedDownArrow = document.createElementNS(
  2639. "http://www.w3.org/2000/svg",
  2640. "path"
  2641. )
  2642. // 定义下弧形箭头的路径数据
  2643. const pathDataCurvedDown = `
  2644. M0,${element.height}
  2645. L${element.width * 0.16},${element.height}
  2646. A${element.width * 0.46} ${element.height} 0 0 1 ${
  2647. element.width * 0.62
  2648. },0
  2649. L${element.width * 0.46},0
  2650. A${element.width * 0.46} ${element.height} 0 0 0 0,${element.height}
  2651. Z
  2652. M${element.width},${element.height}
  2653. L${element.width * 0.84},${element.height * 0.75}
  2654. L${element.width * 0.92},${element.height * 0.75}
  2655. A${element.width * 0.46} ${element.height} 0 0 0 ${
  2656. element.width * 0.46
  2657. },0
  2658. L${element.width * 0.62},0
  2659. A${element.width * 0.46} ${element.height} 0 0 1 ${
  2660. element.width * 1.08
  2661. },${element.height * 0.75}
  2662. L${element.width * 1.16},${element.height * 0.75}
  2663. Z`
  2664. curvedDownArrow.setAttribute("d", pathDataCurvedDown)
  2665. // 设置填充色
  2666. setBackground(element, curvedDownArrow)
  2667. // 设置边框
  2668. if (element.borderWidth > 0) {
  2669. curvedDownArrow.setAttribute("stroke", element.borderColor || "#000")
  2670. curvedDownArrow.setAttribute("stroke-width", element.borderWidth || 1)
  2671. curvedDownArrow.setAttribute("stroke-linecap", "butt")
  2672. curvedDownArrow.setAttribute("stroke-linejoin", "round")
  2673. // 处理虚线边框
  2674. if (
  2675. element.borderType === "dotted" ||
  2676. element.borderType === "dashed"
  2677. ) {
  2678. if (element.borderStrokeDasharray) {
  2679. curvedDownArrow.setAttribute(
  2680. "stroke-dasharray",
  2681. element.borderStrokeDasharray
  2682. )
  2683. } else if (element.borderType === "dotted") {
  2684. curvedDownArrow.setAttribute("stroke-dasharray", "1, 3")
  2685. } else if (element.borderType === "dashed") {
  2686. curvedDownArrow.setAttribute("stroke-dasharray", "5, 5")
  2687. }
  2688. }
  2689. }
  2690. svg.appendChild(curvedDownArrow)
  2691. break
  2692. case "stripedRightArrow":
  2693. const stripedRightArrow = document.createElementNS(
  2694. "http://www.w3.org/2000/svg",
  2695. "g"
  2696. )
  2697. // 主箭头部分
  2698. const mainArrow = document.createElementNS(
  2699. "http://www.w3.org/2000/svg",
  2700. "path"
  2701. )
  2702. mainArrow.setAttribute(
  2703. "d",
  2704. `M${element.width * 0.062},${element.height * 0.25}
  2705. L${element.width * 0.8},${element.height * 0.25}
  2706. L${element.width * 0.8},0
  2707. L${element.width},${element.height * 0.5}
  2708. L${element.width * 0.8},${element.height}
  2709. L${element.width * 0.8},${element.height * 0.75}
  2710. L${element.width * 0.062},${element.height * 0.75}
  2711. Z`
  2712. )
  2713. // 第一条尾部条纹
  2714. const stripe1 = document.createElementNS(
  2715. "http://www.w3.org/2000/svg",
  2716. "path"
  2717. )
  2718. stripe1.setAttribute(
  2719. "d",
  2720. `M0,${element.height * 0.25}
  2721. L${element.width * 0.012},${element.height * 0.25}
  2722. L${element.width * 0.012},${element.height * 0.75}
  2723. L0,${element.height * 0.75}
  2724. Z`
  2725. )
  2726. // 第二条尾部条纹
  2727. const stripe2 = document.createElementNS(
  2728. "http://www.w3.org/2000/svg",
  2729. "path"
  2730. )
  2731. stripe2.setAttribute(
  2732. "d",
  2733. `M${element.width * 0.025},${element.height * 0.25}
  2734. L${element.width * 0.049},${element.height * 0.25}
  2735. L${element.width * 0.049},${element.height * 0.75}
  2736. L${element.width * 0.025},${element.height * 0.75}
  2737. Z`
  2738. )
  2739. // 设置填充色
  2740. setBackground(element, mainArrow)
  2741. setBackground(element, stripe1)
  2742. setBackground(element, stripe2)
  2743. // 设置边框
  2744. if (element.borderWidth > 0) {
  2745. ;[mainArrow, stripe1, stripe2].forEach((path) => {
  2746. path.setAttribute("stroke", element.borderColor || "#000")
  2747. path.setAttribute("stroke-width", element.borderWidth || 1)
  2748. path.setAttribute("stroke-linecap", "butt")
  2749. path.setAttribute("stroke-linejoin", "round")
  2750. // 处理虚线边框
  2751. if (
  2752. element.borderType === "dotted" ||
  2753. element.borderType === "dashed"
  2754. ) {
  2755. if (element.borderStrokeDasharray) {
  2756. path.setAttribute(
  2757. "stroke-dasharray",
  2758. element.borderStrokeDasharray
  2759. )
  2760. } else if (element.borderType === "dotted") {
  2761. path.setAttribute("stroke-dasharray", "1, 3")
  2762. } else if (element.borderType === "dashed") {
  2763. path.setAttribute("stroke-dasharray", "5, 5")
  2764. }
  2765. }
  2766. })
  2767. }
  2768. stripedRightArrow.appendChild(mainArrow)
  2769. stripedRightArrow.appendChild(stripe1)
  2770. stripedRightArrow.appendChild(stripe2)
  2771. svg.appendChild(stripedRightArrow)
  2772. break
  2773. case "rightArrowCallout":
  2774. const rightArrowCallout = document.createElementNS(
  2775. "http://www.w3.org/2000/svg",
  2776. "path"
  2777. )
  2778. rightArrowCallout.setAttribute(
  2779. "d",
  2780. `M0,0
  2781. L${element.width * 0.44},0
  2782. L${element.width * 0.44},${element.height * 0.375}
  2783. L${element.width * 0.79},${element.height * 0.375}
  2784. L${element.width * 0.79},${element.height * 0.25}
  2785. L${element.width},${element.height * 0.5}
  2786. L${element.width * 0.79},${element.height * 0.75}
  2787. L${element.width * 0.79},${element.height * 0.625}
  2788. L${element.width * 0.44},${element.height * 0.625}
  2789. L${element.width * 0.44},${element.height}
  2790. L0,${element.height}
  2791. Z`
  2792. )
  2793. // 设置填充色
  2794. setBackground(element, rightArrowCallout)
  2795. // 设置边框
  2796. if (element.borderWidth > 0) {
  2797. rightArrowCallout.setAttribute("stroke", element.borderColor || "#000")
  2798. rightArrowCallout.setAttribute("stroke-width", element.borderWidth || 1)
  2799. rightArrowCallout.setAttribute("stroke-linecap", "butt")
  2800. rightArrowCallout.setAttribute("stroke-linejoin", "round")
  2801. // 处理虚线边框
  2802. if (
  2803. element.borderType === "dotted" ||
  2804. element.borderType === "dashed"
  2805. ) {
  2806. if (element.borderStrokeDasharray) {
  2807. rightArrowCallout.setAttribute(
  2808. "stroke-dasharray",
  2809. element.borderStrokeDasharray
  2810. )
  2811. } else if (element.borderType === "dotted") {
  2812. rightArrowCallout.setAttribute("stroke-dasharray", "1, 3")
  2813. } else if (element.borderType === "dashed") {
  2814. rightArrowCallout.setAttribute("stroke-dasharray", "5, 5")
  2815. }
  2816. }
  2817. }
  2818. svg.appendChild(rightArrowCallout)
  2819. break
  2820. case "leftRightArrowCallout":
  2821. const leftRightArrowCallout = document.createElementNS(
  2822. "http://www.w3.org/2000/svg",
  2823. "path"
  2824. )
  2825. leftRightArrowCallout.setAttribute(
  2826. "d",
  2827. `M0,${element.height * 0.5}
  2828. L${element.width * 0.139},${element.height * 0.25}
  2829. L${element.width * 0.139},${element.height * 0.375}
  2830. L${element.width * 0.364},${element.height * 0.375}
  2831. L${element.width * 0.364},0
  2832. L${element.width * 0.636},0
  2833. L${element.width * 0.636},${element.height * 0.375}
  2834. L${element.width * 0.861},${element.height * 0.375}
  2835. L${element.width * 0.861},${element.height * 0.25}
  2836. L${element.width},${element.height * 0.5}
  2837. L${element.width * 0.861},${element.height * 0.75}
  2838. L${element.width * 0.861},${element.height * 0.625}
  2839. L${element.width * 0.636},${element.height * 0.625}
  2840. L${element.width * 0.636},${element.height}
  2841. L${element.width * 0.364},${element.height}
  2842. L${element.width * 0.364},${element.height * 0.625}
  2843. L${element.width * 0.139},${element.height * 0.625}
  2844. L${element.width * 0.139},${element.height * 0.75}
  2845. Z`
  2846. )
  2847. // 设置填充色
  2848. setBackground(element, leftRightArrowCallout)
  2849. // 设置边框
  2850. if (element.borderWidth > 0) {
  2851. leftRightArrowCallout.setAttribute(
  2852. "stroke",
  2853. element.borderColor || "#000"
  2854. )
  2855. leftRightArrowCallout.setAttribute(
  2856. "stroke-width",
  2857. element.borderWidth || 1
  2858. )
  2859. leftRightArrowCallout.setAttribute("stroke-linecap", "butt")
  2860. leftRightArrowCallout.setAttribute("stroke-linejoin", "round")
  2861. // 处理虚线边框
  2862. if (
  2863. element.borderType === "dotted" ||
  2864. element.borderType === "dashed"
  2865. ) {
  2866. if (element.borderStrokeDasharray) {
  2867. leftRightArrowCallout.setAttribute(
  2868. "stroke-dasharray",
  2869. element.borderStrokeDasharray
  2870. )
  2871. } else if (element.borderType === "dotted") {
  2872. leftRightArrowCallout.setAttribute("stroke-dasharray", "1, 3")
  2873. } else if (element.borderType === "dashed") {
  2874. leftRightArrowCallout.setAttribute("stroke-dasharray", "5, 5")
  2875. }
  2876. }
  2877. }
  2878. svg.appendChild(leftRightArrowCallout)
  2879. break
  2880. case "quadArrowCallout":
  2881. const quadArrowCallout = document.createElementNS(
  2882. "http://www.w3.org/2000/svg",
  2883. "path"
  2884. )
  2885. quadArrowCallout.setAttribute(
  2886. "d",
  2887. `M0,${element.height * 0.5}
  2888. L${element.width * 0.096},${element.height * 0.315}
  2889. L${element.width * 0.096},${element.height * 0.407}
  2890. L${element.width * 0.26},${element.height * 0.407}
  2891. L${element.width * 0.26},${element.height * 0.259}
  2892. L${element.width * 0.453},${element.height * 0.259}
  2893. L${element.width * 0.453},${element.height * 0.185}
  2894. L${element.width * 0.405},${element.height * 0.185}
  2895. L${element.width * 0.5},0
  2896. L${element.width * 0.595},${element.height * 0.185}
  2897. L${element.width * 0.547},${element.height * 0.185}
  2898. L${element.width * 0.547},${element.height * 0.259}
  2899. L${element.width * 0.74},${element.height * 0.259}
  2900. L${element.width * 0.74},${element.height * 0.407}
  2901. L${element.width * 0.904},${element.height * 0.407}
  2902. L${element.width * 0.904},${element.height * 0.315}
  2903. L${element.width},${element.height * 0.5}
  2904. L${element.width * 0.904},${element.height * 0.685}
  2905. L${element.width * 0.904},${element.height * 0.593}
  2906. L${element.width * 0.74},${element.height * 0.593}
  2907. L${element.width * 0.74},${element.height * 0.741}
  2908. L${element.width * 0.547},${element.height * 0.741}
  2909. L${element.width * 0.547},${element.height * 0.815}
  2910. L${element.width * 0.595},${element.height * 0.815}
  2911. L${element.width * 0.5},${element.height}
  2912. L${element.width * 0.405},${element.height * 0.815}
  2913. L${element.width * 0.453},${element.height * 0.815}
  2914. L${element.width * 0.453},${element.height * 0.741}
  2915. L${element.width * 0.26},${element.height * 0.741}
  2916. L${element.width * 0.26},${element.height * 0.593}
  2917. L${element.width * 0.096},${element.height * 0.593}
  2918. L${element.width * 0.096},${element.height * 0.685}
  2919. Z`
  2920. )
  2921. // 设置填充色
  2922. setBackground(element, quadArrowCallout)
  2923. // 设置边框
  2924. if (element.borderWidth > 0) {
  2925. quadArrowCallout.setAttribute("stroke", element.borderColor || "#000")
  2926. quadArrowCallout.setAttribute("stroke-width", element.borderWidth || 1)
  2927. quadArrowCallout.setAttribute("stroke-linecap", "butt")
  2928. quadArrowCallout.setAttribute("stroke-linejoin", "round")
  2929. // 处理虚线边框
  2930. if (
  2931. element.borderType === "dotted" ||
  2932. element.borderType === "dashed"
  2933. ) {
  2934. if (element.borderStrokeDasharray) {
  2935. quadArrowCallout.setAttribute(
  2936. "stroke-dasharray",
  2937. element.borderStrokeDasharray
  2938. )
  2939. } else if (element.borderType === "dotted") {
  2940. quadArrowCallout.setAttribute("stroke-dasharray", "1, 3")
  2941. } else if (element.borderType === "dashed") {
  2942. quadArrowCallout.setAttribute("stroke-dasharray", "5, 5")
  2943. }
  2944. }
  2945. }
  2946. svg.appendChild(quadArrowCallout)
  2947. break
  2948. case "leftArrowCallout":
  2949. const leftArrowCallout = document.createElementNS(
  2950. "http://www.w3.org/2000/svg",
  2951. "path"
  2952. )
  2953. leftArrowCallout.setAttribute(
  2954. "d",
  2955. `M0,${element.height * 0.5}
  2956. L${element.width * 0.183},${element.height * 0.25}
  2957. L${element.width * 0.183},${element.height * 0.375}
  2958. L${element.width * 0.35},${element.height * 0.375}
  2959. L${element.width * 0.35},0
  2960. L${element.width},0
  2961. L${element.width},${element.height}
  2962. L${element.width * 0.35},${element.height}
  2963. L${element.width * 0.35},${element.height * 0.625}
  2964. L${element.width * 0.183},${element.height * 0.625}
  2965. L${element.width * 0.183},${element.height * 0.75}
  2966. Z`
  2967. )
  2968. // 设置填充色
  2969. setBackground(element, leftArrowCallout)
  2970. // 设置边框
  2971. if (element.borderWidth > 0) {
  2972. leftArrowCallout.setAttribute("stroke", element.borderColor || "#000")
  2973. leftArrowCallout.setAttribute("stroke-width", element.borderWidth || 1)
  2974. leftArrowCallout.setAttribute("stroke-linecap", "butt")
  2975. leftArrowCallout.setAttribute("stroke-linejoin", "round")
  2976. // 处理虚线边框
  2977. if (
  2978. element.borderType === "dotted" ||
  2979. element.borderType === "dashed"
  2980. ) {
  2981. if (element.borderStrokeDasharray) {
  2982. leftArrowCallout.setAttribute(
  2983. "stroke-dasharray",
  2984. element.borderStrokeDasharray
  2985. )
  2986. } else if (element.borderType === "dotted") {
  2987. leftArrowCallout.setAttribute("stroke-dasharray", "1, 3")
  2988. } else if (element.borderType === "dashed") {
  2989. leftArrowCallout.setAttribute("stroke-dasharray", "5, 5")
  2990. }
  2991. }
  2992. }
  2993. svg.appendChild(leftArrowCallout)
  2994. break
  2995. case "upArrowCallout":
  2996. const upArrowCallout = document.createElementNS(
  2997. "http://www.w3.org/2000/svg",
  2998. "path"
  2999. )
  3000. upArrowCallout.setAttribute(
  3001. "d",
  3002. `M0,${element.height * 0.35}
  3003. L${element.width * 0.41},${element.height * 0.35}
  3004. L${element.width * 0.41},${element.height * 0.25}
  3005. L${element.width * 0.32},${element.height * 0.25}
  3006. L${element.width * 0.5},0
  3007. L${element.width * 0.68},${element.height * 0.25}
  3008. L${element.width * 0.59},${element.height * 0.25}
  3009. L${element.width * 0.59},${element.height * 0.35}
  3010. L${element.width},${element.height * 0.35}
  3011. L${element.width},${element.height}
  3012. L0,${element.height}
  3013. Z`
  3014. )
  3015. // 设置填充色
  3016. setBackground(element, upArrowCallout)
  3017. // 设置边框
  3018. if (element.borderWidth > 0) {
  3019. upArrowCallout.setAttribute("stroke", element.borderColor || "#000")
  3020. upArrowCallout.setAttribute("stroke-width", element.borderWidth || 1)
  3021. upArrowCallout.setAttribute("stroke-linecap", "butt")
  3022. upArrowCallout.setAttribute("stroke-linejoin", "round")
  3023. // 处理虚线边框
  3024. if (
  3025. element.borderType === "dotted" ||
  3026. element.borderType === "dashed"
  3027. ) {
  3028. if (element.borderStrokeDasharray) {
  3029. upArrowCallout.setAttribute(
  3030. "stroke-dasharray",
  3031. element.borderStrokeDasharray
  3032. )
  3033. } else if (element.borderType === "dotted") {
  3034. upArrowCallout.setAttribute("stroke-dasharray", "1, 3")
  3035. } else if (element.borderType === "dashed") {
  3036. upArrowCallout.setAttribute("stroke-dasharray", "5, 5")
  3037. }
  3038. }
  3039. }
  3040. svg.appendChild(upArrowCallout)
  3041. break
  3042. case "notchedRightArrow":
  3043. const notchedRightArrow = document.createElementNS(
  3044. "http://www.w3.org/2000/svg",
  3045. "path"
  3046. )
  3047. notchedRightArrow.setAttribute(
  3048. "d",
  3049. `M0,${element.height * 0.25}
  3050. L${element.width * 0.83},${element.height * 0.25}
  3051. L${element.width * 0.83},0
  3052. L${element.width},${element.height * 0.5}
  3053. L${element.width * 0.83},${element.height}
  3054. L${element.width * 0.83},${element.height * 0.75}
  3055. L0,${element.height * 0.75}
  3056. L${element.width * 0.086},${element.height * 0.5}
  3057. Z`
  3058. )
  3059. // 设置填充色
  3060. setBackground(element, notchedRightArrow)
  3061. // 设置边框
  3062. if (element.borderWidth > 0) {
  3063. notchedRightArrow.setAttribute("stroke", element.borderColor || "#000")
  3064. notchedRightArrow.setAttribute("stroke-width", element.borderWidth || 1)
  3065. notchedRightArrow.setAttribute("stroke-linecap", "butt")
  3066. notchedRightArrow.setAttribute("stroke-linejoin", "round")
  3067. // 处理虚线边框
  3068. if (
  3069. element.borderType === "dotted" ||
  3070. element.borderType === "dashed"
  3071. ) {
  3072. if (element.borderStrokeDasharray) {
  3073. notchedRightArrow.setAttribute(
  3074. "stroke-dasharray",
  3075. element.borderStrokeDasharray
  3076. )
  3077. } else if (element.borderType === "dotted") {
  3078. notchedRightArrow.setAttribute("stroke-dasharray", "1, 3")
  3079. } else if (element.borderType === "dashed") {
  3080. notchedRightArrow.setAttribute("stroke-dasharray", "5, 5")
  3081. }
  3082. }
  3083. }
  3084. svg.appendChild(notchedRightArrow)
  3085. break
  3086. case "homePlate":
  3087. const homePlate = document.createElementNS(
  3088. "http://www.w3.org/2000/svg",
  3089. "path"
  3090. )
  3091. homePlate.setAttribute(
  3092. "d",
  3093. `M0,0
  3094. L${element.width * 0.925},0
  3095. L${element.width},${element.height * 0.5}
  3096. L${element.width * 0.925},${element.height}
  3097. L0,${element.height}
  3098. Z`
  3099. )
  3100. // 设置填充色
  3101. setBackground(element, homePlate)
  3102. // 设置边框
  3103. if (element.borderWidth > 0) {
  3104. homePlate.setAttribute("stroke", element.borderColor || "#000")
  3105. homePlate.setAttribute("stroke-width", element.borderWidth || 1)
  3106. homePlate.setAttribute("stroke-linecap", "butt")
  3107. homePlate.setAttribute("stroke-linejoin", "round")
  3108. // 处理虚线边框
  3109. if (
  3110. element.borderType === "dotted" ||
  3111. element.borderType === "dashed"
  3112. ) {
  3113. if (element.borderStrokeDasharray) {
  3114. homePlate.setAttribute(
  3115. "stroke-dasharray",
  3116. element.borderStrokeDasharray
  3117. )
  3118. } else if (element.borderType === "dotted") {
  3119. homePlate.setAttribute("stroke-dasharray", "1, 3")
  3120. } else if (element.borderType === "dashed") {
  3121. homePlate.setAttribute("stroke-dasharray", "5, 5")
  3122. }
  3123. }
  3124. }
  3125. svg.appendChild(homePlate)
  3126. break
  3127. case "rightTriangle":
  3128. const rightTriangle = document.createElementNS(
  3129. "http://www.w3.org/2000/svg",
  3130. "polygon"
  3131. )
  3132. rightTriangle.setAttribute(
  3133. "points",
  3134. `0,0 ${element.width},${element.height} 0,${element.height}`
  3135. )
  3136. // 设置填充色
  3137. setBackground(element, rightTriangle)
  3138. // 设置边框
  3139. if (element.borderWidth > 0) {
  3140. rightTriangle.setAttribute("stroke", element.borderColor || "#000")
  3141. rightTriangle.setAttribute("stroke-width", element.borderWidth || 1)
  3142. // 处理虚线边框
  3143. if (
  3144. element.borderType === "dotted" ||
  3145. element.borderType === "dashed"
  3146. ) {
  3147. if (element.borderStrokeDasharray) {
  3148. rightTriangle.setAttribute(
  3149. "stroke-dasharray",
  3150. element.borderStrokeDasharray
  3151. )
  3152. } else if (element.borderType === "dotted") {
  3153. rightTriangle.setAttribute("stroke-dasharray", "1, 3")
  3154. } else if (element.borderType === "dashed") {
  3155. rightTriangle.setAttribute("stroke-dasharray", "5, 5")
  3156. }
  3157. }
  3158. }
  3159. svg.appendChild(rightTriangle)
  3160. break
  3161. case "semiCircle":
  3162. const semiCircle = document.createElementNS(
  3163. "http://www.w3.org/2000/svg",
  3164. "path"
  3165. )
  3166. semiCircle.setAttribute(
  3167. "d",
  3168. `M0,${element.height} A${element.width / 2},${element.height} 0 0,1 ${
  3169. element.width
  3170. },${element.height} Z`
  3171. )
  3172. // 设置填充色
  3173. setBackground(element, semiCircle)
  3174. // 设置边框
  3175. if (element.borderWidth > 0) {
  3176. semiCircle.setAttribute("stroke", element.borderColor || "#000")
  3177. semiCircle.setAttribute("stroke-width", element.borderWidth || 1)
  3178. // 处理虚线边框
  3179. if (
  3180. element.borderType === "dotted" ||
  3181. element.borderType === "dashed"
  3182. ) {
  3183. if (element.borderStrokeDasharray) {
  3184. semiCircle.setAttribute(
  3185. "stroke-dasharray",
  3186. element.borderStrokeDasharray
  3187. )
  3188. } else if (element.borderType === "dotted") {
  3189. semiCircle.setAttribute("stroke-dasharray", "1, 3")
  3190. } else if (element.borderType === "dashed") {
  3191. semiCircle.setAttribute("stroke-dasharray", "5, 5")
  3192. }
  3193. }
  3194. }
  3195. svg.appendChild(semiCircle)
  3196. break
  3197. case "star":
  3198. const star = document.createElementNS(
  3199. "http://www.w3.org/2000/svg",
  3200. "polygon"
  3201. )
  3202. const cx = element.width / 2
  3203. const cy = element.height / 2
  3204. const outerRadius = Math.min(element.width, element.height) / 2
  3205. const innerRadius = outerRadius * 0.4
  3206. let starPoints = ""
  3207. for (let i = 0; i < 10; i++) {
  3208. const radius = i % 2 === 0 ? outerRadius : innerRadius
  3209. const angle = (Math.PI * i) / 5
  3210. const x = cx + radius * Math.sin(angle)
  3211. const y = cy - radius * Math.cos(angle)
  3212. starPoints += `${x},${y} `
  3213. }
  3214. star.setAttribute("points", starPoints.trim())
  3215. // 设置填充色
  3216. setBackground(element, star)
  3217. // 设置边框
  3218. if (element.borderWidth > 0) {
  3219. star.setAttribute("stroke", element.borderColor || "#000")
  3220. star.setAttribute("stroke-width", element.borderWidth || 1)
  3221. // 处理虚线边框
  3222. if (
  3223. element.borderType === "dotted" ||
  3224. element.borderType === "dashed"
  3225. ) {
  3226. if (element.borderStrokeDasharray) {
  3227. star.setAttribute("stroke-dasharray", element.borderStrokeDasharray)
  3228. } else if (element.borderType === "dotted") {
  3229. star.setAttribute("stroke-dasharray", "1, 3")
  3230. } else if (element.borderType === "dashed") {
  3231. star.setAttribute("stroke-dasharray", "5, 5")
  3232. }
  3233. }
  3234. }
  3235. svg.appendChild(star)
  3236. break
  3237. case "cross":
  3238. const cross = document.createElementNS(
  3239. "http://www.w3.org/2000/svg",
  3240. "polygon"
  3241. )
  3242. cross.setAttribute(
  3243. "points",
  3244. `${element.width * 0.35},0 ${element.width * 0.65},0 ${
  3245. element.width * 0.65
  3246. },${element.height * 0.35} ` +
  3247. `${element.width},${element.height * 0.35} ${element.width},${
  3248. element.height * 0.65
  3249. } ` +
  3250. `${element.width * 0.65},${element.height * 0.65} ${
  3251. element.width * 0.65
  3252. },${element.height} ` +
  3253. `${element.width * 0.35},${element.height} ${element.width * 0.35},${
  3254. element.height * 0.65
  3255. } ` +
  3256. `0,${element.height * 0.65} 0,${element.height * 0.35} ${
  3257. element.width * 0.35
  3258. },${element.height * 0.35}`
  3259. )
  3260. // 设置填充色
  3261. setBackground(element, cross)
  3262. // 设置边框
  3263. if (element.borderWidth > 0) {
  3264. cross.setAttribute("stroke", element.borderColor || "#000")
  3265. cross.setAttribute("stroke-width", element.borderWidth || 1)
  3266. // 处理虚线边框
  3267. if (
  3268. element.borderType === "dotted" ||
  3269. element.borderType === "dashed"
  3270. ) {
  3271. if (element.borderStrokeDasharray) {
  3272. cross.setAttribute(
  3273. "stroke-dasharray",
  3274. element.borderStrokeDasharray
  3275. )
  3276. } else if (element.borderType === "dotted") {
  3277. cross.setAttribute("stroke-dasharray", "1, 3")
  3278. } else if (element.borderType === "dashed") {
  3279. cross.setAttribute("stroke-dasharray", "5, 5")
  3280. }
  3281. }
  3282. }
  3283. svg.appendChild(cross)
  3284. break
  3285. case "chevron":
  3286. const chevron = document.createElementNS(
  3287. "http://www.w3.org/2000/svg",
  3288. "polygon"
  3289. )
  3290. chevron.setAttribute(
  3291. "points",
  3292. `${element.width * 0.5},0 ${element.width},${element.height * 0.5} ` +
  3293. `${element.width * 0.5},${element.height} ` +
  3294. `0,${element.height} ${element.width * 0.5},${
  3295. element.height * 0.5
  3296. } 0,0`
  3297. )
  3298. // 设置填充色
  3299. setBackground(element, chevron)
  3300. // 设置边框
  3301. if (element.borderWidth > 0) {
  3302. chevron.setAttribute("stroke", element.borderColor || "#000")
  3303. chevron.setAttribute("stroke-width", element.borderWidth || 1)
  3304. // 处理虚线边框
  3305. if (
  3306. element.borderType === "dotted" ||
  3307. element.borderType === "dashed"
  3308. ) {
  3309. if (element.borderStrokeDasharray) {
  3310. chevron.setAttribute(
  3311. "stroke-dasharray",
  3312. element.borderStrokeDasharray
  3313. )
  3314. } else if (element.borderType === "dotted") {
  3315. chevron.setAttribute("stroke-dasharray", "1, 3")
  3316. } else if (element.borderType === "dashed") {
  3317. chevron.setAttribute("stroke-dasharray", "5, 5")
  3318. }
  3319. }
  3320. }
  3321. svg.appendChild(chevron)
  3322. break
  3323. case "frame":
  3324. // 创建外框和内框
  3325. const outerRect = document.createElementNS(
  3326. "http://www.w3.org/2000/svg",
  3327. "rect"
  3328. )
  3329. outerRect.setAttribute("x", 0)
  3330. outerRect.setAttribute("y", 0)
  3331. outerRect.setAttribute("width", element.width)
  3332. outerRect.setAttribute("height", element.height)
  3333. const innerRect = document.createElementNS(
  3334. "http://www.w3.org/2000/svg",
  3335. "rect"
  3336. )
  3337. const frameWidth = element.width / 10
  3338. innerRect.setAttribute("x", frameWidth)
  3339. innerRect.setAttribute("y", frameWidth)
  3340. innerRect.setAttribute("width", element.width - frameWidth * 2)
  3341. innerRect.setAttribute("height", element.height - frameWidth * 2)
  3342. // 设置填充色
  3343. setBackground(element, outerRect)
  3344. innerRect.setAttribute("fill", "white") // 内框为白色
  3345. // 设置边框
  3346. if (element.borderWidth > 0) {
  3347. outerRect.setAttribute("stroke", element.borderColor || "#000")
  3348. outerRect.setAttribute("stroke-width", element.borderWidth || 1)
  3349. // 处理虚线边框
  3350. if (
  3351. element.borderType === "dotted" ||
  3352. element.borderType === "dashed"
  3353. ) {
  3354. if (element.borderStrokeDasharray) {
  3355. outerRect.setAttribute(
  3356. "stroke-dasharray",
  3357. element.borderStrokeDasharray
  3358. )
  3359. } else if (element.borderType === "dotted") {
  3360. outerRect.setAttribute("stroke-dasharray", "1, 3")
  3361. } else if (element.borderType === "dashed") {
  3362. outerRect.setAttribute("stroke-dasharray", "5, 5")
  3363. }
  3364. }
  3365. }
  3366. svg.appendChild(outerRect)
  3367. svg.appendChild(innerRect)
  3368. break
  3369. case "cloud":
  3370. // 使用路径绘制云形
  3371. const cloud = document.createElementNS(
  3372. "http://www.w3.org/2000/svg",
  3373. "path"
  3374. )
  3375. const w = element.width
  3376. const h = element.height
  3377. cloud.setAttribute(
  3378. "d",
  3379. `M${w * 0.2},${h * 0.6} ` +
  3380. `C${w * 0.05},${h * 0.6} ${w * 0.05},${h * 0.3} ${w * 0.2},${
  3381. h * 0.3
  3382. } ` +
  3383. `C${w * 0.2},${h * 0.1} ${w * 0.45},${h * 0.1} ${w * 0.5},${
  3384. h * 0.3
  3385. } ` +
  3386. `C${w * 0.55},${h * 0.1} ${w * 0.8},${h * 0.1} ${w * 0.8},${
  3387. h * 0.3
  3388. } ` +
  3389. `C${w * 0.95},${h * 0.3} ${w * 0.95},${h * 0.6} ${w * 0.8},${
  3390. h * 0.6
  3391. } ` +
  3392. `L${w * 0.2},${h * 0.6} Z`
  3393. )
  3394. // 设置填充色
  3395. setBackground(element, cloud)
  3396. // 设置边框
  3397. if (element.borderWidth > 0) {
  3398. cloud.setAttribute("stroke", element.borderColor || "#000")
  3399. cloud.setAttribute("stroke-width", element.borderWidth || 1)
  3400. // 处理虚线边框
  3401. if (
  3402. element.borderType === "dotted" ||
  3403. element.borderType === "dashed"
  3404. ) {
  3405. if (element.borderStrokeDasharray) {
  3406. cloud.setAttribute(
  3407. "stroke-dasharray",
  3408. element.borderStrokeDasharray
  3409. )
  3410. } else if (element.borderType === "dotted") {
  3411. cloud.setAttribute("stroke-dasharray", "1, 3")
  3412. } else if (element.borderType === "dashed") {
  3413. cloud.setAttribute("stroke-dasharray", "5, 5")
  3414. }
  3415. }
  3416. }
  3417. svg.appendChild(cloud)
  3418. break
  3419. case "blockArc":
  3420. const blockArc = document.createElementNS(
  3421. "http://www.w3.org/2000/svg",
  3422. "path"
  3423. )
  3424. // 计算弧形参数
  3425. const rx = element.width / 2
  3426. const ry = element.height / 2
  3427. const innerRatio = 0.86 // 根据示例计算的内弧半径比例
  3428. const innerRx = rx * innerRatio
  3429. const innerRy = ry * innerRatio
  3430. // 构建路径
  3431. const path2 = `
  3432. M 0,${ry}
  3433. A ${rx} ${ry} 0 1 1 ${element.width} ${ry}
  3434. L ${element.width - (rx - innerRx)},${ry}
  3435. A ${innerRx} ${innerRy} 0 1 0 ${rx - innerRx},${ry}
  3436. Z`
  3437. blockArc.setAttribute("d", path2.trim())
  3438. // 设置填充色
  3439. setBackground(element, blockArc)
  3440. // 设置边框
  3441. if (element.borderWidth > 0) {
  3442. blockArc.setAttribute("stroke", element.borderColor || "#000")
  3443. blockArc.setAttribute("stroke-width", element.borderWidth || 1)
  3444. if (
  3445. element.borderType === "dotted" ||
  3446. element.borderType === "dashed"
  3447. ) {
  3448. if (element.borderStrokeDasharray) {
  3449. blockArc.setAttribute(
  3450. "stroke-dasharray",
  3451. element.borderStrokeDasharray
  3452. )
  3453. } else if (element.borderType === "dotted") {
  3454. blockArc.setAttribute("stroke-dasharray", "1, 3")
  3455. } else if (element.borderType === "dashed") {
  3456. blockArc.setAttribute("stroke-dasharray", "5, 5")
  3457. }
  3458. }
  3459. }
  3460. svg.appendChild(blockArc)
  3461. break
  3462. case "rect":
  3463. default:
  3464. const rect = document.createElementNS(
  3465. "http://www.w3.org/2000/svg",
  3466. "rect"
  3467. )
  3468. rect.setAttribute("x", 0)
  3469. rect.setAttribute("y", 0)
  3470. rect.setAttribute("width", element.width)
  3471. rect.setAttribute("height", element.height)
  3472. if (element.fill && element.fill.type) {
  3473. // 设置填充色
  3474. if (element.fill.type === "color") {
  3475. rect.setAttribute("fill", element.fill.value || "transparent")
  3476. } else if (element.fill.type === "gradient") {
  3477. // 渐变填充
  3478. const { colors, path, rot } = element.fill.value
  3479. if (colors && colors.length >= 2) {
  3480. const gradientType = path === "rect" ? "linear" : "radial"
  3481. const gradientAngle =
  3482. gradientType === "linear" ? (90 - (rot || 0)) % 360 : rot || 0
  3483. let gradientString = `${gradientType}-gradient(`
  3484. if (gradientType === "linear") {
  3485. gradientString += `${gradientAngle}deg, `
  3486. }
  3487. colors.forEach((color, i) => {
  3488. gradientString += `${color.color} ${color.pos}${
  3489. i < colors.length - 1 ? ", " : ""
  3490. }`
  3491. })
  3492. gradientString += ")"
  3493. // 创建渐变定义
  3494. const gradientDef = document.createElementNS(
  3495. "http://www.w3.org/2000/svg",
  3496. "defs"
  3497. )
  3498. const gradientEl = document.createElementNS(
  3499. "http://www.w3.org/2000/svg",
  3500. gradientType === "linear" ? "linearGradient" : "radialGradient"
  3501. )
  3502. const gradientId = `gradient-${Date.now()}-${Math.random()
  3503. .toString(36)
  3504. .substr(2, 9)}`
  3505. gradientEl.setAttribute("id", gradientId)
  3506. // 设置渐变属性
  3507. if (gradientType === "linear") {
  3508. gradientEl.setAttribute(
  3509. "gradientTransform",
  3510. `rotate(${gradientAngle})`
  3511. )
  3512. }
  3513. // 添加渐变色标
  3514. colors.forEach((color) => {
  3515. const stop = document.createElementNS(
  3516. "http://www.w3.org/2000/svg",
  3517. "stop"
  3518. )
  3519. stop.setAttribute("offset", color.pos)
  3520. stop.setAttribute("stop-color", color.color)
  3521. gradientEl.appendChild(stop)
  3522. })
  3523. gradientDef.appendChild(gradientEl)
  3524. svg.appendChild(gradientDef)
  3525. // 应用渐变
  3526. rect.setAttribute("fill", `url(#${gradientId})`)
  3527. }
  3528. } else if (element.fill.type === "image") {
  3529. // 创建图案填充
  3530. const pattern = document.createElementNS(
  3531. "http://www.w3.org/2000/svg",
  3532. "pattern"
  3533. )
  3534. const patternId = `pattern-${Date.now()}-${Math.random()
  3535. .toString(36)
  3536. .substr(2, 9)}`
  3537. pattern.setAttribute("id", patternId)
  3538. pattern.setAttribute("patternUnits", "userSpaceOnUse")
  3539. pattern.setAttribute("width", "100%")
  3540. pattern.setAttribute("height", "100%")
  3541. // 创建图片元素
  3542. const image = document.createElementNS(
  3543. "http://www.w3.org/2000/svg",
  3544. "image"
  3545. )
  3546. image.setAttribute("width", "100%")
  3547. image.setAttribute("height", "100%")
  3548. image.setAttribute("preserveAspectRatio", "xMidYMid slice")
  3549. image.setAttributeNS(
  3550. "http://www.w3.org/1999/xlink",
  3551. "href",
  3552. element.fill.value.picBase64
  3553. )
  3554. pattern.appendChild(image)
  3555. // 添加pattern到defs
  3556. const defs = document.createElementNS(
  3557. "http://www.w3.org/2000/svg",
  3558. "defs"
  3559. )
  3560. defs.appendChild(pattern)
  3561. svg.appendChild(defs)
  3562. // 应用图案填充
  3563. rect.setAttribute("fill", `url(#${patternId})`)
  3564. } else {
  3565. rect.setAttribute("fill", "transparent")
  3566. }
  3567. } else {
  3568. rect.setAttribute("fill", "#ffffff00")
  3569. }
  3570. // 设置边框
  3571. if (element.borderWidth > 0) {
  3572. rect.setAttribute("stroke", element.borderColor || "#000")
  3573. rect.setAttribute("stroke-width", element.borderWidth || 1)
  3574. // 处理虚线边框
  3575. if (
  3576. element.borderType === "dotted" ||
  3577. element.borderType === "dashed"
  3578. ) {
  3579. if (element.borderStrokeDasharray) {
  3580. rect.setAttribute("stroke-dasharray", element.borderStrokeDasharray)
  3581. } else if (element.borderType === "dotted") {
  3582. rect.setAttribute("stroke-dasharray", "1, 3")
  3583. } else if (element.borderType === "dashed") {
  3584. rect.setAttribute("stroke-dasharray", "5, 5")
  3585. }
  3586. }
  3587. }
  3588. svg.appendChild(rect)
  3589. break
  3590. }
  3591. const transformList = []
  3592. let rotateDeg = element.rotate || 0
  3593. if (element.isFlipV) {
  3594. transformList.push(`scaleY(-1)`)
  3595. rotateDeg = -rotateDeg // 垂直翻转时反转旋转方向
  3596. }
  3597. if (element.isFlipH) {
  3598. transformList.push(`scaleX(-1)`)
  3599. rotateDeg = -rotateDeg // 水平翻转时反转旋转方向
  3600. }
  3601. if (element.rotate) {
  3602. transformList.push(`rotate(${rotateDeg}deg)`)
  3603. }
  3604. if (transformList.length > 0) {
  3605. el.style.transform = transformList.join(" ")
  3606. el.style.transformOrigin = "center center"
  3607. }
  3608. el.appendChild(svg)
  3609. // 设置形状内容
  3610. if (element.content) {
  3611. const contentContainer = document.createElement("div")
  3612. contentContainer.innerHTML = this.convertPtToPxInContent(element.content)
  3613. contentContainer.style.position = "absolute"
  3614. contentContainer.style.width = element.width + "px"
  3615. contentContainer.style.height = element.height + "px"
  3616. contentContainer.style.top = "0px"
  3617. contentContainer.style.left = "0px"
  3618. contentContainer.style.display = "flex"
  3619. contentContainer.style.alignItems = "center"
  3620. contentContainer.style.justifyContent = "center"
  3621. contentContainer.style.zIndex = element.order
  3622. contentContainer.style.pointerEvents = "none"
  3623. const transformList = []
  3624. let rotateDeg = element.rotate || 0
  3625. if (element.isFlipV) {
  3626. transformList.push(`scaleY(-1)`)
  3627. rotateDeg = -rotateDeg // 垂直翻转时反转旋转方向
  3628. }
  3629. if (element.isFlipH) {
  3630. transformList.push(`scaleX(-1)`)
  3631. rotateDeg = -rotateDeg // 水平翻转时反转旋转方向
  3632. }
  3633. if (element.rotate) {
  3634. transformList.push(`rotate(${rotateDeg}deg)`)
  3635. }
  3636. if (transformList.length > 0) {
  3637. contentContainer.style.transform = transformList.join(" ")
  3638. contentContainer.style.transformOrigin = "center center"
  3639. }
  3640. el.appendChild(contentContainer)
  3641. }
  3642. return el
  3643. }
  3644. // 创建表格元素
  3645. const createTableElement = (element) => {
  3646. const el = document.createElement("div")
  3647. el.style.position = "absolute"
  3648. el.style.top = element.top + "px"
  3649. el.style.left = element.left + "px"
  3650. el.style.width = element.width + "px"
  3651. el.style.height = element.height + "px"
  3652. el.style.zIndex = element.order
  3653. // 创建表格元素
  3654. const table = document.createElement("table")
  3655. table.style.width = element.width + "px"
  3656. table.style.height = element.height + "px"
  3657. table.style.borderCollapse = "collapse"
  3658. table.style.tableLayout = "fixed"
  3659. // 设置表格边框
  3660. if (element.borders) {
  3661. if (element.borders.all) {
  3662. const border = element.borders.all
  3663. table.style.border = `${border.borderWidth || 1}px ${
  3664. border.borderType || "solid"
  3665. } ${border.borderColor || "#000"}`
  3666. } else {
  3667. // 分别设置四边边框
  3668. if (element.borders.top) {
  3669. table.style.borderTop = `${element.borders.top.borderWidth || 1}px ${
  3670. element.borders.top.borderType || "solid"
  3671. } ${element.borders.top.borderColor || "#000"}`
  3672. }
  3673. if (element.borders.bottom) {
  3674. table.style.borderBottom = `${
  3675. element.borders.bottom.borderWidth || 1
  3676. }px ${element.borders.bottom.borderType || "solid"} ${
  3677. element.borders.bottom.borderColor || "#000"
  3678. }`
  3679. }
  3680. if (element.borders.left) {
  3681. table.style.borderLeft = `${element.borders.left.borderWidth || 1}px ${
  3682. element.borders.left.borderType || "solid"
  3683. } ${element.borders.left.borderColor || "#000"}`
  3684. }
  3685. if (element.borders.right) {
  3686. table.style.borderRight = `${
  3687. element.borders.right.borderWidth || 1
  3688. }px ${element.borders.right.borderType || "solid"} ${
  3689. element.borders.right.borderColor || "#000"
  3690. }`
  3691. }
  3692. }
  3693. }
  3694. // 创建表格内容
  3695. const tbody = document.createElement("tbody")
  3696. // 处理表格数据
  3697. if (element.data && element.data.length > 0) {
  3698. element.data.forEach((rowData, rowIndex) => {
  3699. const tr = document.createElement("tr")
  3700. rowData.forEach((cell, colIndex) => {
  3701. // 跳过被合并的单元格
  3702. if (cell.hMerge) return
  3703. const td = document.createElement("td")
  3704. // 设置单元格内容
  3705. if (cell.text) {
  3706. td.innerHTML = this.convertPtToPxInContent(cell.text)
  3707. }
  3708. // 设置单元格样式
  3709. td.style.padding = "0px"
  3710. td.style.verticalAlign = "middle"
  3711. // 设置文本样式
  3712. if (cell.fontColor) td.style.color = cell.fontColor
  3713. if (cell.fontSize) td.style.fontSize = cell.fontSize
  3714. if (cell.fontFamily) td.style.fontFamily = cell.fontFamily
  3715. if (cell.bold) td.style.fontWeight = "bold"
  3716. if (cell.italic) td.style.fontStyle = "italic"
  3717. if (cell.underline) td.style.textDecoration = "underline"
  3718. if (cell.align) td.style.textAlign = cell.align
  3719. // 设置背景色
  3720. if (cell.fillColor) td.style.backgroundColor = cell.fillColor
  3721. // 设置单元格边框
  3722. if (cell.borders) {
  3723. if (cell.borders.all) {
  3724. const border = cell.borders.all
  3725. td.style.border = `${border.borderWidth || 1}px ${
  3726. border.borderType || "solid"
  3727. } ${border.borderColor || "#000"}`
  3728. } else {
  3729. // 分别设置四边边框
  3730. if (cell.borders.top) {
  3731. td.style.borderTop = `${cell.borders.top.borderWidth || 1}px ${
  3732. cell.borders.top.borderType || "solid"
  3733. } ${cell.borders.top.borderColor || "#000"}`
  3734. }
  3735. if (cell.borders.bottom) {
  3736. td.style.borderBottom = `${
  3737. cell.borders.bottom.borderWidth || 1
  3738. }px ${cell.borders.bottom.borderType || "solid"} ${
  3739. cell.borders.bottom.borderColor || "#000"
  3740. }`
  3741. }
  3742. if (cell.borders.left) {
  3743. td.style.borderLeft = `${cell.borders.left.borderWidth || 1}px ${
  3744. cell.borders.left.borderType || "solid"
  3745. } ${cell.borders.left.borderColor || "#000"}`
  3746. }
  3747. if (cell.borders.right) {
  3748. td.style.borderRight = `${
  3749. cell.borders.right.borderWidth || 1
  3750. }px ${cell.borders.right.borderType || "solid"} ${
  3751. cell.borders.right.borderColor || "#000"
  3752. }`
  3753. }
  3754. }
  3755. }
  3756. td.style.width = element.colWidths[colIndex] + "px"
  3757. // 设置单元格合并
  3758. if (cell.colSpan && cell.colSpan > 1) {
  3759. td.colSpan = cell.colSpan
  3760. td.style.width = element.colWidths[colIndex] * cell.colSpan + "px"
  3761. }
  3762. if (cell.rowSpan && cell.rowSpan > 1) {
  3763. td.rowSpan = cell.rowSpan
  3764. }
  3765. tr.appendChild(td)
  3766. })
  3767. tr.style.height = element.rowHeights[rowIndex] + "px"
  3768. tbody.appendChild(tr)
  3769. })
  3770. }
  3771. table.appendChild(tbody)
  3772. el.appendChild(table)
  3773. return el
  3774. }
  3775. const createChartElement = (element) => {
  3776. // 实现图表元素创建逻辑
  3777. return document.createElement("div") // 临时返回空div
  3778. }
  3779. // 创建图表元素
  3780. const createDiagramElement = (element) => {
  3781. const el = document.createElement("div")
  3782. el.style.position = "absolute"
  3783. el.style.top = element.top + "px"
  3784. el.style.left = element.left + "px"
  3785. el.style.width = element.width + "px"
  3786. el.style.height = element.height + "px"
  3787. el.style.zIndex = element.order
  3788. // 递归渲染SmartArt节点
  3789. const renderDiagramNode = (node) => {
  3790. const nodeEl = document.createElement("div")
  3791. nodeEl.style.position = "absolute"
  3792. nodeEl.style.left = node.left + "px"
  3793. nodeEl.style.top = node.top + "px"
  3794. nodeEl.style.width = node.width + "px"
  3795. nodeEl.style.height = node.height + "px"
  3796. // 设置边框
  3797. if (node.borderWidth > 0) {
  3798. nodeEl.style.border = `${node.borderWidth}px ${node.borderType} ${node.borderColor}`
  3799. if (node.borderStrokeDasharray && node.borderStrokeDasharray !== "0") {
  3800. nodeEl.style.borderStyle = "dashed"
  3801. }
  3802. }
  3803. // 设置背景填充
  3804. if (node.fill && node.fill.type === "color") {
  3805. nodeEl.style.backgroundColor = node.fill.value
  3806. }
  3807. // 设置内容
  3808. if (node.content) {
  3809. nodeEl.innerHTML = convertPtToPxInContent(node.content)
  3810. }
  3811. // 设置垂直对齐
  3812. if (node.vAlign === "mid") {
  3813. nodeEl.style.display = "flex"
  3814. nodeEl.style.alignItems = "center"
  3815. nodeEl.style.justifyContent = "center"
  3816. }
  3817. // 设置翻转
  3818. const transforms = []
  3819. if (node.isFlipV) {
  3820. transforms.push("scaleY(-1)")
  3821. }
  3822. if (node.isFlipH) {
  3823. transforms.push("scaleX(-1)")
  3824. }
  3825. if (transforms.length > 0) {
  3826. nodeEl.style.transform = transforms.join(" ")
  3827. }
  3828. return nodeEl
  3829. }
  3830. // 按照 order 排序元素
  3831. const sortedElements = [...element.elements].sort((a, b) => a.order - b.order)
  3832. // 渲染所有节点
  3833. sortedElements.forEach((node) => {
  3834. const nodeEl = renderDiagramNode(node)
  3835. el.appendChild(nodeEl)
  3836. })
  3837. return el
  3838. }
  3839. // 创建数学公式元素
  3840. const createMathElement = (element) => {
  3841. const el = document.createElement("div")
  3842. el.style.position = "absolute"
  3843. el.style.top = element.top + "px"
  3844. el.style.left = element.left + "px"
  3845. el.style.width = element.width + "px"
  3846. el.style.height = element.height + "px"
  3847. el.style.zIndex = element.order
  3848. // 应用旋转变换
  3849. if (element.rotate) {
  3850. el.style.transform = `rotate(${element.rotate}deg)`
  3851. el.style.transformOrigin = "center center"
  3852. }
  3853. // 设置数学公式内容
  3854. if (element.mathXml) {
  3855. // 如果有MathML格式的内容
  3856. try {
  3857. // 创建MathML命名空间的元素
  3858. const mathEl = document.createElementNS(
  3859. "http://www.w3.org/1998/Math/MathML",
  3860. "math"
  3861. )
  3862. mathEl.innerHTML = element.mathXml
  3863. el.appendChild(mathEl)
  3864. } catch (error) {
  3865. console.error("创建MathML元素失败:", error)
  3866. // 回退方案:直接显示文本
  3867. el.textContent = element.text || "数学公式"
  3868. }
  3869. } else if (element.text) {
  3870. // 如果只有文本内容
  3871. el.textContent = element.text
  3872. }
  3873. // 应用样式
  3874. if (element.style) {
  3875. // 字体样式
  3876. if (element.style.fontFamily) {
  3877. el.style.fontFamily = element.style.fontFamily
  3878. }
  3879. // 字体大小
  3880. if (element.style.fontSize) {
  3881. el.style.fontSize = element.style.fontSize + "px"
  3882. }
  3883. // 字体颜色
  3884. if (element.style.color) {
  3885. el.style.color = element.style.color
  3886. }
  3887. }
  3888. // 应用背景色
  3889. if (element.fill && element.fill.type === "color") {
  3890. el.style.backgroundColor = element.fill.value
  3891. }
  3892. // 应用边框
  3893. if (element.borderColor && element.borderWidth) {
  3894. el.style.border = `${element.borderWidth}px ${
  3895. element.borderType || "solid"
  3896. } ${element.borderColor}`
  3897. }
  3898. return el
  3899. }
  3900. // 创建文本元素
  3901. const createTextElement = (element) => {
  3902. const el = document.createElement("div")
  3903. el.style.position = "absolute"
  3904. el.style.top = element.top + "px"
  3905. el.style.left = element.left + "px"
  3906. el.style.width = element.width + "px"
  3907. el.style.height = element.height + "px"
  3908. el.style.zIndex = element.order
  3909. el.style.overflow = "hidden"
  3910. // 应用旋转变换
  3911. if (element.rotate) {
  3912. el.style.transform = `rotate(${element.rotate}deg)`
  3913. el.style.transformOrigin = "center center"
  3914. }
  3915. // 应用文本内容
  3916. if (element.text) {
  3917. el.innerHTML = element.text
  3918. }
  3919. // 应用文本样式
  3920. if (element.style) {
  3921. // 字体样式
  3922. if (element.style.fontFamily) {
  3923. el.style.fontFamily = element.style.fontFamily
  3924. }
  3925. // 字体大小
  3926. if (element.style.fontSize) {
  3927. el.style.fontSize = element.style.fontSize + "px"
  3928. }
  3929. // 字体颜色
  3930. if (element.style.color) {
  3931. el.style.color = element.style.color
  3932. }
  3933. // 字体粗细
  3934. if (element.style.fontWeight) {
  3935. el.style.fontWeight = element.style.fontWeight
  3936. }
  3937. // 字体样式(斜体等)
  3938. if (element.style.fontStyle) {
  3939. el.style.fontStyle = element.style.fontStyle
  3940. }
  3941. // 文本装饰(下划线等)
  3942. if (element.style.textDecoration) {
  3943. el.style.textDecoration = element.style.textDecoration
  3944. }
  3945. // 文本对齐
  3946. if (element.style.textAlign) {
  3947. el.style.textAlign = element.style.textAlign
  3948. }
  3949. // 行高
  3950. if (element.style.lineHeight) {
  3951. el.style.lineHeight = element.style.lineHeight
  3952. }
  3953. // 字间距
  3954. if (element.style.letterSpacing) {
  3955. el.style.letterSpacing = element.style.letterSpacing + "px"
  3956. }
  3957. // 文本缩进
  3958. if (element.style.textIndent) {
  3959. el.style.textIndent = element.style.textIndent + "px"
  3960. }
  3961. // 垂直对齐
  3962. if (element.style.verticalAlign) {
  3963. el.style.verticalAlign = element.style.verticalAlign
  3964. }
  3965. }
  3966. // 应用背景色
  3967. if (element.fill && element.fill.type === "color") {
  3968. el.style.backgroundColor = element.fill.value
  3969. }
  3970. // 应用边框
  3971. if (element.borderColor && element.borderWidth) {
  3972. el.style.border = `${element.borderWidth}px ${
  3973. element.borderType || "solid"
  3974. } ${element.borderColor}`
  3975. }
  3976. // 应用内边距
  3977. if (element.padding) {
  3978. el.style.padding = element.padding + "px"
  3979. }
  3980. return el
  3981. }
  3982. // 递归调整元素位置
  3983. const adjustElementPosition = (element, parentElement) => {
  3984. // 创建深拷贝,但保留特殊属性的引用
  3985. const adjustedElement = JSON.parse(JSON.stringify(element))
  3986. // 调整位置为相对于父组合元素的位置
  3987. adjustedElement.top = element.top - parentElement.top
  3988. adjustedElement.left = element.left - parentElement.left
  3989. // 恢复特殊属性的引用
  3990. const specialProps = [
  3991. "src",
  3992. "blob",
  3993. "fill",
  3994. "text",
  3995. "path",
  3996. "elements",
  3997. "borderColor",
  3998. "borderWidth",
  3999. "borderType",
  4000. ]
  4001. specialProps.forEach((prop) => {
  4002. if (element[prop] !== undefined) {
  4003. adjustedElement[prop] = element[prop]
  4004. }
  4005. })
  4006. // 如果是组合元素,递归处理其子元素
  4007. if (element.type === "group" && element.elements) {
  4008. adjustedElement.elements = element.elements.map((childElement) =>
  4009. adjustElementPosition(childElement, {
  4010. top: element.top,
  4011. left: element.left,
  4012. })
  4013. )
  4014. }
  4015. return adjustedElement
  4016. }
  4017. // 绘制类别标签
  4018. const drawCategoryLabels = (svg, categories, padding, groupWidth, barDir) => {
  4019. if (!categories || !categories.length) return
  4020. // 创建标签组
  4021. const labelGroup = document.createElementNS("http://www.w3.org/2000/svg", "g")
  4022. labelGroup.setAttribute("class", "category-labels")
  4023. // 计算标签位置
  4024. const isVertical = barDir === "vertical"
  4025. const labelSpacing = groupWidth
  4026. // 绘制每个类别标签
  4027. categories.forEach((category, index) => {
  4028. const label = document.createElementNS("http://www.w3.org/2000/svg", "text")
  4029. // 设置标签文本
  4030. label.textContent = category
  4031. // 设置标签样式
  4032. label.setAttribute("font-size", "12")
  4033. label.setAttribute("text-anchor", isVertical ? "middle" : "start")
  4034. // 设置标签位置
  4035. if (isVertical) {
  4036. // 垂直条形图,标签在底部
  4037. label.setAttribute("x", padding + index * labelSpacing + labelSpacing / 2)
  4038. label.setAttribute("y", svg.getAttribute("height") - padding / 3)
  4039. label.setAttribute("dominant-baseline", "hanging")
  4040. } else {
  4041. // 水平条形图,标签在左侧
  4042. label.setAttribute("x", padding / 2)
  4043. label.setAttribute("y", padding + index * labelSpacing + labelSpacing / 2)
  4044. label.setAttribute("dominant-baseline", "middle")
  4045. label.setAttribute("text-anchor", "end")
  4046. }
  4047. // 添加标签到组
  4048. labelGroup.appendChild(label)
  4049. })
  4050. // 将标签组添加到SVG
  4051. svg.appendChild(labelGroup)
  4052. return labelGroup
  4053. }
  4054. // 绘制网格线
  4055. const drawGrid = (svg, padding, width, height, maxValue) => {
  4056. // 创建网格组
  4057. const gridGroup = document.createElementNS("http://www.w3.org/2000/svg", "g")
  4058. gridGroup.setAttribute("class", "chart-grid")
  4059. // 设置网格线样式
  4060. const gridColor = "#e0e0e0"
  4061. const gridWidth = 1
  4062. // 计算网格间距
  4063. const gridCount = 5 // 网格线数量
  4064. const gridStep = (height - padding * 2) / gridCount
  4065. // 绘制水平网格线
  4066. for (let i = 0; i <= gridCount; i++) {
  4067. const y = padding + i * gridStep
  4068. const line = document.createElementNS("http://www.w3.org/2000/svg", "line")
  4069. line.setAttribute("x1", padding)
  4070. line.setAttribute("y1", y)
  4071. line.setAttribute("x2", width - padding)
  4072. line.setAttribute("y2", y)
  4073. line.setAttribute("stroke", gridColor)
  4074. line.setAttribute("stroke-width", gridWidth)
  4075. // 如果是底线,加粗显示
  4076. if (i === gridCount) {
  4077. line.setAttribute("stroke-width", gridWidth * 2)
  4078. line.setAttribute("stroke", "#aaa")
  4079. }
  4080. gridGroup.appendChild(line)
  4081. // 添加刻度值(除了底线)
  4082. if (i < gridCount) {
  4083. const valueText = document.createElementNS(
  4084. "http://www.w3.org/2000/svg",
  4085. "text"
  4086. )
  4087. const value = Math.round((gridCount - i) * (maxValue / gridCount))
  4088. valueText.textContent = value
  4089. valueText.setAttribute("x", padding - 5)
  4090. valueText.setAttribute("y", y)
  4091. valueText.setAttribute("text-anchor", "end")
  4092. valueText.setAttribute("dominant-baseline", "middle")
  4093. valueText.setAttribute("font-size", "10")
  4094. valueText.setAttribute("fill", "#666")
  4095. gridGroup.appendChild(valueText)
  4096. }
  4097. }
  4098. // 添加网格组到SVG
  4099. svg.appendChild(gridGroup)
  4100. return gridGroup
  4101. }
  4102. // 绘制图表图例
  4103. const drawLegend = (svg, series, colors, padding) => {
  4104. // 创建图例容器
  4105. const legendGroup = document.createElementNS(
  4106. "http://www.w3.org/2000/svg",
  4107. "g"
  4108. )
  4109. legendGroup.setAttribute("class", "chart-legend")
  4110. // 设置图例位置
  4111. const legendX = padding
  4112. const legendY = padding
  4113. let currentY = legendY
  4114. // 为每个系列创建图例项
  4115. series.forEach((item, index) => {
  4116. const itemGroup = document.createElementNS(
  4117. "http://www.w3.org/2000/svg",
  4118. "g"
  4119. )
  4120. itemGroup.setAttribute("transform", `translate(${legendX}, ${currentY})`)
  4121. // 创建图例颜色标记
  4122. const colorMark = document.createElementNS(
  4123. "http://www.w3.org/2000/svg",
  4124. "rect"
  4125. )
  4126. colorMark.setAttribute("width", "12")
  4127. colorMark.setAttribute("height", "12")
  4128. colorMark.setAttribute("fill", colors[index % colors.length])
  4129. // 创建图例文本
  4130. const text = document.createElementNS("http://www.w3.org/2000/svg", "text")
  4131. text.setAttribute("x", "20")
  4132. text.setAttribute("y", "10")
  4133. text.setAttribute("font-size", "12")
  4134. text.setAttribute("alignment-baseline", "middle")
  4135. text.textContent = item.name || `系列 ${index + 1}`
  4136. // 将元素添加到组中
  4137. itemGroup.appendChild(colorMark)
  4138. itemGroup.appendChild(text)
  4139. legendGroup.appendChild(itemGroup)
  4140. // 更新下一个图例项的Y坐标
  4141. currentY += 20
  4142. })
  4143. // 将图例组添加到SVG中
  4144. svg.appendChild(legendGroup)
  4145. return legendGroup
  4146. }
  4147. // 调整颜色亮度
  4148. const adjustBrightness = (color, factor) => {
  4149. // 如果颜色为空或不是字符串,返回原值
  4150. if (!color || typeof color !== "string") return color
  4151. // 处理十六进制颜色
  4152. if (color.startsWith("#")) {
  4153. let hex = color.substring(1)
  4154. // 处理简写形式 (#RGB)
  4155. if (hex.length === 3) {
  4156. hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]
  4157. }
  4158. // 解析RGB值
  4159. const r = parseInt(hex.substring(0, 2), 16)
  4160. const g = parseInt(hex.substring(2, 4), 16)
  4161. const b = parseInt(hex.substring(4, 6), 16)
  4162. // 调整亮度
  4163. const adjustedR = Math.min(255, Math.max(0, Math.round(r * factor)))
  4164. const adjustedG = Math.min(255, Math.max(0, Math.round(g * factor)))
  4165. const adjustedB = Math.min(255, Math.max(0, Math.round(b * factor)))
  4166. // 转换回十六进制
  4167. return `#${adjustedR.toString(16).padStart(2, "0")}${adjustedG
  4168. .toString(16)
  4169. .padStart(2, "0")}${adjustedB.toString(16).padStart(2, "0")}`
  4170. }
  4171. // 处理rgb/rgba颜色
  4172. if (color.startsWith("rgb")) {
  4173. const rgbMatch = color.match(
  4174. /rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/
  4175. )
  4176. if (rgbMatch) {
  4177. const r = parseInt(rgbMatch[1])
  4178. const g = parseInt(rgbMatch[2])
  4179. const b = parseInt(rgbMatch[3])
  4180. const a = rgbMatch[4] !== undefined ? parseFloat(rgbMatch[4]) : 1
  4181. // 调整亮度
  4182. const adjustedR = Math.min(255, Math.max(0, Math.round(r * factor)))
  4183. const adjustedG = Math.min(255, Math.max(0, Math.round(g * factor)))
  4184. const adjustedB = Math.min(255, Math.max(0, Math.round(b * factor)))
  4185. // 返回调整后的颜色
  4186. return a < 1
  4187. ? `rgba(${adjustedR}, ${adjustedG}, ${adjustedB}, ${a})`
  4188. : `rgb(${adjustedR}, ${adjustedG}, ${adjustedB})`
  4189. }
  4190. }
  4191. // 如果不是支持的颜色格式,返回原值
  4192. return color
  4193. }
  4194. const convertPtToPxInContent = (content) => {
  4195. if (!content) return ""
  4196. // 使用正则表达式查找所有包含pt单位的样式
  4197. const ptRegex = /(\d+(\.\d+)?)pt/g
  4198. // 将pt转换为px (1pt ≈ 1.33px)
  4199. return content.replace(ptRegex, (match, value) => {
  4200. const pxValue = Math.round(parseFloat(value) * 1.33 * 100) / 100
  4201. return `${pxValue}px`
  4202. })
  4203. }
  4204. const processElements = (elements, slideIndex) => {
  4205. if (!elements || !Array.isArray(elements)) return
  4206. // 对元素进行排序处理
  4207. elements.sort((a, b) => a.order - b.order)
  4208. // 处理每个元素
  4209. elements.forEach((element) => {
  4210. // 设置元素的幻灯片索引
  4211. element.slideIndex = slideIndex
  4212. // 处理元素中的子元素
  4213. if (element.elements && Array.isArray(element.elements)) {
  4214. processElements(element.elements, slideIndex)
  4215. }
  4216. // 处理特殊类型元素
  4217. if (element.type === "group" && element.elements) {
  4218. // 处理组元素
  4219. element.elements.forEach((childElement) => {
  4220. // 调整子元素位置为相对于组的位置
  4221. childElement.top = (childElement.top || 0) + (element.top || 0)
  4222. childElement.left = (childElement.left || 0) + (element.left || 0)
  4223. })
  4224. }
  4225. // 处理图表元素
  4226. if (element.type === "chart" && element.data) {
  4227. // 确保图表数据格式正确
  4228. if (!element.data.series) {
  4229. element.data.series = []
  4230. }
  4231. // 处理图表系列数据
  4232. element.data.series.forEach((series) => {
  4233. if (!series.values) {
  4234. series.values = []
  4235. }
  4236. })
  4237. }
  4238. })
  4239. return elements
  4240. }
  4241. // 监听 pptxJson 变化
  4242. watch(
  4243. () => props.pptxJson,
  4244. (newVal) => {
  4245. if (newVal) {
  4246. renderSlides()
  4247. }
  4248. },
  4249. { deep: true }
  4250. )
  4251. // 组件挂载时渲染幻灯片
  4252. onMounted(() => {
  4253. renderSlides()
  4254. })
  4255. </script>
  4256. <style scoped>
  4257. .pptx-container {
  4258. width: 100%;
  4259. overflow-x: auto;
  4260. padding: 20px 0;
  4261. }
  4262. </style>