ueditor.all.min.js 1.0 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246
  1. /*!
  2. * UEditor
  3. * version: ueditor
  4. * build: Wed Dec 26 2018 17:25:05 GMT+0800 (CST)
  5. */
  6. ;(function () {
  7. // editor.js
  8. UEDITOR_CONFIG = window.UEDITOR_CONFIG || {}
  9. var base_url = 'http://mentor.site.ximengnaikang.com'
  10. var baidu = window.baidu || {}
  11. window.baidu = baidu
  12. window.UE = baidu.editor = window.UE || {}
  13. UE.plugins = {}
  14. UE.commands = {}
  15. UE.instants = {}
  16. UE.I18N = {}
  17. UE._customizeUI = {}
  18. UE.version = '1.4.3'
  19. var dom = (UE.dom = {})
  20. // core/browser.js
  21. /**
  22. * 浏览器判断模块
  23. * @file
  24. * @module UE.browser
  25. * @since 1.2.6.1
  26. */
  27. /**
  28. * 提供浏览器检测的模块
  29. * @unfile
  30. * @module UE.browser
  31. */
  32. var browser = (UE.browser = (function () {
  33. var agent = navigator.userAgent.toLowerCase(),
  34. opera = window.opera,
  35. browser = {
  36. /**
  37. * @property {boolean} ie 检测当前浏览器是否为IE
  38. * @example
  39. * ```javascript
  40. * if ( UE.browser.ie ) {
  41. * console.log( '当前浏览器是IE' );
  42. * }
  43. * ```
  44. */
  45. ie: /(msie\s|trident.*rv:)([\w.]+)/.test(agent),
  46. /**
  47. * @property {boolean} opera 检测当前浏览器是否为Opera
  48. * @example
  49. * ```javascript
  50. * if ( UE.browser.opera ) {
  51. * console.log( '当前浏览器是Opera' );
  52. * }
  53. * ```
  54. */
  55. opera: !!opera && opera.version,
  56. /**
  57. * @property {boolean} webkit 检测当前浏览器是否是webkit内核的浏览器
  58. * @example
  59. * ```javascript
  60. * if ( UE.browser.webkit ) {
  61. * console.log( '当前浏览器是webkit内核浏览器' );
  62. * }
  63. * ```
  64. */
  65. webkit: agent.indexOf(' applewebkit/') > -1,
  66. /**
  67. * @property {boolean} mac 检测当前浏览器是否是运行在mac平台下
  68. * @example
  69. * ```javascript
  70. * if ( UE.browser.mac ) {
  71. * console.log( '当前浏览器运行在mac平台下' );
  72. * }
  73. * ```
  74. */
  75. mac: agent.indexOf('macintosh') > -1,
  76. /**
  77. * @property {boolean} quirks 检测当前浏览器是否处于“怪异模式”下
  78. * @example
  79. * ```javascript
  80. * if ( UE.browser.quirks ) {
  81. * console.log( '当前浏览器运行处于“怪异模式”' );
  82. * }
  83. * ```
  84. */
  85. quirks: document.compatMode == 'BackCompat',
  86. }
  87. /**
  88. * @property {boolean} gecko 检测当前浏览器内核是否是gecko内核
  89. * @example
  90. * ```javascript
  91. * if ( UE.browser.gecko ) {
  92. * console.log( '当前浏览器内核是gecko内核' );
  93. * }
  94. * ```
  95. */
  96. browser.gecko =
  97. navigator.product == 'Gecko' &&
  98. !browser.webkit &&
  99. !browser.opera &&
  100. !browser.ie
  101. var version = 0
  102. // Internet Explorer 6.0+
  103. if (browser.ie) {
  104. var v1 = agent.match(/(?:msie\s([\w.]+))/)
  105. var v2 = agent.match(/(?:trident.*rv:([\w.]+))/)
  106. if (v1 && v2 && v1[1] && v2[1]) {
  107. version = Math.max(v1[1] * 1, v2[1] * 1)
  108. } else if (v1 && v1[1]) {
  109. version = v1[1] * 1
  110. } else if (v2 && v2[1]) {
  111. version = v2[1] * 1
  112. } else {
  113. version = 0
  114. }
  115. browser.ie11Compat = document.documentMode == 11
  116. /**
  117. * @property { boolean } ie9Compat 检测浏览器模式是否为 IE9 兼容模式
  118. * @warning 如果浏览器不是IE, 则该值为undefined
  119. * @example
  120. * ```javascript
  121. * if ( UE.browser.ie9Compat ) {
  122. * console.log( '当前浏览器运行在IE9兼容模式下' );
  123. * }
  124. * ```
  125. */
  126. browser.ie9Compat = document.documentMode == 9
  127. /**
  128. * @property { boolean } ie8 检测浏览器是否是IE8浏览器
  129. * @warning 如果浏览器不是IE, 则该值为undefined
  130. * @example
  131. * ```javascript
  132. * if ( UE.browser.ie8 ) {
  133. * console.log( '当前浏览器是IE8浏览器' );
  134. * }
  135. * ```
  136. */
  137. browser.ie8 = !!document.documentMode
  138. /**
  139. * @property { boolean } ie8Compat 检测浏览器模式是否为 IE8 兼容模式
  140. * @warning 如果浏览器不是IE, 则该值为undefined
  141. * @example
  142. * ```javascript
  143. * if ( UE.browser.ie8Compat ) {
  144. * console.log( '当前浏览器运行在IE8兼容模式下' );
  145. * }
  146. * ```
  147. */
  148. browser.ie8Compat = document.documentMode == 8
  149. /**
  150. * @property { boolean } ie7Compat 检测浏览器模式是否为 IE7 兼容模式
  151. * @warning 如果浏览器不是IE, 则该值为undefined
  152. * @example
  153. * ```javascript
  154. * if ( UE.browser.ie7Compat ) {
  155. * console.log( '当前浏览器运行在IE7兼容模式下' );
  156. * }
  157. * ```
  158. */
  159. browser.ie7Compat =
  160. (version == 7 && !document.documentMode) || document.documentMode == 7
  161. /**
  162. * @property { boolean } ie6Compat 检测浏览器模式是否为 IE6 模式 或者怪异模式
  163. * @warning 如果浏览器不是IE, 则该值为undefined
  164. * @example
  165. * ```javascript
  166. * if ( UE.browser.ie6Compat ) {
  167. * console.log( '当前浏览器运行在IE6模式或者怪异模式下' );
  168. * }
  169. * ```
  170. */
  171. browser.ie6Compat = version < 7 || browser.quirks
  172. browser.ie9above = version > 8
  173. browser.ie9below = version < 9
  174. browser.ie11above = version > 10
  175. browser.ie11below = version < 11
  176. }
  177. // Gecko.
  178. if (browser.gecko) {
  179. var geckoRelease = agent.match(/rv:([\d\.]+)/)
  180. if (geckoRelease) {
  181. geckoRelease = geckoRelease[1].split('.')
  182. version =
  183. geckoRelease[0] * 10000 +
  184. (geckoRelease[1] || 0) * 100 +
  185. (geckoRelease[2] || 0) * 1
  186. }
  187. }
  188. /**
  189. * @property { Number } chrome 检测当前浏览器是否为Chrome, 如果是,则返回Chrome的大版本号
  190. * @warning 如果浏览器不是chrome, 则该值为undefined
  191. * @example
  192. * ```javascript
  193. * if ( UE.browser.chrome ) {
  194. * console.log( '当前浏览器是Chrome' );
  195. * }
  196. * ```
  197. */
  198. if (/chrome\/(\d+\.\d)/i.test(agent)) {
  199. browser.chrome = +RegExp['\x241']
  200. }
  201. /**
  202. * @property { Number } safari 检测当前浏览器是否为Safari, 如果是,则返回Safari的大版本号
  203. * @warning 如果浏览器不是safari, 则该值为undefined
  204. * @example
  205. * ```javascript
  206. * if ( UE.browser.safari ) {
  207. * console.log( '当前浏览器是Safari' );
  208. * }
  209. * ```
  210. */
  211. if (
  212. /(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) &&
  213. !/chrome/i.test(agent)
  214. ) {
  215. browser.safari = +(RegExp['\x241'] || RegExp['\x242'])
  216. }
  217. // Opera 9.50+
  218. if (browser.opera) version = parseFloat(opera.version())
  219. // WebKit 522+ (Safari 3+)
  220. if (browser.webkit)
  221. version = parseFloat(agent.match(/ applewebkit\/(\d+)/)[1])
  222. /**
  223. * @property { Number } version 检测当前浏览器版本号
  224. * @remind
  225. * <ul>
  226. * <li>IE系列返回值为5,6,7,8,9,10等</li>
  227. * <li>gecko系列会返回10900,158900等</li>
  228. * <li>webkit系列会返回其build号 (如 522等)</li>
  229. * </ul>
  230. * @example
  231. * ```javascript
  232. * console.log( '当前浏览器版本号是: ' + UE.browser.version );
  233. * ```
  234. */
  235. browser.version = version
  236. /**
  237. * @property { boolean } isCompatible 检测当前浏览器是否能够与UEditor良好兼容
  238. * @example
  239. * ```javascript
  240. * if ( UE.browser.isCompatible ) {
  241. * console.log( '浏览器与UEditor能够良好兼容' );
  242. * }
  243. * ```
  244. */
  245. browser.isCompatible =
  246. !browser.mobile &&
  247. ((browser.ie && version >= 6) ||
  248. (browser.gecko && version >= 10801) ||
  249. (browser.opera && version >= 9.5) ||
  250. (browser.air && version >= 1) ||
  251. (browser.webkit && version >= 522) ||
  252. false)
  253. return browser
  254. })())
  255. //快捷方式
  256. var ie = browser.ie,
  257. webkit = browser.webkit,
  258. gecko = browser.gecko,
  259. opera = browser.opera
  260. // core/utils.js
  261. /**
  262. * 工具函数包
  263. * @file
  264. * @module UE.utils
  265. * @since 1.2.6.1
  266. */
  267. /**
  268. * UEditor封装使用的静态工具函数
  269. * @module UE.utils
  270. * @unfile
  271. */
  272. var utils = (UE.utils = {
  273. /**
  274. * 用给定的迭代器遍历对象
  275. * @method each
  276. * @param { Object } obj 需要遍历的对象
  277. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  278. * @example
  279. * ```javascript
  280. * var demoObj = {
  281. * key1: 1,
  282. * key2: 2
  283. * };
  284. *
  285. * //output: key1: 1, key2: 2
  286. * UE.utils.each( demoObj, funciton ( value, key ) {
  287. *
  288. * console.log( key + ":" + value );
  289. *
  290. * } );
  291. * ```
  292. */
  293. /**
  294. * 用给定的迭代器遍历数组或类数组对象
  295. * @method each
  296. * @param { Array } array 需要遍历的数组或者类数组
  297. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  298. * @example
  299. * ```javascript
  300. * var divs = document.getElmentByTagNames( "div" );
  301. *
  302. * //output: 0: DIV, 1: DIV ...
  303. * UE.utils.each( divs, funciton ( value, key ) {
  304. *
  305. * console.log( key + ":" + value.tagName );
  306. *
  307. * } );
  308. * ```
  309. */
  310. each: function (obj, iterator, context) {
  311. if (obj == null) return
  312. if (obj.length === +obj.length) {
  313. for (var i = 0, l = obj.length; i < l; i++) {
  314. if (iterator.call(context, obj[i], i, obj) === false) return false
  315. }
  316. } else {
  317. for (var key in obj) {
  318. if (obj.hasOwnProperty(key)) {
  319. if (iterator.call(context, obj[key], key, obj) === false)
  320. return false
  321. }
  322. }
  323. }
  324. },
  325. /**
  326. * 以给定对象作为原型创建一个新对象
  327. * @method makeInstance
  328. * @param { Object } protoObject 该对象将作为新创建对象的原型
  329. * @return { Object } 新的对象, 该对象的原型是给定的protoObject对象
  330. * @example
  331. * ```javascript
  332. *
  333. * var protoObject = { sayHello: function () { console.log('Hello UEditor!'); } };
  334. *
  335. * var newObject = UE.utils.makeInstance( protoObject );
  336. * //output: Hello UEditor!
  337. * newObject.sayHello();
  338. * ```
  339. */
  340. makeInstance: function (obj) {
  341. var noop = new Function()
  342. noop.prototype = obj
  343. obj = new noop()
  344. noop.prototype = null
  345. return obj
  346. },
  347. /**
  348. * 将source对象中的属性扩展到target对象上
  349. * @method extend
  350. * @remind 该方法将强制把source对象上的属性复制到target对象上
  351. * @see UE.utils.extend(Object,Object,Boolean)
  352. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  353. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  354. * @return { Object } 返回target对象
  355. * @example
  356. * ```javascript
  357. *
  358. * var target = { name: 'target', sex: 1 },
  359. * source = { name: 'source', age: 17 };
  360. *
  361. * UE.utils.extend( target, source );
  362. *
  363. * //output: { name: 'source', sex: 1, age: 17 }
  364. * console.log( target );
  365. *
  366. * ```
  367. */
  368. /**
  369. * 将source对象中的属性扩展到target对象上, 根据指定的isKeepTarget值决定是否保留目标对象中与
  370. * 源对象属性名相同的属性值。
  371. * @method extend
  372. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  373. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  374. * @param { Boolean } isKeepTarget 是否保留目标对象中与源对象中属性名相同的属性
  375. * @return { Object } 返回target对象
  376. * @example
  377. * ```javascript
  378. *
  379. * var target = { name: 'target', sex: 1 },
  380. * source = { name: 'source', age: 17 };
  381. *
  382. * UE.utils.extend( target, source, true );
  383. *
  384. * //output: { name: 'target', sex: 1, age: 17 }
  385. * console.log( target );
  386. *
  387. * ```
  388. */
  389. extend: function (t, s, b) {
  390. if (s) {
  391. for (var k in s) {
  392. if (!b || !t.hasOwnProperty(k)) {
  393. t[k] = s[k]
  394. }
  395. }
  396. }
  397. return t
  398. },
  399. /**
  400. * 将给定的多个对象的属性复制到目标对象target上
  401. * @method extend2
  402. * @remind 该方法将强制把源对象上的属性复制到target对象上
  403. * @remind 该方法支持两个及以上的参数, 从第二个参数开始, 其属性都会被复制到第一个参数上。 如果遇到同名的属性,
  404. * 将会覆盖掉之前的值。
  405. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  406. * @param { Object... } source 源对象, 支持多个对象, 该对象的属性会被附加到target对象上
  407. * @return { Object } 返回target对象
  408. * @example
  409. * ```javascript
  410. *
  411. * var target = {},
  412. * source1 = { name: 'source', age: 17 },
  413. * source2 = { title: 'dev' };
  414. *
  415. * UE.utils.extend2( target, source1, source2 );
  416. *
  417. * //output: { name: 'source', age: 17, title: 'dev' }
  418. * console.log( target );
  419. *
  420. * ```
  421. */
  422. extend2: function (t) {
  423. var a = arguments
  424. for (var i = 1; i < a.length; i++) {
  425. var x = a[i]
  426. for (var k in x) {
  427. if (!t.hasOwnProperty(k)) {
  428. t[k] = x[k]
  429. }
  430. }
  431. }
  432. return t
  433. },
  434. /**
  435. * 模拟继承机制, 使得subClass继承自superClass
  436. * @method inherits
  437. * @param { Object } subClass 子类对象
  438. * @param { Object } superClass 超类对象
  439. * @warning 该方法只能让subClass继承超类的原型, subClass对象自身的属性和方法不会被继承
  440. * @return { Object } 继承superClass后的子类对象
  441. * @example
  442. * ```javascript
  443. * function SuperClass(){
  444. * this.name = "小李";
  445. * }
  446. *
  447. * SuperClass.prototype = {
  448. * hello:function(str){
  449. * console.log(this.name + str);
  450. * }
  451. * }
  452. *
  453. * function SubClass(){
  454. * this.name = "小张";
  455. * }
  456. *
  457. * UE.utils.inherits(SubClass,SuperClass);
  458. *
  459. * var sub = new SubClass();
  460. * //output: '小张早上好!
  461. * sub.hello("早上好!");
  462. * ```
  463. */
  464. inherits: function (subClass, superClass) {
  465. var oldP = subClass.prototype,
  466. newP = utils.makeInstance(superClass.prototype)
  467. utils.extend(newP, oldP, true)
  468. subClass.prototype = newP
  469. return (newP.constructor = subClass)
  470. },
  471. /**
  472. * 用指定的context对象作为函数fn的上下文
  473. * @method bind
  474. * @param { Function } fn 需要绑定上下文的函数对象
  475. * @param { Object } content 函数fn新的上下文对象
  476. * @return { Function } 一个新的函数, 该函数作为原始函数fn的代理, 将完成fn的上下文调换工作。
  477. * @example
  478. * ```javascript
  479. *
  480. * var name = 'window',
  481. * newTest = null;
  482. *
  483. * function test () {
  484. * console.log( this.name );
  485. * }
  486. *
  487. * newTest = UE.utils.bind( test, { name: 'object' } );
  488. *
  489. * //output: object
  490. * newTest();
  491. *
  492. * //output: window
  493. * test();
  494. *
  495. * ```
  496. */
  497. bind: function (fn, context) {
  498. return function () {
  499. return fn.apply(context, arguments)
  500. }
  501. },
  502. /**
  503. * 创建延迟指定时间后执行的函数fn
  504. * @method defer
  505. * @param { Function } fn 需要延迟执行的函数对象
  506. * @param { int } delay 延迟的时间, 单位是毫秒
  507. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  508. * 而不能保证刚好到达延迟时间时执行。
  509. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  510. * @example
  511. * ```javascript
  512. * var start = 0;
  513. *
  514. * function test(){
  515. * console.log( new Date() - start );
  516. * }
  517. *
  518. * var testDefer = UE.utils.defer( test, 1000 );
  519. * //
  520. * start = new Date();
  521. * //output: (大约在1000毫秒之后输出) 1000
  522. * testDefer();
  523. * ```
  524. */
  525. /**
  526. * 创建延迟指定时间后执行的函数fn, 如果在延迟时间内再次执行该方法, 将会根据指定的exclusion的值,
  527. * 决定是否取消前一次函数的执行, 如果exclusion的值为true, 则取消执行,反之,将继续执行前一个方法。
  528. * @method defer
  529. * @param { Function } fn 需要延迟执行的函数对象
  530. * @param { int } delay 延迟的时间, 单位是毫秒
  531. * @param { Boolean } exclusion 如果在延迟时间内再次执行该函数,该值将决定是否取消执行前一次函数的执行,
  532. * 值为true表示取消执行, 反之则将在执行前一次函数之后才执行本次函数调用。
  533. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  534. * 而不能保证刚好到达延迟时间时执行。
  535. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  536. * @example
  537. * ```javascript
  538. *
  539. * function test(){
  540. * console.log(1);
  541. * }
  542. *
  543. * var testDefer = UE.utils.defer( test, 1000, true );
  544. *
  545. * //output: (两次调用仅有一次输出) 1
  546. * testDefer();
  547. * testDefer();
  548. * ```
  549. */
  550. defer: function (fn, delay, exclusion) {
  551. var timerID
  552. return function () {
  553. if (exclusion) {
  554. clearTimeout(timerID)
  555. }
  556. timerID = setTimeout(fn, delay)
  557. }
  558. },
  559. /**
  560. * 获取元素item在数组array中首次出现的位置, 如果未找到item, 则返回-1
  561. * @method indexOf
  562. * @remind 该方法的匹配过程使用的是恒等“===”
  563. * @param { Array } array 需要查找的数组对象
  564. * @param { * } item 需要在目标数组中查找的值
  565. * @return { int } 返回item在目标数组array中首次出现的位置, 如果在数组中未找到item, 则返回-1
  566. * @example
  567. * ```javascript
  568. * var item = 1,
  569. * arr = [ 3, 4, 6, 8, 1, 1, 2 ];
  570. *
  571. * //output: 4
  572. * console.log( UE.utils.indexOf( arr, item ) );
  573. * ```
  574. */
  575. /**
  576. * 获取元素item数组array中首次出现的位置, 如果未找到item, 则返回-1。通过start的值可以指定搜索的起始位置。
  577. * @method indexOf
  578. * @remind 该方法的匹配过程使用的是恒等“===”
  579. * @param { Array } array 需要查找的数组对象
  580. * @param { * } item 需要在目标数组中查找的值
  581. * @param { int } start 搜索的起始位置
  582. * @return { int } 返回item在目标数组array中的start位置之后首次出现的位置, 如果在数组中未找到item, 则返回-1
  583. * @example
  584. * ```javascript
  585. * var item = 1,
  586. * arr = [ 3, 4, 6, 8, 1, 2, 8, 3, 2, 1, 1, 4 ];
  587. *
  588. * //output: 9
  589. * console.log( UE.utils.indexOf( arr, item, 5 ) );
  590. * ```
  591. */
  592. indexOf: function (array, item, start) {
  593. var index = -1
  594. start = this.isNumber(start) ? start : 0
  595. this.each(array, function (v, i) {
  596. if (i >= start && v === item) {
  597. index = i
  598. return false
  599. }
  600. })
  601. return index
  602. },
  603. /**
  604. * 移除数组array中所有的元素item
  605. * @method removeItem
  606. * @param { Array } array 要移除元素的目标数组
  607. * @param { * } item 将要被移除的元素
  608. * @remind 该方法的匹配过程使用的是恒等“===”
  609. * @example
  610. * ```javascript
  611. * var arr = [ 4, 5, 7, 1, 3, 4, 6 ];
  612. *
  613. * UE.utils.removeItem( arr, 4 );
  614. * //output: [ 5, 7, 1, 3, 6 ]
  615. * console.log( arr );
  616. *
  617. * ```
  618. */
  619. removeItem: function (array, item) {
  620. for (var i = 0, l = array.length; i < l; i++) {
  621. if (array[i] === item) {
  622. array.splice(i, 1)
  623. i--
  624. }
  625. }
  626. },
  627. /**
  628. * 删除字符串str的首尾空格
  629. * @method trim
  630. * @param { String } str 需要删除首尾空格的字符串
  631. * @return { String } 删除了首尾的空格后的字符串
  632. * @example
  633. * ```javascript
  634. *
  635. * var str = " UEdtior ";
  636. *
  637. * //output: 9
  638. * console.log( str.length );
  639. *
  640. * //output: 7
  641. * console.log( UE.utils.trim( " UEdtior " ).length );
  642. *
  643. * //output: 9
  644. * console.log( str.length );
  645. *
  646. * ```
  647. */
  648. trim: function (str) {
  649. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, '')
  650. },
  651. /**
  652. * 将字符串str以','分隔成数组后,将该数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  653. * @method listToMap
  654. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  655. * @param { String } str 该字符串将被以','分割为数组, 然后进行转化
  656. * @return { Object } 转化之后的hash对象
  657. * @example
  658. * ```javascript
  659. *
  660. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  661. * console.log( UE.utils.listToMap( 'UEdtior,Hello' ) );
  662. *
  663. * ```
  664. */
  665. /**
  666. * 将字符串数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  667. * @method listToMap
  668. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  669. * @param { Array } arr 字符串数组
  670. * @return { Object } 转化之后的hash对象
  671. * @example
  672. * ```javascript
  673. *
  674. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  675. * console.log( UE.utils.listToMap( [ 'UEdtior', 'Hello' ] ) );
  676. *
  677. * ```
  678. */
  679. listToMap: function (list) {
  680. if (!list) return {}
  681. list = utils.isArray(list) ? list : list.split(',')
  682. for (var i = 0, ci, obj = {}; (ci = list[i++]); ) {
  683. obj[ci.toUpperCase()] = obj[ci] = 1
  684. }
  685. return obj
  686. },
  687. /**
  688. * 将str中的html符号转义,将转义“',&,<,",>”五个字符
  689. * @method unhtml
  690. * @param { String } str 需要转义的字符串
  691. * @return { String } 转义后的字符串
  692. * @example
  693. * ```javascript
  694. * var html = '<body>&</body>';
  695. *
  696. * //output: &lt;body&gt;&amp;&lt;/body&gt;
  697. * console.log( UE.utils.unhtml( html ) );
  698. *
  699. * ```
  700. */
  701. unhtml: function (str, reg) {
  702. return str
  703. ? str.replace(
  704. reg || /[&<">'](?:(amp|lt|quot|gt|#39|nbsp|#\d+);)?/g,
  705. function (a, b) {
  706. if (b) {
  707. return a
  708. } else {
  709. return {
  710. '<': '&lt;',
  711. '&': '&amp;',
  712. '"': '&quot;',
  713. '>': '&gt;',
  714. "'": '&#39;',
  715. }[a]
  716. }
  717. }
  718. )
  719. : ''
  720. },
  721. /**
  722. * 将url中的html字符转义, 仅转义 ', ", <, > 四个字符
  723. * @param { String } str 需要转义的字符串
  724. * @param { RegExp } reg 自定义的正则
  725. * @return { String } 转义后的字符串
  726. */
  727. unhtmlForUrl: function (str, reg) {
  728. return str
  729. ? str.replace(reg || /[<">']/g, function (a) {
  730. return {
  731. '<': '&lt;',
  732. '&': '&amp;',
  733. '"': '&quot;',
  734. '>': '&gt;',
  735. "'": '&#39;',
  736. }[a]
  737. })
  738. : ''
  739. },
  740. /**
  741. * 将str中的转义字符还原成html字符
  742. * @see UE.utils.unhtml(String);
  743. * @method html
  744. * @param { String } str 需要逆转义的字符串
  745. * @return { String } 逆转义后的字符串
  746. * @example
  747. * ```javascript
  748. *
  749. * var str = '&lt;body&gt;&amp;&lt;/body&gt;';
  750. *
  751. * //output: <body>&</body>
  752. * console.log( UE.utils.html( str ) );
  753. *
  754. * ```
  755. */
  756. html: function (str) {
  757. return str
  758. ? str.replace(/&((g|l|quo)t|amp|#39|nbsp);/g, function (m) {
  759. return {
  760. '&lt;': '<',
  761. '&amp;': '&',
  762. '&quot;': '"',
  763. '&gt;': '>',
  764. '&#39;': "'",
  765. '&nbsp;': ' ',
  766. }[m]
  767. })
  768. : ''
  769. },
  770. /**
  771. * 将css样式转换为驼峰的形式
  772. * @method cssStyleToDomStyle
  773. * @param { String } cssName 需要转换的css样式名
  774. * @return { String } 转换成驼峰形式后的css样式名
  775. * @example
  776. * ```javascript
  777. *
  778. * var str = 'border-top';
  779. *
  780. * //output: borderTop
  781. * console.log( UE.utils.cssStyleToDomStyle( str ) );
  782. *
  783. * ```
  784. */
  785. cssStyleToDomStyle: (function () {
  786. var test = document.createElement('div').style,
  787. cache = {
  788. float:
  789. test.cssFloat != undefined
  790. ? 'cssFloat'
  791. : test.styleFloat != undefined
  792. ? 'styleFloat'
  793. : 'float',
  794. }
  795. return function (cssName) {
  796. return (
  797. cache[cssName] ||
  798. (cache[cssName] = cssName
  799. .toLowerCase()
  800. .replace(/-./g, function (match) {
  801. return match.charAt(1).toUpperCase()
  802. }))
  803. )
  804. }
  805. })(),
  806. /**
  807. * 动态加载文件到doc中
  808. * @method loadFile
  809. * @param { DomDocument } document 需要加载资源文件的文档对象
  810. * @param { Object } options 加载资源文件的属性集合, 取值请参考代码示例
  811. * @example
  812. * ```javascript
  813. *
  814. * UE.utils.loadFile( document, {
  815. * src:"test.js",
  816. * tag:"script",
  817. * type:"text/javascript",
  818. * defer:"defer"
  819. * } );
  820. *
  821. * ```
  822. */
  823. /**
  824. * 动态加载文件到doc中,加载成功后执行的回调函数fn
  825. * @method loadFile
  826. * @param { DomDocument } document 需要加载资源文件的文档对象
  827. * @param { Object } options 加载资源文件的属性集合, 该集合支持的值是script标签和style标签支持的所有属性。
  828. * @param { Function } fn 资源文件加载成功之后执行的回调
  829. * @warning 对于在同一个文档中多次加载同一URL的文件, 该方法会在第一次加载之后缓存该请求,
  830. * 在此之后的所有同一URL的请求, 将会直接触发回调。
  831. * @example
  832. * ```javascript
  833. *
  834. * UE.utils.loadFile( document, {
  835. * src:"test.js",
  836. * tag:"script",
  837. * type:"text/javascript",
  838. * defer:"defer"
  839. * }, function () {
  840. * console.log('加载成功');
  841. * } );
  842. *
  843. * ```
  844. */
  845. loadFile: (function () {
  846. var tmpList = []
  847. function getItem(doc, obj) {
  848. try {
  849. for (var i = 0, ci; (ci = tmpList[i++]); ) {
  850. if (ci.doc === doc && ci.url == (obj.src || obj.href)) {
  851. return ci
  852. }
  853. }
  854. } catch (e) {
  855. return null
  856. }
  857. }
  858. return function (doc, obj, fn) {
  859. var item = getItem(doc, obj)
  860. if (item) {
  861. if (item.ready) {
  862. fn && fn()
  863. } else {
  864. item.funs.push(fn)
  865. }
  866. return
  867. }
  868. tmpList.push({
  869. doc: doc,
  870. url: obj.src || obj.href,
  871. funs: [fn],
  872. })
  873. if (!doc.body) {
  874. var html = []
  875. for (var p in obj) {
  876. if (p == 'tag') continue
  877. html.push(p + '="' + obj[p] + '"')
  878. }
  879. doc.write(
  880. '<' + obj.tag + ' ' + html.join(' ') + ' ></' + obj.tag + '>'
  881. )
  882. return
  883. }
  884. if (obj.id && doc.getElementById(obj.id)) {
  885. return
  886. }
  887. var element = doc.createElement(obj.tag)
  888. delete obj.tag
  889. for (var p in obj) {
  890. element.setAttribute(p, obj[p])
  891. }
  892. element.onload = element.onreadystatechange = function () {
  893. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  894. item = getItem(doc, obj)
  895. if (item.funs.length > 0) {
  896. item.ready = 1
  897. for (var fi; (fi = item.funs.pop()); ) {
  898. fi()
  899. }
  900. }
  901. element.onload = element.onreadystatechange = null
  902. }
  903. }
  904. element.onerror = function () {
  905. throw Error(
  906. 'The load ' +
  907. (obj.href || obj.src) +
  908. ' fails,check the url settings of file ueditor.config.js '
  909. )
  910. }
  911. doc.getElementsByTagName('head')[0].appendChild(element)
  912. }
  913. })(),
  914. /**
  915. * 判断obj对象是否为空
  916. * @method isEmptyObject
  917. * @param { * } obj 需要判断的对象
  918. * @remind 如果判断的对象是NULL, 将直接返回true, 如果是数组且为空, 返回true, 如果是字符串, 且字符串为空,
  919. * 返回true, 如果是普通对象, 且该对象没有任何实例属性, 返回true
  920. * @return { Boolean } 对象是否为空
  921. * @example
  922. * ```javascript
  923. *
  924. * //output: true
  925. * console.log( UE.utils.isEmptyObject( {} ) );
  926. *
  927. * //output: true
  928. * console.log( UE.utils.isEmptyObject( [] ) );
  929. *
  930. * //output: true
  931. * console.log( UE.utils.isEmptyObject( "" ) );
  932. *
  933. * //output: false
  934. * console.log( UE.utils.isEmptyObject( { key: 1 } ) );
  935. *
  936. * //output: false
  937. * console.log( UE.utils.isEmptyObject( [1] ) );
  938. *
  939. * //output: false
  940. * console.log( UE.utils.isEmptyObject( "1" ) );
  941. *
  942. * ```
  943. */
  944. isEmptyObject: function (obj) {
  945. if (obj == null) return true
  946. if (this.isArray(obj) || this.isString(obj)) return obj.length === 0
  947. for (var key in obj) if (obj.hasOwnProperty(key)) return false
  948. return true
  949. },
  950. /**
  951. * 把rgb格式的颜色值转换成16进制格式
  952. * @method fixColor
  953. * @param { String } rgb格式的颜色值
  954. * @param { String }
  955. * @example
  956. * rgb(255,255,255) => "#ffffff"
  957. */
  958. fixColor: function (name, value) {
  959. if (/color/i.test(name) && /rgba?/.test(value)) {
  960. var array = value.split(',')
  961. if (array.length > 3) return ''
  962. value = '#'
  963. for (var i = 0, color; (color = array[i++]); ) {
  964. color = parseInt(color.replace(/[^\d]/gi, ''), 10).toString(16)
  965. value += color.length == 1 ? '0' + color : color
  966. }
  967. value = value.toUpperCase()
  968. }
  969. return value
  970. },
  971. /**
  972. * 只针对border,padding,margin做了处理,因为性能问题
  973. * @public
  974. * @function
  975. * @param {String} val style字符串
  976. */
  977. optCss: function (val) {
  978. var padding, margin, border
  979. val = val.replace(
  980. /(padding|margin|border)\-([^:]+):([^;]+);?/gi,
  981. function (str, key, name, val) {
  982. if (val.split(' ').length == 1) {
  983. switch (key) {
  984. case 'padding':
  985. !padding && (padding = {})
  986. padding[name] = val
  987. return ''
  988. case 'margin':
  989. !margin && (margin = {})
  990. margin[name] = val
  991. return ''
  992. case 'border':
  993. return val == 'initial' ? '' : str
  994. }
  995. }
  996. return str
  997. }
  998. )
  999. function opt(obj, name) {
  1000. if (!obj) {
  1001. return ''
  1002. }
  1003. var t = obj.top,
  1004. b = obj.bottom,
  1005. l = obj.left,
  1006. r = obj.right,
  1007. val = ''
  1008. if (!t || !l || !b || !r) {
  1009. for (var p in obj) {
  1010. val += ';' + name + '-' + p + ':' + obj[p] + ';'
  1011. }
  1012. } else {
  1013. val +=
  1014. ';' +
  1015. name +
  1016. ':' +
  1017. (t == b && b == l && l == r
  1018. ? t
  1019. : t == b && l == r
  1020. ? t + ' ' + l
  1021. : l == r
  1022. ? t + ' ' + l + ' ' + b
  1023. : t + ' ' + r + ' ' + b + ' ' + l) +
  1024. ';'
  1025. }
  1026. return val
  1027. }
  1028. val += opt(padding, 'padding') + opt(margin, 'margin')
  1029. return val
  1030. .replace(/^[ \n\r\t;]*|[ \n\r\t]*$/, '')
  1031. .replace(/;([ \n\r\t]+)|\1;/g, ';')
  1032. .replace(/(&((l|g)t|quot|#39))?;{2,}/g, function (a, b) {
  1033. return b ? b + ';;' : ';'
  1034. })
  1035. },
  1036. /**
  1037. * 克隆对象
  1038. * @method clone
  1039. * @param { Object } source 源对象
  1040. * @return { Object } source的一个副本
  1041. */
  1042. /**
  1043. * 深度克隆对象,将source的属性克隆到target对象, 会覆盖target重名的属性。
  1044. * @method clone
  1045. * @param { Object } source 源对象
  1046. * @param { Object } target 目标对象
  1047. * @return { Object } 附加了source对象所有属性的target对象
  1048. */
  1049. clone: function (source, target) {
  1050. var tmp
  1051. target = target || {}
  1052. for (var i in source) {
  1053. if (source.hasOwnProperty(i)) {
  1054. tmp = source[i]
  1055. if (typeof tmp == 'object') {
  1056. target[i] = utils.isArray(tmp) ? [] : {}
  1057. utils.clone(source[i], target[i])
  1058. } else {
  1059. target[i] = tmp
  1060. }
  1061. }
  1062. }
  1063. return target
  1064. },
  1065. /**
  1066. * 把cm/pt为单位的值转换为px为单位的值
  1067. * @method transUnitToPx
  1068. * @param { String } 待转换的带单位的字符串
  1069. * @return { String } 转换为px为计量单位的值的字符串
  1070. * @example
  1071. * ```javascript
  1072. *
  1073. * //output: 500px
  1074. * console.log( UE.utils.transUnitToPx( '20cm' ) );
  1075. *
  1076. * //output: 27px
  1077. * console.log( UE.utils.transUnitToPx( '20pt' ) );
  1078. *
  1079. * ```
  1080. */
  1081. transUnitToPx: function (val) {
  1082. if (!/(pt|cm)/.test(val)) {
  1083. return val
  1084. }
  1085. var unit
  1086. val.replace(/([\d.]+)(\w+)/, function (str, v, u) {
  1087. val = v
  1088. unit = u
  1089. })
  1090. switch (unit) {
  1091. case 'cm':
  1092. val = parseFloat(val) * 25
  1093. break
  1094. case 'pt':
  1095. val = Math.round((parseFloat(val) * 96) / 72)
  1096. }
  1097. return val + (val ? 'px' : '')
  1098. },
  1099. /**
  1100. * 在dom树ready之后执行给定的回调函数
  1101. * @method domReady
  1102. * @remind 如果在执行该方法的时候, dom树已经ready, 那么回调函数将立刻执行
  1103. * @param { Function } fn dom树ready之后的回调函数
  1104. * @example
  1105. * ```javascript
  1106. *
  1107. * UE.utils.domReady( function () {
  1108. *
  1109. * console.log('123');
  1110. *
  1111. * } );
  1112. *
  1113. * ```
  1114. */
  1115. domReady: (function () {
  1116. var fnArr = []
  1117. function doReady(doc) {
  1118. //确保onready只执行一次
  1119. doc.isReady = true
  1120. for (var ci; (ci = fnArr.pop()); ci()) {}
  1121. }
  1122. return function (onready, win) {
  1123. win = win || window
  1124. var doc = win.document
  1125. onready && fnArr.push(onready)
  1126. if (doc.readyState === 'complete') {
  1127. doReady(doc)
  1128. } else {
  1129. doc.isReady && doReady(doc)
  1130. if (browser.ie && browser.version != 11) {
  1131. ;(function () {
  1132. if (doc.isReady) return
  1133. try {
  1134. doc.documentElement.doScroll('left')
  1135. } catch (error) {
  1136. setTimeout(arguments.callee, 0)
  1137. return
  1138. }
  1139. doReady(doc)
  1140. })()
  1141. win.attachEvent('onload', function () {
  1142. doReady(doc)
  1143. })
  1144. } else {
  1145. doc.addEventListener(
  1146. 'DOMContentLoaded',
  1147. function () {
  1148. doc.removeEventListener(
  1149. 'DOMContentLoaded',
  1150. arguments.callee,
  1151. false
  1152. )
  1153. doReady(doc)
  1154. },
  1155. false
  1156. )
  1157. win.addEventListener(
  1158. 'load',
  1159. function () {
  1160. doReady(doc)
  1161. },
  1162. false
  1163. )
  1164. }
  1165. }
  1166. }
  1167. })(),
  1168. /**
  1169. * 动态添加css样式
  1170. * @method cssRule
  1171. * @param { String } 节点名称
  1172. * @grammar UE.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
  1173. * @grammar UE.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
  1174. * @grammar UE.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
  1175. * @grammar UE.utils.cssRule('body',document) => 返回指定key的样式,并且指定是哪个document
  1176. * @grammar UE.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
  1177. */
  1178. cssRule:
  1179. browser.ie && browser.version != 11
  1180. ? function (key, style, doc) {
  1181. var indexList, index
  1182. if (
  1183. style === undefined ||
  1184. (style && style.nodeType && style.nodeType == 9)
  1185. ) {
  1186. //获取样式
  1187. doc =
  1188. style && style.nodeType && style.nodeType == 9
  1189. ? style
  1190. : doc || document
  1191. indexList = doc.indexList || (doc.indexList = {})
  1192. index = indexList[key]
  1193. if (index !== undefined) {
  1194. return doc.styleSheets[index].cssText
  1195. }
  1196. return undefined
  1197. }
  1198. doc = doc || document
  1199. indexList = doc.indexList || (doc.indexList = {})
  1200. index = indexList[key]
  1201. //清除样式
  1202. if (style === '') {
  1203. if (index !== undefined) {
  1204. doc.styleSheets[index].cssText = ''
  1205. delete indexList[key]
  1206. return true
  1207. }
  1208. return false
  1209. }
  1210. //添加样式
  1211. if (index !== undefined) {
  1212. sheetStyle = doc.styleSheets[index]
  1213. } else {
  1214. sheetStyle = doc.createStyleSheet(
  1215. '',
  1216. (index = doc.styleSheets.length)
  1217. )
  1218. indexList[key] = index
  1219. }
  1220. sheetStyle.cssText = style
  1221. }
  1222. : function (key, style, doc) {
  1223. var head, node
  1224. if (
  1225. style === undefined ||
  1226. (style && style.nodeType && style.nodeType == 9)
  1227. ) {
  1228. //获取样式
  1229. doc =
  1230. style && style.nodeType && style.nodeType == 9
  1231. ? style
  1232. : doc || document
  1233. node = doc.getElementById(key)
  1234. return node ? node.innerHTML : undefined
  1235. }
  1236. doc = doc || document
  1237. node = doc.getElementById(key)
  1238. //清除样式
  1239. if (style === '') {
  1240. if (node) {
  1241. node.parentNode.removeChild(node)
  1242. return true
  1243. }
  1244. return false
  1245. }
  1246. //添加样式
  1247. if (node) {
  1248. node.innerHTML = style
  1249. } else {
  1250. node = doc.createElement('style')
  1251. node.id = key
  1252. node.innerHTML = style
  1253. doc.getElementsByTagName('head')[0].appendChild(node)
  1254. }
  1255. },
  1256. sort: function (array, compareFn) {
  1257. compareFn =
  1258. compareFn ||
  1259. function (item1, item2) {
  1260. return item1.localeCompare(item2)
  1261. }
  1262. for (var i = 0, len = array.length; i < len; i++) {
  1263. for (var j = i, length = array.length; j < length; j++) {
  1264. if (compareFn(array[i], array[j]) > 0) {
  1265. var t = array[i]
  1266. array[i] = array[j]
  1267. array[j] = t
  1268. }
  1269. }
  1270. }
  1271. return array
  1272. },
  1273. serializeParam: function (json) {
  1274. var strArr = []
  1275. for (var i in json) {
  1276. //忽略默认的几个参数
  1277. if (i == 'method' || i == 'timeout' || i == 'async') continue
  1278. //传递过来的对象和函数不在提交之列
  1279. if (
  1280. !(
  1281. (typeof json[i]).toLowerCase() == 'function' ||
  1282. (typeof json[i]).toLowerCase() == 'object'
  1283. )
  1284. ) {
  1285. strArr.push(encodeURIComponent(i) + '=' + encodeURIComponent(json[i]))
  1286. } else if (utils.isArray(json[i])) {
  1287. //支持传数组内容
  1288. for (var j = 0; j < json[i].length; j++) {
  1289. strArr.push(
  1290. encodeURIComponent(i) + '[]=' + encodeURIComponent(json[i][j])
  1291. )
  1292. }
  1293. }
  1294. }
  1295. return strArr.join('&')
  1296. },
  1297. formatUrl: function (url) {
  1298. var u = url.replace(/&&/g, '&')
  1299. u = u.replace(/\?&/g, '?')
  1300. u = u.replace(/&$/g, '')
  1301. u = u.replace(/&#/g, '#')
  1302. u = u.replace(/&+/g, '&')
  1303. return u
  1304. },
  1305. isCrossDomainUrl: function (url) {
  1306. var a = document.createElement('a')
  1307. a.href = url
  1308. if (browser.ie) {
  1309. a.href = a.href
  1310. }
  1311. return !(
  1312. a.protocol == location.protocol &&
  1313. a.hostname == location.hostname &&
  1314. (a.port == location.port ||
  1315. (a.port == '80' && location.port == '') ||
  1316. (a.port == '' && location.port == '80'))
  1317. )
  1318. },
  1319. clearEmptyAttrs: function (obj) {
  1320. for (var p in obj) {
  1321. if (obj[p] === '') {
  1322. delete obj[p]
  1323. }
  1324. }
  1325. return obj
  1326. },
  1327. str2json: function (s) {
  1328. if (!utils.isString(s)) return null
  1329. if (window.JSON) {
  1330. return JSON.parse(s)
  1331. } else {
  1332. return new Function('return ' + utils.trim(s || ''))()
  1333. }
  1334. },
  1335. json2str: (function () {
  1336. if (window.JSON) {
  1337. return JSON.stringify
  1338. } else {
  1339. var escapeMap = {
  1340. '\b': '\\b',
  1341. '\t': '\\t',
  1342. '\n': '\\n',
  1343. '\f': '\\f',
  1344. '\r': '\\r',
  1345. '"': '\\"',
  1346. '\\': '\\\\',
  1347. }
  1348. function encodeString(source) {
  1349. if (/["\\\x00-\x1f]/.test(source)) {
  1350. source = source.replace(/["\\\x00-\x1f]/g, function (match) {
  1351. var c = escapeMap[match]
  1352. if (c) {
  1353. return c
  1354. }
  1355. c = match.charCodeAt()
  1356. return (
  1357. '\\u00' +
  1358. Math.floor(c / 16).toString(16) +
  1359. (c % 16).toString(16)
  1360. )
  1361. })
  1362. }
  1363. return '"' + source + '"'
  1364. }
  1365. function encodeArray(source) {
  1366. var result = ['['],
  1367. l = source.length,
  1368. preComma,
  1369. i,
  1370. item
  1371. for (i = 0; i < l; i++) {
  1372. item = source[i]
  1373. switch (typeof item) {
  1374. case 'undefined':
  1375. case 'function':
  1376. case 'unknown':
  1377. break
  1378. default:
  1379. if (preComma) {
  1380. result.push(',')
  1381. }
  1382. result.push(utils.json2str(item))
  1383. preComma = 1
  1384. }
  1385. }
  1386. result.push(']')
  1387. return result.join('')
  1388. }
  1389. function pad(source) {
  1390. return source < 10 ? '0' + source : source
  1391. }
  1392. function encodeDate(source) {
  1393. return (
  1394. '"' +
  1395. source.getFullYear() +
  1396. '-' +
  1397. pad(source.getMonth() + 1) +
  1398. '-' +
  1399. pad(source.getDate()) +
  1400. 'T' +
  1401. pad(source.getHours()) +
  1402. ':' +
  1403. pad(source.getMinutes()) +
  1404. ':' +
  1405. pad(source.getSeconds()) +
  1406. '"'
  1407. )
  1408. }
  1409. return function (value) {
  1410. switch (typeof value) {
  1411. case 'undefined':
  1412. return 'undefined'
  1413. case 'number':
  1414. return isFinite(value) ? String(value) : 'null'
  1415. case 'string':
  1416. return encodeString(value)
  1417. case 'boolean':
  1418. return String(value)
  1419. default:
  1420. if (value === null) {
  1421. return 'null'
  1422. } else if (utils.isArray(value)) {
  1423. return encodeArray(value)
  1424. } else if (utils.isDate(value)) {
  1425. return encodeDate(value)
  1426. } else {
  1427. var result = ['{'],
  1428. encode = utils.json2str,
  1429. preComma,
  1430. item
  1431. for (var key in value) {
  1432. if (Object.prototype.hasOwnProperty.call(value, key)) {
  1433. item = value[key]
  1434. switch (typeof item) {
  1435. case 'undefined':
  1436. case 'unknown':
  1437. case 'function':
  1438. break
  1439. default:
  1440. if (preComma) {
  1441. result.push(',')
  1442. }
  1443. preComma = 1
  1444. result.push(encode(key) + ':' + encode(item))
  1445. }
  1446. }
  1447. }
  1448. result.push('}')
  1449. return result.join('')
  1450. }
  1451. }
  1452. }
  1453. }
  1454. })(),
  1455. })
  1456. /**
  1457. * 判断给定的对象是否是字符串
  1458. * @method isString
  1459. * @param { * } object 需要判断的对象
  1460. * @return { Boolean } 给定的对象是否是字符串
  1461. */
  1462. /**
  1463. * 判断给定的对象是否是数组
  1464. * @method isArray
  1465. * @param { * } object 需要判断的对象
  1466. * @return { Boolean } 给定的对象是否是数组
  1467. */
  1468. /**
  1469. * 判断给定的对象是否是一个Function
  1470. * @method isFunction
  1471. * @param { * } object 需要判断的对象
  1472. * @return { Boolean } 给定的对象是否是Function
  1473. */
  1474. /**
  1475. * 判断给定的对象是否是Number
  1476. * @method isNumber
  1477. * @param { * } object 需要判断的对象
  1478. * @return { Boolean } 给定的对象是否是Number
  1479. */
  1480. /**
  1481. * 判断给定的对象是否是一个正则表达式
  1482. * @method isRegExp
  1483. * @param { * } object 需要判断的对象
  1484. * @return { Boolean } 给定的对象是否是正则表达式
  1485. */
  1486. /**
  1487. * 判断给定的对象是否是一个普通对象
  1488. * @method isObject
  1489. * @param { * } object 需要判断的对象
  1490. * @return { Boolean } 给定的对象是否是普通对象
  1491. */
  1492. utils.each(
  1493. ['String', 'Function', 'Array', 'Number', 'RegExp', 'Object', 'Date'],
  1494. function (v) {
  1495. UE.utils['is' + v] = function (obj) {
  1496. return Object.prototype.toString.apply(obj) == '[object ' + v + ']'
  1497. }
  1498. }
  1499. )
  1500. // core/EventBase.js
  1501. /**
  1502. * UE采用的事件基类
  1503. * @file
  1504. * @module UE
  1505. * @class EventBase
  1506. * @since 1.2.6.1
  1507. */
  1508. /**
  1509. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  1510. * @unfile
  1511. * @module UE
  1512. */
  1513. /**
  1514. * UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  1515. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  1516. * @unfile
  1517. * @module UE
  1518. * @class EventBase
  1519. */
  1520. /**
  1521. * 通过此构造器,子类可以继承EventBase获取事件监听的方法
  1522. * @constructor
  1523. * @example
  1524. * ```javascript
  1525. * UE.EventBase.call(editor);
  1526. * ```
  1527. */
  1528. var EventBase = (UE.EventBase = function () {})
  1529. EventBase.prototype = {
  1530. /**
  1531. * 注册事件监听器
  1532. * @method addListener
  1533. * @param { String } types 监听的事件名称,同时监听多个事件使用空格分隔
  1534. * @param { Function } fn 监听的事件被触发时,会执行该回调函数
  1535. * @waining 事件被触发时,监听的函数假如返回的值恒等于true,回调函数的队列中后面的函数将不执行
  1536. * @example
  1537. * ```javascript
  1538. * editor.addListener('selectionchange',function(){
  1539. * console.log("选区已经变化!");
  1540. * })
  1541. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  1542. * if(type == 'beforegetcontent'){
  1543. * //do something
  1544. * }else{
  1545. * //do something
  1546. * }
  1547. * console.log(this.getContent) // this是注册的事件的编辑器实例
  1548. * })
  1549. * ```
  1550. * @see UE.EventBase:fireEvent(String)
  1551. */
  1552. addListener: function (types, listener) {
  1553. types = utils.trim(types).split(/\s+/)
  1554. for (var i = 0, ti; (ti = types[i++]); ) {
  1555. getListener(this, ti, true).push(listener)
  1556. }
  1557. },
  1558. on: function (types, listener) {
  1559. return this.addListener(types, listener)
  1560. },
  1561. off: function (types, listener) {
  1562. return this.removeListener(types, listener)
  1563. },
  1564. trigger: function () {
  1565. return this.fireEvent.apply(this, arguments)
  1566. },
  1567. /**
  1568. * 移除事件监听器
  1569. * @method removeListener
  1570. * @param { String } types 移除的事件名称,同时移除多个事件使用空格分隔
  1571. * @param { Function } fn 移除监听事件的函数引用
  1572. * @example
  1573. * ```javascript
  1574. * //changeCallback为方法体
  1575. * editor.removeListener("selectionchange",changeCallback);
  1576. * ```
  1577. */
  1578. removeListener: function (types, listener) {
  1579. types = utils.trim(types).split(/\s+/)
  1580. for (var i = 0, ti; (ti = types[i++]); ) {
  1581. utils.removeItem(getListener(this, ti) || [], listener)
  1582. }
  1583. },
  1584. /**
  1585. * 触发事件
  1586. * @method fireEvent
  1587. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1588. * @remind 该方法会触发addListener
  1589. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1590. * @example
  1591. * ```javascript
  1592. * editor.fireEvent("selectionchange");
  1593. * ```
  1594. */
  1595. /**
  1596. * 触发事件
  1597. * @method fireEvent
  1598. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1599. * @param { *... } options 可选参数,可以传入一个或多个参数,会传给事件触发的回调函数
  1600. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1601. * @example
  1602. * ```javascript
  1603. *
  1604. * editor.addListener( "selectionchange", function ( type, arg1, arg2 ) {
  1605. *
  1606. * console.log( arg1 + " " + arg2 );
  1607. *
  1608. * } );
  1609. *
  1610. * //触发selectionchange事件, 会执行上面的事件监听器
  1611. * //output: Hello World
  1612. * editor.fireEvent("selectionchange", "Hello", "World");
  1613. * ```
  1614. */
  1615. fireEvent: function () {
  1616. var types = arguments[0]
  1617. types = utils.trim(types).split(' ')
  1618. for (var i = 0, ti; (ti = types[i++]); ) {
  1619. var listeners = getListener(this, ti),
  1620. r,
  1621. t,
  1622. k
  1623. if (listeners) {
  1624. k = listeners.length
  1625. while (k--) {
  1626. if (!listeners[k]) continue
  1627. t = listeners[k].apply(this, arguments)
  1628. if (t === true) {
  1629. return t
  1630. }
  1631. if (t !== undefined) {
  1632. r = t
  1633. }
  1634. }
  1635. }
  1636. if ((t = this['on' + ti.toLowerCase()])) {
  1637. r = t.apply(this, arguments)
  1638. }
  1639. }
  1640. return r
  1641. },
  1642. }
  1643. /**
  1644. * 获得对象所拥有监听类型的所有监听器
  1645. * @unfile
  1646. * @module UE
  1647. * @since 1.2.6.1
  1648. * @method getListener
  1649. * @public
  1650. * @param { Object } obj 查询监听器的对象
  1651. * @param { String } type 事件类型
  1652. * @param { Boolean } force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  1653. * @return { Array } 监听器数组
  1654. */
  1655. function getListener(obj, type, force) {
  1656. var allListeners
  1657. type = type.toLowerCase()
  1658. return (
  1659. (allListeners =
  1660. obj.__allListeners || (force && (obj.__allListeners = {}))) &&
  1661. (allListeners[type] || (force && (allListeners[type] = [])))
  1662. )
  1663. }
  1664. // core/dtd.js
  1665. ///import editor.js
  1666. ///import core/dom/dom.js
  1667. ///import core/utils.js
  1668. /**
  1669. * dtd html语义化的体现类
  1670. * @constructor
  1671. * @namespace dtd
  1672. */
  1673. var dtd = (dom.dtd = (function () {
  1674. function _(s) {
  1675. for (var k in s) {
  1676. s[k.toUpperCase()] = s[k]
  1677. }
  1678. return s
  1679. }
  1680. var X = utils.extend2
  1681. var A = _({ isindex: 1, fieldset: 1 }),
  1682. B = _({ input: 1, button: 1, select: 1, textarea: 1, label: 1 }),
  1683. C = X(_({ a: 1 }), B),
  1684. D = X({ iframe: 1 }, C),
  1685. E = _({
  1686. hr: 1,
  1687. ul: 1,
  1688. menu: 1,
  1689. div: 1,
  1690. blockquote: 1,
  1691. noscript: 1,
  1692. table: 1,
  1693. center: 1,
  1694. address: 1,
  1695. dir: 1,
  1696. pre: 1,
  1697. h5: 1,
  1698. dl: 1,
  1699. h4: 1,
  1700. noframes: 1,
  1701. h6: 1,
  1702. ol: 1,
  1703. h1: 1,
  1704. h3: 1,
  1705. h2: 1,
  1706. }),
  1707. F = _({ ins: 1, del: 1, script: 1, style: 1 }),
  1708. G = X(
  1709. _({
  1710. b: 1,
  1711. acronym: 1,
  1712. bdo: 1,
  1713. var: 1,
  1714. '#': 1,
  1715. abbr: 1,
  1716. code: 1,
  1717. br: 1,
  1718. i: 1,
  1719. cite: 1,
  1720. kbd: 1,
  1721. u: 1,
  1722. strike: 1,
  1723. s: 1,
  1724. tt: 1,
  1725. strong: 1,
  1726. q: 1,
  1727. samp: 1,
  1728. em: 1,
  1729. dfn: 1,
  1730. span: 1,
  1731. }),
  1732. F
  1733. ),
  1734. H = X(
  1735. _({
  1736. sub: 1,
  1737. img: 1,
  1738. embed: 1,
  1739. object: 1,
  1740. sup: 1,
  1741. basefont: 1,
  1742. map: 1,
  1743. applet: 1,
  1744. font: 1,
  1745. big: 1,
  1746. small: 1,
  1747. }),
  1748. G
  1749. ),
  1750. I = X(_({ p: 1 }), H),
  1751. J = X(_({ iframe: 1 }), H, B),
  1752. K = _({
  1753. img: 1,
  1754. embed: 1,
  1755. noscript: 1,
  1756. br: 1,
  1757. kbd: 1,
  1758. center: 1,
  1759. button: 1,
  1760. basefont: 1,
  1761. h5: 1,
  1762. h4: 1,
  1763. samp: 1,
  1764. h6: 1,
  1765. ol: 1,
  1766. h1: 1,
  1767. h3: 1,
  1768. h2: 1,
  1769. form: 1,
  1770. font: 1,
  1771. '#': 1,
  1772. select: 1,
  1773. menu: 1,
  1774. ins: 1,
  1775. abbr: 1,
  1776. label: 1,
  1777. code: 1,
  1778. table: 1,
  1779. script: 1,
  1780. cite: 1,
  1781. input: 1,
  1782. iframe: 1,
  1783. strong: 1,
  1784. textarea: 1,
  1785. noframes: 1,
  1786. big: 1,
  1787. small: 1,
  1788. span: 1,
  1789. hr: 1,
  1790. sub: 1,
  1791. bdo: 1,
  1792. var: 1,
  1793. div: 1,
  1794. object: 1,
  1795. sup: 1,
  1796. strike: 1,
  1797. dir: 1,
  1798. map: 1,
  1799. dl: 1,
  1800. applet: 1,
  1801. del: 1,
  1802. isindex: 1,
  1803. fieldset: 1,
  1804. ul: 1,
  1805. b: 1,
  1806. acronym: 1,
  1807. a: 1,
  1808. blockquote: 1,
  1809. i: 1,
  1810. u: 1,
  1811. s: 1,
  1812. tt: 1,
  1813. address: 1,
  1814. q: 1,
  1815. pre: 1,
  1816. p: 1,
  1817. em: 1,
  1818. dfn: 1,
  1819. }),
  1820. L = X(_({ a: 0 }), J), //a不能被切开,所以把他
  1821. M = _({ tr: 1 }),
  1822. N = _({ '#': 1 }),
  1823. O = X(_({ param: 1 }), K),
  1824. P = X(_({ form: 1 }), A, D, E, I),
  1825. Q = _({ li: 1, ol: 1, ul: 1 }),
  1826. R = _({ style: 1, script: 1 }),
  1827. S = _({ base: 1, link: 1, meta: 1, title: 1 }),
  1828. T = X(S, R),
  1829. U = _({ head: 1, body: 1 }),
  1830. V = _({ html: 1 })
  1831. var block = _({
  1832. address: 1,
  1833. blockquote: 1,
  1834. center: 1,
  1835. dir: 1,
  1836. div: 1,
  1837. dl: 1,
  1838. fieldset: 1,
  1839. form: 1,
  1840. h1: 1,
  1841. h2: 1,
  1842. h3: 1,
  1843. h4: 1,
  1844. h5: 1,
  1845. h6: 1,
  1846. hr: 1,
  1847. isindex: 1,
  1848. menu: 1,
  1849. noframes: 1,
  1850. ol: 1,
  1851. p: 1,
  1852. pre: 1,
  1853. table: 1,
  1854. ul: 1,
  1855. }),
  1856. empty = _({
  1857. area: 1,
  1858. base: 1,
  1859. basefont: 1,
  1860. br: 1,
  1861. col: 1,
  1862. command: 1,
  1863. dialog: 1,
  1864. embed: 1,
  1865. hr: 1,
  1866. img: 1,
  1867. input: 1,
  1868. isindex: 1,
  1869. keygen: 1,
  1870. link: 1,
  1871. meta: 1,
  1872. param: 1,
  1873. source: 1,
  1874. track: 1,
  1875. wbr: 1,
  1876. })
  1877. return _({
  1878. // $ 表示自定的属性
  1879. // body外的元素列表.
  1880. $nonBodyContent: X(V, U, S),
  1881. //块结构元素列表
  1882. $block: block,
  1883. //内联元素列表
  1884. $inline: L,
  1885. $inlineWithA: X(_({ a: 1 }), L),
  1886. $body: X(_({ script: 1, style: 1 }), block),
  1887. $cdata: _({ script: 1, style: 1 }),
  1888. //自闭和元素
  1889. $empty: empty,
  1890. //不是自闭合,但不能让range选中里边
  1891. $nonChild: _({ iframe: 1, textarea: 1 }),
  1892. //列表元素列表
  1893. $listItem: _({ dd: 1, dt: 1, li: 1 }),
  1894. //列表根元素列表
  1895. $list: _({ ul: 1, ol: 1, dl: 1 }),
  1896. //不能认为是空的元素
  1897. $isNotEmpty: _({
  1898. table: 1,
  1899. ul: 1,
  1900. ol: 1,
  1901. dl: 1,
  1902. iframe: 1,
  1903. area: 1,
  1904. base: 1,
  1905. col: 1,
  1906. hr: 1,
  1907. img: 1,
  1908. embed: 1,
  1909. input: 1,
  1910. link: 1,
  1911. meta: 1,
  1912. param: 1,
  1913. h1: 1,
  1914. h2: 1,
  1915. h3: 1,
  1916. h4: 1,
  1917. h5: 1,
  1918. h6: 1,
  1919. }),
  1920. //如果没有子节点就可以删除的元素列表,像span,a
  1921. $removeEmpty: _({
  1922. a: 1,
  1923. abbr: 1,
  1924. acronym: 1,
  1925. address: 1,
  1926. b: 1,
  1927. bdo: 1,
  1928. big: 1,
  1929. cite: 1,
  1930. code: 1,
  1931. del: 1,
  1932. dfn: 1,
  1933. em: 1,
  1934. font: 1,
  1935. i: 1,
  1936. ins: 1,
  1937. label: 1,
  1938. kbd: 1,
  1939. q: 1,
  1940. s: 1,
  1941. samp: 1,
  1942. small: 1,
  1943. span: 1,
  1944. strike: 1,
  1945. strong: 1,
  1946. sub: 1,
  1947. sup: 1,
  1948. tt: 1,
  1949. u: 1,
  1950. var: 1,
  1951. }),
  1952. $removeEmptyBlock: _({ p: 1, div: 1 }),
  1953. //在table元素里的元素列表
  1954. $tableContent: _({
  1955. caption: 1,
  1956. col: 1,
  1957. colgroup: 1,
  1958. tbody: 1,
  1959. td: 1,
  1960. tfoot: 1,
  1961. th: 1,
  1962. thead: 1,
  1963. tr: 1,
  1964. table: 1,
  1965. }),
  1966. //不转换的标签
  1967. $notTransContent: _({ pre: 1, script: 1, style: 1, textarea: 1 }),
  1968. html: U,
  1969. head: T,
  1970. style: N,
  1971. script: N,
  1972. body: P,
  1973. base: {},
  1974. link: {},
  1975. meta: {},
  1976. title: N,
  1977. col: {},
  1978. tr: _({ td: 1, th: 1 }),
  1979. img: {},
  1980. embed: {},
  1981. colgroup: _({ thead: 1, col: 1, tbody: 1, tr: 1, tfoot: 1 }),
  1982. noscript: P,
  1983. td: P,
  1984. br: {},
  1985. th: P,
  1986. center: P,
  1987. kbd: L,
  1988. button: X(I, E),
  1989. basefont: {},
  1990. h5: L,
  1991. h4: L,
  1992. samp: L,
  1993. h6: L,
  1994. ol: Q,
  1995. h1: L,
  1996. h3: L,
  1997. option: N,
  1998. h2: L,
  1999. form: X(A, D, E, I),
  2000. select: _({ optgroup: 1, option: 1 }),
  2001. font: L,
  2002. ins: L,
  2003. menu: Q,
  2004. abbr: L,
  2005. label: L,
  2006. table: _({
  2007. thead: 1,
  2008. col: 1,
  2009. tbody: 1,
  2010. tr: 1,
  2011. colgroup: 1,
  2012. caption: 1,
  2013. tfoot: 1,
  2014. }),
  2015. code: L,
  2016. tfoot: M,
  2017. cite: L,
  2018. li: P,
  2019. input: {},
  2020. iframe: P,
  2021. strong: L,
  2022. textarea: N,
  2023. noframes: P,
  2024. big: L,
  2025. small: L,
  2026. //trace:
  2027. span: _({
  2028. '#': 1,
  2029. br: 1,
  2030. b: 1,
  2031. strong: 1,
  2032. u: 1,
  2033. i: 1,
  2034. em: 1,
  2035. sub: 1,
  2036. sup: 1,
  2037. strike: 1,
  2038. span: 1,
  2039. }),
  2040. hr: L,
  2041. dt: L,
  2042. sub: L,
  2043. optgroup: _({ option: 1 }),
  2044. param: {},
  2045. bdo: L,
  2046. var: L,
  2047. div: P,
  2048. object: O,
  2049. sup: L,
  2050. dd: P,
  2051. strike: L,
  2052. area: {},
  2053. dir: Q,
  2054. map: X(_({ area: 1, form: 1, p: 1 }), A, F, E),
  2055. applet: O,
  2056. dl: _({ dt: 1, dd: 1 }),
  2057. del: L,
  2058. isindex: {},
  2059. fieldset: X(_({ legend: 1 }), K),
  2060. thead: M,
  2061. ul: Q,
  2062. acronym: L,
  2063. b: L,
  2064. a: X(_({ a: 1 }), J),
  2065. blockquote: X(_({ td: 1, tr: 1, tbody: 1, li: 1 }), P),
  2066. caption: L,
  2067. i: L,
  2068. u: L,
  2069. tbody: M,
  2070. s: L,
  2071. address: X(D, I),
  2072. tt: L,
  2073. legend: L,
  2074. q: L,
  2075. pre: X(G, C),
  2076. p: X(_({ a: 1 }), L),
  2077. em: L,
  2078. dfn: L,
  2079. })
  2080. })())
  2081. // core/domUtils.js
  2082. /**
  2083. * Dom操作工具包
  2084. * @file
  2085. * @module UE.dom.domUtils
  2086. * @since 1.2.6.1
  2087. */
  2088. /**
  2089. * Dom操作工具包
  2090. * @unfile
  2091. * @module UE.dom.domUtils
  2092. */
  2093. function getDomNode(node, start, ltr, startFromChild, fn, guard) {
  2094. var tmpNode = startFromChild && node[start],
  2095. parent
  2096. !tmpNode && (tmpNode = node[ltr])
  2097. while (!tmpNode && (parent = (parent || node).parentNode)) {
  2098. if (parent.tagName == 'BODY' || (guard && !guard(parent))) {
  2099. return null
  2100. }
  2101. tmpNode = parent[ltr]
  2102. }
  2103. if (tmpNode && fn && !fn(tmpNode)) {
  2104. return getDomNode(tmpNode, start, ltr, false, fn)
  2105. }
  2106. return tmpNode
  2107. }
  2108. var attrFix =
  2109. ie && browser.version < 9
  2110. ? {
  2111. tabindex: 'tabIndex',
  2112. readonly: 'readOnly',
  2113. for: 'htmlFor',
  2114. class: 'className',
  2115. maxlength: 'maxLength',
  2116. cellspacing: 'cellSpacing',
  2117. cellpadding: 'cellPadding',
  2118. rowspan: 'rowSpan',
  2119. colspan: 'colSpan',
  2120. usemap: 'useMap',
  2121. frameborder: 'frameBorder',
  2122. }
  2123. : {
  2124. tabindex: 'tabIndex',
  2125. readonly: 'readOnly',
  2126. },
  2127. styleBlock = utils.listToMap([
  2128. '-webkit-box',
  2129. '-moz-box',
  2130. 'block',
  2131. 'list-item',
  2132. 'table',
  2133. 'table-row-group',
  2134. 'table-header-group',
  2135. 'table-footer-group',
  2136. 'table-row',
  2137. 'table-column-group',
  2138. 'table-column',
  2139. 'table-cell',
  2140. 'table-caption',
  2141. ])
  2142. var domUtils = (dom.domUtils = {
  2143. //节点常量
  2144. NODE_ELEMENT: 1,
  2145. NODE_DOCUMENT: 9,
  2146. NODE_TEXT: 3,
  2147. NODE_COMMENT: 8,
  2148. NODE_DOCUMENT_FRAGMENT: 11,
  2149. //位置关系
  2150. POSITION_IDENTICAL: 0,
  2151. POSITION_DISCONNECTED: 1,
  2152. POSITION_FOLLOWING: 2,
  2153. POSITION_PRECEDING: 4,
  2154. POSITION_IS_CONTAINED: 8,
  2155. POSITION_CONTAINS: 16,
  2156. //ie6使用其他的会有一段空白出现
  2157. fillChar: ie && browser.version == '6' ? '\ufeff' : '\u200B',
  2158. //-------------------------Node部分--------------------------------
  2159. keys: {
  2160. /*Backspace*/ 8: 1,
  2161. /*Delete*/ 46: 1,
  2162. /*Shift*/ 16: 1,
  2163. /*Ctrl*/ 17: 1,
  2164. /*Alt*/ 18: 1,
  2165. 37: 1,
  2166. 38: 1,
  2167. 39: 1,
  2168. 40: 1,
  2169. 13: 1 /*enter*/,
  2170. },
  2171. /**
  2172. * 获取节点A相对于节点B的位置关系
  2173. * @method getPosition
  2174. * @param { Node } nodeA 需要查询位置关系的节点A
  2175. * @param { Node } nodeB 需要查询位置关系的节点B
  2176. * @return { Number } 节点A与节点B的关系
  2177. * @example
  2178. * ```javascript
  2179. * //output: 20
  2180. * var position = UE.dom.domUtils.getPosition( document.documentElement, document.body );
  2181. *
  2182. * switch ( position ) {
  2183. *
  2184. * //0
  2185. * case UE.dom.domUtils.POSITION_IDENTICAL:
  2186. * console.log('元素相同');
  2187. * break;
  2188. * //1
  2189. * case UE.dom.domUtils.POSITION_DISCONNECTED:
  2190. * console.log('两个节点在不同的文档中');
  2191. * break;
  2192. * //2
  2193. * case UE.dom.domUtils.POSITION_FOLLOWING:
  2194. * console.log('节点A在节点B之后');
  2195. * break;
  2196. * //4
  2197. * case UE.dom.domUtils.POSITION_PRECEDING;
  2198. * console.log('节点A在节点B之前');
  2199. * break;
  2200. * //8
  2201. * case UE.dom.domUtils.POSITION_IS_CONTAINED:
  2202. * console.log('节点A被节点B包含');
  2203. * break;
  2204. * case 10:
  2205. * console.log('节点A被节点B包含且节点A在节点B之后');
  2206. * break;
  2207. * //16
  2208. * case UE.dom.domUtils.POSITION_CONTAINS:
  2209. * console.log('节点A包含节点B');
  2210. * break;
  2211. * case 20:
  2212. * console.log('节点A包含节点B且节点A在节点B之前');
  2213. * break;
  2214. *
  2215. * }
  2216. * ```
  2217. */
  2218. getPosition: function (nodeA, nodeB) {
  2219. // 如果两个节点是同一个节点
  2220. if (nodeA === nodeB) {
  2221. // domUtils.POSITION_IDENTICAL
  2222. return 0
  2223. }
  2224. var node,
  2225. parentsA = [nodeA],
  2226. parentsB = [nodeB]
  2227. node = nodeA
  2228. while ((node = node.parentNode)) {
  2229. // 如果nodeB是nodeA的祖先节点
  2230. if (node === nodeB) {
  2231. // domUtils.POSITION_IS_CONTAINED + domUtils.POSITION_FOLLOWING
  2232. return 10
  2233. }
  2234. parentsA.push(node)
  2235. }
  2236. node = nodeB
  2237. while ((node = node.parentNode)) {
  2238. // 如果nodeA是nodeB的祖先节点
  2239. if (node === nodeA) {
  2240. // domUtils.POSITION_CONTAINS + domUtils.POSITION_PRECEDING
  2241. return 20
  2242. }
  2243. parentsB.push(node)
  2244. }
  2245. parentsA.reverse()
  2246. parentsB.reverse()
  2247. if (parentsA[0] !== parentsB[0]) {
  2248. // domUtils.POSITION_DISCONNECTED
  2249. return 1
  2250. }
  2251. var i = -1
  2252. while ((i++, parentsA[i] === parentsB[i])) {}
  2253. nodeA = parentsA[i]
  2254. nodeB = parentsB[i]
  2255. while ((nodeA = nodeA.nextSibling)) {
  2256. if (nodeA === nodeB) {
  2257. // domUtils.POSITION_PRECEDING
  2258. return 4
  2259. }
  2260. }
  2261. // domUtils.POSITION_FOLLOWING
  2262. return 2
  2263. },
  2264. /**
  2265. * 检测节点node在父节点中的索引位置
  2266. * @method getNodeIndex
  2267. * @param { Node } node 需要检测的节点对象
  2268. * @return { Number } 该节点在父节点中的位置
  2269. * @see UE.dom.domUtils.getNodeIndex(Node,Boolean)
  2270. */
  2271. /**
  2272. * 检测节点node在父节点中的索引位置, 根据给定的mergeTextNode参数决定是否要合并多个连续的文本节点为一个节点
  2273. * @method getNodeIndex
  2274. * @param { Node } node 需要检测的节点对象
  2275. * @param { Boolean } mergeTextNode 是否合并多个连续的文本节点为一个节点
  2276. * @return { Number } 该节点在父节点中的位置
  2277. * @example
  2278. * ```javascript
  2279. *
  2280. * var node = document.createElement("div");
  2281. *
  2282. * node.appendChild( document.createTextNode( "hello" ) );
  2283. * node.appendChild( document.createTextNode( "world" ) );
  2284. * node.appendChild( node = document.createElement( "div" ) );
  2285. *
  2286. * //output: 2
  2287. * console.log( UE.dom.domUtils.getNodeIndex( node ) );
  2288. *
  2289. * //output: 1
  2290. * console.log( UE.dom.domUtils.getNodeIndex( node, true ) );
  2291. *
  2292. * ```
  2293. */
  2294. getNodeIndex: function (node, ignoreTextNode) {
  2295. var preNode = node,
  2296. i = 0
  2297. while ((preNode = preNode.previousSibling)) {
  2298. if (ignoreTextNode && preNode.nodeType == 3) {
  2299. if (preNode.nodeType != preNode.nextSibling.nodeType) {
  2300. i++
  2301. }
  2302. continue
  2303. }
  2304. i++
  2305. }
  2306. return i
  2307. },
  2308. /**
  2309. * 检测节点node是否在给定的document对象上
  2310. * @method inDoc
  2311. * @param { Node } node 需要检测的节点对象
  2312. * @param { DomDocument } doc 需要检测的document对象
  2313. * @return { Boolean } 该节点node是否在给定的document的dom树上
  2314. * @example
  2315. * ```javascript
  2316. *
  2317. * var node = document.createElement("div");
  2318. *
  2319. * //output: false
  2320. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2321. *
  2322. * document.body.appendChild( node );
  2323. *
  2324. * //output: true
  2325. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2326. *
  2327. * ```
  2328. */
  2329. inDoc: function (node, doc) {
  2330. return domUtils.getPosition(node, doc) == 10
  2331. },
  2332. /**
  2333. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2334. * 查找的起点是给定node节点的父节点。
  2335. * @method findParent
  2336. * @param { Node } node 需要查找的节点
  2337. * @param { Function } filterFn 自定义的过滤方法。
  2338. * @warning 查找的终点是到body节点为止
  2339. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2340. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2341. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2342. * @example
  2343. * ```javascript
  2344. * var filterNode = UE.dom.domUtils.findParent( document.body.firstChild, function ( node ) {
  2345. *
  2346. * //由于查找的终点是body节点, 所以永远也不会匹配当前过滤器的条件, 即这里永远会返回false
  2347. * return node.tagName === "HTML";
  2348. *
  2349. * } );
  2350. *
  2351. * //output: true
  2352. * console.log( filterNode === null );
  2353. * ```
  2354. */
  2355. /**
  2356. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2357. * 如果includeSelf的值为true,则查找的起点是给定的节点node, 否则, 起点是node的父节点
  2358. * @method findParent
  2359. * @param { Node } node 需要查找的节点
  2360. * @param { Function } filterFn 自定义的过滤方法。
  2361. * @param { Boolean } includeSelf 查找过程是否包含自身
  2362. * @warning 查找的终点是到body节点为止
  2363. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2364. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2365. * @remind 如果includeSelf为true, 则过滤器第一次执行时的参数会是节点本身。
  2366. * 反之, 过滤器第一次执行时的参数将是该节点的父节点。
  2367. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2368. * @example
  2369. * ```html
  2370. * <body>
  2371. *
  2372. * <div id="test">
  2373. * </div>
  2374. *
  2375. * <script type="text/javascript">
  2376. *
  2377. * //output: DIV, BODY
  2378. * var filterNode = UE.dom.domUtils.findParent( document.getElementById( "test" ), function ( node ) {
  2379. *
  2380. * console.log( node.tagName );
  2381. * return false;
  2382. *
  2383. * }, true );
  2384. *
  2385. * </script>
  2386. * </body>
  2387. * ```
  2388. */
  2389. findParent: function (node, filterFn, includeSelf) {
  2390. if (node && !domUtils.isBody(node)) {
  2391. node = includeSelf ? node : node.parentNode
  2392. while (node) {
  2393. if (!filterFn || filterFn(node) || domUtils.isBody(node)) {
  2394. return filterFn && !filterFn(node) && domUtils.isBody(node)
  2395. ? null
  2396. : node
  2397. }
  2398. node = node.parentNode
  2399. }
  2400. }
  2401. return null
  2402. },
  2403. /**
  2404. * 查找node的节点名为tagName的第一个祖先节点, 查找的起点是node节点的父节点。
  2405. * @method findParentByTagName
  2406. * @param { Node } node 需要查找的节点对象
  2407. * @param { Array } tagNames 需要查找的父节点的名称数组
  2408. * @warning 查找的终点是到body节点为止
  2409. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2410. * @example
  2411. * ```javascript
  2412. * var node = UE.dom.domUtils.findParentByTagName( document.getElementsByTagName("div")[0], [ "BODY" ] );
  2413. * //output: BODY
  2414. * console.log( node.tagName );
  2415. * ```
  2416. */
  2417. /**
  2418. * 查找node的节点名为tagName的祖先节点, 如果includeSelf的值为true,则查找的起点是给定的节点node,
  2419. * 否则, 起点是node的父节点。
  2420. * @method findParentByTagName
  2421. * @param { Node } node 需要查找的节点对象
  2422. * @param { Array } tagNames 需要查找的父节点的名称数组
  2423. * @param { Boolean } includeSelf 查找过程是否包含node节点自身
  2424. * @warning 查找的终点是到body节点为止
  2425. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2426. * @example
  2427. * ```javascript
  2428. * var queryTarget = document.getElementsByTagName("div")[0];
  2429. * var node = UE.dom.domUtils.findParentByTagName( queryTarget, [ "DIV" ], true );
  2430. * //output: true
  2431. * console.log( queryTarget === node );
  2432. * ```
  2433. */
  2434. findParentByTagName: function (node, tagNames, includeSelf, excludeFn) {
  2435. tagNames = utils.listToMap(
  2436. utils.isArray(tagNames) ? tagNames : [tagNames]
  2437. )
  2438. return domUtils.findParent(
  2439. node,
  2440. function (node) {
  2441. return tagNames[node.tagName] && !(excludeFn && excludeFn(node))
  2442. },
  2443. includeSelf
  2444. )
  2445. },
  2446. /**
  2447. * 查找节点node的祖先节点集合, 查找的起点是给定节点的父节点,结果集中不包含给定的节点。
  2448. * @method findParents
  2449. * @param { Node } node 需要查找的节点对象
  2450. * @return { Array } 给定节点的祖先节点数组
  2451. * @grammar UE.dom.domUtils.findParents(node) => Array //返回一个祖先节点数组集合,不包含自身
  2452. * @grammar UE.dom.domUtils.findParents(node,includeSelf) => Array //返回一个祖先节点数组集合,includeSelf指定是否包含自身
  2453. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn) => Array //返回一个祖先节点数组集合,filterFn指定过滤条件,返回true的node将被选取
  2454. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn,closerFirst) => Array //返回一个祖先节点数组集合,closerFirst为true的话,node的直接父亲节点是数组的第0个
  2455. */
  2456. /**
  2457. * 查找节点node的祖先节点集合, 如果includeSelf的值为true,
  2458. * 则返回的结果集中允许出现当前给定的节点, 否则, 该节点不会出现在其结果集中。
  2459. * @method findParents
  2460. * @param { Node } node 需要查找的节点对象
  2461. * @param { Boolean } includeSelf 查找的结果中是否允许包含当前查找的节点对象
  2462. * @return { Array } 给定节点的祖先节点数组
  2463. */
  2464. findParents: function (node, includeSelf, filterFn, closerFirst) {
  2465. var parents =
  2466. includeSelf && ((filterFn && filterFn(node)) || !filterFn) ? [node] : []
  2467. while ((node = domUtils.findParent(node, filterFn))) {
  2468. parents.push(node)
  2469. }
  2470. return closerFirst ? parents : parents.reverse()
  2471. },
  2472. /**
  2473. * 在节点node后面插入新节点newNode
  2474. * @method insertAfter
  2475. * @param { Node } node 目标节点
  2476. * @param { Node } newNode 新插入的节点, 该节点将置于目标节点之后
  2477. * @return { Node } 新插入的节点
  2478. */
  2479. insertAfter: function (node, newNode) {
  2480. return node.nextSibling
  2481. ? node.parentNode.insertBefore(newNode, node.nextSibling)
  2482. : node.parentNode.appendChild(newNode)
  2483. },
  2484. /**
  2485. * 删除节点node及其下属的所有节点
  2486. * @method remove
  2487. * @param { Node } node 需要删除的节点对象
  2488. * @return { Node } 返回刚删除的节点对象
  2489. * @example
  2490. * ```html
  2491. * <div id="test">
  2492. * <div id="child">你好</div>
  2493. * </div>
  2494. * <script>
  2495. * UE.dom.domUtils.remove( document.body, false );
  2496. * //output: false
  2497. * console.log( document.getElementById( "child" ) !== null );
  2498. * </script>
  2499. * ```
  2500. */
  2501. /**
  2502. * 删除节点node,并根据keepChildren的值决定是否保留子节点
  2503. * @method remove
  2504. * @param { Node } node 需要删除的节点对象
  2505. * @param { Boolean } keepChildren 是否需要保留子节点
  2506. * @return { Node } 返回刚删除的节点对象
  2507. * @example
  2508. * ```html
  2509. * <div id="test">
  2510. * <div id="child">你好</div>
  2511. * </div>
  2512. * <script>
  2513. * UE.dom.domUtils.remove( document.body, true );
  2514. * //output: true
  2515. * console.log( document.getElementById( "child" ) !== null );
  2516. * </script>
  2517. * ```
  2518. */
  2519. remove: function (node, keepChildren) {
  2520. var parent = node.parentNode,
  2521. child
  2522. if (parent) {
  2523. if (keepChildren && node.hasChildNodes()) {
  2524. while ((child = node.firstChild)) {
  2525. parent.insertBefore(child, node)
  2526. }
  2527. }
  2528. parent.removeChild(node)
  2529. }
  2530. return node
  2531. },
  2532. /**
  2533. * 取得node节点的下一个兄弟节点, 如果该节点其后没有兄弟节点, 则递归查找其父节点之后的第一个兄弟节点,
  2534. * 直到找到满足条件的节点或者递归到BODY节点之后才会结束。
  2535. * @method getNextDomNode
  2536. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2537. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2538. * @example
  2539. * ```html
  2540. * <body>
  2541. * <div id="test">
  2542. * <span></span>
  2543. * </div>
  2544. * <i>xxx</i>
  2545. * </body>
  2546. * <script>
  2547. *
  2548. * //output: i节点
  2549. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2550. *
  2551. * </script>
  2552. * ```
  2553. * @example
  2554. * ```html
  2555. * <body>
  2556. * <div>
  2557. * <span></span>
  2558. * <i id="test">xxx</i>
  2559. * </div>
  2560. * <b>xxx</b>
  2561. * </body>
  2562. * <script>
  2563. *
  2564. * //由于id为test的i节点之后没有兄弟节点, 则查找其父节点(div)后面的兄弟节点
  2565. * //output: b节点
  2566. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2567. *
  2568. * </script>
  2569. * ```
  2570. */
  2571. /**
  2572. * 取得node节点的下一个兄弟节点, 如果startFromChild的值为ture,则先获取其子节点,
  2573. * 如果有子节点则直接返回第一个子节点;如果没有子节点或者startFromChild的值为false,
  2574. * 则执行<a href="#UE.dom.domUtils.getNextDomNode(Node)">getNextDomNode(Node node)</a>的查找过程。
  2575. * @method getNextDomNode
  2576. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2577. * @param { Boolean } startFromChild 查找过程是否从其子节点开始
  2578. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2579. * @see UE.dom.domUtils.getNextDomNode(Node)
  2580. */
  2581. getNextDomNode: function (node, startFromChild, filterFn, guard) {
  2582. return getDomNode(
  2583. node,
  2584. 'firstChild',
  2585. 'nextSibling',
  2586. startFromChild,
  2587. filterFn,
  2588. guard
  2589. )
  2590. },
  2591. getPreDomNode: function (node, startFromChild, filterFn, guard) {
  2592. return getDomNode(
  2593. node,
  2594. 'lastChild',
  2595. 'previousSibling',
  2596. startFromChild,
  2597. filterFn,
  2598. guard
  2599. )
  2600. },
  2601. /**
  2602. * 检测节点node是否属是UEditor定义的bookmark节点
  2603. * @method isBookmarkNode
  2604. * @private
  2605. * @param { Node } node 需要检测的节点对象
  2606. * @return { Boolean } 是否是bookmark节点
  2607. * @example
  2608. * ```html
  2609. * <span id="_baidu_bookmark_1"></span>
  2610. * <script>
  2611. * var bookmarkNode = document.getElementById("_baidu_bookmark_1");
  2612. * //output: true
  2613. * console.log( UE.dom.domUtils.isBookmarkNode( bookmarkNode ) );
  2614. * </script>
  2615. * ```
  2616. */
  2617. isBookmarkNode: function (node) {
  2618. return node.nodeType == 1 && node.id && /^_baidu_bookmark_/i.test(node.id)
  2619. },
  2620. /**
  2621. * 获取节点node所属的window对象
  2622. * @method getWindow
  2623. * @param { Node } node 节点对象
  2624. * @return { Window } 当前节点所属的window对象
  2625. * @example
  2626. * ```javascript
  2627. * //output: true
  2628. * console.log( UE.dom.domUtils.getWindow( document.body ) === window );
  2629. * ```
  2630. */
  2631. getWindow: function (node) {
  2632. var doc = node.ownerDocument || node
  2633. return doc.defaultView || doc.parentWindow
  2634. },
  2635. /**
  2636. * 获取离nodeA与nodeB最近的公共的祖先节点
  2637. * @method getCommonAncestor
  2638. * @param { Node } nodeA 第一个节点
  2639. * @param { Node } nodeB 第二个节点
  2640. * @remind 如果给定的两个节点是同一个节点, 将直接返回该节点。
  2641. * @return { Node | NULL } 如果未找到公共节点, 返回NULL, 否则返回最近的公共祖先节点。
  2642. * @example
  2643. * ```javascript
  2644. * var commonAncestor = UE.dom.domUtils.getCommonAncestor( document.body, document.body.firstChild );
  2645. * //output: true
  2646. * console.log( commonAncestor.tagName.toLowerCase() === 'body' );
  2647. * ```
  2648. */
  2649. getCommonAncestor: function (nodeA, nodeB) {
  2650. if (nodeA === nodeB) return nodeA
  2651. var parentsA = [nodeA],
  2652. parentsB = [nodeB],
  2653. parent = nodeA,
  2654. i = -1
  2655. while ((parent = parent.parentNode)) {
  2656. if (parent === nodeB) {
  2657. return parent
  2658. }
  2659. parentsA.push(parent)
  2660. }
  2661. parent = nodeB
  2662. while ((parent = parent.parentNode)) {
  2663. if (parent === nodeA) return parent
  2664. parentsB.push(parent)
  2665. }
  2666. parentsA.reverse()
  2667. parentsB.reverse()
  2668. while ((i++, parentsA[i] === parentsB[i])) {}
  2669. return i == 0 ? null : parentsA[i - 1]
  2670. },
  2671. /**
  2672. * 清除node节点左右连续为空的兄弟inline节点
  2673. * @method clearEmptySibling
  2674. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2675. * 则这些兄弟节点将被删除
  2676. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext) //ignoreNext指定是否忽略右边空节点
  2677. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext,ignorePre) //ignorePre指定是否忽略左边空节点
  2678. * @example
  2679. * ```html
  2680. * <body>
  2681. * <div></div>
  2682. * <span id="test"></span>
  2683. * <i></i>
  2684. * <b></b>
  2685. * <em>xxx</em>
  2686. * <span></span>
  2687. * </body>
  2688. * <script>
  2689. *
  2690. * UE.dom.domUtils.clearEmptySibling( document.getElementById( "test" ) );
  2691. *
  2692. * //output: <div></div><span id="test"></span><em>xxx</em><span></span>
  2693. * console.log( document.body.innerHTML );
  2694. *
  2695. * </script>
  2696. * ```
  2697. */
  2698. /**
  2699. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2700. * 则忽略对右边兄弟节点的操作。
  2701. * @method clearEmptySibling
  2702. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2703. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2704. * 则这些兄弟节点将被删除
  2705. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2706. */
  2707. /**
  2708. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2709. * 则忽略对右边兄弟节点的操作, 如果ignorePre的值为true,则忽略对左边兄弟节点的操作。
  2710. * @method clearEmptySibling
  2711. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2712. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2713. * @param { Boolean } ignorePre 是否忽略忽略对左边的兄弟节点的操作
  2714. * 则这些兄弟节点将被删除
  2715. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2716. */
  2717. clearEmptySibling: function (node, ignoreNext, ignorePre) {
  2718. function clear(next, dir) {
  2719. var tmpNode
  2720. while (
  2721. next &&
  2722. !domUtils.isBookmarkNode(next) &&
  2723. (domUtils.isEmptyInlineElement(next) ||
  2724. //这里不能把空格算进来会吧空格干掉,出现文字间的空格丢掉了
  2725. !new RegExp('[^\t\n\r' + domUtils.fillChar + ']').test(
  2726. next.nodeValue
  2727. ))
  2728. ) {
  2729. tmpNode = next[dir]
  2730. domUtils.remove(next)
  2731. next = tmpNode
  2732. }
  2733. }
  2734. !ignoreNext && clear(node.nextSibling, 'nextSibling')
  2735. !ignorePre && clear(node.previousSibling, 'previousSibling')
  2736. },
  2737. /**
  2738. * 将一个文本节点textNode拆分成两个文本节点,offset指定拆分位置
  2739. * @method split
  2740. * @param { Node } textNode 需要拆分的文本节点对象
  2741. * @param { int } offset 需要拆分的位置, 位置计算从0开始
  2742. * @return { Node } 拆分后形成的新节点
  2743. * @example
  2744. * ```html
  2745. * <div id="test">abcdef</div>
  2746. * <script>
  2747. * var newNode = UE.dom.domUtils.split( document.getElementById( "test" ).firstChild, 3 );
  2748. * //output: def
  2749. * console.log( newNode.nodeValue );
  2750. * </script>
  2751. * ```
  2752. */
  2753. split: function (node, offset) {
  2754. var doc = node.ownerDocument
  2755. if (browser.ie && offset == node.nodeValue.length) {
  2756. var next = doc.createTextNode('')
  2757. return domUtils.insertAfter(node, next)
  2758. }
  2759. var retval = node.splitText(offset)
  2760. //ie8下splitText不会跟新childNodes,我们手动触发他的更新
  2761. if (browser.ie8) {
  2762. var tmpNode = doc.createTextNode('')
  2763. domUtils.insertAfter(retval, tmpNode)
  2764. domUtils.remove(tmpNode)
  2765. }
  2766. return retval
  2767. },
  2768. /**
  2769. * 检测文本节点textNode是否为空节点(包括空格、换行、占位符等字符)
  2770. * @method isWhitespace
  2771. * @param { Node } node 需要检测的节点对象
  2772. * @return { Boolean } 检测的节点是否为空
  2773. * @example
  2774. * ```html
  2775. * <div id="test">
  2776. *
  2777. * </div>
  2778. * <script>
  2779. * //output: true
  2780. * console.log( UE.dom.domUtils.isWhitespace( document.getElementById("test").firstChild ) );
  2781. * </script>
  2782. * ```
  2783. */
  2784. isWhitespace: function (node) {
  2785. return !new RegExp('[^ \t\n\r' + domUtils.fillChar + ']').test(
  2786. node.nodeValue
  2787. )
  2788. },
  2789. /**
  2790. * 获取元素element相对于viewport的位置坐标
  2791. * @method getXY
  2792. * @param { Node } element 需要计算位置的节点对象
  2793. * @return { Object } 返回形如{x:left,y:top}的一个key-value映射对象, 其中键x代表水平偏移距离,
  2794. * y代表垂直偏移距离。
  2795. *
  2796. * @example
  2797. * ```javascript
  2798. * var location = UE.dom.domUtils.getXY( document.getElementById("test") );
  2799. * //output: test的坐标为: 12, 24
  2800. * console.log( 'test的坐标为: ', location.x, ',', location.y );
  2801. * ```
  2802. */
  2803. getXY: function (element) {
  2804. var x = 0,
  2805. y = 0
  2806. while (element.offsetParent) {
  2807. y += element.offsetTop
  2808. x += element.offsetLeft
  2809. element = element.offsetParent
  2810. }
  2811. return { x: x, y: y }
  2812. },
  2813. /**
  2814. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2815. * @method on
  2816. * @param { Node } element 需要绑定事件的节点对象
  2817. * @param { String } type 绑定的事件类型
  2818. * @param { Function } handler 事件处理器
  2819. * @example
  2820. * ```javascript
  2821. * UE.dom.domUtils.on(document.body,"click",function(e){
  2822. * //e为事件对象,this为被点击元素对戏那个
  2823. * });
  2824. * ```
  2825. */
  2826. /**
  2827. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2828. * @method on
  2829. * @param { Node } element 需要绑定事件的节点对象
  2830. * @param { Array } type 绑定的事件类型数组
  2831. * @param { Function } handler 事件处理器
  2832. * @example
  2833. * ```javascript
  2834. * UE.dom.domUtils.on(document.body,["click","mousedown"],function(evt){
  2835. * //evt为事件对象,this为被点击元素对象
  2836. * });
  2837. * ```
  2838. */
  2839. on: function (element, type, handler) {
  2840. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2841. k = types.length
  2842. if (k)
  2843. while (k--) {
  2844. type = types[k]
  2845. if (element.addEventListener) {
  2846. element.addEventListener(type, handler, false)
  2847. } else {
  2848. if (!handler._d) {
  2849. handler._d = {
  2850. els: [],
  2851. }
  2852. }
  2853. var key = type + handler.toString(),
  2854. index = utils.indexOf(handler._d.els, element)
  2855. if (!handler._d[key] || index == -1) {
  2856. if (index == -1) {
  2857. handler._d.els.push(element)
  2858. }
  2859. if (!handler._d[key]) {
  2860. handler._d[key] = function (evt) {
  2861. return handler.call(evt.srcElement, evt || window.event)
  2862. }
  2863. }
  2864. element.attachEvent('on' + type, handler._d[key])
  2865. }
  2866. }
  2867. }
  2868. element = null
  2869. },
  2870. /**
  2871. * 解除DOM事件绑定
  2872. * @method un
  2873. * @param { Node } element 需要解除事件绑定的节点对象
  2874. * @param { String } type 需要接触绑定的事件类型
  2875. * @param { Function } handler 对应的事件处理器
  2876. * @example
  2877. * ```javascript
  2878. * UE.dom.domUtils.un(document.body,"click",function(evt){
  2879. * //evt为事件对象,this为被点击元素对象
  2880. * });
  2881. * ```
  2882. */
  2883. /**
  2884. * 解除DOM事件绑定
  2885. * @method un
  2886. * @param { Node } element 需要解除事件绑定的节点对象
  2887. * @param { Array } type 需要接触绑定的事件类型数组
  2888. * @param { Function } handler 对应的事件处理器
  2889. * @example
  2890. * ```javascript
  2891. * UE.dom.domUtils.un(document.body, ["click","mousedown"],function(evt){
  2892. * //evt为事件对象,this为被点击元素对象
  2893. * });
  2894. * ```
  2895. */
  2896. un: function (element, type, handler) {
  2897. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2898. k = types.length
  2899. if (k)
  2900. while (k--) {
  2901. type = types[k]
  2902. if (element.removeEventListener) {
  2903. element.removeEventListener(type, handler, false)
  2904. } else {
  2905. var key = type + handler.toString()
  2906. try {
  2907. element.detachEvent(
  2908. 'on' + type,
  2909. handler._d ? handler._d[key] : handler
  2910. )
  2911. } catch (e) {}
  2912. if (handler._d && handler._d[key]) {
  2913. var index = utils.indexOf(handler._d.els, element)
  2914. if (index != -1) {
  2915. handler._d.els.splice(index, 1)
  2916. }
  2917. handler._d.els.length == 0 && delete handler._d[key]
  2918. }
  2919. }
  2920. }
  2921. },
  2922. /**
  2923. * 比较节点nodeA与节点nodeB是否具有相同的标签名、属性名以及属性值
  2924. * @method isSameElement
  2925. * @param { Node } nodeA 需要比较的节点
  2926. * @param { Node } nodeB 需要比较的节点
  2927. * @return { Boolean } 两个节点是否具有相同的标签名、属性名以及属性值
  2928. * @example
  2929. * ```html
  2930. * <span style="font-size:12px">ssss</span>
  2931. * <span style="font-size:12px">bbbbb</span>
  2932. * <span style="font-size:13px">ssss</span>
  2933. * <span style="font-size:14px">bbbbb</span>
  2934. *
  2935. * <script>
  2936. *
  2937. * var nodes = document.getElementsByTagName( "span" );
  2938. *
  2939. * //output: true
  2940. * console.log( UE.dom.domUtils.isSameElement( nodes[0], nodes[1] ) );
  2941. *
  2942. * //output: false
  2943. * console.log( UE.dom.domUtils.isSameElement( nodes[2], nodes[3] ) );
  2944. *
  2945. * </script>
  2946. * ```
  2947. */
  2948. isSameElement: function (nodeA, nodeB) {
  2949. if (nodeA.tagName != nodeB.tagName) {
  2950. return false
  2951. }
  2952. var thisAttrs = nodeA.attributes,
  2953. otherAttrs = nodeB.attributes
  2954. if (!ie && thisAttrs.length != otherAttrs.length) {
  2955. return false
  2956. }
  2957. var attrA,
  2958. attrB,
  2959. al = 0,
  2960. bl = 0
  2961. for (var i = 0; (attrA = thisAttrs[i++]); ) {
  2962. if (attrA.nodeName == 'style') {
  2963. if (attrA.specified) {
  2964. al++
  2965. }
  2966. if (domUtils.isSameStyle(nodeA, nodeB)) {
  2967. continue
  2968. } else {
  2969. return false
  2970. }
  2971. }
  2972. if (ie) {
  2973. if (attrA.specified) {
  2974. al++
  2975. attrB = otherAttrs.getNamedItem(attrA.nodeName)
  2976. } else {
  2977. continue
  2978. }
  2979. } else {
  2980. attrB = nodeB.attributes[attrA.nodeName]
  2981. }
  2982. if (!attrB.specified || attrA.nodeValue != attrB.nodeValue) {
  2983. return false
  2984. }
  2985. }
  2986. // 有可能attrB的属性包含了attrA的属性之外还有自己的属性
  2987. if (ie) {
  2988. for (i = 0; (attrB = otherAttrs[i++]); ) {
  2989. if (attrB.specified) {
  2990. bl++
  2991. }
  2992. }
  2993. if (al != bl) {
  2994. return false
  2995. }
  2996. }
  2997. return true
  2998. },
  2999. /**
  3000. * 判断节点nodeA与节点nodeB的元素的style属性是否一致
  3001. * @method isSameStyle
  3002. * @param { Node } nodeA 需要比较的节点
  3003. * @param { Node } nodeB 需要比较的节点
  3004. * @return { Boolean } 两个节点是否具有相同的style属性值
  3005. * @example
  3006. * ```html
  3007. * <span style="font-size:12px">ssss</span>
  3008. * <span style="font-size:12px">bbbbb</span>
  3009. * <span style="font-size:13px">ssss</span>
  3010. * <span style="font-size:14px">bbbbb</span>
  3011. *
  3012. * <script>
  3013. *
  3014. * var nodes = document.getElementsByTagName( "span" );
  3015. *
  3016. * //output: true
  3017. * console.log( UE.dom.domUtils.isSameStyle( nodes[0], nodes[1] ) );
  3018. *
  3019. * //output: false
  3020. * console.log( UE.dom.domUtils.isSameStyle( nodes[2], nodes[3] ) );
  3021. *
  3022. * </script>
  3023. * ```
  3024. */
  3025. isSameStyle: function (nodeA, nodeB) {
  3026. var styleA = nodeA.style.cssText
  3027. .replace(/( ?; ?)/g, ';')
  3028. .replace(/( ?: ?)/g, ':'),
  3029. styleB = nodeB.style.cssText
  3030. .replace(/( ?; ?)/g, ';')
  3031. .replace(/( ?: ?)/g, ':')
  3032. if (browser.opera) {
  3033. styleA = nodeA.style
  3034. styleB = nodeB.style
  3035. if (styleA.length != styleB.length) return false
  3036. for (var p in styleA) {
  3037. if (/^(\d+|csstext)$/i.test(p)) {
  3038. continue
  3039. }
  3040. if (styleA[p] != styleB[p]) {
  3041. return false
  3042. }
  3043. }
  3044. return true
  3045. }
  3046. if (!styleA || !styleB) {
  3047. return styleA == styleB
  3048. }
  3049. styleA = styleA.split(';')
  3050. styleB = styleB.split(';')
  3051. if (styleA.length != styleB.length) {
  3052. return false
  3053. }
  3054. for (var i = 0, ci; (ci = styleA[i++]); ) {
  3055. if (utils.indexOf(styleB, ci) == -1) {
  3056. return false
  3057. }
  3058. }
  3059. return true
  3060. },
  3061. /**
  3062. * 检查节点node是否为block元素
  3063. * @method isBlockElm
  3064. * @param { Node } node 需要检测的节点对象
  3065. * @return { Boolean } 是否是block元素节点
  3066. * @warning 该方法的判断规则如下: 如果该元素原本是block元素, 则不论该元素当前的css样式是什么都会返回true;
  3067. * 否则,检测该元素的css样式, 如果该元素当前是block元素, 则返回true。 其余情况下都返回false。
  3068. * @example
  3069. * ```html
  3070. * <span id="test1" style="display: block"></span>
  3071. * <span id="test2"></span>
  3072. * <div id="test3" style="display: inline"></div>
  3073. *
  3074. * <script>
  3075. *
  3076. * //output: true
  3077. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test1") ) );
  3078. *
  3079. * //output: false
  3080. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test2") ) );
  3081. *
  3082. * //output: true
  3083. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test3") ) );
  3084. *
  3085. * </script>
  3086. * ```
  3087. */
  3088. isBlockElm: function (node) {
  3089. return (
  3090. node.nodeType == 1 &&
  3091. (dtd.$block[node.tagName] ||
  3092. styleBlock[domUtils.getComputedStyle(node, 'display')]) &&
  3093. !dtd.$nonChild[node.tagName]
  3094. )
  3095. },
  3096. /**
  3097. * 检测node节点是否为body节点
  3098. * @method isBody
  3099. * @param { Element } node 需要检测的dom元素
  3100. * @return { Boolean } 给定的元素是否是body元素
  3101. * @example
  3102. * ```javascript
  3103. * //output: true
  3104. * console.log( UE.dom.domUtils.isBody( document.body ) );
  3105. * ```
  3106. */
  3107. isBody: function (node) {
  3108. return node && node.nodeType == 1 && node.tagName.toLowerCase() == 'body'
  3109. },
  3110. /**
  3111. * 以node节点为分界,将该节点的指定祖先节点parent拆分成两个独立的节点,
  3112. * 拆分形成的两个节点之间是node节点
  3113. * @method breakParent
  3114. * @param { Node } node 作为分界的节点对象
  3115. * @param { Node } parent 该节点必须是node节点的祖先节点, 且是block节点。
  3116. * @return { Node } 给定的node分界节点
  3117. * @example
  3118. * ```javascript
  3119. *
  3120. * var node = document.createElement("span"),
  3121. * wrapNode = document.createElement( "div" ),
  3122. * parent = document.createElement("p");
  3123. *
  3124. * parent.appendChild( node );
  3125. * wrapNode.appendChild( parent );
  3126. *
  3127. * //拆分前
  3128. * //output: <p><span></span></p>
  3129. * console.log( wrapNode.innerHTML );
  3130. *
  3131. *
  3132. * UE.dom.domUtils.breakParent( node, parent );
  3133. * //拆分后
  3134. * //output: <p></p><span></span><p></p>
  3135. * console.log( wrapNode.innerHTML );
  3136. *
  3137. * ```
  3138. */
  3139. breakParent: function (node, parent) {
  3140. var tmpNode,
  3141. parentClone = node,
  3142. clone = node,
  3143. leftNodes,
  3144. rightNodes
  3145. do {
  3146. parentClone = parentClone.parentNode
  3147. if (leftNodes) {
  3148. tmpNode = parentClone.cloneNode(false)
  3149. tmpNode.appendChild(leftNodes)
  3150. leftNodes = tmpNode
  3151. tmpNode = parentClone.cloneNode(false)
  3152. tmpNode.appendChild(rightNodes)
  3153. rightNodes = tmpNode
  3154. } else {
  3155. leftNodes = parentClone.cloneNode(false)
  3156. rightNodes = leftNodes.cloneNode(false)
  3157. }
  3158. while ((tmpNode = clone.previousSibling)) {
  3159. leftNodes.insertBefore(tmpNode, leftNodes.firstChild)
  3160. }
  3161. while ((tmpNode = clone.nextSibling)) {
  3162. rightNodes.appendChild(tmpNode)
  3163. }
  3164. clone = parentClone
  3165. } while (parent !== parentClone)
  3166. tmpNode = parent.parentNode
  3167. tmpNode.insertBefore(leftNodes, parent)
  3168. tmpNode.insertBefore(rightNodes, parent)
  3169. tmpNode.insertBefore(node, rightNodes)
  3170. domUtils.remove(parent)
  3171. return node
  3172. },
  3173. /**
  3174. * 检查节点node是否是空inline节点
  3175. * @method isEmptyInlineElement
  3176. * @param { Node } node 需要检测的节点对象
  3177. * @return { Number } 如果给定的节点是空的inline节点, 则返回1, 否则返回0。
  3178. * @example
  3179. * ```html
  3180. * <b><i></i></b> => 1
  3181. * <b><i></i><u></u></b> => 1
  3182. * <b></b> => 1
  3183. * <b>xx<i></i></b> => 0
  3184. * ```
  3185. */
  3186. isEmptyInlineElement: function (node) {
  3187. if (node.nodeType != 1 || !dtd.$removeEmpty[node.tagName]) {
  3188. return 0
  3189. }
  3190. node = node.firstChild
  3191. while (node) {
  3192. //如果是创建的bookmark就跳过
  3193. if (domUtils.isBookmarkNode(node)) {
  3194. return 0
  3195. }
  3196. if (
  3197. (node.nodeType == 1 && !domUtils.isEmptyInlineElement(node)) ||
  3198. (node.nodeType == 3 && !domUtils.isWhitespace(node))
  3199. ) {
  3200. return 0
  3201. }
  3202. node = node.nextSibling
  3203. }
  3204. return 1
  3205. },
  3206. /**
  3207. * 删除node节点下首尾两端的空白文本子节点
  3208. * @method trimWhiteTextNode
  3209. * @param { Element } node 需要执行删除操作的元素对象
  3210. * @example
  3211. * ```javascript
  3212. * var node = document.createElement("div");
  3213. *
  3214. * node.appendChild( document.createTextNode( "" ) );
  3215. *
  3216. * node.appendChild( document.createElement("div") );
  3217. *
  3218. * node.appendChild( document.createTextNode( "" ) );
  3219. *
  3220. * //3
  3221. * console.log( node.childNodes.length );
  3222. *
  3223. * UE.dom.domUtils.trimWhiteTextNode( node );
  3224. *
  3225. * //1
  3226. * console.log( node.childNodes.length );
  3227. * ```
  3228. */
  3229. trimWhiteTextNode: function (node) {
  3230. function remove(dir) {
  3231. var child
  3232. while (
  3233. (child = node[dir]) &&
  3234. child.nodeType == 3 &&
  3235. domUtils.isWhitespace(child)
  3236. ) {
  3237. node.removeChild(child)
  3238. }
  3239. }
  3240. remove('firstChild')
  3241. remove('lastChild')
  3242. },
  3243. /**
  3244. * 合并node节点下相同的子节点
  3245. * @name mergeChild
  3246. * @desc
  3247. * UE.dom.domUtils.mergeChild(node,tagName) //tagName要合并的子节点的标签
  3248. * @example
  3249. * <p><span style="font-size:12px;">xx<span style="font-size:12px;">aa</span>xx</span></p>
  3250. * ==> UE.dom.domUtils.mergeChild(node,'span')
  3251. * <p><span style="font-size:12px;">xxaaxx</span></p>
  3252. */
  3253. mergeChild: function (node, tagName, attrs) {
  3254. var list = domUtils.getElementsByTagName(node, node.tagName.toLowerCase())
  3255. for (var i = 0, ci; (ci = list[i++]); ) {
  3256. if (!ci.parentNode || domUtils.isBookmarkNode(ci)) {
  3257. continue
  3258. }
  3259. //span单独处理
  3260. if (ci.tagName.toLowerCase() == 'span') {
  3261. if (node === ci.parentNode) {
  3262. domUtils.trimWhiteTextNode(node)
  3263. if (node.childNodes.length == 1) {
  3264. node.style.cssText = ci.style.cssText + ';' + node.style.cssText
  3265. domUtils.remove(ci, true)
  3266. continue
  3267. }
  3268. }
  3269. ci.style.cssText = node.style.cssText + ';' + ci.style.cssText
  3270. if (attrs) {
  3271. var style = attrs.style
  3272. if (style) {
  3273. style = style.split(';')
  3274. for (var j = 0, s; (s = style[j++]); ) {
  3275. ci.style[utils.cssStyleToDomStyle(s.split(':')[0])] =
  3276. s.split(':')[1]
  3277. }
  3278. }
  3279. }
  3280. if (domUtils.isSameStyle(ci, node)) {
  3281. domUtils.remove(ci, true)
  3282. }
  3283. continue
  3284. }
  3285. if (domUtils.isSameElement(node, ci)) {
  3286. domUtils.remove(ci, true)
  3287. }
  3288. }
  3289. },
  3290. /**
  3291. * 原生方法getElementsByTagName的封装
  3292. * @method getElementsByTagName
  3293. * @param { Node } node 目标节点对象
  3294. * @param { String } tagName 需要查找的节点的tagName, 多个tagName以空格分割
  3295. * @return { Array } 符合条件的节点集合
  3296. */
  3297. getElementsByTagName: function (node, name, filter) {
  3298. if (filter && utils.isString(filter)) {
  3299. var className = filter
  3300. filter = function (node) {
  3301. return domUtils.hasClass(node, className)
  3302. }
  3303. }
  3304. name = utils
  3305. .trim(name)
  3306. .replace(/[ ]{2,}/g, ' ')
  3307. .split(' ')
  3308. var arr = []
  3309. for (var n = 0, ni; (ni = name[n++]); ) {
  3310. var list = node.getElementsByTagName(ni)
  3311. for (var i = 0, ci; (ci = list[i++]); ) {
  3312. if (!filter || filter(ci)) arr.push(ci)
  3313. }
  3314. }
  3315. return arr
  3316. },
  3317. /**
  3318. * 将节点node提取到父节点上
  3319. * @method mergeToParent
  3320. * @param { Element } node 需要提取的元素对象
  3321. * @example
  3322. * ```html
  3323. * <div id="parent">
  3324. * <div id="sub">
  3325. * <span id="child"></span>
  3326. * </div>
  3327. * </div>
  3328. *
  3329. * <script>
  3330. *
  3331. * var child = document.getElementById( "child" );
  3332. *
  3333. * //output: sub
  3334. * console.log( child.parentNode.id );
  3335. *
  3336. * UE.dom.domUtils.mergeToParent( child );
  3337. *
  3338. * //output: parent
  3339. * console.log( child.parentNode.id );
  3340. *
  3341. * </script>
  3342. * ```
  3343. */
  3344. mergeToParent: function (node) {
  3345. var parent = node.parentNode
  3346. while (parent && dtd.$removeEmpty[parent.tagName]) {
  3347. if (parent.tagName == node.tagName || parent.tagName == 'A') {
  3348. //针对a标签单独处理
  3349. domUtils.trimWhiteTextNode(parent)
  3350. //span需要特殊处理 不处理这样的情况 <span stlye="color:#fff">xxx<span style="color:#ccc">xxx</span>xxx</span>
  3351. if (
  3352. (parent.tagName == 'SPAN' && !domUtils.isSameStyle(parent, node)) ||
  3353. (parent.tagName == 'A' && node.tagName == 'SPAN')
  3354. ) {
  3355. if (parent.childNodes.length > 1 || parent !== node.parentNode) {
  3356. node.style.cssText =
  3357. parent.style.cssText + ';' + node.style.cssText
  3358. parent = parent.parentNode
  3359. continue
  3360. } else {
  3361. parent.style.cssText += ';' + node.style.cssText
  3362. //trace:952 a标签要保持下划线
  3363. if (parent.tagName == 'A') {
  3364. parent.style.textDecoration = 'underline'
  3365. }
  3366. }
  3367. }
  3368. if (parent.tagName != 'A') {
  3369. parent === node.parentNode && domUtils.remove(node, true)
  3370. break
  3371. }
  3372. }
  3373. parent = parent.parentNode
  3374. }
  3375. },
  3376. /**
  3377. * 合并节点node的左右兄弟节点
  3378. * @method mergeSibling
  3379. * @param { Element } node 需要合并的目标节点
  3380. * @example
  3381. * ```html
  3382. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3383. *
  3384. * <script>
  3385. * var demoNode = document.getElementById("test");
  3386. * UE.dom.domUtils.mergeSibling( demoNode );
  3387. * //output: xxxxoooxxxx
  3388. * console.log( demoNode.innerHTML );
  3389. * </script>
  3390. * ```
  3391. */
  3392. /**
  3393. * 合并节点node的左右兄弟节点, 可以根据给定的条件选择是否忽略合并左节点。
  3394. * @method mergeSibling
  3395. * @param { Element } node 需要合并的目标节点
  3396. * @param { Boolean } ignorePre 是否忽略合并左节点
  3397. * @example
  3398. * ```html
  3399. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3400. *
  3401. * <script>
  3402. * var demoNode = document.getElementById("test");
  3403. * UE.dom.domUtils.mergeSibling( demoNode, true );
  3404. * //output: oooxxxx
  3405. * console.log( demoNode.innerHTML );
  3406. * </script>
  3407. * ```
  3408. */
  3409. /**
  3410. * 合并节点node的左右兄弟节点,可以根据给定的条件选择是否忽略合并左右节点。
  3411. * @method mergeSibling
  3412. * @param { Element } node 需要合并的目标节点
  3413. * @param { Boolean } ignorePre 是否忽略合并左节点
  3414. * @param { Boolean } ignoreNext 是否忽略合并右节点
  3415. * @remind 如果同时忽略左右节点, 则该操作什么也不会做
  3416. * @example
  3417. * ```html
  3418. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3419. *
  3420. * <script>
  3421. * var demoNode = document.getElementById("test");
  3422. * UE.dom.domUtils.mergeSibling( demoNode, false, true );
  3423. * //output: xxxxooo
  3424. * console.log( demoNode.innerHTML );
  3425. * </script>
  3426. * ```
  3427. */
  3428. mergeSibling: function (node, ignorePre, ignoreNext) {
  3429. function merge(rtl, start, node) {
  3430. var next
  3431. if (
  3432. (next = node[rtl]) &&
  3433. !domUtils.isBookmarkNode(next) &&
  3434. next.nodeType == 1 &&
  3435. domUtils.isSameElement(node, next)
  3436. ) {
  3437. while (next.firstChild) {
  3438. if (start == 'firstChild') {
  3439. node.insertBefore(next.lastChild, node.firstChild)
  3440. } else {
  3441. node.appendChild(next.firstChild)
  3442. }
  3443. }
  3444. domUtils.remove(next)
  3445. }
  3446. }
  3447. !ignorePre && merge('previousSibling', 'firstChild', node)
  3448. !ignoreNext && merge('nextSibling', 'lastChild', node)
  3449. },
  3450. /**
  3451. * 设置节点node及其子节点不会被选中
  3452. * @method unSelectable
  3453. * @param { Element } node 需要执行操作的dom元素
  3454. * @remind 执行该操作后的节点, 将不能被鼠标选中
  3455. * @example
  3456. * ```javascript
  3457. * UE.dom.domUtils.unSelectable( document.body );
  3458. * ```
  3459. */
  3460. unSelectable:
  3461. (ie && browser.ie9below) || browser.opera
  3462. ? function (node) {
  3463. //for ie9
  3464. node.onselectstart = function () {
  3465. return false
  3466. }
  3467. node.onclick =
  3468. node.onkeyup =
  3469. node.onkeydown =
  3470. function () {
  3471. return false
  3472. }
  3473. node.unselectable = 'on'
  3474. node.setAttribute('unselectable', 'on')
  3475. for (var i = 0, ci; (ci = node.all[i++]); ) {
  3476. switch (ci.tagName.toLowerCase()) {
  3477. case 'iframe':
  3478. case 'textarea':
  3479. case 'input':
  3480. case 'select':
  3481. break
  3482. default:
  3483. ci.unselectable = 'on'
  3484. node.setAttribute('unselectable', 'on')
  3485. }
  3486. }
  3487. }
  3488. : function (node) {
  3489. node.style.MozUserSelect =
  3490. node.style.webkitUserSelect =
  3491. node.style.msUserSelect =
  3492. node.style.KhtmlUserSelect =
  3493. 'none'
  3494. },
  3495. /**
  3496. * 删除节点node上的指定属性名称的属性
  3497. * @method removeAttributes
  3498. * @param { Node } node 需要删除属性的节点对象
  3499. * @param { String } attrNames 可以是空格隔开的多个属性名称,该操作将会依次删除相应的属性
  3500. * @example
  3501. * ```html
  3502. * <div id="wrap">
  3503. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3504. * </div>
  3505. *
  3506. * <script>
  3507. *
  3508. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), "id name" );
  3509. *
  3510. * //output: <span style="font-size:14px;">xxxxx</span>
  3511. * console.log( document.getElementById("wrap").innerHTML );
  3512. *
  3513. * </script>
  3514. * ```
  3515. */
  3516. /**
  3517. * 删除节点node上的指定属性名称的属性
  3518. * @method removeAttributes
  3519. * @param { Node } node 需要删除属性的节点对象
  3520. * @param { Array } attrNames 需要删除的属性名数组
  3521. * @example
  3522. * ```html
  3523. * <div id="wrap">
  3524. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3525. * </div>
  3526. *
  3527. * <script>
  3528. *
  3529. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), ["id", "name"] );
  3530. *
  3531. * //output: <span style="font-size:14px;">xxxxx</span>
  3532. * console.log( document.getElementById("wrap").innerHTML );
  3533. *
  3534. * </script>
  3535. * ```
  3536. */
  3537. removeAttributes: function (node, attrNames) {
  3538. attrNames = utils.isArray(attrNames)
  3539. ? attrNames
  3540. : utils
  3541. .trim(attrNames)
  3542. .replace(/[ ]{2,}/g, ' ')
  3543. .split(' ')
  3544. for (var i = 0, ci; (ci = attrNames[i++]); ) {
  3545. ci = attrFix[ci] || ci
  3546. switch (ci) {
  3547. case 'className':
  3548. node[ci] = ''
  3549. break
  3550. case 'style':
  3551. node.style.cssText = ''
  3552. var val = node.getAttributeNode('style')
  3553. !browser.ie && val && node.removeAttributeNode(val)
  3554. }
  3555. node.removeAttribute(ci)
  3556. }
  3557. },
  3558. /**
  3559. * 在doc下创建一个标签名为tag,属性为attrs的元素
  3560. * @method createElement
  3561. * @param { DomDocument } doc 新创建的元素属于该document节点创建
  3562. * @param { String } tagName 需要创建的元素的标签名
  3563. * @param { Object } attrs 新创建的元素的属性key-value集合
  3564. * @return { Element } 新创建的元素对象
  3565. * @example
  3566. * ```javascript
  3567. * var ele = UE.dom.domUtils.createElement( document, 'div', {
  3568. * id: 'test'
  3569. * } );
  3570. *
  3571. * //output: DIV
  3572. * console.log( ele.tagName );
  3573. *
  3574. * //output: test
  3575. * console.log( ele.id );
  3576. *
  3577. * ```
  3578. */
  3579. createElement: function (doc, tag, attrs) {
  3580. return domUtils.setAttributes(doc.createElement(tag), attrs)
  3581. },
  3582. /**
  3583. * 为节点node添加属性attrs,attrs为属性键值对
  3584. * @method setAttributes
  3585. * @param { Element } node 需要设置属性的元素对象
  3586. * @param { Object } attrs 需要设置的属性名-值对
  3587. * @return { Element } 设置属性的元素对象
  3588. * @example
  3589. * ```html
  3590. * <span id="test"></span>
  3591. *
  3592. * <script>
  3593. *
  3594. * var testNode = UE.dom.domUtils.setAttributes( document.getElementById( "test" ), {
  3595. * id: 'demo'
  3596. * } );
  3597. *
  3598. * //output: demo
  3599. * console.log( testNode.id );
  3600. *
  3601. * </script>
  3602. *
  3603. */
  3604. setAttributes: function (node, attrs) {
  3605. for (var attr in attrs) {
  3606. if (attrs.hasOwnProperty(attr)) {
  3607. var value = attrs[attr]
  3608. switch (attr) {
  3609. case 'class':
  3610. //ie下要这样赋值,setAttribute不起作用
  3611. node.className = value
  3612. break
  3613. case 'style':
  3614. node.style.cssText = node.style.cssText + ';' + value
  3615. break
  3616. case 'innerHTML':
  3617. node[attr] = value
  3618. break
  3619. case 'value':
  3620. node.value = value
  3621. break
  3622. default:
  3623. node.setAttribute(attrFix[attr] || attr, value)
  3624. }
  3625. }
  3626. }
  3627. return node
  3628. },
  3629. /**
  3630. * 获取元素element经过计算后的样式值
  3631. * @method getComputedStyle
  3632. * @param { Element } element 需要获取样式的元素对象
  3633. * @param { String } styleName 需要获取的样式名
  3634. * @return { String } 获取到的样式值
  3635. * @example
  3636. * ```html
  3637. * <style type="text/css">
  3638. * #test {
  3639. * font-size: 15px;
  3640. * }
  3641. * </style>
  3642. *
  3643. * <span id="test"></span>
  3644. *
  3645. * <script>
  3646. * //output: 15px
  3647. * console.log( UE.dom.domUtils.getComputedStyle( document.getElementById( "test" ), 'font-size' ) );
  3648. * </script>
  3649. * ```
  3650. */
  3651. getComputedStyle: function (element, styleName) {
  3652. //一下的属性单独处理
  3653. var pros = 'width height top left'
  3654. if (pros.indexOf(styleName) > -1) {
  3655. return (
  3656. element[
  3657. 'offset' +
  3658. styleName.replace(/^\w/, function (s) {
  3659. return s.toUpperCase()
  3660. })
  3661. ] + 'px'
  3662. )
  3663. }
  3664. //忽略文本节点
  3665. if (element.nodeType == 3) {
  3666. element = element.parentNode
  3667. }
  3668. //ie下font-size若body下定义了font-size,则从currentStyle里会取到这个font-size. 取不到实际值,故此修改.
  3669. if (
  3670. browser.ie &&
  3671. browser.version < 9 &&
  3672. styleName == 'font-size' &&
  3673. !element.style.fontSize &&
  3674. !dtd.$empty[element.tagName] &&
  3675. !dtd.$nonChild[element.tagName]
  3676. ) {
  3677. var span = element.ownerDocument.createElement('span')
  3678. span.style.cssText = 'padding:0;border:0;font-family:simsun;'
  3679. span.innerHTML = '.'
  3680. element.appendChild(span)
  3681. var result = span.offsetHeight
  3682. element.removeChild(span)
  3683. span = null
  3684. return result + 'px'
  3685. }
  3686. try {
  3687. var value =
  3688. domUtils.getStyle(element, styleName) ||
  3689. (window.getComputedStyle
  3690. ? domUtils
  3691. .getWindow(element)
  3692. .getComputedStyle(element, '')
  3693. .getPropertyValue(styleName)
  3694. : (element.currentStyle || element.style)[
  3695. utils.cssStyleToDomStyle(styleName)
  3696. ])
  3697. } catch (e) {
  3698. return ''
  3699. }
  3700. return utils.transUnitToPx(utils.fixColor(styleName, value))
  3701. },
  3702. /**
  3703. * 删除元素element指定的className
  3704. * @method removeClasses
  3705. * @param { Element } ele 需要删除class的元素节点
  3706. * @param { String } classNames 需要删除的className, 多个className之间以空格分开
  3707. * @example
  3708. * ```html
  3709. * <span id="test" class="test1 test2 test3">xxx</span>
  3710. *
  3711. * <script>
  3712. *
  3713. * var testNode = document.getElementById( "test" );
  3714. * UE.dom.domUtils.removeClasses( testNode, "test1 test2" );
  3715. *
  3716. * //output: test3
  3717. * console.log( testNode.className );
  3718. *
  3719. * </script>
  3720. * ```
  3721. */
  3722. /**
  3723. * 删除元素element指定的className
  3724. * @method removeClasses
  3725. * @param { Element } ele 需要删除class的元素节点
  3726. * @param { Array } classNames 需要删除的className数组
  3727. * @example
  3728. * ```html
  3729. * <span id="test" class="test1 test2 test3">xxx</span>
  3730. *
  3731. * <script>
  3732. *
  3733. * var testNode = document.getElementById( "test" );
  3734. * UE.dom.domUtils.removeClasses( testNode, ["test1", "test2"] );
  3735. *
  3736. * //output: test3
  3737. * console.log( testNode.className );
  3738. *
  3739. * </script>
  3740. * ```
  3741. */
  3742. removeClasses: function (elm, classNames) {
  3743. classNames = utils.isArray(classNames)
  3744. ? classNames
  3745. : utils
  3746. .trim(classNames)
  3747. .replace(/[ ]{2,}/g, ' ')
  3748. .split(' ')
  3749. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3750. cls = cls.replace(new RegExp('\\b' + ci + '\\b'), '')
  3751. }
  3752. cls = utils.trim(cls).replace(/[ ]{2,}/g, ' ')
  3753. if (cls) {
  3754. elm.className = cls
  3755. } else {
  3756. domUtils.removeAttributes(elm, ['class'])
  3757. }
  3758. },
  3759. /**
  3760. * 给元素element添加className
  3761. * @method addClass
  3762. * @param { Node } ele 需要增加className的元素
  3763. * @param { String } classNames 需要添加的className, 多个className之间以空格分割
  3764. * @remind 相同的类名不会被重复添加
  3765. * @example
  3766. * ```html
  3767. * <span id="test" class="cls1 cls2"></span>
  3768. *
  3769. * <script>
  3770. * var testNode = document.getElementById("test");
  3771. *
  3772. * UE.dom.domUtils.addClass( testNode, "cls2 cls3 cls4" );
  3773. *
  3774. * //output: cl1 cls2 cls3 cls4
  3775. * console.log( testNode.className );
  3776. *
  3777. * <script>
  3778. * ```
  3779. */
  3780. /**
  3781. * 给元素element添加className
  3782. * @method addClass
  3783. * @param { Node } ele 需要增加className的元素
  3784. * @param { Array } classNames 需要添加的className的数组
  3785. * @remind 相同的类名不会被重复添加
  3786. * @example
  3787. * ```html
  3788. * <span id="test" class="cls1 cls2"></span>
  3789. *
  3790. * <script>
  3791. * var testNode = document.getElementById("test");
  3792. *
  3793. * UE.dom.domUtils.addClass( testNode, ["cls2", "cls3", "cls4"] );
  3794. *
  3795. * //output: cl1 cls2 cls3 cls4
  3796. * console.log( testNode.className );
  3797. *
  3798. * <script>
  3799. * ```
  3800. */
  3801. addClass: function (elm, classNames) {
  3802. if (!elm) return
  3803. classNames = utils
  3804. .trim(classNames)
  3805. .replace(/[ ]{2,}/g, ' ')
  3806. .split(' ')
  3807. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3808. if (!new RegExp('\\b' + ci + '\\b').test(cls)) {
  3809. cls += ' ' + ci
  3810. }
  3811. }
  3812. elm.className = utils.trim(cls)
  3813. },
  3814. /**
  3815. * 判断元素element是否包含给定的样式类名className
  3816. * @method hasClass
  3817. * @param { Node } ele 需要检测的元素
  3818. * @param { String } classNames 需要检测的className, 多个className之间用空格分割
  3819. * @return { Boolean } 元素是否包含所有给定的className
  3820. * @example
  3821. * ```html
  3822. * <span id="test1" class="cls1 cls2"></span>
  3823. *
  3824. * <script>
  3825. * var test1 = document.getElementById("test1");
  3826. *
  3827. * //output: false
  3828. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1 cls3" ) );
  3829. *
  3830. * //output: true
  3831. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1" ) );
  3832. * </script>
  3833. * ```
  3834. */
  3835. /**
  3836. * 判断元素element是否包含给定的样式类名className
  3837. * @method hasClass
  3838. * @param { Node } ele 需要检测的元素
  3839. * @param { Array } classNames 需要检测的className数组
  3840. * @return { Boolean } 元素是否包含所有给定的className
  3841. * @example
  3842. * ```html
  3843. * <span id="test1" class="cls1 cls2"></span>
  3844. *
  3845. * <script>
  3846. * var test1 = document.getElementById("test1");
  3847. *
  3848. * //output: false
  3849. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1", "cls3" ] ) );
  3850. *
  3851. * //output: true
  3852. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1" ]) );
  3853. * </script>
  3854. * ```
  3855. */
  3856. hasClass: function (element, className) {
  3857. if (utils.isRegExp(className)) {
  3858. return className.test(element.className)
  3859. }
  3860. className = utils
  3861. .trim(className)
  3862. .replace(/[ ]{2,}/g, ' ')
  3863. .split(' ')
  3864. for (var i = 0, ci, cls = element.className; (ci = className[i++]); ) {
  3865. if (!new RegExp('\\b' + ci + '\\b', 'i').test(cls)) {
  3866. return false
  3867. }
  3868. }
  3869. return i - 1 == className.length
  3870. },
  3871. /**
  3872. * 阻止事件默认行为
  3873. * @method preventDefault
  3874. * @param { Event } evt 需要阻止默认行为的事件对象
  3875. * @example
  3876. * ```javascript
  3877. * UE.dom.domUtils.preventDefault( evt );
  3878. * ```
  3879. */
  3880. preventDefault: function (evt) {
  3881. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false)
  3882. },
  3883. /**
  3884. * 删除元素element指定的样式
  3885. * @method removeStyle
  3886. * @param { Element } element 需要删除样式的元素
  3887. * @param { String } styleName 需要删除的样式名
  3888. * @example
  3889. * ```html
  3890. * <span id="test" style="color: red; background: blue;"></span>
  3891. *
  3892. * <script>
  3893. *
  3894. * var testNode = document.getElementById("test");
  3895. *
  3896. * UE.dom.domUtils.removeStyle( testNode, 'color' );
  3897. *
  3898. * //output: background: blue;
  3899. * console.log( testNode.style.cssText );
  3900. *
  3901. * </script>
  3902. * ```
  3903. */
  3904. removeStyle: function (element, name) {
  3905. if (browser.ie) {
  3906. //针对color先单独处理一下
  3907. if (name == 'color') {
  3908. name = '(^|;)' + name
  3909. }
  3910. element.style.cssText = element.style.cssText.replace(
  3911. new RegExp(name + '[^:]*:[^;]+;?', 'ig'),
  3912. ''
  3913. )
  3914. } else {
  3915. if (element.style.removeProperty) {
  3916. element.style.removeProperty(name)
  3917. } else {
  3918. element.style.removeAttribute(utils.cssStyleToDomStyle(name))
  3919. }
  3920. }
  3921. if (!element.style.cssText) {
  3922. domUtils.removeAttributes(element, ['style'])
  3923. }
  3924. },
  3925. /**
  3926. * 获取元素element的style属性的指定值
  3927. * @method getStyle
  3928. * @param { Element } element 需要获取属性值的元素
  3929. * @param { String } styleName 需要获取的style的名称
  3930. * @warning 该方法仅获取元素style属性中所标明的值
  3931. * @return { String } 该元素包含指定的style属性值
  3932. * @example
  3933. * ```html
  3934. * <div id="test" style="color: red;"></div>
  3935. *
  3936. * <script>
  3937. *
  3938. * var testNode = document.getElementById( "test" );
  3939. *
  3940. * //output: red
  3941. * console.log( UE.dom.domUtils.getStyle( testNode, "color" ) );
  3942. *
  3943. * //output: ""
  3944. * console.log( UE.dom.domUtils.getStyle( testNode, "background" ) );
  3945. *
  3946. * </script>
  3947. * ```
  3948. */
  3949. getStyle: function (element, name) {
  3950. var value = element.style[utils.cssStyleToDomStyle(name)]
  3951. return utils.fixColor(name, value)
  3952. },
  3953. /**
  3954. * 为元素element设置样式属性值
  3955. * @method setStyle
  3956. * @param { Element } element 需要设置样式的元素
  3957. * @param { String } styleName 样式名
  3958. * @param { String } styleValue 样式值
  3959. * @example
  3960. * ```html
  3961. * <div id="test"></div>
  3962. *
  3963. * <script>
  3964. *
  3965. * var testNode = document.getElementById( "test" );
  3966. *
  3967. * //output: ""
  3968. * console.log( testNode.style.color );
  3969. *
  3970. * UE.dom.domUtils.setStyle( testNode, 'color', 'red' );
  3971. * //output: "red"
  3972. * console.log( testNode.style.color );
  3973. *
  3974. * </script>
  3975. * ```
  3976. */
  3977. setStyle: function (element, name, value) {
  3978. element.style[utils.cssStyleToDomStyle(name)] = value
  3979. if (!utils.trim(element.style.cssText)) {
  3980. this.removeAttributes(element, 'style')
  3981. }
  3982. },
  3983. /**
  3984. * 为元素element设置多个样式属性值
  3985. * @method setStyles
  3986. * @param { Element } element 需要设置样式的元素
  3987. * @param { Object } styles 样式名值对
  3988. * @example
  3989. * ```html
  3990. * <div id="test"></div>
  3991. *
  3992. * <script>
  3993. *
  3994. * var testNode = document.getElementById( "test" );
  3995. *
  3996. * //output: ""
  3997. * console.log( testNode.style.color );
  3998. *
  3999. * UE.dom.domUtils.setStyles( testNode, {
  4000. * 'color': 'red'
  4001. * } );
  4002. * //output: "red"
  4003. * console.log( testNode.style.color );
  4004. *
  4005. * </script>
  4006. * ```
  4007. */
  4008. setStyles: function (element, styles) {
  4009. for (var name in styles) {
  4010. if (styles.hasOwnProperty(name)) {
  4011. domUtils.setStyle(element, name, styles[name])
  4012. }
  4013. }
  4014. },
  4015. /**
  4016. * 删除_moz_dirty属性
  4017. * @private
  4018. * @method removeDirtyAttr
  4019. */
  4020. removeDirtyAttr: function (node) {
  4021. for (
  4022. var i = 0, ci, nodes = node.getElementsByTagName('*');
  4023. (ci = nodes[i++]);
  4024. ) {
  4025. ci.removeAttribute('_moz_dirty')
  4026. }
  4027. node.removeAttribute('_moz_dirty')
  4028. },
  4029. /**
  4030. * 获取子节点的数量
  4031. * @method getChildCount
  4032. * @param { Element } node 需要检测的元素
  4033. * @return { Number } 给定的node元素的子节点数量
  4034. * @example
  4035. * ```html
  4036. * <div id="test">
  4037. * <span></span>
  4038. * </div>
  4039. *
  4040. * <script>
  4041. *
  4042. * //output: 3
  4043. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test") ) );
  4044. *
  4045. * </script>
  4046. * ```
  4047. */
  4048. /**
  4049. * 根据给定的过滤规则, 获取符合条件的子节点的数量
  4050. * @method getChildCount
  4051. * @param { Element } node 需要检测的元素
  4052. * @param { Function } fn 过滤器, 要求对符合条件的子节点返回true, 反之则要求返回false
  4053. * @return { Number } 符合过滤条件的node元素的子节点数量
  4054. * @example
  4055. * ```html
  4056. * <div id="test">
  4057. * <span></span>
  4058. * </div>
  4059. *
  4060. * <script>
  4061. *
  4062. * //output: 1
  4063. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test"), function ( node ) {
  4064. *
  4065. * return node.nodeType === 1;
  4066. *
  4067. * } ) );
  4068. *
  4069. * </script>
  4070. * ```
  4071. */
  4072. getChildCount: function (node, fn) {
  4073. var count = 0,
  4074. first = node.firstChild
  4075. fn =
  4076. fn ||
  4077. function () {
  4078. return 1
  4079. }
  4080. while (first) {
  4081. if (fn(first)) {
  4082. count++
  4083. }
  4084. first = first.nextSibling
  4085. }
  4086. return count
  4087. },
  4088. /**
  4089. * 判断给定节点是否为空节点
  4090. * @method isEmptyNode
  4091. * @param { Node } node 需要检测的节点对象
  4092. * @return { Boolean } 节点是否为空
  4093. * @example
  4094. * ```javascript
  4095. * UE.dom.domUtils.isEmptyNode( document.body );
  4096. * ```
  4097. */
  4098. isEmptyNode: function (node) {
  4099. return (
  4100. !node.firstChild ||
  4101. domUtils.getChildCount(node, function (node) {
  4102. return (
  4103. !domUtils.isBr(node) &&
  4104. !domUtils.isBookmarkNode(node) &&
  4105. !domUtils.isWhitespace(node)
  4106. )
  4107. }) == 0
  4108. )
  4109. },
  4110. clearSelectedArr: function (nodes) {
  4111. var node
  4112. while ((node = nodes.pop())) {
  4113. domUtils.removeAttributes(node, ['class'])
  4114. }
  4115. },
  4116. /**
  4117. * 将显示区域滚动到指定节点的位置
  4118. * @method scrollToView
  4119. * @param {Node} node 节点
  4120. * @param {window} win window对象
  4121. * @param {Number} offsetTop 距离上方的偏移量
  4122. */
  4123. scrollToView: function (node, win, offsetTop) {
  4124. var getViewPaneSize = function () {
  4125. var doc = win.document,
  4126. mode = doc.compatMode == 'CSS1Compat'
  4127. return {
  4128. width:
  4129. (mode ? doc.documentElement.clientWidth : doc.body.clientWidth) ||
  4130. 0,
  4131. height:
  4132. (mode
  4133. ? doc.documentElement.clientHeight
  4134. : doc.body.clientHeight) || 0,
  4135. }
  4136. },
  4137. getScrollPosition = function (win) {
  4138. if ('pageXOffset' in win) {
  4139. return {
  4140. x: win.pageXOffset || 0,
  4141. y: win.pageYOffset || 0,
  4142. }
  4143. } else {
  4144. var doc = win.document
  4145. return {
  4146. x: doc.documentElement.scrollLeft || doc.body.scrollLeft || 0,
  4147. y: doc.documentElement.scrollTop || doc.body.scrollTop || 0,
  4148. }
  4149. }
  4150. }
  4151. var winHeight = getViewPaneSize().height,
  4152. offset = winHeight * -1 + offsetTop
  4153. offset += node.offsetHeight || 0
  4154. var elementPosition = domUtils.getXY(node)
  4155. offset += elementPosition.y
  4156. var currentScroll = getScrollPosition(win).y
  4157. // offset += 50;
  4158. if (offset > currentScroll || offset < currentScroll - winHeight) {
  4159. win.scrollTo(0, offset + (offset < 0 ? -20 : 20))
  4160. }
  4161. },
  4162. /**
  4163. * 判断给定节点是否为br
  4164. * @method isBr
  4165. * @param { Node } node 需要判断的节点对象
  4166. * @return { Boolean } 给定的节点是否是br节点
  4167. */
  4168. isBr: function (node) {
  4169. return node.nodeType == 1 && node.tagName == 'BR'
  4170. },
  4171. /**
  4172. * 判断给定的节点是否是一个“填充”节点
  4173. * @private
  4174. * @method isFillChar
  4175. * @param { Node } node 需要判断的节点
  4176. * @param { Boolean } isInStart 是否从节点内容的开始位置匹配
  4177. * @returns { Boolean } 节点是否是填充节点
  4178. */
  4179. isFillChar: function (node, isInStart) {
  4180. if (node.nodeType != 3) return false
  4181. var text = node.nodeValue
  4182. if (isInStart) {
  4183. return new RegExp('^' + domUtils.fillChar).test(text)
  4184. }
  4185. return !text.replace(new RegExp(domUtils.fillChar, 'g'), '').length
  4186. },
  4187. isStartInblock: function (range) {
  4188. var tmpRange = range.cloneRange(),
  4189. flag = 0,
  4190. start = tmpRange.startContainer,
  4191. tmp
  4192. if (start.nodeType == 1 && start.childNodes[tmpRange.startOffset]) {
  4193. start = start.childNodes[tmpRange.startOffset]
  4194. var pre = start.previousSibling
  4195. while (pre && domUtils.isFillChar(pre)) {
  4196. start = pre
  4197. pre = pre.previousSibling
  4198. }
  4199. }
  4200. if (this.isFillChar(start, true) && tmpRange.startOffset == 1) {
  4201. tmpRange.setStartBefore(start)
  4202. start = tmpRange.startContainer
  4203. }
  4204. while (start && domUtils.isFillChar(start)) {
  4205. tmp = start
  4206. start = start.previousSibling
  4207. }
  4208. if (tmp) {
  4209. tmpRange.setStartBefore(tmp)
  4210. start = tmpRange.startContainer
  4211. }
  4212. if (
  4213. start.nodeType == 1 &&
  4214. domUtils.isEmptyNode(start) &&
  4215. tmpRange.startOffset == 1
  4216. ) {
  4217. tmpRange.setStart(start, 0).collapse(true)
  4218. }
  4219. while (!tmpRange.startOffset) {
  4220. start = tmpRange.startContainer
  4221. if (domUtils.isBlockElm(start) || domUtils.isBody(start)) {
  4222. flag = 1
  4223. break
  4224. }
  4225. var pre = tmpRange.startContainer.previousSibling,
  4226. tmpNode
  4227. if (!pre) {
  4228. tmpRange.setStartBefore(tmpRange.startContainer)
  4229. } else {
  4230. while (pre && domUtils.isFillChar(pre)) {
  4231. tmpNode = pre
  4232. pre = pre.previousSibling
  4233. }
  4234. if (tmpNode) {
  4235. tmpRange.setStartBefore(tmpNode)
  4236. } else {
  4237. tmpRange.setStartBefore(tmpRange.startContainer)
  4238. }
  4239. }
  4240. }
  4241. return flag && !domUtils.isBody(tmpRange.startContainer) ? 1 : 0
  4242. },
  4243. /**
  4244. * 判断给定的元素是否是一个空元素
  4245. * @method isEmptyBlock
  4246. * @param { Element } node 需要判断的元素
  4247. * @return { Boolean } 是否是空元素
  4248. * @example
  4249. * ```html
  4250. * <div id="test"></div>
  4251. *
  4252. * <script>
  4253. * //output: true
  4254. * console.log( UE.dom.domUtils.isEmptyBlock( document.getElementById("test") ) );
  4255. * </script>
  4256. * ```
  4257. */
  4258. /**
  4259. * 根据指定的判断规则判断给定的元素是否是一个空元素
  4260. * @method isEmptyBlock
  4261. * @param { Element } node 需要判断的元素
  4262. * @param { RegExp } reg 对内容执行判断的正则表达式对象
  4263. * @return { Boolean } 是否是空元素
  4264. */
  4265. isEmptyBlock: function (node, reg) {
  4266. // HaoChuan9421
  4267. if (!node) {
  4268. return
  4269. }
  4270. if (node.nodeType != 1) return 0
  4271. reg = reg || new RegExp('[ \xa0\t\r\n' + domUtils.fillChar + ']', 'g')
  4272. if (
  4273. node[browser.ie ? 'innerText' : 'textContent'].replace(reg, '').length >
  4274. 0
  4275. ) {
  4276. return 0
  4277. }
  4278. for (var n in dtd.$isNotEmpty) {
  4279. if (node.getElementsByTagName(n).length) {
  4280. return 0
  4281. }
  4282. }
  4283. return 1
  4284. },
  4285. /**
  4286. * 移动元素使得该元素的位置移动指定的偏移量的距离
  4287. * @method setViewportOffset
  4288. * @param { Element } element 需要设置偏移量的元素
  4289. * @param { Object } offset 偏移量, 形如{ left: 100, top: 50 }的一个键值对, 表示该元素将在
  4290. * 现有的位置上向水平方向偏移offset.left的距离, 在竖直方向上偏移
  4291. * offset.top的距离
  4292. * @example
  4293. * ```html
  4294. * <div id="test" style="top: 100px; left: 50px; position: absolute;"></div>
  4295. *
  4296. * <script>
  4297. *
  4298. * var testNode = document.getElementById("test");
  4299. *
  4300. * UE.dom.domUtils.setViewportOffset( testNode, {
  4301. * left: 200,
  4302. * top: 50
  4303. * } );
  4304. *
  4305. * //output: top: 300px; left: 100px; position: absolute;
  4306. * console.log( testNode.style.cssText );
  4307. *
  4308. * </script>
  4309. * ```
  4310. */
  4311. setViewportOffset: function (element, offset) {
  4312. var left = parseInt(element.style.left) | 0
  4313. var top = parseInt(element.style.top) | 0
  4314. var rect = element.getBoundingClientRect()
  4315. var offsetLeft = offset.left - rect.left
  4316. var offsetTop = offset.top - rect.top
  4317. if (offsetLeft) {
  4318. element.style.left = left + offsetLeft + 'px'
  4319. }
  4320. if (offsetTop) {
  4321. element.style.top = top + offsetTop + 'px'
  4322. }
  4323. },
  4324. /**
  4325. * 用“填充字符”填充节点
  4326. * @method fillNode
  4327. * @private
  4328. * @param { DomDocument } doc 填充的节点所在的docment对象
  4329. * @param { Node } node 需要填充的节点对象
  4330. * @example
  4331. * ```html
  4332. * <div id="test"></div>
  4333. *
  4334. * <script>
  4335. * var testNode = document.getElementById("test");
  4336. *
  4337. * //output: 0
  4338. * console.log( testNode.childNodes.length );
  4339. *
  4340. * UE.dom.domUtils.fillNode( document, testNode );
  4341. *
  4342. * //output: 1
  4343. * console.log( testNode.childNodes.length );
  4344. *
  4345. * </script>
  4346. * ```
  4347. */
  4348. fillNode: function (doc, node) {
  4349. var tmpNode = browser.ie
  4350. ? doc.createTextNode(domUtils.fillChar)
  4351. : doc.createElement('br')
  4352. node.innerHTML = ''
  4353. node.appendChild(tmpNode)
  4354. },
  4355. /**
  4356. * 把节点src的所有子节点追加到另一个节点tag上去
  4357. * @method moveChild
  4358. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4359. * @param { Node } tag 目标节点, 从源节点移除的子节点将被追加到该节点下
  4360. * @example
  4361. * ```html
  4362. * <div id="test1">
  4363. * <span></span>
  4364. * </div>
  4365. * <div id="test2">
  4366. * <div></div>
  4367. * </div>
  4368. *
  4369. * <script>
  4370. *
  4371. * var test1 = document.getElementById("test1"),
  4372. * test2 = document.getElementById("test2");
  4373. *
  4374. * UE.dom.domUtils.moveChild( test1, test2 );
  4375. *
  4376. * //output: ""(空字符串)
  4377. * console.log( test1.innerHTML );
  4378. *
  4379. * //output: "<div></div><span></span>"
  4380. * console.log( test2.innerHTML );
  4381. *
  4382. * </script>
  4383. * ```
  4384. */
  4385. /**
  4386. * 把节点src的所有子节点移动到另一个节点tag上去, 可以通过dir参数控制附加的行为是“追加”还是“插入顶部”
  4387. * @method moveChild
  4388. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4389. * @param { Node } tag 目标节点, 从源节点移除的子节点将被附加到该节点下
  4390. * @param { Boolean } dir 附加方式, 如果为true, 则附加进去的节点将被放到目标节点的顶部, 反之,则放到末尾
  4391. * @example
  4392. * ```html
  4393. * <div id="test1">
  4394. * <span></span>
  4395. * </div>
  4396. * <div id="test2">
  4397. * <div></div>
  4398. * </div>
  4399. *
  4400. * <script>
  4401. *
  4402. * var test1 = document.getElementById("test1"),
  4403. * test2 = document.getElementById("test2");
  4404. *
  4405. * UE.dom.domUtils.moveChild( test1, test2, true );
  4406. *
  4407. * //output: ""(空字符串)
  4408. * console.log( test1.innerHTML );
  4409. *
  4410. * //output: "<span></span><div></div>"
  4411. * console.log( test2.innerHTML );
  4412. *
  4413. * </script>
  4414. * ```
  4415. */
  4416. moveChild: function (src, tag, dir) {
  4417. while (src.firstChild) {
  4418. if (dir && tag.firstChild) {
  4419. tag.insertBefore(src.lastChild, tag.firstChild)
  4420. } else {
  4421. tag.appendChild(src.firstChild)
  4422. }
  4423. }
  4424. },
  4425. /**
  4426. * 判断节点的标签上是否不存在任何属性
  4427. * @method hasNoAttributes
  4428. * @private
  4429. * @param { Node } node 需要检测的节点对象
  4430. * @return { Boolean } 节点是否不包含任何属性
  4431. * @example
  4432. * ```html
  4433. * <div id="test"><span>xxxx</span></div>
  4434. *
  4435. * <script>
  4436. *
  4437. * //output: false
  4438. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test") ) );
  4439. *
  4440. * //output: true
  4441. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test").firstChild ) );
  4442. *
  4443. * </script>
  4444. * ```
  4445. */
  4446. hasNoAttributes: function (node) {
  4447. return browser.ie
  4448. ? /^<\w+\s*?>/.test(node.outerHTML)
  4449. : node.attributes.length == 0
  4450. },
  4451. /**
  4452. * 检测节点是否是UEditor所使用的辅助节点
  4453. * @method isCustomeNode
  4454. * @private
  4455. * @param { Node } node 需要检测的节点
  4456. * @remind 辅助节点是指编辑器要完成工作临时添加的节点, 在输出的时候将会从编辑器内移除, 不会影响最终的结果。
  4457. * @return { Boolean } 给定的节点是否是一个辅助节点
  4458. */
  4459. isCustomeNode: function (node) {
  4460. return node.nodeType == 1 && node.getAttribute('_ue_custom_node_')
  4461. },
  4462. /**
  4463. * 检测节点的标签是否是给定的标签
  4464. * @method isTagNode
  4465. * @param { Node } node 需要检测的节点对象
  4466. * @param { String } tagName 标签
  4467. * @return { Boolean } 节点的标签是否是给定的标签
  4468. * @example
  4469. * ```html
  4470. * <div id="test"></div>
  4471. *
  4472. * <script>
  4473. *
  4474. * //output: true
  4475. * console.log( UE.dom.domUtils.isTagNode( document.getElementById("test"), "div" ) );
  4476. *
  4477. * </script>
  4478. * ```
  4479. */
  4480. isTagNode: function (node, tagNames) {
  4481. return (
  4482. node.nodeType == 1 &&
  4483. new RegExp('\\b' + node.tagName + '\\b', 'i').test(tagNames)
  4484. )
  4485. },
  4486. /**
  4487. * 给定一个节点数组,在通过指定的过滤器过滤后, 获取其中满足过滤条件的第一个节点
  4488. * @method filterNodeList
  4489. * @param { Array } nodeList 需要过滤的节点数组
  4490. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4491. * @return { Node | NULL } 如果找到符合过滤条件的节点, 则返回该节点, 否则返回NULL
  4492. * @example
  4493. * ```javascript
  4494. * var divNodes = document.getElementsByTagName("div");
  4495. * divNodes = [].slice.call( divNodes, 0 );
  4496. *
  4497. * //output: null
  4498. * console.log( UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4499. * return node.tagName.toLowerCase() !== 'div';
  4500. * } ) );
  4501. * ```
  4502. */
  4503. /**
  4504. * 给定一个节点数组nodeList和一组标签名tagNames, 获取其中能够匹配标签名的节点集合中的第一个节点
  4505. * @method filterNodeList
  4506. * @param { Array } nodeList 需要过滤的节点数组
  4507. * @param { String } tagNames 需要匹配的标签名, 多个标签名之间用空格分割
  4508. * @return { Node | NULL } 如果找到标签名匹配的节点, 则返回该节点, 否则返回NULL
  4509. * @example
  4510. * ```javascript
  4511. * var divNodes = document.getElementsByTagName("div");
  4512. * divNodes = [].slice.call( divNodes, 0 );
  4513. *
  4514. * //output: null
  4515. * console.log( UE.dom.domUtils.filterNodeList( divNodes, 'a span' ) );
  4516. * ```
  4517. */
  4518. /**
  4519. * 给定一个节点数组,在通过指定的过滤器过滤后, 如果参数forAll为true, 则会返回所有满足过滤
  4520. * 条件的节点集合, 否则, 返回满足条件的节点集合中的第一个节点
  4521. * @method filterNodeList
  4522. * @param { Array } nodeList 需要过滤的节点数组
  4523. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4524. * @param { Boolean } forAll 是否返回整个节点数组, 如果该参数为false, 则返回节点集合中的第一个节点
  4525. * @return { Array | Node | NULL } 如果找到符合过滤条件的节点, 则根据参数forAll的值决定返回满足
  4526. * 过滤条件的节点数组或第一个节点, 否则返回NULL
  4527. * @example
  4528. * ```javascript
  4529. * var divNodes = document.getElementsByTagName("div");
  4530. * divNodes = [].slice.call( divNodes, 0 );
  4531. *
  4532. * //output: 3(假定有3个div)
  4533. * console.log( divNodes.length );
  4534. *
  4535. * var nodes = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4536. * return node.tagName.toLowerCase() === 'div';
  4537. * }, true );
  4538. *
  4539. * //output: 3
  4540. * console.log( nodes.length );
  4541. *
  4542. * var node = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4543. * return node.tagName.toLowerCase() === 'div';
  4544. * }, false );
  4545. *
  4546. * //output: div
  4547. * console.log( node.nodeName );
  4548. * ```
  4549. */
  4550. filterNodeList: function (nodelist, filter, forAll) {
  4551. var results = []
  4552. if (!utils.isFunction(filter)) {
  4553. var str = filter
  4554. filter = function (n) {
  4555. return (
  4556. utils.indexOf(
  4557. utils.isArray(str) ? str : str.split(' '),
  4558. n.tagName.toLowerCase()
  4559. ) != -1
  4560. )
  4561. }
  4562. }
  4563. utils.each(nodelist, function (n) {
  4564. filter(n) && results.push(n)
  4565. })
  4566. return results.length == 0
  4567. ? null
  4568. : results.length == 1 || !forAll
  4569. ? results[0]
  4570. : results
  4571. },
  4572. /**
  4573. * 查询给定的range选区是否在给定的node节点内,且在该节点的最末尾
  4574. * @method isInNodeEndBoundary
  4575. * @param { UE.dom.Range } rng 需要判断的range对象, 该对象的startContainer不能为NULL
  4576. * @param node 需要检测的节点对象
  4577. * @return { Number } 如果给定的选取range对象是在node内部的最末端, 则返回1, 否则返回0
  4578. */
  4579. isInNodeEndBoundary: function (rng, node) {
  4580. var start = rng.startContainer
  4581. if (start.nodeType == 3 && rng.startOffset != start.nodeValue.length) {
  4582. return 0
  4583. }
  4584. if (start.nodeType == 1 && rng.startOffset != start.childNodes.length) {
  4585. return 0
  4586. }
  4587. while (start !== node) {
  4588. if (start.nextSibling) {
  4589. return 0
  4590. }
  4591. start = start.parentNode
  4592. }
  4593. return 1
  4594. },
  4595. isBoundaryNode: function (node, dir) {
  4596. var tmp
  4597. while (!domUtils.isBody(node)) {
  4598. tmp = node
  4599. node = node.parentNode
  4600. if (tmp !== node[dir]) {
  4601. return false
  4602. }
  4603. }
  4604. return true
  4605. },
  4606. fillHtml: browser.ie11below ? '&nbsp;' : '<br/>',
  4607. })
  4608. var fillCharReg = new RegExp(domUtils.fillChar, 'g')
  4609. // core/Range.js
  4610. /**
  4611. * Range封装
  4612. * @file
  4613. * @module UE.dom
  4614. * @class Range
  4615. * @since 1.2.6.1
  4616. */
  4617. /**
  4618. * dom操作封装
  4619. * @unfile
  4620. * @module UE.dom
  4621. */
  4622. /**
  4623. * Range实现类,本类是UEditor底层核心类,封装不同浏览器之间的Range操作。
  4624. * @unfile
  4625. * @module UE.dom
  4626. * @class Range
  4627. */
  4628. ;(function () {
  4629. var guid = 0,
  4630. fillChar = domUtils.fillChar,
  4631. fillData
  4632. /**
  4633. * 更新range的collapse状态
  4634. * @param {Range} range range对象
  4635. */
  4636. function updateCollapse(range) {
  4637. range.collapsed =
  4638. range.startContainer &&
  4639. range.endContainer &&
  4640. range.startContainer === range.endContainer &&
  4641. range.startOffset == range.endOffset
  4642. }
  4643. function selectOneNode(rng) {
  4644. return (
  4645. !rng.collapsed &&
  4646. rng.startContainer.nodeType == 1 &&
  4647. rng.startContainer === rng.endContainer &&
  4648. rng.endOffset - rng.startOffset == 1
  4649. )
  4650. }
  4651. function setEndPoint(toStart, node, offset, range) {
  4652. //如果node是自闭合标签要处理
  4653. if (
  4654. node.nodeType == 1 &&
  4655. (dtd.$empty[node.tagName] || dtd.$nonChild[node.tagName])
  4656. ) {
  4657. offset = domUtils.getNodeIndex(node) + (toStart ? 0 : 1)
  4658. node = node.parentNode
  4659. }
  4660. if (toStart) {
  4661. range.startContainer = node
  4662. range.startOffset = offset
  4663. if (!range.endContainer) {
  4664. range.collapse(true)
  4665. }
  4666. } else {
  4667. range.endContainer = node
  4668. range.endOffset = offset
  4669. if (!range.startContainer) {
  4670. range.collapse(false)
  4671. }
  4672. }
  4673. updateCollapse(range)
  4674. return range
  4675. }
  4676. function execContentsAction(range, action) {
  4677. //调整边界
  4678. //range.includeBookmark();
  4679. var start = range.startContainer,
  4680. end = range.endContainer,
  4681. startOffset = range.startOffset,
  4682. endOffset = range.endOffset,
  4683. doc = range.document,
  4684. frag = doc.createDocumentFragment(),
  4685. tmpStart,
  4686. tmpEnd
  4687. if (start.nodeType == 1) {
  4688. start =
  4689. start.childNodes[startOffset] ||
  4690. (tmpStart = start.appendChild(doc.createTextNode('')))
  4691. }
  4692. if (end.nodeType == 1) {
  4693. end =
  4694. end.childNodes[endOffset] ||
  4695. (tmpEnd = end.appendChild(doc.createTextNode('')))
  4696. }
  4697. if (start === end && start.nodeType == 3) {
  4698. frag.appendChild(
  4699. doc.createTextNode(
  4700. start.substringData(startOffset, endOffset - startOffset)
  4701. )
  4702. )
  4703. //is not clone
  4704. if (action) {
  4705. start.deleteData(startOffset, endOffset - startOffset)
  4706. range.collapse(true)
  4707. }
  4708. return frag
  4709. }
  4710. var current,
  4711. currentLevel,
  4712. clone = frag,
  4713. startParents = domUtils.findParents(start, true),
  4714. endParents = domUtils.findParents(end, true)
  4715. for (var i = 0; startParents[i] == endParents[i]; ) {
  4716. i++
  4717. }
  4718. for (var j = i, si; (si = startParents[j]); j++) {
  4719. current = si.nextSibling
  4720. if (si == start) {
  4721. if (!tmpStart) {
  4722. if (range.startContainer.nodeType == 3) {
  4723. clone.appendChild(
  4724. doc.createTextNode(start.nodeValue.slice(startOffset))
  4725. )
  4726. //is not clone
  4727. if (action) {
  4728. start.deleteData(
  4729. startOffset,
  4730. start.nodeValue.length - startOffset
  4731. )
  4732. }
  4733. } else {
  4734. clone.appendChild(!action ? start.cloneNode(true) : start)
  4735. }
  4736. }
  4737. } else {
  4738. currentLevel = si.cloneNode(false)
  4739. clone.appendChild(currentLevel)
  4740. }
  4741. while (current) {
  4742. if (current === end || current === endParents[j]) {
  4743. break
  4744. }
  4745. si = current.nextSibling
  4746. clone.appendChild(!action ? current.cloneNode(true) : current)
  4747. current = si
  4748. }
  4749. clone = currentLevel
  4750. }
  4751. clone = frag
  4752. if (!startParents[i]) {
  4753. clone.appendChild(startParents[i - 1].cloneNode(false))
  4754. clone = clone.firstChild
  4755. }
  4756. for (var j = i, ei; (ei = endParents[j]); j++) {
  4757. current = ei.previousSibling
  4758. if (ei == end) {
  4759. if (!tmpEnd && range.endContainer.nodeType == 3) {
  4760. clone.appendChild(
  4761. doc.createTextNode(end.substringData(0, endOffset))
  4762. )
  4763. //is not clone
  4764. if (action) {
  4765. end.deleteData(0, endOffset)
  4766. }
  4767. }
  4768. } else {
  4769. currentLevel = ei.cloneNode(false)
  4770. clone.appendChild(currentLevel)
  4771. }
  4772. //如果两端同级,右边第一次已经被开始做了
  4773. if (j != i || !startParents[i]) {
  4774. while (current) {
  4775. if (current === start) {
  4776. break
  4777. }
  4778. ei = current.previousSibling
  4779. clone.insertBefore(
  4780. !action ? current.cloneNode(true) : current,
  4781. clone.firstChild
  4782. )
  4783. current = ei
  4784. }
  4785. }
  4786. clone = currentLevel
  4787. }
  4788. if (action) {
  4789. range
  4790. .setStartBefore(
  4791. !endParents[i]
  4792. ? endParents[i - 1]
  4793. : !startParents[i]
  4794. ? startParents[i - 1]
  4795. : endParents[i]
  4796. )
  4797. .collapse(true)
  4798. }
  4799. tmpStart && domUtils.remove(tmpStart)
  4800. tmpEnd && domUtils.remove(tmpEnd)
  4801. return frag
  4802. }
  4803. /**
  4804. * 创建一个跟document绑定的空的Range实例
  4805. * @constructor
  4806. * @param { Document } document 新建的选区所属的文档对象
  4807. */
  4808. /**
  4809. * @property { Node } startContainer 当前Range的开始边界的容器节点, 可以是一个元素节点或者是文本节点
  4810. */
  4811. /**
  4812. * @property { Node } startOffset 当前Range的开始边界容器节点的偏移量, 如果是元素节点,
  4813. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4814. */
  4815. /**
  4816. * @property { Node } endContainer 当前Range的结束边界的容器节点, 可以是一个元素节点或者是文本节点
  4817. */
  4818. /**
  4819. * @property { Node } endOffset 当前Range的结束边界容器节点的偏移量, 如果是元素节点,
  4820. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4821. */
  4822. /**
  4823. * @property { Boolean } collapsed 当前Range是否闭合
  4824. * @default true
  4825. * @remind Range是闭合的时候, startContainer === endContainer && startOffset === endOffset
  4826. */
  4827. /**
  4828. * @property { Document } document 当前Range所属的Document对象
  4829. * @remind 不同range的的document属性可以是不同的
  4830. */
  4831. var Range = (dom.Range = function (document) {
  4832. var me = this
  4833. me.startContainer = me.startOffset = me.endContainer = me.endOffset = null
  4834. me.document = document
  4835. me.collapsed = true
  4836. })
  4837. /**
  4838. * 删除fillData
  4839. * @param doc
  4840. * @param excludeNode
  4841. */
  4842. function removeFillData(doc, excludeNode) {
  4843. try {
  4844. if (fillData && domUtils.inDoc(fillData, doc)) {
  4845. if (!fillData.nodeValue.replace(fillCharReg, '').length) {
  4846. var tmpNode = fillData.parentNode
  4847. domUtils.remove(fillData)
  4848. while (
  4849. tmpNode &&
  4850. domUtils.isEmptyInlineElement(tmpNode) &&
  4851. //safari的contains有bug
  4852. (browser.safari
  4853. ? !(
  4854. domUtils.getPosition(tmpNode, excludeNode) &
  4855. domUtils.POSITION_CONTAINS
  4856. )
  4857. : !tmpNode.contains(excludeNode))
  4858. ) {
  4859. fillData = tmpNode.parentNode
  4860. domUtils.remove(tmpNode)
  4861. tmpNode = fillData
  4862. }
  4863. } else {
  4864. fillData.nodeValue = fillData.nodeValue.replace(fillCharReg, '')
  4865. }
  4866. }
  4867. } catch (e) {}
  4868. }
  4869. /**
  4870. * @param node
  4871. * @param dir
  4872. */
  4873. function mergeSibling(node, dir) {
  4874. var tmpNode
  4875. node = node[dir]
  4876. while (node && domUtils.isFillChar(node)) {
  4877. tmpNode = node[dir]
  4878. domUtils.remove(node)
  4879. node = tmpNode
  4880. }
  4881. }
  4882. Range.prototype = {
  4883. /**
  4884. * 克隆选区的内容到一个DocumentFragment里
  4885. * @method cloneContents
  4886. * @return { DocumentFragment | NULL } 如果选区是闭合的将返回null, 否则, 返回包含所clone内容的DocumentFragment元素
  4887. * @example
  4888. * ```html
  4889. * <body>
  4890. * <!-- 中括号表示选区 -->
  4891. * <b>x<i>x[x</i>xx]x</b>
  4892. *
  4893. * <script>
  4894. * //range是已选中的选区
  4895. * var fragment = range.cloneContents(),
  4896. * node = document.createElement("div");
  4897. *
  4898. * node.appendChild( fragment );
  4899. *
  4900. * //output: <i>x</i>xx
  4901. * console.log( node.innerHTML );
  4902. *
  4903. * </script>
  4904. * </body>
  4905. * ```
  4906. */
  4907. cloneContents: function () {
  4908. return this.collapsed ? null : execContentsAction(this, 0)
  4909. },
  4910. /**
  4911. * 删除当前选区范围中的所有内容
  4912. * @method deleteContents
  4913. * @remind 执行完该操作后, 当前Range对象变成了闭合状态
  4914. * @return { UE.dom.Range } 当前操作的Range对象
  4915. * @example
  4916. * ```html
  4917. * <body>
  4918. * <!-- 中括号表示选区 -->
  4919. * <b>x<i>x[x</i>xx]x</b>
  4920. *
  4921. * <script>
  4922. * //range是已选中的选区
  4923. * range.deleteContents();
  4924. *
  4925. * //竖线表示闭合后的选区位置
  4926. * //output: <b>x<i>x</i>|x</b>
  4927. * console.log( document.body.innerHTML );
  4928. *
  4929. * //此时, range的各项属性为
  4930. * //output: B
  4931. * console.log( range.startContainer.tagName );
  4932. * //output: 2
  4933. * console.log( range.startOffset );
  4934. * //output: B
  4935. * console.log( range.endContainer.tagName );
  4936. * //output: 2
  4937. * console.log( range.endOffset );
  4938. * //output: true
  4939. * console.log( range.collapsed );
  4940. *
  4941. * </script>
  4942. * </body>
  4943. * ```
  4944. */
  4945. deleteContents: function () {
  4946. var txt
  4947. if (!this.collapsed) {
  4948. execContentsAction(this, 1)
  4949. }
  4950. if (browser.webkit) {
  4951. txt = this.startContainer
  4952. if (txt.nodeType == 3 && !txt.nodeValue.length) {
  4953. this.setStartBefore(txt).collapse(true)
  4954. domUtils.remove(txt)
  4955. }
  4956. }
  4957. return this
  4958. },
  4959. /**
  4960. * 将当前选区的内容提取到一个DocumentFragment里
  4961. * @method extractContents
  4962. * @remind 执行该操作后, 选区将变成闭合状态
  4963. * @warning 执行该操作后, 原来选区所选中的内容将从dom树上剥离出来
  4964. * @return { DocumentFragment } 返回包含所提取内容的DocumentFragment对象
  4965. * @example
  4966. * ```html
  4967. * <body>
  4968. * <!-- 中括号表示选区 -->
  4969. * <b>x<i>x[x</i>xx]x</b>
  4970. *
  4971. * <script>
  4972. * //range是已选中的选区
  4973. * var fragment = range.extractContents(),
  4974. * node = document.createElement( "div" );
  4975. *
  4976. * node.appendChild( fragment );
  4977. *
  4978. * //竖线表示闭合后的选区位置
  4979. *
  4980. * //output: <b>x<i>x</i>|x</b>
  4981. * console.log( document.body.innerHTML );
  4982. * //output: <i>x</i>xx
  4983. * console.log( node.innerHTML );
  4984. *
  4985. * //此时, range的各项属性为
  4986. * //output: B
  4987. * console.log( range.startContainer.tagName );
  4988. * //output: 2
  4989. * console.log( range.startOffset );
  4990. * //output: B
  4991. * console.log( range.endContainer.tagName );
  4992. * //output: 2
  4993. * console.log( range.endOffset );
  4994. * //output: true
  4995. * console.log( range.collapsed );
  4996. *
  4997. * </script>
  4998. * </body>
  4999. */
  5000. extractContents: function () {
  5001. return this.collapsed ? null : execContentsAction(this, 2)
  5002. },
  5003. /**
  5004. * 设置Range的开始容器节点和偏移量
  5005. * @method setStart
  5006. * @remind 如果给定的节点是元素节点,那么offset指的是其子元素中索引为offset的元素,
  5007. * 如果是文本节点,那么offset指的是其文本内容的第offset个字符
  5008. * @remind 如果提供的容器节点是一个不能包含子元素的节点, 则该选区的开始容器将被设置
  5009. * 为该节点的父节点, 此时, 其距离开始容器的偏移量也变成了该节点在其父节点
  5010. * 中的索引
  5011. * @param { Node } node 将被设为当前选区开始边界容器的节点对象
  5012. * @param { int } offset 选区的开始位置偏移量
  5013. * @return { UE.dom.Range } 当前range对象
  5014. * @example
  5015. * ```html
  5016. * <!-- 选区 -->
  5017. * <b>xxx<i>x<span>xx</span>xx<em>xx</em>xxx</i>[xxx]</b>
  5018. *
  5019. * <script>
  5020. *
  5021. * //执行操作
  5022. * range.setStart( document.getElementsByTagName("i")[0], 1 );
  5023. *
  5024. * //此时, 选区变成了
  5025. * //<b>xxx<i>x[<span>xx</span>xx<em>xx</em>xxx</i>xxx]</b>
  5026. *
  5027. * </script>
  5028. * ```
  5029. * @example
  5030. * ```html
  5031. * <!-- 选区 -->
  5032. * <b>xxx<img>[xx]x</b>
  5033. *
  5034. * <script>
  5035. *
  5036. * //执行操作
  5037. * range.setStart( document.getElementsByTagName("img")[0], 3 );
  5038. *
  5039. * //此时, 选区变成了
  5040. * //<b>xxx[<img>xx]x</b>
  5041. *
  5042. * </script>
  5043. * ```
  5044. */
  5045. setStart: function (node, offset) {
  5046. return setEndPoint(true, node, offset, this)
  5047. },
  5048. /**
  5049. * 设置Range的结束容器和偏移量
  5050. * @method setEnd
  5051. * @param { Node } node 作为当前选区结束边界容器的节点对象
  5052. * @param { int } offset 结束边界的偏移量
  5053. * @see UE.dom.Range:setStart(Node,int)
  5054. * @return { UE.dom.Range } 当前range对象
  5055. */
  5056. setEnd: function (node, offset) {
  5057. return setEndPoint(false, node, offset, this)
  5058. },
  5059. /**
  5060. * 将Range开始位置设置到node节点之后
  5061. * @method setStartAfter
  5062. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引+1
  5063. * @param { Node } node 选区的开始边界将紧接着该节点之后
  5064. * @return { UE.dom.Range } 当前range对象
  5065. * @example
  5066. * ```html
  5067. * <!-- 选区示例 -->
  5068. * <b>xx<i>xxx</i><span>xx[x</span>xxx]</b>
  5069. *
  5070. * <script>
  5071. *
  5072. * //执行操作
  5073. * range.setStartAfter( document.getElementsByTagName("i")[0] );
  5074. *
  5075. * //结果选区
  5076. * //<b>xx<i>xxx</i>[<span>xxx</span>xxx]</b>
  5077. *
  5078. * </script>
  5079. * ```
  5080. */
  5081. setStartAfter: function (node) {
  5082. return this.setStart(node.parentNode, domUtils.getNodeIndex(node) + 1)
  5083. },
  5084. /**
  5085. * 将Range开始位置设置到node节点之前
  5086. * @method setStartBefore
  5087. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引
  5088. * @param { Node } node 新的选区开始位置在该节点之前
  5089. * @see UE.dom.Range:setStartAfter(Node)
  5090. * @return { UE.dom.Range } 当前range对象
  5091. */
  5092. setStartBefore: function (node) {
  5093. return this.setStart(node.parentNode, domUtils.getNodeIndex(node))
  5094. },
  5095. /**
  5096. * 将Range结束位置设置到node节点之后
  5097. * @method setEndAfter
  5098. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引+1
  5099. * @param { Node } node 目标节点
  5100. * @see UE.dom.Range:setStartAfter(Node)
  5101. * @return { UE.dom.Range } 当前range对象
  5102. * @example
  5103. * ```html
  5104. * <!-- 选区示例 -->
  5105. * <b>[xx<i>xxx</i><span>xx]x</span>xxx</b>
  5106. *
  5107. * <script>
  5108. *
  5109. * //执行操作
  5110. * range.setStartAfter( document.getElementsByTagName("span")[0] );
  5111. *
  5112. * //结果选区
  5113. * //<b>[xx<i>xxx</i><span>xxx</span>]xxx</b>
  5114. *
  5115. * </script>
  5116. * ```
  5117. */
  5118. setEndAfter: function (node) {
  5119. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node) + 1)
  5120. },
  5121. /**
  5122. * 将Range结束位置设置到node节点之前
  5123. * @method setEndBefore
  5124. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引
  5125. * @param { Node } node 目标节点
  5126. * @see UE.dom.Range:setEndAfter(Node)
  5127. * @return { UE.dom.Range } 当前range对象
  5128. */
  5129. setEndBefore: function (node) {
  5130. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node))
  5131. },
  5132. /**
  5133. * 设置Range的开始位置到node节点内的第一个子节点之前
  5134. * @method setStartAtFirst
  5135. * @remind 选区的开始容器将变成给定的节点, 且偏移量为0
  5136. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5137. * @param { Node } node 目标节点
  5138. * @see UE.dom.Range:setStartBefore(Node)
  5139. * @return { UE.dom.Range } 当前range对象
  5140. * @example
  5141. * ```html
  5142. * <!-- 选区示例 -->
  5143. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5144. *
  5145. * <script>
  5146. *
  5147. * //执行操作
  5148. * range.setStartAtFirst( document.getElementsByTagName("i")[0] );
  5149. *
  5150. * //结果选区
  5151. * //<b>xx<i>[xxx</i><span>xx]x</span>xxx</b>
  5152. *
  5153. * </script>
  5154. * ```
  5155. */
  5156. setStartAtFirst: function (node) {
  5157. return this.setStart(node, 0)
  5158. },
  5159. /**
  5160. * 设置Range的开始位置到node节点内的最后一个节点之后
  5161. * @method setStartAtLast
  5162. * @remind 选区的开始容器将变成给定的节点, 且偏移量为该节点的子节点数
  5163. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5164. * @param { Node } node 目标节点
  5165. * @see UE.dom.Range:setStartAtFirst(Node)
  5166. * @return { UE.dom.Range } 当前range对象
  5167. */
  5168. setStartAtLast: function (node) {
  5169. return this.setStart(
  5170. node,
  5171. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5172. )
  5173. },
  5174. /**
  5175. * 设置Range的结束位置到node节点内的第一个节点之前
  5176. * @method setEndAtFirst
  5177. * @param { Node } node 目标节点
  5178. * @remind 选区的结束容器将变成给定的节点, 且偏移量为0
  5179. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5180. * @see UE.dom.Range:setStartAtFirst(Node)
  5181. * @return { UE.dom.Range } 当前range对象
  5182. */
  5183. setEndAtFirst: function (node) {
  5184. return this.setEnd(node, 0)
  5185. },
  5186. /**
  5187. * 设置Range的结束位置到node节点内的最后一个节点之后
  5188. * @method setEndAtLast
  5189. * @param { Node } node 目标节点
  5190. * @remind 选区的结束容器将变成给定的节点, 且偏移量为该节点的子节点数量
  5191. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5192. * @see UE.dom.Range:setStartAtFirst(Node)
  5193. * @return { UE.dom.Range } 当前range对象
  5194. */
  5195. setEndAtLast: function (node) {
  5196. return this.setEnd(
  5197. node,
  5198. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5199. )
  5200. },
  5201. /**
  5202. * 选中给定节点
  5203. * @method selectNode
  5204. * @remind 此时, 选区的开始容器和结束容器都是该节点的父节点, 其startOffset是该节点在父节点中的位置索引,
  5205. * 而endOffset为startOffset+1
  5206. * @param { Node } node 需要选中的节点
  5207. * @return { UE.dom.Range } 当前range对象,此时的range仅包含当前给定的节点对象
  5208. * @example
  5209. * ```html
  5210. * <!-- 选区示例 -->
  5211. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5212. *
  5213. * <script>
  5214. *
  5215. * //执行操作
  5216. * range.selectNode( document.getElementsByTagName("i")[0] );
  5217. *
  5218. * //结果选区
  5219. * //<b>xx[<i>xxx</i>]<span>xxx</span>xxx</b>
  5220. *
  5221. * </script>
  5222. * ```
  5223. */
  5224. selectNode: function (node) {
  5225. return this.setStartBefore(node).setEndAfter(node)
  5226. },
  5227. /**
  5228. * 选中给定节点内部的所有节点
  5229. * @method selectNodeContents
  5230. * @remind 此时, 选区的开始容器和结束容器都是该节点, 其startOffset为0,
  5231. * 而endOffset是该节点的子节点数。
  5232. * @param { Node } node 目标节点, 当前range将包含该节点内的所有节点
  5233. * @return { UE.dom.Range } 当前range对象, 此时range仅包含给定节点的所有子节点
  5234. * @example
  5235. * ```html
  5236. * <!-- 选区示例 -->
  5237. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5238. *
  5239. * <script>
  5240. *
  5241. * //执行操作
  5242. * range.selectNode( document.getElementsByTagName("b")[0] );
  5243. *
  5244. * //结果选区
  5245. * //<b>[xx<i>xxx</i><span>xxx</span>xxx]</b>
  5246. *
  5247. * </script>
  5248. * ```
  5249. */
  5250. selectNodeContents: function (node) {
  5251. return this.setStart(node, 0).setEndAtLast(node)
  5252. },
  5253. /**
  5254. * clone当前Range对象
  5255. * @method cloneRange
  5256. * @remind 返回的range是一个全新的range对象, 其内部所有属性与当前被clone的range相同。
  5257. * @return { UE.dom.Range } 当前range对象的一个副本
  5258. */
  5259. cloneRange: function () {
  5260. var me = this
  5261. return new Range(me.document)
  5262. .setStart(me.startContainer, me.startOffset)
  5263. .setEnd(me.endContainer, me.endOffset)
  5264. },
  5265. /**
  5266. * 向当前选区的结束处闭合选区
  5267. * @method collapse
  5268. * @return { UE.dom.Range } 当前range对象
  5269. * @example
  5270. * ```html
  5271. * <!-- 选区示例 -->
  5272. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5273. *
  5274. * <script>
  5275. *
  5276. * //执行操作
  5277. * range.collapse();
  5278. *
  5279. * //结果选区
  5280. * //“|”表示选区已闭合
  5281. * //<b>xx<i>xxx</i><span>xx|x</span>xxx</b>
  5282. *
  5283. * </script>
  5284. * ```
  5285. */
  5286. /**
  5287. * 闭合当前选区,根据给定的toStart参数项决定是向当前选区开始处闭合还是向结束处闭合,
  5288. * 如果toStart的值为true,则向开始位置闭合, 反之,向结束位置闭合。
  5289. * @method collapse
  5290. * @param { Boolean } toStart 是否向选区开始处闭合
  5291. * @return { UE.dom.Range } 当前range对象,此时range对象处于闭合状态
  5292. * @see UE.dom.Range:collapse()
  5293. * @example
  5294. * ```html
  5295. * <!-- 选区示例 -->
  5296. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5297. *
  5298. * <script>
  5299. *
  5300. * //执行操作
  5301. * range.collapse( true );
  5302. *
  5303. * //结果选区
  5304. * //“|”表示选区已闭合
  5305. * //<b>xx<i>xxx</i><span>|xxx</span>xxx</b>
  5306. *
  5307. * </script>
  5308. * ```
  5309. */
  5310. collapse: function (toStart) {
  5311. var me = this
  5312. if (toStart) {
  5313. me.endContainer = me.startContainer
  5314. me.endOffset = me.startOffset
  5315. } else {
  5316. me.startContainer = me.endContainer
  5317. me.startOffset = me.endOffset
  5318. }
  5319. me.collapsed = true
  5320. return me
  5321. },
  5322. /**
  5323. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置
  5324. * @method shrinkBoundary
  5325. * @return { UE.dom.Range } 当前range对象
  5326. * @example
  5327. * ```html
  5328. * <span>xx<b>xx[</b>xxxxx]</span> => <span>xx<b>xx</b>[xxxxx]</span>
  5329. * ```
  5330. *
  5331. * @example
  5332. * ```html
  5333. * <!-- 选区示例 -->
  5334. * <b>x[xx</b><i>]xxx</i>
  5335. *
  5336. * <script>
  5337. *
  5338. * //执行收缩
  5339. * range.shrinkBoundary();
  5340. *
  5341. * //结果选区
  5342. * //<b>x[xx]</b><i>xxx</i>
  5343. * </script>
  5344. * ```
  5345. *
  5346. * @example
  5347. * ```html
  5348. * [<b><i>xxxx</i>xxxxxxx</b>] => <b><i>[xxxx</i>xxxxxxx]</b>
  5349. * ```
  5350. */
  5351. /**
  5352. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置,
  5353. * 如果ignoreEnd的值为true,则忽略对结束位置的调整
  5354. * @method shrinkBoundary
  5355. * @param { Boolean } ignoreEnd 是否忽略对结束位置的调整
  5356. * @return { UE.dom.Range } 当前range对象
  5357. * @see UE.dom.domUtils.Range:shrinkBoundary()
  5358. */
  5359. shrinkBoundary: function (ignoreEnd) {
  5360. var me = this,
  5361. child,
  5362. collapsed = me.collapsed
  5363. function check(node) {
  5364. return (
  5365. node.nodeType == 1 &&
  5366. !domUtils.isBookmarkNode(node) &&
  5367. !dtd.$empty[node.tagName] &&
  5368. !dtd.$nonChild[node.tagName]
  5369. )
  5370. }
  5371. while (
  5372. me.startContainer.nodeType == 1 && //是element
  5373. (child = me.startContainer.childNodes[me.startOffset]) && //子节点也是element
  5374. check(child)
  5375. ) {
  5376. me.setStart(child, 0)
  5377. }
  5378. if (collapsed) {
  5379. return me.collapse(true)
  5380. }
  5381. if (!ignoreEnd) {
  5382. while (
  5383. me.endContainer.nodeType == 1 && //是element
  5384. me.endOffset > 0 && //如果是空元素就退出 endOffset=0那么endOffst-1为负值,childNodes[endOffset]报错
  5385. (child = me.endContainer.childNodes[me.endOffset - 1]) && //子节点也是element
  5386. check(child)
  5387. ) {
  5388. me.setEnd(child, child.childNodes.length)
  5389. }
  5390. }
  5391. return me
  5392. },
  5393. /**
  5394. * 获取离当前选区内包含的所有节点最近的公共祖先节点,
  5395. * @method getCommonAncestor
  5396. * @remind 返回的公共祖先节点一定不是range自身的容器节点, 但有可能是一个文本节点
  5397. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5398. * @example
  5399. * ```html
  5400. * //选区示例
  5401. * <span>xxx<b>x[x<em>xx]x</em>xxx</b>xx</span>
  5402. * <script>
  5403. *
  5404. * var node = range.getCommonAncestor();
  5405. *
  5406. * //公共祖先节点是: b节点
  5407. * //输出: B
  5408. * console.log(node.tagName);
  5409. *
  5410. * </script>
  5411. * ```
  5412. */
  5413. /**
  5414. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5415. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5416. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点
  5417. * @method getCommonAncestor
  5418. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5419. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5420. * @see UE.dom.Range:getCommonAncestor()
  5421. * @example
  5422. * ```html
  5423. * <body>
  5424. *
  5425. * <!-- 选区示例 -->
  5426. * <b>xxx<i>xxxx<span>xx[x</span>xx]x</i>xxxxxxx</b>
  5427. *
  5428. * <script>
  5429. *
  5430. * var node = range.getCommonAncestor( false );
  5431. *
  5432. * //这里的公共祖先节点是B而不是I, 是因为参数限制了获取到的节点不能是容器节点
  5433. * //output: B
  5434. * console.log( node.tagName );
  5435. *
  5436. * </script>
  5437. *
  5438. * </body>
  5439. * ```
  5440. */
  5441. /**
  5442. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5443. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5444. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点; 同时可以根据
  5445. * ignoreTextNode 参数的取值决定是否忽略类型为文本节点的祖先节点。
  5446. * @method getCommonAncestor
  5447. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5448. * @param { Boolean } ignoreTextNode 获取祖先节点的过程中是否忽略类型为文本节点的祖先节点
  5449. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5450. * @see UE.dom.Range:getCommonAncestor()
  5451. * @see UE.dom.Range:getCommonAncestor(Boolean)
  5452. * @example
  5453. * ```html
  5454. * <body>
  5455. *
  5456. * <!-- 选区示例 -->
  5457. * <b>xxx<i>xxxx<span>x[x]x</span>xxx</i>xxxxxxx</b>
  5458. *
  5459. * <script>
  5460. *
  5461. * var node = range.getCommonAncestor( true, false );
  5462. *
  5463. * //output: SPAN
  5464. * console.log( node.tagName );
  5465. *
  5466. * </script>
  5467. *
  5468. * </body>
  5469. * ```
  5470. */
  5471. getCommonAncestor: function (includeSelf, ignoreTextNode) {
  5472. var me = this,
  5473. start = me.startContainer,
  5474. end = me.endContainer
  5475. if (start === end) {
  5476. if (includeSelf && selectOneNode(this)) {
  5477. start = start.childNodes[me.startOffset]
  5478. if (start.nodeType == 1) return start
  5479. }
  5480. //只有在上来就相等的情况下才会出现是文本的情况
  5481. return ignoreTextNode && start.nodeType == 3
  5482. ? start.parentNode
  5483. : start
  5484. }
  5485. return domUtils.getCommonAncestor(start, end)
  5486. },
  5487. /**
  5488. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上
  5489. * @method trimBoundary
  5490. * @remind 该操作有可能会引起文本节点被切开
  5491. * @return { UE.dom.Range } 当前range对象
  5492. * @example
  5493. * ```html
  5494. *
  5495. * //选区示例
  5496. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5497. *
  5498. * <script>
  5499. * //未调整前, 选区的开始容器和结束都是文本节点
  5500. * //执行调整
  5501. * range.trimBoundary();
  5502. *
  5503. * //调整之后, 容器节点变成了i节点
  5504. * //<b>xxx[<i>xxxxx</i>]xxx</b>
  5505. * </script>
  5506. * ```
  5507. */
  5508. /**
  5509. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上,
  5510. * 可以根据 ignoreEnd 参数的值决定是否调整对结束边界的调整
  5511. * @method trimBoundary
  5512. * @param { Boolean } ignoreEnd 是否忽略对结束边界的调整
  5513. * @return { UE.dom.Range } 当前range对象
  5514. * @example
  5515. * ```html
  5516. *
  5517. * //选区示例
  5518. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5519. *
  5520. * <script>
  5521. * //未调整前, 选区的开始容器和结束都是文本节点
  5522. * //执行调整
  5523. * range.trimBoundary( true );
  5524. *
  5525. * //调整之后, 开始容器节点变成了i节点
  5526. * //但是, 结束容器没有发生变化
  5527. * //<b>xxx[<i>xxxxx]</i>xxx</b>
  5528. * </script>
  5529. * ```
  5530. */
  5531. trimBoundary: function (ignoreEnd) {
  5532. this.txtToElmBoundary()
  5533. var start = this.startContainer,
  5534. offset = this.startOffset,
  5535. collapsed = this.collapsed,
  5536. end = this.endContainer
  5537. if (start.nodeType == 3) {
  5538. if (offset == 0) {
  5539. this.setStartBefore(start)
  5540. } else {
  5541. if (offset >= start.nodeValue.length) {
  5542. this.setStartAfter(start)
  5543. } else {
  5544. var textNode = domUtils.split(start, offset)
  5545. //跟新结束边界
  5546. if (start === end) {
  5547. this.setEnd(textNode, this.endOffset - offset)
  5548. } else if (start.parentNode === end) {
  5549. this.endOffset += 1
  5550. }
  5551. this.setStartBefore(textNode)
  5552. }
  5553. }
  5554. if (collapsed) {
  5555. return this.collapse(true)
  5556. }
  5557. }
  5558. if (!ignoreEnd) {
  5559. offset = this.endOffset
  5560. end = this.endContainer
  5561. if (end.nodeType == 3) {
  5562. if (offset == 0) {
  5563. this.setEndBefore(end)
  5564. } else {
  5565. offset < end.nodeValue.length && domUtils.split(end, offset)
  5566. this.setEndAfter(end)
  5567. }
  5568. }
  5569. }
  5570. return this
  5571. },
  5572. /**
  5573. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则什么也不做
  5574. * @method txtToElmBoundary
  5575. * @remind 该操作不会修改dom节点
  5576. * @return { UE.dom.Range } 当前range对象
  5577. */
  5578. /**
  5579. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则根据参数项
  5580. * ignoreCollapsed 的值决定是否执行该调整
  5581. * @method txtToElmBoundary
  5582. * @param { Boolean } ignoreCollapsed 是否忽略选区的闭合状态, 如果该参数取值为true, 则
  5583. * 不论选区是否闭合, 都会执行该操作, 反之, 则不会对闭合的选区执行该操作
  5584. * @return { UE.dom.Range } 当前range对象
  5585. */
  5586. txtToElmBoundary: function (ignoreCollapsed) {
  5587. function adjust(r, c) {
  5588. var container = r[c + 'Container'],
  5589. offset = r[c + 'Offset']
  5590. if (container.nodeType == 3) {
  5591. if (!offset) {
  5592. r[
  5593. 'set' +
  5594. c.replace(/(\w)/, function (a) {
  5595. return a.toUpperCase()
  5596. }) +
  5597. 'Before'
  5598. ](container)
  5599. } else if (offset >= container.nodeValue.length) {
  5600. r[
  5601. 'set' +
  5602. c.replace(/(\w)/, function (a) {
  5603. return a.toUpperCase()
  5604. }) +
  5605. 'After'
  5606. ](container)
  5607. }
  5608. }
  5609. }
  5610. if (ignoreCollapsed || !this.collapsed) {
  5611. adjust(this, 'start')
  5612. adjust(this, 'end')
  5613. }
  5614. return this
  5615. },
  5616. /**
  5617. * 在当前选区的开始位置前插入节点,新插入的节点会被该range包含
  5618. * @method insertNode
  5619. * @param { Node } node 需要插入的节点
  5620. * @remind 插入的节点可以是一个DocumentFragment依次插入多个节点
  5621. * @return { UE.dom.Range } 当前range对象
  5622. */
  5623. insertNode: function (node) {
  5624. var first = node,
  5625. length = 1
  5626. if (node.nodeType == 11) {
  5627. first = node.firstChild
  5628. length = node.childNodes.length
  5629. }
  5630. this.trimBoundary(true)
  5631. var start = this.startContainer,
  5632. offset = this.startOffset
  5633. var nextNode = start.childNodes[offset]
  5634. if (nextNode) {
  5635. start.insertBefore(node, nextNode)
  5636. } else {
  5637. start.appendChild(node)
  5638. }
  5639. if (first.parentNode === this.endContainer) {
  5640. this.endOffset = this.endOffset + length
  5641. }
  5642. return this.setStartBefore(first)
  5643. },
  5644. /**
  5645. * 闭合选区到当前选区的开始位置, 并且定位光标到闭合后的位置
  5646. * @method setCursor
  5647. * @return { UE.dom.Range } 当前range对象
  5648. * @see UE.dom.Range:collapse()
  5649. */
  5650. /**
  5651. * 闭合选区,可以根据参数toEnd的值控制选区是向前闭合还是向后闭合, 并且定位光标到闭合后的位置。
  5652. * @method setCursor
  5653. * @param { Boolean } toEnd 是否向后闭合, 如果为true, 则闭合选区时, 将向结束容器方向闭合,
  5654. * 反之,则向开始容器方向闭合
  5655. * @return { UE.dom.Range } 当前range对象
  5656. * @see UE.dom.Range:collapse(Boolean)
  5657. */
  5658. setCursor: function (toEnd, noFillData) {
  5659. return this.collapse(!toEnd).select(noFillData)
  5660. },
  5661. /**
  5662. * 创建当前range的一个书签,记录下当前range的位置,方便当dom树改变时,还能找回原来的选区位置
  5663. * @method createBookmark
  5664. * @param { Boolean } serialize 控制返回的标记位置是对当前位置的引用还是ID,如果该值为true,则
  5665. * 返回标记位置的ID, 反之则返回标记位置节点的引用
  5666. * @return { Object } 返回一个书签记录键值对, 其包含的key有: start => 开始标记的ID或者引用,
  5667. * end => 结束标记的ID或引用, id => 当前标记的类型, 如果为true,则表示
  5668. * 返回的记录的类型为ID, 反之则为引用
  5669. */
  5670. createBookmark: function (serialize, same) {
  5671. var endNode,
  5672. startNode = this.document.createElement('span')
  5673. startNode.style.cssText = 'display:none;line-height:0px;'
  5674. startNode.appendChild(this.document.createTextNode('\u200D'))
  5675. startNode.id = '_baidu_bookmark_start_' + (same ? '' : guid++)
  5676. if (!this.collapsed) {
  5677. endNode = startNode.cloneNode(true)
  5678. endNode.id = '_baidu_bookmark_end_' + (same ? '' : guid++)
  5679. }
  5680. this.insertNode(startNode)
  5681. if (endNode) {
  5682. this.collapse().insertNode(endNode).setEndBefore(endNode)
  5683. }
  5684. this.setStartAfter(startNode)
  5685. return {
  5686. start: serialize ? startNode.id : startNode,
  5687. end: endNode ? (serialize ? endNode.id : endNode) : null,
  5688. id: serialize,
  5689. }
  5690. },
  5691. /**
  5692. * 调整当前range的边界到书签位置,并删除该书签对象所标记的位置内的节点
  5693. * @method moveToBookmark
  5694. * @param { BookMark } bookmark createBookmark所创建的标签对象
  5695. * @return { UE.dom.Range } 当前range对象
  5696. * @see UE.dom.Range:createBookmark(Boolean)
  5697. */
  5698. moveToBookmark: function (bookmark) {
  5699. var start = bookmark.id
  5700. ? this.document.getElementById(bookmark.start)
  5701. : bookmark.start,
  5702. end =
  5703. bookmark.end && bookmark.id
  5704. ? this.document.getElementById(bookmark.end)
  5705. : bookmark.end
  5706. this.setStartBefore(start)
  5707. domUtils.remove(start)
  5708. if (end) {
  5709. this.setEndBefore(end)
  5710. domUtils.remove(end)
  5711. } else {
  5712. this.collapse(true)
  5713. }
  5714. return this
  5715. },
  5716. /**
  5717. * 调整range的边界,使其"放大"到最近的父节点
  5718. * @method enlarge
  5719. * @remind 会引起选区的变化
  5720. * @return { UE.dom.Range } 当前range对象
  5721. */
  5722. /**
  5723. * 调整range的边界,使其"放大"到最近的父节点,根据参数 toBlock 的取值, 可以
  5724. * 要求扩大之后的父节点是block节点
  5725. * @method enlarge
  5726. * @param { Boolean } toBlock 是否要求扩大之后的父节点必须是block节点
  5727. * @return { UE.dom.Range } 当前range对象
  5728. */
  5729. enlarge: function (toBlock, stopFn) {
  5730. var isBody = domUtils.isBody,
  5731. pre,
  5732. node,
  5733. tmp = this.document.createTextNode('')
  5734. if (toBlock) {
  5735. node = this.startContainer
  5736. if (node.nodeType == 1) {
  5737. if (node.childNodes[this.startOffset]) {
  5738. pre = node = node.childNodes[this.startOffset]
  5739. } else {
  5740. node.appendChild(tmp)
  5741. pre = node = tmp
  5742. }
  5743. } else {
  5744. pre = node
  5745. }
  5746. while (1) {
  5747. if (domUtils.isBlockElm(node)) {
  5748. node = pre
  5749. while (
  5750. (pre = node.previousSibling) &&
  5751. !domUtils.isBlockElm(pre)
  5752. ) {
  5753. node = pre
  5754. }
  5755. this.setStartBefore(node)
  5756. break
  5757. }
  5758. pre = node
  5759. node = node.parentNode
  5760. }
  5761. node = this.endContainer
  5762. if (node.nodeType == 1) {
  5763. if ((pre = node.childNodes[this.endOffset])) {
  5764. node.insertBefore(tmp, pre)
  5765. } else {
  5766. node.appendChild(tmp)
  5767. }
  5768. pre = node = tmp
  5769. } else {
  5770. pre = node
  5771. }
  5772. while (1) {
  5773. if (domUtils.isBlockElm(node)) {
  5774. node = pre
  5775. while ((pre = node.nextSibling) && !domUtils.isBlockElm(pre)) {
  5776. node = pre
  5777. }
  5778. this.setEndAfter(node)
  5779. break
  5780. }
  5781. pre = node
  5782. node = node.parentNode
  5783. }
  5784. if (tmp.parentNode === this.endContainer) {
  5785. this.endOffset--
  5786. }
  5787. domUtils.remove(tmp)
  5788. }
  5789. // 扩展边界到最大
  5790. if (!this.collapsed) {
  5791. while (this.startOffset == 0) {
  5792. if (stopFn && stopFn(this.startContainer)) {
  5793. break
  5794. }
  5795. if (isBody(this.startContainer)) {
  5796. break
  5797. }
  5798. this.setStartBefore(this.startContainer)
  5799. }
  5800. while (
  5801. this.endOffset ==
  5802. (this.endContainer.nodeType == 1
  5803. ? this.endContainer.childNodes.length
  5804. : this.endContainer.nodeValue.length)
  5805. ) {
  5806. if (stopFn && stopFn(this.endContainer)) {
  5807. break
  5808. }
  5809. if (isBody(this.endContainer)) {
  5810. break
  5811. }
  5812. this.setEndAfter(this.endContainer)
  5813. }
  5814. }
  5815. return this
  5816. },
  5817. enlargeToBlockElm: function (ignoreEnd) {
  5818. while (!domUtils.isBlockElm(this.startContainer)) {
  5819. this.setStartBefore(this.startContainer)
  5820. }
  5821. if (!ignoreEnd) {
  5822. while (!domUtils.isBlockElm(this.endContainer)) {
  5823. this.setEndAfter(this.endContainer)
  5824. }
  5825. }
  5826. return this
  5827. },
  5828. /**
  5829. * 调整Range的边界,使其"缩小"到最合适的位置
  5830. * @method adjustmentBoundary
  5831. * @return { UE.dom.Range } 当前range对象
  5832. * @see UE.dom.Range:shrinkBoundary()
  5833. */
  5834. adjustmentBoundary: function () {
  5835. if (!this.collapsed) {
  5836. while (
  5837. !domUtils.isBody(this.startContainer) &&
  5838. this.startOffset ==
  5839. this.startContainer[
  5840. this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  5841. ].length &&
  5842. this.startContainer[
  5843. this.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  5844. ].length
  5845. ) {
  5846. this.setStartAfter(this.startContainer)
  5847. }
  5848. while (
  5849. !domUtils.isBody(this.endContainer) &&
  5850. !this.endOffset &&
  5851. this.endContainer[
  5852. this.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  5853. ].length
  5854. ) {
  5855. this.setEndBefore(this.endContainer)
  5856. }
  5857. }
  5858. return this
  5859. },
  5860. /**
  5861. * 给range选区中的内容添加给定的inline标签
  5862. * @method applyInlineStyle
  5863. * @param { String } tagName 需要添加的标签名
  5864. * @example
  5865. * ```html
  5866. * <p>xxxx[xxxx]x</p> ==> range.applyInlineStyle("strong") ==> <p>xxxx[<strong>xxxx</strong>]x</p>
  5867. * ```
  5868. */
  5869. /**
  5870. * 给range选区中的内容添加给定的inline标签, 并且为标签附加上一些初始化属性。
  5871. * @method applyInlineStyle
  5872. * @param { String } tagName 需要添加的标签名
  5873. * @param { Object } attrs 跟随新添加的标签的属性
  5874. * @return { UE.dom.Range } 当前选区
  5875. * @example
  5876. * ```html
  5877. * <p>xxxx[xxxx]x</p>
  5878. *
  5879. * ==>
  5880. *
  5881. * <!-- 执行操作 -->
  5882. * range.applyInlineStyle("strong",{"style":"font-size:12px"})
  5883. *
  5884. * ==>
  5885. *
  5886. * <p>xxxx[<strong style="font-size:12px">xxxx</strong>]x</p>
  5887. * ```
  5888. */
  5889. applyInlineStyle: function (tagName, attrs, list) {
  5890. if (this.collapsed) return this
  5891. this.trimBoundary()
  5892. .enlarge(false, function (node) {
  5893. return node.nodeType == 1 && domUtils.isBlockElm(node)
  5894. })
  5895. .adjustmentBoundary()
  5896. var bookmark = this.createBookmark(),
  5897. end = bookmark.end,
  5898. filterFn = function (node) {
  5899. return node.nodeType == 1
  5900. ? node.tagName.toLowerCase() != 'br'
  5901. : !domUtils.isWhitespace(node)
  5902. },
  5903. current = domUtils.getNextDomNode(bookmark.start, false, filterFn),
  5904. node,
  5905. pre,
  5906. range = this.cloneRange()
  5907. while (
  5908. current &&
  5909. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  5910. ) {
  5911. if (current.nodeType == 3 || dtd[tagName][current.tagName]) {
  5912. range.setStartBefore(current)
  5913. node = current
  5914. while (
  5915. node &&
  5916. (node.nodeType == 3 || dtd[tagName][node.tagName]) &&
  5917. node !== end
  5918. ) {
  5919. pre = node
  5920. node = domUtils.getNextDomNode(
  5921. node,
  5922. node.nodeType == 1,
  5923. null,
  5924. function (parent) {
  5925. return dtd[tagName][parent.tagName]
  5926. }
  5927. )
  5928. }
  5929. var frag = range.setEndAfter(pre).extractContents(),
  5930. elm
  5931. if (list && list.length > 0) {
  5932. var level, top
  5933. top = level = list[0].cloneNode(false)
  5934. for (var i = 1, ci; (ci = list[i++]); ) {
  5935. level.appendChild(ci.cloneNode(false))
  5936. level = level.firstChild
  5937. }
  5938. elm = level
  5939. } else {
  5940. elm = range.document.createElement(tagName)
  5941. }
  5942. if (attrs) {
  5943. domUtils.setAttributes(elm, attrs)
  5944. }
  5945. elm.appendChild(frag)
  5946. range.insertNode(list ? top : elm)
  5947. //处理下滑线在a上的情况
  5948. var aNode
  5949. if (
  5950. tagName == 'span' &&
  5951. attrs.style &&
  5952. /text\-decoration/.test(attrs.style) &&
  5953. (aNode = domUtils.findParentByTagName(elm, 'a', true))
  5954. ) {
  5955. domUtils.setAttributes(aNode, attrs)
  5956. domUtils.remove(elm, true)
  5957. elm = aNode
  5958. } else {
  5959. domUtils.mergeSibling(elm)
  5960. domUtils.clearEmptySibling(elm)
  5961. }
  5962. //去除子节点相同的
  5963. domUtils.mergeChild(elm, attrs)
  5964. current = domUtils.getNextDomNode(elm, false, filterFn)
  5965. domUtils.mergeToParent(elm)
  5966. if (node === end) {
  5967. break
  5968. }
  5969. } else {
  5970. current = domUtils.getNextDomNode(current, true, filterFn)
  5971. }
  5972. }
  5973. return this.moveToBookmark(bookmark)
  5974. },
  5975. /**
  5976. * 移除当前选区内指定的inline标签,但保留其中的内容
  5977. * @method removeInlineStyle
  5978. * @param { String } tagName 需要移除的标签名
  5979. * @return { UE.dom.Range } 当前的range对象
  5980. * @example
  5981. * ```html
  5982. * xx[x<span>xxx<em>yyy</em>zz]z</span> => range.removeInlineStyle(["em"]) => xx[x<span>xxxyyyzz]z</span>
  5983. * ```
  5984. */
  5985. /**
  5986. * 移除当前选区内指定的一组inline标签,但保留其中的内容
  5987. * @method removeInlineStyle
  5988. * @param { Array } tagNameArr 需要移除的标签名的数组
  5989. * @return { UE.dom.Range } 当前的range对象
  5990. * @see UE.dom.Range:removeInlineStyle(String)
  5991. */
  5992. removeInlineStyle: function (tagNames) {
  5993. if (this.collapsed) return this
  5994. tagNames = utils.isArray(tagNames) ? tagNames : [tagNames]
  5995. this.shrinkBoundary().adjustmentBoundary()
  5996. var start = this.startContainer,
  5997. end = this.endContainer
  5998. while (1) {
  5999. if (start.nodeType == 1) {
  6000. if (utils.indexOf(tagNames, start.tagName.toLowerCase()) > -1) {
  6001. break
  6002. }
  6003. if (start.tagName.toLowerCase() == 'body') {
  6004. start = null
  6005. break
  6006. }
  6007. }
  6008. start = start.parentNode
  6009. }
  6010. while (1) {
  6011. if (end.nodeType == 1) {
  6012. if (utils.indexOf(tagNames, end.tagName.toLowerCase()) > -1) {
  6013. break
  6014. }
  6015. if (end.tagName.toLowerCase() == 'body') {
  6016. end = null
  6017. break
  6018. }
  6019. }
  6020. end = end.parentNode
  6021. }
  6022. var bookmark = this.createBookmark(),
  6023. frag,
  6024. tmpRange
  6025. if (start) {
  6026. tmpRange = this.cloneRange()
  6027. .setEndBefore(bookmark.start)
  6028. .setStartBefore(start)
  6029. frag = tmpRange.extractContents()
  6030. tmpRange.insertNode(frag)
  6031. domUtils.clearEmptySibling(start, true)
  6032. start.parentNode.insertBefore(bookmark.start, start)
  6033. }
  6034. if (end) {
  6035. tmpRange = this.cloneRange()
  6036. .setStartAfter(bookmark.end)
  6037. .setEndAfter(end)
  6038. frag = tmpRange.extractContents()
  6039. tmpRange.insertNode(frag)
  6040. domUtils.clearEmptySibling(end, false, true)
  6041. end.parentNode.insertBefore(bookmark.end, end.nextSibling)
  6042. }
  6043. var current = domUtils.getNextDomNode(
  6044. bookmark.start,
  6045. false,
  6046. function (node) {
  6047. return node.nodeType == 1
  6048. }
  6049. ),
  6050. next
  6051. while (current && current !== bookmark.end) {
  6052. next = domUtils.getNextDomNode(current, true, function (node) {
  6053. return node.nodeType == 1
  6054. })
  6055. if (utils.indexOf(tagNames, current.tagName.toLowerCase()) > -1) {
  6056. domUtils.remove(current, true)
  6057. }
  6058. current = next
  6059. }
  6060. return this.moveToBookmark(bookmark)
  6061. },
  6062. /**
  6063. * 获取当前选中的自闭合的节点
  6064. * @method getClosedNode
  6065. * @return { Node | NULL } 如果当前选中的是自闭合节点, 则返回该节点, 否则返回NULL
  6066. */
  6067. getClosedNode: function () {
  6068. var node
  6069. if (!this.collapsed) {
  6070. var range = this.cloneRange().adjustmentBoundary().shrinkBoundary()
  6071. if (selectOneNode(range)) {
  6072. var child = range.startContainer.childNodes[range.startOffset]
  6073. if (
  6074. child &&
  6075. child.nodeType == 1 &&
  6076. (dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName])
  6077. ) {
  6078. node = child
  6079. }
  6080. }
  6081. }
  6082. return node
  6083. },
  6084. /**
  6085. * 在页面上高亮range所表示的选区
  6086. * @method select
  6087. * @return { UE.dom.Range } 返回当前Range对象
  6088. */
  6089. //这里不区分ie9以上,trace:3824
  6090. select: browser.ie
  6091. ? function (noFillData, textRange) {
  6092. var nativeRange
  6093. if (!this.collapsed) this.shrinkBoundary()
  6094. var node = this.getClosedNode()
  6095. if (node && !textRange) {
  6096. try {
  6097. nativeRange = this.document.body.createControlRange()
  6098. nativeRange.addElement(node)
  6099. nativeRange.select()
  6100. } catch (e) {}
  6101. return this
  6102. }
  6103. var bookmark = this.createBookmark(),
  6104. start = bookmark.start,
  6105. end
  6106. nativeRange = this.document.body.createTextRange()
  6107. nativeRange.moveToElementText(start)
  6108. nativeRange.moveStart('character', 1)
  6109. if (!this.collapsed) {
  6110. var nativeRangeEnd = this.document.body.createTextRange()
  6111. end = bookmark.end
  6112. nativeRangeEnd.moveToElementText(end)
  6113. nativeRange.setEndPoint('EndToEnd', nativeRangeEnd)
  6114. } else {
  6115. if (!noFillData && this.startContainer.nodeType != 3) {
  6116. //使用<span>|x<span>固定住光标
  6117. var tmpText = this.document.createTextNode(fillChar),
  6118. tmp = this.document.createElement('span')
  6119. tmp.appendChild(this.document.createTextNode(fillChar))
  6120. start.parentNode.insertBefore(tmp, start)
  6121. start.parentNode.insertBefore(tmpText, start)
  6122. //当点b,i,u时,不能清除i上边的b
  6123. removeFillData(this.document, tmpText)
  6124. fillData = tmpText
  6125. mergeSibling(tmp, 'previousSibling')
  6126. mergeSibling(start, 'nextSibling')
  6127. nativeRange.moveStart('character', -1)
  6128. nativeRange.collapse(true)
  6129. }
  6130. }
  6131. this.moveToBookmark(bookmark)
  6132. tmp && domUtils.remove(tmp)
  6133. //IE在隐藏状态下不支持range操作,catch一下
  6134. try {
  6135. nativeRange.select()
  6136. } catch (e) {}
  6137. return this
  6138. }
  6139. : function (notInsertFillData) {
  6140. function checkOffset(rng) {
  6141. function check(node, offset, dir) {
  6142. if (node.nodeType == 3 && node.nodeValue.length < offset) {
  6143. rng[dir + 'Offset'] = node.nodeValue.length
  6144. }
  6145. }
  6146. check(rng.startContainer, rng.startOffset, 'start')
  6147. check(rng.endContainer, rng.endOffset, 'end')
  6148. }
  6149. var win = domUtils.getWindow(this.document),
  6150. sel = win.getSelection(),
  6151. txtNode
  6152. //FF下关闭自动长高时滚动条在关闭dialog时会跳
  6153. //ff下如果不body.focus将不能定位闭合光标到编辑器内
  6154. browser.gecko ? this.document.body.focus() : win.focus()
  6155. if (sel) {
  6156. sel.removeAllRanges()
  6157. // trace:870 chrome/safari后边是br对于闭合得range不能定位 所以去掉了判断
  6158. // this.startContainer.nodeType != 3 &&! ((child = this.startContainer.childNodes[this.startOffset]) && child.nodeType == 1 && child.tagName == 'BR'
  6159. if (this.collapsed && !notInsertFillData) {
  6160. // //opear如果没有节点接着,原生的不能够定位,不能在body的第一级插入空白节点
  6161. // if (notInsertFillData && browser.opera && !domUtils.isBody(this.startContainer) && this.startContainer.nodeType == 1) {
  6162. // var tmp = this.document.createTextNode('');
  6163. // this.insertNode(tmp).setStart(tmp, 0).collapse(true);
  6164. // }
  6165. //
  6166. //处理光标落在文本节点的情况
  6167. //处理以下的情况
  6168. //<b>|xxxx</b>
  6169. //<b>xxxx</b>|xxxx
  6170. //xxxx<b>|</b>
  6171. var start = this.startContainer,
  6172. child = start
  6173. if (start.nodeType == 1) {
  6174. child = start.childNodes[this.startOffset]
  6175. }
  6176. if (
  6177. !(start.nodeType == 3 && this.startOffset) &&
  6178. (child
  6179. ? !child.previousSibling ||
  6180. child.previousSibling.nodeType != 3
  6181. : !start.lastChild || start.lastChild.nodeType != 3)
  6182. ) {
  6183. txtNode = this.document.createTextNode(fillChar)
  6184. //跟着前边走
  6185. this.insertNode(txtNode)
  6186. removeFillData(this.document, txtNode)
  6187. mergeSibling(txtNode, 'previousSibling')
  6188. mergeSibling(txtNode, 'nextSibling')
  6189. fillData = txtNode
  6190. this.setStart(txtNode, browser.webkit ? 1 : 0).collapse(true)
  6191. }
  6192. }
  6193. var nativeRange = this.document.createRange()
  6194. if (
  6195. this.collapsed &&
  6196. browser.opera &&
  6197. this.startContainer.nodeType == 1
  6198. ) {
  6199. var child = this.startContainer.childNodes[this.startOffset]
  6200. if (!child) {
  6201. //往前靠拢
  6202. child = this.startContainer.lastChild
  6203. if (child && domUtils.isBr(child)) {
  6204. this.setStartBefore(child).collapse(true)
  6205. }
  6206. } else {
  6207. //向后靠拢
  6208. while (child && domUtils.isBlockElm(child)) {
  6209. if (child.nodeType == 1 && child.childNodes[0]) {
  6210. child = child.childNodes[0]
  6211. } else {
  6212. break
  6213. }
  6214. }
  6215. child && this.setStartBefore(child).collapse(true)
  6216. }
  6217. }
  6218. //是createAddress最后一位算的不准,现在这里进行微调
  6219. checkOffset(this)
  6220. nativeRange.setStart(this.startContainer, this.startOffset)
  6221. nativeRange.setEnd(this.endContainer, this.endOffset)
  6222. sel.addRange(nativeRange)
  6223. }
  6224. return this
  6225. },
  6226. /**
  6227. * 滚动到当前range开始的位置
  6228. * @method scrollToView
  6229. * @param { Window } win 当前range对象所属的window对象
  6230. * @return { UE.dom.Range } 当前Range对象
  6231. */
  6232. /**
  6233. * 滚动到距离当前range开始位置 offset 的位置处
  6234. * @method scrollToView
  6235. * @param { Window } win 当前range对象所属的window对象
  6236. * @param { Number } offset 距离range开始位置处的偏移量, 如果为正数, 则向下偏移, 反之, 则向上偏移
  6237. * @return { UE.dom.Range } 当前Range对象
  6238. */
  6239. scrollToView: function (win, offset) {
  6240. win = win ? window : domUtils.getWindow(this.document)
  6241. var me = this,
  6242. span = me.document.createElement('span')
  6243. //trace:717
  6244. span.innerHTML = '&nbsp;'
  6245. me.cloneRange().insertNode(span)
  6246. domUtils.scrollToView(span, win, offset)
  6247. domUtils.remove(span)
  6248. return me
  6249. },
  6250. /**
  6251. * 判断当前选区内容是否占位符
  6252. * @private
  6253. * @method inFillChar
  6254. * @return { Boolean } 如果是占位符返回true,否则返回false
  6255. */
  6256. inFillChar: function () {
  6257. var start = this.startContainer
  6258. if (
  6259. this.collapsed &&
  6260. start.nodeType == 3 &&
  6261. start.nodeValue.replace(new RegExp('^' + domUtils.fillChar), '')
  6262. .length +
  6263. 1 ==
  6264. start.nodeValue.length
  6265. ) {
  6266. return true
  6267. }
  6268. return false
  6269. },
  6270. /**
  6271. * 保存
  6272. * @method createAddress
  6273. * @private
  6274. * @return { Boolean } 返回开始和结束的位置
  6275. * @example
  6276. * ```html
  6277. * <body>
  6278. * <p>
  6279. * aaaa
  6280. * <em>
  6281. * <!-- 选区开始 -->
  6282. * bbbb
  6283. * <!-- 选区结束 -->
  6284. * </em>
  6285. * </p>
  6286. *
  6287. * <script>
  6288. * //output: {startAddress:[0,1,0,0],endAddress:[0,1,0,4]}
  6289. * console.log( range.createAddress() );
  6290. * </script>
  6291. * </body>
  6292. * ```
  6293. */
  6294. createAddress: function (ignoreEnd, ignoreTxt) {
  6295. var addr = {},
  6296. me = this
  6297. function getAddress(isStart) {
  6298. var node = isStart ? me.startContainer : me.endContainer
  6299. var parents = domUtils.findParents(node, true, function (node) {
  6300. return !domUtils.isBody(node)
  6301. }),
  6302. addrs = []
  6303. for (var i = 0, ci; (ci = parents[i++]); ) {
  6304. addrs.push(domUtils.getNodeIndex(ci, ignoreTxt))
  6305. }
  6306. var firstIndex = 0
  6307. if (ignoreTxt) {
  6308. if (node.nodeType == 3) {
  6309. var tmpNode = node.previousSibling
  6310. while (tmpNode && tmpNode.nodeType == 3) {
  6311. firstIndex += tmpNode.nodeValue.replace(fillCharReg, '').length
  6312. tmpNode = tmpNode.previousSibling
  6313. }
  6314. firstIndex += isStart ? me.startOffset : me.endOffset // - (fillCharReg.test(node.nodeValue) ? 1 : 0 )
  6315. } else {
  6316. node = node.childNodes[isStart ? me.startOffset : me.endOffset]
  6317. if (node) {
  6318. firstIndex = domUtils.getNodeIndex(node, ignoreTxt)
  6319. } else {
  6320. node = isStart ? me.startContainer : me.endContainer
  6321. var first = node.firstChild
  6322. while (first) {
  6323. if (domUtils.isFillChar(first)) {
  6324. first = first.nextSibling
  6325. continue
  6326. }
  6327. firstIndex++
  6328. if (first.nodeType == 3) {
  6329. while (first && first.nodeType == 3) {
  6330. first = first.nextSibling
  6331. }
  6332. } else {
  6333. first = first.nextSibling
  6334. }
  6335. }
  6336. }
  6337. }
  6338. } else {
  6339. firstIndex = isStart
  6340. ? domUtils.isFillChar(node)
  6341. ? 0
  6342. : me.startOffset
  6343. : me.endOffset
  6344. }
  6345. if (firstIndex < 0) {
  6346. firstIndex = 0
  6347. }
  6348. addrs.push(firstIndex)
  6349. return addrs
  6350. }
  6351. addr.startAddress = getAddress(true)
  6352. if (!ignoreEnd) {
  6353. addr.endAddress = me.collapsed
  6354. ? [].concat(addr.startAddress)
  6355. : getAddress()
  6356. }
  6357. return addr
  6358. },
  6359. /**
  6360. * 保存
  6361. * @method createAddress
  6362. * @private
  6363. * @return { Boolean } 返回开始和结束的位置
  6364. * @example
  6365. * ```html
  6366. * <body>
  6367. * <p>
  6368. * aaaa
  6369. * <em>
  6370. * <!-- 选区开始 -->
  6371. * bbbb
  6372. * <!-- 选区结束 -->
  6373. * </em>
  6374. * </p>
  6375. *
  6376. * <script>
  6377. * var range = editor.selection.getRange();
  6378. * range.moveToAddress({startAddress:[0,1,0,0],endAddress:[0,1,0,4]});
  6379. * range.select();
  6380. * //output: 'bbbb'
  6381. * console.log(editor.selection.getText());
  6382. * </script>
  6383. * </body>
  6384. * ```
  6385. */
  6386. moveToAddress: function (addr, ignoreEnd) {
  6387. var me = this
  6388. function getNode(address, isStart) {
  6389. var tmpNode = me.document.body,
  6390. parentNode,
  6391. offset
  6392. for (var i = 0, ci, l = address.length; i < l; i++) {
  6393. ci = address[i]
  6394. parentNode = tmpNode
  6395. tmpNode = tmpNode.childNodes[ci]
  6396. if (!tmpNode) {
  6397. offset = ci
  6398. break
  6399. }
  6400. }
  6401. if (isStart) {
  6402. if (tmpNode) {
  6403. me.setStartBefore(tmpNode)
  6404. } else {
  6405. me.setStart(parentNode, offset)
  6406. }
  6407. } else {
  6408. if (tmpNode) {
  6409. me.setEndBefore(tmpNode)
  6410. } else {
  6411. me.setEnd(parentNode, offset)
  6412. }
  6413. }
  6414. }
  6415. getNode(addr.startAddress, true)
  6416. !ignoreEnd && addr.endAddress && getNode(addr.endAddress)
  6417. return me
  6418. },
  6419. /**
  6420. * 判断给定的Range对象是否和当前Range对象表示的是同一个选区
  6421. * @method equals
  6422. * @param { UE.dom.Range } 需要判断的Range对象
  6423. * @return { Boolean } 如果给定的Range对象与当前Range对象表示的是同一个选区, 则返回true, 否则返回false
  6424. */
  6425. equals: function (rng) {
  6426. for (var p in this) {
  6427. if (this.hasOwnProperty(p)) {
  6428. if (this[p] !== rng[p]) return false
  6429. }
  6430. }
  6431. return true
  6432. },
  6433. /**
  6434. * 遍历range内的节点。每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6435. * 作为其参数。
  6436. * @method traversal
  6437. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6438. * @return { UE.dom.Range } 当前range对象
  6439. * @example
  6440. * ```html
  6441. *
  6442. * <body>
  6443. *
  6444. * <!-- 选区开始 -->
  6445. * <span></span>
  6446. * <a></a>
  6447. * <!-- 选区结束 -->
  6448. * </body>
  6449. *
  6450. * <script>
  6451. *
  6452. * //output: <span></span><a></a>
  6453. * console.log( range.cloneContents() );
  6454. *
  6455. * range.traversal( function ( node ) {
  6456. *
  6457. * if ( node.nodeType === 1 ) {
  6458. * node.className = "test";
  6459. * }
  6460. *
  6461. * } );
  6462. *
  6463. * //output: <span class="test"></span><a class="test"></a>
  6464. * console.log( range.cloneContents() );
  6465. *
  6466. * </script>
  6467. * ```
  6468. */
  6469. /**
  6470. * 遍历range内的节点。
  6471. * 每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6472. * 作为其参数。
  6473. * 可以通过参数项 filterFn 来指定一个过滤器, 只有符合该过滤器过滤规则的节点才会触
  6474. * 发doFn函数的执行
  6475. * @method traversal
  6476. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6477. * @param { Function } filterFn 过滤器, 该函数接受当前遍历的节点作为参数, 如果该节点满足过滤
  6478. * 规则, 请返回true, 该节点会触发doFn, 否则, 请返回false, 则该节点不
  6479. * 会触发doFn。
  6480. * @return { UE.dom.Range } 当前range对象
  6481. * @see UE.dom.Range:traversal(Function)
  6482. * @example
  6483. * ```html
  6484. *
  6485. * <body>
  6486. *
  6487. * <!-- 选区开始 -->
  6488. * <span></span>
  6489. * <a></a>
  6490. * <!-- 选区结束 -->
  6491. * </body>
  6492. *
  6493. * <script>
  6494. *
  6495. * //output: <span></span><a></a>
  6496. * console.log( range.cloneContents() );
  6497. *
  6498. * range.traversal( function ( node ) {
  6499. *
  6500. * node.className = "test";
  6501. *
  6502. * }, function ( node ) {
  6503. * return node.nodeType === 1;
  6504. * } );
  6505. *
  6506. * //output: <span class="test"></span><a class="test"></a>
  6507. * console.log( range.cloneContents() );
  6508. *
  6509. * </script>
  6510. * ```
  6511. */
  6512. traversal: function (doFn, filterFn) {
  6513. if (this.collapsed) return this
  6514. var bookmark = this.createBookmark(),
  6515. end = bookmark.end,
  6516. current = domUtils.getNextDomNode(bookmark.start, false, filterFn)
  6517. while (
  6518. current &&
  6519. current !== end &&
  6520. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  6521. ) {
  6522. var tmpNode = domUtils.getNextDomNode(current, false, filterFn)
  6523. doFn(current)
  6524. current = tmpNode
  6525. }
  6526. return this.moveToBookmark(bookmark)
  6527. },
  6528. }
  6529. })()
  6530. // core/Selection.js
  6531. /**
  6532. * 选集
  6533. * @file
  6534. * @module UE.dom
  6535. * @class Selection
  6536. * @since 1.2.6.1
  6537. */
  6538. /**
  6539. * 选区集合
  6540. * @unfile
  6541. * @module UE.dom
  6542. * @class Selection
  6543. */
  6544. ;(function () {
  6545. function getBoundaryInformation(range, start) {
  6546. var getIndex = domUtils.getNodeIndex
  6547. range = range.duplicate()
  6548. range.collapse(start)
  6549. var parent = range.parentElement()
  6550. //如果节点里没有子节点,直接退出
  6551. if (!parent.hasChildNodes()) {
  6552. return { container: parent, offset: 0 }
  6553. }
  6554. var siblings = parent.children,
  6555. child,
  6556. testRange = range.duplicate(),
  6557. startIndex = 0,
  6558. endIndex = siblings.length - 1,
  6559. index = -1,
  6560. distance
  6561. while (startIndex <= endIndex) {
  6562. index = Math.floor((startIndex + endIndex) / 2)
  6563. child = siblings[index]
  6564. testRange.moveToElementText(child)
  6565. var position = testRange.compareEndPoints('StartToStart', range)
  6566. if (position > 0) {
  6567. endIndex = index - 1
  6568. } else if (position < 0) {
  6569. startIndex = index + 1
  6570. } else {
  6571. //trace:1043
  6572. return { container: parent, offset: getIndex(child) }
  6573. }
  6574. }
  6575. if (index == -1) {
  6576. testRange.moveToElementText(parent)
  6577. testRange.setEndPoint('StartToStart', range)
  6578. distance = testRange.text.replace(/(\r\n|\r)/g, '\n').length
  6579. siblings = parent.childNodes
  6580. if (!distance) {
  6581. child = siblings[siblings.length - 1]
  6582. return { container: child, offset: child.nodeValue.length }
  6583. }
  6584. var i = siblings.length
  6585. while (distance > 0) {
  6586. distance -= siblings[--i].nodeValue.length
  6587. }
  6588. return { container: siblings[i], offset: -distance }
  6589. }
  6590. testRange.collapse(position > 0)
  6591. testRange.setEndPoint(position > 0 ? 'StartToStart' : 'EndToStart', range)
  6592. distance = testRange.text.replace(/(\r\n|\r)/g, '\n').length
  6593. if (!distance) {
  6594. return dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName]
  6595. ? {
  6596. container: parent,
  6597. offset: getIndex(child) + (position > 0 ? 0 : 1),
  6598. }
  6599. : {
  6600. container: child,
  6601. offset: position > 0 ? 0 : child.childNodes.length,
  6602. }
  6603. }
  6604. while (distance > 0) {
  6605. try {
  6606. var pre = child
  6607. child = child[position > 0 ? 'previousSibling' : 'nextSibling']
  6608. distance -= child.nodeValue.length
  6609. } catch (e) {
  6610. return { container: parent, offset: getIndex(pre) }
  6611. }
  6612. }
  6613. return {
  6614. container: child,
  6615. offset: position > 0 ? -distance : child.nodeValue.length + distance,
  6616. }
  6617. }
  6618. /**
  6619. * 将ieRange转换为Range对象
  6620. * @param {Range} ieRange ieRange对象
  6621. * @param {Range} range Range对象
  6622. * @return {Range} range 返回转换后的Range对象
  6623. */
  6624. function transformIERangeToRange(ieRange, range) {
  6625. if (ieRange.item) {
  6626. range.selectNode(ieRange.item(0))
  6627. } else {
  6628. var bi = getBoundaryInformation(ieRange, true)
  6629. range.setStart(bi.container, bi.offset)
  6630. if (ieRange.compareEndPoints('StartToEnd', ieRange) != 0) {
  6631. bi = getBoundaryInformation(ieRange, false)
  6632. range.setEnd(bi.container, bi.offset)
  6633. }
  6634. }
  6635. return range
  6636. }
  6637. /**
  6638. * 获得ieRange
  6639. * @param {Selection} sel Selection对象
  6640. * @return {ieRange} 得到ieRange
  6641. */
  6642. function _getIERange(sel) {
  6643. var ieRange
  6644. //ie下有可能报错
  6645. try {
  6646. ieRange = sel.getNative().createRange()
  6647. } catch (e) {
  6648. return null
  6649. }
  6650. var el = ieRange.item ? ieRange.item(0) : ieRange.parentElement()
  6651. if ((el.ownerDocument || el) === sel.document) {
  6652. return ieRange
  6653. }
  6654. return null
  6655. }
  6656. var Selection = (dom.Selection = function (doc) {
  6657. var me = this,
  6658. iframe
  6659. me.document = doc
  6660. if (browser.ie9below) {
  6661. iframe = domUtils.getWindow(doc).frameElement
  6662. domUtils.on(iframe, 'beforedeactivate', function () {
  6663. me._bakIERange = me.getIERange()
  6664. })
  6665. domUtils.on(iframe, 'activate', function () {
  6666. try {
  6667. if (!_getIERange(me) && me._bakIERange) {
  6668. me._bakIERange.select()
  6669. }
  6670. } catch (ex) {}
  6671. me._bakIERange = null
  6672. })
  6673. }
  6674. iframe = doc = null
  6675. })
  6676. Selection.prototype = {
  6677. rangeInBody: function (rng, txtRange) {
  6678. var node =
  6679. browser.ie9below || txtRange
  6680. ? rng.item
  6681. ? rng.item()
  6682. : rng.parentElement()
  6683. : rng.startContainer
  6684. return (
  6685. node === this.document.body || domUtils.inDoc(node, this.document)
  6686. )
  6687. },
  6688. /**
  6689. * 获取原生seleciton对象
  6690. * @method getNative
  6691. * @return { Object } 获得selection对象
  6692. * @example
  6693. * ```javascript
  6694. * editor.selection.getNative();
  6695. * ```
  6696. */
  6697. getNative: function () {
  6698. var doc = this.document
  6699. try {
  6700. return !doc
  6701. ? null
  6702. : browser.ie9below
  6703. ? doc.selection
  6704. : domUtils.getWindow(doc).getSelection()
  6705. } catch (e) {
  6706. return null
  6707. }
  6708. },
  6709. /**
  6710. * 获得ieRange
  6711. * @method getIERange
  6712. * @return { Object } 返回ie原生的Range
  6713. * @example
  6714. * ```javascript
  6715. * editor.selection.getIERange();
  6716. * ```
  6717. */
  6718. getIERange: function () {
  6719. var ieRange = _getIERange(this)
  6720. if (!ieRange) {
  6721. if (this._bakIERange) {
  6722. return this._bakIERange
  6723. }
  6724. }
  6725. return ieRange
  6726. },
  6727. /**
  6728. * 缓存当前选区的range和选区的开始节点
  6729. * @method cache
  6730. */
  6731. cache: function () {
  6732. this.clear()
  6733. this._cachedRange = this.getRange()
  6734. this._cachedStartElement = this.getStart()
  6735. this._cachedStartElementPath = this.getStartElementPath()
  6736. },
  6737. /**
  6738. * 获取选区开始位置的父节点到body
  6739. * @method getStartElementPath
  6740. * @return { Array } 返回父节点集合
  6741. * @example
  6742. * ```javascript
  6743. * editor.selection.getStartElementPath();
  6744. * ```
  6745. */
  6746. getStartElementPath: function () {
  6747. if (this._cachedStartElementPath) {
  6748. return this._cachedStartElementPath
  6749. }
  6750. var start = this.getStart()
  6751. if (start) {
  6752. return domUtils.findParents(start, true, null, true)
  6753. }
  6754. return []
  6755. },
  6756. /**
  6757. * 清空缓存
  6758. * @method clear
  6759. */
  6760. clear: function () {
  6761. this._cachedStartElementPath =
  6762. this._cachedRange =
  6763. this._cachedStartElement =
  6764. null
  6765. },
  6766. /**
  6767. * 编辑器是否得到了选区
  6768. * @method isFocus
  6769. */
  6770. isFocus: function () {
  6771. try {
  6772. if (browser.ie9below) {
  6773. var nativeRange = _getIERange(this)
  6774. return !!(nativeRange && this.rangeInBody(nativeRange))
  6775. } else {
  6776. return !!this.getNative().rangeCount
  6777. }
  6778. } catch (e) {
  6779. return false
  6780. }
  6781. },
  6782. /**
  6783. * 获取选区对应的Range
  6784. * @method getRange
  6785. * @return { Object } 得到Range对象
  6786. * @example
  6787. * ```javascript
  6788. * editor.selection.getRange();
  6789. * ```
  6790. */
  6791. getRange: function () {
  6792. var me = this
  6793. function optimze(range) {
  6794. var child = me.document.body.firstChild,
  6795. collapsed = range.collapsed
  6796. while (child && child.firstChild) {
  6797. range.setStart(child, 0)
  6798. child = child.firstChild
  6799. }
  6800. if (!range.startContainer) {
  6801. range.setStart(me.document.body, 0)
  6802. }
  6803. if (collapsed) {
  6804. range.collapse(true)
  6805. }
  6806. }
  6807. if (me._cachedRange != null) {
  6808. return this._cachedRange
  6809. }
  6810. var range = new baidu.editor.dom.Range(me.document)
  6811. if (browser.ie9below) {
  6812. var nativeRange = me.getIERange()
  6813. if (nativeRange) {
  6814. //备份的_bakIERange可能已经实效了,dom树发生了变化比如从源码模式切回来,所以try一下,实效就放到body开始位置
  6815. try {
  6816. transformIERangeToRange(nativeRange, range)
  6817. } catch (e) {
  6818. optimze(range)
  6819. }
  6820. } else {
  6821. optimze(range)
  6822. }
  6823. } else {
  6824. var sel = me.getNative()
  6825. if (sel && sel.rangeCount) {
  6826. var firstRange = sel.getRangeAt(0)
  6827. var lastRange = sel.getRangeAt(sel.rangeCount - 1)
  6828. range
  6829. .setStart(firstRange.startContainer, firstRange.startOffset)
  6830. .setEnd(lastRange.endContainer, lastRange.endOffset)
  6831. if (
  6832. range.collapsed &&
  6833. domUtils.isBody(range.startContainer) &&
  6834. !range.startOffset
  6835. ) {
  6836. optimze(range)
  6837. }
  6838. } else {
  6839. //trace:1734 有可能已经不在dom树上了,标识的节点
  6840. if (
  6841. this._bakRange &&
  6842. domUtils.inDoc(this._bakRange.startContainer, this.document)
  6843. ) {
  6844. return this._bakRange
  6845. }
  6846. optimze(range)
  6847. }
  6848. }
  6849. return (this._bakRange = range)
  6850. },
  6851. /**
  6852. * 获取开始元素,用于状态反射
  6853. * @method getStart
  6854. * @return { Element } 获得开始元素
  6855. * @example
  6856. * ```javascript
  6857. * editor.selection.getStart();
  6858. * ```
  6859. */
  6860. getStart: function () {
  6861. if (this._cachedStartElement) {
  6862. return this._cachedStartElement
  6863. }
  6864. var range = browser.ie9below ? this.getIERange() : this.getRange(),
  6865. tmpRange,
  6866. start,
  6867. tmp,
  6868. parent
  6869. if (browser.ie9below) {
  6870. if (!range) {
  6871. //todo 给第一个值可能会有问题
  6872. return this.document.body.firstChild
  6873. }
  6874. //control元素
  6875. if (range.item) {
  6876. return range.item(0)
  6877. }
  6878. tmpRange = range.duplicate()
  6879. //修正ie下<b>x</b>[xx] 闭合后 <b>x|</b>xx
  6880. tmpRange.text.length > 0 && tmpRange.moveStart('character', 1)
  6881. tmpRange.collapse(1)
  6882. start = tmpRange.parentElement()
  6883. parent = tmp = range.parentElement()
  6884. while ((tmp = tmp.parentNode)) {
  6885. if (tmp == start) {
  6886. start = parent
  6887. break
  6888. }
  6889. }
  6890. } else {
  6891. range.shrinkBoundary()
  6892. start = range.startContainer
  6893. if (start.nodeType == 1 && start.hasChildNodes()) {
  6894. start =
  6895. start.childNodes[
  6896. Math.min(start.childNodes.length - 1, range.startOffset)
  6897. ]
  6898. }
  6899. if (start.nodeType == 3) {
  6900. return start.parentNode
  6901. }
  6902. }
  6903. return start
  6904. },
  6905. /**
  6906. * 得到选区中的文本
  6907. * @method getText
  6908. * @return { String } 选区中包含的文本
  6909. * @example
  6910. * ```javascript
  6911. * editor.selection.getText();
  6912. * ```
  6913. */
  6914. getText: function () {
  6915. var nativeSel, nativeRange
  6916. if (this.isFocus() && (nativeSel = this.getNative())) {
  6917. nativeRange = browser.ie9below
  6918. ? nativeSel.createRange()
  6919. : nativeSel.getRangeAt(0)
  6920. return browser.ie9below ? nativeRange.text : nativeRange.toString()
  6921. }
  6922. return ''
  6923. },
  6924. /**
  6925. * 清除选区
  6926. * @method clearRange
  6927. * @example
  6928. * ```javascript
  6929. * editor.selection.clearRange();
  6930. * ```
  6931. */
  6932. clearRange: function () {
  6933. this.getNative()[browser.ie9below ? 'empty' : 'removeAllRanges']()
  6934. },
  6935. }
  6936. })()
  6937. // core/Editor.js
  6938. /**
  6939. * 编辑器主类,包含编辑器提供的大部分公用接口
  6940. * @file
  6941. * @module UE
  6942. * @class Editor
  6943. * @since 1.2.6.1
  6944. */
  6945. /**
  6946. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  6947. * @unfile
  6948. * @module UE
  6949. */
  6950. /**
  6951. * UEditor的核心类,为用户提供与编辑器交互的接口。
  6952. * @unfile
  6953. * @module UE
  6954. * @class Editor
  6955. */
  6956. ;(function () {
  6957. var uid = 0,
  6958. _selectionChangeTimer
  6959. /**
  6960. * 获取编辑器的html内容,赋值到编辑器所在表单的textarea文本域里面
  6961. * @private
  6962. * @method setValue
  6963. * @param { UE.Editor } editor 编辑器事例
  6964. */
  6965. function setValue(form, editor) {
  6966. var textarea
  6967. if (editor.textarea) {
  6968. if (utils.isString(editor.textarea)) {
  6969. for (
  6970. var i = 0,
  6971. ti,
  6972. tis = domUtils.getElementsByTagName(form, 'textarea');
  6973. (ti = tis[i++]);
  6974. ) {
  6975. if (ti.id == 'ueditor_textarea_' + editor.options.textarea) {
  6976. textarea = ti
  6977. break
  6978. }
  6979. }
  6980. } else {
  6981. textarea = editor.textarea
  6982. }
  6983. }
  6984. if (!textarea) {
  6985. form.appendChild(
  6986. (textarea = domUtils.createElement(document, 'textarea', {
  6987. name: editor.options.textarea,
  6988. id: 'ueditor_textarea_' + editor.options.textarea,
  6989. style: 'display:none',
  6990. }))
  6991. )
  6992. //不要产生多个textarea
  6993. editor.textarea = textarea
  6994. }
  6995. !textarea.getAttribute('name') &&
  6996. textarea.setAttribute('name', editor.options.textarea)
  6997. textarea.value = editor.hasContents()
  6998. ? editor.options.allHtmlEnabled
  6999. ? editor.getAllHtml()
  7000. : editor.getContent(null, null, true)
  7001. : ''
  7002. }
  7003. function loadPlugins(me) {
  7004. //初始化插件
  7005. for (var pi in UE.plugins) {
  7006. UE.plugins[pi].call(me)
  7007. }
  7008. }
  7009. function checkCurLang(I18N) {
  7010. for (var lang in I18N) {
  7011. return lang
  7012. }
  7013. }
  7014. function langReadied(me) {
  7015. me.langIsReady = true
  7016. me.fireEvent('langReady')
  7017. }
  7018. /**
  7019. * 编辑器准备就绪后会触发该事件
  7020. * @module UE
  7021. * @class Editor
  7022. * @event ready
  7023. * @remind render方法执行完成之后,会触发该事件
  7024. * @remind
  7025. * @example
  7026. * ```javascript
  7027. * editor.addListener( 'ready', function( editor ) {
  7028. * editor.execCommand( 'focus' ); //编辑器家在完成后,让编辑器拿到焦点
  7029. * } );
  7030. * ```
  7031. */
  7032. /**
  7033. * 执行destroy方法,会触发该事件
  7034. * @module UE
  7035. * @class Editor
  7036. * @event destroy
  7037. * @see UE.Editor:destroy()
  7038. */
  7039. /**
  7040. * 执行reset方法,会触发该事件
  7041. * @module UE
  7042. * @class Editor
  7043. * @event reset
  7044. * @see UE.Editor:reset()
  7045. */
  7046. /**
  7047. * 执行focus方法,会触发该事件
  7048. * @module UE
  7049. * @class Editor
  7050. * @event focus
  7051. * @see UE.Editor:focus(Boolean)
  7052. */
  7053. /**
  7054. * 语言加载完成会触发该事件
  7055. * @module UE
  7056. * @class Editor
  7057. * @event langReady
  7058. */
  7059. /**
  7060. * 运行命令之后会触发该命令
  7061. * @module UE
  7062. * @class Editor
  7063. * @event beforeExecCommand
  7064. */
  7065. /**
  7066. * 运行命令之后会触发该命令
  7067. * @module UE
  7068. * @class Editor
  7069. * @event afterExecCommand
  7070. */
  7071. /**
  7072. * 运行命令之前会触发该命令
  7073. * @module UE
  7074. * @class Editor
  7075. * @event firstBeforeExecCommand
  7076. */
  7077. /**
  7078. * 在getContent方法执行之前会触发该事件
  7079. * @module UE
  7080. * @class Editor
  7081. * @event beforeGetContent
  7082. * @see UE.Editor:getContent()
  7083. */
  7084. /**
  7085. * 在getContent方法执行之后会触发该事件
  7086. * @module UE
  7087. * @class Editor
  7088. * @event afterGetContent
  7089. * @see UE.Editor:getContent()
  7090. */
  7091. /**
  7092. * 在getAllHtml方法执行时会触发该事件
  7093. * @module UE
  7094. * @class Editor
  7095. * @event getAllHtml
  7096. * @see UE.Editor:getAllHtml()
  7097. */
  7098. /**
  7099. * 在setContent方法执行之前会触发该事件
  7100. * @module UE
  7101. * @class Editor
  7102. * @event beforeSetContent
  7103. * @see UE.Editor:setContent(String)
  7104. */
  7105. /**
  7106. * 在setContent方法执行之后会触发该事件
  7107. * @module UE
  7108. * @class Editor
  7109. * @event afterSetContent
  7110. * @see UE.Editor:setContent(String)
  7111. */
  7112. /**
  7113. * 每当编辑器内部选区发生改变时,将触发该事件
  7114. * @event selectionchange
  7115. * @warning 该事件的触发非常频繁,不建议在该事件的处理过程中做重量级的处理
  7116. * @example
  7117. * ```javascript
  7118. * editor.addListener( 'selectionchange', function( editor ) {
  7119. * console.log('选区发生改变');
  7120. * }
  7121. */
  7122. /**
  7123. * 在所有selectionchange的监听函数执行之前,会触发该事件
  7124. * @module UE
  7125. * @class Editor
  7126. * @event beforeSelectionChange
  7127. * @see UE.Editor:selectionchange
  7128. */
  7129. /**
  7130. * 在所有selectionchange的监听函数执行完之后,会触发该事件
  7131. * @module UE
  7132. * @class Editor
  7133. * @event afterSelectionChange
  7134. * @see UE.Editor:selectionchange
  7135. */
  7136. /**
  7137. * 编辑器内容发生改变时会触发该事件
  7138. * @module UE
  7139. * @class Editor
  7140. * @event contentChange
  7141. */
  7142. /**
  7143. * 以默认参数构建一个编辑器实例
  7144. * @constructor
  7145. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7146. * @example
  7147. * ```javascript
  7148. * var editor = new UE.Editor();
  7149. * editor.execCommand('blod');
  7150. * ```
  7151. * @see UE.Config
  7152. */
  7153. /**
  7154. * 以给定的参数集合创建一个编辑器实例,对于未指定的参数,将应用默认参数。
  7155. * @constructor
  7156. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7157. * @param { Object } setting 创建编辑器的参数
  7158. * @example
  7159. * ```javascript
  7160. * var editor = new UE.Editor();
  7161. * editor.execCommand('blod');
  7162. * ```
  7163. * @see UE.Config
  7164. */
  7165. var Editor = (UE.Editor = function (options) {
  7166. var me = this
  7167. me.uid = uid++
  7168. EventBase.call(me)
  7169. me.commands = {}
  7170. me.options = utils.extend(
  7171. utils.clone(options || {}),
  7172. UEDITOR_CONFIG,
  7173. true
  7174. )
  7175. me.shortcutkeys = {}
  7176. me.inputRules = []
  7177. me.outputRules = []
  7178. //设置默认的常用属性
  7179. me.setOpt(Editor.defaultOptions(me))
  7180. /* 尝试异步加载后台配置 */
  7181. me.loadServerConfig()
  7182. if (!utils.isEmptyObject(UE.I18N)) {
  7183. //修改默认的语言类型
  7184. me.options.lang = checkCurLang(UE.I18N)
  7185. UE.plugin.load(me)
  7186. langReadied(me)
  7187. } else {
  7188. utils.loadFile(
  7189. document,
  7190. {
  7191. src:
  7192. me.options.langPath +
  7193. me.options.lang +
  7194. '/' +
  7195. me.options.lang +
  7196. '.js',
  7197. tag: 'script',
  7198. type: 'text/javascript',
  7199. defer: 'defer',
  7200. },
  7201. function () {
  7202. UE.plugin.load(me)
  7203. langReadied(me)
  7204. }
  7205. )
  7206. }
  7207. UE.instants['ueditorInstant' + me.uid] = me
  7208. })
  7209. Editor.prototype = {
  7210. registerCommand: function (name, obj) {
  7211. this.commands[name] = obj
  7212. },
  7213. /**
  7214. * 编辑器对外提供的监听ready事件的接口, 通过调用该方法,达到的效果与监听ready事件是一致的
  7215. * @method ready
  7216. * @param { Function } fn 编辑器ready之后所执行的回调, 如果在注册事件之前编辑器已经ready,将会
  7217. * 立即触发该回调。
  7218. * @remind 需要等待编辑器加载完成后才能执行的代码,可以使用该方法传入
  7219. * @example
  7220. * ```javascript
  7221. * editor.ready( function( editor ) {
  7222. * editor.setContent('初始化完毕');
  7223. * } );
  7224. * ```
  7225. * @see UE.Editor.event:ready
  7226. */
  7227. ready: function (fn) {
  7228. var me = this
  7229. if (fn) {
  7230. me.isReady ? fn.apply(me) : me.addListener('ready', fn)
  7231. }
  7232. },
  7233. /**
  7234. * 该方法是提供给插件里面使用,设置配置项默认值
  7235. * @method setOpt
  7236. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7237. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7238. * @param { String } key 编辑器的可接受的选项名称
  7239. * @param { * } val 该选项可接受的值
  7240. * @example
  7241. * ```javascript
  7242. * editor.setOpt( 'initContent', '欢迎使用编辑器' );
  7243. * ```
  7244. */
  7245. /**
  7246. * 该方法是提供给插件里面使用,以{key:value}集合的方式设置插件内用到的配置项默认值
  7247. * @method setOpt
  7248. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7249. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7250. * @param { Object } options 将要设置的选项的键值对对象
  7251. * @example
  7252. * ```javascript
  7253. * editor.setOpt( {
  7254. * 'initContent': '欢迎使用编辑器'
  7255. * } );
  7256. * ```
  7257. */
  7258. setOpt: function (key, val) {
  7259. var obj = {}
  7260. if (utils.isString(key)) {
  7261. obj[key] = val
  7262. } else {
  7263. obj = key
  7264. }
  7265. utils.extend(this.options, obj, true)
  7266. },
  7267. getOpt: function (key) {
  7268. return this.options[key]
  7269. },
  7270. /**
  7271. * 销毁编辑器实例,使用textarea代替
  7272. * @method destroy
  7273. * @example
  7274. * ```javascript
  7275. * editor.destroy();
  7276. * ```
  7277. */
  7278. destroy: function () {
  7279. var me = this
  7280. me.fireEvent('destroy')
  7281. var container = me.container.parentNode
  7282. var textarea = me.textarea
  7283. if (!textarea) {
  7284. textarea = document.createElement('textarea')
  7285. container.parentNode.insertBefore(textarea, container)
  7286. } else {
  7287. textarea.style.display = ''
  7288. }
  7289. textarea.style.width = me.iframe.offsetWidth + 'px'
  7290. textarea.style.height = me.iframe.offsetHeight + 'px'
  7291. textarea.value = me.getContent()
  7292. textarea.id = me.key
  7293. container.innerHTML = ''
  7294. domUtils.remove(container)
  7295. var key = me.key
  7296. //trace:2004
  7297. for (var p in me) {
  7298. if (me.hasOwnProperty(p)) {
  7299. delete this[p]
  7300. }
  7301. }
  7302. UE.delEditor(key)
  7303. },
  7304. /**
  7305. * 渲染编辑器的DOM到指定容器
  7306. * @method render
  7307. * @param { String } containerId 指定一个容器ID
  7308. * @remind 执行该方法,会触发ready事件
  7309. * @warning 必须且只能调用一次
  7310. */
  7311. /**
  7312. * 渲染编辑器的DOM到指定容器
  7313. * @method render
  7314. * @param { Element } containerDom 直接指定容器对象
  7315. * @remind 执行该方法,会触发ready事件
  7316. * @warning 必须且只能调用一次
  7317. */
  7318. render: function (container) {
  7319. var me = this,
  7320. options = me.options,
  7321. getStyleValue = function (attr) {
  7322. return parseInt(domUtils.getComputedStyle(container, attr))
  7323. }
  7324. if (utils.isString(container)) {
  7325. container = document.getElementById(container)
  7326. }
  7327. if (container) {
  7328. if (options.initialFrameWidth) {
  7329. options.minFrameWidth = options.initialFrameWidth
  7330. } else {
  7331. options.minFrameWidth = options.initialFrameWidth =
  7332. container.offsetWidth
  7333. }
  7334. if (options.initialFrameHeight) {
  7335. options.minFrameHeight = options.initialFrameHeight
  7336. } else {
  7337. options.initialFrameHeight = options.minFrameHeight =
  7338. container.offsetHeight
  7339. }
  7340. container.style.width = /%$/.test(options.initialFrameWidth)
  7341. ? '100%'
  7342. : options.initialFrameWidth -
  7343. getStyleValue('padding-left') -
  7344. getStyleValue('padding-right') +
  7345. 'px'
  7346. container.style.height = /%$/.test(options.initialFrameHeight)
  7347. ? '100%'
  7348. : options.initialFrameHeight -
  7349. getStyleValue('padding-top') -
  7350. getStyleValue('padding-bottom') +
  7351. 'px'
  7352. container.style.zIndex = options.zIndex
  7353. var html =
  7354. (ie && browser.version < 9 ? '' : '<!DOCTYPE html>') +
  7355. "<html xmlns='http://www.w3.org/1999/xhtml' class='view' ><head>" +
  7356. "<style type='text/css'>" +
  7357. //设置四周的留边
  7358. '.view{padding:0;word-wrap:break-word;cursor:text;height:90%;}\n' +
  7359. //设置默认字体和字号
  7360. //font-family不能呢随便改,在safari下fillchar会有解析问题
  7361. 'body{margin:8px;font-family:sans-serif;font-size:16px;}' +
  7362. //设置段落间距
  7363. 'p{margin:5px 0;}</style>' +
  7364. (options.iframeCssUrl
  7365. ? "<link rel='stylesheet' type='text/css' href='" +
  7366. utils.unhtml(options.iframeCssUrl) +
  7367. "'/>"
  7368. : '') +
  7369. (options.initialStyle
  7370. ? '<style>' + options.initialStyle + '</style>'
  7371. : '') +
  7372. "</head><body class='view' ></body>" +
  7373. "<script type='text/javascript' " +
  7374. (ie ? "defer='defer'" : '') +
  7375. " id='_initialScript'>" +
  7376. "setTimeout(function(){editor = window.parent.UE.instants['ueditorInstant" +
  7377. me.uid +
  7378. "'];editor._setup(document);},0);" +
  7379. "var _tmpScript = document.getElementById('_initialScript');_tmpScript.parentNode.removeChild(_tmpScript);</script></html>"
  7380. container.appendChild(
  7381. domUtils.createElement(document, 'iframe', {
  7382. id: 'ueditor_' + me.uid,
  7383. width: '100%',
  7384. height: '100%',
  7385. frameborder: '0',
  7386. //先注释掉了,加的原因忘记了,但开启会直接导致全屏模式下内容多时不会出现滚动条
  7387. // scrolling :'no',
  7388. src:
  7389. 'javascript:void(function(){document.open();' +
  7390. (options.customDomain && document.domain != location.hostname
  7391. ? 'document.domain="' + document.domain + '";'
  7392. : '') +
  7393. 'document.write("' +
  7394. html +
  7395. '");document.close();}())',
  7396. })
  7397. )
  7398. container.style.overflow = 'hidden'
  7399. //解决如果是给定的百分比,会导致高度算不对的问题
  7400. setTimeout(function () {
  7401. if (/%$/.test(options.initialFrameWidth)) {
  7402. options.minFrameWidth = options.initialFrameWidth =
  7403. container.offsetWidth
  7404. //如果这里给定宽度,会导致ie在拖动窗口大小时,编辑区域不随着变化
  7405. // container.style.width = options.initialFrameWidth + 'px';
  7406. }
  7407. if (/%$/.test(options.initialFrameHeight)) {
  7408. options.minFrameHeight = options.initialFrameHeight =
  7409. container.offsetHeight
  7410. container.style.height = options.initialFrameHeight + 'px'
  7411. }
  7412. })
  7413. }
  7414. },
  7415. /**
  7416. * 编辑器初始化
  7417. * @method _setup
  7418. * @private
  7419. * @param { Element } doc 编辑器Iframe中的文档对象
  7420. */
  7421. _setup: function (doc) {
  7422. var me = this,
  7423. options = me.options
  7424. if (ie) {
  7425. doc.body.disabled = true
  7426. doc.body.contentEditable = true
  7427. doc.body.disabled = false
  7428. } else {
  7429. doc.body.contentEditable = true
  7430. }
  7431. doc.body.spellcheck = false
  7432. me.document = doc
  7433. me.window = doc.defaultView || doc.parentWindow
  7434. me.iframe = me.window.frameElement
  7435. me.body = doc.body
  7436. me.selection = new dom.Selection(doc)
  7437. //gecko初始化就能得到range,无法判断isFocus了
  7438. var geckoSel
  7439. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7440. geckoSel.removeAllRanges()
  7441. }
  7442. this._initEvents()
  7443. //为form提交提供一个隐藏的textarea
  7444. for (
  7445. var form = this.iframe.parentNode;
  7446. !domUtils.isBody(form);
  7447. form = form.parentNode
  7448. ) {
  7449. if (form.tagName == 'FORM') {
  7450. me.form = form
  7451. if (me.options.autoSyncData) {
  7452. domUtils.on(me.window, 'blur', function () {
  7453. setValue(form, me)
  7454. })
  7455. } else {
  7456. domUtils.on(form, 'submit', function () {
  7457. setValue(this, me)
  7458. })
  7459. }
  7460. break
  7461. }
  7462. }
  7463. if (options.initialContent) {
  7464. if (options.autoClearinitialContent) {
  7465. var oldExecCommand = me.execCommand
  7466. me.execCommand = function () {
  7467. me.fireEvent('firstBeforeExecCommand')
  7468. return oldExecCommand.apply(me, arguments)
  7469. }
  7470. this._setDefaultContent(options.initialContent)
  7471. } else this.setContent(options.initialContent, false, true)
  7472. }
  7473. //编辑器不能为空内容
  7474. if (domUtils.isEmptyNode(me.body)) {
  7475. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  7476. }
  7477. //如果要求focus, 就把光标定位到内容开始
  7478. if (options.focus) {
  7479. setTimeout(function () {
  7480. me.focus(me.options.focusInEnd)
  7481. //如果自动清除开着,就不需要做selectionchange;
  7482. !me.options.autoClearinitialContent && me._selectionChange()
  7483. }, 0)
  7484. }
  7485. if (!me.container) {
  7486. me.container = this.iframe.parentNode
  7487. }
  7488. if (options.fullscreen && me.ui) {
  7489. me.ui.setFullScreen(true)
  7490. }
  7491. try {
  7492. me.document.execCommand('2D-position', false, false)
  7493. } catch (e) {}
  7494. try {
  7495. me.document.execCommand('enableInlineTableEditing', false, false)
  7496. } catch (e) {}
  7497. try {
  7498. me.document.execCommand('enableObjectResizing', false, false)
  7499. } catch (e) {}
  7500. //挂接快捷键
  7501. me._bindshortcutKeys()
  7502. me.isReady = 1
  7503. me.fireEvent('ready')
  7504. options.onready && options.onready.call(me)
  7505. if (!browser.ie9below) {
  7506. domUtils.on(me.window, ['blur', 'focus'], function (e) {
  7507. //chrome下会出现alt+tab切换时,导致选区位置不对
  7508. if (e.type == 'blur') {
  7509. me._bakRange = me.selection.getRange()
  7510. try {
  7511. me._bakNativeRange = me.selection.getNative().getRangeAt(0)
  7512. me.selection.getNative().removeAllRanges()
  7513. } catch (e) {
  7514. me._bakNativeRange = null
  7515. }
  7516. } else {
  7517. try {
  7518. me._bakRange && me._bakRange.select()
  7519. } catch (e) {}
  7520. }
  7521. })
  7522. }
  7523. //trace:1518 ff3.6body不够寛,会导致点击空白处无法获得焦点
  7524. if (browser.gecko && browser.version <= 10902) {
  7525. //修复ff3.6初始化进来,不能点击获得焦点
  7526. me.body.contentEditable = false
  7527. setTimeout(function () {
  7528. me.body.contentEditable = true
  7529. }, 100)
  7530. setInterval(function () {
  7531. me.body.style.height = me.iframe.offsetHeight - 20 + 'px'
  7532. }, 100)
  7533. }
  7534. !options.isShow && me.setHide()
  7535. options.readonly && me.setDisabled()
  7536. },
  7537. /**
  7538. * 同步数据到编辑器所在的form
  7539. * 从编辑器的容器节点向上查找form元素,若找到,就同步编辑内容到找到的form里,为提交数据做准备,主要用于是手动提交的情况
  7540. * 后台取得数据的键值,使用你容器上的name属性,如果没有就使用参数里的textarea项
  7541. * @method sync
  7542. * @example
  7543. * ```javascript
  7544. * editor.sync();
  7545. * form.sumbit(); //form变量已经指向了form元素
  7546. * ```
  7547. */
  7548. /**
  7549. * 根据传入的formId,在页面上查找要同步数据的表单,若找到,就同步编辑内容到找到的form里,为提交数据做准备
  7550. * 后台取得数据的键值,该键值默认使用给定的编辑器容器的name属性,如果没有name属性则使用参数项里给定的“textarea”项
  7551. * @method sync
  7552. * @param { String } formID 指定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
  7553. */
  7554. sync: function (formId) {
  7555. var me = this,
  7556. form = formId
  7557. ? document.getElementById(formId)
  7558. : domUtils.findParent(
  7559. me.iframe.parentNode,
  7560. function (node) {
  7561. return node.tagName == 'FORM'
  7562. },
  7563. true
  7564. )
  7565. form && setValue(form, me)
  7566. },
  7567. /**
  7568. * 设置编辑器高度
  7569. * @method setHeight
  7570. * @remind 当配置项autoHeightEnabled为真时,该方法无效
  7571. * @param { Number } number 设置的高度值,纯数值,不带单位
  7572. * @example
  7573. * ```javascript
  7574. * editor.setHeight(number);
  7575. * ```
  7576. */
  7577. setHeight: function (height, notSetHeight) {
  7578. if (height !== parseInt(this.iframe.parentNode.style.height)) {
  7579. this.iframe.parentNode.style.height = height + 'px'
  7580. }
  7581. !notSetHeight &&
  7582. (this.options.minFrameHeight = this.options.initialFrameHeight =
  7583. height)
  7584. this.body.style.height = height + 'px'
  7585. !notSetHeight && this.trigger('setHeight')
  7586. },
  7587. /**
  7588. * 为编辑器的编辑命令提供快捷键
  7589. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7590. * @method addshortcutkey
  7591. * @param { Object } keyset 命令名和快捷键键值对对象,多个按钮的快捷键用“+”分隔
  7592. * @example
  7593. * ```javascript
  7594. * editor.addshortcutkey({
  7595. * "Bold" : "ctrl+66",//^B
  7596. * "Italic" : "ctrl+73", //^I
  7597. * });
  7598. * ```
  7599. */
  7600. /**
  7601. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7602. * @method addshortcutkey
  7603. * @param { String } cmd 触发快捷键时,响应的命令
  7604. * @param { String } keys 快捷键的字符串,多个按钮用“+”分隔
  7605. * @example
  7606. * ```javascript
  7607. * editor.addshortcutkey("Underline", "ctrl+85"); //^U
  7608. * ```
  7609. */
  7610. addshortcutkey: function (cmd, keys) {
  7611. var obj = {}
  7612. if (keys) {
  7613. obj[cmd] = keys
  7614. } else {
  7615. obj = cmd
  7616. }
  7617. utils.extend(this.shortcutkeys, obj)
  7618. },
  7619. /**
  7620. * 对编辑器设置keydown事件监听,绑定快捷键和命令,当快捷键组合触发成功,会响应对应的命令
  7621. * @method _bindshortcutKeys
  7622. * @private
  7623. */
  7624. _bindshortcutKeys: function () {
  7625. var me = this,
  7626. shortcutkeys = this.shortcutkeys
  7627. me.addListener('keydown', function (type, e) {
  7628. var keyCode = e.keyCode || e.which
  7629. for (var i in shortcutkeys) {
  7630. var tmp = shortcutkeys[i].split(',')
  7631. for (var t = 0, ti; (ti = tmp[t++]); ) {
  7632. ti = ti.split(':')
  7633. var key = ti[0],
  7634. param = ti[1]
  7635. if (
  7636. /^(ctrl)(\+shift)?\+(\d+)$/.test(key.toLowerCase()) ||
  7637. /^(\d+)$/.test(key)
  7638. ) {
  7639. if (
  7640. ((RegExp.$1 == 'ctrl' ? e.ctrlKey || e.metaKey : 0) &&
  7641. (RegExp.$2 != '' ? e[RegExp.$2.slice(1) + 'Key'] : 1) &&
  7642. keyCode == RegExp.$3) ||
  7643. keyCode == RegExp.$1
  7644. ) {
  7645. if (me.queryCommandState(i, param) != -1)
  7646. me.execCommand(i, param)
  7647. domUtils.preventDefault(e)
  7648. }
  7649. }
  7650. }
  7651. }
  7652. })
  7653. },
  7654. /**
  7655. * 获取编辑器的内容
  7656. * @method getContent
  7657. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7658. * @return { String } 编辑器的内容字符串, 如果编辑器的内容为空,或者是空的标签内容(如:”&lt;p&gt;&lt;br/&gt;&lt;/p&gt;“), 则返回空字符串
  7659. * @example
  7660. * ```javascript
  7661. * //编辑器html内容:<p>1<strong>2<em>34</em>5</strong>6</p>
  7662. * var content = editor.getContent(); //返回值:<p>1<strong>2<em>34</em>5</strong>6</p>
  7663. * ```
  7664. */
  7665. /**
  7666. * 获取编辑器的内容。 可以通过参数定义编辑器内置的判空规则
  7667. * @method getContent
  7668. * @param { Function } fn 自定的判空规则, 要求该方法返回一个boolean类型的值,
  7669. * 代表当前编辑器的内容是否空,
  7670. * 如果返回true, 则该方法将直接返回空字符串;如果返回false,则编辑器将返回
  7671. * 经过内置过滤规则处理后的内容。
  7672. * @remind 该方法在处理包含有初始化内容的时候能起到很好的作用。
  7673. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7674. * @return { String } 编辑器的内容字符串
  7675. * @example
  7676. * ```javascript
  7677. * // editor 是一个编辑器的实例
  7678. * var content = editor.getContent( function ( editor ) {
  7679. * return editor.body.innerHTML === '欢迎使用UEditor'; //返回空字符串
  7680. * } );
  7681. * ```
  7682. */
  7683. getContent: function (cmd, fn, notSetCursor, ignoreBlank, formatter) {
  7684. var me = this
  7685. if (cmd && utils.isFunction(cmd)) {
  7686. fn = cmd
  7687. cmd = ''
  7688. }
  7689. if (fn ? !fn() : !this.hasContents()) {
  7690. return ''
  7691. }
  7692. me.fireEvent('beforegetcontent')
  7693. var root = UE.htmlparser(me.body.innerHTML, ignoreBlank)
  7694. me.filterOutputRule(root)
  7695. me.fireEvent('aftergetcontent', cmd, root)
  7696. return root.toHtml(formatter)
  7697. },
  7698. /**
  7699. * 取得完整的html代码,可以直接显示成完整的html文档
  7700. * @method getAllHtml
  7701. * @return { String } 编辑器的内容html文档字符串
  7702. * @eaxmple
  7703. * ```javascript
  7704. * editor.getAllHtml(); //返回格式大致是: <html><head>...</head><body>...</body></html>
  7705. * ```
  7706. */
  7707. getAllHtml: function () {
  7708. var me = this,
  7709. headHtml = [],
  7710. html = ''
  7711. me.fireEvent('getAllHtml', headHtml)
  7712. if (browser.ie && browser.version > 8) {
  7713. var headHtmlForIE9 = ''
  7714. utils.each(me.document.styleSheets, function (si) {
  7715. headHtmlForIE9 += si.href
  7716. ? '<link rel="stylesheet" type="text/css" href="' +
  7717. si.href +
  7718. '" />'
  7719. : '<style>' + si.cssText + '</style>'
  7720. })
  7721. utils.each(me.document.getElementsByTagName('script'), function (si) {
  7722. headHtmlForIE9 += si.outerHTML
  7723. })
  7724. }
  7725. return (
  7726. '<html><head>' +
  7727. (me.options.charset
  7728. ? '<meta http-equiv="Content-Type" content="text/html; charset=' +
  7729. me.options.charset +
  7730. '"/>'
  7731. : '') +
  7732. (headHtmlForIE9 ||
  7733. me.document.getElementsByTagName('head')[0].innerHTML) +
  7734. headHtml.join('\n') +
  7735. '</head>' +
  7736. '<body ' +
  7737. (ie && browser.version < 9 ? 'class="view"' : '') +
  7738. '>' +
  7739. me.getContent(null, null, true) +
  7740. '</body></html>'
  7741. )
  7742. },
  7743. /**
  7744. * 得到编辑器的纯文本内容,但会保留段落格式
  7745. * @method getPlainTxt
  7746. * @return { String } 编辑器带段落格式的纯文本内容字符串
  7747. * @example
  7748. * ```javascript
  7749. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7750. * console.log(editor.getPlainTxt()); //输出:"1\n2\n
  7751. * ```
  7752. */
  7753. getPlainTxt: function () {
  7754. var reg = new RegExp(domUtils.fillChar, 'g'),
  7755. html = this.body.innerHTML.replace(/[\n\r]/g, '') //ie要先去了\n在处理
  7756. html = html
  7757. .replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, '\n')
  7758. .replace(/<br\/?>/gi, '\n')
  7759. .replace(/<[^>/]+>/g, '')
  7760. .replace(/(\n)?<\/([^>]+)>/g, function (a, b, c) {
  7761. return dtd.$block[c] ? '\n' : b ? b : ''
  7762. })
  7763. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7764. return html
  7765. .replace(reg, '')
  7766. .replace(/\u00a0/g, ' ')
  7767. .replace(/&nbsp;/g, ' ')
  7768. },
  7769. /**
  7770. * 获取编辑器中的纯文本内容,没有段落格式
  7771. * @method getContentTxt
  7772. * @return { String } 编辑器不带段落格式的纯文本内容字符串
  7773. * @example
  7774. * ```javascript
  7775. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7776. * console.log(editor.getPlainTxt()); //输出:"12
  7777. * ```
  7778. */
  7779. getContentTxt: function () {
  7780. var reg = new RegExp(domUtils.fillChar, 'g')
  7781. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7782. return this.body[browser.ie ? 'innerText' : 'textContent']
  7783. .replace(reg, '')
  7784. .replace(/\u00a0/g, ' ')
  7785. },
  7786. /**
  7787. * 设置编辑器的内容,可修改编辑器当前的html内容
  7788. * @method setContent
  7789. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7790. * @warning 该方法会触发selectionchange事件
  7791. * @param { String } html 要插入的html内容
  7792. * @example
  7793. * ```javascript
  7794. * editor.getContent('<p>test</p>');
  7795. * ```
  7796. */
  7797. /**
  7798. * 设置编辑器的内容,可修改编辑器当前的html内容
  7799. * @method setContent
  7800. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7801. * @warning 该方法会触发selectionchange事件
  7802. * @param { String } html 要插入的html内容
  7803. * @param { Boolean } isAppendTo 若传入true,不清空原来的内容,在最后插入内容,否则,清空内容再插入
  7804. * @example
  7805. * ```javascript
  7806. * //假设设置前的编辑器内容是 <p>old text</p>
  7807. * editor.setContent('<p>new text</p>', true); //插入的结果是<p>old text</p><p>new text</p>
  7808. * ```
  7809. */
  7810. setContent: function (html, isAppendTo, notFireSelectionchange) {
  7811. var me = this
  7812. me.fireEvent('beforesetcontent', html)
  7813. var root = UE.htmlparser(html)
  7814. me.filterInputRule(root)
  7815. html = root.toHtml()
  7816. me.body.innerHTML = (isAppendTo ? me.body.innerHTML : '') + html
  7817. function isCdataDiv(node) {
  7818. return node.tagName == 'DIV' && node.getAttribute('cdata_tag')
  7819. }
  7820. //给文本或者inline节点套p标签
  7821. if (me.options.enterTag == 'p') {
  7822. var child = this.body.firstChild,
  7823. tmpNode
  7824. if (
  7825. !child ||
  7826. (child.nodeType == 1 &&
  7827. (dtd.$cdata[child.tagName] ||
  7828. isCdataDiv(child) ||
  7829. domUtils.isCustomeNode(child)) &&
  7830. child === this.body.lastChild)
  7831. ) {
  7832. this.body.innerHTML =
  7833. '<p>' +
  7834. (browser.ie ? '&nbsp;' : '<br/>') +
  7835. '</p>' +
  7836. this.body.innerHTML
  7837. } else {
  7838. var p = me.document.createElement('p')
  7839. while (child) {
  7840. while (
  7841. child &&
  7842. (child.nodeType == 3 ||
  7843. (child.nodeType == 1 &&
  7844. dtd.p[child.tagName] &&
  7845. !dtd.$cdata[child.tagName]))
  7846. ) {
  7847. tmpNode = child.nextSibling
  7848. p.appendChild(child)
  7849. child = tmpNode
  7850. }
  7851. if (p.firstChild) {
  7852. if (!child) {
  7853. me.body.appendChild(p)
  7854. break
  7855. } else {
  7856. child.parentNode.insertBefore(p, child)
  7857. p = me.document.createElement('p')
  7858. }
  7859. }
  7860. child = child.nextSibling
  7861. }
  7862. }
  7863. }
  7864. me.fireEvent('aftersetcontent')
  7865. me.fireEvent('contentchange')
  7866. !notFireSelectionchange && me._selectionChange()
  7867. //清除保存的选区
  7868. me._bakRange = me._bakIERange = me._bakNativeRange = null
  7869. //trace:1742 setContent后gecko能得到焦点问题
  7870. var geckoSel
  7871. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7872. geckoSel.removeAllRanges()
  7873. }
  7874. if (me.options.autoSyncData) {
  7875. me.form && setValue(me.form, me)
  7876. }
  7877. },
  7878. /**
  7879. * 让编辑器获得焦点,默认focus到编辑器头部
  7880. * @method focus
  7881. * @example
  7882. * ```javascript
  7883. * editor.focus()
  7884. * ```
  7885. */
  7886. /**
  7887. * 让编辑器获得焦点,toEnd确定focus位置
  7888. * @method focus
  7889. * @param { Boolean } toEnd 默认focus到编辑器头部,toEnd为true时focus到内容尾部
  7890. * @example
  7891. * ```javascript
  7892. * editor.focus(true)
  7893. * ```
  7894. */
  7895. focus: function (toEnd) {
  7896. try {
  7897. var me = this,
  7898. rng = me.selection.getRange()
  7899. if (toEnd) {
  7900. var node = me.body.lastChild
  7901. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7902. if (domUtils.isEmptyBlock(node)) {
  7903. rng.setStartAtFirst(node)
  7904. } else {
  7905. rng.setStartAtLast(node)
  7906. }
  7907. rng.collapse(true)
  7908. }
  7909. rng.setCursor(true)
  7910. } else {
  7911. if (
  7912. !rng.collapsed &&
  7913. domUtils.isBody(rng.startContainer) &&
  7914. rng.startOffset == 0
  7915. ) {
  7916. var node = me.body.firstChild
  7917. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7918. rng.setStartAtFirst(node).collapse(true)
  7919. }
  7920. }
  7921. rng.select(true)
  7922. }
  7923. this.fireEvent('focus selectionchange')
  7924. } catch (e) {}
  7925. },
  7926. isFocus: function () {
  7927. return this.selection.isFocus()
  7928. },
  7929. blur: function () {
  7930. var sel = this.selection.getNative()
  7931. if (sel.empty && browser.ie) {
  7932. var nativeRng = document.body.createTextRange()
  7933. nativeRng.moveToElementText(document.body)
  7934. nativeRng.collapse(true)
  7935. nativeRng.select()
  7936. sel.empty()
  7937. } else {
  7938. sel.removeAllRanges()
  7939. }
  7940. //this.fireEvent('blur selectionchange');
  7941. },
  7942. /**
  7943. * 初始化UE事件及部分事件代理
  7944. * @method _initEvents
  7945. * @private
  7946. */
  7947. _initEvents: function () {
  7948. var me = this,
  7949. doc = me.document,
  7950. win = me.window
  7951. me._proxyDomEvent = utils.bind(me._proxyDomEvent, me)
  7952. domUtils.on(
  7953. doc,
  7954. [
  7955. 'click',
  7956. 'contextmenu',
  7957. 'mousedown',
  7958. 'keydown',
  7959. 'keyup',
  7960. 'keypress',
  7961. 'mouseup',
  7962. 'mouseover',
  7963. 'mouseout',
  7964. 'selectstart',
  7965. ],
  7966. me._proxyDomEvent
  7967. )
  7968. domUtils.on(win, ['focus', 'blur'], me._proxyDomEvent)
  7969. domUtils.on(me.body, 'drop', function (e) {
  7970. //阻止ff下默认的弹出新页面打开图片
  7971. if (browser.gecko && e.stopPropagation) {
  7972. e.stopPropagation()
  7973. }
  7974. me.fireEvent('contentchange')
  7975. })
  7976. domUtils.on(doc, ['mouseup', 'keydown'], function (evt) {
  7977. //特殊键不触发selectionchange
  7978. if (
  7979. evt.type == 'keydown' &&
  7980. (evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.altKey)
  7981. ) {
  7982. return
  7983. }
  7984. if (evt.button == 2) return
  7985. me._selectionChange(250, evt)
  7986. })
  7987. },
  7988. /**
  7989. * 触发事件代理
  7990. * @method _proxyDomEvent
  7991. * @private
  7992. * @return { * } fireEvent的返回值
  7993. * @see UE.EventBase:fireEvent(String)
  7994. */
  7995. _proxyDomEvent: function (evt) {
  7996. if (
  7997. this.fireEvent(
  7998. 'before' + evt.type.replace(/^on/, '').toLowerCase()
  7999. ) === false
  8000. ) {
  8001. return false
  8002. }
  8003. if (this.fireEvent(evt.type.replace(/^on/, ''), evt) === false) {
  8004. return false
  8005. }
  8006. return this.fireEvent(
  8007. 'after' + evt.type.replace(/^on/, '').toLowerCase()
  8008. )
  8009. },
  8010. /**
  8011. * 变化选区
  8012. * @method _selectionChange
  8013. * @private
  8014. */
  8015. _selectionChange: function (delay, evt) {
  8016. var me = this
  8017. //有光标才做selectionchange 为了解决未focus时点击source不能触发更改工具栏状态的问题(source命令notNeedUndo=1)
  8018. // if ( !me.selection.isFocus() ){
  8019. // return;
  8020. // }
  8021. var hackForMouseUp = false
  8022. var mouseX, mouseY
  8023. if (browser.ie && browser.version < 9 && evt && evt.type == 'mouseup') {
  8024. var range = this.selection.getRange()
  8025. if (!range.collapsed) {
  8026. hackForMouseUp = true
  8027. mouseX = evt.clientX
  8028. mouseY = evt.clientY
  8029. }
  8030. }
  8031. clearTimeout(_selectionChangeTimer)
  8032. _selectionChangeTimer = setTimeout(function () {
  8033. if (!me.selection || !me.selection.getNative()) {
  8034. return
  8035. }
  8036. //修复一个IE下的bug: 鼠标点击一段已选择的文本中间时,可能在mouseup后的一段时间内取到的range是在selection的type为None下的错误值.
  8037. //IE下如果用户是拖拽一段已选择文本,则不会触发mouseup事件,所以这里的特殊处理不会对其有影响
  8038. var ieRange
  8039. if (hackForMouseUp && me.selection.getNative().type == 'None') {
  8040. ieRange = me.document.body.createTextRange()
  8041. try {
  8042. ieRange.moveToPoint(mouseX, mouseY)
  8043. } catch (ex) {
  8044. ieRange = null
  8045. }
  8046. }
  8047. var bakGetIERange
  8048. if (ieRange) {
  8049. bakGetIERange = me.selection.getIERange
  8050. me.selection.getIERange = function () {
  8051. return ieRange
  8052. }
  8053. }
  8054. me.selection.cache()
  8055. if (bakGetIERange) {
  8056. me.selection.getIERange = bakGetIERange
  8057. }
  8058. if (me.selection._cachedRange && me.selection._cachedStartElement) {
  8059. me.fireEvent('beforeselectionchange')
  8060. // 第二个参数causeByUi为true代表由用户交互造成的selectionchange.
  8061. me.fireEvent('selectionchange', !!evt)
  8062. me.fireEvent('afterselectionchange')
  8063. me.selection.clear()
  8064. }
  8065. }, delay || 50)
  8066. },
  8067. /**
  8068. * 执行编辑命令
  8069. * @method _callCmdFn
  8070. * @private
  8071. * @param { String } fnName 函数名称
  8072. * @param { * } args 传给命令函数的参数
  8073. * @return { * } 返回命令函数运行的返回值
  8074. */
  8075. _callCmdFn: function (fnName, args) {
  8076. var cmdName = args[0].toLowerCase(),
  8077. cmd,
  8078. cmdFn
  8079. cmd = this.commands[cmdName] || UE.commands[cmdName]
  8080. cmdFn = cmd && cmd[fnName]
  8081. //没有querycommandstate或者没有command的都默认返回0
  8082. if ((!cmd || !cmdFn) && fnName == 'queryCommandState') {
  8083. return 0
  8084. } else if (cmdFn) {
  8085. return cmdFn.apply(this, args)
  8086. }
  8087. },
  8088. /**
  8089. * 执行编辑命令cmdName,完成富文本编辑效果
  8090. * @method execCommand
  8091. * @param { String } cmdName 需要执行的命令
  8092. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8093. * @return { * } 返回命令函数运行的返回值
  8094. * @example
  8095. * ```javascript
  8096. * editor.execCommand(cmdName);
  8097. * ```
  8098. */
  8099. execCommand: function (cmdName) {
  8100. cmdName = cmdName.toLowerCase()
  8101. var me = this,
  8102. result,
  8103. cmd = me.commands[cmdName] || UE.commands[cmdName]
  8104. if (!cmd || !cmd.execCommand) {
  8105. return null
  8106. }
  8107. if (!cmd.notNeedUndo && !me.__hasEnterExecCommand) {
  8108. me.__hasEnterExecCommand = true
  8109. if (me.queryCommandState.apply(me, arguments) != -1) {
  8110. me.fireEvent('saveScene')
  8111. me.fireEvent.apply(
  8112. me,
  8113. ['beforeexeccommand', cmdName].concat(arguments)
  8114. )
  8115. result = this._callCmdFn('execCommand', arguments)
  8116. //保存场景时,做了内容对比,再看是否进行contentchange触发,这里多触发了一次,去掉
  8117. // (!cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange');
  8118. me.fireEvent.apply(
  8119. me,
  8120. ['afterexeccommand', cmdName].concat(arguments)
  8121. )
  8122. me.fireEvent('saveScene')
  8123. }
  8124. me.__hasEnterExecCommand = false
  8125. } else {
  8126. result = this._callCmdFn('execCommand', arguments)
  8127. !me.__hasEnterExecCommand &&
  8128. !cmd.ignoreContentChange &&
  8129. !me._ignoreContentChange &&
  8130. me.fireEvent('contentchange')
  8131. }
  8132. !me.__hasEnterExecCommand &&
  8133. !cmd.ignoreContentChange &&
  8134. !me._ignoreContentChange &&
  8135. me._selectionChange()
  8136. return result
  8137. },
  8138. /**
  8139. * 根据传入的command命令,查选编辑器当前的选区,返回命令的状态
  8140. * @method queryCommandState
  8141. * @param { String } cmdName 需要查询的命令名称
  8142. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8143. * @return { Number } number 返回放前命令的状态,返回值三种情况:(-1|0|1)
  8144. * @example
  8145. * ```javascript
  8146. * editor.queryCommandState(cmdName) => (-1|0|1)
  8147. * ```
  8148. * @see COMMAND.LIST
  8149. */
  8150. queryCommandState: function (cmdName) {
  8151. return this._callCmdFn('queryCommandState', arguments)
  8152. },
  8153. /**
  8154. * 根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
  8155. * @method queryCommandValue
  8156. * @param { String } cmdName 需要查询的命令名称
  8157. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8158. * @remind 只有部分插件有此方法
  8159. * @return { * } 返回每个命令特定的当前状态值
  8160. * @grammar editor.queryCommandValue(cmdName) => {*}
  8161. * @see COMMAND.LIST
  8162. */
  8163. queryCommandValue: function (cmdName) {
  8164. return this._callCmdFn('queryCommandValue', arguments)
  8165. },
  8166. /**
  8167. * 检查编辑区域中是否有内容
  8168. * @method hasContents
  8169. * @remind 默认有文本内容,或者有以下节点都不认为是空
  8170. * table,ul,ol,dl,iframe,area,base,col,hr,img,embed,input,link,meta,param
  8171. * @return { Boolean } 检查有内容返回true,否则返回false
  8172. * @example
  8173. * ```javascript
  8174. * editor.hasContents()
  8175. * ```
  8176. */
  8177. /**
  8178. * 检查编辑区域中是否有内容,若包含参数tags中的节点类型,直接返回true
  8179. * @method hasContents
  8180. * @param { Array } tags 传入数组判断时用到的节点类型
  8181. * @return { Boolean } 若文档中包含tags数组里对应的tag,返回true,否则返回false
  8182. * @example
  8183. * ```javascript
  8184. * editor.hasContents(['span']);
  8185. * ```
  8186. */
  8187. hasContents: function (tags) {
  8188. if (tags) {
  8189. for (var i = 0, ci; (ci = tags[i++]); ) {
  8190. if (this.document.getElementsByTagName(ci).length > 0) {
  8191. return true
  8192. }
  8193. }
  8194. }
  8195. if (!domUtils.isEmptyBlock(this.body)) {
  8196. return true
  8197. }
  8198. //随时添加,定义的特殊标签如果存在,不能认为是空
  8199. tags = ['div']
  8200. for (i = 0; (ci = tags[i++]); ) {
  8201. var nodes = domUtils.getElementsByTagName(this.document, ci)
  8202. for (var n = 0, cn; (cn = nodes[n++]); ) {
  8203. if (domUtils.isCustomeNode(cn)) {
  8204. return true
  8205. }
  8206. }
  8207. }
  8208. return false
  8209. },
  8210. /**
  8211. * 重置编辑器,可用来做多个tab使用同一个编辑器实例
  8212. * @method reset
  8213. * @remind 此方法会清空编辑器内容,清空回退列表,会触发reset事件
  8214. * @example
  8215. * ```javascript
  8216. * editor.reset()
  8217. * ```
  8218. */
  8219. reset: function () {
  8220. this.fireEvent('reset')
  8221. },
  8222. /**
  8223. * 设置当前编辑区域可以编辑
  8224. * @method setEnabled
  8225. * @example
  8226. * ```javascript
  8227. * editor.setEnabled()
  8228. * ```
  8229. */
  8230. setEnabled: function () {
  8231. var me = this,
  8232. range
  8233. if (me.body.contentEditable == 'false') {
  8234. me.body.contentEditable = true
  8235. range = me.selection.getRange()
  8236. //有可能内容丢失了
  8237. try {
  8238. range.moveToBookmark(me.lastBk)
  8239. delete me.lastBk
  8240. } catch (e) {
  8241. range.setStartAtFirst(me.body).collapse(true)
  8242. }
  8243. range.select(true)
  8244. if (me.bkqueryCommandState) {
  8245. me.queryCommandState = me.bkqueryCommandState
  8246. delete me.bkqueryCommandState
  8247. }
  8248. if (me.bkqueryCommandValue) {
  8249. me.queryCommandValue = me.bkqueryCommandValue
  8250. delete me.bkqueryCommandValue
  8251. }
  8252. me.fireEvent('selectionchange')
  8253. }
  8254. },
  8255. enable: function () {
  8256. return this.setEnabled()
  8257. },
  8258. /** 设置当前编辑区域不可编辑
  8259. * @method setDisabled
  8260. */
  8261. /** 设置当前编辑区域不可编辑,except中的命令除外
  8262. * @method setDisabled
  8263. * @param { String } except 例外命令的字符串
  8264. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8265. * @example
  8266. * ```javascript
  8267. * editor.setDisabled('bold'); //禁用工具栏中除加粗之外的所有功能
  8268. * ```
  8269. */
  8270. /** 设置当前编辑区域不可编辑,except中的命令除外
  8271. * @method setDisabled
  8272. * @param { Array } except 例外命令的字符串数组,数组中的命令仍然可以执行
  8273. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8274. * @example
  8275. * ```javascript
  8276. * editor.setDisabled(['bold','insertimage']); //禁用工具栏中除加粗和插入图片之外的所有功能
  8277. * ```
  8278. */
  8279. setDisabled: function (except) {
  8280. var me = this
  8281. except = except ? (utils.isArray(except) ? except : [except]) : []
  8282. if (me.body.contentEditable == 'true') {
  8283. if (!me.lastBk) {
  8284. me.lastBk = me.selection.getRange().createBookmark(true)
  8285. }
  8286. me.body.contentEditable = false
  8287. me.bkqueryCommandState = me.queryCommandState
  8288. me.bkqueryCommandValue = me.queryCommandValue
  8289. me.queryCommandState = function (type) {
  8290. if (utils.indexOf(except, type) != -1) {
  8291. return me.bkqueryCommandState.apply(me, arguments)
  8292. }
  8293. return -1
  8294. }
  8295. me.queryCommandValue = function (type) {
  8296. if (utils.indexOf(except, type) != -1) {
  8297. return me.bkqueryCommandValue.apply(me, arguments)
  8298. }
  8299. return null
  8300. }
  8301. me.fireEvent('selectionchange')
  8302. }
  8303. },
  8304. disable: function (except) {
  8305. return this.setDisabled(except)
  8306. },
  8307. /**
  8308. * 设置默认内容
  8309. * @method _setDefaultContent
  8310. * @private
  8311. * @param { String } cont 要存入的内容
  8312. */
  8313. _setDefaultContent: (function () {
  8314. function clear() {
  8315. var me = this
  8316. if (me.document.getElementById('initContent')) {
  8317. me.body.innerHTML = '<p>' + (ie ? '' : '<br/>') + '</p>'
  8318. me.removeListener('firstBeforeExecCommand focus', clear)
  8319. setTimeout(function () {
  8320. me.focus()
  8321. me._selectionChange()
  8322. }, 0)
  8323. }
  8324. }
  8325. return function (cont) {
  8326. var me = this
  8327. me.body.innerHTML = '<p id="initContent">' + cont + '</p>'
  8328. me.addListener('firstBeforeExecCommand focus', clear)
  8329. }
  8330. })(),
  8331. /**
  8332. * 显示编辑器
  8333. * @method setShow
  8334. * @example
  8335. * ```javascript
  8336. * editor.setShow()
  8337. * ```
  8338. */
  8339. setShow: function () {
  8340. var me = this,
  8341. range = me.selection.getRange()
  8342. if (me.container.style.display == 'none') {
  8343. //有可能内容丢失了
  8344. try {
  8345. range.moveToBookmark(me.lastBk)
  8346. delete me.lastBk
  8347. } catch (e) {
  8348. range.setStartAtFirst(me.body).collapse(true)
  8349. }
  8350. //ie下focus实效,所以做了个延迟
  8351. setTimeout(function () {
  8352. range.select(true)
  8353. }, 100)
  8354. me.container.style.display = ''
  8355. }
  8356. },
  8357. show: function () {
  8358. return this.setShow()
  8359. },
  8360. /**
  8361. * 隐藏编辑器
  8362. * @method setHide
  8363. * @example
  8364. * ```javascript
  8365. * editor.setHide()
  8366. * ```
  8367. */
  8368. setHide: function () {
  8369. var me = this
  8370. if (!me.lastBk) {
  8371. me.lastBk = me.selection.getRange().createBookmark(true)
  8372. }
  8373. me.container.style.display = 'none'
  8374. },
  8375. hide: function () {
  8376. return this.setHide()
  8377. },
  8378. /**
  8379. * 根据指定的路径,获取对应的语言资源
  8380. * @method getLang
  8381. * @param { String } path 路径根据的是lang目录下的语言文件的路径结构
  8382. * @return { Object | String } 根据路径返回语言资源的Json格式对象或者语言字符串
  8383. * @example
  8384. * ```javascript
  8385. * editor.getLang('contextMenu.delete'); //如果当前是中文,那返回是的是'删除'
  8386. * ```
  8387. */
  8388. getLang: function (path) {
  8389. // HaoChuan9421
  8390. if (!this.options) {
  8391. return ''
  8392. }
  8393. var lang = UE.I18N[this.options.lang]
  8394. if (!lang) {
  8395. throw Error('not import language file')
  8396. }
  8397. path = (path || '').split('.')
  8398. for (var i = 0, ci; (ci = path[i++]); ) {
  8399. lang = lang[ci]
  8400. if (!lang) break
  8401. }
  8402. return lang
  8403. },
  8404. /**
  8405. * 计算编辑器html内容字符串的长度
  8406. * @method getContentLength
  8407. * @return { Number } 返回计算的长度
  8408. * @example
  8409. * ```javascript
  8410. * //编辑器html内容<p><strong>132</strong></p>
  8411. * editor.getContentLength() //返回27
  8412. * ```
  8413. */
  8414. /**
  8415. * 计算编辑器当前纯文本内容的长度
  8416. * @method getContentLength
  8417. * @param { Boolean } ingoneHtml 传入true时,只按照纯文本来计算
  8418. * @return { Number } 返回计算的长度,内容中有hr/img/iframe标签,长度加1
  8419. * @example
  8420. * ```javascript
  8421. * //编辑器html内容<p><strong>132</strong></p>
  8422. * editor.getContentLength() //返回3
  8423. * ```
  8424. */
  8425. getContentLength: function (ingoneHtml, tagNames) {
  8426. var count = this.getContent(false, false, true).length
  8427. if (ingoneHtml) {
  8428. tagNames = (tagNames || []).concat(['hr', 'img', 'iframe'])
  8429. count = this.getContentTxt().replace(/[\t\r\n]+/g, '').length
  8430. for (var i = 0, ci; (ci = tagNames[i++]); ) {
  8431. count += this.document.getElementsByTagName(ci).length
  8432. }
  8433. }
  8434. return count
  8435. },
  8436. /**
  8437. * 注册输入过滤规则
  8438. * @method addInputRule
  8439. * @param { Function } rule 要添加的过滤规则
  8440. * @example
  8441. * ```javascript
  8442. * editor.addInputRule(function(root){
  8443. * $.each(root.getNodesByTagName('div'),function(i,node){
  8444. * node.tagName="p";
  8445. * });
  8446. * });
  8447. * ```
  8448. */
  8449. addInputRule: function (rule) {
  8450. this.inputRules.push(rule)
  8451. },
  8452. /**
  8453. * 执行注册的过滤规则
  8454. * @method filterInputRule
  8455. * @param { UE.uNode } root 要过滤的uNode节点
  8456. * @remind 执行editor.setContent方法和执行'inserthtml'命令后,会运行该过滤函数
  8457. * @example
  8458. * ```javascript
  8459. * editor.filterInputRule(editor.body);
  8460. * ```
  8461. * @see UE.Editor:addInputRule
  8462. */
  8463. filterInputRule: function (root) {
  8464. for (var i = 0, ci; (ci = this.inputRules[i++]); ) {
  8465. ci.call(this, root)
  8466. }
  8467. },
  8468. /**
  8469. * 注册输出过滤规则
  8470. * @method addOutputRule
  8471. * @param { Function } rule 要添加的过滤规则
  8472. * @example
  8473. * ```javascript
  8474. * editor.addOutputRule(function(root){
  8475. * $.each(root.getNodesByTagName('p'),function(i,node){
  8476. * node.tagName="div";
  8477. * });
  8478. * });
  8479. * ```
  8480. */
  8481. addOutputRule: function (rule) {
  8482. this.outputRules.push(rule)
  8483. },
  8484. /**
  8485. * 根据输出过滤规则,过滤编辑器内容
  8486. * @method filterOutputRule
  8487. * @remind 执行editor.getContent方法的时候,会先运行该过滤函数
  8488. * @param { UE.uNode } root 要过滤的uNode节点
  8489. * @example
  8490. * ```javascript
  8491. * editor.filterOutputRule(editor.body);
  8492. * ```
  8493. * @see UE.Editor:addOutputRule
  8494. */
  8495. filterOutputRule: function (root) {
  8496. for (var i = 0, ci; (ci = this.outputRules[i++]); ) {
  8497. ci.call(this, root)
  8498. }
  8499. },
  8500. /**
  8501. * 根据action名称获取请求的路径
  8502. * @method getActionUrl
  8503. * @remind 假如没有设置serverUrl,会根据imageUrl设置默认的controller路径
  8504. * @param { String } action action名称
  8505. * @example
  8506. * ```javascript
  8507. * editor.getActionUrl('config'); //返回 "/ueditor/php/controller.php?action=config"
  8508. * editor.getActionUrl('image'); //返回 "/ueditor/php/controller.php?action=uplaodimage"
  8509. * editor.getActionUrl('scrawl'); //返回 "/ueditor/php/controller.php?action=uplaodscrawl"
  8510. * editor.getActionUrl('imageManager'); //返回 "/ueditor/php/controller.php?action=listimage"
  8511. * ```
  8512. */
  8513. getActionUrl: function (action) {
  8514. console.log(action, 'action')
  8515. var actionName = this.getOpt(action) || action,
  8516. imageUrl = this.getOpt('imageUrl'),
  8517. serverUrl = this.getOpt('serverUrl')
  8518. if (!serverUrl && imageUrl) {
  8519. console.log(action, 'action1')
  8520. serverUrl = imageUrl.replace(/^(.*[\/]).+([\.].+)$/, '$1controller$2')
  8521. }
  8522. console.log(serverUrl, 'serverUrl')
  8523. // serverUrl =
  8524. // 'http://mentor.site.ximengnaikang.com/ueditor/controller.php'
  8525. // if (action == 'uploadimage') serverUrl = '/daili/base/common/upload'
  8526. if (serverUrl) {
  8527. console.log(action, 'action2')
  8528. serverUrl =
  8529. serverUrl +
  8530. (serverUrl.indexOf('?') == -1 ? '?' : '&') +
  8531. 'action=' +
  8532. (actionName || '')
  8533. console.log(utils.formatUrl(serverUrl), 'action3')
  8534. return utils.formatUrl(serverUrl)
  8535. } else {
  8536. return ''
  8537. }
  8538. },
  8539. }
  8540. utils.inherits(Editor, EventBase)
  8541. })()
  8542. // core/Editor.defaultoptions.js
  8543. //维护编辑器一下默认的不在插件中的配置项
  8544. UE.Editor.defaultOptions = function (editor) {
  8545. var _url = editor.options.UEDITOR_HOME_URL
  8546. return {
  8547. isShow: true,
  8548. initialContent: '',
  8549. initialStyle: '',
  8550. autoClearinitialContent: false,
  8551. iframeCssUrl: _url + 'themes/iframe.css',
  8552. textarea: 'editorValue',
  8553. focus: false,
  8554. focusInEnd: true,
  8555. autoClearEmptyNode: true,
  8556. fullscreen: false,
  8557. readonly: false,
  8558. zIndex: 999,
  8559. imagePopup: true,
  8560. enterTag: 'p',
  8561. customDomain: false,
  8562. lang: 'zh-cn',
  8563. langPath: _url + 'lang/',
  8564. theme: 'default',
  8565. themePath: _url + 'themes/',
  8566. allHtmlEnabled: false,
  8567. scaleEnabled: false,
  8568. tableNativeEditInFF: false,
  8569. autoSyncData: true,
  8570. fileNameFormat: '{time}{rand:6}',
  8571. }
  8572. }
  8573. // core/loadconfig.js
  8574. ;(function () {
  8575. console.log('此处请求')
  8576. UE.Editor.prototype.loadServerConfig = function () {
  8577. var me = this
  8578. let aa = ''
  8579. setTimeout(function () {
  8580. try {
  8581. console.log('ppppp')
  8582. me.options.imageUrl &&
  8583. me.setOpt(
  8584. 'serverUrl',
  8585. me.options.imageUrl.replace(
  8586. /^(.*[\/]).+([\.].+)$/,
  8587. '$1controller$2'
  8588. )
  8589. )
  8590. var configUrl = me.getActionUrl('config'),
  8591. isJsonp = utils.isCrossDomainUrl(configUrl)
  8592. console.log(me.getActionUrl('config'), 'console.log')
  8593. /* 发出ajax请求 */
  8594. me._serverConfigLoaded = false
  8595. console.log(' 发出ajax请求 ')
  8596. aa =
  8597. configUrl &&
  8598. UE.ajax.request(configUrl, {
  8599. method: 'GET',
  8600. dataType: isJsonp ? 'jsonp' : '',
  8601. onComplete: function (x) {
  8602. //提交成功回调
  8603. console.log(x, 'ppppppppppppppppppppppppppppppppppppppp')
  8604. },
  8605. onsuccess: function (r) {
  8606. console.log(r, 'ooooooooooooooooooooo')
  8607. try {
  8608. var config = isJsonp ? r : eval('(' + r.responseText + ')')
  8609. //var config = ieval('(' + r.responseText + ')')
  8610. utils.extend(me.options, config)
  8611. me.fireEvent('serverConfigLoaded')
  8612. me._serverConfigLoaded = true
  8613. } catch (e) {
  8614. console.log(e, '返回错误')
  8615. showErrorMsg(me.getLang('loadconfigFormatError'))
  8616. }
  8617. },
  8618. onerror: function () {
  8619. showErrorMsg(me.getLang('loadconfigHttpError'))
  8620. },
  8621. })
  8622. } catch (e) {
  8623. console.log(aa, 'aa')
  8624. showErrorMsg(me.getLang('loadconfigError'))
  8625. }
  8626. })
  8627. function showErrorMsg(msg) {
  8628. console.log('aa')
  8629. console && console.error(msg)
  8630. //me.fireEvent('showMessage', {
  8631. // 'title': msg,
  8632. // 'type': 'error'
  8633. //});
  8634. }
  8635. }
  8636. UE.Editor.prototype.isServerConfigLoaded = function () {
  8637. var me = this
  8638. return me._serverConfigLoaded || false
  8639. }
  8640. UE.Editor.prototype.afterConfigReady = function (handler) {
  8641. if (!handler || !utils.isFunction(handler)) return
  8642. var me = this
  8643. var readyHandler = function () {
  8644. handler.apply(me, arguments)
  8645. me.removeListener('serverConfigLoaded', readyHandler)
  8646. }
  8647. if (me.isServerConfigLoaded()) {
  8648. handler.call(me, 'serverConfigLoaded')
  8649. } else {
  8650. me.addListener('serverConfigLoaded', readyHandler)
  8651. }
  8652. }
  8653. })()
  8654. // core/ajax.js
  8655. /**
  8656. * @file
  8657. * @module UE.ajax
  8658. * @since 1.2.6.1
  8659. */
  8660. /**
  8661. * 提供对ajax请求的支持
  8662. * @module UE.ajax
  8663. */
  8664. UE.ajax = (function () {
  8665. //创建一个ajaxRequest对象
  8666. var fnStr = 'XMLHttpRequest()'
  8667. try {
  8668. new ActiveXObject('Msxml2.XMLHTTP')
  8669. fnStr = "ActiveXObject('Msxml2.XMLHTTP')"
  8670. } catch (e) {
  8671. try {
  8672. new ActiveXObject('Microsoft.XMLHTTP')
  8673. fnStr = "ActiveXObject('Microsoft.XMLHTTP')"
  8674. } catch (e) {}
  8675. }
  8676. var creatAjaxRequest = new Function('return new ' + fnStr)
  8677. /**
  8678. * 将json参数转化成适合ajax提交的参数列表
  8679. * @param json
  8680. */
  8681. function json2str(json) {
  8682. var strArr = []
  8683. for (var i in json) {
  8684. //忽略默认的几个参数
  8685. if (
  8686. i == 'method' ||
  8687. i == 'timeout' ||
  8688. i == 'async' ||
  8689. i == 'dataType' ||
  8690. i == 'callback'
  8691. )
  8692. continue
  8693. //忽略控制
  8694. if (json[i] == undefined || json[i] == null) continue
  8695. //传递过来的对象和函数不在提交之列
  8696. if (
  8697. !(
  8698. (typeof json[i]).toLowerCase() == 'function' ||
  8699. (typeof json[i]).toLowerCase() == 'object'
  8700. )
  8701. ) {
  8702. strArr.push(encodeURIComponent(i) + '=' + encodeURIComponent(json[i]))
  8703. } else if (utils.isArray(json[i])) {
  8704. //支持传数组内容
  8705. for (var j = 0; j < json[i].length; j++) {
  8706. strArr.push(
  8707. encodeURIComponent(i) + '[]=' + encodeURIComponent(json[i][j])
  8708. )
  8709. }
  8710. }
  8711. }
  8712. return strArr.join('&')
  8713. }
  8714. function doAjax(url, ajaxOptions) {
  8715. var xhr = creatAjaxRequest(),
  8716. //是否超时
  8717. timeIsOut = false,
  8718. //默认参数
  8719. defaultAjaxOptions = {
  8720. method: 'POST',
  8721. timeout: 5000,
  8722. async: true,
  8723. data: {}, //需要传递对象的话只能覆盖
  8724. onsuccess: function () {},
  8725. onerror: function () {},
  8726. }
  8727. if (typeof url === 'object') {
  8728. ajaxOptions = url
  8729. url = ajaxOptions.url
  8730. }
  8731. if (!xhr || !url) return
  8732. var ajaxOpts = ajaxOptions
  8733. ? utils.extend(defaultAjaxOptions, ajaxOptions)
  8734. : defaultAjaxOptions
  8735. var submitStr = json2str(ajaxOpts) // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8736. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8737. if (!utils.isEmptyObject(ajaxOpts.data)) {
  8738. submitStr += (submitStr ? '&' : '') + json2str(ajaxOpts.data)
  8739. }
  8740. //超时检测
  8741. var timerID = setTimeout(function () {
  8742. if (xhr.readyState != 4) {
  8743. timeIsOut = true
  8744. xhr.abort()
  8745. clearTimeout(timerID)
  8746. }
  8747. }, ajaxOpts.timeout)
  8748. var method = ajaxOpts.method.toUpperCase()
  8749. var str =
  8750. url +
  8751. (url.indexOf('?') == -1 ? '?' : '&') +
  8752. (method == 'POST' ? '' : submitStr + '&noCache=' + +new Date())
  8753. xhr.open(method, str, ajaxOpts.async)
  8754. xhr.onreadystatechange = function () {
  8755. if (xhr.readyState == 4) {
  8756. if (!timeIsOut && xhr.status == 200) {
  8757. ajaxOpts.onsuccess(xhr)
  8758. } else {
  8759. ajaxOpts.onerror(xhr)
  8760. }
  8761. }
  8762. }
  8763. if (method == 'POST') {
  8764. xhr.setRequestHeader(
  8765. 'Content-Type',
  8766. 'application/x-www-form-urlencoded'
  8767. )
  8768. xhr.send(submitStr)
  8769. } else {
  8770. xhr.send(null)
  8771. }
  8772. }
  8773. function doJsonp(url, opts) {
  8774. var successhandler = opts.onsuccess || function () {},
  8775. scr = document.createElement('SCRIPT'),
  8776. options = opts || {},
  8777. charset = options['charset'],
  8778. callbackField = options['jsonp'] || 'callback',
  8779. callbackFnName,
  8780. timeOut = options['timeOut'] || 0,
  8781. timer,
  8782. reg = new RegExp('(\\?|&)' + callbackField + '=([^&]*)'),
  8783. matches
  8784. if (utils.isFunction(successhandler)) {
  8785. callbackFnName =
  8786. 'bd__editor__' + Math.floor(Math.random() * 2147483648).toString(36)
  8787. window[callbackFnName] = getCallBack(0)
  8788. } else if (utils.isString(successhandler)) {
  8789. callbackFnName = successhandler
  8790. } else {
  8791. if ((matches = reg.exec(url))) {
  8792. callbackFnName = matches[2]
  8793. }
  8794. }
  8795. url = url.replace(reg, '\x241' + callbackField + '=' + callbackFnName)
  8796. if (url.search(reg) < 0) {
  8797. url +=
  8798. (url.indexOf('?') < 0 ? '?' : '&') +
  8799. callbackField +
  8800. '=' +
  8801. callbackFnName
  8802. }
  8803. var queryStr = json2str(opts) // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8804. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8805. if (!utils.isEmptyObject(opts.data)) {
  8806. queryStr += (queryStr ? '&' : '') + json2str(opts.data)
  8807. }
  8808. if (queryStr) {
  8809. url = url.replace(/\?/, '?' + queryStr + '&')
  8810. }
  8811. scr.onerror = getCallBack(1)
  8812. if (timeOut) {
  8813. timer = setTimeout(getCallBack(1), timeOut)
  8814. }
  8815. createScriptTag(scr, url, charset)
  8816. function createScriptTag(scr, url, charset) {
  8817. scr.setAttribute('type', 'text/javascript')
  8818. scr.setAttribute('defer', 'defer')
  8819. charset && scr.setAttribute('charset', charset)
  8820. scr.setAttribute('src', url)
  8821. document.getElementsByTagName('head')[0].appendChild(scr)
  8822. }
  8823. function getCallBack(onTimeOut) {
  8824. return function () {
  8825. try {
  8826. if (onTimeOut) {
  8827. options.onerror && options.onerror()
  8828. } else {
  8829. try {
  8830. clearTimeout(timer)
  8831. successhandler.apply(window, arguments)
  8832. } catch (e) {}
  8833. }
  8834. } catch (exception) {
  8835. options.onerror && options.onerror.call(window, exception)
  8836. } finally {
  8837. options.oncomplete && options.oncomplete.apply(window, arguments)
  8838. scr.parentNode && scr.parentNode.removeChild(scr)
  8839. window[callbackFnName] = null
  8840. try {
  8841. delete window[callbackFnName]
  8842. } catch (e) {}
  8843. }
  8844. }
  8845. }
  8846. }
  8847. return {
  8848. /**
  8849. * 根据给定的参数项,向指定的url发起一个ajax请求。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8850. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调
  8851. * @method request
  8852. * @param { URLString } url ajax请求的url地址
  8853. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8854. * @example
  8855. * ```javascript
  8856. * //向sayhello.php发起一个异步的Ajax GET请求, 请求超时时间为10s, 请求完成后执行相应的回调。
  8857. * UE.ajax.requeset( 'sayhello.php', {
  8858. *
  8859. * //请求方法。可选值: 'GET', 'POST',默认值是'POST'
  8860. * method: 'GET',
  8861. *
  8862. * //超时时间。 默认为5000, 单位是ms
  8863. * timeout: 10000,
  8864. *
  8865. * //是否是异步请求。 true为异步请求, false为同步请求
  8866. * async: true,
  8867. *
  8868. * //请求携带的数据。如果请求为GET请求, data会经过stringify后附加到请求url之后。
  8869. * data: {
  8870. * name: 'ueditor'
  8871. * },
  8872. *
  8873. * //请求成功后的回调, 该回调接受当前的XMLHttpRequest对象作为参数。
  8874. * onsuccess: function ( xhr ) {
  8875. * console.log( xhr.responseText );
  8876. * },
  8877. *
  8878. * //请求失败或者超时后的回调。
  8879. * onerror: function ( xhr ) {
  8880. * alert( 'Ajax请求失败' );
  8881. * }
  8882. *
  8883. * } );
  8884. * ```
  8885. */
  8886. /**
  8887. * 根据给定的参数项发起一个ajax请求, 参数项里必须包含一个url地址。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8888. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调。
  8889. * @method request
  8890. * @warning 如果在参数项里未提供一个key为“url”的地址值,则该请求将直接退出。
  8891. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8892. * @example
  8893. * ```javascript
  8894. *
  8895. * //向sayhello.php发起一个异步的Ajax POST请求, 请求超时时间为5s, 请求完成后不执行任何回调。
  8896. * UE.ajax.requeset( 'sayhello.php', {
  8897. *
  8898. * //请求的地址, 该项是必须的。
  8899. * url: 'sayhello.php'
  8900. *
  8901. * } );
  8902. * ```
  8903. */
  8904. request: function (url, opts) {
  8905. if (opts && opts.dataType == 'jsonp') {
  8906. doJsonp(url, opts)
  8907. } else {
  8908. doAjax(url, opts)
  8909. }
  8910. },
  8911. getJSONP: function (url, data, fn) {
  8912. var opts = {
  8913. data: data,
  8914. oncomplete: fn,
  8915. }
  8916. doJsonp(url, opts)
  8917. },
  8918. }
  8919. })()
  8920. // core/filterword.js
  8921. /**
  8922. * UE过滤word的静态方法
  8923. * @file
  8924. */
  8925. /**
  8926. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8927. * @module UE
  8928. */
  8929. /**
  8930. * 根据传入html字符串过滤word
  8931. * @module UE
  8932. * @since 1.2.6.1
  8933. * @method filterWord
  8934. * @param { String } html html字符串
  8935. * @return { String } 已过滤后的结果字符串
  8936. * @example
  8937. * ```javascript
  8938. * UE.filterWord(html);
  8939. * ```
  8940. */
  8941. var filterWord = (UE.filterWord = (function () {
  8942. //是否是word过来的内容
  8943. function isWordDocument(str) {
  8944. return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<(v|o):|lang=)/gi.test(
  8945. str
  8946. )
  8947. }
  8948. //去掉小数
  8949. function transUnit(v) {
  8950. v = v.replace(/[\d.]+\w+/g, function (m) {
  8951. return utils.transUnitToPx(m)
  8952. })
  8953. return v
  8954. }
  8955. function filterPasteWord(str) {
  8956. return (
  8957. str
  8958. .replace(/[\t\r\n]+/g, ' ')
  8959. .replace(/<!--[\s\S]*?-->/gi, '')
  8960. //转换图片
  8961. .replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi, function (str) {
  8962. //opera能自己解析出image所这里直接返回空
  8963. if (browser.opera) {
  8964. return ''
  8965. }
  8966. try {
  8967. //有可能是bitmap占为图,无用,直接过滤掉,主要体现在粘贴excel表格中
  8968. if (/Bitmap/i.test(str)) {
  8969. return ''
  8970. }
  8971. var width = str.match(/width:([ \d.]*p[tx])/i)[1],
  8972. height = str.match(/height:([ \d.]*p[tx])/i)[1],
  8973. src = str.match(/src=\s*"([^"]*)"/i)[1]
  8974. return (
  8975. '<img width="' +
  8976. transUnit(width) +
  8977. '" height="' +
  8978. transUnit(height) +
  8979. '" src="' +
  8980. src +
  8981. '" />'
  8982. )
  8983. } catch (e) {
  8984. return ''
  8985. }
  8986. })
  8987. //针对wps添加的多余标签处理
  8988. .replace(/<\/?div[^>]*>/g, '')
  8989. //去掉多余的属性
  8990. .replace(/v:\w+=(["']?)[^'"]+\1/g, '')
  8991. .replace(
  8992. /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi,
  8993. ''
  8994. )
  8995. .replace(
  8996. /<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi,
  8997. '<p><strong>$1</strong></p>'
  8998. )
  8999. //去掉多余的属性
  9000. .replace(
  9001. /\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/gi,
  9002. function (str, name, marks, val) {
  9003. //保留list的标示
  9004. return name == 'class' && val == 'MsoListParagraph' ? str : ''
  9005. }
  9006. )
  9007. //清除多余的font/span不能匹配&nbsp;有可能是空格
  9008. .replace(/<(font|span)[^>]*>(\s*)<\/\1>/gi, function (a, b, c) {
  9009. return c.replace(/[\t\r\n ]+/g, ' ')
  9010. })
  9011. //处理style的问题
  9012. .replace(
  9013. /(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi,
  9014. function (str, tag, tmp, style) {
  9015. var n = [],
  9016. s = style
  9017. .replace(/^\s+|\s+$/, '')
  9018. .replace(/&#39;/g, "'")
  9019. .replace(/&quot;/gi, "'")
  9020. .replace(/[\d.]+(cm|pt)/g, function (str) {
  9021. return utils.transUnitToPx(str)
  9022. })
  9023. .split(/;\s*/g)
  9024. for (var i = 0, v; (v = s[i]); i++) {
  9025. var name,
  9026. value,
  9027. parts = v.split(':')
  9028. if (parts.length == 2) {
  9029. name = parts[0].toLowerCase()
  9030. value = parts[1].toLowerCase()
  9031. if (
  9032. (/^(background)\w*/.test(name) &&
  9033. value.replace(/(initial|\s)/g, '').length == 0) ||
  9034. (/^(margin)\w*/.test(name) && /^0\w+$/.test(value))
  9035. ) {
  9036. continue
  9037. }
  9038. switch (name) {
  9039. case 'mso-padding-alt':
  9040. case 'mso-padding-top-alt':
  9041. case 'mso-padding-right-alt':
  9042. case 'mso-padding-bottom-alt':
  9043. case 'mso-padding-left-alt':
  9044. case 'mso-margin-alt':
  9045. case 'mso-margin-top-alt':
  9046. case 'mso-margin-right-alt':
  9047. case 'mso-margin-bottom-alt':
  9048. case 'mso-margin-left-alt':
  9049. //ie下会出现挤到一起的情况
  9050. //case "mso-table-layout-alt":
  9051. case 'mso-height':
  9052. case 'mso-width':
  9053. case 'mso-vertical-align-alt':
  9054. //trace:1819 ff下会解析出padding在table上
  9055. if (!/<table/.test(tag))
  9056. n[i] =
  9057. name.replace(/^mso-|-alt$/g, '') +
  9058. ':' +
  9059. transUnit(value)
  9060. continue
  9061. case 'horiz-align':
  9062. n[i] = 'text-align:' + value
  9063. continue
  9064. case 'vert-align':
  9065. n[i] = 'vertical-align:' + value
  9066. continue
  9067. case 'font-color':
  9068. case 'mso-foreground':
  9069. n[i] = 'color:' + value
  9070. continue
  9071. case 'mso-background':
  9072. case 'mso-highlight':
  9073. n[i] = 'background:' + value
  9074. continue
  9075. case 'mso-default-height':
  9076. n[i] = 'min-height:' + transUnit(value)
  9077. continue
  9078. case 'mso-default-width':
  9079. n[i] = 'min-width:' + transUnit(value)
  9080. continue
  9081. case 'mso-padding-between-alt':
  9082. n[i] =
  9083. 'border-collapse:separate;border-spacing:' +
  9084. transUnit(value)
  9085. continue
  9086. case 'text-line-through':
  9087. if (value == 'single' || value == 'double') {
  9088. n[i] = 'text-decoration:line-through'
  9089. }
  9090. continue
  9091. case 'mso-zero-height':
  9092. if (value == 'yes') {
  9093. n[i] = 'display:none'
  9094. }
  9095. continue
  9096. // case 'background':
  9097. // break;
  9098. case 'margin':
  9099. if (!/[1-9]/.test(value)) {
  9100. continue
  9101. }
  9102. }
  9103. if (
  9104. /^(mso|column|font-emph|lang|layout|line-break|list-image|nav|panose|punct|row|ruby|sep|size|src|tab-|table-border|text-(?:decor|trans)|top-bar|version|vnd|word-break)/.test(
  9105. name
  9106. ) ||
  9107. (/text\-indent|padding|margin/.test(name) &&
  9108. /\-[\d.]+/.test(value))
  9109. ) {
  9110. continue
  9111. }
  9112. n[i] = name + ':' + parts[1]
  9113. }
  9114. }
  9115. return (
  9116. tag +
  9117. (n.length
  9118. ? ' style="' + n.join(';').replace(/;{2,}/g, ';') + '"'
  9119. : '')
  9120. )
  9121. }
  9122. )
  9123. )
  9124. }
  9125. return function (html) {
  9126. return isWordDocument(html) ? filterPasteWord(html) : html
  9127. }
  9128. })())
  9129. // core/node.js
  9130. /**
  9131. * 编辑器模拟的节点类
  9132. * @file
  9133. * @module UE
  9134. * @class uNode
  9135. * @since 1.2.6.1
  9136. */
  9137. /**
  9138. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9139. * @unfile
  9140. * @module UE
  9141. */
  9142. ;(function () {
  9143. /**
  9144. * 编辑器模拟的节点类
  9145. * @unfile
  9146. * @module UE
  9147. * @class uNode
  9148. */
  9149. /**
  9150. * 通过一个键值对,创建一个uNode对象
  9151. * @constructor
  9152. * @param { Object } attr 传入要创建的uNode的初始属性
  9153. * @example
  9154. * ```javascript
  9155. * var node = new uNode({
  9156. * type:'element',
  9157. * tagName:'span',
  9158. * attrs:{style:'font-size:14px;'}
  9159. * }
  9160. * ```
  9161. */
  9162. var uNode = (UE.uNode = function (obj) {
  9163. this.type = obj.type
  9164. this.data = obj.data
  9165. this.tagName = obj.tagName
  9166. this.parentNode = obj.parentNode
  9167. this.attrs = obj.attrs || {}
  9168. this.children = obj.children
  9169. })
  9170. var notTransAttrs = {
  9171. href: 1,
  9172. src: 1,
  9173. _src: 1,
  9174. _href: 1,
  9175. cdata_data: 1,
  9176. }
  9177. var notTransTagName = {
  9178. style: 1,
  9179. script: 1,
  9180. }
  9181. var indentChar = ' ',
  9182. breakChar = '\n'
  9183. function insertLine(arr, current, begin) {
  9184. arr.push(breakChar)
  9185. return current + (begin ? 1 : -1)
  9186. }
  9187. function insertIndent(arr, current) {
  9188. //插入缩进
  9189. for (var i = 0; i < current; i++) {
  9190. arr.push(indentChar)
  9191. }
  9192. }
  9193. //创建uNode的静态方法
  9194. //支持标签和html
  9195. uNode.createElement = function (html) {
  9196. if (/[<>]/.test(html)) {
  9197. return UE.htmlparser(html).children[0]
  9198. } else {
  9199. return new uNode({
  9200. type: 'element',
  9201. children: [],
  9202. tagName: html,
  9203. })
  9204. }
  9205. }
  9206. uNode.createText = function (data, noTrans) {
  9207. return new UE.uNode({
  9208. type: 'text',
  9209. data: noTrans ? data : utils.unhtml(data || ''),
  9210. })
  9211. }
  9212. function nodeToHtml(node, arr, formatter, current) {
  9213. switch (node.type) {
  9214. case 'root':
  9215. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9216. //插入新行
  9217. if (
  9218. formatter &&
  9219. ci.type == 'element' &&
  9220. !dtd.$inlineWithA[ci.tagName] &&
  9221. i > 1
  9222. ) {
  9223. insertLine(arr, current, true)
  9224. insertIndent(arr, current)
  9225. }
  9226. nodeToHtml(ci, arr, formatter, current)
  9227. }
  9228. break
  9229. case 'text':
  9230. isText(node, arr)
  9231. break
  9232. case 'element':
  9233. isElement(node, arr, formatter, current)
  9234. break
  9235. case 'comment':
  9236. isComment(node, arr, formatter)
  9237. }
  9238. return arr
  9239. }
  9240. function isText(node, arr) {
  9241. if (node.parentNode.tagName == 'pre') {
  9242. //源码模式下输入html标签,不能做转换处理,直接输出
  9243. arr.push(node.data)
  9244. } else {
  9245. arr.push(
  9246. notTransTagName[node.parentNode.tagName]
  9247. ? utils.html(node.data)
  9248. : node.data.replace(/[ ]{2}/g, ' &nbsp;')
  9249. )
  9250. }
  9251. }
  9252. function isElement(node, arr, formatter, current) {
  9253. var attrhtml = ''
  9254. if (node.attrs) {
  9255. attrhtml = []
  9256. var attrs = node.attrs
  9257. for (var a in attrs) {
  9258. //这里就针对
  9259. //<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>
  9260. //这里边的\"做转换,要不用innerHTML直接被截断了,属性src
  9261. //有可能做的不够
  9262. attrhtml.push(
  9263. a +
  9264. (attrs[a] !== undefined
  9265. ? '="' +
  9266. (notTransAttrs[a]
  9267. ? utils.html(attrs[a]).replace(/["]/g, function (a) {
  9268. return '&quot;'
  9269. })
  9270. : utils.unhtml(attrs[a])) +
  9271. '"'
  9272. : '')
  9273. )
  9274. }
  9275. attrhtml = attrhtml.join(' ')
  9276. }
  9277. arr.push(
  9278. '<' +
  9279. node.tagName +
  9280. (attrhtml ? ' ' + attrhtml : '') +
  9281. (dtd.$empty[node.tagName] ? '/' : '') +
  9282. '>'
  9283. )
  9284. //插入新行
  9285. if (
  9286. formatter &&
  9287. !dtd.$inlineWithA[node.tagName] &&
  9288. node.tagName != 'pre'
  9289. ) {
  9290. if (node.children && node.children.length) {
  9291. current = insertLine(arr, current, true)
  9292. insertIndent(arr, current)
  9293. }
  9294. }
  9295. if (node.children && node.children.length) {
  9296. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9297. if (
  9298. formatter &&
  9299. ci.type == 'element' &&
  9300. !dtd.$inlineWithA[ci.tagName] &&
  9301. i > 1
  9302. ) {
  9303. insertLine(arr, current)
  9304. insertIndent(arr, current)
  9305. }
  9306. nodeToHtml(ci, arr, formatter, current)
  9307. }
  9308. }
  9309. if (!dtd.$empty[node.tagName]) {
  9310. if (
  9311. formatter &&
  9312. !dtd.$inlineWithA[node.tagName] &&
  9313. node.tagName != 'pre'
  9314. ) {
  9315. if (node.children && node.children.length) {
  9316. current = insertLine(arr, current)
  9317. insertIndent(arr, current)
  9318. }
  9319. }
  9320. arr.push('</' + node.tagName + '>')
  9321. }
  9322. }
  9323. function isComment(node, arr) {
  9324. arr.push('<!--' + node.data + '-->')
  9325. }
  9326. function getNodeById(root, id) {
  9327. var node
  9328. if (root.type == 'element' && root.getAttr('id') == id) {
  9329. return root
  9330. }
  9331. if (root.children && root.children.length) {
  9332. for (var i = 0, ci; (ci = root.children[i++]); ) {
  9333. if ((node = getNodeById(ci, id))) {
  9334. return node
  9335. }
  9336. }
  9337. }
  9338. }
  9339. function getNodesByTagName(node, tagName, arr) {
  9340. if (node.type == 'element' && node.tagName == tagName) {
  9341. arr.push(node)
  9342. }
  9343. if (node.children && node.children.length) {
  9344. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9345. getNodesByTagName(ci, tagName, arr)
  9346. }
  9347. }
  9348. }
  9349. function nodeTraversal(root, fn) {
  9350. if (root.children && root.children.length) {
  9351. for (var i = 0, ci; (ci = root.children[i]); ) {
  9352. nodeTraversal(ci, fn)
  9353. //ci被替换的情况,这里就不再走 fn了
  9354. if (ci.parentNode) {
  9355. if (ci.children && ci.children.length) {
  9356. fn(ci)
  9357. }
  9358. if (ci.parentNode) i++
  9359. }
  9360. }
  9361. } else {
  9362. fn(root)
  9363. }
  9364. }
  9365. uNode.prototype = {
  9366. /**
  9367. * 当前节点对象,转换成html文本
  9368. * @method toHtml
  9369. * @return { String } 返回转换后的html字符串
  9370. * @example
  9371. * ```javascript
  9372. * node.toHtml();
  9373. * ```
  9374. */
  9375. /**
  9376. * 当前节点对象,转换成html文本
  9377. * @method toHtml
  9378. * @param { Boolean } formatter 是否格式化返回值
  9379. * @return { String } 返回转换后的html字符串
  9380. * @example
  9381. * ```javascript
  9382. * node.toHtml( true );
  9383. * ```
  9384. */
  9385. toHtml: function (formatter) {
  9386. var arr = []
  9387. nodeToHtml(this, arr, formatter, 0)
  9388. return arr.join('')
  9389. },
  9390. /**
  9391. * 获取节点的html内容
  9392. * @method innerHTML
  9393. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9394. * @return { String } 返回节点的html内容
  9395. * @example
  9396. * ```javascript
  9397. * var htmlstr = node.innerHTML();
  9398. * ```
  9399. */
  9400. /**
  9401. * 设置节点的html内容
  9402. * @method innerHTML
  9403. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9404. * @param { String } htmlstr 传入要设置的html内容
  9405. * @return { UE.uNode } 返回节点本身
  9406. * @example
  9407. * ```javascript
  9408. * node.innerHTML('<span>text</span>');
  9409. * ```
  9410. */
  9411. innerHTML: function (htmlstr) {
  9412. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  9413. return this
  9414. }
  9415. if (utils.isString(htmlstr)) {
  9416. if (this.children) {
  9417. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9418. ci.parentNode = null
  9419. }
  9420. }
  9421. this.children = []
  9422. var tmpRoot = UE.htmlparser(htmlstr)
  9423. for (var i = 0, ci; (ci = tmpRoot.children[i++]); ) {
  9424. this.children.push(ci)
  9425. ci.parentNode = this
  9426. }
  9427. return this
  9428. } else {
  9429. var tmpRoot = new UE.uNode({
  9430. type: 'root',
  9431. children: this.children,
  9432. })
  9433. return tmpRoot.toHtml()
  9434. }
  9435. },
  9436. /**
  9437. * 获取节点的纯文本内容
  9438. * @method innerText
  9439. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9440. * @return { String } 返回节点的存文本内容
  9441. * @example
  9442. * ```javascript
  9443. * var textStr = node.innerText();
  9444. * ```
  9445. */
  9446. /**
  9447. * 设置节点的纯文本内容
  9448. * @method innerText
  9449. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9450. * @param { String } textStr 传入要设置的文本内容
  9451. * @return { UE.uNode } 返回节点本身
  9452. * @example
  9453. * ```javascript
  9454. * node.innerText('<span>text</span>');
  9455. * ```
  9456. */
  9457. innerText: function (textStr, noTrans) {
  9458. if (this.type != 'element' || dtd.$empty[this.tagName]) {
  9459. return this
  9460. }
  9461. if (textStr) {
  9462. if (this.children) {
  9463. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9464. ci.parentNode = null
  9465. }
  9466. }
  9467. this.children = []
  9468. this.appendChild(uNode.createText(textStr, noTrans))
  9469. return this
  9470. } else {
  9471. return this.toHtml().replace(/<[^>]+>/g, '')
  9472. }
  9473. },
  9474. /**
  9475. * 获取当前对象的data属性
  9476. * @method getData
  9477. * @return { Object } 若节点的type值是elemenet,返回空字符串,否则返回节点的data属性
  9478. * @example
  9479. * ```javascript
  9480. * node.getData();
  9481. * ```
  9482. */
  9483. getData: function () {
  9484. if (this.type == 'element') return ''
  9485. return this.data
  9486. },
  9487. /**
  9488. * 获取当前节点下的第一个子节点
  9489. * @method firstChild
  9490. * @return { UE.uNode } 返回第一个子节点
  9491. * @example
  9492. * ```javascript
  9493. * node.firstChild(); //返回第一个子节点
  9494. * ```
  9495. */
  9496. firstChild: function () {
  9497. // if (this.type != 'element' || dtd.$empty[this.tagName]) {
  9498. // return this;
  9499. // }
  9500. return this.children ? this.children[0] : null
  9501. },
  9502. /**
  9503. * 获取当前节点下的最后一个子节点
  9504. * @method lastChild
  9505. * @return { UE.uNode } 返回最后一个子节点
  9506. * @example
  9507. * ```javascript
  9508. * node.lastChild(); //返回最后一个子节点
  9509. * ```
  9510. */
  9511. lastChild: function () {
  9512. // if (this.type != 'element' || dtd.$empty[this.tagName] ) {
  9513. // return this;
  9514. // }
  9515. return this.children ? this.children[this.children.length - 1] : null
  9516. },
  9517. /**
  9518. * 获取和当前节点有相同父亲节点的前一个节点
  9519. * @method previousSibling
  9520. * @return { UE.uNode } 返回前一个节点
  9521. * @example
  9522. * ```javascript
  9523. * node.children[2].previousSibling(); //返回子节点node.children[1]
  9524. * ```
  9525. */
  9526. previousSibling: function () {
  9527. var parent = this.parentNode
  9528. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9529. if (ci === this) {
  9530. return i == 0 ? null : parent.children[i - 1]
  9531. }
  9532. }
  9533. },
  9534. /**
  9535. * 获取和当前节点有相同父亲节点的后一个节点
  9536. * @method nextSibling
  9537. * @return { UE.uNode } 返回后一个节点,找不到返回null
  9538. * @example
  9539. * ```javascript
  9540. * node.children[2].nextSibling(); //如果有,返回子节点node.children[3]
  9541. * ```
  9542. */
  9543. nextSibling: function () {
  9544. var parent = this.parentNode
  9545. for (var i = 0, ci; (ci = parent.children[i++]); ) {
  9546. if (ci === this) {
  9547. return parent.children[i]
  9548. }
  9549. }
  9550. },
  9551. /**
  9552. * 用新的节点替换当前节点
  9553. * @method replaceChild
  9554. * @param { UE.uNode } target 要替换成该节点参数
  9555. * @param { UE.uNode } source 要被替换掉的节点
  9556. * @return { UE.uNode } 返回替换之后的节点对象
  9557. * @example
  9558. * ```javascript
  9559. * node.replaceChild(newNode, childNode); //用newNode替换childNode,childNode是node的子节点
  9560. * ```
  9561. */
  9562. replaceChild: function (target, source) {
  9563. if (this.children) {
  9564. if (target.parentNode) {
  9565. target.parentNode.removeChild(target)
  9566. }
  9567. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9568. if (ci === source) {
  9569. this.children.splice(i, 1, target)
  9570. source.parentNode = null
  9571. target.parentNode = this
  9572. return target
  9573. }
  9574. }
  9575. }
  9576. },
  9577. /**
  9578. * 在节点的子节点列表最后位置插入一个节点
  9579. * @method appendChild
  9580. * @param { UE.uNode } node 要插入的节点
  9581. * @return { UE.uNode } 返回刚插入的子节点
  9582. * @example
  9583. * ```javascript
  9584. * node.appendChild( newNode ); //在node内插入子节点newNode
  9585. * ```
  9586. */
  9587. appendChild: function (node) {
  9588. if (
  9589. this.type == 'root' ||
  9590. (this.type == 'element' && !dtd.$empty[this.tagName])
  9591. ) {
  9592. if (!this.children) {
  9593. this.children = []
  9594. }
  9595. if (node.parentNode) {
  9596. node.parentNode.removeChild(node)
  9597. }
  9598. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9599. if (ci === node) {
  9600. this.children.splice(i, 1)
  9601. break
  9602. }
  9603. }
  9604. this.children.push(node)
  9605. node.parentNode = this
  9606. return node
  9607. }
  9608. },
  9609. /**
  9610. * 在传入节点的前面插入一个节点
  9611. * @method insertBefore
  9612. * @param { UE.uNode } target 要插入的节点
  9613. * @param { UE.uNode } source 在该参数节点前面插入
  9614. * @return { UE.uNode } 返回刚插入的子节点
  9615. * @example
  9616. * ```javascript
  9617. * node.parentNode.insertBefore(newNode, node); //在node节点后面插入newNode
  9618. * ```
  9619. */
  9620. insertBefore: function (target, source) {
  9621. if (this.children) {
  9622. if (target.parentNode) {
  9623. target.parentNode.removeChild(target)
  9624. }
  9625. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9626. if (ci === source) {
  9627. this.children.splice(i, 0, target)
  9628. target.parentNode = this
  9629. return target
  9630. }
  9631. }
  9632. }
  9633. },
  9634. /**
  9635. * 在传入节点的后面插入一个节点
  9636. * @method insertAfter
  9637. * @param { UE.uNode } target 要插入的节点
  9638. * @param { UE.uNode } source 在该参数节点后面插入
  9639. * @return { UE.uNode } 返回刚插入的子节点
  9640. * @example
  9641. * ```javascript
  9642. * node.parentNode.insertAfter(newNode, node); //在node节点后面插入newNode
  9643. * ```
  9644. */
  9645. insertAfter: function (target, source) {
  9646. if (this.children) {
  9647. if (target.parentNode) {
  9648. target.parentNode.removeChild(target)
  9649. }
  9650. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9651. if (ci === source) {
  9652. this.children.splice(i + 1, 0, target)
  9653. target.parentNode = this
  9654. return target
  9655. }
  9656. }
  9657. }
  9658. },
  9659. /**
  9660. * 从当前节点的子节点列表中,移除节点
  9661. * @method removeChild
  9662. * @param { UE.uNode } node 要移除的节点引用
  9663. * @param { Boolean } keepChildren 是否保留移除节点的子节点,若传入true,自动把移除节点的子节点插入到移除的位置
  9664. * @return { * } 返回刚移除的子节点
  9665. * @example
  9666. * ```javascript
  9667. * node.removeChild(childNode,true); //在node的子节点列表中移除child节点,并且吧child的子节点插入到移除的位置
  9668. * ```
  9669. */
  9670. removeChild: function (node, keepChildren) {
  9671. if (this.children) {
  9672. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9673. if (ci === node) {
  9674. this.children.splice(i, 1)
  9675. ci.parentNode = null
  9676. if (keepChildren && ci.children && ci.children.length) {
  9677. for (var j = 0, cj; (cj = ci.children[j]); j++) {
  9678. this.children.splice(i + j, 0, cj)
  9679. cj.parentNode = this
  9680. }
  9681. }
  9682. return ci
  9683. }
  9684. }
  9685. }
  9686. },
  9687. /**
  9688. * 获取当前节点所代表的元素属性,即获取attrs对象下的属性值
  9689. * @method getAttr
  9690. * @param { String } attrName 要获取的属性名称
  9691. * @return { * } 返回attrs对象下的属性值
  9692. * @example
  9693. * ```javascript
  9694. * node.getAttr('title');
  9695. * ```
  9696. */
  9697. getAttr: function (attrName) {
  9698. return this.attrs && this.attrs[attrName.toLowerCase()]
  9699. },
  9700. /**
  9701. * 设置当前节点所代表的元素属性,即设置attrs对象下的属性值
  9702. * @method setAttr
  9703. * @param { String } attrName 要设置的属性名称
  9704. * @param { * } attrVal 要设置的属性值,类型视设置的属性而定
  9705. * @return { * } 返回attrs对象下的属性值
  9706. * @example
  9707. * ```javascript
  9708. * node.setAttr('title','标题');
  9709. * ```
  9710. */
  9711. setAttr: function (attrName, attrVal) {
  9712. if (!attrName) {
  9713. delete this.attrs
  9714. return
  9715. }
  9716. if (!this.attrs) {
  9717. this.attrs = {}
  9718. }
  9719. if (utils.isObject(attrName)) {
  9720. for (var a in attrName) {
  9721. if (!attrName[a]) {
  9722. delete this.attrs[a]
  9723. } else {
  9724. this.attrs[a.toLowerCase()] = attrName[a]
  9725. }
  9726. }
  9727. } else {
  9728. if (!attrVal) {
  9729. delete this.attrs[attrName]
  9730. } else {
  9731. this.attrs[attrName.toLowerCase()] = attrVal
  9732. }
  9733. }
  9734. },
  9735. /**
  9736. * 获取当前节点在父节点下的位置索引
  9737. * @method getIndex
  9738. * @return { Number } 返回索引数值,如果没有父节点,返回-1
  9739. * @example
  9740. * ```javascript
  9741. * node.getIndex();
  9742. * ```
  9743. */
  9744. getIndex: function () {
  9745. var parent = this.parentNode
  9746. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9747. if (ci === this) {
  9748. return i
  9749. }
  9750. }
  9751. return -1
  9752. },
  9753. /**
  9754. * 在当前节点下,根据id查找节点
  9755. * @method getNodeById
  9756. * @param { String } id 要查找的id
  9757. * @return { UE.uNode } 返回找到的节点
  9758. * @example
  9759. * ```javascript
  9760. * node.getNodeById('textId');
  9761. * ```
  9762. */
  9763. getNodeById: function (id) {
  9764. var node
  9765. if (this.children && this.children.length) {
  9766. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9767. if ((node = getNodeById(ci, id))) {
  9768. return node
  9769. }
  9770. }
  9771. }
  9772. },
  9773. /**
  9774. * 在当前节点下,根据元素名称查找节点列表
  9775. * @method getNodesByTagName
  9776. * @param { String } tagNames 要查找的元素名称
  9777. * @return { Array } 返回找到的节点列表
  9778. * @example
  9779. * ```javascript
  9780. * node.getNodesByTagName('span');
  9781. * ```
  9782. */
  9783. getNodesByTagName: function (tagNames) {
  9784. tagNames = utils
  9785. .trim(tagNames)
  9786. .replace(/[ ]{2,}/g, ' ')
  9787. .split(' ')
  9788. var arr = [],
  9789. me = this
  9790. utils.each(tagNames, function (tagName) {
  9791. if (me.children && me.children.length) {
  9792. for (var i = 0, ci; (ci = me.children[i++]); ) {
  9793. getNodesByTagName(ci, tagName, arr)
  9794. }
  9795. }
  9796. })
  9797. return arr
  9798. },
  9799. /**
  9800. * 根据样式名称,获取节点的样式值
  9801. * @method getStyle
  9802. * @param { String } name 要获取的样式名称
  9803. * @return { String } 返回样式值
  9804. * @example
  9805. * ```javascript
  9806. * node.getStyle('font-size');
  9807. * ```
  9808. */
  9809. getStyle: function (name) {
  9810. var cssStyle = this.getAttr('style')
  9811. if (!cssStyle) {
  9812. return ''
  9813. }
  9814. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+)', 'i')
  9815. var match = cssStyle.match(reg)
  9816. if (match && match[0]) {
  9817. return match[2]
  9818. }
  9819. return ''
  9820. },
  9821. /**
  9822. * 给节点设置样式
  9823. * @method setStyle
  9824. * @param { String } name 要设置的的样式名称
  9825. * @param { String } val 要设置的的样值
  9826. * @example
  9827. * ```javascript
  9828. * node.setStyle('font-size', '12px');
  9829. * ```
  9830. */
  9831. setStyle: function (name, val) {
  9832. function exec(name, val) {
  9833. var reg = new RegExp('(^|;)\\s*' + name + ':([^;]+;?)', 'gi')
  9834. cssStyle = cssStyle.replace(reg, '$1')
  9835. if (val) {
  9836. cssStyle = name + ':' + utils.unhtml(val) + ';' + cssStyle
  9837. }
  9838. }
  9839. var cssStyle = this.getAttr('style')
  9840. if (!cssStyle) {
  9841. cssStyle = ''
  9842. }
  9843. if (utils.isObject(name)) {
  9844. for (var a in name) {
  9845. exec(a, name[a])
  9846. }
  9847. } else {
  9848. exec(name, val)
  9849. }
  9850. this.setAttr('style', utils.trim(cssStyle))
  9851. },
  9852. /**
  9853. * 传入一个函数,递归遍历当前节点下的所有节点
  9854. * @method traversal
  9855. * @param { Function } fn 遍历到节点的时,传入节点作为参数,运行此函数
  9856. * @example
  9857. * ```javascript
  9858. * traversal(node, function(){
  9859. * console.log(node.type);
  9860. * });
  9861. * ```
  9862. */
  9863. traversal: function (fn) {
  9864. if (this.children && this.children.length) {
  9865. nodeTraversal(this, fn)
  9866. }
  9867. return this
  9868. },
  9869. }
  9870. })()
  9871. // core/htmlparser.js
  9872. /**
  9873. * html字符串转换成uNode节点
  9874. * @file
  9875. * @module UE
  9876. * @since 1.2.6.1
  9877. */
  9878. /**
  9879. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9880. * @unfile
  9881. * @module UE
  9882. */
  9883. /**
  9884. * html字符串转换成uNode节点的静态方法
  9885. * @method htmlparser
  9886. * @param { String } htmlstr 要转换的html代码
  9887. * @param { Boolean } ignoreBlank 若设置为true,转换的时候忽略\n\r\t等空白字符
  9888. * @return { uNode } 给定的html片段转换形成的uNode对象
  9889. * @example
  9890. * ```javascript
  9891. * var root = UE.htmlparser('<p><b>htmlparser</b></p>', true);
  9892. * ```
  9893. */
  9894. var htmlparser = (UE.htmlparser = function (htmlstr, ignoreBlank) {
  9895. //todo 原来的方式 [^"'<>\/] 有\/就不能配对上 <TD vAlign=top background=../AAA.JPG> 这样的标签了
  9896. //先去掉了,加上的原因忘了,这里先记录
  9897. var re_tag =
  9898. /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/<>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,
  9899. re_attr =
  9900. /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g
  9901. //ie下取得的html可能会有\n存在,要去掉,在处理replace(/[\t\r\n]*/g,'');代码高量的\n不能去除
  9902. var allowEmptyTags = {
  9903. b: 1,
  9904. code: 1,
  9905. i: 1,
  9906. u: 1,
  9907. strike: 1,
  9908. s: 1,
  9909. tt: 1,
  9910. strong: 1,
  9911. q: 1,
  9912. samp: 1,
  9913. em: 1,
  9914. span: 1,
  9915. sub: 1,
  9916. img: 1,
  9917. sup: 1,
  9918. font: 1,
  9919. big: 1,
  9920. small: 1,
  9921. iframe: 1,
  9922. a: 1,
  9923. br: 1,
  9924. pre: 1,
  9925. }
  9926. htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, 'g'), '')
  9927. if (!ignoreBlank) {
  9928. htmlstr = htmlstr.replace(
  9929. new RegExp(
  9930. '[\\r\\t\\n' +
  9931. (ignoreBlank ? '' : ' ') +
  9932. ']*</?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n' +
  9933. (ignoreBlank ? '' : ' ') +
  9934. ']*',
  9935. 'g'
  9936. ),
  9937. function (a, b) {
  9938. //br暂时单独处理
  9939. if (b && allowEmptyTags[b.toLowerCase()]) {
  9940. return a.replace(/(^[\n\r]+)|([\n\r]+$)/g, '')
  9941. }
  9942. return a
  9943. .replace(
  9944. new RegExp('^[\\r\\n' + (ignoreBlank ? '' : ' ') + ']+'),
  9945. ''
  9946. )
  9947. .replace(
  9948. new RegExp('[\\r\\n' + (ignoreBlank ? '' : ' ') + ']+$'),
  9949. ''
  9950. )
  9951. }
  9952. )
  9953. }
  9954. var notTransAttrs = {
  9955. href: 1,
  9956. src: 1,
  9957. }
  9958. var uNode = UE.uNode,
  9959. needParentNode = {
  9960. td: 'tr',
  9961. tr: ['tbody', 'thead', 'tfoot'],
  9962. tbody: 'table',
  9963. th: 'tr',
  9964. thead: 'table',
  9965. tfoot: 'table',
  9966. caption: 'table',
  9967. li: ['ul', 'ol'],
  9968. dt: 'dl',
  9969. dd: 'dl',
  9970. option: 'select',
  9971. },
  9972. needChild = {
  9973. ol: 'li',
  9974. ul: 'li',
  9975. }
  9976. function text(parent, data) {
  9977. if (needChild[parent.tagName]) {
  9978. var tmpNode = uNode.createElement(needChild[parent.tagName])
  9979. parent.appendChild(tmpNode)
  9980. tmpNode.appendChild(uNode.createText(data))
  9981. parent = tmpNode
  9982. } else {
  9983. parent.appendChild(uNode.createText(data))
  9984. }
  9985. }
  9986. function element(parent, tagName, htmlattr) {
  9987. var needParentTag
  9988. if ((needParentTag = needParentNode[tagName])) {
  9989. var tmpParent = parent,
  9990. hasParent
  9991. while (tmpParent.type != 'root') {
  9992. if (
  9993. utils.isArray(needParentTag)
  9994. ? utils.indexOf(needParentTag, tmpParent.tagName) != -1
  9995. : needParentTag == tmpParent.tagName
  9996. ) {
  9997. parent = tmpParent
  9998. hasParent = true
  9999. break
  10000. }
  10001. tmpParent = tmpParent.parentNode
  10002. }
  10003. if (!hasParent) {
  10004. parent = element(
  10005. parent,
  10006. utils.isArray(needParentTag) ? needParentTag[0] : needParentTag
  10007. )
  10008. }
  10009. }
  10010. //按dtd处理嵌套
  10011. // if(parent.type != 'root' && !dtd[parent.tagName][tagName])
  10012. // parent = parent.parentNode;
  10013. var elm = new uNode({
  10014. parentNode: parent,
  10015. type: 'element',
  10016. tagName: tagName.toLowerCase(),
  10017. //是自闭合的处理一下
  10018. children: dtd.$empty[tagName] ? null : [],
  10019. })
  10020. //如果属性存在,处理属性
  10021. if (htmlattr) {
  10022. var attrs = {},
  10023. match
  10024. while ((match = re_attr.exec(htmlattr))) {
  10025. attrs[match[1].toLowerCase()] = notTransAttrs[match[1].toLowerCase()]
  10026. ? match[2] || match[3] || match[4]
  10027. : utils.unhtml(match[2] || match[3] || match[4])
  10028. }
  10029. elm.attrs = attrs
  10030. }
  10031. //trace:3970
  10032. // //如果parent下不能放elm
  10033. // if(dtd.$inline[parent.tagName] && dtd.$block[elm.tagName] && !dtd[parent.tagName][elm.tagName]){
  10034. // parent = parent.parentNode;
  10035. // elm.parentNode = parent;
  10036. // }
  10037. parent.children.push(elm)
  10038. //如果是自闭合节点返回父亲节点
  10039. return dtd.$empty[tagName] ? parent : elm
  10040. }
  10041. function comment(parent, data) {
  10042. parent.children.push(
  10043. new uNode({
  10044. type: 'comment',
  10045. data: data,
  10046. parentNode: parent,
  10047. })
  10048. )
  10049. }
  10050. var match,
  10051. currentIndex = 0,
  10052. nextIndex = 0
  10053. //设置根节点
  10054. var root = new uNode({
  10055. type: 'root',
  10056. children: [],
  10057. })
  10058. var currentParent = root
  10059. while ((match = re_tag.exec(htmlstr))) {
  10060. currentIndex = match.index
  10061. try {
  10062. if (currentIndex > nextIndex) {
  10063. //text node
  10064. text(currentParent, htmlstr.slice(nextIndex, currentIndex))
  10065. }
  10066. if (match[3]) {
  10067. if (dtd.$cdata[currentParent.tagName]) {
  10068. text(currentParent, match[0])
  10069. } else {
  10070. //start tag
  10071. currentParent = element(
  10072. currentParent,
  10073. match[3].toLowerCase(),
  10074. match[4]
  10075. )
  10076. }
  10077. } else if (match[1]) {
  10078. if (currentParent.type != 'root') {
  10079. if (dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]) {
  10080. text(currentParent, match[0])
  10081. } else {
  10082. var tmpParent = currentParent
  10083. while (
  10084. currentParent.type == 'element' &&
  10085. currentParent.tagName != match[1].toLowerCase()
  10086. ) {
  10087. currentParent = currentParent.parentNode
  10088. if (currentParent.type == 'root') {
  10089. currentParent = tmpParent
  10090. throw 'break'
  10091. }
  10092. }
  10093. //end tag
  10094. currentParent = currentParent.parentNode
  10095. }
  10096. }
  10097. } else if (match[2]) {
  10098. //comment
  10099. comment(currentParent, match[2])
  10100. }
  10101. } catch (e) {}
  10102. nextIndex = re_tag.lastIndex
  10103. }
  10104. //如果结束是文本,就有可能丢掉,所以这里手动判断一下
  10105. //例如 <li>sdfsdfsdf<li>sdfsdfsdfsdf
  10106. if (nextIndex < htmlstr.length) {
  10107. text(currentParent, htmlstr.slice(nextIndex))
  10108. }
  10109. return root
  10110. })
  10111. // core/filternode.js
  10112. /**
  10113. * UE过滤节点的静态方法
  10114. * @file
  10115. */
  10116. /**
  10117. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  10118. * @module UE
  10119. */
  10120. /**
  10121. * 根据传入节点和过滤规则过滤相应节点
  10122. * @module UE
  10123. * @since 1.2.6.1
  10124. * @method filterNode
  10125. * @param { Object } root 指定root节点
  10126. * @param { Object } rules 过滤规则json对象
  10127. * @example
  10128. * ```javascript
  10129. * UE.filterNode(root,editor.options.filterRules);
  10130. * ```
  10131. */
  10132. var filterNode = (UE.filterNode = (function () {
  10133. function filterNode(node, rules) {
  10134. switch (node.type) {
  10135. case 'text':
  10136. break
  10137. case 'element':
  10138. var val
  10139. if ((val = rules[node.tagName])) {
  10140. if (val === '-') {
  10141. node.parentNode.removeChild(node)
  10142. } else if (utils.isFunction(val)) {
  10143. var parentNode = node.parentNode,
  10144. index = node.getIndex()
  10145. val(node)
  10146. if (node.parentNode) {
  10147. if (node.children) {
  10148. for (var i = 0, ci; (ci = node.children[i]); ) {
  10149. filterNode(ci, rules)
  10150. if (ci.parentNode) {
  10151. i++
  10152. }
  10153. }
  10154. }
  10155. } else {
  10156. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10157. filterNode(ci, rules)
  10158. if (ci.parentNode) {
  10159. i++
  10160. }
  10161. }
  10162. }
  10163. } else {
  10164. var attrs = val['$']
  10165. if (attrs && node.attrs) {
  10166. var tmpAttrs = {},
  10167. tmpVal
  10168. for (var a in attrs) {
  10169. tmpVal = node.getAttr(a)
  10170. //todo 只先对style单独处理
  10171. if (a == 'style' && utils.isArray(attrs[a])) {
  10172. var tmpCssStyle = []
  10173. utils.each(attrs[a], function (v) {
  10174. var tmp
  10175. if ((tmp = node.getStyle(v))) {
  10176. tmpCssStyle.push(v + ':' + tmp)
  10177. }
  10178. })
  10179. tmpVal = tmpCssStyle.join(';')
  10180. }
  10181. if (tmpVal) {
  10182. tmpAttrs[a] = tmpVal
  10183. }
  10184. }
  10185. node.attrs = tmpAttrs
  10186. }
  10187. if (node.children) {
  10188. for (var i = 0, ci; (ci = node.children[i]); ) {
  10189. filterNode(ci, rules)
  10190. if (ci.parentNode) {
  10191. i++
  10192. }
  10193. }
  10194. }
  10195. }
  10196. } else {
  10197. //如果不在名单里扣出子节点并删除该节点,cdata除外
  10198. if (dtd.$cdata[node.tagName]) {
  10199. node.parentNode.removeChild(node)
  10200. } else {
  10201. var parentNode = node.parentNode,
  10202. index = node.getIndex()
  10203. node.parentNode.removeChild(node, true)
  10204. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10205. filterNode(ci, rules)
  10206. if (ci.parentNode) {
  10207. i++
  10208. }
  10209. }
  10210. }
  10211. }
  10212. break
  10213. case 'comment':
  10214. node.parentNode.removeChild(node)
  10215. }
  10216. }
  10217. return function (root, rules) {
  10218. if (utils.isEmptyObject(rules)) {
  10219. return root
  10220. }
  10221. var val
  10222. if ((val = rules['-'])) {
  10223. utils.each(val.split(' '), function (k) {
  10224. rules[k] = '-'
  10225. })
  10226. }
  10227. for (var i = 0, ci; (ci = root.children[i]); ) {
  10228. filterNode(ci, rules)
  10229. if (ci.parentNode) {
  10230. i++
  10231. }
  10232. }
  10233. return root
  10234. }
  10235. })())
  10236. // core/plugin.js
  10237. /**
  10238. * Created with JetBrains PhpStorm.
  10239. * User: campaign
  10240. * Date: 10/8/13
  10241. * Time: 6:15 PM
  10242. * To change this template use File | Settings | File Templates.
  10243. */
  10244. UE.plugin = (function () {
  10245. var _plugins = {}
  10246. return {
  10247. register: function (pluginName, fn, oldOptionName, afterDisabled) {
  10248. if (oldOptionName && utils.isFunction(oldOptionName)) {
  10249. afterDisabled = oldOptionName
  10250. oldOptionName = null
  10251. }
  10252. _plugins[pluginName] = {
  10253. optionName: oldOptionName || pluginName,
  10254. execFn: fn,
  10255. //当插件被禁用时执行
  10256. afterDisabled: afterDisabled,
  10257. }
  10258. },
  10259. load: function (editor) {
  10260. utils.each(_plugins, function (plugin) {
  10261. var _export = plugin.execFn.call(editor)
  10262. if (editor.options[plugin.optionName] !== false) {
  10263. if (_export) {
  10264. //后边需要再做扩展
  10265. utils.each(_export, function (v, k) {
  10266. switch (k.toLowerCase()) {
  10267. case 'shortcutkey':
  10268. editor.addshortcutkey(v)
  10269. break
  10270. case 'bindevents':
  10271. utils.each(v, function (fn, eventName) {
  10272. editor.addListener(eventName, fn)
  10273. })
  10274. break
  10275. case 'bindmultievents':
  10276. utils.each(utils.isArray(v) ? v : [v], function (event) {
  10277. var types = utils.trim(event.type).split(/\s+/)
  10278. utils.each(types, function (eventName) {
  10279. editor.addListener(eventName, event.handler)
  10280. })
  10281. })
  10282. break
  10283. case 'commands':
  10284. utils.each(v, function (execFn, execName) {
  10285. editor.commands[execName] = execFn
  10286. })
  10287. break
  10288. case 'outputrule':
  10289. editor.addOutputRule(v)
  10290. break
  10291. case 'inputrule':
  10292. editor.addInputRule(v)
  10293. break
  10294. case 'defaultoptions':
  10295. editor.setOpt(v)
  10296. }
  10297. })
  10298. }
  10299. } else if (plugin.afterDisabled) {
  10300. plugin.afterDisabled.call(editor)
  10301. }
  10302. })
  10303. //向下兼容
  10304. utils.each(UE.plugins, function (plugin) {
  10305. plugin.call(editor)
  10306. })
  10307. },
  10308. run: function (pluginName, editor) {
  10309. var plugin = _plugins[pluginName]
  10310. if (plugin) {
  10311. plugin.exeFn.call(editor)
  10312. }
  10313. },
  10314. }
  10315. })()
  10316. // core/keymap.js
  10317. var keymap = (UE.keymap = {
  10318. Backspace: 8,
  10319. Tab: 9,
  10320. Enter: 13,
  10321. Shift: 16,
  10322. Control: 17,
  10323. Alt: 18,
  10324. CapsLock: 20,
  10325. Esc: 27,
  10326. Spacebar: 32,
  10327. PageUp: 33,
  10328. PageDown: 34,
  10329. End: 35,
  10330. Home: 36,
  10331. Left: 37,
  10332. Up: 38,
  10333. Right: 39,
  10334. Down: 40,
  10335. Insert: 45,
  10336. Del: 46,
  10337. NumLock: 144,
  10338. Cmd: 91,
  10339. '=': 187,
  10340. '-': 189,
  10341. b: 66,
  10342. i: 73,
  10343. //回退
  10344. z: 90,
  10345. y: 89,
  10346. //粘贴
  10347. v: 86,
  10348. x: 88,
  10349. s: 83,
  10350. n: 78,
  10351. })
  10352. // core/localstorage.js
  10353. //存储媒介封装
  10354. var LocalStorage = (UE.LocalStorage = (function () {
  10355. var storage = window.localStorage || getUserData() || null,
  10356. LOCAL_FILE = 'localStorage'
  10357. return {
  10358. saveLocalData: function (key, data) {
  10359. if (storage && data) {
  10360. storage.setItem(key, data)
  10361. return true
  10362. }
  10363. return false
  10364. },
  10365. getLocalData: function (key) {
  10366. if (storage) {
  10367. return storage.getItem(key)
  10368. }
  10369. return null
  10370. },
  10371. removeItem: function (key) {
  10372. storage && storage.removeItem(key)
  10373. },
  10374. }
  10375. function getUserData() {
  10376. var container = document.createElement('div')
  10377. container.style.display = 'none'
  10378. if (!container.addBehavior) {
  10379. return null
  10380. }
  10381. container.addBehavior('#default#userdata')
  10382. return {
  10383. getItem: function (key) {
  10384. var result = null
  10385. try {
  10386. document.body.appendChild(container)
  10387. container.load(LOCAL_FILE)
  10388. result = container.getAttribute(key)
  10389. document.body.removeChild(container)
  10390. } catch (e) {}
  10391. return result
  10392. },
  10393. setItem: function (key, value) {
  10394. document.body.appendChild(container)
  10395. container.setAttribute(key, value)
  10396. container.save(LOCAL_FILE)
  10397. document.body.removeChild(container)
  10398. },
  10399. //// 暂时没有用到
  10400. //clear: function () {
  10401. //
  10402. // var expiresTime = new Date();
  10403. // expiresTime.setFullYear(expiresTime.getFullYear() - 1);
  10404. // document.body.appendChild(container);
  10405. // container.expires = expiresTime.toUTCString();
  10406. // container.save(LOCAL_FILE);
  10407. // document.body.removeChild(container);
  10408. //
  10409. //},
  10410. removeItem: function (key) {
  10411. document.body.appendChild(container)
  10412. container.removeAttribute(key)
  10413. container.save(LOCAL_FILE)
  10414. document.body.removeChild(container)
  10415. },
  10416. }
  10417. }
  10418. })())
  10419. ;(function () {
  10420. var ROOTKEY = 'ueditor_preference'
  10421. UE.Editor.prototype.setPreferences = function (key, value) {
  10422. var obj = {}
  10423. if (utils.isString(key)) {
  10424. obj[key] = value
  10425. } else {
  10426. obj = key
  10427. }
  10428. var data = LocalStorage.getLocalData(ROOTKEY)
  10429. if (data && (data = utils.str2json(data))) {
  10430. utils.extend(data, obj)
  10431. } else {
  10432. data = obj
  10433. }
  10434. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data))
  10435. }
  10436. UE.Editor.prototype.getPreferences = function (key) {
  10437. var data = LocalStorage.getLocalData(ROOTKEY)
  10438. if (data && (data = utils.str2json(data))) {
  10439. return key ? data[key] : data
  10440. }
  10441. return null
  10442. }
  10443. UE.Editor.prototype.removePreferences = function (key) {
  10444. var data = LocalStorage.getLocalData(ROOTKEY)
  10445. if (data && (data = utils.str2json(data))) {
  10446. data[key] = undefined
  10447. delete data[key]
  10448. }
  10449. data && LocalStorage.saveLocalData(ROOTKEY, utils.json2str(data))
  10450. }
  10451. })()
  10452. // plugins/defaultfilter.js
  10453. ///import core
  10454. ///plugin 编辑器默认的过滤转换机制
  10455. UE.plugins['defaultfilter'] = function () {
  10456. //return //导入word时新增return
  10457. var me = this
  10458. me.setOpt({
  10459. allowDivTransToP: true,
  10460. disabledTableInTable: true,
  10461. })
  10462. //默认的过滤处理
  10463. //进入编辑器的内容处理
  10464. me.addInputRule(function (root) {
  10465. var allowDivTransToP = this.options.allowDivTransToP
  10466. var val
  10467. function tdParent(node) {
  10468. while (node && node.type == 'element') {
  10469. if (node.tagName == 'td') {
  10470. return true
  10471. }
  10472. node = node.parentNode
  10473. }
  10474. return false
  10475. }
  10476. //进行默认的处理
  10477. root.traversal(function (node) {
  10478. if (node.type == 'element') {
  10479. if (
  10480. !dtd.$cdata[node.tagName] &&
  10481. me.options.autoClearEmptyNode &&
  10482. dtd.$inline[node.tagName] &&
  10483. !dtd.$empty[node.tagName] &&
  10484. (!node.attrs || utils.isEmptyObject(node.attrs))
  10485. ) {
  10486. if (!node.firstChild()) node.parentNode.removeChild(node)
  10487. else if (
  10488. node.tagName == 'span' &&
  10489. (!node.attrs || utils.isEmptyObject(node.attrs))
  10490. ) {
  10491. node.parentNode.removeChild(node, true)
  10492. }
  10493. return
  10494. }
  10495. switch (node.tagName) {
  10496. case 'style':
  10497. case 'script':
  10498. node.setAttr({
  10499. cdata_tag: node.tagName,
  10500. cdata_data: node.innerHTML() || '',
  10501. _ue_custom_node_: 'true',
  10502. })
  10503. node.tagName = 'div'
  10504. node.innerHTML('')
  10505. break
  10506. case 'a':
  10507. if ((val = node.getAttr('href'))) {
  10508. node.setAttr('_href', val)
  10509. }
  10510. break
  10511. case 'img':
  10512. //todo base64暂时去掉,后边做远程图片上传后,干掉这个
  10513. if ((val = node.getAttr('src'))) {
  10514. if (/^data:/.test(val)) {
  10515. node.parentNode.removeChild(node)
  10516. break
  10517. }
  10518. }
  10519. node.setAttr('_src', node.getAttr('src'))
  10520. break
  10521. case 'span':
  10522. if (browser.webkit && (val = node.getStyle('white-space'))) {
  10523. if (/nowrap|normal/.test(val)) {
  10524. node.setStyle('white-space', '')
  10525. if (
  10526. me.options.autoClearEmptyNode &&
  10527. utils.isEmptyObject(node.attrs)
  10528. ) {
  10529. node.parentNode.removeChild(node, true)
  10530. }
  10531. }
  10532. }
  10533. val = node.getAttr('id')
  10534. if (val && /^_baidu_bookmark_/i.test(val)) {
  10535. node.parentNode.removeChild(node)
  10536. }
  10537. break
  10538. case 'p':
  10539. if ((val = node.getAttr('align'))) {
  10540. node.setAttr('align')
  10541. node.setStyle('text-align', val)
  10542. }
  10543. //trace:3431
  10544. // var cssStyle = node.getAttr('style');
  10545. // if (cssStyle) {
  10546. // cssStyle = cssStyle.replace(/(margin|padding)[^;]+/g, '');
  10547. // node.setAttr('style', cssStyle)
  10548. //
  10549. // }
  10550. //p标签不允许嵌套
  10551. utils.each(node.children, function (n) {
  10552. if (n.type == 'element' && n.tagName == 'p') {
  10553. var next = n.nextSibling()
  10554. node.parentNode.insertAfter(n, node)
  10555. var last = n
  10556. while (next) {
  10557. var tmp = next.nextSibling()
  10558. node.parentNode.insertAfter(next, last)
  10559. last = next
  10560. next = tmp
  10561. }
  10562. return false
  10563. }
  10564. })
  10565. if (!node.firstChild()) {
  10566. node.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  10567. }
  10568. break
  10569. case 'div':
  10570. if (node.getAttr('cdata_tag')) {
  10571. break
  10572. }
  10573. //针对代码这里不处理插入代码的div
  10574. val = node.getAttr('class')
  10575. if (val && /^line number\d+/.test(val)) {
  10576. break
  10577. }
  10578. if (!allowDivTransToP) {
  10579. break
  10580. }
  10581. var tmpNode,
  10582. p = UE.uNode.createElement('p')
  10583. while ((tmpNode = node.firstChild())) {
  10584. if (
  10585. tmpNode.type == 'text' ||
  10586. !UE.dom.dtd.$block[tmpNode.tagName]
  10587. ) {
  10588. p.appendChild(tmpNode)
  10589. } else {
  10590. if (p.firstChild()) {
  10591. node.parentNode.insertBefore(p, node)
  10592. p = UE.uNode.createElement('p')
  10593. } else {
  10594. node.parentNode.insertBefore(tmpNode, node)
  10595. }
  10596. }
  10597. }
  10598. if (p.firstChild()) {
  10599. node.parentNode.insertBefore(p, node)
  10600. }
  10601. node.parentNode.removeChild(node)
  10602. break
  10603. case 'dl':
  10604. node.tagName = 'ul'
  10605. break
  10606. case 'dt':
  10607. case 'dd':
  10608. node.tagName = 'li'
  10609. break
  10610. case 'li':
  10611. var className = node.getAttr('class')
  10612. if (!className || !/list\-/.test(className)) {
  10613. node.setAttr()
  10614. }
  10615. var tmpNodes = node.getNodesByTagName('ol ul')
  10616. UE.utils.each(tmpNodes, function (n) {
  10617. node.parentNode.insertAfter(n, node)
  10618. })
  10619. break
  10620. case 'td':
  10621. case 'th':
  10622. case 'caption':
  10623. if (!node.children || !node.children.length) {
  10624. node.appendChild(
  10625. browser.ie11below
  10626. ? UE.uNode.createText(' ')
  10627. : UE.uNode.createElement('br')
  10628. )
  10629. }
  10630. break
  10631. case 'table':
  10632. if (me.options.disabledTableInTable && tdParent(node)) {
  10633. node.parentNode.insertBefore(
  10634. UE.uNode.createText(node.innerText()),
  10635. node
  10636. )
  10637. node.parentNode.removeChild(node)
  10638. }
  10639. }
  10640. }
  10641. // if(node.type == 'comment'){
  10642. // node.parentNode.removeChild(node);
  10643. // }
  10644. })
  10645. })
  10646. //从编辑器出去的内容处理
  10647. me.addOutputRule(function (root) {
  10648. var val
  10649. root.traversal(function (node) {
  10650. if (node.type == 'element') {
  10651. if (
  10652. me.options.autoClearEmptyNode &&
  10653. dtd.$inline[node.tagName] &&
  10654. !dtd.$empty[node.tagName] &&
  10655. (!node.attrs || utils.isEmptyObject(node.attrs))
  10656. ) {
  10657. if (!node.firstChild()) node.parentNode.removeChild(node)
  10658. else if (
  10659. node.tagName == 'span' &&
  10660. (!node.attrs || utils.isEmptyObject(node.attrs))
  10661. ) {
  10662. node.parentNode.removeChild(node, true)
  10663. }
  10664. return
  10665. }
  10666. switch (node.tagName) {
  10667. case 'div':
  10668. if ((val = node.getAttr('cdata_tag'))) {
  10669. node.tagName = val
  10670. node.appendChild(
  10671. UE.uNode.createText(node.getAttr('cdata_data'))
  10672. )
  10673. node.setAttr({
  10674. cdata_tag: '',
  10675. cdata_data: '',
  10676. _ue_custom_node_: '',
  10677. })
  10678. }
  10679. break
  10680. case 'a':
  10681. if ((val = node.getAttr('_href'))) {
  10682. node.setAttr({
  10683. href: utils.html(val),
  10684. _href: '',
  10685. })
  10686. }
  10687. break
  10688. break
  10689. case 'span':
  10690. val = node.getAttr('id')
  10691. if (val && /^_baidu_bookmark_/i.test(val)) {
  10692. node.parentNode.removeChild(node)
  10693. }
  10694. break
  10695. case 'img':
  10696. if ((val = node.getAttr('_src'))) {
  10697. node.setAttr({
  10698. src: node.getAttr('_src'),
  10699. _src: '',
  10700. })
  10701. }
  10702. }
  10703. }
  10704. })
  10705. })
  10706. }
  10707. // plugins/inserthtml.js
  10708. /**
  10709. * 插入html字符串插件
  10710. * @file
  10711. * @since 1.2.6.1
  10712. */
  10713. /**
  10714. * 插入html代码
  10715. * @command inserthtml
  10716. * @method execCommand
  10717. * @param { String } cmd 命令字符串
  10718. * @param { String } html 插入的html字符串
  10719. * @remaind 插入的标签内容是在当前的选区位置上插入,如果当前是闭合状态,那直接插入内容, 如果当前是选中状态,将先清除当前选中内容后,再做插入
  10720. * @warning 注意:该命令会对当前选区的位置,对插入的内容进行过滤转换处理。 过滤的规则遵循html语意化的原则。
  10721. * @example
  10722. * ```javascript
  10723. * //xxx[BB]xxx 当前选区为非闭合选区,选中BB这两个文本
  10724. * //执行命令,插入<b>CC</b>
  10725. * //插入后的效果 xxx<b>CC</b>xxx
  10726. * //<p>xx|xxx</p> 当前选区为闭合状态
  10727. * //插入<p>CC</p>
  10728. * //结果 <p>xx</p><p>CC</p><p>xxx</p>
  10729. * //<p>xxxx</p>|</p>xxx</p> 当前选区在两个p标签之间
  10730. * //插入 xxxx
  10731. * //结果 <p>xxxx</p><p>xxxx</p></p>xxx</p>
  10732. * ```
  10733. */
  10734. UE.commands['inserthtml'] = {
  10735. execCommand: function (command, html, notNeedFilter) {
  10736. var me = this,
  10737. range,
  10738. div
  10739. if (!html) {
  10740. return
  10741. }
  10742. if (me.fireEvent('beforeinserthtml', html) === true) {
  10743. return
  10744. }
  10745. range = me.selection.getRange()
  10746. div = range.document.createElement('div')
  10747. div.style.display = 'inline'
  10748. if (!notNeedFilter) {
  10749. var root = UE.htmlparser(html)
  10750. //如果给了过滤规则就先进行过滤
  10751. if (me.options.filterRules) {
  10752. UE.filterNode(root, me.options.filterRules)
  10753. }
  10754. //执行默认的处理
  10755. me.filterInputRule(root)
  10756. html = root.toHtml()
  10757. }
  10758. div.innerHTML = utils.trim(html)
  10759. if (!range.collapsed) {
  10760. var tmpNode = range.startContainer
  10761. if (domUtils.isFillChar(tmpNode)) {
  10762. range.setStartBefore(tmpNode)
  10763. }
  10764. tmpNode = range.endContainer
  10765. if (domUtils.isFillChar(tmpNode)) {
  10766. range.setEndAfter(tmpNode)
  10767. }
  10768. range.txtToElmBoundary()
  10769. //结束边界可能放到了br的前边,要把br包含进来
  10770. // x[xxx]<br/>
  10771. if (range.endContainer && range.endContainer.nodeType == 1) {
  10772. tmpNode = range.endContainer.childNodes[range.endOffset]
  10773. if (tmpNode && domUtils.isBr(tmpNode)) {
  10774. range.setEndAfter(tmpNode)
  10775. }
  10776. }
  10777. if (range.startOffset == 0) {
  10778. tmpNode = range.startContainer
  10779. if (domUtils.isBoundaryNode(tmpNode, 'firstChild')) {
  10780. tmpNode = range.endContainer
  10781. if (
  10782. range.endOffset ==
  10783. (tmpNode.nodeType == 3
  10784. ? tmpNode.nodeValue.length
  10785. : tmpNode.childNodes.length) &&
  10786. domUtils.isBoundaryNode(tmpNode, 'lastChild')
  10787. ) {
  10788. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  10789. range.setStart(me.body.firstChild, 0).collapse(true)
  10790. }
  10791. }
  10792. }
  10793. !range.collapsed && range.deleteContents()
  10794. if (range.startContainer.nodeType == 1) {
  10795. var child = range.startContainer.childNodes[range.startOffset],
  10796. pre
  10797. if (
  10798. child &&
  10799. domUtils.isBlockElm(child) &&
  10800. (pre = child.previousSibling) &&
  10801. domUtils.isBlockElm(pre)
  10802. ) {
  10803. range.setEnd(pre, pre.childNodes.length).collapse()
  10804. while (child.firstChild) {
  10805. pre.appendChild(child.firstChild)
  10806. }
  10807. domUtils.remove(child)
  10808. }
  10809. }
  10810. }
  10811. var child,
  10812. parent,
  10813. pre,
  10814. tmp,
  10815. hadBreak = 0,
  10816. nextNode
  10817. //如果当前位置选中了fillchar要干掉,要不会产生空行
  10818. if (range.inFillChar()) {
  10819. child = range.startContainer
  10820. if (domUtils.isFillChar(child)) {
  10821. range.setStartBefore(child).collapse(true)
  10822. domUtils.remove(child)
  10823. } else if (domUtils.isFillChar(child, true)) {
  10824. child.nodeValue = child.nodeValue.replace(fillCharReg, '')
  10825. range.startOffset--
  10826. range.collapsed && range.collapse(true)
  10827. }
  10828. }
  10829. //列表单独处理
  10830. var li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  10831. if (li) {
  10832. var next, last
  10833. while ((child = div.firstChild)) {
  10834. //针对hr单独处理一下先
  10835. while (
  10836. child &&
  10837. (child.nodeType == 3 ||
  10838. !domUtils.isBlockElm(child) ||
  10839. child.tagName == 'HR')
  10840. ) {
  10841. next = child.nextSibling
  10842. range.insertNode(child).collapse()
  10843. last = child
  10844. child = next
  10845. }
  10846. if (child) {
  10847. if (/^(ol|ul)$/i.test(child.tagName)) {
  10848. while (child.firstChild) {
  10849. last = child.firstChild
  10850. domUtils.insertAfter(li, child.firstChild)
  10851. li = li.nextSibling
  10852. }
  10853. domUtils.remove(child)
  10854. } else {
  10855. var tmpLi
  10856. next = child.nextSibling
  10857. tmpLi = me.document.createElement('li')
  10858. domUtils.insertAfter(li, tmpLi)
  10859. tmpLi.appendChild(child)
  10860. last = child
  10861. child = next
  10862. li = tmpLi
  10863. }
  10864. }
  10865. }
  10866. li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  10867. if (domUtils.isEmptyBlock(li)) {
  10868. domUtils.remove(li)
  10869. }
  10870. if (last) {
  10871. range.setStartAfter(last).collapse(true).select(true)
  10872. }
  10873. } else {
  10874. while ((child = div.firstChild)) {
  10875. if (hadBreak) {
  10876. var p = me.document.createElement('p')
  10877. while (
  10878. child &&
  10879. (child.nodeType == 3 || !dtd.$block[child.tagName])
  10880. ) {
  10881. nextNode = child.nextSibling
  10882. p.appendChild(child)
  10883. child = nextNode
  10884. }
  10885. if (p.firstChild) {
  10886. child = p
  10887. }
  10888. }
  10889. range.insertNode(child)
  10890. nextNode = child.nextSibling
  10891. if (
  10892. !hadBreak &&
  10893. child.nodeType == domUtils.NODE_ELEMENT &&
  10894. domUtils.isBlockElm(child)
  10895. ) {
  10896. parent = domUtils.findParent(child, function (node) {
  10897. return domUtils.isBlockElm(node)
  10898. })
  10899. if (
  10900. parent &&
  10901. parent.tagName.toLowerCase() != 'body' &&
  10902. !(
  10903. dtd[parent.tagName][child.nodeName] &&
  10904. child.parentNode === parent
  10905. )
  10906. ) {
  10907. if (!dtd[parent.tagName][child.nodeName]) {
  10908. pre = parent
  10909. } else {
  10910. tmp = child.parentNode
  10911. while (tmp !== parent) {
  10912. pre = tmp
  10913. tmp = tmp.parentNode
  10914. }
  10915. }
  10916. domUtils.breakParent(child, pre || tmp)
  10917. //去掉break后前一个多余的节点 <p>|<[p> ==> <p></p><div></div><p>|</p>
  10918. var pre = child.previousSibling
  10919. domUtils.trimWhiteTextNode(pre)
  10920. if (!pre.childNodes.length) {
  10921. domUtils.remove(pre)
  10922. }
  10923. //trace:2012,在非ie的情况,切开后剩下的节点有可能不能点入光标添加br占位
  10924. if (
  10925. !browser.ie &&
  10926. (next = child.nextSibling) &&
  10927. domUtils.isBlockElm(next) &&
  10928. next.lastChild &&
  10929. !domUtils.isBr(next.lastChild)
  10930. ) {
  10931. next.appendChild(me.document.createElement('br'))
  10932. }
  10933. hadBreak = 1
  10934. }
  10935. }
  10936. var next = child.nextSibling
  10937. if (!div.firstChild && next && domUtils.isBlockElm(next)) {
  10938. range.setStart(next, 0).collapse(true)
  10939. break
  10940. }
  10941. range.setEndAfter(child).collapse()
  10942. }
  10943. child = range.startContainer
  10944. if (nextNode && domUtils.isBr(nextNode)) {
  10945. domUtils.remove(nextNode)
  10946. }
  10947. //用chrome可能有空白展位符
  10948. if (domUtils.isBlockElm(child) && domUtils.isEmptyNode(child)) {
  10949. if ((nextNode = child.nextSibling)) {
  10950. domUtils.remove(child)
  10951. if (nextNode.nodeType == 1 && dtd.$block[nextNode.tagName]) {
  10952. range.setStart(nextNode, 0).collapse(true).shrinkBoundary()
  10953. }
  10954. } else {
  10955. try {
  10956. child.innerHTML = browser.ie ? domUtils.fillChar : '<br/>'
  10957. } catch (e) {
  10958. range.setStartBefore(child)
  10959. domUtils.remove(child)
  10960. }
  10961. }
  10962. }
  10963. //加上true因为在删除表情等时会删两次,第一次是删的fillData
  10964. try {
  10965. range.select(true)
  10966. } catch (e) {}
  10967. }
  10968. setTimeout(function () {
  10969. range = me.selection.getRange()
  10970. range.scrollToView(
  10971. me.autoHeightEnabled,
  10972. me.autoHeightEnabled ? domUtils.getXY(me.iframe).y : 0
  10973. )
  10974. me.fireEvent('afterinserthtml', html)
  10975. }, 200)
  10976. },
  10977. }
  10978. // plugins/autotypeset.js
  10979. /**
  10980. * 自动排版
  10981. * @file
  10982. * @since 1.2.6.1
  10983. */
  10984. /**
  10985. * 对当前编辑器的内容执行自动排版, 排版的行为根据config配置文件里的“autotypeset”选项进行控制。
  10986. * @command autotypeset
  10987. * @method execCommand
  10988. * @param { String } cmd 命令字符串
  10989. * @example
  10990. * ```javascript
  10991. * editor.execCommand( 'autotypeset' );
  10992. * ```
  10993. */
  10994. UE.plugins['autotypeset'] = function () {
  10995. this.setOpt({
  10996. autotypeset: {
  10997. mergeEmptyline: true, //合并空行
  10998. removeClass: true, //去掉冗余的class
  10999. removeEmptyline: false, //去掉空行
  11000. textAlign: 'left', //段落的排版方式,可以是 left,right,center,justify 去掉这个属性表示不执行排版
  11001. imageBlockLine: 'center', //图片的浮动方式,独占一行剧中,左右浮动,默认: center,left,right,none 去掉这个属性表示不执行排版
  11002. pasteFilter: false, //根据规则过滤没事粘贴进来的内容
  11003. clearFontSize: false, //去掉所有的内嵌字号,使用编辑器默认的字号
  11004. clearFontFamily: false, //去掉所有的内嵌字体,使用编辑器默认的字体
  11005. removeEmptyNode: false, // 去掉空节点
  11006. //可以去掉的标签
  11007. removeTagNames: utils.extend({ div: 1 }, dtd.$removeEmpty),
  11008. indent: false, // 行首缩进
  11009. indentValue: '2em', //行首缩进的大小
  11010. bdc2sb: false,
  11011. tobdc: false,
  11012. },
  11013. })
  11014. var me = this,
  11015. opt = me.options.autotypeset,
  11016. remainClass = {
  11017. selectTdClass: 1,
  11018. pagebreak: 1,
  11019. anchorclass: 1,
  11020. },
  11021. remainTag = {
  11022. li: 1,
  11023. },
  11024. tags = {
  11025. div: 1,
  11026. p: 1,
  11027. //trace:2183 这些也认为是行
  11028. blockquote: 1,
  11029. center: 1,
  11030. h1: 1,
  11031. h2: 1,
  11032. h3: 1,
  11033. h4: 1,
  11034. h5: 1,
  11035. h6: 1,
  11036. span: 1,
  11037. },
  11038. highlightCont
  11039. //升级了版本,但配置项目里没有autotypeset
  11040. if (!opt) {
  11041. return
  11042. }
  11043. readLocalOpts()
  11044. function isLine(node, notEmpty) {
  11045. if (!node || node.nodeType == 3) return 0
  11046. if (domUtils.isBr(node)) return 1
  11047. if (node && node.parentNode && tags[node.tagName.toLowerCase()]) {
  11048. if (
  11049. (highlightCont && highlightCont.contains(node)) ||
  11050. node.getAttribute('pagebreak')
  11051. ) {
  11052. return 0
  11053. }
  11054. return notEmpty
  11055. ? !domUtils.isEmptyBlock(node)
  11056. : domUtils.isEmptyBlock(
  11057. node,
  11058. new RegExp('[\\s' + domUtils.fillChar + ']', 'g')
  11059. )
  11060. }
  11061. }
  11062. function removeNotAttributeSpan(node) {
  11063. if (!node.style.cssText) {
  11064. domUtils.removeAttributes(node, ['style'])
  11065. if (
  11066. node.tagName.toLowerCase() == 'span' &&
  11067. domUtils.hasNoAttributes(node)
  11068. ) {
  11069. domUtils.remove(node, true)
  11070. }
  11071. }
  11072. }
  11073. function autotype(type, html) {
  11074. var me = this,
  11075. cont
  11076. if (html) {
  11077. if (!opt.pasteFilter) {
  11078. return
  11079. }
  11080. cont = me.document.createElement('div')
  11081. cont.innerHTML = html.html
  11082. } else {
  11083. cont = me.document.body
  11084. }
  11085. var nodes = domUtils.getElementsByTagName(cont, '*')
  11086. // 行首缩进,段落方向,段间距,段内间距
  11087. for (var i = 0, ci; (ci = nodes[i++]); ) {
  11088. if (me.fireEvent('excludeNodeinautotype', ci) === true) {
  11089. continue
  11090. }
  11091. //font-size
  11092. if (opt.clearFontSize && ci.style.fontSize) {
  11093. domUtils.removeStyle(ci, 'font-size')
  11094. removeNotAttributeSpan(ci)
  11095. }
  11096. //font-family
  11097. if (opt.clearFontFamily && ci.style.fontFamily) {
  11098. domUtils.removeStyle(ci, 'font-family')
  11099. removeNotAttributeSpan(ci)
  11100. }
  11101. if (isLine(ci)) {
  11102. //合并空行
  11103. if (opt.mergeEmptyline) {
  11104. var next = ci.nextSibling,
  11105. tmpNode,
  11106. isBr = domUtils.isBr(ci)
  11107. while (isLine(next)) {
  11108. tmpNode = next
  11109. next = tmpNode.nextSibling
  11110. if (isBr && (!next || (next && !domUtils.isBr(next)))) {
  11111. break
  11112. }
  11113. domUtils.remove(tmpNode)
  11114. }
  11115. }
  11116. //去掉空行,保留占位的空行
  11117. if (
  11118. opt.removeEmptyline &&
  11119. domUtils.inDoc(ci, cont) &&
  11120. !remainTag[ci.parentNode.tagName.toLowerCase()]
  11121. ) {
  11122. if (domUtils.isBr(ci)) {
  11123. next = ci.nextSibling
  11124. if (next && !domUtils.isBr(next)) {
  11125. continue
  11126. }
  11127. }
  11128. domUtils.remove(ci)
  11129. continue
  11130. }
  11131. }
  11132. if (isLine(ci, true) && ci.tagName != 'SPAN') {
  11133. if (opt.indent) {
  11134. ci.style.textIndent = opt.indentValue
  11135. }
  11136. if (opt.textAlign) {
  11137. ci.style.textAlign = opt.textAlign
  11138. }
  11139. // if(opt.lineHeight)
  11140. // ci.style.lineHeight = opt.lineHeight + 'cm';
  11141. }
  11142. //去掉class,保留的class不去掉
  11143. if (
  11144. opt.removeClass &&
  11145. ci.className &&
  11146. !remainClass[ci.className.toLowerCase()]
  11147. ) {
  11148. if (highlightCont && highlightCont.contains(ci)) {
  11149. continue
  11150. }
  11151. domUtils.removeAttributes(ci, ['class'])
  11152. }
  11153. //表情不处理
  11154. if (
  11155. opt.imageBlockLine &&
  11156. ci.tagName.toLowerCase() == 'img' &&
  11157. !ci.getAttribute('emotion')
  11158. ) {
  11159. if (html) {
  11160. var img = ci
  11161. switch (opt.imageBlockLine) {
  11162. case 'left':
  11163. case 'right':
  11164. case 'none':
  11165. var pN = img.parentNode,
  11166. tmpNode,
  11167. pre,
  11168. next
  11169. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  11170. pN = pN.parentNode
  11171. }
  11172. tmpNode = pN
  11173. if (
  11174. tmpNode.tagName == 'P' &&
  11175. domUtils.getStyle(tmpNode, 'text-align') == 'center'
  11176. ) {
  11177. if (
  11178. !domUtils.isBody(tmpNode) &&
  11179. domUtils.getChildCount(tmpNode, function (node) {
  11180. return (
  11181. !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  11182. )
  11183. }) == 1
  11184. ) {
  11185. pre = tmpNode.previousSibling
  11186. next = tmpNode.nextSibling
  11187. if (
  11188. pre &&
  11189. next &&
  11190. pre.nodeType == 1 &&
  11191. next.nodeType == 1 &&
  11192. pre.tagName == next.tagName &&
  11193. domUtils.isBlockElm(pre)
  11194. ) {
  11195. pre.appendChild(tmpNode.firstChild)
  11196. while (next.firstChild) {
  11197. pre.appendChild(next.firstChild)
  11198. }
  11199. domUtils.remove(tmpNode)
  11200. domUtils.remove(next)
  11201. } else {
  11202. domUtils.setStyle(tmpNode, 'text-align', '')
  11203. }
  11204. }
  11205. }
  11206. domUtils.setStyle(img, 'float', opt.imageBlockLine)
  11207. break
  11208. case 'center':
  11209. if (me.queryCommandValue('imagefloat') != 'center') {
  11210. pN = img.parentNode
  11211. domUtils.setStyle(img, 'float', 'none')
  11212. tmpNode = img
  11213. while (
  11214. pN &&
  11215. domUtils.getChildCount(pN, function (node) {
  11216. return (
  11217. !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  11218. )
  11219. }) == 1 &&
  11220. (dtd.$inline[pN.tagName] || pN.tagName == 'A')
  11221. ) {
  11222. tmpNode = pN
  11223. pN = pN.parentNode
  11224. }
  11225. var pNode = me.document.createElement('p')
  11226. domUtils.setAttributes(pNode, {
  11227. style: 'text-align:center',
  11228. })
  11229. tmpNode.parentNode.insertBefore(pNode, tmpNode)
  11230. pNode.appendChild(tmpNode)
  11231. domUtils.setStyle(tmpNode, 'float', '')
  11232. }
  11233. }
  11234. } else {
  11235. var range = me.selection.getRange()
  11236. range.selectNode(ci).select()
  11237. me.execCommand('imagefloat', opt.imageBlockLine)
  11238. }
  11239. }
  11240. //去掉冗余的标签
  11241. if (opt.removeEmptyNode) {
  11242. if (
  11243. opt.removeTagNames[ci.tagName.toLowerCase()] &&
  11244. domUtils.hasNoAttributes(ci) &&
  11245. domUtils.isEmptyBlock(ci)
  11246. ) {
  11247. domUtils.remove(ci)
  11248. }
  11249. }
  11250. }
  11251. if (opt.tobdc) {
  11252. var root = UE.htmlparser(cont.innerHTML)
  11253. root.traversal(function (node) {
  11254. if (node.type == 'text') {
  11255. node.data = ToDBC(node.data)
  11256. }
  11257. })
  11258. cont.innerHTML = root.toHtml()
  11259. }
  11260. if (opt.bdc2sb) {
  11261. var root = UE.htmlparser(cont.innerHTML)
  11262. root.traversal(function (node) {
  11263. if (node.type == 'text') {
  11264. node.data = DBC2SB(node.data)
  11265. }
  11266. })
  11267. cont.innerHTML = root.toHtml()
  11268. }
  11269. if (html) {
  11270. html.html = cont.innerHTML
  11271. }
  11272. }
  11273. if (opt.pasteFilter) {
  11274. me.addListener('beforepaste', autotype)
  11275. }
  11276. function DBC2SB(str) {
  11277. var result = ''
  11278. for (var i = 0; i < str.length; i++) {
  11279. var code = str.charCodeAt(i) //获取当前字符的unicode编码
  11280. if (code >= 65281 && code <= 65373) {
  11281. //在这个unicode编码范围中的是所有的英文字母已经各种字符
  11282. result += String.fromCharCode(str.charCodeAt(i) - 65248) //把全角字符的unicode编码转换为对应半角字符的unicode码
  11283. } else if (code == 12288) {
  11284. //空格
  11285. result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32)
  11286. } else {
  11287. result += str.charAt(i)
  11288. }
  11289. }
  11290. return result
  11291. }
  11292. function ToDBC(txtstring) {
  11293. txtstring = utils.html(txtstring)
  11294. var tmp = ''
  11295. var mark = '' /*用于判断,如果是html尖括里的标记,则不进行全角的转换*/
  11296. for (var i = 0; i < txtstring.length; i++) {
  11297. if (txtstring.charCodeAt(i) == 32) {
  11298. tmp = tmp + String.fromCharCode(12288)
  11299. } else if (txtstring.charCodeAt(i) < 127) {
  11300. tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248)
  11301. } else {
  11302. tmp += txtstring.charAt(i)
  11303. }
  11304. }
  11305. return tmp
  11306. }
  11307. function readLocalOpts() {
  11308. var cookieOpt = me.getPreferences('autotypeset')
  11309. utils.extend(me.options.autotypeset, cookieOpt)
  11310. }
  11311. me.commands['autotypeset'] = {
  11312. execCommand: function () {
  11313. me.removeListener('beforepaste', autotype)
  11314. if (opt.pasteFilter) {
  11315. me.addListener('beforepaste', autotype)
  11316. }
  11317. autotype.call(me)
  11318. },
  11319. }
  11320. }
  11321. // plugins/autosubmit.js
  11322. /**
  11323. * 快捷键提交
  11324. * @file
  11325. * @since 1.2.6.1
  11326. */
  11327. /**
  11328. * 提交表单
  11329. * @command autosubmit
  11330. * @method execCommand
  11331. * @param { String } cmd 命令字符串
  11332. * @example
  11333. * ```javascript
  11334. * editor.execCommand( 'autosubmit' );
  11335. * ```
  11336. */
  11337. UE.plugin.register('autosubmit', function () {
  11338. return {
  11339. shortcutkey: {
  11340. autosubmit: 'ctrl+13', //手动提交
  11341. },
  11342. commands: {
  11343. autosubmit: {
  11344. execCommand: function () {
  11345. var me = this,
  11346. form = domUtils.findParentByTagName(me.iframe, 'form', false)
  11347. if (form) {
  11348. if (me.fireEvent('beforesubmit') === false) {
  11349. return
  11350. }
  11351. me.sync()
  11352. form.submit()
  11353. }
  11354. },
  11355. },
  11356. },
  11357. }
  11358. })
  11359. // plugins/background.js
  11360. /**
  11361. * 背景插件,为UEditor提供设置背景功能
  11362. * @file
  11363. * @since 1.2.6.1
  11364. */
  11365. UE.plugin.register('background', function () {
  11366. var me = this,
  11367. cssRuleId = 'editor_background',
  11368. isSetColored,
  11369. reg = new RegExp('body[\\s]*\\{(.+)\\}', 'i')
  11370. function stringToObj(str) {
  11371. var obj = {},
  11372. styles = str.split(';')
  11373. utils.each(styles, function (v) {
  11374. var index = v.indexOf(':'),
  11375. key = utils.trim(v.substr(0, index)).toLowerCase()
  11376. key && (obj[key] = utils.trim(v.substr(index + 1) || ''))
  11377. })
  11378. return obj
  11379. }
  11380. function setBackground(obj) {
  11381. if (obj) {
  11382. var styles = []
  11383. for (var name in obj) {
  11384. if (obj.hasOwnProperty(name)) {
  11385. styles.push(name + ':' + obj[name] + '; ')
  11386. }
  11387. }
  11388. utils.cssRule(
  11389. cssRuleId,
  11390. styles.length ? 'body{' + styles.join('') + '}' : '',
  11391. me.document
  11392. )
  11393. } else {
  11394. utils.cssRule(cssRuleId, '', me.document)
  11395. }
  11396. }
  11397. //重写editor.hasContent方法
  11398. var orgFn = me.hasContents
  11399. me.hasContents = function () {
  11400. if (me.queryCommandValue('background')) {
  11401. return true
  11402. }
  11403. return orgFn.apply(me, arguments)
  11404. }
  11405. return {
  11406. bindEvents: {
  11407. getAllHtml: function (type, headHtml) {
  11408. var body = this.body,
  11409. su = domUtils.getComputedStyle(body, 'background-image'),
  11410. url = ''
  11411. if (su.indexOf(me.options.imagePath) > 0) {
  11412. url = su
  11413. .substring(su.indexOf(me.options.imagePath), su.length - 1)
  11414. .replace(/"|\(|\)/gi, '')
  11415. } else {
  11416. url = su != 'none' ? su.replace(/url\("?|"?\)/gi, '') : ''
  11417. }
  11418. var html = '<style type="text/css">body{'
  11419. var bgObj = {
  11420. 'background-color':
  11421. domUtils.getComputedStyle(body, 'background-color') || '#ffffff',
  11422. 'background-image': url ? 'url(' + url + ')' : '',
  11423. 'background-repeat':
  11424. domUtils.getComputedStyle(body, 'background-repeat') || '',
  11425. 'background-position': browser.ie
  11426. ? domUtils.getComputedStyle(body, 'background-position-x') +
  11427. ' ' +
  11428. domUtils.getComputedStyle(body, 'background-position-y')
  11429. : domUtils.getComputedStyle(body, 'background-position'),
  11430. height: domUtils.getComputedStyle(body, 'height'),
  11431. }
  11432. for (var name in bgObj) {
  11433. if (bgObj.hasOwnProperty(name)) {
  11434. html += name + ':' + bgObj[name] + '; '
  11435. }
  11436. }
  11437. html += '}</style> '
  11438. headHtml.push(html)
  11439. },
  11440. aftersetcontent: function () {
  11441. if (isSetColored == false) setBackground()
  11442. },
  11443. },
  11444. inputRule: function (root) {
  11445. isSetColored = false
  11446. utils.each(root.getNodesByTagName('p'), function (p) {
  11447. var styles = p.getAttr('data-background')
  11448. if (styles) {
  11449. isSetColored = true
  11450. setBackground(stringToObj(styles))
  11451. p.parentNode.removeChild(p)
  11452. }
  11453. })
  11454. },
  11455. outputRule: function (root) {
  11456. var me = this,
  11457. styles = (utils.cssRule(cssRuleId, me.document) || '')
  11458. .replace(/[\n\r]+/g, '')
  11459. .match(reg)
  11460. if (styles) {
  11461. root.appendChild(
  11462. UE.uNode.createElement(
  11463. '<p style="display:none;" data-background="' +
  11464. utils.trim(styles[1].replace(/"/g, '').replace(/[\s]+/g, ' ')) +
  11465. '"><br/></p>'
  11466. )
  11467. )
  11468. }
  11469. },
  11470. commands: {
  11471. background: {
  11472. execCommand: function (cmd, obj) {
  11473. setBackground(obj)
  11474. },
  11475. queryCommandValue: function () {
  11476. var me = this,
  11477. styles = (utils.cssRule(cssRuleId, me.document) || '')
  11478. .replace(/[\n\r]+/g, '')
  11479. .match(reg)
  11480. return styles ? stringToObj(styles[1]) : null
  11481. },
  11482. notNeedUndo: true,
  11483. },
  11484. },
  11485. }
  11486. })
  11487. // plugins/image.js
  11488. /**
  11489. * 图片插入、排版插件
  11490. * @file
  11491. * @since 1.2.6.1
  11492. */
  11493. /**
  11494. * 图片对齐方式
  11495. * @command imagefloat
  11496. * @method execCommand
  11497. * @remind 值center为独占一行居中
  11498. * @param { String } cmd 命令字符串
  11499. * @param { String } align 对齐方式,可传left、right、none、center
  11500. * @remaind center表示图片独占一行
  11501. * @example
  11502. * ```javascript
  11503. * editor.execCommand( 'imagefloat', 'center' );
  11504. * ```
  11505. */
  11506. /**
  11507. * 如果选区所在位置是图片区域
  11508. * @command imagefloat
  11509. * @method queryCommandValue
  11510. * @param { String } cmd 命令字符串
  11511. * @return { String } 返回图片对齐方式
  11512. * @example
  11513. * ```javascript
  11514. * editor.queryCommandValue( 'imagefloat' );
  11515. * ```
  11516. */
  11517. UE.commands['imagefloat'] = {
  11518. execCommand: function (cmd, align) {
  11519. var me = this,
  11520. range = me.selection.getRange()
  11521. if (!range.collapsed) {
  11522. var img = range.getClosedNode()
  11523. if (img && img.tagName == 'IMG') {
  11524. switch (align) {
  11525. case 'left':
  11526. case 'right':
  11527. case 'none':
  11528. var pN = img.parentNode,
  11529. tmpNode,
  11530. pre,
  11531. next
  11532. while (dtd.$inline[pN.tagName] || pN.tagName == 'A') {
  11533. pN = pN.parentNode
  11534. }
  11535. tmpNode = pN
  11536. if (
  11537. tmpNode.tagName == 'P' &&
  11538. domUtils.getStyle(tmpNode, 'text-align') == 'center'
  11539. ) {
  11540. if (
  11541. !domUtils.isBody(tmpNode) &&
  11542. domUtils.getChildCount(tmpNode, function (node) {
  11543. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  11544. }) == 1
  11545. ) {
  11546. pre = tmpNode.previousSibling
  11547. next = tmpNode.nextSibling
  11548. if (
  11549. pre &&
  11550. next &&
  11551. pre.nodeType == 1 &&
  11552. next.nodeType == 1 &&
  11553. pre.tagName == next.tagName &&
  11554. domUtils.isBlockElm(pre)
  11555. ) {
  11556. pre.appendChild(tmpNode.firstChild)
  11557. while (next.firstChild) {
  11558. pre.appendChild(next.firstChild)
  11559. }
  11560. domUtils.remove(tmpNode)
  11561. domUtils.remove(next)
  11562. } else {
  11563. domUtils.setStyle(tmpNode, 'text-align', '')
  11564. }
  11565. }
  11566. range.selectNode(img).select()
  11567. }
  11568. domUtils.setStyle(img, 'float', align == 'none' ? '' : align)
  11569. if (align == 'none') {
  11570. domUtils.removeAttributes(img, 'align')
  11571. }
  11572. break
  11573. case 'center':
  11574. if (me.queryCommandValue('imagefloat') != 'center') {
  11575. pN = img.parentNode
  11576. domUtils.setStyle(img, 'float', '')
  11577. domUtils.removeAttributes(img, 'align')
  11578. tmpNode = img
  11579. while (
  11580. pN &&
  11581. domUtils.getChildCount(pN, function (node) {
  11582. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  11583. }) == 1 &&
  11584. (dtd.$inline[pN.tagName] || pN.tagName == 'A')
  11585. ) {
  11586. tmpNode = pN
  11587. pN = pN.parentNode
  11588. }
  11589. range.setStartBefore(tmpNode).setCursor(false)
  11590. pN = me.document.createElement('div')
  11591. pN.appendChild(tmpNode)
  11592. domUtils.setStyle(tmpNode, 'float', '')
  11593. me.execCommand(
  11594. 'insertHtml',
  11595. '<p id="_img_parent_tmp" style="text-align:center">' +
  11596. pN.innerHTML +
  11597. '</p>'
  11598. )
  11599. tmpNode = me.document.getElementById('_img_parent_tmp')
  11600. tmpNode.removeAttribute('id')
  11601. tmpNode = tmpNode.firstChild
  11602. range.selectNode(tmpNode).select()
  11603. //去掉后边多余的元素
  11604. next = tmpNode.parentNode.nextSibling
  11605. if (next && domUtils.isEmptyNode(next)) {
  11606. domUtils.remove(next)
  11607. }
  11608. }
  11609. break
  11610. }
  11611. }
  11612. }
  11613. },
  11614. queryCommandValue: function () {
  11615. var range = this.selection.getRange(),
  11616. startNode,
  11617. floatStyle
  11618. if (range.collapsed) {
  11619. return 'none'
  11620. }
  11621. startNode = range.getClosedNode()
  11622. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  11623. floatStyle =
  11624. domUtils.getComputedStyle(startNode, 'float') ||
  11625. startNode.getAttribute('align')
  11626. if (floatStyle == 'none') {
  11627. floatStyle =
  11628. domUtils.getComputedStyle(startNode.parentNode, 'text-align') ==
  11629. 'center'
  11630. ? 'center'
  11631. : floatStyle
  11632. }
  11633. return {
  11634. left: 1,
  11635. right: 1,
  11636. center: 1,
  11637. }[floatStyle]
  11638. ? floatStyle
  11639. : 'none'
  11640. }
  11641. return 'none'
  11642. },
  11643. queryCommandState: function () {
  11644. var range = this.selection.getRange(),
  11645. startNode
  11646. if (range.collapsed) return -1
  11647. startNode = range.getClosedNode()
  11648. if (startNode && startNode.nodeType == 1 && startNode.tagName == 'IMG') {
  11649. return 0
  11650. }
  11651. return -1
  11652. },
  11653. }
  11654. /**
  11655. * 插入图片
  11656. * @command insertimage
  11657. * @method execCommand
  11658. * @param { String } cmd 命令字符串
  11659. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  11660. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  11661. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  11662. * @example
  11663. * ```javascript
  11664. * editor.execCommand( 'insertimage', {
  11665. * src:'a/b/c.jpg',
  11666. * width:'100',
  11667. * height:'100'
  11668. * } );
  11669. * ```
  11670. * @example
  11671. * ```javascript
  11672. * editor.execCommand( 'insertimage', [{
  11673. * src:'a/b/c.jpg',
  11674. * width:'100',
  11675. * height:'100'
  11676. * },{
  11677. * src:'a/b/d.jpg',
  11678. * width:'100',
  11679. * height:'100'
  11680. * }] );
  11681. * ```
  11682. */
  11683. UE.commands['insertimage'] = {
  11684. execCommand: function (cmd, opt) {
  11685. opt = utils.isArray(opt) ? opt : [opt]
  11686. if (!opt.length) {
  11687. return
  11688. }
  11689. var me = this,
  11690. range = me.selection.getRange(),
  11691. img = range.getClosedNode()
  11692. if (me.fireEvent('beforeinsertimage', opt) === true) {
  11693. return
  11694. }
  11695. function unhtmlData(imgCi) {
  11696. utils.each(
  11697. 'width,height,border,hspace,vspace'.split(','),
  11698. function (item) {
  11699. if (imgCi[item]) {
  11700. imgCi[item] = parseInt(imgCi[item], 10) || 0
  11701. }
  11702. }
  11703. )
  11704. utils.each('src,_src'.split(','), function (item) {
  11705. if (imgCi[item]) {
  11706. imgCi[item] = utils.unhtmlForUrl(imgCi[item])
  11707. }
  11708. })
  11709. utils.each('title,alt'.split(','), function (item) {
  11710. if (imgCi[item]) {
  11711. imgCi[item] = utils.unhtml(imgCi[item])
  11712. }
  11713. })
  11714. }
  11715. if (
  11716. img &&
  11717. /img/i.test(img.tagName) &&
  11718. (img.className != 'edui-faked-video' ||
  11719. img.className.indexOf('edui-upload-video') != -1) &&
  11720. !img.getAttribute('word_img')
  11721. ) {
  11722. var first = opt.shift()
  11723. var floatStyle = first['floatStyle']
  11724. delete first['floatStyle']
  11725. //// img.style.border = (first.border||0) +"px solid #000";
  11726. //// img.style.margin = (first.margin||0) +"px";
  11727. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  11728. domUtils.setAttributes(img, first)
  11729. me.execCommand('imagefloat', floatStyle)
  11730. if (opt.length > 0) {
  11731. range.setStartAfter(img).setCursor(false, true)
  11732. me.execCommand('insertimage', opt)
  11733. }
  11734. } else {
  11735. var html = [],
  11736. str = '',
  11737. ci
  11738. ci = opt[0]
  11739. if (opt.length == 1) {
  11740. unhtmlData(ci)
  11741. //此处拼接富文本上传图片地址
  11742. str =
  11743. '<img src="' +
  11744. base_url +
  11745. ci.src +
  11746. '" ' +
  11747. (ci._src ? ' _src="' + ci._src + '" ' : '') +
  11748. (ci.width ? 'width="' + ci.width + '" ' : '') +
  11749. (ci.height ? ' height="' + ci.height + '" ' : '') +
  11750. (ci['floatStyle'] == 'left' || ci['floatStyle'] == 'right'
  11751. ? ' style="float:' + ci['floatStyle'] + ';"'
  11752. : '') +
  11753. (ci.title && ci.title != '' ? ' title="' + ci.title + '"' : '') +
  11754. (ci.border && ci.border != '0'
  11755. ? ' border="' + ci.border + '"'
  11756. : '') +
  11757. (ci.alt && ci.alt != '' ? ' alt="' + ci.alt + '"' : '') +
  11758. (ci.hspace && ci.hspace != '0'
  11759. ? ' hspace = "' + ci.hspace + '"'
  11760. : '') +
  11761. (ci.vspace && ci.vspace != '0'
  11762. ? ' vspace = "' + ci.vspace + '"'
  11763. : '') +
  11764. '/>'
  11765. if (ci['floatStyle'] == 'center') {
  11766. str = '<p style="text-align: center">' + str + '</p>'
  11767. }
  11768. html.push(str)
  11769. } else {
  11770. //此处拼接多文件上传图片展示地址
  11771. for (var i = 0; (ci = opt[i++]); ) {
  11772. unhtmlData(ci)
  11773. str =
  11774. '<p ' +
  11775. (ci['floatStyle'] == 'center'
  11776. ? 'style="text-align: center" '
  11777. : '') +
  11778. '><img src="' +
  11779. base_url +
  11780. ci.src +
  11781. '" ' +
  11782. (ci.width ? 'width="' + ci.width + '" ' : '') +
  11783. (ci._src ? ' _src="' + ci._src + '" ' : '') +
  11784. (ci.height ? ' height="' + ci.height + '" ' : '') +
  11785. ' style="' +
  11786. (ci['floatStyle'] && ci['floatStyle'] != 'center'
  11787. ? 'float:' + ci['floatStyle'] + ';'
  11788. : '') +
  11789. (ci.border || '') +
  11790. '" ' +
  11791. (ci.title ? ' title="' + ci.title + '"' : '') +
  11792. ' /></p>'
  11793. html.push(str)
  11794. }
  11795. }
  11796. me.execCommand('insertHtml', html.join(''))
  11797. }
  11798. me.fireEvent('afterinsertimage', opt)
  11799. },
  11800. }
  11801. // plugins/justify.js
  11802. /**
  11803. * 段落格式
  11804. * @file
  11805. * @since 1.2.6.1
  11806. */
  11807. /**
  11808. * 段落对齐方式
  11809. * @command justify
  11810. * @method execCommand
  11811. * @param { String } cmd 命令字符串
  11812. * @param { String } align 对齐方式:left => 居左,right => 居右,center => 居中,justify => 两端对齐
  11813. * @example
  11814. * ```javascript
  11815. * editor.execCommand( 'justify', 'center' );
  11816. * ```
  11817. */
  11818. /**
  11819. * 如果选区所在位置是段落区域,返回当前段落对齐方式
  11820. * @command justify
  11821. * @method queryCommandValue
  11822. * @param { String } cmd 命令字符串
  11823. * @return { String } 返回段落对齐方式
  11824. * @example
  11825. * ```javascript
  11826. * editor.queryCommandValue( 'justify' );
  11827. * ```
  11828. */
  11829. UE.plugins['justify'] = function () {
  11830. var me = this,
  11831. block = domUtils.isBlockElm,
  11832. defaultValue = {
  11833. left: 1,
  11834. right: 1,
  11835. center: 1,
  11836. justify: 1,
  11837. },
  11838. doJustify = function (range, style) {
  11839. var bookmark = range.createBookmark(),
  11840. filterFn = function (node) {
  11841. return node.nodeType == 1
  11842. ? node.tagName.toLowerCase() != 'br' &&
  11843. !domUtils.isBookmarkNode(node)
  11844. : !domUtils.isWhitespace(node)
  11845. }
  11846. range.enlarge(true)
  11847. var bookmark2 = range.createBookmark(),
  11848. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  11849. tmpRange = range.cloneRange(),
  11850. tmpNode
  11851. while (
  11852. current &&
  11853. !(
  11854. domUtils.getPosition(current, bookmark2.end) &
  11855. domUtils.POSITION_FOLLOWING
  11856. )
  11857. ) {
  11858. if (current.nodeType == 3 || !block(current)) {
  11859. tmpRange.setStartBefore(current)
  11860. while (current && current !== bookmark2.end && !block(current)) {
  11861. tmpNode = current
  11862. current = domUtils.getNextDomNode(
  11863. current,
  11864. false,
  11865. null,
  11866. function (node) {
  11867. return !block(node)
  11868. }
  11869. )
  11870. }
  11871. tmpRange.setEndAfter(tmpNode)
  11872. var common = tmpRange.getCommonAncestor()
  11873. if (!domUtils.isBody(common) && block(common)) {
  11874. domUtils.setStyles(
  11875. common,
  11876. utils.isString(style) ? { 'text-align': style } : style
  11877. )
  11878. current = common
  11879. } else {
  11880. var p = range.document.createElement('p')
  11881. domUtils.setStyles(
  11882. p,
  11883. utils.isString(style) ? { 'text-align': style } : style
  11884. )
  11885. var frag = tmpRange.extractContents()
  11886. p.appendChild(frag)
  11887. tmpRange.insertNode(p)
  11888. current = p
  11889. }
  11890. current = domUtils.getNextDomNode(current, false, filterFn)
  11891. } else {
  11892. current = domUtils.getNextDomNode(current, true, filterFn)
  11893. }
  11894. }
  11895. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark)
  11896. }
  11897. UE.commands['justify'] = {
  11898. execCommand: function (cmdName, align) {
  11899. var range = this.selection.getRange(),
  11900. txt
  11901. //闭合时单独处理
  11902. if (range.collapsed) {
  11903. txt = this.document.createTextNode('p')
  11904. range.insertNode(txt)
  11905. }
  11906. doJustify(range, align)
  11907. if (txt) {
  11908. range.setStartBefore(txt).collapse(true)
  11909. domUtils.remove(txt)
  11910. }
  11911. range.select()
  11912. return true
  11913. },
  11914. queryCommandValue: function () {
  11915. var startNode = this.selection.getStart(),
  11916. value = domUtils.getComputedStyle(startNode, 'text-align')
  11917. return defaultValue[value] ? value : 'left'
  11918. },
  11919. queryCommandState: function () {
  11920. var start = this.selection.getStart(),
  11921. cell =
  11922. start &&
  11923. domUtils.findParentByTagName(start, ['td', 'th', 'caption'], true)
  11924. return cell ? -1 : 0
  11925. },
  11926. }
  11927. }
  11928. // plugins/font.js
  11929. /**
  11930. * 字体颜色,背景色,字号,字体,下划线,删除线
  11931. * @file
  11932. * @since 1.2.6.1
  11933. */
  11934. /**
  11935. * 字体颜色
  11936. * @command forecolor
  11937. * @method execCommand
  11938. * @param { String } cmd 命令字符串
  11939. * @param { String } value 色值(必须十六进制)
  11940. * @example
  11941. * ```javascript
  11942. * editor.execCommand( 'forecolor', '#000' );
  11943. * ```
  11944. */
  11945. /**
  11946. * 返回选区字体颜色
  11947. * @command forecolor
  11948. * @method queryCommandValue
  11949. * @param { String } cmd 命令字符串
  11950. * @return { String } 返回字体颜色
  11951. * @example
  11952. * ```javascript
  11953. * editor.queryCommandValue( 'forecolor' );
  11954. * ```
  11955. */
  11956. /**
  11957. * 字体背景颜色
  11958. * @command backcolor
  11959. * @method execCommand
  11960. * @param { String } cmd 命令字符串
  11961. * @param { String } value 色值(必须十六进制)
  11962. * @example
  11963. * ```javascript
  11964. * editor.execCommand( 'backcolor', '#000' );
  11965. * ```
  11966. */
  11967. /**
  11968. * 返回选区字体颜色
  11969. * @command backcolor
  11970. * @method queryCommandValue
  11971. * @param { String } cmd 命令字符串
  11972. * @return { String } 返回字体背景颜色
  11973. * @example
  11974. * ```javascript
  11975. * editor.queryCommandValue( 'backcolor' );
  11976. * ```
  11977. */
  11978. /**
  11979. * 字体大小
  11980. * @command fontsize
  11981. * @method execCommand
  11982. * @param { String } cmd 命令字符串
  11983. * @param { String } value 字体大小
  11984. * @example
  11985. * ```javascript
  11986. * editor.execCommand( 'fontsize', '14px' );
  11987. * ```
  11988. */
  11989. /**
  11990. * 返回选区字体大小
  11991. * @command fontsize
  11992. * @method queryCommandValue
  11993. * @param { String } cmd 命令字符串
  11994. * @return { String } 返回字体大小
  11995. * @example
  11996. * ```javascript
  11997. * editor.queryCommandValue( 'fontsize' );
  11998. * ```
  11999. */
  12000. /**
  12001. * 字体样式
  12002. * @command fontfamily
  12003. * @method execCommand
  12004. * @param { String } cmd 命令字符串
  12005. * @param { String } value 字体样式
  12006. * @example
  12007. * ```javascript
  12008. * editor.execCommand( 'fontfamily', '微软雅黑' );
  12009. * ```
  12010. */
  12011. /**
  12012. * 返回选区字体样式
  12013. * @command fontfamily
  12014. * @method queryCommandValue
  12015. * @param { String } cmd 命令字符串
  12016. * @return { String } 返回字体样式
  12017. * @example
  12018. * ```javascript
  12019. * editor.queryCommandValue( 'fontfamily' );
  12020. * ```
  12021. */
  12022. /**
  12023. * 字体下划线,与删除线互斥
  12024. * @command underline
  12025. * @method execCommand
  12026. * @param { String } cmd 命令字符串
  12027. * @example
  12028. * ```javascript
  12029. * editor.execCommand( 'underline' );
  12030. * ```
  12031. */
  12032. /**
  12033. * 字体删除线,与下划线互斥
  12034. * @command strikethrough
  12035. * @method execCommand
  12036. * @param { String } cmd 命令字符串
  12037. * @example
  12038. * ```javascript
  12039. * editor.execCommand( 'strikethrough' );
  12040. * ```
  12041. */
  12042. /**
  12043. * 字体边框
  12044. * @command fontborder
  12045. * @method execCommand
  12046. * @param { String } cmd 命令字符串
  12047. * @example
  12048. * ```javascript
  12049. * editor.execCommand( 'fontborder' );
  12050. * ```
  12051. */
  12052. UE.plugins['font'] = function () {
  12053. var me = this,
  12054. fonts = {
  12055. forecolor: 'color',
  12056. backcolor: 'background-color',
  12057. fontsize: 'font-size',
  12058. fontfamily: 'font-family',
  12059. underline: 'text-decoration',
  12060. strikethrough: 'text-decoration',
  12061. fontborder: 'border',
  12062. },
  12063. needCmd = { underline: 1, strikethrough: 1, fontborder: 1 },
  12064. needSetChild = {
  12065. forecolor: 'color',
  12066. backcolor: 'background-color',
  12067. fontsize: 'font-size',
  12068. fontfamily: 'font-family',
  12069. }
  12070. me.setOpt({
  12071. fontfamily: [
  12072. { name: 'songti', val: '宋体,SimSun' },
  12073. { name: 'yahei', val: '微软雅黑,Microsoft YaHei' },
  12074. { name: 'kaiti', val: '楷体,楷体_GB2312, SimKai' },
  12075. { name: 'heiti', val: '黑体, SimHei' },
  12076. { name: 'lishu', val: '隶书, SimLi' },
  12077. { name: 'andaleMono', val: 'andale mono' },
  12078. { name: 'arial', val: 'arial, helvetica,sans-serif' },
  12079. { name: 'arialBlack', val: 'arial black,avant garde' },
  12080. { name: 'comicSansMs', val: 'comic sans ms' },
  12081. { name: 'impact', val: 'impact,chicago' },
  12082. { name: 'timesNewRoman', val: 'times new roman' },
  12083. ],
  12084. fontsize: [10, 11, 12, 14, 16, 18, 20, 24, 36],
  12085. })
  12086. function mergeWithParent(node) {
  12087. var parent
  12088. while ((parent = node.parentNode)) {
  12089. if (
  12090. parent.tagName == 'SPAN' &&
  12091. domUtils.getChildCount(parent, function (child) {
  12092. return !domUtils.isBookmarkNode(child) && !domUtils.isBr(child)
  12093. }) == 1
  12094. ) {
  12095. parent.style.cssText += node.style.cssText
  12096. domUtils.remove(node, true)
  12097. node = parent
  12098. } else {
  12099. break
  12100. }
  12101. }
  12102. }
  12103. function mergeChild(rng, cmdName, value) {
  12104. if (needSetChild[cmdName]) {
  12105. rng.adjustmentBoundary()
  12106. if (!rng.collapsed && rng.startContainer.nodeType == 1) {
  12107. var start = rng.startContainer.childNodes[rng.startOffset]
  12108. if (start && domUtils.isTagNode(start, 'span')) {
  12109. var bk = rng.createBookmark()
  12110. utils.each(
  12111. domUtils.getElementsByTagName(start, 'span'),
  12112. function (span) {
  12113. if (!span.parentNode || domUtils.isBookmarkNode(span)) return
  12114. if (
  12115. cmdName == 'backcolor' &&
  12116. domUtils
  12117. .getComputedStyle(span, 'background-color')
  12118. .toLowerCase() === value
  12119. ) {
  12120. return
  12121. }
  12122. domUtils.removeStyle(span, needSetChild[cmdName])
  12123. if (span.style.cssText.replace(/^\s+$/, '').length == 0) {
  12124. domUtils.remove(span, true)
  12125. }
  12126. }
  12127. )
  12128. rng.moveToBookmark(bk)
  12129. }
  12130. }
  12131. }
  12132. }
  12133. function mergesibling(rng, cmdName, value) {
  12134. var collapsed = rng.collapsed,
  12135. bk = rng.createBookmark(),
  12136. common
  12137. if (collapsed) {
  12138. common = bk.start.parentNode
  12139. while (dtd.$inline[common.tagName]) {
  12140. common = common.parentNode
  12141. }
  12142. } else {
  12143. common = domUtils.getCommonAncestor(bk.start, bk.end)
  12144. }
  12145. utils.each(
  12146. domUtils.getElementsByTagName(common, 'span'),
  12147. function (span) {
  12148. if (!span.parentNode || domUtils.isBookmarkNode(span)) return
  12149. if (/\s*border\s*:\s*none;?\s*/i.test(span.style.cssText)) {
  12150. if (/^\s*border\s*:\s*none;?\s*$/.test(span.style.cssText)) {
  12151. domUtils.remove(span, true)
  12152. } else {
  12153. domUtils.removeStyle(span, 'border')
  12154. }
  12155. return
  12156. }
  12157. if (
  12158. /border/i.test(span.style.cssText) &&
  12159. span.parentNode.tagName == 'SPAN' &&
  12160. /border/i.test(span.parentNode.style.cssText)
  12161. ) {
  12162. span.style.cssText = span.style.cssText.replace(
  12163. /border[^:]*:[^;]+;?/gi,
  12164. ''
  12165. )
  12166. }
  12167. if (!(cmdName == 'fontborder' && value == 'none')) {
  12168. var next = span.nextSibling
  12169. while (next && next.nodeType == 1 && next.tagName == 'SPAN') {
  12170. if (domUtils.isBookmarkNode(next) && cmdName == 'fontborder') {
  12171. span.appendChild(next)
  12172. next = span.nextSibling
  12173. continue
  12174. }
  12175. if (next.style.cssText == span.style.cssText) {
  12176. domUtils.moveChild(next, span)
  12177. domUtils.remove(next)
  12178. }
  12179. if (span.nextSibling === next) break
  12180. next = span.nextSibling
  12181. }
  12182. }
  12183. mergeWithParent(span)
  12184. if (browser.ie && browser.version > 8) {
  12185. //拷贝父亲们的特别的属性,这里只做背景颜色的处理
  12186. var parent = domUtils.findParent(span, function (n) {
  12187. return (
  12188. n.tagName == 'SPAN' && /background-color/.test(n.style.cssText)
  12189. )
  12190. })
  12191. if (parent && !/background-color/.test(span.style.cssText)) {
  12192. span.style.backgroundColor = parent.style.backgroundColor
  12193. }
  12194. }
  12195. }
  12196. )
  12197. rng.moveToBookmark(bk)
  12198. mergeChild(rng, cmdName, value)
  12199. }
  12200. me.addInputRule(function (root) {
  12201. utils.each(
  12202. root.getNodesByTagName('u s del font strike'),
  12203. function (node) {
  12204. if (node.tagName == 'font') {
  12205. var cssStyle = []
  12206. for (var p in node.attrs) {
  12207. switch (p) {
  12208. case 'size':
  12209. cssStyle.push(
  12210. 'font-size:' +
  12211. ({
  12212. 1: '10',
  12213. 2: '12',
  12214. 3: '16',
  12215. 4: '18',
  12216. 5: '24',
  12217. 6: '32',
  12218. 7: '48',
  12219. }[node.attrs[p]] || node.attrs[p]) +
  12220. 'px'
  12221. )
  12222. break
  12223. case 'color':
  12224. cssStyle.push('color:' + node.attrs[p])
  12225. break
  12226. case 'face':
  12227. cssStyle.push('font-family:' + node.attrs[p])
  12228. break
  12229. case 'style':
  12230. cssStyle.push(node.attrs[p])
  12231. }
  12232. }
  12233. node.attrs = {
  12234. style: cssStyle.join(';'),
  12235. }
  12236. } else {
  12237. var val = node.tagName == 'u' ? 'underline' : 'line-through'
  12238. node.attrs = {
  12239. style:
  12240. (node.getAttr('style') || '') + 'text-decoration:' + val + ';',
  12241. }
  12242. }
  12243. node.tagName = 'span'
  12244. }
  12245. )
  12246. // utils.each(root.getNodesByTagName('span'), function (node) {
  12247. // var val;
  12248. // if(val = node.getAttr('class')){
  12249. // if(/fontstrikethrough/.test(val)){
  12250. // node.setStyle('text-decoration','line-through');
  12251. // if(node.attrs['class']){
  12252. // node.attrs['class'] = node.attrs['class'].replace(/fontstrikethrough/,'');
  12253. // }else{
  12254. // node.setAttr('class')
  12255. // }
  12256. // }
  12257. // if(/fontborder/.test(val)){
  12258. // node.setStyle('border','1px solid #000');
  12259. // if(node.attrs['class']){
  12260. // node.attrs['class'] = node.attrs['class'].replace(/fontborder/,'');
  12261. // }else{
  12262. // node.setAttr('class')
  12263. // }
  12264. // }
  12265. // }
  12266. // });
  12267. })
  12268. // me.addOutputRule(function(root){
  12269. // utils.each(root.getNodesByTagName('span'), function (node) {
  12270. // var val;
  12271. // if(val = node.getStyle('text-decoration')){
  12272. // if(/line-through/.test(val)){
  12273. // if(node.attrs['class']){
  12274. // node.attrs['class'] += ' fontstrikethrough';
  12275. // }else{
  12276. // node.setAttr('class','fontstrikethrough')
  12277. // }
  12278. // }
  12279. //
  12280. // node.setStyle('text-decoration')
  12281. // }
  12282. // if(val = node.getStyle('border')){
  12283. // if(/1px/.test(val) && /solid/.test(val)){
  12284. // if(node.attrs['class']){
  12285. // node.attrs['class'] += ' fontborder';
  12286. //
  12287. // }else{
  12288. // node.setAttr('class','fontborder')
  12289. // }
  12290. // }
  12291. // node.setStyle('border')
  12292. //
  12293. // }
  12294. // });
  12295. // });
  12296. for (var p in fonts) {
  12297. ;(function (cmd, style) {
  12298. UE.commands[cmd] = {
  12299. execCommand: function (cmdName, value) {
  12300. value =
  12301. value ||
  12302. (this.queryCommandState(cmdName)
  12303. ? 'none'
  12304. : cmdName == 'underline'
  12305. ? 'underline'
  12306. : cmdName == 'fontborder'
  12307. ? '1px solid #000'
  12308. : 'line-through')
  12309. var me = this,
  12310. range = this.selection.getRange(),
  12311. text
  12312. if (value == 'default') {
  12313. if (range.collapsed) {
  12314. text = me.document.createTextNode('font')
  12315. range.insertNode(text).select()
  12316. }
  12317. me.execCommand('removeFormat', 'span,a', style)
  12318. if (text) {
  12319. range.setStartBefore(text).collapse(true)
  12320. domUtils.remove(text)
  12321. }
  12322. mergesibling(range, cmdName, value)
  12323. range.select()
  12324. } else {
  12325. if (!range.collapsed) {
  12326. if (needCmd[cmd] && me.queryCommandValue(cmd)) {
  12327. me.execCommand('removeFormat', 'span,a', style)
  12328. }
  12329. range = me.selection.getRange()
  12330. range.applyInlineStyle('span', { style: style + ':' + value })
  12331. mergesibling(range, cmdName, value)
  12332. range.select()
  12333. } else {
  12334. var span = domUtils.findParentByTagName(
  12335. range.startContainer,
  12336. 'span',
  12337. true
  12338. )
  12339. text = me.document.createTextNode('font')
  12340. if (
  12341. span &&
  12342. !span.children.length &&
  12343. !span[browser.ie ? 'innerText' : 'textContent'].replace(
  12344. fillCharReg,
  12345. ''
  12346. ).length
  12347. ) {
  12348. //for ie hack when enter
  12349. range.insertNode(text)
  12350. if (needCmd[cmd]) {
  12351. range.selectNode(text).select()
  12352. me.execCommand('removeFormat', 'span,a', style, null)
  12353. span = domUtils.findParentByTagName(text, 'span', true)
  12354. range.setStartBefore(text)
  12355. }
  12356. span && (span.style.cssText += ';' + style + ':' + value)
  12357. range.collapse(true).select()
  12358. } else {
  12359. range.insertNode(text)
  12360. range.selectNode(text).select()
  12361. span = range.document.createElement('span')
  12362. if (needCmd[cmd]) {
  12363. //a标签内的不处理跳过
  12364. if (domUtils.findParentByTagName(text, 'a', true)) {
  12365. range.setStartBefore(text).setCursor()
  12366. domUtils.remove(text)
  12367. return
  12368. }
  12369. me.execCommand('removeFormat', 'span,a', style)
  12370. }
  12371. span.style.cssText = style + ':' + value
  12372. text.parentNode.insertBefore(span, text)
  12373. //修复,span套span 但样式不继承的问题
  12374. if (!browser.ie || (browser.ie && browser.version == 9)) {
  12375. var spanParent = span.parentNode
  12376. while (!domUtils.isBlockElm(spanParent)) {
  12377. if (spanParent.tagName == 'SPAN') {
  12378. //opera合并style不会加入";"
  12379. span.style.cssText =
  12380. spanParent.style.cssText + ';' + span.style.cssText
  12381. }
  12382. spanParent = spanParent.parentNode
  12383. }
  12384. }
  12385. if (opera) {
  12386. setTimeout(function () {
  12387. range.setStart(span, 0).collapse(true)
  12388. mergesibling(range, cmdName, value)
  12389. range.select()
  12390. })
  12391. } else {
  12392. range.setStart(span, 0).collapse(true)
  12393. mergesibling(range, cmdName, value)
  12394. range.select()
  12395. }
  12396. //trace:981
  12397. //domUtils.mergeToParent(span)
  12398. }
  12399. domUtils.remove(text)
  12400. }
  12401. }
  12402. return true
  12403. },
  12404. queryCommandValue: function (cmdName) {
  12405. var startNode = this.selection.getStart()
  12406. //trace:946
  12407. if (cmdName == 'underline' || cmdName == 'strikethrough') {
  12408. var tmpNode = startNode,
  12409. value
  12410. while (
  12411. tmpNode &&
  12412. !domUtils.isBlockElm(tmpNode) &&
  12413. !domUtils.isBody(tmpNode)
  12414. ) {
  12415. if (tmpNode.nodeType == 1) {
  12416. value = domUtils.getComputedStyle(tmpNode, style)
  12417. if (value != 'none') {
  12418. return value
  12419. }
  12420. }
  12421. tmpNode = tmpNode.parentNode
  12422. }
  12423. return 'none'
  12424. }
  12425. if (cmdName == 'fontborder') {
  12426. var tmp = startNode,
  12427. val
  12428. while (tmp && dtd.$inline[tmp.tagName]) {
  12429. if ((val = domUtils.getComputedStyle(tmp, 'border'))) {
  12430. if (/1px/.test(val) && /solid/.test(val)) {
  12431. return val
  12432. }
  12433. }
  12434. tmp = tmp.parentNode
  12435. }
  12436. return ''
  12437. }
  12438. if (cmdName == 'FontSize') {
  12439. var styleVal = domUtils.getComputedStyle(startNode, style),
  12440. tmp = /^([\d\.]+)(\w+)$/.exec(styleVal)
  12441. if (tmp) {
  12442. return Math.floor(tmp[1]) + tmp[2]
  12443. }
  12444. return styleVal
  12445. }
  12446. return domUtils.getComputedStyle(startNode, style)
  12447. },
  12448. queryCommandState: function (cmdName) {
  12449. if (!needCmd[cmdName]) return 0
  12450. var val = this.queryCommandValue(cmdName)
  12451. if (cmdName == 'fontborder') {
  12452. return /1px/.test(val) && /solid/.test(val)
  12453. } else {
  12454. return cmdName == 'underline'
  12455. ? /underline/.test(val)
  12456. : /line\-through/.test(val)
  12457. }
  12458. },
  12459. }
  12460. })(p, fonts[p])
  12461. }
  12462. }
  12463. // plugins/link.js
  12464. /**
  12465. * 超链接
  12466. * @file
  12467. * @since 1.2.6.1
  12468. */
  12469. /**
  12470. * 插入超链接
  12471. * @command link
  12472. * @method execCommand
  12473. * @param { String } cmd 命令字符串
  12474. * @param { Object } options 设置自定义属性,例如:url、title、target
  12475. * @example
  12476. * ```javascript
  12477. * editor.execCommand( 'link', '{
  12478. * url:'ueditor.baidu.com',
  12479. * title:'ueditor',
  12480. * target:'_blank'
  12481. * }' );
  12482. * ```
  12483. */
  12484. /**
  12485. * 返回当前选中的第一个超链接节点
  12486. * @command link
  12487. * @method queryCommandValue
  12488. * @param { String } cmd 命令字符串
  12489. * @return { Element } 超链接节点
  12490. * @example
  12491. * ```javascript
  12492. * editor.queryCommandValue( 'link' );
  12493. * ```
  12494. */
  12495. /**
  12496. * 取消超链接
  12497. * @command unlink
  12498. * @method execCommand
  12499. * @param { String } cmd 命令字符串
  12500. * @example
  12501. * ```javascript
  12502. * editor.execCommand( 'unlink');
  12503. * ```
  12504. */
  12505. UE.plugins['link'] = function () {
  12506. function optimize(range) {
  12507. var start = range.startContainer,
  12508. end = range.endContainer
  12509. if ((start = domUtils.findParentByTagName(start, 'a', true))) {
  12510. range.setStartBefore(start)
  12511. }
  12512. if ((end = domUtils.findParentByTagName(end, 'a', true))) {
  12513. range.setEndAfter(end)
  12514. }
  12515. }
  12516. UE.commands['unlink'] = {
  12517. execCommand: function () {
  12518. var range = this.selection.getRange(),
  12519. bookmark
  12520. if (
  12521. range.collapsed &&
  12522. !domUtils.findParentByTagName(range.startContainer, 'a', true)
  12523. ) {
  12524. return
  12525. }
  12526. bookmark = range.createBookmark()
  12527. optimize(range)
  12528. range.removeInlineStyle('a').moveToBookmark(bookmark).select()
  12529. },
  12530. queryCommandState: function () {
  12531. return !this.highlight && this.queryCommandValue('link') ? 0 : -1
  12532. },
  12533. }
  12534. function doLink(range, opt, me) {
  12535. var rngClone = range.cloneRange(),
  12536. link = me.queryCommandValue('link')
  12537. optimize((range = range.adjustmentBoundary()))
  12538. var start = range.startContainer
  12539. if (start.nodeType == 1 && link) {
  12540. start = start.childNodes[range.startOffset]
  12541. if (
  12542. start &&
  12543. start.nodeType == 1 &&
  12544. start.tagName == 'A' &&
  12545. /^(?:https?|ftp|file)\s*:\s*\/\//.test(
  12546. start[browser.ie ? 'innerText' : 'textContent']
  12547. )
  12548. ) {
  12549. start[browser.ie ? 'innerText' : 'textContent'] = utils.html(
  12550. opt.textValue || opt.href
  12551. )
  12552. }
  12553. }
  12554. if (!rngClone.collapsed || link) {
  12555. range.removeInlineStyle('a')
  12556. rngClone = range.cloneRange()
  12557. }
  12558. if (rngClone.collapsed) {
  12559. var a = range.document.createElement('a'),
  12560. text = ''
  12561. if (opt.textValue) {
  12562. text = utils.html(opt.textValue)
  12563. delete opt.textValue
  12564. } else {
  12565. text = utils.html(opt.href)
  12566. }
  12567. domUtils.setAttributes(a, opt)
  12568. start = domUtils.findParentByTagName(rngClone.startContainer, 'a', true)
  12569. if (start && domUtils.isInNodeEndBoundary(rngClone, start)) {
  12570. range.setStartAfter(start).collapse(true)
  12571. }
  12572. a[browser.ie ? 'innerText' : 'textContent'] = text
  12573. range.insertNode(a).selectNode(a)
  12574. } else {
  12575. range.applyInlineStyle('a', opt)
  12576. }
  12577. }
  12578. UE.commands['link'] = {
  12579. execCommand: function (cmdName, opt) {
  12580. var range
  12581. opt._href && (opt._href = utils.unhtml(opt._href, /[<">]/g))
  12582. opt.href && (opt.href = utils.unhtml(opt.href, /[<">]/g))
  12583. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue, /[<">]/g))
  12584. doLink((range = this.selection.getRange()), opt, this)
  12585. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  12586. range.collapse().select(true)
  12587. },
  12588. queryCommandValue: function () {
  12589. var range = this.selection.getRange(),
  12590. node
  12591. if (range.collapsed) {
  12592. // node = this.selection.getStart();
  12593. //在ie下getstart()取值偏上了
  12594. node = range.startContainer
  12595. node = node.nodeType == 1 ? node : node.parentNode
  12596. if (
  12597. node &&
  12598. (node = domUtils.findParentByTagName(node, 'a', true)) &&
  12599. !domUtils.isInNodeEndBoundary(range, node)
  12600. ) {
  12601. return node
  12602. }
  12603. } else {
  12604. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  12605. range.shrinkBoundary()
  12606. var start =
  12607. range.startContainer.nodeType == 3 ||
  12608. !range.startContainer.childNodes[range.startOffset]
  12609. ? range.startContainer
  12610. : range.startContainer.childNodes[range.startOffset],
  12611. end =
  12612. range.endContainer.nodeType == 3 || range.endOffset == 0
  12613. ? range.endContainer
  12614. : range.endContainer.childNodes[range.endOffset - 1],
  12615. common = range.getCommonAncestor()
  12616. node = domUtils.findParentByTagName(common, 'a', true)
  12617. if (!node && common.nodeType == 1) {
  12618. var as = common.getElementsByTagName('a'),
  12619. ps,
  12620. pe
  12621. for (var i = 0, ci; (ci = as[i++]); ) {
  12622. ;(ps = domUtils.getPosition(ci, start)),
  12623. (pe = domUtils.getPosition(ci, end))
  12624. if (
  12625. (ps & domUtils.POSITION_FOLLOWING ||
  12626. ps & domUtils.POSITION_CONTAINS) &&
  12627. (pe & domUtils.POSITION_PRECEDING ||
  12628. pe & domUtils.POSITION_CONTAINS)
  12629. ) {
  12630. node = ci
  12631. break
  12632. }
  12633. }
  12634. }
  12635. return node
  12636. }
  12637. },
  12638. queryCommandState: function () {
  12639. //判断如果是视频的话连接不可用
  12640. //fix 853
  12641. var img = this.selection.getRange().getClosedNode(),
  12642. flag =
  12643. img &&
  12644. (img.className == 'edui-faked-video' ||
  12645. img.className.indexOf('edui-upload-video') != -1)
  12646. return flag ? -1 : 0
  12647. },
  12648. }
  12649. }
  12650. // plugins/iframe.js
  12651. ///import core
  12652. ///import plugins\inserthtml.js
  12653. ///commands 插入框架
  12654. ///commandsName InsertFrame
  12655. ///commandsTitle 插入Iframe
  12656. ///commandsDialog dialogs\insertframe
  12657. UE.plugins['insertframe'] = function () {
  12658. var me = this
  12659. function deleteIframe() {
  12660. me._iframe && delete me._iframe
  12661. }
  12662. me.addListener('selectionchange', function () {
  12663. deleteIframe()
  12664. })
  12665. }
  12666. // plugins/scrawl.js
  12667. ///import core
  12668. ///commands 涂鸦
  12669. ///commandsName Scrawl
  12670. ///commandsTitle 涂鸦
  12671. ///commandsDialog dialogs\scrawl
  12672. UE.commands['scrawl'] = {
  12673. queryCommandState: function () {
  12674. return browser.ie && browser.version <= 8 ? -1 : 0
  12675. },
  12676. }
  12677. // plugins/removeformat.js
  12678. /**
  12679. * 清除格式
  12680. * @file
  12681. * @since 1.2.6.1
  12682. */
  12683. /**
  12684. * 清除文字样式
  12685. * @command removeformat
  12686. * @method execCommand
  12687. * @param { String } cmd 命令字符串
  12688. * @param {String} tags 以逗号隔开的标签。如:strong
  12689. * @param {String} style 样式如:color
  12690. * @param {String} attrs 属性如:width
  12691. * @example
  12692. * ```javascript
  12693. * editor.execCommand( 'removeformat', 'strong','color','width' );
  12694. * ```
  12695. */
  12696. UE.plugins['removeformat'] = function () {
  12697. var me = this
  12698. me.setOpt({
  12699. removeFormatTags:
  12700. 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var',
  12701. removeFormatAttributes:
  12702. 'class,style,lang,width,height,align,hspace,valign',
  12703. })
  12704. me.commands['removeformat'] = {
  12705. execCommand: function (cmdName, tags, style, attrs, notIncludeA) {
  12706. var tagReg = new RegExp(
  12707. '^(?:' +
  12708. (tags || this.options.removeFormatTags).replace(/,/g, '|') +
  12709. ')$',
  12710. 'i'
  12711. ),
  12712. removeFormatAttributes = style
  12713. ? []
  12714. : (attrs || this.options.removeFormatAttributes).split(','),
  12715. range = new dom.Range(this.document),
  12716. bookmark,
  12717. node,
  12718. parent,
  12719. filter = function (node) {
  12720. return node.nodeType == 1
  12721. }
  12722. function isRedundantSpan(node) {
  12723. if (node.nodeType == 3 || node.tagName.toLowerCase() != 'span') {
  12724. return 0
  12725. }
  12726. if (browser.ie) {
  12727. //ie 下判断实效,所以只能简单用style来判断
  12728. //return node.style.cssText == '' ? 1 : 0;
  12729. var attrs = node.attributes
  12730. if (attrs.length) {
  12731. for (var i = 0, l = attrs.length; i < l; i++) {
  12732. if (attrs[i].specified) {
  12733. return 0
  12734. }
  12735. }
  12736. return 1
  12737. }
  12738. }
  12739. return !node.attributes.length
  12740. }
  12741. function doRemove(range) {
  12742. var bookmark1 = range.createBookmark()
  12743. if (range.collapsed) {
  12744. range.enlarge(true)
  12745. }
  12746. //不能把a标签切了
  12747. if (!notIncludeA) {
  12748. var aNode = domUtils.findParentByTagName(
  12749. range.startContainer,
  12750. 'a',
  12751. true
  12752. )
  12753. if (aNode) {
  12754. range.setStartBefore(aNode)
  12755. }
  12756. aNode = domUtils.findParentByTagName(range.endContainer, 'a', true)
  12757. if (aNode) {
  12758. range.setEndAfter(aNode)
  12759. }
  12760. }
  12761. bookmark = range.createBookmark()
  12762. node = bookmark.start
  12763. //切开始
  12764. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12765. domUtils.breakParent(node, parent)
  12766. domUtils.clearEmptySibling(node)
  12767. }
  12768. if (bookmark.end) {
  12769. //切结束
  12770. node = bookmark.end
  12771. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12772. domUtils.breakParent(node, parent)
  12773. domUtils.clearEmptySibling(node)
  12774. }
  12775. //开始去除样式
  12776. var current = domUtils.getNextDomNode(
  12777. bookmark.start,
  12778. false,
  12779. filter
  12780. ),
  12781. next
  12782. while (current) {
  12783. if (current == bookmark.end) {
  12784. break
  12785. }
  12786. next = domUtils.getNextDomNode(current, true, filter)
  12787. if (
  12788. !dtd.$empty[current.tagName.toLowerCase()] &&
  12789. !domUtils.isBookmarkNode(current)
  12790. ) {
  12791. if (tagReg.test(current.tagName)) {
  12792. if (style) {
  12793. domUtils.removeStyle(current, style)
  12794. if (
  12795. isRedundantSpan(current) &&
  12796. style != 'text-decoration'
  12797. ) {
  12798. domUtils.remove(current, true)
  12799. }
  12800. } else {
  12801. domUtils.remove(current, true)
  12802. }
  12803. } else {
  12804. //trace:939 不能把list上的样式去掉
  12805. if (
  12806. !dtd.$tableContent[current.tagName] &&
  12807. !dtd.$list[current.tagName]
  12808. ) {
  12809. domUtils.removeAttributes(current, removeFormatAttributes)
  12810. if (isRedundantSpan(current)) {
  12811. domUtils.remove(current, true)
  12812. }
  12813. }
  12814. }
  12815. }
  12816. current = next
  12817. }
  12818. }
  12819. //trace:1035
  12820. //trace:1096 不能把td上的样式去掉,比如边框
  12821. var pN = bookmark.start.parentNode
  12822. if (
  12823. domUtils.isBlockElm(pN) &&
  12824. !dtd.$tableContent[pN.tagName] &&
  12825. !dtd.$list[pN.tagName]
  12826. ) {
  12827. domUtils.removeAttributes(pN, removeFormatAttributes)
  12828. }
  12829. pN = bookmark.end.parentNode
  12830. if (
  12831. bookmark.end &&
  12832. domUtils.isBlockElm(pN) &&
  12833. !dtd.$tableContent[pN.tagName] &&
  12834. !dtd.$list[pN.tagName]
  12835. ) {
  12836. domUtils.removeAttributes(pN, removeFormatAttributes)
  12837. }
  12838. range.moveToBookmark(bookmark).moveToBookmark(bookmark1)
  12839. //清除冗余的代码 <b><bookmark></b>
  12840. var node = range.startContainer,
  12841. tmp,
  12842. collapsed = range.collapsed
  12843. while (
  12844. node.nodeType == 1 &&
  12845. domUtils.isEmptyNode(node) &&
  12846. dtd.$removeEmpty[node.tagName]
  12847. ) {
  12848. tmp = node.parentNode
  12849. range.setStartBefore(node)
  12850. //trace:937
  12851. //更新结束边界
  12852. if (range.startContainer === range.endContainer) {
  12853. range.endOffset--
  12854. }
  12855. domUtils.remove(node)
  12856. node = tmp
  12857. }
  12858. if (!collapsed) {
  12859. node = range.endContainer
  12860. while (
  12861. node.nodeType == 1 &&
  12862. domUtils.isEmptyNode(node) &&
  12863. dtd.$removeEmpty[node.tagName]
  12864. ) {
  12865. tmp = node.parentNode
  12866. range.setEndBefore(node)
  12867. domUtils.remove(node)
  12868. node = tmp
  12869. }
  12870. }
  12871. }
  12872. range = this.selection.getRange()
  12873. doRemove(range)
  12874. range.select()
  12875. },
  12876. }
  12877. }
  12878. // plugins/blockquote.js
  12879. /**
  12880. * 添加引用
  12881. * @file
  12882. * @since 1.2.6.1
  12883. */
  12884. /**
  12885. * 添加引用
  12886. * @command blockquote
  12887. * @method execCommand
  12888. * @param { String } cmd 命令字符串
  12889. * @example
  12890. * ```javascript
  12891. * editor.execCommand( 'blockquote' );
  12892. * ```
  12893. */
  12894. /**
  12895. * 添加引用
  12896. * @command blockquote
  12897. * @method execCommand
  12898. * @param { String } cmd 命令字符串
  12899. * @param { Object } attrs 节点属性
  12900. * @example
  12901. * ```javascript
  12902. * editor.execCommand( 'blockquote',{
  12903. * style: "color: red;"
  12904. * } );
  12905. * ```
  12906. */
  12907. UE.plugins['blockquote'] = function () {
  12908. var me = this
  12909. function getObj(editor) {
  12910. return domUtils.filterNodeList(
  12911. editor.selection.getStartElementPath(),
  12912. 'blockquote'
  12913. )
  12914. }
  12915. me.commands['blockquote'] = {
  12916. execCommand: function (cmdName, attrs) {
  12917. var range = this.selection.getRange(),
  12918. obj = getObj(this),
  12919. blockquote = dtd.blockquote,
  12920. bookmark = range.createBookmark()
  12921. if (obj) {
  12922. var start = range.startContainer,
  12923. startBlock = domUtils.isBlockElm(start)
  12924. ? start
  12925. : domUtils.findParent(start, function (node) {
  12926. return domUtils.isBlockElm(node)
  12927. }),
  12928. end = range.endContainer,
  12929. endBlock = domUtils.isBlockElm(end)
  12930. ? end
  12931. : domUtils.findParent(end, function (node) {
  12932. return domUtils.isBlockElm(node)
  12933. })
  12934. //处理一下li
  12935. startBlock =
  12936. domUtils.findParentByTagName(startBlock, 'li', true) || startBlock
  12937. endBlock =
  12938. domUtils.findParentByTagName(endBlock, 'li', true) || endBlock
  12939. if (
  12940. startBlock.tagName == 'LI' ||
  12941. startBlock.tagName == 'TD' ||
  12942. startBlock === obj ||
  12943. domUtils.isBody(startBlock)
  12944. ) {
  12945. domUtils.remove(obj, true)
  12946. } else {
  12947. domUtils.breakParent(startBlock, obj)
  12948. }
  12949. if (startBlock !== endBlock) {
  12950. obj = domUtils.findParentByTagName(endBlock, 'blockquote')
  12951. if (obj) {
  12952. if (
  12953. endBlock.tagName == 'LI' ||
  12954. endBlock.tagName == 'TD' ||
  12955. domUtils.isBody(endBlock)
  12956. ) {
  12957. obj.parentNode && domUtils.remove(obj, true)
  12958. } else {
  12959. domUtils.breakParent(endBlock, obj)
  12960. }
  12961. }
  12962. }
  12963. var blockquotes = domUtils.getElementsByTagName(
  12964. this.document,
  12965. 'blockquote'
  12966. )
  12967. for (var i = 0, bi; (bi = blockquotes[i++]); ) {
  12968. if (!bi.childNodes.length) {
  12969. domUtils.remove(bi)
  12970. } else if (
  12971. domUtils.getPosition(bi, startBlock) &
  12972. domUtils.POSITION_FOLLOWING &&
  12973. domUtils.getPosition(bi, endBlock) & domUtils.POSITION_PRECEDING
  12974. ) {
  12975. domUtils.remove(bi, true)
  12976. }
  12977. }
  12978. } else {
  12979. var tmpRange = range.cloneRange(),
  12980. node =
  12981. tmpRange.startContainer.nodeType == 1
  12982. ? tmpRange.startContainer
  12983. : tmpRange.startContainer.parentNode,
  12984. preNode = node,
  12985. doEnd = 1
  12986. //调整开始
  12987. while (1) {
  12988. if (domUtils.isBody(node)) {
  12989. if (preNode !== node) {
  12990. if (range.collapsed) {
  12991. tmpRange.selectNode(preNode)
  12992. doEnd = 0
  12993. } else {
  12994. tmpRange.setStartBefore(preNode)
  12995. }
  12996. } else {
  12997. tmpRange.setStart(node, 0)
  12998. }
  12999. break
  13000. }
  13001. if (!blockquote[node.tagName]) {
  13002. if (range.collapsed) {
  13003. tmpRange.selectNode(preNode)
  13004. } else {
  13005. tmpRange.setStartBefore(preNode)
  13006. }
  13007. break
  13008. }
  13009. preNode = node
  13010. node = node.parentNode
  13011. }
  13012. //调整结束
  13013. if (doEnd) {
  13014. preNode =
  13015. node =
  13016. node =
  13017. tmpRange.endContainer.nodeType == 1
  13018. ? tmpRange.endContainer
  13019. : tmpRange.endContainer.parentNode
  13020. while (1) {
  13021. if (domUtils.isBody(node)) {
  13022. if (preNode !== node) {
  13023. tmpRange.setEndAfter(preNode)
  13024. } else {
  13025. tmpRange.setEnd(node, node.childNodes.length)
  13026. }
  13027. break
  13028. }
  13029. if (!blockquote[node.tagName]) {
  13030. tmpRange.setEndAfter(preNode)
  13031. break
  13032. }
  13033. preNode = node
  13034. node = node.parentNode
  13035. }
  13036. }
  13037. node = range.document.createElement('blockquote')
  13038. domUtils.setAttributes(node, attrs)
  13039. node.appendChild(tmpRange.extractContents())
  13040. tmpRange.insertNode(node)
  13041. //去除重复的
  13042. var childs = domUtils.getElementsByTagName(node, 'blockquote')
  13043. for (var i = 0, ci; (ci = childs[i++]); ) {
  13044. if (ci.parentNode) {
  13045. domUtils.remove(ci, true)
  13046. }
  13047. }
  13048. }
  13049. range.moveToBookmark(bookmark).select()
  13050. },
  13051. queryCommandState: function () {
  13052. return getObj(this) ? 1 : 0
  13053. },
  13054. }
  13055. }
  13056. // plugins/convertcase.js
  13057. /**
  13058. * 大小写转换
  13059. * @file
  13060. * @since 1.2.6.1
  13061. */
  13062. /**
  13063. * 把选区内文本变大写,与“tolowercase”命令互斥
  13064. * @command touppercase
  13065. * @method execCommand
  13066. * @param { String } cmd 命令字符串
  13067. * @example
  13068. * ```javascript
  13069. * editor.execCommand( 'touppercase' );
  13070. * ```
  13071. */
  13072. /**
  13073. * 把选区内文本变小写,与“touppercase”命令互斥
  13074. * @command tolowercase
  13075. * @method execCommand
  13076. * @param { String } cmd 命令字符串
  13077. * @example
  13078. * ```javascript
  13079. * editor.execCommand( 'tolowercase' );
  13080. * ```
  13081. */
  13082. UE.commands['touppercase'] = UE.commands['tolowercase'] = {
  13083. execCommand: function (cmd) {
  13084. var me = this
  13085. var rng = me.selection.getRange()
  13086. if (rng.collapsed) {
  13087. return rng
  13088. }
  13089. var bk = rng.createBookmark(),
  13090. bkEnd = bk.end,
  13091. filterFn = function (node) {
  13092. return !domUtils.isBr(node) && !domUtils.isWhitespace(node)
  13093. },
  13094. curNode = domUtils.getNextDomNode(bk.start, false, filterFn)
  13095. while (
  13096. curNode &&
  13097. domUtils.getPosition(curNode, bkEnd) & domUtils.POSITION_PRECEDING
  13098. ) {
  13099. if (curNode.nodeType == 3) {
  13100. curNode.nodeValue =
  13101. curNode.nodeValue[
  13102. cmd == 'touppercase' ? 'toUpperCase' : 'toLowerCase'
  13103. ]()
  13104. }
  13105. curNode = domUtils.getNextDomNode(curNode, true, filterFn)
  13106. if (curNode === bkEnd) {
  13107. break
  13108. }
  13109. }
  13110. rng.moveToBookmark(bk).select()
  13111. },
  13112. }
  13113. // plugins/indent.js
  13114. /**
  13115. * 首行缩进
  13116. * @file
  13117. * @since 1.2.6.1
  13118. */
  13119. /**
  13120. * 缩进
  13121. * @command indent
  13122. * @method execCommand
  13123. * @param { String } cmd 命令字符串
  13124. * @example
  13125. * ```javascript
  13126. * editor.execCommand( 'indent' );
  13127. * ```
  13128. */
  13129. UE.commands['indent'] = {
  13130. execCommand: function () {
  13131. var me = this,
  13132. value = me.queryCommandState('indent')
  13133. ? '0em'
  13134. : me.options.indentValue || '2em'
  13135. me.execCommand('Paragraph', 'p', { style: 'text-indent:' + value })
  13136. },
  13137. queryCommandState: function () {
  13138. var pN = domUtils.filterNodeList(
  13139. this.selection.getStartElementPath(),
  13140. 'p h1 h2 h3 h4 h5 h6'
  13141. )
  13142. return pN && pN.style.textIndent && parseInt(pN.style.textIndent) ? 1 : 0
  13143. },
  13144. }
  13145. // plugins/print.js
  13146. /**
  13147. * 打印
  13148. * @file
  13149. * @since 1.2.6.1
  13150. */
  13151. /**
  13152. * 打印
  13153. * @command print
  13154. * @method execCommand
  13155. * @param { String } cmd 命令字符串
  13156. * @example
  13157. * ```javascript
  13158. * editor.execCommand( 'print' );
  13159. * ```
  13160. */
  13161. UE.commands['print'] = {
  13162. execCommand: function () {
  13163. this.window.print()
  13164. },
  13165. notNeedUndo: 1,
  13166. }
  13167. // plugins/preview.js
  13168. /**
  13169. * 预览
  13170. * @file
  13171. * @since 1.2.6.1
  13172. */
  13173. /**
  13174. * 预览
  13175. * @command preview
  13176. * @method execCommand
  13177. * @param { String } cmd 命令字符串
  13178. * @example
  13179. * ```javascript
  13180. * editor.execCommand( 'preview' );
  13181. * ```
  13182. */
  13183. UE.commands['preview'] = {
  13184. execCommand: function () {
  13185. var w = window.open('', '_blank', ''),
  13186. d = w.document
  13187. d.open()
  13188. d.write(
  13189. '<!DOCTYPE html><html><head><meta charset="utf-8"/><script src="' +
  13190. this.options.UEDITOR_HOME_URL +
  13191. 'ueditor.parse.js"></script><script>' +
  13192. "setTimeout(function(){uParse('div',{rootPath: '" +
  13193. this.options.UEDITOR_HOME_URL +
  13194. "'})},300)" +
  13195. '</script></head><body><div>' +
  13196. this.getContent(null, null, true) +
  13197. '</div></body></html>'
  13198. )
  13199. d.close()
  13200. },
  13201. notNeedUndo: 1,
  13202. }
  13203. // plugins/selectall.js
  13204. /**
  13205. * 全选
  13206. * @file
  13207. * @since 1.2.6.1
  13208. */
  13209. /**
  13210. * 选中所有内容
  13211. * @command selectall
  13212. * @method execCommand
  13213. * @param { String } cmd 命令字符串
  13214. * @example
  13215. * ```javascript
  13216. * editor.execCommand( 'selectall' );
  13217. * ```
  13218. */
  13219. UE.plugins['selectall'] = function () {
  13220. var me = this
  13221. me.commands['selectall'] = {
  13222. execCommand: function () {
  13223. //去掉了原生的selectAll,因为会出现报错和当内容为空时,不能出现闭合状态的光标
  13224. var me = this,
  13225. body = me.body,
  13226. range = me.selection.getRange()
  13227. range.selectNodeContents(body)
  13228. if (domUtils.isEmptyBlock(body)) {
  13229. //opera不能自动合并到元素的里边,要手动处理一下
  13230. if (
  13231. browser.opera &&
  13232. body.firstChild &&
  13233. body.firstChild.nodeType == 1
  13234. ) {
  13235. range.setStartAtFirst(body.firstChild)
  13236. }
  13237. range.collapse(true)
  13238. }
  13239. range.select(true)
  13240. },
  13241. notNeedUndo: 1,
  13242. }
  13243. //快捷键
  13244. me.addshortcutkey({
  13245. selectAll: 'ctrl+65',
  13246. })
  13247. }
  13248. // plugins/paragraph.js
  13249. /**
  13250. * 段落样式
  13251. * @file
  13252. * @since 1.2.6.1
  13253. */
  13254. /**
  13255. * 段落格式
  13256. * @command paragraph
  13257. * @method execCommand
  13258. * @param { String } cmd 命令字符串
  13259. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  13260. * @param {Object} attrs 标签的属性
  13261. * @example
  13262. * ```javascript
  13263. * editor.execCommand( 'Paragraph','h1','{
  13264. * class:'test'
  13265. * }' );
  13266. * ```
  13267. */
  13268. /**
  13269. * 返回选区内节点标签名
  13270. * @command paragraph
  13271. * @method queryCommandValue
  13272. * @param { String } cmd 命令字符串
  13273. * @return { String } 节点标签名
  13274. * @example
  13275. * ```javascript
  13276. * editor.queryCommandValue( 'Paragraph' );
  13277. * ```
  13278. */
  13279. UE.plugins['paragraph'] = function () {
  13280. var me = this,
  13281. block = domUtils.isBlockElm,
  13282. notExchange = ['TD', 'LI', 'PRE'],
  13283. doParagraph = function (range, style, attrs, sourceCmdName) {
  13284. var bookmark = range.createBookmark(),
  13285. filterFn = function (node) {
  13286. return node.nodeType == 1
  13287. ? node.tagName.toLowerCase() != 'br' &&
  13288. !domUtils.isBookmarkNode(node)
  13289. : !domUtils.isWhitespace(node)
  13290. },
  13291. para
  13292. range.enlarge(true)
  13293. var bookmark2 = range.createBookmark(),
  13294. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13295. tmpRange = range.cloneRange(),
  13296. tmpNode
  13297. while (
  13298. current &&
  13299. !(
  13300. domUtils.getPosition(current, bookmark2.end) &
  13301. domUtils.POSITION_FOLLOWING
  13302. )
  13303. ) {
  13304. if (current.nodeType == 3 || !block(current)) {
  13305. tmpRange.setStartBefore(current)
  13306. while (current && current !== bookmark2.end && !block(current)) {
  13307. tmpNode = current
  13308. current = domUtils.getNextDomNode(
  13309. current,
  13310. false,
  13311. null,
  13312. function (node) {
  13313. return !block(node)
  13314. }
  13315. )
  13316. }
  13317. tmpRange.setEndAfter(tmpNode)
  13318. para = range.document.createElement(style)
  13319. if (attrs) {
  13320. domUtils.setAttributes(para, attrs)
  13321. if (
  13322. sourceCmdName &&
  13323. sourceCmdName == 'customstyle' &&
  13324. attrs.style
  13325. ) {
  13326. para.style.cssText = attrs.style
  13327. }
  13328. }
  13329. para.appendChild(tmpRange.extractContents())
  13330. //需要内容占位
  13331. if (domUtils.isEmptyNode(para)) {
  13332. domUtils.fillChar(range.document, para)
  13333. }
  13334. tmpRange.insertNode(para)
  13335. var parent = para.parentNode
  13336. //如果para上一级是一个block元素且不是body,td就删除它
  13337. if (
  13338. block(parent) &&
  13339. !domUtils.isBody(para.parentNode) &&
  13340. utils.indexOf(notExchange, parent.tagName) == -1
  13341. ) {
  13342. //存储dir,style
  13343. if (!(sourceCmdName && sourceCmdName == 'customstyle')) {
  13344. parent.getAttribute('dir') &&
  13345. para.setAttribute('dir', parent.getAttribute('dir'))
  13346. //trace:1070
  13347. parent.style.cssText &&
  13348. (para.style.cssText =
  13349. parent.style.cssText + ';' + para.style.cssText)
  13350. //trace:1030
  13351. parent.style.textAlign &&
  13352. !para.style.textAlign &&
  13353. (para.style.textAlign = parent.style.textAlign)
  13354. parent.style.textIndent &&
  13355. !para.style.textIndent &&
  13356. (para.style.textIndent = parent.style.textIndent)
  13357. parent.style.padding &&
  13358. !para.style.padding &&
  13359. (para.style.padding = parent.style.padding)
  13360. }
  13361. //trace:1706 选择的就是h1-6要删除
  13362. if (
  13363. attrs &&
  13364. /h\d/i.test(parent.tagName) &&
  13365. !/h\d/i.test(para.tagName)
  13366. ) {
  13367. domUtils.setAttributes(parent, attrs)
  13368. if (
  13369. sourceCmdName &&
  13370. sourceCmdName == 'customstyle' &&
  13371. attrs.style
  13372. ) {
  13373. parent.style.cssText = attrs.style
  13374. }
  13375. domUtils.remove(para, true)
  13376. para = parent
  13377. } else {
  13378. domUtils.remove(para.parentNode, true)
  13379. }
  13380. }
  13381. if (utils.indexOf(notExchange, parent.tagName) != -1) {
  13382. current = parent
  13383. } else {
  13384. current = para
  13385. }
  13386. current = domUtils.getNextDomNode(current, false, filterFn)
  13387. } else {
  13388. current = domUtils.getNextDomNode(current, true, filterFn)
  13389. }
  13390. }
  13391. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark)
  13392. }
  13393. me.setOpt('paragraph', {
  13394. p: '',
  13395. h1: '',
  13396. h2: '',
  13397. h3: '',
  13398. h4: '',
  13399. h5: '',
  13400. h6: '',
  13401. })
  13402. me.commands['paragraph'] = {
  13403. execCommand: function (cmdName, style, attrs, sourceCmdName) {
  13404. var range = this.selection.getRange()
  13405. //闭合时单独处理
  13406. if (range.collapsed) {
  13407. var txt = this.document.createTextNode('p')
  13408. range.insertNode(txt)
  13409. //去掉冗余的fillchar
  13410. if (browser.ie) {
  13411. var node = txt.previousSibling
  13412. if (node && domUtils.isWhitespace(node)) {
  13413. domUtils.remove(node)
  13414. }
  13415. node = txt.nextSibling
  13416. if (node && domUtils.isWhitespace(node)) {
  13417. domUtils.remove(node)
  13418. }
  13419. }
  13420. }
  13421. range = doParagraph(range, style, attrs, sourceCmdName)
  13422. if (txt) {
  13423. range.setStartBefore(txt).collapse(true)
  13424. pN = txt.parentNode
  13425. domUtils.remove(txt)
  13426. if (domUtils.isBlockElm(pN) && domUtils.isEmptyNode(pN)) {
  13427. domUtils.fillNode(this.document, pN)
  13428. }
  13429. }
  13430. if (
  13431. browser.gecko &&
  13432. range.collapsed &&
  13433. range.startContainer.nodeType == 1
  13434. ) {
  13435. var child = range.startContainer.childNodes[range.startOffset]
  13436. if (
  13437. child &&
  13438. child.nodeType == 1 &&
  13439. child.tagName.toLowerCase() == style
  13440. ) {
  13441. range.setStart(child, 0).collapse(true)
  13442. }
  13443. }
  13444. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  13445. range.select()
  13446. return true
  13447. },
  13448. queryCommandValue: function () {
  13449. var node = domUtils.filterNodeList(
  13450. this.selection.getStartElementPath(),
  13451. 'p h1 h2 h3 h4 h5 h6'
  13452. )
  13453. return node ? node.tagName.toLowerCase() : ''
  13454. },
  13455. }
  13456. }
  13457. // plugins/directionality.js
  13458. /**
  13459. * 设置文字输入的方向的插件
  13460. * @file
  13461. * @since 1.2.6.1
  13462. */
  13463. ;(function () {
  13464. var block = domUtils.isBlockElm,
  13465. getObj = function (editor) {
  13466. // var startNode = editor.selection.getStart(),
  13467. // parents;
  13468. // if ( startNode ) {
  13469. // //查找所有的是block的父亲节点
  13470. // parents = domUtils.findParents( startNode, true, block, true );
  13471. // for ( var i = 0,ci; ci = parents[i++]; ) {
  13472. // if ( ci.getAttribute( 'dir' ) ) {
  13473. // return ci;
  13474. // }
  13475. // }
  13476. // }
  13477. return domUtils.filterNodeList(
  13478. editor.selection.getStartElementPath(),
  13479. function (n) {
  13480. return n && n.nodeType == 1 && n.getAttribute('dir')
  13481. }
  13482. )
  13483. },
  13484. doDirectionality = function (range, editor, forward) {
  13485. var bookmark,
  13486. filterFn = function (node) {
  13487. return node.nodeType == 1
  13488. ? !domUtils.isBookmarkNode(node)
  13489. : !domUtils.isWhitespace(node)
  13490. },
  13491. obj = getObj(editor)
  13492. if (obj && range.collapsed) {
  13493. obj.setAttribute('dir', forward)
  13494. return range
  13495. }
  13496. bookmark = range.createBookmark()
  13497. range.enlarge(true)
  13498. var bookmark2 = range.createBookmark(),
  13499. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13500. tmpRange = range.cloneRange(),
  13501. tmpNode
  13502. while (
  13503. current &&
  13504. !(
  13505. domUtils.getPosition(current, bookmark2.end) &
  13506. domUtils.POSITION_FOLLOWING
  13507. )
  13508. ) {
  13509. if (current.nodeType == 3 || !block(current)) {
  13510. tmpRange.setStartBefore(current)
  13511. while (current && current !== bookmark2.end && !block(current)) {
  13512. tmpNode = current
  13513. current = domUtils.getNextDomNode(
  13514. current,
  13515. false,
  13516. null,
  13517. function (node) {
  13518. return !block(node)
  13519. }
  13520. )
  13521. }
  13522. tmpRange.setEndAfter(tmpNode)
  13523. var common = tmpRange.getCommonAncestor()
  13524. if (!domUtils.isBody(common) && block(common)) {
  13525. //遍历到了block节点
  13526. common.setAttribute('dir', forward)
  13527. current = common
  13528. } else {
  13529. //没有遍历到,添加一个block节点
  13530. var p = range.document.createElement('p')
  13531. p.setAttribute('dir', forward)
  13532. var frag = tmpRange.extractContents()
  13533. p.appendChild(frag)
  13534. tmpRange.insertNode(p)
  13535. current = p
  13536. }
  13537. current = domUtils.getNextDomNode(current, false, filterFn)
  13538. } else {
  13539. current = domUtils.getNextDomNode(current, true, filterFn)
  13540. }
  13541. }
  13542. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark)
  13543. }
  13544. /**
  13545. * 文字输入方向
  13546. * @command directionality
  13547. * @method execCommand
  13548. * @param { String } cmdName 命令字符串
  13549. * @param { String } forward 传入'ltr'表示从左向右输入,传入'rtl'表示从右向左输入
  13550. * @example
  13551. * ```javascript
  13552. * editor.execCommand( 'directionality', 'ltr');
  13553. * ```
  13554. */
  13555. /**
  13556. * 查询当前选区的文字输入方向
  13557. * @command directionality
  13558. * @method queryCommandValue
  13559. * @param { String } cmdName 命令字符串
  13560. * @return { String } 返回'ltr'表示从左向右输入,返回'rtl'表示从右向左输入
  13561. * @example
  13562. * ```javascript
  13563. * editor.queryCommandValue( 'directionality');
  13564. * ```
  13565. */
  13566. UE.commands['directionality'] = {
  13567. execCommand: function (cmdName, forward) {
  13568. var range = this.selection.getRange()
  13569. //闭合时单独处理
  13570. if (range.collapsed) {
  13571. var txt = this.document.createTextNode('d')
  13572. range.insertNode(txt)
  13573. }
  13574. doDirectionality(range, this, forward)
  13575. if (txt) {
  13576. range.setStartBefore(txt).collapse(true)
  13577. domUtils.remove(txt)
  13578. }
  13579. range.select()
  13580. return true
  13581. },
  13582. queryCommandValue: function () {
  13583. var node = getObj(this)
  13584. return node ? node.getAttribute('dir') : 'ltr'
  13585. },
  13586. }
  13587. })()
  13588. // plugins/horizontal.js
  13589. /**
  13590. * 插入分割线插件
  13591. * @file
  13592. * @since 1.2.6.1
  13593. */
  13594. /**
  13595. * 插入分割线
  13596. * @command horizontal
  13597. * @method execCommand
  13598. * @param { String } cmdName 命令字符串
  13599. * @example
  13600. * ```javascript
  13601. * editor.execCommand( 'horizontal' );
  13602. * ```
  13603. */
  13604. UE.plugins['horizontal'] = function () {
  13605. var me = this
  13606. me.commands['horizontal'] = {
  13607. execCommand: function (cmdName) {
  13608. var me = this
  13609. if (me.queryCommandState(cmdName) !== -1) {
  13610. me.execCommand('insertHtml', '<hr>')
  13611. var range = me.selection.getRange(),
  13612. start = range.startContainer
  13613. if (start.nodeType == 1 && !start.childNodes[range.startOffset]) {
  13614. var tmp
  13615. if ((tmp = start.childNodes[range.startOffset - 1])) {
  13616. if (tmp.nodeType == 1 && tmp.tagName == 'HR') {
  13617. if (me.options.enterTag == 'p') {
  13618. tmp = me.document.createElement('p')
  13619. range.insertNode(tmp)
  13620. range.setStart(tmp, 0).setCursor()
  13621. } else {
  13622. tmp = me.document.createElement('br')
  13623. range.insertNode(tmp)
  13624. range.setStartBefore(tmp).setCursor()
  13625. }
  13626. }
  13627. }
  13628. }
  13629. return true
  13630. }
  13631. },
  13632. //边界在table里不能加分隔线
  13633. queryCommandState: function () {
  13634. return domUtils.filterNodeList(
  13635. this.selection.getStartElementPath(),
  13636. 'table'
  13637. )
  13638. ? -1
  13639. : 0
  13640. },
  13641. }
  13642. // me.addListener('delkeyup',function(){
  13643. // var rng = this.selection.getRange();
  13644. // if(browser.ie && browser.version > 8){
  13645. // rng.txtToElmBoundary(true);
  13646. // if(domUtils.isStartInblock(rng)){
  13647. // var tmpNode = rng.startContainer;
  13648. // var pre = tmpNode.previousSibling;
  13649. // if(pre && domUtils.isTagNode(pre,'hr')){
  13650. // domUtils.remove(pre);
  13651. // rng.select();
  13652. // return;
  13653. // }
  13654. // }
  13655. // }
  13656. // if(domUtils.isBody(rng.startContainer)){
  13657. // var hr = rng.startContainer.childNodes[rng.startOffset -1];
  13658. // if(hr && hr.nodeName == 'HR'){
  13659. // var next = hr.nextSibling;
  13660. // if(next){
  13661. // rng.setStart(next,0)
  13662. // }else if(hr.previousSibling){
  13663. // rng.setStartAtLast(hr.previousSibling)
  13664. // }else{
  13665. // var p = this.document.createElement('p');
  13666. // hr.parentNode.insertBefore(p,hr);
  13667. // domUtils.fillNode(this.document,p);
  13668. // rng.setStart(p,0);
  13669. // }
  13670. // domUtils.remove(hr);
  13671. // rng.setCursor(false,true);
  13672. // }
  13673. // }
  13674. // })
  13675. me.addListener('delkeydown', function (name, evt) {
  13676. var rng = this.selection.getRange()
  13677. rng.txtToElmBoundary(true)
  13678. if (domUtils.isStartInblock(rng)) {
  13679. var tmpNode = rng.startContainer
  13680. var pre = tmpNode.previousSibling
  13681. if (pre && domUtils.isTagNode(pre, 'hr')) {
  13682. domUtils.remove(pre)
  13683. rng.select()
  13684. domUtils.preventDefault(evt)
  13685. return true
  13686. }
  13687. }
  13688. })
  13689. }
  13690. // plugins/time.js
  13691. /**
  13692. * 插入时间和日期
  13693. * @file
  13694. * @since 1.2.6.1
  13695. */
  13696. /**
  13697. * 插入时间,默认格式:12:59:59
  13698. * @command time
  13699. * @method execCommand
  13700. * @param { String } cmd 命令字符串
  13701. * @example
  13702. * ```javascript
  13703. * editor.execCommand( 'time');
  13704. * ```
  13705. */
  13706. /**
  13707. * 插入日期,默认格式:2013-08-30
  13708. * @command date
  13709. * @method execCommand
  13710. * @param { String } cmd 命令字符串
  13711. * @example
  13712. * ```javascript
  13713. * editor.execCommand( 'date');
  13714. * ```
  13715. */
  13716. UE.commands['time'] = UE.commands['date'] = {
  13717. execCommand: function (cmd, format) {
  13718. var date = new Date()
  13719. function formatTime(date, format) {
  13720. var hh = ('0' + date.getHours()).slice(-2),
  13721. ii = ('0' + date.getMinutes()).slice(-2),
  13722. ss = ('0' + date.getSeconds()).slice(-2)
  13723. format = format || 'hh:ii:ss'
  13724. return format
  13725. .replace(/hh/gi, hh)
  13726. .replace(/ii/gi, ii)
  13727. .replace(/ss/gi, ss)
  13728. }
  13729. function formatDate(date, format) {
  13730. var yyyy = ('000' + date.getFullYear()).slice(-4),
  13731. yy = yyyy.slice(-2),
  13732. mm = ('0' + (date.getMonth() + 1)).slice(-2),
  13733. dd = ('0' + date.getDate()).slice(-2)
  13734. format = format || 'yyyy-mm-dd'
  13735. return format
  13736. .replace(/yyyy/gi, yyyy)
  13737. .replace(/yy/gi, yy)
  13738. .replace(/mm/gi, mm)
  13739. .replace(/dd/gi, dd)
  13740. }
  13741. this.execCommand(
  13742. 'insertHtml',
  13743. cmd == 'time' ? formatTime(date, format) : formatDate(date, format)
  13744. )
  13745. },
  13746. }
  13747. // plugins/rowspacing.js
  13748. /**
  13749. * 段前段后间距插件
  13750. * @file
  13751. * @since 1.2.6.1
  13752. */
  13753. /**
  13754. * 设置段间距
  13755. * @command rowspacing
  13756. * @method execCommand
  13757. * @param { String } cmd 命令字符串
  13758. * @param { String } value 段间距的值,以px为单位
  13759. * @param { String } dir 间距位置,top或bottom,分别表示段前和段后
  13760. * @example
  13761. * ```javascript
  13762. * editor.execCommand( 'rowspacing', '10', 'top' );
  13763. * ```
  13764. */
  13765. UE.plugins['rowspacing'] = function () {
  13766. var me = this
  13767. me.setOpt({
  13768. rowspacingtop: ['5', '10', '15', '20', '25'],
  13769. rowspacingbottom: ['5', '10', '15', '20', '25'],
  13770. })
  13771. me.commands['rowspacing'] = {
  13772. execCommand: function (cmdName, value, dir) {
  13773. this.execCommand('paragraph', 'p', {
  13774. style: 'margin-' + dir + ':' + value + 'px',
  13775. })
  13776. return true
  13777. },
  13778. queryCommandValue: function (cmdName, dir) {
  13779. var pN = domUtils.filterNodeList(
  13780. this.selection.getStartElementPath(),
  13781. function (node) {
  13782. return domUtils.isBlockElm(node)
  13783. }
  13784. ),
  13785. value
  13786. //trace:1026
  13787. if (pN) {
  13788. value = domUtils
  13789. .getComputedStyle(pN, 'margin-' + dir)
  13790. .replace(/[^\d]/g, '')
  13791. return !value ? 0 : value
  13792. }
  13793. return 0
  13794. },
  13795. }
  13796. }
  13797. // plugins/lineheight.js
  13798. /**
  13799. * 设置行内间距
  13800. * @file
  13801. * @since 1.2.6.1
  13802. */
  13803. UE.plugins['lineheight'] = function () {
  13804. var me = this
  13805. me.setOpt({ lineheight: ['1', '1.5', '1.75', '2', '3', '4', '5'] })
  13806. /**
  13807. * 行距
  13808. * @command lineheight
  13809. * @method execCommand
  13810. * @param { String } cmdName 命令字符串
  13811. * @param { String } value 传入的行高值, 该值是当前字体的倍数, 例如: 1.5, 1.75
  13812. * @example
  13813. * ```javascript
  13814. * editor.execCommand( 'lineheight', 1.5);
  13815. * ```
  13816. */
  13817. /**
  13818. * 查询当前选区内容的行高大小
  13819. * @command lineheight
  13820. * @method queryCommandValue
  13821. * @param { String } cmd 命令字符串
  13822. * @return { String } 返回当前行高大小
  13823. * @example
  13824. * ```javascript
  13825. * editor.queryCommandValue( 'lineheight' );
  13826. * ```
  13827. */
  13828. me.commands['lineheight'] = {
  13829. execCommand: function (cmdName, value) {
  13830. this.execCommand('paragraph', 'p', {
  13831. style: 'line-height:' + (value == '1' ? 'normal' : value + 'em'),
  13832. })
  13833. return true
  13834. },
  13835. queryCommandValue: function () {
  13836. var pN = domUtils.filterNodeList(
  13837. this.selection.getStartElementPath(),
  13838. function (node) {
  13839. return domUtils.isBlockElm(node)
  13840. }
  13841. )
  13842. if (pN) {
  13843. var value = domUtils.getComputedStyle(pN, 'line-height')
  13844. return value == 'normal' ? 1 : value.replace(/[^\d.]*/gi, '')
  13845. }
  13846. },
  13847. }
  13848. }
  13849. // plugins/insertcode.js
  13850. /**
  13851. * 插入代码插件
  13852. * @file
  13853. * @since 1.2.6.1
  13854. */
  13855. UE.plugins['insertcode'] = function () {
  13856. var me = this
  13857. me.ready(function () {
  13858. utils.cssRule(
  13859. 'pre',
  13860. 'pre{margin:.5em 0;padding:.4em .6em;border-radius:8px;background:#f8f8f8;}',
  13861. me.document
  13862. )
  13863. })
  13864. me.setOpt('insertcode', {
  13865. as3: 'ActionScript3',
  13866. bash: 'Bash/Shell',
  13867. cpp: 'C/C++',
  13868. css: 'Css',
  13869. cf: 'CodeFunction',
  13870. 'c#': 'C#',
  13871. delphi: 'Delphi',
  13872. diff: 'Diff',
  13873. erlang: 'Erlang',
  13874. groovy: 'Groovy',
  13875. html: 'Html',
  13876. java: 'Java',
  13877. jfx: 'JavaFx',
  13878. js: 'Javascript',
  13879. pl: 'Perl',
  13880. php: 'Php',
  13881. plain: 'Plain Text',
  13882. ps: 'PowerShell',
  13883. python: 'Python',
  13884. ruby: 'Ruby',
  13885. scala: 'Scala',
  13886. sql: 'Sql',
  13887. vb: 'Vb',
  13888. xml: 'Xml',
  13889. })
  13890. /**
  13891. * 插入代码
  13892. * @command insertcode
  13893. * @method execCommand
  13894. * @param { String } cmd 命令字符串
  13895. * @param { String } lang 插入代码的语言
  13896. * @example
  13897. * ```javascript
  13898. * editor.execCommand( 'insertcode', 'javascript' );
  13899. * ```
  13900. */
  13901. /**
  13902. * 如果选区所在位置是插入插入代码区域,返回代码的语言
  13903. * @command insertcode
  13904. * @method queryCommandValue
  13905. * @param { String } cmd 命令字符串
  13906. * @return { String } 返回代码的语言
  13907. * @example
  13908. * ```javascript
  13909. * editor.queryCommandValue( 'insertcode' );
  13910. * ```
  13911. */
  13912. me.commands['insertcode'] = {
  13913. execCommand: function (cmd, lang) {
  13914. var me = this,
  13915. rng = me.selection.getRange(),
  13916. pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)
  13917. if (pre) {
  13918. pre.className = 'brush:' + lang + ';toolbar:false;'
  13919. } else {
  13920. var code = ''
  13921. if (rng.collapsed) {
  13922. code =
  13923. browser.ie && browser.ie11below
  13924. ? browser.version <= 8
  13925. ? '&nbsp;'
  13926. : ''
  13927. : '<br/>'
  13928. } else {
  13929. var frag = rng.extractContents()
  13930. var div = me.document.createElement('div')
  13931. div.appendChild(frag)
  13932. utils.each(
  13933. UE.filterNode(
  13934. UE.htmlparser(div.innerHTML.replace(/[\r\t]/g, '')),
  13935. me.options.filterTxtRules
  13936. ).children,
  13937. function (node) {
  13938. if (browser.ie && browser.ie11below && browser.version > 8) {
  13939. if (node.type == 'element') {
  13940. if (node.tagName == 'br') {
  13941. code += '\n'
  13942. } else if (!dtd.$empty[node.tagName]) {
  13943. utils.each(node.children, function (cn) {
  13944. if (cn.type == 'element') {
  13945. if (cn.tagName == 'br') {
  13946. code += '\n'
  13947. } else if (!dtd.$empty[node.tagName]) {
  13948. code += cn.innerText()
  13949. }
  13950. } else {
  13951. code += cn.data
  13952. }
  13953. })
  13954. if (!/\n$/.test(code)) {
  13955. code += '\n'
  13956. }
  13957. }
  13958. } else {
  13959. code += node.data + '\n'
  13960. }
  13961. if (!node.nextSibling() && /\n$/.test(code)) {
  13962. code = code.replace(/\n$/, '')
  13963. }
  13964. } else {
  13965. if (browser.ie && browser.ie11below) {
  13966. if (node.type == 'element') {
  13967. if (node.tagName == 'br') {
  13968. code += '<br>'
  13969. } else if (!dtd.$empty[node.tagName]) {
  13970. utils.each(node.children, function (cn) {
  13971. if (cn.type == 'element') {
  13972. if (cn.tagName == 'br') {
  13973. code += '<br>'
  13974. } else if (!dtd.$empty[node.tagName]) {
  13975. code += cn.innerText()
  13976. }
  13977. } else {
  13978. code += cn.data
  13979. }
  13980. })
  13981. if (!/br>$/.test(code)) {
  13982. code += '<br>'
  13983. }
  13984. }
  13985. } else {
  13986. code += node.data + '<br>'
  13987. }
  13988. if (!node.nextSibling() && /<br>$/.test(code)) {
  13989. code = code.replace(/<br>$/, '')
  13990. }
  13991. } else {
  13992. code +=
  13993. node.type == 'element'
  13994. ? dtd.$empty[node.tagName]
  13995. ? ''
  13996. : node.innerText()
  13997. : node.data
  13998. if (!/br\/?\s*>$/.test(code)) {
  13999. if (!node.nextSibling()) return
  14000. code += '<br>'
  14001. }
  14002. }
  14003. }
  14004. }
  14005. )
  14006. }
  14007. me.execCommand(
  14008. 'inserthtml',
  14009. '<pre id="coder"class="brush:' +
  14010. lang +
  14011. ';toolbar:false">' +
  14012. code +
  14013. '</pre>',
  14014. true
  14015. )
  14016. pre = me.document.getElementById('coder')
  14017. domUtils.removeAttributes(pre, 'id')
  14018. var tmpNode = pre.previousSibling
  14019. if (
  14020. tmpNode &&
  14021. ((tmpNode.nodeType == 3 &&
  14022. tmpNode.nodeValue.length == 1 &&
  14023. browser.ie &&
  14024. browser.version == 6) ||
  14025. domUtils.isEmptyBlock(tmpNode))
  14026. ) {
  14027. domUtils.remove(tmpNode)
  14028. }
  14029. var rng = me.selection.getRange()
  14030. if (domUtils.isEmptyBlock(pre)) {
  14031. rng.setStart(pre, 0).setCursor(false, true)
  14032. } else {
  14033. rng.selectNodeContents(pre).select()
  14034. }
  14035. }
  14036. },
  14037. queryCommandValue: function () {
  14038. var path = this.selection.getStartElementPath()
  14039. var lang = ''
  14040. utils.each(path, function (node) {
  14041. if (node.nodeName == 'PRE') {
  14042. var match = node.className.match(/brush:([^;]+)/)
  14043. lang = match && match[1] ? match[1] : ''
  14044. return false
  14045. }
  14046. })
  14047. return lang
  14048. },
  14049. }
  14050. me.addInputRule(function (root) {
  14051. utils.each(root.getNodesByTagName('pre'), function (pre) {
  14052. var brs = pre.getNodesByTagName('br')
  14053. if (brs.length) {
  14054. browser.ie &&
  14055. browser.ie11below &&
  14056. browser.version > 8 &&
  14057. utils.each(brs, function (br) {
  14058. var txt = UE.uNode.createText('\n')
  14059. br.parentNode.insertBefore(txt, br)
  14060. br.parentNode.removeChild(br)
  14061. })
  14062. return
  14063. }
  14064. if (browser.ie && browser.ie11below && browser.version > 8) return
  14065. var code = pre.innerText().split(/\n/)
  14066. pre.innerHTML('')
  14067. utils.each(code, function (c) {
  14068. if (c.length) {
  14069. pre.appendChild(UE.uNode.createText(c))
  14070. }
  14071. pre.appendChild(UE.uNode.createElement('br'))
  14072. })
  14073. })
  14074. })
  14075. me.addOutputRule(function (root) {
  14076. utils.each(root.getNodesByTagName('pre'), function (pre) {
  14077. var code = ''
  14078. utils.each(pre.children, function (n) {
  14079. if (n.type == 'text') {
  14080. //在ie下文本内容有可能末尾带有\n要去掉
  14081. //trace:3396
  14082. code += n.data.replace(/[ ]/g, '&nbsp;').replace(/\n$/, '')
  14083. } else {
  14084. if (n.tagName == 'br') {
  14085. code += '\n'
  14086. } else {
  14087. code += !dtd.$empty[n.tagName] ? '' : n.innerText()
  14088. }
  14089. }
  14090. })
  14091. pre.innerText(code.replace(/(&nbsp;|\n)+$/, ''))
  14092. })
  14093. })
  14094. //不需要判断highlight的command列表
  14095. me.notNeedCodeQuery = {
  14096. help: 1,
  14097. undo: 1,
  14098. redo: 1,
  14099. source: 1,
  14100. print: 1,
  14101. searchreplace: 1,
  14102. fullscreen: 1,
  14103. preview: 1,
  14104. insertparagraph: 1,
  14105. elementpath: 1,
  14106. insertcode: 1,
  14107. inserthtml: 1,
  14108. selectall: 1,
  14109. }
  14110. //将queyCommamndState重置
  14111. var orgQuery = me.queryCommandState
  14112. me.queryCommandState = function (cmd) {
  14113. var me = this
  14114. if (
  14115. !me.notNeedCodeQuery[cmd.toLowerCase()] &&
  14116. me.selection &&
  14117. me.queryCommandValue('insertcode')
  14118. ) {
  14119. return -1
  14120. }
  14121. return UE.Editor.prototype.queryCommandState.apply(this, arguments)
  14122. }
  14123. me.addListener('beforeenterkeydown', function () {
  14124. var rng = me.selection.getRange()
  14125. var pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)
  14126. if (pre) {
  14127. me.fireEvent('saveScene')
  14128. if (!rng.collapsed) {
  14129. rng.deleteContents()
  14130. }
  14131. if (!browser.ie || browser.ie9above) {
  14132. var tmpNode = me.document.createElement('br'),
  14133. pre
  14134. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true)
  14135. var next = tmpNode.nextSibling
  14136. if (!next && (!browser.ie || browser.version > 10)) {
  14137. rng.insertNode(tmpNode.cloneNode(false))
  14138. } else {
  14139. rng.setStartAfter(tmpNode)
  14140. }
  14141. pre = tmpNode.previousSibling
  14142. var tmp
  14143. while (pre) {
  14144. tmp = pre
  14145. pre = pre.previousSibling
  14146. if (!pre || pre.nodeName == 'BR') {
  14147. pre = tmp
  14148. break
  14149. }
  14150. }
  14151. if (pre) {
  14152. var str = ''
  14153. while (
  14154. pre &&
  14155. pre.nodeName != 'BR' &&
  14156. new RegExp('^[\\s' + domUtils.fillChar + ']*$').test(
  14157. pre.nodeValue
  14158. )
  14159. ) {
  14160. str += pre.nodeValue
  14161. pre = pre.nextSibling
  14162. }
  14163. if (pre.nodeName != 'BR') {
  14164. var match = pre.nodeValue.match(
  14165. new RegExp('^([\\s' + domUtils.fillChar + ']+)')
  14166. )
  14167. if (match && match[1]) {
  14168. str += match[1]
  14169. }
  14170. }
  14171. if (str) {
  14172. str = me.document.createTextNode(str)
  14173. rng.insertNode(str).setStartAfter(str)
  14174. }
  14175. }
  14176. rng.collapse(true).select(true)
  14177. } else {
  14178. if (browser.version > 8) {
  14179. var txt = me.document.createTextNode('\n')
  14180. var start = rng.startContainer
  14181. if (rng.startOffset == 0) {
  14182. var preNode = start.previousSibling
  14183. if (preNode) {
  14184. rng.insertNode(txt)
  14185. var fillchar = me.document.createTextNode(' ')
  14186. rng
  14187. .setStartAfter(txt)
  14188. .insertNode(fillchar)
  14189. .setStart(fillchar, 0)
  14190. .collapse(true)
  14191. .select(true)
  14192. }
  14193. } else {
  14194. rng.insertNode(txt).setStartAfter(txt)
  14195. var fillchar = me.document.createTextNode(' ')
  14196. start = rng.startContainer.childNodes[rng.startOffset]
  14197. if (start && !/^\n/.test(start.nodeValue)) {
  14198. rng.setStartBefore(txt)
  14199. }
  14200. rng
  14201. .insertNode(fillchar)
  14202. .setStart(fillchar, 0)
  14203. .collapse(true)
  14204. .select(true)
  14205. }
  14206. } else {
  14207. var tmpNode = me.document.createElement('br')
  14208. rng.insertNode(tmpNode)
  14209. rng.insertNode(me.document.createTextNode(domUtils.fillChar))
  14210. rng.setStartAfter(tmpNode)
  14211. pre = tmpNode.previousSibling
  14212. var tmp
  14213. while (pre) {
  14214. tmp = pre
  14215. pre = pre.previousSibling
  14216. if (!pre || pre.nodeName == 'BR') {
  14217. pre = tmp
  14218. break
  14219. }
  14220. }
  14221. if (pre) {
  14222. var str = ''
  14223. while (
  14224. pre &&
  14225. pre.nodeName != 'BR' &&
  14226. new RegExp('^[ ' + domUtils.fillChar + ']*$').test(
  14227. pre.nodeValue
  14228. )
  14229. ) {
  14230. str += pre.nodeValue
  14231. pre = pre.nextSibling
  14232. }
  14233. if (pre.nodeName != 'BR') {
  14234. var match = pre.nodeValue.match(
  14235. new RegExp('^([ ' + domUtils.fillChar + ']+)')
  14236. )
  14237. if (match && match[1]) {
  14238. str += match[1]
  14239. }
  14240. }
  14241. str = me.document.createTextNode(str)
  14242. rng.insertNode(str).setStartAfter(str)
  14243. }
  14244. rng.collapse(true).select()
  14245. }
  14246. }
  14247. me.fireEvent('saveScene')
  14248. return true
  14249. }
  14250. })
  14251. me.addListener('tabkeydown', function (cmd, evt) {
  14252. var rng = me.selection.getRange()
  14253. var pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)
  14254. if (pre) {
  14255. me.fireEvent('saveScene')
  14256. if (evt.shiftKey) {
  14257. } else {
  14258. if (!rng.collapsed) {
  14259. var bk = rng.createBookmark()
  14260. var start = bk.start.previousSibling
  14261. while (start) {
  14262. if (pre.firstChild === start && !domUtils.isBr(start)) {
  14263. pre.insertBefore(me.document.createTextNode(' '), start)
  14264. break
  14265. }
  14266. if (domUtils.isBr(start)) {
  14267. pre.insertBefore(
  14268. me.document.createTextNode(' '),
  14269. start.nextSibling
  14270. )
  14271. break
  14272. }
  14273. start = start.previousSibling
  14274. }
  14275. var end = bk.end
  14276. start = bk.start.nextSibling
  14277. if (pre.firstChild === bk.start) {
  14278. pre.insertBefore(
  14279. me.document.createTextNode(' '),
  14280. start.nextSibling
  14281. )
  14282. }
  14283. while (start && start !== end) {
  14284. if (domUtils.isBr(start) && start.nextSibling) {
  14285. if (start.nextSibling === end) {
  14286. break
  14287. }
  14288. pre.insertBefore(
  14289. me.document.createTextNode(' '),
  14290. start.nextSibling
  14291. )
  14292. }
  14293. start = start.nextSibling
  14294. }
  14295. rng.moveToBookmark(bk).select()
  14296. } else {
  14297. var tmpNode = me.document.createTextNode(' ')
  14298. rng
  14299. .insertNode(tmpNode)
  14300. .setStartAfter(tmpNode)
  14301. .collapse(true)
  14302. .select(true)
  14303. }
  14304. }
  14305. me.fireEvent('saveScene')
  14306. return true
  14307. }
  14308. })
  14309. me.addListener('beforeinserthtml', function (evtName, html) {
  14310. var me = this,
  14311. rng = me.selection.getRange(),
  14312. pre = domUtils.findParentByTagName(rng.startContainer, 'pre', true)
  14313. if (pre) {
  14314. if (!rng.collapsed) {
  14315. rng.deleteContents()
  14316. }
  14317. var htmlstr = ''
  14318. if (browser.ie && browser.version > 8) {
  14319. utils.each(
  14320. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14321. .children,
  14322. function (node) {
  14323. if (node.type == 'element') {
  14324. if (node.tagName == 'br') {
  14325. htmlstr += '\n'
  14326. } else if (!dtd.$empty[node.tagName]) {
  14327. utils.each(node.children, function (cn) {
  14328. if (cn.type == 'element') {
  14329. if (cn.tagName == 'br') {
  14330. htmlstr += '\n'
  14331. } else if (!dtd.$empty[node.tagName]) {
  14332. htmlstr += cn.innerText()
  14333. }
  14334. } else {
  14335. htmlstr += cn.data
  14336. }
  14337. })
  14338. if (!/\n$/.test(htmlstr)) {
  14339. htmlstr += '\n'
  14340. }
  14341. }
  14342. } else {
  14343. htmlstr += node.data + '\n'
  14344. }
  14345. if (!node.nextSibling() && /\n$/.test(htmlstr)) {
  14346. htmlstr = htmlstr.replace(/\n$/, '')
  14347. }
  14348. }
  14349. )
  14350. var tmpNode = me.document.createTextNode(
  14351. utils.html(htmlstr.replace(/&nbsp;/g, ' '))
  14352. )
  14353. rng.insertNode(tmpNode).selectNode(tmpNode).select()
  14354. } else {
  14355. var frag = me.document.createDocumentFragment()
  14356. utils.each(
  14357. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14358. .children,
  14359. function (node) {
  14360. if (node.type == 'element') {
  14361. if (node.tagName == 'br') {
  14362. frag.appendChild(me.document.createElement('br'))
  14363. } else if (!dtd.$empty[node.tagName]) {
  14364. utils.each(node.children, function (cn) {
  14365. if (cn.type == 'element') {
  14366. if (cn.tagName == 'br') {
  14367. frag.appendChild(me.document.createElement('br'))
  14368. } else if (!dtd.$empty[node.tagName]) {
  14369. frag.appendChild(
  14370. me.document.createTextNode(
  14371. utils.html(cn.innerText().replace(/&nbsp;/g, ' '))
  14372. )
  14373. )
  14374. }
  14375. } else {
  14376. frag.appendChild(
  14377. me.document.createTextNode(
  14378. utils.html(cn.data.replace(/&nbsp;/g, ' '))
  14379. )
  14380. )
  14381. }
  14382. })
  14383. if (frag.lastChild.nodeName != 'BR') {
  14384. frag.appendChild(me.document.createElement('br'))
  14385. }
  14386. }
  14387. } else {
  14388. frag.appendChild(
  14389. me.document.createTextNode(
  14390. utils.html(node.data.replace(/&nbsp;/g, ' '))
  14391. )
  14392. )
  14393. }
  14394. if (!node.nextSibling() && frag.lastChild.nodeName == 'BR') {
  14395. frag.removeChild(frag.lastChild)
  14396. }
  14397. }
  14398. )
  14399. rng.insertNode(frag).select()
  14400. }
  14401. return true
  14402. }
  14403. })
  14404. //方向键的处理
  14405. me.addListener('keydown', function (cmd, evt) {
  14406. var me = this,
  14407. keyCode = evt.keyCode || evt.which
  14408. if (keyCode == 40) {
  14409. var rng = me.selection.getRange(),
  14410. pre,
  14411. start = rng.startContainer
  14412. if (
  14413. rng.collapsed &&
  14414. (pre = domUtils.findParentByTagName(
  14415. rng.startContainer,
  14416. 'pre',
  14417. true
  14418. )) &&
  14419. !pre.nextSibling
  14420. ) {
  14421. var last = pre.lastChild
  14422. while (last && last.nodeName == 'BR') {
  14423. last = last.previousSibling
  14424. }
  14425. if (
  14426. last === start ||
  14427. (rng.startContainer === pre &&
  14428. rng.startOffset == pre.childNodes.length)
  14429. ) {
  14430. me.execCommand('insertparagraph')
  14431. domUtils.preventDefault(evt)
  14432. }
  14433. }
  14434. }
  14435. })
  14436. //trace:3395
  14437. me.addListener('delkeydown', function (type, evt) {
  14438. var rng = this.selection.getRange()
  14439. rng.txtToElmBoundary(true)
  14440. var start = rng.startContainer
  14441. if (
  14442. domUtils.isTagNode(start, 'pre') &&
  14443. rng.collapsed &&
  14444. domUtils.isStartInblock(rng)
  14445. ) {
  14446. var p = me.document.createElement('p')
  14447. domUtils.fillNode(me.document, p)
  14448. start.parentNode.insertBefore(p, start)
  14449. domUtils.remove(start)
  14450. rng.setStart(p, 0).setCursor(false, true)
  14451. domUtils.preventDefault(evt)
  14452. return true
  14453. }
  14454. })
  14455. }
  14456. // plugins/cleardoc.js
  14457. /**
  14458. * 清空文档插件
  14459. * @file
  14460. * @since 1.2.6.1
  14461. */
  14462. /**
  14463. * 清空文档
  14464. * @command cleardoc
  14465. * @method execCommand
  14466. * @param { String } cmd 命令字符串
  14467. * @example
  14468. * ```javascript
  14469. * //editor 是编辑器实例
  14470. * editor.execCommand('cleardoc');
  14471. * ```
  14472. */
  14473. UE.commands['cleardoc'] = {
  14474. execCommand: function (cmdName) {
  14475. var me = this,
  14476. enterTag = me.options.enterTag,
  14477. range = me.selection.getRange()
  14478. if (enterTag == 'br') {
  14479. me.body.innerHTML = '<br/>'
  14480. range.setStart(me.body, 0).setCursor()
  14481. } else {
  14482. me.body.innerHTML = '<p>' + (ie ? '' : '<br/>') + '</p>'
  14483. range.setStart(me.body.firstChild, 0).setCursor(false, true)
  14484. }
  14485. setTimeout(function () {
  14486. me.fireEvent('clearDoc')
  14487. }, 0)
  14488. },
  14489. }
  14490. // plugins/anchor.js
  14491. /**
  14492. * 锚点插件,为UEditor提供插入锚点支持
  14493. * @file
  14494. * @since 1.2.6.1
  14495. */
  14496. UE.plugin.register('anchor', function () {
  14497. return {
  14498. bindEvents: {
  14499. ready: function () {
  14500. utils.cssRule(
  14501. 'anchor',
  14502. ".anchorclass{background: url('" +
  14503. this.options.themePath +
  14504. this.options.theme +
  14505. "/images/anchor.gif') no-repeat scroll left center transparent;cursor: auto;display: inline-block;height: 16px;width: 15px;}",
  14506. this.document
  14507. )
  14508. },
  14509. },
  14510. outputRule: function (root) {
  14511. utils.each(root.getNodesByTagName('img'), function (a) {
  14512. var val
  14513. if ((val = a.getAttr('anchorname'))) {
  14514. a.tagName = 'a'
  14515. a.setAttr({
  14516. anchorname: '',
  14517. name: val,
  14518. class: '',
  14519. })
  14520. }
  14521. })
  14522. },
  14523. inputRule: function (root) {
  14524. utils.each(root.getNodesByTagName('a'), function (a) {
  14525. var val
  14526. if ((val = a.getAttr('name')) && !a.getAttr('href')) {
  14527. a.tagName = 'img'
  14528. a.setAttr({
  14529. anchorname: a.getAttr('name'),
  14530. class: 'anchorclass',
  14531. })
  14532. a.setAttr('name')
  14533. }
  14534. })
  14535. },
  14536. commands: {
  14537. /**
  14538. * 插入锚点
  14539. * @command anchor
  14540. * @method execCommand
  14541. * @param { String } cmd 命令字符串
  14542. * @param { String } name 锚点名称字符串
  14543. * @example
  14544. * ```javascript
  14545. * //editor 是编辑器实例
  14546. * editor.execCommand('anchor', 'anchor1');
  14547. * ```
  14548. */
  14549. anchor: {
  14550. execCommand: function (cmd, name) {
  14551. var range = this.selection.getRange(),
  14552. img = range.getClosedNode()
  14553. if (img && img.getAttribute('anchorname')) {
  14554. if (name) {
  14555. img.setAttribute('anchorname', name)
  14556. } else {
  14557. range.setStartBefore(img).setCursor()
  14558. domUtils.remove(img)
  14559. }
  14560. } else {
  14561. if (name) {
  14562. //只在选区的开始插入
  14563. var anchor = this.document.createElement('img')
  14564. range.collapse(true)
  14565. domUtils.setAttributes(anchor, {
  14566. anchorname: name,
  14567. class: 'anchorclass',
  14568. })
  14569. range
  14570. .insertNode(anchor)
  14571. .setStartAfter(anchor)
  14572. .setCursor(false, true)
  14573. }
  14574. }
  14575. },
  14576. },
  14577. },
  14578. }
  14579. })
  14580. // plugins/wordcount.js
  14581. ///import core
  14582. ///commands 字数统计
  14583. ///commandsName WordCount,wordCount
  14584. ///commandsTitle 字数统计
  14585. /*
  14586. * Created by JetBrains WebStorm.
  14587. * User: taoqili
  14588. * Date: 11-9-7
  14589. * Time: 下午8:18
  14590. * To change this template use File | Settings | File Templates.
  14591. */
  14592. UE.plugins['wordcount'] = function () {
  14593. var me = this
  14594. me.setOpt('wordCount', true)
  14595. me.addListener('contentchange', function () {
  14596. me.fireEvent('wordcount')
  14597. })
  14598. var timer
  14599. me.addListener('ready', function () {
  14600. var me = this
  14601. domUtils.on(me.body, 'keyup', function (evt) {
  14602. var code = evt.keyCode || evt.which,
  14603. //忽略的按键,ctr,alt,shift,方向键
  14604. ignores = { 16: 1, 18: 1, 20: 1, 37: 1, 38: 1, 39: 1, 40: 1 }
  14605. if (code in ignores) return
  14606. clearTimeout(timer)
  14607. timer = setTimeout(function () {
  14608. me.fireEvent('wordcount')
  14609. }, 200)
  14610. })
  14611. })
  14612. }
  14613. // plugins/pagebreak.js
  14614. /**
  14615. * 分页功能插件
  14616. * @file
  14617. * @since 1.2.6.1
  14618. */
  14619. UE.plugins['pagebreak'] = function () {
  14620. var me = this,
  14621. notBreakTags = ['td']
  14622. me.setOpt('pageBreakTag', '_ueditor_page_break_tag_')
  14623. function fillNode(node) {
  14624. if (domUtils.isEmptyBlock(node)) {
  14625. var firstChild = node.firstChild,
  14626. tmpNode
  14627. while (
  14628. firstChild &&
  14629. firstChild.nodeType == 1 &&
  14630. domUtils.isEmptyBlock(firstChild)
  14631. ) {
  14632. tmpNode = firstChild
  14633. firstChild = firstChild.firstChild
  14634. }
  14635. !tmpNode && (tmpNode = node)
  14636. domUtils.fillNode(me.document, tmpNode)
  14637. }
  14638. }
  14639. //分页符样式添加
  14640. me.ready(function () {
  14641. utils.cssRule(
  14642. 'pagebreak',
  14643. '.pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}',
  14644. me.document
  14645. )
  14646. })
  14647. function isHr(node) {
  14648. return (
  14649. node &&
  14650. node.nodeType == 1 &&
  14651. node.tagName == 'HR' &&
  14652. node.className == 'pagebreak'
  14653. )
  14654. }
  14655. me.addInputRule(function (root) {
  14656. root.traversal(function (node) {
  14657. if (node.type == 'text' && node.data == me.options.pageBreakTag) {
  14658. var hr = UE.uNode.createElement(
  14659. '<hr class="pagebreak" noshade="noshade" size="5" style="-webkit-user-select: none;">'
  14660. )
  14661. node.parentNode.insertBefore(hr, node)
  14662. node.parentNode.removeChild(node)
  14663. }
  14664. })
  14665. })
  14666. me.addOutputRule(function (node) {
  14667. utils.each(node.getNodesByTagName('hr'), function (n) {
  14668. if (n.getAttr('class') == 'pagebreak') {
  14669. var txt = UE.uNode.createText(me.options.pageBreakTag)
  14670. n.parentNode.insertBefore(txt, n)
  14671. n.parentNode.removeChild(n)
  14672. }
  14673. })
  14674. })
  14675. /**
  14676. * 插入分页符
  14677. * @command pagebreak
  14678. * @method execCommand
  14679. * @param { String } cmd 命令字符串
  14680. * @remind 在表格中插入分页符会把表格切分成两部分
  14681. * @remind 获取编辑器内的数据时, 编辑器会把分页符转换成“_ueditor_page_break_tag_”字符串,
  14682. * 以便于提交数据到服务器端后处理分页。
  14683. * @example
  14684. * ```javascript
  14685. * editor.execCommand( 'pagebreak'); //插入一个hr标签,带有样式类名pagebreak
  14686. * ```
  14687. */
  14688. me.commands['pagebreak'] = {
  14689. execCommand: function () {
  14690. var range = me.selection.getRange(),
  14691. hr = me.document.createElement('hr')
  14692. domUtils.setAttributes(hr, {
  14693. class: 'pagebreak',
  14694. noshade: 'noshade',
  14695. size: '5',
  14696. })
  14697. domUtils.unSelectable(hr)
  14698. //table单独处理
  14699. var node = domUtils.findParentByTagName(
  14700. range.startContainer,
  14701. notBreakTags,
  14702. true
  14703. ),
  14704. parents = [],
  14705. pN
  14706. if (node) {
  14707. switch (node.tagName) {
  14708. case 'TD':
  14709. pN = node.parentNode
  14710. if (!pN.previousSibling) {
  14711. var table = domUtils.findParentByTagName(pN, 'table')
  14712. // var tableWrapDiv = table.parentNode;
  14713. // if(tableWrapDiv && tableWrapDiv.nodeType == 1
  14714. // && tableWrapDiv.tagName == 'DIV'
  14715. // && tableWrapDiv.getAttribute('dropdrag')
  14716. // ){
  14717. // domUtils.remove(tableWrapDiv,true);
  14718. // }
  14719. table.parentNode.insertBefore(hr, table)
  14720. parents = domUtils.findParents(hr, true)
  14721. } else {
  14722. pN.parentNode.insertBefore(hr, pN)
  14723. parents = domUtils.findParents(hr)
  14724. }
  14725. pN = parents[1]
  14726. if (hr !== pN) {
  14727. domUtils.breakParent(hr, pN)
  14728. }
  14729. //table要重写绑定一下拖拽
  14730. me.fireEvent('afteradjusttable', me.document)
  14731. }
  14732. } else {
  14733. if (!range.collapsed) {
  14734. range.deleteContents()
  14735. var start = range.startContainer
  14736. while (
  14737. !domUtils.isBody(start) &&
  14738. domUtils.isBlockElm(start) &&
  14739. domUtils.isEmptyNode(start)
  14740. ) {
  14741. range.setStartBefore(start).collapse(true)
  14742. domUtils.remove(start)
  14743. start = range.startContainer
  14744. }
  14745. }
  14746. range.insertNode(hr)
  14747. var pN = hr.parentNode,
  14748. nextNode
  14749. while (!domUtils.isBody(pN)) {
  14750. domUtils.breakParent(hr, pN)
  14751. nextNode = hr.nextSibling
  14752. if (nextNode && domUtils.isEmptyBlock(nextNode)) {
  14753. domUtils.remove(nextNode)
  14754. }
  14755. pN = hr.parentNode
  14756. }
  14757. nextNode = hr.nextSibling
  14758. var pre = hr.previousSibling
  14759. if (isHr(pre)) {
  14760. domUtils.remove(pre)
  14761. } else {
  14762. pre && fillNode(pre)
  14763. }
  14764. if (!nextNode) {
  14765. var p = me.document.createElement('p')
  14766. hr.parentNode.appendChild(p)
  14767. domUtils.fillNode(me.document, p)
  14768. range.setStart(p, 0).collapse(true)
  14769. } else {
  14770. if (isHr(nextNode)) {
  14771. domUtils.remove(nextNode)
  14772. } else {
  14773. fillNode(nextNode)
  14774. }
  14775. range.setEndAfter(hr).collapse(false)
  14776. }
  14777. range.select(true)
  14778. }
  14779. },
  14780. }
  14781. }
  14782. // plugins/wordimage.js
  14783. ///import core
  14784. ///commands 本地图片引导上传
  14785. ///commandsName WordImage
  14786. ///commandsTitle 本地图片引导上传
  14787. ///commandsDialog dialogs\wordimage
  14788. UE.plugin.register('wordimage', function () {
  14789. var me = this,
  14790. images = []
  14791. return {
  14792. commands: {
  14793. wordimage: {
  14794. execCommand: function () {
  14795. var images = domUtils.getElementsByTagName(me.body, 'img')
  14796. var urlList = []
  14797. for (var i = 0, ci; (ci = images[i++]); ) {
  14798. var url = ci.getAttribute('word_img')
  14799. url && urlList.push(url)
  14800. }
  14801. return urlList
  14802. },
  14803. queryCommandState: function () {
  14804. images = domUtils.getElementsByTagName(me.body, 'img')
  14805. for (var i = 0, ci; (ci = images[i++]); ) {
  14806. if (ci.getAttribute('word_img')) {
  14807. return 1
  14808. }
  14809. }
  14810. return -1
  14811. },
  14812. notNeedUndo: true,
  14813. },
  14814. },
  14815. inputRule: function (root) {
  14816. utils.each(root.getNodesByTagName('img'), function (img) {
  14817. var attrs = img.attrs,
  14818. flag = parseInt(attrs.width) < 128 || parseInt(attrs.height) < 43,
  14819. opt = me.options,
  14820. src = opt.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif'
  14821. if (attrs['src'] && /^(?:(file:\/+))/.test(attrs['src'])) {
  14822. img.setAttr({
  14823. width: attrs.width,
  14824. height: attrs.height,
  14825. alt: attrs.alt,
  14826. word_img: attrs.src,
  14827. src: src,
  14828. style:
  14829. 'background:url(' +
  14830. (flag
  14831. ? opt.themePath + opt.theme + '/images/word.gif'
  14832. : opt.langPath + opt.lang + '/images/localimage.png') +
  14833. ') no-repeat center center;border:1px solid #ddd',
  14834. })
  14835. }
  14836. })
  14837. },
  14838. }
  14839. })
  14840. // plugins/dragdrop.js
  14841. UE.plugins['dragdrop'] = function () {
  14842. var me = this
  14843. me.ready(function () {
  14844. domUtils.on(this.body, 'dragend', function () {
  14845. var rng = me.selection.getRange()
  14846. var node = rng.getClosedNode() || me.selection.getStart()
  14847. if (node && node.tagName == 'IMG') {
  14848. var pre = node.previousSibling,
  14849. next
  14850. while ((next = node.nextSibling)) {
  14851. if (
  14852. next.nodeType == 1 &&
  14853. next.tagName == 'SPAN' &&
  14854. !next.firstChild
  14855. ) {
  14856. domUtils.remove(next)
  14857. } else {
  14858. break
  14859. }
  14860. }
  14861. if (
  14862. ((pre && pre.nodeType == 1 && !domUtils.isEmptyBlock(pre)) ||
  14863. !pre) &&
  14864. (!next || (next && !domUtils.isEmptyBlock(next)))
  14865. ) {
  14866. if (pre && pre.tagName == 'P' && !domUtils.isEmptyBlock(pre)) {
  14867. pre.appendChild(node)
  14868. domUtils.moveChild(next, pre)
  14869. domUtils.remove(next)
  14870. } else if (
  14871. next &&
  14872. next.tagName == 'P' &&
  14873. !domUtils.isEmptyBlock(next)
  14874. ) {
  14875. next.insertBefore(node, next.firstChild)
  14876. }
  14877. if (pre && pre.tagName == 'P' && domUtils.isEmptyBlock(pre)) {
  14878. domUtils.remove(pre)
  14879. }
  14880. if (next && next.tagName == 'P' && domUtils.isEmptyBlock(next)) {
  14881. domUtils.remove(next)
  14882. }
  14883. rng.selectNode(node).select()
  14884. me.fireEvent('saveScene')
  14885. }
  14886. }
  14887. })
  14888. })
  14889. me.addListener('keyup', function (type, evt) {
  14890. var keyCode = evt.keyCode || evt.which
  14891. if (keyCode == 13) {
  14892. var rng = me.selection.getRange(),
  14893. node
  14894. if (
  14895. (node = domUtils.findParentByTagName(rng.startContainer, 'p', true))
  14896. ) {
  14897. if (domUtils.getComputedStyle(node, 'text-align') == 'center') {
  14898. domUtils.removeStyle(node, 'text-align')
  14899. }
  14900. }
  14901. }
  14902. })
  14903. }
  14904. // plugins/undo.js
  14905. /**
  14906. * undo redo
  14907. * @file
  14908. * @since 1.2.6.1
  14909. */
  14910. /**
  14911. * 撤销上一次执行的命令
  14912. * @command undo
  14913. * @method execCommand
  14914. * @param { String } cmd 命令字符串
  14915. * @example
  14916. * ```javascript
  14917. * editor.execCommand( 'undo' );
  14918. * ```
  14919. */
  14920. /**
  14921. * 重做上一次执行的命令
  14922. * @command redo
  14923. * @method execCommand
  14924. * @param { String } cmd 命令字符串
  14925. * @example
  14926. * ```javascript
  14927. * editor.execCommand( 'redo' );
  14928. * ```
  14929. */
  14930. UE.plugins['undo'] = function () {
  14931. var saveSceneTimer
  14932. var me = this,
  14933. maxUndoCount = me.options.maxUndoCount || 20,
  14934. maxInputCount = me.options.maxInputCount || 20,
  14935. fillchar = new RegExp(domUtils.fillChar + '|</hr>', 'gi') // ie会产生多余的</hr>
  14936. var noNeedFillCharTags = {
  14937. ol: 1,
  14938. ul: 1,
  14939. table: 1,
  14940. tbody: 1,
  14941. tr: 1,
  14942. body: 1,
  14943. }
  14944. var orgState = me.options.autoClearEmptyNode
  14945. function compareAddr(indexA, indexB) {
  14946. if (indexA.length != indexB.length) return 0
  14947. for (var i = 0, l = indexA.length; i < l; i++) {
  14948. if (indexA[i] != indexB[i]) return 0
  14949. }
  14950. return 1
  14951. }
  14952. function compareRangeAddress(rngAddrA, rngAddrB) {
  14953. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  14954. return 0
  14955. }
  14956. if (
  14957. !compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) ||
  14958. !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)
  14959. ) {
  14960. return 0
  14961. }
  14962. return 1
  14963. }
  14964. function UndoManager() {
  14965. this.list = []
  14966. this.index = 0
  14967. this.hasUndo = false
  14968. this.hasRedo = false
  14969. this.undo = function () {
  14970. if (this.hasUndo) {
  14971. if (!this.list[this.index - 1] && this.list.length == 1) {
  14972. this.reset()
  14973. return
  14974. }
  14975. while (
  14976. this.list[this.index].content == this.list[this.index - 1].content
  14977. ) {
  14978. this.index--
  14979. if (this.index == 0) {
  14980. return this.restore(0)
  14981. }
  14982. }
  14983. this.restore(--this.index)
  14984. }
  14985. }
  14986. this.redo = function () {
  14987. if (this.hasRedo) {
  14988. while (
  14989. this.list[this.index].content == this.list[this.index + 1].content
  14990. ) {
  14991. this.index++
  14992. if (this.index == this.list.length - 1) {
  14993. return this.restore(this.index)
  14994. }
  14995. }
  14996. this.restore(++this.index)
  14997. }
  14998. }
  14999. this.restore = function () {
  15000. var me = this.editor
  15001. var scene = this.list[this.index]
  15002. var root = UE.htmlparser(scene.content.replace(fillchar, ''))
  15003. me.options.autoClearEmptyNode = false
  15004. me.filterInputRule(root)
  15005. me.options.autoClearEmptyNode = orgState
  15006. //trace:873
  15007. //去掉展位符
  15008. me.document.body.innerHTML = root.toHtml()
  15009. me.fireEvent('afterscencerestore')
  15010. //处理undo后空格不展位的问题
  15011. if (browser.ie) {
  15012. utils.each(
  15013. domUtils.getElementsByTagName(me.document, 'td th caption p'),
  15014. function (node) {
  15015. if (domUtils.isEmptyNode(node)) {
  15016. domUtils.fillNode(me.document, node)
  15017. }
  15018. }
  15019. )
  15020. }
  15021. try {
  15022. var rng = new dom.Range(me.document).moveToAddress(scene.address)
  15023. rng.select(
  15024. noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]
  15025. )
  15026. } catch (e) {}
  15027. this.update()
  15028. this.clearKey()
  15029. //不能把自己reset了
  15030. me.fireEvent('reset', true)
  15031. }
  15032. this.getScene = function () {
  15033. var me = this.editor
  15034. var rng = me.selection.getRange(),
  15035. rngAddress = rng.createAddress(false, true)
  15036. me.fireEvent('beforegetscene')
  15037. var root = UE.htmlparser(me.body.innerHTML)
  15038. me.options.autoClearEmptyNode = false
  15039. me.filterOutputRule(root)
  15040. me.options.autoClearEmptyNode = orgState
  15041. var cont = root.toHtml()
  15042. //trace:3461
  15043. //这个会引起回退时导致空格丢失的情况
  15044. // browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  15045. me.fireEvent('aftergetscene')
  15046. return {
  15047. address: rngAddress,
  15048. content: cont,
  15049. }
  15050. }
  15051. this.save = function (notCompareRange, notSetCursor) {
  15052. clearTimeout(saveSceneTimer)
  15053. var currentScene = this.getScene(notSetCursor),
  15054. lastScene = this.list[this.index]
  15055. if (lastScene && lastScene.content != currentScene.content) {
  15056. me.trigger('contentchange')
  15057. }
  15058. //内容相同位置相同不存
  15059. if (
  15060. lastScene &&
  15061. lastScene.content == currentScene.content &&
  15062. (notCompareRange
  15063. ? 1
  15064. : compareRangeAddress(lastScene.address, currentScene.address))
  15065. ) {
  15066. return
  15067. }
  15068. this.list = this.list.slice(0, this.index + 1)
  15069. this.list.push(currentScene)
  15070. //如果大于最大数量了,就把最前的剔除
  15071. if (this.list.length > maxUndoCount) {
  15072. this.list.shift()
  15073. }
  15074. this.index = this.list.length - 1
  15075. this.clearKey()
  15076. //跟新undo/redo状态
  15077. this.update()
  15078. }
  15079. this.update = function () {
  15080. this.hasRedo = !!this.list[this.index + 1]
  15081. this.hasUndo = !!this.list[this.index - 1]
  15082. }
  15083. this.reset = function () {
  15084. this.list = []
  15085. this.index = 0
  15086. this.hasUndo = false
  15087. this.hasRedo = false
  15088. this.clearKey()
  15089. }
  15090. this.clearKey = function () {
  15091. keycont = 0
  15092. lastKeyCode = null
  15093. }
  15094. }
  15095. me.undoManger = new UndoManager()
  15096. me.undoManger.editor = me
  15097. function saveScene() {
  15098. this.undoManger.save()
  15099. }
  15100. me.addListener('saveScene', function () {
  15101. var args = Array.prototype.splice.call(arguments, 1)
  15102. this.undoManger.save.apply(this.undoManger, args)
  15103. })
  15104. // me.addListener('beforeexeccommand', saveScene);
  15105. // me.addListener('afterexeccommand', saveScene);
  15106. me.addListener('reset', function (type, exclude) {
  15107. if (!exclude) {
  15108. this.undoManger.reset()
  15109. }
  15110. })
  15111. me.commands['redo'] = me.commands['undo'] = {
  15112. execCommand: function (cmdName) {
  15113. this.undoManger[cmdName]()
  15114. },
  15115. queryCommandState: function (cmdName) {
  15116. return this.undoManger[
  15117. 'has' + (cmdName.toLowerCase() == 'undo' ? 'Undo' : 'Redo')
  15118. ]
  15119. ? 0
  15120. : -1
  15121. },
  15122. notNeedUndo: 1,
  15123. }
  15124. var keys = {
  15125. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  15126. /*Shift*/ 16: 1,
  15127. /*Ctrl*/ 17: 1,
  15128. /*Alt*/ 18: 1,
  15129. 37: 1,
  15130. 38: 1,
  15131. 39: 1,
  15132. 40: 1,
  15133. },
  15134. keycont = 0,
  15135. lastKeyCode
  15136. //输入法状态下不计算字符数
  15137. var inputType = false
  15138. me.addListener('ready', function () {
  15139. domUtils.on(this.body, 'compositionstart', function () {
  15140. inputType = true
  15141. })
  15142. domUtils.on(this.body, 'compositionend', function () {
  15143. inputType = false
  15144. })
  15145. })
  15146. //快捷键
  15147. me.addshortcutkey({
  15148. Undo: 'ctrl+90', //undo
  15149. Redo: 'ctrl+89', //redo
  15150. })
  15151. var isCollapsed = true
  15152. me.addListener('keydown', function (type, evt) {
  15153. var me = this
  15154. var keyCode = evt.keyCode || evt.which
  15155. if (
  15156. !keys[keyCode] &&
  15157. !evt.ctrlKey &&
  15158. !evt.metaKey &&
  15159. !evt.shiftKey &&
  15160. !evt.altKey
  15161. ) {
  15162. if (inputType) return
  15163. if (!me.selection.getRange().collapsed) {
  15164. me.undoManger.save(false, true)
  15165. isCollapsed = false
  15166. return
  15167. }
  15168. if (me.undoManger.list.length == 0) {
  15169. me.undoManger.save(true)
  15170. }
  15171. clearTimeout(saveSceneTimer)
  15172. function save(cont) {
  15173. cont.undoManger.save(false, true)
  15174. cont.fireEvent('selectionchange')
  15175. }
  15176. saveSceneTimer = setTimeout(function () {
  15177. if (inputType) {
  15178. var interalTimer = setInterval(function () {
  15179. if (!inputType) {
  15180. save(me)
  15181. clearInterval(interalTimer)
  15182. }
  15183. }, 300)
  15184. return
  15185. }
  15186. save(me)
  15187. }, 200)
  15188. lastKeyCode = keyCode
  15189. keycont++
  15190. if (keycont >= maxInputCount) {
  15191. save(me)
  15192. }
  15193. }
  15194. })
  15195. me.addListener('keyup', function (type, evt) {
  15196. var keyCode = evt.keyCode || evt.which
  15197. if (
  15198. !keys[keyCode] &&
  15199. !evt.ctrlKey &&
  15200. !evt.metaKey &&
  15201. !evt.shiftKey &&
  15202. !evt.altKey
  15203. ) {
  15204. if (inputType) return
  15205. if (!isCollapsed) {
  15206. this.undoManger.save(false, true)
  15207. isCollapsed = true
  15208. }
  15209. }
  15210. })
  15211. //扩展实例,添加关闭和开启命令undo
  15212. me.stopCmdUndo = function () {
  15213. me.__hasEnterExecCommand = true
  15214. }
  15215. me.startCmdUndo = function () {
  15216. me.__hasEnterExecCommand = false
  15217. }
  15218. }
  15219. // plugins/copy.js
  15220. UE.plugin.register('copy', function () {
  15221. var me = this
  15222. function initZeroClipboard() {
  15223. ZeroClipboard.config({
  15224. debug: false,
  15225. swfPath:
  15226. me.options.UEDITOR_HOME_URL +
  15227. 'third-party/zeroclipboard/ZeroClipboard.swf',
  15228. })
  15229. var client = (me.zeroclipboard = new ZeroClipboard())
  15230. // 复制内容
  15231. client.on('copy', function (e) {
  15232. var client = e.client,
  15233. rng = me.selection.getRange(),
  15234. div = document.createElement('div')
  15235. div.appendChild(rng.cloneContents())
  15236. client.setText(div.innerText || div.textContent)
  15237. client.setHtml(div.innerHTML)
  15238. rng.select()
  15239. })
  15240. // hover事件传递到target
  15241. client.on('mouseover mouseout', function (e) {
  15242. var target = e.target
  15243. if (e.type == 'mouseover') {
  15244. domUtils.addClass(target, 'edui-state-hover')
  15245. } else if (e.type == 'mouseout') {
  15246. domUtils.removeClasses(target, 'edui-state-hover')
  15247. }
  15248. })
  15249. // flash加载不成功
  15250. client.on('wrongflash noflash', function () {
  15251. ZeroClipboard.destroy()
  15252. })
  15253. }
  15254. return {
  15255. bindEvents: {
  15256. ready: function () {
  15257. if (!browser.ie) {
  15258. if (window.ZeroClipboard) {
  15259. initZeroClipboard()
  15260. } else {
  15261. utils.loadFile(
  15262. document,
  15263. {
  15264. src:
  15265. me.options.UEDITOR_HOME_URL +
  15266. 'third-party/zeroclipboard/ZeroClipboard.js',
  15267. tag: 'script',
  15268. type: 'text/javascript',
  15269. defer: 'defer',
  15270. },
  15271. function () {
  15272. initZeroClipboard()
  15273. }
  15274. )
  15275. }
  15276. }
  15277. },
  15278. },
  15279. commands: {
  15280. copy: {
  15281. execCommand: function (cmd) {
  15282. if (!me.document.execCommand('copy')) {
  15283. alert(me.getLang('copymsg'))
  15284. }
  15285. },
  15286. },
  15287. },
  15288. }
  15289. })
  15290. // plugins/paste.js
  15291. ///import core
  15292. ///import plugins/inserthtml.js
  15293. ///import plugins/undo.js
  15294. ///import plugins/serialize.js
  15295. ///commands 粘贴
  15296. ///commandsName PastePlain
  15297. ///commandsTitle 纯文本粘贴模式
  15298. /**
  15299. * @description 粘贴
  15300. * @author zhanyi
  15301. */
  15302. UE.plugins['paste'] = function () {
  15303. function getClipboardData(callback) {
  15304. var doc = this.document
  15305. if (doc.getElementById('baidu_pastebin')) {
  15306. return
  15307. }
  15308. var range = this.selection.getRange(),
  15309. bk = range.createBookmark(),
  15310. //创建剪贴的容器div
  15311. pastebin = doc.createElement('div')
  15312. pastebin.id = 'baidu_pastebin'
  15313. // Safari 要求div必须有内容,才能粘贴内容进来
  15314. browser.webkit &&
  15315. pastebin.appendChild(
  15316. doc.createTextNode(domUtils.fillChar + domUtils.fillChar)
  15317. )
  15318. doc.body.appendChild(pastebin)
  15319. //trace:717 隐藏的span不能得到top
  15320. //bk.start.innerHTML = '&nbsp;';
  15321. bk.start.style.display = ''
  15322. pastebin.style.cssText =
  15323. 'position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:' +
  15324. //要在现在光标平行的位置加入,否则会出现跳动的问题
  15325. domUtils.getXY(bk.start).y +
  15326. 'px'
  15327. range.selectNodeContents(pastebin).select(true)
  15328. setTimeout(function () {
  15329. if (browser.webkit) {
  15330. for (
  15331. var i = 0, pastebins = doc.querySelectorAll('#baidu_pastebin'), pi;
  15332. (pi = pastebins[i++]);
  15333. ) {
  15334. if (domUtils.isEmptyNode(pi)) {
  15335. domUtils.remove(pi)
  15336. } else {
  15337. pastebin = pi
  15338. break
  15339. }
  15340. }
  15341. }
  15342. try {
  15343. pastebin.parentNode.removeChild(pastebin)
  15344. } catch (e) {}
  15345. range.moveToBookmark(bk).select(true)
  15346. callback(pastebin)
  15347. }, 0)
  15348. }
  15349. var me = this
  15350. me.setOpt({
  15351. retainOnlyLabelPasted: false,
  15352. })
  15353. var txtContent, htmlContent, address
  15354. function getPureHtml(html) {
  15355. return html.replace(
  15356. /<(\/?)([\w\-]+)([^>]*)>/gi,
  15357. function (a, b, tagName, attrs) {
  15358. tagName = tagName.toLowerCase()
  15359. if ({ img: 1 }[tagName]) {
  15360. return a
  15361. }
  15362. attrs = attrs.replace(
  15363. /([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi,
  15364. function (str, atr, val) {
  15365. if (
  15366. {
  15367. src: 1,
  15368. href: 1,
  15369. name: 1,
  15370. }[atr.toLowerCase()]
  15371. ) {
  15372. return atr + '=' + val + ' '
  15373. }
  15374. return ''
  15375. }
  15376. )
  15377. if (
  15378. {
  15379. span: 1,
  15380. div: 1,
  15381. }[tagName]
  15382. ) {
  15383. return ''
  15384. } else {
  15385. return '<' + b + tagName + ' ' + utils.trim(attrs) + '>'
  15386. }
  15387. }
  15388. )
  15389. }
  15390. function filter(div) {
  15391. var html
  15392. if (div.firstChild) {
  15393. //去掉cut中添加的边界值
  15394. var nodes = domUtils.getElementsByTagName(div, 'span')
  15395. for (var i = 0, ni; (ni = nodes[i++]); ) {
  15396. if (ni.id == '_baidu_cut_start' || ni.id == '_baidu_cut_end') {
  15397. domUtils.remove(ni)
  15398. }
  15399. }
  15400. if (browser.webkit) {
  15401. var brs = div.querySelectorAll('div br')
  15402. for (var i = 0, bi; (bi = brs[i++]); ) {
  15403. var pN = bi.parentNode
  15404. if (pN.tagName == 'DIV' && pN.childNodes.length == 1) {
  15405. pN.innerHTML = '<p><br/></p>'
  15406. domUtils.remove(pN)
  15407. }
  15408. }
  15409. var divs = div.querySelectorAll('#baidu_pastebin')
  15410. for (var i = 0, di; (di = divs[i++]); ) {
  15411. var tmpP = me.document.createElement('p')
  15412. di.parentNode.insertBefore(tmpP, di)
  15413. while (di.firstChild) {
  15414. tmpP.appendChild(di.firstChild)
  15415. }
  15416. domUtils.remove(di)
  15417. }
  15418. var metas = div.querySelectorAll('meta')
  15419. for (var i = 0, ci; (ci = metas[i++]); ) {
  15420. domUtils.remove(ci)
  15421. }
  15422. var brs = div.querySelectorAll('br')
  15423. for (i = 0; (ci = brs[i++]); ) {
  15424. if (/^apple-/i.test(ci.className)) {
  15425. domUtils.remove(ci)
  15426. }
  15427. }
  15428. }
  15429. if (browser.gecko) {
  15430. var dirtyNodes = div.querySelectorAll('[_moz_dirty]')
  15431. for (i = 0; (ci = dirtyNodes[i++]); ) {
  15432. ci.removeAttribute('_moz_dirty')
  15433. }
  15434. }
  15435. if (!browser.ie) {
  15436. var spans = div.querySelectorAll('span.Apple-style-span')
  15437. for (var i = 0, ci; (ci = spans[i++]); ) {
  15438. domUtils.remove(ci, true)
  15439. }
  15440. }
  15441. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  15442. html = div.innerHTML //.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  15443. //过滤word粘贴过来的冗余属性
  15444. html = UE.filterWord(html)
  15445. //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签
  15446. var root = UE.htmlparser(html)
  15447. //如果给了过滤规则就先进行过滤
  15448. if (me.options.filterRules) {
  15449. UE.filterNode(root, me.options.filterRules)
  15450. }
  15451. //执行默认的处理
  15452. me.filterInputRule(root)
  15453. //针对chrome的处理
  15454. if (browser.webkit) {
  15455. var br = root.lastChild()
  15456. if (br && br.type == 'element' && br.tagName == 'br') {
  15457. root.removeChild(br)
  15458. }
  15459. utils.each(me.body.querySelectorAll('div'), function (node) {
  15460. if (domUtils.isEmptyBlock(node)) {
  15461. domUtils.remove(node, true)
  15462. }
  15463. })
  15464. }
  15465. html = { html: root.toHtml() }
  15466. me.fireEvent('beforepaste', html, root)
  15467. //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴
  15468. if (!html.html) {
  15469. return
  15470. }
  15471. root = UE.htmlparser(html.html, true)
  15472. //如果开启了纯文本模式
  15473. if (me.queryCommandState('pasteplain') === 1) {
  15474. me.execCommand(
  15475. 'insertHtml',
  15476. UE.filterNode(root, me.options.filterTxtRules).toHtml(),
  15477. true
  15478. )
  15479. } else {
  15480. //文本模式
  15481. UE.filterNode(root, me.options.filterTxtRules)
  15482. txtContent = root.toHtml()
  15483. //完全模式
  15484. htmlContent = html.html
  15485. address = me.selection.getRange().createAddress(true)
  15486. me.execCommand(
  15487. 'insertHtml',
  15488. me.getOpt('retainOnlyLabelPasted') === true
  15489. ? getPureHtml(htmlContent)
  15490. : htmlContent,
  15491. true
  15492. )
  15493. }
  15494. me.fireEvent('afterpaste', html)
  15495. }
  15496. }
  15497. me.addListener('pasteTransfer', function (cmd, plainType) {
  15498. if (address && txtContent && htmlContent && txtContent != htmlContent) {
  15499. var range = me.selection.getRange()
  15500. range.moveToAddress(address, true)
  15501. if (!range.collapsed) {
  15502. while (!domUtils.isBody(range.startContainer)) {
  15503. var start = range.startContainer
  15504. if (start.nodeType == 1) {
  15505. start = start.childNodes[range.startOffset]
  15506. if (!start) {
  15507. range.setStartBefore(range.startContainer)
  15508. continue
  15509. }
  15510. var pre = start.previousSibling
  15511. if (
  15512. pre &&
  15513. pre.nodeType == 3 &&
  15514. new RegExp('^[\n\r\t ' + domUtils.fillChar + ']*$').test(
  15515. pre.nodeValue
  15516. )
  15517. ) {
  15518. range.setStartBefore(pre)
  15519. }
  15520. }
  15521. if (range.startOffset == 0) {
  15522. range.setStartBefore(range.startContainer)
  15523. } else {
  15524. break
  15525. }
  15526. }
  15527. while (!domUtils.isBody(range.endContainer)) {
  15528. var end = range.endContainer
  15529. if (end.nodeType == 1) {
  15530. end = end.childNodes[range.endOffset]
  15531. if (!end) {
  15532. range.setEndAfter(range.endContainer)
  15533. continue
  15534. }
  15535. var next = end.nextSibling
  15536. if (
  15537. next &&
  15538. next.nodeType == 3 &&
  15539. new RegExp('^[\n\r\t' + domUtils.fillChar + ']*$').test(
  15540. next.nodeValue
  15541. )
  15542. ) {
  15543. range.setEndAfter(next)
  15544. }
  15545. }
  15546. if (
  15547. range.endOffset ==
  15548. range.endContainer[
  15549. range.endContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  15550. ].length
  15551. ) {
  15552. range.setEndAfter(range.endContainer)
  15553. } else {
  15554. break
  15555. }
  15556. }
  15557. }
  15558. range.deleteContents()
  15559. range.select(true)
  15560. me.__hasEnterExecCommand = true
  15561. var html = htmlContent
  15562. if (plainType === 2) {
  15563. html = getPureHtml(html)
  15564. } else if (plainType) {
  15565. html = txtContent
  15566. }
  15567. me.execCommand('inserthtml', html, true)
  15568. me.__hasEnterExecCommand = false
  15569. var rng = me.selection.getRange()
  15570. while (
  15571. !domUtils.isBody(rng.startContainer) &&
  15572. !rng.startOffset &&
  15573. rng.startContainer[
  15574. rng.startContainer.nodeType == 3 ? 'nodeValue' : 'childNodes'
  15575. ].length
  15576. ) {
  15577. rng.setStartBefore(rng.startContainer)
  15578. }
  15579. var tmpAddress = rng.createAddress(true)
  15580. address.endAddress = tmpAddress.startAddress
  15581. }
  15582. })
  15583. me.addListener('ready', function () {
  15584. domUtils.on(me.body, 'cut', function () {
  15585. var range = me.selection.getRange()
  15586. if (!range.collapsed && me.undoManger) {
  15587. me.undoManger.save()
  15588. }
  15589. })
  15590. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  15591. domUtils.on(
  15592. me.body,
  15593. browser.ie || browser.opera ? 'keydown' : 'paste',
  15594. function (e) {
  15595. if (
  15596. (browser.ie || browser.opera) &&
  15597. ((!e.ctrlKey && !e.metaKey) || e.keyCode != '86')
  15598. ) {
  15599. return
  15600. }
  15601. getClipboardData.call(me, function (div) {
  15602. filter(div)
  15603. })
  15604. }
  15605. )
  15606. })
  15607. me.commands['paste'] = {
  15608. execCommand: function (cmd) {
  15609. if (browser.ie) {
  15610. getClipboardData.call(me, function (div) {
  15611. filter(div)
  15612. })
  15613. me.document.execCommand('paste')
  15614. } else {
  15615. alert(me.getLang('pastemsg'))
  15616. }
  15617. },
  15618. }
  15619. }
  15620. // plugins/puretxtpaste.js
  15621. /**
  15622. * 纯文本粘贴插件
  15623. * @file
  15624. * @since 1.2.6.1
  15625. */
  15626. UE.plugins['pasteplain'] = function () {
  15627. var me = this
  15628. me.setOpt({
  15629. pasteplain: false,
  15630. filterTxtRules: (function () {
  15631. function transP(node) {
  15632. node.tagName = 'p'
  15633. node.setStyle()
  15634. }
  15635. function removeNode(node) {
  15636. node.parentNode.removeChild(node, true)
  15637. }
  15638. return {
  15639. //直接删除及其字节点内容
  15640. '-': 'script style object iframe embed input select',
  15641. p: { $: {} },
  15642. br: { $: {} },
  15643. div: function (node) {
  15644. var tmpNode,
  15645. p = UE.uNode.createElement('p')
  15646. while ((tmpNode = node.firstChild())) {
  15647. if (
  15648. tmpNode.type == 'text' ||
  15649. !UE.dom.dtd.$block[tmpNode.tagName]
  15650. ) {
  15651. p.appendChild(tmpNode)
  15652. } else {
  15653. if (p.firstChild()) {
  15654. node.parentNode.insertBefore(p, node)
  15655. p = UE.uNode.createElement('p')
  15656. } else {
  15657. node.parentNode.insertBefore(tmpNode, node)
  15658. }
  15659. }
  15660. }
  15661. if (p.firstChild()) {
  15662. node.parentNode.insertBefore(p, node)
  15663. }
  15664. node.parentNode.removeChild(node)
  15665. },
  15666. ol: removeNode,
  15667. ul: removeNode,
  15668. dl: removeNode,
  15669. dt: removeNode,
  15670. dd: removeNode,
  15671. li: removeNode,
  15672. caption: transP,
  15673. th: transP,
  15674. tr: transP,
  15675. h1: transP,
  15676. h2: transP,
  15677. h3: transP,
  15678. h4: transP,
  15679. h5: transP,
  15680. h6: transP,
  15681. td: function (node) {
  15682. //没有内容的td直接删掉
  15683. var txt = !!node.innerText()
  15684. if (txt) {
  15685. node.parentNode.insertAfter(
  15686. UE.uNode.createText(' &nbsp; &nbsp;'),
  15687. node
  15688. )
  15689. }
  15690. node.parentNode.removeChild(node, node.innerText())
  15691. },
  15692. }
  15693. })(),
  15694. })
  15695. //暂时这里支持一下老版本的属性
  15696. var pasteplain = me.options.pasteplain
  15697. /**
  15698. * 启用或取消纯文本粘贴模式
  15699. * @command pasteplain
  15700. * @method execCommand
  15701. * @param { String } cmd 命令字符串
  15702. * @example
  15703. * ```javascript
  15704. * editor.queryCommandState( 'pasteplain' );
  15705. * ```
  15706. */
  15707. /**
  15708. * 查询当前是否处于纯文本粘贴模式
  15709. * @command pasteplain
  15710. * @method queryCommandState
  15711. * @param { String } cmd 命令字符串
  15712. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  15713. * @example
  15714. * ```javascript
  15715. * editor.queryCommandState( 'pasteplain' );
  15716. * ```
  15717. */
  15718. me.commands['pasteplain'] = {
  15719. queryCommandState: function () {
  15720. return pasteplain ? 1 : 0
  15721. },
  15722. execCommand: function () {
  15723. pasteplain = !pasteplain | 0
  15724. },
  15725. notNeedUndo: 1,
  15726. }
  15727. }
  15728. // plugins/list.js
  15729. /**
  15730. * 有序列表,无序列表插件
  15731. * @file
  15732. * @since 1.2.6.1
  15733. */
  15734. UE.plugins['list'] = function () {
  15735. var me = this,
  15736. notExchange = {
  15737. TD: 1,
  15738. PRE: 1,
  15739. BLOCKQUOTE: 1,
  15740. }
  15741. var customStyle = {
  15742. cn: 'cn-1-',
  15743. cn1: 'cn-2-',
  15744. cn2: 'cn-3-',
  15745. num: 'num-1-',
  15746. num1: 'num-2-',
  15747. num2: 'num-3-',
  15748. dash: 'dash',
  15749. dot: 'dot',
  15750. }
  15751. me.setOpt({
  15752. autoTransWordToList: false,
  15753. insertorderedlist: {
  15754. num: '',
  15755. num1: '',
  15756. num2: '',
  15757. cn: '',
  15758. cn1: '',
  15759. cn2: '',
  15760. decimal: '',
  15761. 'lower-alpha': '',
  15762. 'lower-roman': '',
  15763. 'upper-alpha': '',
  15764. 'upper-roman': '',
  15765. },
  15766. insertunorderedlist: {
  15767. circle: '',
  15768. disc: '',
  15769. square: '',
  15770. dash: '',
  15771. dot: '',
  15772. },
  15773. listDefaultPaddingLeft: '30',
  15774. listiconpath: 'http://bs.baidu.com/listicon/',
  15775. maxListLevel: -1, //-1不限制
  15776. disablePInList: false,
  15777. })
  15778. function listToArray(list) {
  15779. var arr = []
  15780. for (var p in list) {
  15781. arr.push(p)
  15782. }
  15783. return arr
  15784. }
  15785. var listStyle = {
  15786. OL: listToArray(me.options.insertorderedlist),
  15787. UL: listToArray(me.options.insertunorderedlist),
  15788. }
  15789. var liiconpath = me.options.listiconpath
  15790. //根据用户配置,调整customStyle
  15791. for (var s in customStyle) {
  15792. if (
  15793. !me.options.insertorderedlist.hasOwnProperty(s) &&
  15794. !me.options.insertunorderedlist.hasOwnProperty(s)
  15795. ) {
  15796. delete customStyle[s]
  15797. }
  15798. }
  15799. me.ready(function () {
  15800. var customCss = []
  15801. for (var p in customStyle) {
  15802. if (p == 'dash' || p == 'dot') {
  15803. customCss.push(
  15804. 'li.list-' +
  15805. customStyle[p] +
  15806. '{background-image:url(' +
  15807. liiconpath +
  15808. customStyle[p] +
  15809. '.gif)}'
  15810. )
  15811. customCss.push(
  15812. 'ul.custom_' +
  15813. p +
  15814. '{list-style:none;}ul.custom_' +
  15815. p +
  15816. ' li{background-position:0 3px;background-repeat:no-repeat}'
  15817. )
  15818. } else {
  15819. for (var i = 0; i < 99; i++) {
  15820. customCss.push(
  15821. 'li.list-' +
  15822. customStyle[p] +
  15823. i +
  15824. '{background-image:url(' +
  15825. liiconpath +
  15826. 'list-' +
  15827. customStyle[p] +
  15828. i +
  15829. '.gif)}'
  15830. )
  15831. }
  15832. customCss.push(
  15833. 'ol.custom_' +
  15834. p +
  15835. '{list-style:none;}ol.custom_' +
  15836. p +
  15837. ' li{background-position:0 3px;background-repeat:no-repeat}'
  15838. )
  15839. }
  15840. switch (p) {
  15841. case 'cn':
  15842. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:25px}')
  15843. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}')
  15844. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:55px}')
  15845. break
  15846. case 'cn1':
  15847. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:30px}')
  15848. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}')
  15849. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:55px}')
  15850. break
  15851. case 'cn2':
  15852. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:40px}')
  15853. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:55px}')
  15854. customCss.push('li.list-' + p + '-paddingleft-3{padding-left:68px}')
  15855. break
  15856. case 'num':
  15857. case 'num1':
  15858. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:25px}')
  15859. break
  15860. case 'num2':
  15861. customCss.push('li.list-' + p + '-paddingleft-1{padding-left:35px}')
  15862. customCss.push('li.list-' + p + '-paddingleft-2{padding-left:40px}')
  15863. break
  15864. case 'dash':
  15865. customCss.push('li.list-' + p + '-paddingleft{padding-left:35px}')
  15866. break
  15867. case 'dot':
  15868. customCss.push('li.list-' + p + '-paddingleft{padding-left:20px}')
  15869. }
  15870. }
  15871. customCss.push('.list-paddingleft-1{padding-left:0}')
  15872. customCss.push(
  15873. '.list-paddingleft-2{padding-left:' +
  15874. me.options.listDefaultPaddingLeft +
  15875. 'px}'
  15876. )
  15877. customCss.push(
  15878. '.list-paddingleft-3{padding-left:' +
  15879. me.options.listDefaultPaddingLeft * 2 +
  15880. 'px}'
  15881. )
  15882. //如果不给宽度会在自定应样式里出现滚动条
  15883. utils.cssRule(
  15884. 'list',
  15885. 'ol,ul{margin:0;pading:0;' +
  15886. (browser.ie ? '' : 'width:95%') +
  15887. '}li{clear:both;}' +
  15888. customCss.join('\n'),
  15889. me.document
  15890. )
  15891. })
  15892. //单独处理剪切的问题
  15893. me.ready(function () {
  15894. domUtils.on(me.body, 'cut', function () {
  15895. setTimeout(function () {
  15896. var rng = me.selection.getRange(),
  15897. li
  15898. //trace:3416
  15899. if (!rng.collapsed) {
  15900. if (
  15901. (li = domUtils.findParentByTagName(
  15902. rng.startContainer,
  15903. 'li',
  15904. true
  15905. ))
  15906. ) {
  15907. if (!li.nextSibling && domUtils.isEmptyBlock(li)) {
  15908. var pn = li.parentNode,
  15909. node
  15910. if ((node = pn.previousSibling)) {
  15911. domUtils.remove(pn)
  15912. rng.setStartAtLast(node).collapse(true)
  15913. rng.select(true)
  15914. } else if ((node = pn.nextSibling)) {
  15915. domUtils.remove(pn)
  15916. rng.setStartAtFirst(node).collapse(true)
  15917. rng.select(true)
  15918. } else {
  15919. var tmpNode = me.document.createElement('p')
  15920. domUtils.fillNode(me.document, tmpNode)
  15921. pn.parentNode.insertBefore(tmpNode, pn)
  15922. domUtils.remove(pn)
  15923. rng.setStart(tmpNode, 0).collapse(true)
  15924. rng.select(true)
  15925. }
  15926. }
  15927. }
  15928. }
  15929. })
  15930. })
  15931. })
  15932. function getStyle(node) {
  15933. var cls = node.className
  15934. if (domUtils.hasClass(node, /custom_/)) {
  15935. return cls.match(/custom_(\w+)/)[1]
  15936. }
  15937. return domUtils.getStyle(node, 'list-style-type')
  15938. }
  15939. me.addListener('beforepaste', function (type, html) {
  15940. var me = this,
  15941. rng = me.selection.getRange(),
  15942. li
  15943. var root = UE.htmlparser(html.html, true)
  15944. if ((li = domUtils.findParentByTagName(rng.startContainer, 'li', true))) {
  15945. var list = li.parentNode,
  15946. tagName = list.tagName == 'OL' ? 'ul' : 'ol'
  15947. utils.each(root.getNodesByTagName(tagName), function (n) {
  15948. n.tagName = list.tagName
  15949. n.setAttr()
  15950. if (n.parentNode === root) {
  15951. type = getStyle(list) || (list.tagName == 'OL' ? 'decimal' : 'disc')
  15952. } else {
  15953. var className = n.parentNode.getAttr('class')
  15954. if (className && /custom_/.test(className)) {
  15955. type = className.match(/custom_(\w+)/)[1]
  15956. } else {
  15957. type = n.parentNode.getStyle('list-style-type')
  15958. }
  15959. if (!type) {
  15960. type = list.tagName == 'OL' ? 'decimal' : 'disc'
  15961. }
  15962. }
  15963. var index = utils.indexOf(listStyle[list.tagName], type)
  15964. if (n.parentNode !== root)
  15965. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1
  15966. var currentStyle = listStyle[list.tagName][index]
  15967. if (customStyle[currentStyle]) {
  15968. n.setAttr('class', 'custom_' + currentStyle)
  15969. } else {
  15970. n.setStyle('list-style-type', currentStyle)
  15971. }
  15972. })
  15973. }
  15974. html.html = root.toHtml()
  15975. })
  15976. //导出时,去掉p标签
  15977. me.getOpt('disablePInList') === true &&
  15978. me.addOutputRule(function (root) {
  15979. utils.each(root.getNodesByTagName('li'), function (li) {
  15980. var newChildrens = [],
  15981. index = 0
  15982. utils.each(li.children, function (n) {
  15983. if (n.tagName == 'p') {
  15984. var tmpNode
  15985. while ((tmpNode = n.children.pop())) {
  15986. newChildrens.splice(index, 0, tmpNode)
  15987. tmpNode.parentNode = li
  15988. lastNode = tmpNode
  15989. }
  15990. tmpNode = newChildrens[newChildrens.length - 1]
  15991. if (
  15992. !tmpNode ||
  15993. tmpNode.type != 'element' ||
  15994. tmpNode.tagName != 'br'
  15995. ) {
  15996. var br = UE.uNode.createElement('br')
  15997. br.parentNode = li
  15998. newChildrens.push(br)
  15999. }
  16000. index = newChildrens.length
  16001. }
  16002. })
  16003. if (newChildrens.length) {
  16004. li.children = newChildrens
  16005. }
  16006. })
  16007. })
  16008. //进入编辑器的li要套p标签
  16009. me.addInputRule(function (root) {
  16010. utils.each(root.getNodesByTagName('li'), function (li) {
  16011. var tmpP = UE.uNode.createElement('p')
  16012. for (var i = 0, ci; (ci = li.children[i]); ) {
  16013. if (ci.type == 'text' || dtd.p[ci.tagName]) {
  16014. tmpP.appendChild(ci)
  16015. } else {
  16016. if (tmpP.firstChild()) {
  16017. li.insertBefore(tmpP, ci)
  16018. tmpP = UE.uNode.createElement('p')
  16019. i = i + 2
  16020. } else {
  16021. i++
  16022. }
  16023. }
  16024. }
  16025. if ((tmpP.firstChild() && !tmpP.parentNode) || !li.firstChild()) {
  16026. li.appendChild(tmpP)
  16027. }
  16028. //trace:3357
  16029. //p不能为空
  16030. if (!tmpP.firstChild()) {
  16031. tmpP.innerHTML(browser.ie ? '&nbsp;' : '<br/>')
  16032. }
  16033. //去掉末尾的空白
  16034. var p = li.firstChild()
  16035. var lastChild = p.lastChild()
  16036. if (
  16037. lastChild &&
  16038. lastChild.type == 'text' &&
  16039. /^\s*$/.test(lastChild.data)
  16040. ) {
  16041. p.removeChild(lastChild)
  16042. }
  16043. })
  16044. if (me.options.autoTransWordToList) {
  16045. var orderlisttype = {
  16046. num1: /^\d+\)/,
  16047. decimal: /^\d+\./,
  16048. 'lower-alpha': /^[a-z]+\)/,
  16049. 'upper-alpha': /^[A-Z]+\./,
  16050. cn: /^[\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+[\u3001]/,
  16051. cn2: /^\([\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+\)/,
  16052. },
  16053. unorderlisttype = {
  16054. square: 'n',
  16055. }
  16056. function checkListType(content, container) {
  16057. var span = container.firstChild()
  16058. if (
  16059. span &&
  16060. span.type == 'element' &&
  16061. span.tagName == 'span' &&
  16062. /Wingdings|Symbol/.test(span.getStyle('font-family'))
  16063. ) {
  16064. for (var p in unorderlisttype) {
  16065. if (unorderlisttype[p] == span.data) {
  16066. return p
  16067. }
  16068. }
  16069. return 'disc'
  16070. }
  16071. for (var p in orderlisttype) {
  16072. if (orderlisttype[p].test(content)) {
  16073. return p
  16074. }
  16075. }
  16076. }
  16077. utils.each(root.getNodesByTagName('p'), function (node) {
  16078. if (node.getAttr('class') != 'MsoListParagraph') {
  16079. return
  16080. }
  16081. //word粘贴过来的会带有margin要去掉,但这样也可能会误命中一些央视
  16082. node.setStyle('margin', '')
  16083. node.setStyle('margin-left', '')
  16084. node.setAttr('class', '')
  16085. function appendLi(list, p, type) {
  16086. if (list.tagName == 'ol') {
  16087. if (browser.ie) {
  16088. var first = p.firstChild()
  16089. if (
  16090. first.type == 'element' &&
  16091. first.tagName == 'span' &&
  16092. orderlisttype[type].test(first.innerText())
  16093. ) {
  16094. p.removeChild(first)
  16095. }
  16096. } else {
  16097. p.innerHTML(p.innerHTML().replace(orderlisttype[type], ''))
  16098. }
  16099. } else {
  16100. p.removeChild(p.firstChild())
  16101. }
  16102. var li = UE.uNode.createElement('li')
  16103. li.appendChild(p)
  16104. list.appendChild(li)
  16105. }
  16106. var tmp = node,
  16107. type,
  16108. cacheNode = node
  16109. if (
  16110. node.parentNode.tagName != 'li' &&
  16111. (type = checkListType(node.innerText(), node))
  16112. ) {
  16113. var list = UE.uNode.createElement(
  16114. me.options.insertorderedlist.hasOwnProperty(type) ? 'ol' : 'ul'
  16115. )
  16116. if (customStyle[type]) {
  16117. list.setAttr('class', 'custom_' + type)
  16118. } else {
  16119. list.setStyle('list-style-type', type)
  16120. }
  16121. while (
  16122. node &&
  16123. node.parentNode.tagName != 'li' &&
  16124. checkListType(node.innerText(), node)
  16125. ) {
  16126. tmp = node.nextSibling()
  16127. if (!tmp) {
  16128. node.parentNode.insertBefore(list, node)
  16129. }
  16130. appendLi(list, node, type)
  16131. node = tmp
  16132. }
  16133. if (!list.parentNode && node && node.parentNode) {
  16134. node.parentNode.insertBefore(list, node)
  16135. }
  16136. }
  16137. var span = cacheNode.firstChild()
  16138. if (
  16139. span &&
  16140. span.type == 'element' &&
  16141. span.tagName == 'span' &&
  16142. /^\s*(&nbsp;)+\s*$/.test(span.innerText())
  16143. ) {
  16144. span.parentNode.removeChild(span)
  16145. }
  16146. })
  16147. }
  16148. })
  16149. //调整索引标签
  16150. me.addListener('contentchange', function () {
  16151. adjustListStyle(me.document)
  16152. })
  16153. function adjustListStyle(doc, ignore) {
  16154. utils.each(domUtils.getElementsByTagName(doc, 'ol ul'), function (node) {
  16155. if (!domUtils.inDoc(node, doc)) return
  16156. var parent = node.parentNode
  16157. if (parent.tagName == node.tagName) {
  16158. var nodeStyleType =
  16159. getStyle(node) || (node.tagName == 'OL' ? 'decimal' : 'disc'),
  16160. parentStyleType =
  16161. getStyle(parent) || (parent.tagName == 'OL' ? 'decimal' : 'disc')
  16162. if (nodeStyleType == parentStyleType) {
  16163. var styleIndex = utils.indexOf(
  16164. listStyle[node.tagName],
  16165. nodeStyleType
  16166. )
  16167. styleIndex =
  16168. styleIndex + 1 == listStyle[node.tagName].length
  16169. ? 0
  16170. : styleIndex + 1
  16171. setListStyle(node, listStyle[node.tagName][styleIndex])
  16172. }
  16173. }
  16174. var index = 0,
  16175. type = 2
  16176. if (domUtils.hasClass(node, /custom_/)) {
  16177. if (
  16178. !(
  16179. /[ou]l/i.test(parent.tagName) &&
  16180. domUtils.hasClass(parent, /custom_/)
  16181. )
  16182. ) {
  16183. type = 1
  16184. }
  16185. } else {
  16186. if (
  16187. /[ou]l/i.test(parent.tagName) &&
  16188. domUtils.hasClass(parent, /custom_/)
  16189. ) {
  16190. type = 3
  16191. }
  16192. }
  16193. var style = domUtils.getStyle(node, 'list-style-type')
  16194. style && (node.style.cssText = 'list-style-type:' + style)
  16195. node.className =
  16196. utils.trim(node.className.replace(/list-paddingleft-\w+/, '')) +
  16197. ' list-paddingleft-' +
  16198. type
  16199. utils.each(domUtils.getElementsByTagName(node, 'li'), function (li) {
  16200. li.style.cssText && (li.style.cssText = '')
  16201. if (!li.firstChild) {
  16202. domUtils.remove(li)
  16203. return
  16204. }
  16205. if (li.parentNode !== node) {
  16206. return
  16207. }
  16208. index++
  16209. if (domUtils.hasClass(node, /custom_/)) {
  16210. var paddingLeft = 1,
  16211. currentStyle = getStyle(node)
  16212. if (node.tagName == 'OL') {
  16213. if (currentStyle) {
  16214. switch (currentStyle) {
  16215. case 'cn':
  16216. case 'cn1':
  16217. case 'cn2':
  16218. if (
  16219. index > 10 &&
  16220. (index % 10 == 0 || (index > 10 && index < 20))
  16221. ) {
  16222. paddingLeft = 2
  16223. } else if (index > 20) {
  16224. paddingLeft = 3
  16225. }
  16226. break
  16227. case 'num2':
  16228. if (index > 9) {
  16229. paddingLeft = 2
  16230. }
  16231. }
  16232. }
  16233. li.className =
  16234. 'list-' +
  16235. customStyle[currentStyle] +
  16236. index +
  16237. ' ' +
  16238. 'list-' +
  16239. currentStyle +
  16240. '-paddingleft-' +
  16241. paddingLeft
  16242. } else {
  16243. li.className =
  16244. 'list-' +
  16245. customStyle[currentStyle] +
  16246. ' ' +
  16247. 'list-' +
  16248. currentStyle +
  16249. '-paddingleft'
  16250. }
  16251. } else {
  16252. li.className = li.className.replace(/list-[\w\-]+/gi, '')
  16253. }
  16254. var className = li.getAttribute('class')
  16255. if (className !== null && !className.replace(/\s/g, '')) {
  16256. domUtils.removeAttributes(li, 'class')
  16257. }
  16258. })
  16259. !ignore &&
  16260. adjustList(
  16261. node,
  16262. node.tagName.toLowerCase(),
  16263. getStyle(node) || domUtils.getStyle(node, 'list-style-type'),
  16264. true
  16265. )
  16266. })
  16267. }
  16268. function adjustList(list, tag, style, ignoreEmpty) {
  16269. var nextList = list.nextSibling
  16270. if (
  16271. nextList &&
  16272. nextList.nodeType == 1 &&
  16273. nextList.tagName.toLowerCase() == tag &&
  16274. (getStyle(nextList) ||
  16275. domUtils.getStyle(nextList, 'list-style-type') ||
  16276. (tag == 'ol' ? 'decimal' : 'disc')) == style
  16277. ) {
  16278. domUtils.moveChild(nextList, list)
  16279. if (nextList.childNodes.length == 0) {
  16280. domUtils.remove(nextList)
  16281. }
  16282. }
  16283. if (nextList && domUtils.isFillChar(nextList)) {
  16284. domUtils.remove(nextList)
  16285. }
  16286. var preList = list.previousSibling
  16287. if (
  16288. preList &&
  16289. preList.nodeType == 1 &&
  16290. preList.tagName.toLowerCase() == tag &&
  16291. (getStyle(preList) ||
  16292. domUtils.getStyle(preList, 'list-style-type') ||
  16293. (tag == 'ol' ? 'decimal' : 'disc')) == style
  16294. ) {
  16295. domUtils.moveChild(list, preList)
  16296. }
  16297. if (preList && domUtils.isFillChar(preList)) {
  16298. domUtils.remove(preList)
  16299. }
  16300. !ignoreEmpty && domUtils.isEmptyBlock(list) && domUtils.remove(list)
  16301. if (getStyle(list)) {
  16302. adjustListStyle(list.ownerDocument, true)
  16303. }
  16304. }
  16305. function setListStyle(list, style) {
  16306. if (customStyle[style]) {
  16307. list.className = 'custom_' + style
  16308. }
  16309. try {
  16310. domUtils.setStyle(list, 'list-style-type', style)
  16311. } catch (e) {}
  16312. }
  16313. function clearEmptySibling(node) {
  16314. var tmpNode = node.previousSibling
  16315. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16316. domUtils.remove(tmpNode)
  16317. }
  16318. tmpNode = node.nextSibling
  16319. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16320. domUtils.remove(tmpNode)
  16321. }
  16322. }
  16323. me.addListener('keydown', function (type, evt) {
  16324. function preventAndSave() {
  16325. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false)
  16326. me.fireEvent('contentchange')
  16327. me.undoManger && me.undoManger.save()
  16328. }
  16329. function findList(node, filterFn) {
  16330. while (node && !domUtils.isBody(node)) {
  16331. if (filterFn(node)) {
  16332. return null
  16333. }
  16334. if (node.nodeType == 1 && /[ou]l/i.test(node.tagName)) {
  16335. return node
  16336. }
  16337. node = node.parentNode
  16338. }
  16339. return null
  16340. }
  16341. var keyCode = evt.keyCode || evt.which
  16342. if (keyCode == 13 && !evt.shiftKey) {
  16343. //回车
  16344. var rng = me.selection.getRange(),
  16345. parent = domUtils.findParent(
  16346. rng.startContainer,
  16347. function (node) {
  16348. return domUtils.isBlockElm(node)
  16349. },
  16350. true
  16351. ),
  16352. li = domUtils.findParentByTagName(rng.startContainer, 'li', true)
  16353. if (parent && parent.tagName != 'PRE' && !li) {
  16354. var html = parent.innerHTML.replace(
  16355. new RegExp(domUtils.fillChar, 'g'),
  16356. ''
  16357. )
  16358. if (/^\s*1\s*\.[^\d]/.test(html)) {
  16359. parent.innerHTML = html.replace(/^\s*1\s*\./, '')
  16360. rng.setStartAtLast(parent).collapse(true).select()
  16361. me.__hasEnterExecCommand = true
  16362. me.execCommand('insertorderedlist')
  16363. me.__hasEnterExecCommand = false
  16364. }
  16365. }
  16366. var range = me.selection.getRange(),
  16367. start = findList(range.startContainer, function (node) {
  16368. return node.tagName == 'TABLE'
  16369. }),
  16370. end = range.collapsed
  16371. ? start
  16372. : findList(range.endContainer, function (node) {
  16373. return node.tagName == 'TABLE'
  16374. })
  16375. if (start && end && start === end) {
  16376. if (!range.collapsed) {
  16377. start = domUtils.findParentByTagName(
  16378. range.startContainer,
  16379. 'li',
  16380. true
  16381. )
  16382. end = domUtils.findParentByTagName(range.endContainer, 'li', true)
  16383. if (start && end && start === end) {
  16384. range.deleteContents()
  16385. li = domUtils.findParentByTagName(
  16386. range.startContainer,
  16387. 'li',
  16388. true
  16389. )
  16390. if (li && domUtils.isEmptyBlock(li)) {
  16391. pre = li.previousSibling
  16392. next = li.nextSibling
  16393. p = me.document.createElement('p')
  16394. domUtils.fillNode(me.document, p)
  16395. parentList = li.parentNode
  16396. if (pre && next) {
  16397. range.setStart(next, 0).collapse(true).select(true)
  16398. domUtils.remove(li)
  16399. } else {
  16400. if ((!pre && !next) || !pre) {
  16401. parentList.parentNode.insertBefore(p, parentList)
  16402. } else {
  16403. li.parentNode.parentNode.insertBefore(
  16404. p,
  16405. parentList.nextSibling
  16406. )
  16407. }
  16408. domUtils.remove(li)
  16409. if (!parentList.firstChild) {
  16410. domUtils.remove(parentList)
  16411. }
  16412. range.setStart(p, 0).setCursor()
  16413. }
  16414. preventAndSave()
  16415. return
  16416. }
  16417. } else {
  16418. var tmpRange = range.cloneRange(),
  16419. bk = tmpRange.collapse(false).createBookmark()
  16420. range.deleteContents()
  16421. tmpRange.moveToBookmark(bk)
  16422. var li = domUtils.findParentByTagName(
  16423. tmpRange.startContainer,
  16424. 'li',
  16425. true
  16426. )
  16427. clearEmptySibling(li)
  16428. tmpRange.select()
  16429. preventAndSave()
  16430. return
  16431. }
  16432. }
  16433. li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  16434. if (li) {
  16435. if (domUtils.isEmptyBlock(li)) {
  16436. bk = range.createBookmark()
  16437. var parentList = li.parentNode
  16438. if (li !== parentList.lastChild) {
  16439. domUtils.breakParent(li, parentList)
  16440. clearEmptySibling(li)
  16441. } else {
  16442. parentList.parentNode.insertBefore(li, parentList.nextSibling)
  16443. if (domUtils.isEmptyNode(parentList)) {
  16444. domUtils.remove(parentList)
  16445. }
  16446. }
  16447. //嵌套不处理
  16448. if (!dtd.$list[li.parentNode.tagName]) {
  16449. if (!domUtils.isBlockElm(li.firstChild)) {
  16450. p = me.document.createElement('p')
  16451. li.parentNode.insertBefore(p, li)
  16452. while (li.firstChild) {
  16453. p.appendChild(li.firstChild)
  16454. }
  16455. domUtils.remove(li)
  16456. } else {
  16457. domUtils.remove(li, true)
  16458. }
  16459. }
  16460. range.moveToBookmark(bk).select()
  16461. } else {
  16462. var first = li.firstChild
  16463. if (!first || !domUtils.isBlockElm(first)) {
  16464. var p = me.document.createElement('p')
  16465. !li.firstChild && domUtils.fillNode(me.document, p)
  16466. while (li.firstChild) {
  16467. p.appendChild(li.firstChild)
  16468. }
  16469. li.appendChild(p)
  16470. first = p
  16471. }
  16472. var span = me.document.createElement('span')
  16473. range.insertNode(span)
  16474. domUtils.breakParent(span, li)
  16475. var nextLi = span.nextSibling
  16476. first = nextLi.firstChild
  16477. if (!first) {
  16478. p = me.document.createElement('p')
  16479. domUtils.fillNode(me.document, p)
  16480. nextLi.appendChild(p)
  16481. first = p
  16482. }
  16483. if (domUtils.isEmptyNode(first)) {
  16484. first.innerHTML = ''
  16485. domUtils.fillNode(me.document, first)
  16486. }
  16487. range.setStart(first, 0).collapse(true).shrinkBoundary().select()
  16488. domUtils.remove(span)
  16489. var pre = nextLi.previousSibling
  16490. if (pre && domUtils.isEmptyBlock(pre)) {
  16491. pre.innerHTML = '<p></p>'
  16492. domUtils.fillNode(me.document, pre.firstChild)
  16493. }
  16494. }
  16495. // }
  16496. preventAndSave()
  16497. }
  16498. }
  16499. }
  16500. if (keyCode == 8) {
  16501. //修中ie中li下的问题
  16502. range = me.selection.getRange()
  16503. if (range.collapsed && domUtils.isStartInblock(range)) {
  16504. tmpRange = range.cloneRange().trimBoundary()
  16505. li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  16506. //要在li的最左边,才能处理
  16507. if (li && domUtils.isStartInblock(tmpRange)) {
  16508. start = domUtils.findParentByTagName(
  16509. range.startContainer,
  16510. 'p',
  16511. true
  16512. )
  16513. if (start && start !== li.firstChild) {
  16514. var parentList = domUtils.findParentByTagName(start, ['ol', 'ul'])
  16515. domUtils.breakParent(start, parentList)
  16516. clearEmptySibling(start)
  16517. me.fireEvent('contentchange')
  16518. range.setStart(start, 0).setCursor(false, true)
  16519. me.fireEvent('saveScene')
  16520. domUtils.preventDefault(evt)
  16521. return
  16522. }
  16523. if (li && (pre = li.previousSibling)) {
  16524. if (keyCode == 46 && li.childNodes.length) {
  16525. return
  16526. }
  16527. //有可能上边的兄弟节点是个2级菜单,要追加到2级菜单的最后的li
  16528. if (dtd.$list[pre.tagName]) {
  16529. pre = pre.lastChild
  16530. }
  16531. me.undoManger && me.undoManger.save()
  16532. first = li.firstChild
  16533. if (domUtils.isBlockElm(first)) {
  16534. if (domUtils.isEmptyNode(first)) {
  16535. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16536. pre.appendChild(first)
  16537. range.setStart(first, 0).setCursor(false, true)
  16538. //first不是唯一的节点
  16539. while (li.firstChild) {
  16540. pre.appendChild(li.firstChild)
  16541. }
  16542. } else {
  16543. span = me.document.createElement('span')
  16544. range.insertNode(span)
  16545. //判断pre是否是空的节点,如果是<p><br/></p>类型的空节点,干掉p标签防止它占位
  16546. if (domUtils.isEmptyBlock(pre)) {
  16547. pre.innerHTML = ''
  16548. }
  16549. domUtils.moveChild(li, pre)
  16550. range.setStartBefore(span).collapse(true).select(true)
  16551. domUtils.remove(span)
  16552. }
  16553. } else {
  16554. if (domUtils.isEmptyNode(li)) {
  16555. var p = me.document.createElement('p')
  16556. pre.appendChild(p)
  16557. range.setStart(p, 0).setCursor()
  16558. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16559. } else {
  16560. range
  16561. .setEnd(pre, pre.childNodes.length)
  16562. .collapse()
  16563. .select(true)
  16564. while (li.firstChild) {
  16565. pre.appendChild(li.firstChild)
  16566. }
  16567. }
  16568. }
  16569. domUtils.remove(li)
  16570. me.fireEvent('contentchange')
  16571. me.fireEvent('saveScene')
  16572. domUtils.preventDefault(evt)
  16573. return
  16574. }
  16575. //trace:980
  16576. if (li && !li.previousSibling) {
  16577. var parentList = li.parentNode
  16578. var bk = range.createBookmark()
  16579. if (domUtils.isTagNode(parentList.parentNode, 'ol ul')) {
  16580. parentList.parentNode.insertBefore(li, parentList)
  16581. if (domUtils.isEmptyNode(parentList)) {
  16582. domUtils.remove(parentList)
  16583. }
  16584. } else {
  16585. while (li.firstChild) {
  16586. parentList.parentNode.insertBefore(li.firstChild, parentList)
  16587. }
  16588. domUtils.remove(li)
  16589. if (domUtils.isEmptyNode(parentList)) {
  16590. domUtils.remove(parentList)
  16591. }
  16592. }
  16593. range.moveToBookmark(bk).setCursor(false, true)
  16594. me.fireEvent('contentchange')
  16595. me.fireEvent('saveScene')
  16596. domUtils.preventDefault(evt)
  16597. return
  16598. }
  16599. }
  16600. }
  16601. }
  16602. })
  16603. me.addListener('keyup', function (type, evt) {
  16604. var keyCode = evt.keyCode || evt.which
  16605. if (keyCode == 8) {
  16606. var rng = me.selection.getRange(),
  16607. list
  16608. if (
  16609. (list = domUtils.findParentByTagName(
  16610. rng.startContainer,
  16611. ['ol', 'ul'],
  16612. true
  16613. ))
  16614. ) {
  16615. adjustList(
  16616. list,
  16617. list.tagName.toLowerCase(),
  16618. getStyle(list) ||
  16619. domUtils.getComputedStyle(list, 'list-style-type'),
  16620. true
  16621. )
  16622. }
  16623. }
  16624. })
  16625. //处理tab键
  16626. me.addListener('tabkeydown', function () {
  16627. var range = me.selection.getRange()
  16628. //控制级数
  16629. function checkLevel(li) {
  16630. if (me.options.maxListLevel != -1) {
  16631. var level = li.parentNode,
  16632. levelNum = 0
  16633. while (/[ou]l/i.test(level.tagName)) {
  16634. levelNum++
  16635. level = level.parentNode
  16636. }
  16637. if (levelNum >= me.options.maxListLevel) {
  16638. return true
  16639. }
  16640. }
  16641. }
  16642. //只以开始为准
  16643. //todo 后续改进
  16644. var li = domUtils.findParentByTagName(range.startContainer, 'li', true)
  16645. if (li) {
  16646. var bk
  16647. if (range.collapsed) {
  16648. if (checkLevel(li)) return true
  16649. var parentLi = li.parentNode,
  16650. list = me.document.createElement(parentLi.tagName),
  16651. index = utils.indexOf(
  16652. listStyle[list.tagName],
  16653. getStyle(parentLi) ||
  16654. domUtils.getComputedStyle(parentLi, 'list-style-type')
  16655. )
  16656. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1
  16657. var currentStyle = listStyle[list.tagName][index]
  16658. setListStyle(list, currentStyle)
  16659. if (domUtils.isStartInblock(range)) {
  16660. me.fireEvent('saveScene')
  16661. bk = range.createBookmark()
  16662. parentLi.insertBefore(list, li)
  16663. list.appendChild(li)
  16664. adjustList(list, list.tagName.toLowerCase(), currentStyle)
  16665. me.fireEvent('contentchange')
  16666. range.moveToBookmark(bk).select(true)
  16667. return true
  16668. }
  16669. } else {
  16670. me.fireEvent('saveScene')
  16671. bk = range.createBookmark()
  16672. for (
  16673. var i = 0, closeList, parents = domUtils.findParents(li), ci;
  16674. (ci = parents[i++]);
  16675. ) {
  16676. if (domUtils.isTagNode(ci, 'ol ul')) {
  16677. closeList = ci
  16678. break
  16679. }
  16680. }
  16681. var current = li
  16682. if (bk.end) {
  16683. while (
  16684. current &&
  16685. !(
  16686. domUtils.getPosition(current, bk.end) &
  16687. domUtils.POSITION_FOLLOWING
  16688. )
  16689. ) {
  16690. if (checkLevel(current)) {
  16691. current = domUtils.getNextDomNode(
  16692. current,
  16693. false,
  16694. null,
  16695. function (node) {
  16696. return node !== closeList
  16697. }
  16698. )
  16699. continue
  16700. }
  16701. var parentLi = current.parentNode,
  16702. list = me.document.createElement(parentLi.tagName),
  16703. index = utils.indexOf(
  16704. listStyle[list.tagName],
  16705. getStyle(parentLi) ||
  16706. domUtils.getComputedStyle(parentLi, 'list-style-type')
  16707. )
  16708. var currentIndex =
  16709. index + 1 == listStyle[list.tagName].length ? 0 : index + 1
  16710. var currentStyle = listStyle[list.tagName][currentIndex]
  16711. setListStyle(list, currentStyle)
  16712. parentLi.insertBefore(list, current)
  16713. while (
  16714. current &&
  16715. !(
  16716. domUtils.getPosition(current, bk.end) &
  16717. domUtils.POSITION_FOLLOWING
  16718. )
  16719. ) {
  16720. li = current.nextSibling
  16721. list.appendChild(current)
  16722. if (!li || domUtils.isTagNode(li, 'ol ul')) {
  16723. if (li) {
  16724. while ((li = li.firstChild)) {
  16725. if (li.tagName == 'LI') {
  16726. break
  16727. }
  16728. }
  16729. } else {
  16730. li = domUtils.getNextDomNode(
  16731. current,
  16732. false,
  16733. null,
  16734. function (node) {
  16735. return node !== closeList
  16736. }
  16737. )
  16738. }
  16739. break
  16740. }
  16741. current = li
  16742. }
  16743. adjustList(list, list.tagName.toLowerCase(), currentStyle)
  16744. current = li
  16745. }
  16746. }
  16747. me.fireEvent('contentchange')
  16748. range.moveToBookmark(bk).select()
  16749. return true
  16750. }
  16751. }
  16752. })
  16753. function getLi(start) {
  16754. while (start && !domUtils.isBody(start)) {
  16755. if (start.nodeName == 'TABLE') {
  16756. return null
  16757. }
  16758. if (start.nodeName == 'LI') {
  16759. return start
  16760. }
  16761. start = start.parentNode
  16762. }
  16763. }
  16764. /**
  16765. * 有序列表,与“insertunorderedlist”命令互斥
  16766. * @command insertorderedlist
  16767. * @method execCommand
  16768. * @param { String } command 命令字符串
  16769. * @param { String } style 插入的有序列表类型,值为:decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16770. * @example
  16771. * ```javascript
  16772. * editor.execCommand( 'insertorderedlist','decimal');
  16773. * ```
  16774. */
  16775. /**
  16776. * 查询当前选区内容是否有序列表
  16777. * @command insertorderedlist
  16778. * @method queryCommandState
  16779. * @param { String } cmd 命令字符串
  16780. * @return { int } 如果当前选区是有序列表返回1,否则返回0
  16781. * @example
  16782. * ```javascript
  16783. * editor.queryCommandState( 'insertorderedlist' );
  16784. * ```
  16785. */
  16786. /**
  16787. * 查询当前选区内容是否有序列表
  16788. * @command insertorderedlist
  16789. * @method queryCommandValue
  16790. * @param { String } cmd 命令字符串
  16791. * @return { String } 返回当前有序列表的类型,值为null或decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16792. * @example
  16793. * ```javascript
  16794. * editor.queryCommandValue( 'insertorderedlist' );
  16795. * ```
  16796. */
  16797. /**
  16798. * 无序列表,与“insertorderedlist”命令互斥
  16799. * @command insertunorderedlist
  16800. * @method execCommand
  16801. * @param { String } command 命令字符串
  16802. * @param { String } style 插入的无序列表类型,值为:circle,disc,square,dash,dot
  16803. * @example
  16804. * ```javascript
  16805. * editor.execCommand( 'insertunorderedlist','circle');
  16806. * ```
  16807. */
  16808. /**
  16809. * 查询当前是否有word文档粘贴进来的图片
  16810. * @command insertunorderedlist
  16811. * @method insertunorderedlist
  16812. * @param { String } command 命令字符串
  16813. * @return { int } 如果当前选区是无序列表返回1,否则返回0
  16814. * @example
  16815. * ```javascript
  16816. * editor.queryCommandState( 'insertunorderedlist' );
  16817. * ```
  16818. */
  16819. /**
  16820. * 查询当前选区内容是否有序列表
  16821. * @command insertunorderedlist
  16822. * @method queryCommandValue
  16823. * @param { String } command 命令字符串
  16824. * @return { String } 返回当前无序列表的类型,值为null或circle,disc,square,dash,dot
  16825. * @example
  16826. * ```javascript
  16827. * editor.queryCommandValue( 'insertunorderedlist' );
  16828. * ```
  16829. */
  16830. me.commands['insertorderedlist'] = me.commands['insertunorderedlist'] = {
  16831. execCommand: function (command, style) {
  16832. if (!style) {
  16833. style =
  16834. command.toLowerCase() == 'insertorderedlist' ? 'decimal' : 'disc'
  16835. }
  16836. var me = this,
  16837. range = this.selection.getRange(),
  16838. filterFn = function (node) {
  16839. return node.nodeType == 1
  16840. ? node.tagName.toLowerCase() != 'br'
  16841. : !domUtils.isWhitespace(node)
  16842. },
  16843. tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul',
  16844. frag = me.document.createDocumentFragment()
  16845. //去掉是因为会出现选到末尾,导致adjustmentBoundary缩到ol/ul的位置
  16846. //range.shrinkBoundary();//.adjustmentBoundary();
  16847. range.adjustmentBoundary().shrinkBoundary()
  16848. var bko = range.createBookmark(true),
  16849. start = getLi(me.document.getElementById(bko.start)),
  16850. modifyStart = 0,
  16851. end = getLi(me.document.getElementById(bko.end)),
  16852. modifyEnd = 0,
  16853. startParent,
  16854. endParent,
  16855. list,
  16856. tmp
  16857. if (start || end) {
  16858. start && (startParent = start.parentNode)
  16859. if (!bko.end) {
  16860. end = start
  16861. }
  16862. end && (endParent = end.parentNode)
  16863. if (startParent === endParent) {
  16864. while (start !== end) {
  16865. tmp = start
  16866. start = start.nextSibling
  16867. if (!domUtils.isBlockElm(tmp.firstChild)) {
  16868. var p = me.document.createElement('p')
  16869. while (tmp.firstChild) {
  16870. p.appendChild(tmp.firstChild)
  16871. }
  16872. tmp.appendChild(p)
  16873. }
  16874. frag.appendChild(tmp)
  16875. }
  16876. tmp = me.document.createElement('span')
  16877. startParent.insertBefore(tmp, end)
  16878. if (!domUtils.isBlockElm(end.firstChild)) {
  16879. p = me.document.createElement('p')
  16880. while (end.firstChild) {
  16881. p.appendChild(end.firstChild)
  16882. }
  16883. end.appendChild(p)
  16884. }
  16885. frag.appendChild(end)
  16886. domUtils.breakParent(tmp, startParent)
  16887. if (domUtils.isEmptyNode(tmp.previousSibling)) {
  16888. domUtils.remove(tmp.previousSibling)
  16889. }
  16890. if (domUtils.isEmptyNode(tmp.nextSibling)) {
  16891. domUtils.remove(tmp.nextSibling)
  16892. }
  16893. var nodeStyle =
  16894. getStyle(startParent) ||
  16895. domUtils.getComputedStyle(startParent, 'list-style-type') ||
  16896. (command.toLowerCase() == 'insertorderedlist'
  16897. ? 'decimal'
  16898. : 'disc')
  16899. if (
  16900. startParent.tagName.toLowerCase() == tag &&
  16901. nodeStyle == style
  16902. ) {
  16903. for (
  16904. var i = 0, ci, tmpFrag = me.document.createDocumentFragment();
  16905. (ci = frag.firstChild);
  16906. ) {
  16907. if (domUtils.isTagNode(ci, 'ol ul')) {
  16908. // 删除时,子列表不处理
  16909. // utils.each(domUtils.getElementsByTagName(ci,'li'),function(li){
  16910. // while(li.firstChild){
  16911. // tmpFrag.appendChild(li.firstChild);
  16912. // }
  16913. //
  16914. // });
  16915. tmpFrag.appendChild(ci)
  16916. } else {
  16917. while (ci.firstChild) {
  16918. tmpFrag.appendChild(ci.firstChild)
  16919. domUtils.remove(ci)
  16920. }
  16921. }
  16922. }
  16923. tmp.parentNode.insertBefore(tmpFrag, tmp)
  16924. } else {
  16925. list = me.document.createElement(tag)
  16926. setListStyle(list, style)
  16927. list.appendChild(frag)
  16928. tmp.parentNode.insertBefore(list, tmp)
  16929. }
  16930. domUtils.remove(tmp)
  16931. list && adjustList(list, tag, style)
  16932. range.moveToBookmark(bko).select()
  16933. return
  16934. }
  16935. //开始
  16936. if (start) {
  16937. while (start) {
  16938. tmp = start.nextSibling
  16939. if (domUtils.isTagNode(start, 'ol ul')) {
  16940. frag.appendChild(start)
  16941. } else {
  16942. var tmpfrag = me.document.createDocumentFragment(),
  16943. hasBlock = 0
  16944. while (start.firstChild) {
  16945. if (domUtils.isBlockElm(start.firstChild)) {
  16946. hasBlock = 1
  16947. }
  16948. tmpfrag.appendChild(start.firstChild)
  16949. }
  16950. if (!hasBlock) {
  16951. var tmpP = me.document.createElement('p')
  16952. tmpP.appendChild(tmpfrag)
  16953. frag.appendChild(tmpP)
  16954. } else {
  16955. frag.appendChild(tmpfrag)
  16956. }
  16957. domUtils.remove(start)
  16958. }
  16959. start = tmp
  16960. }
  16961. startParent.parentNode.insertBefore(frag, startParent.nextSibling)
  16962. if (domUtils.isEmptyNode(startParent)) {
  16963. range.setStartBefore(startParent)
  16964. domUtils.remove(startParent)
  16965. } else {
  16966. range.setStartAfter(startParent)
  16967. }
  16968. modifyStart = 1
  16969. }
  16970. if (end && domUtils.inDoc(endParent, me.document)) {
  16971. //结束
  16972. start = endParent.firstChild
  16973. while (start && start !== end) {
  16974. tmp = start.nextSibling
  16975. if (domUtils.isTagNode(start, 'ol ul')) {
  16976. frag.appendChild(start)
  16977. } else {
  16978. tmpfrag = me.document.createDocumentFragment()
  16979. hasBlock = 0
  16980. while (start.firstChild) {
  16981. if (domUtils.isBlockElm(start.firstChild)) {
  16982. hasBlock = 1
  16983. }
  16984. tmpfrag.appendChild(start.firstChild)
  16985. }
  16986. if (!hasBlock) {
  16987. tmpP = me.document.createElement('p')
  16988. tmpP.appendChild(tmpfrag)
  16989. frag.appendChild(tmpP)
  16990. } else {
  16991. frag.appendChild(tmpfrag)
  16992. }
  16993. domUtils.remove(start)
  16994. }
  16995. start = tmp
  16996. }
  16997. var tmpDiv = domUtils.createElement(me.document, 'div', {
  16998. tmpDiv: 1,
  16999. })
  17000. domUtils.moveChild(end, tmpDiv)
  17001. frag.appendChild(tmpDiv)
  17002. domUtils.remove(end)
  17003. endParent.parentNode.insertBefore(frag, endParent)
  17004. range.setEndBefore(endParent)
  17005. if (domUtils.isEmptyNode(endParent)) {
  17006. domUtils.remove(endParent)
  17007. }
  17008. modifyEnd = 1
  17009. }
  17010. }
  17011. if (!modifyStart) {
  17012. range.setStartBefore(me.document.getElementById(bko.start))
  17013. }
  17014. if (bko.end && !modifyEnd) {
  17015. range.setEndAfter(me.document.getElementById(bko.end))
  17016. }
  17017. range.enlarge(true, function (node) {
  17018. return notExchange[node.tagName]
  17019. })
  17020. frag = me.document.createDocumentFragment()
  17021. var bk = range.createBookmark(),
  17022. current = domUtils.getNextDomNode(bk.start, false, filterFn),
  17023. tmpRange = range.cloneRange(),
  17024. tmpNode,
  17025. block = domUtils.isBlockElm
  17026. while (
  17027. current &&
  17028. current !== bk.end &&
  17029. domUtils.getPosition(current, bk.end) & domUtils.POSITION_PRECEDING
  17030. ) {
  17031. if (current.nodeType == 3 || dtd.li[current.tagName]) {
  17032. if (current.nodeType == 1 && dtd.$list[current.tagName]) {
  17033. while (current.firstChild) {
  17034. frag.appendChild(current.firstChild)
  17035. }
  17036. tmpNode = domUtils.getNextDomNode(current, false, filterFn)
  17037. domUtils.remove(current)
  17038. current = tmpNode
  17039. continue
  17040. }
  17041. tmpNode = current
  17042. tmpRange.setStartBefore(current)
  17043. while (
  17044. current &&
  17045. current !== bk.end &&
  17046. (!block(current) || domUtils.isBookmarkNode(current))
  17047. ) {
  17048. tmpNode = current
  17049. current = domUtils.getNextDomNode(
  17050. current,
  17051. false,
  17052. null,
  17053. function (node) {
  17054. return !notExchange[node.tagName]
  17055. }
  17056. )
  17057. }
  17058. if (current && block(current)) {
  17059. tmp = domUtils.getNextDomNode(tmpNode, false, filterFn)
  17060. if (tmp && domUtils.isBookmarkNode(tmp)) {
  17061. current = domUtils.getNextDomNode(tmp, false, filterFn)
  17062. tmpNode = tmp
  17063. }
  17064. }
  17065. tmpRange.setEndAfter(tmpNode)
  17066. current = domUtils.getNextDomNode(tmpNode, false, filterFn)
  17067. var li = range.document.createElement('li')
  17068. li.appendChild(tmpRange.extractContents())
  17069. if (domUtils.isEmptyNode(li)) {
  17070. var tmpNode = range.document.createElement('p')
  17071. while (li.firstChild) {
  17072. tmpNode.appendChild(li.firstChild)
  17073. }
  17074. li.appendChild(tmpNode)
  17075. }
  17076. frag.appendChild(li)
  17077. } else {
  17078. current = domUtils.getNextDomNode(current, true, filterFn)
  17079. }
  17080. }
  17081. range.moveToBookmark(bk).collapse(true)
  17082. list = me.document.createElement(tag)
  17083. setListStyle(list, style)
  17084. list.appendChild(frag)
  17085. range.insertNode(list)
  17086. //当前list上下看能否合并
  17087. adjustList(list, tag, style)
  17088. //去掉冗余的tmpDiv
  17089. for (
  17090. var i = 0, ci, tmpDivs = domUtils.getElementsByTagName(list, 'div');
  17091. (ci = tmpDivs[i++]);
  17092. ) {
  17093. if (ci.getAttribute('tmpDiv')) {
  17094. domUtils.remove(ci, true)
  17095. }
  17096. }
  17097. range.moveToBookmark(bko).select()
  17098. },
  17099. queryCommandState: function (command) {
  17100. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul'
  17101. var path = this.selection.getStartElementPath()
  17102. for (var i = 0, ci; (ci = path[i++]); ) {
  17103. if (ci.nodeName == 'TABLE') {
  17104. return 0
  17105. }
  17106. if (tag == ci.nodeName.toLowerCase()) {
  17107. return 1
  17108. }
  17109. }
  17110. return 0
  17111. },
  17112. queryCommandValue: function (command) {
  17113. var tag = command.toLowerCase() == 'insertorderedlist' ? 'ol' : 'ul'
  17114. var path = this.selection.getStartElementPath(),
  17115. node
  17116. for (var i = 0, ci; (ci = path[i++]); ) {
  17117. if (ci.nodeName == 'TABLE') {
  17118. node = null
  17119. break
  17120. }
  17121. if (tag == ci.nodeName.toLowerCase()) {
  17122. node = ci
  17123. break
  17124. }
  17125. }
  17126. return node
  17127. ? getStyle(node) || domUtils.getComputedStyle(node, 'list-style-type')
  17128. : null
  17129. },
  17130. }
  17131. }
  17132. // plugins/source.js
  17133. /**
  17134. * 源码编辑插件
  17135. * @file
  17136. * @since 1.2.6.1
  17137. */
  17138. ;(function () {
  17139. var sourceEditors = {
  17140. textarea: function (editor, holder) {
  17141. var textarea = holder.ownerDocument.createElement('textarea')
  17142. textarea.style.cssText =
  17143. 'position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;'
  17144. // todo: IE下只有onresize属性可用... 很纠结
  17145. if (browser.ie && browser.version < 8) {
  17146. textarea.style.width = holder.offsetWidth + 'px'
  17147. textarea.style.height = holder.offsetHeight + 'px'
  17148. holder.onresize = function () {
  17149. textarea.style.width = holder.offsetWidth + 'px'
  17150. textarea.style.height = holder.offsetHeight + 'px'
  17151. }
  17152. }
  17153. holder.appendChild(textarea)
  17154. return {
  17155. setContent: function (content) {
  17156. textarea.value = content
  17157. },
  17158. getContent: function () {
  17159. return textarea.value
  17160. },
  17161. select: function () {
  17162. var range
  17163. if (browser.ie) {
  17164. range = textarea.createTextRange()
  17165. range.collapse(true)
  17166. range.select()
  17167. } else {
  17168. //todo: chrome下无法设置焦点
  17169. textarea.setSelectionRange(0, 0)
  17170. textarea.focus()
  17171. }
  17172. },
  17173. dispose: function () {
  17174. holder.removeChild(textarea)
  17175. // todo
  17176. holder.onresize = null
  17177. textarea = null
  17178. holder = null
  17179. },
  17180. }
  17181. },
  17182. codemirror: function (editor, holder) {
  17183. var codeEditor = window.CodeMirror(holder, {
  17184. mode: 'text/html',
  17185. tabMode: 'indent',
  17186. lineNumbers: true,
  17187. lineWrapping: true,
  17188. })
  17189. var dom = codeEditor.getWrapperElement()
  17190. dom.style.cssText =
  17191. 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;'
  17192. codeEditor.getScrollerElement().style.cssText =
  17193. 'position:absolute;left:0;top:0;width:100%;height:100%;'
  17194. codeEditor.refresh()
  17195. return {
  17196. getCodeMirror: function () {
  17197. return codeEditor
  17198. },
  17199. setContent: function (content) {
  17200. codeEditor.setValue(content)
  17201. },
  17202. getContent: function () {
  17203. return codeEditor.getValue()
  17204. },
  17205. select: function () {
  17206. codeEditor.focus()
  17207. },
  17208. dispose: function () {
  17209. holder.removeChild(dom)
  17210. dom = null
  17211. codeEditor = null
  17212. },
  17213. }
  17214. },
  17215. }
  17216. UE.plugins['source'] = function () {
  17217. var me = this
  17218. var opt = this.options
  17219. var sourceMode = false
  17220. var sourceEditor
  17221. var orgSetContent
  17222. opt.sourceEditor = browser.ie
  17223. ? 'textarea'
  17224. : opt.sourceEditor || 'codemirror'
  17225. me.setOpt({
  17226. sourceEditorFirst: false,
  17227. })
  17228. function createSourceEditor(holder) {
  17229. return sourceEditors[
  17230. opt.sourceEditor == 'codemirror' && window.CodeMirror
  17231. ? 'codemirror'
  17232. : 'textarea'
  17233. ](me, holder)
  17234. }
  17235. var bakCssText
  17236. //解决在源码模式下getContent不能得到最新的内容问题
  17237. var oldGetContent, bakAddress
  17238. /**
  17239. * 切换源码模式和编辑模式
  17240. * @command source
  17241. * @method execCommand
  17242. * @param { String } cmd 命令字符串
  17243. * @example
  17244. * ```javascript
  17245. * editor.execCommand( 'source');
  17246. * ```
  17247. */
  17248. /**
  17249. * 查询当前编辑区域的状态是源码模式还是可视化模式
  17250. * @command source
  17251. * @method queryCommandState
  17252. * @param { String } cmd 命令字符串
  17253. * @return { int } 如果当前是源码编辑模式,返回1,否则返回0
  17254. * @example
  17255. * ```javascript
  17256. * editor.queryCommandState( 'source' );
  17257. * ```
  17258. */
  17259. me.commands['source'] = {
  17260. execCommand: function () {
  17261. sourceMode = !sourceMode
  17262. if (sourceMode) {
  17263. bakAddress = me.selection.getRange().createAddress(false, true)
  17264. me.undoManger && me.undoManger.save(true)
  17265. if (browser.gecko) {
  17266. me.body.contentEditable = false
  17267. }
  17268. bakCssText = me.iframe.style.cssText
  17269. me.iframe.style.cssText +=
  17270. 'position:absolute;left:-32768px;top:-32768px;'
  17271. me.fireEvent('beforegetcontent')
  17272. var root = UE.htmlparser(me.body.innerHTML)
  17273. me.filterOutputRule(root)
  17274. root.traversal(function (node) {
  17275. if (node.type == 'element') {
  17276. switch (node.tagName) {
  17277. case 'td':
  17278. case 'th':
  17279. case 'caption':
  17280. if (node.children && node.children.length == 1) {
  17281. if (node.firstChild().tagName == 'br') {
  17282. node.removeChild(node.firstChild())
  17283. }
  17284. }
  17285. break
  17286. case 'pre':
  17287. node.innerText(node.innerText().replace(/&nbsp;/g, ' '))
  17288. }
  17289. }
  17290. })
  17291. me.fireEvent('aftergetcontent')
  17292. var content = root.toHtml(true)
  17293. sourceEditor = createSourceEditor(me.iframe.parentNode)
  17294. sourceEditor.setContent(content)
  17295. orgSetContent = me.setContent
  17296. me.setContent = function (html) {
  17297. //这里暂时不触发事件,防止报错
  17298. var root = UE.htmlparser(html)
  17299. me.filterInputRule(root)
  17300. html = root.toHtml()
  17301. sourceEditor.setContent(html)
  17302. }
  17303. setTimeout(function () {
  17304. sourceEditor.select()
  17305. me.addListener('fullscreenchanged', function () {
  17306. try {
  17307. sourceEditor.getCodeMirror().refresh()
  17308. } catch (e) {}
  17309. })
  17310. })
  17311. //重置getContent,源码模式下取值也能是最新的数据
  17312. oldGetContent = me.getContent
  17313. me.getContent = function () {
  17314. return (
  17315. sourceEditor.getContent() ||
  17316. '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  17317. )
  17318. }
  17319. } else {
  17320. me.iframe.style.cssText = bakCssText
  17321. var cont =
  17322. sourceEditor.getContent() ||
  17323. '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  17324. //处理掉block节点前后的空格,有可能会误命中,暂时不考虑
  17325. cont = cont.replace(
  17326. new RegExp('[\\r\\t\\n ]*</?(\\w+)\\s*(?:[^>]*)>', 'g'),
  17327. function (a, b) {
  17328. if (b && !dtd.$inlineWithA[b.toLowerCase()]) {
  17329. return a.replace(/(^[\n\r\t ]*)|([\n\r\t ]*$)/g, '')
  17330. }
  17331. return a.replace(/(^[\n\r\t]*)|([\n\r\t]*$)/g, '')
  17332. }
  17333. )
  17334. me.setContent = orgSetContent
  17335. me.setContent(cont)
  17336. sourceEditor.dispose()
  17337. sourceEditor = null
  17338. //还原getContent方法
  17339. me.getContent = oldGetContent
  17340. var first = me.body.firstChild
  17341. //trace:1106 都删除空了,下边会报错,所以补充一个p占位
  17342. if (!first) {
  17343. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  17344. first = me.body.firstChild
  17345. }
  17346. //要在ifm为显示时ff才能取到selection,否则报错
  17347. //这里不能比较位置了
  17348. me.undoManger && me.undoManger.save(true)
  17349. if (browser.gecko) {
  17350. var input = document.createElement('input')
  17351. input.style.cssText = 'position:absolute;left:0;top:-32768px'
  17352. document.body.appendChild(input)
  17353. me.body.contentEditable = false
  17354. setTimeout(function () {
  17355. domUtils.setViewportOffset(input, { left: -32768, top: 0 })
  17356. input.focus()
  17357. setTimeout(function () {
  17358. me.body.contentEditable = true
  17359. me.selection.getRange().moveToAddress(bakAddress).select(true)
  17360. domUtils.remove(input)
  17361. })
  17362. })
  17363. } else {
  17364. //ie下有可能报错,比如在代码顶头的情况
  17365. try {
  17366. me.selection.getRange().moveToAddress(bakAddress).select(true)
  17367. } catch (e) {}
  17368. }
  17369. }
  17370. this.fireEvent('sourcemodechanged', sourceMode)
  17371. },
  17372. queryCommandState: function () {
  17373. return sourceMode | 0
  17374. },
  17375. notNeedUndo: 1,
  17376. }
  17377. var oldQueryCommandState = me.queryCommandState
  17378. me.queryCommandState = function (cmdName) {
  17379. cmdName = cmdName.toLowerCase()
  17380. if (sourceMode) {
  17381. //源码模式下可以开启的命令
  17382. return cmdName in
  17383. {
  17384. source: 1,
  17385. fullscreen: 1,
  17386. }
  17387. ? 1
  17388. : -1
  17389. }
  17390. return oldQueryCommandState.apply(this, arguments)
  17391. }
  17392. if (opt.sourceEditor == 'codemirror') {
  17393. me.addListener('ready', function () {
  17394. utils.loadFile(
  17395. document,
  17396. {
  17397. src:
  17398. opt.codeMirrorJsUrl ||
  17399. opt.UEDITOR_HOME_URL + 'third-party/codemirror/codemirror.js',
  17400. tag: 'script',
  17401. type: 'text/javascript',
  17402. defer: 'defer',
  17403. },
  17404. function () {
  17405. if (opt.sourceEditorFirst) {
  17406. setTimeout(function () {
  17407. me.execCommand('source')
  17408. }, 0)
  17409. }
  17410. }
  17411. )
  17412. utils.loadFile(document, {
  17413. tag: 'link',
  17414. rel: 'stylesheet',
  17415. type: 'text/css',
  17416. href:
  17417. opt.codeMirrorCssUrl ||
  17418. opt.UEDITOR_HOME_URL + 'third-party/codemirror/codemirror.css',
  17419. })
  17420. })
  17421. }
  17422. }
  17423. })()
  17424. // plugins/enterkey.js
  17425. ///import core
  17426. ///import plugins/undo.js
  17427. ///commands 设置回车标签p或br
  17428. ///commandsName EnterKey
  17429. ///commandsTitle 设置回车标签p或br
  17430. /**
  17431. * @description 处理回车
  17432. * @author zhanyi
  17433. */
  17434. UE.plugins['enterkey'] = function () {
  17435. var hTag,
  17436. me = this,
  17437. tag = me.options.enterTag
  17438. me.addListener('keyup', function (type, evt) {
  17439. var keyCode = evt.keyCode || evt.which
  17440. if (keyCode == 13) {
  17441. var range = me.selection.getRange(),
  17442. start = range.startContainer,
  17443. doSave
  17444. //修正在h1-h6里边回车后不能嵌套p的问题
  17445. if (!browser.ie) {
  17446. if (/h\d/i.test(hTag)) {
  17447. if (browser.gecko) {
  17448. var h = domUtils.findParentByTagName(
  17449. start,
  17450. [
  17451. 'h1',
  17452. 'h2',
  17453. 'h3',
  17454. 'h4',
  17455. 'h5',
  17456. 'h6',
  17457. 'blockquote',
  17458. 'caption',
  17459. 'table',
  17460. ],
  17461. true
  17462. )
  17463. if (!h) {
  17464. me.document.execCommand('formatBlock', false, '<p>')
  17465. doSave = 1
  17466. }
  17467. } else {
  17468. //chrome remove div
  17469. if (start.nodeType == 1) {
  17470. var tmp = me.document.createTextNode(''),
  17471. div
  17472. range.insertNode(tmp)
  17473. div = domUtils.findParentByTagName(tmp, 'div', true)
  17474. if (div) {
  17475. var p = me.document.createElement('p')
  17476. while (div.firstChild) {
  17477. p.appendChild(div.firstChild)
  17478. }
  17479. div.parentNode.insertBefore(p, div)
  17480. domUtils.remove(div)
  17481. range.setStartBefore(tmp).setCursor()
  17482. doSave = 1
  17483. }
  17484. domUtils.remove(tmp)
  17485. }
  17486. }
  17487. if (me.undoManger && doSave) {
  17488. me.undoManger.save()
  17489. }
  17490. }
  17491. //没有站位符,会出现多行的问题
  17492. browser.opera && range.select()
  17493. } else {
  17494. me.fireEvent('saveScene', true, true)
  17495. }
  17496. }
  17497. })
  17498. me.addListener('keydown', function (type, evt) {
  17499. var keyCode = evt.keyCode || evt.which
  17500. if (keyCode == 13) {
  17501. //回车
  17502. if (me.fireEvent('beforeenterkeydown')) {
  17503. domUtils.preventDefault(evt)
  17504. return
  17505. }
  17506. me.fireEvent('saveScene', true, true)
  17507. hTag = ''
  17508. var range = me.selection.getRange()
  17509. if (!range.collapsed) {
  17510. //跨td不能删
  17511. var start = range.startContainer,
  17512. end = range.endContainer,
  17513. startTd = domUtils.findParentByTagName(start, 'td', true),
  17514. endTd = domUtils.findParentByTagName(end, 'td', true)
  17515. if (
  17516. (startTd && endTd && startTd !== endTd) ||
  17517. (!startTd && endTd) ||
  17518. (startTd && !endTd)
  17519. ) {
  17520. evt.preventDefault
  17521. ? evt.preventDefault()
  17522. : (evt.returnValue = false)
  17523. return
  17524. }
  17525. }
  17526. if (tag == 'p') {
  17527. if (!browser.ie) {
  17528. start = domUtils.findParentByTagName(
  17529. range.startContainer,
  17530. [
  17531. 'ol',
  17532. 'ul',
  17533. 'p',
  17534. 'h1',
  17535. 'h2',
  17536. 'h3',
  17537. 'h4',
  17538. 'h5',
  17539. 'h6',
  17540. 'blockquote',
  17541. 'caption',
  17542. ],
  17543. true
  17544. )
  17545. //opera下执行formatblock会在table的场景下有问题,回车在opera原生支持很好,所以暂时在opera去掉调用这个原生的command
  17546. //trace:2431
  17547. if (!start && !browser.opera) {
  17548. me.document.execCommand('formatBlock', false, '<p>')
  17549. if (browser.gecko) {
  17550. range = me.selection.getRange()
  17551. start = domUtils.findParentByTagName(
  17552. range.startContainer,
  17553. 'p',
  17554. true
  17555. )
  17556. start && domUtils.removeDirtyAttr(start)
  17557. }
  17558. } else {
  17559. hTag = start.tagName
  17560. start.tagName.toLowerCase() == 'p' &&
  17561. browser.gecko &&
  17562. domUtils.removeDirtyAttr(start)
  17563. }
  17564. }
  17565. } else {
  17566. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false)
  17567. if (!range.collapsed) {
  17568. range.deleteContents()
  17569. start = range.startContainer
  17570. if (
  17571. start.nodeType == 1 &&
  17572. (start = start.childNodes[range.startOffset])
  17573. ) {
  17574. while (start.nodeType == 1) {
  17575. if (dtd.$empty[start.tagName]) {
  17576. range.setStartBefore(start).setCursor()
  17577. if (me.undoManger) {
  17578. me.undoManger.save()
  17579. }
  17580. return false
  17581. }
  17582. if (!start.firstChild) {
  17583. var br = range.document.createElement('br')
  17584. start.appendChild(br)
  17585. range.setStart(start, 0).setCursor()
  17586. if (me.undoManger) {
  17587. me.undoManger.save()
  17588. }
  17589. return false
  17590. }
  17591. start = start.firstChild
  17592. }
  17593. if (
  17594. start === range.startContainer.childNodes[range.startOffset]
  17595. ) {
  17596. br = range.document.createElement('br')
  17597. range.insertNode(br).setCursor()
  17598. } else {
  17599. range.setStart(start, 0).setCursor()
  17600. }
  17601. } else {
  17602. br = range.document.createElement('br')
  17603. range.insertNode(br).setStartAfter(br).setCursor()
  17604. }
  17605. } else {
  17606. br = range.document.createElement('br')
  17607. range.insertNode(br)
  17608. var parent = br.parentNode
  17609. if (parent.lastChild === br) {
  17610. br.parentNode.insertBefore(br.cloneNode(true), br)
  17611. range.setStartBefore(br)
  17612. } else {
  17613. range.setStartAfter(br)
  17614. }
  17615. range.setCursor()
  17616. }
  17617. }
  17618. }
  17619. })
  17620. }
  17621. // plugins/keystrokes.js
  17622. /* 处理特殊键的兼容性问题 */
  17623. UE.plugins['keystrokes'] = function () {
  17624. var me = this
  17625. var collapsed = true
  17626. me.addListener('keydown', function (type, evt) {
  17627. var keyCode = evt.keyCode || evt.which,
  17628. rng = me.selection.getRange()
  17629. //处理全选的情况
  17630. if (
  17631. !rng.collapsed &&
  17632. !(evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey) &&
  17633. ((keyCode >= 65 && keyCode <= 90) ||
  17634. (keyCode >= 48 && keyCode <= 57) ||
  17635. (keyCode >= 96 && keyCode <= 111) ||
  17636. {
  17637. 13: 1,
  17638. 8: 1,
  17639. 46: 1,
  17640. }[keyCode])
  17641. ) {
  17642. var tmpNode = rng.startContainer
  17643. if (domUtils.isFillChar(tmpNode)) {
  17644. rng.setStartBefore(tmpNode)
  17645. }
  17646. tmpNode = rng.endContainer
  17647. if (domUtils.isFillChar(tmpNode)) {
  17648. rng.setEndAfter(tmpNode)
  17649. }
  17650. rng.txtToElmBoundary()
  17651. //结束边界可能放到了br的前边,要把br包含进来
  17652. // x[xxx]<br/>
  17653. if (rng.endContainer && rng.endContainer.nodeType == 1) {
  17654. tmpNode = rng.endContainer.childNodes[rng.endOffset]
  17655. if (tmpNode && domUtils.isBr(tmpNode)) {
  17656. rng.setEndAfter(tmpNode)
  17657. }
  17658. }
  17659. if (rng.startOffset == 0) {
  17660. tmpNode = rng.startContainer
  17661. if (domUtils.isBoundaryNode(tmpNode, 'firstChild')) {
  17662. tmpNode = rng.endContainer
  17663. if (
  17664. rng.endOffset ==
  17665. (tmpNode.nodeType == 3
  17666. ? tmpNode.nodeValue.length
  17667. : tmpNode.childNodes.length) &&
  17668. domUtils.isBoundaryNode(tmpNode, 'lastChild')
  17669. ) {
  17670. me.fireEvent('saveScene')
  17671. me.body.innerHTML = '<p>' + (browser.ie ? '' : '<br/>') + '</p>'
  17672. rng.setStart(me.body.firstChild, 0).setCursor(false, true)
  17673. me._selectionChange()
  17674. return
  17675. }
  17676. }
  17677. }
  17678. }
  17679. //处理backspace
  17680. if (keyCode == keymap.Backspace) {
  17681. rng = me.selection.getRange()
  17682. collapsed = rng.collapsed
  17683. if (me.fireEvent('delkeydown', evt)) {
  17684. return
  17685. }
  17686. var start, end
  17687. //避免按两次删除才能生效的问题
  17688. if (rng.collapsed && rng.inFillChar()) {
  17689. start = rng.startContainer
  17690. if (domUtils.isFillChar(start)) {
  17691. rng.setStartBefore(start).shrinkBoundary(true).collapse(true)
  17692. domUtils.remove(start)
  17693. } else {
  17694. start.nodeValue = start.nodeValue.replace(
  17695. new RegExp('^' + domUtils.fillChar),
  17696. ''
  17697. )
  17698. rng.startOffset--
  17699. rng.collapse(true).select(true)
  17700. }
  17701. }
  17702. //解决选中control元素不能删除的问题
  17703. if ((start = rng.getClosedNode())) {
  17704. me.fireEvent('saveScene')
  17705. rng.setStartBefore(start)
  17706. domUtils.remove(start)
  17707. rng.setCursor()
  17708. me.fireEvent('saveScene')
  17709. domUtils.preventDefault(evt)
  17710. return
  17711. }
  17712. //阻止在table上的删除
  17713. if (!browser.ie) {
  17714. start = domUtils.findParentByTagName(
  17715. rng.startContainer,
  17716. 'table',
  17717. true
  17718. )
  17719. end = domUtils.findParentByTagName(rng.endContainer, 'table', true)
  17720. if ((start && !end) || (!start && end) || start !== end) {
  17721. evt.preventDefault()
  17722. return
  17723. }
  17724. }
  17725. }
  17726. //处理tab键的逻辑
  17727. if (keyCode == keymap.Tab) {
  17728. //不处理以下标签
  17729. var excludeTagNameForTabKey = {
  17730. ol: 1,
  17731. ul: 1,
  17732. table: 1,
  17733. }
  17734. //处理组件里的tab按下事件
  17735. if (me.fireEvent('tabkeydown', evt)) {
  17736. domUtils.preventDefault(evt)
  17737. return
  17738. }
  17739. var range = me.selection.getRange()
  17740. me.fireEvent('saveScene')
  17741. for (
  17742. var i = 0,
  17743. txt = '',
  17744. tabSize = me.options.tabSize || 4,
  17745. tabNode = me.options.tabNode || '&nbsp;';
  17746. i < tabSize;
  17747. i++
  17748. ) {
  17749. txt += tabNode
  17750. }
  17751. var span = me.document.createElement('span')
  17752. span.innerHTML = txt + domUtils.fillChar
  17753. if (range.collapsed) {
  17754. range.insertNode(span.cloneNode(true).firstChild).setCursor(true)
  17755. } else {
  17756. var filterFn = function (node) {
  17757. return (
  17758. domUtils.isBlockElm(node) &&
  17759. !excludeTagNameForTabKey[node.tagName.toLowerCase()]
  17760. )
  17761. }
  17762. //普通的情况
  17763. start = domUtils.findParent(range.startContainer, filterFn, true)
  17764. end = domUtils.findParent(range.endContainer, filterFn, true)
  17765. if (start && end && start === end) {
  17766. range.deleteContents()
  17767. range.insertNode(span.cloneNode(true).firstChild).setCursor(true)
  17768. } else {
  17769. var bookmark = range.createBookmark()
  17770. range.enlarge(true)
  17771. var bookmark2 = range.createBookmark(),
  17772. current = domUtils.getNextDomNode(
  17773. bookmark2.start,
  17774. false,
  17775. filterFn
  17776. )
  17777. while (
  17778. current &&
  17779. !(
  17780. domUtils.getPosition(current, bookmark2.end) &
  17781. domUtils.POSITION_FOLLOWING
  17782. )
  17783. ) {
  17784. current.insertBefore(
  17785. span.cloneNode(true).firstChild,
  17786. current.firstChild
  17787. )
  17788. current = domUtils.getNextDomNode(current, false, filterFn)
  17789. }
  17790. range.moveToBookmark(bookmark2).moveToBookmark(bookmark).select()
  17791. }
  17792. }
  17793. domUtils.preventDefault(evt)
  17794. }
  17795. //trace:1634
  17796. //ff的del键在容器空的时候,也会删除
  17797. if (browser.gecko && keyCode == 46) {
  17798. range = me.selection.getRange()
  17799. if (range.collapsed) {
  17800. start = range.startContainer
  17801. if (domUtils.isEmptyBlock(start)) {
  17802. var parent = start.parentNode
  17803. while (
  17804. domUtils.getChildCount(parent) == 1 &&
  17805. !domUtils.isBody(parent)
  17806. ) {
  17807. start = parent
  17808. parent = parent.parentNode
  17809. }
  17810. if (start === parent.lastChild) evt.preventDefault()
  17811. return
  17812. }
  17813. }
  17814. }
  17815. })
  17816. me.addListener('keyup', function (type, evt) {
  17817. var keyCode = evt.keyCode || evt.which,
  17818. rng,
  17819. me = this
  17820. if (keyCode == keymap.Backspace) {
  17821. if (me.fireEvent('delkeyup')) {
  17822. return
  17823. }
  17824. rng = me.selection.getRange()
  17825. if (rng.collapsed) {
  17826. var tmpNode,
  17827. autoClearTagName = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
  17828. if (
  17829. (tmpNode = domUtils.findParentByTagName(
  17830. rng.startContainer,
  17831. autoClearTagName,
  17832. true
  17833. ))
  17834. ) {
  17835. if (domUtils.isEmptyBlock(tmpNode)) {
  17836. var pre = tmpNode.previousSibling
  17837. if (pre && pre.nodeName != 'TABLE') {
  17838. domUtils.remove(tmpNode)
  17839. rng.setStartAtLast(pre).setCursor(false, true)
  17840. return
  17841. } else {
  17842. var next = tmpNode.nextSibling
  17843. if (next && next.nodeName != 'TABLE') {
  17844. domUtils.remove(tmpNode)
  17845. rng.setStartAtFirst(next).setCursor(false, true)
  17846. return
  17847. }
  17848. }
  17849. }
  17850. }
  17851. //处理当删除到body时,要重新给p标签展位
  17852. if (domUtils.isBody(rng.startContainer)) {
  17853. var tmpNode = domUtils.createElement(me.document, 'p', {
  17854. innerHTML: browser.ie ? domUtils.fillChar : '<br/>',
  17855. })
  17856. rng.insertNode(tmpNode).setStart(tmpNode, 0).setCursor(false, true)
  17857. }
  17858. }
  17859. //chrome下如果删除了inline标签,浏览器会有记忆,在输入文字还是会套上刚才删除的标签,所以这里再选一次就不会了
  17860. if (
  17861. !collapsed &&
  17862. (rng.startContainer.nodeType == 3 ||
  17863. (rng.startContainer.nodeType == 1 &&
  17864. domUtils.isEmptyBlock(rng.startContainer)))
  17865. ) {
  17866. if (browser.ie) {
  17867. var span = rng.document.createElement('span')
  17868. rng.insertNode(span).setStartBefore(span).collapse(true)
  17869. rng.select()
  17870. domUtils.remove(span)
  17871. } else {
  17872. rng.select()
  17873. }
  17874. }
  17875. }
  17876. })
  17877. }
  17878. // plugins/fiximgclick.js
  17879. ///import core
  17880. ///commands 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17881. ///commandsName FixImgClick
  17882. ///commandsTitle 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17883. //修复chrome下图片不能点击的问题,出现八个角可改变大小
  17884. UE.plugins['fiximgclick'] = (function () {
  17885. var elementUpdated = false
  17886. function Scale() {
  17887. this.editor = null
  17888. this.resizer = null
  17889. this.cover = null
  17890. this.doc = document
  17891. this.prePos = { x: 0, y: 0 }
  17892. this.startPos = { x: 0, y: 0 }
  17893. }
  17894. ;(function () {
  17895. var rect = [
  17896. //[left, top, width, height]
  17897. [0, 0, -1, -1],
  17898. [0, 0, 0, -1],
  17899. [0, 0, 1, -1],
  17900. [0, 0, -1, 0],
  17901. [0, 0, 1, 0],
  17902. [0, 0, -1, 1],
  17903. [0, 0, 0, 1],
  17904. [0, 0, 1, 1],
  17905. ]
  17906. Scale.prototype = {
  17907. init: function (editor) {
  17908. var me = this
  17909. me.editor = editor
  17910. me.startPos = this.prePos = { x: 0, y: 0 }
  17911. me.dragId = -1
  17912. var hands = [],
  17913. cover = (me.cover = document.createElement('div')),
  17914. resizer = (me.resizer = document.createElement('div'))
  17915. cover.id = me.editor.ui.id + '_imagescale_cover'
  17916. cover.style.cssText =
  17917. 'position:absolute;display:none;z-index:' +
  17918. me.editor.options.zIndex +
  17919. ';filter:alpha(opacity=0); opacity:0;background:#CCC;'
  17920. domUtils.on(cover, 'mousedown click', function () {
  17921. me.hide()
  17922. })
  17923. for (i = 0; i < 8; i++) {
  17924. hands.push(
  17925. '<span class="edui-editor-imagescale-hand' + i + '"></span>'
  17926. )
  17927. }
  17928. resizer.id = me.editor.ui.id + '_imagescale'
  17929. resizer.className = 'edui-editor-imagescale'
  17930. resizer.innerHTML = hands.join('')
  17931. resizer.style.cssText +=
  17932. ';display:none;border:1px solid #3b77ff;z-index:' +
  17933. me.editor.options.zIndex +
  17934. ';'
  17935. me.editor.ui.getDom().appendChild(cover)
  17936. me.editor.ui.getDom().appendChild(resizer)
  17937. me.initStyle()
  17938. me.initEvents()
  17939. },
  17940. initStyle: function () {
  17941. utils.cssRule(
  17942. 'imagescale',
  17943. '.edui-editor-imagescale{display:none;position:absolute;border:1px solid #38B2CE;cursor:hand;-webkit-box-sizing: content-box;-moz-box-sizing: content-box;box-sizing: content-box;}' +
  17944. '.edui-editor-imagescale span{position:absolute;width:6px;height:6px;overflow:hidden;font-size:0px;display:block;background-color:#3C9DD0;}' +
  17945. '.edui-editor-imagescale .edui-editor-imagescale-hand0{cursor:nw-resize;top:0;margin-top:-4px;left:0;margin-left:-4px;}' +
  17946. '.edui-editor-imagescale .edui-editor-imagescale-hand1{cursor:n-resize;top:0;margin-top:-4px;left:50%;margin-left:-4px;}' +
  17947. '.edui-editor-imagescale .edui-editor-imagescale-hand2{cursor:ne-resize;top:0;margin-top:-4px;left:100%;margin-left:-3px;}' +
  17948. '.edui-editor-imagescale .edui-editor-imagescale-hand3{cursor:w-resize;top:50%;margin-top:-4px;left:0;margin-left:-4px;}' +
  17949. '.edui-editor-imagescale .edui-editor-imagescale-hand4{cursor:e-resize;top:50%;margin-top:-4px;left:100%;margin-left:-3px;}' +
  17950. '.edui-editor-imagescale .edui-editor-imagescale-hand5{cursor:sw-resize;top:100%;margin-top:-3px;left:0;margin-left:-4px;}' +
  17951. '.edui-editor-imagescale .edui-editor-imagescale-hand6{cursor:s-resize;top:100%;margin-top:-3px;left:50%;margin-left:-4px;}' +
  17952. '.edui-editor-imagescale .edui-editor-imagescale-hand7{cursor:se-resize;top:100%;margin-top:-3px;left:100%;margin-left:-3px;}'
  17953. )
  17954. },
  17955. initEvents: function () {
  17956. var me = this
  17957. me.startPos.x = me.startPos.y = 0
  17958. me.isDraging = false
  17959. },
  17960. _eventHandler: function (e) {
  17961. var me = this
  17962. switch (e.type) {
  17963. case 'mousedown':
  17964. var hand = e.target || e.srcElement,
  17965. hand
  17966. if (
  17967. hand.className.indexOf('edui-editor-imagescale-hand') != -1 &&
  17968. me.dragId == -1
  17969. ) {
  17970. me.dragId = hand.className.slice(-1)
  17971. me.startPos.x = me.prePos.x = e.clientX
  17972. me.startPos.y = me.prePos.y = e.clientY
  17973. domUtils.on(me.doc, 'mousemove', me.proxy(me._eventHandler, me))
  17974. }
  17975. break
  17976. case 'mousemove':
  17977. if (me.dragId != -1) {
  17978. me.updateContainerStyle(me.dragId, {
  17979. x: e.clientX - me.prePos.x,
  17980. y: e.clientY - me.prePos.y,
  17981. })
  17982. me.prePos.x = e.clientX
  17983. me.prePos.y = e.clientY
  17984. elementUpdated = true
  17985. me.updateTargetElement()
  17986. }
  17987. break
  17988. case 'mouseup':
  17989. if (me.dragId != -1) {
  17990. me.updateContainerStyle(me.dragId, {
  17991. x: e.clientX - me.prePos.x,
  17992. y: e.clientY - me.prePos.y,
  17993. })
  17994. me.updateTargetElement()
  17995. if (me.target.parentNode) me.attachTo(me.target)
  17996. me.dragId = -1
  17997. }
  17998. domUtils.un(me.doc, 'mousemove', me.proxy(me._eventHandler, me))
  17999. //修复只是点击挪动点,但没有改变大小,不应该触发contentchange
  18000. if (elementUpdated) {
  18001. elementUpdated = false
  18002. me.editor.fireEvent('contentchange')
  18003. }
  18004. break
  18005. default:
  18006. break
  18007. }
  18008. },
  18009. updateTargetElement: function () {
  18010. var me = this
  18011. domUtils.setStyles(me.target, {
  18012. width: me.resizer.style.width,
  18013. height: me.resizer.style.height,
  18014. })
  18015. me.target.width = parseInt(me.resizer.style.width)
  18016. me.target.height = parseInt(me.resizer.style.height)
  18017. me.attachTo(me.target)
  18018. },
  18019. updateContainerStyle: function (dir, offset) {
  18020. var me = this,
  18021. dom = me.resizer,
  18022. tmp
  18023. if (rect[dir][0] != 0) {
  18024. tmp = parseInt(dom.style.left) + offset.x
  18025. dom.style.left = me._validScaledProp('left', tmp) + 'px'
  18026. }
  18027. if (rect[dir][1] != 0) {
  18028. tmp = parseInt(dom.style.top) + offset.y
  18029. dom.style.top = me._validScaledProp('top', tmp) + 'px'
  18030. }
  18031. if (rect[dir][2] != 0) {
  18032. tmp = dom.clientWidth + rect[dir][2] * offset.x
  18033. dom.style.width = me._validScaledProp('width', tmp) + 'px'
  18034. }
  18035. if (rect[dir][3] != 0) {
  18036. tmp = dom.clientHeight + rect[dir][3] * offset.y
  18037. dom.style.height = me._validScaledProp('height', tmp) + 'px'
  18038. }
  18039. },
  18040. _validScaledProp: function (prop, value) {
  18041. var ele = this.resizer,
  18042. wrap = document
  18043. value = isNaN(value) ? 0 : value
  18044. switch (prop) {
  18045. case 'left':
  18046. return value < 0
  18047. ? 0
  18048. : value + ele.clientWidth > wrap.clientWidth
  18049. ? wrap.clientWidth - ele.clientWidth
  18050. : value
  18051. case 'top':
  18052. return value < 0
  18053. ? 0
  18054. : value + ele.clientHeight > wrap.clientHeight
  18055. ? wrap.clientHeight - ele.clientHeight
  18056. : value
  18057. case 'width':
  18058. return value <= 0
  18059. ? 1
  18060. : value + ele.offsetLeft > wrap.clientWidth
  18061. ? wrap.clientWidth - ele.offsetLeft
  18062. : value
  18063. case 'height':
  18064. return value <= 0
  18065. ? 1
  18066. : value + ele.offsetTop > wrap.clientHeight
  18067. ? wrap.clientHeight - ele.offsetTop
  18068. : value
  18069. }
  18070. },
  18071. hideCover: function () {
  18072. this.cover.style.display = 'none'
  18073. },
  18074. showCover: function () {
  18075. var me = this,
  18076. editorPos = domUtils.getXY(me.editor.ui.getDom()),
  18077. iframePos = domUtils.getXY(me.editor.iframe)
  18078. domUtils.setStyles(me.cover, {
  18079. width: me.editor.iframe.offsetWidth + 'px',
  18080. height: me.editor.iframe.offsetHeight + 'px',
  18081. top: iframePos.y - editorPos.y + 'px',
  18082. left: iframePos.x - editorPos.x + 'px',
  18083. position: 'absolute',
  18084. display: '',
  18085. })
  18086. },
  18087. show: function (targetObj) {
  18088. var me = this
  18089. me.resizer.style.display = 'block'
  18090. if (targetObj) me.attachTo(targetObj)
  18091. domUtils.on(this.resizer, 'mousedown', me.proxy(me._eventHandler, me))
  18092. domUtils.on(me.doc, 'mouseup', me.proxy(me._eventHandler, me))
  18093. me.showCover()
  18094. me.editor.fireEvent('afterscaleshow', me)
  18095. me.editor.fireEvent('saveScene')
  18096. },
  18097. hide: function () {
  18098. var me = this
  18099. me.hideCover()
  18100. me.resizer.style.display = 'none'
  18101. domUtils.un(me.resizer, 'mousedown', me.proxy(me._eventHandler, me))
  18102. domUtils.un(me.doc, 'mouseup', me.proxy(me._eventHandler, me))
  18103. me.editor.fireEvent('afterscalehide', me)
  18104. },
  18105. proxy: function (fn, context) {
  18106. return function (e) {
  18107. return fn.apply(context || this, arguments)
  18108. }
  18109. },
  18110. attachTo: function (targetObj) {
  18111. var me = this,
  18112. target = (me.target = targetObj),
  18113. resizer = this.resizer,
  18114. imgPos = domUtils.getXY(target),
  18115. iframePos = domUtils.getXY(me.editor.iframe),
  18116. editorPos = domUtils.getXY(resizer.parentNode)
  18117. domUtils.setStyles(resizer, {
  18118. width: target.width + 'px',
  18119. height: target.height + 'px',
  18120. left:
  18121. iframePos.x +
  18122. imgPos.x -
  18123. me.editor.document.body.scrollLeft -
  18124. editorPos.x -
  18125. parseInt(resizer.style.borderLeftWidth) +
  18126. 'px',
  18127. top:
  18128. iframePos.y +
  18129. imgPos.y -
  18130. me.editor.document.body.scrollTop -
  18131. editorPos.y -
  18132. parseInt(resizer.style.borderTopWidth) +
  18133. 'px',
  18134. })
  18135. },
  18136. }
  18137. })()
  18138. return function () {
  18139. var me = this,
  18140. imageScale
  18141. me.setOpt('imageScaleEnabled', true)
  18142. if (!browser.ie && me.options.imageScaleEnabled) {
  18143. me.addListener('click', function (type, e) {
  18144. var range = me.selection.getRange(),
  18145. img = range.getClosedNode()
  18146. if (
  18147. img &&
  18148. img.tagName == 'IMG' &&
  18149. me.body.contentEditable != 'false'
  18150. ) {
  18151. if (
  18152. img.className.indexOf('edui-faked-music') != -1 ||
  18153. img.getAttribute('anchorname') ||
  18154. domUtils.hasClass(img, 'loadingclass') ||
  18155. domUtils.hasClass(img, 'loaderrorclass')
  18156. ) {
  18157. return
  18158. }
  18159. if (!imageScale) {
  18160. imageScale = new Scale()
  18161. imageScale.init(me)
  18162. me.ui.getDom().appendChild(imageScale.resizer)
  18163. var _keyDownHandler = function (e) {
  18164. imageScale.hide()
  18165. if (imageScale.target)
  18166. me.selection
  18167. .getRange()
  18168. .selectNode(imageScale.target)
  18169. .select()
  18170. },
  18171. _mouseDownHandler = function (e) {
  18172. var ele = e.target || e.srcElement
  18173. if (
  18174. ele &&
  18175. (ele.className === undefined ||
  18176. ele.className.indexOf('edui-editor-imagescale') == -1)
  18177. ) {
  18178. _keyDownHandler(e)
  18179. }
  18180. },
  18181. timer
  18182. me.addListener('afterscaleshow', function (e) {
  18183. me.addListener('beforekeydown', _keyDownHandler)
  18184. me.addListener('beforemousedown', _mouseDownHandler)
  18185. domUtils.on(document, 'keydown', _keyDownHandler)
  18186. domUtils.on(document, 'mousedown', _mouseDownHandler)
  18187. me.selection.getNative().removeAllRanges()
  18188. })
  18189. me.addListener('afterscalehide', function (e) {
  18190. me.removeListener('beforekeydown', _keyDownHandler)
  18191. me.removeListener('beforemousedown', _mouseDownHandler)
  18192. domUtils.un(document, 'keydown', _keyDownHandler)
  18193. domUtils.un(document, 'mousedown', _mouseDownHandler)
  18194. var target = imageScale.target
  18195. if (target.parentNode) {
  18196. me.selection.getRange().selectNode(target).select()
  18197. }
  18198. })
  18199. //TODO 有iframe的情况,mousedown不能往下传。。
  18200. domUtils.on(imageScale.resizer, 'mousedown', function (e) {
  18201. me.selection.getNative().removeAllRanges()
  18202. var ele = e.target || e.srcElement
  18203. if (
  18204. ele &&
  18205. ele.className.indexOf('edui-editor-imagescale-hand') == -1
  18206. ) {
  18207. timer = setTimeout(function () {
  18208. imageScale.hide()
  18209. if (imageScale.target)
  18210. me.selection.getRange().selectNode(ele).select()
  18211. }, 200)
  18212. }
  18213. })
  18214. domUtils.on(imageScale.resizer, 'mouseup', function (e) {
  18215. var ele = e.target || e.srcElement
  18216. if (
  18217. ele &&
  18218. ele.className.indexOf('edui-editor-imagescale-hand') == -1
  18219. ) {
  18220. clearTimeout(timer)
  18221. }
  18222. })
  18223. }
  18224. imageScale.show(img)
  18225. } else {
  18226. if (imageScale && imageScale.resizer.style.display != 'none')
  18227. imageScale.hide()
  18228. }
  18229. })
  18230. }
  18231. if (browser.webkit) {
  18232. me.addListener('click', function (type, e) {
  18233. if (e.target.tagName == 'IMG' && me.body.contentEditable != 'false') {
  18234. var range = new dom.Range(me.document)
  18235. range.selectNode(e.target).select()
  18236. }
  18237. })
  18238. }
  18239. }
  18240. })()
  18241. // plugins/autolink.js
  18242. ///import core
  18243. ///commands 为非ie浏览器自动添加a标签
  18244. ///commandsName AutoLink
  18245. ///commandsTitle 自动增加链接
  18246. /**
  18247. * @description 为非ie浏览器自动添加a标签
  18248. * @author zhanyi
  18249. */
  18250. UE.plugin.register(
  18251. 'autolink',
  18252. function () {
  18253. var cont = 0
  18254. return !browser.ie
  18255. ? {
  18256. bindEvents: {
  18257. reset: function () {
  18258. cont = 0
  18259. },
  18260. keydown: function (type, evt) {
  18261. var me = this
  18262. var keyCode = evt.keyCode || evt.which
  18263. if (keyCode == 32 || keyCode == 13) {
  18264. var sel = me.selection.getNative(),
  18265. range = sel.getRangeAt(0).cloneRange(),
  18266. offset,
  18267. charCode
  18268. var start = range.startContainer
  18269. while (start.nodeType == 1 && range.startOffset > 0) {
  18270. start =
  18271. range.startContainer.childNodes[range.startOffset - 1]
  18272. if (!start) {
  18273. break
  18274. }
  18275. range.setStart(
  18276. start,
  18277. start.nodeType == 1
  18278. ? start.childNodes.length
  18279. : start.nodeValue.length
  18280. )
  18281. range.collapse(true)
  18282. start = range.startContainer
  18283. }
  18284. do {
  18285. if (range.startOffset == 0) {
  18286. start = range.startContainer.previousSibling
  18287. while (start && start.nodeType == 1) {
  18288. start = start.lastChild
  18289. }
  18290. if (!start || domUtils.isFillChar(start)) {
  18291. break
  18292. }
  18293. offset = start.nodeValue.length
  18294. } else {
  18295. start = range.startContainer
  18296. offset = range.startOffset
  18297. }
  18298. range.setStart(start, offset - 1)
  18299. charCode = range.toString().charCodeAt(0)
  18300. } while (charCode != 160 && charCode != 32)
  18301. if (
  18302. range
  18303. .toString()
  18304. .replace(new RegExp(domUtils.fillChar, 'g'), '')
  18305. .match(/(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)
  18306. ) {
  18307. while (range.toString().length) {
  18308. if (
  18309. /^(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i.test(
  18310. range.toString()
  18311. )
  18312. ) {
  18313. break
  18314. }
  18315. try {
  18316. range.setStart(
  18317. range.startContainer,
  18318. range.startOffset + 1
  18319. )
  18320. } catch (e) {
  18321. //trace:2121
  18322. var start = range.startContainer
  18323. while (!(next = start.nextSibling)) {
  18324. if (domUtils.isBody(start)) {
  18325. return
  18326. }
  18327. start = start.parentNode
  18328. }
  18329. range.setStart(next, 0)
  18330. }
  18331. }
  18332. //range的开始边界已经在a标签里的不再处理
  18333. if (
  18334. domUtils.findParentByTagName(
  18335. range.startContainer,
  18336. 'a',
  18337. true
  18338. )
  18339. ) {
  18340. return
  18341. }
  18342. var a = me.document.createElement('a'),
  18343. text = me.document.createTextNode(' '),
  18344. href
  18345. me.undoManger && me.undoManger.save()
  18346. a.appendChild(range.extractContents())
  18347. a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g, '')
  18348. href = a
  18349. .getAttribute('href')
  18350. .replace(new RegExp(domUtils.fillChar, 'g'), '')
  18351. href = /^(?:https?:\/\/)/gi.test(href)
  18352. ? href
  18353. : 'http://' + href
  18354. a.setAttribute('_src', utils.html(href))
  18355. a.href = utils.html(href)
  18356. range.insertNode(a)
  18357. a.parentNode.insertBefore(text, a.nextSibling)
  18358. range.setStart(text, 0)
  18359. range.collapse(true)
  18360. sel.removeAllRanges()
  18361. sel.addRange(range)
  18362. me.undoManger && me.undoManger.save()
  18363. }
  18364. }
  18365. },
  18366. },
  18367. }
  18368. : {}
  18369. },
  18370. function () {
  18371. var keyCodes = {
  18372. 37: 1,
  18373. 38: 1,
  18374. 39: 1,
  18375. 40: 1,
  18376. 13: 1,
  18377. 32: 1,
  18378. }
  18379. function checkIsCludeLink(node) {
  18380. if (node.nodeType == 3) {
  18381. return null
  18382. }
  18383. if (node.nodeName == 'A') {
  18384. return node
  18385. }
  18386. var lastChild = node.lastChild
  18387. while (lastChild) {
  18388. if (lastChild.nodeName == 'A') {
  18389. return lastChild
  18390. }
  18391. if (lastChild.nodeType == 3) {
  18392. if (domUtils.isWhitespace(lastChild)) {
  18393. lastChild = lastChild.previousSibling
  18394. continue
  18395. }
  18396. return null
  18397. }
  18398. lastChild = lastChild.lastChild
  18399. }
  18400. }
  18401. browser.ie &&
  18402. this.addListener('keyup', function (cmd, evt) {
  18403. var me = this,
  18404. keyCode = evt.keyCode
  18405. if (keyCodes[keyCode]) {
  18406. var rng = me.selection.getRange()
  18407. var start = rng.startContainer
  18408. if (keyCode == 13) {
  18409. while (
  18410. start &&
  18411. !domUtils.isBody(start) &&
  18412. !domUtils.isBlockElm(start)
  18413. ) {
  18414. start = start.parentNode
  18415. }
  18416. if (start && !domUtils.isBody(start) && start.nodeName == 'P') {
  18417. var pre = start.previousSibling
  18418. if (pre && pre.nodeType == 1) {
  18419. var pre = checkIsCludeLink(pre)
  18420. if (pre && !pre.getAttribute('_href')) {
  18421. domUtils.remove(pre, true)
  18422. }
  18423. }
  18424. }
  18425. } else if (keyCode == 32) {
  18426. if (start.nodeType == 3 && /^\s$/.test(start.nodeValue)) {
  18427. start = start.previousSibling
  18428. if (
  18429. start &&
  18430. start.nodeName == 'A' &&
  18431. !start.getAttribute('_href')
  18432. ) {
  18433. domUtils.remove(start, true)
  18434. }
  18435. }
  18436. } else {
  18437. start = domUtils.findParentByTagName(start, 'a', true)
  18438. if (start && !start.getAttribute('_href')) {
  18439. var bk = rng.createBookmark()
  18440. domUtils.remove(start, true)
  18441. rng.moveToBookmark(bk).select(true)
  18442. }
  18443. }
  18444. }
  18445. })
  18446. }
  18447. )
  18448. // plugins/autoheight.js
  18449. ///import core
  18450. ///commands 当输入内容超过编辑器高度时,编辑器自动增高
  18451. ///commandsName AutoHeight,autoHeightEnabled
  18452. ///commandsTitle 自动增高
  18453. /**
  18454. * @description 自动伸展
  18455. * @author zhanyi
  18456. */
  18457. UE.plugins['autoheight'] = function () {
  18458. var me = this
  18459. //提供开关,就算加载也可以关闭
  18460. me.autoHeightEnabled = me.options.autoHeightEnabled !== false
  18461. if (!me.autoHeightEnabled) {
  18462. return
  18463. }
  18464. var bakOverflow,
  18465. lastHeight = 0,
  18466. options = me.options,
  18467. currentHeight,
  18468. timer
  18469. function adjustHeight() {
  18470. var me = this
  18471. clearTimeout(timer)
  18472. if (isFullscreen) return
  18473. if (
  18474. !me.queryCommandState ||
  18475. (me.queryCommandState && me.queryCommandState('source') != 1)
  18476. ) {
  18477. timer = setTimeout(function () {
  18478. var node = me.body.lastChild
  18479. while (node && node.nodeType != 1) {
  18480. node = node.previousSibling
  18481. }
  18482. if (node && node.nodeType == 1) {
  18483. node.style.clear = 'both'
  18484. currentHeight = Math.max(
  18485. domUtils.getXY(node).y + node.offsetHeight + 25,
  18486. Math.max(options.minFrameHeight, options.initialFrameHeight)
  18487. )
  18488. if (currentHeight != lastHeight) {
  18489. if (
  18490. currentHeight !== parseInt(me.iframe.parentNode.style.height)
  18491. ) {
  18492. me.iframe.parentNode.style.height = currentHeight + 'px'
  18493. }
  18494. me.body.style.height = currentHeight + 'px'
  18495. lastHeight = currentHeight
  18496. }
  18497. domUtils.removeStyle(node, 'clear')
  18498. }
  18499. }, 50)
  18500. }
  18501. }
  18502. var isFullscreen
  18503. me.addListener('fullscreenchanged', function (cmd, f) {
  18504. isFullscreen = f
  18505. })
  18506. me.addListener('destroy', function () {
  18507. me.removeListener(
  18508. 'contentchange afterinserthtml keyup mouseup',
  18509. adjustHeight
  18510. )
  18511. })
  18512. me.enableAutoHeight = function () {
  18513. var me = this
  18514. if (!me.autoHeightEnabled) {
  18515. return
  18516. }
  18517. var doc = me.document
  18518. me.autoHeightEnabled = true
  18519. bakOverflow = doc.body.style.overflowY
  18520. doc.body.style.overflowY = 'hidden'
  18521. me.addListener(
  18522. 'contentchange afterinserthtml keyup mouseup',
  18523. adjustHeight
  18524. )
  18525. //ff不给事件算得不对
  18526. setTimeout(
  18527. function () {
  18528. adjustHeight.call(me)
  18529. },
  18530. browser.gecko ? 100 : 0
  18531. )
  18532. me.fireEvent('autoheightchanged', me.autoHeightEnabled)
  18533. }
  18534. me.disableAutoHeight = function () {
  18535. me.body.style.overflowY = bakOverflow || ''
  18536. me.removeListener('contentchange', adjustHeight)
  18537. me.removeListener('keyup', adjustHeight)
  18538. me.removeListener('mouseup', adjustHeight)
  18539. me.autoHeightEnabled = false
  18540. me.fireEvent('autoheightchanged', me.autoHeightEnabled)
  18541. }
  18542. me.on('setHeight', function () {
  18543. me.disableAutoHeight()
  18544. })
  18545. me.addListener('ready', function () {
  18546. me.enableAutoHeight()
  18547. //trace:1764
  18548. var timer
  18549. domUtils.on(
  18550. browser.ie ? me.body : me.document,
  18551. browser.webkit ? 'dragover' : 'drop',
  18552. function () {
  18553. clearTimeout(timer)
  18554. timer = setTimeout(function () {
  18555. //trace:3681
  18556. adjustHeight.call(me)
  18557. }, 100)
  18558. }
  18559. )
  18560. //修复内容过多时,回到顶部,顶部内容被工具栏遮挡问题
  18561. var lastScrollY
  18562. window.onscroll = function () {
  18563. if (lastScrollY === null) {
  18564. lastScrollY = this.scrollY
  18565. } else if (this.scrollY == 0 && lastScrollY != 0) {
  18566. me.window.scrollTo(0, 0)
  18567. lastScrollY = null
  18568. }
  18569. }
  18570. })
  18571. }
  18572. // plugins/autofloat.js
  18573. ///import core
  18574. ///commands 悬浮工具栏
  18575. ///commandsName AutoFloat,autoFloatEnabled
  18576. ///commandsTitle 悬浮工具栏
  18577. /**
  18578. * modified by chengchao01
  18579. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  18580. */
  18581. UE.plugins['autofloat'] = function () {
  18582. var me = this,
  18583. lang = me.getLang()
  18584. me.setOpt({
  18585. topOffset: 0,
  18586. })
  18587. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  18588. topOffset = me.options.topOffset
  18589. //如果不固定toolbar的位置,则直接退出
  18590. if (!optsAutoFloatEnabled) {
  18591. return
  18592. }
  18593. var uiUtils = UE.ui.uiUtils,
  18594. LteIE6 = browser.ie && browser.version <= 6,
  18595. quirks = browser.quirks
  18596. function checkHasUI() {
  18597. if (!UE.ui) {
  18598. alert(lang.autofloatMsg)
  18599. return 0
  18600. }
  18601. return 1
  18602. }
  18603. function fixIE6FixedPos() {
  18604. var docStyle = document.body.style
  18605. docStyle.backgroundImage = 'url("about:blank")'
  18606. docStyle.backgroundAttachment = 'fixed'
  18607. }
  18608. var bakCssText,
  18609. placeHolder = document.createElement('div'),
  18610. toolbarBox,
  18611. orgTop,
  18612. getPosition,
  18613. flag = true //ie7模式下需要偏移
  18614. function setFloating() {
  18615. var toobarBoxPos = domUtils.getXY(toolbarBox),
  18616. origalFloat = domUtils.getComputedStyle(toolbarBox, 'position'),
  18617. origalLeft = domUtils.getComputedStyle(toolbarBox, 'left')
  18618. toolbarBox.style.width = toolbarBox.offsetWidth + 'px'
  18619. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1
  18620. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox)
  18621. if (LteIE6 || (quirks && browser.ie)) {
  18622. if (toolbarBox.style.position != 'absolute') {
  18623. toolbarBox.style.position = 'absolute'
  18624. }
  18625. toolbarBox.style.top =
  18626. (document.body.scrollTop || document.documentElement.scrollTop) -
  18627. orgTop +
  18628. topOffset +
  18629. 'px'
  18630. } else {
  18631. if (browser.ie7Compat && flag) {
  18632. flag = false
  18633. toolbarBox.style.left =
  18634. domUtils.getXY(toolbarBox).x -
  18635. document.documentElement.getBoundingClientRect().left +
  18636. 2 +
  18637. 'px'
  18638. }
  18639. if (toolbarBox.style.position != 'fixed') {
  18640. toolbarBox.style.position = 'fixed'
  18641. toolbarBox.style.top = topOffset + 'px'
  18642. ;(origalFloat == 'absolute' || origalFloat == 'relative') &&
  18643. parseFloat(origalLeft) &&
  18644. (toolbarBox.style.left = toobarBoxPos.x + 'px')
  18645. }
  18646. }
  18647. }
  18648. function unsetFloating() {
  18649. flag = true
  18650. if (placeHolder.parentNode) {
  18651. placeHolder.parentNode.removeChild(placeHolder)
  18652. }
  18653. toolbarBox.style.cssText = bakCssText
  18654. }
  18655. function updateFloating() {
  18656. var rect3 = getPosition(me.container)
  18657. var offset = me.options.toolbarTopOffset || 0
  18658. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  18659. setFloating()
  18660. } else {
  18661. unsetFloating()
  18662. }
  18663. }
  18664. var defer_updateFloating = utils.defer(
  18665. function () {
  18666. updateFloating()
  18667. },
  18668. browser.ie ? 200 : 100,
  18669. true
  18670. )
  18671. me.addListener('destroy', function () {
  18672. domUtils.un(window, ['scroll', 'resize'], updateFloating)
  18673. me.removeListener('keydown', defer_updateFloating)
  18674. })
  18675. me.addListener('ready', function () {
  18676. if (checkHasUI(me)) {
  18677. //加载了ui组件,但在new时,没有加载ui,导致编辑器实例上没有ui类,所以这里做判断
  18678. if (!me.ui) {
  18679. return
  18680. }
  18681. getPosition = uiUtils.getClientRect
  18682. toolbarBox = me.ui.getDom('toolbarbox')
  18683. orgTop = getPosition(toolbarBox).top
  18684. bakCssText = toolbarBox.style.cssText
  18685. placeHolder.style.height = toolbarBox.offsetHeight + 'px'
  18686. if (LteIE6) {
  18687. fixIE6FixedPos()
  18688. }
  18689. domUtils.on(window, ['scroll', 'resize'], updateFloating)
  18690. me.addListener('keydown', defer_updateFloating)
  18691. me.addListener('beforefullscreenchange', function (t, enabled) {
  18692. if (enabled) {
  18693. unsetFloating()
  18694. }
  18695. })
  18696. me.addListener('fullscreenchanged', function (t, enabled) {
  18697. if (!enabled) {
  18698. updateFloating()
  18699. }
  18700. })
  18701. me.addListener('sourcemodechanged', function (t, enabled) {
  18702. setTimeout(function () {
  18703. updateFloating()
  18704. }, 0)
  18705. })
  18706. me.addListener('clearDoc', function () {
  18707. setTimeout(function () {
  18708. updateFloating()
  18709. }, 0)
  18710. })
  18711. }
  18712. })
  18713. }
  18714. // plugins/video.js
  18715. /**
  18716. * video插件, 为UEditor提供视频插入支持
  18717. * @file
  18718. * @since 1.2.6.1
  18719. */
  18720. UE.plugins['video'] = function () {
  18721. var me = this
  18722. /**
  18723. * 创建插入视频字符窜
  18724. * @param url 视频地址
  18725. * @param width 视频宽度
  18726. * @param height 视频高度
  18727. * @param align 视频对齐
  18728. * @param toEmbed 是否以flash代替显示
  18729. * @param addParagraph 是否需要添加P 标签
  18730. */
  18731. function creatInsertStr(url, width, height, id, align, classname, type) {
  18732. url = utils.unhtmlForUrl(url)
  18733. align = utils.unhtml(align)
  18734. classname = utils.unhtml(classname).trim()
  18735. width = parseInt(width, 10) || 0
  18736. height = parseInt(height, 10) || 0
  18737. var str
  18738. switch (type) {
  18739. case 'image':
  18740. str =
  18741. '<img ' +
  18742. (id ? 'id="' + id + '"' : '') +
  18743. ' width="' +
  18744. width +
  18745. '" height="' +
  18746. height +
  18747. '" _url="' +
  18748. url +
  18749. '" class="' +
  18750. classname.replace(/\bvideo-js\b/, '') +
  18751. '"' +
  18752. ' src="' +
  18753. me.options.UEDITOR_HOME_URL +
  18754. 'themes/default/images/spacer.gif" style="background:url(' +
  18755. me.options.UEDITOR_HOME_URL +
  18756. 'themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;' +
  18757. (align ? 'float:' + align + ';' : '') +
  18758. '" />'
  18759. break
  18760. case 'embed':
  18761. str =
  18762. '<embed type="application/x-shockwave-flash" class="' +
  18763. classname +
  18764. '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  18765. ' src="' +
  18766. utils.html(url) +
  18767. '" width="' +
  18768. width +
  18769. '" height="' +
  18770. height +
  18771. '"' +
  18772. (align ? ' style="float:' + align + '"' : '') +
  18773. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >'
  18774. break
  18775. case 'video':
  18776. var ext = url.substr(url.lastIndexOf('.') + 1)
  18777. if (ext == 'ogv') ext = 'ogg'
  18778. str =
  18779. '<video' +
  18780. (id ? ' id="' + id + '"' : '') +
  18781. ' class="' +
  18782. classname +
  18783. ' video-js" ' +
  18784. (align ? ' style="float:' + align + '"' : '') +
  18785. ' controls preload="none" width="' +
  18786. width +
  18787. '" height="' +
  18788. height +
  18789. '" src="' +
  18790. url +
  18791. '" data-setup="{}">' +
  18792. '<source src="' +
  18793. url +
  18794. '" type="video/' +
  18795. ext +
  18796. '" /></video>'
  18797. break
  18798. }
  18799. return str
  18800. }
  18801. function switchImgAndVideo(root, img2video) {
  18802. utils.each(
  18803. root.getNodesByTagName(img2video ? 'img' : 'embed video'),
  18804. function (node) {
  18805. var className = node.getAttr('class')
  18806. if (className && className.indexOf('edui-faked-video') != -1) {
  18807. var html = creatInsertStr(
  18808. img2video ? node.getAttr('_url') : node.getAttr('src'),
  18809. node.getAttr('width'),
  18810. node.getAttr('height'),
  18811. null,
  18812. node.getStyle('float') || '',
  18813. className,
  18814. img2video ? 'embed' : 'image'
  18815. )
  18816. node.parentNode.replaceChild(UE.uNode.createElement(html), node)
  18817. }
  18818. if (className && className.indexOf('edui-upload-video') != -1) {
  18819. var html = creatInsertStr(
  18820. img2video ? node.getAttr('_url') : node.getAttr('src'),
  18821. node.getAttr('width'),
  18822. node.getAttr('height'),
  18823. null,
  18824. node.getStyle('float') || '',
  18825. className,
  18826. img2video ? 'video' : 'image'
  18827. )
  18828. node.parentNode.replaceChild(UE.uNode.createElement(html), node)
  18829. }
  18830. }
  18831. )
  18832. }
  18833. me.addOutputRule(function (root) {
  18834. switchImgAndVideo(root, true)
  18835. })
  18836. me.addInputRule(function (root) {
  18837. switchImgAndVideo(root)
  18838. })
  18839. /**
  18840. * 插入视频
  18841. * @command insertvideo
  18842. * @method execCommand
  18843. * @param { String } cmd 命令字符串
  18844. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  18845. * @example
  18846. * ```javascript
  18847. *
  18848. * var videoAttr = {
  18849. * //视频地址
  18850. * url: 'http://www.youku.com/xxx',
  18851. * //视频宽高值, 单位px
  18852. * width: 200,
  18853. * height: 100
  18854. * };
  18855. *
  18856. * //editor 是编辑器实例
  18857. * //向编辑器插入单个视频
  18858. * editor.execCommand( 'insertvideo', videoAttr );
  18859. * ```
  18860. */
  18861. /**
  18862. * 插入视频
  18863. * @command insertvideo
  18864. * @method execCommand
  18865. * @param { String } cmd 命令字符串
  18866. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  18867. * @example
  18868. * ```javascript
  18869. *
  18870. * var videoAttr1 = {
  18871. * //视频地址
  18872. * url: 'http://www.youku.com/xxx',
  18873. * //视频宽高值, 单位px
  18874. * width: 200,
  18875. * height: 100
  18876. * },
  18877. * videoAttr2 = {
  18878. * //视频地址
  18879. * url: 'http://www.youku.com/xxx',
  18880. * //视频宽高值, 单位px
  18881. * width: 200,
  18882. * height: 100
  18883. * }
  18884. *
  18885. * //editor 是编辑器实例
  18886. * //该方法将会向编辑器内插入两个视频
  18887. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  18888. * ```
  18889. */
  18890. /**
  18891. * 查询当前光标所在处是否是一个视频
  18892. * @command insertvideo
  18893. * @method queryCommandState
  18894. * @param { String } cmd 需要查询的命令字符串
  18895. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  18896. * @example
  18897. * ```javascript
  18898. *
  18899. * //editor 是编辑器实例
  18900. * editor.queryCommandState( 'insertvideo' );
  18901. * ```
  18902. */
  18903. me.commands['insertvideo'] = {
  18904. execCommand: function (cmd, videoObjs, type) {
  18905. videoObjs = utils.isArray(videoObjs) ? videoObjs : [videoObjs]
  18906. var html = [],
  18907. id = 'tmpVedio',
  18908. cl
  18909. for (var i = 0, vi, len = videoObjs.length; i < len; i++) {
  18910. vi = videoObjs[i]
  18911. cl =
  18912. type == 'upload'
  18913. ? 'edui-upload-video video-js vjs-default-skin'
  18914. : 'edui-faked-video'
  18915. html.push(
  18916. creatInsertStr(
  18917. vi.url,
  18918. vi.width || 420,
  18919. vi.height || 280,
  18920. id + i,
  18921. null,
  18922. cl,
  18923. 'image'
  18924. )
  18925. )
  18926. }
  18927. me.execCommand('inserthtml', html.join(''), true)
  18928. var rng = this.selection.getRange()
  18929. for (var i = 0, len = videoObjs.length; i < len; i++) {
  18930. var img = this.document.getElementById('tmpVedio' + i)
  18931. domUtils.removeAttributes(img, 'id')
  18932. rng.selectNode(img).select()
  18933. me.execCommand('imagefloat', videoObjs[i].align)
  18934. }
  18935. },
  18936. queryCommandState: function () {
  18937. var img = me.selection.getRange().getClosedNode(),
  18938. flag =
  18939. img &&
  18940. (img.className == 'edui-faked-video' ||
  18941. img.className.indexOf('edui-upload-video') != -1)
  18942. return flag ? 1 : 0
  18943. },
  18944. }
  18945. }
  18946. // plugins/table.core.js
  18947. /**
  18948. * Created with JetBrains WebStorm.
  18949. * User: taoqili
  18950. * Date: 13-1-18
  18951. * Time: 上午11:09
  18952. * To change this template use File | Settings | File Templates.
  18953. */
  18954. /**
  18955. * UE表格操作类
  18956. * @param table
  18957. * @constructor
  18958. */
  18959. ;(function () {
  18960. var UETable = (UE.UETable = function (table) {
  18961. this.table = table
  18962. this.indexTable = []
  18963. this.selectedTds = []
  18964. this.cellsRange = {}
  18965. this.update(table)
  18966. })
  18967. //===以下为静态工具方法===
  18968. UETable.removeSelectedClass = function (cells) {
  18969. utils.each(cells, function (cell) {
  18970. domUtils.removeClasses(cell, 'selectTdClass')
  18971. })
  18972. }
  18973. UETable.addSelectedClass = function (cells) {
  18974. utils.each(cells, function (cell) {
  18975. domUtils.addClass(cell, 'selectTdClass')
  18976. })
  18977. }
  18978. UETable.isEmptyBlock = function (node) {
  18979. var reg = new RegExp(domUtils.fillChar, 'g')
  18980. if (
  18981. node[browser.ie ? 'innerText' : 'textContent']
  18982. .replace(/^\s*$/, '')
  18983. .replace(reg, '').length > 0
  18984. ) {
  18985. return 0
  18986. }
  18987. for (var i in dtd.$isNotEmpty)
  18988. if (dtd.$isNotEmpty.hasOwnProperty(i)) {
  18989. if (node.getElementsByTagName(i).length) {
  18990. return 0
  18991. }
  18992. }
  18993. return 1
  18994. }
  18995. UETable.getWidth = function (cell) {
  18996. if (!cell) return 0
  18997. return parseInt(domUtils.getComputedStyle(cell, 'width'), 10)
  18998. }
  18999. /**
  19000. * 获取单元格或者单元格组的“对齐”状态。 如果当前的检测对象是一个单元格组, 只有在满足所有单元格的 水平和竖直 对齐属性都相同的
  19001. * 条件时才会返回其状态值,否则将返回null; 如果当前只检测了一个单元格, 则直接返回当前单元格的对齐状态;
  19002. * @param table cell or table cells , 支持单个单元格dom对象 或者 单元格dom对象数组
  19003. * @return { align: 'left' || 'right' || 'center', valign: 'top' || 'middle' || 'bottom' } 或者 null
  19004. */
  19005. UETable.getTableCellAlignState = function (cells) {
  19006. !utils.isArray(cells) && (cells = [cells])
  19007. var result = {},
  19008. status = ['align', 'valign'],
  19009. tempStatus = null,
  19010. isSame = true //状态是否相同
  19011. utils.each(cells, function (cellNode) {
  19012. utils.each(status, function (currentState) {
  19013. tempStatus = cellNode.getAttribute(currentState)
  19014. if (!result[currentState] && tempStatus) {
  19015. result[currentState] = tempStatus
  19016. } else if (
  19017. !result[currentState] ||
  19018. tempStatus !== result[currentState]
  19019. ) {
  19020. isSame = false
  19021. return false
  19022. }
  19023. })
  19024. return isSame
  19025. })
  19026. return isSame ? result : null
  19027. }
  19028. /**
  19029. * 根据当前选区获取相关的table信息
  19030. * @return {Object}
  19031. */
  19032. UETable.getTableItemsByRange = function (editor) {
  19033. var start = editor.selection.getStart()
  19034. //ff下会选中bookmark
  19035. if (
  19036. start &&
  19037. start.id &&
  19038. start.id.indexOf('_baidu_bookmark_start_') === 0 &&
  19039. start.nextSibling
  19040. ) {
  19041. start = start.nextSibling
  19042. }
  19043. //在table或者td边缘有可能存在选中tr的情况
  19044. var cell =
  19045. start && domUtils.findParentByTagName(start, ['td', 'th'], true),
  19046. tr = cell && cell.parentNode,
  19047. caption = start && domUtils.findParentByTagName(start, 'caption', true),
  19048. table = caption ? caption.parentNode : tr && tr.parentNode.parentNode
  19049. return {
  19050. cell: cell,
  19051. tr: tr,
  19052. table: table,
  19053. caption: caption,
  19054. }
  19055. }
  19056. UETable.getUETableBySelected = function (editor) {
  19057. var table = UETable.getTableItemsByRange(editor).table
  19058. if (table && table.ueTable && table.ueTable.selectedTds.length) {
  19059. return table.ueTable
  19060. }
  19061. return null
  19062. }
  19063. UETable.getDefaultValue = function (editor, table) {
  19064. var borderMap = {
  19065. thin: '0px',
  19066. medium: '1px',
  19067. thick: '2px',
  19068. },
  19069. tableBorder,
  19070. tdPadding,
  19071. tdBorder,
  19072. tmpValue
  19073. if (!table) {
  19074. table = editor.document.createElement('table')
  19075. table.insertRow(0).insertCell(0).innerHTML = 'xxx'
  19076. editor.body.appendChild(table)
  19077. var td = table.getElementsByTagName('td')[0]
  19078. tmpValue = domUtils.getComputedStyle(table, 'border-left-width')
  19079. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19080. tmpValue = domUtils.getComputedStyle(td, 'padding-left')
  19081. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19082. tmpValue = domUtils.getComputedStyle(td, 'border-left-width')
  19083. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19084. domUtils.remove(table)
  19085. return {
  19086. tableBorder: tableBorder,
  19087. tdPadding: tdPadding,
  19088. tdBorder: tdBorder,
  19089. }
  19090. } else {
  19091. td = table.getElementsByTagName('td')[0]
  19092. tmpValue = domUtils.getComputedStyle(table, 'border-left-width')
  19093. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19094. tmpValue = domUtils.getComputedStyle(td, 'padding-left')
  19095. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19096. tmpValue = domUtils.getComputedStyle(td, 'border-left-width')
  19097. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10)
  19098. return {
  19099. tableBorder: tableBorder,
  19100. tdPadding: tdPadding,
  19101. tdBorder: tdBorder,
  19102. }
  19103. }
  19104. }
  19105. /**
  19106. * 根据当前点击的td或者table获取索引对象
  19107. * @param tdOrTable
  19108. */
  19109. UETable.getUETable = function (tdOrTable) {
  19110. var tag = tdOrTable.tagName.toLowerCase()
  19111. tdOrTable =
  19112. tag == 'td' || tag == 'th' || tag == 'caption'
  19113. ? domUtils.findParentByTagName(tdOrTable, 'table', true)
  19114. : tdOrTable
  19115. if (!tdOrTable.ueTable) {
  19116. tdOrTable.ueTable = new UETable(tdOrTable)
  19117. }
  19118. return tdOrTable.ueTable
  19119. }
  19120. UETable.cloneCell = function (cell, ignoreMerge, keepPro) {
  19121. if (!cell || utils.isString(cell)) {
  19122. return this.table.ownerDocument.createElement(cell || 'td')
  19123. }
  19124. var flag = domUtils.hasClass(cell, 'selectTdClass')
  19125. flag && domUtils.removeClasses(cell, 'selectTdClass')
  19126. var tmpCell = cell.cloneNode(true)
  19127. if (ignoreMerge) {
  19128. tmpCell.rowSpan = tmpCell.colSpan = 1
  19129. }
  19130. //去掉宽高
  19131. !keepPro && domUtils.removeAttributes(tmpCell, 'width height')
  19132. !keepPro && domUtils.removeAttributes(tmpCell, 'style')
  19133. tmpCell.style.borderLeftStyle = ''
  19134. tmpCell.style.borderTopStyle = ''
  19135. tmpCell.style.borderLeftColor = cell.style.borderRightColor
  19136. tmpCell.style.borderLeftWidth = cell.style.borderRightWidth
  19137. tmpCell.style.borderTopColor = cell.style.borderBottomColor
  19138. tmpCell.style.borderTopWidth = cell.style.borderBottomWidth
  19139. flag && domUtils.addClass(cell, 'selectTdClass')
  19140. return tmpCell
  19141. }
  19142. UETable.prototype = {
  19143. getMaxRows: function () {
  19144. var rows = this.table.rows,
  19145. maxLen = 1
  19146. for (var i = 0, row; (row = rows[i]); i++) {
  19147. var currentMax = 1
  19148. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  19149. currentMax = Math.max(cj.rowSpan || 1, currentMax)
  19150. }
  19151. maxLen = Math.max(currentMax + i, maxLen)
  19152. }
  19153. return maxLen
  19154. },
  19155. /**
  19156. * 获取当前表格的最大列数
  19157. */
  19158. getMaxCols: function () {
  19159. var rows = this.table.rows,
  19160. maxLen = 0,
  19161. cellRows = {}
  19162. for (var i = 0, row; (row = rows[i]); i++) {
  19163. var cellsNum = 0
  19164. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  19165. cellsNum += cj.colSpan || 1
  19166. if (cj.rowSpan && cj.rowSpan > 1) {
  19167. for (var k = 1; k < cj.rowSpan; k++) {
  19168. if (!cellRows['row_' + (i + k)]) {
  19169. cellRows['row_' + (i + k)] = cj.colSpan || 1
  19170. } else {
  19171. cellRows['row_' + (i + k)]++
  19172. }
  19173. }
  19174. }
  19175. }
  19176. cellsNum += cellRows['row_' + i] || 0
  19177. maxLen = Math.max(cellsNum, maxLen)
  19178. }
  19179. return maxLen
  19180. },
  19181. getCellColIndex: function (cell) {},
  19182. /**
  19183. * 获取当前cell旁边的单元格,
  19184. * @param cell
  19185. * @param right
  19186. */
  19187. getHSideCell: function (cell, right) {
  19188. try {
  19189. var cellInfo = this.getCellInfo(cell),
  19190. previewRowIndex,
  19191. previewColIndex
  19192. var len = this.selectedTds.length,
  19193. range = this.cellsRange
  19194. //首行或者首列没有前置单元格
  19195. if (
  19196. (!right && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19197. (right &&
  19198. (!len
  19199. ? cellInfo.colIndex == this.colsNum - 1
  19200. : range.endColIndex == this.colsNum - 1))
  19201. )
  19202. return null
  19203. previewRowIndex = !len ? cellInfo.rowIndex : range.beginRowIndex
  19204. previewColIndex = !right
  19205. ? !len
  19206. ? cellInfo.colIndex < 1
  19207. ? 0
  19208. : cellInfo.colIndex - 1
  19209. : range.beginColIndex - 1
  19210. : !len
  19211. ? cellInfo.colIndex + 1
  19212. : range.endColIndex + 1
  19213. return this.getCell(
  19214. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19215. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19216. )
  19217. } catch (e) {
  19218. showError(e)
  19219. }
  19220. },
  19221. getTabNextCell: function (cell, preRowIndex) {
  19222. var cellInfo = this.getCellInfo(cell),
  19223. rowIndex = preRowIndex || cellInfo.rowIndex,
  19224. colIndex = cellInfo.colIndex + 1 + (cellInfo.colSpan - 1),
  19225. nextCell
  19226. try {
  19227. nextCell = this.getCell(
  19228. this.indexTable[rowIndex][colIndex].rowIndex,
  19229. this.indexTable[rowIndex][colIndex].cellIndex
  19230. )
  19231. } catch (e) {
  19232. try {
  19233. rowIndex = rowIndex * 1 + 1
  19234. colIndex = 0
  19235. nextCell = this.getCell(
  19236. this.indexTable[rowIndex][colIndex].rowIndex,
  19237. this.indexTable[rowIndex][colIndex].cellIndex
  19238. )
  19239. } catch (e) {}
  19240. }
  19241. return nextCell
  19242. },
  19243. /**
  19244. * 获取视觉上的后置单元格
  19245. * @param cell
  19246. * @param bottom
  19247. */
  19248. getVSideCell: function (cell, bottom, ignoreRange) {
  19249. try {
  19250. var cellInfo = this.getCellInfo(cell),
  19251. nextRowIndex,
  19252. nextColIndex
  19253. var len = this.selectedTds.length && !ignoreRange,
  19254. range = this.cellsRange
  19255. //末行或者末列没有后置单元格
  19256. if (
  19257. (!bottom && cellInfo.rowIndex == 0) ||
  19258. (bottom &&
  19259. (!len
  19260. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19261. : range.endRowIndex == this.rowsNum - 1))
  19262. )
  19263. return null
  19264. nextRowIndex = !bottom
  19265. ? !len
  19266. ? cellInfo.rowIndex - 1
  19267. : range.beginRowIndex - 1
  19268. : !len
  19269. ? cellInfo.rowIndex + cellInfo.rowSpan
  19270. : range.endRowIndex + 1
  19271. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex
  19272. return this.getCell(
  19273. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19274. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19275. )
  19276. } catch (e) {
  19277. showError(e)
  19278. }
  19279. },
  19280. /**
  19281. * 获取相同结束位置的单元格,xOrY指代了是获取x轴相同还是y轴相同
  19282. */
  19283. getSameEndPosCells: function (cell, xOrY) {
  19284. try {
  19285. var flag = xOrY.toLowerCase() === 'x',
  19286. end =
  19287. domUtils.getXY(cell)[flag ? 'x' : 'y'] +
  19288. cell['offset' + (flag ? 'Width' : 'Height')],
  19289. rows = this.table.rows,
  19290. cells = null,
  19291. returns = []
  19292. for (var i = 0; i < this.rowsNum; i++) {
  19293. cells = rows[i].cells
  19294. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19295. var tmpEnd =
  19296. domUtils.getXY(tmpCell)[flag ? 'x' : 'y'] +
  19297. tmpCell['offset' + (flag ? 'Width' : 'Height')]
  19298. //对应行的td已经被上面行rowSpan了
  19299. if (tmpEnd > end && flag) break
  19300. if (cell == tmpCell || end == tmpEnd) {
  19301. //只获取单一的单元格
  19302. //todo 仅获取单一单元格在特定情况下会造成returns为空,从而影响后续的拖拽实现,修正这个。需考虑性能
  19303. if (tmpCell[flag ? 'colSpan' : 'rowSpan'] == 1) {
  19304. returns.push(tmpCell)
  19305. }
  19306. if (flag) break
  19307. }
  19308. }
  19309. }
  19310. return returns
  19311. } catch (e) {
  19312. showError(e)
  19313. }
  19314. },
  19315. setCellContent: function (cell, content) {
  19316. cell.innerHTML = content || (browser.ie ? domUtils.fillChar : '<br />')
  19317. },
  19318. cloneCell: UETable.cloneCell,
  19319. /**
  19320. * 获取跟当前单元格的右边竖线为左边的所有未合并单元格
  19321. */
  19322. getSameStartPosXCells: function (cell) {
  19323. try {
  19324. var start = domUtils.getXY(cell).x + cell.offsetWidth,
  19325. rows = this.table.rows,
  19326. cells,
  19327. returns = []
  19328. for (var i = 0; i < this.rowsNum; i++) {
  19329. cells = rows[i].cells
  19330. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19331. var tmpStart = domUtils.getXY(tmpCell).x
  19332. if (tmpStart > start) break
  19333. if (tmpStart == start && tmpCell.colSpan == 1) {
  19334. returns.push(tmpCell)
  19335. break
  19336. }
  19337. }
  19338. }
  19339. return returns
  19340. } catch (e) {
  19341. showError(e)
  19342. }
  19343. },
  19344. /**
  19345. * 更新table对应的索引表
  19346. */
  19347. update: function (table) {
  19348. this.table = table || this.table
  19349. this.selectedTds = []
  19350. this.cellsRange = {}
  19351. this.indexTable = []
  19352. var rows = this.table.rows,
  19353. rowsNum = this.getMaxRows(),
  19354. dNum = rowsNum - rows.length,
  19355. colsNum = this.getMaxCols()
  19356. while (dNum--) {
  19357. this.table.insertRow(rows.length)
  19358. }
  19359. this.rowsNum = rowsNum
  19360. this.colsNum = colsNum
  19361. for (var i = 0, len = rows.length; i < len; i++) {
  19362. this.indexTable[i] = new Array(colsNum)
  19363. }
  19364. //填充索引表
  19365. for (var rowIndex = 0, row; (row = rows[rowIndex]); rowIndex++) {
  19366. for (
  19367. var cellIndex = 0, cell, cells = row.cells;
  19368. (cell = cells[cellIndex]);
  19369. cellIndex++
  19370. ) {
  19371. //修正整行被rowSpan时导致的行数计算错误
  19372. if (cell.rowSpan > rowsNum) {
  19373. cell.rowSpan = rowsNum
  19374. }
  19375. var colIndex = cellIndex,
  19376. rowSpan = cell.rowSpan || 1,
  19377. colSpan = cell.colSpan || 1
  19378. //当已经被上一行rowSpan或者被前一列colSpan了,则跳到下一个单元格进行
  19379. while (this.indexTable[rowIndex][colIndex]) colIndex++
  19380. for (var j = 0; j < rowSpan; j++) {
  19381. for (var k = 0; k < colSpan; k++) {
  19382. this.indexTable[rowIndex + j][colIndex + k] = {
  19383. rowIndex: rowIndex,
  19384. cellIndex: cellIndex,
  19385. colIndex: colIndex,
  19386. rowSpan: rowSpan,
  19387. colSpan: colSpan,
  19388. }
  19389. }
  19390. }
  19391. }
  19392. }
  19393. //修复残缺td
  19394. for (j = 0; j < rowsNum; j++) {
  19395. for (k = 0; k < colsNum; k++) {
  19396. if (this.indexTable[j][k] === undefined) {
  19397. row = rows[j]
  19398. cell = row.cells[row.cells.length - 1]
  19399. cell = cell
  19400. ? cell.cloneNode(true)
  19401. : this.table.ownerDocument.createElement('td')
  19402. this.setCellContent(cell)
  19403. if (cell.colSpan !== 1) cell.colSpan = 1
  19404. if (cell.rowSpan !== 1) cell.rowSpan = 1
  19405. row.appendChild(cell)
  19406. this.indexTable[j][k] = {
  19407. rowIndex: j,
  19408. cellIndex: cell.cellIndex,
  19409. colIndex: k,
  19410. rowSpan: 1,
  19411. colSpan: 1,
  19412. }
  19413. }
  19414. }
  19415. }
  19416. //当框选后删除行或者列后撤销,需要重建选区。
  19417. var tds = domUtils.getElementsByTagName(this.table, 'td'),
  19418. selectTds = []
  19419. utils.each(tds, function (td) {
  19420. if (domUtils.hasClass(td, 'selectTdClass')) {
  19421. selectTds.push(td)
  19422. }
  19423. })
  19424. if (selectTds.length) {
  19425. var start = selectTds[0],
  19426. end = selectTds[selectTds.length - 1],
  19427. startInfo = this.getCellInfo(start),
  19428. endInfo = this.getCellInfo(end)
  19429. this.selectedTds = selectTds
  19430. this.cellsRange = {
  19431. beginRowIndex: startInfo.rowIndex,
  19432. beginColIndex: startInfo.colIndex,
  19433. endRowIndex: endInfo.rowIndex + endInfo.rowSpan - 1,
  19434. endColIndex: endInfo.colIndex + endInfo.colSpan - 1,
  19435. }
  19436. }
  19437. //给第一行设置firstRow的样式名称,在排序图标的样式上使用到
  19438. if (!domUtils.hasClass(this.table.rows[0], 'firstRow')) {
  19439. domUtils.addClass(this.table.rows[0], 'firstRow')
  19440. for (var i = 1; i < this.table.rows.length; i++) {
  19441. domUtils.removeClasses(this.table.rows[i], 'firstRow')
  19442. }
  19443. }
  19444. },
  19445. /**
  19446. * 获取单元格的索引信息
  19447. */
  19448. getCellInfo: function (cell) {
  19449. if (!cell) return
  19450. var cellIndex = cell.cellIndex,
  19451. rowIndex = cell.parentNode.rowIndex,
  19452. rowInfo = this.indexTable[rowIndex],
  19453. numCols = this.colsNum
  19454. for (var colIndex = cellIndex; colIndex < numCols; colIndex++) {
  19455. var cellInfo = rowInfo[colIndex]
  19456. if (
  19457. cellInfo.rowIndex === rowIndex &&
  19458. cellInfo.cellIndex === cellIndex
  19459. ) {
  19460. return cellInfo
  19461. }
  19462. }
  19463. },
  19464. /**
  19465. * 根据行列号获取单元格
  19466. */
  19467. getCell: function (rowIndex, cellIndex) {
  19468. return (
  19469. (rowIndex < this.rowsNum &&
  19470. this.table.rows[rowIndex].cells[cellIndex]) ||
  19471. null
  19472. )
  19473. },
  19474. /**
  19475. * 删除单元格
  19476. */
  19477. deleteCell: function (cell, rowIndex) {
  19478. rowIndex =
  19479. typeof rowIndex == 'number' ? rowIndex : cell.parentNode.rowIndex
  19480. var row = this.table.rows[rowIndex]
  19481. row.deleteCell(cell.cellIndex)
  19482. },
  19483. /**
  19484. * 根据始末两个单元格获取被框选的所有单元格范围
  19485. */
  19486. getCellsRange: function (cellA, cellB) {
  19487. function checkRange(
  19488. beginRowIndex,
  19489. beginColIndex,
  19490. endRowIndex,
  19491. endColIndex
  19492. ) {
  19493. var tmpBeginRowIndex = beginRowIndex,
  19494. tmpBeginColIndex = beginColIndex,
  19495. tmpEndRowIndex = endRowIndex,
  19496. tmpEndColIndex = endColIndex,
  19497. cellInfo,
  19498. colIndex,
  19499. rowIndex
  19500. // 通过indexTable检查是否存在超出TableRange上边界的情况
  19501. if (beginRowIndex > 0) {
  19502. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19503. cellInfo = me.indexTable[beginRowIndex][colIndex]
  19504. rowIndex = cellInfo.rowIndex
  19505. if (rowIndex < beginRowIndex) {
  19506. tmpBeginRowIndex = Math.min(rowIndex, tmpBeginRowIndex)
  19507. }
  19508. }
  19509. }
  19510. // 通过indexTable检查是否存在超出TableRange右边界的情况
  19511. if (endColIndex < me.colsNum) {
  19512. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19513. cellInfo = me.indexTable[rowIndex][endColIndex]
  19514. colIndex = cellInfo.colIndex + cellInfo.colSpan - 1
  19515. if (colIndex > endColIndex) {
  19516. tmpEndColIndex = Math.max(colIndex, tmpEndColIndex)
  19517. }
  19518. }
  19519. }
  19520. // 检查是否有超出TableRange下边界的情况
  19521. if (endRowIndex < me.rowsNum) {
  19522. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19523. cellInfo = me.indexTable[endRowIndex][colIndex]
  19524. rowIndex = cellInfo.rowIndex + cellInfo.rowSpan - 1
  19525. if (rowIndex > endRowIndex) {
  19526. tmpEndRowIndex = Math.max(rowIndex, tmpEndRowIndex)
  19527. }
  19528. }
  19529. }
  19530. // 检查是否有超出TableRange左边界的情况
  19531. if (beginColIndex > 0) {
  19532. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19533. cellInfo = me.indexTable[rowIndex][beginColIndex]
  19534. colIndex = cellInfo.colIndex
  19535. if (colIndex < beginColIndex) {
  19536. tmpBeginColIndex = Math.min(cellInfo.colIndex, tmpBeginColIndex)
  19537. }
  19538. }
  19539. }
  19540. //递归调用直至所有完成所有框选单元格的扩展
  19541. if (
  19542. tmpBeginRowIndex != beginRowIndex ||
  19543. tmpBeginColIndex != beginColIndex ||
  19544. tmpEndRowIndex != endRowIndex ||
  19545. tmpEndColIndex != endColIndex
  19546. ) {
  19547. return checkRange(
  19548. tmpBeginRowIndex,
  19549. tmpBeginColIndex,
  19550. tmpEndRowIndex,
  19551. tmpEndColIndex
  19552. )
  19553. } else {
  19554. // 不需要扩展TableRange的情况
  19555. return {
  19556. beginRowIndex: beginRowIndex,
  19557. beginColIndex: beginColIndex,
  19558. endRowIndex: endRowIndex,
  19559. endColIndex: endColIndex,
  19560. }
  19561. }
  19562. }
  19563. try {
  19564. var me = this,
  19565. cellAInfo = me.getCellInfo(cellA)
  19566. if (cellA === cellB) {
  19567. return {
  19568. beginRowIndex: cellAInfo.rowIndex,
  19569. beginColIndex: cellAInfo.colIndex,
  19570. endRowIndex: cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19571. endColIndex: cellAInfo.colIndex + cellAInfo.colSpan - 1,
  19572. }
  19573. }
  19574. var cellBInfo = me.getCellInfo(cellB)
  19575. // 计算TableRange的四个边
  19576. var beginRowIndex = Math.min(cellAInfo.rowIndex, cellBInfo.rowIndex),
  19577. beginColIndex = Math.min(cellAInfo.colIndex, cellBInfo.colIndex),
  19578. endRowIndex = Math.max(
  19579. cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19580. cellBInfo.rowIndex + cellBInfo.rowSpan - 1
  19581. ),
  19582. endColIndex = Math.max(
  19583. cellAInfo.colIndex + cellAInfo.colSpan - 1,
  19584. cellBInfo.colIndex + cellBInfo.colSpan - 1
  19585. )
  19586. return checkRange(
  19587. beginRowIndex,
  19588. beginColIndex,
  19589. endRowIndex,
  19590. endColIndex
  19591. )
  19592. } catch (e) {
  19593. //throw e;
  19594. }
  19595. },
  19596. /**
  19597. * 依据cellsRange获取对应的单元格集合
  19598. */
  19599. getCells: function (range) {
  19600. //每次获取cells之前必须先清除上次的选择,否则会对后续获取操作造成影响
  19601. this.clearSelected()
  19602. var beginRowIndex = range.beginRowIndex,
  19603. beginColIndex = range.beginColIndex,
  19604. endRowIndex = range.endRowIndex,
  19605. endColIndex = range.endColIndex,
  19606. cellInfo,
  19607. rowIndex,
  19608. colIndex,
  19609. tdHash = {},
  19610. returnTds = []
  19611. for (var i = beginRowIndex; i <= endRowIndex; i++) {
  19612. for (var j = beginColIndex; j <= endColIndex; j++) {
  19613. cellInfo = this.indexTable[i][j]
  19614. rowIndex = cellInfo.rowIndex
  19615. colIndex = cellInfo.colIndex
  19616. // 如果Cells里已经包含了此Cell则跳过
  19617. var key = rowIndex + '|' + colIndex
  19618. if (tdHash[key]) continue
  19619. tdHash[key] = 1
  19620. if (
  19621. rowIndex < i ||
  19622. colIndex < j ||
  19623. rowIndex + cellInfo.rowSpan - 1 > endRowIndex ||
  19624. colIndex + cellInfo.colSpan - 1 > endColIndex
  19625. ) {
  19626. return null
  19627. }
  19628. returnTds.push(this.getCell(rowIndex, cellInfo.cellIndex))
  19629. }
  19630. }
  19631. return returnTds
  19632. },
  19633. /**
  19634. * 清理已经选中的单元格
  19635. */
  19636. clearSelected: function () {
  19637. UETable.removeSelectedClass(this.selectedTds)
  19638. this.selectedTds = []
  19639. this.cellsRange = {}
  19640. },
  19641. /**
  19642. * 根据range设置已经选中的单元格
  19643. */
  19644. setSelected: function (range) {
  19645. var cells = this.getCells(range)
  19646. UETable.addSelectedClass(cells)
  19647. this.selectedTds = cells
  19648. this.cellsRange = range
  19649. },
  19650. isFullRow: function () {
  19651. var range = this.cellsRange
  19652. return range.endColIndex - range.beginColIndex + 1 == this.colsNum
  19653. },
  19654. isFullCol: function () {
  19655. var range = this.cellsRange,
  19656. table = this.table,
  19657. ths = table.getElementsByTagName('th'),
  19658. rows = range.endRowIndex - range.beginRowIndex + 1
  19659. return !ths.length
  19660. ? rows == this.rowsNum
  19661. : rows == this.rowsNum || rows == this.rowsNum - 1
  19662. },
  19663. /**
  19664. * 获取视觉上的前置单元格,默认是左边,top传入时
  19665. * @param cell
  19666. * @param top
  19667. */
  19668. getNextCell: function (cell, bottom, ignoreRange) {
  19669. try {
  19670. var cellInfo = this.getCellInfo(cell),
  19671. nextRowIndex,
  19672. nextColIndex
  19673. var len = this.selectedTds.length && !ignoreRange,
  19674. range = this.cellsRange
  19675. //末行或者末列没有后置单元格
  19676. if (
  19677. (!bottom && cellInfo.rowIndex == 0) ||
  19678. (bottom &&
  19679. (!len
  19680. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19681. : range.endRowIndex == this.rowsNum - 1))
  19682. )
  19683. return null
  19684. nextRowIndex = !bottom
  19685. ? !len
  19686. ? cellInfo.rowIndex - 1
  19687. : range.beginRowIndex - 1
  19688. : !len
  19689. ? cellInfo.rowIndex + cellInfo.rowSpan
  19690. : range.endRowIndex + 1
  19691. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex
  19692. return this.getCell(
  19693. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19694. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19695. )
  19696. } catch (e) {
  19697. showError(e)
  19698. }
  19699. },
  19700. getPreviewCell: function (cell, top) {
  19701. try {
  19702. var cellInfo = this.getCellInfo(cell),
  19703. previewRowIndex,
  19704. previewColIndex
  19705. var len = this.selectedTds.length,
  19706. range = this.cellsRange
  19707. //首行或者首列没有前置单元格
  19708. if (
  19709. (!top && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19710. (top &&
  19711. (!len
  19712. ? cellInfo.rowIndex > this.colsNum - 1
  19713. : range.endColIndex == this.colsNum - 1))
  19714. )
  19715. return null
  19716. previewRowIndex = !top
  19717. ? !len
  19718. ? cellInfo.rowIndex
  19719. : range.beginRowIndex
  19720. : !len
  19721. ? cellInfo.rowIndex < 1
  19722. ? 0
  19723. : cellInfo.rowIndex - 1
  19724. : range.beginRowIndex
  19725. previewColIndex = !top
  19726. ? !len
  19727. ? cellInfo.colIndex < 1
  19728. ? 0
  19729. : cellInfo.colIndex - 1
  19730. : range.beginColIndex - 1
  19731. : !len
  19732. ? cellInfo.colIndex
  19733. : range.endColIndex + 1
  19734. return this.getCell(
  19735. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19736. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19737. )
  19738. } catch (e) {
  19739. showError(e)
  19740. }
  19741. },
  19742. /**
  19743. * 移动单元格中的内容
  19744. */
  19745. moveContent: function (cellTo, cellFrom) {
  19746. if (UETable.isEmptyBlock(cellFrom)) return
  19747. if (UETable.isEmptyBlock(cellTo)) {
  19748. cellTo.innerHTML = cellFrom.innerHTML
  19749. return
  19750. }
  19751. var child = cellTo.lastChild
  19752. if (child.nodeType == 3 || !dtd.$block[child.tagName]) {
  19753. cellTo.appendChild(cellTo.ownerDocument.createElement('br'))
  19754. }
  19755. while ((child = cellFrom.firstChild)) {
  19756. cellTo.appendChild(child)
  19757. }
  19758. },
  19759. /**
  19760. * 向右合并单元格
  19761. */
  19762. mergeRight: function (cell) {
  19763. var cellInfo = this.getCellInfo(cell),
  19764. rightColIndex = cellInfo.colIndex + cellInfo.colSpan,
  19765. rightCellInfo = this.indexTable[cellInfo.rowIndex][rightColIndex],
  19766. rightCell = this.getCell(
  19767. rightCellInfo.rowIndex,
  19768. rightCellInfo.cellIndex
  19769. )
  19770. //合并
  19771. cell.colSpan = cellInfo.colSpan + rightCellInfo.colSpan
  19772. //被合并的单元格不应存在宽度属性
  19773. cell.removeAttribute('width')
  19774. //移动内容
  19775. this.moveContent(cell, rightCell)
  19776. //删掉被合并的Cell
  19777. this.deleteCell(rightCell, rightCellInfo.rowIndex)
  19778. this.update()
  19779. },
  19780. /**
  19781. * 向下合并单元格
  19782. */
  19783. mergeDown: function (cell) {
  19784. var cellInfo = this.getCellInfo(cell),
  19785. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan,
  19786. downCellInfo = this.indexTable[downRowIndex][cellInfo.colIndex],
  19787. downCell = this.getCell(downCellInfo.rowIndex, downCellInfo.cellIndex)
  19788. cell.rowSpan = cellInfo.rowSpan + downCellInfo.rowSpan
  19789. cell.removeAttribute('height')
  19790. this.moveContent(cell, downCell)
  19791. this.deleteCell(downCell, downCellInfo.rowIndex)
  19792. this.update()
  19793. },
  19794. /**
  19795. * 合并整个range中的内容
  19796. */
  19797. mergeRange: function () {
  19798. //由于合并操作可以在任意时刻进行,所以无法通过鼠标位置等信息实时生成range,只能通过缓存实例中的cellsRange对象来访问
  19799. var range = this.cellsRange,
  19800. leftTopCell = this.getCell(
  19801. range.beginRowIndex,
  19802. this.indexTable[range.beginRowIndex][range.beginColIndex].cellIndex
  19803. )
  19804. if (
  19805. leftTopCell.tagName == 'TH' &&
  19806. range.endRowIndex !== range.beginRowIndex
  19807. ) {
  19808. var index = this.indexTable,
  19809. info = this.getCellInfo(leftTopCell)
  19810. leftTopCell = this.getCell(1, index[1][info.colIndex].cellIndex)
  19811. range = this.getCellsRange(
  19812. leftTopCell,
  19813. this.getCell(
  19814. index[this.rowsNum - 1][info.colIndex].rowIndex,
  19815. index[this.rowsNum - 1][info.colIndex].cellIndex
  19816. )
  19817. )
  19818. }
  19819. // 删除剩余的Cells
  19820. var cells = this.getCells(range)
  19821. for (var i = 0, ci; (ci = cells[i++]); ) {
  19822. if (ci !== leftTopCell) {
  19823. this.moveContent(leftTopCell, ci)
  19824. this.deleteCell(ci)
  19825. }
  19826. }
  19827. // 修改左上角Cell的rowSpan和colSpan,并调整宽度属性设置
  19828. leftTopCell.rowSpan = range.endRowIndex - range.beginRowIndex + 1
  19829. leftTopCell.rowSpan > 1 && leftTopCell.removeAttribute('height')
  19830. leftTopCell.colSpan = range.endColIndex - range.beginColIndex + 1
  19831. leftTopCell.colSpan > 1 && leftTopCell.removeAttribute('width')
  19832. if (leftTopCell.rowSpan == this.rowsNum && leftTopCell.colSpan != 1) {
  19833. leftTopCell.colSpan = 1
  19834. }
  19835. if (leftTopCell.colSpan == this.colsNum && leftTopCell.rowSpan != 1) {
  19836. var rowIndex = leftTopCell.parentNode.rowIndex
  19837. //解决IE下的表格操作问题
  19838. if (this.table.deleteRow) {
  19839. for (
  19840. var i = rowIndex + 1,
  19841. curIndex = rowIndex + 1,
  19842. len = leftTopCell.rowSpan;
  19843. i < len;
  19844. i++
  19845. ) {
  19846. this.table.deleteRow(curIndex)
  19847. }
  19848. } else {
  19849. for (var i = 0, len = leftTopCell.rowSpan - 1; i < len; i++) {
  19850. var row = this.table.rows[rowIndex + 1]
  19851. row.parentNode.removeChild(row)
  19852. }
  19853. }
  19854. leftTopCell.rowSpan = 1
  19855. }
  19856. this.update()
  19857. },
  19858. /**
  19859. * 插入一行单元格
  19860. */
  19861. insertRow: function (rowIndex, sourceCell) {
  19862. var numCols = this.colsNum,
  19863. table = this.table,
  19864. row = table.insertRow(rowIndex),
  19865. cell,
  19866. isInsertTitle =
  19867. typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH'
  19868. function replaceTdToTh(colIndex, cell, tableRow) {
  19869. if (colIndex == 0) {
  19870. var tr = tableRow.nextSibling || tableRow.previousSibling,
  19871. th = tr.cells[colIndex]
  19872. if (th.tagName == 'TH') {
  19873. th = cell.ownerDocument.createElement('th')
  19874. th.appendChild(cell.firstChild)
  19875. tableRow.insertBefore(th, cell)
  19876. domUtils.remove(cell)
  19877. }
  19878. } else {
  19879. if (cell.tagName == 'TH') {
  19880. var td = cell.ownerDocument.createElement('td')
  19881. td.appendChild(cell.firstChild)
  19882. tableRow.insertBefore(td, cell)
  19883. domUtils.remove(cell)
  19884. }
  19885. }
  19886. }
  19887. //首行直接插入,无需考虑部分单元格被rowspan的情况
  19888. if (rowIndex == 0 || rowIndex == this.rowsNum) {
  19889. for (var colIndex = 0; colIndex < numCols; colIndex++) {
  19890. cell = this.cloneCell(sourceCell, true)
  19891. this.setCellContent(cell)
  19892. cell.getAttribute('vAlign') &&
  19893. cell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  19894. row.appendChild(cell)
  19895. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row)
  19896. }
  19897. } else {
  19898. var infoRow = this.indexTable[rowIndex],
  19899. cellIndex = 0
  19900. for (colIndex = 0; colIndex < numCols; colIndex++) {
  19901. var cellInfo = infoRow[colIndex]
  19902. //如果存在某个单元格的rowspan穿过待插入行的位置,则修改该单元格的rowspan即可,无需插入单元格
  19903. if (cellInfo.rowIndex < rowIndex) {
  19904. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex)
  19905. cell.rowSpan = cellInfo.rowSpan + 1
  19906. } else {
  19907. cell = this.cloneCell(sourceCell, true)
  19908. this.setCellContent(cell)
  19909. row.appendChild(cell)
  19910. }
  19911. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row)
  19912. }
  19913. }
  19914. //框选时插入不触发contentchange,需要手动更新索引。
  19915. this.update()
  19916. return row
  19917. },
  19918. /**
  19919. * 删除一行单元格
  19920. * @param rowIndex
  19921. */
  19922. deleteRow: function (rowIndex) {
  19923. var row = this.table.rows[rowIndex],
  19924. infoRow = this.indexTable[rowIndex],
  19925. colsNum = this.colsNum,
  19926. count = 0 //处理计数
  19927. for (var colIndex = 0; colIndex < colsNum; ) {
  19928. var cellInfo = infoRow[colIndex],
  19929. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex)
  19930. if (cell.rowSpan > 1) {
  19931. if (cellInfo.rowIndex == rowIndex) {
  19932. var clone = cell.cloneNode(true)
  19933. clone.rowSpan = cell.rowSpan - 1
  19934. clone.innerHTML = ''
  19935. cell.rowSpan = 1
  19936. var nextRowIndex = rowIndex + 1,
  19937. nextRow = this.table.rows[nextRowIndex],
  19938. insertCellIndex,
  19939. preMerged =
  19940. this.getPreviewMergedCellsNum(nextRowIndex, colIndex) - count
  19941. if (preMerged < colIndex) {
  19942. insertCellIndex = colIndex - preMerged - 1
  19943. //nextRow.insertCell(insertCellIndex);
  19944. domUtils.insertAfter(nextRow.cells[insertCellIndex], clone)
  19945. } else {
  19946. if (nextRow.cells.length)
  19947. nextRow.insertBefore(clone, nextRow.cells[0])
  19948. }
  19949. count += 1
  19950. //cell.parentNode.removeChild(cell);
  19951. }
  19952. }
  19953. colIndex += cell.colSpan || 1
  19954. }
  19955. var deleteTds = [],
  19956. cacheMap = {}
  19957. for (colIndex = 0; colIndex < colsNum; colIndex++) {
  19958. var tmpRowIndex = infoRow[colIndex].rowIndex,
  19959. tmpCellIndex = infoRow[colIndex].cellIndex,
  19960. key = tmpRowIndex + '_' + tmpCellIndex
  19961. if (cacheMap[key]) continue
  19962. cacheMap[key] = 1
  19963. cell = this.getCell(tmpRowIndex, tmpCellIndex)
  19964. deleteTds.push(cell)
  19965. }
  19966. var mergeTds = []
  19967. utils.each(deleteTds, function (td) {
  19968. if (td.rowSpan == 1) {
  19969. td.parentNode.removeChild(td)
  19970. } else {
  19971. mergeTds.push(td)
  19972. }
  19973. })
  19974. utils.each(mergeTds, function (td) {
  19975. td.rowSpan--
  19976. })
  19977. row.parentNode.removeChild(row)
  19978. //浏览器方法本身存在bug,采用自定义方法删除
  19979. //this.table.deleteRow(rowIndex);
  19980. this.update()
  19981. },
  19982. insertCol: function (colIndex, sourceCell, defaultValue) {
  19983. var rowsNum = this.rowsNum,
  19984. rowIndex = 0,
  19985. tableRow,
  19986. cell,
  19987. backWidth = parseInt(
  19988. (this.table.offsetWidth -
  19989. (this.colsNum + 1) * 20 -
  19990. (this.colsNum + 1)) /
  19991. (this.colsNum + 1),
  19992. 10
  19993. ),
  19994. isInsertTitleCol =
  19995. typeof sourceCell == 'string' && sourceCell.toUpperCase() == 'TH'
  19996. function replaceTdToTh(rowIndex, cell, tableRow) {
  19997. if (rowIndex == 0) {
  19998. var th = cell.nextSibling || cell.previousSibling
  19999. if (th.tagName == 'TH') {
  20000. th = cell.ownerDocument.createElement('th')
  20001. th.appendChild(cell.firstChild)
  20002. tableRow.insertBefore(th, cell)
  20003. domUtils.remove(cell)
  20004. }
  20005. } else {
  20006. if (cell.tagName == 'TH') {
  20007. var td = cell.ownerDocument.createElement('td')
  20008. td.appendChild(cell.firstChild)
  20009. tableRow.insertBefore(td, cell)
  20010. domUtils.remove(cell)
  20011. }
  20012. }
  20013. }
  20014. var preCell
  20015. if (colIndex == 0 || colIndex == this.colsNum) {
  20016. for (; rowIndex < rowsNum; rowIndex++) {
  20017. tableRow = this.table.rows[rowIndex]
  20018. preCell =
  20019. tableRow.cells[colIndex == 0 ? colIndex : tableRow.cells.length]
  20020. cell = this.cloneCell(sourceCell, true) //tableRow.insertCell(colIndex == 0 ? colIndex : tableRow.cells.length);
  20021. this.setCellContent(cell)
  20022. cell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20023. preCell && cell.setAttribute('width', preCell.getAttribute('width'))
  20024. if (!colIndex) {
  20025. tableRow.insertBefore(cell, tableRow.cells[0])
  20026. } else {
  20027. domUtils.insertAfter(
  20028. tableRow.cells[tableRow.cells.length - 1],
  20029. cell
  20030. )
  20031. }
  20032. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow)
  20033. }
  20034. } else {
  20035. for (; rowIndex < rowsNum; rowIndex++) {
  20036. var cellInfo = this.indexTable[rowIndex][colIndex]
  20037. if (cellInfo.colIndex < colIndex) {
  20038. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex)
  20039. cell.colSpan = cellInfo.colSpan + 1
  20040. } else {
  20041. tableRow = this.table.rows[rowIndex]
  20042. preCell = tableRow.cells[cellInfo.cellIndex]
  20043. cell = this.cloneCell(sourceCell, true) //tableRow.insertCell(cellInfo.cellIndex);
  20044. this.setCellContent(cell)
  20045. cell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20046. preCell &&
  20047. cell.setAttribute('width', preCell.getAttribute('width'))
  20048. //防止IE下报错
  20049. preCell
  20050. ? tableRow.insertBefore(cell, preCell)
  20051. : tableRow.appendChild(cell)
  20052. }
  20053. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow)
  20054. }
  20055. }
  20056. //框选时插入不触发contentchange,需要手动更新索引
  20057. this.update()
  20058. this.updateWidth(
  20059. backWidth,
  20060. defaultValue || { tdPadding: 10, tdBorder: 1 }
  20061. )
  20062. },
  20063. updateWidth: function (width, defaultValue) {
  20064. var table = this.table,
  20065. tmpWidth =
  20066. UETable.getWidth(table) -
  20067. defaultValue.tdPadding * 2 -
  20068. defaultValue.tdBorder +
  20069. width
  20070. if (tmpWidth < table.ownerDocument.body.offsetWidth) {
  20071. table.setAttribute('width', tmpWidth)
  20072. return
  20073. }
  20074. var tds = domUtils.getElementsByTagName(this.table, 'td th')
  20075. utils.each(tds, function (td) {
  20076. td.setAttribute('width', width)
  20077. })
  20078. },
  20079. deleteCol: function (colIndex) {
  20080. var indexTable = this.indexTable,
  20081. tableRows = this.table.rows,
  20082. backTableWidth = this.table.getAttribute('width'),
  20083. backTdWidth = 0,
  20084. rowsNum = this.rowsNum,
  20085. cacheMap = {}
  20086. for (var rowIndex = 0; rowIndex < rowsNum; ) {
  20087. var infoRow = indexTable[rowIndex],
  20088. cellInfo = infoRow[colIndex],
  20089. key = cellInfo.rowIndex + '_' + cellInfo.colIndex
  20090. // 跳过已经处理过的Cell
  20091. if (cacheMap[key]) continue
  20092. cacheMap[key] = 1
  20093. var cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex)
  20094. if (!backTdWidth)
  20095. backTdWidth =
  20096. cell && parseInt(cell.offsetWidth / cell.colSpan, 10).toFixed(0)
  20097. // 如果Cell的colSpan大于1, 就修改colSpan, 否则就删掉这个Cell
  20098. if (cell.colSpan > 1) {
  20099. cell.colSpan--
  20100. } else {
  20101. tableRows[rowIndex].deleteCell(cellInfo.cellIndex)
  20102. }
  20103. rowIndex += cellInfo.rowSpan || 1
  20104. }
  20105. this.table.setAttribute('width', backTableWidth - backTdWidth)
  20106. this.update()
  20107. },
  20108. splitToCells: function (cell) {
  20109. var me = this,
  20110. cells = this.splitToRows(cell)
  20111. utils.each(cells, function (cell) {
  20112. me.splitToCols(cell)
  20113. })
  20114. },
  20115. splitToRows: function (cell) {
  20116. var cellInfo = this.getCellInfo(cell),
  20117. rowIndex = cellInfo.rowIndex,
  20118. colIndex = cellInfo.colIndex,
  20119. results = []
  20120. // 修改Cell的rowSpan
  20121. cell.rowSpan = 1
  20122. results.push(cell)
  20123. // 补齐单元格
  20124. for (
  20125. var i = rowIndex, endRow = rowIndex + cellInfo.rowSpan;
  20126. i < endRow;
  20127. i++
  20128. ) {
  20129. if (i == rowIndex) continue
  20130. var tableRow = this.table.rows[i],
  20131. tmpCell = tableRow.insertCell(
  20132. colIndex - this.getPreviewMergedCellsNum(i, colIndex)
  20133. )
  20134. tmpCell.colSpan = cellInfo.colSpan
  20135. this.setCellContent(tmpCell)
  20136. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20137. tmpCell.setAttribute('align', cell.getAttribute('align'))
  20138. if (cell.style.cssText) {
  20139. tmpCell.style.cssText = cell.style.cssText
  20140. }
  20141. results.push(tmpCell)
  20142. }
  20143. this.update()
  20144. return results
  20145. },
  20146. getPreviewMergedCellsNum: function (rowIndex, colIndex) {
  20147. var indexRow = this.indexTable[rowIndex],
  20148. num = 0
  20149. for (var i = 0; i < colIndex; ) {
  20150. var colSpan = indexRow[i].colSpan,
  20151. tmpRowIndex = indexRow[i].rowIndex
  20152. num += colSpan - (tmpRowIndex == rowIndex ? 1 : 0)
  20153. i += colSpan
  20154. }
  20155. return num
  20156. },
  20157. splitToCols: function (cell) {
  20158. var backWidth = (cell.offsetWidth / cell.colSpan - 22).toFixed(0),
  20159. cellInfo = this.getCellInfo(cell),
  20160. rowIndex = cellInfo.rowIndex,
  20161. colIndex = cellInfo.colIndex,
  20162. results = []
  20163. // 修改Cell的rowSpan
  20164. cell.colSpan = 1
  20165. cell.setAttribute('width', backWidth)
  20166. results.push(cell)
  20167. // 补齐单元格
  20168. for (
  20169. var j = colIndex, endCol = colIndex + cellInfo.colSpan;
  20170. j < endCol;
  20171. j++
  20172. ) {
  20173. if (j == colIndex) continue
  20174. var tableRow = this.table.rows[rowIndex],
  20175. tmpCell = tableRow.insertCell(
  20176. this.indexTable[rowIndex][j].cellIndex + 1
  20177. )
  20178. tmpCell.rowSpan = cellInfo.rowSpan
  20179. this.setCellContent(tmpCell)
  20180. tmpCell.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20181. tmpCell.setAttribute('align', cell.getAttribute('align'))
  20182. tmpCell.setAttribute('width', backWidth)
  20183. if (cell.style.cssText) {
  20184. tmpCell.style.cssText = cell.style.cssText
  20185. }
  20186. //处理th的情况
  20187. if (cell.tagName == 'TH') {
  20188. var th = cell.ownerDocument.createElement('th')
  20189. th.appendChild(tmpCell.firstChild)
  20190. th.setAttribute('vAlign', cell.getAttribute('vAlign'))
  20191. th.rowSpan = tmpCell.rowSpan
  20192. tableRow.insertBefore(th, tmpCell)
  20193. domUtils.remove(tmpCell)
  20194. }
  20195. results.push(tmpCell)
  20196. }
  20197. this.update()
  20198. return results
  20199. },
  20200. isLastCell: function (cell, rowsNum, colsNum) {
  20201. rowsNum = rowsNum || this.rowsNum
  20202. colsNum = colsNum || this.colsNum
  20203. var cellInfo = this.getCellInfo(cell)
  20204. return (
  20205. cellInfo.rowIndex + cellInfo.rowSpan == rowsNum &&
  20206. cellInfo.colIndex + cellInfo.colSpan == colsNum
  20207. )
  20208. },
  20209. getLastCell: function (cells) {
  20210. cells = cells || this.table.getElementsByTagName('td')
  20211. var firstInfo = this.getCellInfo(cells[0])
  20212. var me = this,
  20213. last = cells[0],
  20214. tr = last.parentNode,
  20215. cellsNum = 0,
  20216. cols = 0,
  20217. rows
  20218. utils.each(cells, function (cell) {
  20219. if (cell.parentNode == tr) cols += cell.colSpan || 1
  20220. cellsNum += cell.rowSpan * cell.colSpan || 1
  20221. })
  20222. rows = cellsNum / cols
  20223. utils.each(cells, function (cell) {
  20224. if (me.isLastCell(cell, rows, cols)) {
  20225. last = cell
  20226. return false
  20227. }
  20228. })
  20229. return last
  20230. },
  20231. selectRow: function (rowIndex) {
  20232. var indexRow = this.indexTable[rowIndex],
  20233. start = this.getCell(indexRow[0].rowIndex, indexRow[0].cellIndex),
  20234. end = this.getCell(
  20235. indexRow[this.colsNum - 1].rowIndex,
  20236. indexRow[this.colsNum - 1].cellIndex
  20237. ),
  20238. range = this.getCellsRange(start, end)
  20239. this.setSelected(range)
  20240. },
  20241. selectTable: function () {
  20242. var tds = this.table.getElementsByTagName('td'),
  20243. range = this.getCellsRange(tds[0], tds[tds.length - 1])
  20244. this.setSelected(range)
  20245. },
  20246. setBackground: function (cells, value) {
  20247. if (typeof value === 'string') {
  20248. utils.each(cells, function (cell) {
  20249. cell.style.backgroundColor = value
  20250. })
  20251. } else if (typeof value === 'object') {
  20252. value = utils.extend(
  20253. {
  20254. repeat: true,
  20255. colorList: ['#ddd', '#fff'],
  20256. },
  20257. value
  20258. )
  20259. var rowIndex = this.getCellInfo(cells[0]).rowIndex,
  20260. count = 0,
  20261. colors = value.colorList,
  20262. getColor = function (list, index, repeat) {
  20263. return list[index]
  20264. ? list[index]
  20265. : repeat
  20266. ? list[index % list.length]
  20267. : ''
  20268. }
  20269. for (var i = 0, cell; (cell = cells[i++]); ) {
  20270. var cellInfo = this.getCellInfo(cell)
  20271. cell.style.backgroundColor = getColor(
  20272. colors,
  20273. rowIndex + count == cellInfo.rowIndex ? count : ++count,
  20274. value.repeat
  20275. )
  20276. }
  20277. }
  20278. },
  20279. removeBackground: function (cells) {
  20280. utils.each(cells, function (cell) {
  20281. cell.style.backgroundColor = ''
  20282. })
  20283. },
  20284. }
  20285. function showError(e) {}
  20286. })()
  20287. // plugins/table.cmds.js
  20288. /**
  20289. * Created with JetBrains PhpStorm.
  20290. * User: taoqili
  20291. * Date: 13-2-20
  20292. * Time: 下午6:25
  20293. * To change this template use File | Settings | File Templates.
  20294. */
  20295. ;(function () {
  20296. var UT = UE.UETable,
  20297. getTableItemsByRange = function (editor) {
  20298. return UT.getTableItemsByRange(editor)
  20299. },
  20300. getUETableBySelected = function (editor) {
  20301. return UT.getUETableBySelected(editor)
  20302. },
  20303. getDefaultValue = function (editor, table) {
  20304. return UT.getDefaultValue(editor, table)
  20305. },
  20306. getUETable = function (tdOrTable) {
  20307. return UT.getUETable(tdOrTable)
  20308. }
  20309. UE.commands['inserttable'] = {
  20310. queryCommandState: function () {
  20311. return getTableItemsByRange(this).table ? -1 : 0
  20312. },
  20313. execCommand: function (cmd, opt) {
  20314. function createTable(opt, tdWidth) {
  20315. var html = [],
  20316. rowsNum = opt.numRows,
  20317. colsNum = opt.numCols
  20318. for (var r = 0; r < rowsNum; r++) {
  20319. html.push('<tr' + (r == 0 ? ' class="firstRow"' : '') + '>')
  20320. for (var c = 0; c < colsNum; c++) {
  20321. html.push(
  20322. '<td width="' +
  20323. tdWidth +
  20324. '" vAlign="' +
  20325. opt.tdvalign +
  20326. '" >' +
  20327. (browser.ie && browser.version < 11
  20328. ? domUtils.fillChar
  20329. : '<br/>') +
  20330. '</td>'
  20331. )
  20332. }
  20333. html.push('</tr>')
  20334. }
  20335. //禁止指定table-width
  20336. return '<table><tbody>' + html.join('') + '</tbody></table>'
  20337. }
  20338. if (!opt) {
  20339. opt = utils.extend(
  20340. {},
  20341. {
  20342. numCols: this.options.defaultCols,
  20343. numRows: this.options.defaultRows,
  20344. tdvalign: this.options.tdvalign,
  20345. }
  20346. )
  20347. }
  20348. var me = this
  20349. var range = this.selection.getRange(),
  20350. start = range.startContainer,
  20351. firstParentBlock =
  20352. domUtils.findParent(
  20353. start,
  20354. function (node) {
  20355. return domUtils.isBlockElm(node)
  20356. },
  20357. true
  20358. ) || me.body
  20359. var defaultValue = getDefaultValue(me),
  20360. tableWidth = firstParentBlock.offsetWidth,
  20361. tdWidth = Math.floor(
  20362. tableWidth / opt.numCols -
  20363. defaultValue.tdPadding * 2 -
  20364. defaultValue.tdBorder
  20365. )
  20366. //todo其他属性
  20367. !opt.tdvalign && (opt.tdvalign = me.options.tdvalign)
  20368. me.execCommand('inserthtml', createTable(opt, tdWidth))
  20369. },
  20370. }
  20371. UE.commands['insertparagraphbeforetable'] = {
  20372. queryCommandState: function () {
  20373. return getTableItemsByRange(this).cell ? 0 : -1
  20374. },
  20375. execCommand: function () {
  20376. var table = getTableItemsByRange(this).table
  20377. if (table) {
  20378. var p = this.document.createElement('p')
  20379. p.innerHTML = browser.ie ? '&nbsp;' : '<br />'
  20380. table.parentNode.insertBefore(p, table)
  20381. this.selection.getRange().setStart(p, 0).setCursor()
  20382. }
  20383. },
  20384. }
  20385. UE.commands['deletetable'] = {
  20386. queryCommandState: function () {
  20387. var rng = this.selection.getRange()
  20388. return domUtils.findParentByTagName(rng.startContainer, 'table', true)
  20389. ? 0
  20390. : -1
  20391. },
  20392. execCommand: function (cmd, table) {
  20393. var rng = this.selection.getRange()
  20394. table =
  20395. table ||
  20396. domUtils.findParentByTagName(rng.startContainer, 'table', true)
  20397. if (table) {
  20398. var next = table.nextSibling
  20399. if (!next) {
  20400. next = domUtils.createElement(this.document, 'p', {
  20401. innerHTML: browser.ie ? domUtils.fillChar : '<br/>',
  20402. })
  20403. table.parentNode.insertBefore(next, table)
  20404. }
  20405. domUtils.remove(table)
  20406. rng = this.selection.getRange()
  20407. if (next.nodeType == 3) {
  20408. rng.setStartBefore(next)
  20409. } else {
  20410. rng.setStart(next, 0)
  20411. }
  20412. rng.setCursor(false, true)
  20413. this.fireEvent('tablehasdeleted')
  20414. }
  20415. },
  20416. }
  20417. UE.commands['cellalign'] = {
  20418. queryCommandState: function () {
  20419. return getSelectedArr(this).length ? 0 : -1
  20420. },
  20421. execCommand: function (cmd, align) {
  20422. var selectedTds = getSelectedArr(this)
  20423. if (selectedTds.length) {
  20424. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20425. ci.setAttribute('align', align)
  20426. }
  20427. }
  20428. },
  20429. }
  20430. UE.commands['cellvalign'] = {
  20431. queryCommandState: function () {
  20432. return getSelectedArr(this).length ? 0 : -1
  20433. },
  20434. execCommand: function (cmd, valign) {
  20435. var selectedTds = getSelectedArr(this)
  20436. if (selectedTds.length) {
  20437. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20438. ci.setAttribute('vAlign', valign)
  20439. }
  20440. }
  20441. },
  20442. }
  20443. UE.commands['insertcaption'] = {
  20444. queryCommandState: function () {
  20445. var table = getTableItemsByRange(this).table
  20446. if (table) {
  20447. return table.getElementsByTagName('caption').length == 0 ? 1 : -1
  20448. }
  20449. return -1
  20450. },
  20451. execCommand: function () {
  20452. var table = getTableItemsByRange(this).table
  20453. if (table) {
  20454. var caption = this.document.createElement('caption')
  20455. caption.innerHTML = browser.ie ? domUtils.fillChar : '<br/>'
  20456. table.insertBefore(caption, table.firstChild)
  20457. var range = this.selection.getRange()
  20458. range.setStart(caption, 0).setCursor()
  20459. }
  20460. },
  20461. }
  20462. UE.commands['deletecaption'] = {
  20463. queryCommandState: function () {
  20464. var rng = this.selection.getRange(),
  20465. table = domUtils.findParentByTagName(rng.startContainer, 'table')
  20466. if (table) {
  20467. return table.getElementsByTagName('caption').length == 0 ? -1 : 1
  20468. }
  20469. return -1
  20470. },
  20471. execCommand: function () {
  20472. var rng = this.selection.getRange(),
  20473. table = domUtils.findParentByTagName(rng.startContainer, 'table')
  20474. if (table) {
  20475. domUtils.remove(table.getElementsByTagName('caption')[0])
  20476. var range = this.selection.getRange()
  20477. range.setStart(table.rows[0].cells[0], 0).setCursor()
  20478. }
  20479. },
  20480. }
  20481. UE.commands['inserttitle'] = {
  20482. queryCommandState: function () {
  20483. var table = getTableItemsByRange(this).table
  20484. if (table) {
  20485. var firstRow = table.rows[0]
  20486. return firstRow.cells[
  20487. firstRow.cells.length - 1
  20488. ].tagName.toLowerCase() != 'th'
  20489. ? 0
  20490. : -1
  20491. }
  20492. return -1
  20493. },
  20494. execCommand: function () {
  20495. var table = getTableItemsByRange(this).table
  20496. if (table) {
  20497. getUETable(table).insertRow(0, 'th')
  20498. }
  20499. var th = table.getElementsByTagName('th')[0]
  20500. this.selection.getRange().setStart(th, 0).setCursor(false, true)
  20501. },
  20502. }
  20503. UE.commands['deletetitle'] = {
  20504. queryCommandState: function () {
  20505. var table = getTableItemsByRange(this).table
  20506. if (table) {
  20507. var firstRow = table.rows[0]
  20508. return firstRow.cells[
  20509. firstRow.cells.length - 1
  20510. ].tagName.toLowerCase() == 'th'
  20511. ? 0
  20512. : -1
  20513. }
  20514. return -1
  20515. },
  20516. execCommand: function () {
  20517. var table = getTableItemsByRange(this).table
  20518. if (table) {
  20519. domUtils.remove(table.rows[0])
  20520. }
  20521. var td = table.getElementsByTagName('td')[0]
  20522. this.selection.getRange().setStart(td, 0).setCursor(false, true)
  20523. },
  20524. }
  20525. UE.commands['inserttitlecol'] = {
  20526. queryCommandState: function () {
  20527. var table = getTableItemsByRange(this).table
  20528. if (table) {
  20529. var lastRow = table.rows[table.rows.length - 1]
  20530. return lastRow.getElementsByTagName('th').length ? -1 : 0
  20531. }
  20532. return -1
  20533. },
  20534. execCommand: function (cmd) {
  20535. var table = getTableItemsByRange(this).table
  20536. if (table) {
  20537. getUETable(table).insertCol(0, 'th')
  20538. }
  20539. resetTdWidth(table, this)
  20540. var th = table.getElementsByTagName('th')[0]
  20541. this.selection.getRange().setStart(th, 0).setCursor(false, true)
  20542. },
  20543. }
  20544. UE.commands['deletetitlecol'] = {
  20545. queryCommandState: function () {
  20546. var table = getTableItemsByRange(this).table
  20547. if (table) {
  20548. var lastRow = table.rows[table.rows.length - 1]
  20549. return lastRow.getElementsByTagName('th').length ? 0 : -1
  20550. }
  20551. return -1
  20552. },
  20553. execCommand: function () {
  20554. var table = getTableItemsByRange(this).table
  20555. if (table) {
  20556. for (var i = 0; i < table.rows.length; i++) {
  20557. domUtils.remove(table.rows[i].children[0])
  20558. }
  20559. }
  20560. resetTdWidth(table, this)
  20561. var td = table.getElementsByTagName('td')[0]
  20562. this.selection.getRange().setStart(td, 0).setCursor(false, true)
  20563. },
  20564. }
  20565. UE.commands['mergeright'] = {
  20566. queryCommandState: function (cmd) {
  20567. var tableItems = getTableItemsByRange(this),
  20568. table = tableItems.table,
  20569. cell = tableItems.cell
  20570. if (!table || !cell) return -1
  20571. var ut = getUETable(table)
  20572. if (ut.selectedTds.length) return -1
  20573. var cellInfo = ut.getCellInfo(cell),
  20574. rightColIndex = cellInfo.colIndex + cellInfo.colSpan
  20575. if (rightColIndex >= ut.colsNum) return -1 // 如果处于最右边则不能向右合并
  20576. var rightCellInfo = ut.indexTable[cellInfo.rowIndex][rightColIndex],
  20577. rightCell =
  20578. table.rows[rightCellInfo.rowIndex].cells[rightCellInfo.cellIndex]
  20579. if (!rightCell || cell.tagName != rightCell.tagName) return -1 // TH和TD不能相互合并
  20580. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20581. return rightCellInfo.rowIndex == cellInfo.rowIndex &&
  20582. rightCellInfo.rowSpan == cellInfo.rowSpan
  20583. ? 0
  20584. : -1
  20585. },
  20586. execCommand: function (cmd) {
  20587. var rng = this.selection.getRange(),
  20588. bk = rng.createBookmark(true)
  20589. var cell = getTableItemsByRange(this).cell,
  20590. ut = getUETable(cell)
  20591. ut.mergeRight(cell)
  20592. rng.moveToBookmark(bk).select()
  20593. },
  20594. }
  20595. UE.commands['mergedown'] = {
  20596. queryCommandState: function (cmd) {
  20597. var tableItems = getTableItemsByRange(this),
  20598. table = tableItems.table,
  20599. cell = tableItems.cell
  20600. if (!table || !cell) return -1
  20601. var ut = getUETable(table)
  20602. if (ut.selectedTds.length) return -1
  20603. var cellInfo = ut.getCellInfo(cell),
  20604. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan
  20605. if (downRowIndex >= ut.rowsNum) return -1 // 如果处于最下边则不能向下合并
  20606. var downCellInfo = ut.indexTable[downRowIndex][cellInfo.colIndex],
  20607. downCell =
  20608. table.rows[downCellInfo.rowIndex].cells[downCellInfo.cellIndex]
  20609. if (!downCell || cell.tagName != downCell.tagName) return -1 // TH和TD不能相互合并
  20610. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20611. return downCellInfo.colIndex == cellInfo.colIndex &&
  20612. downCellInfo.colSpan == cellInfo.colSpan
  20613. ? 0
  20614. : -1
  20615. },
  20616. execCommand: function () {
  20617. var rng = this.selection.getRange(),
  20618. bk = rng.createBookmark(true)
  20619. var cell = getTableItemsByRange(this).cell,
  20620. ut = getUETable(cell)
  20621. ut.mergeDown(cell)
  20622. rng.moveToBookmark(bk).select()
  20623. },
  20624. }
  20625. UE.commands['mergecells'] = {
  20626. queryCommandState: function () {
  20627. return getUETableBySelected(this) ? 0 : -1
  20628. },
  20629. execCommand: function () {
  20630. var ut = getUETableBySelected(this)
  20631. if (ut && ut.selectedTds.length) {
  20632. var cell = ut.selectedTds[0]
  20633. ut.mergeRange()
  20634. var rng = this.selection.getRange()
  20635. if (domUtils.isEmptyBlock(cell)) {
  20636. rng.setStart(cell, 0).collapse(true)
  20637. } else {
  20638. rng.selectNodeContents(cell)
  20639. }
  20640. rng.select()
  20641. }
  20642. },
  20643. }
  20644. UE.commands['insertrow'] = {
  20645. queryCommandState: function () {
  20646. var tableItems = getTableItemsByRange(this),
  20647. cell = tableItems.cell
  20648. return cell &&
  20649. (cell.tagName == 'TD' ||
  20650. (cell.tagName == 'TH' &&
  20651. tableItems.tr !== tableItems.table.rows[0])) &&
  20652. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20653. ? 0
  20654. : -1
  20655. },
  20656. execCommand: function () {
  20657. var rng = this.selection.getRange(),
  20658. bk = rng.createBookmark(true)
  20659. var tableItems = getTableItemsByRange(this),
  20660. cell = tableItems.cell,
  20661. table = tableItems.table,
  20662. ut = getUETable(table),
  20663. cellInfo = ut.getCellInfo(cell)
  20664. //ut.insertRow(!ut.selectedTds.length ? cellInfo.rowIndex:ut.cellsRange.beginRowIndex,'');
  20665. if (!ut.selectedTds.length) {
  20666. ut.insertRow(cellInfo.rowIndex, cell)
  20667. } else {
  20668. var range = ut.cellsRange
  20669. for (
  20670. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20671. i < len;
  20672. i++
  20673. ) {
  20674. ut.insertRow(range.beginRowIndex, cell)
  20675. }
  20676. }
  20677. rng.moveToBookmark(bk).select()
  20678. if (table.getAttribute('interlaced') === 'enabled')
  20679. this.fireEvent('interlacetable', table)
  20680. },
  20681. }
  20682. //后插入行
  20683. UE.commands['insertrownext'] = {
  20684. queryCommandState: function () {
  20685. var tableItems = getTableItemsByRange(this),
  20686. cell = tableItems.cell
  20687. return cell &&
  20688. cell.tagName == 'TD' &&
  20689. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20690. ? 0
  20691. : -1
  20692. },
  20693. execCommand: function () {
  20694. var rng = this.selection.getRange(),
  20695. bk = rng.createBookmark(true)
  20696. var tableItems = getTableItemsByRange(this),
  20697. cell = tableItems.cell,
  20698. table = tableItems.table,
  20699. ut = getUETable(table),
  20700. cellInfo = ut.getCellInfo(cell)
  20701. //ut.insertRow(!ut.selectedTds.length? cellInfo.rowIndex + cellInfo.rowSpan : ut.cellsRange.endRowIndex + 1,'');
  20702. if (!ut.selectedTds.length) {
  20703. ut.insertRow(cellInfo.rowIndex + cellInfo.rowSpan, cell)
  20704. } else {
  20705. var range = ut.cellsRange
  20706. for (
  20707. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20708. i < len;
  20709. i++
  20710. ) {
  20711. ut.insertRow(range.endRowIndex + 1, cell)
  20712. }
  20713. }
  20714. rng.moveToBookmark(bk).select()
  20715. if (table.getAttribute('interlaced') === 'enabled')
  20716. this.fireEvent('interlacetable', table)
  20717. },
  20718. }
  20719. UE.commands['deleterow'] = {
  20720. queryCommandState: function () {
  20721. var tableItems = getTableItemsByRange(this)
  20722. return tableItems.cell ? 0 : -1
  20723. },
  20724. execCommand: function () {
  20725. var cell = getTableItemsByRange(this).cell,
  20726. ut = getUETable(cell),
  20727. cellsRange = ut.cellsRange,
  20728. cellInfo = ut.getCellInfo(cell),
  20729. preCell = ut.getVSideCell(cell),
  20730. nextCell = ut.getVSideCell(cell, true),
  20731. rng = this.selection.getRange()
  20732. if (utils.isEmptyObject(cellsRange)) {
  20733. ut.deleteRow(cellInfo.rowIndex)
  20734. } else {
  20735. for (
  20736. var i = cellsRange.beginRowIndex;
  20737. i < cellsRange.endRowIndex + 1;
  20738. i++
  20739. ) {
  20740. ut.deleteRow(cellsRange.beginRowIndex)
  20741. }
  20742. }
  20743. var table = ut.table
  20744. if (!table.getElementsByTagName('td').length) {
  20745. var nextSibling = table.nextSibling
  20746. domUtils.remove(table)
  20747. if (nextSibling) {
  20748. rng.setStart(nextSibling, 0).setCursor(false, true)
  20749. }
  20750. } else {
  20751. if (
  20752. cellInfo.rowSpan == 1 ||
  20753. cellInfo.rowSpan ==
  20754. cellsRange.endRowIndex - cellsRange.beginRowIndex + 1
  20755. ) {
  20756. if (nextCell || preCell)
  20757. rng.selectNodeContents(nextCell || preCell).setCursor(false, true)
  20758. } else {
  20759. var newCell = ut.getCell(
  20760. cellInfo.rowIndex,
  20761. ut.indexTable[cellInfo.rowIndex][cellInfo.colIndex].cellIndex
  20762. )
  20763. if (newCell) rng.selectNodeContents(newCell).setCursor(false, true)
  20764. }
  20765. }
  20766. if (table.getAttribute('interlaced') === 'enabled')
  20767. this.fireEvent('interlacetable', table)
  20768. },
  20769. }
  20770. UE.commands['insertcol'] = {
  20771. queryCommandState: function (cmd) {
  20772. var tableItems = getTableItemsByRange(this),
  20773. cell = tableItems.cell
  20774. return cell &&
  20775. (cell.tagName == 'TD' ||
  20776. (cell.tagName == 'TH' && cell !== tableItems.tr.cells[0])) &&
  20777. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20778. ? 0
  20779. : -1
  20780. },
  20781. execCommand: function (cmd) {
  20782. var rng = this.selection.getRange(),
  20783. bk = rng.createBookmark(true)
  20784. if (this.queryCommandState(cmd) == -1) return
  20785. var cell = getTableItemsByRange(this).cell,
  20786. ut = getUETable(cell),
  20787. cellInfo = ut.getCellInfo(cell)
  20788. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex:ut.cellsRange.beginColIndex);
  20789. if (!ut.selectedTds.length) {
  20790. ut.insertCol(cellInfo.colIndex, cell)
  20791. } else {
  20792. var range = ut.cellsRange
  20793. for (
  20794. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20795. i < len;
  20796. i++
  20797. ) {
  20798. ut.insertCol(range.beginColIndex, cell)
  20799. }
  20800. }
  20801. rng.moveToBookmark(bk).select(true)
  20802. },
  20803. }
  20804. UE.commands['insertcolnext'] = {
  20805. queryCommandState: function () {
  20806. var tableItems = getTableItemsByRange(this),
  20807. cell = tableItems.cell
  20808. return cell &&
  20809. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20810. ? 0
  20811. : -1
  20812. },
  20813. execCommand: function () {
  20814. var rng = this.selection.getRange(),
  20815. bk = rng.createBookmark(true)
  20816. var cell = getTableItemsByRange(this).cell,
  20817. ut = getUETable(cell),
  20818. cellInfo = ut.getCellInfo(cell)
  20819. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex + cellInfo.colSpan:ut.cellsRange.endColIndex +1);
  20820. if (!ut.selectedTds.length) {
  20821. ut.insertCol(cellInfo.colIndex + cellInfo.colSpan, cell)
  20822. } else {
  20823. var range = ut.cellsRange
  20824. for (
  20825. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20826. i < len;
  20827. i++
  20828. ) {
  20829. ut.insertCol(range.endColIndex + 1, cell)
  20830. }
  20831. }
  20832. rng.moveToBookmark(bk).select()
  20833. },
  20834. }
  20835. UE.commands['deletecol'] = {
  20836. queryCommandState: function () {
  20837. var tableItems = getTableItemsByRange(this)
  20838. return tableItems.cell ? 0 : -1
  20839. },
  20840. execCommand: function () {
  20841. var cell = getTableItemsByRange(this).cell,
  20842. ut = getUETable(cell),
  20843. range = ut.cellsRange,
  20844. cellInfo = ut.getCellInfo(cell),
  20845. preCell = ut.getHSideCell(cell),
  20846. nextCell = ut.getHSideCell(cell, true)
  20847. if (utils.isEmptyObject(range)) {
  20848. ut.deleteCol(cellInfo.colIndex)
  20849. } else {
  20850. for (var i = range.beginColIndex; i < range.endColIndex + 1; i++) {
  20851. ut.deleteCol(range.beginColIndex)
  20852. }
  20853. }
  20854. var table = ut.table,
  20855. rng = this.selection.getRange()
  20856. if (!table.getElementsByTagName('td').length) {
  20857. var nextSibling = table.nextSibling
  20858. domUtils.remove(table)
  20859. if (nextSibling) {
  20860. rng.setStart(nextSibling, 0).setCursor(false, true)
  20861. }
  20862. } else {
  20863. if (domUtils.inDoc(cell, this.document)) {
  20864. rng.setStart(cell, 0).setCursor(false, true)
  20865. } else {
  20866. if (nextCell && domUtils.inDoc(nextCell, this.document)) {
  20867. rng.selectNodeContents(nextCell).setCursor(false, true)
  20868. } else {
  20869. if (preCell && domUtils.inDoc(preCell, this.document)) {
  20870. rng.selectNodeContents(preCell).setCursor(true, true)
  20871. }
  20872. }
  20873. }
  20874. }
  20875. },
  20876. }
  20877. UE.commands['splittocells'] = {
  20878. queryCommandState: function () {
  20879. var tableItems = getTableItemsByRange(this),
  20880. cell = tableItems.cell
  20881. if (!cell) return -1
  20882. var ut = getUETable(tableItems.table)
  20883. if (ut.selectedTds.length > 0) return -1
  20884. return cell && (cell.colSpan > 1 || cell.rowSpan > 1) ? 0 : -1
  20885. },
  20886. execCommand: function () {
  20887. var rng = this.selection.getRange(),
  20888. bk = rng.createBookmark(true)
  20889. var cell = getTableItemsByRange(this).cell,
  20890. ut = getUETable(cell)
  20891. ut.splitToCells(cell)
  20892. rng.moveToBookmark(bk).select()
  20893. },
  20894. }
  20895. UE.commands['splittorows'] = {
  20896. queryCommandState: function () {
  20897. var tableItems = getTableItemsByRange(this),
  20898. cell = tableItems.cell
  20899. if (!cell) return -1
  20900. var ut = getUETable(tableItems.table)
  20901. if (ut.selectedTds.length > 0) return -1
  20902. return cell && cell.rowSpan > 1 ? 0 : -1
  20903. },
  20904. execCommand: function () {
  20905. var rng = this.selection.getRange(),
  20906. bk = rng.createBookmark(true)
  20907. var cell = getTableItemsByRange(this).cell,
  20908. ut = getUETable(cell)
  20909. ut.splitToRows(cell)
  20910. rng.moveToBookmark(bk).select()
  20911. },
  20912. }
  20913. UE.commands['splittocols'] = {
  20914. queryCommandState: function () {
  20915. var tableItems = getTableItemsByRange(this),
  20916. cell = tableItems.cell
  20917. if (!cell) return -1
  20918. var ut = getUETable(tableItems.table)
  20919. if (ut.selectedTds.length > 0) return -1
  20920. return cell && cell.colSpan > 1 ? 0 : -1
  20921. },
  20922. execCommand: function () {
  20923. var rng = this.selection.getRange(),
  20924. bk = rng.createBookmark(true)
  20925. var cell = getTableItemsByRange(this).cell,
  20926. ut = getUETable(cell)
  20927. ut.splitToCols(cell)
  20928. rng.moveToBookmark(bk).select()
  20929. },
  20930. }
  20931. UE.commands['adaptbytext'] = UE.commands['adaptbywindow'] = {
  20932. queryCommandState: function () {
  20933. return getTableItemsByRange(this).table ? 0 : -1
  20934. },
  20935. execCommand: function (cmd) {
  20936. var tableItems = getTableItemsByRange(this),
  20937. table = tableItems.table
  20938. if (table) {
  20939. if (cmd == 'adaptbywindow') {
  20940. resetTdWidth(table, this)
  20941. } else {
  20942. var cells = domUtils.getElementsByTagName(table, 'td th')
  20943. utils.each(cells, function (cell) {
  20944. cell.removeAttribute('width')
  20945. })
  20946. table.removeAttribute('width')
  20947. }
  20948. }
  20949. },
  20950. }
  20951. //平均分配各列
  20952. UE.commands['averagedistributecol'] = {
  20953. queryCommandState: function () {
  20954. var ut = getUETableBySelected(this)
  20955. if (!ut) return -1
  20956. return ut.isFullRow() || ut.isFullCol() ? 0 : -1
  20957. },
  20958. execCommand: function (cmd) {
  20959. var me = this,
  20960. ut = getUETableBySelected(me)
  20961. function getAverageWidth() {
  20962. var tb = ut.table,
  20963. averageWidth,
  20964. sumWidth = 0,
  20965. colsNum = 0,
  20966. tbAttr = getDefaultValue(me, tb)
  20967. if (ut.isFullRow()) {
  20968. sumWidth = tb.offsetWidth
  20969. colsNum = ut.colsNum
  20970. } else {
  20971. var begin = ut.cellsRange.beginColIndex,
  20972. end = ut.cellsRange.endColIndex,
  20973. node
  20974. for (var i = begin; i <= end; ) {
  20975. node = ut.selectedTds[i]
  20976. sumWidth += node.offsetWidth
  20977. i += node.colSpan
  20978. colsNum += 1
  20979. }
  20980. }
  20981. averageWidth =
  20982. Math.ceil(sumWidth / colsNum) -
  20983. tbAttr.tdBorder * 2 -
  20984. tbAttr.tdPadding * 2
  20985. return averageWidth
  20986. }
  20987. function setAverageWidth(averageWidth) {
  20988. utils.each(
  20989. domUtils.getElementsByTagName(ut.table, 'th'),
  20990. function (node) {
  20991. node.setAttribute('width', '')
  20992. }
  20993. )
  20994. var cells = ut.isFullRow()
  20995. ? domUtils.getElementsByTagName(ut.table, 'td')
  20996. : ut.selectedTds
  20997. utils.each(cells, function (node) {
  20998. if (node.colSpan == 1) {
  20999. node.setAttribute('width', averageWidth)
  21000. }
  21001. })
  21002. }
  21003. if (ut && ut.selectedTds.length) {
  21004. setAverageWidth(getAverageWidth())
  21005. }
  21006. },
  21007. }
  21008. //平均分配各行
  21009. UE.commands['averagedistributerow'] = {
  21010. queryCommandState: function () {
  21011. var ut = getUETableBySelected(this)
  21012. if (!ut) return -1
  21013. if (ut.selectedTds && /th/gi.test(ut.selectedTds[0].tagName)) return -1
  21014. return ut.isFullRow() || ut.isFullCol() ? 0 : -1
  21015. },
  21016. execCommand: function (cmd) {
  21017. var me = this,
  21018. ut = getUETableBySelected(me)
  21019. function getAverageHeight() {
  21020. var averageHeight,
  21021. rowNum,
  21022. sumHeight = 0,
  21023. tb = ut.table,
  21024. tbAttr = getDefaultValue(me, tb),
  21025. tdpadding = parseInt(
  21026. domUtils.getComputedStyle(
  21027. tb.getElementsByTagName('td')[0],
  21028. 'padding-top'
  21029. )
  21030. )
  21031. if (ut.isFullCol()) {
  21032. var captionArr = domUtils.getElementsByTagName(tb, 'caption'),
  21033. thArr = domUtils.getElementsByTagName(tb, 'th'),
  21034. captionHeight,
  21035. thHeight
  21036. if (captionArr.length > 0) {
  21037. captionHeight = captionArr[0].offsetHeight
  21038. }
  21039. if (thArr.length > 0) {
  21040. thHeight = thArr[0].offsetHeight
  21041. }
  21042. sumHeight = tb.offsetHeight - (captionHeight || 0) - (thHeight || 0)
  21043. rowNum = thArr.length == 0 ? ut.rowsNum : ut.rowsNum - 1
  21044. } else {
  21045. var begin = ut.cellsRange.beginRowIndex,
  21046. end = ut.cellsRange.endRowIndex,
  21047. count = 0,
  21048. trs = domUtils.getElementsByTagName(tb, 'tr')
  21049. for (var i = begin; i <= end; i++) {
  21050. sumHeight += trs[i].offsetHeight
  21051. count += 1
  21052. }
  21053. rowNum = count
  21054. }
  21055. //ie8下是混杂模式
  21056. if (browser.ie && browser.version < 9) {
  21057. averageHeight = Math.ceil(sumHeight / rowNum)
  21058. } else {
  21059. averageHeight =
  21060. Math.ceil(sumHeight / rowNum) -
  21061. tbAttr.tdBorder * 2 -
  21062. tdpadding * 2
  21063. }
  21064. return averageHeight
  21065. }
  21066. function setAverageHeight(averageHeight) {
  21067. var cells = ut.isFullCol()
  21068. ? domUtils.getElementsByTagName(ut.table, 'td')
  21069. : ut.selectedTds
  21070. utils.each(cells, function (node) {
  21071. if (node.rowSpan == 1) {
  21072. node.setAttribute('height', averageHeight)
  21073. }
  21074. })
  21075. }
  21076. if (ut && ut.selectedTds.length) {
  21077. setAverageHeight(getAverageHeight())
  21078. }
  21079. },
  21080. }
  21081. //单元格对齐方式
  21082. UE.commands['cellalignment'] = {
  21083. queryCommandState: function () {
  21084. return getTableItemsByRange(this).table ? 0 : -1
  21085. },
  21086. execCommand: function (cmd, data) {
  21087. var me = this,
  21088. ut = getUETableBySelected(me)
  21089. if (!ut) {
  21090. var start = me.selection.getStart(),
  21091. cell =
  21092. start &&
  21093. domUtils.findParentByTagName(start, ['td', 'th', 'caption'], true)
  21094. if (!/caption/gi.test(cell.tagName)) {
  21095. domUtils.setAttributes(cell, data)
  21096. } else {
  21097. cell.style.textAlign = data.align
  21098. cell.style.verticalAlign = data.vAlign
  21099. }
  21100. me.selection.getRange().setCursor(true)
  21101. } else {
  21102. utils.each(ut.selectedTds, function (cell) {
  21103. domUtils.setAttributes(cell, data)
  21104. })
  21105. }
  21106. },
  21107. /**
  21108. * 查询当前点击的单元格的对齐状态, 如果当前已经选择了多个单元格, 则会返回所有单元格经过统一协调过后的状态
  21109. * @see UE.UETable.getTableCellAlignState
  21110. */
  21111. queryCommandValue: function (cmd) {
  21112. var activeMenuCell = getTableItemsByRange(this).cell
  21113. if (!activeMenuCell) {
  21114. activeMenuCell = getSelectedArr(this)[0]
  21115. }
  21116. if (!activeMenuCell) {
  21117. return null
  21118. } else {
  21119. //获取同时选中的其他单元格
  21120. var cells = UE.UETable.getUETable(activeMenuCell).selectedTds
  21121. !cells.length && (cells = activeMenuCell)
  21122. return UE.UETable.getTableCellAlignState(cells)
  21123. }
  21124. },
  21125. }
  21126. //表格对齐方式
  21127. UE.commands['tablealignment'] = {
  21128. queryCommandState: function () {
  21129. if (browser.ie && browser.version < 8) {
  21130. return -1
  21131. }
  21132. return getTableItemsByRange(this).table ? 0 : -1
  21133. },
  21134. execCommand: function (cmd, value) {
  21135. var me = this,
  21136. start = me.selection.getStart(),
  21137. table = start && domUtils.findParentByTagName(start, ['table'], true)
  21138. if (table) {
  21139. table.setAttribute('align', value)
  21140. }
  21141. },
  21142. }
  21143. //表格属性
  21144. UE.commands['edittable'] = {
  21145. queryCommandState: function () {
  21146. return getTableItemsByRange(this).table ? 0 : -1
  21147. },
  21148. execCommand: function (cmd, color) {
  21149. var rng = this.selection.getRange(),
  21150. table = domUtils.findParentByTagName(rng.startContainer, 'table')
  21151. if (table) {
  21152. var arr = domUtils
  21153. .getElementsByTagName(table, 'td')
  21154. .concat(
  21155. domUtils.getElementsByTagName(table, 'th'),
  21156. domUtils.getElementsByTagName(table, 'caption')
  21157. )
  21158. utils.each(arr, function (node) {
  21159. node.style.borderColor = color
  21160. })
  21161. }
  21162. },
  21163. }
  21164. //单元格属性
  21165. UE.commands['edittd'] = {
  21166. queryCommandState: function () {
  21167. return getTableItemsByRange(this).table ? 0 : -1
  21168. },
  21169. execCommand: function (cmd, bkColor) {
  21170. var me = this,
  21171. ut = getUETableBySelected(me)
  21172. if (!ut) {
  21173. var start = me.selection.getStart(),
  21174. cell =
  21175. start &&
  21176. domUtils.findParentByTagName(start, ['td', 'th', 'caption'], true)
  21177. if (cell) {
  21178. cell.style.backgroundColor = bkColor
  21179. }
  21180. } else {
  21181. utils.each(ut.selectedTds, function (cell) {
  21182. cell.style.backgroundColor = bkColor
  21183. })
  21184. }
  21185. },
  21186. }
  21187. UE.commands['settablebackground'] = {
  21188. queryCommandState: function () {
  21189. return getSelectedArr(this).length > 1 ? 0 : -1
  21190. },
  21191. execCommand: function (cmd, value) {
  21192. var cells, ut
  21193. cells = getSelectedArr(this)
  21194. ut = getUETable(cells[0])
  21195. ut.setBackground(cells, value)
  21196. },
  21197. }
  21198. UE.commands['cleartablebackground'] = {
  21199. queryCommandState: function () {
  21200. var cells = getSelectedArr(this)
  21201. if (!cells.length) return -1
  21202. for (var i = 0, cell; (cell = cells[i++]); ) {
  21203. if (cell.style.backgroundColor !== '') return 0
  21204. }
  21205. return -1
  21206. },
  21207. execCommand: function () {
  21208. var cells = getSelectedArr(this),
  21209. ut = getUETable(cells[0])
  21210. ut.removeBackground(cells)
  21211. },
  21212. }
  21213. UE.commands['interlacetable'] = UE.commands['uninterlacetable'] = {
  21214. queryCommandState: function (cmd) {
  21215. var table = getTableItemsByRange(this).table
  21216. if (!table) return -1
  21217. var interlaced = table.getAttribute('interlaced')
  21218. if (cmd == 'interlacetable') {
  21219. //TODO 待定
  21220. //是否需要待定,如果设置,则命令只能单次执行成功,但反射具备toggle效果;否则可以覆盖前次命令,但反射将不存在toggle效果
  21221. return interlaced === 'enabled' ? -1 : 0
  21222. } else {
  21223. return !interlaced || interlaced === 'disabled' ? -1 : 0
  21224. }
  21225. },
  21226. execCommand: function (cmd, classList) {
  21227. var table = getTableItemsByRange(this).table
  21228. if (cmd == 'interlacetable') {
  21229. table.setAttribute('interlaced', 'enabled')
  21230. this.fireEvent('interlacetable', table, classList)
  21231. } else {
  21232. table.setAttribute('interlaced', 'disabled')
  21233. this.fireEvent('uninterlacetable', table)
  21234. }
  21235. },
  21236. }
  21237. UE.commands['setbordervisible'] = {
  21238. queryCommandState: function (cmd) {
  21239. var table = getTableItemsByRange(this).table
  21240. if (!table) return -1
  21241. return 0
  21242. },
  21243. execCommand: function () {
  21244. var table = getTableItemsByRange(this).table
  21245. utils.each(domUtils.getElementsByTagName(table, 'td'), function (td) {
  21246. td.style.borderWidth = '1px'
  21247. td.style.borderStyle = 'solid'
  21248. })
  21249. },
  21250. }
  21251. function resetTdWidth(table, editor) {
  21252. var tds = domUtils.getElementsByTagName(table, 'td th')
  21253. utils.each(tds, function (td) {
  21254. td.removeAttribute('width')
  21255. })
  21256. table.setAttribute(
  21257. 'width',
  21258. getTableWidth(editor, true, getDefaultValue(editor, table))
  21259. )
  21260. var tdsWidths = []
  21261. setTimeout(function () {
  21262. utils.each(tds, function (td) {
  21263. td.colSpan == 1 && tdsWidths.push(td.offsetWidth)
  21264. })
  21265. utils.each(tds, function (td, i) {
  21266. td.colSpan == 1 && td.setAttribute('width', tdsWidths[i] + '')
  21267. })
  21268. }, 0)
  21269. }
  21270. function getTableWidth(editor, needIEHack, defaultValue) {
  21271. var body = editor.body
  21272. return (
  21273. body.offsetWidth -
  21274. (needIEHack
  21275. ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2
  21276. : 0) -
  21277. defaultValue.tableBorder * 2 -
  21278. (editor.options.offsetWidth || 0)
  21279. )
  21280. }
  21281. function getSelectedArr(editor) {
  21282. var cell = getTableItemsByRange(editor).cell
  21283. if (cell) {
  21284. var ut = getUETable(cell)
  21285. return ut.selectedTds.length ? ut.selectedTds : [cell]
  21286. } else {
  21287. return []
  21288. }
  21289. }
  21290. })()
  21291. // plugins/table.action.js
  21292. /**
  21293. * Created with JetBrains PhpStorm.
  21294. * User: taoqili
  21295. * Date: 12-10-12
  21296. * Time: 上午10:05
  21297. * To change this template use File | Settings | File Templates.
  21298. */
  21299. UE.plugins['table'] = function () {
  21300. var me = this,
  21301. tabTimer = null,
  21302. //拖动计时器
  21303. tableDragTimer = null,
  21304. //双击计时器
  21305. tableResizeTimer = null,
  21306. //单元格最小宽度
  21307. cellMinWidth = 5,
  21308. isInResizeBuffer = false,
  21309. //单元格边框大小
  21310. cellBorderWidth = 5,
  21311. //鼠标偏移距离
  21312. offsetOfTableCell = 10,
  21313. //记录在有限时间内的点击状态, 共有3个取值, 0, 1, 2。 0代表未初始化, 1代表单击了1次,2代表2次
  21314. singleClickState = 0,
  21315. userActionStatus = null,
  21316. //双击允许的时间范围
  21317. dblclickTime = 360,
  21318. UT = UE.UETable,
  21319. getUETable = function (tdOrTable) {
  21320. return UT.getUETable(tdOrTable)
  21321. },
  21322. getUETableBySelected = function (editor) {
  21323. return UT.getUETableBySelected(editor)
  21324. },
  21325. getDefaultValue = function (editor, table) {
  21326. return UT.getDefaultValue(editor, table)
  21327. },
  21328. removeSelectedClass = function (cells) {
  21329. return UT.removeSelectedClass(cells)
  21330. }
  21331. function showError(e) {
  21332. // throw e;
  21333. }
  21334. me.ready(function () {
  21335. var me = this
  21336. var orgGetText = me.selection.getText
  21337. me.selection.getText = function () {
  21338. var table = getUETableBySelected(me)
  21339. if (table) {
  21340. var str = ''
  21341. utils.each(table.selectedTds, function (td) {
  21342. str += td[browser.ie ? 'innerText' : 'textContent']
  21343. })
  21344. return str
  21345. } else {
  21346. return orgGetText.call(me.selection)
  21347. }
  21348. }
  21349. })
  21350. //处理拖动及框选相关方法
  21351. var startTd = null, //鼠标按下时的锚点td
  21352. currentTd = null, //当前鼠标经过时的td
  21353. onDrag = '', //指示当前拖动状态,其值可为"","h","v" ,分别表示未拖动状态,横向拖动状态,纵向拖动状态,用于鼠标移动过程中的判断
  21354. onBorder = false, //检测鼠标按下时是否处在单元格边缘位置
  21355. dragButton = null,
  21356. dragOver = false,
  21357. dragLine = null, //模拟的拖动线
  21358. dragTd = null //发生拖动的目标td
  21359. var mousedown = false,
  21360. //todo 判断混乱模式
  21361. needIEHack = true
  21362. me.setOpt({
  21363. maxColNum: 20,
  21364. maxRowNum: 100,
  21365. defaultCols: 5,
  21366. defaultRows: 5,
  21367. tdvalign: 'top',
  21368. cursorpath: me.options.UEDITOR_HOME_URL + 'themes/default/images/cursor_',
  21369. tableDragable: false,
  21370. classList: [
  21371. 'ue-table-interlace-color-single',
  21372. 'ue-table-interlace-color-double',
  21373. ],
  21374. })
  21375. me.getUETable = getUETable
  21376. var commands = {
  21377. deletetable: 1,
  21378. inserttable: 1,
  21379. cellvalign: 1,
  21380. insertcaption: 1,
  21381. deletecaption: 1,
  21382. inserttitle: 1,
  21383. deletetitle: 1,
  21384. mergeright: 1,
  21385. mergedown: 1,
  21386. mergecells: 1,
  21387. insertrow: 1,
  21388. insertrownext: 1,
  21389. deleterow: 1,
  21390. insertcol: 1,
  21391. insertcolnext: 1,
  21392. deletecol: 1,
  21393. splittocells: 1,
  21394. splittorows: 1,
  21395. splittocols: 1,
  21396. adaptbytext: 1,
  21397. adaptbywindow: 1,
  21398. adaptbycustomer: 1,
  21399. insertparagraph: 1,
  21400. insertparagraphbeforetable: 1,
  21401. averagedistributecol: 1,
  21402. averagedistributerow: 1,
  21403. }
  21404. me.ready(function () {
  21405. utils.cssRule(
  21406. 'table',
  21407. //选中的td上的样式
  21408. '.selectTdClass{background-color:#edf5fa !important}' +
  21409. 'table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #ddd !important}' +
  21410. //插入的表格的默认样式
  21411. 'table{margin-bottom:10px;border-collapse:collapse;display:table;}' +
  21412. 'td,th{padding: 5px 10px;border: 1px solid #DDD;}' +
  21413. 'caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}' +
  21414. 'th{border-top:1px solid #BBB;background-color:#F7F7F7;}' +
  21415. 'table tr.firstRow th{border-top-width:2px;}' +
  21416. '.ue-table-interlace-color-single{ background-color: #fcfcfc; } .ue-table-interlace-color-double{ background-color: #f7faff; }' +
  21417. 'td p{margin:0;padding:0;}',
  21418. me.document
  21419. )
  21420. var tableCopyList, isFullCol, isFullRow
  21421. //注册del/backspace事件
  21422. me.addListener('keydown', function (cmd, evt) {
  21423. var me = this
  21424. var keyCode = evt.keyCode || evt.which
  21425. if (keyCode == 8) {
  21426. var ut = getUETableBySelected(me)
  21427. if (ut && ut.selectedTds.length) {
  21428. if (ut.isFullCol()) {
  21429. me.execCommand('deletecol')
  21430. } else if (ut.isFullRow()) {
  21431. me.execCommand('deleterow')
  21432. } else {
  21433. me.fireEvent('delcells')
  21434. }
  21435. domUtils.preventDefault(evt)
  21436. }
  21437. var caption = domUtils.findParentByTagName(
  21438. me.selection.getStart(),
  21439. 'caption',
  21440. true
  21441. ),
  21442. range = me.selection.getRange()
  21443. if (range.collapsed && caption && isEmptyBlock(caption)) {
  21444. me.fireEvent('saveScene')
  21445. var table = caption.parentNode
  21446. domUtils.remove(caption)
  21447. if (table) {
  21448. range.setStart(table.rows[0].cells[0], 0).setCursor(false, true)
  21449. }
  21450. me.fireEvent('saveScene')
  21451. }
  21452. }
  21453. if (keyCode == 46) {
  21454. ut = getUETableBySelected(me)
  21455. if (ut) {
  21456. me.fireEvent('saveScene')
  21457. for (var i = 0, ci; (ci = ut.selectedTds[i++]); ) {
  21458. domUtils.fillNode(me.document, ci)
  21459. }
  21460. me.fireEvent('saveScene')
  21461. domUtils.preventDefault(evt)
  21462. }
  21463. }
  21464. if (keyCode == 13) {
  21465. var rng = me.selection.getRange(),
  21466. caption = domUtils.findParentByTagName(
  21467. rng.startContainer,
  21468. 'caption',
  21469. true
  21470. )
  21471. if (caption) {
  21472. var table = domUtils.findParentByTagName(caption, 'table')
  21473. if (!rng.collapsed) {
  21474. rng.deleteContents()
  21475. me.fireEvent('saveScene')
  21476. } else {
  21477. if (caption) {
  21478. rng.setStart(table.rows[0].cells[0], 0).setCursor(false, true)
  21479. }
  21480. }
  21481. domUtils.preventDefault(evt)
  21482. return
  21483. }
  21484. if (rng.collapsed) {
  21485. var table = domUtils.findParentByTagName(
  21486. rng.startContainer,
  21487. 'table'
  21488. )
  21489. if (table) {
  21490. var cell = table.rows[0].cells[0],
  21491. start = domUtils.findParentByTagName(
  21492. me.selection.getStart(),
  21493. ['td', 'th'],
  21494. true
  21495. ),
  21496. preNode = table.previousSibling
  21497. if (
  21498. cell === start &&
  21499. (!preNode ||
  21500. (preNode.nodeType == 1 && preNode.tagName == 'TABLE')) &&
  21501. domUtils.isStartInblock(rng)
  21502. ) {
  21503. var first = domUtils.findParent(
  21504. me.selection.getStart(),
  21505. function (n) {
  21506. return domUtils.isBlockElm(n)
  21507. },
  21508. true
  21509. )
  21510. if (
  21511. first &&
  21512. (/t(h|d)/i.test(first.tagName) || first === start.firstChild)
  21513. ) {
  21514. me.execCommand('insertparagraphbeforetable')
  21515. domUtils.preventDefault(evt)
  21516. }
  21517. }
  21518. }
  21519. }
  21520. }
  21521. if ((evt.ctrlKey || evt.metaKey) && evt.keyCode == '67') {
  21522. tableCopyList = null
  21523. var ut = getUETableBySelected(me)
  21524. if (ut) {
  21525. var tds = ut.selectedTds
  21526. isFullCol = ut.isFullCol()
  21527. isFullRow = ut.isFullRow()
  21528. tableCopyList = [[ut.cloneCell(tds[0], null, true)]]
  21529. for (var i = 1, ci; (ci = tds[i]); i++) {
  21530. if (ci.parentNode !== tds[i - 1].parentNode) {
  21531. tableCopyList.push([ut.cloneCell(ci, null, true)])
  21532. } else {
  21533. tableCopyList[tableCopyList.length - 1].push(
  21534. ut.cloneCell(ci, null, true)
  21535. )
  21536. }
  21537. }
  21538. }
  21539. }
  21540. })
  21541. me.addListener('tablehasdeleted', function () {
  21542. toggleDraggableState(this, false, '', null)
  21543. if (dragButton) domUtils.remove(dragButton)
  21544. })
  21545. me.addListener('beforepaste', function (cmd, html) {
  21546. var me = this
  21547. var rng = me.selection.getRange()
  21548. if (domUtils.findParentByTagName(rng.startContainer, 'caption', true)) {
  21549. var div = me.document.createElement('div')
  21550. div.innerHTML = html.html
  21551. //trace:3729
  21552. html.html = div[browser.ie9below ? 'innerText' : 'textContent']
  21553. return
  21554. }
  21555. var table = getUETableBySelected(me)
  21556. if (tableCopyList) {
  21557. me.fireEvent('saveScene')
  21558. var rng = me.selection.getRange()
  21559. var td = domUtils.findParentByTagName(
  21560. rng.startContainer,
  21561. ['td', 'th'],
  21562. true
  21563. ),
  21564. tmpNode,
  21565. preNode
  21566. if (td) {
  21567. var ut = getUETable(td)
  21568. if (isFullRow) {
  21569. var rowIndex = ut.getCellInfo(td).rowIndex
  21570. if (td.tagName == 'TH') {
  21571. rowIndex++
  21572. }
  21573. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21574. var tr = ut.insertRow(rowIndex++, 'td')
  21575. for (var j = 0, cj; (cj = ci[j]); j++) {
  21576. var cell = tr.cells[j]
  21577. if (!cell) {
  21578. cell = tr.insertCell(j)
  21579. }
  21580. cell.innerHTML = cj.innerHTML
  21581. cj.getAttribute('width') &&
  21582. cell.setAttribute('width', cj.getAttribute('width'))
  21583. cj.getAttribute('vAlign') &&
  21584. cell.setAttribute('vAlign', cj.getAttribute('vAlign'))
  21585. cj.getAttribute('align') &&
  21586. cell.setAttribute('align', cj.getAttribute('align'))
  21587. cj.style.cssText && (cell.style.cssText = cj.style.cssText)
  21588. }
  21589. for (var j = 0, cj; (cj = tr.cells[j]); j++) {
  21590. if (!ci[j]) break
  21591. cj.innerHTML = ci[j].innerHTML
  21592. ci[j].getAttribute('width') &&
  21593. cj.setAttribute('width', ci[j].getAttribute('width'))
  21594. ci[j].getAttribute('vAlign') &&
  21595. cj.setAttribute('vAlign', ci[j].getAttribute('vAlign'))
  21596. ci[j].getAttribute('align') &&
  21597. cj.setAttribute('align', ci[j].getAttribute('align'))
  21598. ci[j].style.cssText &&
  21599. (cj.style.cssText = ci[j].style.cssText)
  21600. }
  21601. }
  21602. } else {
  21603. if (isFullCol) {
  21604. cellInfo = ut.getCellInfo(td)
  21605. var maxColNum = 0
  21606. for (var j = 0, ci = tableCopyList[0], cj; (cj = ci[j++]); ) {
  21607. maxColNum += cj.colSpan || 1
  21608. }
  21609. me.__hasEnterExecCommand = true
  21610. for (i = 0; i < maxColNum; i++) {
  21611. me.execCommand('insertcol')
  21612. }
  21613. me.__hasEnterExecCommand = false
  21614. td = ut.table.rows[0].cells[cellInfo.cellIndex]
  21615. if (td.tagName == 'TH') {
  21616. td = ut.table.rows[1].cells[cellInfo.cellIndex]
  21617. }
  21618. }
  21619. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21620. tmpNode = td
  21621. for (var j = 0, cj; (cj = ci[j++]); ) {
  21622. if (td) {
  21623. td.innerHTML = cj.innerHTML
  21624. //todo 定制处理
  21625. cj.getAttribute('width') &&
  21626. td.setAttribute('width', cj.getAttribute('width'))
  21627. cj.getAttribute('vAlign') &&
  21628. td.setAttribute('vAlign', cj.getAttribute('vAlign'))
  21629. cj.getAttribute('align') &&
  21630. td.setAttribute('align', cj.getAttribute('align'))
  21631. cj.style.cssText && (td.style.cssText = cj.style.cssText)
  21632. preNode = td
  21633. td = td.nextSibling
  21634. } else {
  21635. var cloneTd = cj.cloneNode(true)
  21636. domUtils.removeAttributes(cloneTd, [
  21637. 'class',
  21638. 'rowSpan',
  21639. 'colSpan',
  21640. ])
  21641. preNode.parentNode.appendChild(cloneTd)
  21642. }
  21643. }
  21644. td = ut.getNextCell(tmpNode, true, true)
  21645. if (!tableCopyList[i]) break
  21646. if (!td) {
  21647. var cellInfo = ut.getCellInfo(tmpNode)
  21648. ut.table.insertRow(ut.table.rows.length)
  21649. ut.update()
  21650. td = ut.getVSideCell(tmpNode, true)
  21651. }
  21652. }
  21653. }
  21654. ut.update()
  21655. } else {
  21656. table = me.document.createElement('table')
  21657. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21658. var tr = table.insertRow(table.rows.length)
  21659. for (var j = 0, cj; (cj = ci[j++]); ) {
  21660. cloneTd = UT.cloneCell(cj, null, true)
  21661. domUtils.removeAttributes(cloneTd, ['class'])
  21662. tr.appendChild(cloneTd)
  21663. }
  21664. if (j == 2 && cloneTd.rowSpan > 1) {
  21665. cloneTd.rowSpan = 1
  21666. }
  21667. }
  21668. var defaultValue = getDefaultValue(me),
  21669. width =
  21670. me.body.offsetWidth -
  21671. (needIEHack
  21672. ? parseInt(
  21673. domUtils.getComputedStyle(me.body, 'margin-left'),
  21674. 10
  21675. ) * 2
  21676. : 0) -
  21677. defaultValue.tableBorder * 2 -
  21678. (me.options.offsetWidth || 0)
  21679. me.execCommand(
  21680. 'insertHTML',
  21681. '<table ' +
  21682. (isFullCol && isFullRow ? 'width="' + width + '"' : '') +
  21683. '>' +
  21684. table.innerHTML
  21685. .replace(/>\s*</g, '><')
  21686. .replace(/\bth\b/gi, 'td') +
  21687. '</table>'
  21688. )
  21689. }
  21690. me.fireEvent('contentchange')
  21691. me.fireEvent('saveScene')
  21692. html.html = ''
  21693. return true
  21694. } else {
  21695. var div = me.document.createElement('div'),
  21696. tables
  21697. div.innerHTML = html.html
  21698. tables = div.getElementsByTagName('table')
  21699. if (domUtils.findParentByTagName(me.selection.getStart(), 'table')) {
  21700. utils.each(tables, function (t) {
  21701. domUtils.remove(t)
  21702. })
  21703. if (
  21704. domUtils.findParentByTagName(
  21705. me.selection.getStart(),
  21706. 'caption',
  21707. true
  21708. )
  21709. ) {
  21710. div.innerHTML = div[browser.ie ? 'innerText' : 'textContent']
  21711. }
  21712. } else {
  21713. utils.each(tables, function (table) {
  21714. removeStyleSize(table, true)
  21715. domUtils.removeAttributes(table, ['style', 'border'])
  21716. utils.each(
  21717. domUtils.getElementsByTagName(table, 'td'),
  21718. function (td) {
  21719. if (isEmptyBlock(td)) {
  21720. domUtils.fillNode(me.document, td)
  21721. }
  21722. removeStyleSize(td, true)
  21723. // domUtils.removeAttributes(td, ['style'])
  21724. }
  21725. )
  21726. })
  21727. }
  21728. html.html = div.innerHTML
  21729. }
  21730. })
  21731. me.addListener('afterpaste', function () {
  21732. utils.each(
  21733. domUtils.getElementsByTagName(me.body, 'table'),
  21734. function (table) {
  21735. if (table.offsetWidth > me.body.offsetWidth) {
  21736. var defaultValue = getDefaultValue(me, table)
  21737. table.style.width =
  21738. me.body.offsetWidth -
  21739. (needIEHack
  21740. ? parseInt(
  21741. domUtils.getComputedStyle(me.body, 'margin-left'),
  21742. 10
  21743. ) * 2
  21744. : 0) -
  21745. defaultValue.tableBorder * 2 -
  21746. (me.options.offsetWidth || 0) +
  21747. 'px'
  21748. }
  21749. }
  21750. )
  21751. })
  21752. me.addListener('blur', function () {
  21753. tableCopyList = null
  21754. })
  21755. var timer
  21756. me.addListener('keydown', function () {
  21757. clearTimeout(timer)
  21758. timer = setTimeout(function () {
  21759. var rng = me.selection.getRange(),
  21760. cell = domUtils.findParentByTagName(
  21761. rng.startContainer,
  21762. ['th', 'td'],
  21763. true
  21764. )
  21765. if (cell) {
  21766. var table = cell.parentNode.parentNode.parentNode
  21767. if (table.offsetWidth > table.getAttribute('width')) {
  21768. cell.style.wordBreak = 'break-all'
  21769. }
  21770. }
  21771. }, 100)
  21772. })
  21773. me.addListener('selectionchange', function () {
  21774. toggleDraggableState(me, false, '', null)
  21775. })
  21776. //内容变化时触发索引更新
  21777. //todo 可否考虑标记检测,如果不涉及表格的变化就不进行索引重建和更新
  21778. me.addListener('contentchange', function () {
  21779. var me = this
  21780. //尽可能排除一些不需要更新的状况
  21781. hideDragLine(me)
  21782. if (getUETableBySelected(me)) return
  21783. var rng = me.selection.getRange()
  21784. var start = rng.startContainer
  21785. start = domUtils.findParentByTagName(start, ['td', 'th'], true)
  21786. utils.each(
  21787. domUtils.getElementsByTagName(me.document, 'table'),
  21788. function (table) {
  21789. if (me.fireEvent('excludetable', table) === true) return
  21790. table.ueTable = new UT(table)
  21791. //trace:3742
  21792. // utils.each(domUtils.getElementsByTagName(me.document, 'td'), function (td) {
  21793. //
  21794. // if (domUtils.isEmptyBlock(td) && td !== start) {
  21795. // domUtils.fillNode(me.document, td);
  21796. // if (browser.ie && browser.version == 6) {
  21797. // td.innerHTML = '&nbsp;'
  21798. // }
  21799. // }
  21800. // });
  21801. // utils.each(domUtils.getElementsByTagName(me.document, 'th'), function (th) {
  21802. // if (domUtils.isEmptyBlock(th) && th !== start) {
  21803. // domUtils.fillNode(me.document, th);
  21804. // if (browser.ie && browser.version == 6) {
  21805. // th.innerHTML = '&nbsp;'
  21806. // }
  21807. // }
  21808. // });
  21809. table.onmouseover = function () {
  21810. me.fireEvent('tablemouseover', table)
  21811. }
  21812. table.onmousemove = function () {
  21813. me.fireEvent('tablemousemove', table)
  21814. me.options.tableDragable && toggleDragButton(true, this, me)
  21815. utils.defer(function () {
  21816. me.fireEvent('contentchange', 50)
  21817. }, true)
  21818. }
  21819. table.onmouseout = function () {
  21820. me.fireEvent('tablemouseout', table)
  21821. toggleDraggableState(me, false, '', null)
  21822. hideDragLine(me)
  21823. }
  21824. table.onclick = function (evt) {
  21825. evt = me.window.event || evt
  21826. var target = getParentTdOrTh(evt.target || evt.srcElement)
  21827. if (!target) return
  21828. var ut = getUETable(target),
  21829. table = ut.table,
  21830. cellInfo = ut.getCellInfo(target),
  21831. cellsRange,
  21832. rng = me.selection.getRange()
  21833. // if ("topLeft" == inPosition(table, mouseCoords(evt))) {
  21834. // cellsRange = ut.getCellsRange(ut.table.rows[0].cells[0], ut.getLastCell());
  21835. // ut.setSelected(cellsRange);
  21836. // return;
  21837. // }
  21838. // if ("bottomRight" == inPosition(table, mouseCoords(evt))) {
  21839. //
  21840. // return;
  21841. // }
  21842. if (inTableSide(table, target, evt, true)) {
  21843. var endTdCol = ut.getCell(
  21844. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].rowIndex,
  21845. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].cellIndex
  21846. )
  21847. if (evt.shiftKey && ut.selectedTds.length) {
  21848. if (ut.selectedTds[0] !== endTdCol) {
  21849. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdCol)
  21850. ut.setSelected(cellsRange)
  21851. } else {
  21852. rng && rng.selectNodeContents(endTdCol).select()
  21853. }
  21854. } else {
  21855. if (target !== endTdCol) {
  21856. cellsRange = ut.getCellsRange(target, endTdCol)
  21857. ut.setSelected(cellsRange)
  21858. } else {
  21859. rng && rng.selectNodeContents(endTdCol).select()
  21860. }
  21861. }
  21862. return
  21863. }
  21864. if (inTableSide(table, target, evt)) {
  21865. var endTdRow = ut.getCell(
  21866. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].rowIndex,
  21867. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].cellIndex
  21868. )
  21869. if (evt.shiftKey && ut.selectedTds.length) {
  21870. if (ut.selectedTds[0] !== endTdRow) {
  21871. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdRow)
  21872. ut.setSelected(cellsRange)
  21873. } else {
  21874. rng && rng.selectNodeContents(endTdRow).select()
  21875. }
  21876. } else {
  21877. if (target !== endTdRow) {
  21878. cellsRange = ut.getCellsRange(target, endTdRow)
  21879. ut.setSelected(cellsRange)
  21880. } else {
  21881. rng && rng.selectNodeContents(endTdRow).select()
  21882. }
  21883. }
  21884. }
  21885. }
  21886. }
  21887. )
  21888. switchBorderColor(me, true)
  21889. })
  21890. domUtils.on(me.document, 'mousemove', mouseMoveEvent)
  21891. domUtils.on(me.document, 'mouseout', function (evt) {
  21892. var target = evt.target || evt.srcElement
  21893. if (target.tagName == 'TABLE') {
  21894. toggleDraggableState(me, false, '', null)
  21895. }
  21896. })
  21897. /**
  21898. * 表格隔行变色
  21899. */
  21900. me.addListener('interlacetable', function (type, table, classList) {
  21901. if (!table) return
  21902. var me = this,
  21903. rows = table.rows,
  21904. len = rows.length,
  21905. getClass = function (list, index, repeat) {
  21906. return list[index]
  21907. ? list[index]
  21908. : repeat
  21909. ? list[index % list.length]
  21910. : ''
  21911. }
  21912. for (var i = 0; i < len; i++) {
  21913. rows[i].className = getClass(
  21914. classList || me.options.classList,
  21915. i,
  21916. true
  21917. )
  21918. }
  21919. })
  21920. me.addListener('uninterlacetable', function (type, table) {
  21921. if (!table) return
  21922. var me = this,
  21923. rows = table.rows,
  21924. classList = me.options.classList,
  21925. len = rows.length
  21926. for (var i = 0; i < len; i++) {
  21927. domUtils.removeClasses(rows[i], classList)
  21928. }
  21929. })
  21930. me.addListener('mousedown', mouseDownEvent)
  21931. me.addListener('mouseup', mouseUpEvent)
  21932. //拖动的时候触发mouseup
  21933. domUtils.on(me.body, 'dragstart', function (evt) {
  21934. mouseUpEvent.call(me, 'dragstart', evt)
  21935. })
  21936. me.addOutputRule(function (root) {
  21937. utils.each(root.getNodesByTagName('div'), function (n) {
  21938. if (n.getAttr('id') == 'ue_tableDragLine') {
  21939. n.parentNode.removeChild(n)
  21940. }
  21941. })
  21942. })
  21943. var currentRowIndex = 0
  21944. me.addListener('mousedown', function () {
  21945. currentRowIndex = 0
  21946. })
  21947. me.addListener('tabkeydown', function () {
  21948. var range = this.selection.getRange(),
  21949. common = range.getCommonAncestor(true, true),
  21950. table = domUtils.findParentByTagName(common, 'table')
  21951. if (table) {
  21952. if (domUtils.findParentByTagName(common, 'caption', true)) {
  21953. var cell = domUtils.getElementsByTagName(table, 'th td')
  21954. if (cell && cell.length) {
  21955. range.setStart(cell[0], 0).setCursor(false, true)
  21956. }
  21957. } else {
  21958. var cell = domUtils.findParentByTagName(common, ['td', 'th'], true),
  21959. ua = getUETable(cell)
  21960. currentRowIndex =
  21961. cell.rowSpan > 1 ? currentRowIndex : ua.getCellInfo(cell).rowIndex
  21962. var nextCell = ua.getTabNextCell(cell, currentRowIndex)
  21963. if (nextCell) {
  21964. if (isEmptyBlock(nextCell)) {
  21965. range.setStart(nextCell, 0).setCursor(false, true)
  21966. } else {
  21967. range.selectNodeContents(nextCell).select()
  21968. }
  21969. } else {
  21970. me.fireEvent('saveScene')
  21971. me.__hasEnterExecCommand = true
  21972. this.execCommand('insertrownext')
  21973. me.__hasEnterExecCommand = false
  21974. range = this.selection.getRange()
  21975. range
  21976. .setStart(table.rows[table.rows.length - 1].cells[0], 0)
  21977. .setCursor()
  21978. me.fireEvent('saveScene')
  21979. }
  21980. }
  21981. return true
  21982. }
  21983. })
  21984. browser.ie &&
  21985. me.addListener('selectionchange', function () {
  21986. toggleDraggableState(this, false, '', null)
  21987. })
  21988. me.addListener('keydown', function (type, evt) {
  21989. var me = this
  21990. //处理在表格的最后一个输入tab产生新的表格
  21991. var keyCode = evt.keyCode || evt.which
  21992. if (keyCode == 8 || keyCode == 46) {
  21993. return
  21994. }
  21995. var notCtrlKey =
  21996. !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey
  21997. notCtrlKey &&
  21998. removeSelectedClass(domUtils.getElementsByTagName(me.body, 'td'))
  21999. var ut = getUETableBySelected(me)
  22000. if (!ut) return
  22001. notCtrlKey && ut.clearSelected()
  22002. })
  22003. me.addListener('beforegetcontent', function () {
  22004. switchBorderColor(this, false)
  22005. browser.ie &&
  22006. utils.each(
  22007. this.document.getElementsByTagName('caption'),
  22008. function (ci) {
  22009. if (domUtils.isEmptyNode(ci)) {
  22010. ci.innerHTML = '&nbsp;'
  22011. }
  22012. }
  22013. )
  22014. })
  22015. me.addListener('aftergetcontent', function () {
  22016. switchBorderColor(this, true)
  22017. })
  22018. me.addListener('getAllHtml', function () {
  22019. removeSelectedClass(me.document.getElementsByTagName('td'))
  22020. })
  22021. //修正全屏状态下插入的表格宽度在非全屏状态下撑开编辑器的情况
  22022. me.addListener('fullscreenchanged', function (type, fullscreen) {
  22023. if (!fullscreen) {
  22024. var ratio = this.body.offsetWidth / document.body.offsetWidth,
  22025. tables = domUtils.getElementsByTagName(this.body, 'table')
  22026. utils.each(tables, function (table) {
  22027. if (table.offsetWidth < me.body.offsetWidth) return false
  22028. var tds = domUtils.getElementsByTagName(table, 'td'),
  22029. backWidths = []
  22030. utils.each(tds, function (td) {
  22031. backWidths.push(td.offsetWidth)
  22032. })
  22033. for (var i = 0, td; (td = tds[i]); i++) {
  22034. td.setAttribute('width', Math.floor(backWidths[i] * ratio))
  22035. }
  22036. table.setAttribute(
  22037. 'width',
  22038. Math.floor(getTableWidth(me, needIEHack, getDefaultValue(me)))
  22039. )
  22040. })
  22041. }
  22042. })
  22043. //重写execCommand命令,用于处理框选时的处理
  22044. var oldExecCommand = me.execCommand
  22045. me.execCommand = function (cmd, datatat) {
  22046. var me = this,
  22047. args = arguments
  22048. cmd = cmd.toLowerCase()
  22049. var ut = getUETableBySelected(me),
  22050. tds,
  22051. range = new dom.Range(me.document),
  22052. cmdFun = me.commands[cmd] || UE.commands[cmd],
  22053. result
  22054. if (!cmdFun) return
  22055. if (
  22056. ut &&
  22057. !commands[cmd] &&
  22058. !cmdFun.notNeedUndo &&
  22059. !me.__hasEnterExecCommand
  22060. ) {
  22061. me.__hasEnterExecCommand = true
  22062. me.fireEvent('beforeexeccommand', cmd)
  22063. tds = ut.selectedTds
  22064. var lastState = -2,
  22065. lastValue = -2,
  22066. value,
  22067. state
  22068. for (var i = 0, td; (td = tds[i]); i++) {
  22069. if (isEmptyBlock(td)) {
  22070. range.setStart(td, 0).setCursor(false, true)
  22071. } else {
  22072. range.selectNode(td).select(true)
  22073. }
  22074. state = me.queryCommandState(cmd)
  22075. value = me.queryCommandValue(cmd)
  22076. if (state != -1) {
  22077. if (lastState !== state || lastValue !== value) {
  22078. me._ignoreContentChange = true
  22079. result = oldExecCommand.apply(me, arguments)
  22080. me._ignoreContentChange = false
  22081. }
  22082. lastState = me.queryCommandState(cmd)
  22083. lastValue = me.queryCommandValue(cmd)
  22084. if (domUtils.isEmptyBlock(td)) {
  22085. domUtils.fillNode(me.document, td)
  22086. }
  22087. }
  22088. }
  22089. range.setStart(tds[0], 0).shrinkBoundary(true).setCursor(false, true)
  22090. me.fireEvent('contentchange')
  22091. me.fireEvent('afterexeccommand', cmd)
  22092. me.__hasEnterExecCommand = false
  22093. me._selectionChange()
  22094. } else {
  22095. result = oldExecCommand.apply(me, arguments)
  22096. }
  22097. return result
  22098. }
  22099. })
  22100. /**
  22101. * 删除obj的宽高style,改成属性宽高
  22102. * @param obj
  22103. * @param replaceToProperty
  22104. */
  22105. function removeStyleSize(obj, replaceToProperty) {
  22106. removeStyle(obj, 'width', true)
  22107. removeStyle(obj, 'height', true)
  22108. }
  22109. function removeStyle(obj, styleName, replaceToProperty) {
  22110. if (obj.style[styleName]) {
  22111. replaceToProperty &&
  22112. obj.setAttribute(styleName, parseInt(obj.style[styleName], 10))
  22113. obj.style[styleName] = ''
  22114. }
  22115. }
  22116. function getParentTdOrTh(ele) {
  22117. if (ele.tagName == 'TD' || ele.tagName == 'TH') return ele
  22118. var td
  22119. if (
  22120. (td =
  22121. domUtils.findParentByTagName(ele, 'td', true) ||
  22122. domUtils.findParentByTagName(ele, 'th', true))
  22123. )
  22124. return td
  22125. return null
  22126. }
  22127. function isEmptyBlock(node) {
  22128. var reg = new RegExp(domUtils.fillChar, 'g')
  22129. if (
  22130. node[browser.ie ? 'innerText' : 'textContent']
  22131. .replace(/^\s*$/, '')
  22132. .replace(reg, '').length > 0
  22133. ) {
  22134. return 0
  22135. }
  22136. for (var n in dtd.$isNotEmpty) {
  22137. if (node.getElementsByTagName(n).length) {
  22138. return 0
  22139. }
  22140. }
  22141. return 1
  22142. }
  22143. function mouseCoords(evt) {
  22144. if (evt.pageX || evt.pageY) {
  22145. return { x: evt.pageX, y: evt.pageY }
  22146. }
  22147. return {
  22148. x:
  22149. evt.clientX +
  22150. me.document.body.scrollLeft -
  22151. me.document.body.clientLeft,
  22152. y:
  22153. evt.clientY + me.document.body.scrollTop - me.document.body.clientTop,
  22154. }
  22155. }
  22156. function mouseMoveEvent(evt) {
  22157. if (isEditorDisabled()) {
  22158. return
  22159. }
  22160. try {
  22161. //普通状态下鼠标移动
  22162. var target = getParentTdOrTh(evt.target || evt.srcElement),
  22163. pos
  22164. //区分用户的行为是拖动还是双击
  22165. if (isInResizeBuffer) {
  22166. me.body.style.webkitUserSelect = 'none'
  22167. if (
  22168. Math.abs(userActionStatus.x - evt.clientX) > offsetOfTableCell ||
  22169. Math.abs(userActionStatus.y - evt.clientY) > offsetOfTableCell
  22170. ) {
  22171. clearTableDragTimer()
  22172. isInResizeBuffer = false
  22173. singleClickState = 0
  22174. //drag action
  22175. tableBorderDrag(evt)
  22176. }
  22177. }
  22178. //修改单元格大小时的鼠标移动
  22179. if (onDrag && dragTd) {
  22180. singleClickState = 0
  22181. me.body.style.webkitUserSelect = 'none'
  22182. me.selection
  22183. .getNative()
  22184. [browser.ie9below ? 'empty' : 'removeAllRanges']()
  22185. pos = mouseCoords(evt)
  22186. toggleDraggableState(me, true, onDrag, pos, target)
  22187. if (onDrag == 'h') {
  22188. dragLine.style.left = getPermissionX(dragTd, evt) + 'px'
  22189. } else if (onDrag == 'v') {
  22190. dragLine.style.top = getPermissionY(dragTd, evt) + 'px'
  22191. }
  22192. return
  22193. }
  22194. //当鼠标处于table上时,修改移动过程中的光标状态
  22195. if (target) {
  22196. //针对使用table作为容器的组件不触发拖拽效果
  22197. if (me.fireEvent('excludetable', target) === true) return
  22198. pos = mouseCoords(evt)
  22199. var state = getRelation(target, pos),
  22200. table = domUtils.findParentByTagName(target, 'table', true)
  22201. if (inTableSide(table, target, evt, true)) {
  22202. if (me.fireEvent('excludetable', table) === true) return
  22203. me.body.style.cursor =
  22204. 'url(' + me.options.cursorpath + 'h.png),pointer'
  22205. } else if (inTableSide(table, target, evt)) {
  22206. if (me.fireEvent('excludetable', table) === true) return
  22207. me.body.style.cursor =
  22208. 'url(' + me.options.cursorpath + 'v.png),pointer'
  22209. } else {
  22210. me.body.style.cursor = 'text'
  22211. var curCell = target
  22212. if (/\d/.test(state)) {
  22213. state = state.replace(/\d/, '')
  22214. target = getUETable(target).getPreviewCell(target, state == 'v')
  22215. }
  22216. //位于第一行的顶部或者第一列的左边时不可拖动
  22217. toggleDraggableState(
  22218. me,
  22219. target ? !!state : false,
  22220. target ? state : '',
  22221. pos,
  22222. target
  22223. )
  22224. }
  22225. } else {
  22226. toggleDragButton(false, table, me)
  22227. }
  22228. } catch (e) {
  22229. showError(e)
  22230. }
  22231. }
  22232. var dragButtonTimer
  22233. function toggleDragButton(show, table, editor) {
  22234. if (!show) {
  22235. if (dragOver) return
  22236. dragButtonTimer = setTimeout(function () {
  22237. !dragOver &&
  22238. dragButton &&
  22239. dragButton.parentNode &&
  22240. dragButton.parentNode.removeChild(dragButton)
  22241. }, 2000)
  22242. } else {
  22243. createDragButton(table, editor)
  22244. }
  22245. }
  22246. function createDragButton(table, editor) {
  22247. var pos = domUtils.getXY(table),
  22248. doc = table.ownerDocument
  22249. if (dragButton && dragButton.parentNode) return dragButton
  22250. dragButton = doc.createElement('div')
  22251. dragButton.contentEditable = false
  22252. dragButton.innerHTML = ''
  22253. dragButton.style.cssText =
  22254. 'width:15px;height:15px;background-image:url(' +
  22255. editor.options.UEDITOR_HOME_URL +
  22256. 'dialogs/table/dragicon.png);position: absolute;cursor:move;top:' +
  22257. (pos.y - 15) +
  22258. 'px;left:' +
  22259. pos.x +
  22260. 'px;'
  22261. domUtils.unSelectable(dragButton)
  22262. dragButton.onmouseover = function (evt) {
  22263. dragOver = true
  22264. }
  22265. dragButton.onmouseout = function (evt) {
  22266. dragOver = false
  22267. }
  22268. domUtils.on(dragButton, 'click', function (type, evt) {
  22269. doClick(evt, this)
  22270. })
  22271. domUtils.on(dragButton, 'dblclick', function (type, evt) {
  22272. doDblClick(evt)
  22273. })
  22274. domUtils.on(dragButton, 'dragstart', function (type, evt) {
  22275. domUtils.preventDefault(evt)
  22276. })
  22277. var timer
  22278. function doClick(evt, button) {
  22279. // 部分浏览器下需要清理
  22280. clearTimeout(timer)
  22281. timer = setTimeout(function () {
  22282. editor.fireEvent('tableClicked', table, button)
  22283. }, 300)
  22284. }
  22285. function doDblClick(evt) {
  22286. clearTimeout(timer)
  22287. var ut = getUETable(table),
  22288. start = table.rows[0].cells[0],
  22289. end = ut.getLastCell(),
  22290. range = ut.getCellsRange(start, end)
  22291. editor.selection.getRange().setStart(start, 0).setCursor(false, true)
  22292. ut.setSelected(range)
  22293. }
  22294. doc.body.appendChild(dragButton)
  22295. }
  22296. // function inPosition(table, pos) {
  22297. // var tablePos = domUtils.getXY(table),
  22298. // width = table.offsetWidth,
  22299. // height = table.offsetHeight;
  22300. // if (pos.x - tablePos.x < 5 && pos.y - tablePos.y < 5) {
  22301. // return "topLeft";
  22302. // } else if (tablePos.x + width - pos.x < 5 && tablePos.y + height - pos.y < 5) {
  22303. // return "bottomRight";
  22304. // }
  22305. // }
  22306. function inTableSide(table, cell, evt, top) {
  22307. var pos = mouseCoords(evt),
  22308. state = getRelation(cell, pos)
  22309. if (top) {
  22310. var caption = table.getElementsByTagName('caption')[0],
  22311. capHeight = caption ? caption.offsetHeight : 0
  22312. return state == 'v1' && pos.y - domUtils.getXY(table).y - capHeight < 8
  22313. } else {
  22314. return state == 'h1' && pos.x - domUtils.getXY(table).x < 8
  22315. }
  22316. }
  22317. /**
  22318. * 获取拖动时允许的X轴坐标
  22319. * @param dragTd
  22320. * @param evt
  22321. */
  22322. function getPermissionX(dragTd, evt) {
  22323. var ut = getUETable(dragTd)
  22324. if (ut) {
  22325. var preTd = ut.getSameEndPosCells(dragTd, 'x')[0],
  22326. nextTd = ut.getSameStartPosXCells(dragTd)[0],
  22327. mouseX = mouseCoords(evt).x,
  22328. left =
  22329. (preTd ? domUtils.getXY(preTd).x : domUtils.getXY(ut.table).x) + 20,
  22330. right = nextTd
  22331. ? domUtils.getXY(nextTd).x + nextTd.offsetWidth - 20
  22332. : me.body.offsetWidth + 5 ||
  22333. parseInt(domUtils.getComputedStyle(me.body, 'width'), 10)
  22334. left += cellMinWidth
  22335. right -= cellMinWidth
  22336. return mouseX < left ? left : mouseX > right ? right : mouseX
  22337. }
  22338. }
  22339. /**
  22340. * 获取拖动时允许的Y轴坐标
  22341. */
  22342. function getPermissionY(dragTd, evt) {
  22343. try {
  22344. var top = domUtils.getXY(dragTd).y,
  22345. mousePosY = mouseCoords(evt).y
  22346. return mousePosY < top ? top : mousePosY
  22347. } catch (e) {
  22348. showError(e)
  22349. }
  22350. }
  22351. /**
  22352. * 移动状态切换
  22353. */
  22354. function toggleDraggableState(editor, draggable, dir, mousePos, cell) {
  22355. try {
  22356. editor.body.style.cursor =
  22357. dir == 'h' ? 'col-resize' : dir == 'v' ? 'row-resize' : 'text'
  22358. if (browser.ie) {
  22359. if (dir && !mousedown && !getUETableBySelected(editor)) {
  22360. getDragLine(editor, editor.document)
  22361. showDragLineAt(dir, cell)
  22362. } else {
  22363. hideDragLine(editor)
  22364. }
  22365. }
  22366. onBorder = draggable
  22367. } catch (e) {
  22368. showError(e)
  22369. }
  22370. }
  22371. /**
  22372. * 获取与UETable相关的resize line
  22373. * @param uetable UETable对象
  22374. */
  22375. function getResizeLineByUETable() {
  22376. var lineId = '_UETableResizeLine',
  22377. line = this.document.getElementById(lineId)
  22378. if (!line) {
  22379. line = this.document.createElement('div')
  22380. line.id = lineId
  22381. line.contnetEditable = false
  22382. line.setAttribute('unselectable', 'on')
  22383. var styles = {
  22384. width: 2 * cellBorderWidth + 1 + 'px',
  22385. position: 'absolute',
  22386. 'z-index': 100000,
  22387. cursor: 'col-resize',
  22388. background: 'red',
  22389. display: 'none',
  22390. }
  22391. //切换状态
  22392. line.onmouseout = function () {
  22393. this.style.display = 'none'
  22394. }
  22395. utils.extend(line.style, styles)
  22396. this.document.body.appendChild(line)
  22397. }
  22398. return line
  22399. }
  22400. /**
  22401. * 更新resize-line
  22402. */
  22403. function updateResizeLine(cell, uetable) {
  22404. var line = getResizeLineByUETable.call(this),
  22405. table = uetable.table,
  22406. styles = {
  22407. top: domUtils.getXY(table).y + 'px',
  22408. left:
  22409. domUtils.getXY(cell).x + cell.offsetWidth - cellBorderWidth + 'px',
  22410. display: 'block',
  22411. height: table.offsetHeight + 'px',
  22412. }
  22413. utils.extend(line.style, styles)
  22414. }
  22415. /**
  22416. * 显示resize-line
  22417. */
  22418. function showResizeLine(cell) {
  22419. var uetable = getUETable(cell)
  22420. updateResizeLine.call(this, cell, uetable)
  22421. }
  22422. /**
  22423. * 获取鼠标与当前单元格的相对位置
  22424. * @param ele
  22425. * @param mousePos
  22426. */
  22427. function getRelation(ele, mousePos) {
  22428. var elePos = domUtils.getXY(ele)
  22429. if (!elePos) {
  22430. return ''
  22431. }
  22432. if (elePos.x + ele.offsetWidth - mousePos.x < cellBorderWidth) {
  22433. return 'h'
  22434. }
  22435. if (mousePos.x - elePos.x < cellBorderWidth) {
  22436. return 'h1'
  22437. }
  22438. if (elePos.y + ele.offsetHeight - mousePos.y < cellBorderWidth) {
  22439. return 'v'
  22440. }
  22441. if (mousePos.y - elePos.y < cellBorderWidth) {
  22442. return 'v1'
  22443. }
  22444. return ''
  22445. }
  22446. function mouseDownEvent(type, evt) {
  22447. if (isEditorDisabled()) {
  22448. return
  22449. }
  22450. userActionStatus = {
  22451. x: evt.clientX,
  22452. y: evt.clientY,
  22453. }
  22454. //右键菜单单独处理
  22455. if (evt.button == 2) {
  22456. var ut = getUETableBySelected(me),
  22457. flag = false
  22458. if (ut) {
  22459. var td = getTargetTd(me, evt)
  22460. utils.each(ut.selectedTds, function (ti) {
  22461. if (ti === td) {
  22462. flag = true
  22463. }
  22464. })
  22465. if (!flag) {
  22466. removeSelectedClass(domUtils.getElementsByTagName(me.body, 'th td'))
  22467. ut.clearSelected()
  22468. } else {
  22469. td = ut.selectedTds[0]
  22470. setTimeout(function () {
  22471. me.selection.getRange().setStart(td, 0).setCursor(false, true)
  22472. }, 0)
  22473. }
  22474. }
  22475. } else {
  22476. tableClickHander(evt)
  22477. }
  22478. }
  22479. //清除表格的计时器
  22480. function clearTableTimer() {
  22481. tabTimer && clearTimeout(tabTimer)
  22482. tabTimer = null
  22483. }
  22484. //双击收缩
  22485. function tableDbclickHandler(evt) {
  22486. singleClickState = 0
  22487. evt = evt || me.window.event
  22488. var target = getParentTdOrTh(evt.target || evt.srcElement)
  22489. if (target) {
  22490. var h
  22491. if ((h = getRelation(target, mouseCoords(evt)))) {
  22492. hideDragLine(me)
  22493. if (h == 'h1') {
  22494. h = 'h'
  22495. if (
  22496. inTableSide(
  22497. domUtils.findParentByTagName(target, 'table'),
  22498. target,
  22499. evt
  22500. )
  22501. ) {
  22502. me.execCommand('adaptbywindow')
  22503. } else {
  22504. target = getUETable(target).getPreviewCell(target)
  22505. if (target) {
  22506. var rng = me.selection.getRange()
  22507. rng.selectNodeContents(target).setCursor(true, true)
  22508. }
  22509. }
  22510. }
  22511. if (h == 'h') {
  22512. var ut = getUETable(target),
  22513. table = ut.table,
  22514. cells = getCellsByMoveBorder(target, table, true)
  22515. cells = extractArray(cells, 'left')
  22516. ut.width = ut.offsetWidth
  22517. var oldWidth = [],
  22518. newWidth = []
  22519. utils.each(cells, function (cell) {
  22520. oldWidth.push(cell.offsetWidth)
  22521. })
  22522. utils.each(cells, function (cell) {
  22523. cell.removeAttribute('width')
  22524. })
  22525. window.setTimeout(function () {
  22526. //是否允许改变
  22527. var changeable = true
  22528. utils.each(cells, function (cell, index) {
  22529. var width = cell.offsetWidth
  22530. if (width > oldWidth[index]) {
  22531. changeable = false
  22532. return false
  22533. }
  22534. newWidth.push(width)
  22535. })
  22536. var change = changeable ? newWidth : oldWidth
  22537. utils.each(cells, function (cell, index) {
  22538. cell.width = change[index] - getTabcellSpace()
  22539. })
  22540. }, 0)
  22541. // minWidth -= cellMinWidth;
  22542. //
  22543. // table.removeAttribute("width");
  22544. // utils.each(cells, function (cell) {
  22545. // cell.style.width = "";
  22546. // cell.width -= minWidth;
  22547. // });
  22548. }
  22549. }
  22550. }
  22551. }
  22552. function tableClickHander(evt) {
  22553. removeSelectedClass(domUtils.getElementsByTagName(me.body, 'td th'))
  22554. //trace:3113
  22555. //选中单元格,点击table外部,不会清掉table上挂的ueTable,会引起getUETableBySelected方法返回值
  22556. utils.each(me.document.getElementsByTagName('table'), function (t) {
  22557. t.ueTable = null
  22558. })
  22559. startTd = getTargetTd(me, evt)
  22560. if (!startTd) return
  22561. var table = domUtils.findParentByTagName(startTd, 'table', true)
  22562. ut = getUETable(table)
  22563. ut && ut.clearSelected()
  22564. //判断当前鼠标状态
  22565. if (!onBorder) {
  22566. me.document.body.style.webkitUserSelect = ''
  22567. mousedown = true
  22568. me.addListener('mouseover', mouseOverEvent)
  22569. } else {
  22570. //边框上的动作处理
  22571. borderActionHandler(evt)
  22572. }
  22573. }
  22574. //处理表格边框上的动作, 这里做延时处理,避免两种动作互相影响
  22575. function borderActionHandler(evt) {
  22576. if (browser.ie) {
  22577. evt = reconstruct(evt)
  22578. }
  22579. clearTableDragTimer()
  22580. //是否正在等待resize的缓冲中
  22581. isInResizeBuffer = true
  22582. tableDragTimer = setTimeout(function () {
  22583. tableBorderDrag(evt)
  22584. }, dblclickTime)
  22585. }
  22586. function extractArray(originArr, key) {
  22587. var result = [],
  22588. tmp = null
  22589. for (var i = 0, len = originArr.length; i < len; i++) {
  22590. tmp = originArr[i][key]
  22591. if (tmp) {
  22592. result.push(tmp)
  22593. }
  22594. }
  22595. return result
  22596. }
  22597. function clearTableDragTimer() {
  22598. tableDragTimer && clearTimeout(tableDragTimer)
  22599. tableDragTimer = null
  22600. }
  22601. function reconstruct(obj) {
  22602. var attrs = [
  22603. 'pageX',
  22604. 'pageY',
  22605. 'clientX',
  22606. 'clientY',
  22607. 'srcElement',
  22608. 'target',
  22609. ],
  22610. newObj = {}
  22611. if (obj) {
  22612. for (var i = 0, key, val; (key = attrs[i]); i++) {
  22613. val = obj[key]
  22614. val && (newObj[key] = val)
  22615. }
  22616. }
  22617. return newObj
  22618. }
  22619. //边框拖动
  22620. function tableBorderDrag(evt) {
  22621. isInResizeBuffer = false
  22622. startTd = evt.target || evt.srcElement
  22623. if (!startTd) return
  22624. var state = getRelation(startTd, mouseCoords(evt))
  22625. if (/\d/.test(state)) {
  22626. state = state.replace(/\d/, '')
  22627. startTd = getUETable(startTd).getPreviewCell(startTd, state == 'v')
  22628. }
  22629. hideDragLine(me)
  22630. getDragLine(me, me.document)
  22631. me.fireEvent('saveScene')
  22632. showDragLineAt(state, startTd)
  22633. mousedown = true
  22634. //拖动开始
  22635. onDrag = state
  22636. dragTd = startTd
  22637. }
  22638. function mouseUpEvent(type, evt) {
  22639. if (isEditorDisabled()) {
  22640. return
  22641. }
  22642. clearTableDragTimer()
  22643. isInResizeBuffer = false
  22644. if (onBorder) {
  22645. singleClickState = ++singleClickState % 3
  22646. userActionStatus = {
  22647. x: evt.clientX,
  22648. y: evt.clientY,
  22649. }
  22650. tableResizeTimer = setTimeout(function () {
  22651. singleClickState > 0 && singleClickState--
  22652. }, dblclickTime)
  22653. if (singleClickState === 2) {
  22654. singleClickState = 0
  22655. tableDbclickHandler(evt)
  22656. return
  22657. }
  22658. }
  22659. if (evt.button == 2) return
  22660. var me = this
  22661. //清除表格上原生跨选问题
  22662. var range = me.selection.getRange(),
  22663. start = domUtils.findParentByTagName(
  22664. range.startContainer,
  22665. 'table',
  22666. true
  22667. ),
  22668. end = domUtils.findParentByTagName(range.endContainer, 'table', true)
  22669. if (start || end) {
  22670. if (start === end) {
  22671. start = domUtils.findParentByTagName(
  22672. range.startContainer,
  22673. ['td', 'th', 'caption'],
  22674. true
  22675. )
  22676. end = domUtils.findParentByTagName(
  22677. range.endContainer,
  22678. ['td', 'th', 'caption'],
  22679. true
  22680. )
  22681. if (start !== end) {
  22682. me.selection.clearRange()
  22683. }
  22684. } else {
  22685. me.selection.clearRange()
  22686. }
  22687. }
  22688. mousedown = false
  22689. me.document.body.style.webkitUserSelect = ''
  22690. //拖拽状态下的mouseUP
  22691. if (onDrag && dragTd) {
  22692. me.selection
  22693. .getNative()
  22694. [browser.ie9below ? 'empty' : 'removeAllRanges']()
  22695. singleClickState = 0
  22696. dragLine = me.document.getElementById('ue_tableDragLine')
  22697. // trace 3973
  22698. if (dragLine) {
  22699. var dragTdPos = domUtils.getXY(dragTd),
  22700. dragLinePos = domUtils.getXY(dragLine)
  22701. switch (onDrag) {
  22702. case 'h':
  22703. changeColWidth(dragTd, dragLinePos.x - dragTdPos.x)
  22704. break
  22705. case 'v':
  22706. changeRowHeight(
  22707. dragTd,
  22708. dragLinePos.y - dragTdPos.y - dragTd.offsetHeight
  22709. )
  22710. break
  22711. default:
  22712. }
  22713. onDrag = ''
  22714. dragTd = null
  22715. hideDragLine(me)
  22716. me.fireEvent('saveScene')
  22717. return
  22718. }
  22719. }
  22720. //正常状态下的mouseup
  22721. if (!startTd) {
  22722. var target = domUtils.findParentByTagName(
  22723. evt.target || evt.srcElement,
  22724. 'td',
  22725. true
  22726. )
  22727. if (!target)
  22728. target = domUtils.findParentByTagName(
  22729. evt.target || evt.srcElement,
  22730. 'th',
  22731. true
  22732. )
  22733. if (target && (target.tagName == 'TD' || target.tagName == 'TH')) {
  22734. if (me.fireEvent('excludetable', target) === true) return
  22735. range = new dom.Range(me.document)
  22736. range.setStart(target, 0).setCursor(false, true)
  22737. }
  22738. } else {
  22739. var ut = getUETable(startTd),
  22740. cell = ut ? ut.selectedTds[0] : null
  22741. if (cell) {
  22742. range = new dom.Range(me.document)
  22743. if (domUtils.isEmptyBlock(cell)) {
  22744. range.setStart(cell, 0).setCursor(false, true)
  22745. } else {
  22746. range
  22747. .selectNodeContents(cell)
  22748. .shrinkBoundary()
  22749. .setCursor(false, true)
  22750. }
  22751. } else {
  22752. range = me.selection.getRange().shrinkBoundary()
  22753. if (!range.collapsed) {
  22754. var start = domUtils.findParentByTagName(
  22755. range.startContainer,
  22756. ['td', 'th'],
  22757. true
  22758. ),
  22759. end = domUtils.findParentByTagName(
  22760. range.endContainer,
  22761. ['td', 'th'],
  22762. true
  22763. )
  22764. //在table里边的不能清除
  22765. if (
  22766. (start && !end) ||
  22767. (!start && end) ||
  22768. (start && end && start !== end)
  22769. ) {
  22770. range.setCursor(false, true)
  22771. }
  22772. }
  22773. }
  22774. startTd = null
  22775. me.removeListener('mouseover', mouseOverEvent)
  22776. }
  22777. me._selectionChange(250, evt)
  22778. }
  22779. function mouseOverEvent(type, evt) {
  22780. if (isEditorDisabled()) {
  22781. return
  22782. }
  22783. var me = this,
  22784. tar = evt.target || evt.srcElement
  22785. currentTd =
  22786. domUtils.findParentByTagName(tar, 'td', true) ||
  22787. domUtils.findParentByTagName(tar, 'th', true)
  22788. //需要判断两个TD是否位于同一个表格内
  22789. if (
  22790. startTd &&
  22791. currentTd &&
  22792. ((startTd.tagName == 'TD' && currentTd.tagName == 'TD') ||
  22793. (startTd.tagName == 'TH' && currentTd.tagName == 'TH')) &&
  22794. domUtils.findParentByTagName(startTd, 'table') ==
  22795. domUtils.findParentByTagName(currentTd, 'table')
  22796. ) {
  22797. var ut = getUETable(currentTd)
  22798. if (startTd != currentTd) {
  22799. me.document.body.style.webkitUserSelect = 'none'
  22800. me.selection
  22801. .getNative()
  22802. [browser.ie9below ? 'empty' : 'removeAllRanges']()
  22803. var range = ut.getCellsRange(startTd, currentTd)
  22804. ut.setSelected(range)
  22805. } else {
  22806. me.document.body.style.webkitUserSelect = ''
  22807. ut.clearSelected()
  22808. }
  22809. }
  22810. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false)
  22811. }
  22812. function setCellHeight(cell, height, backHeight) {
  22813. var lineHight = parseInt(
  22814. domUtils.getComputedStyle(cell, 'line-height'),
  22815. 10
  22816. ),
  22817. tmpHeight = backHeight + height
  22818. height = tmpHeight < lineHight ? lineHight : tmpHeight
  22819. if (cell.style.height) cell.style.height = ''
  22820. cell.rowSpan == 1
  22821. ? cell.setAttribute('height', height)
  22822. : cell.removeAttribute && cell.removeAttribute('height')
  22823. }
  22824. function getWidth(cell) {
  22825. if (!cell) return 0
  22826. return parseInt(domUtils.getComputedStyle(cell, 'width'), 10)
  22827. }
  22828. function changeColWidth(cell, changeValue) {
  22829. var ut = getUETable(cell)
  22830. if (ut) {
  22831. //根据当前移动的边框获取相关的单元格
  22832. var table = ut.table,
  22833. cells = getCellsByMoveBorder(cell, table)
  22834. table.style.width = ''
  22835. table.removeAttribute('width')
  22836. //修正改变量
  22837. changeValue = correctChangeValue(changeValue, cell, cells)
  22838. if (cell.nextSibling) {
  22839. var i = 0
  22840. utils.each(cells, function (cellGroup) {
  22841. cellGroup.left.width = +cellGroup.left.width + changeValue
  22842. cellGroup.right &&
  22843. (cellGroup.right.width = +cellGroup.right.width - changeValue)
  22844. })
  22845. } else {
  22846. utils.each(cells, function (cellGroup) {
  22847. cellGroup.left.width -= -changeValue
  22848. })
  22849. }
  22850. }
  22851. }
  22852. function isEditorDisabled() {
  22853. return me.body.contentEditable === 'false'
  22854. }
  22855. function changeRowHeight(td, changeValue) {
  22856. if (Math.abs(changeValue) < 10) return
  22857. var ut = getUETable(td)
  22858. if (ut) {
  22859. var cells = ut.getSameEndPosCells(td, 'y'),
  22860. //备份需要连带变化的td的原始高度,否则后期无法获取正确的值
  22861. backHeight = cells[0] ? cells[0].offsetHeight : 0
  22862. for (var i = 0, cell; (cell = cells[i++]); ) {
  22863. setCellHeight(cell, changeValue, backHeight)
  22864. }
  22865. }
  22866. }
  22867. /**
  22868. * 获取调整单元格大小的相关单元格
  22869. * @isContainMergeCell 返回的结果中是否包含发生合并后的单元格
  22870. */
  22871. function getCellsByMoveBorder(cell, table, isContainMergeCell) {
  22872. if (!table) {
  22873. table = domUtils.findParentByTagName(cell, 'table')
  22874. }
  22875. if (!table) {
  22876. return null
  22877. }
  22878. //获取到该单元格所在行的序列号
  22879. var index = domUtils.getNodeIndex(cell),
  22880. temp = cell,
  22881. rows = table.rows,
  22882. colIndex = 0
  22883. while (temp) {
  22884. //获取到当前单元格在未发生单元格合并时的序列
  22885. if (temp.nodeType === 1) {
  22886. colIndex += temp.colSpan || 1
  22887. }
  22888. temp = temp.previousSibling
  22889. }
  22890. temp = null
  22891. //记录想关的单元格
  22892. var borderCells = []
  22893. utils.each(rows, function (tabRow) {
  22894. var cells = tabRow.cells,
  22895. currIndex = 0
  22896. utils.each(cells, function (tabCell) {
  22897. currIndex += tabCell.colSpan || 1
  22898. if (currIndex === colIndex) {
  22899. borderCells.push({
  22900. left: tabCell,
  22901. right: tabCell.nextSibling || null,
  22902. })
  22903. return false
  22904. } else if (currIndex > colIndex) {
  22905. if (isContainMergeCell) {
  22906. borderCells.push({
  22907. left: tabCell,
  22908. })
  22909. }
  22910. return false
  22911. }
  22912. })
  22913. })
  22914. return borderCells
  22915. }
  22916. /**
  22917. * 通过给定的单元格集合获取最小的单元格width
  22918. */
  22919. function getMinWidthByTableCells(cells) {
  22920. var minWidth = Number.MAX_VALUE
  22921. for (var i = 0, curCell; (curCell = cells[i]); i++) {
  22922. minWidth = Math.min(
  22923. minWidth,
  22924. curCell.width || getTableCellWidth(curCell)
  22925. )
  22926. }
  22927. return minWidth
  22928. }
  22929. function correctChangeValue(changeValue, relatedCell, cells) {
  22930. //为单元格的paading预留空间
  22931. changeValue -= getTabcellSpace()
  22932. if (changeValue < 0) {
  22933. return 0
  22934. }
  22935. changeValue -= getTableCellWidth(relatedCell)
  22936. //确定方向
  22937. var direction = changeValue < 0 ? 'left' : 'right'
  22938. changeValue = Math.abs(changeValue)
  22939. //只关心非最后一个单元格就可以
  22940. utils.each(cells, function (cellGroup) {
  22941. var curCell = cellGroup[direction]
  22942. //为单元格保留最小空间
  22943. if (curCell) {
  22944. changeValue = Math.min(
  22945. changeValue,
  22946. getTableCellWidth(curCell) - cellMinWidth
  22947. )
  22948. }
  22949. })
  22950. //修正越界
  22951. changeValue = changeValue < 0 ? 0 : changeValue
  22952. return direction === 'left' ? -changeValue : changeValue
  22953. }
  22954. function getTableCellWidth(cell) {
  22955. var width = 0,
  22956. //偏移纠正量
  22957. offset = 0,
  22958. width = cell.offsetWidth - getTabcellSpace()
  22959. //最后一个节点纠正一下
  22960. if (!cell.nextSibling) {
  22961. width -= getTableCellOffset(cell)
  22962. }
  22963. width = width < 0 ? 0 : width
  22964. try {
  22965. cell.width = width
  22966. } catch (e) {}
  22967. return width
  22968. }
  22969. /**
  22970. * 获取单元格所在表格的最末单元格的偏移量
  22971. */
  22972. function getTableCellOffset(cell) {
  22973. tab = domUtils.findParentByTagName(cell, 'table', false)
  22974. if (tab.offsetVal === undefined) {
  22975. var prev = cell.previousSibling
  22976. if (prev) {
  22977. //最后一个单元格和前一个单元格的width diff结果 如果恰好为一个border width, 则条件成立
  22978. tab.offsetVal =
  22979. cell.offsetWidth - prev.offsetWidth === UT.borderWidth
  22980. ? UT.borderWidth
  22981. : 0
  22982. } else {
  22983. tab.offsetVal = 0
  22984. }
  22985. }
  22986. return tab.offsetVal
  22987. }
  22988. function getTabcellSpace() {
  22989. if (UT.tabcellSpace === undefined) {
  22990. var cell = null,
  22991. tab = me.document.createElement('table'),
  22992. tbody = me.document.createElement('tbody'),
  22993. trow = me.document.createElement('tr'),
  22994. tabcell = me.document.createElement('td'),
  22995. mirror = null
  22996. tabcell.style.cssText = 'border: 0;'
  22997. tabcell.width = 1
  22998. trow.appendChild(tabcell)
  22999. trow.appendChild((mirror = tabcell.cloneNode(false)))
  23000. tbody.appendChild(trow)
  23001. tab.appendChild(tbody)
  23002. tab.style.cssText = 'visibility: hidden;'
  23003. me.body.appendChild(tab)
  23004. UT.paddingSpace = tabcell.offsetWidth - 1
  23005. var tmpTabWidth = tab.offsetWidth
  23006. tabcell.style.cssText = ''
  23007. mirror.style.cssText = ''
  23008. UT.borderWidth = (tab.offsetWidth - tmpTabWidth) / 3
  23009. UT.tabcellSpace = UT.paddingSpace + UT.borderWidth
  23010. me.body.removeChild(tab)
  23011. }
  23012. getTabcellSpace = function () {
  23013. return UT.tabcellSpace
  23014. }
  23015. return UT.tabcellSpace
  23016. }
  23017. function getDragLine(editor, doc) {
  23018. if (mousedown) return
  23019. dragLine = editor.document.createElement('div')
  23020. domUtils.setAttributes(dragLine, {
  23021. id: 'ue_tableDragLine',
  23022. unselectable: 'on',
  23023. contenteditable: false,
  23024. onresizestart: 'return false',
  23025. ondragstart: 'return false',
  23026. onselectstart: 'return false',
  23027. style:
  23028. 'background-color:blue;position:absolute;padding:0;margin:0;background-image:none;border:0px none;opacity:0;filter:alpha(opacity=0)',
  23029. })
  23030. editor.body.appendChild(dragLine)
  23031. }
  23032. function hideDragLine(editor) {
  23033. if (mousedown) return
  23034. var line
  23035. while ((line = editor.document.getElementById('ue_tableDragLine'))) {
  23036. domUtils.remove(line)
  23037. }
  23038. }
  23039. /**
  23040. * 依据state(v|h)在cell位置显示横线
  23041. * @param state
  23042. * @param cell
  23043. */
  23044. function showDragLineAt(state, cell) {
  23045. if (!cell) return
  23046. var table = domUtils.findParentByTagName(cell, 'table'),
  23047. caption = table.getElementsByTagName('caption'),
  23048. width = table.offsetWidth,
  23049. height =
  23050. table.offsetHeight -
  23051. (caption.length > 0 ? caption[0].offsetHeight : 0),
  23052. tablePos = domUtils.getXY(table),
  23053. cellPos = domUtils.getXY(cell),
  23054. css
  23055. switch (state) {
  23056. case 'h':
  23057. css =
  23058. 'height:' +
  23059. height +
  23060. 'px;top:' +
  23061. (tablePos.y + (caption.length > 0 ? caption[0].offsetHeight : 0)) +
  23062. 'px;left:' +
  23063. (cellPos.x + cell.offsetWidth)
  23064. dragLine.style.cssText =
  23065. css +
  23066. 'px;position: absolute;display:block;background-color:blue;width:1px;border:0; color:blue;opacity:.3;filter:alpha(opacity=30)'
  23067. break
  23068. case 'v':
  23069. css =
  23070. 'width:' +
  23071. width +
  23072. 'px;left:' +
  23073. tablePos.x +
  23074. 'px;top:' +
  23075. (cellPos.y + cell.offsetHeight)
  23076. //必须加上border:0和color:blue,否则低版ie不支持背景色显示
  23077. dragLine.style.cssText =
  23078. css +
  23079. 'px;overflow:hidden;position: absolute;display:block;background-color:blue;height:1px;border:0;color:blue;opacity:.2;filter:alpha(opacity=20)'
  23080. break
  23081. default:
  23082. }
  23083. }
  23084. /**
  23085. * 当表格边框颜色为白色时设置为虚线,true为添加虚线
  23086. * @param editor
  23087. * @param flag
  23088. */
  23089. function switchBorderColor(editor, flag) {
  23090. var tableArr = domUtils.getElementsByTagName(editor.body, 'table'),
  23091. color
  23092. for (var i = 0, node; (node = tableArr[i++]); ) {
  23093. var td = domUtils.getElementsByTagName(node, 'td')
  23094. if (td[0]) {
  23095. if (flag) {
  23096. color = td[0].style.borderColor.replace(/\s/g, '')
  23097. if (/(#ffffff)|(rgb\(255,255,255\))/gi.test(color))
  23098. domUtils.addClass(node, 'noBorderTable')
  23099. } else {
  23100. domUtils.removeClasses(node, 'noBorderTable')
  23101. }
  23102. }
  23103. }
  23104. }
  23105. function getTableWidth(editor, needIEHack, defaultValue) {
  23106. var body = editor.body
  23107. return (
  23108. body.offsetWidth -
  23109. (needIEHack
  23110. ? parseInt(domUtils.getComputedStyle(body, 'margin-left'), 10) * 2
  23111. : 0) -
  23112. defaultValue.tableBorder * 2 -
  23113. (editor.options.offsetWidth || 0)
  23114. )
  23115. }
  23116. /**
  23117. * 获取当前拖动的单元格
  23118. */
  23119. function getTargetTd(editor, evt) {
  23120. var target = domUtils.findParentByTagName(
  23121. evt.target || evt.srcElement,
  23122. ['td', 'th'],
  23123. true
  23124. ),
  23125. dir = null
  23126. if (!target) {
  23127. return null
  23128. }
  23129. dir = getRelation(target, mouseCoords(evt))
  23130. //如果有前一个节点, 需要做一个修正, 否则可能会得到一个错误的td
  23131. if (!target) {
  23132. return null
  23133. }
  23134. if (dir === 'h1' && target.previousSibling) {
  23135. var position = domUtils.getXY(target),
  23136. cellWidth = target.offsetWidth
  23137. if (Math.abs(position.x + cellWidth - evt.clientX) > cellWidth / 3) {
  23138. target = target.previousSibling
  23139. }
  23140. } else if (dir === 'v1' && target.parentNode.previousSibling) {
  23141. var position = domUtils.getXY(target),
  23142. cellHeight = target.offsetHeight
  23143. if (Math.abs(position.y + cellHeight - evt.clientY) > cellHeight / 3) {
  23144. target = target.parentNode.previousSibling.firstChild
  23145. }
  23146. }
  23147. //排除了非td内部以及用于代码高亮部分的td
  23148. return target && !(editor.fireEvent('excludetable', target) === true)
  23149. ? target
  23150. : null
  23151. }
  23152. }
  23153. // plugins/table.sort.js
  23154. /**
  23155. * Created with JetBrains PhpStorm.
  23156. * User: Jinqn
  23157. * Date: 13-10-12
  23158. * Time: 上午10:20
  23159. * To change this template use File | Settings | File Templates.
  23160. */
  23161. UE.UETable.prototype.sortTable = function (sortByCellIndex, compareFn) {
  23162. var table = this.table,
  23163. rows = table.rows,
  23164. trArray = [],
  23165. flag = rows[0].cells[0].tagName === 'TH',
  23166. lastRowIndex = 0
  23167. if (this.selectedTds.length) {
  23168. var range = this.cellsRange,
  23169. len = range.endRowIndex + 1
  23170. for (var i = range.beginRowIndex; i < len; i++) {
  23171. trArray[i] = rows[i]
  23172. }
  23173. trArray.splice(0, range.beginRowIndex)
  23174. lastRowIndex =
  23175. range.endRowIndex + 1 === this.rowsNum ? 0 : range.endRowIndex + 1
  23176. } else {
  23177. for (var i = 0, len = rows.length; i < len; i++) {
  23178. trArray[i] = rows[i]
  23179. }
  23180. }
  23181. var Fn = {
  23182. reversecurrent: function (td1, td2) {
  23183. return 1
  23184. },
  23185. orderbyasc: function (td1, td2) {
  23186. var value1 = td1.innerText || td1.textContent,
  23187. value2 = td2.innerText || td2.textContent
  23188. return value1.localeCompare(value2)
  23189. },
  23190. reversebyasc: function (td1, td2) {
  23191. var value1 = td1.innerHTML,
  23192. value2 = td2.innerHTML
  23193. return value2.localeCompare(value1)
  23194. },
  23195. orderbynum: function (td1, td2) {
  23196. var value1 = td1[browser.ie ? 'innerText' : 'textContent'].match(/\d+/),
  23197. value2 = td2[browser.ie ? 'innerText' : 'textContent'].match(/\d+/)
  23198. if (value1) value1 = +value1[0]
  23199. if (value2) value2 = +value2[0]
  23200. return (value1 || 0) - (value2 || 0)
  23201. },
  23202. reversebynum: function (td1, td2) {
  23203. var value1 = td1[browser.ie ? 'innerText' : 'textContent'].match(/\d+/),
  23204. value2 = td2[browser.ie ? 'innerText' : 'textContent'].match(/\d+/)
  23205. if (value1) value1 = +value1[0]
  23206. if (value2) value2 = +value2[0]
  23207. return (value2 || 0) - (value1 || 0)
  23208. },
  23209. }
  23210. //对表格设置排序的标记data-sort-type
  23211. table.setAttribute(
  23212. 'data-sort-type',
  23213. compareFn && typeof compareFn === 'string' && Fn[compareFn]
  23214. ? compareFn
  23215. : ''
  23216. )
  23217. //th不参与排序
  23218. flag && trArray.splice(0, 1)
  23219. trArray = utils.sort(trArray, function (tr1, tr2) {
  23220. var result
  23221. if (compareFn && typeof compareFn === 'function') {
  23222. result = compareFn.call(
  23223. this,
  23224. tr1.cells[sortByCellIndex],
  23225. tr2.cells[sortByCellIndex]
  23226. )
  23227. } else if (compareFn && typeof compareFn === 'number') {
  23228. result = 1
  23229. } else if (compareFn && typeof compareFn === 'string' && Fn[compareFn]) {
  23230. result = Fn[compareFn].call(
  23231. this,
  23232. tr1.cells[sortByCellIndex],
  23233. tr2.cells[sortByCellIndex]
  23234. )
  23235. } else {
  23236. result = Fn['orderbyasc'].call(
  23237. this,
  23238. tr1.cells[sortByCellIndex],
  23239. tr2.cells[sortByCellIndex]
  23240. )
  23241. }
  23242. return result
  23243. })
  23244. var fragment = table.ownerDocument.createDocumentFragment()
  23245. for (var j = 0, len = trArray.length; j < len; j++) {
  23246. fragment.appendChild(trArray[j])
  23247. }
  23248. var tbody = table.getElementsByTagName('tbody')[0]
  23249. if (!lastRowIndex) {
  23250. tbody.appendChild(fragment)
  23251. } else {
  23252. tbody.insertBefore(
  23253. fragment,
  23254. rows[lastRowIndex - range.endRowIndex + range.beginRowIndex - 1]
  23255. )
  23256. }
  23257. }
  23258. UE.plugins['tablesort'] = function () {
  23259. var me = this,
  23260. UT = UE.UETable,
  23261. getUETable = function (tdOrTable) {
  23262. return UT.getUETable(tdOrTable)
  23263. },
  23264. getTableItemsByRange = function (editor) {
  23265. return UT.getTableItemsByRange(editor)
  23266. }
  23267. me.ready(function () {
  23268. //添加表格可排序的样式
  23269. utils.cssRule(
  23270. 'tablesort',
  23271. 'table.sortEnabled tr.firstRow th,table.sortEnabled tr.firstRow td{padding-right:20px;background-repeat: no-repeat;background-position: center right;' +
  23272. ' background-image:url(' +
  23273. me.options.themePath +
  23274. me.options.theme +
  23275. '/images/sortable.png);}',
  23276. me.document
  23277. )
  23278. //做单元格合并操作时,清除可排序标识
  23279. me.addListener('afterexeccommand', function (type, cmd) {
  23280. if (cmd == 'mergeright' || cmd == 'mergedown' || cmd == 'mergecells') {
  23281. this.execCommand('disablesort')
  23282. }
  23283. })
  23284. })
  23285. //表格排序
  23286. UE.commands['sorttable'] = {
  23287. queryCommandState: function () {
  23288. var me = this,
  23289. tableItems = getTableItemsByRange(me)
  23290. if (!tableItems.cell) return -1
  23291. var table = tableItems.table,
  23292. cells = table.getElementsByTagName('td')
  23293. for (var i = 0, cell; (cell = cells[i++]); ) {
  23294. if (cell.rowSpan != 1 || cell.colSpan != 1) return -1
  23295. }
  23296. return 0
  23297. },
  23298. execCommand: function (cmd, fn) {
  23299. var me = this,
  23300. range = me.selection.getRange(),
  23301. bk = range.createBookmark(true),
  23302. tableItems = getTableItemsByRange(me),
  23303. cell = tableItems.cell,
  23304. ut = getUETable(tableItems.table),
  23305. cellInfo = ut.getCellInfo(cell)
  23306. ut.sortTable(cellInfo.cellIndex, fn)
  23307. range.moveToBookmark(bk)
  23308. try {
  23309. range.select()
  23310. } catch (e) {}
  23311. },
  23312. }
  23313. //设置表格可排序,清除表格可排序
  23314. UE.commands['enablesort'] = UE.commands['disablesort'] = {
  23315. queryCommandState: function (cmd) {
  23316. var table = getTableItemsByRange(this).table
  23317. if (table && cmd == 'enablesort') {
  23318. var cells = domUtils.getElementsByTagName(table, 'th td')
  23319. for (var i = 0; i < cells.length; i++) {
  23320. if (
  23321. cells[i].getAttribute('colspan') > 1 ||
  23322. cells[i].getAttribute('rowspan') > 1
  23323. )
  23324. return -1
  23325. }
  23326. }
  23327. return !table
  23328. ? -1
  23329. : (cmd == 'enablesort') ^
  23330. (table.getAttribute('data-sort') != 'sortEnabled')
  23331. ? -1
  23332. : 0
  23333. },
  23334. execCommand: function (cmd) {
  23335. var table = getTableItemsByRange(this).table
  23336. table.setAttribute(
  23337. 'data-sort',
  23338. cmd == 'enablesort' ? 'sortEnabled' : 'sortDisabled'
  23339. )
  23340. cmd == 'enablesort'
  23341. ? domUtils.addClass(table, 'sortEnabled')
  23342. : domUtils.removeClasses(table, 'sortEnabled')
  23343. },
  23344. }
  23345. }
  23346. // plugins/contextmenu.js
  23347. ///import core
  23348. ///commands 右键菜单
  23349. ///commandsName ContextMenu
  23350. ///commandsTitle 右键菜单
  23351. /**
  23352. * 右键菜单
  23353. * @function
  23354. * @name baidu.editor.plugins.contextmenu
  23355. * @author zhanyi
  23356. */
  23357. UE.plugins['contextmenu'] = function () {
  23358. var me = this
  23359. me.setOpt('enableContextMenu', true)
  23360. if (me.getOpt('enableContextMenu') === false) {
  23361. return
  23362. }
  23363. var lang = me.getLang('contextMenu'),
  23364. menu,
  23365. items = me.options.contextMenu || [
  23366. { label: lang['selectall'], cmdName: 'selectall' },
  23367. {
  23368. label: lang.cleardoc,
  23369. cmdName: 'cleardoc',
  23370. exec: function () {
  23371. if (confirm(lang.confirmclear)) {
  23372. this.execCommand('cleardoc')
  23373. }
  23374. },
  23375. },
  23376. '-',
  23377. {
  23378. label: lang.unlink,
  23379. cmdName: 'unlink',
  23380. },
  23381. '-',
  23382. {
  23383. group: lang.paragraph,
  23384. icon: 'justifyjustify',
  23385. subMenu: [
  23386. {
  23387. label: lang.justifyleft,
  23388. cmdName: 'justify',
  23389. value: 'left',
  23390. },
  23391. {
  23392. label: lang.justifyright,
  23393. cmdName: 'justify',
  23394. value: 'right',
  23395. },
  23396. {
  23397. label: lang.justifycenter,
  23398. cmdName: 'justify',
  23399. value: 'center',
  23400. },
  23401. {
  23402. label: lang.justifyjustify,
  23403. cmdName: 'justify',
  23404. value: 'justify',
  23405. },
  23406. ],
  23407. },
  23408. '-',
  23409. {
  23410. group: lang.table,
  23411. icon: 'table',
  23412. subMenu: [
  23413. {
  23414. label: lang.inserttable,
  23415. cmdName: 'inserttable',
  23416. },
  23417. {
  23418. label: lang.deletetable,
  23419. cmdName: 'deletetable',
  23420. },
  23421. '-',
  23422. {
  23423. label: lang.deleterow,
  23424. cmdName: 'deleterow',
  23425. },
  23426. {
  23427. label: lang.deletecol,
  23428. cmdName: 'deletecol',
  23429. },
  23430. {
  23431. label: lang.insertcol,
  23432. cmdName: 'insertcol',
  23433. },
  23434. {
  23435. label: lang.insertcolnext,
  23436. cmdName: 'insertcolnext',
  23437. },
  23438. {
  23439. label: lang.insertrow,
  23440. cmdName: 'insertrow',
  23441. },
  23442. {
  23443. label: lang.insertrownext,
  23444. cmdName: 'insertrownext',
  23445. },
  23446. '-',
  23447. {
  23448. label: lang.insertcaption,
  23449. cmdName: 'insertcaption',
  23450. },
  23451. {
  23452. label: lang.deletecaption,
  23453. cmdName: 'deletecaption',
  23454. },
  23455. {
  23456. label: lang.inserttitle,
  23457. cmdName: 'inserttitle',
  23458. },
  23459. {
  23460. label: lang.deletetitle,
  23461. cmdName: 'deletetitle',
  23462. },
  23463. {
  23464. label: lang.inserttitlecol,
  23465. cmdName: 'inserttitlecol',
  23466. },
  23467. {
  23468. label: lang.deletetitlecol,
  23469. cmdName: 'deletetitlecol',
  23470. },
  23471. '-',
  23472. {
  23473. label: lang.mergecells,
  23474. cmdName: 'mergecells',
  23475. },
  23476. {
  23477. label: lang.mergeright,
  23478. cmdName: 'mergeright',
  23479. },
  23480. {
  23481. label: lang.mergedown,
  23482. cmdName: 'mergedown',
  23483. },
  23484. '-',
  23485. {
  23486. label: lang.splittorows,
  23487. cmdName: 'splittorows',
  23488. },
  23489. {
  23490. label: lang.splittocols,
  23491. cmdName: 'splittocols',
  23492. },
  23493. {
  23494. label: lang.splittocells,
  23495. cmdName: 'splittocells',
  23496. },
  23497. '-',
  23498. {
  23499. label: lang.averageDiseRow,
  23500. cmdName: 'averagedistributerow',
  23501. },
  23502. {
  23503. label: lang.averageDisCol,
  23504. cmdName: 'averagedistributecol',
  23505. },
  23506. '-',
  23507. {
  23508. label: lang.edittd,
  23509. cmdName: 'edittd',
  23510. exec: function () {
  23511. if (UE.ui['edittd']) {
  23512. new UE.ui['edittd'](this)
  23513. }
  23514. this.getDialog('edittd').open()
  23515. },
  23516. },
  23517. {
  23518. label: lang.edittable,
  23519. cmdName: 'edittable',
  23520. exec: function () {
  23521. if (UE.ui['edittable']) {
  23522. new UE.ui['edittable'](this)
  23523. }
  23524. this.getDialog('edittable').open()
  23525. },
  23526. },
  23527. {
  23528. label: lang.setbordervisible,
  23529. cmdName: 'setbordervisible',
  23530. },
  23531. ],
  23532. },
  23533. {
  23534. group: lang.tablesort,
  23535. icon: 'tablesort',
  23536. subMenu: [
  23537. {
  23538. label: lang.enablesort,
  23539. cmdName: 'enablesort',
  23540. },
  23541. {
  23542. label: lang.disablesort,
  23543. cmdName: 'disablesort',
  23544. },
  23545. '-',
  23546. {
  23547. label: lang.reversecurrent,
  23548. cmdName: 'sorttable',
  23549. value: 'reversecurrent',
  23550. },
  23551. {
  23552. label: lang.orderbyasc,
  23553. cmdName: 'sorttable',
  23554. value: 'orderbyasc',
  23555. },
  23556. {
  23557. label: lang.reversebyasc,
  23558. cmdName: 'sorttable',
  23559. value: 'reversebyasc',
  23560. },
  23561. {
  23562. label: lang.orderbynum,
  23563. cmdName: 'sorttable',
  23564. value: 'orderbynum',
  23565. },
  23566. {
  23567. label: lang.reversebynum,
  23568. cmdName: 'sorttable',
  23569. value: 'reversebynum',
  23570. },
  23571. ],
  23572. },
  23573. {
  23574. group: lang.borderbk,
  23575. icon: 'borderBack',
  23576. subMenu: [
  23577. {
  23578. label: lang.setcolor,
  23579. cmdName: 'interlacetable',
  23580. exec: function () {
  23581. this.execCommand('interlacetable')
  23582. },
  23583. },
  23584. {
  23585. label: lang.unsetcolor,
  23586. cmdName: 'uninterlacetable',
  23587. exec: function () {
  23588. this.execCommand('uninterlacetable')
  23589. },
  23590. },
  23591. {
  23592. label: lang.setbackground,
  23593. cmdName: 'settablebackground',
  23594. exec: function () {
  23595. this.execCommand('settablebackground', {
  23596. repeat: true,
  23597. colorList: ['#bbb', '#ccc'],
  23598. })
  23599. },
  23600. },
  23601. {
  23602. label: lang.unsetbackground,
  23603. cmdName: 'cleartablebackground',
  23604. exec: function () {
  23605. this.execCommand('cleartablebackground')
  23606. },
  23607. },
  23608. {
  23609. label: lang.redandblue,
  23610. cmdName: 'settablebackground',
  23611. exec: function () {
  23612. this.execCommand('settablebackground', {
  23613. repeat: true,
  23614. colorList: ['red', 'blue'],
  23615. })
  23616. },
  23617. },
  23618. {
  23619. label: lang.threecolorgradient,
  23620. cmdName: 'settablebackground',
  23621. exec: function () {
  23622. this.execCommand('settablebackground', {
  23623. repeat: true,
  23624. colorList: ['#aaa', '#bbb', '#ccc'],
  23625. })
  23626. },
  23627. },
  23628. ],
  23629. },
  23630. {
  23631. group: lang.aligntd,
  23632. icon: 'aligntd',
  23633. subMenu: [
  23634. {
  23635. cmdName: 'cellalignment',
  23636. value: { align: 'left', vAlign: 'top' },
  23637. },
  23638. {
  23639. cmdName: 'cellalignment',
  23640. value: { align: 'center', vAlign: 'top' },
  23641. },
  23642. {
  23643. cmdName: 'cellalignment',
  23644. value: { align: 'right', vAlign: 'top' },
  23645. },
  23646. {
  23647. cmdName: 'cellalignment',
  23648. value: { align: 'left', vAlign: 'middle' },
  23649. },
  23650. {
  23651. cmdName: 'cellalignment',
  23652. value: { align: 'center', vAlign: 'middle' },
  23653. },
  23654. {
  23655. cmdName: 'cellalignment',
  23656. value: { align: 'right', vAlign: 'middle' },
  23657. },
  23658. {
  23659. cmdName: 'cellalignment',
  23660. value: { align: 'left', vAlign: 'bottom' },
  23661. },
  23662. {
  23663. cmdName: 'cellalignment',
  23664. value: { align: 'center', vAlign: 'bottom' },
  23665. },
  23666. {
  23667. cmdName: 'cellalignment',
  23668. value: { align: 'right', vAlign: 'bottom' },
  23669. },
  23670. ],
  23671. },
  23672. {
  23673. group: lang.aligntable,
  23674. icon: 'aligntable',
  23675. subMenu: [
  23676. {
  23677. cmdName: 'tablealignment',
  23678. className: 'left',
  23679. label: lang.tableleft,
  23680. value: 'left',
  23681. },
  23682. {
  23683. cmdName: 'tablealignment',
  23684. className: 'center',
  23685. label: lang.tablecenter,
  23686. value: 'center',
  23687. },
  23688. {
  23689. cmdName: 'tablealignment',
  23690. className: 'right',
  23691. label: lang.tableright,
  23692. value: 'right',
  23693. },
  23694. ],
  23695. },
  23696. '-',
  23697. {
  23698. label: lang.insertparagraphbefore,
  23699. cmdName: 'insertparagraph',
  23700. value: true,
  23701. },
  23702. {
  23703. label: lang.insertparagraphafter,
  23704. cmdName: 'insertparagraph',
  23705. },
  23706. {
  23707. label: lang['copy'],
  23708. cmdName: 'copy',
  23709. },
  23710. {
  23711. label: lang['paste'],
  23712. cmdName: 'paste',
  23713. },
  23714. ]
  23715. if (!items.length) {
  23716. return
  23717. }
  23718. var uiUtils = UE.ui.uiUtils
  23719. me.addListener('contextmenu', function (type, evt) {
  23720. var offset = uiUtils.getViewportOffsetByEvent(evt)
  23721. me.fireEvent('beforeselectionchange')
  23722. if (menu) {
  23723. menu.destroy()
  23724. }
  23725. for (var i = 0, ti, contextItems = []; (ti = items[i]); i++) {
  23726. var last
  23727. ;(function (item) {
  23728. if (item == '-') {
  23729. if (
  23730. (last = contextItems[contextItems.length - 1]) &&
  23731. last !== '-'
  23732. ) {
  23733. contextItems.push('-')
  23734. }
  23735. } else if (item.hasOwnProperty('group')) {
  23736. for (var j = 0, cj, subMenu = []; (cj = item.subMenu[j]); j++) {
  23737. ;(function (subItem) {
  23738. if (subItem == '-') {
  23739. if ((last = subMenu[subMenu.length - 1]) && last !== '-') {
  23740. subMenu.push('-')
  23741. } else {
  23742. subMenu.splice(subMenu.length - 1)
  23743. }
  23744. } else {
  23745. if (
  23746. (me.commands[subItem.cmdName] ||
  23747. UE.commands[subItem.cmdName] ||
  23748. subItem.query) &&
  23749. (subItem.query
  23750. ? subItem.query()
  23751. : me.queryCommandState(subItem.cmdName)) > -1
  23752. ) {
  23753. subMenu.push({
  23754. label:
  23755. subItem.label ||
  23756. me.getLang(
  23757. 'contextMenu.' +
  23758. subItem.cmdName +
  23759. (subItem.value || '')
  23760. ) ||
  23761. '',
  23762. className:
  23763. 'edui-for-' +
  23764. subItem.cmdName +
  23765. (subItem.className
  23766. ? ' edui-for-' +
  23767. subItem.cmdName +
  23768. '-' +
  23769. subItem.className
  23770. : ''),
  23771. onclick: subItem.exec
  23772. ? function () {
  23773. subItem.exec.call(me)
  23774. }
  23775. : function () {
  23776. me.execCommand(subItem.cmdName, subItem.value)
  23777. },
  23778. })
  23779. }
  23780. }
  23781. })(cj)
  23782. }
  23783. if (subMenu.length) {
  23784. function getLabel() {
  23785. switch (item.icon) {
  23786. case 'table':
  23787. return me.getLang('contextMenu.table')
  23788. case 'justifyjustify':
  23789. return me.getLang('contextMenu.paragraph')
  23790. case 'aligntd':
  23791. return me.getLang('contextMenu.aligntd')
  23792. case 'aligntable':
  23793. return me.getLang('contextMenu.aligntable')
  23794. case 'tablesort':
  23795. return lang.tablesort
  23796. case 'borderBack':
  23797. return lang.borderbk
  23798. default:
  23799. return ''
  23800. }
  23801. }
  23802. contextItems.push({
  23803. //todo 修正成自动获取方式
  23804. label: getLabel(),
  23805. className: 'edui-for-' + item.icon,
  23806. subMenu: {
  23807. items: subMenu,
  23808. editor: me,
  23809. },
  23810. })
  23811. }
  23812. } else {
  23813. //有可能commmand没有加载右键不能出来,或者没有command也想能展示出来添加query方法
  23814. if (
  23815. (me.commands[item.cmdName] ||
  23816. UE.commands[item.cmdName] ||
  23817. item.query) &&
  23818. (item.query
  23819. ? item.query.call(me)
  23820. : me.queryCommandState(item.cmdName)) > -1
  23821. ) {
  23822. contextItems.push({
  23823. label: item.label || me.getLang('contextMenu.' + item.cmdName),
  23824. className:
  23825. 'edui-for-' +
  23826. (item.icon ? item.icon : item.cmdName + (item.value || '')),
  23827. onclick: item.exec
  23828. ? function () {
  23829. item.exec.call(me)
  23830. }
  23831. : function () {
  23832. me.execCommand(item.cmdName, item.value)
  23833. },
  23834. })
  23835. }
  23836. }
  23837. })(ti)
  23838. }
  23839. if (contextItems[contextItems.length - 1] == '-') {
  23840. contextItems.pop()
  23841. }
  23842. menu = new UE.ui.Menu({
  23843. items: contextItems,
  23844. className: 'edui-contextmenu',
  23845. editor: me,
  23846. })
  23847. menu.render()
  23848. menu.showAt(offset)
  23849. me.fireEvent('aftershowcontextmenu', menu)
  23850. domUtils.preventDefault(evt)
  23851. if (browser.ie) {
  23852. var ieRange
  23853. try {
  23854. ieRange = me.selection.getNative().createRange()
  23855. } catch (e) {
  23856. return
  23857. }
  23858. if (ieRange.item) {
  23859. var range = new dom.Range(me.document)
  23860. range.selectNode(ieRange.item(0)).select(true, true)
  23861. }
  23862. }
  23863. })
  23864. // 添加复制的flash按钮
  23865. me.addListener('aftershowcontextmenu', function (type, menu) {
  23866. if (me.zeroclipboard) {
  23867. var items = menu.items
  23868. for (var key in items) {
  23869. if (items[key].className == 'edui-for-copy') {
  23870. me.zeroclipboard.clip(items[key].getDom())
  23871. }
  23872. }
  23873. }
  23874. })
  23875. }
  23876. // plugins/shortcutmenu.js
  23877. ///import core
  23878. ///commands 弹出菜单
  23879. // commandsName popupmenu
  23880. ///commandsTitle 弹出菜单
  23881. /**
  23882. * 弹出菜单
  23883. * @function
  23884. * @name baidu.editor.plugins.popupmenu
  23885. * @author xuheng
  23886. */
  23887. UE.plugins['shortcutmenu'] = function () {
  23888. var me = this,
  23889. menu,
  23890. items = me.options.shortcutMenu || []
  23891. if (!items.length) {
  23892. return
  23893. }
  23894. me.addListener('contextmenu mouseup', function (type, e) {
  23895. var me = this,
  23896. customEvt = {
  23897. type: type,
  23898. target: e.target || e.srcElement,
  23899. screenX: e.screenX,
  23900. screenY: e.screenY,
  23901. clientX: e.clientX,
  23902. clientY: e.clientY,
  23903. }
  23904. setTimeout(function () {
  23905. var rng = me.selection.getRange()
  23906. if (rng.collapsed === false || type == 'contextmenu') {
  23907. if (!menu) {
  23908. menu = new baidu.editor.ui.ShortCutMenu({
  23909. editor: me,
  23910. items: items,
  23911. theme: me.options.theme,
  23912. className: 'edui-shortcutmenu',
  23913. })
  23914. menu.render()
  23915. me.fireEvent('afterrendershortcutmenu', menu)
  23916. }
  23917. menu.show(customEvt, !!UE.plugins['contextmenu'])
  23918. }
  23919. })
  23920. if (type == 'contextmenu') {
  23921. domUtils.preventDefault(e)
  23922. if (browser.ie9below) {
  23923. var ieRange
  23924. try {
  23925. ieRange = me.selection.getNative().createRange()
  23926. } catch (e) {
  23927. return
  23928. }
  23929. if (ieRange.item) {
  23930. var range = new dom.Range(me.document)
  23931. range.selectNode(ieRange.item(0)).select(true, true)
  23932. }
  23933. }
  23934. }
  23935. })
  23936. me.addListener('keydown', function (type) {
  23937. if (type == 'keydown') {
  23938. menu && !menu.isHidden && menu.hide()
  23939. }
  23940. })
  23941. }
  23942. // plugins/basestyle.js
  23943. /**
  23944. * B、I、sub、super命令支持
  23945. * @file
  23946. * @since 1.2.6.1
  23947. */
  23948. UE.plugins['basestyle'] = function () {
  23949. /**
  23950. * 字体加粗
  23951. * @command bold
  23952. * @param { String } cmd 命令字符串
  23953. * @remind 对已加粗的文本内容执行该命令, 将取消加粗
  23954. * @method execCommand
  23955. * @example
  23956. * ```javascript
  23957. * //editor是编辑器实例
  23958. * //对当前选中的文本内容执行加粗操作
  23959. * //第一次执行, 文本内容加粗
  23960. * editor.execCommand( 'bold' );
  23961. *
  23962. * //第二次执行, 文本内容取消加粗
  23963. * editor.execCommand( 'bold' );
  23964. * ```
  23965. */
  23966. /**
  23967. * 字体倾斜
  23968. * @command italic
  23969. * @method execCommand
  23970. * @param { String } cmd 命令字符串
  23971. * @remind 对已倾斜的文本内容执行该命令, 将取消倾斜
  23972. * @example
  23973. * ```javascript
  23974. * //editor是编辑器实例
  23975. * //对当前选中的文本内容执行斜体操作
  23976. * //第一次操作, 文本内容将变成斜体
  23977. * editor.execCommand( 'italic' );
  23978. *
  23979. * //再次对同一文本内容执行, 则文本内容将恢复正常
  23980. * editor.execCommand( 'italic' );
  23981. * ```
  23982. */
  23983. /**
  23984. * 下标文本,与“superscript”命令互斥
  23985. * @command subscript
  23986. * @method execCommand
  23987. * @remind 把选中的文本内容切换成下标文本, 如果当前选中的文本已经是下标, 则该操作会把文本内容还原成正常文本
  23988. * @param { String } cmd 命令字符串
  23989. * @example
  23990. * ```javascript
  23991. * //editor是编辑器实例
  23992. * //对当前选中的文本内容执行下标操作
  23993. * //第一次操作, 文本内容将变成下标文本
  23994. * editor.execCommand( 'subscript' );
  23995. *
  23996. * //再次对同一文本内容执行, 则文本内容将恢复正常
  23997. * editor.execCommand( 'subscript' );
  23998. * ```
  23999. */
  24000. /**
  24001. * 上标文本,与“subscript”命令互斥
  24002. * @command superscript
  24003. * @method execCommand
  24004. * @remind 把选中的文本内容切换成上标文本, 如果当前选中的文本已经是上标, 则该操作会把文本内容还原成正常文本
  24005. * @param { String } cmd 命令字符串
  24006. * @example
  24007. * ```javascript
  24008. * //editor是编辑器实例
  24009. * //对当前选中的文本内容执行上标操作
  24010. * //第一次操作, 文本内容将变成上标文本
  24011. * editor.execCommand( 'superscript' );
  24012. *
  24013. * //再次对同一文本内容执行, 则文本内容将恢复正常
  24014. * editor.execCommand( 'superscript' );
  24015. * ```
  24016. */
  24017. var basestyles = {
  24018. bold: ['strong', 'b'],
  24019. italic: ['em', 'i'],
  24020. subscript: ['sub'],
  24021. superscript: ['sup'],
  24022. },
  24023. getObj = function (editor, tagNames) {
  24024. return domUtils.filterNodeList(
  24025. editor.selection.getStartElementPath(),
  24026. tagNames
  24027. )
  24028. },
  24029. me = this
  24030. //添加快捷键
  24031. me.addshortcutkey({
  24032. Bold: 'ctrl+66', //^B
  24033. Italic: 'ctrl+73', //^I
  24034. Underline: 'ctrl+85', //^U
  24035. })
  24036. me.addInputRule(function (root) {
  24037. utils.each(root.getNodesByTagName('b i'), function (node) {
  24038. switch (node.tagName) {
  24039. case 'b':
  24040. node.tagName = 'strong'
  24041. break
  24042. case 'i':
  24043. node.tagName = 'em'
  24044. }
  24045. })
  24046. })
  24047. for (var style in basestyles) {
  24048. ;(function (cmd, tagNames) {
  24049. me.commands[cmd] = {
  24050. execCommand: function (cmdName) {
  24051. var range = me.selection.getRange(),
  24052. obj = getObj(this, tagNames)
  24053. if (range.collapsed) {
  24054. if (obj) {
  24055. var tmpText = me.document.createTextNode('')
  24056. range.insertNode(tmpText).removeInlineStyle(tagNames)
  24057. range.setStartBefore(tmpText)
  24058. domUtils.remove(tmpText)
  24059. } else {
  24060. var tmpNode = range.document.createElement(tagNames[0])
  24061. if (cmdName == 'superscript' || cmdName == 'subscript') {
  24062. tmpText = me.document.createTextNode('')
  24063. range
  24064. .insertNode(tmpText)
  24065. .removeInlineStyle(['sub', 'sup'])
  24066. .setStartBefore(tmpText)
  24067. .collapse(true)
  24068. }
  24069. range.insertNode(tmpNode).setStart(tmpNode, 0)
  24070. }
  24071. range.collapse(true)
  24072. } else {
  24073. if (cmdName == 'superscript' || cmdName == 'subscript') {
  24074. if (!obj || obj.tagName.toLowerCase() != cmdName) {
  24075. range.removeInlineStyle(['sub', 'sup'])
  24076. }
  24077. }
  24078. obj
  24079. ? range.removeInlineStyle(tagNames)
  24080. : range.applyInlineStyle(tagNames[0])
  24081. }
  24082. range.select()
  24083. },
  24084. queryCommandState: function () {
  24085. return getObj(this, tagNames) ? 1 : 0
  24086. },
  24087. }
  24088. })(style, basestyles[style])
  24089. }
  24090. }
  24091. // plugins/elementpath.js
  24092. /**
  24093. * 选取路径命令
  24094. * @file
  24095. */
  24096. UE.plugins['elementpath'] = function () {
  24097. var currentLevel,
  24098. tagNames,
  24099. me = this
  24100. me.setOpt('elementPathEnabled', true)
  24101. if (!me.options.elementPathEnabled) {
  24102. return
  24103. }
  24104. me.commands['elementpath'] = {
  24105. execCommand: function (cmdName, level) {
  24106. var start = tagNames[level],
  24107. range = me.selection.getRange()
  24108. currentLevel = level * 1
  24109. range.selectNode(start).select()
  24110. },
  24111. queryCommandValue: function () {
  24112. //产生一个副本,不能修改原来的startElementPath;
  24113. var parents = [].concat(this.selection.getStartElementPath()).reverse(),
  24114. names = []
  24115. tagNames = parents
  24116. for (var i = 0, ci; (ci = parents[i]); i++) {
  24117. if (ci.nodeType == 3) {
  24118. continue
  24119. }
  24120. var name = ci.tagName.toLowerCase()
  24121. if (name == 'img' && ci.getAttribute('anchorname')) {
  24122. name = 'anchor'
  24123. }
  24124. names[i] = name
  24125. if (currentLevel == i) {
  24126. currentLevel = -1
  24127. break
  24128. }
  24129. }
  24130. return names
  24131. },
  24132. }
  24133. }
  24134. // plugins/formatmatch.js
  24135. /**
  24136. * 格式刷,只格式inline的
  24137. * @file
  24138. * @since 1.2.6.1
  24139. */
  24140. /**
  24141. * 格式刷
  24142. * @command formatmatch
  24143. * @method execCommand
  24144. * @remind 该操作不能复制段落格式
  24145. * @param { String } cmd 命令字符串
  24146. * @example
  24147. * ```javascript
  24148. * //editor是编辑器实例
  24149. * //获取格式刷
  24150. * editor.execCommand( 'formatmatch' );
  24151. * ```
  24152. */
  24153. UE.plugins['formatmatch'] = function () {
  24154. var me = this,
  24155. list = [],
  24156. img,
  24157. flag = 0
  24158. me.addListener('reset', function () {
  24159. list = []
  24160. flag = 0
  24161. })
  24162. function addList(type, evt) {
  24163. if (browser.webkit) {
  24164. var target = evt.target.tagName == 'IMG' ? evt.target : null
  24165. }
  24166. function addFormat(range) {
  24167. if (text) {
  24168. range.selectNode(text)
  24169. }
  24170. return range.applyInlineStyle(list[list.length - 1].tagName, null, list)
  24171. }
  24172. me.undoManger && me.undoManger.save()
  24173. var range = me.selection.getRange(),
  24174. imgT = target || range.getClosedNode()
  24175. if (img && imgT && imgT.tagName == 'IMG') {
  24176. //trace:964
  24177. imgT.style.cssText +=
  24178. ';float:' +
  24179. (img.style.cssFloat || img.style.styleFloat || 'none') +
  24180. ';display:' +
  24181. (img.style.display || 'inline')
  24182. img = null
  24183. } else {
  24184. if (!img) {
  24185. var collapsed = range.collapsed
  24186. if (collapsed) {
  24187. var text = me.document.createTextNode('match')
  24188. range.insertNode(text).select()
  24189. }
  24190. me.__hasEnterExecCommand = true
  24191. //不能把block上的属性干掉
  24192. //trace:1553
  24193. var removeFormatAttributes = me.options.removeFormatAttributes
  24194. me.options.removeFormatAttributes = ''
  24195. me.execCommand('removeformat')
  24196. me.options.removeFormatAttributes = removeFormatAttributes
  24197. me.__hasEnterExecCommand = false
  24198. //trace:969
  24199. range = me.selection.getRange()
  24200. if (list.length) {
  24201. addFormat(range)
  24202. }
  24203. if (text) {
  24204. range.setStartBefore(text).collapse(true)
  24205. }
  24206. range.select()
  24207. text && domUtils.remove(text)
  24208. }
  24209. }
  24210. me.undoManger && me.undoManger.save()
  24211. me.removeListener('mouseup', addList)
  24212. flag = 0
  24213. }
  24214. me.commands['formatmatch'] = {
  24215. execCommand: function (cmdName) {
  24216. if (flag) {
  24217. flag = 0
  24218. list = []
  24219. me.removeListener('mouseup', addList)
  24220. return
  24221. }
  24222. var range = me.selection.getRange()
  24223. img = range.getClosedNode()
  24224. if (!img || img.tagName != 'IMG') {
  24225. range.collapse(true).shrinkBoundary()
  24226. var start = range.startContainer
  24227. list = domUtils.findParents(start, true, function (node) {
  24228. return !domUtils.isBlockElm(node) && node.nodeType == 1
  24229. })
  24230. //a不能加入格式刷, 并且克隆节点
  24231. for (var i = 0, ci; (ci = list[i]); i++) {
  24232. if (ci.tagName == 'A') {
  24233. list.splice(i, 1)
  24234. break
  24235. }
  24236. }
  24237. }
  24238. me.addListener('mouseup', addList)
  24239. flag = 1
  24240. },
  24241. queryCommandState: function () {
  24242. return flag
  24243. },
  24244. notNeedUndo: 1,
  24245. }
  24246. }
  24247. // plugins/searchreplace.js
  24248. ///import core
  24249. ///commands 查找替换
  24250. ///commandsName SearchReplace
  24251. ///commandsTitle 查询替换
  24252. ///commandsDialog dialogs\searchreplace
  24253. /**
  24254. * @description 查找替换
  24255. * @author zhanyi
  24256. */
  24257. UE.plugin.register('searchreplace', function () {
  24258. var me = this
  24259. var _blockElm = { table: 1, tbody: 1, tr: 1, ol: 1, ul: 1 }
  24260. function findTextInString(textContent, opt, currentIndex) {
  24261. var str = opt.searchStr
  24262. if (opt.dir == -1) {
  24263. textContent = textContent.split('').reverse().join('')
  24264. str = str.split('').reverse().join('')
  24265. currentIndex = textContent.length - currentIndex
  24266. }
  24267. var reg = new RegExp(str, 'g' + (opt.casesensitive ? '' : 'i')),
  24268. match
  24269. while ((match = reg.exec(textContent))) {
  24270. if (match.index >= currentIndex) {
  24271. return opt.dir == -1
  24272. ? textContent.length - match.index - opt.searchStr.length
  24273. : match.index
  24274. }
  24275. }
  24276. return -1
  24277. }
  24278. function findTextBlockElm(node, currentIndex, opt) {
  24279. var textContent,
  24280. index,
  24281. methodName =
  24282. opt.all || opt.dir == 1 ? 'getNextDomNode' : 'getPreDomNode'
  24283. if (domUtils.isBody(node)) {
  24284. node = node.firstChild
  24285. }
  24286. var first = 1
  24287. while (node) {
  24288. textContent =
  24289. node.nodeType == 3
  24290. ? node.nodeValue
  24291. : node[browser.ie ? 'innerText' : 'textContent']
  24292. index = findTextInString(textContent, opt, currentIndex)
  24293. first = 0
  24294. if (index != -1) {
  24295. return {
  24296. node: node,
  24297. index: index,
  24298. }
  24299. }
  24300. node = domUtils[methodName](node)
  24301. while (node && _blockElm[node.nodeName.toLowerCase()]) {
  24302. node = domUtils[methodName](node, true)
  24303. }
  24304. if (node) {
  24305. currentIndex =
  24306. opt.dir == -1
  24307. ? (node.nodeType == 3
  24308. ? node.nodeValue
  24309. : node[browser.ie ? 'innerText' : 'textContent']
  24310. ).length
  24311. : 0
  24312. }
  24313. }
  24314. }
  24315. function findNTextInBlockElm(node, index, str) {
  24316. var currentIndex = 0,
  24317. currentNode = node.firstChild,
  24318. currentNodeLength = 0,
  24319. result
  24320. while (currentNode) {
  24321. if (currentNode.nodeType == 3) {
  24322. currentNodeLength = currentNode.nodeValue.replace(
  24323. /(^[\t\r\n]+)|([\t\r\n]+$)/,
  24324. ''
  24325. ).length
  24326. currentIndex += currentNodeLength
  24327. if (currentIndex >= index) {
  24328. return {
  24329. node: currentNode,
  24330. index: currentNodeLength - (currentIndex - index),
  24331. }
  24332. }
  24333. } else if (!dtd.$empty[currentNode.tagName]) {
  24334. currentNodeLength = currentNode[
  24335. browser.ie ? 'innerText' : 'textContent'
  24336. ].replace(/(^[\t\r\n]+)|([\t\r\n]+$)/, '').length
  24337. currentIndex += currentNodeLength
  24338. if (currentIndex >= index) {
  24339. result = findNTextInBlockElm(
  24340. currentNode,
  24341. currentNodeLength - (currentIndex - index),
  24342. str
  24343. )
  24344. if (result) {
  24345. return result
  24346. }
  24347. }
  24348. }
  24349. currentNode = domUtils.getNextDomNode(currentNode)
  24350. }
  24351. }
  24352. function searchReplace(me, opt) {
  24353. var rng = me.selection.getRange(),
  24354. startBlockNode,
  24355. searchStr = opt.searchStr,
  24356. span = me.document.createElement('span')
  24357. span.innerHTML = '$$ueditor_searchreplace_key$$'
  24358. rng.shrinkBoundary(true)
  24359. //判断是不是第一次选中
  24360. if (!rng.collapsed) {
  24361. rng.select()
  24362. var rngText = me.selection.getText()
  24363. if (
  24364. new RegExp(
  24365. '^' + opt.searchStr + '$',
  24366. opt.casesensitive ? '' : 'i'
  24367. ).test(rngText)
  24368. ) {
  24369. if (opt.replaceStr != undefined) {
  24370. replaceText(rng, opt.replaceStr)
  24371. rng.select()
  24372. return true
  24373. } else {
  24374. rng.collapse(opt.dir == -1)
  24375. }
  24376. }
  24377. }
  24378. rng.insertNode(span)
  24379. rng.enlargeToBlockElm(true)
  24380. startBlockNode = rng.startContainer
  24381. var currentIndex = startBlockNode[
  24382. browser.ie ? 'innerText' : 'textContent'
  24383. ].indexOf('$$ueditor_searchreplace_key$$')
  24384. rng.setStartBefore(span)
  24385. domUtils.remove(span)
  24386. var result = findTextBlockElm(startBlockNode, currentIndex, opt)
  24387. if (result) {
  24388. var rngStart = findNTextInBlockElm(result.node, result.index, searchStr)
  24389. var rngEnd = findNTextInBlockElm(
  24390. result.node,
  24391. result.index + searchStr.length,
  24392. searchStr
  24393. )
  24394. rng
  24395. .setStart(rngStart.node, rngStart.index)
  24396. .setEnd(rngEnd.node, rngEnd.index)
  24397. if (opt.replaceStr !== undefined) {
  24398. replaceText(rng, opt.replaceStr)
  24399. }
  24400. rng.select()
  24401. return true
  24402. } else {
  24403. rng.setCursor()
  24404. }
  24405. }
  24406. function replaceText(rng, str) {
  24407. str = me.document.createTextNode(str)
  24408. rng.deleteContents().insertNode(str)
  24409. }
  24410. return {
  24411. commands: {
  24412. searchreplace: {
  24413. execCommand: function (cmdName, opt) {
  24414. utils.extend(
  24415. opt,
  24416. {
  24417. all: false,
  24418. casesensitive: false,
  24419. dir: 1,
  24420. },
  24421. true
  24422. )
  24423. var num = 0
  24424. if (opt.all) {
  24425. var rng = me.selection.getRange(),
  24426. first = me.body.firstChild
  24427. if (first && first.nodeType == 1) {
  24428. rng.setStart(first, 0)
  24429. rng.shrinkBoundary(true)
  24430. } else if (first.nodeType == 3) {
  24431. rng.setStartBefore(first)
  24432. }
  24433. rng.collapse(true).select(true)
  24434. if (opt.replaceStr !== undefined) {
  24435. me.fireEvent('saveScene')
  24436. }
  24437. while (searchReplace(this, opt)) {
  24438. num++
  24439. }
  24440. if (num) {
  24441. me.fireEvent('saveScene')
  24442. }
  24443. } else {
  24444. if (opt.replaceStr !== undefined) {
  24445. me.fireEvent('saveScene')
  24446. }
  24447. if (searchReplace(this, opt)) {
  24448. num++
  24449. }
  24450. if (num) {
  24451. me.fireEvent('saveScene')
  24452. }
  24453. }
  24454. return num
  24455. },
  24456. notNeedUndo: 1,
  24457. },
  24458. },
  24459. }
  24460. })
  24461. // plugins/customstyle.js
  24462. /**
  24463. * 自定义样式
  24464. * @file
  24465. * @since 1.2.6.1
  24466. */
  24467. /**
  24468. * 根据config配置文件里“customstyle”选项的值对匹配的标签执行样式替换。
  24469. * @command customstyle
  24470. * @method execCommand
  24471. * @param { String } cmd 命令字符串
  24472. * @example
  24473. * ```javascript
  24474. * editor.execCommand( 'customstyle' );
  24475. * ```
  24476. */
  24477. UE.plugins['customstyle'] = function () {
  24478. var me = this
  24479. me.setOpt({
  24480. customstyle: [
  24481. {
  24482. tag: 'h1',
  24483. name: 'tc',
  24484. style:
  24485. 'font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:center;margin:0 0 20px 0;',
  24486. },
  24487. {
  24488. tag: 'h1',
  24489. name: 'tl',
  24490. style:
  24491. 'font-size:32px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:left;margin:0 0 10px 0;',
  24492. },
  24493. {
  24494. tag: 'span',
  24495. name: 'im',
  24496. style:
  24497. 'font-size:16px;font-style:italic;font-weight:bold;line-height:18px;',
  24498. },
  24499. {
  24500. tag: 'span',
  24501. name: 'hi',
  24502. style:
  24503. 'font-size:16px;font-style:italic;font-weight:bold;color:rgb(51, 153, 204);line-height:18px;',
  24504. },
  24505. ],
  24506. })
  24507. me.commands['customstyle'] = {
  24508. execCommand: function (cmdName, obj) {
  24509. var me = this,
  24510. tagName = obj.tag,
  24511. node = domUtils.findParent(
  24512. me.selection.getStart(),
  24513. function (node) {
  24514. return node.getAttribute('label')
  24515. },
  24516. true
  24517. ),
  24518. range,
  24519. bk,
  24520. tmpObj = {}
  24521. for (var p in obj) {
  24522. if (obj[p] !== undefined) tmpObj[p] = obj[p]
  24523. }
  24524. delete tmpObj.tag
  24525. if (node && node.getAttribute('label') == obj.label) {
  24526. range = this.selection.getRange()
  24527. bk = range.createBookmark()
  24528. if (range.collapsed) {
  24529. //trace:1732 删掉自定义标签,要有p来回填站位
  24530. if (dtd.$block[node.tagName]) {
  24531. var fillNode = me.document.createElement('p')
  24532. domUtils.moveChild(node, fillNode)
  24533. node.parentNode.insertBefore(fillNode, node)
  24534. domUtils.remove(node)
  24535. } else {
  24536. domUtils.remove(node, true)
  24537. }
  24538. } else {
  24539. var common = domUtils.getCommonAncestor(bk.start, bk.end),
  24540. nodes = domUtils.getElementsByTagName(common, tagName)
  24541. if (new RegExp(tagName, 'i').test(common.tagName)) {
  24542. nodes.push(common)
  24543. }
  24544. for (var i = 0, ni; (ni = nodes[i++]); ) {
  24545. if (ni.getAttribute('label') == obj.label) {
  24546. var ps = domUtils.getPosition(ni, bk.start),
  24547. pe = domUtils.getPosition(ni, bk.end)
  24548. if (
  24549. (ps & domUtils.POSITION_FOLLOWING ||
  24550. ps & domUtils.POSITION_CONTAINS) &&
  24551. (pe & domUtils.POSITION_PRECEDING ||
  24552. pe & domUtils.POSITION_CONTAINS)
  24553. )
  24554. if (dtd.$block[tagName]) {
  24555. var fillNode = me.document.createElement('p')
  24556. domUtils.moveChild(ni, fillNode)
  24557. ni.parentNode.insertBefore(fillNode, ni)
  24558. }
  24559. domUtils.remove(ni, true)
  24560. }
  24561. }
  24562. node = domUtils.findParent(
  24563. common,
  24564. function (node) {
  24565. return node.getAttribute('label') == obj.label
  24566. },
  24567. true
  24568. )
  24569. if (node) {
  24570. domUtils.remove(node, true)
  24571. }
  24572. }
  24573. range.moveToBookmark(bk).select()
  24574. } else {
  24575. if (dtd.$block[tagName]) {
  24576. this.execCommand('paragraph', tagName, tmpObj, 'customstyle')
  24577. range = me.selection.getRange()
  24578. if (!range.collapsed) {
  24579. range.collapse()
  24580. node = domUtils.findParent(
  24581. me.selection.getStart(),
  24582. function (node) {
  24583. return node.getAttribute('label') == obj.label
  24584. },
  24585. true
  24586. )
  24587. var pNode = me.document.createElement('p')
  24588. domUtils.insertAfter(node, pNode)
  24589. domUtils.fillNode(me.document, pNode)
  24590. range.setStart(pNode, 0).setCursor()
  24591. }
  24592. } else {
  24593. range = me.selection.getRange()
  24594. if (range.collapsed) {
  24595. node = me.document.createElement(tagName)
  24596. domUtils.setAttributes(node, tmpObj)
  24597. range.insertNode(node).setStart(node, 0).setCursor()
  24598. return
  24599. }
  24600. bk = range.createBookmark()
  24601. range.applyInlineStyle(tagName, tmpObj).moveToBookmark(bk).select()
  24602. }
  24603. }
  24604. },
  24605. queryCommandValue: function () {
  24606. var parent = domUtils.filterNodeList(
  24607. this.selection.getStartElementPath(),
  24608. function (node) {
  24609. return node.getAttribute('label')
  24610. }
  24611. )
  24612. return parent ? parent.getAttribute('label') : ''
  24613. },
  24614. }
  24615. //当去掉customstyle是,如果是块元素,用p代替
  24616. me.addListener('keyup', function (type, evt) {
  24617. var keyCode = evt.keyCode || evt.which
  24618. if (keyCode == 32 || keyCode == 13) {
  24619. var range = me.selection.getRange()
  24620. if (range.collapsed) {
  24621. var node = domUtils.findParent(
  24622. me.selection.getStart(),
  24623. function (node) {
  24624. return node.getAttribute('label')
  24625. },
  24626. true
  24627. )
  24628. if (node && dtd.$block[node.tagName] && domUtils.isEmptyNode(node)) {
  24629. var p = me.document.createElement('p')
  24630. domUtils.insertAfter(node, p)
  24631. domUtils.fillNode(me.document, p)
  24632. domUtils.remove(node)
  24633. range.setStart(p, 0).setCursor()
  24634. }
  24635. }
  24636. }
  24637. })
  24638. }
  24639. // plugins/catchremoteimage.js
  24640. ///import core
  24641. ///commands 远程图片抓取
  24642. ///commandsName catchRemoteImage,catchremoteimageenable
  24643. ///commandsTitle 远程图片抓取
  24644. /**
  24645. * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
  24646. */
  24647. UE.plugins['catchremoteimage'] = function () {
  24648. var me = this,
  24649. ajax = UE.ajax
  24650. /* 设置默认值 */
  24651. if (me.options.catchRemoteImageEnable === false) return
  24652. me.setOpt({
  24653. catchRemoteImageEnable: false,
  24654. })
  24655. me.addListener('afterpaste', function () {
  24656. me.fireEvent('catchRemoteImage')
  24657. })
  24658. me.addListener('catchRemoteImage', function () {
  24659. var catcherLocalDomain = me.getOpt('catcherLocalDomain'),
  24660. catcherActionUrl = me.getActionUrl(me.getOpt('catcherActionName')),
  24661. catcherUrlPrefix = me.getOpt('catcherUrlPrefix'),
  24662. catcherFieldName = me.getOpt('catcherFieldName')
  24663. var remoteImages = [],
  24664. imgs = domUtils.getElementsByTagName(me.document, 'img'),
  24665. test = function (src, urls) {
  24666. if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
  24667. return true
  24668. }
  24669. if (urls) {
  24670. for (var j = 0, url; (url = urls[j++]); ) {
  24671. if (src.indexOf(url) !== -1) {
  24672. return true
  24673. }
  24674. }
  24675. }
  24676. return false
  24677. }
  24678. for (var i = 0, ci; (ci = imgs[i++]); ) {
  24679. if (ci.getAttribute('word_img')) {
  24680. continue
  24681. }
  24682. var src = ci.getAttribute('_src') || ci.src || ''
  24683. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  24684. remoteImages.push(src)
  24685. }
  24686. }
  24687. if (remoteImages.length) {
  24688. catchremoteimage(remoteImages, {
  24689. //成功抓取
  24690. success: function (r) {
  24691. try {
  24692. var info =
  24693. r.state !== undefined ? r : eval('(' + r.responseText + ')')
  24694. } catch (e) {
  24695. return
  24696. }
  24697. /* 获取源路径和新路径 */
  24698. var i,
  24699. j,
  24700. ci,
  24701. cj,
  24702. oldSrc,
  24703. newSrc,
  24704. list = info.list
  24705. for (i = 0; (ci = imgs[i++]); ) {
  24706. oldSrc = ci.getAttribute('_src') || ci.src || ''
  24707. for (j = 0; (cj = list[j++]); ) {
  24708. if (oldSrc == cj.source && cj.state == 'SUCCESS') {
  24709. //抓取失败时不做替换处理
  24710. newSrc = catcherUrlPrefix + cj.url
  24711. domUtils.setAttributes(ci, {
  24712. src: newSrc,
  24713. _src: newSrc,
  24714. })
  24715. break
  24716. }
  24717. }
  24718. }
  24719. me.fireEvent('catchremotesuccess')
  24720. },
  24721. //回调失败,本次请求超时
  24722. error: function () {
  24723. me.fireEvent('catchremoteerror')
  24724. },
  24725. })
  24726. }
  24727. function catchremoteimage(imgs, callbacks) {
  24728. var params =
  24729. utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  24730. url = utils.formatUrl(
  24731. catcherActionUrl +
  24732. (catcherActionUrl.indexOf('?') == -1 ? '?' : '&') +
  24733. params
  24734. ),
  24735. isJsonp = utils.isCrossDomainUrl(url),
  24736. opt = {
  24737. method: 'POST',
  24738. dataType: isJsonp ? 'jsonp' : '',
  24739. timeout: 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
  24740. onsuccess: callbacks['success'],
  24741. onerror: callbacks['error'],
  24742. }
  24743. opt[catcherFieldName] = imgs
  24744. ajax.request(url, opt)
  24745. }
  24746. })
  24747. }
  24748. // plugins/snapscreen.js
  24749. /**
  24750. * 截屏插件,为UEditor提供插入支持
  24751. * @file
  24752. * @since 1.4.2
  24753. */
  24754. UE.plugin.register('snapscreen', function () {
  24755. var me = this
  24756. var snapplugin
  24757. function getLocation(url) {
  24758. var search,
  24759. a = document.createElement('a'),
  24760. params = utils.serializeParam(me.queryCommandValue('serverparam')) || ''
  24761. a.href = url
  24762. if (browser.ie) {
  24763. a.href = a.href
  24764. }
  24765. search = a.search
  24766. if (params) {
  24767. search = search + (search.indexOf('?') == -1 ? '?' : '&') + params
  24768. search = search.replace(/[&]+/gi, '&')
  24769. }
  24770. return {
  24771. port: a.port,
  24772. hostname: a.hostname,
  24773. path: a.pathname + search || +a.hash,
  24774. }
  24775. }
  24776. return {
  24777. commands: {
  24778. /**
  24779. * 字体背景颜色
  24780. * @command snapscreen
  24781. * @method execCommand
  24782. * @param { String } cmd 命令字符串
  24783. * @example
  24784. * ```javascript
  24785. * editor.execCommand('snapscreen');
  24786. * ```
  24787. */
  24788. snapscreen: {
  24789. execCommand: function (cmd) {
  24790. var url, local, res
  24791. var lang = me.getLang('snapScreen_plugin')
  24792. if (!snapplugin) {
  24793. var container = me.container
  24794. var doc = me.container.ownerDocument || me.container.document
  24795. snapplugin = doc.createElement('object')
  24796. try {
  24797. snapplugin.type = 'application/x-pluginbaidusnap'
  24798. } catch (e) {
  24799. return
  24800. }
  24801. snapplugin.style.cssText =
  24802. 'position:absolute;left:-9999px;width:0;height:0;'
  24803. snapplugin.setAttribute('width', '0')
  24804. snapplugin.setAttribute('height', '0')
  24805. container.appendChild(snapplugin)
  24806. }
  24807. function onSuccess(rs) {
  24808. try {
  24809. rs = eval('(' + rs + ')')
  24810. if (rs.state == 'SUCCESS') {
  24811. var opt = me.options
  24812. me.execCommand('insertimage', {
  24813. src: opt.snapscreenUrlPrefix + rs.url,
  24814. _src: opt.snapscreenUrlPrefix + rs.url,
  24815. alt: rs.title || '',
  24816. floatStyle: opt.snapscreenImgAlign,
  24817. })
  24818. } else {
  24819. alert(rs.state)
  24820. }
  24821. } catch (e) {
  24822. alert(lang.callBackErrorMsg)
  24823. }
  24824. }
  24825. url = me.getActionUrl(me.getOpt('snapscreenActionName'))
  24826. local = getLocation(url)
  24827. setTimeout(function () {
  24828. try {
  24829. res = snapplugin.saveSnapshot(
  24830. local.hostname,
  24831. local.path,
  24832. local.port
  24833. )
  24834. } catch (e) {
  24835. me.ui._dialogs['snapscreenDialog'].open()
  24836. return
  24837. }
  24838. onSuccess(res)
  24839. }, 50)
  24840. },
  24841. queryCommandState: function () {
  24842. return navigator.userAgent.indexOf('Windows', 0) != -1 ? 0 : -1
  24843. },
  24844. },
  24845. },
  24846. }
  24847. })
  24848. // plugins/insertparagraph.js
  24849. /**
  24850. * 插入段落
  24851. * @file
  24852. * @since 1.2.6.1
  24853. */
  24854. /**
  24855. * 插入段落
  24856. * @command insertparagraph
  24857. * @method execCommand
  24858. * @param { String } cmd 命令字符串
  24859. * @example
  24860. * ```javascript
  24861. * //editor是编辑器实例
  24862. * editor.execCommand( 'insertparagraph' );
  24863. * ```
  24864. */
  24865. UE.commands['insertparagraph'] = {
  24866. execCommand: function (cmdName, front) {
  24867. var me = this,
  24868. range = me.selection.getRange(),
  24869. start = range.startContainer,
  24870. tmpNode
  24871. while (start) {
  24872. if (domUtils.isBody(start)) {
  24873. break
  24874. }
  24875. tmpNode = start
  24876. start = start.parentNode
  24877. }
  24878. if (tmpNode) {
  24879. var p = me.document.createElement('p')
  24880. if (front) {
  24881. tmpNode.parentNode.insertBefore(p, tmpNode)
  24882. } else {
  24883. tmpNode.parentNode.insertBefore(p, tmpNode.nextSibling)
  24884. }
  24885. domUtils.fillNode(me.document, p)
  24886. range.setStart(p, 0).setCursor(false, true)
  24887. }
  24888. },
  24889. }
  24890. // plugins/webapp.js
  24891. /**
  24892. * 百度应用
  24893. * @file
  24894. * @since 1.2.6.1
  24895. */
  24896. /**
  24897. * 插入百度应用
  24898. * @command webapp
  24899. * @method execCommand
  24900. * @remind 需要百度APPKey
  24901. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  24902. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  24903. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  24904. * @example
  24905. * ```javascript
  24906. * //editor是编辑器实例
  24907. * //在编辑器里插入一个“植物大战僵尸”的APP
  24908. * editor.execCommand( 'webapp' , {
  24909. * title: '植物大战僵尸',
  24910. * width: 560,
  24911. * height: 465,
  24912. * logo: '应用展示的图片',
  24913. * url: '百度应用的地址'
  24914. * } );
  24915. * ```
  24916. */
  24917. //UE.plugins['webapp'] = function () {
  24918. // var me = this;
  24919. // function createInsertStr( obj, toIframe, addParagraph ) {
  24920. // return !toIframe ?
  24921. // (addParagraph ? '<p>' : '') + '<img title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '"' +
  24922. // ' src="' + me.options.UEDITOR_HOME_URL + 'themes/default/images/spacer.gif" style="background:url(' + obj.logo+') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' + obj.url + '" />' +
  24923. // (addParagraph ? '</p>' : '')
  24924. // :
  24925. // '<iframe class="edui-faked-webapp" title="'+obj.title+'" width="' + obj.width + '" height="' + obj.height + '" scrolling="no" frameborder="0" src="' + obj.url + '" logo_url = '+obj.logo+'></iframe>';
  24926. // }
  24927. //
  24928. // function switchImgAndIframe( img2frame ) {
  24929. // var tmpdiv,
  24930. // nodes = domUtils.getElementsByTagName( me.document, !img2frame ? "iframe" : "img" );
  24931. // for ( var i = 0, node; node = nodes[i++]; ) {
  24932. // if ( node.className != "edui-faked-webapp" ){
  24933. // continue;
  24934. // }
  24935. // tmpdiv = me.document.createElement( "div" );
  24936. // tmpdiv.innerHTML = createInsertStr( img2frame ? {url:node.getAttribute( "_url" ), width:node.width, height:node.height,title:node.title,logo:node.style.backgroundImage.replace("url(","").replace(")","")} : {url:node.getAttribute( "src", 2 ),title:node.title, width:node.width, height:node.height,logo:node.getAttribute("logo_url")}, img2frame ? true : false,false );
  24937. // node.parentNode.replaceChild( tmpdiv.firstChild, node );
  24938. // }
  24939. // }
  24940. //
  24941. // me.addListener( "beforegetcontent", function () {
  24942. // switchImgAndIframe( true );
  24943. // } );
  24944. // me.addListener( 'aftersetcontent', function () {
  24945. // switchImgAndIframe( false );
  24946. // } );
  24947. // me.addListener( 'aftergetcontent', function ( cmdName ) {
  24948. // if ( cmdName == 'aftergetcontent' && me.queryCommandState( 'source' ) ){
  24949. // return;
  24950. // }
  24951. // switchImgAndIframe( false );
  24952. // } );
  24953. //
  24954. // me.commands['webapp'] = {
  24955. // execCommand:function ( cmd, obj ) {
  24956. // me.execCommand( "inserthtml", createInsertStr( obj, false,true ) );
  24957. // }
  24958. // };
  24959. //};
  24960. UE.plugin.register('webapp', function () {
  24961. var me = this
  24962. function createInsertStr(obj, toEmbed) {
  24963. return !toEmbed
  24964. ? '<img title="' +
  24965. obj.title +
  24966. '" width="' +
  24967. obj.width +
  24968. '" height="' +
  24969. obj.height +
  24970. '"' +
  24971. ' src="' +
  24972. me.options.UEDITOR_HOME_URL +
  24973. 'themes/default/images/spacer.gif" _logo_url="' +
  24974. obj.logo +
  24975. '" style="background:url(' +
  24976. obj.logo +
  24977. ') no-repeat center center; border:1px solid gray;" class="edui-faked-webapp" _url="' +
  24978. obj.url +
  24979. '" ' +
  24980. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : '') +
  24981. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  24982. '/>'
  24983. : '<iframe class="edui-faked-webapp" title="' +
  24984. obj.title +
  24985. '" ' +
  24986. (obj.align && !obj.cssfloat ? 'align="' + obj.align + '"' : '') +
  24987. (obj.cssfloat ? 'style="float:' + obj.cssfloat + '"' : '') +
  24988. 'width="' +
  24989. obj.width +
  24990. '" height="' +
  24991. obj.height +
  24992. '" scrolling="no" frameborder="0" src="' +
  24993. obj.url +
  24994. '" logo_url = "' +
  24995. obj.logo +
  24996. '"></iframe>'
  24997. }
  24998. return {
  24999. outputRule: function (root) {
  25000. utils.each(root.getNodesByTagName('img'), function (node) {
  25001. var html
  25002. if (node.getAttr('class') == 'edui-faked-webapp') {
  25003. html = createInsertStr(
  25004. {
  25005. title: node.getAttr('title'),
  25006. width: node.getAttr('width'),
  25007. height: node.getAttr('height'),
  25008. align: node.getAttr('align'),
  25009. cssfloat: node.getStyle('float'),
  25010. url: node.getAttr('_url'),
  25011. logo: node.getAttr('_logo_url'),
  25012. },
  25013. true
  25014. )
  25015. var embed = UE.uNode.createElement(html)
  25016. node.parentNode.replaceChild(embed, node)
  25017. }
  25018. })
  25019. },
  25020. inputRule: function (root) {
  25021. utils.each(root.getNodesByTagName('iframe'), function (node) {
  25022. if (node.getAttr('class') == 'edui-faked-webapp') {
  25023. var img = UE.uNode.createElement(
  25024. createInsertStr({
  25025. title: node.getAttr('title'),
  25026. width: node.getAttr('width'),
  25027. height: node.getAttr('height'),
  25028. align: node.getAttr('align'),
  25029. cssfloat: node.getStyle('float'),
  25030. url: node.getAttr('src'),
  25031. logo: node.getAttr('logo_url'),
  25032. })
  25033. )
  25034. node.parentNode.replaceChild(img, node)
  25035. }
  25036. })
  25037. },
  25038. commands: {
  25039. /**
  25040. * 插入百度应用
  25041. * @command webapp
  25042. * @method execCommand
  25043. * @remind 需要百度APPKey
  25044. * @remind 百度应用主页: <a href="http://app.baidu.com/" target="_blank">http://app.baidu.com/</a>
  25045. * @param { Object } appOptions 应用所需的参数项, 支持的key有: title=>应用标题, width=>应用容器宽度,
  25046. * height=>应用容器高度,logo=>应用logo,url=>应用地址
  25047. * @example
  25048. * ```javascript
  25049. * //editor是编辑器实例
  25050. * //在编辑器里插入一个“植物大战僵尸”的APP
  25051. * editor.execCommand( 'webapp' , {
  25052. * title: '植物大战僵尸',
  25053. * width: 560,
  25054. * height: 465,
  25055. * logo: '应用展示的图片',
  25056. * url: '百度应用的地址'
  25057. * } );
  25058. * ```
  25059. */
  25060. webapp: {
  25061. execCommand: function (cmd, obj) {
  25062. var me = this,
  25063. str = createInsertStr(
  25064. utils.extend(obj, {
  25065. align: 'none',
  25066. }),
  25067. false
  25068. )
  25069. me.execCommand('inserthtml', str)
  25070. },
  25071. queryCommandState: function () {
  25072. var me = this,
  25073. img = me.selection.getRange().getClosedNode(),
  25074. flag = img && img.className == 'edui-faked-webapp'
  25075. return flag ? 1 : 0
  25076. },
  25077. },
  25078. },
  25079. }
  25080. })
  25081. // plugins/template.js
  25082. ///import core
  25083. ///import plugins\inserthtml.js
  25084. ///import plugins\cleardoc.js
  25085. ///commands 模板
  25086. ///commandsName template
  25087. ///commandsTitle 模板
  25088. ///commandsDialog dialogs\template
  25089. UE.plugins['template'] = function () {
  25090. UE.commands['template'] = {
  25091. execCommand: function (cmd, obj) {
  25092. obj.html && this.execCommand('inserthtml', obj.html)
  25093. },
  25094. }
  25095. this.addListener('click', function (type, evt) {
  25096. var el = evt.target || evt.srcElement,
  25097. range = this.selection.getRange()
  25098. var tnode = domUtils.findParent(
  25099. el,
  25100. function (node) {
  25101. if (node.className && domUtils.hasClass(node, 'ue_t')) {
  25102. return node
  25103. }
  25104. },
  25105. true
  25106. )
  25107. tnode && range.selectNode(tnode).shrinkBoundary().select()
  25108. })
  25109. this.addListener('keydown', function (type, evt) {
  25110. var range = this.selection.getRange()
  25111. if (!range.collapsed) {
  25112. if (!evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  25113. var tnode = domUtils.findParent(
  25114. range.startContainer,
  25115. function (node) {
  25116. if (node.className && domUtils.hasClass(node, 'ue_t')) {
  25117. return node
  25118. }
  25119. },
  25120. true
  25121. )
  25122. if (tnode) {
  25123. domUtils.removeClasses(tnode, ['ue_t'])
  25124. }
  25125. }
  25126. }
  25127. })
  25128. }
  25129. // plugins/music.js
  25130. /**
  25131. * 插入音乐命令
  25132. * @file
  25133. */
  25134. UE.plugin.register('music', function () {
  25135. var me = this
  25136. function creatInsertStr(url, width, height, align, cssfloat, toEmbed) {
  25137. return !toEmbed
  25138. ? '<img ' +
  25139. (align && !cssfloat ? 'align="' + align + '"' : '') +
  25140. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  25141. ' width="' +
  25142. width +
  25143. '" height="' +
  25144. height +
  25145. '" _url="' +
  25146. url +
  25147. '" class="edui-faked-music"' +
  25148. ' src="' +
  25149. me.options.langPath +
  25150. me.options.lang +
  25151. '/images/music.png" />'
  25152. : '<embed type="application/x-shockwave-flash" class="edui-faked-music" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  25153. ' src="' +
  25154. url +
  25155. '" width="' +
  25156. width +
  25157. '" height="' +
  25158. height +
  25159. '" ' +
  25160. (align && !cssfloat ? 'align="' + align + '"' : '') +
  25161. (cssfloat ? 'style="float:' + cssfloat + '"' : '') +
  25162. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >'
  25163. }
  25164. return {
  25165. outputRule: function (root) {
  25166. utils.each(root.getNodesByTagName('img'), function (node) {
  25167. var html
  25168. if (node.getAttr('class') == 'edui-faked-music') {
  25169. var cssfloat = node.getStyle('float')
  25170. var align = node.getAttr('align')
  25171. html = creatInsertStr(
  25172. node.getAttr('_url'),
  25173. node.getAttr('width'),
  25174. node.getAttr('height'),
  25175. align,
  25176. cssfloat,
  25177. true
  25178. )
  25179. var embed = UE.uNode.createElement(html)
  25180. node.parentNode.replaceChild(embed, node)
  25181. }
  25182. })
  25183. },
  25184. inputRule: function (root) {
  25185. utils.each(root.getNodesByTagName('embed'), function (node) {
  25186. if (node.getAttr('class') == 'edui-faked-music') {
  25187. var cssfloat = node.getStyle('float')
  25188. var align = node.getAttr('align')
  25189. html = creatInsertStr(
  25190. node.getAttr('src'),
  25191. node.getAttr('width'),
  25192. node.getAttr('height'),
  25193. align,
  25194. cssfloat,
  25195. false
  25196. )
  25197. var img = UE.uNode.createElement(html)
  25198. node.parentNode.replaceChild(img, node)
  25199. }
  25200. })
  25201. },
  25202. commands: {
  25203. /**
  25204. * 插入音乐
  25205. * @command music
  25206. * @method execCommand
  25207. * @param { Object } musicOptions 插入音乐的参数项, 支持的key有: url=>音乐地址;
  25208. * width=>音乐容器宽度;height=>音乐容器高度;align=>音乐文件的对齐方式, 可选值有: left, center, right, none
  25209. * @example
  25210. * ```javascript
  25211. * //editor是编辑器实例
  25212. * //在编辑器里插入一个“植物大战僵尸”的APP
  25213. * editor.execCommand( 'music' , {
  25214. * width: 400,
  25215. * height: 95,
  25216. * align: "center",
  25217. * url: "音乐地址"
  25218. * } );
  25219. * ```
  25220. */
  25221. music: {
  25222. execCommand: function (cmd, musicObj) {
  25223. var me = this,
  25224. str = creatInsertStr(
  25225. musicObj.url,
  25226. musicObj.width || 400,
  25227. musicObj.height || 95,
  25228. 'none',
  25229. false
  25230. )
  25231. me.execCommand('inserthtml', str)
  25232. },
  25233. queryCommandState: function () {
  25234. var me = this,
  25235. img = me.selection.getRange().getClosedNode(),
  25236. flag = img && img.className == 'edui-faked-music'
  25237. return flag ? 1 : 0
  25238. },
  25239. },
  25240. },
  25241. }
  25242. })
  25243. // plugins/autoupload.js
  25244. /**
  25245. * @description
  25246. * 1.拖放文件到编辑区域,自动上传并插入到选区
  25247. * 2.插入粘贴板的图片,自动上传并插入到选区
  25248. * @author Jinqn
  25249. * @date 2013-10-14
  25250. */
  25251. UE.plugin.register('autoupload', function () {
  25252. function sendAndInsertFile(file, editor) {
  25253. var me = editor
  25254. //模拟数据
  25255. var fieldName,
  25256. urlPrefix,
  25257. maxSize,
  25258. allowFiles,
  25259. actionUrl,
  25260. loadingHtml,
  25261. errorHandler,
  25262. successHandler,
  25263. filetype = /image\/\w+/i.test(file.type) ? 'image' : 'file',
  25264. loadingId = 'loading_' + (+new Date()).toString(36)
  25265. fieldName = me.getOpt(filetype + 'FieldName')
  25266. urlPrefix = me.getOpt(filetype + 'UrlPrefix')
  25267. maxSize = me.getOpt(filetype + 'MaxSize')
  25268. allowFiles = me.getOpt(filetype + 'AllowFiles')
  25269. actionUrl = me.getActionUrl(me.getOpt(filetype + 'ActionName'))
  25270. errorHandler = function (title) {
  25271. var loader = me.document.getElementById(loadingId)
  25272. loader && domUtils.remove(loader)
  25273. me.fireEvent('showmessage', {
  25274. id: loadingId,
  25275. content: title,
  25276. type: 'error',
  25277. timeout: 4000,
  25278. })
  25279. }
  25280. if (filetype == 'image') {
  25281. loadingHtml =
  25282. '<img class="loadingclass" id="' +
  25283. loadingId +
  25284. '" src="' +
  25285. me.options.themePath +
  25286. me.options.theme +
  25287. '/images/spacer.gif" title="' +
  25288. (me.getLang('autoupload.loading') || '') +
  25289. '" >'
  25290. successHandler = function (data) {
  25291. var link = urlPrefix + data.url,
  25292. loader = me.document.getElementById(loadingId)
  25293. if (loader) {
  25294. loader.setAttribute('src', link)
  25295. loader.setAttribute('_src', link)
  25296. loader.setAttribute('title', data.title || '')
  25297. loader.setAttribute('alt', data.original || '')
  25298. loader.removeAttribute('id')
  25299. domUtils.removeClasses(loader, 'loadingclass')
  25300. }
  25301. }
  25302. } else {
  25303. loadingHtml =
  25304. '<p>' +
  25305. '<img class="loadingclass" id="' +
  25306. loadingId +
  25307. '" src="' +
  25308. me.options.themePath +
  25309. me.options.theme +
  25310. '/images/spacer.gif" title="' +
  25311. (me.getLang('autoupload.loading') || '') +
  25312. '" >' +
  25313. '</p>'
  25314. successHandler = function (data) {
  25315. var link = urlPrefix + data.url,
  25316. loader = me.document.getElementById(loadingId)
  25317. var rng = me.selection.getRange(),
  25318. bk = rng.createBookmark()
  25319. rng.selectNode(loader).select()
  25320. me.execCommand('insertfile', { url: link })
  25321. rng.moveToBookmark(bk).select()
  25322. }
  25323. }
  25324. /* 插入loading的占位符 */
  25325. me.execCommand('inserthtml', loadingHtml)
  25326. /* 判断后端配置是否没有加载成功 */
  25327. if (!me.getOpt(filetype + 'ActionName')) {
  25328. console.log('此处baocuo')
  25329. errorHandler(me.getLang('autoupload.errorLoadConfig'))
  25330. return
  25331. }
  25332. /* 判断文件大小是否超出限制 */
  25333. if (file.size > maxSize) {
  25334. errorHandler(me.getLang('autoupload.exceedSizeError'))
  25335. return
  25336. }
  25337. /* 判断文件格式是否超出允许 */
  25338. var fileext = file.name
  25339. ? file.name.substr(file.name.lastIndexOf('.'))
  25340. : ''
  25341. if (
  25342. (fileext && filetype != 'image') ||
  25343. (allowFiles &&
  25344. (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') ==
  25345. -1)
  25346. ) {
  25347. errorHandler(me.getLang('autoupload.exceedTypeError'))
  25348. return
  25349. }
  25350. /* 创建Ajax并提交 */
  25351. var xhr = new XMLHttpRequest(),
  25352. fd = new FormData(),
  25353. params =
  25354. utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  25355. url = utils.formatUrl(
  25356. actionUrl + (actionUrl.indexOf('?') == -1 ? '?' : '&') + params
  25357. )
  25358. fd.append(
  25359. fieldName,
  25360. file,
  25361. file.name || 'blob.' + file.type.substr('image/'.length)
  25362. )
  25363. fd.append('type', 'ajax')
  25364. xhr.open('post', url, true)
  25365. xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
  25366. xhr.addEventListener('load', function (e) {
  25367. try {
  25368. var json = new Function('return ' + utils.trim(e.target.response))()
  25369. if (json.state == 'SUCCESS' && json.url) {
  25370. successHandler(json)
  25371. } else {
  25372. errorHandler(json.state)
  25373. }
  25374. } catch (er) {
  25375. errorHandler(me.getLang('autoupload.loadError'))
  25376. }
  25377. })
  25378. xhr.send(fd)
  25379. }
  25380. function getPasteImage(e) {
  25381. return e.clipboardData &&
  25382. e.clipboardData.items &&
  25383. e.clipboardData.items.length == 1 &&
  25384. /^image\//.test(e.clipboardData.items[0].type)
  25385. ? e.clipboardData.items
  25386. : null
  25387. }
  25388. function getDropImage(e) {
  25389. return e.dataTransfer && e.dataTransfer.files
  25390. ? e.dataTransfer.files
  25391. : null
  25392. }
  25393. return {
  25394. outputRule: function (root) {
  25395. utils.each(root.getNodesByTagName('img'), function (n) {
  25396. if (
  25397. /\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))
  25398. ) {
  25399. n.parentNode.removeChild(n)
  25400. }
  25401. })
  25402. utils.each(root.getNodesByTagName('p'), function (n) {
  25403. if (/\bloadpara\b/.test(n.getAttr('class'))) {
  25404. n.parentNode.removeChild(n)
  25405. }
  25406. })
  25407. },
  25408. bindEvents: {
  25409. //插入粘贴板的图片,拖放插入图片
  25410. ready: function (e) {
  25411. var me = this
  25412. if (window.FormData && window.FileReader) {
  25413. domUtils.on(me.body, 'paste drop', function (e) {
  25414. var hasImg = false,
  25415. items
  25416. //获取粘贴板文件列表或者拖放文件列表
  25417. items = e.type == 'paste' ? getPasteImage(e) : getDropImage(e)
  25418. if (items) {
  25419. var len = items.length,
  25420. file
  25421. while (len--) {
  25422. file = items[len]
  25423. if (file.getAsFile) file = file.getAsFile()
  25424. if (file && file.size > 0) {
  25425. sendAndInsertFile(file, me)
  25426. hasImg = true
  25427. }
  25428. }
  25429. hasImg && e.preventDefault()
  25430. }
  25431. })
  25432. //取消拖放图片时出现的文字光标位置提示
  25433. domUtils.on(me.body, 'dragover', function (e) {
  25434. if (e.dataTransfer.types[0] == 'Files') {
  25435. e.preventDefault()
  25436. }
  25437. })
  25438. //设置loading的样式
  25439. utils.cssRule(
  25440. 'loading',
  25441. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  25442. this.options.themePath +
  25443. this.options.theme +
  25444. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-left:1px;height: 22px;width: 22px;}\n" +
  25445. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  25446. this.options.themePath +
  25447. this.options.theme +
  25448. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  25449. '}',
  25450. this.document
  25451. )
  25452. }
  25453. },
  25454. },
  25455. }
  25456. })
  25457. // plugins/autosave.js
  25458. UE.plugin.register('autosave', function () {
  25459. var me = this,
  25460. //无限循环保护
  25461. lastSaveTime = new Date(),
  25462. //最小保存间隔时间
  25463. MIN_TIME = 20,
  25464. //auto save key
  25465. saveKey = null
  25466. function save(editor) {
  25467. var saveData
  25468. if (new Date() - lastSaveTime < MIN_TIME) {
  25469. return
  25470. }
  25471. if (!editor.hasContents()) {
  25472. //这里不能调用命令来删除, 会造成事件死循环
  25473. saveKey && me.removePreferences(saveKey)
  25474. return
  25475. }
  25476. lastSaveTime = new Date()
  25477. editor._saveFlag = null
  25478. saveData = me.body.innerHTML
  25479. if (
  25480. editor.fireEvent('beforeautosave', {
  25481. content: saveData,
  25482. }) === false
  25483. ) {
  25484. return
  25485. }
  25486. me.setPreferences(saveKey, saveData)
  25487. editor.fireEvent('afterautosave', {
  25488. content: saveData,
  25489. })
  25490. }
  25491. return {
  25492. defaultOptions: {
  25493. //默认间隔时间
  25494. saveInterval: 500,
  25495. enableAutoSave: true, // HaoChuan9421
  25496. },
  25497. bindEvents: {
  25498. ready: function () {
  25499. var _suffix = '-drafts-data',
  25500. key = null
  25501. if (me.key) {
  25502. key = me.key + _suffix
  25503. } else {
  25504. key = (me.container.parentNode.id || 'ue-common') + _suffix
  25505. }
  25506. //页面地址+编辑器ID 保持唯一
  25507. saveKey =
  25508. (location.protocol + location.host + location.pathname).replace(
  25509. /[.:\/]/g,
  25510. '_'
  25511. ) + key
  25512. },
  25513. contentchange: function () {
  25514. // HaoChuan9421
  25515. if (!me.getOpt('enableAutoSave')) {
  25516. return
  25517. }
  25518. if (!saveKey) {
  25519. return
  25520. }
  25521. if (me._saveFlag) {
  25522. window.clearTimeout(me._saveFlag)
  25523. }
  25524. if (me.options.saveInterval > 0) {
  25525. me._saveFlag = window.setTimeout(function () {
  25526. save(me)
  25527. }, me.options.saveInterval)
  25528. } else {
  25529. save(me)
  25530. }
  25531. },
  25532. },
  25533. commands: {
  25534. clearlocaldata: {
  25535. execCommand: function (cmd, name) {
  25536. if (saveKey && me.getPreferences(saveKey)) {
  25537. me.removePreferences(saveKey)
  25538. }
  25539. },
  25540. notNeedUndo: true,
  25541. ignoreContentChange: true,
  25542. },
  25543. getlocaldata: {
  25544. execCommand: function (cmd, name) {
  25545. return saveKey ? me.getPreferences(saveKey) || '' : ''
  25546. },
  25547. notNeedUndo: true,
  25548. ignoreContentChange: true,
  25549. },
  25550. drafts: {
  25551. execCommand: function (cmd, name) {
  25552. if (saveKey) {
  25553. me.body.innerHTML =
  25554. me.getPreferences(saveKey) || '<p>' + domUtils.fillHtml + '</p>'
  25555. me.focus(true)
  25556. }
  25557. },
  25558. queryCommandState: function () {
  25559. return saveKey ? (me.getPreferences(saveKey) === null ? -1 : 0) : -1
  25560. },
  25561. notNeedUndo: true,
  25562. ignoreContentChange: true,
  25563. },
  25564. },
  25565. }
  25566. })
  25567. // plugins/charts.js
  25568. UE.plugin.register('charts', function () {
  25569. var me = this
  25570. return {
  25571. bindEvents: {
  25572. chartserror: function () {},
  25573. },
  25574. commands: {
  25575. charts: {
  25576. execCommand: function (cmd, data) {
  25577. var tableNode = domUtils.findParentByTagName(
  25578. this.selection.getRange().startContainer,
  25579. 'table',
  25580. true
  25581. ),
  25582. flagText = [],
  25583. config = {}
  25584. if (!tableNode) {
  25585. return false
  25586. }
  25587. if (!validData(tableNode)) {
  25588. me.fireEvent('chartserror')
  25589. return false
  25590. }
  25591. config.title = data.title || ''
  25592. config.subTitle = data.subTitle || ''
  25593. config.xTitle = data.xTitle || ''
  25594. config.yTitle = data.yTitle || ''
  25595. config.suffix = data.suffix || ''
  25596. config.tip = data.tip || ''
  25597. //数据对齐方式
  25598. config.dataFormat = data.tableDataFormat || ''
  25599. //图表类型
  25600. config.chartType = data.chartType || 0
  25601. for (var key in config) {
  25602. if (!config.hasOwnProperty(key)) {
  25603. continue
  25604. }
  25605. flagText.push(key + ':' + config[key])
  25606. }
  25607. tableNode.setAttribute('data-chart', flagText.join(';'))
  25608. domUtils.addClass(tableNode, 'edui-charts-table')
  25609. },
  25610. queryCommandState: function (cmd, name) {
  25611. var tableNode = domUtils.findParentByTagName(
  25612. this.selection.getRange().startContainer,
  25613. 'table',
  25614. true
  25615. )
  25616. return tableNode && validData(tableNode) ? 0 : -1
  25617. },
  25618. },
  25619. },
  25620. inputRule: function (root) {
  25621. utils.each(root.getNodesByTagName('table'), function (tableNode) {
  25622. if (tableNode.getAttr('data-chart') !== undefined) {
  25623. tableNode.setAttr('style')
  25624. }
  25625. })
  25626. },
  25627. outputRule: function (root) {
  25628. utils.each(root.getNodesByTagName('table'), function (tableNode) {
  25629. if (tableNode.getAttr('data-chart') !== undefined) {
  25630. tableNode.setAttr('style', 'display: none;')
  25631. }
  25632. })
  25633. },
  25634. }
  25635. function validData(table) {
  25636. var firstRows = null,
  25637. cellCount = 0
  25638. //行数不够
  25639. if (table.rows.length < 2) {
  25640. return false
  25641. }
  25642. //列数不够
  25643. if (table.rows[0].cells.length < 2) {
  25644. return false
  25645. }
  25646. //第一行所有cell必须是th
  25647. firstRows = table.rows[0].cells
  25648. cellCount = firstRows.length
  25649. for (var i = 0, cell; (cell = firstRows[i]); i++) {
  25650. if (cell.tagName.toLowerCase() !== 'th') {
  25651. return false
  25652. }
  25653. }
  25654. for (var i = 1, row; (row = table.rows[i]); i++) {
  25655. //每行单元格数不匹配, 返回false
  25656. if (row.cells.length != cellCount) {
  25657. return false
  25658. }
  25659. //第一列不是th也返回false
  25660. if (row.cells[0].tagName.toLowerCase() !== 'th') {
  25661. return false
  25662. }
  25663. for (var j = 1, cell; (cell = row.cells[j]); j++) {
  25664. var value = utils.trim(cell.innerText || cell.textContent || '')
  25665. value = value
  25666. .replace(new RegExp(UE.dom.domUtils.fillChar, 'g'), '')
  25667. .replace(/^\s+|\s+$/g, '')
  25668. //必须是数字
  25669. if (!/^\d*\.?\d+$/.test(value)) {
  25670. return false
  25671. }
  25672. }
  25673. }
  25674. return true
  25675. }
  25676. })
  25677. // plugins/section.js
  25678. /**
  25679. * 目录大纲支持插件
  25680. * @file
  25681. * @since 1.3.0
  25682. */
  25683. UE.plugin.register('section', function () {
  25684. /* 目录节点对象 */
  25685. function Section(option) {
  25686. this.tag = ''
  25687. ;(this.level = -1), (this.dom = null)
  25688. this.nextSection = null
  25689. this.previousSection = null
  25690. this.parentSection = null
  25691. this.startAddress = []
  25692. this.endAddress = []
  25693. this.children = []
  25694. }
  25695. function getSection(option) {
  25696. var section = new Section()
  25697. return utils.extend(section, option)
  25698. }
  25699. function getNodeFromAddress(startAddress, root) {
  25700. var current = root
  25701. for (var i = 0; i < startAddress.length; i++) {
  25702. if (!current.childNodes) return null
  25703. current = current.childNodes[startAddress[i]]
  25704. }
  25705. return current
  25706. }
  25707. var me = this
  25708. return {
  25709. bindMultiEvents: {
  25710. type: 'aftersetcontent afterscencerestore',
  25711. handler: function () {
  25712. me.fireEvent('updateSections')
  25713. },
  25714. },
  25715. bindEvents: {
  25716. /* 初始化、拖拽、粘贴、执行setcontent之后 */
  25717. ready: function () {
  25718. me.fireEvent('updateSections')
  25719. domUtils.on(me.body, 'drop paste', function () {
  25720. me.fireEvent('updateSections')
  25721. })
  25722. },
  25723. /* 执行paragraph命令之后 */
  25724. afterexeccommand: function (type, cmd) {
  25725. if (cmd == 'paragraph') {
  25726. me.fireEvent('updateSections')
  25727. }
  25728. },
  25729. /* 部分键盘操作,触发updateSections事件 */
  25730. keyup: function (type, e) {
  25731. var me = this,
  25732. range = me.selection.getRange()
  25733. if (range.collapsed != true) {
  25734. me.fireEvent('updateSections')
  25735. } else {
  25736. var keyCode = e.keyCode || e.which
  25737. if (keyCode == 13 || keyCode == 8 || keyCode == 46) {
  25738. me.fireEvent('updateSections')
  25739. }
  25740. }
  25741. },
  25742. },
  25743. commands: {
  25744. getsections: {
  25745. execCommand: function (cmd, levels) {
  25746. var levelFn = levels || ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
  25747. for (var i = 0; i < levelFn.length; i++) {
  25748. if (typeof levelFn[i] == 'string') {
  25749. levelFn[i] = (function (fn) {
  25750. return function (node) {
  25751. return node.tagName == fn.toUpperCase()
  25752. }
  25753. })(levelFn[i])
  25754. } else if (typeof levelFn[i] != 'function') {
  25755. levelFn[i] = function (node) {
  25756. return null
  25757. }
  25758. }
  25759. }
  25760. function getSectionLevel(node) {
  25761. for (var i = 0; i < levelFn.length; i++) {
  25762. if (levelFn[i](node)) return i
  25763. }
  25764. return -1
  25765. }
  25766. var me = this,
  25767. Directory = getSection({ level: -1, title: 'root' }),
  25768. previous = Directory
  25769. function traversal(node, Directory) {
  25770. var level,
  25771. tmpSection = null,
  25772. parent,
  25773. child,
  25774. children = node.childNodes
  25775. for (var i = 0, len = children.length; i < len; i++) {
  25776. child = children[i]
  25777. level = getSectionLevel(child)
  25778. if (level >= 0) {
  25779. var address = me.selection
  25780. .getRange()
  25781. .selectNode(child)
  25782. .createAddress(true).startAddress,
  25783. current = getSection({
  25784. tag: child.tagName,
  25785. title: child.innerText || child.textContent || '',
  25786. level: level,
  25787. dom: child,
  25788. startAddress: utils.clone(address, []),
  25789. endAddress: utils.clone(address, []),
  25790. children: [],
  25791. })
  25792. previous.nextSection = current
  25793. current.previousSection = previous
  25794. parent = previous
  25795. while (level <= parent.level) {
  25796. parent = parent.parentSection
  25797. }
  25798. current.parentSection = parent
  25799. parent.children.push(current)
  25800. tmpSection = previous = current
  25801. } else {
  25802. child.nodeType === 1 && traversal(child, Directory)
  25803. tmpSection &&
  25804. tmpSection.endAddress[tmpSection.endAddress.length - 1]++
  25805. }
  25806. }
  25807. }
  25808. traversal(me.body, Directory)
  25809. return Directory
  25810. },
  25811. notNeedUndo: true,
  25812. },
  25813. movesection: {
  25814. execCommand: function (cmd, sourceSection, targetSection, isAfter) {
  25815. var me = this,
  25816. targetAddress,
  25817. target
  25818. if (!sourceSection || !targetSection || targetSection.level == -1)
  25819. return
  25820. targetAddress = isAfter
  25821. ? targetSection.endAddress
  25822. : targetSection.startAddress
  25823. target = getNodeFromAddress(targetAddress, me.body)
  25824. /* 判断目标地址是否被源章节包含 */
  25825. if (
  25826. !targetAddress ||
  25827. !target ||
  25828. isContainsAddress(
  25829. sourceSection.startAddress,
  25830. sourceSection.endAddress,
  25831. targetAddress
  25832. )
  25833. )
  25834. return
  25835. var startNode = getNodeFromAddress(
  25836. sourceSection.startAddress,
  25837. me.body
  25838. ),
  25839. endNode = getNodeFromAddress(sourceSection.endAddress, me.body),
  25840. current,
  25841. nextNode
  25842. if (isAfter) {
  25843. current = endNode
  25844. while (
  25845. current &&
  25846. !(
  25847. domUtils.getPosition(startNode, current) &
  25848. domUtils.POSITION_FOLLOWING
  25849. )
  25850. ) {
  25851. nextNode = current.previousSibling
  25852. domUtils.insertAfter(target, current)
  25853. if (current == startNode) break
  25854. current = nextNode
  25855. }
  25856. } else {
  25857. current = startNode
  25858. while (
  25859. current &&
  25860. !(
  25861. domUtils.getPosition(current, endNode) &
  25862. domUtils.POSITION_FOLLOWING
  25863. )
  25864. ) {
  25865. nextNode = current.nextSibling
  25866. target.parentNode.insertBefore(current, target)
  25867. if (current == endNode) break
  25868. current = nextNode
  25869. }
  25870. }
  25871. me.fireEvent('updateSections')
  25872. /* 获取地址的包含关系 */
  25873. function isContainsAddress(
  25874. startAddress,
  25875. endAddress,
  25876. addressTarget
  25877. ) {
  25878. var isAfterStartAddress = false,
  25879. isBeforeEndAddress = false
  25880. for (var i = 0; i < startAddress.length; i++) {
  25881. if (i >= addressTarget.length) break
  25882. if (addressTarget[i] > startAddress[i]) {
  25883. isAfterStartAddress = true
  25884. break
  25885. } else if (addressTarget[i] < startAddress[i]) {
  25886. break
  25887. }
  25888. }
  25889. for (var i = 0; i < endAddress.length; i++) {
  25890. if (i >= addressTarget.length) break
  25891. if (addressTarget[i] < startAddress[i]) {
  25892. isBeforeEndAddress = true
  25893. break
  25894. } else if (addressTarget[i] > startAddress[i]) {
  25895. break
  25896. }
  25897. }
  25898. return isAfterStartAddress && isBeforeEndAddress
  25899. }
  25900. },
  25901. },
  25902. deletesection: {
  25903. execCommand: function (cmd, section, keepChildren) {
  25904. var me = this
  25905. if (!section) return
  25906. function getNodeFromAddress(startAddress) {
  25907. var current = me.body
  25908. for (var i = 0; i < startAddress.length; i++) {
  25909. if (!current.childNodes) return null
  25910. current = current.childNodes[startAddress[i]]
  25911. }
  25912. return current
  25913. }
  25914. var startNode = getNodeFromAddress(section.startAddress),
  25915. endNode = getNodeFromAddress(section.endAddress),
  25916. current = startNode,
  25917. nextNode
  25918. if (!keepChildren) {
  25919. while (
  25920. current &&
  25921. domUtils.inDoc(endNode, me.document) &&
  25922. !(
  25923. domUtils.getPosition(current, endNode) &
  25924. domUtils.POSITION_FOLLOWING
  25925. )
  25926. ) {
  25927. nextNode = current.nextSibling
  25928. domUtils.remove(current)
  25929. current = nextNode
  25930. }
  25931. } else {
  25932. domUtils.remove(current)
  25933. }
  25934. me.fireEvent('updateSections')
  25935. },
  25936. },
  25937. selectsection: {
  25938. execCommand: function (cmd, section) {
  25939. if (!section && !section.dom) return false
  25940. var me = this,
  25941. range = me.selection.getRange(),
  25942. address = {
  25943. startAddress: utils.clone(section.startAddress, []),
  25944. endAddress: utils.clone(section.endAddress, []),
  25945. }
  25946. address.endAddress[address.endAddress.length - 1]++
  25947. range.moveToAddress(address).select().scrollToView()
  25948. return true
  25949. },
  25950. notNeedUndo: true,
  25951. },
  25952. scrolltosection: {
  25953. execCommand: function (cmd, section) {
  25954. if (!section && !section.dom) return false
  25955. var me = this,
  25956. range = me.selection.getRange(),
  25957. address = {
  25958. startAddress: section.startAddress,
  25959. endAddress: section.endAddress,
  25960. }
  25961. address.endAddress[address.endAddress.length - 1]++
  25962. range.moveToAddress(address).scrollToView()
  25963. return true
  25964. },
  25965. notNeedUndo: true,
  25966. },
  25967. },
  25968. }
  25969. })
  25970. // plugins/simpleupload.js
  25971. /**
  25972. * @description
  25973. * 简单上传:点击按钮,直接选择文件上传。
  25974. * 原 UEditor 作者使用了 form 表单 + iframe 的方式上传
  25975. * 但由于同源策略的限制,父页面无法访问跨域的 iframe 内容
  25976. * 导致无法获取接口返回的数据,使得单图上传无法在跨域的情况下使用
  25977. * 这里改为普通的XHR上传,兼容到IE10+
  25978. * @author HaoChuan9421 <hc199421@gmail.com>
  25979. * @date 2018-12-20
  25980. */
  25981. UE.plugin.register('simpleupload', function () {
  25982. var me = this,
  25983. containerBtn,
  25984. timestrap = (+new Date()).toString(36)
  25985. function initUploadBtn() {
  25986. var w = containerBtn.offsetWidth || 20,
  25987. h = containerBtn.offsetHeight || 20,
  25988. btnStyle =
  25989. 'display:block;width:' +
  25990. w +
  25991. 'px;height:' +
  25992. h +
  25993. 'px;overflow:hidden;border:0;margin:0;padding:0;position:absolute;top:0;left:0;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;cursor:pointer;'
  25994. var form = document.createElement('form')
  25995. var input = document.createElement('input')
  25996. form.id = 'edui_form_' + timestrap
  25997. form.enctype = 'multipart/form-data'
  25998. form.style = btnStyle
  25999. input.id = 'edui_input_' + timestrap
  26000. input.type = 'file'
  26001. input.accept = 'image/*'
  26002. input.name = me.options.imageFieldName
  26003. input.style = btnStyle
  26004. form.appendChild(input)
  26005. containerBtn.appendChild(form)
  26006. input.addEventListener('change', function (event) {
  26007. if (!input.value) return
  26008. var loadingId = 'loading_' + (+new Date()).toString(36)
  26009. var imageActionUrl = me.getActionUrl(me.getOpt('imageActionName'))
  26010. var params =
  26011. utils.serializeParam(me.queryCommandValue('serverparam')) || ''
  26012. var action = utils.formatUrl(
  26013. imageActionUrl +
  26014. (imageActionUrl.indexOf('?') == -1 ? '?' : '&') +
  26015. params
  26016. )
  26017. var allowFiles = me.getOpt('imageAllowFiles')
  26018. me.focus()
  26019. me.execCommand(
  26020. 'inserthtml',
  26021. '<img class="loadingclass" id="' +
  26022. loadingId +
  26023. '" src="' +
  26024. me.options.themePath +
  26025. me.options.theme +
  26026. '/images/spacer.gif" title="' +
  26027. (me.getLang('simpleupload.loading') || '') +
  26028. '" >'
  26029. )
  26030. function showErrorLoader(title) {
  26031. if (loadingId) {
  26032. var loader = me.document.getElementById(loadingId)
  26033. loader && domUtils.remove(loader)
  26034. me.fireEvent('showmessage', {
  26035. id: loadingId,
  26036. content: title,
  26037. type: 'error',
  26038. timeout: 4000,
  26039. })
  26040. }
  26041. }
  26042. /* 判断后端配置是否没有加载成功 */
  26043. if (!me.getOpt('imageActionName')) {
  26044. console.log('此处baocuo1111111111111111111')
  26045. showErrorLoader(me.getLang('autoupload.errorLoadConfig'))
  26046. return
  26047. }
  26048. // 判断文件格式是否错误
  26049. var filename = input.value,
  26050. fileext = filename ? filename.substr(filename.lastIndexOf('.')) : ''
  26051. if (
  26052. !fileext ||
  26053. (allowFiles &&
  26054. (allowFiles.join('') + '.').indexOf(fileext.toLowerCase() + '.') ==
  26055. -1)
  26056. ) {
  26057. showErrorLoader(me.getLang('simpleupload.exceedTypeError'))
  26058. return
  26059. }
  26060. var xhr = new XMLHttpRequest()
  26061. xhr.open('post', action, true)
  26062. if (
  26063. me.options.headers &&
  26064. Object.prototype.toString.apply(me.options.headers) ===
  26065. '[object Object]'
  26066. ) {
  26067. for (var key in me.options.headers) {
  26068. xhr.setRequestHeader(key, me.options.headers[key])
  26069. }
  26070. }
  26071. xhr.onload = function () {
  26072. if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
  26073. var res = JSON.parse(xhr.responseText)
  26074. var link = me.options.imageUrlPrefix + res.url
  26075. if (res.state == 'SUCCESS' && res.url) {
  26076. loader = me.document.getElementById(loadingId)
  26077. loader.setAttribute('src', link)
  26078. loader.setAttribute('_src', link)
  26079. loader.setAttribute('title', res.title || '')
  26080. loader.setAttribute('alt', res.original || '')
  26081. loader.removeAttribute('id')
  26082. domUtils.removeClasses(loader, 'loadingclass')
  26083. me.fireEvent('contentchange')
  26084. } else {
  26085. showErrorLoader(res.state)
  26086. }
  26087. } else {
  26088. showErrorLoader(me.getLang('simpleupload.loadError'))
  26089. }
  26090. }
  26091. xhr.onerror = function () {
  26092. showErrorLoader(me.getLang('simpleupload.loadError'))
  26093. }
  26094. xhr.send(new FormData(form))
  26095. form.reset()
  26096. })
  26097. }
  26098. return {
  26099. bindEvents: {
  26100. ready: function () {
  26101. //设置loading的样式
  26102. utils.cssRule(
  26103. 'loading',
  26104. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  26105. this.options.themePath +
  26106. this.options.theme +
  26107. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n" +
  26108. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  26109. this.options.themePath +
  26110. this.options.theme +
  26111. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  26112. '}',
  26113. this.document
  26114. )
  26115. },
  26116. /* 初始化简单上传按钮 */
  26117. simpleuploadbtnready: function (type, container) {
  26118. containerBtn = container
  26119. me.afterConfigReady(initUploadBtn)
  26120. },
  26121. },
  26122. outputRule: function (root) {
  26123. utils.each(root.getNodesByTagName('img'), function (n) {
  26124. if (
  26125. /\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))
  26126. ) {
  26127. n.parentNode.removeChild(n)
  26128. }
  26129. })
  26130. },
  26131. }
  26132. })
  26133. // plugins/serverparam.js
  26134. /**
  26135. * 服务器提交的额外参数列表设置插件
  26136. * @file
  26137. * @since 1.2.6.1
  26138. */
  26139. UE.plugin.register('serverparam', function () {
  26140. var me = this,
  26141. serverParam = {}
  26142. return {
  26143. commands: {
  26144. /**
  26145. * 修改服务器提交的额外参数列表,清除所有项
  26146. * @command serverparam
  26147. * @method execCommand
  26148. * @param { String } cmd 命令字符串
  26149. * @example
  26150. * ```javascript
  26151. * editor.execCommand('serverparam');
  26152. * editor.queryCommandValue('serverparam'); //返回空
  26153. * ```
  26154. */
  26155. /**
  26156. * 修改服务器提交的额外参数列表,删除指定项
  26157. * @command serverparam
  26158. * @method execCommand
  26159. * @param { String } cmd 命令字符串
  26160. * @param { String } key 要清除的属性
  26161. * @example
  26162. * ```javascript
  26163. * editor.execCommand('serverparam', 'name'); //删除属性name
  26164. * ```
  26165. */
  26166. /**
  26167. * 修改服务器提交的额外参数列表,使用键值添加项
  26168. * @command serverparam
  26169. * @method execCommand
  26170. * @param { String } cmd 命令字符串
  26171. * @param { String } key 要添加的属性
  26172. * @param { String } value 要添加属性的值
  26173. * @example
  26174. * ```javascript
  26175. * editor.execCommand('serverparam', 'name', 'hello');
  26176. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  26177. * ```
  26178. */
  26179. /**
  26180. * 修改服务器提交的额外参数列表,传入键值对对象添加多项
  26181. * @command serverparam
  26182. * @method execCommand
  26183. * @param { String } cmd 命令字符串
  26184. * @param { Object } key 传入的键值对对象
  26185. * @example
  26186. * ```javascript
  26187. * editor.execCommand('serverparam', {'name': 'hello'});
  26188. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  26189. * ```
  26190. */
  26191. /**
  26192. * 修改服务器提交的额外参数列表,使用自定义函数添加多项
  26193. * @command serverparam
  26194. * @method execCommand
  26195. * @param { String } cmd 命令字符串
  26196. * @param { Function } key 自定义获取参数的函数
  26197. * @example
  26198. * ```javascript
  26199. * editor.execCommand('serverparam', function(editor){
  26200. * return {'key': 'value'};
  26201. * });
  26202. * editor.queryCommandValue('serverparam'); //返回对象 {'key': 'value'}
  26203. * ```
  26204. */
  26205. /**
  26206. * 获取服务器提交的额外参数列表
  26207. * @command serverparam
  26208. * @method queryCommandValue
  26209. * @param { String } cmd 命令字符串
  26210. * @example
  26211. * ```javascript
  26212. * editor.queryCommandValue( 'serverparam' ); //返回对象 {'key': 'value'}
  26213. * ```
  26214. */
  26215. serverparam: {
  26216. execCommand: function (cmd, key, value) {
  26217. if (key === undefined || key === null) {
  26218. //不传参数,清空列表
  26219. serverParam = {}
  26220. } else if (utils.isString(key)) {
  26221. //传入键值
  26222. if (value === undefined || value === null) {
  26223. delete serverParam[key]
  26224. } else {
  26225. serverParam[key] = value
  26226. }
  26227. } else if (utils.isObject(key)) {
  26228. //传入对象,覆盖列表项
  26229. utils.extend(serverParam, key, true)
  26230. } else if (utils.isFunction(key)) {
  26231. //传入函数,添加列表项
  26232. utils.extend(serverParam, key(), true)
  26233. }
  26234. },
  26235. queryCommandValue: function () {
  26236. return serverParam || {}
  26237. },
  26238. },
  26239. },
  26240. }
  26241. })
  26242. // plugins/insertfile.js
  26243. /**
  26244. * 插入附件
  26245. */
  26246. UE.plugin.register('insertfile', function () {
  26247. var me = this
  26248. function getFileIcon(url) {
  26249. var ext = url.substr(url.lastIndexOf('.') + 1).toLowerCase(),
  26250. maps = {
  26251. rar: 'icon_rar.gif',
  26252. zip: 'icon_rar.gif',
  26253. tar: 'icon_rar.gif',
  26254. gz: 'icon_rar.gif',
  26255. bz2: 'icon_rar.gif',
  26256. doc: 'icon_doc.gif',
  26257. docx: 'icon_doc.gif',
  26258. pdf: 'icon_pdf.gif',
  26259. mp3: 'icon_mp3.gif',
  26260. xls: 'icon_xls.gif',
  26261. chm: 'icon_chm.gif',
  26262. ppt: 'icon_ppt.gif',
  26263. pptx: 'icon_ppt.gif',
  26264. avi: 'icon_mv.gif',
  26265. rmvb: 'icon_mv.gif',
  26266. wmv: 'icon_mv.gif',
  26267. flv: 'icon_mv.gif',
  26268. swf: 'icon_mv.gif',
  26269. rm: 'icon_mv.gif',
  26270. exe: 'icon_exe.gif',
  26271. psd: 'icon_psd.gif',
  26272. txt: 'icon_txt.gif',
  26273. jpg: 'icon_jpg.gif',
  26274. png: 'icon_jpg.gif',
  26275. jpeg: 'icon_jpg.gif',
  26276. gif: 'icon_jpg.gif',
  26277. ico: 'icon_jpg.gif',
  26278. bmp: 'icon_jpg.gif',
  26279. }
  26280. return maps[ext] ? maps[ext] : maps['txt']
  26281. }
  26282. return {
  26283. commands: {
  26284. insertfile: {
  26285. execCommand: function (command, filelist) {
  26286. filelist = utils.isArray(filelist) ? filelist : [filelist]
  26287. var i,
  26288. item,
  26289. icon,
  26290. title,
  26291. html = '',
  26292. URL = me.getOpt('UEDITOR_HOME_URL'),
  26293. iconDir =
  26294. URL +
  26295. (URL.substr(URL.length - 1) == '/' ? '' : '/') +
  26296. 'dialogs/attachment/fileTypeImages/'
  26297. for (i = 0; i < filelist.length; i++) {
  26298. item = filelist[i]
  26299. icon = iconDir + getFileIcon(item.url)
  26300. title =
  26301. item.title || item.url.substr(item.url.lastIndexOf('/') + 1)
  26302. html +=
  26303. '<p style="line-height: 16px;">' +
  26304. '<img style="vertical-align: middle; margin-right: 2px;" src="' +
  26305. icon +
  26306. '" _src="' +
  26307. icon +
  26308. '" />' +
  26309. '<a style="font-size:12px; color:#0066cc;" href="' +
  26310. item.url +
  26311. '" title="' +
  26312. title +
  26313. '">' +
  26314. title +
  26315. '</a>' +
  26316. '</p>'
  26317. }
  26318. me.execCommand('insertHtml', html)
  26319. },
  26320. },
  26321. },
  26322. }
  26323. })
  26324. /**
  26325. * 插入word
  26326. */
  26327. UE.plugin.register('wordupload', function () {
  26328. var me = this,
  26329. isLoaded = false,
  26330. containerBtn
  26331. function initUploadBtn() {
  26332. console.log(me.options, 'me.options')
  26333. var w = containerBtn.offsetWidth || 20,
  26334. h = containerBtn.offsetHeight || 20,
  26335. btnIframe = document.createElement('iframe'),
  26336. btnStyle =
  26337. 'display:block;width:' +
  26338. w +
  26339. 'px;height:' +
  26340. h +
  26341. 'px;overflow:hidden;border:0;margin:0;padding:0;position:absolute;top:0;left:0;filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;cursor:pointer;'
  26342. domUtils.on(btnIframe, 'load', function () {
  26343. var timestrap = (+new Date()).toString(36),
  26344. wrapper,
  26345. btnIframeDoc,
  26346. btnIframeBody
  26347. btnIframeDoc =
  26348. btnIframe.contentDocument || btnIframe.contentWindow.document
  26349. btnIframeBody = btnIframeDoc.body
  26350. wrapper = btnIframeDoc.createElement('div')
  26351. wrapper.innerHTML =
  26352. '<form id="edui_form_' +
  26353. timestrap +
  26354. '" target="edui_iframe_' +
  26355. timestrap +
  26356. '" method="POST" enctype="multipart/form-data" action="' +
  26357. me.getOpt('serverUrl') +
  26358. '" ' +
  26359. 'style="' +
  26360. btnStyle +
  26361. '">' +
  26362. '<input id="edui_input_' +
  26363. // application/msword
  26364. timestrap +
  26365. '" type="file" accept=".doc,.docx" name="' +
  26366. me.options.wordFieldName +
  26367. '" ' +
  26368. 'style="' +
  26369. btnStyle +
  26370. '">' +
  26371. '</form>' +
  26372. '<iframe id="edui_iframe_' +
  26373. timestrap +
  26374. '" name="edui_iframe_' +
  26375. timestrap +
  26376. '" style="display:none;width:0;height:0;border:0;margin:0;padding:0;position:absolute;"></iframe>'
  26377. wrapper.className = 'edui-' + me.options.theme
  26378. wrapper.id = me.ui.id + '_iframeupload'
  26379. btnIframeBody.style.cssText = btnStyle
  26380. btnIframeBody.style.width = w + 'px'
  26381. btnIframeBody.style.height = h + 'px'
  26382. btnIframeBody.appendChild(wrapper)
  26383. if (btnIframeBody.parentNode) {
  26384. btnIframeBody.parentNode.style.width = w + 'px'
  26385. btnIframeBody.parentNode.style.height = w + 'px'
  26386. }
  26387. var form = btnIframeDoc.getElementById('edui_form_' + timestrap)
  26388. var input = btnIframeDoc.getElementById('edui_input_' + timestrap)
  26389. var iframe = btnIframeDoc.getElementById('edui_iframe_' + timestrap)
  26390. domUtils.on(input, 'change', function () {
  26391. if (!input.value) return
  26392. var loadingId = 'loading_' + (+new Date()).toString(36)
  26393. var allowFiles = me.getOpt('wordAllowFiles')
  26394. me.focus()
  26395. me.execCommand(
  26396. 'inserthtml',
  26397. '<img class="loadingclass" id="' +
  26398. loadingId +
  26399. '" src="' +
  26400. me.options.themePath +
  26401. me.options.theme +
  26402. '/images/spacer.gif" title="' +
  26403. (me.getLang('wordupload.loading') || '') +
  26404. '" >'
  26405. )
  26406. function callback() {
  26407. try {
  26408. // 获取到内容
  26409. var body = (
  26410. iframe.contentDocument || iframe.contentWindow.document
  26411. ).body
  26412. // 获取加载中图片并关闭
  26413. var loader = me.document.getElementById(loadingId)
  26414. loader.removeAttribute('id')
  26415. domUtils.removeClasses(loader, 'loadingclass')
  26416. // 向编辑器赋值
  26417. me.setContent(body.innerHTML, false)
  26418. // me.execCommand('insertHtml', body.innerHTML);
  26419. } catch (er) {
  26420. showErrorLoader &&
  26421. showErrorLoader(me.getLang('wordupload.loadError'))
  26422. }
  26423. form.reset()
  26424. domUtils.un(iframe, 'load', callback)
  26425. }
  26426. function showErrorLoader(title) {
  26427. if (loadingId) {
  26428. var loader = me.document.getElementById(loadingId)
  26429. loader && domUtils.remove(loader)
  26430. me.fireEvent('showmessage', {
  26431. id: loadingId,
  26432. content: title,
  26433. type: 'error',
  26434. timeout: 4000,
  26435. })
  26436. }
  26437. }
  26438. /* 判断后端配置是否没有加载成功 */
  26439. if (!me.getOpt('wordActionName')) {
  26440. errorHandler(me.getLang('autoupload.errorLoadConfig'))
  26441. return
  26442. }
  26443. // 判断文件格式是否错误
  26444. var filename = input.value,
  26445. fileext = filename ? filename.substr(filename.lastIndexOf('.')) : ''
  26446. if (
  26447. !fileext ||
  26448. (allowFiles &&
  26449. (allowFiles.join('') + '.').indexOf(
  26450. fileext.toLowerCase() + '.'
  26451. ) == -1)
  26452. ) {
  26453. showErrorLoader(me.getLang('wordupload.exceedTypeError'))
  26454. return
  26455. }
  26456. domUtils.on(iframe, 'load', callback)
  26457. // 上传操作
  26458. // form.action = utils.formatUrl(imageActionUrl + (imageActionUrl.indexOf('?') == -1 ? '?':'&') + params);
  26459. // 替换请求地址为框架后台地址
  26460. form.action = '/admin.php?m=fwordToHtml&a=index'
  26461. //form.action ='/daili/ueditor/controller.php'
  26462. form.method = 'post'
  26463. form.submit()
  26464. })
  26465. var stateTimer
  26466. me.addListener('selectionchange', function () {
  26467. clearTimeout(stateTimer)
  26468. stateTimer = setTimeout(function () {
  26469. var state = me.queryCommandState('wordupload')
  26470. if (state == -1) {
  26471. input.disabled = 'disabled'
  26472. } else {
  26473. input.disabled = false
  26474. }
  26475. }, 400)
  26476. })
  26477. isLoaded = true
  26478. })
  26479. btnIframe.style.cssText = btnStyle
  26480. containerBtn.appendChild(btnIframe)
  26481. }
  26482. return {
  26483. bindEvents: {
  26484. ready: function () {
  26485. //设置loading的样式
  26486. utils.cssRule(
  26487. 'loading',
  26488. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  26489. this.options.themePath +
  26490. this.options.theme +
  26491. "/images/loading.gif') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;}\n" +
  26492. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  26493. this.options.themePath +
  26494. this.options.theme +
  26495. "/images/loaderror.png') no-repeat center center transparent;border:1px solid #cccccc;margin-right:1px;height: 22px;width: 22px;" +
  26496. '}',
  26497. this.document
  26498. )
  26499. },
  26500. /* 初始化word上传按钮 */
  26501. worduploadbtnready: function (type, container) {
  26502. containerBtn = container
  26503. me.afterConfigReady(initUploadBtn)
  26504. },
  26505. },
  26506. outputRule: function (root) {
  26507. utils.each(root.getNodesByTagName('img'), function (n) {
  26508. if (
  26509. /\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr('class'))
  26510. ) {
  26511. n.parentNode.removeChild(n)
  26512. }
  26513. })
  26514. },
  26515. commands: {
  26516. wordupload: {
  26517. queryCommandState: function () {
  26518. return isLoaded ? 0 : -1
  26519. },
  26520. },
  26521. },
  26522. }
  26523. })
  26524. // plugins/xssFilter.js
  26525. /**
  26526. * @file xssFilter.js
  26527. * @desc xss过滤器
  26528. * @author robbenmu
  26529. */
  26530. UE.plugins.xssFilter = function () {
  26531. var config = UEDITOR_CONFIG
  26532. var whitList = config.whitList
  26533. function filter(node) {
  26534. var tagName = node.tagName
  26535. var attrs = node.attrs
  26536. if (!whitList.hasOwnProperty(tagName)) {
  26537. node.parentNode.removeChild(node)
  26538. return false
  26539. }
  26540. UE.utils.each(attrs, function (val, key) {
  26541. if (whitList[tagName].indexOf(key) === -1) {
  26542. node.setAttr(key)
  26543. }
  26544. })
  26545. }
  26546. // 添加inserthtml\paste等操作用的过滤规则
  26547. if (whitList && config.xssFilterRules) {
  26548. this.options.filterRules = (function () {
  26549. var result = {}
  26550. UE.utils.each(whitList, function (val, key) {
  26551. result[key] = function (node) {
  26552. return filter(node)
  26553. }
  26554. })
  26555. return result
  26556. })()
  26557. }
  26558. var tagList = []
  26559. UE.utils.each(whitList, function (val, key) {
  26560. tagList.push(key)
  26561. })
  26562. // 添加input过滤规则
  26563. //
  26564. if (whitList && config.inputXssFilter) {
  26565. this.addInputRule(function (root) {
  26566. root.traversal(function (node) {
  26567. if (node.type !== 'element') {
  26568. return false
  26569. }
  26570. filter(node)
  26571. })
  26572. })
  26573. }
  26574. // 添加output过滤规则
  26575. //
  26576. if (whitList && config.outputXssFilter) {
  26577. this.addOutputRule(function (root) {
  26578. root.traversal(function (node) {
  26579. if (node.type !== 'element') {
  26580. return false
  26581. }
  26582. filter(node)
  26583. })
  26584. })
  26585. }
  26586. }
  26587. // ui/ui.js
  26588. var baidu = baidu || {}
  26589. baidu.editor = baidu.editor || {}
  26590. UE.ui = baidu.editor.ui = {}
  26591. // ui/uiutils.js
  26592. ;(function () {
  26593. var browser = baidu.editor.browser,
  26594. domUtils = baidu.editor.dom.domUtils
  26595. var magic = '$EDITORUI'
  26596. var root = (window[magic] = {})
  26597. var uidMagic = 'ID' + magic
  26598. var uidCount = 0
  26599. var uiUtils = (baidu.editor.ui.uiUtils = {
  26600. uid: function (obj) {
  26601. return obj ? obj[uidMagic] || (obj[uidMagic] = ++uidCount) : ++uidCount
  26602. },
  26603. hook: function (fn, callback) {
  26604. var dg
  26605. if (fn && fn._callbacks) {
  26606. dg = fn
  26607. } else {
  26608. dg = function () {
  26609. var q
  26610. if (fn) {
  26611. q = fn.apply(this, arguments)
  26612. }
  26613. var callbacks = dg._callbacks
  26614. var k = callbacks.length
  26615. while (k--) {
  26616. var r = callbacks[k].apply(this, arguments)
  26617. if (q === undefined) {
  26618. q = r
  26619. }
  26620. }
  26621. return q
  26622. }
  26623. dg._callbacks = []
  26624. }
  26625. dg._callbacks.push(callback)
  26626. return dg
  26627. },
  26628. createElementByHtml: function (html) {
  26629. var el = document.createElement('div')
  26630. el.innerHTML = html
  26631. el = el.firstChild
  26632. el.parentNode.removeChild(el)
  26633. return el
  26634. },
  26635. getViewportElement: function () {
  26636. return browser.ie && browser.quirks
  26637. ? document.body
  26638. : document.documentElement
  26639. },
  26640. getClientRect: function (element) {
  26641. var bcr
  26642. //trace IE6下在控制编辑器显隐时可能会报错,catch一下
  26643. try {
  26644. bcr = element.getBoundingClientRect()
  26645. } catch (e) {
  26646. bcr = { left: 0, top: 0, height: 0, width: 0 }
  26647. }
  26648. var rect = {
  26649. left: Math.round(bcr.left),
  26650. top: Math.round(bcr.top),
  26651. height: Math.round(bcr.bottom - bcr.top),
  26652. width: Math.round(bcr.right - bcr.left),
  26653. }
  26654. var doc
  26655. while (
  26656. (doc = element.ownerDocument) !== document &&
  26657. (element = domUtils.getWindow(doc).frameElement)
  26658. ) {
  26659. bcr = element.getBoundingClientRect()
  26660. rect.left += bcr.left
  26661. rect.top += bcr.top
  26662. }
  26663. rect.bottom = rect.top + rect.height
  26664. rect.right = rect.left + rect.width
  26665. return rect
  26666. },
  26667. getViewportRect: function () {
  26668. var viewportEl = uiUtils.getViewportElement()
  26669. var width = (window.innerWidth || viewportEl.clientWidth) | 0
  26670. var height = (window.innerHeight || viewportEl.clientHeight) | 0
  26671. return {
  26672. left: 0,
  26673. top: 0,
  26674. height: height,
  26675. width: width,
  26676. bottom: height,
  26677. right: width,
  26678. }
  26679. },
  26680. setViewportOffset: function (element, offset) {
  26681. var rect
  26682. var fixedLayer = uiUtils.getFixedLayer()
  26683. if (element.parentNode === fixedLayer) {
  26684. element.style.left = offset.left + 'px'
  26685. element.style.top = offset.top + 'px'
  26686. } else {
  26687. domUtils.setViewportOffset(element, offset)
  26688. }
  26689. },
  26690. getEventOffset: function (evt) {
  26691. var el = evt.target || evt.srcElement
  26692. var rect = uiUtils.getClientRect(el)
  26693. var offset = uiUtils.getViewportOffsetByEvent(evt)
  26694. return {
  26695. left: offset.left - rect.left,
  26696. top: offset.top - rect.top,
  26697. }
  26698. },
  26699. getViewportOffsetByEvent: function (evt) {
  26700. var el = evt.target || evt.srcElement
  26701. var frameEl = domUtils.getWindow(el).frameElement
  26702. var offset = {
  26703. left: evt.clientX,
  26704. top: evt.clientY,
  26705. }
  26706. if (frameEl && el.ownerDocument !== document) {
  26707. var rect = uiUtils.getClientRect(frameEl)
  26708. offset.left += rect.left
  26709. offset.top += rect.top
  26710. }
  26711. return offset
  26712. },
  26713. setGlobal: function (id, obj) {
  26714. root[id] = obj
  26715. return magic + '["' + id + '"]'
  26716. },
  26717. unsetGlobal: function (id) {
  26718. delete root[id]
  26719. },
  26720. copyAttributes: function (tgt, src) {
  26721. var attributes = src.attributes
  26722. var k = attributes.length
  26723. while (k--) {
  26724. var attrNode = attributes[k]
  26725. if (
  26726. attrNode.nodeName != 'style' &&
  26727. attrNode.nodeName != 'class' &&
  26728. (!browser.ie || attrNode.specified)
  26729. ) {
  26730. tgt.setAttribute(attrNode.nodeName, attrNode.nodeValue)
  26731. }
  26732. }
  26733. if (src.className) {
  26734. domUtils.addClass(tgt, src.className)
  26735. }
  26736. if (src.style.cssText) {
  26737. tgt.style.cssText += ';' + src.style.cssText
  26738. }
  26739. },
  26740. removeStyle: function (el, styleName) {
  26741. if (el.style.removeProperty) {
  26742. el.style.removeProperty(styleName)
  26743. } else if (el.style.removeAttribute) {
  26744. el.style.removeAttribute(styleName)
  26745. } else throw ''
  26746. },
  26747. contains: function (elA, elB) {
  26748. return (
  26749. elA &&
  26750. elB &&
  26751. (elA === elB
  26752. ? false
  26753. : elA.contains
  26754. ? elA.contains(elB)
  26755. : elA.compareDocumentPosition(elB) & 16)
  26756. )
  26757. },
  26758. startDrag: function (evt, callbacks, doc) {
  26759. var doc = doc || document
  26760. var startX = evt.clientX
  26761. var startY = evt.clientY
  26762. function handleMouseMove(evt) {
  26763. var x = evt.clientX - startX
  26764. var y = evt.clientY - startY
  26765. callbacks.ondragmove(x, y, evt)
  26766. if (evt.stopPropagation) {
  26767. evt.stopPropagation()
  26768. } else {
  26769. evt.cancelBubble = true
  26770. }
  26771. }
  26772. if (doc.addEventListener) {
  26773. function handleMouseUp(evt) {
  26774. doc.removeEventListener('mousemove', handleMouseMove, true)
  26775. doc.removeEventListener('mouseup', handleMouseUp, true)
  26776. window.removeEventListener('mouseup', handleMouseUp, true)
  26777. callbacks.ondragstop()
  26778. }
  26779. doc.addEventListener('mousemove', handleMouseMove, true)
  26780. doc.addEventListener('mouseup', handleMouseUp, true)
  26781. window.addEventListener('mouseup', handleMouseUp, true)
  26782. evt.preventDefault()
  26783. } else {
  26784. var elm = evt.srcElement
  26785. elm.setCapture()
  26786. function releaseCaptrue() {
  26787. elm.releaseCapture()
  26788. elm.detachEvent('onmousemove', handleMouseMove)
  26789. elm.detachEvent('onmouseup', releaseCaptrue)
  26790. elm.detachEvent('onlosecaptrue', releaseCaptrue)
  26791. callbacks.ondragstop()
  26792. }
  26793. elm.attachEvent('onmousemove', handleMouseMove)
  26794. elm.attachEvent('onmouseup', releaseCaptrue)
  26795. elm.attachEvent('onlosecaptrue', releaseCaptrue)
  26796. evt.returnValue = false
  26797. }
  26798. callbacks.ondragstart()
  26799. },
  26800. getFixedLayer: function () {
  26801. var layer = document.getElementById('edui_fixedlayer')
  26802. if (layer == null) {
  26803. layer = document.createElement('div')
  26804. layer.id = 'edui_fixedlayer'
  26805. document.body.appendChild(layer)
  26806. if (browser.ie && browser.version <= 8) {
  26807. layer.style.position = 'absolute'
  26808. bindFixedLayer()
  26809. setTimeout(updateFixedOffset)
  26810. } else {
  26811. layer.style.position = 'fixed'
  26812. }
  26813. layer.style.left = '0'
  26814. layer.style.top = '0'
  26815. layer.style.width = '0'
  26816. layer.style.height = '0'
  26817. }
  26818. return layer
  26819. },
  26820. makeUnselectable: function (element) {
  26821. if (browser.opera || (browser.ie && browser.version < 9)) {
  26822. element.unselectable = 'on'
  26823. if (element.hasChildNodes()) {
  26824. for (var i = 0; i < element.childNodes.length; i++) {
  26825. if (element.childNodes[i].nodeType == 1) {
  26826. uiUtils.makeUnselectable(element.childNodes[i])
  26827. }
  26828. }
  26829. }
  26830. } else {
  26831. if (element.style.MozUserSelect !== undefined) {
  26832. element.style.MozUserSelect = 'none'
  26833. } else if (element.style.WebkitUserSelect !== undefined) {
  26834. element.style.WebkitUserSelect = 'none'
  26835. } else if (element.style.KhtmlUserSelect !== undefined) {
  26836. element.style.KhtmlUserSelect = 'none'
  26837. }
  26838. }
  26839. },
  26840. })
  26841. function updateFixedOffset() {
  26842. var layer = document.getElementById('edui_fixedlayer')
  26843. uiUtils.setViewportOffset(layer, {
  26844. left: 0,
  26845. top: 0,
  26846. })
  26847. // layer.style.display = 'none';
  26848. // layer.style.display = 'block';
  26849. //#trace: 1354
  26850. // setTimeout(updateFixedOffset);
  26851. }
  26852. function bindFixedLayer(adjOffset) {
  26853. domUtils.on(window, 'scroll', updateFixedOffset)
  26854. domUtils.on(
  26855. window,
  26856. 'resize',
  26857. baidu.editor.utils.defer(updateFixedOffset, 0, true)
  26858. )
  26859. }
  26860. })()
  26861. // ui/uibase.js
  26862. ;(function () {
  26863. var utils = baidu.editor.utils,
  26864. uiUtils = baidu.editor.ui.uiUtils,
  26865. EventBase = baidu.editor.EventBase,
  26866. UIBase = (baidu.editor.ui.UIBase = function () {})
  26867. UIBase.prototype = {
  26868. className: '',
  26869. uiName: '',
  26870. initOptions: function (options) {
  26871. var me = this
  26872. for (var k in options) {
  26873. me[k] = options[k]
  26874. }
  26875. this.id = this.id || 'edui' + uiUtils.uid()
  26876. },
  26877. initUIBase: function () {
  26878. this._globalKey = utils.unhtml(uiUtils.setGlobal(this.id, this))
  26879. },
  26880. render: function (holder) {
  26881. var html = this.renderHtml()
  26882. var el = uiUtils.createElementByHtml(html)
  26883. //by xuheng 给每个node添加class
  26884. var list = domUtils.getElementsByTagName(el, '*')
  26885. var theme = 'edui-' + (this.theme || this.editor.options.theme)
  26886. var layer = document.getElementById('edui_fixedlayer')
  26887. for (var i = 0, node; (node = list[i++]); ) {
  26888. domUtils.addClass(node, theme)
  26889. }
  26890. domUtils.addClass(el, theme)
  26891. if (layer) {
  26892. layer.className = ''
  26893. domUtils.addClass(layer, theme)
  26894. }
  26895. var seatEl = this.getDom()
  26896. if (seatEl != null) {
  26897. seatEl.parentNode.replaceChild(el, seatEl)
  26898. uiUtils.copyAttributes(el, seatEl)
  26899. } else {
  26900. if (typeof holder == 'string') {
  26901. holder = document.getElementById(holder)
  26902. }
  26903. holder = holder || uiUtils.getFixedLayer()
  26904. domUtils.addClass(holder, theme)
  26905. holder.appendChild(el)
  26906. }
  26907. this.postRender()
  26908. },
  26909. getDom: function (name) {
  26910. if (!name) {
  26911. return document.getElementById(this.id)
  26912. } else {
  26913. return document.getElementById(this.id + '_' + name)
  26914. }
  26915. },
  26916. postRender: function () {
  26917. this.fireEvent('postrender')
  26918. },
  26919. getHtmlTpl: function () {
  26920. return ''
  26921. },
  26922. formatHtml: function (tpl) {
  26923. var prefix = 'edui-' + this.uiName
  26924. return tpl
  26925. .replace(/##/g, this.id)
  26926. .replace(/%%-/g, this.uiName ? prefix + '-' : '')
  26927. .replace(/%%/g, (this.uiName ? prefix : '') + ' ' + this.className)
  26928. .replace(/\$\$/g, this._globalKey)
  26929. },
  26930. renderHtml: function () {
  26931. return this.formatHtml(this.getHtmlTpl())
  26932. },
  26933. dispose: function () {
  26934. var box = this.getDom()
  26935. if (box) baidu.editor.dom.domUtils.remove(box)
  26936. uiUtils.unsetGlobal(this.id)
  26937. },
  26938. }
  26939. utils.inherits(UIBase, EventBase)
  26940. })()
  26941. // ui/separator.js
  26942. ;(function () {
  26943. var utils = baidu.editor.utils,
  26944. UIBase = baidu.editor.ui.UIBase,
  26945. Separator = (baidu.editor.ui.Separator = function (options) {
  26946. this.initOptions(options)
  26947. this.initSeparator()
  26948. })
  26949. Separator.prototype = {
  26950. uiName: 'separator',
  26951. initSeparator: function () {
  26952. this.initUIBase()
  26953. },
  26954. getHtmlTpl: function () {
  26955. return '<div id="##" class="edui-box %%"></div>'
  26956. },
  26957. }
  26958. utils.inherits(Separator, UIBase)
  26959. })()
  26960. // ui/mask.js
  26961. ///import core
  26962. ///import uicore
  26963. ;(function () {
  26964. var utils = baidu.editor.utils,
  26965. domUtils = baidu.editor.dom.domUtils,
  26966. UIBase = baidu.editor.ui.UIBase,
  26967. uiUtils = baidu.editor.ui.uiUtils
  26968. var Mask = (baidu.editor.ui.Mask = function (options) {
  26969. this.initOptions(options)
  26970. this.initUIBase()
  26971. })
  26972. Mask.prototype = {
  26973. getHtmlTpl: function () {
  26974. return '<div id="##" class="edui-mask %%" onclick="return $$._onClick(event, this);" onmousedown="return $$._onMouseDown(event, this);"></div>'
  26975. },
  26976. postRender: function () {
  26977. var me = this
  26978. domUtils.on(window, 'resize', function () {
  26979. setTimeout(function () {
  26980. if (!me.isHidden()) {
  26981. me._fill()
  26982. }
  26983. })
  26984. })
  26985. },
  26986. show: function (zIndex) {
  26987. this._fill()
  26988. this.getDom().style.display = ''
  26989. this.getDom().style.zIndex = zIndex
  26990. },
  26991. hide: function () {
  26992. this.getDom().style.display = 'none'
  26993. this.getDom().style.zIndex = ''
  26994. },
  26995. isHidden: function () {
  26996. return this.getDom().style.display == 'none'
  26997. },
  26998. _onMouseDown: function () {
  26999. return false
  27000. },
  27001. _onClick: function (e, target) {
  27002. this.fireEvent('click', e, target)
  27003. },
  27004. _fill: function () {
  27005. var el = this.getDom()
  27006. var vpRect = uiUtils.getViewportRect()
  27007. el.style.width = vpRect.width + 'px'
  27008. el.style.height = vpRect.height + 'px'
  27009. },
  27010. }
  27011. utils.inherits(Mask, UIBase)
  27012. })()
  27013. // ui/popup.js
  27014. ///import core
  27015. ///import uicore
  27016. ;(function () {
  27017. var utils = baidu.editor.utils,
  27018. uiUtils = baidu.editor.ui.uiUtils,
  27019. domUtils = baidu.editor.dom.domUtils,
  27020. UIBase = baidu.editor.ui.UIBase,
  27021. Popup = (baidu.editor.ui.Popup = function (options) {
  27022. this.initOptions(options)
  27023. this.initPopup()
  27024. })
  27025. var allPopups = []
  27026. function closeAllPopup(evt, el) {
  27027. for (var i = 0; i < allPopups.length; i++) {
  27028. var pop = allPopups[i]
  27029. if (!pop.isHidden()) {
  27030. if (pop.queryAutoHide(el) !== false) {
  27031. if (
  27032. evt &&
  27033. /scroll/gi.test(evt.type) &&
  27034. pop.className == 'edui-wordpastepop'
  27035. )
  27036. return
  27037. pop.hide()
  27038. }
  27039. }
  27040. }
  27041. if (allPopups.length) pop.editor.fireEvent('afterhidepop')
  27042. }
  27043. Popup.postHide = closeAllPopup
  27044. var ANCHOR_CLASSES = [
  27045. 'edui-anchor-topleft',
  27046. 'edui-anchor-topright',
  27047. 'edui-anchor-bottomleft',
  27048. 'edui-anchor-bottomright',
  27049. ]
  27050. Popup.prototype = {
  27051. SHADOW_RADIUS: 5,
  27052. content: null,
  27053. _hidden: false,
  27054. autoRender: true,
  27055. canSideLeft: true,
  27056. canSideUp: true,
  27057. initPopup: function () {
  27058. this.initUIBase()
  27059. allPopups.push(this)
  27060. },
  27061. getHtmlTpl: function () {
  27062. return (
  27063. '<div id="##" class="edui-popup %%" onmousedown="return false;">' +
  27064. ' <div id="##_body" class="edui-popup-body">' +
  27065. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  27066. ' <div class="edui-shadow"></div>' +
  27067. ' <div id="##_content" class="edui-popup-content">' +
  27068. this.getContentHtmlTpl() +
  27069. ' </div>' +
  27070. ' </div>' +
  27071. '</div>'
  27072. )
  27073. },
  27074. getContentHtmlTpl: function () {
  27075. if (this.content) {
  27076. if (typeof this.content == 'string') {
  27077. return this.content
  27078. }
  27079. return this.content.renderHtml()
  27080. } else {
  27081. return ''
  27082. }
  27083. },
  27084. _UIBase_postRender: UIBase.prototype.postRender,
  27085. postRender: function () {
  27086. if (this.content instanceof UIBase) {
  27087. this.content.postRender()
  27088. }
  27089. //捕获鼠标滚轮
  27090. if (this.captureWheel && !this.captured) {
  27091. this.captured = true
  27092. var winHeight =
  27093. (document.documentElement.clientHeight ||
  27094. document.body.clientHeight) - 80,
  27095. _height = this.getDom().offsetHeight,
  27096. _top = uiUtils.getClientRect(this.combox.getDom()).top,
  27097. content = this.getDom('content'),
  27098. ifr = this.getDom('body').getElementsByTagName('iframe'),
  27099. me = this
  27100. ifr.length && (ifr = ifr[0])
  27101. while (_top + _height > winHeight) {
  27102. _height -= 30
  27103. }
  27104. content.style.height = _height + 'px'
  27105. //同步更改iframe高度
  27106. ifr && (ifr.style.height = _height + 'px')
  27107. //阻止在combox上的鼠标滚轮事件, 防止用户的正常操作被误解
  27108. if (window.XMLHttpRequest) {
  27109. domUtils.on(
  27110. content,
  27111. 'onmousewheel' in document.body ? 'mousewheel' : 'DOMMouseScroll',
  27112. function (e) {
  27113. if (e.preventDefault) {
  27114. e.preventDefault()
  27115. } else {
  27116. e.returnValue = false
  27117. }
  27118. if (e.wheelDelta) {
  27119. content.scrollTop -= (e.wheelDelta / 120) * 60
  27120. } else {
  27121. content.scrollTop -= (e.detail / -3) * 60
  27122. }
  27123. }
  27124. )
  27125. } else {
  27126. //ie6
  27127. domUtils.on(this.getDom(), 'mousewheel', function (e) {
  27128. e.returnValue = false
  27129. me.getDom('content').scrollTop -= (e.wheelDelta / 120) * 60
  27130. })
  27131. }
  27132. }
  27133. this.fireEvent('postRenderAfter')
  27134. this.hide(true)
  27135. this._UIBase_postRender()
  27136. },
  27137. _doAutoRender: function () {
  27138. if (!this.getDom() && this.autoRender) {
  27139. this.render()
  27140. }
  27141. },
  27142. mesureSize: function () {
  27143. var box = this.getDom('content')
  27144. return uiUtils.getClientRect(box)
  27145. },
  27146. fitSize: function () {
  27147. if (this.captureWheel && this.sized) {
  27148. return this.__size
  27149. }
  27150. this.sized = true
  27151. var popBodyEl = this.getDom('body')
  27152. popBodyEl.style.width = ''
  27153. popBodyEl.style.height = ''
  27154. var size = this.mesureSize()
  27155. if (this.captureWheel) {
  27156. popBodyEl.style.width = -(-20 - size.width) + 'px'
  27157. var height = parseInt(this.getDom('content').style.height, 10)
  27158. !window.isNaN(height) && (size.height = height)
  27159. } else {
  27160. popBodyEl.style.width = size.width + 'px'
  27161. }
  27162. popBodyEl.style.height = size.height + 'px'
  27163. this.__size = size
  27164. this.captureWheel && (this.getDom('content').style.overflow = 'auto')
  27165. return size
  27166. },
  27167. showAnchor: function (element, hoz) {
  27168. this.showAnchorRect(uiUtils.getClientRect(element), hoz)
  27169. },
  27170. showAnchorRect: function (rect, hoz, adj) {
  27171. this._doAutoRender()
  27172. var vpRect = uiUtils.getViewportRect()
  27173. this.getDom().style.visibility = 'hidden'
  27174. this._show()
  27175. var popSize = this.fitSize()
  27176. var sideLeft, sideUp, left, top
  27177. if (hoz) {
  27178. sideLeft =
  27179. this.canSideLeft &&
  27180. rect.right + popSize.width > vpRect.right &&
  27181. rect.left > popSize.width
  27182. sideUp =
  27183. this.canSideUp &&
  27184. rect.top + popSize.height > vpRect.bottom &&
  27185. rect.bottom > popSize.height
  27186. left = sideLeft ? rect.left - popSize.width : rect.right
  27187. top = sideUp ? rect.bottom - popSize.height : rect.top
  27188. } else {
  27189. sideLeft =
  27190. this.canSideLeft &&
  27191. rect.right + popSize.width > vpRect.right &&
  27192. rect.left > popSize.width
  27193. sideUp =
  27194. this.canSideUp &&
  27195. rect.top + popSize.height > vpRect.bottom &&
  27196. rect.bottom > popSize.height
  27197. left = sideLeft ? rect.right - popSize.width : rect.left
  27198. top = sideUp ? rect.top - popSize.height : rect.bottom
  27199. }
  27200. var popEl = this.getDom()
  27201. uiUtils.setViewportOffset(popEl, {
  27202. left: left,
  27203. top: top,
  27204. })
  27205. domUtils.removeClasses(popEl, ANCHOR_CLASSES)
  27206. popEl.className +=
  27207. ' ' + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)]
  27208. if (this.editor) {
  27209. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10
  27210. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  27211. popEl.style.zIndex - 1
  27212. }
  27213. this.getDom().style.visibility = 'visible'
  27214. },
  27215. showAt: function (offset) {
  27216. var left = offset.left
  27217. var top = offset.top
  27218. var rect = {
  27219. left: left,
  27220. top: top,
  27221. right: left,
  27222. bottom: top,
  27223. height: 0,
  27224. width: 0,
  27225. }
  27226. this.showAnchorRect(rect, false, true)
  27227. },
  27228. _show: function () {
  27229. if (this._hidden) {
  27230. var box = this.getDom()
  27231. box.style.display = ''
  27232. this._hidden = false
  27233. // if (box.setActive) {
  27234. // box.setActive();
  27235. // }
  27236. this.fireEvent('show')
  27237. }
  27238. },
  27239. isHidden: function () {
  27240. return this._hidden
  27241. },
  27242. show: function () {
  27243. this._doAutoRender()
  27244. this._show()
  27245. },
  27246. hide: function (notNofity) {
  27247. if (!this._hidden && this.getDom()) {
  27248. this.getDom().style.display = 'none'
  27249. this._hidden = true
  27250. if (!notNofity) {
  27251. this.fireEvent('hide')
  27252. }
  27253. }
  27254. },
  27255. queryAutoHide: function (el) {
  27256. return !el || !uiUtils.contains(this.getDom(), el)
  27257. },
  27258. }
  27259. utils.inherits(Popup, UIBase)
  27260. domUtils.on(document, 'mousedown', function (evt) {
  27261. var el = evt.target || evt.srcElement
  27262. closeAllPopup(evt, el)
  27263. })
  27264. domUtils.on(window, 'scroll', function (evt, el) {
  27265. closeAllPopup(evt, el)
  27266. })
  27267. })()
  27268. // ui/colorpicker.js
  27269. ///import core
  27270. ///import uicore
  27271. ;(function () {
  27272. var utils = baidu.editor.utils,
  27273. UIBase = baidu.editor.ui.UIBase,
  27274. ColorPicker = (baidu.editor.ui.ColorPicker = function (options) {
  27275. this.initOptions(options)
  27276. this.noColorText = this.noColorText || this.editor.getLang('clearColor')
  27277. this.initUIBase()
  27278. })
  27279. ColorPicker.prototype = {
  27280. getHtmlTpl: function () {
  27281. return genColorPicker(this.noColorText, this.editor)
  27282. },
  27283. _onTableClick: function (evt) {
  27284. var tgt = evt.target || evt.srcElement
  27285. var color = tgt.getAttribute('data-color')
  27286. if (color) {
  27287. this.fireEvent('pickcolor', color)
  27288. }
  27289. },
  27290. _onTableOver: function (evt) {
  27291. var tgt = evt.target || evt.srcElement
  27292. var color = tgt.getAttribute('data-color')
  27293. if (color) {
  27294. this.getDom('preview').style.backgroundColor = color
  27295. }
  27296. },
  27297. _onTableOut: function () {
  27298. this.getDom('preview').style.backgroundColor = ''
  27299. },
  27300. _onPickNoColor: function () {
  27301. this.fireEvent('picknocolor')
  27302. },
  27303. }
  27304. utils.inherits(ColorPicker, UIBase)
  27305. var COLORS = (
  27306. 'ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646,' +
  27307. 'f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada,' +
  27308. 'd8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5,' +
  27309. 'bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f,' +
  27310. 'a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09,' +
  27311. '7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806,' +
  27312. 'c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,'
  27313. ).split(',')
  27314. function genColorPicker(noColorText, editor) {
  27315. var html =
  27316. '<div id="##" class="edui-colorpicker %%">' +
  27317. '<div class="edui-colorpicker-topbar edui-clearfix">' +
  27318. '<div unselectable="on" id="##_preview" class="edui-colorpicker-preview"></div>' +
  27319. '<div unselectable="on" class="edui-colorpicker-nocolor" onclick="$$._onPickNoColor(event, this);">' +
  27320. noColorText +
  27321. '</div>' +
  27322. '</div>' +
  27323. '<table class="edui-box" style="border-collapse: collapse;" onmouseover="$$._onTableOver(event, this);" onmouseout="$$._onTableOut(event, this);" onclick="return $$._onTableClick(event, this);" cellspacing="0" cellpadding="0">' +
  27324. '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;padding-top: 2px"><td colspan="10">' +
  27325. editor.getLang('themeColor') +
  27326. '</td> </tr>' +
  27327. '<tr class="edui-colorpicker-tablefirstrow" >'
  27328. for (var i = 0; i < COLORS.length; i++) {
  27329. if (i && i % 10 === 0) {
  27330. html +=
  27331. '</tr>' +
  27332. (i == 60
  27333. ? '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;"><td colspan="10">' +
  27334. editor.getLang('standardColor') +
  27335. '</td></tr>'
  27336. : '') +
  27337. '<tr' +
  27338. (i == 60 ? ' class="edui-colorpicker-tablefirstrow"' : '') +
  27339. '>'
  27340. }
  27341. html +=
  27342. i < 70
  27343. ? '<td style="padding: 0 2px;"><a hidefocus title="' +
  27344. COLORS[i] +
  27345. '" onclick="return false;" href="javascript:" unselectable="on" class="edui-box edui-colorpicker-colorcell"' +
  27346. ' data-color="#' +
  27347. COLORS[i] +
  27348. '"' +
  27349. ' style="background-color:#' +
  27350. COLORS[i] +
  27351. ';border:solid #ccc;' +
  27352. (i < 10 || i >= 60
  27353. ? 'border-width:1px;'
  27354. : i >= 10 && i < 20
  27355. ? 'border-width:1px 1px 0 1px;'
  27356. : 'border-width:0 1px 0 1px;') +
  27357. '"' +
  27358. '></a></td>'
  27359. : ''
  27360. }
  27361. html += '</tr></table></div>'
  27362. return html
  27363. }
  27364. })()
  27365. // ui/tablepicker.js
  27366. ///import core
  27367. ///import uicore
  27368. ;(function () {
  27369. var utils = baidu.editor.utils,
  27370. uiUtils = baidu.editor.ui.uiUtils,
  27371. UIBase = baidu.editor.ui.UIBase
  27372. var TablePicker = (baidu.editor.ui.TablePicker = function (options) {
  27373. this.initOptions(options)
  27374. this.initTablePicker()
  27375. })
  27376. TablePicker.prototype = {
  27377. defaultNumRows: 10,
  27378. defaultNumCols: 10,
  27379. maxNumRows: 20,
  27380. maxNumCols: 20,
  27381. numRows: 10,
  27382. numCols: 10,
  27383. lengthOfCellSide: 22,
  27384. initTablePicker: function () {
  27385. this.initUIBase()
  27386. },
  27387. getHtmlTpl: function () {
  27388. var me = this
  27389. return (
  27390. '<div id="##" class="edui-tablepicker %%">' +
  27391. '<div class="edui-tablepicker-body">' +
  27392. '<div class="edui-infoarea">' +
  27393. '<span id="##_label" class="edui-label"></span>' +
  27394. '</div>' +
  27395. '<div class="edui-pickarea"' +
  27396. ' onmousemove="$$._onMouseMove(event, this);"' +
  27397. ' onmouseover="$$._onMouseOver(event, this);"' +
  27398. ' onmouseout="$$._onMouseOut(event, this);"' +
  27399. ' onclick="$$._onClick(event, this);"' +
  27400. '>' +
  27401. '<div id="##_overlay" class="edui-overlay"></div>' +
  27402. '</div>' +
  27403. '</div>' +
  27404. '</div>'
  27405. )
  27406. },
  27407. _UIBase_render: UIBase.prototype.render,
  27408. render: function (holder) {
  27409. this._UIBase_render(holder)
  27410. this.getDom('label').innerHTML =
  27411. '0' +
  27412. this.editor.getLang('t_row') +
  27413. ' x 0' +
  27414. this.editor.getLang('t_col')
  27415. },
  27416. _track: function (numCols, numRows) {
  27417. var style = this.getDom('overlay').style
  27418. var sideLen = this.lengthOfCellSide
  27419. style.width = numCols * sideLen + 'px'
  27420. style.height = numRows * sideLen + 'px'
  27421. var label = this.getDom('label')
  27422. label.innerHTML =
  27423. numCols +
  27424. this.editor.getLang('t_col') +
  27425. ' x ' +
  27426. numRows +
  27427. this.editor.getLang('t_row')
  27428. this.numCols = numCols
  27429. this.numRows = numRows
  27430. },
  27431. _onMouseOver: function (evt, el) {
  27432. var rel = evt.relatedTarget || evt.fromElement
  27433. if (!uiUtils.contains(el, rel) && el !== rel) {
  27434. this.getDom('label').innerHTML =
  27435. '0' +
  27436. this.editor.getLang('t_col') +
  27437. ' x 0' +
  27438. this.editor.getLang('t_row')
  27439. this.getDom('overlay').style.visibility = ''
  27440. }
  27441. },
  27442. _onMouseOut: function (evt, el) {
  27443. var rel = evt.relatedTarget || evt.toElement
  27444. if (!uiUtils.contains(el, rel) && el !== rel) {
  27445. this.getDom('label').innerHTML =
  27446. '0' +
  27447. this.editor.getLang('t_col') +
  27448. ' x 0' +
  27449. this.editor.getLang('t_row')
  27450. this.getDom('overlay').style.visibility = 'hidden'
  27451. }
  27452. },
  27453. _onMouseMove: function (evt, el) {
  27454. var style = this.getDom('overlay').style
  27455. var offset = uiUtils.getEventOffset(evt)
  27456. var sideLen = this.lengthOfCellSide
  27457. var numCols = Math.ceil(offset.left / sideLen)
  27458. var numRows = Math.ceil(offset.top / sideLen)
  27459. this._track(numCols, numRows)
  27460. },
  27461. _onClick: function () {
  27462. this.fireEvent('picktable', this.numCols, this.numRows)
  27463. },
  27464. }
  27465. utils.inherits(TablePicker, UIBase)
  27466. })()
  27467. // ui/stateful.js
  27468. ;(function () {
  27469. var browser = baidu.editor.browser,
  27470. domUtils = baidu.editor.dom.domUtils,
  27471. uiUtils = baidu.editor.ui.uiUtils
  27472. var TPL_STATEFUL =
  27473. 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
  27474. ' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
  27475. (browser.ie
  27476. ? ' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
  27477. ' onmouseleave="$$.Stateful_onMouseLeave(event, this);"'
  27478. : ' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
  27479. ' onmouseout="$$.Stateful_onMouseOut(event, this);"')
  27480. baidu.editor.ui.Stateful = {
  27481. alwalysHoverable: false,
  27482. target: null, //目标元素和this指向dom不一样
  27483. Stateful_init: function () {
  27484. this._Stateful_dGetHtmlTpl = this.getHtmlTpl
  27485. this.getHtmlTpl = this.Stateful_getHtmlTpl
  27486. },
  27487. Stateful_getHtmlTpl: function () {
  27488. var tpl = this._Stateful_dGetHtmlTpl()
  27489. // 使用function避免$转义
  27490. return tpl.replace(/stateful/g, function () {
  27491. return TPL_STATEFUL
  27492. })
  27493. },
  27494. Stateful_onMouseEnter: function (evt, el) {
  27495. this.target = el
  27496. if (!this.isDisabled() || this.alwalysHoverable) {
  27497. this.addState('hover')
  27498. this.fireEvent('over')
  27499. }
  27500. },
  27501. Stateful_onMouseLeave: function (evt, el) {
  27502. if (!this.isDisabled() || this.alwalysHoverable) {
  27503. this.removeState('hover')
  27504. this.removeState('active')
  27505. this.fireEvent('out')
  27506. }
  27507. },
  27508. Stateful_onMouseOver: function (evt, el) {
  27509. var rel = evt.relatedTarget
  27510. if (!uiUtils.contains(el, rel) && el !== rel) {
  27511. this.Stateful_onMouseEnter(evt, el)
  27512. }
  27513. },
  27514. Stateful_onMouseOut: function (evt, el) {
  27515. var rel = evt.relatedTarget
  27516. if (!uiUtils.contains(el, rel) && el !== rel) {
  27517. this.Stateful_onMouseLeave(evt, el)
  27518. }
  27519. },
  27520. Stateful_onMouseDown: function (evt, el) {
  27521. if (!this.isDisabled()) {
  27522. this.addState('active')
  27523. }
  27524. },
  27525. Stateful_onMouseUp: function (evt, el) {
  27526. if (!this.isDisabled()) {
  27527. this.removeState('active')
  27528. }
  27529. },
  27530. Stateful_postRender: function () {
  27531. if (this.disabled && !this.hasState('disabled')) {
  27532. this.addState('disabled')
  27533. }
  27534. },
  27535. hasState: function (state) {
  27536. return domUtils.hasClass(this.getStateDom(), 'edui-state-' + state)
  27537. },
  27538. addState: function (state) {
  27539. if (!this.hasState(state)) {
  27540. this.getStateDom().className += ' edui-state-' + state
  27541. }
  27542. },
  27543. removeState: function (state) {
  27544. if (this.hasState(state)) {
  27545. domUtils.removeClasses(this.getStateDom(), ['edui-state-' + state])
  27546. }
  27547. },
  27548. getStateDom: function () {
  27549. return this.getDom('state')
  27550. },
  27551. isChecked: function () {
  27552. return this.hasState('checked')
  27553. },
  27554. setChecked: function (checked) {
  27555. if (!this.isDisabled() && checked) {
  27556. this.addState('checked')
  27557. } else {
  27558. this.removeState('checked')
  27559. }
  27560. },
  27561. isDisabled: function () {
  27562. return this.hasState('disabled')
  27563. },
  27564. setDisabled: function (disabled) {
  27565. if (disabled) {
  27566. this.removeState('hover')
  27567. this.removeState('checked')
  27568. this.removeState('active')
  27569. this.addState('disabled')
  27570. } else {
  27571. this.removeState('disabled')
  27572. }
  27573. },
  27574. }
  27575. })()
  27576. // ui/button.js
  27577. ///import core
  27578. ///import uicore
  27579. ///import ui/stateful.js
  27580. ;(function () {
  27581. var utils = baidu.editor.utils,
  27582. UIBase = baidu.editor.ui.UIBase,
  27583. Stateful = baidu.editor.ui.Stateful,
  27584. Button = (baidu.editor.ui.Button = function (options) {
  27585. if (options.name) {
  27586. var btnName = options.name
  27587. var cssRules = options.cssRules
  27588. if (!options.className) {
  27589. options.className = 'edui-for-' + btnName
  27590. }
  27591. options.cssRules =
  27592. '.edui-default .edui-for-' +
  27593. btnName +
  27594. ' .edui-icon {' +
  27595. cssRules +
  27596. '}'
  27597. }
  27598. this.initOptions(options)
  27599. this.initButton()
  27600. })
  27601. Button.prototype = {
  27602. uiName: 'button',
  27603. label: '',
  27604. title: '',
  27605. showIcon: true,
  27606. showText: true,
  27607. cssRules: '',
  27608. initButton: function () {
  27609. this.initUIBase()
  27610. this.Stateful_init()
  27611. if (this.cssRules) {
  27612. utils.cssRule('edui-customize-' + this.name + '-style', this.cssRules)
  27613. }
  27614. },
  27615. getHtmlTpl: function () {
  27616. return (
  27617. '<div id="##" class="edui-box %%">' +
  27618. '<div id="##_state" stateful>' +
  27619. '<div class="%%-wrap"><div id="##_body" unselectable="on" ' +
  27620. (this.title ? 'title="' + this.title + '"' : '') +
  27621. ' class="%%-body" onmousedown="return $$._onMouseDown(event, this);" onclick="return $$._onClick(event, this);">' +
  27622. (this.showIcon ? '<div class="edui-box edui-icon"></div>' : '') +
  27623. (this.showText
  27624. ? '<div class="edui-box edui-label">' + this.label + '</div>'
  27625. : '') +
  27626. '</div>' +
  27627. '</div>' +
  27628. '</div></div>'
  27629. )
  27630. },
  27631. postRender: function () {
  27632. this.Stateful_postRender()
  27633. this.setDisabled(this.disabled)
  27634. },
  27635. _onMouseDown: function (e) {
  27636. var target = e.target || e.srcElement,
  27637. tagName = target && target.tagName && target.tagName.toLowerCase()
  27638. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  27639. return false
  27640. }
  27641. },
  27642. _onClick: function () {
  27643. if (!this.isDisabled()) {
  27644. this.fireEvent('click')
  27645. }
  27646. },
  27647. setTitle: function (text) {
  27648. var label = this.getDom('label')
  27649. label.innerHTML = text
  27650. },
  27651. }
  27652. utils.inherits(Button, UIBase)
  27653. utils.extend(Button.prototype, Stateful)
  27654. })()
  27655. // ui/splitbutton.js
  27656. ///import core
  27657. ///import uicore
  27658. ///import ui/stateful.js
  27659. ;(function () {
  27660. var utils = baidu.editor.utils,
  27661. uiUtils = baidu.editor.ui.uiUtils,
  27662. domUtils = baidu.editor.dom.domUtils,
  27663. UIBase = baidu.editor.ui.UIBase,
  27664. Stateful = baidu.editor.ui.Stateful,
  27665. SplitButton = (baidu.editor.ui.SplitButton = function (options) {
  27666. this.initOptions(options)
  27667. this.initSplitButton()
  27668. })
  27669. SplitButton.prototype = {
  27670. popup: null,
  27671. uiName: 'splitbutton',
  27672. title: '',
  27673. initSplitButton: function () {
  27674. this.initUIBase()
  27675. this.Stateful_init()
  27676. var me = this
  27677. if (this.popup != null) {
  27678. var popup = this.popup
  27679. this.popup = null
  27680. this.setPopup(popup)
  27681. }
  27682. },
  27683. _UIBase_postRender: UIBase.prototype.postRender,
  27684. postRender: function () {
  27685. this.Stateful_postRender()
  27686. this._UIBase_postRender()
  27687. },
  27688. setPopup: function (popup) {
  27689. if (this.popup === popup) return
  27690. if (this.popup != null) {
  27691. this.popup.dispose()
  27692. }
  27693. popup.addListener('show', utils.bind(this._onPopupShow, this))
  27694. popup.addListener('hide', utils.bind(this._onPopupHide, this))
  27695. popup.addListener(
  27696. 'postrender',
  27697. utils.bind(function () {
  27698. popup
  27699. .getDom('body')
  27700. .appendChild(
  27701. uiUtils.createElementByHtml(
  27702. '<div id="' +
  27703. this.popup.id +
  27704. '_bordereraser" class="edui-bordereraser edui-background" style="width:' +
  27705. (uiUtils.getClientRect(this.getDom()).width + 20) +
  27706. 'px"></div>'
  27707. )
  27708. )
  27709. popup.getDom().className += ' ' + this.className
  27710. }, this)
  27711. )
  27712. this.popup = popup
  27713. },
  27714. _onPopupShow: function () {
  27715. this.addState('opened')
  27716. },
  27717. _onPopupHide: function () {
  27718. this.removeState('opened')
  27719. },
  27720. getHtmlTpl: function () {
  27721. return (
  27722. '<div id="##" class="edui-box %%">' +
  27723. '<div ' +
  27724. (this.title ? 'title="' + this.title + '"' : '') +
  27725. ' id="##_state" stateful><div class="%%-body">' +
  27726. '<div id="##_button_body" class="edui-box edui-button-body" onclick="$$._onButtonClick(event, this);">' +
  27727. '<div class="edui-box edui-icon"></div>' +
  27728. '</div>' +
  27729. '<div class="edui-box edui-splitborder"></div>' +
  27730. '<div class="edui-box edui-arrow" onclick="$$._onArrowClick();"></div>' +
  27731. '</div></div></div>'
  27732. )
  27733. },
  27734. showPopup: function () {
  27735. // 当popup往上弹出的时候,做特殊处理
  27736. var rect = uiUtils.getClientRect(this.getDom())
  27737. rect.top -= this.popup.SHADOW_RADIUS
  27738. rect.height += this.popup.SHADOW_RADIUS
  27739. this.popup.showAnchorRect(rect)
  27740. },
  27741. _onArrowClick: function (event, el) {
  27742. if (!this.isDisabled()) {
  27743. this.showPopup()
  27744. }
  27745. },
  27746. _onButtonClick: function () {
  27747. if (!this.isDisabled()) {
  27748. this.fireEvent('buttonclick')
  27749. }
  27750. },
  27751. }
  27752. utils.inherits(SplitButton, UIBase)
  27753. utils.extend(SplitButton.prototype, Stateful, true)
  27754. })()
  27755. // ui/colorbutton.js
  27756. ///import core
  27757. ///import uicore
  27758. ///import ui/colorpicker.js
  27759. ///import ui/popup.js
  27760. ///import ui/splitbutton.js
  27761. ;(function () {
  27762. var utils = baidu.editor.utils,
  27763. uiUtils = baidu.editor.ui.uiUtils,
  27764. ColorPicker = baidu.editor.ui.ColorPicker,
  27765. Popup = baidu.editor.ui.Popup,
  27766. SplitButton = baidu.editor.ui.SplitButton,
  27767. ColorButton = (baidu.editor.ui.ColorButton = function (options) {
  27768. this.initOptions(options)
  27769. this.initColorButton()
  27770. })
  27771. ColorButton.prototype = {
  27772. initColorButton: function () {
  27773. var me = this
  27774. this.popup = new Popup({
  27775. content: new ColorPicker({
  27776. noColorText: me.editor.getLang('clearColor'),
  27777. editor: me.editor,
  27778. onpickcolor: function (t, color) {
  27779. me._onPickColor(color)
  27780. },
  27781. onpicknocolor: function (t, color) {
  27782. me._onPickNoColor(color)
  27783. },
  27784. }),
  27785. editor: me.editor,
  27786. })
  27787. this.initSplitButton()
  27788. },
  27789. _SplitButton_postRender: SplitButton.prototype.postRender,
  27790. postRender: function () {
  27791. this._SplitButton_postRender()
  27792. this.getDom('button_body').appendChild(
  27793. uiUtils.createElementByHtml(
  27794. '<div id="' + this.id + '_colorlump" class="edui-colorlump"></div>'
  27795. )
  27796. )
  27797. this.getDom().className += ' edui-colorbutton'
  27798. },
  27799. setColor: function (color) {
  27800. this.getDom('colorlump').style.backgroundColor = color
  27801. this.color = color
  27802. },
  27803. _onPickColor: function (color) {
  27804. if (this.fireEvent('pickcolor', color) !== false) {
  27805. this.setColor(color)
  27806. this.popup.hide()
  27807. }
  27808. },
  27809. _onPickNoColor: function (color) {
  27810. if (this.fireEvent('picknocolor') !== false) {
  27811. this.popup.hide()
  27812. }
  27813. },
  27814. }
  27815. utils.inherits(ColorButton, SplitButton)
  27816. })()
  27817. // ui/tablebutton.js
  27818. ///import core
  27819. ///import uicore
  27820. ///import ui/popup.js
  27821. ///import ui/tablepicker.js
  27822. ///import ui/splitbutton.js
  27823. ;(function () {
  27824. var utils = baidu.editor.utils,
  27825. Popup = baidu.editor.ui.Popup,
  27826. TablePicker = baidu.editor.ui.TablePicker,
  27827. SplitButton = baidu.editor.ui.SplitButton,
  27828. TableButton = (baidu.editor.ui.TableButton = function (options) {
  27829. this.initOptions(options)
  27830. this.initTableButton()
  27831. })
  27832. TableButton.prototype = {
  27833. initTableButton: function () {
  27834. var me = this
  27835. this.popup = new Popup({
  27836. content: new TablePicker({
  27837. editor: me.editor,
  27838. onpicktable: function (t, numCols, numRows) {
  27839. me._onPickTable(numCols, numRows)
  27840. },
  27841. }),
  27842. editor: me.editor,
  27843. })
  27844. this.initSplitButton()
  27845. },
  27846. _onPickTable: function (numCols, numRows) {
  27847. if (this.fireEvent('picktable', numCols, numRows) !== false) {
  27848. this.popup.hide()
  27849. }
  27850. },
  27851. }
  27852. utils.inherits(TableButton, SplitButton)
  27853. })()
  27854. // ui/autotypesetpicker.js
  27855. ///import core
  27856. ///import uicore
  27857. ;(function () {
  27858. var utils = baidu.editor.utils,
  27859. UIBase = baidu.editor.ui.UIBase
  27860. var AutoTypeSetPicker = (baidu.editor.ui.AutoTypeSetPicker = function (
  27861. options
  27862. ) {
  27863. this.initOptions(options)
  27864. this.initAutoTypeSetPicker()
  27865. })
  27866. AutoTypeSetPicker.prototype = {
  27867. initAutoTypeSetPicker: function () {
  27868. this.initUIBase()
  27869. },
  27870. getHtmlTpl: function () {
  27871. var me = this.editor,
  27872. opt = me.options.autotypeset,
  27873. lang = me.getLang('autoTypeSet')
  27874. var textAlignInputName = 'textAlignValue' + me.uid,
  27875. imageBlockInputName = 'imageBlockLineValue' + me.uid,
  27876. symbolConverInputName = 'symbolConverValue' + me.uid
  27877. return (
  27878. '<div id="##" class="edui-autotypesetpicker %%">' +
  27879. '<div class="edui-autotypesetpicker-body">' +
  27880. '<table >' +
  27881. '<tr><td nowrap><input type="checkbox" name="mergeEmptyline" ' +
  27882. (opt['mergeEmptyline'] ? 'checked' : '') +
  27883. '>' +
  27884. lang.mergeLine +
  27885. '</td><td colspan="2"><input type="checkbox" name="removeEmptyline" ' +
  27886. (opt['removeEmptyline'] ? 'checked' : '') +
  27887. '>' +
  27888. lang.delLine +
  27889. '</td></tr>' +
  27890. '<tr><td nowrap><input type="checkbox" name="removeClass" ' +
  27891. (opt['removeClass'] ? 'checked' : '') +
  27892. '>' +
  27893. lang.removeFormat +
  27894. '</td><td colspan="2"><input type="checkbox" name="indent" ' +
  27895. (opt['indent'] ? 'checked' : '') +
  27896. '>' +
  27897. lang.indent +
  27898. '</td></tr>' +
  27899. '<tr>' +
  27900. '<td nowrap><input type="checkbox" name="textAlign" ' +
  27901. (opt['textAlign'] ? 'checked' : '') +
  27902. '>' +
  27903. lang.alignment +
  27904. '</td>' +
  27905. '<td colspan="2" id="' +
  27906. textAlignInputName +
  27907. '">' +
  27908. '<input type="radio" name="' +
  27909. textAlignInputName +
  27910. '" value="left" ' +
  27911. (opt['textAlign'] && opt['textAlign'] == 'left' ? 'checked' : '') +
  27912. '>' +
  27913. me.getLang('justifyleft') +
  27914. '<input type="radio" name="' +
  27915. textAlignInputName +
  27916. '" value="center" ' +
  27917. (opt['textAlign'] && opt['textAlign'] == 'center' ? 'checked' : '') +
  27918. '>' +
  27919. me.getLang('justifycenter') +
  27920. '<input type="radio" name="' +
  27921. textAlignInputName +
  27922. '" value="right" ' +
  27923. (opt['textAlign'] && opt['textAlign'] == 'right' ? 'checked' : '') +
  27924. '>' +
  27925. me.getLang('justifyright') +
  27926. '</td>' +
  27927. '</tr>' +
  27928. '<tr>' +
  27929. '<td nowrap><input type="checkbox" name="imageBlockLine" ' +
  27930. (opt['imageBlockLine'] ? 'checked' : '') +
  27931. '>' +
  27932. lang.imageFloat +
  27933. '</td>' +
  27934. '<td nowrap id="' +
  27935. imageBlockInputName +
  27936. '">' +
  27937. '<input type="radio" name="' +
  27938. imageBlockInputName +
  27939. '" value="none" ' +
  27940. (opt['imageBlockLine'] && opt['imageBlockLine'] == 'none'
  27941. ? 'checked'
  27942. : '') +
  27943. '>' +
  27944. me.getLang('default') +
  27945. '<input type="radio" name="' +
  27946. imageBlockInputName +
  27947. '" value="left" ' +
  27948. (opt['imageBlockLine'] && opt['imageBlockLine'] == 'left'
  27949. ? 'checked'
  27950. : '') +
  27951. '>' +
  27952. me.getLang('justifyleft') +
  27953. '<input type="radio" name="' +
  27954. imageBlockInputName +
  27955. '" value="center" ' +
  27956. (opt['imageBlockLine'] && opt['imageBlockLine'] == 'center'
  27957. ? 'checked'
  27958. : '') +
  27959. '>' +
  27960. me.getLang('justifycenter') +
  27961. '<input type="radio" name="' +
  27962. imageBlockInputName +
  27963. '" value="right" ' +
  27964. (opt['imageBlockLine'] && opt['imageBlockLine'] == 'right'
  27965. ? 'checked'
  27966. : '') +
  27967. '>' +
  27968. me.getLang('justifyright') +
  27969. '</td>' +
  27970. '</tr>' +
  27971. '<tr><td nowrap><input type="checkbox" name="clearFontSize" ' +
  27972. (opt['clearFontSize'] ? 'checked' : '') +
  27973. '>' +
  27974. lang.removeFontsize +
  27975. '</td><td colspan="2"><input type="checkbox" name="clearFontFamily" ' +
  27976. (opt['clearFontFamily'] ? 'checked' : '') +
  27977. '>' +
  27978. lang.removeFontFamily +
  27979. '</td></tr>' +
  27980. '<tr><td nowrap colspan="3"><input type="checkbox" name="removeEmptyNode" ' +
  27981. (opt['removeEmptyNode'] ? 'checked' : '') +
  27982. '>' +
  27983. lang.removeHtml +
  27984. '</td></tr>' +
  27985. '<tr><td nowrap colspan="3"><input type="checkbox" name="pasteFilter" ' +
  27986. (opt['pasteFilter'] ? 'checked' : '') +
  27987. '>' +
  27988. lang.pasteFilter +
  27989. '</td></tr>' +
  27990. '<tr>' +
  27991. '<td nowrap><input type="checkbox" name="symbolConver" ' +
  27992. (opt['bdc2sb'] || opt['tobdc'] ? 'checked' : '') +
  27993. '>' +
  27994. lang.symbol +
  27995. '</td>' +
  27996. '<td id="' +
  27997. symbolConverInputName +
  27998. '">' +
  27999. '<input type="radio" name="bdc" value="bdc2sb" ' +
  28000. (opt['bdc2sb'] ? 'checked' : '') +
  28001. '>' +
  28002. lang.bdc2sb +
  28003. '<input type="radio" name="bdc" value="tobdc" ' +
  28004. (opt['tobdc'] ? 'checked' : '') +
  28005. '>' +
  28006. lang.tobdc +
  28007. '' +
  28008. '</td>' +
  28009. '<td nowrap align="right"><button >' +
  28010. lang.run +
  28011. '</button></td>' +
  28012. '</tr>' +
  28013. '</table>' +
  28014. '</div>' +
  28015. '</div>'
  28016. )
  28017. },
  28018. _UIBase_render: UIBase.prototype.render,
  28019. }
  28020. utils.inherits(AutoTypeSetPicker, UIBase)
  28021. })()
  28022. // ui/autotypesetbutton.js
  28023. ///import core
  28024. ///import uicore
  28025. ///import ui/popup.js
  28026. ///import ui/autotypesetpicker.js
  28027. ///import ui/splitbutton.js
  28028. ;(function () {
  28029. var utils = baidu.editor.utils,
  28030. Popup = baidu.editor.ui.Popup,
  28031. AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker,
  28032. SplitButton = baidu.editor.ui.SplitButton,
  28033. AutoTypeSetButton = (baidu.editor.ui.AutoTypeSetButton = function (
  28034. options
  28035. ) {
  28036. this.initOptions(options)
  28037. this.initAutoTypeSetButton()
  28038. })
  28039. function getPara(me) {
  28040. var opt = {},
  28041. cont = me.getDom(),
  28042. editorId = me.editor.uid,
  28043. inputType = null,
  28044. attrName = null,
  28045. ipts = domUtils.getElementsByTagName(cont, 'input')
  28046. for (var i = ipts.length - 1, ipt; (ipt = ipts[i--]); ) {
  28047. inputType = ipt.getAttribute('type')
  28048. if (inputType == 'checkbox') {
  28049. attrName = ipt.getAttribute('name')
  28050. opt[attrName] && delete opt[attrName]
  28051. if (ipt.checked) {
  28052. var attrValue = document.getElementById(
  28053. attrName + 'Value' + editorId
  28054. )
  28055. if (attrValue) {
  28056. if (/input/gi.test(attrValue.tagName)) {
  28057. opt[attrName] = attrValue.value
  28058. } else {
  28059. var iptChilds = attrValue.getElementsByTagName('input')
  28060. for (
  28061. var j = iptChilds.length - 1, iptchild;
  28062. (iptchild = iptChilds[j--]);
  28063. ) {
  28064. if (iptchild.checked) {
  28065. opt[attrName] = iptchild.value
  28066. break
  28067. }
  28068. }
  28069. }
  28070. } else {
  28071. opt[attrName] = true
  28072. }
  28073. } else {
  28074. opt[attrName] = false
  28075. }
  28076. } else {
  28077. opt[ipt.getAttribute('value')] = ipt.checked
  28078. }
  28079. }
  28080. var selects = domUtils.getElementsByTagName(cont, 'select')
  28081. for (var i = 0, si; (si = selects[i++]); ) {
  28082. var attr = si.getAttribute('name')
  28083. opt[attr] = opt[attr] ? si.value : ''
  28084. }
  28085. utils.extend(me.editor.options.autotypeset, opt)
  28086. me.editor.setPreferences('autotypeset', opt)
  28087. }
  28088. AutoTypeSetButton.prototype = {
  28089. initAutoTypeSetButton: function () {
  28090. var me = this
  28091. this.popup = new Popup({
  28092. //传入配置参数
  28093. content: new AutoTypeSetPicker({ editor: me.editor }),
  28094. editor: me.editor,
  28095. hide: function () {
  28096. if (!this._hidden && this.getDom()) {
  28097. getPara(this)
  28098. this.getDom().style.display = 'none'
  28099. this._hidden = true
  28100. this.fireEvent('hide')
  28101. }
  28102. },
  28103. })
  28104. var flag = 0
  28105. this.popup.addListener('postRenderAfter', function () {
  28106. var popupUI = this
  28107. if (flag) return
  28108. var cont = this.getDom(),
  28109. btn = cont.getElementsByTagName('button')[0]
  28110. btn.onclick = function () {
  28111. getPara(popupUI)
  28112. me.editor.execCommand('autotypeset')
  28113. popupUI.hide()
  28114. }
  28115. domUtils.on(cont, 'click', function (e) {
  28116. var target = e.target || e.srcElement,
  28117. editorId = me.editor.uid
  28118. if (target && target.tagName == 'INPUT') {
  28119. // 点击图片浮动的checkbox,去除对应的radio
  28120. if (
  28121. target.name == 'imageBlockLine' ||
  28122. target.name == 'textAlign' ||
  28123. target.name == 'symbolConver'
  28124. ) {
  28125. var checked = target.checked,
  28126. radioTd = document.getElementById(
  28127. target.name + 'Value' + editorId
  28128. ),
  28129. radios = radioTd.getElementsByTagName('input'),
  28130. defalutSelect = {
  28131. imageBlockLine: 'none',
  28132. textAlign: 'left',
  28133. symbolConver: 'tobdc',
  28134. }
  28135. for (var i = 0; i < radios.length; i++) {
  28136. if (checked) {
  28137. if (radios[i].value == defalutSelect[target.name]) {
  28138. radios[i].checked = 'checked'
  28139. }
  28140. } else {
  28141. radios[i].checked = false
  28142. }
  28143. }
  28144. }
  28145. // 点击radio,选中对应的checkbox
  28146. if (
  28147. target.name == 'imageBlockLineValue' + editorId ||
  28148. target.name == 'textAlignValue' + editorId ||
  28149. target.name == 'bdc'
  28150. ) {
  28151. var checkboxs =
  28152. target.parentNode.previousSibling.getElementsByTagName(
  28153. 'input'
  28154. )
  28155. checkboxs && (checkboxs[0].checked = true)
  28156. }
  28157. getPara(popupUI)
  28158. }
  28159. })
  28160. flag = 1
  28161. })
  28162. this.initSplitButton()
  28163. },
  28164. }
  28165. utils.inherits(AutoTypeSetButton, SplitButton)
  28166. })()
  28167. // ui/cellalignpicker.js
  28168. ///import core
  28169. ///import uicore
  28170. ;(function () {
  28171. var utils = baidu.editor.utils,
  28172. Popup = baidu.editor.ui.Popup,
  28173. Stateful = baidu.editor.ui.Stateful,
  28174. UIBase = baidu.editor.ui.UIBase
  28175. /**
  28176. * 该参数将新增一个参数: selected, 参数类型为一个Object, 形如{ 'align': 'center', 'valign': 'top' }, 表示单元格的初始
  28177. * 对齐状态为: 竖直居上,水平居中; 其中 align的取值为:'center', 'left', 'right'; valign的取值为: 'top', 'middle', 'bottom'
  28178. * @update 2013/4/2 hancong03@baidu.com
  28179. */
  28180. var CellAlignPicker = (baidu.editor.ui.CellAlignPicker = function (
  28181. options
  28182. ) {
  28183. this.initOptions(options)
  28184. this.initSelected()
  28185. this.initCellAlignPicker()
  28186. })
  28187. CellAlignPicker.prototype = {
  28188. //初始化选中状态, 该方法将根据传递进来的参数获取到应该选中的对齐方式图标的索引
  28189. initSelected: function () {
  28190. var status = {
  28191. valign: {
  28192. top: 0,
  28193. middle: 1,
  28194. bottom: 2,
  28195. },
  28196. align: {
  28197. left: 0,
  28198. center: 1,
  28199. right: 2,
  28200. },
  28201. count: 3,
  28202. },
  28203. result = -1
  28204. if (this.selected) {
  28205. this.selectedIndex =
  28206. status.valign[this.selected.valign] * status.count +
  28207. status.align[this.selected.align]
  28208. }
  28209. },
  28210. initCellAlignPicker: function () {
  28211. this.initUIBase()
  28212. this.Stateful_init()
  28213. },
  28214. getHtmlTpl: function () {
  28215. var alignType = ['left', 'center', 'right'],
  28216. COUNT = 9,
  28217. tempClassName = null,
  28218. tempIndex = -1,
  28219. tmpl = []
  28220. for (var i = 0; i < COUNT; i++) {
  28221. tempClassName =
  28222. this.selectedIndex === i ? ' class="edui-cellalign-selected" ' : ''
  28223. tempIndex = i % 3
  28224. tempIndex === 0 && tmpl.push('<tr>')
  28225. tmpl.push(
  28226. '<td index="' +
  28227. i +
  28228. '" ' +
  28229. tempClassName +
  28230. ' stateful><div class="edui-icon edui-' +
  28231. alignType[tempIndex] +
  28232. '"></div></td>'
  28233. )
  28234. tempIndex === 2 && tmpl.push('</tr>')
  28235. }
  28236. return (
  28237. '<div id="##" class="edui-cellalignpicker %%">' +
  28238. '<div class="edui-cellalignpicker-body">' +
  28239. '<table onclick="$$._onClick(event);">' +
  28240. tmpl.join('') +
  28241. '</table>' +
  28242. '</div>' +
  28243. '</div>'
  28244. )
  28245. },
  28246. getStateDom: function () {
  28247. return this.target
  28248. },
  28249. _onClick: function (evt) {
  28250. var target = evt.target || evt.srcElement
  28251. if (/icon/.test(target.className)) {
  28252. this.items[target.parentNode.getAttribute('index')].onclick()
  28253. Popup.postHide(evt)
  28254. }
  28255. },
  28256. _UIBase_render: UIBase.prototype.render,
  28257. }
  28258. utils.inherits(CellAlignPicker, UIBase)
  28259. utils.extend(CellAlignPicker.prototype, Stateful, true)
  28260. })()
  28261. // ui/pastepicker.js
  28262. ///import core
  28263. ///import uicore
  28264. ;(function () {
  28265. var utils = baidu.editor.utils,
  28266. Stateful = baidu.editor.ui.Stateful,
  28267. uiUtils = baidu.editor.ui.uiUtils,
  28268. UIBase = baidu.editor.ui.UIBase
  28269. var PastePicker = (baidu.editor.ui.PastePicker = function (options) {
  28270. this.initOptions(options)
  28271. this.initPastePicker()
  28272. })
  28273. PastePicker.prototype = {
  28274. initPastePicker: function () {
  28275. this.initUIBase()
  28276. this.Stateful_init()
  28277. },
  28278. getHtmlTpl: function () {
  28279. return (
  28280. '<div class="edui-pasteicon" onclick="$$._onClick(this)"></div>' +
  28281. '<div class="edui-pastecontainer">' +
  28282. '<div class="edui-title">' +
  28283. this.editor.getLang('pasteOpt') +
  28284. '</div>' +
  28285. '<div class="edui-button">' +
  28286. '<div title="' +
  28287. this.editor.getLang('pasteSourceFormat') +
  28288. '" onclick="$$.format(false)" stateful>' +
  28289. '<div class="edui-richtxticon"></div></div>' +
  28290. '<div title="' +
  28291. this.editor.getLang('tagFormat') +
  28292. '" onclick="$$.format(2)" stateful>' +
  28293. '<div class="edui-tagicon"></div></div>' +
  28294. '<div title="' +
  28295. this.editor.getLang('pasteTextFormat') +
  28296. '" onclick="$$.format(true)" stateful>' +
  28297. '<div class="edui-plaintxticon"></div></div>' +
  28298. '</div>' +
  28299. '</div>' +
  28300. '</div>'
  28301. )
  28302. },
  28303. getStateDom: function () {
  28304. return this.target
  28305. },
  28306. format: function (param) {
  28307. this.editor.ui._isTransfer = true
  28308. this.editor.fireEvent('pasteTransfer', param)
  28309. },
  28310. _onClick: function (cur) {
  28311. var node = domUtils.getNextDomNode(cur),
  28312. screenHt = uiUtils.getViewportRect().height,
  28313. subPop = uiUtils.getClientRect(node)
  28314. if (subPop.top + subPop.height > screenHt)
  28315. node.style.top = -subPop.height - cur.offsetHeight + 'px'
  28316. else node.style.top = ''
  28317. if (/hidden/gi.test(domUtils.getComputedStyle(node, 'visibility'))) {
  28318. node.style.visibility = 'visible'
  28319. domUtils.addClass(cur, 'edui-state-opened')
  28320. } else {
  28321. node.style.visibility = 'hidden'
  28322. domUtils.removeClasses(cur, 'edui-state-opened')
  28323. }
  28324. },
  28325. _UIBase_render: UIBase.prototype.render,
  28326. }
  28327. utils.inherits(PastePicker, UIBase)
  28328. utils.extend(PastePicker.prototype, Stateful, true)
  28329. })()
  28330. // ui/toolbar.js
  28331. ;(function () {
  28332. var utils = baidu.editor.utils,
  28333. uiUtils = baidu.editor.ui.uiUtils,
  28334. UIBase = baidu.editor.ui.UIBase,
  28335. Toolbar = (baidu.editor.ui.Toolbar = function (options) {
  28336. this.initOptions(options)
  28337. this.initToolbar()
  28338. })
  28339. Toolbar.prototype = {
  28340. items: null,
  28341. initToolbar: function () {
  28342. this.items = this.items || []
  28343. this.initUIBase()
  28344. },
  28345. add: function (item, index) {
  28346. if (index === undefined) {
  28347. this.items.push(item)
  28348. } else {
  28349. this.items.splice(index, 0, item)
  28350. }
  28351. },
  28352. getHtmlTpl: function () {
  28353. var buff = []
  28354. for (var i = 0; i < this.items.length; i++) {
  28355. buff[i] = this.items[i].renderHtml()
  28356. }
  28357. return (
  28358. '<div id="##" class="edui-toolbar %%" onselectstart="return false;" onmousedown="return $$._onMouseDown(event, this);">' +
  28359. buff.join('') +
  28360. '</div>'
  28361. )
  28362. },
  28363. postRender: function () {
  28364. var box = this.getDom()
  28365. for (var i = 0; i < this.items.length; i++) {
  28366. this.items[i].postRender()
  28367. }
  28368. uiUtils.makeUnselectable(box)
  28369. },
  28370. _onMouseDown: function (e) {
  28371. var target = e.target || e.srcElement,
  28372. tagName = target && target.tagName && target.tagName.toLowerCase()
  28373. if (tagName == 'input' || tagName == 'object' || tagName == 'object') {
  28374. return false
  28375. }
  28376. },
  28377. }
  28378. utils.inherits(Toolbar, UIBase)
  28379. })()
  28380. // ui/menu.js
  28381. ///import core
  28382. ///import uicore
  28383. ///import ui\popup.js
  28384. ///import ui\stateful.js
  28385. ;(function () {
  28386. var utils = baidu.editor.utils,
  28387. domUtils = baidu.editor.dom.domUtils,
  28388. uiUtils = baidu.editor.ui.uiUtils,
  28389. UIBase = baidu.editor.ui.UIBase,
  28390. Popup = baidu.editor.ui.Popup,
  28391. Stateful = baidu.editor.ui.Stateful,
  28392. CellAlignPicker = baidu.editor.ui.CellAlignPicker,
  28393. Menu = (baidu.editor.ui.Menu = function (options) {
  28394. this.initOptions(options)
  28395. this.initMenu()
  28396. })
  28397. var menuSeparator = {
  28398. renderHtml: function () {
  28399. return '<div class="edui-menuitem edui-menuseparator"><div class="edui-menuseparator-inner"></div></div>'
  28400. },
  28401. postRender: function () {},
  28402. queryAutoHide: function () {
  28403. return true
  28404. },
  28405. }
  28406. Menu.prototype = {
  28407. items: null,
  28408. uiName: 'menu',
  28409. initMenu: function () {
  28410. this.items = this.items || []
  28411. this.initPopup()
  28412. this.initItems()
  28413. },
  28414. initItems: function () {
  28415. for (var i = 0; i < this.items.length; i++) {
  28416. var item = this.items[i]
  28417. if (item == '-') {
  28418. this.items[i] = this.getSeparator()
  28419. } else if (!(item instanceof MenuItem)) {
  28420. item.editor = this.editor
  28421. item.theme = this.editor.options.theme
  28422. this.items[i] = this.createItem(item)
  28423. }
  28424. }
  28425. },
  28426. getSeparator: function () {
  28427. return menuSeparator
  28428. },
  28429. createItem: function (item) {
  28430. //新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28431. item.menu = this
  28432. return new MenuItem(item)
  28433. },
  28434. _Popup_getContentHtmlTpl: Popup.prototype.getContentHtmlTpl,
  28435. getContentHtmlTpl: function () {
  28436. if (this.items.length == 0) {
  28437. return this._Popup_getContentHtmlTpl()
  28438. }
  28439. var buff = []
  28440. for (var i = 0; i < this.items.length; i++) {
  28441. var item = this.items[i]
  28442. buff[i] = item.renderHtml()
  28443. }
  28444. return '<div class="%%-body">' + buff.join('') + '</div>'
  28445. },
  28446. _Popup_postRender: Popup.prototype.postRender,
  28447. postRender: function () {
  28448. var me = this
  28449. for (var i = 0; i < this.items.length; i++) {
  28450. var item = this.items[i]
  28451. item.ownerMenu = this
  28452. item.postRender()
  28453. }
  28454. domUtils.on(this.getDom(), 'mouseover', function (evt) {
  28455. evt = evt || event
  28456. var rel = evt.relatedTarget || evt.fromElement
  28457. var el = me.getDom()
  28458. if (!uiUtils.contains(el, rel) && el !== rel) {
  28459. me.fireEvent('over')
  28460. }
  28461. })
  28462. this._Popup_postRender()
  28463. },
  28464. queryAutoHide: function (el) {
  28465. if (el) {
  28466. if (uiUtils.contains(this.getDom(), el)) {
  28467. return false
  28468. }
  28469. for (var i = 0; i < this.items.length; i++) {
  28470. var item = this.items[i]
  28471. if (item.queryAutoHide(el) === false) {
  28472. return false
  28473. }
  28474. }
  28475. }
  28476. },
  28477. clearItems: function () {
  28478. for (var i = 0; i < this.items.length; i++) {
  28479. var item = this.items[i]
  28480. clearTimeout(item._showingTimer)
  28481. clearTimeout(item._closingTimer)
  28482. if (item.subMenu) {
  28483. item.subMenu.destroy()
  28484. }
  28485. }
  28486. this.items = []
  28487. },
  28488. destroy: function () {
  28489. if (this.getDom()) {
  28490. domUtils.remove(this.getDom())
  28491. }
  28492. this.clearItems()
  28493. },
  28494. dispose: function () {
  28495. this.destroy()
  28496. },
  28497. }
  28498. utils.inherits(Menu, Popup)
  28499. /**
  28500. * @update 2013/04/03 hancong03 新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28501. * @type {Function}
  28502. */
  28503. var MenuItem = (baidu.editor.ui.MenuItem = function (options) {
  28504. this.initOptions(options)
  28505. this.initUIBase()
  28506. this.Stateful_init()
  28507. if (this.subMenu && !(this.subMenu instanceof Menu)) {
  28508. if (options.className && options.className.indexOf('aligntd') != -1) {
  28509. var me = this
  28510. //获取单元格对齐初始状态
  28511. this.subMenu.selected = this.editor.queryCommandValue('cellalignment')
  28512. this.subMenu = new Popup({
  28513. content: new CellAlignPicker(this.subMenu),
  28514. parentMenu: me,
  28515. editor: me.editor,
  28516. destroy: function () {
  28517. if (this.getDom()) {
  28518. domUtils.remove(this.getDom())
  28519. }
  28520. },
  28521. })
  28522. this.subMenu.addListener('postRenderAfter', function () {
  28523. domUtils.on(this.getDom(), 'mouseover', function () {
  28524. me.addState('opened')
  28525. })
  28526. })
  28527. } else {
  28528. this.subMenu = new Menu(this.subMenu)
  28529. }
  28530. }
  28531. })
  28532. MenuItem.prototype = {
  28533. label: '',
  28534. subMenu: null,
  28535. ownerMenu: null,
  28536. uiName: 'menuitem',
  28537. alwalysHoverable: true,
  28538. getHtmlTpl: function () {
  28539. return (
  28540. '<div id="##" class="%%" stateful onclick="$$._onClick(event, this);">' +
  28541. '<div class="%%-body">' +
  28542. this.renderLabelHtml() +
  28543. '</div>' +
  28544. '</div>'
  28545. )
  28546. },
  28547. postRender: function () {
  28548. var me = this
  28549. this.addListener('over', function () {
  28550. me.ownerMenu.fireEvent('submenuover', me)
  28551. if (me.subMenu) {
  28552. me.delayShowSubMenu()
  28553. }
  28554. })
  28555. if (this.subMenu) {
  28556. this.getDom().className += ' edui-hassubmenu'
  28557. this.subMenu.render()
  28558. this.addListener('out', function () {
  28559. me.delayHideSubMenu()
  28560. })
  28561. this.subMenu.addListener('over', function () {
  28562. clearTimeout(me._closingTimer)
  28563. me._closingTimer = null
  28564. me.addState('opened')
  28565. })
  28566. this.ownerMenu.addListener('hide', function () {
  28567. me.hideSubMenu()
  28568. })
  28569. this.ownerMenu.addListener('submenuover', function (t, subMenu) {
  28570. if (subMenu !== me) {
  28571. me.delayHideSubMenu()
  28572. }
  28573. })
  28574. this.subMenu._bakQueryAutoHide = this.subMenu.queryAutoHide
  28575. this.subMenu.queryAutoHide = function (el) {
  28576. if (el && uiUtils.contains(me.getDom(), el)) {
  28577. return false
  28578. }
  28579. return this._bakQueryAutoHide(el)
  28580. }
  28581. }
  28582. this.getDom().style.tabIndex = '-1'
  28583. uiUtils.makeUnselectable(this.getDom())
  28584. this.Stateful_postRender()
  28585. },
  28586. delayShowSubMenu: function () {
  28587. var me = this
  28588. if (!me.isDisabled()) {
  28589. me.addState('opened')
  28590. clearTimeout(me._showingTimer)
  28591. clearTimeout(me._closingTimer)
  28592. me._closingTimer = null
  28593. me._showingTimer = setTimeout(function () {
  28594. me.showSubMenu()
  28595. }, 250)
  28596. }
  28597. },
  28598. delayHideSubMenu: function () {
  28599. var me = this
  28600. if (!me.isDisabled()) {
  28601. me.removeState('opened')
  28602. clearTimeout(me._showingTimer)
  28603. if (!me._closingTimer) {
  28604. me._closingTimer = setTimeout(function () {
  28605. if (!me.hasState('opened')) {
  28606. me.hideSubMenu()
  28607. }
  28608. me._closingTimer = null
  28609. }, 400)
  28610. }
  28611. }
  28612. },
  28613. renderLabelHtml: function () {
  28614. return (
  28615. '<div class="edui-arrow"></div>' +
  28616. '<div class="edui-box edui-icon"></div>' +
  28617. '<div class="edui-box edui-label %%-label">' +
  28618. (this.label || '') +
  28619. '</div>'
  28620. )
  28621. },
  28622. getStateDom: function () {
  28623. return this.getDom()
  28624. },
  28625. queryAutoHide: function (el) {
  28626. if (this.subMenu && this.hasState('opened')) {
  28627. return this.subMenu.queryAutoHide(el)
  28628. }
  28629. },
  28630. _onClick: function (event, this_) {
  28631. if (this.hasState('disabled')) return
  28632. if (this.fireEvent('click', event, this_) !== false) {
  28633. if (this.subMenu) {
  28634. this.showSubMenu()
  28635. } else {
  28636. Popup.postHide(event)
  28637. }
  28638. }
  28639. },
  28640. showSubMenu: function () {
  28641. var rect = uiUtils.getClientRect(this.getDom())
  28642. rect.right -= 5
  28643. rect.left += 2
  28644. rect.width -= 7
  28645. rect.top -= 4
  28646. rect.bottom += 4
  28647. rect.height += 8
  28648. this.subMenu.showAnchorRect(rect, true, true)
  28649. },
  28650. hideSubMenu: function () {
  28651. this.subMenu.hide()
  28652. },
  28653. }
  28654. utils.inherits(MenuItem, UIBase)
  28655. utils.extend(MenuItem.prototype, Stateful, true)
  28656. })()
  28657. // ui/combox.js
  28658. ///import core
  28659. ///import uicore
  28660. ///import ui/menu.js
  28661. ///import ui/splitbutton.js
  28662. ;(function () {
  28663. // todo: menu和item提成通用list
  28664. var utils = baidu.editor.utils,
  28665. uiUtils = baidu.editor.ui.uiUtils,
  28666. Menu = baidu.editor.ui.Menu,
  28667. SplitButton = baidu.editor.ui.SplitButton,
  28668. Combox = (baidu.editor.ui.Combox = function (options) {
  28669. this.initOptions(options)
  28670. this.initCombox()
  28671. })
  28672. Combox.prototype = {
  28673. uiName: 'combox',
  28674. onbuttonclick: function () {
  28675. this.showPopup()
  28676. },
  28677. initCombox: function () {
  28678. var me = this
  28679. this.items = this.items || []
  28680. for (var i = 0; i < this.items.length; i++) {
  28681. var item = this.items[i]
  28682. item.uiName = 'listitem'
  28683. item.index = i
  28684. item.onclick = function () {
  28685. me.selectByIndex(this.index)
  28686. }
  28687. }
  28688. this.popup = new Menu({
  28689. items: this.items,
  28690. uiName: 'list',
  28691. editor: this.editor,
  28692. captureWheel: true,
  28693. combox: this,
  28694. })
  28695. this.initSplitButton()
  28696. },
  28697. _SplitButton_postRender: SplitButton.prototype.postRender,
  28698. postRender: function () {
  28699. this._SplitButton_postRender()
  28700. this.setLabel(this.label || '')
  28701. this.setValue(this.initValue || '')
  28702. },
  28703. showPopup: function () {
  28704. var rect = uiUtils.getClientRect(this.getDom())
  28705. rect.top += 1
  28706. rect.bottom -= 1
  28707. rect.height -= 2
  28708. this.popup.showAnchorRect(rect)
  28709. },
  28710. getValue: function () {
  28711. return this.value
  28712. },
  28713. setValue: function (value) {
  28714. var index = this.indexByValue(value)
  28715. if (index != -1) {
  28716. this.selectedIndex = index
  28717. this.setLabel(this.items[index].label)
  28718. this.value = this.items[index].value
  28719. } else {
  28720. this.selectedIndex = -1
  28721. this.setLabel(this.getLabelForUnknowValue(value))
  28722. this.value = value
  28723. }
  28724. },
  28725. setLabel: function (label) {
  28726. this.getDom('button_body').innerHTML = label
  28727. this.label = label
  28728. },
  28729. getLabelForUnknowValue: function (value) {
  28730. return value
  28731. },
  28732. indexByValue: function (value) {
  28733. for (var i = 0; i < this.items.length; i++) {
  28734. if (value == this.items[i].value) {
  28735. return i
  28736. }
  28737. }
  28738. return -1
  28739. },
  28740. getItem: function (index) {
  28741. return this.items[index]
  28742. },
  28743. selectByIndex: function (index) {
  28744. if (
  28745. index < this.items.length &&
  28746. this.fireEvent('select', index) !== false
  28747. ) {
  28748. this.selectedIndex = index
  28749. this.value = this.items[index].value
  28750. this.setLabel(this.items[index].label)
  28751. }
  28752. },
  28753. }
  28754. utils.inherits(Combox, SplitButton)
  28755. })()
  28756. // ui/dialog.js
  28757. ///import core
  28758. ///import uicore
  28759. ///import ui/mask.js
  28760. ///import ui/button.js
  28761. ;(function () {
  28762. var utils = baidu.editor.utils,
  28763. domUtils = baidu.editor.dom.domUtils,
  28764. uiUtils = baidu.editor.ui.uiUtils,
  28765. Mask = baidu.editor.ui.Mask,
  28766. UIBase = baidu.editor.ui.UIBase,
  28767. Button = baidu.editor.ui.Button,
  28768. Dialog = (baidu.editor.ui.Dialog = function (options) {
  28769. if (options.name) {
  28770. var name = options.name
  28771. var cssRules = options.cssRules
  28772. if (!options.className) {
  28773. options.className = 'edui-for-' + name
  28774. }
  28775. if (cssRules) {
  28776. options.cssRules =
  28777. '.edui-default .edui-for-' +
  28778. name +
  28779. ' .edui-dialog-content {' +
  28780. cssRules +
  28781. '}'
  28782. }
  28783. }
  28784. this.initOptions(
  28785. utils.extend(
  28786. {
  28787. autoReset: true,
  28788. draggable: true,
  28789. onok: function () {},
  28790. oncancel: function () {},
  28791. onclose: function (t, ok) {
  28792. return ok ? this.onok() : this.oncancel()
  28793. },
  28794. //是否控制dialog中的scroll事件, 默认为不阻止
  28795. holdScroll: false,
  28796. },
  28797. options
  28798. )
  28799. )
  28800. this.initDialog()
  28801. })
  28802. var modalMask
  28803. var dragMask
  28804. var activeDialog
  28805. Dialog.prototype = {
  28806. draggable: false,
  28807. uiName: 'dialog',
  28808. initDialog: function () {
  28809. var me = this,
  28810. theme = this.editor.options.theme
  28811. if (this.cssRules) {
  28812. utils.cssRule('edui-customize-' + this.name + '-style', this.cssRules)
  28813. }
  28814. this.initUIBase()
  28815. this.modalMask =
  28816. modalMask ||
  28817. (modalMask = new Mask({
  28818. className: 'edui-dialog-modalmask',
  28819. theme: theme,
  28820. onclick: function () {
  28821. activeDialog && activeDialog.close(false)
  28822. },
  28823. }))
  28824. this.dragMask =
  28825. dragMask ||
  28826. (dragMask = new Mask({
  28827. className: 'edui-dialog-dragmask',
  28828. theme: theme,
  28829. }))
  28830. this.closeButton = new Button({
  28831. className: 'edui-dialog-closebutton',
  28832. title: me.closeDialog,
  28833. theme: theme,
  28834. onclick: function () {
  28835. me.close(false)
  28836. },
  28837. })
  28838. this.fullscreen && this.initResizeEvent()
  28839. if (this.buttons) {
  28840. for (var i = 0; i < this.buttons.length; i++) {
  28841. if (!(this.buttons[i] instanceof Button)) {
  28842. this.buttons[i] = new Button(
  28843. utils.extend(
  28844. this.buttons[i],
  28845. {
  28846. editor: this.editor,
  28847. },
  28848. true
  28849. )
  28850. )
  28851. }
  28852. }
  28853. }
  28854. },
  28855. initResizeEvent: function () {
  28856. var me = this
  28857. domUtils.on(window, 'resize', function () {
  28858. if (me._hidden || me._hidden === undefined) {
  28859. return
  28860. }
  28861. if (me.__resizeTimer) {
  28862. window.clearTimeout(me.__resizeTimer)
  28863. }
  28864. me.__resizeTimer = window.setTimeout(function () {
  28865. me.__resizeTimer = null
  28866. var dialogWrapNode = me.getDom(),
  28867. contentNode = me.getDom('content'),
  28868. wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28869. contentRect = UE.ui.uiUtils.getClientRect(contentNode),
  28870. vpRect = uiUtils.getViewportRect()
  28871. contentNode.style.width =
  28872. vpRect.width - wrapRect.width + contentRect.width + 'px'
  28873. contentNode.style.height =
  28874. vpRect.height - wrapRect.height + contentRect.height + 'px'
  28875. dialogWrapNode.style.width = vpRect.width + 'px'
  28876. dialogWrapNode.style.height = vpRect.height + 'px'
  28877. me.fireEvent('resize')
  28878. }, 100)
  28879. })
  28880. },
  28881. fitSize: function () {
  28882. var popBodyEl = this.getDom('body')
  28883. // if (!(baidu.editor.browser.ie && baidu.editor.browser.version == 7)) {
  28884. // uiUtils.removeStyle(popBodyEl, 'width');
  28885. // uiUtils.removeStyle(popBodyEl, 'height');
  28886. // }
  28887. var size = this.mesureSize()
  28888. popBodyEl.style.width = size.width + 'px'
  28889. popBodyEl.style.height = size.height + 'px'
  28890. return size
  28891. },
  28892. safeSetOffset: function (offset) {
  28893. var me = this
  28894. var el = me.getDom()
  28895. var vpRect = uiUtils.getViewportRect()
  28896. var rect = uiUtils.getClientRect(el)
  28897. var left = offset.left
  28898. if (left + rect.width > vpRect.right) {
  28899. left = vpRect.right - rect.width
  28900. }
  28901. var top = offset.top
  28902. if (top + rect.height > vpRect.bottom) {
  28903. top = vpRect.bottom - rect.height
  28904. }
  28905. el.style.left = Math.max(left, 0) + 'px'
  28906. el.style.top = Math.max(top, 0) + 'px'
  28907. },
  28908. showAtCenter: function () {
  28909. var vpRect = uiUtils.getViewportRect()
  28910. if (!this.fullscreen) {
  28911. this.getDom().style.display = ''
  28912. var popSize = this.fitSize()
  28913. var titleHeight = this.getDom('titlebar').offsetHeight | 0
  28914. var left = vpRect.width / 2 - popSize.width / 2
  28915. var top =
  28916. vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight
  28917. var popEl = this.getDom()
  28918. this.safeSetOffset({
  28919. left: Math.max(left | 0, 0),
  28920. top: Math.max(top | 0, 0),
  28921. })
  28922. if (!domUtils.hasClass(popEl, 'edui-state-centered')) {
  28923. popEl.className += ' edui-state-centered'
  28924. }
  28925. } else {
  28926. var dialogWrapNode = this.getDom(),
  28927. contentNode = this.getDom('content')
  28928. dialogWrapNode.style.display = 'block'
  28929. var wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28930. contentRect = UE.ui.uiUtils.getClientRect(contentNode)
  28931. dialogWrapNode.style.left = '-100000px'
  28932. contentNode.style.width =
  28933. vpRect.width - wrapRect.width + contentRect.width + 'px'
  28934. contentNode.style.height =
  28935. vpRect.height - wrapRect.height + contentRect.height + 'px'
  28936. dialogWrapNode.style.width = vpRect.width + 'px'
  28937. dialogWrapNode.style.height = vpRect.height + 'px'
  28938. dialogWrapNode.style.left = 0
  28939. //保存环境的overflow值
  28940. this._originalContext = {
  28941. html: {
  28942. overflowX: document.documentElement.style.overflowX,
  28943. overflowY: document.documentElement.style.overflowY,
  28944. },
  28945. body: {
  28946. overflowX: document.body.style.overflowX,
  28947. overflowY: document.body.style.overflowY,
  28948. },
  28949. }
  28950. document.documentElement.style.overflowX = 'hidden'
  28951. document.documentElement.style.overflowY = 'hidden'
  28952. document.body.style.overflowX = 'hidden'
  28953. document.body.style.overflowY = 'hidden'
  28954. }
  28955. this._show()
  28956. },
  28957. getContentHtml: function () {
  28958. var contentHtml = ''
  28959. if (typeof this.content == 'string') {
  28960. contentHtml = this.content
  28961. } else if (this.iframeUrl) {
  28962. contentHtml =
  28963. '<span id="' +
  28964. this.id +
  28965. '_contmask" class="dialogcontmask"></span><iframe id="' +
  28966. this.id +
  28967. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="' +
  28968. this.iframeUrl +
  28969. '"></iframe>'
  28970. }
  28971. return contentHtml
  28972. },
  28973. getHtmlTpl: function () {
  28974. var footHtml = ''
  28975. if (this.buttons) {
  28976. var buff = []
  28977. for (var i = 0; i < this.buttons.length; i++) {
  28978. buff[i] = this.buttons[i].renderHtml()
  28979. }
  28980. footHtml =
  28981. '<div class="%%-foot">' +
  28982. '<div id="##_buttons" class="%%-buttons">' +
  28983. buff.join('') +
  28984. '</div>' +
  28985. '</div>'
  28986. }
  28987. return (
  28988. '<div id="##" class="%%"><div ' +
  28989. (!this.fullscreen
  28990. ? 'class="%%"'
  28991. : 'class="%%-wrap edui-dialog-fullscreen-flag"') +
  28992. '><div id="##_body" class="%%-body">' +
  28993. '<div class="%%-shadow"></div>' +
  28994. '<div id="##_titlebar" class="%%-titlebar">' +
  28995. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  28996. '<span class="%%-caption">' +
  28997. (this.title || '') +
  28998. '</span>' +
  28999. '</div>' +
  29000. this.closeButton.renderHtml() +
  29001. '</div>' +
  29002. '<div id="##_content" class="%%-content">' +
  29003. (this.autoReset ? '' : this.getContentHtml()) +
  29004. '</div>' +
  29005. footHtml +
  29006. '</div></div></div>'
  29007. )
  29008. },
  29009. postRender: function () {
  29010. // todo: 保持居中/记住上次关闭位置选项
  29011. if (!this.modalMask.getDom()) {
  29012. this.modalMask.render()
  29013. this.modalMask.hide()
  29014. }
  29015. if (!this.dragMask.getDom()) {
  29016. this.dragMask.render()
  29017. this.dragMask.hide()
  29018. }
  29019. var me = this
  29020. this.addListener('show', function () {
  29021. me.modalMask.show(this.getDom().style.zIndex - 2)
  29022. })
  29023. this.addListener('hide', function () {
  29024. me.modalMask.hide()
  29025. })
  29026. if (this.buttons) {
  29027. for (var i = 0; i < this.buttons.length; i++) {
  29028. this.buttons[i].postRender()
  29029. }
  29030. }
  29031. domUtils.on(window, 'resize', function () {
  29032. setTimeout(function () {
  29033. if (!me.isHidden()) {
  29034. me.safeSetOffset(uiUtils.getClientRect(me.getDom()))
  29035. }
  29036. })
  29037. })
  29038. //hold住scroll事件,防止dialog的滚动影响页面
  29039. // if( this.holdScroll ) {
  29040. //
  29041. // if( !me.iframeUrl ) {
  29042. // domUtils.on( document.getElementById( me.id + "_iframe"), !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  29043. // domUtils.preventDefault(e);
  29044. // } );
  29045. // } else {
  29046. // me.addListener('dialogafterreset', function(){
  29047. // window.setTimeout(function(){
  29048. // var iframeWindow = document.getElementById( me.id + "_iframe").contentWindow;
  29049. //
  29050. // if( browser.ie ) {
  29051. //
  29052. // var timer = window.setInterval(function(){
  29053. //
  29054. // if( iframeWindow.document && iframeWindow.document.body ) {
  29055. // window.clearInterval( timer );
  29056. // timer = null;
  29057. // domUtils.on( iframeWindow.document.body, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  29058. // domUtils.preventDefault(e);
  29059. // } );
  29060. // }
  29061. //
  29062. // }, 100);
  29063. //
  29064. // } else {
  29065. // domUtils.on( iframeWindow, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  29066. // domUtils.preventDefault(e);
  29067. // } );
  29068. // }
  29069. //
  29070. // }, 1);
  29071. // });
  29072. // }
  29073. //
  29074. // }
  29075. this._hide()
  29076. },
  29077. mesureSize: function () {
  29078. var body = this.getDom('body')
  29079. var width = uiUtils.getClientRect(this.getDom('content')).width
  29080. var dialogBodyStyle = body.style
  29081. dialogBodyStyle.width = width
  29082. return uiUtils.getClientRect(body)
  29083. },
  29084. _onTitlebarMouseDown: function (evt, el) {
  29085. if (this.draggable) {
  29086. var rect
  29087. var vpRect = uiUtils.getViewportRect()
  29088. var me = this
  29089. uiUtils.startDrag(evt, {
  29090. ondragstart: function () {
  29091. rect = uiUtils.getClientRect(me.getDom())
  29092. me.getDom('contmask').style.visibility = 'visible'
  29093. me.dragMask.show(me.getDom().style.zIndex - 1)
  29094. },
  29095. ondragmove: function (x, y) {
  29096. var left = rect.left + x
  29097. var top = rect.top + y
  29098. me.safeSetOffset({
  29099. left: left,
  29100. top: top,
  29101. })
  29102. },
  29103. ondragstop: function () {
  29104. me.getDom('contmask').style.visibility = 'hidden'
  29105. domUtils.removeClasses(me.getDom(), ['edui-state-centered'])
  29106. me.dragMask.hide()
  29107. },
  29108. })
  29109. }
  29110. },
  29111. reset: function () {
  29112. this.getDom('content').innerHTML = this.getContentHtml()
  29113. this.fireEvent('dialogafterreset')
  29114. },
  29115. _show: function () {
  29116. if (this._hidden) {
  29117. this.getDom().style.display = ''
  29118. //要高过编辑器的zindxe
  29119. this.editor.container.style.zIndex &&
  29120. (this.getDom().style.zIndex =
  29121. this.editor.container.style.zIndex * 1 + 10)
  29122. this._hidden = false
  29123. this.fireEvent('show')
  29124. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  29125. this.getDom().style.zIndex - 4
  29126. }
  29127. },
  29128. isHidden: function () {
  29129. return this._hidden
  29130. },
  29131. _hide: function () {
  29132. if (!this._hidden) {
  29133. var wrapNode = this.getDom()
  29134. wrapNode.style.display = 'none'
  29135. wrapNode.style.zIndex = ''
  29136. wrapNode.style.width = ''
  29137. wrapNode.style.height = ''
  29138. this._hidden = true
  29139. this.fireEvent('hide')
  29140. }
  29141. },
  29142. open: function () {
  29143. if (this.autoReset) {
  29144. //有可能还没有渲染
  29145. try {
  29146. this.reset()
  29147. } catch (e) {
  29148. this.render()
  29149. this.open()
  29150. }
  29151. }
  29152. this.showAtCenter()
  29153. if (this.iframeUrl) {
  29154. try {
  29155. this.getDom('iframe').focus()
  29156. } catch (ex) {}
  29157. }
  29158. activeDialog = this
  29159. },
  29160. _onCloseButtonClick: function (evt, el) {
  29161. this.close(false)
  29162. },
  29163. close: function (ok) {
  29164. if (this.fireEvent('close', ok) !== false) {
  29165. //还原环境
  29166. if (this.fullscreen) {
  29167. document.documentElement.style.overflowX =
  29168. this._originalContext.html.overflowX
  29169. document.documentElement.style.overflowY =
  29170. this._originalContext.html.overflowY
  29171. document.body.style.overflowX = this._originalContext.body.overflowX
  29172. document.body.style.overflowY = this._originalContext.body.overflowY
  29173. delete this._originalContext
  29174. }
  29175. this._hide()
  29176. //销毁content
  29177. var content = this.getDom('content')
  29178. var iframe = this.getDom('iframe')
  29179. if (content && iframe) {
  29180. var doc = iframe.contentDocument || iframe.contentWindow.document
  29181. doc && (doc.body.innerHTML = '')
  29182. domUtils.remove(content)
  29183. }
  29184. }
  29185. },
  29186. }
  29187. utils.inherits(Dialog, UIBase)
  29188. })()
  29189. // ui/menubutton.js
  29190. ///import core
  29191. ///import uicore
  29192. ///import ui/menu.js
  29193. ///import ui/splitbutton.js
  29194. ;(function () {
  29195. var utils = baidu.editor.utils,
  29196. Menu = baidu.editor.ui.Menu,
  29197. SplitButton = baidu.editor.ui.SplitButton,
  29198. MenuButton = (baidu.editor.ui.MenuButton = function (options) {
  29199. this.initOptions(options)
  29200. this.initMenuButton()
  29201. })
  29202. MenuButton.prototype = {
  29203. initMenuButton: function () {
  29204. var me = this
  29205. this.uiName = 'menubutton'
  29206. this.popup = new Menu({
  29207. items: me.items,
  29208. className: me.className,
  29209. editor: me.editor,
  29210. })
  29211. this.popup.addListener('show', function () {
  29212. var list = this
  29213. for (var i = 0; i < list.items.length; i++) {
  29214. list.items[i].removeState('checked')
  29215. if (list.items[i].value == me._value) {
  29216. list.items[i].addState('checked')
  29217. this.value = me._value
  29218. }
  29219. }
  29220. })
  29221. this.initSplitButton()
  29222. },
  29223. setValue: function (value) {
  29224. this._value = value
  29225. },
  29226. }
  29227. utils.inherits(MenuButton, SplitButton)
  29228. })()
  29229. // ui/multiMenu.js
  29230. ///import core
  29231. ///import uicore
  29232. ///commands 表情
  29233. ;(function () {
  29234. var utils = baidu.editor.utils,
  29235. Popup = baidu.editor.ui.Popup,
  29236. SplitButton = baidu.editor.ui.SplitButton,
  29237. MultiMenuPop = (baidu.editor.ui.MultiMenuPop = function (options) {
  29238. this.initOptions(options)
  29239. this.initMultiMenu()
  29240. })
  29241. MultiMenuPop.prototype = {
  29242. initMultiMenu: function () {
  29243. var me = this
  29244. this.popup = new Popup({
  29245. content: '',
  29246. editor: me.editor,
  29247. iframe_rendered: false,
  29248. onshow: function () {
  29249. if (!this.iframe_rendered) {
  29250. this.iframe_rendered = true
  29251. this.getDom('content').innerHTML =
  29252. '<iframe id="' +
  29253. me.id +
  29254. '_iframe" src="' +
  29255. me.iframeUrl +
  29256. '" frameborder="0"></iframe>'
  29257. me.editor.container.style.zIndex &&
  29258. (this.getDom().style.zIndex =
  29259. me.editor.container.style.zIndex * 1 + 1)
  29260. }
  29261. },
  29262. // canSideUp:false,
  29263. // canSideLeft:false
  29264. })
  29265. this.onbuttonclick = function () {
  29266. this.showPopup()
  29267. }
  29268. this.initSplitButton()
  29269. },
  29270. }
  29271. utils.inherits(MultiMenuPop, SplitButton)
  29272. })()
  29273. // ui/shortcutmenu.js
  29274. ;(function () {
  29275. var UI = baidu.editor.ui,
  29276. UIBase = UI.UIBase,
  29277. uiUtils = UI.uiUtils,
  29278. utils = baidu.editor.utils,
  29279. domUtils = baidu.editor.dom.domUtils
  29280. var allMenus = [], //存储所有快捷菜单
  29281. timeID,
  29282. isSubMenuShow = false //是否有子pop显示
  29283. var ShortCutMenu = (UI.ShortCutMenu = function (options) {
  29284. this.initOptions(options)
  29285. this.initShortCutMenu()
  29286. })
  29287. ShortCutMenu.postHide = hideAllMenu
  29288. ShortCutMenu.prototype = {
  29289. isHidden: true,
  29290. SPACE: 5,
  29291. initShortCutMenu: function () {
  29292. this.items = this.items || []
  29293. this.initUIBase()
  29294. this.initItems()
  29295. this.initEvent()
  29296. allMenus.push(this)
  29297. },
  29298. initEvent: function () {
  29299. var me = this,
  29300. doc = me.editor.document
  29301. domUtils.on(doc, 'mousemove', function (e) {
  29302. if (me.isHidden === false) {
  29303. //有pop显示就不隐藏快捷菜单
  29304. if (me.getSubMenuMark() || me.eventType == 'contextmenu') return
  29305. var flag = true,
  29306. el = me.getDom(),
  29307. wt = el.offsetWidth,
  29308. ht = el.offsetHeight,
  29309. distanceX = wt / 2 + me.SPACE, //距离中心X标准
  29310. distanceY = ht / 2, //距离中心Y标准
  29311. x = Math.abs(e.screenX - me.left), //离中心距离横坐标
  29312. y = Math.abs(e.screenY - me.top) //离中心距离纵坐标
  29313. clearTimeout(timeID)
  29314. timeID = setTimeout(function () {
  29315. if (y > 0 && y < distanceY) {
  29316. me.setOpacity(el, '1')
  29317. } else if (y > distanceY && y < distanceY + 70) {
  29318. me.setOpacity(el, '0.5')
  29319. flag = false
  29320. } else if (y > distanceY + 70 && y < distanceY + 140) {
  29321. me.hide()
  29322. }
  29323. if (flag && x > 0 && x < distanceX) {
  29324. me.setOpacity(el, '1')
  29325. } else if (x > distanceX && x < distanceX + 70) {
  29326. me.setOpacity(el, '0.5')
  29327. } else if (x > distanceX + 70 && x < distanceX + 140) {
  29328. me.hide()
  29329. }
  29330. })
  29331. }
  29332. })
  29333. //ie\ff下 mouseout不准
  29334. if (browser.chrome) {
  29335. domUtils.on(doc, 'mouseout', function (e) {
  29336. var relatedTgt = e.relatedTarget || e.toElement
  29337. if (relatedTgt == null || relatedTgt.tagName == 'HTML') {
  29338. me.hide()
  29339. }
  29340. })
  29341. }
  29342. me.editor.addListener('afterhidepop', function () {
  29343. if (!me.isHidden) {
  29344. isSubMenuShow = true
  29345. }
  29346. })
  29347. },
  29348. initItems: function () {
  29349. if (utils.isArray(this.items)) {
  29350. for (var i = 0, len = this.items.length; i < len; i++) {
  29351. var item = this.items[i].toLowerCase()
  29352. if (UI[item]) {
  29353. this.items[i] = new UI[item](this.editor)
  29354. this.items[i].className += ' edui-shortcutsubmenu '
  29355. }
  29356. }
  29357. }
  29358. },
  29359. setOpacity: function (el, value) {
  29360. if (browser.ie && browser.version < 9) {
  29361. el.style.filter = 'alpha(opacity = ' + parseFloat(value) * 100 + ');'
  29362. } else {
  29363. el.style.opacity = value
  29364. }
  29365. },
  29366. getSubMenuMark: function () {
  29367. isSubMenuShow = false
  29368. var layerEle = uiUtils.getFixedLayer()
  29369. var list = domUtils.getElementsByTagName(
  29370. layerEle,
  29371. 'div',
  29372. function (node) {
  29373. return domUtils.hasClass(node, 'edui-shortcutsubmenu edui-popup')
  29374. }
  29375. )
  29376. for (var i = 0, node; (node = list[i++]); ) {
  29377. if (node.style.display != 'none') {
  29378. isSubMenuShow = true
  29379. }
  29380. }
  29381. return isSubMenuShow
  29382. },
  29383. show: function (e, hasContextmenu) {
  29384. var me = this,
  29385. offset = {},
  29386. el = this.getDom(),
  29387. fixedlayer = uiUtils.getFixedLayer()
  29388. function setPos(offset) {
  29389. if (offset.left < 0) {
  29390. offset.left = 0
  29391. }
  29392. if (offset.top < 0) {
  29393. offset.top = 0
  29394. }
  29395. el.style.cssText =
  29396. 'position:absolute;left:' +
  29397. offset.left +
  29398. 'px;top:' +
  29399. offset.top +
  29400. 'px;'
  29401. }
  29402. function setPosByCxtMenu(menu) {
  29403. if (!menu.tagName) {
  29404. menu = menu.getDom()
  29405. }
  29406. offset.left = parseInt(menu.style.left)
  29407. offset.top = parseInt(menu.style.top)
  29408. offset.top -= el.offsetHeight + 15
  29409. setPos(offset)
  29410. }
  29411. me.eventType = e.type
  29412. el.style.cssText = 'display:block;left:-9999px'
  29413. if (e.type == 'contextmenu' && hasContextmenu) {
  29414. var menu = domUtils.getElementsByTagName(
  29415. fixedlayer,
  29416. 'div',
  29417. 'edui-contextmenu'
  29418. )[0]
  29419. if (menu) {
  29420. setPosByCxtMenu(menu)
  29421. } else {
  29422. me.editor.addListener(
  29423. 'aftershowcontextmenu',
  29424. function (type, menu) {
  29425. setPosByCxtMenu(menu)
  29426. }
  29427. )
  29428. }
  29429. } else {
  29430. offset = uiUtils.getViewportOffsetByEvent(e)
  29431. offset.top -= el.offsetHeight + me.SPACE
  29432. offset.left += me.SPACE + 20
  29433. setPos(offset)
  29434. me.setOpacity(el, 0.2)
  29435. }
  29436. me.isHidden = false
  29437. me.left = e.screenX + el.offsetWidth / 2 - me.SPACE
  29438. me.top = e.screenY - el.offsetHeight / 2 - me.SPACE
  29439. if (me.editor) {
  29440. el.style.zIndex = me.editor.container.style.zIndex * 1 + 10
  29441. fixedlayer.style.zIndex = el.style.zIndex - 1
  29442. }
  29443. },
  29444. hide: function () {
  29445. if (this.getDom()) {
  29446. this.getDom().style.display = 'none'
  29447. }
  29448. this.isHidden = true
  29449. },
  29450. postRender: function () {
  29451. if (utils.isArray(this.items)) {
  29452. for (var i = 0, item; (item = this.items[i++]); ) {
  29453. item.postRender()
  29454. }
  29455. }
  29456. },
  29457. getHtmlTpl: function () {
  29458. var buff
  29459. if (utils.isArray(this.items)) {
  29460. buff = []
  29461. for (var i = 0; i < this.items.length; i++) {
  29462. buff[i] = this.items[i].renderHtml()
  29463. }
  29464. buff = buff.join('')
  29465. } else {
  29466. buff = this.items
  29467. }
  29468. return (
  29469. '<div id="##" class="%% edui-toolbar" data-src="shortcutmenu" onmousedown="return false;" onselectstart="return false;" >' +
  29470. buff +
  29471. '</div>'
  29472. )
  29473. },
  29474. }
  29475. utils.inherits(ShortCutMenu, UIBase)
  29476. function hideAllMenu(e) {
  29477. var tgt = e.target || e.srcElement,
  29478. cur = domUtils.findParent(
  29479. tgt,
  29480. function (node) {
  29481. return (
  29482. domUtils.hasClass(node, 'edui-shortcutmenu') ||
  29483. domUtils.hasClass(node, 'edui-popup')
  29484. )
  29485. },
  29486. true
  29487. )
  29488. if (!cur) {
  29489. for (var i = 0, menu; (menu = allMenus[i++]); ) {
  29490. menu.hide()
  29491. }
  29492. }
  29493. }
  29494. domUtils.on(document, 'mousedown', function (e) {
  29495. hideAllMenu(e)
  29496. })
  29497. domUtils.on(window, 'scroll', function (e) {
  29498. hideAllMenu(e)
  29499. })
  29500. })()
  29501. // ui/breakline.js
  29502. ;(function () {
  29503. var utils = baidu.editor.utils,
  29504. UIBase = baidu.editor.ui.UIBase,
  29505. Breakline = (baidu.editor.ui.Breakline = function (options) {
  29506. this.initOptions(options)
  29507. this.initSeparator()
  29508. })
  29509. Breakline.prototype = {
  29510. uiName: 'Breakline',
  29511. initSeparator: function () {
  29512. this.initUIBase()
  29513. },
  29514. getHtmlTpl: function () {
  29515. return '<br/>'
  29516. },
  29517. }
  29518. utils.inherits(Breakline, UIBase)
  29519. })()
  29520. // ui/message.js
  29521. ///import core
  29522. ///import uicore
  29523. ;(function () {
  29524. var utils = baidu.editor.utils,
  29525. domUtils = baidu.editor.dom.domUtils,
  29526. UIBase = baidu.editor.ui.UIBase,
  29527. Message = (baidu.editor.ui.Message = function (options) {
  29528. this.initOptions(options)
  29529. this.initMessage()
  29530. })
  29531. Message.prototype = {
  29532. initMessage: function () {
  29533. this.initUIBase()
  29534. },
  29535. getHtmlTpl: function () {
  29536. return (
  29537. '<div id="##" class="edui-message %%">' +
  29538. ' <div id="##_closer" class="edui-message-closer">×</div>' +
  29539. ' <div id="##_body" class="edui-message-body edui-message-type-info">' +
  29540. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  29541. ' <div class="edui-shadow"></div>' +
  29542. ' <div id="##_content" class="edui-message-content">' +
  29543. ' </div>' +
  29544. ' </div>' +
  29545. '</div>'
  29546. )
  29547. },
  29548. reset: function (opt) {
  29549. var me = this
  29550. if (!opt.keepshow) {
  29551. clearTimeout(this.timer)
  29552. me.timer = setTimeout(function () {
  29553. me.hide()
  29554. }, opt.timeout || 4000)
  29555. }
  29556. opt.content !== undefined && me.setContent(opt.content)
  29557. opt.type !== undefined && me.setType(opt.type)
  29558. me.show()
  29559. },
  29560. postRender: function () {
  29561. var me = this,
  29562. closer = this.getDom('closer')
  29563. closer &&
  29564. domUtils.on(closer, 'click', function () {
  29565. me.hide()
  29566. })
  29567. },
  29568. setContent: function (content) {
  29569. this.getDom('content').innerHTML = content
  29570. },
  29571. setType: function (type) {
  29572. type = type || 'info'
  29573. var body = this.getDom('body')
  29574. body.className = body.className.replace(
  29575. /edui-message-type-[\w-]+/,
  29576. 'edui-message-type-' + type
  29577. )
  29578. },
  29579. getContent: function () {
  29580. return this.getDom('content').innerHTML
  29581. },
  29582. getType: function () {
  29583. var arr = this.getDom('body').match(/edui-message-type-([\w-]+)/)
  29584. return arr ? arr[1] : ''
  29585. },
  29586. show: function () {
  29587. this.getDom().style.display = 'block'
  29588. },
  29589. hide: function () {
  29590. var dom = this.getDom()
  29591. if (dom) {
  29592. dom.style.display = 'none'
  29593. dom.parentNode && dom.parentNode.removeChild(dom)
  29594. }
  29595. },
  29596. }
  29597. utils.inherits(Message, UIBase)
  29598. })()
  29599. // adapter/editorui.js
  29600. //ui跟编辑器的适配層
  29601. //那个按钮弹出是dialog,是下拉筐等都是在这个js中配置
  29602. //自己写的ui也要在这里配置,放到baidu.editor.ui下边,当编辑器实例化的时候会根据ueditor.config中的toolbars找到相应的进行实例化
  29603. ;(function () {
  29604. var utils = baidu.editor.utils
  29605. var editorui = baidu.editor.ui
  29606. var _Dialog = editorui.Dialog
  29607. editorui.buttons = {}
  29608. editorui.Dialog = function (options) {
  29609. var dialog = new _Dialog(options)
  29610. dialog.addListener('hide', function () {
  29611. if (dialog.editor) {
  29612. var editor = dialog.editor
  29613. try {
  29614. if (browser.gecko) {
  29615. var y = editor.window.scrollY,
  29616. x = editor.window.scrollX
  29617. editor.body.focus()
  29618. editor.window.scrollTo(x, y)
  29619. } else {
  29620. editor.focus()
  29621. }
  29622. } catch (ex) {}
  29623. }
  29624. })
  29625. return dialog
  29626. }
  29627. var iframeUrlMap = {
  29628. anchor: '~/dialogs/anchor/anchor.html',
  29629. insertimage: '~/dialogs/image/image.html',
  29630. link: '~/dialogs/link/link.html',
  29631. spechars: '~/dialogs/spechars/spechars.html',
  29632. searchreplace: '~/dialogs/searchreplace/searchreplace.html',
  29633. map: '~/dialogs/map/map.html',
  29634. gmap: '~/dialogs/gmap/gmap.html',
  29635. insertvideo: '~/dialogs/video/video.html',
  29636. help: '~/dialogs/help/help.html',
  29637. preview: '~/dialogs/preview/preview.html',
  29638. emotion: '~/dialogs/emotion/emotion.html',
  29639. wordimage: '~/dialogs/wordimage/wordimage.html',
  29640. attachment: '~/dialogs/attachment/attachment.html',
  29641. insertframe: '~/dialogs/insertframe/insertframe.html',
  29642. edittip: '~/dialogs/table/edittip.html',
  29643. edittable: '~/dialogs/table/edittable.html',
  29644. edittd: '~/dialogs/table/edittd.html',
  29645. webapp: '~/dialogs/webapp/webapp.html',
  29646. snapscreen: '~/dialogs/snapscreen/snapscreen.html',
  29647. scrawl: '~/dialogs/scrawl/scrawl.html',
  29648. music: '~/dialogs/music/music.html',
  29649. template: '~/dialogs/template/template.html',
  29650. background: '~/dialogs/background/background.html',
  29651. charts: '~/dialogs/charts/charts.html',
  29652. }
  29653. //为工具栏添加按钮,以下都是统一的按钮触发命令,所以写在一起
  29654. var btnCmds = [
  29655. 'undo',
  29656. 'redo',
  29657. 'formatmatch',
  29658. 'bold',
  29659. 'italic',
  29660. 'underline',
  29661. 'fontborder',
  29662. 'touppercase',
  29663. 'tolowercase',
  29664. 'strikethrough',
  29665. 'subscript',
  29666. 'superscript',
  29667. 'source',
  29668. 'indent',
  29669. 'outdent',
  29670. 'blockquote',
  29671. 'pasteplain',
  29672. 'pagebreak',
  29673. 'selectall',
  29674. 'print',
  29675. 'horizontal',
  29676. 'removeformat',
  29677. 'time',
  29678. 'date',
  29679. 'unlink',
  29680. 'insertparagraphbeforetable',
  29681. 'insertrow',
  29682. 'insertcol',
  29683. 'mergeright',
  29684. 'mergedown',
  29685. 'deleterow',
  29686. 'deletecol',
  29687. 'splittorows',
  29688. 'splittocols',
  29689. 'splittocells',
  29690. 'mergecells',
  29691. 'deletetable',
  29692. 'drafts',
  29693. 'wordupload',
  29694. ]
  29695. for (var i = 0, ci; (ci = btnCmds[i++]); ) {
  29696. ci = ci.toLowerCase()
  29697. editorui[ci] = (function (cmd) {
  29698. return function (editor) {
  29699. var ui = new editorui.Button({
  29700. className: 'edui-for-' + cmd,
  29701. title:
  29702. editor.options.labelMap[cmd] ||
  29703. editor.getLang('labelMap.' + cmd) ||
  29704. '',
  29705. onclick: function () {
  29706. editor.execCommand(cmd)
  29707. },
  29708. theme: editor.options.theme,
  29709. showText: false,
  29710. })
  29711. editorui.buttons[cmd] = ui
  29712. editor.addListener(
  29713. 'selectionchange',
  29714. function (type, causeByUi, uiReady) {
  29715. var state = editor.queryCommandState(cmd)
  29716. if (state == -1) {
  29717. ui.setDisabled(true)
  29718. ui.setChecked(false)
  29719. } else {
  29720. if (!uiReady) {
  29721. ui.setDisabled(false)
  29722. ui.setChecked(state)
  29723. }
  29724. }
  29725. }
  29726. )
  29727. return ui
  29728. }
  29729. })(ci)
  29730. }
  29731. //清除文档
  29732. editorui.cleardoc = function (editor) {
  29733. var ui = new editorui.Button({
  29734. className: 'edui-for-cleardoc',
  29735. title:
  29736. editor.options.labelMap.cleardoc ||
  29737. editor.getLang('labelMap.cleardoc') ||
  29738. '',
  29739. theme: editor.options.theme,
  29740. onclick: function () {
  29741. if (confirm(editor.getLang('confirmClear'))) {
  29742. editor.execCommand('cleardoc')
  29743. }
  29744. },
  29745. })
  29746. editorui.buttons['cleardoc'] = ui
  29747. editor.addListener('selectionchange', function () {
  29748. ui.setDisabled(editor.queryCommandState('cleardoc') == -1)
  29749. })
  29750. return ui
  29751. }
  29752. //排版,图片排版,文字方向
  29753. var typeset = {
  29754. justify: ['left', 'right', 'center', 'justify'],
  29755. imagefloat: ['none', 'left', 'center', 'right'],
  29756. directionality: ['ltr', 'rtl'],
  29757. }
  29758. for (var p in typeset) {
  29759. ;(function (cmd, val) {
  29760. for (var i = 0, ci; (ci = val[i++]); ) {
  29761. ;(function (cmd2) {
  29762. editorui[cmd.replace('float', '') + cmd2] = function (editor) {
  29763. var ui = new editorui.Button({
  29764. className: 'edui-for-' + cmd.replace('float', '') + cmd2,
  29765. title:
  29766. editor.options.labelMap[cmd.replace('float', '') + cmd2] ||
  29767. editor.getLang(
  29768. 'labelMap.' + cmd.replace('float', '') + cmd2
  29769. ) ||
  29770. '',
  29771. theme: editor.options.theme,
  29772. onclick: function () {
  29773. editor.execCommand(cmd, cmd2)
  29774. },
  29775. })
  29776. editorui.buttons[cmd] = ui
  29777. editor.addListener(
  29778. 'selectionchange',
  29779. function (type, causeByUi, uiReady) {
  29780. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  29781. ui.setChecked(
  29782. editor.queryCommandValue(cmd) == cmd2 && !uiReady
  29783. )
  29784. }
  29785. )
  29786. return ui
  29787. }
  29788. })(ci)
  29789. }
  29790. })(p, typeset[p])
  29791. }
  29792. //字体颜色和背景颜色
  29793. for (var i = 0, ci; (ci = ['backcolor', 'forecolor'][i++]); ) {
  29794. editorui[ci] = (function (cmd) {
  29795. return function (editor) {
  29796. var ui = new editorui.ColorButton({
  29797. className: 'edui-for-' + cmd,
  29798. color: 'default',
  29799. title:
  29800. editor.options.labelMap[cmd] ||
  29801. editor.getLang('labelMap.' + cmd) ||
  29802. '',
  29803. editor: editor,
  29804. onpickcolor: function (t, color) {
  29805. editor.execCommand(cmd, color)
  29806. },
  29807. onpicknocolor: function () {
  29808. editor.execCommand(cmd, 'default')
  29809. this.setColor('transparent')
  29810. this.color = 'default'
  29811. },
  29812. onbuttonclick: function () {
  29813. editor.execCommand(cmd, this.color)
  29814. },
  29815. })
  29816. editorui.buttons[cmd] = ui
  29817. editor.addListener('selectionchange', function () {
  29818. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  29819. })
  29820. return ui
  29821. }
  29822. })(ci)
  29823. }
  29824. var dialogBtns = {
  29825. noOk: ['searchreplace', 'help', 'spechars', 'webapp', 'preview'],
  29826. ok: [
  29827. 'attachment',
  29828. 'anchor',
  29829. 'link',
  29830. 'insertimage',
  29831. 'map',
  29832. 'gmap',
  29833. 'insertframe',
  29834. 'wordimage',
  29835. 'insertvideo',
  29836. 'insertframe',
  29837. 'edittip',
  29838. 'edittable',
  29839. 'edittd',
  29840. 'scrawl',
  29841. 'template',
  29842. 'music',
  29843. 'background',
  29844. 'charts',
  29845. ],
  29846. }
  29847. for (var p in dialogBtns) {
  29848. ;(function (type, vals) {
  29849. for (var i = 0, ci; (ci = vals[i++]); ) {
  29850. //todo opera下存在问题
  29851. if (browser.opera && ci === 'searchreplace') {
  29852. continue
  29853. }
  29854. ;(function (cmd) {
  29855. editorui[cmd] = function (editor, iframeUrl, title) {
  29856. iframeUrl =
  29857. iframeUrl ||
  29858. (editor.options.iframeUrlMap || {})[cmd] ||
  29859. iframeUrlMap[cmd]
  29860. title =
  29861. editor.options.labelMap[cmd] ||
  29862. editor.getLang('labelMap.' + cmd) ||
  29863. ''
  29864. var dialog
  29865. //没有iframeUrl不创建dialog
  29866. if (iframeUrl) {
  29867. dialog = new editorui.Dialog(
  29868. utils.extend(
  29869. {
  29870. iframeUrl: editor.ui.mapUrl(iframeUrl),
  29871. editor: editor,
  29872. className: 'edui-for-' + cmd,
  29873. title: title,
  29874. holdScroll: cmd === 'insertimage',
  29875. fullscreen: /charts|preview/.test(cmd),
  29876. closeDialog: editor.getLang('closeDialog'),
  29877. },
  29878. type == 'ok'
  29879. ? {
  29880. buttons: [
  29881. {
  29882. className: 'edui-okbutton',
  29883. label: editor.getLang('ok'),
  29884. editor: editor,
  29885. onclick: function () {
  29886. dialog.close(true)
  29887. },
  29888. },
  29889. {
  29890. className: 'edui-cancelbutton',
  29891. label: editor.getLang('cancel'),
  29892. editor: editor,
  29893. onclick: function () {
  29894. dialog.close(false)
  29895. },
  29896. },
  29897. ],
  29898. }
  29899. : {}
  29900. )
  29901. )
  29902. editor.ui._dialogs[cmd + 'Dialog'] = dialog
  29903. }
  29904. var ui = new editorui.Button({
  29905. className: 'edui-for-' + cmd,
  29906. title: title,
  29907. onclick: function () {
  29908. if (dialog) {
  29909. switch (cmd) {
  29910. case 'wordimage':
  29911. var images = editor.execCommand('wordimage')
  29912. if (images && images.length) {
  29913. dialog.render()
  29914. dialog.open()
  29915. }
  29916. break
  29917. case 'scrawl':
  29918. if (editor.queryCommandState('scrawl') != -1) {
  29919. dialog.render()
  29920. dialog.open()
  29921. }
  29922. break
  29923. default:
  29924. dialog.render()
  29925. dialog.open()
  29926. }
  29927. }
  29928. },
  29929. theme: editor.options.theme,
  29930. disabled:
  29931. (cmd == 'scrawl' &&
  29932. editor.queryCommandState('scrawl') == -1) ||
  29933. cmd == 'charts',
  29934. })
  29935. editorui.buttons[cmd] = ui
  29936. editor.addListener('selectionchange', function () {
  29937. //只存在于右键菜单而无工具栏按钮的ui不需要检测状态
  29938. var unNeedCheckState = { edittable: 1 }
  29939. if (cmd in unNeedCheckState) return
  29940. var state = editor.queryCommandState(cmd)
  29941. if (ui.getDom()) {
  29942. ui.setDisabled(state == -1)
  29943. ui.setChecked(state)
  29944. }
  29945. })
  29946. return ui
  29947. }
  29948. })(ci.toLowerCase())
  29949. }
  29950. })(p, dialogBtns[p])
  29951. }
  29952. editorui.snapscreen = function (editor, iframeUrl, title) {
  29953. title =
  29954. editor.options.labelMap['snapscreen'] ||
  29955. editor.getLang('labelMap.snapscreen') ||
  29956. ''
  29957. var ui = new editorui.Button({
  29958. className: 'edui-for-snapscreen',
  29959. title: title,
  29960. onclick: function () {
  29961. editor.execCommand('snapscreen')
  29962. },
  29963. theme: editor.options.theme,
  29964. })
  29965. editorui.buttons['snapscreen'] = ui
  29966. iframeUrl =
  29967. iframeUrl ||
  29968. (editor.options.iframeUrlMap || {})['snapscreen'] ||
  29969. iframeUrlMap['snapscreen']
  29970. if (iframeUrl) {
  29971. var dialog = new editorui.Dialog({
  29972. iframeUrl: editor.ui.mapUrl(iframeUrl),
  29973. editor: editor,
  29974. className: 'edui-for-snapscreen',
  29975. title: title,
  29976. buttons: [
  29977. {
  29978. className: 'edui-okbutton',
  29979. label: editor.getLang('ok'),
  29980. editor: editor,
  29981. onclick: function () {
  29982. dialog.close(true)
  29983. },
  29984. },
  29985. {
  29986. className: 'edui-cancelbutton',
  29987. label: editor.getLang('cancel'),
  29988. editor: editor,
  29989. onclick: function () {
  29990. dialog.close(false)
  29991. },
  29992. },
  29993. ],
  29994. })
  29995. dialog.render()
  29996. editor.ui._dialogs['snapscreenDialog'] = dialog
  29997. }
  29998. editor.addListener('selectionchange', function () {
  29999. ui.setDisabled(editor.queryCommandState('snapscreen') == -1)
  30000. })
  30001. return ui
  30002. }
  30003. editorui.insertcode = function (editor, list, title) {
  30004. list = editor.options['insertcode'] || []
  30005. title =
  30006. editor.options.labelMap['insertcode'] ||
  30007. editor.getLang('labelMap.insertcode') ||
  30008. ''
  30009. // if (!list.length) return;
  30010. var items = []
  30011. utils.each(list, function (key, val) {
  30012. items.push({
  30013. label: key,
  30014. value: val,
  30015. theme: editor.options.theme,
  30016. renderLabelHtml: function () {
  30017. return (
  30018. '<div class="edui-label %%-label" >' +
  30019. (this.label || '') +
  30020. '</div>'
  30021. )
  30022. },
  30023. })
  30024. })
  30025. var ui = new editorui.Combox({
  30026. editor: editor,
  30027. items: items,
  30028. onselect: function (t, index) {
  30029. editor.execCommand('insertcode', this.items[index].value)
  30030. },
  30031. onbuttonclick: function () {
  30032. this.showPopup()
  30033. },
  30034. title: title,
  30035. initValue: title,
  30036. className: 'edui-for-insertcode',
  30037. indexByValue: function (value) {
  30038. if (value) {
  30039. for (var i = 0, ci; (ci = this.items[i]); i++) {
  30040. if (ci.value.indexOf(value) != -1) return i
  30041. }
  30042. }
  30043. return -1
  30044. },
  30045. })
  30046. editorui.buttons['insertcode'] = ui
  30047. editor.addListener(
  30048. 'selectionchange',
  30049. function (type, causeByUi, uiReady) {
  30050. if (!uiReady) {
  30051. var state = editor.queryCommandState('insertcode')
  30052. if (state == -1) {
  30053. ui.setDisabled(true)
  30054. } else {
  30055. ui.setDisabled(false)
  30056. var value = editor.queryCommandValue('insertcode')
  30057. if (!value) {
  30058. ui.setValue(title)
  30059. return
  30060. }
  30061. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  30062. value && (value = value.replace(/['"]/g, '').split(',')[0])
  30063. ui.setValue(value)
  30064. }
  30065. }
  30066. }
  30067. )
  30068. return ui
  30069. }
  30070. editorui.fontfamily = function (editor, list, title) {
  30071. list = editor.options['fontfamily'] || []
  30072. title =
  30073. editor.options.labelMap['fontfamily'] ||
  30074. editor.getLang('labelMap.fontfamily') ||
  30075. ''
  30076. if (!list.length) return
  30077. for (var i = 0, ci, items = []; (ci = list[i]); i++) {
  30078. var langLabel = editor.getLang('fontfamily')[ci.name] || ''
  30079. ;(function (key, val) {
  30080. items.push({
  30081. label: key,
  30082. value: val,
  30083. theme: editor.options.theme,
  30084. renderLabelHtml: function () {
  30085. return (
  30086. '<div class="edui-label %%-label" style="font-family:' +
  30087. utils.unhtml(this.value) +
  30088. '">' +
  30089. (this.label || '') +
  30090. '</div>'
  30091. )
  30092. },
  30093. })
  30094. })(ci.label || langLabel, ci.val)
  30095. }
  30096. var ui = new editorui.Combox({
  30097. editor: editor,
  30098. items: items,
  30099. onselect: function (t, index) {
  30100. editor.execCommand('FontFamily', this.items[index].value)
  30101. },
  30102. onbuttonclick: function () {
  30103. this.showPopup()
  30104. },
  30105. title: title,
  30106. initValue: title,
  30107. className: 'edui-for-fontfamily',
  30108. indexByValue: function (value) {
  30109. if (value) {
  30110. for (var i = 0, ci; (ci = this.items[i]); i++) {
  30111. if (ci.value.indexOf(value) != -1) return i
  30112. }
  30113. }
  30114. return -1
  30115. },
  30116. })
  30117. editorui.buttons['fontfamily'] = ui
  30118. editor.addListener(
  30119. 'selectionchange',
  30120. function (type, causeByUi, uiReady) {
  30121. if (!uiReady) {
  30122. var state = editor.queryCommandState('FontFamily')
  30123. if (state == -1) {
  30124. ui.setDisabled(true)
  30125. } else {
  30126. ui.setDisabled(false)
  30127. var value = editor.queryCommandValue('FontFamily')
  30128. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  30129. value && (value = value.replace(/['"]/g, '').split(',')[0])
  30130. ui.setValue(value)
  30131. }
  30132. }
  30133. }
  30134. )
  30135. return ui
  30136. }
  30137. editorui.fontsize = function (editor, list, title) {
  30138. title =
  30139. editor.options.labelMap['fontsize'] ||
  30140. editor.getLang('labelMap.fontsize') ||
  30141. ''
  30142. list = list || editor.options['fontsize'] || []
  30143. if (!list.length) return
  30144. var items = []
  30145. for (var i = 0; i < list.length; i++) {
  30146. var size = list[i] + 'px'
  30147. items.push({
  30148. label: size,
  30149. value: size,
  30150. theme: editor.options.theme,
  30151. renderLabelHtml: function () {
  30152. return (
  30153. '<div class="edui-label %%-label" style="line-height:1;font-size:' +
  30154. this.value +
  30155. '">' +
  30156. (this.label || '') +
  30157. '</div>'
  30158. )
  30159. },
  30160. })
  30161. }
  30162. var ui = new editorui.Combox({
  30163. editor: editor,
  30164. items: items,
  30165. title: title,
  30166. initValue: title,
  30167. onselect: function (t, index) {
  30168. editor.execCommand('FontSize', this.items[index].value)
  30169. },
  30170. onbuttonclick: function () {
  30171. this.showPopup()
  30172. },
  30173. className: 'edui-for-fontsize',
  30174. })
  30175. editorui.buttons['fontsize'] = ui
  30176. editor.addListener(
  30177. 'selectionchange',
  30178. function (type, causeByUi, uiReady) {
  30179. if (!uiReady) {
  30180. var state = editor.queryCommandState('FontSize')
  30181. if (state == -1) {
  30182. ui.setDisabled(true)
  30183. } else {
  30184. ui.setDisabled(false)
  30185. ui.setValue(editor.queryCommandValue('FontSize'))
  30186. }
  30187. }
  30188. }
  30189. )
  30190. return ui
  30191. }
  30192. editorui.paragraph = function (editor, list, title) {
  30193. title =
  30194. editor.options.labelMap['paragraph'] ||
  30195. editor.getLang('labelMap.paragraph') ||
  30196. ''
  30197. list = editor.options['paragraph'] || []
  30198. if (utils.isEmptyObject(list)) return
  30199. var items = []
  30200. for (var i in list) {
  30201. items.push({
  30202. value: i,
  30203. label: list[i] || editor.getLang('paragraph')[i],
  30204. theme: editor.options.theme,
  30205. renderLabelHtml: function () {
  30206. return (
  30207. '<div class="edui-label %%-label"><span class="edui-for-' +
  30208. this.value +
  30209. '">' +
  30210. (this.label || '') +
  30211. '</span></div>'
  30212. )
  30213. },
  30214. })
  30215. }
  30216. var ui = new editorui.Combox({
  30217. editor: editor,
  30218. items: items,
  30219. title: title,
  30220. initValue: title,
  30221. className: 'edui-for-paragraph',
  30222. onselect: function (t, index) {
  30223. editor.execCommand('Paragraph', this.items[index].value)
  30224. },
  30225. onbuttonclick: function () {
  30226. this.showPopup()
  30227. },
  30228. })
  30229. editorui.buttons['paragraph'] = ui
  30230. editor.addListener(
  30231. 'selectionchange',
  30232. function (type, causeByUi, uiReady) {
  30233. if (!uiReady) {
  30234. var state = editor.queryCommandState('Paragraph')
  30235. if (state == -1) {
  30236. ui.setDisabled(true)
  30237. } else {
  30238. ui.setDisabled(false)
  30239. var value = editor.queryCommandValue('Paragraph')
  30240. var index = ui.indexByValue(value)
  30241. if (index != -1) {
  30242. ui.setValue(value)
  30243. } else {
  30244. ui.setValue(ui.initValue)
  30245. }
  30246. }
  30247. }
  30248. }
  30249. )
  30250. return ui
  30251. }
  30252. //自定义标题
  30253. editorui.customstyle = function (editor) {
  30254. var list = editor.options['customstyle'] || [],
  30255. title =
  30256. editor.options.labelMap['customstyle'] ||
  30257. editor.getLang('labelMap.customstyle') ||
  30258. ''
  30259. if (!list.length) return
  30260. var langCs = editor.getLang('customstyle')
  30261. for (var i = 0, items = [], t; (t = list[i++]); ) {
  30262. ;(function (t) {
  30263. var ck = {}
  30264. ck.label = t.label ? t.label : langCs[t.name]
  30265. ck.style = t.style
  30266. ck.className = t.className
  30267. ck.tag = t.tag
  30268. items.push({
  30269. label: ck.label,
  30270. value: ck,
  30271. theme: editor.options.theme,
  30272. renderLabelHtml: function () {
  30273. return (
  30274. '<div class="edui-label %%-label">' +
  30275. '<' +
  30276. ck.tag +
  30277. ' ' +
  30278. (ck.className ? ' class="' + ck.className + '"' : '') +
  30279. (ck.style ? ' style="' + ck.style + '"' : '') +
  30280. '>' +
  30281. ck.label +
  30282. '</' +
  30283. ck.tag +
  30284. '>' +
  30285. '</div>'
  30286. )
  30287. },
  30288. })
  30289. })(t)
  30290. }
  30291. var ui = new editorui.Combox({
  30292. editor: editor,
  30293. items: items,
  30294. title: title,
  30295. initValue: title,
  30296. className: 'edui-for-customstyle',
  30297. onselect: function (t, index) {
  30298. editor.execCommand('customstyle', this.items[index].value)
  30299. },
  30300. onbuttonclick: function () {
  30301. this.showPopup()
  30302. },
  30303. indexByValue: function (value) {
  30304. for (var i = 0, ti; (ti = this.items[i++]); ) {
  30305. if (ti.label == value) {
  30306. return i - 1
  30307. }
  30308. }
  30309. return -1
  30310. },
  30311. })
  30312. editorui.buttons['customstyle'] = ui
  30313. editor.addListener(
  30314. 'selectionchange',
  30315. function (type, causeByUi, uiReady) {
  30316. if (!uiReady) {
  30317. var state = editor.queryCommandState('customstyle')
  30318. if (state == -1) {
  30319. ui.setDisabled(true)
  30320. } else {
  30321. ui.setDisabled(false)
  30322. var value = editor.queryCommandValue('customstyle')
  30323. var index = ui.indexByValue(value)
  30324. if (index != -1) {
  30325. ui.setValue(value)
  30326. } else {
  30327. ui.setValue(ui.initValue)
  30328. }
  30329. }
  30330. }
  30331. }
  30332. )
  30333. return ui
  30334. }
  30335. editorui.inserttable = function (editor, iframeUrl, title) {
  30336. title =
  30337. editor.options.labelMap['inserttable'] ||
  30338. editor.getLang('labelMap.inserttable') ||
  30339. ''
  30340. var ui = new editorui.TableButton({
  30341. editor: editor,
  30342. title: title,
  30343. className: 'edui-for-inserttable',
  30344. onpicktable: function (t, numCols, numRows) {
  30345. editor.execCommand('InsertTable', {
  30346. numRows: numRows,
  30347. numCols: numCols,
  30348. border: 1,
  30349. })
  30350. },
  30351. onbuttonclick: function () {
  30352. this.showPopup()
  30353. },
  30354. })
  30355. editorui.buttons['inserttable'] = ui
  30356. editor.addListener('selectionchange', function () {
  30357. ui.setDisabled(editor.queryCommandState('inserttable') == -1)
  30358. })
  30359. return ui
  30360. }
  30361. editorui.lineheight = function (editor) {
  30362. var val = editor.options.lineheight || []
  30363. if (!val.length) return
  30364. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  30365. items.push({
  30366. //todo:写死了
  30367. label: ci,
  30368. value: ci,
  30369. theme: editor.options.theme,
  30370. onclick: function () {
  30371. editor.execCommand('lineheight', this.value)
  30372. },
  30373. })
  30374. }
  30375. var ui = new editorui.MenuButton({
  30376. editor: editor,
  30377. className: 'edui-for-lineheight',
  30378. title:
  30379. editor.options.labelMap['lineheight'] ||
  30380. editor.getLang('labelMap.lineheight') ||
  30381. '',
  30382. items: items,
  30383. onbuttonclick: function () {
  30384. var value = editor.queryCommandValue('LineHeight') || this.value
  30385. editor.execCommand('LineHeight', value)
  30386. },
  30387. })
  30388. editorui.buttons['lineheight'] = ui
  30389. editor.addListener('selectionchange', function () {
  30390. var state = editor.queryCommandState('LineHeight')
  30391. if (state == -1) {
  30392. ui.setDisabled(true)
  30393. } else {
  30394. ui.setDisabled(false)
  30395. var value = editor.queryCommandValue('LineHeight')
  30396. value && ui.setValue((value + '').replace(/cm/, ''))
  30397. ui.setChecked(state)
  30398. }
  30399. })
  30400. return ui
  30401. }
  30402. var rowspacings = ['top', 'bottom']
  30403. for (var r = 0, ri; (ri = rowspacings[r++]); ) {
  30404. ;(function (cmd) {
  30405. editorui['rowspacing' + cmd] = function (editor) {
  30406. var val = editor.options['rowspacing' + cmd] || []
  30407. if (!val.length) return null
  30408. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  30409. items.push({
  30410. label: ci,
  30411. value: ci,
  30412. theme: editor.options.theme,
  30413. onclick: function () {
  30414. editor.execCommand('rowspacing', this.value, cmd)
  30415. },
  30416. })
  30417. }
  30418. var ui = new editorui.MenuButton({
  30419. editor: editor,
  30420. className: 'edui-for-rowspacing' + cmd,
  30421. title:
  30422. editor.options.labelMap['rowspacing' + cmd] ||
  30423. editor.getLang('labelMap.rowspacing' + cmd) ||
  30424. '',
  30425. items: items,
  30426. onbuttonclick: function () {
  30427. var value =
  30428. editor.queryCommandValue('rowspacing', cmd) || this.value
  30429. editor.execCommand('rowspacing', value, cmd)
  30430. },
  30431. })
  30432. editorui.buttons[cmd] = ui
  30433. editor.addListener('selectionchange', function () {
  30434. var state = editor.queryCommandState('rowspacing', cmd)
  30435. if (state == -1) {
  30436. ui.setDisabled(true)
  30437. } else {
  30438. ui.setDisabled(false)
  30439. var value = editor.queryCommandValue('rowspacing', cmd)
  30440. value && ui.setValue((value + '').replace(/%/, ''))
  30441. ui.setChecked(state)
  30442. }
  30443. })
  30444. return ui
  30445. }
  30446. })(ri)
  30447. }
  30448. //有序,无序列表
  30449. var lists = ['insertorderedlist', 'insertunorderedlist']
  30450. for (var l = 0, cl; (cl = lists[l++]); ) {
  30451. ;(function (cmd) {
  30452. editorui[cmd] = function (editor) {
  30453. var vals = editor.options[cmd],
  30454. _onMenuClick = function () {
  30455. editor.execCommand(cmd, this.value)
  30456. },
  30457. items = []
  30458. for (var i in vals) {
  30459. items.push({
  30460. label: vals[i] || editor.getLang()[cmd][i] || '',
  30461. value: i,
  30462. theme: editor.options.theme,
  30463. onclick: _onMenuClick,
  30464. })
  30465. }
  30466. var ui = new editorui.MenuButton({
  30467. editor: editor,
  30468. className: 'edui-for-' + cmd,
  30469. title: editor.getLang('labelMap.' + cmd) || '',
  30470. items: items,
  30471. onbuttonclick: function () {
  30472. var value = editor.queryCommandValue(cmd) || this.value
  30473. editor.execCommand(cmd, value)
  30474. },
  30475. })
  30476. editorui.buttons[cmd] = ui
  30477. editor.addListener('selectionchange', function () {
  30478. var state = editor.queryCommandState(cmd)
  30479. if (state == -1) {
  30480. ui.setDisabled(true)
  30481. } else {
  30482. ui.setDisabled(false)
  30483. var value = editor.queryCommandValue(cmd)
  30484. ui.setValue(value)
  30485. ui.setChecked(state)
  30486. }
  30487. })
  30488. return ui
  30489. }
  30490. })(cl)
  30491. }
  30492. editorui.fullscreen = function (editor, title) {
  30493. title =
  30494. editor.options.labelMap['fullscreen'] ||
  30495. editor.getLang('labelMap.fullscreen') ||
  30496. ''
  30497. var ui = new editorui.Button({
  30498. className: 'edui-for-fullscreen',
  30499. title: title,
  30500. theme: editor.options.theme,
  30501. onclick: function () {
  30502. if (editor.ui) {
  30503. editor.ui.setFullScreen(!editor.ui.isFullScreen())
  30504. }
  30505. this.setChecked(editor.ui.isFullScreen())
  30506. },
  30507. })
  30508. editorui.buttons['fullscreen'] = ui
  30509. editor.addListener('selectionchange', function () {
  30510. var state = editor.queryCommandState('fullscreen')
  30511. ui.setDisabled(state == -1)
  30512. ui.setChecked(editor.ui.isFullScreen())
  30513. })
  30514. return ui
  30515. }
  30516. // 表情
  30517. editorui['emotion'] = function (editor, iframeUrl) {
  30518. var cmd = 'emotion'
  30519. var ui = new editorui.MultiMenuPop({
  30520. title:
  30521. editor.options.labelMap[cmd] ||
  30522. editor.getLang('labelMap.' + cmd + '') ||
  30523. '',
  30524. editor: editor,
  30525. className: 'edui-for-' + cmd,
  30526. iframeUrl: editor.ui.mapUrl(
  30527. iframeUrl ||
  30528. (editor.options.iframeUrlMap || {})[cmd] ||
  30529. iframeUrlMap[cmd]
  30530. ),
  30531. })
  30532. editorui.buttons[cmd] = ui
  30533. editor.addListener('selectionchange', function () {
  30534. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  30535. })
  30536. return ui
  30537. }
  30538. editorui.autotypeset = function (editor) {
  30539. var ui = new editorui.AutoTypeSetButton({
  30540. editor: editor,
  30541. title:
  30542. editor.options.labelMap['autotypeset'] ||
  30543. editor.getLang('labelMap.autotypeset') ||
  30544. '',
  30545. className: 'edui-for-autotypeset',
  30546. onbuttonclick: function () {
  30547. editor.execCommand('autotypeset')
  30548. },
  30549. })
  30550. editorui.buttons['autotypeset'] = ui
  30551. editor.addListener('selectionchange', function () {
  30552. ui.setDisabled(editor.queryCommandState('autotypeset') == -1)
  30553. })
  30554. return ui
  30555. }
  30556. /* 简单上传插件 */
  30557. editorui['simpleupload'] = function (editor) {
  30558. var name = 'simpleupload',
  30559. ui = new editorui.Button({
  30560. className: 'edui-for-' + name,
  30561. title:
  30562. editor.options.labelMap[name] ||
  30563. editor.getLang('labelMap.' + name) ||
  30564. '',
  30565. onclick: function () {},
  30566. theme: editor.options.theme,
  30567. showText: false,
  30568. })
  30569. editorui.buttons[name] = ui
  30570. editor.addListener('ready', function () {
  30571. var b = ui.getDom('body'),
  30572. iconSpan = b.children[0]
  30573. editor.fireEvent('simpleuploadbtnready', iconSpan)
  30574. })
  30575. editor.addListener(
  30576. 'selectionchange',
  30577. function (type, causeByUi, uiReady) {
  30578. var state = editor.queryCommandState(name)
  30579. if (state == -1) {
  30580. ui.setDisabled(true)
  30581. ui.setChecked(false)
  30582. } else {
  30583. if (!uiReady) {
  30584. ui.setDisabled(false)
  30585. ui.setChecked(state)
  30586. }
  30587. }
  30588. }
  30589. )
  30590. return ui
  30591. }
  30592. /* word上传 */
  30593. editorui['wordupload'] = function (editor) {
  30594. var name = 'wordupload',
  30595. ui = new editorui.Button({
  30596. className: 'edui-for-' + name,
  30597. title:
  30598. editor.options.labelMap[name] ||
  30599. editor.getLang('labelMap.' + name) ||
  30600. '',
  30601. onclick: function () {},
  30602. theme: editor.options.theme,
  30603. showText: false,
  30604. })
  30605. editorui.buttons[name] = ui
  30606. editor.addListener('ready', function () {
  30607. var b = ui.getDom('body'),
  30608. iconSpan = b.children[0]
  30609. editor.fireEvent('worduploadbtnready', iconSpan)
  30610. })
  30611. editor.addListener(
  30612. 'selectionchange',
  30613. function (type, causeByUi, uiReady) {
  30614. var state = editor.queryCommandState(name)
  30615. if (state == -1) {
  30616. ui.setDisabled(true)
  30617. ui.setChecked(false)
  30618. } else {
  30619. if (!uiReady) {
  30620. ui.setDisabled(false)
  30621. ui.setChecked(state)
  30622. }
  30623. }
  30624. }
  30625. )
  30626. return ui
  30627. }
  30628. })()
  30629. // adapter/editor.js
  30630. ///import core
  30631. ///commands 全屏
  30632. ///commandsName FullScreen
  30633. ///commandsTitle 全屏
  30634. ;(function () {
  30635. var utils = baidu.editor.utils,
  30636. uiUtils = baidu.editor.ui.uiUtils,
  30637. UIBase = baidu.editor.ui.UIBase,
  30638. domUtils = baidu.editor.dom.domUtils
  30639. var nodeStack = []
  30640. function EditorUI(options) {
  30641. this.initOptions(options)
  30642. this.initEditorUI()
  30643. }
  30644. EditorUI.prototype = {
  30645. uiName: 'editor',
  30646. initEditorUI: function () {
  30647. this.editor.ui = this
  30648. this._dialogs = {}
  30649. this.initUIBase()
  30650. this._initToolbars()
  30651. var editor = this.editor,
  30652. me = this
  30653. editor.addListener('ready', function () {
  30654. //提供getDialog方法
  30655. editor.getDialog = function (name) {
  30656. return editor.ui._dialogs[name + 'Dialog']
  30657. }
  30658. domUtils.on(editor.window, 'scroll', function (evt) {
  30659. baidu.editor.ui.Popup.postHide(evt)
  30660. })
  30661. //提供编辑器实时宽高(全屏时宽高不变化)
  30662. editor.ui._actualFrameWidth = editor.options.initialFrameWidth
  30663. UE.browser.ie &&
  30664. UE.browser.version === 6 &&
  30665. editor.container.ownerDocument.execCommand(
  30666. 'BackgroundImageCache',
  30667. false,
  30668. true
  30669. )
  30670. //display bottom-bar label based on config
  30671. if (editor.options.elementPathEnabled) {
  30672. editor.ui.getDom('elementpath').innerHTML =
  30673. '<div class="edui-editor-breadcrumb">' +
  30674. editor.getLang('elementPathTip') +
  30675. ':</div>'
  30676. }
  30677. if (editor.options.wordCount) {
  30678. function countFn() {
  30679. setCount(editor, me)
  30680. domUtils.un(editor.document, 'click', arguments.callee)
  30681. }
  30682. domUtils.on(editor.document, 'click', countFn)
  30683. editor.ui.getDom('wordcount').innerHTML =
  30684. editor.getLang('wordCountTip')
  30685. }
  30686. editor.ui._scale()
  30687. if (editor.options.scaleEnabled) {
  30688. if (editor.autoHeightEnabled) {
  30689. editor.disableAutoHeight()
  30690. }
  30691. me.enableScale()
  30692. } else {
  30693. me.disableScale()
  30694. }
  30695. if (
  30696. !editor.options.elementPathEnabled &&
  30697. !editor.options.wordCount &&
  30698. !editor.options.scaleEnabled
  30699. ) {
  30700. editor.ui.getDom('elementpath').style.display = 'none'
  30701. editor.ui.getDom('wordcount').style.display = 'none'
  30702. editor.ui.getDom('scale').style.display = 'none'
  30703. }
  30704. if (!editor.selection.isFocus()) return
  30705. editor.fireEvent('selectionchange', false, true)
  30706. })
  30707. editor.addListener('mousedown', function (t, evt) {
  30708. var el = evt.target || evt.srcElement
  30709. baidu.editor.ui.Popup.postHide(evt, el)
  30710. baidu.editor.ui.ShortCutMenu.postHide(evt)
  30711. })
  30712. editor.addListener('delcells', function () {
  30713. if (UE.ui['edittip']) {
  30714. new UE.ui['edittip'](editor)
  30715. }
  30716. editor.getDialog('edittip').open()
  30717. })
  30718. var pastePop,
  30719. isPaste = false,
  30720. timer
  30721. editor.addListener('afterpaste', function () {
  30722. if (editor.queryCommandState('pasteplain')) return
  30723. if (baidu.editor.ui.PastePicker) {
  30724. pastePop = new baidu.editor.ui.Popup({
  30725. content: new baidu.editor.ui.PastePicker({ editor: editor }),
  30726. editor: editor,
  30727. className: 'edui-wordpastepop',
  30728. })
  30729. pastePop.render()
  30730. }
  30731. isPaste = true
  30732. })
  30733. editor.addListener('afterinserthtml', function () {
  30734. clearTimeout(timer)
  30735. timer = setTimeout(function () {
  30736. if (pastePop && (isPaste || editor.ui._isTransfer)) {
  30737. if (pastePop.isHidden()) {
  30738. var span = domUtils.createElement(editor.document, 'span', {
  30739. style: 'line-height:0px;',
  30740. innerHTML: '\ufeff',
  30741. }),
  30742. range = editor.selection.getRange()
  30743. range.insertNode(span)
  30744. var tmp = getDomNode(span, 'firstChild', 'previousSibling')
  30745. tmp &&
  30746. pastePop.showAnchor(tmp.nodeType == 3 ? tmp.parentNode : tmp)
  30747. domUtils.remove(span)
  30748. } else {
  30749. pastePop.show()
  30750. }
  30751. delete editor.ui._isTransfer
  30752. isPaste = false
  30753. }
  30754. }, 200)
  30755. })
  30756. editor.addListener('contextmenu', function (t, evt) {
  30757. baidu.editor.ui.Popup.postHide(evt)
  30758. })
  30759. editor.addListener('keydown', function (t, evt) {
  30760. if (pastePop) pastePop.dispose(evt)
  30761. var keyCode = evt.keyCode || evt.which
  30762. if (evt.altKey && keyCode == 90) {
  30763. UE.ui.buttons['fullscreen'].onclick()
  30764. }
  30765. })
  30766. editor.addListener('wordcount', function (type) {
  30767. setCount(this, me)
  30768. })
  30769. function setCount(editor, ui) {
  30770. editor.setOpt({
  30771. wordCount: true,
  30772. maximumWords: 10000,
  30773. wordCountMsg:
  30774. editor.options.wordCountMsg || editor.getLang('wordCountMsg'),
  30775. wordOverFlowMsg:
  30776. editor.options.wordOverFlowMsg ||
  30777. editor.getLang('wordOverFlowMsg'),
  30778. })
  30779. var opt = editor.options,
  30780. max = opt.maximumWords,
  30781. msg = opt.wordCountMsg,
  30782. errMsg = opt.wordOverFlowMsg,
  30783. countDom = ui.getDom('wordcount')
  30784. if (!opt.wordCount) {
  30785. return
  30786. }
  30787. var count = editor.getContentLength(true)
  30788. if (count > max) {
  30789. countDom.innerHTML = errMsg
  30790. editor.fireEvent('wordcountoverflow')
  30791. } else {
  30792. countDom.innerHTML = msg
  30793. .replace('{#leave}', max - count)
  30794. .replace('{#count}', count)
  30795. }
  30796. }
  30797. editor.addListener('selectionchange', function () {
  30798. if (editor.options.elementPathEnabled) {
  30799. me[
  30800. (editor.queryCommandState('elementpath') == -1 ? 'dis' : 'en') +
  30801. 'ableElementPath'
  30802. ]()
  30803. }
  30804. if (editor.options.scaleEnabled) {
  30805. me[
  30806. (editor.queryCommandState('scale') == -1 ? 'dis' : 'en') +
  30807. 'ableScale'
  30808. ]()
  30809. }
  30810. })
  30811. var popup = new baidu.editor.ui.Popup({
  30812. editor: editor,
  30813. content: '',
  30814. className: 'edui-bubble',
  30815. _onEditButtonClick: function () {
  30816. this.hide()
  30817. editor.ui._dialogs.linkDialog.open()
  30818. },
  30819. _onImgEditButtonClick: function (name) {
  30820. this.hide()
  30821. editor.ui._dialogs[name] && editor.ui._dialogs[name].open()
  30822. },
  30823. _onImgSetFloat: function (value) {
  30824. this.hide()
  30825. editor.execCommand('imagefloat', value)
  30826. },
  30827. _setIframeAlign: function (value) {
  30828. var frame = popup.anchorEl
  30829. var newFrame = frame.cloneNode(true)
  30830. switch (value) {
  30831. case -2:
  30832. newFrame.setAttribute('align', '')
  30833. break
  30834. case -1:
  30835. newFrame.setAttribute('align', 'left')
  30836. break
  30837. case 1:
  30838. newFrame.setAttribute('align', 'right')
  30839. break
  30840. }
  30841. frame.parentNode.insertBefore(newFrame, frame)
  30842. domUtils.remove(frame)
  30843. popup.anchorEl = newFrame
  30844. popup.showAnchor(popup.anchorEl)
  30845. },
  30846. _updateIframe: function () {
  30847. var frame = (editor._iframe = popup.anchorEl)
  30848. if (domUtils.hasClass(frame, 'ueditor_baidumap')) {
  30849. editor.selection.getRange().selectNode(frame).select()
  30850. editor.ui._dialogs.mapDialog.open()
  30851. popup.hide()
  30852. } else {
  30853. editor.ui._dialogs.insertframeDialog.open()
  30854. popup.hide()
  30855. }
  30856. },
  30857. _onRemoveButtonClick: function (cmdName) {
  30858. editor.execCommand(cmdName)
  30859. this.hide()
  30860. },
  30861. queryAutoHide: function (el) {
  30862. if (el && el.ownerDocument == editor.document) {
  30863. if (
  30864. el.tagName.toLowerCase() == 'img' ||
  30865. domUtils.findParentByTagName(el, 'a', true)
  30866. ) {
  30867. return el !== popup.anchorEl
  30868. }
  30869. }
  30870. return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el)
  30871. },
  30872. })
  30873. popup.render()
  30874. if (editor.options.imagePopup) {
  30875. editor.addListener('mouseover', function (t, evt) {
  30876. evt = evt || window.event
  30877. var el = evt.target || evt.srcElement
  30878. if (
  30879. editor.ui._dialogs.insertframeDialog &&
  30880. /iframe/gi.test(el.tagName)
  30881. ) {
  30882. var html = popup.formatHtml(
  30883. '<nobr>' +
  30884. editor.getLang('property') +
  30885. ': <span onclick=$$._setIframeAlign(-2) class="edui-clickable">' +
  30886. editor.getLang('default') +
  30887. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(-1) class="edui-clickable">' +
  30888. editor.getLang('justifyleft') +
  30889. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(1) class="edui-clickable">' +
  30890. editor.getLang('justifyright') +
  30891. '</span>&nbsp;&nbsp;' +
  30892. ' <span onclick="$$._updateIframe( this);" class="edui-clickable">' +
  30893. editor.getLang('modify') +
  30894. '</span></nobr>'
  30895. )
  30896. if (html) {
  30897. popup.getDom('content').innerHTML = html
  30898. popup.anchorEl = el
  30899. popup.showAnchor(popup.anchorEl)
  30900. } else {
  30901. popup.hide()
  30902. }
  30903. }
  30904. })
  30905. editor.addListener('selectionchange', function (t, causeByUi) {
  30906. if (!causeByUi) return
  30907. var html = '',
  30908. str = '',
  30909. img = editor.selection.getRange().getClosedNode(),
  30910. dialogs = editor.ui._dialogs
  30911. if (img && img.tagName == 'IMG') {
  30912. var dialogName = 'insertimageDialog'
  30913. if (
  30914. img.className.indexOf('edui-faked-video') != -1 ||
  30915. img.className.indexOf('edui-upload-video') != -1
  30916. ) {
  30917. dialogName = 'insertvideoDialog'
  30918. }
  30919. if (img.className.indexOf('edui-faked-webapp') != -1) {
  30920. dialogName = 'webappDialog'
  30921. }
  30922. if (img.src.indexOf('http://api.map.baidu.com') != -1) {
  30923. dialogName = 'mapDialog'
  30924. }
  30925. if (img.className.indexOf('edui-faked-music') != -1) {
  30926. dialogName = 'musicDialog'
  30927. }
  30928. if (
  30929. img.src.indexOf('http://maps.google.com/maps/api/staticmap') !=
  30930. -1
  30931. ) {
  30932. dialogName = 'gmapDialog'
  30933. }
  30934. if (img.getAttribute('anchorname')) {
  30935. dialogName = 'anchorDialog'
  30936. html = popup.formatHtml(
  30937. '<nobr>' +
  30938. editor.getLang('property') +
  30939. ': <span onclick=$$._onImgEditButtonClick("anchorDialog") class="edui-clickable">' +
  30940. editor.getLang('modify') +
  30941. '</span>&nbsp;&nbsp;' +
  30942. '<span onclick=$$._onRemoveButtonClick(\'anchor\') class="edui-clickable">' +
  30943. editor.getLang('delete') +
  30944. '</span></nobr>'
  30945. )
  30946. }
  30947. if (img.getAttribute('word_img')) {
  30948. //todo 放到dialog去做查询
  30949. editor.word_img = [img.getAttribute('word_img')]
  30950. dialogName = 'wordimageDialog'
  30951. }
  30952. if (
  30953. domUtils.hasClass(img, 'loadingclass') ||
  30954. domUtils.hasClass(img, 'loaderrorclass')
  30955. ) {
  30956. dialogName = ''
  30957. }
  30958. if (!dialogs[dialogName]) {
  30959. return
  30960. }
  30961. str =
  30962. '<nobr>' +
  30963. editor.getLang('property') +
  30964. ': ' +
  30965. '<span onclick=$$._onImgSetFloat("none") class="edui-clickable">' +
  30966. editor.getLang('default') +
  30967. '</span>&nbsp;&nbsp;' +
  30968. '<span onclick=$$._onImgSetFloat("left") class="edui-clickable">' +
  30969. editor.getLang('justifyleft') +
  30970. '</span>&nbsp;&nbsp;' +
  30971. '<span onclick=$$._onImgSetFloat("right") class="edui-clickable">' +
  30972. editor.getLang('justifyright') +
  30973. '</span>&nbsp;&nbsp;' +
  30974. '<span onclick=$$._onImgSetFloat("center") class="edui-clickable">' +
  30975. editor.getLang('justifycenter') +
  30976. '</span>&nbsp;&nbsp;' +
  30977. '<span onclick="$$._onImgEditButtonClick(\'' +
  30978. dialogName +
  30979. '\');" class="edui-clickable">' +
  30980. editor.getLang('modify') +
  30981. '</span></nobr>'
  30982. !html && (html = popup.formatHtml(str))
  30983. }
  30984. if (editor.ui._dialogs.linkDialog) {
  30985. var link = editor.queryCommandValue('link')
  30986. var url
  30987. if (
  30988. link &&
  30989. (url =
  30990. link.getAttribute('_href') || link.getAttribute('href', 2))
  30991. ) {
  30992. var txt = url
  30993. if (url.length > 30) {
  30994. txt = url.substring(0, 20) + '...'
  30995. }
  30996. if (html) {
  30997. html += '<div style="height:5px;"></div>'
  30998. }
  30999. html += popup.formatHtml(
  31000. '<nobr>' +
  31001. editor.getLang('anthorMsg') +
  31002. ': <a target="_blank" href="' +
  31003. url +
  31004. '" title="' +
  31005. url +
  31006. '" >' +
  31007. txt +
  31008. '</a>' +
  31009. ' <span class="edui-clickable" onclick="$$._onEditButtonClick();">' +
  31010. editor.getLang('modify') +
  31011. '</span>' +
  31012. ' <span class="edui-clickable" onclick="$$._onRemoveButtonClick(\'unlink\');"> ' +
  31013. editor.getLang('clear') +
  31014. '</span></nobr>'
  31015. )
  31016. popup.showAnchor(link)
  31017. }
  31018. }
  31019. if (html) {
  31020. popup.getDom('content').innerHTML = html
  31021. popup.anchorEl = img || link
  31022. popup.showAnchor(popup.anchorEl)
  31023. } else {
  31024. popup.hide()
  31025. }
  31026. })
  31027. }
  31028. },
  31029. _initToolbars: function () {
  31030. var editor = this.editor
  31031. var toolbars = this.toolbars || []
  31032. var toolbarUis = []
  31033. for (var i = 0; i < toolbars.length; i++) {
  31034. var toolbar = toolbars[i]
  31035. var toolbarUi = new baidu.editor.ui.Toolbar({
  31036. theme: editor.options.theme,
  31037. })
  31038. for (var j = 0; j < toolbar.length; j++) {
  31039. var toolbarItem = toolbar[j]
  31040. var toolbarItemUi = null
  31041. if (typeof toolbarItem == 'string') {
  31042. toolbarItem = toolbarItem.toLowerCase()
  31043. if (toolbarItem == '|') {
  31044. toolbarItem = 'Separator'
  31045. }
  31046. if (toolbarItem == '||') {
  31047. toolbarItem = 'Breakline'
  31048. }
  31049. if (baidu.editor.ui[toolbarItem]) {
  31050. toolbarItemUi = new baidu.editor.ui[toolbarItem](editor)
  31051. }
  31052. //fullscreen这里单独处理一下,放到首行去
  31053. if (toolbarItem == 'fullscreen') {
  31054. if (toolbarUis && toolbarUis[0]) {
  31055. toolbarUis[0].items.splice(0, 0, toolbarItemUi)
  31056. } else {
  31057. toolbarItemUi && toolbarUi.items.splice(0, 0, toolbarItemUi)
  31058. }
  31059. continue
  31060. }
  31061. } else {
  31062. toolbarItemUi = toolbarItem
  31063. }
  31064. if (toolbarItemUi && toolbarItemUi.id) {
  31065. toolbarUi.add(toolbarItemUi)
  31066. }
  31067. }
  31068. toolbarUis[i] = toolbarUi
  31069. }
  31070. //接受外部定制的UI(修复因 utils.each 无法准确的循环出对象的全部元素而导致的自定义 UI 不符合预期的 BUG by HaoChuan9421)
  31071. // utils.each(UE._customizeUI,function(obj,key){
  31072. // var itemUI,index;
  31073. // if(obj.id && obj.id != editor.key){
  31074. // return false;
  31075. // }
  31076. // itemUI = obj.execFn.call(editor,editor,key);
  31077. // if(itemUI){
  31078. // index = obj.index;
  31079. // if(index === undefined){
  31080. // index = toolbarUi.items.length;
  31081. // }
  31082. // toolbarUi.add(itemUI,index)
  31083. // }
  31084. // });
  31085. for (var key in UE._customizeUI) {
  31086. var obj = UE._customizeUI[key]
  31087. var itemUI, index
  31088. if (!obj.id || obj.id == editor.key) {
  31089. itemUI = obj.execFn.call(editor, editor, key)
  31090. if (itemUI) {
  31091. index = obj.index
  31092. if (index === undefined) {
  31093. index = toolbarUi.items.length
  31094. }
  31095. toolbarUi.add(itemUI, index)
  31096. }
  31097. }
  31098. }
  31099. this.toolbars = toolbarUis
  31100. },
  31101. getHtmlTpl: function () {
  31102. return (
  31103. '<div id="##" class="%%">' +
  31104. '<div id="##_toolbarbox" class="%%-toolbarbox">' +
  31105. (this.toolbars.length
  31106. ? '<div id="##_toolbarboxouter" class="%%-toolbarboxouter"><div class="%%-toolbarboxinner">' +
  31107. this.renderToolbarBoxHtml() +
  31108. '</div></div>'
  31109. : '') +
  31110. '<div id="##_toolbarmsg" class="%%-toolbarmsg" style="display:none;">' +
  31111. '<div id = "##_upload_dialog" class="%%-toolbarmsg-upload" onclick="$$.showWordImageDialog();">' +
  31112. this.editor.getLang('clickToUpload') +
  31113. '</div>' +
  31114. '<div class="%%-toolbarmsg-close" onclick="$$.hideToolbarMsg();">x</div>' +
  31115. '<div id="##_toolbarmsg_label" class="%%-toolbarmsg-label"></div>' +
  31116. '<div style="height:0;overflow:hidden;clear:both;"></div>' +
  31117. '</div>' +
  31118. '<div id="##_message_holder" class="%%-messageholder"></div>' +
  31119. '</div>' +
  31120. '<div id="##_iframeholder" class="%%-iframeholder">' +
  31121. '</div>' +
  31122. //modify wdcount by matao
  31123. '<div id="##_bottombar" class="%%-bottomContainer"><table><tr>' +
  31124. '<td id="##_elementpath" class="%%-bottombar"></td>' +
  31125. '<td id="##_wordcount" class="%%-wordcount"></td>' +
  31126. '<td id="##_scale" class="%%-scale"><div class="%%-icon"></div></td>' +
  31127. '</tr></table></div>' +
  31128. '<div id="##_scalelayer"></div>' +
  31129. '</div>'
  31130. )
  31131. },
  31132. showWordImageDialog: function () {
  31133. this._dialogs['wordimageDialog'].open()
  31134. },
  31135. renderToolbarBoxHtml: function () {
  31136. var buff = []
  31137. for (var i = 0; i < this.toolbars.length; i++) {
  31138. buff.push(this.toolbars[i].renderHtml())
  31139. }
  31140. return buff.join('')
  31141. },
  31142. setFullScreen: function (fullscreen) {
  31143. var editor = this.editor,
  31144. container = editor.container.parentNode.parentNode
  31145. if (this._fullscreen != fullscreen) {
  31146. this._fullscreen = fullscreen
  31147. this.editor.fireEvent('beforefullscreenchange', fullscreen)
  31148. if (baidu.editor.browser.gecko) {
  31149. var bk = editor.selection.getRange().createBookmark()
  31150. }
  31151. if (fullscreen) {
  31152. while (container.tagName != 'BODY') {
  31153. var position = baidu.editor.dom.domUtils.getComputedStyle(
  31154. container,
  31155. 'position'
  31156. )
  31157. nodeStack.push(position)
  31158. container.style.position = 'static'
  31159. container = container.parentNode
  31160. }
  31161. this._bakHtmlOverflow = document.documentElement.style.overflow
  31162. this._bakBodyOverflow = document.body.style.overflow
  31163. this._bakAutoHeight = this.editor.autoHeightEnabled
  31164. this._bakScrollTop = Math.max(
  31165. document.documentElement.scrollTop,
  31166. document.body.scrollTop
  31167. )
  31168. this._bakEditorContaninerWidth =
  31169. editor.iframe.parentNode.offsetWidth
  31170. if (this._bakAutoHeight) {
  31171. //当全屏时不能执行自动长高
  31172. editor.autoHeightEnabled = false
  31173. this.editor.disableAutoHeight()
  31174. }
  31175. document.documentElement.style.overflow = 'hidden'
  31176. //修复,滚动条不收起的问题
  31177. window.scrollTo(0, window.scrollY)
  31178. this._bakCssText = this.getDom().style.cssText
  31179. this._bakCssText1 = this.getDom('iframeholder').style.cssText
  31180. editor.iframe.parentNode.style.width = ''
  31181. this._updateFullScreen()
  31182. } else {
  31183. while (container.tagName != 'BODY') {
  31184. container.style.position = nodeStack.shift()
  31185. container = container.parentNode
  31186. }
  31187. this.getDom().style.cssText = this._bakCssText
  31188. this.getDom('iframeholder').style.cssText = this._bakCssText1
  31189. if (this._bakAutoHeight) {
  31190. editor.autoHeightEnabled = true
  31191. this.editor.enableAutoHeight()
  31192. }
  31193. document.documentElement.style.overflow = this._bakHtmlOverflow
  31194. document.body.style.overflow = this._bakBodyOverflow
  31195. editor.iframe.parentNode.style.width =
  31196. this._bakEditorContaninerWidth + 'px'
  31197. window.scrollTo(0, this._bakScrollTop)
  31198. }
  31199. if (browser.gecko && editor.body.contentEditable === 'true') {
  31200. var input = document.createElement('input')
  31201. document.body.appendChild(input)
  31202. editor.body.contentEditable = false
  31203. setTimeout(function () {
  31204. input.focus()
  31205. setTimeout(function () {
  31206. editor.body.contentEditable = true
  31207. editor.fireEvent('fullscreenchanged', fullscreen)
  31208. editor.selection.getRange().moveToBookmark(bk).select(true)
  31209. baidu.editor.dom.domUtils.remove(input)
  31210. fullscreen && window.scroll(0, 0)
  31211. }, 0)
  31212. }, 0)
  31213. }
  31214. if (editor.body.contentEditable === 'true') {
  31215. this.editor.fireEvent('fullscreenchanged', fullscreen)
  31216. this.triggerLayout()
  31217. }
  31218. }
  31219. },
  31220. _updateFullScreen: function () {
  31221. if (this._fullscreen) {
  31222. var vpRect = uiUtils.getViewportRect()
  31223. this.getDom().style.cssText =
  31224. 'border:0;position:absolute;left:0;top:' +
  31225. (this.editor.options.topOffset || 0) +
  31226. 'px;width:' +
  31227. vpRect.width +
  31228. 'px;height:' +
  31229. vpRect.height +
  31230. 'px;z-index:' +
  31231. (this.getDom().style.zIndex * 1 + 100)
  31232. uiUtils.setViewportOffset(this.getDom(), {
  31233. left: 0,
  31234. top: this.editor.options.topOffset || 0,
  31235. })
  31236. this.editor.setHeight(
  31237. vpRect.height -
  31238. this.getDom('toolbarbox').offsetHeight -
  31239. this.getDom('bottombar').offsetHeight -
  31240. (this.editor.options.topOffset || 0),
  31241. true
  31242. )
  31243. //不手动调一下,会导致全屏失效
  31244. if (browser.gecko) {
  31245. try {
  31246. window.onresize()
  31247. } catch (e) {}
  31248. }
  31249. }
  31250. },
  31251. _updateElementPath: function () {
  31252. var bottom = this.getDom('elementpath'),
  31253. list
  31254. if (
  31255. this.elementPathEnabled &&
  31256. (list = this.editor.queryCommandValue('elementpath'))
  31257. ) {
  31258. var buff = []
  31259. for (var i = 0, ci; (ci = list[i]); i++) {
  31260. buff[i] = this.formatHtml(
  31261. '<span unselectable="on" onclick="$$.editor.execCommand(&quot;elementpath&quot;, &quot;' +
  31262. i +
  31263. '&quot;);">' +
  31264. ci +
  31265. '</span>'
  31266. )
  31267. }
  31268. bottom.innerHTML =
  31269. '<div class="edui-editor-breadcrumb" onmousedown="return false;">' +
  31270. this.editor.getLang('elementPathTip') +
  31271. ': ' +
  31272. buff.join(' &gt; ') +
  31273. '</div>'
  31274. } else {
  31275. bottom.style.display = 'none'
  31276. }
  31277. },
  31278. disableElementPath: function () {
  31279. var bottom = this.getDom('elementpath')
  31280. bottom.innerHTML = ''
  31281. bottom.style.display = 'none'
  31282. this.elementPathEnabled = false
  31283. },
  31284. enableElementPath: function () {
  31285. var bottom = this.getDom('elementpath')
  31286. bottom.style.display = ''
  31287. this.elementPathEnabled = true
  31288. this._updateElementPath()
  31289. },
  31290. _scale: function () {
  31291. var doc = document,
  31292. editor = this.editor,
  31293. editorHolder = editor.container,
  31294. editorDocument = editor.document,
  31295. toolbarBox = this.getDom('toolbarbox'),
  31296. bottombar = this.getDom('bottombar'),
  31297. scale = this.getDom('scale'),
  31298. scalelayer = this.getDom('scalelayer')
  31299. var isMouseMove = false,
  31300. position = null,
  31301. minEditorHeight = 0,
  31302. minEditorWidth = editor.options.minFrameWidth,
  31303. pageX = 0,
  31304. pageY = 0,
  31305. scaleWidth = 0,
  31306. scaleHeight = 0
  31307. function down() {
  31308. position = domUtils.getXY(editorHolder)
  31309. if (!minEditorHeight) {
  31310. minEditorHeight =
  31311. editor.options.minFrameHeight +
  31312. toolbarBox.offsetHeight +
  31313. bottombar.offsetHeight
  31314. }
  31315. scalelayer.style.cssText =
  31316. 'position:absolute;left:0;display:;top:0;background-color:#41ABFF;opacity:0.4;filter: Alpha(opacity=40);width:' +
  31317. editorHolder.offsetWidth +
  31318. 'px;height:' +
  31319. editorHolder.offsetHeight +
  31320. 'px;z-index:' +
  31321. (editor.options.zIndex + 1)
  31322. domUtils.on(doc, 'mousemove', move)
  31323. domUtils.on(editorDocument, 'mouseup', up)
  31324. domUtils.on(doc, 'mouseup', up)
  31325. }
  31326. var me = this
  31327. //by xuheng 全屏时关掉缩放
  31328. this.editor.addListener('fullscreenchanged', function (e, fullScreen) {
  31329. if (fullScreen) {
  31330. me.disableScale()
  31331. } else {
  31332. if (me.editor.options.scaleEnabled) {
  31333. me.enableScale()
  31334. var tmpNode = me.editor.document.createElement('span')
  31335. me.editor.body.appendChild(tmpNode)
  31336. me.editor.body.style.height =
  31337. Math.max(
  31338. domUtils.getXY(tmpNode).y,
  31339. me.editor.iframe.offsetHeight - 20
  31340. ) + 'px'
  31341. domUtils.remove(tmpNode)
  31342. }
  31343. }
  31344. })
  31345. function move(event) {
  31346. clearSelection()
  31347. var e = event || window.event
  31348. pageX = e.pageX || doc.documentElement.scrollLeft + e.clientX
  31349. pageY = e.pageY || doc.documentElement.scrollTop + e.clientY
  31350. scaleWidth = pageX - position.x
  31351. scaleHeight = pageY - position.y
  31352. if (scaleWidth >= minEditorWidth) {
  31353. isMouseMove = true
  31354. scalelayer.style.width = scaleWidth + 'px'
  31355. }
  31356. if (scaleHeight >= minEditorHeight) {
  31357. isMouseMove = true
  31358. scalelayer.style.height = scaleHeight + 'px'
  31359. }
  31360. }
  31361. function up() {
  31362. if (isMouseMove) {
  31363. isMouseMove = false
  31364. editor.ui._actualFrameWidth = scalelayer.offsetWidth - 2
  31365. editorHolder.style.width = editor.ui._actualFrameWidth + 'px'
  31366. editor.setHeight(
  31367. scalelayer.offsetHeight -
  31368. bottombar.offsetHeight -
  31369. toolbarBox.offsetHeight -
  31370. 2,
  31371. true
  31372. )
  31373. }
  31374. if (scalelayer) {
  31375. scalelayer.style.display = 'none'
  31376. }
  31377. clearSelection()
  31378. domUtils.un(doc, 'mousemove', move)
  31379. domUtils.un(editorDocument, 'mouseup', up)
  31380. domUtils.un(doc, 'mouseup', up)
  31381. }
  31382. function clearSelection() {
  31383. if (browser.ie) doc.selection.clear()
  31384. else window.getSelection().removeAllRanges()
  31385. }
  31386. this.enableScale = function () {
  31387. //trace:2868
  31388. if (editor.queryCommandState('source') == 1) return
  31389. scale.style.display = ''
  31390. this.scaleEnabled = true
  31391. domUtils.on(scale, 'mousedown', down)
  31392. }
  31393. this.disableScale = function () {
  31394. scale.style.display = 'none'
  31395. this.scaleEnabled = false
  31396. domUtils.un(scale, 'mousedown', down)
  31397. }
  31398. },
  31399. isFullScreen: function () {
  31400. return this._fullscreen
  31401. },
  31402. postRender: function () {
  31403. UIBase.prototype.postRender.call(this)
  31404. for (var i = 0; i < this.toolbars.length; i++) {
  31405. this.toolbars[i].postRender()
  31406. }
  31407. var me = this
  31408. var timerId,
  31409. domUtils = baidu.editor.dom.domUtils,
  31410. updateFullScreenTime = function () {
  31411. clearTimeout(timerId)
  31412. timerId = setTimeout(function () {
  31413. me._updateFullScreen()
  31414. })
  31415. }
  31416. domUtils.on(window, 'resize', updateFullScreenTime)
  31417. me.addListener('destroy', function () {
  31418. domUtils.un(window, 'resize', updateFullScreenTime)
  31419. clearTimeout(timerId)
  31420. })
  31421. },
  31422. showToolbarMsg: function (msg, flag) {
  31423. this.getDom('toolbarmsg_label').innerHTML = msg
  31424. this.getDom('toolbarmsg').style.display = ''
  31425. //
  31426. if (!flag) {
  31427. var w = this.getDom('upload_dialog')
  31428. w.style.display = 'none'
  31429. }
  31430. },
  31431. hideToolbarMsg: function () {
  31432. this.getDom('toolbarmsg').style.display = 'none'
  31433. },
  31434. mapUrl: function (url) {
  31435. return url
  31436. ? url.replace('~/', this.editor.options.UEDITOR_HOME_URL || '')
  31437. : ''
  31438. },
  31439. triggerLayout: function () {
  31440. var dom = this.getDom()
  31441. if (dom.style.zoom == '1') {
  31442. dom.style.zoom = '100%'
  31443. } else {
  31444. dom.style.zoom = '1'
  31445. }
  31446. },
  31447. }
  31448. utils.inherits(EditorUI, baidu.editor.ui.UIBase)
  31449. var instances = {}
  31450. UE.ui.Editor = function (options) {
  31451. var editor = new UE.Editor(options)
  31452. editor.options.editor = editor
  31453. utils.loadFile(document, {
  31454. href:
  31455. editor.options.themePath + editor.options.theme + '/css/ueditor.css',
  31456. tag: 'link',
  31457. type: 'text/css',
  31458. rel: 'stylesheet',
  31459. })
  31460. var oldRender = editor.render
  31461. editor.render = function (holder) {
  31462. if (holder.constructor === String) {
  31463. editor.key = holder
  31464. instances[holder] = editor
  31465. }
  31466. utils.domReady(function () {
  31467. editor.langIsReady
  31468. ? renderUI()
  31469. : editor.addListener('langReady', renderUI)
  31470. function renderUI() {
  31471. editor.setOpt({
  31472. labelMap: editor.options.labelMap || editor.getLang('labelMap'),
  31473. })
  31474. new EditorUI(editor.options)
  31475. if (holder) {
  31476. if (holder.constructor === String) {
  31477. holder = document.getElementById(holder)
  31478. }
  31479. holder &&
  31480. holder.getAttribute('name') &&
  31481. (editor.options.textarea = holder.getAttribute('name'))
  31482. if (holder && /script|textarea/gi.test(holder.tagName)) {
  31483. var newDiv = document.createElement('div')
  31484. holder.parentNode.insertBefore(newDiv, holder)
  31485. var cont = holder.value || holder.innerHTML
  31486. editor.options.initialContent = /^[\t\r\n ]*$/.test(cont)
  31487. ? editor.options.initialContent
  31488. : cont
  31489. .replace(/>[\n\r\t]+([ ]{4})+/g, '>')
  31490. .replace(/[\n\r\t]+([ ]{4})+</g, '<')
  31491. .replace(/>[\n\r\t]+</g, '><')
  31492. holder.className && (newDiv.className = holder.className)
  31493. holder.style.cssText &&
  31494. (newDiv.style.cssText = holder.style.cssText)
  31495. if (/textarea/i.test(holder.tagName)) {
  31496. editor.textarea = holder
  31497. editor.textarea.style.display = 'none'
  31498. } else {
  31499. holder.parentNode.removeChild(holder)
  31500. }
  31501. if (holder.id) {
  31502. newDiv.id = holder.id
  31503. domUtils.removeAttributes(holder, 'id')
  31504. }
  31505. holder = newDiv
  31506. holder.innerHTML = ''
  31507. }
  31508. }
  31509. domUtils.addClass(holder, 'edui-' + editor.options.theme)
  31510. editor.ui.render(holder)
  31511. var opt = editor.options
  31512. //给实例添加一个编辑器的容器引用
  31513. editor.container = editor.ui.getDom()
  31514. var parents = domUtils.findParents(holder, true)
  31515. var displays = []
  31516. for (var i = 0, ci; (ci = parents[i]); i++) {
  31517. displays[i] = ci.style.display
  31518. ci.style.display = 'block'
  31519. }
  31520. if (opt.initialFrameWidth) {
  31521. opt.minFrameWidth = opt.initialFrameWidth
  31522. } else {
  31523. opt.minFrameWidth = opt.initialFrameWidth = holder.offsetWidth
  31524. var styleWidth = holder.style.width
  31525. if (/%$/.test(styleWidth)) {
  31526. opt.initialFrameWidth = styleWidth
  31527. }
  31528. }
  31529. if (opt.initialFrameHeight) {
  31530. opt.minFrameHeight = opt.initialFrameHeight
  31531. } else {
  31532. opt.initialFrameHeight = opt.minFrameHeight = holder.offsetHeight
  31533. }
  31534. for (var i = 0, ci; (ci = parents[i]); i++) {
  31535. ci.style.display = displays[i]
  31536. }
  31537. //编辑器最外容器设置了高度,会导致,编辑器不占位
  31538. //todo 先去掉,没有找到原因
  31539. if (holder.style.height) {
  31540. holder.style.height = ''
  31541. }
  31542. editor.container.style.width =
  31543. opt.initialFrameWidth +
  31544. (/%$/.test(opt.initialFrameWidth) ? '' : 'px')
  31545. editor.container.style.zIndex = opt.zIndex
  31546. oldRender.call(editor, editor.ui.getDom('iframeholder'))
  31547. editor.fireEvent('afteruiready')
  31548. }
  31549. })
  31550. }
  31551. return editor
  31552. }
  31553. /**
  31554. * @file
  31555. * @name UE
  31556. * @short UE
  31557. * @desc UEditor的顶部命名空间
  31558. */
  31559. /**
  31560. * @name getEditor
  31561. * @since 1.2.4+
  31562. * @grammar UE.getEditor(id,[opt]) => Editor实例
  31563. * @desc 提供一个全局的方法得到编辑器实例
  31564. *
  31565. * * ''id'' 放置编辑器的容器id, 如果容器下的编辑器已经存在,就直接返回
  31566. * * ''opt'' 编辑器的可选参数
  31567. * @example
  31568. * UE.getEditor('containerId',{onready:function(){//创建一个编辑器实例
  31569. * this.setContent('hello')
  31570. * }});
  31571. * UE.getEditor('containerId'); //返回刚创建的实例
  31572. *
  31573. */
  31574. UE.getEditor = function (id, opt) {
  31575. var editor = instances[id]
  31576. if (!editor) {
  31577. editor = instances[id] = new UE.ui.Editor(opt)
  31578. editor.render(id)
  31579. }
  31580. return editor
  31581. }
  31582. UE.delEditor = function (id) {
  31583. var editor
  31584. if ((editor = instances[id])) {
  31585. editor.key && editor.destroy()
  31586. delete instances[id]
  31587. }
  31588. }
  31589. UE.registerUI = function (uiName, fn, index, editorId) {
  31590. utils.each(uiName.split(/\s+/), function (name) {
  31591. UE._customizeUI[name] = {
  31592. id: editorId,
  31593. execFn: fn,
  31594. index: index,
  31595. }
  31596. })
  31597. }
  31598. })()
  31599. // adapter/message.js
  31600. UE.registerUI('message', function (editor) {
  31601. var editorui = baidu.editor.ui
  31602. var Message = editorui.Message
  31603. var holder
  31604. var _messageItems = []
  31605. var me = editor
  31606. me.addListener('ready', function () {
  31607. holder = document.getElementById(me.ui.id + '_message_holder')
  31608. updateHolderPos()
  31609. // HaoChuan9421
  31610. // setTimeout(function(){
  31611. // updateHolderPos();
  31612. // }, 500);
  31613. })
  31614. me.addListener('showmessage', function (type, opt) {
  31615. opt = utils.isString(opt)
  31616. ? {
  31617. content: opt,
  31618. }
  31619. : opt
  31620. var message = new Message({
  31621. timeout: opt.timeout,
  31622. type: opt.type,
  31623. content: opt.content,
  31624. keepshow: opt.keepshow,
  31625. editor: me,
  31626. }),
  31627. mid = opt.id || 'msg_' + (+new Date()).toString(36)
  31628. message.render(holder)
  31629. _messageItems[mid] = message
  31630. message.reset(opt)
  31631. updateHolderPos()
  31632. return mid
  31633. })
  31634. me.addListener('updatemessage', function (type, id, opt) {
  31635. opt = utils.isString(opt)
  31636. ? {
  31637. content: opt,
  31638. }
  31639. : opt
  31640. var message = _messageItems[id]
  31641. message.render(holder)
  31642. message && message.reset(opt)
  31643. })
  31644. me.addListener('hidemessage', function (type, id) {
  31645. var message = _messageItems[id]
  31646. message && message.hide()
  31647. })
  31648. function updateHolderPos() {
  31649. var toolbarbox = me.ui.getDom('toolbarbox')
  31650. if (toolbarbox) {
  31651. holder.style.top = toolbarbox.offsetHeight + 3 + 'px'
  31652. }
  31653. holder.style.zIndex =
  31654. Math.max(me.options.zIndex, me.iframe.style.zIndex) + 1
  31655. }
  31656. })
  31657. // adapter/autosave.js
  31658. UE.registerUI('autosave', function (editor) {
  31659. var timer = null,
  31660. uid = null
  31661. editor.on('afterautosave', function () {
  31662. clearTimeout(timer)
  31663. timer = setTimeout(function () {
  31664. if (uid) {
  31665. editor.trigger('hidemessage', uid)
  31666. }
  31667. uid = editor.trigger('showmessage', {
  31668. content: editor.getLang('autosave.success'),
  31669. timeout: 2000,
  31670. })
  31671. }, 2000)
  31672. })
  31673. })
  31674. })()