心理咨询网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ueditor.all.js 989KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946
  1. /*!
  2. * UEditorPlus
  3. * version: 2.0.0
  4. */
  5. (function(){
  6. // editor.js
  7. UEDITOR_CONFIG = window.UEDITOR_CONFIG || {};
  8. var baidu = window.baidu || {};
  9. window.baidu = baidu;
  10. window.UE = baidu.editor = {
  11. plugins: {},
  12. commands: {},
  13. instants: {},
  14. I18N: {},
  15. _customizeUI: {},
  16. version: "3.0.0"
  17. };
  18. var dom = (UE.dom = {});
  19. // core/browser.js
  20. /**
  21. * 浏览器判断模块
  22. * @file
  23. * @module UE.browser
  24. * @since 1.2.6.1
  25. */
  26. /**
  27. * 提供浏览器检测的模块
  28. * @unfile
  29. * @module UE.browser
  30. */
  31. var browser = (UE.browser = (function() {
  32. var agent = navigator.userAgent.toLowerCase(),
  33. opera = window.opera,
  34. browser = {
  35. /**
  36. * @property {boolean} ie 检测当前浏览器是否为IE
  37. * @example
  38. * ```javascript
  39. * if ( UE.browser.ie ) {
  40. * console.log( '当前浏览器是IE' );
  41. * }
  42. * ```
  43. */
  44. ie: /(msie\s|trident.*rv:)([\w.]+)/i.test(agent),
  45. /**
  46. * @property {boolean} opera 检测当前浏览器是否为Opera
  47. * @example
  48. * ```javascript
  49. * if ( UE.browser.opera ) {
  50. * console.log( '当前浏览器是Opera' );
  51. * }
  52. * ```
  53. */
  54. opera: !!opera && opera.version,
  55. /**
  56. * @property {boolean} webkit 检测当前浏览器是否是webkit内核的浏览器
  57. * @example
  58. * ```javascript
  59. * if ( UE.browser.webkit ) {
  60. * console.log( '当前浏览器是webkit内核浏览器' );
  61. * }
  62. * ```
  63. */
  64. webkit: agent.indexOf(" applewebkit/") > -1,
  65. /**
  66. * @property {boolean} mac 检测当前浏览器是否是运行在mac平台下
  67. * @example
  68. * ```javascript
  69. * if ( UE.browser.mac ) {
  70. * console.log( '当前浏览器运行在mac平台下' );
  71. * }
  72. * ```
  73. */
  74. mac: agent.indexOf("macintosh") > -1,
  75. /**
  76. * @property {boolean} quirks 检测当前浏览器是否处于“怪异模式”下
  77. * @example
  78. * ```javascript
  79. * if ( UE.browser.quirks ) {
  80. * console.log( '当前浏览器运行处于“怪异模式”' );
  81. * }
  82. * ```
  83. */
  84. quirks: document.compatMode == "BackCompat"
  85. };
  86. /**
  87. * @property {boolean} gecko 检测当前浏览器内核是否是gecko内核
  88. * @example
  89. * ```javascript
  90. * if ( UE.browser.gecko ) {
  91. * console.log( '当前浏览器内核是gecko内核' );
  92. * }
  93. * ```
  94. */
  95. browser.gecko =
  96. navigator.product == "Gecko" &&
  97. !browser.webkit &&
  98. !browser.opera &&
  99. !browser.ie;
  100. var version = 0;
  101. // Internet Explorer 6.0+
  102. if (browser.ie) {
  103. var v1 = agent.match(/(?:msie\s([\w.]+))/);
  104. var v2 = agent.match(/(?:trident.*rv:([\w.]+))/);
  105. if (v1 && v2 && v1[1] && v2[1]) {
  106. version = Math.max(v1[1] * 1, v2[1] * 1);
  107. } else if (v1 && v1[1]) {
  108. version = v1[1] * 1;
  109. } else if (v2 && v2[1]) {
  110. version = v2[1] * 1;
  111. } else {
  112. version = 0;
  113. }
  114. browser.ie11Compat = document.documentMode == 11;
  115. /**
  116. * @property { boolean } ie9Compat 检测浏览器模式是否为 IE9 兼容模式
  117. * @warning 如果浏览器不是IE, 则该值为undefined
  118. * @example
  119. * ```javascript
  120. * if ( UE.browser.ie9Compat ) {
  121. * console.log( '当前浏览器运行在IE9兼容模式下' );
  122. * }
  123. * ```
  124. */
  125. browser.ie9Compat = document.documentMode == 9;
  126. /**
  127. * @property { boolean } ie8 检测浏览器是否是IE8浏览器
  128. * @warning 如果浏览器不是IE, 则该值为undefined
  129. * @example
  130. * ```javascript
  131. * if ( UE.browser.ie8 ) {
  132. * console.log( '当前浏览器是IE8浏览器' );
  133. * }
  134. * ```
  135. */
  136. browser.ie8 = !!document.documentMode;
  137. /**
  138. * @property { boolean } ie8Compat 检测浏览器模式是否为 IE8 兼容模式
  139. * @warning 如果浏览器不是IE, 则该值为undefined
  140. * @example
  141. * ```javascript
  142. * if ( UE.browser.ie8Compat ) {
  143. * console.log( '当前浏览器运行在IE8兼容模式下' );
  144. * }
  145. * ```
  146. */
  147. browser.ie8Compat = document.documentMode == 8;
  148. /**
  149. * @property { boolean } ie7Compat 检测浏览器模式是否为 IE7 兼容模式
  150. * @warning 如果浏览器不是IE, 则该值为undefined
  151. * @example
  152. * ```javascript
  153. * if ( UE.browser.ie7Compat ) {
  154. * console.log( '当前浏览器运行在IE7兼容模式下' );
  155. * }
  156. * ```
  157. */
  158. browser.ie7Compat =
  159. (version == 7 && !document.documentMode) || document.documentMode == 7;
  160. /**
  161. * @property { boolean } ie6Compat 检测浏览器模式是否为 IE6 模式 或者怪异模式
  162. * @warning 如果浏览器不是IE, 则该值为undefined
  163. * @example
  164. * ```javascript
  165. * if ( UE.browser.ie6Compat ) {
  166. * console.log( '当前浏览器运行在IE6模式或者怪异模式下' );
  167. * }
  168. * ```
  169. */
  170. browser.ie6Compat = version < 7 || browser.quirks;
  171. browser.ie9above = version > 8;
  172. browser.ie9below = version < 9;
  173. browser.ie11above = version > 10;
  174. browser.ie11below = version < 11;
  175. }
  176. // Gecko.
  177. if (browser.gecko) {
  178. var geckoRelease = agent.match(/rv:([\d\.]+)/);
  179. if (geckoRelease) {
  180. geckoRelease = geckoRelease[1].split(".");
  181. version =
  182. geckoRelease[0] * 10000 +
  183. (geckoRelease[1] || 0) * 100 +
  184. (geckoRelease[2] || 0) * 1;
  185. }
  186. }
  187. /**
  188. * @property { Number } chrome 检测当前浏览器是否为Chrome, 如果是,则返回Chrome的大版本号
  189. * @warning 如果浏览器不是chrome, 则该值为undefined
  190. * @example
  191. * ```javascript
  192. * if ( UE.browser.chrome ) {
  193. * console.log( '当前浏览器是Chrome' );
  194. * }
  195. * ```
  196. */
  197. if (/chrome\/(\d+\.\d)/i.test(agent)) {
  198. browser.chrome = +RegExp["\x241"];
  199. }
  200. /**
  201. * @property { Number } safari 检测当前浏览器是否为Safari, 如果是,则返回Safari的大版本号
  202. * @warning 如果浏览器不是safari, 则该值为undefined
  203. * @example
  204. * ```javascript
  205. * if ( UE.browser.safari ) {
  206. * console.log( '当前浏览器是Safari' );
  207. * }
  208. * ```
  209. */
  210. if (
  211. /(\d+\.\d)?(?:\.\d)?\s+safari\/?(\d+\.\d+)?/i.test(agent) &&
  212. !/chrome/i.test(agent)
  213. ) {
  214. browser.safari = +(RegExp["\x241"] || RegExp["\x242"]);
  215. }
  216. // Opera 9.50+
  217. if (browser.opera) version = parseFloat(opera.version());
  218. // WebKit 522+ (Safari 3+)
  219. if (browser.webkit)
  220. version = parseFloat(agent.match(/ applewebkit\/(\d+)/)[1]);
  221. /**
  222. * @property { Number } version 检测当前浏览器版本号
  223. * @remind
  224. * <ul>
  225. * <li>IE系列返回值为5,6,7,8,9,10等</li>
  226. * <li>gecko系列会返回10900,158900等</li>
  227. * <li>webkit系列会返回其build号 (如 522等)</li>
  228. * </ul>
  229. * @example
  230. * ```javascript
  231. * console.log( '当前浏览器版本号是: ' + UE.browser.version );
  232. * ```
  233. */
  234. browser.version = version;
  235. /**
  236. * @property { boolean } isCompatible 检测当前浏览器是否能够与UEditor良好兼容
  237. * @example
  238. * ```javascript
  239. * if ( UE.browser.isCompatible ) {
  240. * console.log( '浏览器与UEditor能够良好兼容' );
  241. * }
  242. * ```
  243. */
  244. browser.isCompatible =
  245. !browser.mobile &&
  246. ((browser.ie && version >= 6) ||
  247. (browser.gecko && version >= 10801) ||
  248. (browser.opera && version >= 9.5) ||
  249. (browser.air && version >= 1) ||
  250. (browser.webkit && version >= 522) ||
  251. false);
  252. return browser;
  253. })());
  254. //快捷方式
  255. var ie = browser.ie,
  256. webkit = browser.webkit,
  257. gecko = browser.gecko,
  258. opera = browser.opera;
  259. // core/utils.js
  260. /**
  261. * 工具函数包
  262. * @file
  263. * @module UE.utils
  264. * @since 1.2.6.1
  265. */
  266. /**
  267. * UEditor封装使用的静态工具函数
  268. * @module UE.utils
  269. * @unfile
  270. */
  271. var utils = (UE.utils = {
  272. /**
  273. * 用给定的迭代器遍历对象
  274. * @method each
  275. * @param { Object } obj 需要遍历的对象
  276. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  277. * @example
  278. * ```javascript
  279. * var demoObj = {
  280. * key1: 1,
  281. * key2: 2
  282. * };
  283. *
  284. * //output: key1: 1, key2: 2
  285. * UE.utils.each( demoObj, funciton ( value, key ) {
  286. *
  287. * console.log( key + ":" + value );
  288. *
  289. * } );
  290. * ```
  291. */
  292. /**
  293. * 用给定的迭代器遍历数组或类数组对象
  294. * @method each
  295. * @param { Array } array 需要遍历的数组或者类数组
  296. * @param { Function } iterator 迭代器, 该方法接受两个参数, 第一个参数是当前所处理的value, 第二个参数是当前遍历对象的key
  297. * @example
  298. * ```javascript
  299. * var divs = document.getElmentByTagNames( "div" );
  300. *
  301. * //output: 0: DIV, 1: DIV ...
  302. * UE.utils.each( divs, funciton ( value, key ) {
  303. *
  304. * console.log( key + ":" + value.tagName );
  305. *
  306. * } );
  307. * ```
  308. */
  309. each: function(obj, iterator, context) {
  310. if (obj == null) return;
  311. if (obj.length === +obj.length) {
  312. for (var i = 0, l = obj.length; i < l; i++) {
  313. if (iterator.call(context, obj[i], i, obj) === false) return false;
  314. }
  315. } else {
  316. for (var key in obj) {
  317. if (obj.hasOwnProperty(key)) {
  318. if (iterator.call(context, obj[key], key, obj) === false)
  319. return false;
  320. }
  321. }
  322. }
  323. },
  324. /**
  325. * 以给定对象作为原型创建一个新对象
  326. * @method makeInstance
  327. * @param { Object } protoObject 该对象将作为新创建对象的原型
  328. * @return { Object } 新的对象, 该对象的原型是给定的protoObject对象
  329. * @example
  330. * ```javascript
  331. *
  332. * var protoObject = { sayHello: function () { console.log('Hello UEditor!'); } };
  333. *
  334. * var newObject = UE.utils.makeInstance( protoObject );
  335. * //output: Hello UEditor!
  336. * newObject.sayHello();
  337. * ```
  338. */
  339. makeInstance: function(obj) {
  340. var noop = new Function();
  341. noop.prototype = obj;
  342. obj = new noop();
  343. noop.prototype = null;
  344. return obj;
  345. },
  346. /**
  347. * 将source对象中的属性扩展到target对象上
  348. * @method extend
  349. * @remind 该方法将强制把source对象上的属性复制到target对象上
  350. * @see UE.utils.extend(Object,Object,Boolean)
  351. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  352. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  353. * @return { Object } 返回target对象
  354. * @example
  355. * ```javascript
  356. *
  357. * var target = { name: 'target', sex: 1 },
  358. * source = { name: 'source', age: 17 };
  359. *
  360. * UE.utils.extend( target, source );
  361. *
  362. * //output: { name: 'source', sex: 1, age: 17 }
  363. * console.log( target );
  364. *
  365. * ```
  366. */
  367. /**
  368. * 将source对象中的属性扩展到target对象上, 根据指定的isKeepTarget值决定是否保留目标对象中与
  369. * 源对象属性名相同的属性值。
  370. * @method extend
  371. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  372. * @param { Object } source 源对象, 该对象的属性会被附加到target对象上
  373. * @param { Boolean } isKeepTarget 是否保留目标对象中与源对象中属性名相同的属性
  374. * @return { Object } 返回target对象
  375. * @example
  376. * ```javascript
  377. *
  378. * var target = { name: 'target', sex: 1 },
  379. * source = { name: 'source', age: 17 };
  380. *
  381. * UE.utils.extend( target, source, true );
  382. *
  383. * //output: { name: 'target', sex: 1, age: 17 }
  384. * console.log( target );
  385. *
  386. * ```
  387. */
  388. extend: function(t, s, b) {
  389. if (s) {
  390. for (var k in s) {
  391. if (!b || !t.hasOwnProperty(k)) {
  392. t[k] = s[k];
  393. }
  394. }
  395. }
  396. return t;
  397. },
  398. /**
  399. * 将给定的多个对象的属性复制到目标对象target上
  400. * @method extend2
  401. * @remind 该方法将强制把源对象上的属性复制到target对象上
  402. * @remind 该方法支持两个及以上的参数, 从第二个参数开始, 其属性都会被复制到第一个参数上。 如果遇到同名的属性,
  403. * 将会覆盖掉之前的值。
  404. * @param { Object } target 目标对象, 新的属性将附加到该对象上
  405. * @param { Object... } source 源对象, 支持多个对象, 该对象的属性会被附加到target对象上
  406. * @return { Object } 返回target对象
  407. * @example
  408. * ```javascript
  409. *
  410. * var target = {},
  411. * source1 = { name: 'source', age: 17 },
  412. * source2 = { title: 'dev' };
  413. *
  414. * UE.utils.extend2( target, source1, source2 );
  415. *
  416. * //output: { name: 'source', age: 17, title: 'dev' }
  417. * console.log( target );
  418. *
  419. * ```
  420. */
  421. extend2: function(t) {
  422. var a = arguments;
  423. for (var i = 1; i < a.length; i++) {
  424. var x = a[i];
  425. for (var k in x) {
  426. if (!t.hasOwnProperty(k)) {
  427. t[k] = x[k];
  428. }
  429. }
  430. }
  431. return t;
  432. },
  433. /**
  434. * 模拟继承机制, 使得subClass继承自superClass
  435. * @method inherits
  436. * @param { Object } subClass 子类对象
  437. * @param { Object } superClass 超类对象
  438. * @warning 该方法只能让subClass继承超类的原型, subClass对象自身的属性和方法不会被继承
  439. * @return { Object } 继承superClass后的子类对象
  440. * @example
  441. * ```javascript
  442. * function SuperClass(){
  443. * this.name = "小李";
  444. * }
  445. *
  446. * SuperClass.prototype = {
  447. * hello:function(str){
  448. * console.log(this.name + str);
  449. * }
  450. * }
  451. *
  452. * function SubClass(){
  453. * this.name = "小张";
  454. * }
  455. *
  456. * UE.utils.inherits(SubClass,SuperClass);
  457. *
  458. * var sub = new SubClass();
  459. * //output: '小张早上好!
  460. * sub.hello("早上好!");
  461. * ```
  462. */
  463. inherits: function(subClass, superClass) {
  464. var oldP = subClass.prototype,
  465. newP = utils.makeInstance(superClass.prototype);
  466. utils.extend(newP, oldP, true);
  467. subClass.prototype = newP;
  468. return (newP.constructor = subClass);
  469. },
  470. /**
  471. * 用指定的context对象作为函数fn的上下文
  472. * @method bind
  473. * @param { Function } fn 需要绑定上下文的函数对象
  474. * @param { Object } content 函数fn新的上下文对象
  475. * @return { Function } 一个新的函数, 该函数作为原始函数fn的代理, 将完成fn的上下文调换工作。
  476. * @example
  477. * ```javascript
  478. *
  479. * var name = 'window',
  480. * newTest = null;
  481. *
  482. * function test () {
  483. * console.log( this.name );
  484. * }
  485. *
  486. * newTest = UE.utils.bind( test, { name: 'object' } );
  487. *
  488. * //output: object
  489. * newTest();
  490. *
  491. * //output: window
  492. * test();
  493. *
  494. * ```
  495. */
  496. bind: function(fn, context) {
  497. return function() {
  498. return fn.apply(context, arguments);
  499. };
  500. },
  501. /**
  502. * 创建延迟指定时间后执行的函数fn
  503. * @method defer
  504. * @param { Function } fn 需要延迟执行的函数对象
  505. * @param { int } delay 延迟的时间, 单位是毫秒
  506. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  507. * 而不能保证刚好到达延迟时间时执行。
  508. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  509. * @example
  510. * ```javascript
  511. * var start = 0;
  512. *
  513. * function test(){
  514. * console.log( new Date() - start );
  515. * }
  516. *
  517. * var testDefer = UE.utils.defer( test, 1000 );
  518. * //
  519. * start = new Date();
  520. * //output: (大约在1000毫秒之后输出) 1000
  521. * testDefer();
  522. * ```
  523. */
  524. /**
  525. * 创建延迟指定时间后执行的函数fn, 如果在延迟时间内再次执行该方法, 将会根据指定的exclusion的值,
  526. * 决定是否取消前一次函数的执行, 如果exclusion的值为true, 则取消执行,反之,将继续执行前一个方法。
  527. * @method defer
  528. * @param { Function } fn 需要延迟执行的函数对象
  529. * @param { int } delay 延迟的时间, 单位是毫秒
  530. * @param { Boolean } exclusion 如果在延迟时间内再次执行该函数,该值将决定是否取消执行前一次函数的执行,
  531. * 值为true表示取消执行, 反之则将在执行前一次函数之后才执行本次函数调用。
  532. * @warning 该方法的时间控制是不精确的,仅仅只能保证函数的执行是在给定的时间之后,
  533. * 而不能保证刚好到达延迟时间时执行。
  534. * @return { Function } 目标函数fn的代理函数, 只有执行该函数才能起到延时效果
  535. * @example
  536. * ```javascript
  537. *
  538. * function test(){
  539. * console.log(1);
  540. * }
  541. *
  542. * var testDefer = UE.utils.defer( test, 1000, true );
  543. *
  544. * //output: (两次调用仅有一次输出) 1
  545. * testDefer();
  546. * testDefer();
  547. * ```
  548. */
  549. defer: function(fn, delay, exclusion) {
  550. var timerID;
  551. return function() {
  552. if (exclusion) {
  553. clearTimeout(timerID);
  554. }
  555. timerID = setTimeout(fn, delay);
  556. };
  557. },
  558. /**
  559. * 获取元素item在数组array中首次出现的位置, 如果未找到item, 则返回-1
  560. * @method indexOf
  561. * @remind 该方法的匹配过程使用的是恒等“===”
  562. * @param { Array } array 需要查找的数组对象
  563. * @param { * } item 需要在目标数组中查找的值
  564. * @return { int } 返回item在目标数组array中首次出现的位置, 如果在数组中未找到item, 则返回-1
  565. * @example
  566. * ```javascript
  567. * var item = 1,
  568. * arr = [ 3, 4, 6, 8, 1, 1, 2 ];
  569. *
  570. * //output: 4
  571. * console.log( UE.utils.indexOf( arr, item ) );
  572. * ```
  573. */
  574. /**
  575. * 获取元素item数组array中首次出现的位置, 如果未找到item, 则返回-1。通过start的值可以指定搜索的起始位置。
  576. * @method indexOf
  577. * @remind 该方法的匹配过程使用的是恒等“===”
  578. * @param { Array } array 需要查找的数组对象
  579. * @param { * } item 需要在目标数组中查找的值
  580. * @param { int } start 搜索的起始位置
  581. * @return { int } 返回item在目标数组array中的start位置之后首次出现的位置, 如果在数组中未找到item, 则返回-1
  582. * @example
  583. * ```javascript
  584. * var item = 1,
  585. * arr = [ 3, 4, 6, 8, 1, 2, 8, 3, 2, 1, 1, 4 ];
  586. *
  587. * //output: 9
  588. * console.log( UE.utils.indexOf( arr, item, 5 ) );
  589. * ```
  590. */
  591. indexOf: function(array, item, start) {
  592. var index = -1;
  593. start = this.isNumber(start) ? start : 0;
  594. this.each(array, function(v, i) {
  595. if (i >= start && v === item) {
  596. index = i;
  597. return false;
  598. }
  599. });
  600. return index;
  601. },
  602. /**
  603. * 移除数组array中所有的元素item
  604. * @method removeItem
  605. * @param { Array } array 要移除元素的目标数组
  606. * @param { * } item 将要被移除的元素
  607. * @remind 该方法的匹配过程使用的是恒等“===”
  608. * @example
  609. * ```javascript
  610. * var arr = [ 4, 5, 7, 1, 3, 4, 6 ];
  611. *
  612. * UE.utils.removeItem( arr, 4 );
  613. * //output: [ 5, 7, 1, 3, 6 ]
  614. * console.log( arr );
  615. *
  616. * ```
  617. */
  618. removeItem: function(array, item) {
  619. for (var i = 0, l = array.length; i < l; i++) {
  620. if (array[i] === item) {
  621. array.splice(i, 1);
  622. i--;
  623. }
  624. }
  625. },
  626. /**
  627. * 删除字符串str的首尾空格
  628. * @method trim
  629. * @param { String } str 需要删除首尾空格的字符串
  630. * @return { String } 删除了首尾的空格后的字符串
  631. * @example
  632. * ```javascript
  633. *
  634. * var str = " UEdtior ";
  635. *
  636. * //output: 9
  637. * console.log( str.length );
  638. *
  639. * //output: 7
  640. * console.log( UE.utils.trim( " UEdtior " ).length );
  641. *
  642. * //output: 9
  643. * console.log( str.length );
  644. *
  645. * ```
  646. */
  647. trim: function(str) {
  648. return str.replace(/(^[ \t\n\r]+)|([ \t\n\r]+$)/g, "");
  649. },
  650. /**
  651. * 将字符串str以','分隔成数组后,将该数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  652. * @method listToMap
  653. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  654. * @param { String } str 该字符串将被以','分割为数组, 然后进行转化
  655. * @return { Object } 转化之后的hash对象
  656. * @example
  657. * ```javascript
  658. *
  659. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  660. * console.log( UE.utils.listToMap( 'UEdtior,Hello' ) );
  661. *
  662. * ```
  663. */
  664. /**
  665. * 将字符串数组转换成哈希对象, 其生成的hash对象的key为数组中的元素, value为1
  666. * @method listToMap
  667. * @warning 该方法在生成的hash对象中,会为每一个key同时生成一个另一个全大写的key。
  668. * @param { Array } arr 字符串数组
  669. * @return { Object } 转化之后的hash对象
  670. * @example
  671. * ```javascript
  672. *
  673. * //output: Object {UEdtior: 1, UEDTIOR: 1, Hello: 1, HELLO: 1}
  674. * console.log( UE.utils.listToMap( [ 'UEdtior', 'Hello' ] ) );
  675. *
  676. * ```
  677. */
  678. listToMap: function(list) {
  679. if (!list) return {};
  680. list = utils.isArray(list) ? list : list.split(",");
  681. for (var i = 0, ci, obj = {}; (ci = list[i++]); ) {
  682. obj[ci.toUpperCase()] = obj[ci] = 1;
  683. }
  684. return obj;
  685. },
  686. /**
  687. * 将str中的html符号转义,将转义“',&,<,",>,”,“”七个字符
  688. * @method unhtml
  689. * @param { String } str 需要转义的字符串
  690. * @return { String } 转义后的字符串
  691. * @example
  692. * ```javascript
  693. * var html = '<body>&</body>';
  694. *
  695. * //output: &lt;body&gt;&amp;&lt;/body&gt;
  696. * console.log( UE.utils.unhtml( html ) );
  697. *
  698. * ```
  699. */
  700. unhtml: function(str, reg) {
  701. return str
  702. ? str.replace(
  703. reg || /[&<">'](?:(amp|lt|ldquo|rdquo|quot|gt|#39|nbsp|#\d+);)?/g,
  704. function(a, b) {
  705. if (b) {
  706. return a;
  707. } else {
  708. return {
  709. "<": "&lt;",
  710. "&": "&amp;",
  711. '"': "&quot;",
  712. "“": "&ldquo;",
  713. "”": "&rdquo;",
  714. ">": "&gt;",
  715. "'": "&#39;"
  716. }[a];
  717. }
  718. }
  719. )
  720. : "";
  721. },
  722. /**
  723. * 将str中的转义字符还原成html字符
  724. * @see UE.utils.unhtml(String);
  725. * @method html
  726. * @param { String } str 需要逆转义的字符串
  727. * @return { String } 逆转义后的字符串
  728. * @example
  729. * ```javascript
  730. *
  731. * var str = '&lt;body&gt;&amp;&lt;/body&gt;';
  732. *
  733. * //output: <body>&</body>
  734. * console.log( UE.utils.html( str ) );
  735. *
  736. * ```
  737. */
  738. html: function(str) {
  739. return str
  740. ? str.replace(/&((g|l|quo|ldquo|rdquo)t|amp|#39|nbsp);/g, function(m) {
  741. return {
  742. "&lt;": "<",
  743. "&amp;": "&",
  744. "&quot;": '"',
  745. "&ldquo;": "“",
  746. "&rdquo;": "”",
  747. "&gt;": ">",
  748. "&#39;": "'",
  749. "&nbsp;": " "
  750. }[m];
  751. })
  752. : "";
  753. },
  754. /**
  755. * 将css样式转换为驼峰的形式
  756. * @method cssStyleToDomStyle
  757. * @param { String } cssName 需要转换的css样式名
  758. * @return { String } 转换成驼峰形式后的css样式名
  759. * @example
  760. * ```javascript
  761. *
  762. * var str = 'border-top';
  763. *
  764. * //output: borderTop
  765. * console.log( UE.utils.cssStyleToDomStyle( str ) );
  766. *
  767. * ```
  768. */
  769. cssStyleToDomStyle: (function() {
  770. var test = document.createElement("div").style,
  771. cache = {
  772. float: test.cssFloat !== undefined
  773. ? "cssFloat"
  774. : test.styleFloat !== undefined ? "styleFloat" : "float"
  775. };
  776. return function(cssName) {
  777. return (
  778. cache[cssName] ||
  779. (cache[cssName] = cssName.toLowerCase().replace(/-./g, function(match) {
  780. return match.charAt(1).toUpperCase();
  781. }))
  782. );
  783. };
  784. })(),
  785. /**
  786. * 动态加载文件到doc中
  787. * @method loadFile
  788. * @param { DomDocument } document 需要加载资源文件的文档对象
  789. * @param { Object } options 加载资源文件的属性集合, 取值请参考代码示例
  790. * @example
  791. * ```javascript
  792. *
  793. * UE.utils.loadFile( document, {
  794. * src:"test.js",
  795. * tag:"script",
  796. * type:"text/javascript",
  797. * defer:"defer"
  798. * } );
  799. *
  800. * ```
  801. */
  802. /**
  803. * 动态加载文件到doc中,加载成功后执行的回调函数fn
  804. * @method loadFile
  805. * @param { DomDocument } document 需要加载资源文件的文档对象
  806. * @param { Object } options 加载资源文件的属性集合, 该集合支持的值是script标签和style标签支持的所有属性。
  807. * @param { Function } fn 资源文件加载成功之后执行的回调
  808. * @warning 对于在同一个文档中多次加载同一URL的文件, 该方法会在第一次加载之后缓存该请求,
  809. * 在此之后的所有同一URL的请求, 将会直接触发回调。
  810. * @example
  811. * ```javascript
  812. *
  813. * UE.utils.loadFile( document, {
  814. * src:"test.js",
  815. * tag:"script",
  816. * type:"text/javascript",
  817. * defer:"defer"
  818. * }, function () {
  819. * console.log('加载成功');
  820. * } );
  821. *
  822. * ```
  823. */
  824. loadFile: (function() {
  825. var tmpList = [];
  826. function getItem(doc, obj) {
  827. try {
  828. for (var i = 0, ci; (ci = tmpList[i++]); ) {
  829. if (ci.doc === doc && ci.url == (obj.src || obj.href)) {
  830. return ci;
  831. }
  832. }
  833. } catch (e) {
  834. return null;
  835. }
  836. }
  837. return function(doc, obj, fn) {
  838. var item = getItem(doc, obj);
  839. if (item) {
  840. if (item.ready) {
  841. fn && fn();
  842. } else {
  843. item.funs.push(fn);
  844. }
  845. return;
  846. }
  847. tmpList.push({
  848. doc: doc,
  849. url: obj.src || obj.href,
  850. funs: [fn]
  851. });
  852. if (!doc.body) {
  853. var html = [];
  854. for (var p in obj) {
  855. if (p == "tag") continue;
  856. html.push(p + '="' + obj[p] + '"');
  857. }
  858. doc.write(
  859. "<" + obj.tag + " " + html.join(" ") + " ></" + obj.tag + ">"
  860. );
  861. return;
  862. }
  863. if (obj.id && doc.getElementById(obj.id)) {
  864. return;
  865. }
  866. var element = doc.createElement(obj.tag);
  867. delete obj.tag;
  868. for (var p in obj) {
  869. element.setAttribute(p, obj[p]);
  870. }
  871. element.onload = element.onreadystatechange = function() {
  872. if (!this.readyState || /loaded|complete/.test(this.readyState)) {
  873. item = getItem(doc, obj);
  874. if (item.funs.length > 0) {
  875. item.ready = 1;
  876. for (var fi; (fi = item.funs.pop()); ) {
  877. fi();
  878. }
  879. }
  880. element.onload = element.onreadystatechange = null;
  881. }
  882. };
  883. element.onerror = function() {
  884. throw Error(
  885. "The load " +
  886. (obj.href || obj.src) +
  887. " fails,check the url settings of file ueditor.config.js "
  888. );
  889. };
  890. doc.getElementsByTagName("head")[0].appendChild(element);
  891. };
  892. })(),
  893. /**
  894. * 判断obj对象是否为空
  895. * @method isEmptyObject
  896. * @param { * } obj 需要判断的对象
  897. * @remind 如果判断的对象是NULL, 将直接返回true, 如果是数组且为空, 返回true, 如果是字符串, 且字符串为空,
  898. * 返回true, 如果是普通对象, 且该对象没有任何实例属性, 返回true
  899. * @return { Boolean } 对象是否为空
  900. * @example
  901. * ```javascript
  902. *
  903. * //output: true
  904. * console.log( UE.utils.isEmptyObject( {} ) );
  905. *
  906. * //output: true
  907. * console.log( UE.utils.isEmptyObject( [] ) );
  908. *
  909. * //output: true
  910. * console.log( UE.utils.isEmptyObject( "" ) );
  911. *
  912. * //output: false
  913. * console.log( UE.utils.isEmptyObject( { key: 1 } ) );
  914. *
  915. * //output: false
  916. * console.log( UE.utils.isEmptyObject( [1] ) );
  917. *
  918. * //output: false
  919. * console.log( UE.utils.isEmptyObject( "1" ) );
  920. *
  921. * ```
  922. */
  923. isEmptyObject: function(obj) {
  924. if (obj == null) return true;
  925. if (this.isArray(obj) || this.isString(obj)) return obj.length === 0;
  926. for (var key in obj) if (obj.hasOwnProperty(key)) return false;
  927. return true;
  928. },
  929. /**
  930. * 把rgb格式的颜色值转换成16进制格式
  931. * @method fixColor
  932. * @param { String } rgb格式的颜色值
  933. * @param { String }
  934. * @example
  935. * rgb(255,255,255) => "#ffffff"
  936. */
  937. fixColor: function(name, value) {
  938. if (/color/i.test(name) && /rgba?/.test(value)) {
  939. var array = value.split(",");
  940. if (array.length > 3) return "";
  941. value = "#";
  942. for (var i = 0, color; (color = array[i++]); ) {
  943. color = parseInt(color.replace(/[^\d]/gi, ""), 10).toString(16);
  944. value += color.length == 1 ? "0" + color : color;
  945. }
  946. value = value.toUpperCase();
  947. }
  948. return value;
  949. },
  950. /**
  951. * 只针对border,padding,margin做了处理,因为性能问题
  952. * @public
  953. * @function
  954. * @param {String} val style字符串
  955. */
  956. optCss: function(val) {
  957. var padding, margin, border;
  958. val = val.replace(/(padding|margin|border)\-([^:]+):([^;]+);?/gi, function(
  959. str,
  960. key,
  961. name,
  962. val
  963. ) {
  964. if (val.split(" ").length == 1) {
  965. switch (key) {
  966. case "padding":
  967. !padding && (padding = {});
  968. padding[name] = val;
  969. return "";
  970. case "margin":
  971. !margin && (margin = {});
  972. margin[name] = val;
  973. return "";
  974. case "border":
  975. return val == "initial" ? "" : str;
  976. }
  977. }
  978. return str;
  979. });
  980. function opt(obj, name) {
  981. if (!obj) {
  982. return "";
  983. }
  984. var t = obj.top,
  985. b = obj.bottom,
  986. l = obj.left,
  987. r = obj.right,
  988. val = "";
  989. if (!t || !l || !b || !r) {
  990. for (var p in obj) {
  991. val += ";" + name + "-" + p + ":" + obj[p] + ";";
  992. }
  993. } else {
  994. val +=
  995. ";" +
  996. name +
  997. ":" +
  998. (t == b && b == l && l == r
  999. ? t
  1000. : t == b && l == r
  1001. ? t + " " + l
  1002. : l == r
  1003. ? t + " " + l + " " + b
  1004. : t + " " + r + " " + b + " " + l) +
  1005. ";";
  1006. }
  1007. return val;
  1008. }
  1009. val += opt(padding, "padding") + opt(margin, "margin");
  1010. return val
  1011. .replace(/^[ \n\r\t;]*|[ \n\r\t]*$/, "")
  1012. .replace(/;([ \n\r\t]+)|\1;/g, ";")
  1013. .replace(/(&((l|g)t|quot|#39))?;{2,}/g, function(a, b) {
  1014. return b ? b + ";;" : ";";
  1015. });
  1016. },
  1017. /**
  1018. * 克隆对象
  1019. * @method clone
  1020. * @param { Object } source 源对象
  1021. * @return { Object } source的一个副本
  1022. */
  1023. /**
  1024. * 深度克隆对象,将source的属性克隆到target对象, 会覆盖target重名的属性。
  1025. * @method clone
  1026. * @param { Object } source 源对象
  1027. * @param { Object } target 目标对象
  1028. * @return { Object } 附加了source对象所有属性的target对象
  1029. */
  1030. clone: function(source, target) {
  1031. var tmp;
  1032. target = target || {};
  1033. for (var i in source) {
  1034. if (source.hasOwnProperty(i)) {
  1035. tmp = source[i];
  1036. if (typeof tmp == "object") {
  1037. target[i] = utils.isArray(tmp) ? [] : {};
  1038. utils.clone(source[i], target[i]);
  1039. } else {
  1040. target[i] = tmp;
  1041. }
  1042. }
  1043. }
  1044. return target;
  1045. },
  1046. /**
  1047. * 把cm/pt为单位的值转换为px为单位的值
  1048. * @method transUnitToPx
  1049. * @param { String } 待转换的带单位的字符串
  1050. * @return { String } 转换为px为计量单位的值的字符串
  1051. * @example
  1052. * ```javascript
  1053. *
  1054. * //output: 500px
  1055. * console.log( UE.utils.transUnitToPx( '20cm' ) );
  1056. *
  1057. * //output: 27px
  1058. * console.log( UE.utils.transUnitToPx( '20pt' ) );
  1059. *
  1060. * ```
  1061. */
  1062. transUnitToPx: function(val) {
  1063. if (!/(pt|cm)/.test(val)) {
  1064. return val;
  1065. }
  1066. var unit;
  1067. val.replace(/([\d.]+)(\w+)/, function(str, v, u) {
  1068. val = v;
  1069. unit = u;
  1070. });
  1071. switch (unit) {
  1072. case "cm":
  1073. val = parseFloat(val) * 25;
  1074. break;
  1075. case "pt":
  1076. val = Math.round(parseFloat(val) * 96 / 72);
  1077. }
  1078. return val + (val ? "px" : "");
  1079. },
  1080. /**
  1081. * 在dom树ready之后执行给定的回调函数
  1082. * @method domReady
  1083. * @remind 如果在执行该方法的时候, dom树已经ready, 那么回调函数将立刻执行
  1084. * @param { Function } fn dom树ready之后的回调函数
  1085. * @example
  1086. * ```javascript
  1087. *
  1088. * UE.utils.domReady( function () {
  1089. *
  1090. * console.log('123');
  1091. *
  1092. * } );
  1093. *
  1094. * ```
  1095. */
  1096. domReady: (function() {
  1097. var fnArr = [];
  1098. function doReady(doc) {
  1099. //确保onready只执行一次
  1100. doc.isReady = true;
  1101. for (var ci; (ci = fnArr.pop()); ci()) {}
  1102. }
  1103. return function(onready, win) {
  1104. win = win || window;
  1105. var doc = win.document;
  1106. onready && fnArr.push(onready);
  1107. if (doc.readyState === "complete") {
  1108. doReady(doc);
  1109. } else {
  1110. doc.isReady && doReady(doc);
  1111. if (browser.ie && browser.version != 11) {
  1112. (function() {
  1113. if (doc.isReady) return;
  1114. try {
  1115. doc.documentElement.doScroll("left");
  1116. } catch (error) {
  1117. setTimeout(arguments.callee, 0);
  1118. return;
  1119. }
  1120. doReady(doc);
  1121. })();
  1122. win.attachEvent("onload", function() {
  1123. doReady(doc);
  1124. });
  1125. } else {
  1126. doc.addEventListener(
  1127. "DOMContentLoaded",
  1128. function() {
  1129. doc.removeEventListener(
  1130. "DOMContentLoaded",
  1131. arguments.callee,
  1132. false
  1133. );
  1134. doReady(doc);
  1135. },
  1136. false
  1137. );
  1138. win.addEventListener(
  1139. "load",
  1140. function() {
  1141. doReady(doc);
  1142. },
  1143. false
  1144. );
  1145. }
  1146. }
  1147. };
  1148. })(),
  1149. /**
  1150. * 动态添加css样式
  1151. * @method cssRule
  1152. * @param { String } 节点名称
  1153. * @grammar UE.utils.cssRule('添加的样式的节点名称',['样式','放到哪个document上'])
  1154. * @grammar UE.utils.cssRule('body','body{background:#ccc}') => null //给body添加背景颜色
  1155. * @grammar UE.utils.cssRule('body') =>样式的字符串 //取得key值为body的样式的内容,如果没有找到key值先关的样式将返回空,例如刚才那个背景颜色,将返回 body{background:#ccc}
  1156. * @grammar UE.utils.cssRule('body',document) => 返回指定key的样式,并且指定是哪个document
  1157. * @grammar UE.utils.cssRule('body','') =>null //清空给定的key值的背景颜色
  1158. */
  1159. cssRule: browser.ie && browser.version != 11
  1160. ? function(key, style, doc) {
  1161. var indexList, index;
  1162. if (
  1163. style === undefined ||
  1164. (style && style.nodeType && style.nodeType == 9)
  1165. ) {
  1166. //获取样式
  1167. doc = style && style.nodeType && style.nodeType == 9
  1168. ? style
  1169. : doc || document;
  1170. indexList = doc.indexList || (doc.indexList = {});
  1171. index = indexList[key];
  1172. if (index !== undefined) {
  1173. return doc.styleSheets[index].cssText;
  1174. }
  1175. return undefined;
  1176. }
  1177. doc = doc || document;
  1178. indexList = doc.indexList || (doc.indexList = {});
  1179. index = indexList[key];
  1180. //清除样式
  1181. if (style === "") {
  1182. if (index !== undefined) {
  1183. doc.styleSheets[index].cssText = "";
  1184. delete indexList[key];
  1185. return true;
  1186. }
  1187. return false;
  1188. }
  1189. //添加样式
  1190. if (index !== undefined) {
  1191. sheetStyle = doc.styleSheets[index];
  1192. } else {
  1193. sheetStyle = doc.createStyleSheet(
  1194. "",
  1195. (index = doc.styleSheets.length)
  1196. );
  1197. indexList[key] = index;
  1198. }
  1199. sheetStyle.cssText = style;
  1200. }
  1201. : function(key, style, doc) {
  1202. var head, node;
  1203. if (
  1204. style === undefined ||
  1205. (style && style.nodeType && style.nodeType == 9)
  1206. ) {
  1207. //获取样式
  1208. doc = style && style.nodeType && style.nodeType == 9
  1209. ? style
  1210. : doc || document;
  1211. node = doc.getElementById(key);
  1212. return node ? node.innerHTML : undefined;
  1213. }
  1214. doc = doc || document;
  1215. node = doc.getElementById(key);
  1216. //清除样式
  1217. if (style === "") {
  1218. if (node) {
  1219. node.parentNode.removeChild(node);
  1220. return true;
  1221. }
  1222. return false;
  1223. }
  1224. //添加样式
  1225. if (node) {
  1226. node.innerHTML = style;
  1227. } else {
  1228. node = doc.createElement("style");
  1229. node.id = key;
  1230. node.innerHTML = style;
  1231. doc.getElementsByTagName("head")[0].appendChild(node);
  1232. }
  1233. },
  1234. sort: function(array, compareFn) {
  1235. compareFn =
  1236. compareFn ||
  1237. function(item1, item2) {
  1238. return item1.localeCompare(item2);
  1239. };
  1240. for (var i = 0, len = array.length; i < len; i++) {
  1241. for (var j = i, length = array.length; j < length; j++) {
  1242. if (compareFn(array[i], array[j]) > 0) {
  1243. var t = array[i];
  1244. array[i] = array[j];
  1245. array[j] = t;
  1246. }
  1247. }
  1248. }
  1249. return array;
  1250. },
  1251. serializeParam: function(json) {
  1252. var strArr = [];
  1253. for (var i in json) {
  1254. //忽略默认的几个参数
  1255. if (i == "method" || i == "timeout" || i == "async") continue;
  1256. //传递过来的对象和函数不在提交之列
  1257. if (
  1258. !(
  1259. (typeof json[i]).toLowerCase() == "function" ||
  1260. (typeof json[i]).toLowerCase() == "object"
  1261. )
  1262. ) {
  1263. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  1264. } else if (utils.isArray(json[i])) {
  1265. //支持传数组内容
  1266. for (var j = 0; j < json[i].length; j++) {
  1267. strArr.push(
  1268. encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j])
  1269. );
  1270. }
  1271. }
  1272. }
  1273. return strArr.join("&");
  1274. },
  1275. formatUrl: function(url) {
  1276. var u = url.replace(/&&/g, "&");
  1277. u = u.replace(/\?&/g, "?");
  1278. u = u.replace(/&$/g, "");
  1279. u = u.replace(/&#/g, "#");
  1280. u = u.replace(/&+/g, "&");
  1281. return u;
  1282. },
  1283. addStyleContent: function(cssContent){
  1284. var style = document.createElement("style");
  1285. style.innerHTML = cssContent;
  1286. document.head.appendChild(style);
  1287. },
  1288. isCrossDomainUrl: function(url) {
  1289. var a = document.createElement("a");
  1290. a.href = url;
  1291. if (browser.ie) {
  1292. a.href = a.href;
  1293. }
  1294. return !(
  1295. a.protocol == location.protocol &&
  1296. a.hostname == location.hostname &&
  1297. (a.port == location.port ||
  1298. (a.port == "80" && location.port == "") ||
  1299. (a.port == "" && location.port == "80"))
  1300. );
  1301. },
  1302. clearEmptyAttrs: function(obj) {
  1303. for (var p in obj) {
  1304. if (obj[p] === "") {
  1305. delete obj[p];
  1306. }
  1307. }
  1308. return obj;
  1309. },
  1310. str2json: function(s) {
  1311. if (!utils.isString(s)) return null;
  1312. if (window.JSON) {
  1313. return JSON.parse(s);
  1314. } else {
  1315. return new Function("return " + utils.trim(s || ""))();
  1316. }
  1317. },
  1318. json2str: (function() {
  1319. if (window.JSON) {
  1320. return JSON.stringify;
  1321. } else {
  1322. var escapeMap = {
  1323. "\b": "\\b",
  1324. "\t": "\\t",
  1325. "\n": "\\n",
  1326. "\f": "\\f",
  1327. "\r": "\\r",
  1328. '"': '\\"',
  1329. "\\": "\\\\"
  1330. };
  1331. function encodeString(source) {
  1332. if (/["\\\x00-\x1f]/.test(source)) {
  1333. source = source.replace(/["\\\x00-\x1f]/g, function(match) {
  1334. var c = escapeMap[match];
  1335. if (c) {
  1336. return c;
  1337. }
  1338. c = match.charCodeAt();
  1339. return (
  1340. "\\u00" + Math.floor(c / 16).toString(16) + (c % 16).toString(16)
  1341. );
  1342. });
  1343. }
  1344. return '"' + source + '"';
  1345. }
  1346. function encodeArray(source) {
  1347. var result = ["["],
  1348. l = source.length,
  1349. preComma,
  1350. i,
  1351. item;
  1352. for (i = 0; i < l; i++) {
  1353. item = source[i];
  1354. switch (typeof item) {
  1355. case "undefined":
  1356. case "function":
  1357. case "unknown":
  1358. break;
  1359. default:
  1360. if (preComma) {
  1361. result.push(",");
  1362. }
  1363. result.push(utils.json2str(item));
  1364. preComma = 1;
  1365. }
  1366. }
  1367. result.push("]");
  1368. return result.join("");
  1369. }
  1370. function pad(source) {
  1371. return source < 10 ? "0" + source : source;
  1372. }
  1373. function encodeDate(source) {
  1374. return (
  1375. '"' +
  1376. source.getFullYear() +
  1377. "-" +
  1378. pad(source.getMonth() + 1) +
  1379. "-" +
  1380. pad(source.getDate()) +
  1381. "T" +
  1382. pad(source.getHours()) +
  1383. ":" +
  1384. pad(source.getMinutes()) +
  1385. ":" +
  1386. pad(source.getSeconds()) +
  1387. '"'
  1388. );
  1389. }
  1390. return function(value) {
  1391. switch (typeof value) {
  1392. case "undefined":
  1393. return "undefined";
  1394. case "number":
  1395. return isFinite(value) ? String(value) : "null";
  1396. case "string":
  1397. return encodeString(value);
  1398. case "boolean":
  1399. return String(value);
  1400. default:
  1401. if (value === null) {
  1402. return "null";
  1403. } else if (utils.isArray(value)) {
  1404. return encodeArray(value);
  1405. } else if (utils.isDate(value)) {
  1406. return encodeDate(value);
  1407. } else {
  1408. var result = ["{"],
  1409. encode = utils.json2str,
  1410. preComma,
  1411. item;
  1412. for (var key in value) {
  1413. if (Object.prototype.hasOwnProperty.call(value, key)) {
  1414. item = value[key];
  1415. switch (typeof item) {
  1416. case "undefined":
  1417. case "unknown":
  1418. case "function":
  1419. break;
  1420. default:
  1421. if (preComma) {
  1422. result.push(",");
  1423. }
  1424. preComma = 1;
  1425. result.push(encode(key) + ":" + encode(item));
  1426. }
  1427. }
  1428. }
  1429. result.push("}");
  1430. return result.join("");
  1431. }
  1432. }
  1433. };
  1434. }
  1435. })()
  1436. });
  1437. /**
  1438. * 判断给定的对象是否是字符串
  1439. * @method isString
  1440. * @param { * } object 需要判断的对象
  1441. * @return { Boolean } 给定的对象是否是字符串
  1442. */
  1443. /**
  1444. * 判断给定的对象是否是数组
  1445. * @method isArray
  1446. * @param { * } object 需要判断的对象
  1447. * @return { Boolean } 给定的对象是否是数组
  1448. */
  1449. /**
  1450. * 判断给定的对象是否是一个Function
  1451. * @method isFunction
  1452. * @param { * } object 需要判断的对象
  1453. * @return { Boolean } 给定的对象是否是Function
  1454. */
  1455. /**
  1456. * 判断给定的对象是否是Number
  1457. * @method isNumber
  1458. * @param { * } object 需要判断的对象
  1459. * @return { Boolean } 给定的对象是否是Number
  1460. */
  1461. /**
  1462. * 判断给定的对象是否是一个正则表达式
  1463. * @method isRegExp
  1464. * @param { * } object 需要判断的对象
  1465. * @return { Boolean } 给定的对象是否是正则表达式
  1466. */
  1467. /**
  1468. * 判断给定的对象是否是一个普通对象
  1469. * @method isObject
  1470. * @param { * } object 需要判断的对象
  1471. * @return { Boolean } 给定的对象是否是普通对象
  1472. */
  1473. utils.each(
  1474. ["String", "Function", "Array", "Number", "RegExp", "Object", "Date"],
  1475. function(v) {
  1476. UE.utils["is" + v] = function(obj) {
  1477. return Object.prototype.toString.apply(obj) == "[object " + v + "]";
  1478. };
  1479. }
  1480. );
  1481. // core/EventBase.js
  1482. /**
  1483. * UE采用的事件基类
  1484. * @file
  1485. * @module UE
  1486. * @class EventBase
  1487. * @since 1.2.6.1
  1488. */
  1489. /**
  1490. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  1491. * @unfile
  1492. * @module UE
  1493. */
  1494. /**
  1495. * UE采用的事件基类,继承此类的对应类将获取addListener,removeListener,fireEvent方法。
  1496. * 在UE中,Editor以及所有ui实例都继承了该类,故可以在对应的ui对象以及editor对象上使用上述方法。
  1497. * @unfile
  1498. * @module UE
  1499. * @class EventBase
  1500. */
  1501. /**
  1502. * 通过此构造器,子类可以继承EventBase获取事件监听的方法
  1503. * @constructor
  1504. * @example
  1505. * ```javascript
  1506. * UE.EventBase.call(editor);
  1507. * ```
  1508. */
  1509. var EventBase = (UE.EventBase = function() {});
  1510. EventBase.prototype = {
  1511. /**
  1512. * 注册事件监听器
  1513. * @method addListener
  1514. * @param { String } types 监听的事件名称,同时监听多个事件使用空格分隔
  1515. * @param { Function } fn 监听的事件被触发时,会执行该回调函数
  1516. * @waining 事件被触发时,监听的函数假如返回的值恒等于true,回调函数的队列中后面的函数将不执行
  1517. * @example
  1518. * ```javascript
  1519. * editor.addListener('selectionchange',function(){
  1520. * console.log("选区已经变化!");
  1521. * })
  1522. * editor.addListener('beforegetcontent aftergetcontent',function(type){
  1523. * if(type == 'beforegetcontent'){
  1524. * //do something
  1525. * }else{
  1526. * //do something
  1527. * }
  1528. * console.log(this.getContent) // this是注册的事件的编辑器实例
  1529. * })
  1530. * ```
  1531. * @see UE.EventBase:fireEvent(String)
  1532. */
  1533. addListener: function(types, listener) {
  1534. types = utils.trim(types).split(/\s+/);
  1535. for (var i = 0, ti; (ti = types[i++]); ) {
  1536. getListener(this, ti, true).push(listener);
  1537. }
  1538. },
  1539. on: function(types, listener) {
  1540. return this.addListener(types, listener);
  1541. },
  1542. off: function(types, listener) {
  1543. return this.removeListener(types, listener);
  1544. },
  1545. trigger: function() {
  1546. return this.fireEvent.apply(this, arguments);
  1547. },
  1548. /**
  1549. * 移除事件监听器
  1550. * @method removeListener
  1551. * @param { String } types 移除的事件名称,同时移除多个事件使用空格分隔
  1552. * @param { Function } fn 移除监听事件的函数引用
  1553. * @example
  1554. * ```javascript
  1555. * //changeCallback为方法体
  1556. * editor.removeListener("selectionchange",changeCallback);
  1557. * ```
  1558. */
  1559. removeListener: function(types, listener) {
  1560. types = utils.trim(types).split(/\s+/);
  1561. for (var i = 0, ti; (ti = types[i++]); ) {
  1562. utils.removeItem(getListener(this, ti) || [], listener);
  1563. }
  1564. },
  1565. /**
  1566. * 触发事件
  1567. * @method fireEvent
  1568. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1569. * @remind 该方法会触发addListener
  1570. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1571. * @example
  1572. * ```javascript
  1573. * editor.fireEvent("selectionchange");
  1574. * ```
  1575. */
  1576. /**
  1577. * 触发事件
  1578. * @method fireEvent
  1579. * @param { String } types 触发的事件名称,同时触发多个事件使用空格分隔
  1580. * @param { *... } options 可选参数,可以传入一个或多个参数,会传给事件触发的回调函数
  1581. * @return { * } 返回触发事件的队列中,最后执行的回调函数的返回值
  1582. * @example
  1583. * ```javascript
  1584. *
  1585. * editor.addListener( "selectionchange", function ( type, arg1, arg2 ) {
  1586. *
  1587. * console.log( arg1 + " " + arg2 );
  1588. *
  1589. * } );
  1590. *
  1591. * //触发selectionchange事件, 会执行上面的事件监听器
  1592. * //output: Hello World
  1593. * editor.fireEvent("selectionchange", "Hello", "World");
  1594. * ```
  1595. */
  1596. fireEvent: function() {
  1597. var types = arguments[0];
  1598. types = utils.trim(types).split(" ");
  1599. for (var i = 0, ti; (ti = types[i++]); ) {
  1600. var listeners = getListener(this, ti),
  1601. r,
  1602. t,
  1603. k;
  1604. if (listeners) {
  1605. k = listeners.length;
  1606. while (k--) {
  1607. if (!listeners[k]) continue;
  1608. t = listeners[k].apply(this, arguments);
  1609. if (t === true) {
  1610. return t;
  1611. }
  1612. if (t !== undefined) {
  1613. r = t;
  1614. }
  1615. }
  1616. }
  1617. if ((t = this["on" + ti.toLowerCase()])) {
  1618. r = t.apply(this, arguments);
  1619. }
  1620. }
  1621. return r;
  1622. }
  1623. };
  1624. /**
  1625. * 获得对象所拥有监听类型的所有监听器
  1626. * @unfile
  1627. * @module UE
  1628. * @since 1.2.6.1
  1629. * @method getListener
  1630. * @public
  1631. * @param { Object } obj 查询监听器的对象
  1632. * @param { String } type 事件类型
  1633. * @param { Boolean } force 为true且当前所有type类型的侦听器不存在时,创建一个空监听器数组
  1634. * @return { Array } 监听器数组
  1635. */
  1636. function getListener(obj, type, force) {
  1637. var allListeners;
  1638. type = type.toLowerCase();
  1639. return (
  1640. (allListeners =
  1641. obj.__allListeners || (force && (obj.__allListeners = {}))) &&
  1642. (allListeners[type] || (force && (allListeners[type] = [])))
  1643. );
  1644. }
  1645. // core/dtd.js
  1646. ///import editor.js
  1647. ///import core/dom/dom.js
  1648. ///import core/utils.js
  1649. /**
  1650. * dtd html语义化的体现类
  1651. * @constructor
  1652. * @namespace dtd
  1653. */
  1654. var dtd = (dom.dtd = (function() {
  1655. function _(s) {
  1656. for (var k in s) {
  1657. s[k.toUpperCase()] = s[k];
  1658. }
  1659. return s;
  1660. }
  1661. var X = utils.extend2;
  1662. var A = _({ isindex: 1, fieldset: 1 }),
  1663. B = _({ input: 1, button: 1, select: 1, textarea: 1, label: 1 }),
  1664. C = X(_({ a: 1 }), B),
  1665. D = X({ iframe: 1 }, C),
  1666. E = _({
  1667. hr: 1,
  1668. ul: 1,
  1669. menu: 1,
  1670. div: 1,
  1671. blockquote: 1,
  1672. noscript: 1,
  1673. table: 1,
  1674. center: 1,
  1675. address: 1,
  1676. dir: 1,
  1677. pre: 1,
  1678. h5: 1,
  1679. dl: 1,
  1680. h4: 1,
  1681. noframes: 1,
  1682. h6: 1,
  1683. ol: 1,
  1684. h1: 1,
  1685. h3: 1,
  1686. h2: 1
  1687. }),
  1688. F = _({ ins: 1, del: 1, script: 1, style: 1 }),
  1689. G = X(
  1690. _({
  1691. mark: 1,
  1692. b: 1,
  1693. acronym: 1,
  1694. bdo: 1,
  1695. var: 1,
  1696. "#": 1,
  1697. abbr: 1,
  1698. code: 1,
  1699. br: 1,
  1700. i: 1,
  1701. cite: 1,
  1702. kbd: 1,
  1703. u: 1,
  1704. strike: 1,
  1705. s: 1,
  1706. tt: 1,
  1707. strong: 1,
  1708. q: 1,
  1709. samp: 1,
  1710. em: 1,
  1711. dfn: 1,
  1712. span: 1
  1713. }),
  1714. F
  1715. ),
  1716. H = X(
  1717. _({
  1718. sub: 1,
  1719. img: 1,
  1720. embed: 1,
  1721. object: 1,
  1722. sup: 1,
  1723. basefont: 1,
  1724. map: 1,
  1725. applet: 1,
  1726. font: 1,
  1727. big: 1,
  1728. small: 1
  1729. }),
  1730. G
  1731. ),
  1732. I = X(_({ p: 1 }), H),
  1733. J = X(_({ iframe: 1 }), H, B),
  1734. K = _({
  1735. img: 1,
  1736. embed: 1,
  1737. noscript: 1,
  1738. br: 1,
  1739. kbd: 1,
  1740. center: 1,
  1741. button: 1,
  1742. basefont: 1,
  1743. h5: 1,
  1744. h4: 1,
  1745. samp: 1,
  1746. h6: 1,
  1747. ol: 1,
  1748. h1: 1,
  1749. h3: 1,
  1750. h2: 1,
  1751. form: 1,
  1752. font: 1,
  1753. "#": 1,
  1754. select: 1,
  1755. menu: 1,
  1756. ins: 1,
  1757. abbr: 1,
  1758. label: 1,
  1759. code: 1,
  1760. table: 1,
  1761. script: 1,
  1762. cite: 1,
  1763. input: 1,
  1764. iframe: 1,
  1765. strong: 1,
  1766. textarea: 1,
  1767. noframes: 1,
  1768. big: 1,
  1769. small: 1,
  1770. span: 1,
  1771. hr: 1,
  1772. sub: 1,
  1773. bdo: 1,
  1774. var: 1,
  1775. div: 1,
  1776. object: 1,
  1777. sup: 1,
  1778. strike: 1,
  1779. dir: 1,
  1780. map: 1,
  1781. dl: 1,
  1782. applet: 1,
  1783. del: 1,
  1784. isindex: 1,
  1785. fieldset: 1,
  1786. ul: 1,
  1787. b: 1,
  1788. acronym: 1,
  1789. a: 1,
  1790. blockquote: 1,
  1791. i: 1,
  1792. u: 1,
  1793. s: 1,
  1794. tt: 1,
  1795. address: 1,
  1796. q: 1,
  1797. pre: 1,
  1798. p: 1,
  1799. em: 1,
  1800. dfn: 1
  1801. }),
  1802. L = X(_({ a: 0 }), J), //a不能被切开,所以把他
  1803. M = _({ tr: 1 }),
  1804. N = _({ "#": 1 }),
  1805. O = X(_({ param: 1 }), K),
  1806. P = X(_({ form: 1 }), A, D, E, I),
  1807. Q = _({ li: 1, ol: 1, ul: 1 }),
  1808. R = _({ style: 1, script: 1 }),
  1809. S = _({ base: 1, link: 1, meta: 1, title: 1 }),
  1810. T = X(S, R),
  1811. U = _({ head: 1, body: 1 }),
  1812. V = _({ html: 1 });
  1813. var block = _({
  1814. address: 1,
  1815. blockquote: 1,
  1816. center: 1,
  1817. dir: 1,
  1818. div: 1,
  1819. dl: 1,
  1820. fieldset: 1,
  1821. form: 1,
  1822. h1: 1,
  1823. h2: 1,
  1824. h3: 1,
  1825. h4: 1,
  1826. h5: 1,
  1827. h6: 1,
  1828. hr: 1,
  1829. isindex: 1,
  1830. menu: 1,
  1831. noframes: 1,
  1832. ol: 1,
  1833. p: 1,
  1834. pre: 1,
  1835. table: 1,
  1836. ul: 1
  1837. }),
  1838. empty = _({
  1839. area: 1,
  1840. base: 1,
  1841. basefont: 1,
  1842. br: 1,
  1843. col: 1,
  1844. command: 1,
  1845. dialog: 1,
  1846. embed: 1,
  1847. hr: 1,
  1848. img: 1,
  1849. input: 1,
  1850. isindex: 1,
  1851. keygen: 1,
  1852. link: 1,
  1853. meta: 1,
  1854. param: 1,
  1855. source: 1,
  1856. track: 1,
  1857. wbr: 1
  1858. });
  1859. return _({
  1860. // $ 表示自定的属性
  1861. // body外的元素列表.
  1862. $nonBodyContent: X(V, U, S),
  1863. //块结构元素列表
  1864. $block: block,
  1865. //内联元素列表
  1866. $inline: L,
  1867. $inlineWithA: X(_({ a: 1 }), L),
  1868. $body: X(_({ script: 1, style: 1 }), block),
  1869. $cdata: _({ script: 1, style: 1 }),
  1870. //自闭和元素
  1871. $empty: empty,
  1872. //不是自闭合,但不能让range选中里边
  1873. $nonChild: _({ iframe: 1, textarea: 1 }),
  1874. //列表元素列表
  1875. $listItem: _({ dd: 1, dt: 1, li: 1 }),
  1876. //列表根元素列表
  1877. $list: _({ ul: 1, ol: 1, dl: 1 }),
  1878. //不能认为是空的元素
  1879. $isNotEmpty: _({
  1880. table: 1,
  1881. ul: 1,
  1882. ol: 1,
  1883. dl: 1,
  1884. iframe: 1,
  1885. area: 1,
  1886. base: 1,
  1887. col: 1,
  1888. hr: 1,
  1889. img: 1,
  1890. embed: 1,
  1891. input: 1,
  1892. textarea: 1,
  1893. link: 1,
  1894. meta: 1,
  1895. param: 1,
  1896. h1: 1,
  1897. h2: 1,
  1898. h3: 1,
  1899. h4: 1,
  1900. h5: 1,
  1901. h6: 1
  1902. }),
  1903. //如果没有子节点就可以删除的元素列表,像span,a
  1904. $removeEmpty: _({
  1905. a: 1,
  1906. abbr: 1,
  1907. acronym: 1,
  1908. address: 1,
  1909. b: 1,
  1910. bdo: 1,
  1911. big: 1,
  1912. cite: 1,
  1913. code: 1,
  1914. del: 1,
  1915. dfn: 1,
  1916. em: 1,
  1917. font: 1,
  1918. i: 1,
  1919. ins: 1,
  1920. label: 1,
  1921. kbd: 1,
  1922. q: 1,
  1923. s: 1,
  1924. samp: 1,
  1925. small: 1,
  1926. span: 1,
  1927. strike: 1,
  1928. strong: 1,
  1929. sub: 1,
  1930. sup: 1,
  1931. tt: 1,
  1932. u: 1,
  1933. var: 1
  1934. }),
  1935. $removeEmptyBlock: _({ p: 1, div: 1 }),
  1936. //在table元素里的元素列表
  1937. $tableContent: _({
  1938. caption: 1,
  1939. col: 1,
  1940. colgroup: 1,
  1941. tbody: 1,
  1942. td: 1,
  1943. tfoot: 1,
  1944. th: 1,
  1945. thead: 1,
  1946. tr: 1,
  1947. table: 1
  1948. }),
  1949. //不转换的标签
  1950. $notTransContent: _({ pre: 1, script: 1, style: 1, textarea: 1 }),
  1951. html: U,
  1952. head: T,
  1953. style: N,
  1954. script: N,
  1955. body: P,
  1956. base: {},
  1957. link: {},
  1958. meta: {},
  1959. title: N,
  1960. col: {},
  1961. tr: _({ td: 1, th: 1 }),
  1962. img: {},
  1963. embed: {},
  1964. colgroup: _({ thead: 1, col: 1, tbody: 1, tr: 1, tfoot: 1 }),
  1965. noscript: P,
  1966. td: P,
  1967. br: {},
  1968. th: P,
  1969. center: P,
  1970. kbd: L,
  1971. button: X(I, E),
  1972. basefont: {},
  1973. h5: L,
  1974. h4: L,
  1975. samp: L,
  1976. h6: L,
  1977. ol: Q,
  1978. h1: L,
  1979. h3: L,
  1980. option: N,
  1981. h2: L,
  1982. form: X(A, D, E, I),
  1983. select: _({ optgroup: 1, option: 1 }),
  1984. font: L,
  1985. ins: L,
  1986. menu: Q,
  1987. abbr: L,
  1988. label: L,
  1989. table: _({
  1990. thead: 1,
  1991. col: 1,
  1992. tbody: 1,
  1993. tr: 1,
  1994. colgroup: 1,
  1995. caption: 1,
  1996. tfoot: 1
  1997. }),
  1998. code: L,
  1999. tfoot: M,
  2000. cite: L,
  2001. li: P,
  2002. input: {},
  2003. iframe: P,
  2004. strong: L,
  2005. textarea: N,
  2006. noframes: P,
  2007. big: L,
  2008. small: L,
  2009. //trace:
  2010. span: _({
  2011. "#": 1,
  2012. br: 1,
  2013. b: 1,
  2014. strong: 1,
  2015. u: 1,
  2016. i: 1,
  2017. em: 1,
  2018. sub: 1,
  2019. sup: 1,
  2020. strike: 1,
  2021. span: 1
  2022. }),
  2023. hr: L,
  2024. dt: L,
  2025. sub: L,
  2026. optgroup: _({ option: 1 }),
  2027. param: {},
  2028. bdo: L,
  2029. var: L,
  2030. div: P,
  2031. object: O,
  2032. sup: L,
  2033. dd: P,
  2034. strike: L,
  2035. area: {},
  2036. dir: Q,
  2037. map: X(_({ area: 1, form: 1, p: 1 }), A, F, E),
  2038. applet: O,
  2039. dl: _({ dt: 1, dd: 1 }),
  2040. del: L,
  2041. isindex: {},
  2042. fieldset: X(_({ legend: 1 }), K),
  2043. thead: M,
  2044. ul: Q,
  2045. acronym: L,
  2046. b: L,
  2047. a: X(_({ a: 1 }), J),
  2048. blockquote: X(_({ td: 1, tr: 1, tbody: 1, li: 1 }), P),
  2049. caption: L,
  2050. i: L,
  2051. u: L,
  2052. tbody: M,
  2053. s: L,
  2054. address: X(D, I),
  2055. tt: L,
  2056. legend: L,
  2057. q: L,
  2058. pre: X(G, C),
  2059. p: X(_({ a: 1 }), L),
  2060. em: L,
  2061. dfn: L,
  2062. mark: L
  2063. });
  2064. })());
  2065. // core/domUtils.js
  2066. /**
  2067. * Dom操作工具包
  2068. * @file
  2069. * @module UE.dom.domUtils
  2070. * @since 1.2.6.1
  2071. */
  2072. /**
  2073. * Dom操作工具包
  2074. * @unfile
  2075. * @module UE.dom.domUtils
  2076. */
  2077. function getDomNode(node, start, ltr, startFromChild, fn, guard) {
  2078. var tmpNode = startFromChild && node[start],
  2079. parent;
  2080. !tmpNode && (tmpNode = node[ltr]);
  2081. while (!tmpNode && (parent = (parent || node).parentNode)) {
  2082. if (parent.tagName == "BODY" || (guard && !guard(parent))) {
  2083. return null;
  2084. }
  2085. tmpNode = parent[ltr];
  2086. }
  2087. if (tmpNode && fn && !fn(tmpNode)) {
  2088. return getDomNode(tmpNode, start, ltr, false, fn);
  2089. }
  2090. return tmpNode;
  2091. }
  2092. var attrFix = ie && browser.version < 9
  2093. ? {
  2094. tabindex: "tabIndex",
  2095. readonly: "readOnly",
  2096. for: "htmlFor",
  2097. class: "className",
  2098. maxlength: "maxLength",
  2099. cellspacing: "cellSpacing",
  2100. cellpadding: "cellPadding",
  2101. rowspan: "rowSpan",
  2102. colspan: "colSpan",
  2103. usemap: "useMap",
  2104. frameborder: "frameBorder"
  2105. }
  2106. : {
  2107. tabindex: "tabIndex",
  2108. readonly: "readOnly"
  2109. },
  2110. styleBlock = utils.listToMap([
  2111. "-webkit-box",
  2112. "-moz-box",
  2113. "block",
  2114. "list-item",
  2115. "table",
  2116. "table-row-group",
  2117. "table-header-group",
  2118. "table-footer-group",
  2119. "table-row",
  2120. "table-column-group",
  2121. "table-column",
  2122. "table-cell",
  2123. "table-caption"
  2124. ]);
  2125. var domUtils = (dom.domUtils = {
  2126. //节点常量
  2127. NODE_ELEMENT: 1,
  2128. NODE_DOCUMENT: 9,
  2129. NODE_TEXT: 3,
  2130. NODE_COMMENT: 8,
  2131. NODE_DOCUMENT_FRAGMENT: 11,
  2132. //位置关系
  2133. POSITION_IDENTICAL: 0,
  2134. POSITION_DISCONNECTED: 1,
  2135. POSITION_FOLLOWING: 2,
  2136. POSITION_PRECEDING: 4,
  2137. POSITION_IS_CONTAINED: 8,
  2138. POSITION_CONTAINS: 16,
  2139. //ie6使用其他的会有一段空白出现
  2140. fillChar: ie && browser.version === "6" ? "\ufeff" : "\u200B",
  2141. //-------------------------Node部分--------------------------------
  2142. keys: {
  2143. /*Backspace*/ 8: 1,
  2144. /*Delete*/ 46: 1,
  2145. /*Shift*/ 16: 1,
  2146. /*Ctrl*/ 17: 1,
  2147. /*Alt*/ 18: 1,
  2148. 37: 1,
  2149. 38: 1,
  2150. 39: 1,
  2151. 40: 1,
  2152. 13: 1 /*enter*/
  2153. },
  2154. /**
  2155. * 获取节点A相对于节点B的位置关系
  2156. * @method getPosition
  2157. * @param { Node } nodeA 需要查询位置关系的节点A
  2158. * @param { Node } nodeB 需要查询位置关系的节点B
  2159. * @return { Number } 节点A与节点B的关系
  2160. * @example
  2161. * ```javascript
  2162. * //output: 20
  2163. * var position = UE.dom.domUtils.getPosition( document.documentElement, document.body );
  2164. *
  2165. * switch ( position ) {
  2166. *
  2167. * //0
  2168. * case UE.dom.domUtils.POSITION_IDENTICAL:
  2169. * console.log('元素相同');
  2170. * break;
  2171. * //1
  2172. * case UE.dom.domUtils.POSITION_DISCONNECTED:
  2173. * console.log('两个节点在不同的文档中');
  2174. * break;
  2175. * //2
  2176. * case UE.dom.domUtils.POSITION_FOLLOWING:
  2177. * console.log('节点A在节点B之后');
  2178. * break;
  2179. * //4
  2180. * case UE.dom.domUtils.POSITION_PRECEDING;
  2181. * console.log('节点A在节点B之前');
  2182. * break;
  2183. * //8
  2184. * case UE.dom.domUtils.POSITION_IS_CONTAINED:
  2185. * console.log('节点A被节点B包含');
  2186. * break;
  2187. * case 10:
  2188. * console.log('节点A被节点B包含且节点A在节点B之后');
  2189. * break;
  2190. * //16
  2191. * case UE.dom.domUtils.POSITION_CONTAINS:
  2192. * console.log('节点A包含节点B');
  2193. * break;
  2194. * case 20:
  2195. * console.log('节点A包含节点B且节点A在节点B之前');
  2196. * break;
  2197. *
  2198. * }
  2199. * ```
  2200. */
  2201. getPosition: function(nodeA, nodeB) {
  2202. // 如果两个节点是同一个节点
  2203. if (nodeA === nodeB) {
  2204. // domUtils.POSITION_IDENTICAL
  2205. return 0;
  2206. }
  2207. var node,
  2208. parentsA = [nodeA],
  2209. parentsB = [nodeB];
  2210. node = nodeA;
  2211. while ((node = node.parentNode)) {
  2212. // 如果nodeB是nodeA的祖先节点
  2213. if (node === nodeB) {
  2214. // domUtils.POSITION_IS_CONTAINED + domUtils.POSITION_FOLLOWING
  2215. return 10;
  2216. }
  2217. parentsA.push(node);
  2218. }
  2219. node = nodeB;
  2220. while ((node = node.parentNode)) {
  2221. // 如果nodeA是nodeB的祖先节点
  2222. if (node === nodeA) {
  2223. // domUtils.POSITION_CONTAINS + domUtils.POSITION_PRECEDING
  2224. return 20;
  2225. }
  2226. parentsB.push(node);
  2227. }
  2228. parentsA.reverse();
  2229. parentsB.reverse();
  2230. if (parentsA[0] !== parentsB[0]) {
  2231. // domUtils.POSITION_DISCONNECTED
  2232. return 1;
  2233. }
  2234. var i = -1;
  2235. while ((i++, parentsA[i] === parentsB[i])) {}
  2236. nodeA = parentsA[i];
  2237. nodeB = parentsB[i];
  2238. while ((nodeA = nodeA.nextSibling)) {
  2239. if (nodeA === nodeB) {
  2240. // domUtils.POSITION_PRECEDING
  2241. return 4;
  2242. }
  2243. }
  2244. // domUtils.POSITION_FOLLOWING
  2245. return 2;
  2246. },
  2247. /**
  2248. * 检测节点node在父节点中的索引位置
  2249. * @method getNodeIndex
  2250. * @param { Node } node 需要检测的节点对象
  2251. * @return { Number } 该节点在父节点中的位置
  2252. * @see UE.dom.domUtils.getNodeIndex(Node,Boolean)
  2253. */
  2254. /**
  2255. * 检测节点node在父节点中的索引位置, 根据给定的mergeTextNode参数决定是否要合并多个连续的文本节点为一个节点
  2256. * @method getNodeIndex
  2257. * @param { Node } node 需要检测的节点对象
  2258. * @param { Boolean } mergeTextNode 是否合并多个连续的文本节点为一个节点
  2259. * @return { Number } 该节点在父节点中的位置
  2260. * @example
  2261. * ```javascript
  2262. *
  2263. * var node = document.createElement("div");
  2264. *
  2265. * node.appendChild( document.createTextNode( "hello" ) );
  2266. * node.appendChild( document.createTextNode( "world" ) );
  2267. * node.appendChild( node = document.createElement( "div" ) );
  2268. *
  2269. * //output: 2
  2270. * console.log( UE.dom.domUtils.getNodeIndex( node ) );
  2271. *
  2272. * //output: 1
  2273. * console.log( UE.dom.domUtils.getNodeIndex( node, true ) );
  2274. *
  2275. * ```
  2276. */
  2277. getNodeIndex: function(node, ignoreTextNode) {
  2278. var preNode = node,
  2279. i = 0;
  2280. while ((preNode = preNode.previousSibling)) {
  2281. if (ignoreTextNode && preNode.nodeType == 3) {
  2282. if (preNode.nodeType != preNode.nextSibling.nodeType) {
  2283. i++;
  2284. }
  2285. continue;
  2286. }
  2287. i++;
  2288. }
  2289. return i;
  2290. },
  2291. /**
  2292. * 检测节点node是否在给定的document对象上
  2293. * @method inDoc
  2294. * @param { Node } node 需要检测的节点对象
  2295. * @param { DomDocument } doc 需要检测的document对象
  2296. * @return { Boolean } 该节点node是否在给定的document的dom树上
  2297. * @example
  2298. * ```javascript
  2299. *
  2300. * var node = document.createElement("div");
  2301. *
  2302. * //output: false
  2303. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2304. *
  2305. * document.body.appendChild( node );
  2306. *
  2307. * //output: true
  2308. * console.log( UE.do.domUtils.inDoc( node, document ) );
  2309. *
  2310. * ```
  2311. */
  2312. inDoc: function(node, doc) {
  2313. return domUtils.getPosition(node, doc) === 10;
  2314. },
  2315. /**
  2316. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2317. * 查找的起点是给定node节点的父节点。
  2318. * @method findParent
  2319. * @param { Node } node 需要查找的节点
  2320. * @param { Function } filterFn 自定义的过滤方法。
  2321. * @warning 查找的终点是到body节点为止
  2322. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2323. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2324. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2325. * @example
  2326. * ```javascript
  2327. * var filterNode = UE.dom.domUtils.findParent( document.body.firstChild, function ( node ) {
  2328. *
  2329. * //由于查找的终点是body节点, 所以永远也不会匹配当前过滤器的条件, 即这里永远会返回false
  2330. * return node.tagName === "HTML";
  2331. *
  2332. * } );
  2333. *
  2334. * //output: true
  2335. * console.log( filterNode === null );
  2336. * ```
  2337. */
  2338. /**
  2339. * 根据给定的过滤规则filterFn, 查找符合该过滤规则的node节点的第一个祖先节点,
  2340. * 如果includeSelf的值为true,则查找的起点是给定的节点node, 否则, 起点是node的父节点
  2341. * @method findParent
  2342. * @param { Node } node 需要查找的节点
  2343. * @param { Function } filterFn 自定义的过滤方法。
  2344. * @param { Boolean } includeSelf 查找过程是否包含自身
  2345. * @warning 查找的终点是到body节点为止
  2346. * @remind 自定义的过滤方法filterFn接受一个Node对象作为参数, 该对象代表当前执行检测的祖先节点。 如果该
  2347. * 节点满足过滤条件, 则要求返回true, 这时将直接返回该节点作为findParent()的结果, 否则, 请返回false。
  2348. * @remind 如果includeSelf为true, 则过滤器第一次执行时的参数会是节点本身。
  2349. * 反之, 过滤器第一次执行时的参数将是该节点的父节点。
  2350. * @return { Node | Null } 如果找到符合过滤条件的节点, 就返回该节点, 否则返回NULL
  2351. * @example
  2352. * ```html
  2353. * <body>
  2354. *
  2355. * <div id="test">
  2356. * </div>
  2357. *
  2358. * <script type="text/javascript">
  2359. *
  2360. * //output: DIV, BODY
  2361. * var filterNode = UE.dom.domUtils.findParent( document.getElementById( "test" ), function ( node ) {
  2362. *
  2363. * console.log( node.tagName );
  2364. * return false;
  2365. *
  2366. * }, true );
  2367. *
  2368. * </script>
  2369. * </body>
  2370. * ```
  2371. */
  2372. findParent: function(node, filterFn, includeSelf) {
  2373. if (node && !domUtils.isBody(node)) {
  2374. node = includeSelf ? node : node.parentNode;
  2375. while (node) {
  2376. if (!filterFn || filterFn(node) || domUtils.isBody(node)) {
  2377. return filterFn && !filterFn(node) && domUtils.isBody(node)
  2378. ? null
  2379. : node;
  2380. }
  2381. node = node.parentNode;
  2382. }
  2383. }
  2384. return null;
  2385. },
  2386. /**
  2387. * 查找node的节点名为tagName的第一个祖先节点, 查找的起点是node节点的父节点。
  2388. * @method findParentByTagName
  2389. * @param { Node } node 需要查找的节点对象
  2390. * @param { Array } tagNames 需要查找的父节点的名称数组
  2391. * @warning 查找的终点是到body节点为止
  2392. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2393. * @example
  2394. * ```javascript
  2395. * var node = UE.dom.domUtils.findParentByTagName( document.getElementsByTagName("div")[0], [ "BODY" ] );
  2396. * //output: BODY
  2397. * console.log( node.tagName );
  2398. * ```
  2399. */
  2400. /**
  2401. * 查找node的节点名为tagName的祖先节点, 如果includeSelf的值为true,则查找的起点是给定的节点node,
  2402. * 否则, 起点是node的父节点。
  2403. * @method findParentByTagName
  2404. * @param { Node } node 需要查找的节点对象
  2405. * @param { Array } tagNames 需要查找的父节点的名称数组
  2406. * @param { Boolean } includeSelf 查找过程是否包含node节点自身
  2407. * @warning 查找的终点是到body节点为止
  2408. * @return { Node | NULL } 如果找到符合条件的节点, 则返回该节点, 否则返回NULL
  2409. * @example
  2410. * ```javascript
  2411. * var queryTarget = document.getElementsByTagName("div")[0];
  2412. * var node = UE.dom.domUtils.findParentByTagName( queryTarget, [ "DIV" ], true );
  2413. * //output: true
  2414. * console.log( queryTarget === node );
  2415. * ```
  2416. */
  2417. findParentByTagName: function(node, tagNames, includeSelf, excludeFn) {
  2418. tagNames = utils.listToMap(utils.isArray(tagNames) ? tagNames : [tagNames]);
  2419. return domUtils.findParent(
  2420. node,
  2421. function(node) {
  2422. return tagNames[node.tagName] && !(excludeFn && excludeFn(node));
  2423. },
  2424. includeSelf
  2425. );
  2426. },
  2427. /**
  2428. * 查找节点node的祖先节点集合, 查找的起点是给定节点的父节点,结果集中不包含给定的节点。
  2429. * @method findParents
  2430. * @param { Node } node 需要查找的节点对象
  2431. * @return { Array } 给定节点的祖先节点数组
  2432. * @grammar UE.dom.domUtils.findParents(node) => Array //返回一个祖先节点数组集合,不包含自身
  2433. * @grammar UE.dom.domUtils.findParents(node,includeSelf) => Array //返回一个祖先节点数组集合,includeSelf指定是否包含自身
  2434. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn) => Array //返回一个祖先节点数组集合,filterFn指定过滤条件,返回true的node将被选取
  2435. * @grammar UE.dom.domUtils.findParents(node,includeSelf,filterFn,closerFirst) => Array //返回一个祖先节点数组集合,closerFirst为true的话,node的直接父亲节点是数组的第0个
  2436. */
  2437. /**
  2438. * 查找节点node的祖先节点集合, 如果includeSelf的值为true,
  2439. * 则返回的结果集中允许出现当前给定的节点, 否则, 该节点不会出现在其结果集中。
  2440. * @method findParents
  2441. * @param { Node } node 需要查找的节点对象
  2442. * @param { Boolean } includeSelf 查找的结果中是否允许包含当前查找的节点对象
  2443. * @return { Array } 给定节点的祖先节点数组
  2444. */
  2445. findParents: function(node, includeSelf, filterFn, closerFirst) {
  2446. var parents = includeSelf && ((filterFn && filterFn(node)) || !filterFn)
  2447. ? [node]
  2448. : [];
  2449. while ((node = domUtils.findParent(node, filterFn))) {
  2450. parents.push(node);
  2451. }
  2452. return closerFirst ? parents : parents.reverse();
  2453. },
  2454. /**
  2455. * 在节点node后面插入新节点newNode
  2456. * @method insertAfter
  2457. * @param { Node } node 目标节点
  2458. * @param { Node } newNode 新插入的节点, 该节点将置于目标节点之后
  2459. * @return { Node } 新插入的节点
  2460. */
  2461. insertAfter: function(node, newNode) {
  2462. return node.nextSibling
  2463. ? node.parentNode.insertBefore(newNode, node.nextSibling)
  2464. : node.parentNode.appendChild(newNode);
  2465. },
  2466. /**
  2467. * 删除节点node及其下属的所有节点
  2468. * @method remove
  2469. * @param { Node } node 需要删除的节点对象
  2470. * @return { Node } 返回刚删除的节点对象
  2471. * @example
  2472. * ```html
  2473. * <div id="test">
  2474. * <div id="child">你好</div>
  2475. * </div>
  2476. * <script>
  2477. * UE.dom.domUtils.remove( document.body, false );
  2478. * //output: false
  2479. * console.log( document.getElementById( "child" ) !== null );
  2480. * </script>
  2481. * ```
  2482. */
  2483. /**
  2484. * 删除节点node,并根据keepChildren的值决定是否保留子节点
  2485. * @method remove
  2486. * @param { Node } node 需要删除的节点对象
  2487. * @param { Boolean } keepChildren 是否需要保留子节点
  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, true );
  2496. * //output: true
  2497. * console.log( document.getElementById( "child" ) !== null );
  2498. * </script>
  2499. * ```
  2500. */
  2501. remove: function(node, keepChildren) {
  2502. var parent = node.parentNode,
  2503. child;
  2504. if (parent) {
  2505. if (keepChildren && node.hasChildNodes()) {
  2506. while ((child = node.firstChild)) {
  2507. parent.insertBefore(child, node);
  2508. }
  2509. }
  2510. parent.removeChild(node);
  2511. }
  2512. return node;
  2513. },
  2514. /**
  2515. * 取得node节点的下一个兄弟节点, 如果该节点其后没有兄弟节点, 则递归查找其父节点之后的第一个兄弟节点,
  2516. * 直到找到满足条件的节点或者递归到BODY节点之后才会结束。
  2517. * @method getNextDomNode
  2518. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2519. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2520. * @example
  2521. * ```html
  2522. * <body>
  2523. * <div id="test">
  2524. * <span></span>
  2525. * </div>
  2526. * <i>xxx</i>
  2527. * </body>
  2528. * <script>
  2529. *
  2530. * //output: i节点
  2531. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2532. *
  2533. * </script>
  2534. * ```
  2535. * @example
  2536. * ```html
  2537. * <body>
  2538. * <div>
  2539. * <span></span>
  2540. * <i id="test">xxx</i>
  2541. * </div>
  2542. * <b>xxx</b>
  2543. * </body>
  2544. * <script>
  2545. *
  2546. * //由于id为test的i节点之后没有兄弟节点, 则查找其父节点(div)后面的兄弟节点
  2547. * //output: b节点
  2548. * console.log( UE.dom.domUtils.getNextDomNode( document.getElementById( "test" ) ) );
  2549. *
  2550. * </script>
  2551. * ```
  2552. */
  2553. /**
  2554. * 取得node节点的下一个兄弟节点, 如果startFromChild的值为ture,则先获取其子节点,
  2555. * 如果有子节点则直接返回第一个子节点;如果没有子节点或者startFromChild的值为false,
  2556. * 则执行<a href="#UE.dom.domUtils.getNextDomNode(Node)">getNextDomNode(Node node)</a>的查找过程。
  2557. * @method getNextDomNode
  2558. * @param { Node } node 需要获取其后的兄弟节点的节点对象
  2559. * @param { Boolean } startFromChild 查找过程是否从其子节点开始
  2560. * @return { Node | NULL } 如果找满足条件的节点, 则返回该节点, 否则返回NULL
  2561. * @see UE.dom.domUtils.getNextDomNode(Node)
  2562. */
  2563. getNextDomNode: function(node, startFromChild, filterFn, guard) {
  2564. return getDomNode(
  2565. node,
  2566. "firstChild",
  2567. "nextSibling",
  2568. startFromChild,
  2569. filterFn,
  2570. guard
  2571. );
  2572. },
  2573. getPreDomNode: function(node, startFromChild, filterFn, guard) {
  2574. return getDomNode(
  2575. node,
  2576. "lastChild",
  2577. "previousSibling",
  2578. startFromChild,
  2579. filterFn,
  2580. guard
  2581. );
  2582. },
  2583. /**
  2584. * 检测节点node是否属是UEditor定义的bookmark节点
  2585. * @method isBookmarkNode
  2586. * @private
  2587. * @param { Node } node 需要检测的节点对象
  2588. * @return { Boolean } 是否是bookmark节点
  2589. * @example
  2590. * ```html
  2591. * <span id="_baidu_bookmark_1"></span>
  2592. * <script>
  2593. * var bookmarkNode = document.getElementById("_baidu_bookmark_1");
  2594. * //output: true
  2595. * console.log( UE.dom.domUtils.isBookmarkNode( bookmarkNode ) );
  2596. * </script>
  2597. * ```
  2598. */
  2599. isBookmarkNode: function(node) {
  2600. return node.nodeType == 1 && node.id && /^_baidu_bookmark_/i.test(node.id);
  2601. },
  2602. /**
  2603. * 获取节点node所属的window对象
  2604. * @method getWindow
  2605. * @param { Node } node 节点对象
  2606. * @return { Window } 当前节点所属的window对象
  2607. * @example
  2608. * ```javascript
  2609. * //output: true
  2610. * console.log( UE.dom.domUtils.getWindow( document.body ) === window );
  2611. * ```
  2612. */
  2613. getWindow: function(node) {
  2614. var doc = node.ownerDocument || node;
  2615. return doc.defaultView || doc.parentWindow;
  2616. },
  2617. /**
  2618. * 获取离nodeA与nodeB最近的公共的祖先节点
  2619. * @method getCommonAncestor
  2620. * @param { Node } nodeA 第一个节点
  2621. * @param { Node } nodeB 第二个节点
  2622. * @remind 如果给定的两个节点是同一个节点, 将直接返回该节点。
  2623. * @return { Node | NULL } 如果未找到公共节点, 返回NULL, 否则返回最近的公共祖先节点。
  2624. * @example
  2625. * ```javascript
  2626. * var commonAncestor = UE.dom.domUtils.getCommonAncestor( document.body, document.body.firstChild );
  2627. * //output: true
  2628. * console.log( commonAncestor.tagName.toLowerCase() === 'body' );
  2629. * ```
  2630. */
  2631. getCommonAncestor: function(nodeA, nodeB) {
  2632. if (nodeA === nodeB) return nodeA;
  2633. var parentsA = [nodeA],
  2634. parentsB = [nodeB],
  2635. parent = nodeA,
  2636. i = -1;
  2637. while ((parent = parent.parentNode)) {
  2638. if (parent === nodeB) {
  2639. return parent;
  2640. }
  2641. parentsA.push(parent);
  2642. }
  2643. parent = nodeB;
  2644. while ((parent = parent.parentNode)) {
  2645. if (parent === nodeA) return parent;
  2646. parentsB.push(parent);
  2647. }
  2648. parentsA.reverse();
  2649. parentsB.reverse();
  2650. while ((i++, parentsA[i] === parentsB[i])) {}
  2651. return i == 0 ? null : parentsA[i - 1];
  2652. },
  2653. /**
  2654. * 清除node节点左右连续为空的兄弟inline节点
  2655. * @method clearEmptySibling
  2656. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2657. * 则这些兄弟节点将被删除
  2658. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext) //ignoreNext指定是否忽略右边空节点
  2659. * @grammar UE.dom.domUtils.clearEmptySibling(node,ignoreNext,ignorePre) //ignorePre指定是否忽略左边空节点
  2660. * @example
  2661. * ```html
  2662. * <body>
  2663. * <div></div>
  2664. * <span id="test"></span>
  2665. * <i></i>
  2666. * <b></b>
  2667. * <em>xxx</em>
  2668. * <span></span>
  2669. * </body>
  2670. * <script>
  2671. *
  2672. * UE.dom.domUtils.clearEmptySibling( document.getElementById( "test" ) );
  2673. *
  2674. * //output: <div></div><span id="test"></span><em>xxx</em><span></span>
  2675. * console.log( document.body.innerHTML );
  2676. *
  2677. * </script>
  2678. * ```
  2679. */
  2680. /**
  2681. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2682. * 则忽略对右边兄弟节点的操作。
  2683. * @method clearEmptySibling
  2684. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2685. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2686. * 则这些兄弟节点将被删除
  2687. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2688. */
  2689. /**
  2690. * 清除node节点左右连续为空的兄弟inline节点, 如果ignoreNext的值为true,
  2691. * 则忽略对右边兄弟节点的操作, 如果ignorePre的值为true,则忽略对左边兄弟节点的操作。
  2692. * @method clearEmptySibling
  2693. * @param { Node } node 执行的节点对象, 如果该节点的左右连续的兄弟节点是空的inline节点,
  2694. * @param { Boolean } ignoreNext 是否忽略忽略对右边的兄弟节点的操作
  2695. * @param { Boolean } ignorePre 是否忽略忽略对左边的兄弟节点的操作
  2696. * 则这些兄弟节点将被删除
  2697. * @see UE.dom.domUtils.clearEmptySibling(Node)
  2698. */
  2699. clearEmptySibling: function(node, ignoreNext, ignorePre) {
  2700. function clear(next, dir) {
  2701. var tmpNode;
  2702. while (
  2703. next &&
  2704. !domUtils.isBookmarkNode(next) &&
  2705. (domUtils.isEmptyInlineElement(next) ||
  2706. //这里不能把空格算进来会吧空格干掉,出现文字间的空格丢掉了
  2707. !new RegExp("[^\t\n\r" + domUtils.fillChar + "]").test(
  2708. next.nodeValue
  2709. ))
  2710. ) {
  2711. tmpNode = next[dir];
  2712. domUtils.remove(next);
  2713. next = tmpNode;
  2714. }
  2715. }
  2716. !ignoreNext && clear(node.nextSibling, "nextSibling");
  2717. !ignorePre && clear(node.previousSibling, "previousSibling");
  2718. },
  2719. /**
  2720. * 将一个文本节点textNode拆分成两个文本节点,offset指定拆分位置
  2721. * @method split
  2722. * @param { Node } textNode 需要拆分的文本节点对象
  2723. * @param { int } offset 需要拆分的位置, 位置计算从0开始
  2724. * @return { Node } 拆分后形成的新节点
  2725. * @example
  2726. * ```html
  2727. * <div id="test">abcdef</div>
  2728. * <script>
  2729. * var newNode = UE.dom.domUtils.split( document.getElementById( "test" ).firstChild, 3 );
  2730. * //output: def
  2731. * console.log( newNode.nodeValue );
  2732. * </script>
  2733. * ```
  2734. */
  2735. split: function(node, offset) {
  2736. var doc = node.ownerDocument;
  2737. if (browser.ie && offset == node.nodeValue.length) {
  2738. var next = doc.createTextNode("");
  2739. return domUtils.insertAfter(node, next);
  2740. }
  2741. var retval = node.splitText(offset);
  2742. //ie8下splitText不会跟新childNodes,我们手动触发他的更新
  2743. if (browser.ie8) {
  2744. var tmpNode = doc.createTextNode("");
  2745. domUtils.insertAfter(retval, tmpNode);
  2746. domUtils.remove(tmpNode);
  2747. }
  2748. return retval;
  2749. },
  2750. /**
  2751. * 检测文本节点textNode是否为空节点(包括空格、换行、占位符等字符)
  2752. * @method isWhitespace
  2753. * @param { Node } node 需要检测的节点对象
  2754. * @return { Boolean } 检测的节点是否为空
  2755. * @example
  2756. * ```html
  2757. * <div id="test">
  2758. *
  2759. * </div>
  2760. * <script>
  2761. * //output: true
  2762. * console.log( UE.dom.domUtils.isWhitespace( document.getElementById("test").firstChild ) );
  2763. * </script>
  2764. * ```
  2765. */
  2766. isWhitespace: function(node) {
  2767. return !new RegExp("[^ \t\n\r" + domUtils.fillChar + "]").test(
  2768. node.nodeValue
  2769. );
  2770. },
  2771. /**
  2772. * 获取元素element相对于viewport的位置坐标
  2773. * @method getXY
  2774. * @param { Node } element 需要计算位置的节点对象
  2775. * @return { Object } 返回形如{x:left,y:top}的一个key-value映射对象, 其中键x代表水平偏移距离,
  2776. * y代表垂直偏移距离。
  2777. *
  2778. * @example
  2779. * ```javascript
  2780. * var location = UE.dom.domUtils.getXY( document.getElementById("test") );
  2781. * //output: test的坐标为: 12, 24
  2782. * console.log( 'test的坐标为: ', location.x, ',', location.y );
  2783. * ```
  2784. */
  2785. getXY: function(element) {
  2786. var x = 0,
  2787. y = 0;
  2788. while (element.offsetParent) {
  2789. y += element.offsetTop;
  2790. x += element.offsetLeft;
  2791. element = element.offsetParent;
  2792. }
  2793. return { x: x, y: y };
  2794. },
  2795. /**
  2796. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2797. * @method on
  2798. * @param { Node } element 需要绑定事件的节点对象
  2799. * @param { String } type 绑定的事件类型
  2800. * @param { Function } handler 事件处理器
  2801. * @example
  2802. * ```javascript
  2803. * UE.dom.domUtils.on(document.body,"click",function(e){
  2804. * //e为事件对象,this为被点击元素对戏那个
  2805. * });
  2806. * ```
  2807. */
  2808. /**
  2809. * 为元素element绑定原生DOM事件,type为事件类型,handler为处理函数
  2810. * @method on
  2811. * @param { Node } element 需要绑定事件的节点对象
  2812. * @param { Array } type 绑定的事件类型数组
  2813. * @param { Function } handler 事件处理器
  2814. * @example
  2815. * ```javascript
  2816. * UE.dom.domUtils.on(document.body,["click","mousedown"],function(evt){
  2817. * //evt为事件对象,this为被点击元素对象
  2818. * });
  2819. * ```
  2820. */
  2821. on: function(element, type, handler) {
  2822. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2823. k = types.length;
  2824. if (k)
  2825. while (k--) {
  2826. type = types[k];
  2827. if (element.addEventListener) {
  2828. element.addEventListener(type, handler, false);
  2829. } else {
  2830. if (!handler._d) {
  2831. handler._d = {
  2832. els: []
  2833. };
  2834. }
  2835. var key = type + handler.toString(),
  2836. index = utils.indexOf(handler._d.els, element);
  2837. if (!handler._d[key] || index == -1) {
  2838. if (index == -1) {
  2839. handler._d.els.push(element);
  2840. }
  2841. if (!handler._d[key]) {
  2842. handler._d[key] = function(evt) {
  2843. return handler.call(evt.srcElement, evt || window.event);
  2844. };
  2845. }
  2846. element.attachEvent("on" + type, handler._d[key]);
  2847. }
  2848. }
  2849. }
  2850. element = null;
  2851. },
  2852. /**
  2853. * 解除DOM事件绑定
  2854. * @method un
  2855. * @param { Node } element 需要解除事件绑定的节点对象
  2856. * @param { String } type 需要接触绑定的事件类型
  2857. * @param { Function } handler 对应的事件处理器
  2858. * @example
  2859. * ```javascript
  2860. * UE.dom.domUtils.un(document.body,"click",function(evt){
  2861. * //evt为事件对象,this为被点击元素对象
  2862. * });
  2863. * ```
  2864. */
  2865. /**
  2866. * 解除DOM事件绑定
  2867. * @method un
  2868. * @param { Node } element 需要解除事件绑定的节点对象
  2869. * @param { Array } type 需要接触绑定的事件类型数组
  2870. * @param { Function } handler 对应的事件处理器
  2871. * @example
  2872. * ```javascript
  2873. * UE.dom.domUtils.un(document.body, ["click","mousedown"],function(evt){
  2874. * //evt为事件对象,this为被点击元素对象
  2875. * });
  2876. * ```
  2877. */
  2878. un: function(element, type, handler) {
  2879. var types = utils.isArray(type) ? type : utils.trim(type).split(/\s+/),
  2880. k = types.length;
  2881. if (k)
  2882. while (k--) {
  2883. type = types[k];
  2884. if (element.removeEventListener) {
  2885. element.removeEventListener(type, handler, false);
  2886. } else {
  2887. var key = type + handler.toString();
  2888. try {
  2889. element.detachEvent(
  2890. "on" + type,
  2891. handler._d ? handler._d[key] : handler
  2892. );
  2893. } catch (e) {}
  2894. if (handler._d && handler._d[key]) {
  2895. var index = utils.indexOf(handler._d.els, element);
  2896. if (index != -1) {
  2897. handler._d.els.splice(index, 1);
  2898. }
  2899. handler._d.els.length == 0 && delete handler._d[key];
  2900. }
  2901. }
  2902. }
  2903. },
  2904. /**
  2905. * 比较节点nodeA与节点nodeB是否具有相同的标签名、属性名以及属性值
  2906. * @method isSameElement
  2907. * @param { Node } nodeA 需要比较的节点
  2908. * @param { Node } nodeB 需要比较的节点
  2909. * @return { Boolean } 两个节点是否具有相同的标签名、属性名以及属性值
  2910. * @example
  2911. * ```html
  2912. * <span style="font-size:12px">ssss</span>
  2913. * <span style="font-size:12px">bbbbb</span>
  2914. * <span style="font-size:13px">ssss</span>
  2915. * <span style="font-size:14px">bbbbb</span>
  2916. *
  2917. * <script>
  2918. *
  2919. * var nodes = document.getElementsByTagName( "span" );
  2920. *
  2921. * //output: true
  2922. * console.log( UE.dom.domUtils.isSameElement( nodes[0], nodes[1] ) );
  2923. *
  2924. * //output: false
  2925. * console.log( UE.dom.domUtils.isSameElement( nodes[2], nodes[3] ) );
  2926. *
  2927. * </script>
  2928. * ```
  2929. */
  2930. isSameElement: function(nodeA, nodeB) {
  2931. if (nodeA.tagName != nodeB.tagName) {
  2932. return false;
  2933. }
  2934. var thisAttrs = nodeA.attributes,
  2935. otherAttrs = nodeB.attributes;
  2936. if (!ie && thisAttrs.length != otherAttrs.length) {
  2937. return false;
  2938. }
  2939. var attrA,
  2940. attrB,
  2941. al = 0,
  2942. bl = 0;
  2943. for (var i = 0; (attrA = thisAttrs[i++]); ) {
  2944. if (attrA.nodeName == "style") {
  2945. if (attrA.specified) {
  2946. al++;
  2947. }
  2948. if (domUtils.isSameStyle(nodeA, nodeB)) {
  2949. continue;
  2950. } else {
  2951. return false;
  2952. }
  2953. }
  2954. if (ie) {
  2955. if (attrA.specified) {
  2956. al++;
  2957. attrB = otherAttrs.getNamedItem(attrA.nodeName);
  2958. } else {
  2959. continue;
  2960. }
  2961. } else {
  2962. attrB = nodeB.attributes[attrA.nodeName];
  2963. }
  2964. if (!attrB.specified || attrA.nodeValue != attrB.nodeValue) {
  2965. return false;
  2966. }
  2967. }
  2968. // 有可能attrB的属性包含了attrA的属性之外还有自己的属性
  2969. if (ie) {
  2970. for (i = 0; (attrB = otherAttrs[i++]); ) {
  2971. if (attrB.specified) {
  2972. bl++;
  2973. }
  2974. }
  2975. if (al != bl) {
  2976. return false;
  2977. }
  2978. }
  2979. return true;
  2980. },
  2981. /**
  2982. * 判断节点nodeA与节点nodeB的元素的style属性是否一致
  2983. * @method isSameStyle
  2984. * @param { Node } nodeA 需要比较的节点
  2985. * @param { Node } nodeB 需要比较的节点
  2986. * @return { Boolean } 两个节点是否具有相同的style属性值
  2987. * @example
  2988. * ```html
  2989. * <span style="font-size:12px">ssss</span>
  2990. * <span style="font-size:12px">bbbbb</span>
  2991. * <span style="font-size:13px">ssss</span>
  2992. * <span style="font-size:14px">bbbbb</span>
  2993. *
  2994. * <script>
  2995. *
  2996. * var nodes = document.getElementsByTagName( "span" );
  2997. *
  2998. * //output: true
  2999. * console.log( UE.dom.domUtils.isSameStyle( nodes[0], nodes[1] ) );
  3000. *
  3001. * //output: false
  3002. * console.log( UE.dom.domUtils.isSameStyle( nodes[2], nodes[3] ) );
  3003. *
  3004. * </script>
  3005. * ```
  3006. */
  3007. isSameStyle: function(nodeA, nodeB) {
  3008. var styleA = nodeA.style.cssText
  3009. .replace(/( ?; ?)/g, ";")
  3010. .replace(/( ?: ?)/g, ":"),
  3011. styleB = nodeB.style.cssText
  3012. .replace(/( ?; ?)/g, ";")
  3013. .replace(/( ?: ?)/g, ":");
  3014. if (browser.opera) {
  3015. styleA = nodeA.style;
  3016. styleB = nodeB.style;
  3017. if (styleA.length != styleB.length) return false;
  3018. for (var p in styleA) {
  3019. if (/^(\d+|csstext)$/i.test(p)) {
  3020. continue;
  3021. }
  3022. if (styleA[p] != styleB[p]) {
  3023. return false;
  3024. }
  3025. }
  3026. return true;
  3027. }
  3028. if (!styleA || !styleB) {
  3029. return styleA == styleB;
  3030. }
  3031. styleA = styleA.split(";");
  3032. styleB = styleB.split(";");
  3033. if (styleA.length != styleB.length) {
  3034. return false;
  3035. }
  3036. for (var i = 0, ci; (ci = styleA[i++]); ) {
  3037. if (utils.indexOf(styleB, ci) == -1) {
  3038. return false;
  3039. }
  3040. }
  3041. return true;
  3042. },
  3043. /**
  3044. * 检查节点node是否为block元素
  3045. * @method isBlockElm
  3046. * @param { Node } node 需要检测的节点对象
  3047. * @return { Boolean } 是否是block元素节点
  3048. * @warning 该方法的判断规则如下: 如果该元素原本是block元素, 则不论该元素当前的css样式是什么都会返回true;
  3049. * 否则,检测该元素的css样式, 如果该元素当前是block元素, 则返回true。 其余情况下都返回false。
  3050. * @example
  3051. * ```html
  3052. * <span id="test1" style="display: block"></span>
  3053. * <span id="test2"></span>
  3054. * <div id="test3" style="display: inline"></div>
  3055. *
  3056. * <script>
  3057. *
  3058. * //output: true
  3059. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test1") ) );
  3060. *
  3061. * //output: false
  3062. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test2") ) );
  3063. *
  3064. * //output: true
  3065. * console.log( UE.dom.domUtils.isBlockElm( document.getElementById("test3") ) );
  3066. *
  3067. * </script>
  3068. * ```
  3069. */
  3070. isBlockElm: function(node) {
  3071. return (
  3072. node.nodeType == 1 &&
  3073. (dtd.$block[node.tagName] ||
  3074. styleBlock[domUtils.getComputedStyle(node, "display")]) &&
  3075. !dtd.$nonChild[node.tagName]
  3076. );
  3077. },
  3078. /**
  3079. * 检测node节点是否为body节点
  3080. * @method isBody
  3081. * @param { Element } node 需要检测的dom元素
  3082. * @return { Boolean } 给定的元素是否是body元素
  3083. * @example
  3084. * ```javascript
  3085. * //output: true
  3086. * console.log( UE.dom.domUtils.isBody( document.body ) );
  3087. * ```
  3088. */
  3089. isBody: function(node) {
  3090. return node && node.nodeType == 1 && node.tagName.toLowerCase() == "body";
  3091. },
  3092. /**
  3093. * 以node节点为分界,将该节点的指定祖先节点parent拆分成两个独立的节点,
  3094. * 拆分形成的两个节点之间是node节点
  3095. * @method breakParent
  3096. * @param { Node } node 作为分界的节点对象
  3097. * @param { Node } parent 该节点必须是node节点的祖先节点, 且是block节点。
  3098. * @return { Node } 给定的node分界节点
  3099. * @example
  3100. * ```javascript
  3101. *
  3102. * var node = document.createElement("span"),
  3103. * wrapNode = document.createElement( "div" ),
  3104. * parent = document.createElement("p");
  3105. *
  3106. * parent.appendChild( node );
  3107. * wrapNode.appendChild( parent );
  3108. *
  3109. * //拆分前
  3110. * //output: <p><span></span></p>
  3111. * console.log( wrapNode.innerHTML );
  3112. *
  3113. *
  3114. * UE.dom.domUtils.breakParent( node, parent );
  3115. * //拆分后
  3116. * //output: <p></p><span></span><p></p>
  3117. * console.log( wrapNode.innerHTML );
  3118. *
  3119. * ```
  3120. */
  3121. breakParent: function(node, parent) {
  3122. var tmpNode,
  3123. parentClone = node,
  3124. clone = node,
  3125. leftNodes,
  3126. rightNodes;
  3127. do {
  3128. parentClone = parentClone.parentNode;
  3129. if (leftNodes) {
  3130. tmpNode = parentClone.cloneNode(false);
  3131. tmpNode.appendChild(leftNodes);
  3132. leftNodes = tmpNode;
  3133. tmpNode = parentClone.cloneNode(false);
  3134. tmpNode.appendChild(rightNodes);
  3135. rightNodes = tmpNode;
  3136. } else {
  3137. leftNodes = parentClone.cloneNode(false);
  3138. rightNodes = leftNodes.cloneNode(false);
  3139. }
  3140. while ((tmpNode = clone.previousSibling)) {
  3141. leftNodes.insertBefore(tmpNode, leftNodes.firstChild);
  3142. }
  3143. while ((tmpNode = clone.nextSibling)) {
  3144. rightNodes.appendChild(tmpNode);
  3145. }
  3146. clone = parentClone;
  3147. } while (parent !== parentClone);
  3148. tmpNode = parent.parentNode;
  3149. tmpNode.insertBefore(leftNodes, parent);
  3150. tmpNode.insertBefore(rightNodes, parent);
  3151. tmpNode.insertBefore(node, rightNodes);
  3152. domUtils.remove(parent);
  3153. return node;
  3154. },
  3155. /**
  3156. * 检查节点node是否是空inline节点
  3157. * @method isEmptyInlineElement
  3158. * @param { Node } node 需要检测的节点对象
  3159. * @return { Number } 如果给定的节点是空的inline节点, 则返回1, 否则返回0。
  3160. * @example
  3161. * ```html
  3162. * <b><i></i></b> => 1
  3163. * <b><i></i><u></u></b> => 1
  3164. * <b></b> => 1
  3165. * <b>xx<i></i></b> => 0
  3166. * ```
  3167. */
  3168. isEmptyInlineElement: function(node) {
  3169. if (node.nodeType != 1 || !dtd.$removeEmpty[node.tagName]) {
  3170. return 0;
  3171. }
  3172. node = node.firstChild;
  3173. while (node) {
  3174. //如果是创建的bookmark就跳过
  3175. if (domUtils.isBookmarkNode(node)) {
  3176. return 0;
  3177. }
  3178. if (
  3179. (node.nodeType == 1 && !domUtils.isEmptyInlineElement(node)) ||
  3180. (node.nodeType == 3 && !domUtils.isWhitespace(node))
  3181. ) {
  3182. return 0;
  3183. }
  3184. node = node.nextSibling;
  3185. }
  3186. return 1;
  3187. },
  3188. /**
  3189. * 删除node节点下首尾两端的空白文本子节点
  3190. * @method trimWhiteTextNode
  3191. * @param { Element } node 需要执行删除操作的元素对象
  3192. * @example
  3193. * ```javascript
  3194. * var node = document.createElement("div");
  3195. *
  3196. * node.appendChild( document.createTextNode( "" ) );
  3197. *
  3198. * node.appendChild( document.createElement("div") );
  3199. *
  3200. * node.appendChild( document.createTextNode( "" ) );
  3201. *
  3202. * //3
  3203. * console.log( node.childNodes.length );
  3204. *
  3205. * UE.dom.domUtils.trimWhiteTextNode( node );
  3206. *
  3207. * //1
  3208. * console.log( node.childNodes.length );
  3209. * ```
  3210. */
  3211. trimWhiteTextNode: function(node) {
  3212. function remove(dir) {
  3213. var child;
  3214. while (
  3215. (child = node[dir]) &&
  3216. child.nodeType == 3 &&
  3217. domUtils.isWhitespace(child)
  3218. ) {
  3219. node.removeChild(child);
  3220. }
  3221. }
  3222. remove("firstChild");
  3223. remove("lastChild");
  3224. },
  3225. /**
  3226. * 合并node节点下相同的子节点
  3227. * @name mergeChild
  3228. * @desc
  3229. * UE.dom.domUtils.mergeChild(node,tagName) //tagName要合并的子节点的标签
  3230. * @example
  3231. * <p><span style="font-size:12px;">xx<span style="font-size:12px;">aa</span>xx</span></p>
  3232. * ==> UE.dom.domUtils.mergeChild(node,'span')
  3233. * <p><span style="font-size:12px;">xxaaxx</span></p>
  3234. */
  3235. mergeChild: function(node, tagName, attrs) {
  3236. var list = domUtils.getElementsByTagName(node, node.tagName.toLowerCase());
  3237. for (var i = 0, ci; (ci = list[i++]); ) {
  3238. if (!ci.parentNode || domUtils.isBookmarkNode(ci)) {
  3239. continue;
  3240. }
  3241. //span单独处理
  3242. if (ci.tagName.toLowerCase() == "span") {
  3243. if (node === ci.parentNode) {
  3244. domUtils.trimWhiteTextNode(node);
  3245. if (node.childNodes.length == 1) {
  3246. node.style.cssText = ci.style.cssText + ";" + node.style.cssText;
  3247. domUtils.remove(ci, true);
  3248. continue;
  3249. }
  3250. }
  3251. ci.style.cssText = node.style.cssText + ";" + ci.style.cssText;
  3252. if (attrs) {
  3253. var style = attrs.style;
  3254. if (style) {
  3255. style = style.split(";");
  3256. for (var j = 0, s; (s = style[j++]); ) {
  3257. ci.style[utils.cssStyleToDomStyle(s.split(":")[0])] = s.split(
  3258. ":"
  3259. )[1];
  3260. }
  3261. }
  3262. }
  3263. if (domUtils.isSameStyle(ci, node)) {
  3264. domUtils.remove(ci, true);
  3265. }
  3266. continue;
  3267. }
  3268. if (domUtils.isSameElement(node, ci)) {
  3269. domUtils.remove(ci, true);
  3270. }
  3271. }
  3272. },
  3273. /**
  3274. * 原生方法getElementsByTagName的封装
  3275. * @method getElementsByTagName
  3276. * @param { Node } node 目标节点对象
  3277. * @param { String } tagName 需要查找的节点的tagName, 多个tagName以空格分割
  3278. * @return { Array } 符合条件的节点集合
  3279. */
  3280. getElementsByTagName: function(node, name, filter) {
  3281. if (filter && utils.isString(filter)) {
  3282. var className = filter;
  3283. filter = function(node) {
  3284. return domUtils.hasClass(node, className);
  3285. };
  3286. }
  3287. name = utils.trim(name).replace(/[ ]{2,}/g, " ").split(" ");
  3288. var arr = [];
  3289. for (var n = 0, ni; (ni = name[n++]); ) {
  3290. var list = node.getElementsByTagName(ni);
  3291. for (var i = 0, ci; (ci = list[i++]); ) {
  3292. if (!filter || filter(ci)) arr.push(ci);
  3293. }
  3294. }
  3295. return arr;
  3296. },
  3297. /**
  3298. * 将节点node提取到父节点上
  3299. * @method mergeToParent
  3300. * @param { Element } node 需要提取的元素对象
  3301. * @example
  3302. * ```html
  3303. * <div id="parent">
  3304. * <div id="sub">
  3305. * <span id="child"></span>
  3306. * </div>
  3307. * </div>
  3308. *
  3309. * <script>
  3310. *
  3311. * var child = document.getElementById( "child" );
  3312. *
  3313. * //output: sub
  3314. * console.log( child.parentNode.id );
  3315. *
  3316. * UE.dom.domUtils.mergeToParent( child );
  3317. *
  3318. * //output: parent
  3319. * console.log( child.parentNode.id );
  3320. *
  3321. * </script>
  3322. * ```
  3323. */
  3324. mergeToParent: function(node) {
  3325. var parent = node.parentNode;
  3326. while (parent && dtd.$removeEmpty[parent.tagName]) {
  3327. if (parent.tagName == node.tagName || parent.tagName == "A") {
  3328. //针对a标签单独处理
  3329. domUtils.trimWhiteTextNode(parent);
  3330. //span需要特殊处理 不处理这样的情况 <span stlye="color:#fff">xxx<span style="color:#ccc">xxx</span>xxx</span>
  3331. if (
  3332. (parent.tagName == "SPAN" && !domUtils.isSameStyle(parent, node)) ||
  3333. (parent.tagName == "A" && node.tagName == "SPAN")
  3334. ) {
  3335. if (parent.childNodes.length > 1 || parent !== node.parentNode) {
  3336. node.style.cssText =
  3337. parent.style.cssText + ";" + node.style.cssText;
  3338. parent = parent.parentNode;
  3339. continue;
  3340. } else {
  3341. parent.style.cssText += ";" + node.style.cssText;
  3342. //trace:952 a标签要保持下划线
  3343. if (parent.tagName == "A") {
  3344. parent.style.textDecoration = "underline";
  3345. }
  3346. }
  3347. }
  3348. if (parent.tagName != "A") {
  3349. parent === node.parentNode && domUtils.remove(node, true);
  3350. break;
  3351. }
  3352. }
  3353. parent = parent.parentNode;
  3354. }
  3355. },
  3356. /**
  3357. * 合并节点node的左右兄弟节点
  3358. * @method mergeSibling
  3359. * @param { Element } node 需要合并的目标节点
  3360. * @example
  3361. * ```html
  3362. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3363. *
  3364. * <script>
  3365. * var demoNode = document.getElementById("test");
  3366. * UE.dom.domUtils.mergeSibling( demoNode );
  3367. * //output: xxxxoooxxxx
  3368. * console.log( demoNode.innerHTML );
  3369. * </script>
  3370. * ```
  3371. */
  3372. /**
  3373. * 合并节点node的左右兄弟节点, 可以根据给定的条件选择是否忽略合并左节点。
  3374. * @method mergeSibling
  3375. * @param { Element } node 需要合并的目标节点
  3376. * @param { Boolean } ignorePre 是否忽略合并左节点
  3377. * @example
  3378. * ```html
  3379. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3380. *
  3381. * <script>
  3382. * var demoNode = document.getElementById("test");
  3383. * UE.dom.domUtils.mergeSibling( demoNode, true );
  3384. * //output: oooxxxx
  3385. * console.log( demoNode.innerHTML );
  3386. * </script>
  3387. * ```
  3388. */
  3389. /**
  3390. * 合并节点node的左右兄弟节点,可以根据给定的条件选择是否忽略合并左右节点。
  3391. * @method mergeSibling
  3392. * @param { Element } node 需要合并的目标节点
  3393. * @param { Boolean } ignorePre 是否忽略合并左节点
  3394. * @param { Boolean } ignoreNext 是否忽略合并右节点
  3395. * @remind 如果同时忽略左右节点, 则该操作什么也不会做
  3396. * @example
  3397. * ```html
  3398. * <b>xxxx</b><b id="test">ooo</b><b>xxxx</b>
  3399. *
  3400. * <script>
  3401. * var demoNode = document.getElementById("test");
  3402. * UE.dom.domUtils.mergeSibling( demoNode, false, true );
  3403. * //output: xxxxooo
  3404. * console.log( demoNode.innerHTML );
  3405. * </script>
  3406. * ```
  3407. */
  3408. mergeSibling: function(node, ignorePre, ignoreNext) {
  3409. function merge(rtl, start, node) {
  3410. var next;
  3411. if (
  3412. (next = node[rtl]) &&
  3413. !domUtils.isBookmarkNode(next) &&
  3414. next.nodeType == 1 &&
  3415. domUtils.isSameElement(node, next)
  3416. ) {
  3417. while (next.firstChild) {
  3418. if (start == "firstChild") {
  3419. node.insertBefore(next.lastChild, node.firstChild);
  3420. } else {
  3421. node.appendChild(next.firstChild);
  3422. }
  3423. }
  3424. domUtils.remove(next);
  3425. }
  3426. }
  3427. !ignorePre && merge("previousSibling", "firstChild", node);
  3428. !ignoreNext && merge("nextSibling", "lastChild", node);
  3429. },
  3430. /**
  3431. * 设置节点node及其子节点不会被选中
  3432. * @method unSelectable
  3433. * @param { Element } node 需要执行操作的dom元素
  3434. * @remind 执行该操作后的节点, 将不能被鼠标选中
  3435. * @example
  3436. * ```javascript
  3437. * UE.dom.domUtils.unSelectable( document.body );
  3438. * ```
  3439. */
  3440. unSelectable: (ie && browser.ie9below) || browser.opera
  3441. ? function(node) {
  3442. //for ie9
  3443. node.onselectstart = function() {
  3444. return false;
  3445. };
  3446. node.onclick = node.onkeyup = node.onkeydown = function() {
  3447. return false;
  3448. };
  3449. node.unselectable = "on";
  3450. node.setAttribute("unselectable", "on");
  3451. for (var i = 0, ci; (ci = node.all[i++]); ) {
  3452. switch (ci.tagName.toLowerCase()) {
  3453. case "iframe":
  3454. case "textarea":
  3455. case "input":
  3456. case "select":
  3457. break;
  3458. default:
  3459. ci.unselectable = "on";
  3460. node.setAttribute("unselectable", "on");
  3461. }
  3462. }
  3463. }
  3464. : function(node) {
  3465. node.style.MozUserSelect = node.style.webkitUserSelect = node.style.msUserSelect = node.style.KhtmlUserSelect =
  3466. "none";
  3467. },
  3468. /**
  3469. * 删除节点node上的指定属性名称的属性
  3470. * @method removeAttributes
  3471. * @param { Node } node 需要删除属性的节点对象
  3472. * @param { String } attrNames 可以是空格隔开的多个属性名称,该操作将会依次删除相应的属性
  3473. * @example
  3474. * ```html
  3475. * <div id="wrap">
  3476. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3477. * </div>
  3478. *
  3479. * <script>
  3480. *
  3481. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), "id name" );
  3482. *
  3483. * //output: <span style="font-size:14px;">xxxxx</span>
  3484. * console.log( document.getElementById("wrap").innerHTML );
  3485. *
  3486. * </script>
  3487. * ```
  3488. */
  3489. /**
  3490. * 删除节点node上的指定属性名称的属性
  3491. * @method removeAttributes
  3492. * @param { Node } node 需要删除属性的节点对象
  3493. * @param { Array } attrNames 需要删除的属性名数组
  3494. * @example
  3495. * ```html
  3496. * <div id="wrap">
  3497. * <span style="font-size:14px;" id="test" name="followMe">xxxxx</span>
  3498. * </div>
  3499. *
  3500. * <script>
  3501. *
  3502. * UE.dom.domUtils.removeAttributes( document.getElementById( "test" ), ["id", "name"] );
  3503. *
  3504. * //output: <span style="font-size:14px;">xxxxx</span>
  3505. * console.log( document.getElementById("wrap").innerHTML );
  3506. *
  3507. * </script>
  3508. * ```
  3509. */
  3510. removeAttributes: function(node, attrNames) {
  3511. attrNames = utils.isArray(attrNames)
  3512. ? attrNames
  3513. : utils.trim(attrNames).replace(/[ ]{2,}/g, " ").split(" ");
  3514. for (var i = 0, ci; (ci = attrNames[i++]); ) {
  3515. ci = attrFix[ci] || ci;
  3516. switch (ci) {
  3517. case "className":
  3518. node[ci] = "";
  3519. break;
  3520. case "style":
  3521. node.style.cssText = "";
  3522. var val = node.getAttributeNode("style");
  3523. !browser.ie && val && node.removeAttributeNode(val);
  3524. }
  3525. node.removeAttribute(ci);
  3526. }
  3527. },
  3528. /**
  3529. * 在doc下创建一个标签名为tag,属性为attrs的元素
  3530. * @method createElement
  3531. * @param { DomDocument } doc 新创建的元素属于该document节点创建
  3532. * @param { String } tagName 需要创建的元素的标签名
  3533. * @param { Object } attrs 新创建的元素的属性key-value集合
  3534. * @return { Element } 新创建的元素对象
  3535. * @example
  3536. * ```javascript
  3537. * var ele = UE.dom.domUtils.createElement( document, 'div', {
  3538. * id: 'test'
  3539. * } );
  3540. *
  3541. * //output: DIV
  3542. * console.log( ele.tagName );
  3543. *
  3544. * //output: test
  3545. * console.log( ele.id );
  3546. *
  3547. * ```
  3548. */
  3549. createElement: function(doc, tag, attrs) {
  3550. return domUtils.setAttributes(doc.createElement(tag), attrs);
  3551. },
  3552. /**
  3553. * 为节点node添加属性attrs,attrs为属性键值对
  3554. * @method setAttributes
  3555. * @param { Element } node 需要设置属性的元素对象
  3556. * @param { Object } attrs 需要设置的属性名-值对
  3557. * @return { Element } 设置属性的元素对象
  3558. * @example
  3559. * ```html
  3560. * <span id="test"></span>
  3561. *
  3562. * <script>
  3563. *
  3564. * var testNode = UE.dom.domUtils.setAttributes( document.getElementById( "test" ), {
  3565. * id: 'demo'
  3566. * } );
  3567. *
  3568. * //output: demo
  3569. * console.log( testNode.id );
  3570. *
  3571. * </script>
  3572. *
  3573. */
  3574. setAttributes: function(node, attrs) {
  3575. for (var attr in attrs) {
  3576. if('_propertyDelete'===attr){
  3577. for(var j=0;j<attrs[attr].length;j++){
  3578. if(node.hasAttribute(attrs[attr][j])){
  3579. node.removeAttribute(attrs[attr][j]);
  3580. }
  3581. }
  3582. continue;
  3583. }
  3584. if (attrs.hasOwnProperty(attr)) {
  3585. var value = attrs[attr];
  3586. switch (attr) {
  3587. case "class":
  3588. //ie下要这样赋值,setAttribute不起作用
  3589. node.className = value;
  3590. break;
  3591. case "style":
  3592. node.style.cssText = node.style.cssText + ";" + value;
  3593. break;
  3594. case "innerHTML":
  3595. node[attr] = value;
  3596. break;
  3597. case "value":
  3598. node.value = value;
  3599. break;
  3600. default:
  3601. node.setAttribute(attrFix[attr] || attr, value);
  3602. }
  3603. }
  3604. }
  3605. return node;
  3606. },
  3607. /**
  3608. * 获取元素element经过计算后的样式值
  3609. * @method getComputedStyle
  3610. * @param { Element } element 需要获取样式的元素对象
  3611. * @param { String } styleName 需要获取的样式名
  3612. * @return { String } 获取到的样式值
  3613. * @example
  3614. * ```html
  3615. * <style type="text/css">
  3616. * #test {
  3617. * font-size: 15px;
  3618. * }
  3619. * </style>
  3620. *
  3621. * <span id="test"></span>
  3622. *
  3623. * <script>
  3624. * //output: 15px
  3625. * console.log( UE.dom.domUtils.getComputedStyle( document.getElementById( "test" ), 'font-size' ) );
  3626. * </script>
  3627. * ```
  3628. */
  3629. getComputedStyle: function(element, styleName) {
  3630. //以下的属性单独处理
  3631. var pros = "width height top left";
  3632. if (pros.indexOf(styleName) > -1) {
  3633. return (
  3634. element[
  3635. "offset" +
  3636. styleName.replace(/^\w/, function(s) {
  3637. return s.toUpperCase();
  3638. })
  3639. ] + "px"
  3640. );
  3641. }
  3642. //忽略文本节点
  3643. if (element.nodeType === 3) {
  3644. element = element.parentNode;
  3645. }
  3646. //ie下font-size若body下定义了font-size,则从currentStyle里会取到这个font-size. 取不到实际值,故此修改.
  3647. if (
  3648. browser.ie &&
  3649. browser.version < 9 &&
  3650. styleName === "font-size" &&
  3651. !element.style.fontSize &&
  3652. !dtd.$empty[element.tagName] &&
  3653. !dtd.$nonChild[element.tagName]
  3654. ) {
  3655. var span = element.ownerDocument.createElement("span");
  3656. span.style.cssText = "padding:0;border:0;font-family:simsun;";
  3657. span.innerHTML = ".";
  3658. element.appendChild(span);
  3659. var result = span.offsetHeight;
  3660. element.removeChild(span);
  3661. span = null;
  3662. return result + "px";
  3663. }
  3664. try {
  3665. var value =
  3666. domUtils.getStyle(element, styleName) ||
  3667. (window.getComputedStyle
  3668. ? domUtils
  3669. .getWindow(element)
  3670. .getComputedStyle(element, "")
  3671. .getPropertyValue(styleName)
  3672. : (element.currentStyle || element.style)[
  3673. utils.cssStyleToDomStyle(styleName)
  3674. ]);
  3675. } catch (e) {
  3676. return "";
  3677. }
  3678. return utils.transUnitToPx(utils.fixColor(styleName, value));
  3679. },
  3680. /**
  3681. * 删除元素element指定的className
  3682. * @method removeClasses
  3683. * @param { Element } ele 需要删除class的元素节点
  3684. * @param { String } classNames 需要删除的className, 多个className之间以空格分开
  3685. * @example
  3686. * ```html
  3687. * <span id="test" class="test1 test2 test3">xxx</span>
  3688. *
  3689. * <script>
  3690. *
  3691. * var testNode = document.getElementById( "test" );
  3692. * UE.dom.domUtils.removeClasses( testNode, "test1 test2" );
  3693. *
  3694. * //output: test3
  3695. * console.log( testNode.className );
  3696. *
  3697. * </script>
  3698. * ```
  3699. */
  3700. /**
  3701. * 删除元素element指定的className
  3702. * @method removeClasses
  3703. * @param { Element } ele 需要删除class的元素节点
  3704. * @param { Array } classNames 需要删除的className数组
  3705. * @example
  3706. * ```html
  3707. * <span id="test" class="test1 test2 test3">xxx</span>
  3708. *
  3709. * <script>
  3710. *
  3711. * var testNode = document.getElementById( "test" );
  3712. * UE.dom.domUtils.removeClasses( testNode, ["test1", "test2"] );
  3713. *
  3714. * //output: test3
  3715. * console.log( testNode.className );
  3716. *
  3717. * </script>
  3718. * ```
  3719. */
  3720. removeClasses: function(elm, classNames) {
  3721. classNames = utils.isArray(classNames)
  3722. ? classNames
  3723. : utils.trim(classNames).replace(/[ ]{2,}/g, " ").split(" ");
  3724. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3725. cls = cls.replace(new RegExp("\\b" + ci + "\\b"), "");
  3726. }
  3727. cls = utils.trim(cls).replace(/[ ]{2,}/g, " ");
  3728. if (cls) {
  3729. elm.className = cls;
  3730. } else {
  3731. domUtils.removeAttributes(elm, ["class"]);
  3732. }
  3733. },
  3734. /**
  3735. * 给元素element添加className
  3736. * @method addClass
  3737. * @param { Node } ele 需要增加className的元素
  3738. * @param { String } classNames 需要添加的className, 多个className之间以空格分割
  3739. * @remind 相同的类名不会被重复添加
  3740. * @example
  3741. * ```html
  3742. * <span id="test" class="cls1 cls2"></span>
  3743. *
  3744. * <script>
  3745. * var testNode = document.getElementById("test");
  3746. *
  3747. * UE.dom.domUtils.addClass( testNode, "cls2 cls3 cls4" );
  3748. *
  3749. * //output: cl1 cls2 cls3 cls4
  3750. * console.log( testNode.className );
  3751. *
  3752. * <script>
  3753. * ```
  3754. */
  3755. /**
  3756. * 给元素element添加className
  3757. * @method addClass
  3758. * @param { Node } ele 需要增加className的元素
  3759. * @param { Array } classNames 需要添加的className的数组
  3760. * @remind 相同的类名不会被重复添加
  3761. * @example
  3762. * ```html
  3763. * <span id="test" class="cls1 cls2"></span>
  3764. *
  3765. * <script>
  3766. * var testNode = document.getElementById("test");
  3767. *
  3768. * UE.dom.domUtils.addClass( testNode, ["cls2", "cls3", "cls4"] );
  3769. *
  3770. * //output: cl1 cls2 cls3 cls4
  3771. * console.log( testNode.className );
  3772. *
  3773. * <script>
  3774. * ```
  3775. */
  3776. addClass: function(elm, classNames) {
  3777. if (!elm) return;
  3778. classNames = utils.trim(classNames).replace(/[ ]{2,}/g, " ").split(" ");
  3779. for (var i = 0, ci, cls = elm.className; (ci = classNames[i++]); ) {
  3780. if (!new RegExp("\\b" + ci + "\\b").test(cls)) {
  3781. cls += " " + ci;
  3782. }
  3783. }
  3784. elm.className = utils.trim(cls);
  3785. },
  3786. /**
  3787. * 判断元素element是否包含给定的样式类名className
  3788. * @method hasClass
  3789. * @param { Node } ele 需要检测的元素
  3790. * @param { String } classNames 需要检测的className, 多个className之间用空格分割
  3791. * @return { Boolean } 元素是否包含所有给定的className
  3792. * @example
  3793. * ```html
  3794. * <span id="test1" class="cls1 cls2"></span>
  3795. *
  3796. * <script>
  3797. * var test1 = document.getElementById("test1");
  3798. *
  3799. * //output: false
  3800. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1 cls3" ) );
  3801. *
  3802. * //output: true
  3803. * console.log( UE.dom.domUtils.hasClass( test1, "cls2 cls1" ) );
  3804. * </script>
  3805. * ```
  3806. */
  3807. /**
  3808. * 判断元素element是否包含给定的样式类名className
  3809. * @method hasClass
  3810. * @param { Node } ele 需要检测的元素
  3811. * @param { Array } classNames 需要检测的className数组
  3812. * @return { Boolean } 元素是否包含所有给定的className
  3813. * @example
  3814. * ```html
  3815. * <span id="test1" class="cls1 cls2"></span>
  3816. *
  3817. * <script>
  3818. * var test1 = document.getElementById("test1");
  3819. *
  3820. * //output: false
  3821. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1", "cls3" ] ) );
  3822. *
  3823. * //output: true
  3824. * console.log( UE.dom.domUtils.hasClass( test1, [ "cls2", "cls1" ]) );
  3825. * </script>
  3826. * ```
  3827. */
  3828. hasClass: function(element, className) {
  3829. if (utils.isRegExp(className)) {
  3830. return className.test(element.className);
  3831. }
  3832. className = utils.trim(className).replace(/[ ]{2,}/g, " ").split(" ");
  3833. for (var i = 0, ci, cls = element.className; (ci = className[i++]); ) {
  3834. if (!new RegExp("\\b" + ci + "\\b", "i").test(cls)) {
  3835. return false;
  3836. }
  3837. }
  3838. return i - 1 == className.length;
  3839. },
  3840. /**
  3841. * 阻止事件默认行为
  3842. * @method preventDefault
  3843. * @param { Event } evt 需要阻止默认行为的事件对象
  3844. * @example
  3845. * ```javascript
  3846. * UE.dom.domUtils.preventDefault( evt );
  3847. * ```
  3848. */
  3849. preventDefault: function(evt) {
  3850. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  3851. },
  3852. /**
  3853. * 删除元素element指定的样式
  3854. * @method removeStyle
  3855. * @param { Element } element 需要删除样式的元素
  3856. * @param { String } styleName 需要删除的样式名
  3857. * @example
  3858. * ```html
  3859. * <span id="test" style="color: red; background: blue;"></span>
  3860. *
  3861. * <script>
  3862. *
  3863. * var testNode = document.getElementById("test");
  3864. *
  3865. * UE.dom.domUtils.removeStyle( testNode, 'color' );
  3866. *
  3867. * //output: background: blue;
  3868. * console.log( testNode.style.cssText );
  3869. *
  3870. * </script>
  3871. * ```
  3872. */
  3873. removeStyle: function(element, name) {
  3874. if (browser.ie) {
  3875. //针对color先单独处理一下
  3876. if (name == "color") {
  3877. name = "(^|;)" + name;
  3878. }
  3879. element.style.cssText = element.style.cssText.replace(
  3880. new RegExp(name + "[^:]*:[^;]+;?", "ig"),
  3881. ""
  3882. );
  3883. } else {
  3884. if (element.style.removeProperty) {
  3885. element.style.removeProperty(name);
  3886. } else {
  3887. element.style.removeAttribute(utils.cssStyleToDomStyle(name));
  3888. }
  3889. }
  3890. if (!element.style.cssText) {
  3891. domUtils.removeAttributes(element, ["style"]);
  3892. }
  3893. },
  3894. /**
  3895. * 获取元素element的style属性的指定值
  3896. * @method getStyle
  3897. * @param { Element } element 需要获取属性值的元素
  3898. * @param { String } styleName 需要获取的style的名称
  3899. * @warning 该方法仅获取元素style属性中所标明的值
  3900. * @return { String } 该元素包含指定的style属性值
  3901. * @example
  3902. * ```html
  3903. * <div id="test" style="color: red;"></div>
  3904. *
  3905. * <script>
  3906. *
  3907. * var testNode = document.getElementById( "test" );
  3908. *
  3909. * //output: red
  3910. * console.log( UE.dom.domUtils.getStyle( testNode, "color" ) );
  3911. *
  3912. * //output: ""
  3913. * console.log( UE.dom.domUtils.getStyle( testNode, "background" ) );
  3914. *
  3915. * </script>
  3916. * ```
  3917. */
  3918. getStyle: function(element, name) {
  3919. var value = element.style[utils.cssStyleToDomStyle(name)];
  3920. return utils.fixColor(name, value);
  3921. },
  3922. /**
  3923. * 为元素element设置样式属性值
  3924. * @method setStyle
  3925. * @param { Element } element 需要设置样式的元素
  3926. * @param { String } styleName 样式名
  3927. * @param { String } styleValue 样式值
  3928. * @example
  3929. * ```html
  3930. * <div id="test"></div>
  3931. *
  3932. * <script>
  3933. *
  3934. * var testNode = document.getElementById( "test" );
  3935. *
  3936. * //output: ""
  3937. * console.log( testNode.style.color );
  3938. *
  3939. * UE.dom.domUtils.setStyle( testNode, 'color', 'red' );
  3940. * //output: "red"
  3941. * console.log( testNode.style.color );
  3942. *
  3943. * </script>
  3944. * ```
  3945. */
  3946. setStyle: function(element, name, value) {
  3947. element.style[utils.cssStyleToDomStyle(name)] = value;
  3948. if (!utils.trim(element.style.cssText)) {
  3949. this.removeAttributes(element, "style");
  3950. }
  3951. },
  3952. /**
  3953. * 为元素element设置多个样式属性值
  3954. * @method setStyles
  3955. * @param { Element } element 需要设置样式的元素
  3956. * @param { Object } styles 样式名值对
  3957. * @example
  3958. * ```html
  3959. * <div id="test"></div>
  3960. *
  3961. * <script>
  3962. *
  3963. * var testNode = document.getElementById( "test" );
  3964. *
  3965. * //output: ""
  3966. * console.log( testNode.style.color );
  3967. *
  3968. * UE.dom.domUtils.setStyles( testNode, {
  3969. * 'color': 'red'
  3970. * } );
  3971. * //output: "red"
  3972. * console.log( testNode.style.color );
  3973. *
  3974. * </script>
  3975. * ```
  3976. */
  3977. setStyles: function(element, styles) {
  3978. for (var name in styles) {
  3979. if (styles.hasOwnProperty(name)) {
  3980. domUtils.setStyle(element, name, styles[name]);
  3981. }
  3982. }
  3983. },
  3984. /**
  3985. * 删除_moz_dirty属性
  3986. * @private
  3987. * @method removeDirtyAttr
  3988. */
  3989. removeDirtyAttr: function(node) {
  3990. for (
  3991. var i = 0, ci, nodes = node.getElementsByTagName("*");
  3992. (ci = nodes[i++]);
  3993. ) {
  3994. ci.removeAttribute("_moz_dirty");
  3995. }
  3996. node.removeAttribute("_moz_dirty");
  3997. },
  3998. /**
  3999. * 获取子节点的数量
  4000. * @method getChildCount
  4001. * @param { Element } node 需要检测的元素
  4002. * @return { Number } 给定的node元素的子节点数量
  4003. * @example
  4004. * ```html
  4005. * <div id="test">
  4006. * <span></span>
  4007. * </div>
  4008. *
  4009. * <script>
  4010. *
  4011. * //output: 3
  4012. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test") ) );
  4013. *
  4014. * </script>
  4015. * ```
  4016. */
  4017. /**
  4018. * 根据给定的过滤规则, 获取符合条件的子节点的数量
  4019. * @method getChildCount
  4020. * @param { Element } node 需要检测的元素
  4021. * @param { Function } fn 过滤器, 要求对符合条件的子节点返回true, 反之则要求返回false
  4022. * @return { Number } 符合过滤条件的node元素的子节点数量
  4023. * @example
  4024. * ```html
  4025. * <div id="test">
  4026. * <span></span>
  4027. * </div>
  4028. *
  4029. * <script>
  4030. *
  4031. * //output: 1
  4032. * console.log( UE.dom.domUtils.getChildCount( document.getElementById("test"), function ( node ) {
  4033. *
  4034. * return node.nodeType === 1;
  4035. *
  4036. * } ) );
  4037. *
  4038. * </script>
  4039. * ```
  4040. */
  4041. getChildCount: function(node, fn) {
  4042. var count = 0,
  4043. first = node.firstChild;
  4044. fn =
  4045. fn ||
  4046. function() {
  4047. return 1;
  4048. };
  4049. while (first) {
  4050. if (fn(first)) {
  4051. count++;
  4052. }
  4053. first = first.nextSibling;
  4054. }
  4055. return count;
  4056. },
  4057. /**
  4058. * 判断给定节点是否为空节点
  4059. * @method isEmptyNode
  4060. * @param { Node } node 需要检测的节点对象
  4061. * @return { Boolean } 节点是否为空
  4062. * @example
  4063. * ```javascript
  4064. * UE.dom.domUtils.isEmptyNode( document.body );
  4065. * ```
  4066. */
  4067. isEmptyNode: function(node) {
  4068. return (
  4069. !node.firstChild ||
  4070. domUtils.getChildCount(node, function(node) {
  4071. return (
  4072. !domUtils.isBr(node) &&
  4073. !domUtils.isBookmarkNode(node) &&
  4074. !domUtils.isWhitespace(node)
  4075. );
  4076. }) == 0
  4077. );
  4078. },
  4079. clearSelectedArr: function(nodes) {
  4080. var node;
  4081. while ((node = nodes.pop())) {
  4082. domUtils.removeAttributes(node, ["class"]);
  4083. }
  4084. },
  4085. /**
  4086. * 将显示区域滚动到指定节点的位置
  4087. * @method scrollToView
  4088. * @param {Node} node 节点
  4089. * @param {window} win window对象
  4090. * @param {Number} offsetTop 距离上方的偏移量
  4091. */
  4092. scrollToView: function(node, win, offsetTop) {
  4093. var getViewPaneSize = function() {
  4094. var doc = win.document,
  4095. mode = doc.compatMode == "CSS1Compat";
  4096. return {
  4097. width:
  4098. (mode ? doc.documentElement.clientWidth : doc.body.clientWidth) || 0,
  4099. height:
  4100. (mode ? doc.documentElement.clientHeight : doc.body.clientHeight) || 0
  4101. };
  4102. },
  4103. getScrollPosition = function(win) {
  4104. if ("pageXOffset" in win) {
  4105. return {
  4106. x: win.pageXOffset || 0,
  4107. y: win.pageYOffset || 0
  4108. };
  4109. } else {
  4110. var doc = win.document;
  4111. return {
  4112. x: doc.documentElement.scrollLeft || doc.body.scrollLeft || 0,
  4113. y: doc.documentElement.scrollTop || doc.body.scrollTop || 0
  4114. };
  4115. }
  4116. };
  4117. var winHeight = getViewPaneSize().height,
  4118. offset = winHeight * -1 + offsetTop;
  4119. offset += node.offsetHeight || 0;
  4120. var elementPosition = domUtils.getXY(node);
  4121. offset += elementPosition.y;
  4122. var currentScroll = getScrollPosition(win).y;
  4123. // offset += 50;
  4124. if (offset > currentScroll || offset < currentScroll - winHeight) {
  4125. win.scrollTo(0, offset + (offset < 0 ? -20 : 20));
  4126. }
  4127. },
  4128. /**
  4129. * 判断给定节点是否为br
  4130. * @method isBr
  4131. * @param { Node } node 需要判断的节点对象
  4132. * @return { Boolean } 给定的节点是否是br节点
  4133. */
  4134. isBr: function(node) {
  4135. return node.nodeType == 1 && node.tagName == "BR";
  4136. },
  4137. /**
  4138. * 判断给定的节点是否是一个“填充”节点
  4139. * @private
  4140. * @method isFillChar
  4141. * @param { Node } node 需要判断的节点
  4142. * @param { Boolean } isInStart 是否从节点内容的开始位置匹配
  4143. * @returns { Boolean } 节点是否是填充节点
  4144. */
  4145. isFillChar: function(node, isInStart) {
  4146. if (node.nodeType != 3) return false;
  4147. var text = node.nodeValue;
  4148. if (isInStart) {
  4149. return new RegExp("^" + domUtils.fillChar).test(text);
  4150. }
  4151. return !text.replace(new RegExp(domUtils.fillChar, "g"), "").length;
  4152. },
  4153. isStartInblock: function(range) {
  4154. var tmpRange = range.cloneRange(),
  4155. flag = 0,
  4156. start = tmpRange.startContainer,
  4157. tmp;
  4158. if (start.nodeType == 1 && start.childNodes[tmpRange.startOffset]) {
  4159. start = start.childNodes[tmpRange.startOffset];
  4160. var pre = start.previousSibling;
  4161. while (pre && domUtils.isFillChar(pre)) {
  4162. start = pre;
  4163. pre = pre.previousSibling;
  4164. }
  4165. }
  4166. if (this.isFillChar(start, true) && tmpRange.startOffset == 1) {
  4167. tmpRange.setStartBefore(start);
  4168. start = tmpRange.startContainer;
  4169. }
  4170. while (start && domUtils.isFillChar(start)) {
  4171. tmp = start;
  4172. start = start.previousSibling;
  4173. }
  4174. if (tmp) {
  4175. tmpRange.setStartBefore(tmp);
  4176. start = tmpRange.startContainer;
  4177. }
  4178. if (
  4179. start.nodeType == 1 &&
  4180. domUtils.isEmptyNode(start) &&
  4181. tmpRange.startOffset == 1
  4182. ) {
  4183. tmpRange.setStart(start, 0).collapse(true);
  4184. }
  4185. while (!tmpRange.startOffset) {
  4186. start = tmpRange.startContainer;
  4187. if (domUtils.isBlockElm(start) || domUtils.isBody(start)) {
  4188. flag = 1;
  4189. break;
  4190. }
  4191. var pre = tmpRange.startContainer.previousSibling,
  4192. tmpNode;
  4193. if (!pre) {
  4194. tmpRange.setStartBefore(tmpRange.startContainer);
  4195. } else {
  4196. while (pre && domUtils.isFillChar(pre)) {
  4197. tmpNode = pre;
  4198. pre = pre.previousSibling;
  4199. }
  4200. if (tmpNode) {
  4201. tmpRange.setStartBefore(tmpNode);
  4202. } else {
  4203. tmpRange.setStartBefore(tmpRange.startContainer);
  4204. }
  4205. }
  4206. }
  4207. return flag && !domUtils.isBody(tmpRange.startContainer) ? 1 : 0;
  4208. },
  4209. /**
  4210. * 判断给定的元素是否是一个空元素
  4211. * @method isEmptyBlock
  4212. * @param { Element } node 需要判断的元素
  4213. * @return { Boolean } 是否是空元素
  4214. * @example
  4215. * ```html
  4216. * <div id="test"></div>
  4217. *
  4218. * <script>
  4219. * //output: true
  4220. * console.log( UE.dom.domUtils.isEmptyBlock( document.getElementById("test") ) );
  4221. * </script>
  4222. * ```
  4223. */
  4224. /**
  4225. * 根据指定的判断规则判断给定的元素是否是一个空元素
  4226. * @method isEmptyBlock
  4227. * @param { Element } node 需要判断的元素
  4228. * @param { RegExp } reg 对内容执行判断的正则表达式对象
  4229. * @return { Boolean } 是否是空元素
  4230. */
  4231. isEmptyBlock: function(node, reg) {
  4232. if (node.nodeType != 1) return 0;
  4233. reg = reg || new RegExp("[ \xa0\t\r\n" + domUtils.fillChar + "]", "g");
  4234. if (
  4235. node[browser.ie ? "innerText" : "textContent"].replace(reg, "").length > 0
  4236. ) {
  4237. return 0;
  4238. }
  4239. for (var n in dtd.$isNotEmpty) {
  4240. if (node.getElementsByTagName(n).length) {
  4241. return 0;
  4242. }
  4243. }
  4244. return 1;
  4245. },
  4246. /**
  4247. * 移动元素使得该元素的位置移动指定的偏移量的距离
  4248. * @method setViewportOffset
  4249. * @param { Element } element 需要设置偏移量的元素
  4250. * @param { Object } offset 偏移量, 形如{ left: 100, top: 50 }的一个键值对, 表示该元素将在
  4251. * 现有的位置上向水平方向偏移offset.left的距离, 在竖直方向上偏移
  4252. * offset.top的距离
  4253. * @example
  4254. * ```html
  4255. * <div id="test" style="top: 100px; left: 50px; position: absolute;"></div>
  4256. *
  4257. * <script>
  4258. *
  4259. * var testNode = document.getElementById("test");
  4260. *
  4261. * UE.dom.domUtils.setViewportOffset( testNode, {
  4262. * left: 200,
  4263. * top: 50
  4264. * } );
  4265. *
  4266. * //output: top: 300px; left: 100px; position: absolute;
  4267. * console.log( testNode.style.cssText );
  4268. *
  4269. * </script>
  4270. * ```
  4271. */
  4272. setViewportOffset: function(element, offset) {
  4273. var left = parseInt(element.style.left) | 0;
  4274. var top = parseInt(element.style.top) | 0;
  4275. var rect = element.getBoundingClientRect();
  4276. var offsetLeft = offset.left - rect.left;
  4277. var offsetTop = offset.top - rect.top;
  4278. if (offsetLeft) {
  4279. element.style.left = left + offsetLeft + "px";
  4280. }
  4281. if (offsetTop) {
  4282. element.style.top = top + offsetTop + "px";
  4283. }
  4284. },
  4285. /**
  4286. * 用“填充字符”填充节点
  4287. * @method fillNode
  4288. * @private
  4289. * @param { DomDocument } doc 填充的节点所在的docment对象
  4290. * @param { Node } node 需要填充的节点对象
  4291. * @example
  4292. * ```html
  4293. * <div id="test"></div>
  4294. *
  4295. * <script>
  4296. * var testNode = document.getElementById("test");
  4297. *
  4298. * //output: 0
  4299. * console.log( testNode.childNodes.length );
  4300. *
  4301. * UE.dom.domUtils.fillNode( document, testNode );
  4302. *
  4303. * //output: 1
  4304. * console.log( testNode.childNodes.length );
  4305. *
  4306. * </script>
  4307. * ```
  4308. */
  4309. fillNode: function(doc, node) {
  4310. var tmpNode = browser.ie
  4311. ? doc.createTextNode(domUtils.fillChar)
  4312. : doc.createElement("br");
  4313. node.innerHTML = "";
  4314. node.appendChild(tmpNode);
  4315. },
  4316. /**
  4317. * 把节点src的所有子节点追加到另一个节点tag上去
  4318. * @method moveChild
  4319. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4320. * @param { Node } tag 目标节点, 从源节点移除的子节点将被追加到该节点下
  4321. * @example
  4322. * ```html
  4323. * <div id="test1">
  4324. * <span></span>
  4325. * </div>
  4326. * <div id="test2">
  4327. * <div></div>
  4328. * </div>
  4329. *
  4330. * <script>
  4331. *
  4332. * var test1 = document.getElementById("test1"),
  4333. * test2 = document.getElementById("test2");
  4334. *
  4335. * UE.dom.domUtils.moveChild( test1, test2 );
  4336. *
  4337. * //output: ""(空字符串)
  4338. * console.log( test1.innerHTML );
  4339. *
  4340. * //output: "<div></div><span></span>"
  4341. * console.log( test2.innerHTML );
  4342. *
  4343. * </script>
  4344. * ```
  4345. */
  4346. /**
  4347. * 把节点src的所有子节点移动到另一个节点tag上去, 可以通过dir参数控制附加的行为是“追加”还是“插入顶部”
  4348. * @method moveChild
  4349. * @param { Node } src 源节点, 该节点下的所有子节点将被移除
  4350. * @param { Node } tag 目标节点, 从源节点移除的子节点将被附加到该节点下
  4351. * @param { Boolean } dir 附加方式, 如果为true, 则附加进去的节点将被放到目标节点的顶部, 反之,则放到末尾
  4352. * @example
  4353. * ```html
  4354. * <div id="test1">
  4355. * <span></span>
  4356. * </div>
  4357. * <div id="test2">
  4358. * <div></div>
  4359. * </div>
  4360. *
  4361. * <script>
  4362. *
  4363. * var test1 = document.getElementById("test1"),
  4364. * test2 = document.getElementById("test2");
  4365. *
  4366. * UE.dom.domUtils.moveChild( test1, test2, true );
  4367. *
  4368. * //output: ""(空字符串)
  4369. * console.log( test1.innerHTML );
  4370. *
  4371. * //output: "<span></span><div></div>"
  4372. * console.log( test2.innerHTML );
  4373. *
  4374. * </script>
  4375. * ```
  4376. */
  4377. moveChild: function(src, tag, dir) {
  4378. while (src.firstChild) {
  4379. if (dir && tag.firstChild) {
  4380. tag.insertBefore(src.lastChild, tag.firstChild);
  4381. } else {
  4382. tag.appendChild(src.firstChild);
  4383. }
  4384. }
  4385. },
  4386. /**
  4387. * 判断节点的标签上是否不存在任何属性
  4388. * @method hasNoAttributes
  4389. * @private
  4390. * @param { Node } node 需要检测的节点对象
  4391. * @return { Boolean } 节点是否不包含任何属性
  4392. * @example
  4393. * ```html
  4394. * <div id="test"><span>xxxx</span></div>
  4395. *
  4396. * <script>
  4397. *
  4398. * //output: false
  4399. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test") ) );
  4400. *
  4401. * //output: true
  4402. * console.log( UE.dom.domUtils.hasNoAttributes( document.getElementById("test").firstChild ) );
  4403. *
  4404. * </script>
  4405. * ```
  4406. */
  4407. hasNoAttributes: function(node) {
  4408. return browser.ie
  4409. ? /^<\w+\s*?>/.test(node.outerHTML)
  4410. : node.attributes.length == 0;
  4411. },
  4412. /**
  4413. * 检测节点是否是UEditor所使用的辅助节点
  4414. * @method isCustomeNode
  4415. * @private
  4416. * @param { Node } node 需要检测的节点
  4417. * @remind 辅助节点是指编辑器要完成工作临时添加的节点, 在输出的时候将会从编辑器内移除, 不会影响最终的结果。
  4418. * @return { Boolean } 给定的节点是否是一个辅助节点
  4419. */
  4420. isCustomeNode: function(node) {
  4421. return node.nodeType == 1 && node.getAttribute("_ue_custom_node_");
  4422. },
  4423. /**
  4424. * 检测节点的标签是否是给定的标签
  4425. * @method isTagNode
  4426. * @param { Node } node 需要检测的节点对象
  4427. * @param { String } tagName 标签
  4428. * @return { Boolean } 节点的标签是否是给定的标签
  4429. * @example
  4430. * ```html
  4431. * <div id="test"></div>
  4432. *
  4433. * <script>
  4434. *
  4435. * //output: true
  4436. * console.log( UE.dom.domUtils.isTagNode( document.getElementById("test"), "div" ) );
  4437. *
  4438. * </script>
  4439. * ```
  4440. */
  4441. isTagNode: function(node, tagNames) {
  4442. return (
  4443. node.nodeType == 1 &&
  4444. new RegExp("\\b" + node.tagName + "\\b", "i").test(tagNames)
  4445. );
  4446. },
  4447. /**
  4448. * 给定一个节点数组,在通过指定的过滤器过滤后, 获取其中满足过滤条件的第一个节点
  4449. * @method filterNodeList
  4450. * @param { Array } nodeList 需要过滤的节点数组
  4451. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4452. * @return { Node | NULL } 如果找到符合过滤条件的节点, 则返回该节点, 否则返回NULL
  4453. * @example
  4454. * ```javascript
  4455. * var divNodes = document.getElementsByTagName("div");
  4456. * divNodes = [].slice.call( divNodes, 0 );
  4457. *
  4458. * //output: null
  4459. * console.log( UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4460. * return node.tagName.toLowerCase() !== 'div';
  4461. * } ) );
  4462. * ```
  4463. */
  4464. /**
  4465. * 给定一个节点数组nodeList和一组标签名tagNames, 获取其中能够匹配标签名的节点集合中的第一个节点
  4466. * @method filterNodeList
  4467. * @param { Array } nodeList 需要过滤的节点数组
  4468. * @param { String } tagNames 需要匹配的标签名, 多个标签名之间用空格分割
  4469. * @return { Node | NULL } 如果找到标签名匹配的节点, 则返回该节点, 否则返回NULL
  4470. * @example
  4471. * ```javascript
  4472. * var divNodes = document.getElementsByTagName("div");
  4473. * divNodes = [].slice.call( divNodes, 0 );
  4474. *
  4475. * //output: null
  4476. * console.log( UE.dom.domUtils.filterNodeList( divNodes, 'a span' ) );
  4477. * ```
  4478. */
  4479. /**
  4480. * 给定一个节点数组,在通过指定的过滤器过滤后, 如果参数forAll为true, 则会返回所有满足过滤
  4481. * 条件的节点集合, 否则, 返回满足条件的节点集合中的第一个节点
  4482. * @method filterNodeList
  4483. * @param { Array } nodeList 需要过滤的节点数组
  4484. * @param { Function } fn 过滤器, 对符合条件的节点, 执行结果返回true, 反之则返回false
  4485. * @param { Boolean } forAll 是否返回整个节点数组, 如果该参数为false, 则返回节点集合中的第一个节点
  4486. * @return { Array | Node | NULL } 如果找到符合过滤条件的节点, 则根据参数forAll的值决定返回满足
  4487. * 过滤条件的节点数组或第一个节点, 否则返回NULL
  4488. * @example
  4489. * ```javascript
  4490. * var divNodes = document.getElementsByTagName("div");
  4491. * divNodes = [].slice.call( divNodes, 0 );
  4492. *
  4493. * //output: 3(假定有3个div)
  4494. * console.log( divNodes.length );
  4495. *
  4496. * var nodes = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4497. * return node.tagName.toLowerCase() === 'div';
  4498. * }, true );
  4499. *
  4500. * //output: 3
  4501. * console.log( nodes.length );
  4502. *
  4503. * var node = UE.dom.domUtils.filterNodeList( divNodes, function ( node ) {
  4504. * return node.tagName.toLowerCase() === 'div';
  4505. * }, false );
  4506. *
  4507. * //output: div
  4508. * console.log( node.nodeName );
  4509. * ```
  4510. */
  4511. filterNodeList: function(nodelist, filter, forAll) {
  4512. var results = [];
  4513. if (!utils.isFunction(filter)) {
  4514. var str = filter;
  4515. filter = function(n) {
  4516. return (
  4517. utils.indexOf(
  4518. utils.isArray(str) ? str : str.split(" "),
  4519. n.tagName.toLowerCase()
  4520. ) != -1
  4521. );
  4522. };
  4523. }
  4524. utils.each(nodelist, function(n) {
  4525. filter(n) && results.push(n);
  4526. });
  4527. return results.length == 0
  4528. ? null
  4529. : results.length == 1 || !forAll ? results[0] : results;
  4530. },
  4531. /**
  4532. * 查询给定的range选区是否在给定的node节点内,且在该节点的最末尾
  4533. * @method isInNodeEndBoundary
  4534. * @param { UE.dom.Range } rng 需要判断的range对象, 该对象的startContainer不能为NULL
  4535. * @param node 需要检测的节点对象
  4536. * @return { Number } 如果给定的选取range对象是在node内部的最末端, 则返回1, 否则返回0
  4537. */
  4538. isInNodeEndBoundary: function(rng, node) {
  4539. var start = rng.startContainer;
  4540. if (start.nodeType == 3 && rng.startOffset != start.nodeValue.length) {
  4541. return 0;
  4542. }
  4543. if (start.nodeType == 1 && rng.startOffset != start.childNodes.length) {
  4544. return 0;
  4545. }
  4546. while (start !== node) {
  4547. if (start.nextSibling) {
  4548. return 0;
  4549. }
  4550. start = start.parentNode;
  4551. }
  4552. return 1;
  4553. },
  4554. isBoundaryNode: function(node, dir) {
  4555. var tmp;
  4556. while (!domUtils.isBody(node)) {
  4557. tmp = node;
  4558. node = node.parentNode;
  4559. if (tmp !== node[dir]) {
  4560. return false;
  4561. }
  4562. }
  4563. return true;
  4564. },
  4565. fillHtml: browser.ie11below ? "&nbsp;" : "<br/>"
  4566. });
  4567. var fillCharReg = new RegExp(domUtils.fillChar, "g");
  4568. // core/Range.js
  4569. /**
  4570. * Range封装
  4571. * @file
  4572. * @module UE.dom
  4573. * @class Range
  4574. * @since 1.2.6.1
  4575. */
  4576. /**
  4577. * dom操作封装
  4578. * @unfile
  4579. * @module UE.dom
  4580. */
  4581. /**
  4582. * Range实现类,本类是UEditor底层核心类,封装不同浏览器之间的Range操作。
  4583. * @unfile
  4584. * @module UE.dom
  4585. * @class Range
  4586. */
  4587. (function() {
  4588. var guid = 0,
  4589. fillChar = domUtils.fillChar,
  4590. fillData;
  4591. /**
  4592. * 更新range的collapse状态
  4593. * @param {Range} range range对象
  4594. */
  4595. function updateCollapse(range) {
  4596. range.collapsed =
  4597. range.startContainer &&
  4598. range.endContainer &&
  4599. range.startContainer === range.endContainer &&
  4600. range.startOffset === range.endOffset;
  4601. }
  4602. function selectOneNode(rng) {
  4603. return (
  4604. !rng.collapsed &&
  4605. rng.startContainer.nodeType === 1 &&
  4606. rng.startContainer === rng.endContainer &&
  4607. rng.endOffset - rng.startOffset === 1
  4608. );
  4609. }
  4610. function setEndPoint(toStart, node, offset, range) {
  4611. //如果node是自闭合标签要处理
  4612. if (
  4613. node.nodeType === 1 &&
  4614. (dtd.$empty[node.tagName] || dtd.$nonChild[node.tagName])
  4615. ) {
  4616. offset = domUtils.getNodeIndex(node) + (toStart ? 0 : 1);
  4617. node = node.parentNode;
  4618. }
  4619. if (toStart) {
  4620. range.startContainer = node;
  4621. range.startOffset = offset;
  4622. if (!range.endContainer) {
  4623. range.collapse(true);
  4624. }
  4625. } else {
  4626. range.endContainer = node;
  4627. range.endOffset = offset;
  4628. if (!range.startContainer) {
  4629. range.collapse(false);
  4630. }
  4631. }
  4632. updateCollapse(range);
  4633. return range;
  4634. }
  4635. function execContentsAction(range, action) {
  4636. //调整边界
  4637. //range.includeBookmark();
  4638. var start = range.startContainer,
  4639. end = range.endContainer,
  4640. startOffset = range.startOffset,
  4641. endOffset = range.endOffset,
  4642. doc = range.document,
  4643. frag = doc.createDocumentFragment(),
  4644. tmpStart,
  4645. tmpEnd;
  4646. if (start.nodeType == 1) {
  4647. start =
  4648. start.childNodes[startOffset] ||
  4649. (tmpStart = start.appendChild(doc.createTextNode("")));
  4650. }
  4651. if (end.nodeType == 1) {
  4652. end =
  4653. end.childNodes[endOffset] ||
  4654. (tmpEnd = end.appendChild(doc.createTextNode("")));
  4655. }
  4656. if (start === end && start.nodeType == 3) {
  4657. frag.appendChild(
  4658. doc.createTextNode(
  4659. start.substringData(startOffset, endOffset - startOffset)
  4660. )
  4661. );
  4662. //is not clone
  4663. if (action) {
  4664. start.deleteData(startOffset, endOffset - startOffset);
  4665. range.collapse(true);
  4666. }
  4667. return frag;
  4668. }
  4669. var current,
  4670. currentLevel,
  4671. clone = frag,
  4672. startParents = domUtils.findParents(start, true),
  4673. endParents = domUtils.findParents(end, true);
  4674. for (var i = 0; startParents[i] == endParents[i]; ) {
  4675. i++;
  4676. }
  4677. for (var j = i, si; (si = startParents[j]); j++) {
  4678. current = si.nextSibling;
  4679. if (si == start) {
  4680. if (!tmpStart) {
  4681. if (range.startContainer.nodeType == 3) {
  4682. clone.appendChild(
  4683. doc.createTextNode(start.nodeValue.slice(startOffset))
  4684. );
  4685. //is not clone
  4686. if (action) {
  4687. start.deleteData(
  4688. startOffset,
  4689. start.nodeValue.length - startOffset
  4690. );
  4691. }
  4692. } else {
  4693. clone.appendChild(!action ? start.cloneNode(true) : start);
  4694. }
  4695. }
  4696. } else {
  4697. currentLevel = si.cloneNode(false);
  4698. clone.appendChild(currentLevel);
  4699. }
  4700. while (current) {
  4701. if (current === end || current === endParents[j]) {
  4702. break;
  4703. }
  4704. si = current.nextSibling;
  4705. clone.appendChild(!action ? current.cloneNode(true) : current);
  4706. current = si;
  4707. }
  4708. clone = currentLevel;
  4709. }
  4710. clone = frag;
  4711. if (!startParents[i]) {
  4712. clone.appendChild(startParents[i - 1].cloneNode(false));
  4713. clone = clone.firstChild;
  4714. }
  4715. for (var j = i, ei; (ei = endParents[j]); j++) {
  4716. current = ei.previousSibling;
  4717. if (ei == end) {
  4718. if (!tmpEnd && range.endContainer.nodeType == 3) {
  4719. clone.appendChild(
  4720. doc.createTextNode(end.substringData(0, endOffset))
  4721. );
  4722. //is not clone
  4723. if (action) {
  4724. end.deleteData(0, endOffset);
  4725. }
  4726. }
  4727. } else {
  4728. currentLevel = ei.cloneNode(false);
  4729. clone.appendChild(currentLevel);
  4730. }
  4731. //如果两端同级,右边第一次已经被开始做了
  4732. if (j != i || !startParents[i]) {
  4733. while (current) {
  4734. if (current === start) {
  4735. break;
  4736. }
  4737. ei = current.previousSibling;
  4738. clone.insertBefore(
  4739. !action ? current.cloneNode(true) : current,
  4740. clone.firstChild
  4741. );
  4742. current = ei;
  4743. }
  4744. }
  4745. clone = currentLevel;
  4746. }
  4747. if (action) {
  4748. range
  4749. .setStartBefore(
  4750. !endParents[i]
  4751. ? endParents[i - 1]
  4752. : !startParents[i] ? startParents[i - 1] : endParents[i]
  4753. )
  4754. .collapse(true);
  4755. }
  4756. tmpStart && domUtils.remove(tmpStart);
  4757. tmpEnd && domUtils.remove(tmpEnd);
  4758. return frag;
  4759. }
  4760. /**
  4761. * 创建一个跟document绑定的空的Range实例
  4762. * @constructor
  4763. * @param { Document } document 新建的选区所属的文档对象
  4764. */
  4765. /**
  4766. * @property { Node } startContainer 当前Range的开始边界的容器节点, 可以是一个元素节点或者是文本节点
  4767. */
  4768. /**
  4769. * @property { Node } startOffset 当前Range的开始边界容器节点的偏移量, 如果是元素节点,
  4770. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4771. */
  4772. /**
  4773. * @property { Node } endContainer 当前Range的结束边界的容器节点, 可以是一个元素节点或者是文本节点
  4774. */
  4775. /**
  4776. * @property { Node } endOffset 当前Range的结束边界容器节点的偏移量, 如果是元素节点,
  4777. * 该值就是childNodes中的第几个节点, 如果是文本节点就是文本内容的第几个字符
  4778. */
  4779. /**
  4780. * @property { Boolean } collapsed 当前Range是否闭合
  4781. * @default true
  4782. * @remind Range是闭合的时候, startContainer === endContainer && startOffset === endOffset
  4783. */
  4784. /**
  4785. * @property { Document } document 当前Range所属的Document对象
  4786. * @remind 不同range的的document属性可以是不同的
  4787. */
  4788. var Range = (dom.Range = function(document) {
  4789. var me = this;
  4790. me.startContainer = me.startOffset = me.endContainer = me.endOffset = null;
  4791. me.document = document;
  4792. me.collapsed = true;
  4793. });
  4794. /**
  4795. * 删除fillData
  4796. * @param doc
  4797. * @param excludeNode
  4798. */
  4799. function removeFillData(doc, excludeNode) {
  4800. try {
  4801. if (fillData && domUtils.inDoc(fillData, doc)) {
  4802. if (!fillData.nodeValue.replace(fillCharReg, "").length) {
  4803. var tmpNode = fillData.parentNode;
  4804. domUtils.remove(fillData);
  4805. while (
  4806. tmpNode &&
  4807. domUtils.isEmptyInlineElement(tmpNode) &&
  4808. //safari的contains有bug
  4809. (browser.safari
  4810. ? !(
  4811. domUtils.getPosition(tmpNode, excludeNode) &
  4812. domUtils.POSITION_CONTAINS
  4813. )
  4814. : !tmpNode.contains(excludeNode))
  4815. ) {
  4816. fillData = tmpNode.parentNode;
  4817. domUtils.remove(tmpNode);
  4818. tmpNode = fillData;
  4819. }
  4820. } else {
  4821. fillData.nodeValue = fillData.nodeValue.replace(fillCharReg, "");
  4822. }
  4823. }
  4824. } catch (e) {}
  4825. }
  4826. /**
  4827. * @param node
  4828. * @param dir
  4829. */
  4830. function mergeSibling(node, dir) {
  4831. var tmpNode;
  4832. node = node[dir];
  4833. while (node && domUtils.isFillChar(node)) {
  4834. tmpNode = node[dir];
  4835. domUtils.remove(node);
  4836. node = tmpNode;
  4837. }
  4838. }
  4839. Range.prototype = {
  4840. /**
  4841. * 克隆选区的内容到一个DocumentFragment里
  4842. * @method cloneContents
  4843. * @return { DocumentFragment | NULL } 如果选区是闭合的将返回null, 否则, 返回包含所clone内容的DocumentFragment元素
  4844. * @example
  4845. * ```html
  4846. * <body>
  4847. * <!-- 中括号表示选区 -->
  4848. * <b>x<i>x[x</i>xx]x</b>
  4849. *
  4850. * <script>
  4851. * //range是已选中的选区
  4852. * var fragment = range.cloneContents(),
  4853. * node = document.createElement("div");
  4854. *
  4855. * node.appendChild( fragment );
  4856. *
  4857. * //output: <i>x</i>xx
  4858. * console.log( node.innerHTML );
  4859. *
  4860. * </script>
  4861. * </body>
  4862. * ```
  4863. */
  4864. cloneContents: function() {
  4865. return this.collapsed ? null : execContentsAction(this, 0);
  4866. },
  4867. /**
  4868. * 删除当前选区范围中的所有内容
  4869. * @method deleteContents
  4870. * @remind 执行完该操作后, 当前Range对象变成了闭合状态
  4871. * @return { UE.dom.Range } 当前操作的Range对象
  4872. * @example
  4873. * ```html
  4874. * <body>
  4875. * <!-- 中括号表示选区 -->
  4876. * <b>x<i>x[x</i>xx]x</b>
  4877. *
  4878. * <script>
  4879. * //range是已选中的选区
  4880. * range.deleteContents();
  4881. *
  4882. * //竖线表示闭合后的选区位置
  4883. * //output: <b>x<i>x</i>|x</b>
  4884. * console.log( document.body.innerHTML );
  4885. *
  4886. * //此时, range的各项属性为
  4887. * //output: B
  4888. * console.log( range.startContainer.tagName );
  4889. * //output: 2
  4890. * console.log( range.startOffset );
  4891. * //output: B
  4892. * console.log( range.endContainer.tagName );
  4893. * //output: 2
  4894. * console.log( range.endOffset );
  4895. * //output: true
  4896. * console.log( range.collapsed );
  4897. *
  4898. * </script>
  4899. * </body>
  4900. * ```
  4901. */
  4902. deleteContents: function() {
  4903. var txt;
  4904. if (!this.collapsed) {
  4905. execContentsAction(this, 1);
  4906. }
  4907. if (browser.webkit) {
  4908. txt = this.startContainer;
  4909. if (txt.nodeType == 3 && !txt.nodeValue.length) {
  4910. this.setStartBefore(txt).collapse(true);
  4911. domUtils.remove(txt);
  4912. }
  4913. }
  4914. return this;
  4915. },
  4916. /**
  4917. * 将当前选区的内容提取到一个DocumentFragment里
  4918. * @method extractContents
  4919. * @remind 执行该操作后, 选区将变成闭合状态
  4920. * @warning 执行该操作后, 原来选区所选中的内容将从dom树上剥离出来
  4921. * @return { DocumentFragment } 返回包含所提取内容的DocumentFragment对象
  4922. * @example
  4923. * ```html
  4924. * <body>
  4925. * <!-- 中括号表示选区 -->
  4926. * <b>x<i>x[x</i>xx]x</b>
  4927. *
  4928. * <script>
  4929. * //range是已选中的选区
  4930. * var fragment = range.extractContents(),
  4931. * node = document.createElement( "div" );
  4932. *
  4933. * node.appendChild( fragment );
  4934. *
  4935. * //竖线表示闭合后的选区位置
  4936. *
  4937. * //output: <b>x<i>x</i>|x</b>
  4938. * console.log( document.body.innerHTML );
  4939. * //output: <i>x</i>xx
  4940. * console.log( node.innerHTML );
  4941. *
  4942. * //此时, range的各项属性为
  4943. * //output: B
  4944. * console.log( range.startContainer.tagName );
  4945. * //output: 2
  4946. * console.log( range.startOffset );
  4947. * //output: B
  4948. * console.log( range.endContainer.tagName );
  4949. * //output: 2
  4950. * console.log( range.endOffset );
  4951. * //output: true
  4952. * console.log( range.collapsed );
  4953. *
  4954. * </script>
  4955. * </body>
  4956. */
  4957. extractContents: function() {
  4958. return this.collapsed ? null : execContentsAction(this, 2);
  4959. },
  4960. /**
  4961. * 设置Range的开始容器节点和偏移量
  4962. * @method setStart
  4963. * @remind 如果给定的节点是元素节点,那么offset指的是其子元素中索引为offset的元素,
  4964. * 如果是文本节点,那么offset指的是其文本内容的第offset个字符
  4965. * @remind 如果提供的容器节点是一个不能包含子元素的节点, 则该选区的开始容器将被设置
  4966. * 为该节点的父节点, 此时, 其距离开始容器的偏移量也变成了该节点在其父节点
  4967. * 中的索引
  4968. * @param { Node } node 将被设为当前选区开始边界容器的节点对象
  4969. * @param { int } offset 选区的开始位置偏移量
  4970. * @return { UE.dom.Range } 当前range对象
  4971. * @example
  4972. * ```html
  4973. * <!-- 选区 -->
  4974. * <b>xxx<i>x<span>xx</span>xx<em>xx</em>xxx</i>[xxx]</b>
  4975. *
  4976. * <script>
  4977. *
  4978. * //执行操作
  4979. * range.setStart( document.getElementsByTagName("i")[0], 1 );
  4980. *
  4981. * //此时, 选区变成了
  4982. * //<b>xxx<i>x[<span>xx</span>xx<em>xx</em>xxx</i>xxx]</b>
  4983. *
  4984. * </script>
  4985. * ```
  4986. * @example
  4987. * ```html
  4988. * <!-- 选区 -->
  4989. * <b>xxx<img>[xx]x</b>
  4990. *
  4991. * <script>
  4992. *
  4993. * //执行操作
  4994. * range.setStart( document.getElementsByTagName("img")[0], 3 );
  4995. *
  4996. * //此时, 选区变成了
  4997. * //<b>xxx[<img>xx]x</b>
  4998. *
  4999. * </script>
  5000. * ```
  5001. */
  5002. setStart: function(node, offset) {
  5003. return setEndPoint(true, node, offset, this);
  5004. },
  5005. /**
  5006. * 设置Range的结束容器和偏移量
  5007. * @method setEnd
  5008. * @param { Node } node 作为当前选区结束边界容器的节点对象
  5009. * @param { int } offset 结束边界的偏移量
  5010. * @see UE.dom.Range:setStart(Node,int)
  5011. * @return { UE.dom.Range } 当前range对象
  5012. */
  5013. setEnd: function(node, offset) {
  5014. return setEndPoint(false, node, offset, this);
  5015. },
  5016. /**
  5017. * 将Range开始位置设置到node节点之后
  5018. * @method setStartAfter
  5019. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引+1
  5020. * @param { Node } node 选区的开始边界将紧接着该节点之后
  5021. * @return { UE.dom.Range } 当前range对象
  5022. * @example
  5023. * ```html
  5024. * <!-- 选区示例 -->
  5025. * <b>xx<i>xxx</i><span>xx[x</span>xxx]</b>
  5026. *
  5027. * <script>
  5028. *
  5029. * //执行操作
  5030. * range.setStartAfter( document.getElementsByTagName("i")[0] );
  5031. *
  5032. * //结果选区
  5033. * //<b>xx<i>xxx</i>[<span>xxx</span>xxx]</b>
  5034. *
  5035. * </script>
  5036. * ```
  5037. */
  5038. setStartAfter: function(node) {
  5039. return this.setStart(node.parentNode, domUtils.getNodeIndex(node) + 1);
  5040. },
  5041. /**
  5042. * 将Range开始位置设置到node节点之前
  5043. * @method setStartBefore
  5044. * @remind 该操作将会把给定节点的父节点作为range的开始容器, 且偏移量是该节点在其父节点中的位置索引
  5045. * @param { Node } node 新的选区开始位置在该节点之前
  5046. * @see UE.dom.Range:setStartAfter(Node)
  5047. * @return { UE.dom.Range } 当前range对象
  5048. */
  5049. setStartBefore: function(node) {
  5050. return this.setStart(node.parentNode, domUtils.getNodeIndex(node));
  5051. },
  5052. /**
  5053. * 将Range结束位置设置到node节点之后
  5054. * @method setEndAfter
  5055. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引+1
  5056. * @param { Node } node 目标节点
  5057. * @see UE.dom.Range:setStartAfter(Node)
  5058. * @return { UE.dom.Range } 当前range对象
  5059. * @example
  5060. * ```html
  5061. * <!-- 选区示例 -->
  5062. * <b>[xx<i>xxx</i><span>xx]x</span>xxx</b>
  5063. *
  5064. * <script>
  5065. *
  5066. * //执行操作
  5067. * range.setStartAfter( document.getElementsByTagName("span")[0] );
  5068. *
  5069. * //结果选区
  5070. * //<b>[xx<i>xxx</i><span>xxx</span>]xxx</b>
  5071. *
  5072. * </script>
  5073. * ```
  5074. */
  5075. setEndAfter: function(node) {
  5076. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node) + 1);
  5077. },
  5078. /**
  5079. * 将Range结束位置设置到node节点之前
  5080. * @method setEndBefore
  5081. * @remind 该操作将会把给定节点的父节点作为range的结束容器, 且偏移量是该节点在其父节点中的位置索引
  5082. * @param { Node } node 目标节点
  5083. * @see UE.dom.Range:setEndAfter(Node)
  5084. * @return { UE.dom.Range } 当前range对象
  5085. */
  5086. setEndBefore: function(node) {
  5087. return this.setEnd(node.parentNode, domUtils.getNodeIndex(node));
  5088. },
  5089. /**
  5090. * 设置Range的开始位置到node节点内的第一个子节点之前
  5091. * @method setStartAtFirst
  5092. * @remind 选区的开始容器将变成给定的节点, 且偏移量为0
  5093. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5094. * @param { Node } node 目标节点
  5095. * @see UE.dom.Range:setStartBefore(Node)
  5096. * @return { UE.dom.Range } 当前range对象
  5097. * @example
  5098. * ```html
  5099. * <!-- 选区示例 -->
  5100. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5101. *
  5102. * <script>
  5103. *
  5104. * //执行操作
  5105. * range.setStartAtFirst( document.getElementsByTagName("i")[0] );
  5106. *
  5107. * //结果选区
  5108. * //<b>xx<i>[xxx</i><span>xx]x</span>xxx</b>
  5109. *
  5110. * </script>
  5111. * ```
  5112. */
  5113. setStartAtFirst: function(node) {
  5114. return this.setStart(node, 0);
  5115. },
  5116. /**
  5117. * 设置Range的开始位置到node节点内的最后一个节点之后
  5118. * @method setStartAtLast
  5119. * @remind 选区的开始容器将变成给定的节点, 且偏移量为该节点的子节点数
  5120. * @remind 如果给定的节点是元素节点, 则该节点必须是允许包含子节点的元素。
  5121. * @param { Node } node 目标节点
  5122. * @see UE.dom.Range:setStartAtFirst(Node)
  5123. * @return { UE.dom.Range } 当前range对象
  5124. */
  5125. setStartAtLast: function(node) {
  5126. return this.setStart(
  5127. node,
  5128. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5129. );
  5130. },
  5131. /**
  5132. * 设置Range的结束位置到node节点内的第一个节点之前
  5133. * @method setEndAtFirst
  5134. * @param { Node } node 目标节点
  5135. * @remind 选区的结束容器将变成给定的节点, 且偏移量为0
  5136. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5137. * @see UE.dom.Range:setStartAtFirst(Node)
  5138. * @return { UE.dom.Range } 当前range对象
  5139. */
  5140. setEndAtFirst: function(node) {
  5141. return this.setEnd(node, 0);
  5142. },
  5143. /**
  5144. * 设置Range的结束位置到node节点内的最后一个节点之后
  5145. * @method setEndAtLast
  5146. * @param { Node } node 目标节点
  5147. * @remind 选区的结束容器将变成给定的节点, 且偏移量为该节点的子节点数量
  5148. * @remind node必须是一个元素节点, 且必须是允许包含子节点的元素。
  5149. * @see UE.dom.Range:setStartAtFirst(Node)
  5150. * @return { UE.dom.Range } 当前range对象
  5151. */
  5152. setEndAtLast: function(node) {
  5153. return this.setEnd(
  5154. node,
  5155. node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length
  5156. );
  5157. },
  5158. /**
  5159. * 选中给定节点
  5160. * @method selectNode
  5161. * @remind 此时, 选区的开始容器和结束容器都是该节点的父节点, 其startOffset是该节点在父节点中的位置索引,
  5162. * 而endOffset为startOffset+1
  5163. * @param { Node } node 需要选中的节点
  5164. * @return { UE.dom.Range } 当前range对象,此时的range仅包含当前给定的节点对象
  5165. * @example
  5166. * ```html
  5167. * <!-- 选区示例 -->
  5168. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5169. *
  5170. * <script>
  5171. *
  5172. * //执行操作
  5173. * range.selectNode( document.getElementsByTagName("i")[0] );
  5174. *
  5175. * //结果选区
  5176. * //<b>xx[<i>xxx</i>]<span>xxx</span>xxx</b>
  5177. *
  5178. * </script>
  5179. * ```
  5180. */
  5181. selectNode: function(node) {
  5182. return this.setStartBefore(node).setEndAfter(node);
  5183. },
  5184. /**
  5185. * 选中给定节点内部的所有节点
  5186. * @method selectNodeContents
  5187. * @remind 此时, 选区的开始容器和结束容器都是该节点, 其startOffset为0,
  5188. * 而endOffset是该节点的子节点数。
  5189. * @param { Node } node 目标节点, 当前range将包含该节点内的所有节点
  5190. * @return { UE.dom.Range } 当前range对象, 此时range仅包含给定节点的所有子节点
  5191. * @example
  5192. * ```html
  5193. * <!-- 选区示例 -->
  5194. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5195. *
  5196. * <script>
  5197. *
  5198. * //执行操作
  5199. * range.selectNode( document.getElementsByTagName("b")[0] );
  5200. *
  5201. * //结果选区
  5202. * //<b>[xx<i>xxx</i><span>xxx</span>xxx]</b>
  5203. *
  5204. * </script>
  5205. * ```
  5206. */
  5207. selectNodeContents: function(node) {
  5208. return this.setStart(node, 0).setEndAtLast(node);
  5209. },
  5210. /**
  5211. * clone当前Range对象
  5212. * @method cloneRange
  5213. * @remind 返回的range是一个全新的range对象, 其内部所有属性与当前被clone的range相同。
  5214. * @return { UE.dom.Range } 当前range对象的一个副本
  5215. */
  5216. cloneRange: function() {
  5217. var me = this;
  5218. return new Range(me.document)
  5219. .setStart(me.startContainer, me.startOffset)
  5220. .setEnd(me.endContainer, me.endOffset);
  5221. },
  5222. /**
  5223. * 向当前选区的结束处闭合选区
  5224. * @method collapse
  5225. * @return { UE.dom.Range } 当前range对象
  5226. * @example
  5227. * ```html
  5228. * <!-- 选区示例 -->
  5229. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5230. *
  5231. * <script>
  5232. *
  5233. * //执行操作
  5234. * range.collapse();
  5235. *
  5236. * //结果选区
  5237. * //“|”表示选区已闭合
  5238. * //<b>xx<i>xxx</i><span>xx|x</span>xxx</b>
  5239. *
  5240. * </script>
  5241. * ```
  5242. */
  5243. /**
  5244. * 闭合当前选区,根据给定的toStart参数项决定是向当前选区开始处闭合还是向结束处闭合,
  5245. * 如果toStart的值为true,则向开始位置闭合, 反之,向结束位置闭合。
  5246. * @method collapse
  5247. * @param { Boolean } toStart 是否向选区开始处闭合
  5248. * @return { UE.dom.Range } 当前range对象,此时range对象处于闭合状态
  5249. * @see UE.dom.Range:collapse()
  5250. * @example
  5251. * ```html
  5252. * <!-- 选区示例 -->
  5253. * <b>xx<i>xxx</i><span>[xx]x</span>xxx</b>
  5254. *
  5255. * <script>
  5256. *
  5257. * //执行操作
  5258. * range.collapse( true );
  5259. *
  5260. * //结果选区
  5261. * //“|”表示选区已闭合
  5262. * //<b>xx<i>xxx</i><span>|xxx</span>xxx</b>
  5263. *
  5264. * </script>
  5265. * ```
  5266. */
  5267. collapse: function(toStart) {
  5268. var me = this;
  5269. if (toStart) {
  5270. me.endContainer = me.startContainer;
  5271. me.endOffset = me.startOffset;
  5272. } else {
  5273. me.startContainer = me.endContainer;
  5274. me.startOffset = me.endOffset;
  5275. }
  5276. me.collapsed = true;
  5277. return me;
  5278. },
  5279. /**
  5280. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置
  5281. * @method shrinkBoundary
  5282. * @return { UE.dom.Range } 当前range对象
  5283. * @example
  5284. * ```html
  5285. * <span>xx<b>xx[</b>xxxxx]</span> => <span>xx<b>xx</b>[xxxxx]</span>
  5286. * ```
  5287. *
  5288. * @example
  5289. * ```html
  5290. * <!-- 选区示例 -->
  5291. * <b>x[xx</b><i>]xxx</i>
  5292. *
  5293. * <script>
  5294. *
  5295. * //执行收缩
  5296. * range.shrinkBoundary();
  5297. *
  5298. * //结果选区
  5299. * //<b>x[xx]</b><i>xxx</i>
  5300. * </script>
  5301. * ```
  5302. *
  5303. * @example
  5304. * ```html
  5305. * [<b><i>xxxx</i>xxxxxxx</b>] => <b><i>[xxxx</i>xxxxxxx]</b>
  5306. * ```
  5307. */
  5308. /**
  5309. * 调整range的开始位置和结束位置,使其"收缩"到最小的位置,
  5310. * 如果ignoreEnd的值为true,则忽略对结束位置的调整
  5311. * @method shrinkBoundary
  5312. * @param { Boolean } ignoreEnd 是否忽略对结束位置的调整
  5313. * @return { UE.dom.Range } 当前range对象
  5314. * @see UE.dom.domUtils.Range:shrinkBoundary()
  5315. */
  5316. shrinkBoundary: function(ignoreEnd) {
  5317. var me = this,
  5318. child,
  5319. collapsed = me.collapsed;
  5320. function check(node) {
  5321. return (
  5322. node.nodeType == 1 &&
  5323. !domUtils.isBookmarkNode(node) &&
  5324. !dtd.$empty[node.tagName] &&
  5325. !dtd.$nonChild[node.tagName]
  5326. );
  5327. }
  5328. while (
  5329. me.startContainer.nodeType == 1 && //是element
  5330. (child = me.startContainer.childNodes[me.startOffset]) && //子节点也是element
  5331. check(child)
  5332. ) {
  5333. me.setStart(child, 0);
  5334. }
  5335. if (collapsed) {
  5336. return me.collapse(true);
  5337. }
  5338. if (!ignoreEnd) {
  5339. while (
  5340. me.endContainer.nodeType == 1 && //是element
  5341. me.endOffset > 0 && //如果是空元素就退出 endOffset=0那么endOffst-1为负值,childNodes[endOffset]报错
  5342. (child = me.endContainer.childNodes[me.endOffset - 1]) && //子节点也是element
  5343. check(child)
  5344. ) {
  5345. me.setEnd(child, child.childNodes.length);
  5346. }
  5347. }
  5348. return me;
  5349. },
  5350. /**
  5351. * 获取离当前选区内包含的所有节点最近的公共祖先节点,
  5352. * @method getCommonAncestor
  5353. * @remind 返回的公共祖先节点一定不是range自身的容器节点, 但有可能是一个文本节点
  5354. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5355. * @example
  5356. * ```html
  5357. * //选区示例
  5358. * <span>xxx<b>x[x<em>xx]x</em>xxx</b>xx</span>
  5359. * <script>
  5360. *
  5361. * var node = range.getCommonAncestor();
  5362. *
  5363. * //公共祖先节点是: b节点
  5364. * //输出: B
  5365. * console.log(node.tagName);
  5366. *
  5367. * </script>
  5368. * ```
  5369. */
  5370. /**
  5371. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5372. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5373. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点
  5374. * @method getCommonAncestor
  5375. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5376. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5377. * @see UE.dom.Range:getCommonAncestor()
  5378. * @example
  5379. * ```html
  5380. * <body>
  5381. *
  5382. * <!-- 选区示例 -->
  5383. * <b>xxx<i>xxxx<span>xx[x</span>xx]x</i>xxxxxxx</b>
  5384. *
  5385. * <script>
  5386. *
  5387. * var node = range.getCommonAncestor( false );
  5388. *
  5389. * //这里的公共祖先节点是B而不是I, 是因为参数限制了获取到的节点不能是容器节点
  5390. * //output: B
  5391. * console.log( node.tagName );
  5392. *
  5393. * </script>
  5394. *
  5395. * </body>
  5396. * ```
  5397. */
  5398. /**
  5399. * 获取当前选区所包含的所有节点的公共祖先节点, 可以根据给定的参数 includeSelf 决定获取到
  5400. * 的公共祖先节点是否可以是当前选区的startContainer或endContainer节点, 如果 includeSelf
  5401. * 的取值为true, 则返回的节点可以是自身的容器节点, 否则, 则不能是容器节点; 同时可以根据
  5402. * ignoreTextNode 参数的取值决定是否忽略类型为文本节点的祖先节点。
  5403. * @method getCommonAncestor
  5404. * @param { Boolean } includeSelf 是否允许获取到的公共祖先节点是当前range对象的容器节点
  5405. * @param { Boolean } ignoreTextNode 获取祖先节点的过程中是否忽略类型为文本节点的祖先节点
  5406. * @return { Node } 当前range对象内所有节点的公共祖先节点
  5407. * @see UE.dom.Range:getCommonAncestor()
  5408. * @see UE.dom.Range:getCommonAncestor(Boolean)
  5409. * @example
  5410. * ```html
  5411. * <body>
  5412. *
  5413. * <!-- 选区示例 -->
  5414. * <b>xxx<i>xxxx<span>x[x]x</span>xxx</i>xxxxxxx</b>
  5415. *
  5416. * <script>
  5417. *
  5418. * var node = range.getCommonAncestor( true, false );
  5419. *
  5420. * //output: SPAN
  5421. * console.log( node.tagName );
  5422. *
  5423. * </script>
  5424. *
  5425. * </body>
  5426. * ```
  5427. */
  5428. getCommonAncestor: function(includeSelf, ignoreTextNode) {
  5429. var me = this,
  5430. start = me.startContainer,
  5431. end = me.endContainer;
  5432. if (start === end) {
  5433. if (includeSelf && selectOneNode(this)) {
  5434. start = start.childNodes[me.startOffset];
  5435. if (start.nodeType == 1) return start;
  5436. }
  5437. //只有在上来就相等的情况下才会出现是文本的情况
  5438. return ignoreTextNode && start.nodeType == 3 ? start.parentNode : start;
  5439. }
  5440. return domUtils.getCommonAncestor(start, end);
  5441. },
  5442. /**
  5443. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上
  5444. * @method trimBoundary
  5445. * @remind 该操作有可能会引起文本节点被切开
  5446. * @return { UE.dom.Range } 当前range对象
  5447. * @example
  5448. * ```html
  5449. *
  5450. * //选区示例
  5451. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5452. *
  5453. * <script>
  5454. * //未调整前, 选区的开始容器和结束都是文本节点
  5455. * //执行调整
  5456. * range.trimBoundary();
  5457. *
  5458. * //调整之后, 容器节点变成了i节点
  5459. * //<b>xxx[<i>xxxxx</i>]xxx</b>
  5460. * </script>
  5461. * ```
  5462. */
  5463. /**
  5464. * 调整当前Range的开始和结束边界容器,如果是容器节点是文本节点,就调整到包含该文本节点的父节点上,
  5465. * 可以根据 ignoreEnd 参数的值决定是否调整对结束边界的调整
  5466. * @method trimBoundary
  5467. * @param { Boolean } ignoreEnd 是否忽略对结束边界的调整
  5468. * @return { UE.dom.Range } 当前range对象
  5469. * @example
  5470. * ```html
  5471. *
  5472. * //选区示例
  5473. * <b>xxx<i>[xxxxx]</i>xxx</b>
  5474. *
  5475. * <script>
  5476. * //未调整前, 选区的开始容器和结束都是文本节点
  5477. * //执行调整
  5478. * range.trimBoundary( true );
  5479. *
  5480. * //调整之后, 开始容器节点变成了i节点
  5481. * //但是, 结束容器没有发生变化
  5482. * //<b>xxx[<i>xxxxx]</i>xxx</b>
  5483. * </script>
  5484. * ```
  5485. */
  5486. trimBoundary: function(ignoreEnd) {
  5487. this.txtToElmBoundary();
  5488. var start = this.startContainer,
  5489. offset = this.startOffset,
  5490. collapsed = this.collapsed,
  5491. end = this.endContainer;
  5492. if (start.nodeType == 3) {
  5493. if (offset == 0) {
  5494. this.setStartBefore(start);
  5495. } else {
  5496. if (offset >= start.nodeValue.length) {
  5497. this.setStartAfter(start);
  5498. } else {
  5499. var textNode = domUtils.split(start, offset);
  5500. //跟新结束边界
  5501. if (start === end) {
  5502. this.setEnd(textNode, this.endOffset - offset);
  5503. } else if (start.parentNode === end) {
  5504. this.endOffset += 1;
  5505. }
  5506. this.setStartBefore(textNode);
  5507. }
  5508. }
  5509. if (collapsed) {
  5510. return this.collapse(true);
  5511. }
  5512. }
  5513. if (!ignoreEnd) {
  5514. offset = this.endOffset;
  5515. end = this.endContainer;
  5516. if (end.nodeType == 3) {
  5517. if (offset == 0) {
  5518. this.setEndBefore(end);
  5519. } else {
  5520. offset < end.nodeValue.length && domUtils.split(end, offset);
  5521. this.setEndAfter(end);
  5522. }
  5523. }
  5524. }
  5525. return this;
  5526. },
  5527. /**
  5528. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则什么也不做
  5529. * @method txtToElmBoundary
  5530. * @remind 该操作不会修改dom节点
  5531. * @return { UE.dom.Range } 当前range对象
  5532. */
  5533. /**
  5534. * 如果选区在文本的边界上,就扩展选区到文本的父节点上, 如果当前选区是闭合的, 则根据参数项
  5535. * ignoreCollapsed 的值决定是否执行该调整
  5536. * @method txtToElmBoundary
  5537. * @param { Boolean } ignoreCollapsed 是否忽略选区的闭合状态, 如果该参数取值为true, 则
  5538. * 不论选区是否闭合, 都会执行该操作, 反之, 则不会对闭合的选区执行该操作
  5539. * @return { UE.dom.Range } 当前range对象
  5540. */
  5541. txtToElmBoundary: function(ignoreCollapsed) {
  5542. function adjust(r, c) {
  5543. var container = r[c + "Container"],
  5544. offset = r[c + "Offset"];
  5545. if (container.nodeType == 3) {
  5546. if (!offset) {
  5547. r[
  5548. "set" +
  5549. c.replace(/(\w)/, function(a) {
  5550. return a.toUpperCase();
  5551. }) +
  5552. "Before"
  5553. ](container);
  5554. } else if (offset >= container.nodeValue.length) {
  5555. r[
  5556. "set" +
  5557. c.replace(/(\w)/, function(a) {
  5558. return a.toUpperCase();
  5559. }) +
  5560. "After"
  5561. ](container);
  5562. }
  5563. }
  5564. }
  5565. if (ignoreCollapsed || !this.collapsed) {
  5566. adjust(this, "start");
  5567. adjust(this, "end");
  5568. }
  5569. return this;
  5570. },
  5571. /**
  5572. * 在当前选区的开始位置前插入节点,新插入的节点会被该range包含
  5573. * @method insertNode
  5574. * @param { Node } node 需要插入的节点
  5575. * @remind 插入的节点可以是一个DocumentFragment依次插入多个节点
  5576. * @return { UE.dom.Range } 当前range对象
  5577. */
  5578. insertNode: function(node) {
  5579. var first = node,
  5580. length = 1;
  5581. if (node.nodeType == 11) {
  5582. first = node.firstChild;
  5583. length = node.childNodes.length;
  5584. }
  5585. this.trimBoundary(true);
  5586. var start = this.startContainer,
  5587. offset = this.startOffset;
  5588. var nextNode = start.childNodes[offset];
  5589. if (nextNode) {
  5590. start.insertBefore(node, nextNode);
  5591. } else {
  5592. start.appendChild(node);
  5593. }
  5594. if (first.parentNode === this.endContainer) {
  5595. this.endOffset = this.endOffset + length;
  5596. }
  5597. return this.setStartBefore(first);
  5598. },
  5599. /**
  5600. * 闭合选区到当前选区的开始位置, 并且定位光标到闭合后的位置
  5601. * @method setCursor
  5602. * @return { UE.dom.Range } 当前range对象
  5603. * @see UE.dom.Range:collapse()
  5604. */
  5605. /**
  5606. * 闭合选区,可以根据参数toEnd的值控制选区是向前闭合还是向后闭合, 并且定位光标到闭合后的位置。
  5607. * @method setCursor
  5608. * @param { Boolean } toEnd 是否向后闭合, 如果为true, 则闭合选区时, 将向结束容器方向闭合,
  5609. * 反之,则向开始容器方向闭合
  5610. * @return { UE.dom.Range } 当前range对象
  5611. * @see UE.dom.Range:collapse(Boolean)
  5612. */
  5613. setCursor: function(toEnd, noFillData) {
  5614. return this.collapse(!toEnd).select(noFillData);
  5615. },
  5616. /**
  5617. * 创建当前range的一个书签,记录下当前range的位置,方便当dom树改变时,还能找回原来的选区位置
  5618. * @method createBookmark
  5619. * @param { Boolean } serialize 控制返回的标记位置是对当前位置的引用还是ID,如果该值为true,则
  5620. * 返回标记位置的ID, 反之则返回标记位置节点的引用
  5621. * @return { Object } 返回一个书签记录键值对, 其包含的key有: start => 开始标记的ID或者引用,
  5622. * end => 结束标记的ID或引用, id => 当前标记的类型, 如果为true,则表示
  5623. * 返回的记录的类型为ID, 反之则为引用
  5624. */
  5625. createBookmark: function(serialize, same) {
  5626. var endNode,
  5627. startNode = this.document.createElement("span");
  5628. startNode.style.cssText = "display:none;line-height:0px;";
  5629. startNode.appendChild(this.document.createTextNode("\u200D"));
  5630. startNode.id = "_baidu_bookmark_start_" + (same ? "" : guid++);
  5631. if (!this.collapsed) {
  5632. endNode = startNode.cloneNode(true);
  5633. endNode.id = "_baidu_bookmark_end_" + (same ? "" : guid++);
  5634. }
  5635. this.insertNode(startNode);
  5636. if (endNode) {
  5637. this.collapse().insertNode(endNode).setEndBefore(endNode);
  5638. }
  5639. this.setStartAfter(startNode);
  5640. return {
  5641. start: serialize ? startNode.id : startNode,
  5642. end: endNode ? (serialize ? endNode.id : endNode) : null,
  5643. id: serialize
  5644. };
  5645. },
  5646. /**
  5647. * 调整当前range的边界到书签位置,并删除该书签对象所标记的位置内的节点
  5648. * @method moveToBookmark
  5649. * @param { BookMark } bookmark createBookmark所创建的标签对象
  5650. * @return { UE.dom.Range } 当前range对象
  5651. * @see UE.dom.Range:createBookmark(Boolean)
  5652. */
  5653. moveToBookmark: function(bookmark) {
  5654. var start = bookmark.id
  5655. ? this.document.getElementById(bookmark.start)
  5656. : bookmark.start,
  5657. end = bookmark.end && bookmark.id
  5658. ? this.document.getElementById(bookmark.end)
  5659. : bookmark.end;
  5660. this.setStartBefore(start);
  5661. domUtils.remove(start);
  5662. if (end) {
  5663. this.setEndBefore(end);
  5664. domUtils.remove(end);
  5665. } else {
  5666. this.collapse(true);
  5667. }
  5668. return this;
  5669. },
  5670. /**
  5671. * 调整range的边界,使其"放大"到最近的父节点
  5672. * @method enlarge
  5673. * @remind 会引起选区的变化
  5674. * @return { UE.dom.Range } 当前range对象
  5675. */
  5676. /**
  5677. * 调整range的边界,使其"放大"到最近的父节点,根据参数 toBlock 的取值, 可以
  5678. * 要求扩大之后的父节点是block节点
  5679. * @method enlarge
  5680. * @param { Boolean } toBlock 是否要求扩大之后的父节点必须是block节点
  5681. * @return { UE.dom.Range } 当前range对象
  5682. */
  5683. enlarge: function(toBlock, stopFn) {
  5684. var isBody = domUtils.isBody,
  5685. pre,
  5686. node,
  5687. tmp = this.document.createTextNode("");
  5688. if (toBlock) {
  5689. node = this.startContainer;
  5690. if (node.nodeType == 1) {
  5691. if (node.childNodes[this.startOffset]) {
  5692. pre = node = node.childNodes[this.startOffset];
  5693. } else {
  5694. node.appendChild(tmp);
  5695. pre = node = tmp;
  5696. }
  5697. } else {
  5698. pre = node;
  5699. }
  5700. while (1) {
  5701. if (domUtils.isBlockElm(node)) {
  5702. node = pre;
  5703. while ((pre = node.previousSibling) && !domUtils.isBlockElm(pre)) {
  5704. node = pre;
  5705. }
  5706. this.setStartBefore(node);
  5707. break;
  5708. }
  5709. pre = node;
  5710. node = node.parentNode;
  5711. }
  5712. node = this.endContainer;
  5713. if (node.nodeType == 1) {
  5714. if ((pre = node.childNodes[this.endOffset])) {
  5715. node.insertBefore(tmp, pre);
  5716. } else {
  5717. node.appendChild(tmp);
  5718. }
  5719. pre = node = tmp;
  5720. } else {
  5721. pre = node;
  5722. }
  5723. while (1) {
  5724. if (domUtils.isBlockElm(node)) {
  5725. node = pre;
  5726. while ((pre = node.nextSibling) && !domUtils.isBlockElm(pre)) {
  5727. node = pre;
  5728. }
  5729. this.setEndAfter(node);
  5730. break;
  5731. }
  5732. pre = node;
  5733. node = node.parentNode;
  5734. }
  5735. if (tmp.parentNode === this.endContainer) {
  5736. this.endOffset--;
  5737. }
  5738. domUtils.remove(tmp);
  5739. }
  5740. // 扩展边界到最大
  5741. if (!this.collapsed) {
  5742. while (this.startOffset == 0) {
  5743. if (stopFn && stopFn(this.startContainer)) {
  5744. break;
  5745. }
  5746. if (isBody(this.startContainer)) {
  5747. break;
  5748. }
  5749. this.setStartBefore(this.startContainer);
  5750. }
  5751. while (
  5752. this.endOffset ==
  5753. (this.endContainer.nodeType == 1
  5754. ? this.endContainer.childNodes.length
  5755. : this.endContainer.nodeValue.length)
  5756. ) {
  5757. if (stopFn && stopFn(this.endContainer)) {
  5758. break;
  5759. }
  5760. if (isBody(this.endContainer)) {
  5761. break;
  5762. }
  5763. this.setEndAfter(this.endContainer);
  5764. }
  5765. }
  5766. return this;
  5767. },
  5768. enlargeToBlockElm: function(ignoreEnd) {
  5769. while (!domUtils.isBlockElm(this.startContainer)) {
  5770. this.setStartBefore(this.startContainer);
  5771. }
  5772. if (!ignoreEnd) {
  5773. while (!domUtils.isBlockElm(this.endContainer)) {
  5774. this.setEndAfter(this.endContainer);
  5775. }
  5776. }
  5777. return this;
  5778. },
  5779. /**
  5780. * 调整Range的边界,使其"缩小"到最合适的位置
  5781. * @method adjustmentBoundary
  5782. * @return { UE.dom.Range } 当前range对象
  5783. * @see UE.dom.Range:shrinkBoundary()
  5784. */
  5785. adjustmentBoundary: function() {
  5786. if (!this.collapsed) {
  5787. while (
  5788. !domUtils.isBody(this.startContainer) &&
  5789. this.startOffset ==
  5790. this.startContainer[
  5791. this.startContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  5792. ].length &&
  5793. this.startContainer[
  5794. this.startContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  5795. ].length
  5796. ) {
  5797. this.setStartAfter(this.startContainer);
  5798. }
  5799. while (
  5800. !domUtils.isBody(this.endContainer) &&
  5801. !this.endOffset &&
  5802. this.endContainer[
  5803. this.endContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  5804. ].length
  5805. ) {
  5806. this.setEndBefore(this.endContainer);
  5807. }
  5808. }
  5809. return this;
  5810. },
  5811. /**
  5812. * 给range选区中的内容添加给定的inline标签
  5813. * @method applyInlineStyle
  5814. * @param { String } tagName 需要添加的标签名
  5815. * @example
  5816. * ```html
  5817. * <p>xxxx[xxxx]x</p> ==> range.applyInlineStyle("strong") ==> <p>xxxx[<strong>xxxx</strong>]x</p>
  5818. * ```
  5819. */
  5820. /**
  5821. * 给range选区中的内容添加给定的inline标签, 并且为标签附加上一些初始化属性。
  5822. * @method applyInlineStyle
  5823. * @param { String } tagName 需要添加的标签名
  5824. * @param { Object } attrs 跟随新添加的标签的属性
  5825. * @return { UE.dom.Range } 当前选区
  5826. * @example
  5827. * ```html
  5828. * <p>xxxx[xxxx]x</p>
  5829. *
  5830. * ==>
  5831. *
  5832. * <!-- 执行操作 -->
  5833. * range.applyInlineStyle("strong",{"style":"font-size:12px"})
  5834. *
  5835. * ==>
  5836. *
  5837. * <p>xxxx[<strong style="font-size:12px">xxxx</strong>]x</p>
  5838. * ```
  5839. */
  5840. applyInlineStyle: function(tagName, attrs, list) {
  5841. if (this.collapsed) return this;
  5842. this.trimBoundary()
  5843. .enlarge(false, function(node) {
  5844. return node.nodeType == 1 && domUtils.isBlockElm(node);
  5845. })
  5846. .adjustmentBoundary();
  5847. var bookmark = this.createBookmark(),
  5848. end = bookmark.end,
  5849. filterFn = function(node) {
  5850. return node.nodeType == 1
  5851. ? node.tagName.toLowerCase() != "br"
  5852. : !domUtils.isWhitespace(node);
  5853. },
  5854. current = domUtils.getNextDomNode(bookmark.start, false, filterFn),
  5855. node,
  5856. pre,
  5857. range = this.cloneRange();
  5858. while (
  5859. current &&
  5860. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  5861. ) {
  5862. if (current.nodeType == 3 || dtd[tagName][current.tagName]) {
  5863. range.setStartBefore(current);
  5864. node = current;
  5865. while (
  5866. node &&
  5867. (node.nodeType == 3 || dtd[tagName][node.tagName]) &&
  5868. node !== end
  5869. ) {
  5870. pre = node;
  5871. node = domUtils.getNextDomNode(
  5872. node,
  5873. node.nodeType == 1,
  5874. null,
  5875. function(parent) {
  5876. return dtd[tagName][parent.tagName];
  5877. }
  5878. );
  5879. }
  5880. var frag = range.setEndAfter(pre).extractContents(),
  5881. elm;
  5882. if (list && list.length > 0) {
  5883. var level, top;
  5884. top = level = list[0].cloneNode(false);
  5885. for (var i = 1, ci; (ci = list[i++]); ) {
  5886. level.appendChild(ci.cloneNode(false));
  5887. level = level.firstChild;
  5888. }
  5889. elm = level;
  5890. } else {
  5891. elm = range.document.createElement(tagName);
  5892. }
  5893. if (attrs) {
  5894. domUtils.setAttributes(elm, attrs);
  5895. }
  5896. elm.appendChild(frag);
  5897. //针对嵌套span的全局样式指定,做容错处理
  5898. if (elm.tagName == "SPAN" && attrs && attrs.style) {
  5899. utils.each(elm.getElementsByTagName("span"), function(s) {
  5900. s.style.cssText = s.style.cssText + ";" + attrs.style;
  5901. });
  5902. }
  5903. range.insertNode(list ? top : elm);
  5904. //处理下滑线在a上的情况
  5905. var aNode;
  5906. if (
  5907. tagName == "span" &&
  5908. attrs.style &&
  5909. /text\-decoration/.test(attrs.style) &&
  5910. (aNode = domUtils.findParentByTagName(elm, "a", true))
  5911. ) {
  5912. domUtils.setAttributes(aNode, attrs);
  5913. domUtils.remove(elm, true);
  5914. elm = aNode;
  5915. } else {
  5916. domUtils.mergeSibling(elm);
  5917. domUtils.clearEmptySibling(elm);
  5918. }
  5919. //去除子节点相同的
  5920. domUtils.mergeChild(elm, attrs);
  5921. current = domUtils.getNextDomNode(elm, false, filterFn);
  5922. domUtils.mergeToParent(elm);
  5923. if (node === end) {
  5924. break;
  5925. }
  5926. } else {
  5927. current = domUtils.getNextDomNode(current, true, filterFn);
  5928. }
  5929. }
  5930. return this.moveToBookmark(bookmark);
  5931. },
  5932. /**
  5933. * 移除当前选区内指定的inline标签,但保留其中的内容
  5934. * @method removeInlineStyle
  5935. * @param { String } tagName 需要移除的标签名
  5936. * @return { UE.dom.Range } 当前的range对象
  5937. * @example
  5938. * ```html
  5939. * xx[x<span>xxx<em>yyy</em>zz]z</span> => range.removeInlineStyle(["em"]) => xx[x<span>xxxyyyzz]z</span>
  5940. * ```
  5941. */
  5942. /**
  5943. * 移除当前选区内指定的一组inline标签,但保留其中的内容
  5944. * @method removeInlineStyle
  5945. * @param { Array } tagNameArr 需要移除的标签名的数组
  5946. * @return { UE.dom.Range } 当前的range对象
  5947. * @see UE.dom.Range:removeInlineStyle(String)
  5948. */
  5949. removeInlineStyle: function(tagNames) {
  5950. if (this.collapsed) return this;
  5951. tagNames = utils.isArray(tagNames) ? tagNames : [tagNames];
  5952. this.shrinkBoundary().adjustmentBoundary();
  5953. var start = this.startContainer,
  5954. end = this.endContainer;
  5955. while (1) {
  5956. if (start.nodeType == 1) {
  5957. if (utils.indexOf(tagNames, start.tagName.toLowerCase()) > -1) {
  5958. break;
  5959. }
  5960. if (start.tagName.toLowerCase() == "body") {
  5961. start = null;
  5962. break;
  5963. }
  5964. }
  5965. start = start.parentNode;
  5966. }
  5967. while (1) {
  5968. if (end.nodeType == 1) {
  5969. if (utils.indexOf(tagNames, end.tagName.toLowerCase()) > -1) {
  5970. break;
  5971. }
  5972. if (end.tagName.toLowerCase() == "body") {
  5973. end = null;
  5974. break;
  5975. }
  5976. }
  5977. end = end.parentNode;
  5978. }
  5979. var bookmark = this.createBookmark(),
  5980. frag,
  5981. tmpRange;
  5982. if (start) {
  5983. tmpRange = this.cloneRange()
  5984. .setEndBefore(bookmark.start)
  5985. .setStartBefore(start);
  5986. frag = tmpRange.extractContents();
  5987. tmpRange.insertNode(frag);
  5988. domUtils.clearEmptySibling(start, true);
  5989. start.parentNode.insertBefore(bookmark.start, start);
  5990. }
  5991. if (end) {
  5992. tmpRange = this.cloneRange()
  5993. .setStartAfter(bookmark.end)
  5994. .setEndAfter(end);
  5995. frag = tmpRange.extractContents();
  5996. tmpRange.insertNode(frag);
  5997. domUtils.clearEmptySibling(end, false, true);
  5998. end.parentNode.insertBefore(bookmark.end, end.nextSibling);
  5999. }
  6000. var current = domUtils.getNextDomNode(bookmark.start, false, function(
  6001. node
  6002. ) {
  6003. return node.nodeType == 1;
  6004. }),
  6005. next;
  6006. while (current && current !== bookmark.end) {
  6007. next = domUtils.getNextDomNode(current, true, function(node) {
  6008. return node.nodeType == 1;
  6009. });
  6010. if (utils.indexOf(tagNames, current.tagName.toLowerCase()) > -1) {
  6011. domUtils.remove(current, true);
  6012. }
  6013. current = next;
  6014. }
  6015. return this.moveToBookmark(bookmark);
  6016. },
  6017. /**
  6018. * 获取当前选中的自闭合的节点
  6019. * @method getClosedNode
  6020. * @return { Node | NULL } 如果当前选中的是自闭合节点, 则返回该节点, 否则返回NULL
  6021. */
  6022. getClosedNode: function() {
  6023. var node;
  6024. if (!this.collapsed) {
  6025. var range = this.cloneRange().adjustmentBoundary().shrinkBoundary();
  6026. if (selectOneNode(range)) {
  6027. var child = range.startContainer.childNodes[range.startOffset];
  6028. if (
  6029. child &&
  6030. child.nodeType === 1 &&
  6031. (dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName])
  6032. ) {
  6033. node = child;
  6034. }
  6035. }
  6036. }
  6037. return node;
  6038. },
  6039. /**
  6040. * 在页面上高亮range所表示的选区
  6041. * @method select
  6042. * @return { UE.dom.Range } 返回当前Range对象
  6043. */
  6044. //这里不区分ie9以上,trace:3824
  6045. select: browser.ie
  6046. ? function(noFillData, textRange) {
  6047. var nativeRange;
  6048. if (!this.collapsed) this.shrinkBoundary();
  6049. var node = this.getClosedNode();
  6050. if (node && !textRange) {
  6051. try {
  6052. nativeRange = this.document.body.createControlRange();
  6053. nativeRange.addElement(node);
  6054. nativeRange.select();
  6055. } catch (e) {}
  6056. return this;
  6057. }
  6058. var bookmark = this.createBookmark(),
  6059. start = bookmark.start,
  6060. end;
  6061. nativeRange = this.document.body.createTextRange();
  6062. nativeRange.moveToElementText(start);
  6063. nativeRange.moveStart("character", 1);
  6064. if (!this.collapsed) {
  6065. var nativeRangeEnd = this.document.body.createTextRange();
  6066. end = bookmark.end;
  6067. nativeRangeEnd.moveToElementText(end);
  6068. nativeRange.setEndPoint("EndToEnd", nativeRangeEnd);
  6069. } else {
  6070. if (!noFillData && this.startContainer.nodeType != 3) {
  6071. //使用<span>|x<span>固定住光标
  6072. var tmpText = this.document.createTextNode(fillChar),
  6073. tmp = this.document.createElement("span");
  6074. tmp.appendChild(this.document.createTextNode(fillChar));
  6075. start.parentNode.insertBefore(tmp, start);
  6076. start.parentNode.insertBefore(tmpText, start);
  6077. //当点b,i,u时,不能清除i上边的b
  6078. removeFillData(this.document, tmpText);
  6079. fillData = tmpText;
  6080. mergeSibling(tmp, "previousSibling");
  6081. mergeSibling(start, "nextSibling");
  6082. nativeRange.moveStart("character", -1);
  6083. nativeRange.collapse(true);
  6084. }
  6085. }
  6086. this.moveToBookmark(bookmark);
  6087. tmp && domUtils.remove(tmp);
  6088. //IE在隐藏状态下不支持range操作,catch一下
  6089. try {
  6090. nativeRange.select();
  6091. } catch (e) {}
  6092. return this;
  6093. }
  6094. : function(notInsertFillData) {
  6095. function checkOffset(rng) {
  6096. function check(node, offset, dir) {
  6097. if (node.nodeType == 3 && node.nodeValue.length < offset) {
  6098. rng[dir + "Offset"] = node.nodeValue.length;
  6099. }
  6100. }
  6101. check(rng.startContainer, rng.startOffset, "start");
  6102. check(rng.endContainer, rng.endOffset, "end");
  6103. }
  6104. var win = domUtils.getWindow(this.document),
  6105. sel = win.getSelection(),
  6106. txtNode;
  6107. //FF下关闭自动长高时滚动条在关闭dialog时会跳
  6108. //ff下如果不body.focus将不能定位闭合光标到编辑器内
  6109. browser.gecko ? this.document.body.focus() : win.focus();
  6110. if (sel) {
  6111. sel.removeAllRanges();
  6112. // trace:870 chrome/safari后边是br对于闭合得range不能定位 所以去掉了判断
  6113. // this.startContainer.nodeType != 3 &&! ((child = this.startContainer.childNodes[this.startOffset]) && child.nodeType == 1 && child.tagName == 'BR'
  6114. if (this.collapsed && !notInsertFillData) {
  6115. // //opear如果没有节点接着,原生的不能够定位,不能在body的第一级插入空白节点
  6116. // if (notInsertFillData && browser.opera && !domUtils.isBody(this.startContainer) && this.startContainer.nodeType == 1) {
  6117. // var tmp = this.document.createTextNode('');
  6118. // this.insertNode(tmp).setStart(tmp, 0).collapse(true);
  6119. // }
  6120. //
  6121. //处理光标落在文本节点的情况
  6122. //处理以下的情况
  6123. //<b>|xxxx</b>
  6124. //<b>xxxx</b>|xxxx
  6125. //xxxx<b>|</b>
  6126. var start = this.startContainer,
  6127. child = start;
  6128. if (start.nodeType == 1) {
  6129. child = start.childNodes[this.startOffset];
  6130. }
  6131. if (
  6132. !(start.nodeType == 3 && this.startOffset) &&
  6133. (child
  6134. ? !child.previousSibling ||
  6135. child.previousSibling.nodeType != 3
  6136. : !start.lastChild || start.lastChild.nodeType != 3)
  6137. ) {
  6138. txtNode = this.document.createTextNode(fillChar);
  6139. //跟着前边走
  6140. this.insertNode(txtNode);
  6141. removeFillData(this.document, txtNode);
  6142. mergeSibling(txtNode, "previousSibling");
  6143. mergeSibling(txtNode, "nextSibling");
  6144. fillData = txtNode;
  6145. this.setStart(txtNode, browser.webkit ? 1 : 0).collapse(true);
  6146. }
  6147. }
  6148. var nativeRange = this.document.createRange();
  6149. if (
  6150. this.collapsed &&
  6151. browser.opera &&
  6152. this.startContainer.nodeType == 1
  6153. ) {
  6154. var child = this.startContainer.childNodes[this.startOffset];
  6155. if (!child) {
  6156. //往前靠拢
  6157. child = this.startContainer.lastChild;
  6158. if (child && domUtils.isBr(child)) {
  6159. this.setStartBefore(child).collapse(true);
  6160. }
  6161. } else {
  6162. //向后靠拢
  6163. while (child && domUtils.isBlockElm(child)) {
  6164. if (child.nodeType == 1 && child.childNodes[0]) {
  6165. child = child.childNodes[0];
  6166. } else {
  6167. break;
  6168. }
  6169. }
  6170. child && this.setStartBefore(child).collapse(true);
  6171. }
  6172. }
  6173. //是createAddress最后一位算的不准,现在这里进行微调
  6174. checkOffset(this);
  6175. nativeRange.setStart(this.startContainer, this.startOffset);
  6176. nativeRange.setEnd(this.endContainer, this.endOffset);
  6177. sel.addRange(nativeRange);
  6178. }
  6179. return this;
  6180. },
  6181. /**
  6182. * 滚动到当前range开始的位置
  6183. * @method scrollToView
  6184. * @param { Window } win 当前range对象所属的window对象
  6185. * @return { UE.dom.Range } 当前Range对象
  6186. */
  6187. /**
  6188. * 滚动到距离当前range开始位置 offset 的位置处
  6189. * @method scrollToView
  6190. * @param { Window } win 当前range对象所属的window对象
  6191. * @param { Number } offset 距离range开始位置处的偏移量, 如果为正数, 则向下偏移, 反之, 则向上偏移
  6192. * @return { UE.dom.Range } 当前Range对象
  6193. */
  6194. scrollToView: function(win, offset) {
  6195. win = win ? window : domUtils.getWindow(this.document);
  6196. var me = this,
  6197. span = me.document.createElement("span");
  6198. //trace:717
  6199. span.innerHTML = "&nbsp;";
  6200. me.cloneRange().insertNode(span);
  6201. domUtils.scrollToView(span, win, offset);
  6202. domUtils.remove(span);
  6203. return me;
  6204. },
  6205. /**
  6206. * 判断当前选区内容是否占位符
  6207. * @private
  6208. * @method inFillChar
  6209. * @return { Boolean } 如果是占位符返回true,否则返回false
  6210. */
  6211. inFillChar: function() {
  6212. var start = this.startContainer;
  6213. if (
  6214. this.collapsed &&
  6215. start.nodeType == 3 &&
  6216. start.nodeValue.replace(new RegExp("^" + domUtils.fillChar), "")
  6217. .length +
  6218. 1 ==
  6219. start.nodeValue.length
  6220. ) {
  6221. return true;
  6222. }
  6223. return false;
  6224. },
  6225. /**
  6226. * 保存
  6227. * @method createAddress
  6228. * @private
  6229. * @return { Boolean } 返回开始和结束的位置
  6230. * @example
  6231. * ```html
  6232. * <body>
  6233. * <p>
  6234. * aaaa
  6235. * <em>
  6236. * <!-- 选区开始 -->
  6237. * bbbb
  6238. * <!-- 选区结束 -->
  6239. * </em>
  6240. * </p>
  6241. *
  6242. * <script>
  6243. * //output: {startAddress:[0,1,0,0],endAddress:[0,1,0,4]}
  6244. * console.log( range.createAddress() );
  6245. * </script>
  6246. * </body>
  6247. * ```
  6248. */
  6249. createAddress: function(ignoreEnd, ignoreTxt) {
  6250. var addr = {},
  6251. me = this;
  6252. function getAddress(isStart) {
  6253. var node = isStart ? me.startContainer : me.endContainer;
  6254. var parents = domUtils.findParents(node, true, function(node) {
  6255. return !domUtils.isBody(node);
  6256. }),
  6257. addrs = [];
  6258. for (var i = 0, ci; (ci = parents[i++]); ) {
  6259. addrs.push(domUtils.getNodeIndex(ci, ignoreTxt));
  6260. }
  6261. var firstIndex = 0;
  6262. if (ignoreTxt) {
  6263. if (node.nodeType == 3) {
  6264. var tmpNode = node.previousSibling;
  6265. while (tmpNode && tmpNode.nodeType == 3) {
  6266. firstIndex += tmpNode.nodeValue.replace(fillCharReg, "").length;
  6267. tmpNode = tmpNode.previousSibling;
  6268. }
  6269. firstIndex += isStart ? me.startOffset : me.endOffset; // - (fillCharReg.test(node.nodeValue) ? 1 : 0 )
  6270. } else {
  6271. node = node.childNodes[isStart ? me.startOffset : me.endOffset];
  6272. if (node) {
  6273. firstIndex = domUtils.getNodeIndex(node, ignoreTxt);
  6274. } else {
  6275. node = isStart ? me.startContainer : me.endContainer;
  6276. var first = node.firstChild;
  6277. while (first) {
  6278. if (domUtils.isFillChar(first)) {
  6279. first = first.nextSibling;
  6280. continue;
  6281. }
  6282. firstIndex++;
  6283. if (first.nodeType == 3) {
  6284. while (first && first.nodeType == 3) {
  6285. first = first.nextSibling;
  6286. }
  6287. } else {
  6288. first = first.nextSibling;
  6289. }
  6290. }
  6291. }
  6292. }
  6293. } else {
  6294. firstIndex = isStart
  6295. ? domUtils.isFillChar(node) ? 0 : me.startOffset
  6296. : me.endOffset;
  6297. }
  6298. if (firstIndex < 0) {
  6299. firstIndex = 0;
  6300. }
  6301. addrs.push(firstIndex);
  6302. return addrs;
  6303. }
  6304. addr.startAddress = getAddress(true);
  6305. if (!ignoreEnd) {
  6306. addr.endAddress = me.collapsed
  6307. ? [].concat(addr.startAddress)
  6308. : getAddress();
  6309. }
  6310. return addr;
  6311. },
  6312. /**
  6313. * 保存
  6314. * @method createAddress
  6315. * @private
  6316. * @return { Boolean } 返回开始和结束的位置
  6317. * @example
  6318. * ```html
  6319. * <body>
  6320. * <p>
  6321. * aaaa
  6322. * <em>
  6323. * <!-- 选区开始 -->
  6324. * bbbb
  6325. * <!-- 选区结束 -->
  6326. * </em>
  6327. * </p>
  6328. *
  6329. * <script>
  6330. * var range = editor.selection.getRange();
  6331. * range.moveToAddress({startAddress:[0,1,0,0],endAddress:[0,1,0,4]});
  6332. * range.select();
  6333. * //output: 'bbbb'
  6334. * console.log(editor.selection.getText());
  6335. * </script>
  6336. * </body>
  6337. * ```
  6338. */
  6339. moveToAddress: function(addr, ignoreEnd) {
  6340. var me = this;
  6341. function getNode(address, isStart) {
  6342. var tmpNode = me.document.body,
  6343. parentNode,
  6344. offset;
  6345. for (var i = 0, ci, l = address.length; i < l; i++) {
  6346. ci = address[i];
  6347. parentNode = tmpNode;
  6348. tmpNode = tmpNode.childNodes[ci];
  6349. if (!tmpNode) {
  6350. offset = ci;
  6351. break;
  6352. }
  6353. }
  6354. if (isStart) {
  6355. if (tmpNode) {
  6356. me.setStartBefore(tmpNode);
  6357. } else {
  6358. me.setStart(parentNode, offset);
  6359. }
  6360. } else {
  6361. if (tmpNode) {
  6362. me.setEndBefore(tmpNode);
  6363. } else {
  6364. me.setEnd(parentNode, offset);
  6365. }
  6366. }
  6367. }
  6368. getNode(addr.startAddress, true);
  6369. !ignoreEnd && addr.endAddress && getNode(addr.endAddress);
  6370. return me;
  6371. },
  6372. /**
  6373. * 判断给定的Range对象是否和当前Range对象表示的是同一个选区
  6374. * @method equals
  6375. * @param { UE.dom.Range } 需要判断的Range对象
  6376. * @return { Boolean } 如果给定的Range对象与当前Range对象表示的是同一个选区, 则返回true, 否则返回false
  6377. */
  6378. equals: function(rng) {
  6379. for (var p in this) {
  6380. if (this.hasOwnProperty(p)) {
  6381. if (this[p] !== rng[p]) return false;
  6382. }
  6383. }
  6384. return true;
  6385. },
  6386. /**
  6387. * 遍历range内的节点。每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6388. * 作为其参数。
  6389. * @method traversal
  6390. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6391. * @return { UE.dom.Range } 当前range对象
  6392. * @example
  6393. * ```html
  6394. *
  6395. * <body>
  6396. *
  6397. * <!-- 选区开始 -->
  6398. * <span></span>
  6399. * <a></a>
  6400. * <!-- 选区结束 -->
  6401. * </body>
  6402. *
  6403. * <script>
  6404. *
  6405. * //output: <span></span><a></a>
  6406. * console.log( range.cloneContents() );
  6407. *
  6408. * range.traversal( function ( node ) {
  6409. *
  6410. * if ( node.nodeType === 1 ) {
  6411. * node.className = "test";
  6412. * }
  6413. *
  6414. * } );
  6415. *
  6416. * //output: <span class="test"></span><a class="test"></a>
  6417. * console.log( range.cloneContents() );
  6418. *
  6419. * </script>
  6420. * ```
  6421. */
  6422. /**
  6423. * 遍历range内的节点。
  6424. * 每当遍历一个节点时, 都会执行参数项 doFn 指定的函数, 该函数的接受当前遍历的节点
  6425. * 作为其参数。
  6426. * 可以通过参数项 filterFn 来指定一个过滤器, 只有符合该过滤器过滤规则的节点才会触
  6427. * 发doFn函数的执行
  6428. * @method traversal
  6429. * @param { Function } doFn 对每个遍历的节点要执行的方法, 该方法接受当前遍历的节点作为其参数
  6430. * @param { Function } filterFn 过滤器, 该函数接受当前遍历的节点作为参数, 如果该节点满足过滤
  6431. * 规则, 请返回true, 该节点会触发doFn, 否则, 请返回false, 则该节点不
  6432. * 会触发doFn。
  6433. * @return { UE.dom.Range } 当前range对象
  6434. * @see UE.dom.Range:traversal(Function)
  6435. * @example
  6436. * ```html
  6437. *
  6438. * <body>
  6439. *
  6440. * <!-- 选区开始 -->
  6441. * <span></span>
  6442. * <a></a>
  6443. * <!-- 选区结束 -->
  6444. * </body>
  6445. *
  6446. * <script>
  6447. *
  6448. * //output: <span></span><a></a>
  6449. * console.log( range.cloneContents() );
  6450. *
  6451. * range.traversal( function ( node ) {
  6452. *
  6453. * node.className = "test";
  6454. *
  6455. * }, function ( node ) {
  6456. * return node.nodeType === 1;
  6457. * } );
  6458. *
  6459. * //output: <span class="test"></span><a class="test"></a>
  6460. * console.log( range.cloneContents() );
  6461. *
  6462. * </script>
  6463. * ```
  6464. */
  6465. traversal: function(doFn, filterFn) {
  6466. if (this.collapsed) return this;
  6467. var bookmark = this.createBookmark(),
  6468. end = bookmark.end,
  6469. current = domUtils.getNextDomNode(bookmark.start, false, filterFn);
  6470. while (
  6471. current &&
  6472. current !== end &&
  6473. domUtils.getPosition(current, end) & domUtils.POSITION_PRECEDING
  6474. ) {
  6475. var tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  6476. doFn(current);
  6477. current = tmpNode;
  6478. }
  6479. return this.moveToBookmark(bookmark);
  6480. }
  6481. };
  6482. })();
  6483. // core/Selection.js
  6484. /**
  6485. * 选集
  6486. * @file
  6487. * @module UE.dom
  6488. * @class Selection
  6489. * @since 1.2.6.1
  6490. */
  6491. /**
  6492. * 选区集合
  6493. * @unfile
  6494. * @module UE.dom
  6495. * @class Selection
  6496. */
  6497. (function() {
  6498. function getBoundaryInformation(range, start) {
  6499. var getIndex = domUtils.getNodeIndex;
  6500. range = range.duplicate();
  6501. range.collapse(start);
  6502. var parent = range.parentElement();
  6503. //如果节点里没有子节点,直接退出
  6504. if (!parent.hasChildNodes()) {
  6505. return { container: parent, offset: 0 };
  6506. }
  6507. var siblings = parent.children,
  6508. child,
  6509. testRange = range.duplicate(),
  6510. startIndex = 0,
  6511. endIndex = siblings.length - 1,
  6512. index = -1,
  6513. distance;
  6514. while (startIndex <= endIndex) {
  6515. index = Math.floor((startIndex + endIndex) / 2);
  6516. child = siblings[index];
  6517. testRange.moveToElementText(child);
  6518. var position = testRange.compareEndPoints("StartToStart", range);
  6519. if (position > 0) {
  6520. endIndex = index - 1;
  6521. } else if (position < 0) {
  6522. startIndex = index + 1;
  6523. } else {
  6524. //trace:1043
  6525. return { container: parent, offset: getIndex(child) };
  6526. }
  6527. }
  6528. if (index == -1) {
  6529. testRange.moveToElementText(parent);
  6530. testRange.setEndPoint("StartToStart", range);
  6531. distance = testRange.text.replace(/(\r\n|\r)/g, "\n").length;
  6532. siblings = parent.childNodes;
  6533. if (!distance) {
  6534. child = siblings[siblings.length - 1];
  6535. return { container: child, offset: child.nodeValue.length };
  6536. }
  6537. var i = siblings.length;
  6538. while (distance > 0) {
  6539. distance -= siblings[--i].nodeValue.length;
  6540. }
  6541. return { container: siblings[i], offset: -distance };
  6542. }
  6543. testRange.collapse(position > 0);
  6544. testRange.setEndPoint(position > 0 ? "StartToStart" : "EndToStart", range);
  6545. distance = testRange.text.replace(/(\r\n|\r)/g, "\n").length;
  6546. if (!distance) {
  6547. return dtd.$empty[child.tagName] || dtd.$nonChild[child.tagName]
  6548. ? {
  6549. container: parent,
  6550. offset: getIndex(child) + (position > 0 ? 0 : 1)
  6551. }
  6552. : {
  6553. container: child,
  6554. offset: position > 0 ? 0 : child.childNodes.length
  6555. };
  6556. }
  6557. while (distance > 0) {
  6558. try {
  6559. var pre = child;
  6560. child = child[position > 0 ? "previousSibling" : "nextSibling"];
  6561. distance -= child.nodeValue.length;
  6562. } catch (e) {
  6563. return { container: parent, offset: getIndex(pre) };
  6564. }
  6565. }
  6566. return {
  6567. container: child,
  6568. offset: position > 0 ? -distance : child.nodeValue.length + distance
  6569. };
  6570. }
  6571. /**
  6572. * 将ieRange转换为Range对象
  6573. * @param {Range} ieRange ieRange对象
  6574. * @param {Range} range Range对象
  6575. * @return {Range} range 返回转换后的Range对象
  6576. */
  6577. function transformIERangeToRange(ieRange, range) {
  6578. if (ieRange.item) {
  6579. range.selectNode(ieRange.item(0));
  6580. } else {
  6581. var bi = getBoundaryInformation(ieRange, true);
  6582. range.setStart(bi.container, bi.offset);
  6583. if (ieRange.compareEndPoints("StartToEnd", ieRange) != 0) {
  6584. bi = getBoundaryInformation(ieRange, false);
  6585. range.setEnd(bi.container, bi.offset);
  6586. }
  6587. }
  6588. return range;
  6589. }
  6590. /**
  6591. * 获得ieRange
  6592. * @param {Selection} sel Selection对象
  6593. * @return {ieRange} 得到ieRange
  6594. */
  6595. function _getIERange(sel) {
  6596. var ieRange;
  6597. //ie下有可能报错
  6598. try {
  6599. ieRange = sel.getNative().createRange();
  6600. } catch (e) {
  6601. return null;
  6602. }
  6603. var el = ieRange.item ? ieRange.item(0) : ieRange.parentElement();
  6604. if ((el.ownerDocument || el) === sel.document) {
  6605. return ieRange;
  6606. }
  6607. return null;
  6608. }
  6609. var Selection = (dom.Selection = function(doc) {
  6610. var me = this,
  6611. iframe;
  6612. me.document = doc;
  6613. if (browser.ie9below) {
  6614. iframe = domUtils.getWindow(doc).frameElement;
  6615. domUtils.on(iframe, "beforedeactivate", function() {
  6616. me._bakIERange = me.getIERange();
  6617. });
  6618. domUtils.on(iframe, "activate", function() {
  6619. try {
  6620. if (!_getIERange(me) && me._bakIERange) {
  6621. me._bakIERange.select();
  6622. }
  6623. } catch (ex) {}
  6624. me._bakIERange = null;
  6625. });
  6626. }
  6627. iframe = doc = null;
  6628. });
  6629. Selection.prototype = {
  6630. rangeInBody: function(rng, txtRange) {
  6631. var node = browser.ie9below || txtRange
  6632. ? rng.item ? rng.item() : rng.parentElement()
  6633. : rng.startContainer;
  6634. return node === this.document.body || domUtils.inDoc(node, this.document);
  6635. },
  6636. /**
  6637. * 获取原生seleciton对象
  6638. * @method getNative
  6639. * @return { Object } 获得selection对象
  6640. * @example
  6641. * ```javascript
  6642. * editor.selection.getNative();
  6643. * ```
  6644. */
  6645. getNative: function() {
  6646. var doc = this.document;
  6647. try {
  6648. return !doc
  6649. ? null
  6650. : browser.ie9below
  6651. ? doc.selection
  6652. : domUtils.getWindow(doc).getSelection();
  6653. } catch (e) {
  6654. return null;
  6655. }
  6656. },
  6657. /**
  6658. * 获得ieRange
  6659. * @method getIERange
  6660. * @return { Object } 返回ie原生的Range
  6661. * @example
  6662. * ```javascript
  6663. * editor.selection.getIERange();
  6664. * ```
  6665. */
  6666. getIERange: function() {
  6667. var ieRange = _getIERange(this);
  6668. if (!ieRange) {
  6669. if (this._bakIERange) {
  6670. return this._bakIERange;
  6671. }
  6672. }
  6673. return ieRange;
  6674. },
  6675. /**
  6676. * 缓存当前选区的range和选区的开始节点
  6677. * @method cache
  6678. */
  6679. cache: function() {
  6680. this.clear();
  6681. this._cachedRange = this.getRange();
  6682. this._cachedStartElement = this.getStart();
  6683. this._cachedStartElementPath = this.getStartElementPath();
  6684. },
  6685. /**
  6686. * 获取选区开始位置的父节点到body
  6687. * @method getStartElementPath
  6688. * @return { Array } 返回父节点集合
  6689. * @example
  6690. * ```javascript
  6691. * editor.selection.getStartElementPath();
  6692. * ```
  6693. */
  6694. getStartElementPath: function() {
  6695. if (this._cachedStartElementPath) {
  6696. return this._cachedStartElementPath;
  6697. }
  6698. var start = this.getStart();
  6699. if (start) {
  6700. return domUtils.findParents(start, true, null, true);
  6701. }
  6702. return [];
  6703. },
  6704. /**
  6705. * 清空缓存
  6706. * @method clear
  6707. */
  6708. clear: function() {
  6709. this._cachedStartElementPath = this._cachedRange = this._cachedStartElement = null;
  6710. },
  6711. /**
  6712. * 编辑器是否得到了选区
  6713. * @method isFocus
  6714. */
  6715. isFocus: function() {
  6716. try {
  6717. if (browser.ie9below) {
  6718. var nativeRange = _getIERange(this);
  6719. return !!(nativeRange && this.rangeInBody(nativeRange));
  6720. } else {
  6721. return !!this.getNative().rangeCount;
  6722. }
  6723. } catch (e) {
  6724. return false;
  6725. }
  6726. },
  6727. /**
  6728. * 获取选区对应的Range
  6729. * @method getRange
  6730. * @return { Object } 得到Range对象
  6731. * @example
  6732. * ```javascript
  6733. * editor.selection.getRange();
  6734. * ```
  6735. */
  6736. getRange: function() {
  6737. var me = this;
  6738. function optimze(range) {
  6739. var child = me.document.body.firstChild,
  6740. collapsed = range.collapsed;
  6741. while (child && child.firstChild) {
  6742. range.setStart(child, 0);
  6743. child = child.firstChild;
  6744. }
  6745. if (!range.startContainer) {
  6746. range.setStart(me.document.body, 0);
  6747. }
  6748. if (collapsed) {
  6749. range.collapse(true);
  6750. }
  6751. }
  6752. if (me._cachedRange != null) {
  6753. return this._cachedRange;
  6754. }
  6755. var range = new baidu.editor.dom.Range(me.document);
  6756. if (browser.ie9below) {
  6757. var nativeRange = me.getIERange();
  6758. if (nativeRange) {
  6759. //备份的_bakIERange可能已经实效了,dom树发生了变化比如从源码模式切回来,所以try一下,实效就放到body开始位置
  6760. try {
  6761. transformIERangeToRange(nativeRange, range);
  6762. } catch (e) {
  6763. optimze(range);
  6764. }
  6765. } else {
  6766. optimze(range);
  6767. }
  6768. } else {
  6769. var sel = me.getNative();
  6770. if (sel && sel.rangeCount) {
  6771. var firstRange = sel.getRangeAt(0);
  6772. var lastRange = sel.getRangeAt(sel.rangeCount - 1);
  6773. range
  6774. .setStart(firstRange.startContainer, firstRange.startOffset)
  6775. .setEnd(lastRange.endContainer, lastRange.endOffset);
  6776. if (
  6777. range.collapsed &&
  6778. domUtils.isBody(range.startContainer) &&
  6779. !range.startOffset
  6780. ) {
  6781. optimze(range);
  6782. }
  6783. } else {
  6784. //trace:1734 有可能已经不在dom树上了,标识的节点
  6785. if (
  6786. this._bakRange &&
  6787. domUtils.inDoc(this._bakRange.startContainer, this.document)
  6788. ) {
  6789. return this._bakRange;
  6790. }
  6791. optimze(range);
  6792. }
  6793. }
  6794. return (this._bakRange = range);
  6795. },
  6796. /**
  6797. * 获取开始元素,用于状态反射
  6798. * @method getStart
  6799. * @return { Element } 获得开始元素
  6800. * @example
  6801. * ```javascript
  6802. * editor.selection.getStart();
  6803. * ```
  6804. */
  6805. getStart: function() {
  6806. if (this._cachedStartElement) {
  6807. return this._cachedStartElement;
  6808. }
  6809. var range = browser.ie9below ? this.getIERange() : this.getRange(),
  6810. tmpRange,
  6811. start,
  6812. tmp,
  6813. parent;
  6814. if (browser.ie9below) {
  6815. if (!range) {
  6816. //todo 给第一个值可能会有问题
  6817. return this.document.body.firstChild;
  6818. }
  6819. //control元素
  6820. if (range.item) {
  6821. return range.item(0);
  6822. }
  6823. tmpRange = range.duplicate();
  6824. //修正ie下<b>x</b>[xx] 闭合后 <b>x|</b>xx
  6825. tmpRange.text.length > 0 && tmpRange.moveStart("character", 1);
  6826. tmpRange.collapse(1);
  6827. start = tmpRange.parentElement();
  6828. parent = tmp = range.parentElement();
  6829. while ((tmp = tmp.parentNode)) {
  6830. if (tmp == start) {
  6831. start = parent;
  6832. break;
  6833. }
  6834. }
  6835. } else {
  6836. range.shrinkBoundary();
  6837. start = range.startContainer;
  6838. if (start.nodeType == 1 && start.hasChildNodes()) {
  6839. start =
  6840. start.childNodes[
  6841. Math.min(start.childNodes.length - 1, range.startOffset)
  6842. ];
  6843. }
  6844. if (start.nodeType == 3) {
  6845. return start.parentNode;
  6846. }
  6847. }
  6848. return start;
  6849. },
  6850. /**
  6851. * 得到选区中的文本
  6852. * @method getText
  6853. * @return { String } 选区中包含的文本
  6854. * @example
  6855. * ```javascript
  6856. * editor.selection.getText();
  6857. * ```
  6858. */
  6859. getText: function() {
  6860. var nativeSel, nativeRange;
  6861. if (this.isFocus() && (nativeSel = this.getNative())) {
  6862. nativeRange = browser.ie9below
  6863. ? nativeSel.createRange()
  6864. : nativeSel.getRangeAt(0);
  6865. return browser.ie9below ? nativeRange.text : nativeRange.toString();
  6866. }
  6867. return "";
  6868. },
  6869. /**
  6870. * 清除选区
  6871. * @method clearRange
  6872. * @example
  6873. * ```javascript
  6874. * editor.selection.clearRange();
  6875. * ```
  6876. */
  6877. clearRange: function() {
  6878. this.getNative()[browser.ie9below ? "empty" : "removeAllRanges"]();
  6879. }
  6880. };
  6881. })();
  6882. // core/Editor.js
  6883. /**
  6884. * 编辑器主类,包含编辑器提供的大部分公用接口
  6885. * @file
  6886. * @module UE
  6887. * @class Editor
  6888. * @since 1.2.6.1
  6889. */
  6890. /**
  6891. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  6892. * @unfile
  6893. * @module UE
  6894. */
  6895. /**
  6896. * UEditor的核心类,为用户提供与编辑器交互的接口。
  6897. * @unfile
  6898. * @module UE
  6899. * @class Editor
  6900. */
  6901. (function() {
  6902. var uid = 0,
  6903. _selectionChangeTimer;
  6904. /**
  6905. * 获取编辑器的html内容,赋值到编辑器所在表单的textarea文本域里面
  6906. * @private
  6907. * @method setValue
  6908. * @param { UE.Editor } editor 编辑器事例
  6909. */
  6910. function setValue(form, editor) {
  6911. var textarea;
  6912. if (editor.options.textarea) {
  6913. if (utils.isString(editor.options.textarea)) {
  6914. for (
  6915. var i = 0, ti, tis = domUtils.getElementsByTagName(form, "textarea");
  6916. (ti = tis[i++]);
  6917. ) {
  6918. if (ti.id == "ueditor_textarea_" + editor.options.textarea) {
  6919. textarea = ti;
  6920. break;
  6921. }
  6922. }
  6923. } else {
  6924. textarea = editor.textarea;
  6925. }
  6926. }
  6927. if (!textarea) {
  6928. form.appendChild(
  6929. (textarea = domUtils.createElement(document, "textarea", {
  6930. name: editor.options.textarea,
  6931. id: "ueditor_textarea_" + editor.options.textarea,
  6932. style: "display:none"
  6933. }))
  6934. );
  6935. //不要产生多个textarea
  6936. editor.textarea = textarea;
  6937. }
  6938. !textarea.getAttribute("name") &&
  6939. textarea.setAttribute("name", editor.options.textarea);
  6940. textarea.value = editor.hasContents()
  6941. ? editor.options.allHtmlEnabled
  6942. ? editor.getAllHtml()
  6943. : editor.getContent(null, null, true)
  6944. : "";
  6945. }
  6946. function loadPlugins(me) {
  6947. //初始化插件
  6948. for (var pi in UE.plugins) {
  6949. UE.plugins[pi].call(me);
  6950. }
  6951. }
  6952. function checkCurLang(I18N) {
  6953. for (var lang in I18N) {
  6954. return lang;
  6955. }
  6956. }
  6957. function langReadied(me) {
  6958. me.langIsReady = true;
  6959. me.fireEvent("langReady");
  6960. }
  6961. /**
  6962. * 编辑器准备就绪后会触发该事件
  6963. * @module UE
  6964. * @class Editor
  6965. * @event ready
  6966. * @remind render方法执行完成之后,会触发该事件
  6967. * @remind
  6968. * @example
  6969. * ```javascript
  6970. * editor.addListener( 'ready', function( editor ) {
  6971. * editor.execCommand( 'focus' ); //编辑器家在完成后,让编辑器拿到焦点
  6972. * } );
  6973. * ```
  6974. */
  6975. /**
  6976. * 执行destroy方法,会触发该事件
  6977. * @module UE
  6978. * @class Editor
  6979. * @event destroy
  6980. * @see UE.Editor:destroy()
  6981. */
  6982. /**
  6983. * 执行reset方法,会触发该事件
  6984. * @module UE
  6985. * @class Editor
  6986. * @event reset
  6987. * @see UE.Editor:reset()
  6988. */
  6989. /**
  6990. * 执行focus方法,会触发该事件
  6991. * @module UE
  6992. * @class Editor
  6993. * @event focus
  6994. * @see UE.Editor:focus(Boolean)
  6995. */
  6996. /**
  6997. * 语言加载完成会触发该事件
  6998. * @module UE
  6999. * @class Editor
  7000. * @event langReady
  7001. */
  7002. /**
  7003. * 运行命令之后会触发该命令
  7004. * @module UE
  7005. * @class Editor
  7006. * @event beforeExecCommand
  7007. */
  7008. /**
  7009. * 运行命令之后会触发该命令
  7010. * @module UE
  7011. * @class Editor
  7012. * @event afterExecCommand
  7013. */
  7014. /**
  7015. * 运行命令之前会触发该命令
  7016. * @module UE
  7017. * @class Editor
  7018. * @event firstBeforeExecCommand
  7019. */
  7020. /**
  7021. * 在getContent方法执行之前会触发该事件
  7022. * @module UE
  7023. * @class Editor
  7024. * @event beforeGetContent
  7025. * @see UE.Editor:getContent()
  7026. */
  7027. /**
  7028. * 在getContent方法执行之后会触发该事件
  7029. * @module UE
  7030. * @class Editor
  7031. * @event afterGetContent
  7032. * @see UE.Editor:getContent()
  7033. */
  7034. /**
  7035. * 在getAllHtml方法执行时会触发该事件
  7036. * @module UE
  7037. * @class Editor
  7038. * @event getAllHtml
  7039. * @see UE.Editor:getAllHtml()
  7040. */
  7041. /**
  7042. * 在setContent方法执行之前会触发该事件
  7043. * @module UE
  7044. * @class Editor
  7045. * @event beforeSetContent
  7046. * @see UE.Editor:setContent(String)
  7047. */
  7048. /**
  7049. * 在setContent方法执行之后会触发该事件
  7050. * @module UE
  7051. * @class Editor
  7052. * @event afterSetContent
  7053. * @see UE.Editor:setContent(String)
  7054. */
  7055. /**
  7056. * 每当编辑器内部选区发生改变时,将触发该事件
  7057. * @event selectionchange
  7058. * @warning 该事件的触发非常频繁,不建议在该事件的处理过程中做重量级的处理
  7059. * @example
  7060. * ```javascript
  7061. * editor.addListener( 'selectionchange', function( editor ) {
  7062. * console.log('选区发生改变');
  7063. * }
  7064. */
  7065. /**
  7066. * 在所有selectionchange的监听函数执行之前,会触发该事件
  7067. * @module UE
  7068. * @class Editor
  7069. * @event beforeSelectionChange
  7070. * @see UE.Editor:selectionchange
  7071. */
  7072. /**
  7073. * 在所有selectionchange的监听函数执行完之后,会触发该事件
  7074. * @module UE
  7075. * @class Editor
  7076. * @event afterSelectionChange
  7077. * @see UE.Editor:selectionchange
  7078. */
  7079. /**
  7080. * 编辑器内容发生改变时会触发该事件
  7081. * @module UE
  7082. * @class Editor
  7083. * @event contentChange
  7084. */
  7085. /**
  7086. * 以默认参数构建一个编辑器实例
  7087. * @constructor
  7088. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7089. * @example
  7090. * ```javascript
  7091. * var editor = new UE.Editor();
  7092. * editor.execCommand('blod');
  7093. * ```
  7094. * @see UE.Config
  7095. */
  7096. /**
  7097. * 以给定的参数集合创建一个编辑器实例,对于未指定的参数,将应用默认参数。
  7098. * @constructor
  7099. * @remind 通过 改构造方法实例化的编辑器,不带ui层.需要render到一个容器,编辑器实例才能正常渲染到页面
  7100. * @param { Object } setting 创建编辑器的参数
  7101. * @example
  7102. * ```javascript
  7103. * var editor = new UE.Editor();
  7104. * editor.execCommand('blod');
  7105. * ```
  7106. * @see UE.Config
  7107. */
  7108. var Editor = (UE.Editor = function(options) {
  7109. var me = this;
  7110. me.uid = uid++;
  7111. EventBase.call(me);
  7112. me.commands = {};
  7113. me.options = utils.extend(utils.clone(options || {}), UEDITOR_CONFIG, true);
  7114. me.shortcutkeys = {};
  7115. me.inputRules = [];
  7116. me.outputRules = [];
  7117. //设置默认的常用属性
  7118. me.setOpt(Editor.defaultOptions(me));
  7119. /* 尝试异步加载后台配置 */
  7120. me.loadServerConfig();
  7121. if (!utils.isEmptyObject(UE.I18N)) {
  7122. //修改默认的语言类型
  7123. me.options.lang = checkCurLang(UE.I18N);
  7124. UE.plugin.load(me);
  7125. langReadied(me);
  7126. } else {
  7127. utils.loadFile(
  7128. document,
  7129. {
  7130. src:
  7131. me.options.langPath +
  7132. me.options.lang +
  7133. "/" +
  7134. me.options.lang +
  7135. ".js?20230319",
  7136. tag: "script",
  7137. type: "text/javascript",
  7138. defer: "defer"
  7139. },
  7140. function() {
  7141. UE.plugin.load(me);
  7142. langReadied(me);
  7143. }
  7144. );
  7145. }
  7146. UE.instants["ueditorInstant" + me.uid] = me;
  7147. });
  7148. Editor.prototype = {
  7149. registerCommand: function(name, obj) {
  7150. this.commands[name] = obj;
  7151. },
  7152. /**
  7153. * 编辑器对外提供的监听ready事件的接口, 通过调用该方法,达到的效果与监听ready事件是一致的
  7154. * @method ready
  7155. * @param { Function } fn 编辑器ready之后所执行的回调, 如果在注册事件之前编辑器已经ready,将会
  7156. * 立即触发该回调。
  7157. * @remind 需要等待编辑器加载完成后才能执行的代码,可以使用该方法传入
  7158. * @example
  7159. * ```javascript
  7160. * editor.ready( function( editor ) {
  7161. * editor.setContent('初始化完毕');
  7162. * } );
  7163. * ```
  7164. * @see UE.Editor.event:ready
  7165. */
  7166. ready: function(fn) {
  7167. var me = this;
  7168. if (fn) {
  7169. me.isReady ? fn.apply(me) : me.addListener("ready", fn);
  7170. }
  7171. },
  7172. /**
  7173. * 该方法是提供给插件里面使用,设置配置项默认值
  7174. * @method setOpt
  7175. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7176. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7177. * @param { String } key 编辑器的可接受的选项名称
  7178. * @param { * } val 该选项可接受的值
  7179. * @example
  7180. * ```javascript
  7181. * editor.setOpt( 'initContent', '欢迎使用编辑器' );
  7182. * ```
  7183. */
  7184. /**
  7185. * 该方法是提供给插件里面使用,以{key:value}集合的方式设置插件内用到的配置项默认值
  7186. * @method setOpt
  7187. * @warning 三处设置配置项的优先级: 实例化时传入参数 > setOpt()设置 > config文件里设置
  7188. * @warning 该方法仅供编辑器插件内部和编辑器初始化时调用,其他地方不能调用。
  7189. * @param { Object } options 将要设置的选项的键值对对象
  7190. * @example
  7191. * ```javascript
  7192. * editor.setOpt( {
  7193. * 'initContent': '欢迎使用编辑器'
  7194. * } );
  7195. * ```
  7196. */
  7197. setOpt: function(key, val) {
  7198. var obj = {};
  7199. if (utils.isString(key)) {
  7200. obj[key] = val;
  7201. } else {
  7202. obj = key;
  7203. }
  7204. utils.extend(this.options, obj, true);
  7205. },
  7206. getOpt: function(key) {
  7207. return this.options[key];
  7208. },
  7209. /**
  7210. * 销毁编辑器实例,使用textarea代替
  7211. * @method destroy
  7212. * @example
  7213. * ```javascript
  7214. * editor.destroy();
  7215. * ```
  7216. */
  7217. destroy: function() {
  7218. var me = this;
  7219. me.fireEvent("destroy");
  7220. var container = me.container.parentNode;
  7221. var textarea = me.textarea;
  7222. if (!textarea) {
  7223. textarea = document.createElement("textarea");
  7224. container.parentNode.insertBefore(textarea, container);
  7225. } else {
  7226. textarea.style.display = "";
  7227. }
  7228. textarea.style.width = me.iframe.offsetWidth + "px";
  7229. textarea.style.height = me.iframe.offsetHeight + "px";
  7230. textarea.value = me.getContent();
  7231. textarea.id = me.key;
  7232. container.innerHTML = "";
  7233. domUtils.remove(container);
  7234. var key = me.key;
  7235. //trace:2004
  7236. for (var p in me) {
  7237. if (me.hasOwnProperty(p)) {
  7238. delete this[p];
  7239. }
  7240. }
  7241. UE.delEditor(key);
  7242. },
  7243. /**
  7244. * 渲染编辑器的DOM到指定容器
  7245. * @method render
  7246. * @param { String } containerId 指定一个容器ID
  7247. * @remind 执行该方法,会触发ready事件
  7248. * @warning 必须且只能调用一次
  7249. */
  7250. /**
  7251. * 渲染编辑器的DOM到指定容器
  7252. * @method render
  7253. * @param { Element } containerDom 直接指定容器对象
  7254. * @remind 执行该方法,会触发ready事件
  7255. * @warning 必须且只能调用一次
  7256. */
  7257. render: function(container) {
  7258. var me = this,
  7259. options = me.options,
  7260. getStyleValue = function(attr) {
  7261. return parseInt(domUtils.getComputedStyle(container, attr));
  7262. };
  7263. if (utils.isString(container)) {
  7264. container = document.getElementById(container);
  7265. }
  7266. if (container) {
  7267. if (options.initialFrameWidth) {
  7268. options.minFrameWidth = options.initialFrameWidth;
  7269. } else {
  7270. options.minFrameWidth = options.initialFrameWidth =
  7271. container.offsetWidth;
  7272. }
  7273. if (options.initialFrameHeight) {
  7274. options.minFrameHeight = options.initialFrameHeight;
  7275. } else {
  7276. options.initialFrameHeight = options.minFrameHeight =
  7277. container.offsetHeight;
  7278. }
  7279. container.style.width = /%$/.test(options.initialFrameWidth)
  7280. ? "100%"
  7281. : options.initialFrameWidth -
  7282. getStyleValue("padding-left") -
  7283. getStyleValue("padding-right") +
  7284. "px";
  7285. container.style.height = /%$/.test(options.initialFrameHeight)
  7286. ? "100%"
  7287. : options.initialFrameHeight -
  7288. getStyleValue("padding-top") -
  7289. getStyleValue("padding-bottom") +
  7290. "px";
  7291. container.style.zIndex = options.zIndex;
  7292. var additionCssHtml = [];
  7293. for(var i in options.iframeCssUrlsAddition){
  7294. additionCssHtml.push("<link rel='stylesheet' type='text/css' href='" + utils.unhtml(options.iframeCssUrlsAddition[i]) + "'/>")
  7295. }
  7296. var html =
  7297. (ie && browser.version < 9 ? "" : "<!DOCTYPE html>") +
  7298. "<html xmlns='http://www.w3.org/1999/xhtml' class='view' >" +
  7299. "<head>" +
  7300. "<style type='text/css'>" +
  7301. //设置四周的留边
  7302. ".view{padding:0;word-wrap:break-word;cursor:text;height:90%;}\n" +
  7303. //设置默认字体和字号
  7304. //font-family不能呢随便改,在safari下fillchar会有解析问题
  7305. "body{margin:8px;font-family:sans-serif;font-size:16px;}" +
  7306. //设置段落间距
  7307. "p{margin:5px 0;}</style>" +
  7308. (options.iframeCssUrl
  7309. ? "<link rel='stylesheet' type='text/css' href='" +
  7310. utils.unhtml(options.iframeCssUrl) +
  7311. "'/>"
  7312. : "") +
  7313. (options.initialStyle
  7314. ? "<style>" + options.initialStyle + "</style>"
  7315. : "") +
  7316. additionCssHtml.join("") +
  7317. "</head>" +
  7318. "<body class='view' ></body>" +
  7319. "<script type='text/javascript' " +
  7320. (ie ? "defer='defer'" : "") +
  7321. " id='_initialScript'>" +
  7322. "setTimeout(function(){editor = window.parent.UE.instants['ueditorInstant" +
  7323. me.uid +
  7324. "'];editor._setup(document);},0);" +
  7325. "var _tmpScript = document.getElementById('_initialScript');_tmpScript.parentNode.removeChild(_tmpScript);" +
  7326. "</script>" +
  7327. (options.iframeJsUrl
  7328. ? "<script type='text/javascript' src='" +
  7329. utils.unhtml(options.iframeJsUrl) +
  7330. "'></script>"
  7331. : "") +
  7332. "</html>";
  7333. container.appendChild(
  7334. domUtils.createElement(document, "iframe", {
  7335. id: "ueditor_" + me.uid,
  7336. width: "100%",
  7337. height: "100%",
  7338. frameborder: "0",
  7339. //先注释掉了,加的原因忘记了,但开启会直接导致全屏模式下内容多时不会出现滚动条
  7340. // scrolling :'no',
  7341. src:
  7342. "javascript:void(function(){document.open();" +
  7343. (options.customDomain && document.domain != location.hostname
  7344. ? 'document.domain="' + document.domain + '";'
  7345. : "") +
  7346. 'document.write("' +
  7347. html +
  7348. '");document.close();}())'
  7349. })
  7350. );
  7351. container.style.overflow = "hidden";
  7352. //解决如果是给定的百分比,会导致高度算不对的问题
  7353. setTimeout(function() {
  7354. if (/%$/.test(options.initialFrameWidth)) {
  7355. options.minFrameWidth = options.initialFrameWidth =
  7356. container.offsetWidth;
  7357. //如果这里给定宽度,会导致ie在拖动窗口大小时,编辑区域不随着变化
  7358. // container.style.width = options.initialFrameWidth + 'px';
  7359. }
  7360. if (/%$/.test(options.initialFrameHeight)) {
  7361. options.minFrameHeight = options.initialFrameHeight =
  7362. container.offsetHeight;
  7363. container.style.height = options.initialFrameHeight + "px";
  7364. }
  7365. });
  7366. }
  7367. },
  7368. /**
  7369. * 编辑器初始化
  7370. * @method _setup
  7371. * @private
  7372. * @param { Element } doc 编辑器Iframe中的文档对象
  7373. */
  7374. _setup: function(doc) {
  7375. var me = this,
  7376. options = me.options;
  7377. if (ie) {
  7378. doc.body.disabled = true;
  7379. doc.body.contentEditable = true;
  7380. doc.body.disabled = false;
  7381. } else {
  7382. doc.body.contentEditable = true;
  7383. }
  7384. doc.body.spellcheck = false;
  7385. me.document = doc;
  7386. me.window = doc.defaultView || doc.parentWindow;
  7387. me.iframe = me.window.frameElement;
  7388. me.body = doc.body;
  7389. me.selection = new dom.Selection(doc);
  7390. //gecko初始化就能得到range,无法判断isFocus了
  7391. var geckoSel;
  7392. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7393. geckoSel.removeAllRanges();
  7394. }
  7395. this._initEvents();
  7396. //为form提交提供一个隐藏的textarea
  7397. for (
  7398. var form = this.iframe.parentNode;
  7399. !domUtils.isBody(form);
  7400. form = form.parentNode
  7401. ) {
  7402. if (form.tagName === "FORM") {
  7403. me.form = form;
  7404. if (me.options.autoSyncData) {
  7405. domUtils.on(me.window, "blur", function() {
  7406. setValue(form, me);
  7407. });
  7408. domUtils.on(form, "submit", function() {
  7409. me.fireEvent("beforesubmit");
  7410. });
  7411. } else {
  7412. domUtils.on(form, "submit", function() {
  7413. setValue(this, me);
  7414. me.fireEvent("beforesubmit");
  7415. });
  7416. }
  7417. break;
  7418. }
  7419. }
  7420. if (options.initialContent) {
  7421. if (options.autoClearinitialContent) {
  7422. var oldExecCommand = me.execCommand;
  7423. me.execCommand = function() {
  7424. me.fireEvent("firstBeforeExecCommand");
  7425. return oldExecCommand.apply(me, arguments);
  7426. };
  7427. this._setDefaultContent(options.initialContent);
  7428. } else this.setContent(options.initialContent, false, true);
  7429. }
  7430. //编辑器不能为空内容
  7431. if (domUtils.isEmptyNode(me.body)) {
  7432. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  7433. }
  7434. //如果要求focus, 就把光标定位到内容开始
  7435. if (options.focus) {
  7436. setTimeout(function() {
  7437. me.focus(me.options.focusInEnd);
  7438. //如果自动清除开着,就不需要做selectionchange;
  7439. !me.options.autoClearinitialContent && me._selectionChange();
  7440. }, 0);
  7441. }
  7442. if (!me.container) {
  7443. me.container = this.iframe.parentNode;
  7444. }
  7445. if (options.fullscreen && me.ui) {
  7446. me.ui.setFullScreen(true);
  7447. }
  7448. try {
  7449. me.document.execCommand("2D-position", false, false);
  7450. } catch (e) {}
  7451. try {
  7452. me.document.execCommand("enableInlineTableEditing", false, false);
  7453. } catch (e) {}
  7454. try {
  7455. me.document.execCommand("enableObjectResizing", false, false);
  7456. } catch (e) {}
  7457. //挂接快捷键
  7458. me._bindshortcutKeys();
  7459. me.isReady = 1;
  7460. me.fireEvent("ready");
  7461. options.onready && options.onready.call(me);
  7462. if (!browser.ie9below) {
  7463. domUtils.on(me.window, ["blur", "focus"], function(e) {
  7464. //chrome下会出现alt+tab切换时,导致选区位置不对
  7465. if (e.type == "blur") {
  7466. me._bakRange = me.selection.getRange();
  7467. try {
  7468. me._bakNativeRange = me.selection.getNative().getRangeAt(0);
  7469. me.selection.getNative().removeAllRanges();
  7470. } catch (e) {
  7471. me._bakNativeRange = null;
  7472. }
  7473. } else {
  7474. try {
  7475. me._bakRange && me._bakRange.select();
  7476. } catch (e) {}
  7477. }
  7478. });
  7479. }
  7480. //trace:1518 ff3.6body不够寛,会导致点击空白处无法获得焦点
  7481. if (browser.gecko && browser.version <= 10902) {
  7482. //修复ff3.6初始化进来,不能点击获得焦点
  7483. me.body.contentEditable = false;
  7484. setTimeout(function() {
  7485. me.body.contentEditable = true;
  7486. }, 100);
  7487. setInterval(function() {
  7488. me.body.style.height = me.iframe.offsetHeight - 20 + "px";
  7489. }, 100);
  7490. }
  7491. !options.isShow && me.setHide();
  7492. options.readonly && me.setDisabled();
  7493. },
  7494. /**
  7495. * 同步数据到编辑器所在的form
  7496. * 从编辑器的容器节点向上查找form元素,若找到,就同步编辑内容到找到的form里,为提交数据做准备,主要用于是手动提交的情况
  7497. * 后台取得数据的键值,使用你容器上的name属性,如果没有就使用参数里的textarea项
  7498. * @method sync
  7499. * @example
  7500. * ```javascript
  7501. * editor.sync();
  7502. * form.sumbit(); //form变量已经指向了form元素
  7503. * ```
  7504. */
  7505. /**
  7506. * 根据传入的formId,在页面上查找要同步数据的表单,若找到,就同步编辑内容到找到的form里,为提交数据做准备
  7507. * 后台取得数据的键值,该键值默认使用给定的编辑器容器的name属性,如果没有name属性则使用参数项里给定的“textarea”项
  7508. * @method sync
  7509. * @param { String } formID 指定一个要同步数据的form的id,编辑器的数据会同步到你指定form下
  7510. */
  7511. sync: function(formId) {
  7512. var me = this,
  7513. form = formId
  7514. ? document.getElementById(formId)
  7515. : domUtils.findParent(
  7516. me.iframe.parentNode,
  7517. function(node) {
  7518. return node.tagName === "FORM";
  7519. },
  7520. true
  7521. );
  7522. form && setValue(form, me);
  7523. },
  7524. /**
  7525. * 手动触发更新按钮栏状态
  7526. */
  7527. syncCommandState: function(){
  7528. this.fireEvent("selectionchange");
  7529. },
  7530. /**
  7531. * 设置编辑器宽度
  7532. * @param width
  7533. */
  7534. setWidth: function(width){
  7535. if (width !== parseInt(this.iframe.parentNode.parentNode.style.width)) {
  7536. this.iframe.parentNode.parentNode.style.width = width + "px";
  7537. }
  7538. },
  7539. /**
  7540. * 设置编辑器高度
  7541. * @method setHeight
  7542. * @remind 当配置项autoHeightEnabled为真时,该方法无效
  7543. * @param { Number } number 设置的高度值,纯数值,不带单位
  7544. * @example
  7545. * ```javascript
  7546. * editor.setHeight(number);
  7547. * ```
  7548. */
  7549. setHeight: function(height, notSetHeight) {
  7550. if (height !== parseInt(this.iframe.parentNode.style.height)) {
  7551. this.iframe.parentNode.style.height = height + "px";
  7552. }
  7553. !notSetHeight &&
  7554. (this.options.minFrameHeight = this.options.initialFrameHeight = height);
  7555. this.body.style.height = height + "px";
  7556. !notSetHeight && this.trigger("setHeight");
  7557. },
  7558. /**
  7559. * 为编辑器的编辑命令提供快捷键
  7560. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7561. * @method addshortcutkey
  7562. * @param { Object } keyset 命令名和快捷键键值对对象,多个按钮的快捷键用“+”分隔
  7563. * @example
  7564. * ```javascript
  7565. * editor.addshortcutkey({
  7566. * "Bold" : "ctrl+66",//^B
  7567. * "Italic" : "ctrl+73", //^I
  7568. * });
  7569. * ```
  7570. */
  7571. /**
  7572. * 这个接口是为插件扩展提供的接口,主要是为新添加的插件,如果需要添加快捷键,所提供的接口
  7573. * @method addshortcutkey
  7574. * @param { String } cmd 触发快捷键时,响应的命令
  7575. * @param { String } keys 快捷键的字符串,多个按钮用“+”分隔
  7576. * @example
  7577. * ```javascript
  7578. * editor.addshortcutkey("Underline", "ctrl+85"); //^U
  7579. * ```
  7580. */
  7581. addshortcutkey: function(cmd, keys) {
  7582. var obj = {};
  7583. if (keys) {
  7584. obj[cmd] = keys;
  7585. } else {
  7586. obj = cmd;
  7587. }
  7588. utils.extend(this.shortcutkeys, obj);
  7589. },
  7590. /**
  7591. * 对编辑器设置keydown事件监听,绑定快捷键和命令,当快捷键组合触发成功,会响应对应的命令
  7592. * @method _bindshortcutKeys
  7593. * @private
  7594. */
  7595. _bindshortcutKeys: function() {
  7596. var me = this,
  7597. shortcutkeys = this.shortcutkeys;
  7598. me.addListener("keydown", function(type, e) {
  7599. var keyCode = e.keyCode || e.which;
  7600. for (var i in shortcutkeys) {
  7601. var tmp = shortcutkeys[i].split(",");
  7602. for (var t = 0, ti; (ti = tmp[t++]); ) {
  7603. ti = ti.split(":");
  7604. var key = ti[0],
  7605. param = ti[1];
  7606. if (
  7607. /^(ctrl)(\+shift)?\+(\d+)$/.test(key.toLowerCase()) ||
  7608. /^(\d+)$/.test(key)
  7609. ) {
  7610. if (
  7611. ((RegExp.$1 == "ctrl" ? e.ctrlKey || e.metaKey : 0) &&
  7612. (RegExp.$2 != "" ? e[RegExp.$2.slice(1) + "Key"] : 1) &&
  7613. keyCode == RegExp.$3) ||
  7614. keyCode == RegExp.$1
  7615. ) {
  7616. if (me.queryCommandState(i, param) != -1)
  7617. me.execCommand(i, param);
  7618. domUtils.preventDefault(e);
  7619. }
  7620. }
  7621. }
  7622. }
  7623. });
  7624. },
  7625. /**
  7626. * 获取编辑器的内容
  7627. * @method getContent
  7628. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7629. * @return { String } 编辑器的内容字符串, 如果编辑器的内容为空,或者是空的标签内容(如:”&lt;p&gt;&lt;br/&gt;&lt;/p&gt;“), 则返回空字符串
  7630. * @example
  7631. * ```javascript
  7632. * //编辑器html内容:<p>1<strong>2<em>34</em>5</strong>6</p>
  7633. * var content = editor.getContent(); //返回值:<p>1<strong>2<em>34</em>5</strong>6</p>
  7634. * ```
  7635. */
  7636. /**
  7637. * 获取编辑器的内容。 可以通过参数定义编辑器内置的判空规则
  7638. * @method getContent
  7639. * @param { Function } fn 自定的判空规则, 要求该方法返回一个boolean类型的值,
  7640. * 代表当前编辑器的内容是否空,
  7641. * 如果返回true, 则该方法将直接返回空字符串;如果返回false,则编辑器将返回
  7642. * 经过内置过滤规则处理后的内容。
  7643. * @remind 该方法在处理包含有初始化内容的时候能起到很好的作用。
  7644. * @warning 该方法获取到的是经过编辑器内置的过滤规则进行过滤后得到的内容
  7645. * @return { String } 编辑器的内容字符串
  7646. * @example
  7647. * ```javascript
  7648. * // editor 是一个编辑器的实例
  7649. * var content = editor.getContent( function ( editor ) {
  7650. * return editor.body.innerHTML === '欢迎使用UEditor'; //返回空字符串
  7651. * } );
  7652. * ```
  7653. */
  7654. getContent: function(cmd, fn, notSetCursor, ignoreBlank, formatter) {
  7655. var me = this;
  7656. if (cmd && utils.isFunction(cmd)) {
  7657. fn = cmd;
  7658. cmd = "";
  7659. }
  7660. if (fn ? !fn() : !this.hasContents()) {
  7661. return "";
  7662. }
  7663. me.fireEvent("beforegetcontent");
  7664. var root = UE.htmlparser(me.body.innerHTML, ignoreBlank);
  7665. me.filterOutputRule(root);
  7666. me.fireEvent("aftergetcontent", cmd, root);
  7667. return root.toHtml(formatter);
  7668. },
  7669. /**
  7670. * 取得完整的html代码,可以直接显示成完整的html文档
  7671. * @method getAllHtml
  7672. * @return { String } 编辑器的内容html文档字符串
  7673. * @eaxmple
  7674. * ```javascript
  7675. * editor.getAllHtml(); //返回格式大致是: <html><head>...</head><body>...</body></html>
  7676. * ```
  7677. */
  7678. getAllHtml: function() {
  7679. var me = this,
  7680. headHtml = [],
  7681. html = "";
  7682. me.fireEvent("getAllHtml", headHtml);
  7683. if (browser.ie && browser.version > 8) {
  7684. var headHtmlForIE9 = "";
  7685. utils.each(me.document.styleSheets, function(si) {
  7686. headHtmlForIE9 += si.href
  7687. ? '<link rel="stylesheet" type="text/css" href="' + si.href + '" />'
  7688. : "<style>" + si.cssText + "</style>";
  7689. });
  7690. utils.each(me.document.getElementsByTagName("script"), function(si) {
  7691. headHtmlForIE9 += si.outerHTML;
  7692. });
  7693. }
  7694. return (
  7695. "<html><head>" +
  7696. (me.options.charset
  7697. ? '<meta http-equiv="Content-Type" content="text/html; charset=' +
  7698. me.options.charset +
  7699. '"/>'
  7700. : "") +
  7701. (headHtmlForIE9 ||
  7702. me.document.getElementsByTagName("head")[0].innerHTML) +
  7703. headHtml.join("\n") +
  7704. "</head>" +
  7705. "<body " +
  7706. (ie && browser.version < 9 ? 'class="view"' : "") +
  7707. ">" +
  7708. me.getContent(null, null, true) +
  7709. "</body></html>"
  7710. );
  7711. },
  7712. /**
  7713. * 得到编辑器的纯文本内容,但会保留段落格式
  7714. * @method getPlainTxt
  7715. * @return { String } 编辑器带段落格式的纯文本内容字符串
  7716. * @example
  7717. * ```javascript
  7718. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7719. * console.log(editor.getPlainTxt()); //输出:"1\n2\n
  7720. * ```
  7721. */
  7722. getPlainTxt: function() {
  7723. var reg = new RegExp(domUtils.fillChar, "g"),
  7724. html = this.body.innerHTML.replace(/[\n\r]/g, ""); //ie要先去了\n在处理
  7725. html = html
  7726. .replace(/<(p|div)[^>]*>(<br\/?>|&nbsp;)<\/\1>/gi, "\n")
  7727. .replace(/<br\/?>/gi, "\n")
  7728. .replace(/<[^>/]+>/g, "")
  7729. .replace(/(\n)?<\/([^>]+)>/g, function(a, b, c) {
  7730. return dtd.$block[c] ? "\n" : b ? b : "";
  7731. });
  7732. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7733. return html
  7734. .replace(reg, "")
  7735. .replace(/\u00a0/g, " ")
  7736. .replace(/&nbsp;/g, " ");
  7737. },
  7738. /**
  7739. * 获取编辑器中的纯文本内容,没有段落格式
  7740. * @method getContentTxt
  7741. * @return { String } 编辑器不带段落格式的纯文本内容字符串
  7742. * @example
  7743. * ```javascript
  7744. * //编辑器html内容:<p><strong>1</strong></p><p><strong>2</strong></p>
  7745. * console.log(editor.getPlainTxt()); //输出:"12
  7746. * ```
  7747. */
  7748. getContentTxt: function() {
  7749. var reg = new RegExp(domUtils.fillChar, "g");
  7750. //取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0
  7751. return this.body[browser.ie ? "innerText" : "textContent"]
  7752. .replace(reg, "")
  7753. .replace(/\u00a0/g, " ");
  7754. },
  7755. /**
  7756. * 设置编辑器的内容,可修改编辑器当前的html内容
  7757. * @method setContent
  7758. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7759. * @warning 该方法会触发selectionchange事件
  7760. * @param { String } html 要插入的html内容
  7761. * @example
  7762. * ```javascript
  7763. * editor.getContent('<p>test</p>');
  7764. * ```
  7765. */
  7766. /**
  7767. * 设置编辑器的内容,可修改编辑器当前的html内容
  7768. * @method setContent
  7769. * @warning 通过该方法插入的内容,是经过编辑器内置的过滤规则进行过滤后得到的内容
  7770. * @warning 该方法会触发selectionchange事件
  7771. * @param { String } html 要插入的html内容
  7772. * @param { Boolean } isAppendTo 若传入true,不清空原来的内容,在最后插入内容,否则,清空内容再插入
  7773. * @example
  7774. * ```javascript
  7775. * //假设设置前的编辑器内容是 <p>old text</p>
  7776. * editor.setContent('<p>new text</p>', true); //插入的结果是<p>old text</p><p>new text</p>
  7777. * ```
  7778. */
  7779. setContent: function(html, isAppendTo, notFireSelectionchange) {
  7780. var me = this;
  7781. me.fireEvent("beforesetcontent", html);
  7782. var root = UE.htmlparser(html);
  7783. me.filterInputRule(root);
  7784. html = root.toHtml();
  7785. me.body.innerHTML = (isAppendTo ? me.body.innerHTML : "") + html;
  7786. function isCdataDiv(node) {
  7787. return node.tagName == "DIV" && node.getAttribute("cdata_tag");
  7788. }
  7789. //给文本或者inline节点套p标签
  7790. if (me.options.enterTag == "p") {
  7791. var child = this.body.firstChild,
  7792. tmpNode;
  7793. if (
  7794. !child ||
  7795. (child.nodeType == 1 &&
  7796. (dtd.$cdata[child.tagName] ||
  7797. isCdataDiv(child) ||
  7798. domUtils.isCustomeNode(child)) &&
  7799. child === this.body.lastChild)
  7800. ) {
  7801. this.body.innerHTML =
  7802. "<p>" +
  7803. (browser.ie ? "&nbsp;" : "<br/>") +
  7804. "</p>" +
  7805. this.body.innerHTML;
  7806. } else {
  7807. var p = me.document.createElement("p");
  7808. while (child) {
  7809. while (
  7810. child &&
  7811. (child.nodeType == 3 ||
  7812. (child.nodeType == 1 &&
  7813. dtd.p[child.tagName] &&
  7814. !dtd.$cdata[child.tagName]))
  7815. ) {
  7816. tmpNode = child.nextSibling;
  7817. p.appendChild(child);
  7818. child = tmpNode;
  7819. }
  7820. if (p.firstChild) {
  7821. if (!child) {
  7822. me.body.appendChild(p);
  7823. break;
  7824. } else {
  7825. child.parentNode.insertBefore(p, child);
  7826. p = me.document.createElement("p");
  7827. }
  7828. }
  7829. child = child.nextSibling;
  7830. }
  7831. }
  7832. }
  7833. me.fireEvent("aftersetcontent");
  7834. me.fireEvent("contentchange");
  7835. !notFireSelectionchange && me._selectionChange();
  7836. //清除保存的选区
  7837. me._bakRange = me._bakIERange = me._bakNativeRange = null;
  7838. //trace:1742 setContent后gecko能得到焦点问题
  7839. var geckoSel;
  7840. if (browser.gecko && (geckoSel = this.selection.getNative())) {
  7841. geckoSel.removeAllRanges();
  7842. }
  7843. if (me.options.autoSyncData) {
  7844. me.form && setValue(me.form, me);
  7845. }
  7846. },
  7847. /**
  7848. * 让编辑器获得焦点,默认focus到编辑器头部
  7849. * @method focus
  7850. * @example
  7851. * ```javascript
  7852. * editor.focus()
  7853. * ```
  7854. */
  7855. /**
  7856. * 让编辑器获得焦点,toEnd确定focus位置
  7857. * @method focus
  7858. * @param { Boolean } toEnd 默认focus到编辑器头部,toEnd为true时focus到内容尾部
  7859. * @example
  7860. * ```javascript
  7861. * editor.focus(true)
  7862. * ```
  7863. */
  7864. focus: function(toEnd) {
  7865. try {
  7866. var me = this,
  7867. rng = me.selection.getRange();
  7868. if (toEnd) {
  7869. var node = me.body.lastChild;
  7870. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7871. if (domUtils.isEmptyBlock(node)) {
  7872. rng.setStartAtFirst(node);
  7873. } else {
  7874. rng.setStartAtLast(node);
  7875. }
  7876. rng.collapse(true);
  7877. }
  7878. rng.setCursor(true);
  7879. } else {
  7880. if (
  7881. !rng.collapsed &&
  7882. domUtils.isBody(rng.startContainer) &&
  7883. rng.startOffset == 0
  7884. ) {
  7885. var node = me.body.firstChild;
  7886. if (node && node.nodeType == 1 && !dtd.$empty[node.tagName]) {
  7887. rng.setStartAtFirst(node).collapse(true);
  7888. }
  7889. }
  7890. rng.select(true);
  7891. }
  7892. this.fireEvent("focus selectionchange");
  7893. } catch (e) {}
  7894. },
  7895. isFocus: function() {
  7896. return this.selection.isFocus();
  7897. },
  7898. blur: function() {
  7899. var sel = this.selection.getNative();
  7900. if (sel.empty && browser.ie) {
  7901. var nativeRng = document.body.createTextRange();
  7902. nativeRng.moveToElementText(document.body);
  7903. nativeRng.collapse(true);
  7904. nativeRng.select();
  7905. sel.empty();
  7906. } else {
  7907. sel.removeAllRanges();
  7908. }
  7909. //this.fireEvent('blur selectionchange');
  7910. },
  7911. /**
  7912. * 初始化UE事件及部分事件代理
  7913. * @method _initEvents
  7914. * @private
  7915. */
  7916. _initEvents: function() {
  7917. var me = this,
  7918. doc = me.document,
  7919. win = me.window;
  7920. me._proxyDomEvent = utils.bind(me._proxyDomEvent, me);
  7921. domUtils.on(
  7922. doc,
  7923. [
  7924. "click",
  7925. "contextmenu",
  7926. "mousedown",
  7927. "keydown",
  7928. "keyup",
  7929. "keypress",
  7930. "mouseup",
  7931. "mouseover",
  7932. "mouseout",
  7933. "selectstart"
  7934. ],
  7935. me._proxyDomEvent
  7936. );
  7937. domUtils.on(win, ["focus", "blur"], me._proxyDomEvent);
  7938. domUtils.on(me.body, "drop", function(e) {
  7939. //阻止ff下默认的弹出新页面打开图片
  7940. if (browser.gecko && e.stopPropagation) {
  7941. e.stopPropagation();
  7942. }
  7943. me.fireEvent("contentchange");
  7944. });
  7945. // 当内容最末尾为非字符时,比较难以在最后插入字符,所以在点击时,自动添加一个空的p标签
  7946. domUtils.on(me.body, "click", function(e) {
  7947. try {
  7948. var node = me.body.lastChild;
  7949. if (!node) {
  7950. return;
  7951. }
  7952. var rect = node.getBoundingClientRect();
  7953. if (e.clientY > rect.top + rect.height) {
  7954. var p = document.createElement('p');
  7955. p.innerHTML = '<br />';
  7956. me.body.appendChild(p);
  7957. setTimeout(function () {
  7958. me.focus(true);
  7959. }, 100);
  7960. }
  7961. } catch (e) {
  7962. console.error('auto insert p at end', e);
  7963. }
  7964. });
  7965. domUtils.on(doc, ["mouseup", "keydown"], function(evt) {
  7966. //特殊键不触发selectionchange
  7967. if (
  7968. evt.type === "keydown" &&
  7969. (evt.ctrlKey || evt.metaKey || evt.shiftKey || evt.altKey)
  7970. ) {
  7971. return;
  7972. }
  7973. if (evt.button === 2) return;
  7974. me._selectionChange(250, evt);
  7975. });
  7976. },
  7977. /**
  7978. * 触发事件代理
  7979. * @method _proxyDomEvent
  7980. * @private
  7981. * @return { * } fireEvent的返回值
  7982. * @see UE.EventBase:fireEvent(String)
  7983. */
  7984. _proxyDomEvent: function(evt) {
  7985. if (
  7986. this.fireEvent("before" + evt.type.replace(/^on/, "").toLowerCase()) ===
  7987. false
  7988. ) {
  7989. return false;
  7990. }
  7991. if (this.fireEvent(evt.type.replace(/^on/, ""), evt) === false) {
  7992. return false;
  7993. }
  7994. return this.fireEvent(
  7995. "after" + evt.type.replace(/^on/, "").toLowerCase()
  7996. );
  7997. },
  7998. /**
  7999. * 变化选区
  8000. * @method _selectionChange
  8001. * @private
  8002. */
  8003. _selectionChange: function(delay, evt) {
  8004. var me = this;
  8005. //有光标才做selectionchange 为了解决未focus时点击source不能触发更改工具栏状态的问题(source命令notNeedUndo=1)
  8006. // if ( !me.selection.isFocus() ){
  8007. // return;
  8008. // }
  8009. var hackForMouseUp = false;
  8010. var mouseX, mouseY;
  8011. if (browser.ie && browser.version < 9 && evt && evt.type == "mouseup") {
  8012. var range = this.selection.getRange();
  8013. if (!range.collapsed) {
  8014. hackForMouseUp = true;
  8015. mouseX = evt.clientX;
  8016. mouseY = evt.clientY;
  8017. }
  8018. }
  8019. clearTimeout(_selectionChangeTimer);
  8020. _selectionChangeTimer = setTimeout(function() {
  8021. if (!me.selection || !me.selection.getNative()) {
  8022. return;
  8023. }
  8024. //修复一个IE下的bug: 鼠标点击一段已选择的文本中间时,可能在mouseup后的一段时间内取到的range是在selection的type为None下的错误值.
  8025. //IE下如果用户是拖拽一段已选择文本,则不会触发mouseup事件,所以这里的特殊处理不会对其有影响
  8026. var ieRange;
  8027. if (hackForMouseUp && me.selection.getNative().type == "None") {
  8028. ieRange = me.document.body.createTextRange();
  8029. try {
  8030. ieRange.moveToPoint(mouseX, mouseY);
  8031. } catch (ex) {
  8032. ieRange = null;
  8033. }
  8034. }
  8035. var bakGetIERange;
  8036. if (ieRange) {
  8037. bakGetIERange = me.selection.getIERange;
  8038. me.selection.getIERange = function() {
  8039. return ieRange;
  8040. };
  8041. }
  8042. me.selection.cache();
  8043. if (bakGetIERange) {
  8044. me.selection.getIERange = bakGetIERange;
  8045. }
  8046. if (me.selection._cachedRange && me.selection._cachedStartElement) {
  8047. me.fireEvent("beforeselectionchange");
  8048. // 第二个参数causeByUi为true代表由用户交互造成的selectionchange.
  8049. me.fireEvent("selectionchange", !!evt);
  8050. me.fireEvent("afterselectionchange");
  8051. me.selection.clear();
  8052. }
  8053. }, delay || 50);
  8054. },
  8055. /**
  8056. * 执行编辑命令
  8057. * @method _callCmdFn
  8058. * @private
  8059. * @param { String } fnName 函数名称
  8060. * @param { * } args 传给命令函数的参数
  8061. * @return { * } 返回命令函数运行的返回值
  8062. */
  8063. _callCmdFn: function(fnName, args) {
  8064. var cmdName = args[0].toLowerCase(),
  8065. cmd,
  8066. cmdFn;
  8067. cmd = this.commands[cmdName] || UE.commands[cmdName];
  8068. cmdFn = cmd && cmd[fnName];
  8069. //没有querycommandstate或者没有command的都默认返回0
  8070. if ((!cmd || !cmdFn) && fnName == "queryCommandState") {
  8071. return 0;
  8072. } else if (cmdFn) {
  8073. return cmdFn.apply(this, args);
  8074. }
  8075. },
  8076. /**
  8077. * 执行编辑命令cmdName,完成富文本编辑效果
  8078. * @method execCommand
  8079. * @param { String } cmdName 需要执行的命令
  8080. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8081. * @return { * } 返回命令函数运行的返回值
  8082. * @example
  8083. * ```javascript
  8084. * editor.execCommand(cmdName);
  8085. * ```
  8086. */
  8087. execCommand: function(cmdName) {
  8088. cmdName = cmdName.toLowerCase();
  8089. var me = this,
  8090. result,
  8091. cmd = me.commands[cmdName] || UE.commands[cmdName];
  8092. if (!cmd || !cmd.execCommand) {
  8093. return null;
  8094. }
  8095. if (!cmd.notNeedUndo && !me.__hasEnterExecCommand) {
  8096. me.__hasEnterExecCommand = true;
  8097. if (me.queryCommandState.apply(me, arguments) != -1) {
  8098. me.fireEvent("saveScene");
  8099. me.fireEvent.apply(
  8100. me,
  8101. ["beforeexeccommand", cmdName].concat(arguments)
  8102. );
  8103. result = this._callCmdFn("execCommand", arguments);
  8104. //保存场景时,做了内容对比,再看是否进行contentchange触发,这里多触发了一次,去掉
  8105. // (!cmd.ignoreContentChange && !me._ignoreContentChange) && me.fireEvent('contentchange');
  8106. me.fireEvent.apply(
  8107. me,
  8108. ["afterexeccommand", cmdName].concat(arguments)
  8109. );
  8110. me.fireEvent("saveScene");
  8111. }
  8112. me.__hasEnterExecCommand = false;
  8113. } else {
  8114. result = this._callCmdFn("execCommand", arguments);
  8115. !me.__hasEnterExecCommand &&
  8116. !cmd.ignoreContentChange &&
  8117. !me._ignoreContentChange &&
  8118. me.fireEvent("contentchange");
  8119. }
  8120. !me.__hasEnterExecCommand &&
  8121. !cmd.ignoreContentChange &&
  8122. !me._ignoreContentChange &&
  8123. me._selectionChange();
  8124. return result;
  8125. },
  8126. /**
  8127. * 根据传入的command命令,查选编辑器当前的选区,返回命令的状态
  8128. * @method queryCommandState
  8129. * @param { String } cmdName 需要查询的命令名称
  8130. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8131. * @return { Number } number 返回放前命令的状态,返回值三种情况:(-1|0|1)
  8132. * @example
  8133. * ```javascript
  8134. * editor.queryCommandState(cmdName) => (-1|0|1)
  8135. * ```
  8136. * @see COMMAND.LIST
  8137. */
  8138. queryCommandState: function(cmdName) {
  8139. return this._callCmdFn("queryCommandState", arguments);
  8140. },
  8141. /**
  8142. * 根据传入的command命令,查选编辑器当前的选区,根据命令返回相关的值
  8143. * @method queryCommandValue
  8144. * @param { String } cmdName 需要查询的命令名称
  8145. * @remind 具体命令的使用请参考<a href="#COMMAND.LIST">命令列表</a>
  8146. * @remind 只有部分插件有此方法
  8147. * @return { * } 返回每个命令特定的当前状态值
  8148. * @grammar editor.queryCommandValue(cmdName) => {*}
  8149. * @see COMMAND.LIST
  8150. */
  8151. queryCommandValue: function(cmdName) {
  8152. return this._callCmdFn("queryCommandValue", arguments);
  8153. },
  8154. /**
  8155. * 检查编辑区域中是否有内容
  8156. * @method hasContents
  8157. * @remind 默认有文本内容,或者有以下节点都不认为是空
  8158. * table,ul,ol,dl,iframe,area,base,col,hr,img,embed,input,link,meta,param
  8159. * @return { Boolean } 检查有内容返回true,否则返回false
  8160. * @example
  8161. * ```javascript
  8162. * editor.hasContents()
  8163. * ```
  8164. */
  8165. /**
  8166. * 检查编辑区域中是否有内容,若包含参数tags中的节点类型,直接返回true
  8167. * @method hasContents
  8168. * @param { Array } tags 传入数组判断时用到的节点类型
  8169. * @return { Boolean } 若文档中包含tags数组里对应的tag,返回true,否则返回false
  8170. * @example
  8171. * ```javascript
  8172. * editor.hasContents(['span']);
  8173. * ```
  8174. */
  8175. hasContents: function(tags) {
  8176. if (tags) {
  8177. for (var i = 0, ci; (ci = tags[i++]); ) {
  8178. if (this.document.getElementsByTagName(ci).length > 0) {
  8179. return true;
  8180. }
  8181. }
  8182. }
  8183. if (!domUtils.isEmptyBlock(this.body)) {
  8184. return true;
  8185. }
  8186. // 随时添加,定义的特殊标签如果存在,不能认为是空
  8187. tags = ["div"];
  8188. for (i = 0; (ci = tags[i++]); ) {
  8189. var nodes = domUtils.getElementsByTagName(this.document, ci);
  8190. for (var n = 0, cn; (cn = nodes[n++]); ) {
  8191. if (domUtils.isCustomeNode(cn)) {
  8192. return true;
  8193. }
  8194. }
  8195. }
  8196. // 部分如媒体标签,不能认为为空
  8197. tags = [ "video","iframe" ]
  8198. for (i = 0; (ci = tags[i++]); ) {
  8199. var nodes = domUtils.getElementsByTagName(this.document, ci);
  8200. for (var n = 0, cn; (cn = nodes[n++]); ) {
  8201. return true;
  8202. }
  8203. }
  8204. return false;
  8205. },
  8206. /**
  8207. * 重置编辑器,可用来做多个tab使用同一个编辑器实例
  8208. * @method reset
  8209. * @remind 此方法会清空编辑器内容,清空回退列表,会触发reset事件
  8210. * @example
  8211. * ```javascript
  8212. * editor.reset()
  8213. * ```
  8214. */
  8215. reset: function() {
  8216. this.fireEvent("reset");
  8217. },
  8218. /**
  8219. * 设置当前编辑区域可以编辑
  8220. * @method setEnabled
  8221. * @example
  8222. * ```javascript
  8223. * editor.setEnabled()
  8224. * ```
  8225. */
  8226. setEnabled: function() {
  8227. var me = this,
  8228. range;
  8229. if (me.body.contentEditable === "false") {
  8230. me.body.contentEditable = true;
  8231. range = me.selection.getRange();
  8232. //有可能内容丢失了
  8233. try {
  8234. range.moveToBookmark(me.lastBk);
  8235. delete me.lastBk;
  8236. } catch (e) {
  8237. range.setStartAtFirst(me.body).collapse(true);
  8238. }
  8239. range.select(true);
  8240. if (me.bkqueryCommandState) {
  8241. me.queryCommandState = me.bkqueryCommandState;
  8242. delete me.bkqueryCommandState;
  8243. }
  8244. if (me.bkqueryCommandValue) {
  8245. me.queryCommandValue = me.bkqueryCommandValue;
  8246. delete me.bkqueryCommandValue;
  8247. }
  8248. me.fireEvent("selectionchange");
  8249. }
  8250. },
  8251. enable: function() {
  8252. return this.setEnabled();
  8253. },
  8254. /** 设置当前编辑区域不可编辑
  8255. * @method setDisabled
  8256. */
  8257. /** 设置当前编辑区域不可编辑,except中的命令除外
  8258. * @method setDisabled
  8259. * @param { String } except 例外命令的字符串
  8260. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8261. * @example
  8262. * ```javascript
  8263. * editor.setDisabled('bold'); //禁用工具栏中除加粗之外的所有功能
  8264. * ```
  8265. */
  8266. /** 设置当前编辑区域不可编辑,except中的命令除外
  8267. * @method setDisabled
  8268. * @param { Array } except 例外命令的字符串数组,数组中的命令仍然可以执行
  8269. * @remind 即使设置了disable,此处配置的例外命令仍然可以执行
  8270. * @example
  8271. * ```javascript
  8272. * editor.setDisabled(['bold','insertimage']); //禁用工具栏中除加粗和插入图片之外的所有功能
  8273. * ```
  8274. */
  8275. setDisabled: function(except) {
  8276. var me = this;
  8277. except = except ? (utils.isArray(except) ? except : [except]) : [];
  8278. if (me.body.contentEditable == "true") {
  8279. if (!me.lastBk) {
  8280. me.lastBk = me.selection.getRange().createBookmark(true);
  8281. }
  8282. me.body.contentEditable = false;
  8283. me.bkqueryCommandState = me.queryCommandState;
  8284. me.bkqueryCommandValue = me.queryCommandValue;
  8285. me.queryCommandState = function(type) {
  8286. if (utils.indexOf(except, type) != -1) {
  8287. return me.bkqueryCommandState.apply(me, arguments);
  8288. }
  8289. return -1;
  8290. };
  8291. me.queryCommandValue = function(type) {
  8292. if (utils.indexOf(except, type) != -1) {
  8293. return me.bkqueryCommandValue.apply(me, arguments);
  8294. }
  8295. return null;
  8296. };
  8297. me.fireEvent("selectionchange");
  8298. }
  8299. },
  8300. disable: function(except) {
  8301. return this.setDisabled(except);
  8302. },
  8303. /**
  8304. * 设置默认内容
  8305. * @method _setDefaultContent
  8306. * @private
  8307. * @param { String } cont 要存入的内容
  8308. */
  8309. _setDefaultContent: (function() {
  8310. function clear() {
  8311. var me = this;
  8312. if (me.document.getElementById("initContent")) {
  8313. me.body.innerHTML = "<p>" + (ie ? "" : "<br/>") + "</p>";
  8314. me.removeListener("firstBeforeExecCommand focus", clear);
  8315. setTimeout(function() {
  8316. me.focus();
  8317. me._selectionChange();
  8318. }, 0);
  8319. }
  8320. }
  8321. return function(cont) {
  8322. var me = this;
  8323. me.body.innerHTML = '<p id="initContent">' + cont + "</p>";
  8324. me.addListener("firstBeforeExecCommand focus", clear);
  8325. };
  8326. })(),
  8327. /**
  8328. * 显示编辑器
  8329. * @method setShow
  8330. * @example
  8331. * ```javascript
  8332. * editor.setShow()
  8333. * ```
  8334. */
  8335. setShow: function() {
  8336. var me = this,
  8337. range = me.selection.getRange();
  8338. if (me.container.style.display == "none") {
  8339. //有可能内容丢失了
  8340. try {
  8341. range.moveToBookmark(me.lastBk);
  8342. delete me.lastBk;
  8343. } catch (e) {
  8344. range.setStartAtFirst(me.body).collapse(true);
  8345. }
  8346. //ie下focus实效,所以做了个延迟
  8347. setTimeout(function() {
  8348. range.select(true);
  8349. }, 100);
  8350. me.container.style.display = "";
  8351. }
  8352. },
  8353. show: function() {
  8354. return this.setShow();
  8355. },
  8356. /**
  8357. * 隐藏编辑器
  8358. * @method setHide
  8359. * @example
  8360. * ```javascript
  8361. * editor.setHide()
  8362. * ```
  8363. */
  8364. setHide: function() {
  8365. var me = this;
  8366. if (!me.lastBk) {
  8367. me.lastBk = me.selection.getRange().createBookmark(true);
  8368. }
  8369. me.container.style.display = "none";
  8370. },
  8371. hide: function() {
  8372. return this.setHide();
  8373. },
  8374. /**
  8375. * 根据指定的路径,获取对应的语言资源
  8376. * @method getLang
  8377. * @param { String } path 路径根据的是lang目录下的语言文件的路径结构
  8378. * @return { Object | String } 根据路径返回语言资源的Json格式对象或者语言字符串
  8379. * @example
  8380. * ```javascript
  8381. * editor.getLang('contextMenu.delete'); //如果当前是中文,那返回是的是'删除'
  8382. * ```
  8383. */
  8384. getLang: function(path) {
  8385. var lang = UE.I18N[this.options.lang];
  8386. if (!lang) {
  8387. throw Error("not import language file");
  8388. }
  8389. path = (path || "").split(".");
  8390. for (var i = 0, ci; (ci = path[i++]); ) {
  8391. lang = lang[ci];
  8392. if (!lang) break;
  8393. }
  8394. return lang;
  8395. },
  8396. /**
  8397. * 计算编辑器html内容字符串的长度
  8398. * @method getContentLength
  8399. * @return { Number } 返回计算的长度
  8400. * @example
  8401. * ```javascript
  8402. * //编辑器html内容<p><strong>132</strong></p>
  8403. * editor.getContentLength() //返回27
  8404. * ```
  8405. */
  8406. /**
  8407. * 计算编辑器当前纯文本内容的长度
  8408. * @method getContentLength
  8409. * @param { Boolean } ingoneHtml 传入true时,只按照纯文本来计算
  8410. * @return { Number } 返回计算的长度,内容中有hr/img/iframe标签,长度加1
  8411. * @example
  8412. * ```javascript
  8413. * //编辑器html内容<p><strong>132</strong></p>
  8414. * editor.getContentLength() //返回3
  8415. * ```
  8416. */
  8417. getContentLength: function(ingoneHtml, tagNames) {
  8418. var count = this.getContent(false, false, true).length;
  8419. if (ingoneHtml) {
  8420. tagNames = (tagNames || []).concat(["hr", "img", "iframe"]);
  8421. count = this.getContentTxt().replace(/[\t\r\n]+/g, "").length;
  8422. for (var i = 0, ci; (ci = tagNames[i++]); ) {
  8423. count += this.document.getElementsByTagName(ci).length;
  8424. }
  8425. }
  8426. return count;
  8427. },
  8428. /**
  8429. * 注册输入过滤规则
  8430. * @method addInputRule
  8431. * @param { Function } rule 要添加的过滤规则
  8432. * @example
  8433. * ```javascript
  8434. * editor.addInputRule(function(root){
  8435. * $.each(root.getNodesByTagName('div'),function(i,node){
  8436. * node.tagName="p";
  8437. * });
  8438. * });
  8439. * ```
  8440. */
  8441. addInputRule: function(rule) {
  8442. this.inputRules.push(rule);
  8443. },
  8444. /**
  8445. * 执行注册的过滤规则
  8446. * @method filterInputRule
  8447. * @param { UE.uNode } root 要过滤的uNode节点
  8448. * @remind 执行editor.setContent方法和执行'inserthtml'命令后,会运行该过滤函数
  8449. * @example
  8450. * ```javascript
  8451. * editor.filterInputRule(editor.body);
  8452. * ```
  8453. * @see UE.Editor:addInputRule
  8454. */
  8455. filterInputRule: function(root) {
  8456. for (var i = 0, ci; (ci = this.inputRules[i++]); ) {
  8457. ci.call(this, root);
  8458. }
  8459. },
  8460. /**
  8461. * 注册输出过滤规则
  8462. * @method addOutputRule
  8463. * @param { Function } rule 要添加的过滤规则
  8464. * @example
  8465. * ```javascript
  8466. * editor.addOutputRule(function(root){
  8467. * $.each(root.getNodesByTagName('p'),function(i,node){
  8468. * node.tagName="div";
  8469. * });
  8470. * });
  8471. * ```
  8472. */
  8473. addOutputRule: function(rule) {
  8474. this.outputRules.push(rule);
  8475. },
  8476. /**
  8477. * 根据输出过滤规则,过滤编辑器内容
  8478. * @method filterOutputRule
  8479. * @remind 执行editor.getContent方法的时候,会先运行该过滤函数
  8480. * @param { UE.uNode } root 要过滤的uNode节点
  8481. * @example
  8482. * ```javascript
  8483. * editor.filterOutputRule(editor.body);
  8484. * ```
  8485. * @see UE.Editor:addOutputRule
  8486. */
  8487. filterOutputRule: function(root) {
  8488. for (var i = 0, ci; (ci = this.outputRules[i++]); ) {
  8489. ci.call(this, root);
  8490. }
  8491. },
  8492. /**
  8493. * 根据action名称获取请求的路径
  8494. * @method getActionUrl
  8495. * @remind 假如没有设置serverUrl,会根据imageUrl设置默认的controller路径
  8496. * @param { String } action action名称
  8497. * @example
  8498. * ```javascript
  8499. * editor.getActionUrl('config'); //返回 "/ueditor/php/controller.php?action=config"
  8500. * editor.getActionUrl('image'); //返回 "/ueditor/php/controller.php?action=uplaodimage"
  8501. * editor.getActionUrl('scrawl'); //返回 "/ueditor/php/controller.php?action=uplaodscrawl"
  8502. * editor.getActionUrl('imageManager'); //返回 "/ueditor/php/controller.php?action=listimage"
  8503. * ```
  8504. */
  8505. getActionUrl: function(action) {
  8506. var actionName = this.getOpt(action) || action,
  8507. imageUrl = this.getOpt("imageUrl"),
  8508. serverUrl = this.getOpt("serverUrl");
  8509. if (!serverUrl && imageUrl) {
  8510. serverUrl = imageUrl.replace(/^(.*[\/]).+([\.].+)$/, "$1controller$2");
  8511. }
  8512. if (serverUrl) {
  8513. serverUrl =
  8514. serverUrl +
  8515. (serverUrl.indexOf("?") == -1 ? "?" : "&") +
  8516. "action=" +
  8517. (actionName || "");
  8518. return utils.formatUrl(serverUrl);
  8519. } else {
  8520. return "";
  8521. }
  8522. }
  8523. };
  8524. utils.inherits(Editor, EventBase);
  8525. })();
  8526. // core/Editor.defaultoptions.js
  8527. //维护编辑器一下默认的不在插件中的配置项
  8528. UE.Editor.defaultOptions = function(editor) {
  8529. var _url = editor.options.UEDITOR_HOME_URL;
  8530. return {
  8531. isShow: true,
  8532. initialContent: "",
  8533. initialStyle: "",
  8534. autoClearinitialContent: false,
  8535. iframeCssUrl: _url + "themes/iframe.css?20221113",
  8536. iframeCssUrlsAddition: [],
  8537. textarea: "editorValue",
  8538. focus: false,
  8539. focusInEnd: true,
  8540. autoClearEmptyNode: true,
  8541. fullscreen: false,
  8542. readonly: false,
  8543. zIndex: 999,
  8544. imagePopup: true,
  8545. enterTag: "p",
  8546. customDomain: false,
  8547. lang: "zh-cn",
  8548. langPath: _url + "lang/",
  8549. theme: "default",
  8550. themePath: _url + "themes/",
  8551. allHtmlEnabled: false,
  8552. scaleEnabled: false,
  8553. tableNativeEditInFF: false,
  8554. autoSyncData: true,
  8555. fileNameFormat: "{time}{rand:6}"
  8556. };
  8557. };
  8558. // core/loadconfig.js
  8559. (function() {
  8560. UE.Editor.prototype.loadServerConfig = function() {
  8561. var me = this;
  8562. setTimeout(function() {
  8563. try {
  8564. me.options.imageUrl &&
  8565. me.setOpt(
  8566. "serverUrl",
  8567. me.options.imageUrl.replace(
  8568. /^(.*[\/]).+([\.].+)$/,
  8569. "$1controller$2"
  8570. )
  8571. );
  8572. var configUrl = me.getActionUrl("config"),
  8573. isJsonp = utils.isCrossDomainUrl(configUrl);
  8574. /* 发出ajax请求 */
  8575. me._serverConfigLoaded = false;
  8576. configUrl &&
  8577. UE.ajax.request(configUrl, {
  8578. method: "GET",
  8579. dataType: isJsonp ? "jsonp" : "",
  8580. headers: me.options.serverHeaders || {},
  8581. onsuccess: function(r) {
  8582. try {
  8583. var config = isJsonp ? r : eval("(" + r.responseText + ")");
  8584. utils.extend(me.options, config);
  8585. // console.log('me.options.before',me.options);
  8586. // console.log('server.config',config);
  8587. // console.log('me.options.after',me.options);
  8588. me.fireEvent("serverConfigLoaded");
  8589. me._serverConfigLoaded = true;
  8590. } catch (e) {
  8591. showErrorMsg(me.getLang("loadconfigFormatError"));
  8592. }
  8593. },
  8594. onerror: function() {
  8595. showErrorMsg(me.getLang("loadconfigHttpError"));
  8596. }
  8597. });
  8598. } catch (e) {
  8599. showErrorMsg(me.getLang("loadconfigError"));
  8600. }
  8601. });
  8602. function showErrorMsg(msg) {
  8603. console && console.error(msg);
  8604. //me.fireEvent('showMessage', {
  8605. // 'title': msg,
  8606. // 'type': 'error'
  8607. //});
  8608. }
  8609. };
  8610. UE.Editor.prototype.isServerConfigLoaded = function() {
  8611. var me = this;
  8612. return me._serverConfigLoaded || false;
  8613. };
  8614. UE.Editor.prototype.afterConfigReady = function(handler) {
  8615. if (!handler || !utils.isFunction(handler)) return;
  8616. var me = this;
  8617. var readyHandler = function() {
  8618. handler.apply(me, arguments);
  8619. me.removeListener("serverConfigLoaded", readyHandler);
  8620. };
  8621. if (me.isServerConfigLoaded()) {
  8622. handler.call(me, "serverConfigLoaded");
  8623. } else {
  8624. me.addListener("serverConfigLoaded", readyHandler);
  8625. }
  8626. };
  8627. })();
  8628. // core/ajax.js
  8629. /**
  8630. * @file
  8631. * @module UE.ajax
  8632. * @since 1.2.6.1
  8633. */
  8634. /**
  8635. * 提供对ajax请求的支持
  8636. * @module UE.ajax
  8637. */
  8638. UE.ajax = (function() {
  8639. //创建一个ajaxRequest对象
  8640. var fnStr = "XMLHttpRequest()";
  8641. try {
  8642. new ActiveXObject("Msxml2.XMLHTTP");
  8643. fnStr = "ActiveXObject('Msxml2.XMLHTTP')";
  8644. } catch (e) {
  8645. try {
  8646. new ActiveXObject("Microsoft.XMLHTTP");
  8647. fnStr = "ActiveXObject('Microsoft.XMLHTTP')";
  8648. } catch (e) {}
  8649. }
  8650. var creatAjaxRequest = new Function("return new " + fnStr);
  8651. /**
  8652. * 将json参数转化成适合ajax提交的参数列表
  8653. * @param json
  8654. */
  8655. function json2str(json) {
  8656. var strArr = [];
  8657. for (var i in json) {
  8658. //忽略默认的几个参数
  8659. if (
  8660. i == "method" ||
  8661. i == "timeout" ||
  8662. i == "async" ||
  8663. i == "dataType" ||
  8664. i == "callback"
  8665. )
  8666. continue;
  8667. //忽略控制
  8668. if (json[i] == undefined || json[i] == null) continue;
  8669. //传递过来的对象和函数不在提交之列
  8670. if (
  8671. !(
  8672. (typeof json[i]).toLowerCase() == "function" ||
  8673. (typeof json[i]).toLowerCase() == "object"
  8674. )
  8675. ) {
  8676. strArr.push(encodeURIComponent(i) + "=" + encodeURIComponent(json[i]));
  8677. } else if (utils.isArray(json[i])) {
  8678. //支持传数组内容
  8679. for (var j = 0; j < json[i].length; j++) {
  8680. strArr.push(
  8681. encodeURIComponent(i) + "[]=" + encodeURIComponent(json[i][j])
  8682. );
  8683. }
  8684. }
  8685. }
  8686. return strArr.join("&");
  8687. }
  8688. function doAjax(url, ajaxOptions) {
  8689. var xhr = creatAjaxRequest(),
  8690. //是否超时
  8691. timeIsOut = false,
  8692. //默认参数
  8693. defaultAjaxOptions = {
  8694. method: "POST",
  8695. timeout: 5000,
  8696. async: true,
  8697. headers: {},
  8698. data: {}, //需要传递对象的话只能覆盖
  8699. onsuccess: function() {},
  8700. onerror: function() {}
  8701. };
  8702. if (typeof url === "object") {
  8703. ajaxOptions = url;
  8704. url = ajaxOptions.url;
  8705. }
  8706. if (!xhr || !url) return;
  8707. var ajaxOpts = ajaxOptions
  8708. ? utils.extend(defaultAjaxOptions, ajaxOptions)
  8709. : defaultAjaxOptions;
  8710. // console.log('ajaxOpts',ajaxOpts);
  8711. var submitStr = json2str(ajaxOpts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8712. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8713. if (!utils.isEmptyObject(ajaxOpts.data)) {
  8714. submitStr += (submitStr ? "&" : "") + json2str(ajaxOpts.data);
  8715. }
  8716. //超时检测
  8717. var timerID = setTimeout(function() {
  8718. if (xhr.readyState !== 4) {
  8719. timeIsOut = true;
  8720. xhr.abort();
  8721. clearTimeout(timerID);
  8722. }
  8723. }, ajaxOpts.timeout);
  8724. var method = ajaxOpts.method.toUpperCase();
  8725. var str =
  8726. url +
  8727. (url.indexOf("?") === -1 ? "?" : "&") +
  8728. (method === "POST" ? "" : submitStr + "&noCache=" + +new Date());
  8729. xhr.open(method, str, ajaxOpts.async);
  8730. xhr.onreadystatechange = function() {
  8731. if (xhr.readyState === 4) {
  8732. if (!timeIsOut && xhr.status === 200) {
  8733. ajaxOpts.onsuccess(xhr);
  8734. } else {
  8735. ajaxOpts.onerror(xhr);
  8736. }
  8737. }
  8738. };
  8739. if(ajaxOpts.headers){
  8740. for(var key in ajaxOpts.headers){
  8741. xhr.setRequestHeader(key,ajaxOpts.headers[key]);
  8742. }
  8743. }
  8744. if (method === "POST") {
  8745. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  8746. xhr.send(submitStr);
  8747. } else {
  8748. xhr.send(null);
  8749. }
  8750. }
  8751. function doJsonp(url, opts) {
  8752. var successhandler = opts.onsuccess || function() {},
  8753. scr = document.createElement("SCRIPT"),
  8754. options = opts || {},
  8755. charset = options["charset"],
  8756. callbackField = options["jsonp"] || "callback",
  8757. callbackFnName,
  8758. timeOut = options["timeOut"] || 0,
  8759. timer,
  8760. reg = new RegExp("(\\?|&)" + callbackField + "=([^&]*)"),
  8761. matches;
  8762. if (utils.isFunction(successhandler)) {
  8763. callbackFnName =
  8764. "bd__editor__" + Math.floor(Math.random() * 2147483648).toString(36);
  8765. window[callbackFnName] = getCallBack(0);
  8766. } else if (utils.isString(successhandler)) {
  8767. callbackFnName = successhandler;
  8768. } else {
  8769. if ((matches = reg.exec(url))) {
  8770. callbackFnName = matches[2];
  8771. }
  8772. }
  8773. url = url.replace(reg, "\x241" + callbackField + "=" + callbackFnName);
  8774. if (url.search(reg) < 0) {
  8775. url +=
  8776. (url.indexOf("?") < 0 ? "?" : "&") +
  8777. callbackField +
  8778. "=" +
  8779. callbackFnName;
  8780. }
  8781. var queryStr = json2str(opts); // { name:"Jim",city:"Beijing" } --> "name=Jim&city=Beijing"
  8782. //如果用户直接通过data参数传递json对象过来,则也要将此json对象转化为字符串
  8783. if (!utils.isEmptyObject(opts.data)) {
  8784. queryStr += (queryStr ? "&" : "") + json2str(opts.data);
  8785. }
  8786. if (queryStr) {
  8787. url = url.replace(/\?/, "?" + queryStr + "&");
  8788. }
  8789. scr.onerror = getCallBack(1);
  8790. if (timeOut) {
  8791. timer = setTimeout(getCallBack(1), timeOut);
  8792. }
  8793. createScriptTag(scr, url, charset);
  8794. function createScriptTag(scr, url, charset) {
  8795. scr.setAttribute("type", "text/javascript");
  8796. scr.setAttribute("defer", "defer");
  8797. charset && scr.setAttribute("charset", charset);
  8798. scr.setAttribute("src", url);
  8799. document.getElementsByTagName("head")[0].appendChild(scr);
  8800. }
  8801. function getCallBack(onTimeOut) {
  8802. return function() {
  8803. try {
  8804. if (onTimeOut) {
  8805. options.onerror && options.onerror();
  8806. } else {
  8807. try {
  8808. clearTimeout(timer);
  8809. successhandler.apply(window, arguments);
  8810. } catch (e) {}
  8811. }
  8812. } catch (exception) {
  8813. options.onerror && options.onerror.call(window, exception);
  8814. } finally {
  8815. options.oncomplete && options.oncomplete.apply(window, arguments);
  8816. scr.parentNode && scr.parentNode.removeChild(scr);
  8817. window[callbackFnName] = null;
  8818. try {
  8819. delete window[callbackFnName];
  8820. } catch (e) {}
  8821. }
  8822. };
  8823. }
  8824. }
  8825. return {
  8826. /**
  8827. * 根据给定的参数项,向指定的url发起一个ajax请求。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8828. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调
  8829. * @method request
  8830. * @param { URLString } url ajax请求的url地址
  8831. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8832. * @example
  8833. * ```javascript
  8834. * //向sayhello.php发起一个异步的Ajax GET请求, 请求超时时间为10s, 请求完成后执行相应的回调。
  8835. * UE.ajax.requeset( 'sayhello.php', {
  8836. *
  8837. * //请求方法。可选值: 'GET', 'POST',默认值是'POST'
  8838. * method: 'GET',
  8839. *
  8840. * //超时时间。 默认为5000, 单位是ms
  8841. * timeout: 10000,
  8842. *
  8843. * //是否是异步请求。 true为异步请求, false为同步请求
  8844. * async: true,
  8845. *
  8846. * //请求携带的数据。如果请求为GET请求, data会经过stringify后附加到请求url之后。
  8847. * data: {
  8848. * name: 'ueditor'
  8849. * },
  8850. *
  8851. * //请求成功后的回调, 该回调接受当前的XMLHttpRequest对象作为参数。
  8852. * onsuccess: function ( xhr ) {
  8853. * console.log( xhr.responseText );
  8854. * },
  8855. *
  8856. * //请求失败或者超时后的回调。
  8857. * onerror: function ( xhr ) {
  8858. * alert( 'Ajax请求失败' );
  8859. * }
  8860. *
  8861. * } );
  8862. * ```
  8863. */
  8864. /**
  8865. * 根据给定的参数项发起一个ajax请求, 参数项里必须包含一个url地址。 ajax请求完成后,会根据请求结果调用相应回调: 如果请求
  8866. * 成功, 则调用onsuccess回调, 失败则调用 onerror 回调。
  8867. * @method request
  8868. * @warning 如果在参数项里未提供一个key为“url”的地址值,则该请求将直接退出。
  8869. * @param { Object } ajaxOptions ajax请求选项的键值对,支持的选项如下:
  8870. * @example
  8871. * ```javascript
  8872. *
  8873. * //向sayhello.php发起一个异步的Ajax POST请求, 请求超时时间为5s, 请求完成后不执行任何回调。
  8874. * UE.ajax.requeset( 'sayhello.php', {
  8875. *
  8876. * //请求的地址, 该项是必须的。
  8877. * url: 'sayhello.php'
  8878. *
  8879. * } );
  8880. * ```
  8881. */
  8882. request: function(url, opts) {
  8883. if (opts && opts.dataType === "jsonp") {
  8884. doJsonp(url, opts);
  8885. } else {
  8886. doAjax(url, opts);
  8887. }
  8888. },
  8889. getJSONP: function(url, data, fn) {
  8890. var opts = {
  8891. data: data,
  8892. oncomplete: fn
  8893. };
  8894. doJsonp(url, opts);
  8895. }
  8896. };
  8897. })();
  8898. // core/filterword.js
  8899. /**
  8900. * UE过滤word的静态方法
  8901. * @file
  8902. */
  8903. /**
  8904. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  8905. * @module UE
  8906. */
  8907. /**
  8908. * 根据传入html字符串过滤word
  8909. * @module UE
  8910. * @since 1.2.6.1
  8911. * @method filterWord
  8912. * @param { String } html html字符串
  8913. * @return { String } 已过滤后的结果字符串
  8914. * @example
  8915. * ```javascript
  8916. * UE.filterWord(html);
  8917. * ```
  8918. */
  8919. var filterWord = (UE.filterWord = (function() {
  8920. //是否是word过来的内容
  8921. function isWordDocument(str) {
  8922. return /(class="?Mso|style="[^"]*\bmso\-|w:WordDocument|<(v|o):|lang=)/gi.test(
  8923. str
  8924. );
  8925. }
  8926. //去掉小数
  8927. function transUnit(v) {
  8928. v = v.replace(/[\d.]+\w+/g, function(m) {
  8929. return utils.transUnitToPx(m);
  8930. });
  8931. return v;
  8932. }
  8933. function filterPasteWord(str) {
  8934. return (
  8935. str
  8936. .replace(/[\t\r\n]+/g, " ")
  8937. .replace(/<!--[\s\S]*?-->/gi, "")
  8938. //转换图片
  8939. .replace(/<v:shape [^>]*>[\s\S]*?.<\/v:shape>/gi, function(str) {
  8940. //opera能自己解析出image所这里直接返回空
  8941. if (browser.opera) {
  8942. return "";
  8943. }
  8944. try {
  8945. //有可能是bitmap占为图,无用,直接过滤掉,主要体现在粘贴excel表格中
  8946. if (/Bitmap/i.test(str)) {
  8947. return "";
  8948. }
  8949. var width = str.match(/width:([ \d.]*p[tx])/i)[1],
  8950. height = str.match(/height:([ \d.]*p[tx])/i)[1],
  8951. src = str.match(/src=\s*"([^"]*)"/i)[1];
  8952. return (
  8953. '<img width="' +
  8954. transUnit(width) +
  8955. '" height="' +
  8956. transUnit(height) +
  8957. '" src="' +
  8958. src +
  8959. '" />'
  8960. );
  8961. } catch (e) {
  8962. return "";
  8963. }
  8964. })
  8965. //针对wps添加的多余标签处理
  8966. .replace(/<\/?div[^>]*>/g, "")
  8967. //去掉多余的属性
  8968. .replace(/v:\w+=(["']?)[^'"]+\1/g, "")
  8969. .replace(
  8970. /<(!|script[^>]*>.*?<\/script(?=[>\s])|\/?(\?xml(:\w+)?|xml|meta|link|style|\w+:\w+)(?=[\s\/>]))[^>]*>/gi,
  8971. ""
  8972. )
  8973. .replace(
  8974. /<p [^>]*class="?MsoHeading"?[^>]*>(.*?)<\/p>/gi,
  8975. "<p><strong>$1</strong></p>"
  8976. )
  8977. //去掉多余的属性
  8978. .replace(/\s+(class|lang|align)\s*=\s*(['"]?)([\w-]+)\2/gi, function(
  8979. str,
  8980. name,
  8981. marks,
  8982. val
  8983. ) {
  8984. //保留list的标示
  8985. return name == "class" && val == "MsoListParagraph" ? str : "";
  8986. })
  8987. //清除多余的font/span不能匹配&nbsp;有可能是空格
  8988. .replace(/<(font|span)[^>]*>(\s*)<\/\1>/gi, function(a, b, c) {
  8989. return c.replace(/[\t\r\n ]+/g, " ");
  8990. })
  8991. //处理style的问题
  8992. .replace(/(<[a-z][^>]*)\sstyle=(["'])([^\2]*?)\2/gi, function(
  8993. str,
  8994. tag,
  8995. tmp,
  8996. style
  8997. ) {
  8998. var n = [],
  8999. s = style
  9000. .replace(/^\s+|\s+$/, "")
  9001. .replace(/&#39;/g, "'")
  9002. .replace(/&quot;/gi, "'")
  9003. .replace(/[\d.]+(cm|pt)/g, function(str) {
  9004. return utils.transUnitToPx(str);
  9005. })
  9006. .split(/;\s*/g);
  9007. for (var i = 0, v; (v = s[i]); i++) {
  9008. var name,
  9009. value,
  9010. parts = v.split(":");
  9011. if (parts.length == 2) {
  9012. name = parts[0].toLowerCase();
  9013. value = parts[1].toLowerCase();
  9014. if (
  9015. (/^(background)\w*/.test(name) &&
  9016. value.replace(/(initial|\s)/g, "").length == 0) ||
  9017. (/^(margin)\w*/.test(name) && /^0\w+$/.test(value))
  9018. ) {
  9019. continue;
  9020. }
  9021. switch (name) {
  9022. case "mso-padding-alt":
  9023. case "mso-padding-top-alt":
  9024. case "mso-padding-right-alt":
  9025. case "mso-padding-bottom-alt":
  9026. case "mso-padding-left-alt":
  9027. case "mso-margin-alt":
  9028. case "mso-margin-top-alt":
  9029. case "mso-margin-right-alt":
  9030. case "mso-margin-bottom-alt":
  9031. case "mso-margin-left-alt":
  9032. //ie下会出现挤到一起的情况
  9033. //case "mso-table-layout-alt":
  9034. case "mso-height":
  9035. case "mso-width":
  9036. case "mso-vertical-align-alt":
  9037. //trace:1819 ff下会解析出padding在table上
  9038. if (!/<table/.test(tag))
  9039. n[i] =
  9040. name.replace(/^mso-|-alt$/g, "") + ":" + transUnit(value);
  9041. continue;
  9042. case "horiz-align":
  9043. n[i] = "text-align:" + value;
  9044. continue;
  9045. case "vert-align":
  9046. n[i] = "vertical-align:" + value;
  9047. continue;
  9048. case "font-color":
  9049. case "mso-foreground":
  9050. n[i] = "color:" + value;
  9051. continue;
  9052. case "mso-background":
  9053. case "mso-highlight":
  9054. n[i] = "background:" + value;
  9055. continue;
  9056. case "mso-default-height":
  9057. n[i] = "min-height:" + transUnit(value);
  9058. continue;
  9059. case "mso-default-width":
  9060. n[i] = "min-width:" + transUnit(value);
  9061. continue;
  9062. case "mso-padding-between-alt":
  9063. n[i] =
  9064. "border-collapse:separate;border-spacing:" +
  9065. transUnit(value);
  9066. continue;
  9067. case "text-line-through":
  9068. if (value == "single" || value == "double") {
  9069. n[i] = "text-decoration:line-through";
  9070. }
  9071. continue;
  9072. case "mso-zero-height":
  9073. if (value == "yes") {
  9074. n[i] = "display:none";
  9075. }
  9076. continue;
  9077. // case 'background':
  9078. // break;
  9079. case "margin":
  9080. if (!/[1-9]/.test(value)) {
  9081. continue;
  9082. }
  9083. }
  9084. if (
  9085. /^(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(
  9086. name
  9087. ) ||
  9088. (/text\-indent|padding|margin/.test(name) &&
  9089. /\-[\d.]+/.test(value))
  9090. ) {
  9091. continue;
  9092. }
  9093. n[i] = name + ":" + parts[1];
  9094. }
  9095. }
  9096. return (
  9097. tag +
  9098. (n.length
  9099. ? ' style="' + n.join(";").replace(/;{2,}/g, ";") + '"'
  9100. : "")
  9101. );
  9102. })
  9103. );
  9104. }
  9105. return function(html) {
  9106. return isWordDocument(html) ? filterPasteWord(html) : html;
  9107. };
  9108. })());
  9109. // core/node.js
  9110. /**
  9111. * 编辑器模拟的节点类
  9112. * @file
  9113. * @module UE
  9114. * @class uNode
  9115. * @since 1.2.6.1
  9116. */
  9117. /**
  9118. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9119. * @unfile
  9120. * @module UE
  9121. */
  9122. (function() {
  9123. /**
  9124. * 编辑器模拟的节点类
  9125. * @unfile
  9126. * @module UE
  9127. * @class uNode
  9128. */
  9129. /**
  9130. * 通过一个键值对,创建一个uNode对象
  9131. * @constructor
  9132. * @param { Object } attr 传入要创建的uNode的初始属性
  9133. * @example
  9134. * ```javascript
  9135. * var node = new uNode({
  9136. * type:'element',
  9137. * tagName:'span',
  9138. * attrs:{style:'font-size:14px;'}
  9139. * })
  9140. * ```
  9141. */
  9142. var uNode = (UE.uNode = function(obj) {
  9143. this.type = obj.type;
  9144. this.data = obj.data;
  9145. this.tagName = obj.tagName;
  9146. this.parentNode = obj.parentNode;
  9147. this.attrs = obj.attrs || {};
  9148. this.children = obj.children;
  9149. });
  9150. var notTransAttrs = {
  9151. href: 1,
  9152. src: 1,
  9153. _src: 1,
  9154. _href: 1,
  9155. cdata_data: 1
  9156. };
  9157. var notTransTagName = {
  9158. style: 1,
  9159. script: 1
  9160. };
  9161. var indentChar = " ",
  9162. breakChar = "\n";
  9163. function insertLine(arr, current, begin) {
  9164. arr.push(breakChar);
  9165. return current + (begin ? 1 : -1);
  9166. }
  9167. function insertIndent(arr, current) {
  9168. //插入缩进
  9169. for (var i = 0; i < current; i++) {
  9170. arr.push(indentChar);
  9171. }
  9172. }
  9173. //创建uNode的静态方法
  9174. //支持标签和html
  9175. uNode.createElement = function(html) {
  9176. if (/[<>]/.test(html)) {
  9177. return UE.htmlparser(html).children[0];
  9178. } else {
  9179. return new uNode({
  9180. type: "element",
  9181. children: [],
  9182. tagName: html
  9183. });
  9184. }
  9185. };
  9186. uNode.createText = function(data, noTrans) {
  9187. return new UE.uNode({
  9188. type: "text",
  9189. data: noTrans ? data : utils.unhtml(data || "")
  9190. });
  9191. };
  9192. function nodeToHtml(node, arr, formatter, current) {
  9193. switch (node.type) {
  9194. case "root":
  9195. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9196. //插入新行
  9197. if (
  9198. formatter &&
  9199. ci.type == "element" &&
  9200. !dtd.$inlineWithA[ci.tagName] &&
  9201. i > 1
  9202. ) {
  9203. insertLine(arr, current, true);
  9204. insertIndent(arr, current);
  9205. }
  9206. nodeToHtml(ci, arr, formatter, current);
  9207. }
  9208. break;
  9209. case "text":
  9210. isText(node, arr);
  9211. break;
  9212. case "element":
  9213. isElement(node, arr, formatter, current);
  9214. break;
  9215. case "comment":
  9216. isComment(node, arr, formatter);
  9217. }
  9218. return arr;
  9219. }
  9220. function isText(node, arr) {
  9221. if (node.parentNode.tagName == "pre") {
  9222. //源码模式下输入html标签,不能做转换处理,直接输出
  9223. arr.push(node.data);
  9224. } else {
  9225. arr.push(
  9226. notTransTagName[node.parentNode.tagName]
  9227. ? utils.html(node.data)
  9228. : node.data.replace(/[ ]{2}/g, " &nbsp;")
  9229. );
  9230. }
  9231. }
  9232. function isElement(node, arr, formatter, current) {
  9233. var attrhtml = "";
  9234. if (node.attrs) {
  9235. attrhtml = [];
  9236. var attrs = node.attrs;
  9237. for (var a in attrs) {
  9238. //这里就针对
  9239. //<p>'<img src='http://nsclick.baidu.com/u.gif?&asdf=\"sdf&asdfasdfs;asdf'></p>
  9240. //这里边的\"做转换,要不用innerHTML直接被截断了,属性src
  9241. //有可能做的不够
  9242. attrhtml.push(
  9243. a +
  9244. (attrs[a] !== undefined
  9245. ? '="' +
  9246. (notTransAttrs[a]
  9247. ? utils.html(attrs[a]).replace(/["]/g, function(a) {
  9248. return "&quot;";
  9249. })
  9250. : utils.unhtml(attrs[a])) +
  9251. '"'
  9252. : "")
  9253. );
  9254. }
  9255. attrhtml = attrhtml.join(" ");
  9256. }
  9257. arr.push(
  9258. "<" +
  9259. node.tagName +
  9260. (attrhtml ? " " + attrhtml : "") +
  9261. (dtd.$empty[node.tagName] ? "/" : "") +
  9262. ">"
  9263. );
  9264. //插入新行
  9265. if (formatter && !dtd.$inlineWithA[node.tagName] && node.tagName != "pre") {
  9266. if (node.children && node.children.length) {
  9267. current = insertLine(arr, current, true);
  9268. insertIndent(arr, current);
  9269. }
  9270. }
  9271. if (node.children && node.children.length) {
  9272. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9273. if (
  9274. formatter &&
  9275. ci.type == "element" &&
  9276. !dtd.$inlineWithA[ci.tagName] &&
  9277. i > 1
  9278. ) {
  9279. insertLine(arr, current);
  9280. insertIndent(arr, current);
  9281. }
  9282. nodeToHtml(ci, arr, formatter, current);
  9283. }
  9284. }
  9285. if (!dtd.$empty[node.tagName]) {
  9286. if (
  9287. formatter &&
  9288. !dtd.$inlineWithA[node.tagName] &&
  9289. node.tagName != "pre"
  9290. ) {
  9291. if (node.children && node.children.length) {
  9292. current = insertLine(arr, current);
  9293. insertIndent(arr, current);
  9294. }
  9295. }
  9296. arr.push("</" + node.tagName + ">");
  9297. }
  9298. }
  9299. function isComment(node, arr) {
  9300. arr.push("<!--" + node.data + "-->");
  9301. }
  9302. function getNodeById(root, id) {
  9303. var node;
  9304. if (root.type == "element" && root.getAttr("id") == id) {
  9305. return root;
  9306. }
  9307. if (root.children && root.children.length) {
  9308. for (var i = 0, ci; (ci = root.children[i++]); ) {
  9309. if ((node = getNodeById(ci, id))) {
  9310. return node;
  9311. }
  9312. }
  9313. }
  9314. }
  9315. function getNodesByTagName(node, tagName, arr) {
  9316. if (node.type == "element" && node.tagName == tagName) {
  9317. arr.push(node);
  9318. }
  9319. if (node.children && node.children.length) {
  9320. for (var i = 0, ci; (ci = node.children[i++]); ) {
  9321. getNodesByTagName(ci, tagName, arr);
  9322. }
  9323. }
  9324. }
  9325. function nodeTraversal(root, fn) {
  9326. if (root.children && root.children.length) {
  9327. for (var i = 0, ci; (ci = root.children[i]); ) {
  9328. nodeTraversal(ci, fn);
  9329. //ci被替换的情况,这里就不再走 fn了
  9330. if (ci.parentNode) {
  9331. if (ci.children && ci.children.length) {
  9332. fn(ci);
  9333. }
  9334. if (ci.parentNode) i++;
  9335. }
  9336. }
  9337. } else {
  9338. fn(root);
  9339. }
  9340. }
  9341. uNode.prototype = {
  9342. /**
  9343. * 当前节点对象,转换成html文本
  9344. * @method toHtml
  9345. * @return { String } 返回转换后的html字符串
  9346. * @example
  9347. * ```javascript
  9348. * node.toHtml();
  9349. * ```
  9350. */
  9351. /**
  9352. * 当前节点对象,转换成html文本
  9353. * @method toHtml
  9354. * @param { Boolean } formatter 是否格式化返回值
  9355. * @return { String } 返回转换后的html字符串
  9356. * @example
  9357. * ```javascript
  9358. * node.toHtml( true );
  9359. * ```
  9360. */
  9361. toHtml: function(formatter) {
  9362. var arr = [];
  9363. nodeToHtml(this, arr, formatter, 0);
  9364. return arr.join("");
  9365. },
  9366. /**
  9367. * 获取节点的html内容
  9368. * @method innerHTML
  9369. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9370. * @return { String } 返回节点的html内容
  9371. * @example
  9372. * ```javascript
  9373. * var htmlstr = node.innerHTML();
  9374. * ```
  9375. */
  9376. /**
  9377. * 设置节点的html内容
  9378. * @method innerHTML
  9379. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9380. * @param { String } htmlstr 传入要设置的html内容
  9381. * @return { UE.uNode } 返回节点本身
  9382. * @example
  9383. * ```javascript
  9384. * node.innerHTML('<span>text</span>');
  9385. * ```
  9386. */
  9387. innerHTML: function(htmlstr) {
  9388. if (this.type != "element" || dtd.$empty[this.tagName]) {
  9389. return this;
  9390. }
  9391. if (utils.isString(htmlstr)) {
  9392. if (this.children) {
  9393. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9394. ci.parentNode = null;
  9395. }
  9396. }
  9397. this.children = [];
  9398. var tmpRoot = UE.htmlparser(htmlstr);
  9399. for (var i = 0, ci; (ci = tmpRoot.children[i++]); ) {
  9400. this.children.push(ci);
  9401. ci.parentNode = this;
  9402. }
  9403. return this;
  9404. } else {
  9405. var tmpRoot = new UE.uNode({
  9406. type: "root",
  9407. children: this.children
  9408. });
  9409. return tmpRoot.toHtml();
  9410. }
  9411. },
  9412. /**
  9413. * 获取节点的纯文本内容
  9414. * @method innerText
  9415. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9416. * @return { String } 返回节点的存文本内容
  9417. * @example
  9418. * ```javascript
  9419. * var textStr = node.innerText();
  9420. * ```
  9421. */
  9422. /**
  9423. * 设置节点的纯文本内容
  9424. * @method innerText
  9425. * @warning 假如节点的type不是'element',或节点的标签名称不在dtd列表里,直接返回当前节点
  9426. * @param { String } textStr 传入要设置的文本内容
  9427. * @return { UE.uNode } 返回节点本身
  9428. * @example
  9429. * ```javascript
  9430. * node.innerText('<span>text</span>');
  9431. * ```
  9432. */
  9433. innerText: function(textStr, noTrans) {
  9434. if (this.type != "element" || dtd.$empty[this.tagName]) {
  9435. return this;
  9436. }
  9437. if (textStr) {
  9438. if (this.children) {
  9439. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9440. ci.parentNode = null;
  9441. }
  9442. }
  9443. this.children = [];
  9444. this.appendChild(uNode.createText(textStr, noTrans));
  9445. return this;
  9446. } else {
  9447. return this.toHtml().replace(/<[^>]+>/g, "");
  9448. }
  9449. },
  9450. /**
  9451. * 获取当前对象的data属性
  9452. * @method getData
  9453. * @return { Object } 若节点的type值是elemenet,返回空字符串,否则返回节点的data属性
  9454. * @example
  9455. * ```javascript
  9456. * node.getData();
  9457. * ```
  9458. */
  9459. getData: function() {
  9460. if (this.type == "element") return "";
  9461. return this.data;
  9462. },
  9463. /**
  9464. * 获取当前节点下的第一个子节点
  9465. * @method firstChild
  9466. * @return { UE.uNode } 返回第一个子节点
  9467. * @example
  9468. * ```javascript
  9469. * node.firstChild(); //返回第一个子节点
  9470. * ```
  9471. */
  9472. firstChild: function() {
  9473. // if (this.type != 'element' || dtd.$empty[this.tagName]) {
  9474. // return this;
  9475. // }
  9476. return this.children ? this.children[0] : null;
  9477. },
  9478. /**
  9479. * 获取当前节点下的最后一个子节点
  9480. * @method lastChild
  9481. * @return { UE.uNode } 返回最后一个子节点
  9482. * @example
  9483. * ```javascript
  9484. * node.lastChild(); //返回最后一个子节点
  9485. * ```
  9486. */
  9487. lastChild: function() {
  9488. // if (this.type != 'element' || dtd.$empty[this.tagName] ) {
  9489. // return this;
  9490. // }
  9491. return this.children ? this.children[this.children.length - 1] : null;
  9492. },
  9493. /**
  9494. * 获取和当前节点有相同父亲节点的前一个节点
  9495. * @method previousSibling
  9496. * @return { UE.uNode } 返回前一个节点
  9497. * @example
  9498. * ```javascript
  9499. * node.children[2].previousSibling(); //返回子节点node.children[1]
  9500. * ```
  9501. */
  9502. previousSibling: function() {
  9503. var parent = this.parentNode;
  9504. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9505. if (ci === this) {
  9506. return i == 0 ? null : parent.children[i - 1];
  9507. }
  9508. }
  9509. },
  9510. /**
  9511. * 获取和当前节点有相同父亲节点的后一个节点
  9512. * @method nextSibling
  9513. * @return { UE.uNode } 返回后一个节点,找不到返回null
  9514. * @example
  9515. * ```javascript
  9516. * node.children[2].nextSibling(); //如果有,返回子节点node.children[3]
  9517. * ```
  9518. */
  9519. nextSibling: function() {
  9520. var parent = this.parentNode;
  9521. for (var i = 0, ci; (ci = parent.children[i++]); ) {
  9522. if (ci === this) {
  9523. return parent.children[i];
  9524. }
  9525. }
  9526. },
  9527. /**
  9528. * 用新的节点替换当前节点
  9529. * @method replaceChild
  9530. * @param { UE.uNode } target 要替换成该节点参数
  9531. * @param { UE.uNode } source 要被替换掉的节点
  9532. * @return { UE.uNode } 返回替换之后的节点对象
  9533. * @example
  9534. * ```javascript
  9535. * node.replaceChild(newNode, childNode); //用newNode替换childNode,childNode是node的子节点
  9536. * ```
  9537. */
  9538. replaceChild: function(target, source) {
  9539. if (this.children) {
  9540. if (target.parentNode) {
  9541. target.parentNode.removeChild(target);
  9542. }
  9543. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9544. if (ci === source) {
  9545. this.children.splice(i, 1, target);
  9546. source.parentNode = null;
  9547. target.parentNode = this;
  9548. return target;
  9549. }
  9550. }
  9551. }
  9552. },
  9553. /**
  9554. * 在节点的子节点列表最后位置插入一个节点
  9555. * @method appendChild
  9556. * @param { UE.uNode } node 要插入的节点
  9557. * @return { UE.uNode } 返回刚插入的子节点
  9558. * @example
  9559. * ```javascript
  9560. * node.appendChild( newNode ); //在node内插入子节点newNode
  9561. * ```
  9562. */
  9563. appendChild: function(node) {
  9564. if (
  9565. this.type == "root" ||
  9566. (this.type == "element" && !dtd.$empty[this.tagName])
  9567. ) {
  9568. if (!this.children) {
  9569. this.children = [];
  9570. }
  9571. if (node.parentNode) {
  9572. node.parentNode.removeChild(node);
  9573. }
  9574. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9575. if (ci === node) {
  9576. this.children.splice(i, 1);
  9577. break;
  9578. }
  9579. }
  9580. this.children.push(node);
  9581. node.parentNode = this;
  9582. return node;
  9583. }
  9584. },
  9585. /**
  9586. * 在传入节点的前面插入一个节点
  9587. * @method insertBefore
  9588. * @param { UE.uNode } target 要插入的节点
  9589. * @param { UE.uNode } source 在该参数节点前面插入
  9590. * @return { UE.uNode } 返回刚插入的子节点
  9591. * @example
  9592. * ```javascript
  9593. * node.parentNode.insertBefore(newNode, node); //在node节点后面插入newNode
  9594. * ```
  9595. */
  9596. insertBefore: function(target, source) {
  9597. if (this.children) {
  9598. if (target.parentNode) {
  9599. target.parentNode.removeChild(target);
  9600. }
  9601. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9602. if (ci === source) {
  9603. this.children.splice(i, 0, target);
  9604. target.parentNode = this;
  9605. return target;
  9606. }
  9607. }
  9608. }
  9609. },
  9610. /**
  9611. * 在传入节点的后面插入一个节点
  9612. * @method insertAfter
  9613. * @param { UE.uNode } target 要插入的节点
  9614. * @param { UE.uNode } source 在该参数节点后面插入
  9615. * @return { UE.uNode } 返回刚插入的子节点
  9616. * @example
  9617. * ```javascript
  9618. * node.parentNode.insertAfter(newNode, node); //在node节点后面插入newNode
  9619. * ```
  9620. */
  9621. insertAfter: function(target, source) {
  9622. if (this.children) {
  9623. if (target.parentNode) {
  9624. target.parentNode.removeChild(target);
  9625. }
  9626. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9627. if (ci === source) {
  9628. this.children.splice(i + 1, 0, target);
  9629. target.parentNode = this;
  9630. return target;
  9631. }
  9632. }
  9633. }
  9634. },
  9635. /**
  9636. * 从当前节点的子节点列表中,移除节点
  9637. * @method removeChild
  9638. * @param { UE.uNode } node 要移除的节点引用
  9639. * @param { Boolean } keepChildren 是否保留移除节点的子节点,若传入true,自动把移除节点的子节点插入到移除的位置
  9640. * @return { * } 返回刚移除的子节点
  9641. * @example
  9642. * ```javascript
  9643. * node.removeChild(childNode,true); //在node的子节点列表中移除child节点,并且吧child的子节点插入到移除的位置
  9644. * ```
  9645. */
  9646. removeChild: function(node, keepChildren) {
  9647. if (this.children) {
  9648. for (var i = 0, ci; (ci = this.children[i]); i++) {
  9649. if (ci === node) {
  9650. this.children.splice(i, 1);
  9651. ci.parentNode = null;
  9652. if (keepChildren && ci.children && ci.children.length) {
  9653. for (var j = 0, cj; (cj = ci.children[j]); j++) {
  9654. this.children.splice(i + j, 0, cj);
  9655. cj.parentNode = this;
  9656. }
  9657. }
  9658. return ci;
  9659. }
  9660. }
  9661. }
  9662. },
  9663. /**
  9664. * 获取当前节点所代表的元素属性,即获取attrs对象下的属性值
  9665. * @method getAttr
  9666. * @param { String } attrName 要获取的属性名称
  9667. * @return { * } 返回attrs对象下的属性值
  9668. * @example
  9669. * ```javascript
  9670. * node.getAttr('title');
  9671. * ```
  9672. */
  9673. getAttr: function(attrName) {
  9674. return this.attrs && this.attrs[attrName.toLowerCase()];
  9675. },
  9676. /**
  9677. * 设置当前节点所代表的元素属性,即设置attrs对象下的属性值
  9678. * @method setAttr
  9679. * @param { String } attrName 要设置的属性名称
  9680. * @param { * } attrVal 要设置的属性值,类型视设置的属性而定
  9681. * @return { * } 返回attrs对象下的属性值
  9682. * @example
  9683. * ```javascript
  9684. * node.setAttr('title','标题');
  9685. * ```
  9686. */
  9687. setAttr: function(attrName, attrVal) {
  9688. if (!attrName) {
  9689. delete this.attrs;
  9690. return;
  9691. }
  9692. if (!this.attrs) {
  9693. this.attrs = {};
  9694. }
  9695. if (utils.isObject(attrName)) {
  9696. for (var a in attrName) {
  9697. if (!attrName[a]) {
  9698. delete this.attrs[a];
  9699. } else {
  9700. this.attrs[a.toLowerCase()] = attrName[a];
  9701. }
  9702. }
  9703. } else {
  9704. if (!attrVal) {
  9705. delete this.attrs[attrName];
  9706. } else {
  9707. this.attrs[attrName.toLowerCase()] = attrVal;
  9708. }
  9709. }
  9710. },
  9711. /**
  9712. * 获取当前节点在父节点下的位置索引
  9713. * @method getIndex
  9714. * @return { Number } 返回索引数值,如果没有父节点,返回-1
  9715. * @example
  9716. * ```javascript
  9717. * node.getIndex();
  9718. * ```
  9719. */
  9720. getIndex: function() {
  9721. var parent = this.parentNode;
  9722. for (var i = 0, ci; (ci = parent.children[i]); i++) {
  9723. if (ci === this) {
  9724. return i;
  9725. }
  9726. }
  9727. return -1;
  9728. },
  9729. /**
  9730. * 在当前节点下,根据id查找节点
  9731. * @method getNodeById
  9732. * @param { String } id 要查找的id
  9733. * @return { UE.uNode } 返回找到的节点
  9734. * @example
  9735. * ```javascript
  9736. * node.getNodeById('textId');
  9737. * ```
  9738. */
  9739. getNodeById: function(id) {
  9740. var node;
  9741. if (this.children && this.children.length) {
  9742. for (var i = 0, ci; (ci = this.children[i++]); ) {
  9743. if ((node = getNodeById(ci, id))) {
  9744. return node;
  9745. }
  9746. }
  9747. }
  9748. },
  9749. /**
  9750. * 在当前节点下,根据元素名称查找节点列表
  9751. * @method getNodesByTagName
  9752. * @param { String } tagNames 要查找的元素名称
  9753. * @return { Array } 返回找到的节点列表
  9754. * @example
  9755. * ```javascript
  9756. * node.getNodesByTagName('span');
  9757. * ```
  9758. */
  9759. getNodesByTagName: function(tagNames) {
  9760. tagNames = utils.trim(tagNames).replace(/[ ]{2,}/g, " ").split(" ");
  9761. var arr = [],
  9762. me = this;
  9763. utils.each(tagNames, function(tagName) {
  9764. if (me.children && me.children.length) {
  9765. for (var i = 0, ci; (ci = me.children[i++]); ) {
  9766. getNodesByTagName(ci, tagName, arr);
  9767. }
  9768. }
  9769. });
  9770. return arr;
  9771. },
  9772. /**
  9773. * 根据样式名称,获取节点的样式值
  9774. * @method getStyle
  9775. * @param { String } name 要获取的样式名称
  9776. * @return { String } 返回样式值
  9777. * @example
  9778. * ```javascript
  9779. * node.getStyle('font-size');
  9780. * ```
  9781. */
  9782. getStyle: function(name) {
  9783. var cssStyle = this.getAttr("style");
  9784. if (!cssStyle) {
  9785. return "";
  9786. }
  9787. var reg = new RegExp("(^|;)\\s*" + name + ":([^;]+)", "i");
  9788. var match = cssStyle.match(reg);
  9789. if (match && match[0]) {
  9790. return match[2];
  9791. }
  9792. return "";
  9793. },
  9794. /**
  9795. * 给节点设置样式
  9796. * @method setStyle
  9797. * @param { String } name 要设置的的样式名称
  9798. * @param { String } val 要设置的的样值
  9799. * @example
  9800. * ```javascript
  9801. * node.setStyle('font-size', '12px');
  9802. * ```
  9803. */
  9804. setStyle: function(name, val) {
  9805. function exec(name, val) {
  9806. var reg = new RegExp("(^|;)\\s*" + name + ":([^;]+;?)", "gi");
  9807. cssStyle = cssStyle.replace(reg, "$1");
  9808. if (val) {
  9809. cssStyle = name + ":" + utils.unhtml(val) + ";" + cssStyle;
  9810. }
  9811. }
  9812. var cssStyle = this.getAttr("style");
  9813. if (!cssStyle) {
  9814. cssStyle = "";
  9815. }
  9816. if (utils.isObject(name)) {
  9817. for (var a in name) {
  9818. exec(a, name[a]);
  9819. }
  9820. } else {
  9821. exec(name, val);
  9822. }
  9823. this.setAttr("style", utils.trim(cssStyle));
  9824. },
  9825. /**
  9826. * 传入一个函数,递归遍历当前节点下的所有节点
  9827. * @method traversal
  9828. * @param { Function } fn 遍历到节点的时,传入节点作为参数,运行此函数
  9829. * @example
  9830. * ```javascript
  9831. * traversal(node, function(){
  9832. * console.log(node.type);
  9833. * });
  9834. * ```
  9835. */
  9836. traversal: function(fn) {
  9837. if (this.children && this.children.length) {
  9838. nodeTraversal(this, fn);
  9839. }
  9840. return this;
  9841. }
  9842. };
  9843. })();
  9844. // core/htmlparser.js
  9845. /**
  9846. * html字符串转换成uNode节点
  9847. * @file
  9848. * @module UE
  9849. * @since 1.2.6.1
  9850. */
  9851. /**
  9852. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  9853. * @unfile
  9854. * @module UE
  9855. */
  9856. /**
  9857. * html字符串转换成uNode节点的静态方法
  9858. * @method htmlparser
  9859. * @param { String } htmlstr 要转换的html代码
  9860. * @param { Boolean } ignoreBlank 若设置为true,转换的时候忽略\n\r\t等空白字符
  9861. * @return { uNode } 给定的html片段转换形成的uNode对象
  9862. * @example
  9863. * ```javascript
  9864. * var root = UE.htmlparser('<p><b>htmlparser</b></p>', true);
  9865. * ```
  9866. */
  9867. var htmlparser = (UE.htmlparser = function(htmlstr, ignoreBlank) {
  9868. //todo 原来的方式 [^"'<>\/] 有\/就不能配对上 <TD vAlign=top background=../AAA.JPG> 这样的标签了
  9869. //先去掉了,加上的原因忘了,这里先记录
  9870. //var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\s\/<>]+)\s*((?:(?:"[^"]*")|(?:'[^']*')|[^"'<>])*)\/?>))/g,
  9871. //以上的正则表达式无法匹配:<div style="text-align:center;font-family:" font-size:14px;"=""><img src="http://hs-album.oss.aliyuncs.com/static/27/78/35/image/20161206/20161206174331_41105.gif" alt="" /><br /></div>
  9872. //修改为如下正则表达式:
  9873. var re_tag = /<(?:(?:\/([^>]+)>)|(?:!--([\S|\s]*?)-->)|(?:([^\/\s>]+)((?:\s+[\w\-:.]+(?:\s*=\s*?(?:(?:"[^"]*")|(?:'[^']*')|[^\s"'\/>]+))?)*)[\S\s]*?(\/?)>))/g,
  9874. re_attr = /([\w\-:.]+)(?:(?:\s*=\s*(?:(?:"([^"]*)")|(?:'([^']*)')|([^\s>]+)))|(?=\s|$))/g;
  9875. //ie下取得的html可能会有\n存在,要去掉,在处理replace(/[\t\r\n]*/g,'');代码高量的\n不能去除
  9876. var allowEmptyTags = {
  9877. b: 1,
  9878. code: 1,
  9879. i: 1,
  9880. u: 1,
  9881. strike: 1,
  9882. s: 1,
  9883. tt: 1,
  9884. strong: 1,
  9885. q: 1,
  9886. samp: 1,
  9887. em: 1,
  9888. span: 1,
  9889. sub: 1,
  9890. img: 1,
  9891. sup: 1,
  9892. font: 1,
  9893. big: 1,
  9894. small: 1,
  9895. iframe: 1,
  9896. a: 1,
  9897. br: 1,
  9898. pre: 1
  9899. };
  9900. htmlstr = htmlstr.replace(new RegExp(domUtils.fillChar, "g"), "");
  9901. if (!ignoreBlank) {
  9902. htmlstr = htmlstr.replace(
  9903. new RegExp(
  9904. "[\\r\\t\\n" +
  9905. (ignoreBlank ? "" : " ") +
  9906. "]*</?(\\w+)\\s*(?:[^>]*)>[\\r\\t\\n" +
  9907. (ignoreBlank ? "" : " ") +
  9908. "]*",
  9909. "g"
  9910. ),
  9911. function(a, b) {
  9912. //br暂时单独处理
  9913. if (b && allowEmptyTags[b.toLowerCase()]) {
  9914. return a.replace(/(^[\n\r]+)|([\n\r]+$)/g, "");
  9915. }
  9916. return a
  9917. .replace(new RegExp("^[\\r\\n" + (ignoreBlank ? "" : " ") + "]+"), "")
  9918. .replace(
  9919. new RegExp("[\\r\\n" + (ignoreBlank ? "" : " ") + "]+$"),
  9920. ""
  9921. );
  9922. }
  9923. );
  9924. }
  9925. var notTransAttrs = {
  9926. href: 1,
  9927. src: 1
  9928. };
  9929. var uNode = UE.uNode,
  9930. needParentNode = {
  9931. td: "tr",
  9932. tr: ["tbody", "thead", "tfoot"],
  9933. tbody: "table",
  9934. th: "tr",
  9935. thead: "table",
  9936. tfoot: "table",
  9937. caption: "table",
  9938. li: ["ul", "ol"],
  9939. dt: "dl",
  9940. dd: "dl",
  9941. option: "select"
  9942. },
  9943. needChild = {
  9944. ol: "li",
  9945. ul: "li"
  9946. };
  9947. function text(parent, data) {
  9948. if (needChild[parent.tagName]) {
  9949. var tmpNode = uNode.createElement(needChild[parent.tagName]);
  9950. parent.appendChild(tmpNode);
  9951. tmpNode.appendChild(uNode.createText(data));
  9952. parent = tmpNode;
  9953. } else {
  9954. parent.appendChild(uNode.createText(data));
  9955. }
  9956. }
  9957. function element(parent, tagName, htmlattr) {
  9958. var needParentTag;
  9959. if ((needParentTag = needParentNode[tagName])) {
  9960. var tmpParent = parent,
  9961. hasParent;
  9962. while (tmpParent.type != "root") {
  9963. if (
  9964. utils.isArray(needParentTag)
  9965. ? utils.indexOf(needParentTag, tmpParent.tagName) != -1
  9966. : needParentTag == tmpParent.tagName
  9967. ) {
  9968. parent = tmpParent;
  9969. hasParent = true;
  9970. break;
  9971. }
  9972. tmpParent = tmpParent.parentNode;
  9973. }
  9974. if (!hasParent) {
  9975. parent = element(
  9976. parent,
  9977. utils.isArray(needParentTag) ? needParentTag[0] : needParentTag
  9978. );
  9979. }
  9980. }
  9981. //按dtd处理嵌套
  9982. // if(parent.type != 'root' && !dtd[parent.tagName][tagName])
  9983. // parent = parent.parentNode;
  9984. var elm = new uNode({
  9985. parentNode: parent,
  9986. type: "element",
  9987. tagName: tagName.toLowerCase(),
  9988. //是自闭合的处理一下
  9989. children: dtd.$empty[tagName] ? null : []
  9990. });
  9991. //如果属性存在,处理属性
  9992. if (htmlattr) {
  9993. var attrs = {},
  9994. match;
  9995. while ((match = re_attr.exec(htmlattr))) {
  9996. attrs[match[1].toLowerCase()] = notTransAttrs[match[1].toLowerCase()]
  9997. ? match[2] || match[3] || match[4]
  9998. : utils.unhtml(match[2] || match[3] || match[4]);
  9999. }
  10000. elm.attrs = attrs;
  10001. }
  10002. //trace:3970
  10003. // //如果parent下不能放elm
  10004. // if(dtd.$inline[parent.tagName] && dtd.$block[elm.tagName] && !dtd[parent.tagName][elm.tagName]){
  10005. // parent = parent.parentNode;
  10006. // elm.parentNode = parent;
  10007. // }
  10008. parent.children.push(elm);
  10009. //如果是自闭合节点返回父亲节点
  10010. return dtd.$empty[tagName] ? parent : elm;
  10011. }
  10012. function comment(parent, data) {
  10013. parent.children.push(
  10014. new uNode({
  10015. type: "comment",
  10016. data: data,
  10017. parentNode: parent
  10018. })
  10019. );
  10020. }
  10021. var match,
  10022. currentIndex = 0,
  10023. nextIndex = 0;
  10024. //设置根节点
  10025. var root = new uNode({
  10026. type: "root",
  10027. children: []
  10028. });
  10029. var currentParent = root;
  10030. while ((match = re_tag.exec(htmlstr))) {
  10031. currentIndex = match.index;
  10032. try {
  10033. if (currentIndex > nextIndex) {
  10034. //text node
  10035. text(currentParent, htmlstr.slice(nextIndex, currentIndex));
  10036. }
  10037. if (match[3]) {
  10038. if (dtd.$cdata[currentParent.tagName]) {
  10039. text(currentParent, match[0]);
  10040. } else {
  10041. //start tag
  10042. currentParent = element(
  10043. currentParent,
  10044. match[3].toLowerCase(),
  10045. match[4]
  10046. );
  10047. }
  10048. } else if (match[1]) {
  10049. if (currentParent.type != "root") {
  10050. if (dtd.$cdata[currentParent.tagName] && !dtd.$cdata[match[1]]) {
  10051. text(currentParent, match[0]);
  10052. } else {
  10053. var tmpParent = currentParent;
  10054. while (
  10055. currentParent.type == "element" &&
  10056. currentParent.tagName != match[1].toLowerCase()
  10057. ) {
  10058. currentParent = currentParent.parentNode;
  10059. if (currentParent.type == "root") {
  10060. currentParent = tmpParent;
  10061. throw "break";
  10062. }
  10063. }
  10064. //end tag
  10065. currentParent = currentParent.parentNode;
  10066. }
  10067. }
  10068. } else if (match[2]) {
  10069. //comment
  10070. comment(currentParent, match[2]);
  10071. }
  10072. } catch (e) {}
  10073. nextIndex = re_tag.lastIndex;
  10074. }
  10075. //如果结束是文本,就有可能丢掉,所以这里手动判断一下
  10076. //例如 <li>sdfsdfsdf<li>sdfsdfsdfsdf
  10077. if (nextIndex < htmlstr.length) {
  10078. text(currentParent, htmlstr.slice(nextIndex));
  10079. }
  10080. return root;
  10081. });
  10082. // core/filternode.js
  10083. /**
  10084. * UE过滤节点的静态方法
  10085. * @file
  10086. */
  10087. /**
  10088. * UEditor公用空间,UEditor所有的功能都挂载在该空间下
  10089. * @module UE
  10090. */
  10091. /**
  10092. * 根据传入节点和过滤规则过滤相应节点
  10093. * @module UE
  10094. * @since 1.2.6.1
  10095. * @method filterNode
  10096. * @param { Object } root 指定root节点
  10097. * @param { Object } rules 过滤规则json对象
  10098. * @example
  10099. * ```javascript
  10100. * UE.filterNode(root,editor.options.filterRules);
  10101. * ```
  10102. */
  10103. var filterNode = (UE.filterNode = (function() {
  10104. function filterNode(node, rules) {
  10105. switch (node.type) {
  10106. case "text":
  10107. break;
  10108. case "element":
  10109. var val;
  10110. if ((val = rules[node.tagName])) {
  10111. if (val === "-") {
  10112. node.parentNode.removeChild(node);
  10113. } else if (utils.isFunction(val)) {
  10114. var parentNode = node.parentNode,
  10115. index = node.getIndex();
  10116. val(node);
  10117. if (node.parentNode) {
  10118. if (node.children) {
  10119. for (var i = 0, ci; (ci = node.children[i]); ) {
  10120. filterNode(ci, rules);
  10121. if (ci.parentNode) {
  10122. i++;
  10123. }
  10124. }
  10125. }
  10126. } else {
  10127. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10128. filterNode(ci, rules);
  10129. if (ci.parentNode) {
  10130. i++;
  10131. }
  10132. }
  10133. }
  10134. } else {
  10135. var attrs = val["$"];
  10136. if (attrs && node.attrs) {
  10137. var tmpAttrs = {},
  10138. tmpVal;
  10139. for (var a in attrs) {
  10140. tmpVal = node.getAttr(a);
  10141. //todo 只先对style单独处理
  10142. if (a == "style" && utils.isArray(attrs[a])) {
  10143. var tmpCssStyle = [];
  10144. utils.each(attrs[a], function(v) {
  10145. var tmp;
  10146. if ((tmp = node.getStyle(v))) {
  10147. tmpCssStyle.push(v + ":" + tmp);
  10148. }
  10149. });
  10150. tmpVal = tmpCssStyle.join(";");
  10151. }
  10152. if (tmpVal) {
  10153. tmpAttrs[a] = tmpVal;
  10154. }
  10155. }
  10156. node.attrs = tmpAttrs;
  10157. }
  10158. if (node.children) {
  10159. for (var i = 0, ci; (ci = node.children[i]); ) {
  10160. filterNode(ci, rules);
  10161. if (ci.parentNode) {
  10162. i++;
  10163. }
  10164. }
  10165. }
  10166. }
  10167. } else {
  10168. //如果不在名单里扣出子节点并删除该节点,cdata除外
  10169. if (dtd.$cdata[node.tagName]) {
  10170. node.parentNode.removeChild(node);
  10171. } else {
  10172. var parentNode = node.parentNode,
  10173. index = node.getIndex();
  10174. node.parentNode.removeChild(node, true);
  10175. for (var i = index, ci; (ci = parentNode.children[i]); ) {
  10176. filterNode(ci, rules);
  10177. if (ci.parentNode) {
  10178. i++;
  10179. }
  10180. }
  10181. }
  10182. }
  10183. break;
  10184. case "comment":
  10185. node.parentNode.removeChild(node);
  10186. }
  10187. }
  10188. return function(root, rules) {
  10189. if (utils.isEmptyObject(rules)) {
  10190. return root;
  10191. }
  10192. var val;
  10193. if ((val = rules["-"])) {
  10194. utils.each(val.split(" "), function(k) {
  10195. rules[k] = "-";
  10196. });
  10197. }
  10198. for (var i = 0, ci; (ci = root.children[i]); ) {
  10199. filterNode(ci, rules);
  10200. if (ci.parentNode) {
  10201. i++;
  10202. }
  10203. }
  10204. return root;
  10205. };
  10206. })());
  10207. // core/plugin.js
  10208. /**
  10209. * Created with JetBrains PhpStorm.
  10210. * User: campaign
  10211. * Date: 10/8/13
  10212. * Time: 6:15 PM
  10213. * To change this template use File | Settings | File Templates.
  10214. */
  10215. UE.plugin = (function() {
  10216. var _plugins = {};
  10217. return {
  10218. register: function(pluginName, fn, oldOptionName, afterDisabled) {
  10219. if (oldOptionName && utils.isFunction(oldOptionName)) {
  10220. afterDisabled = oldOptionName;
  10221. oldOptionName = null;
  10222. }
  10223. _plugins[pluginName] = {
  10224. optionName: oldOptionName || pluginName,
  10225. execFn: fn,
  10226. //当插件被禁用时执行
  10227. afterDisabled: afterDisabled
  10228. };
  10229. },
  10230. load: function(editor) {
  10231. utils.each(_plugins, function(plugin) {
  10232. var _export = plugin.execFn.call(editor);
  10233. if (editor.options[plugin.optionName] !== false) {
  10234. if (_export) {
  10235. //后边需要再做扩展
  10236. utils.each(_export, function(v, k) {
  10237. switch (k.toLowerCase()) {
  10238. case "shortcutkey":
  10239. editor.addshortcutkey(v);
  10240. break;
  10241. case "bindevents":
  10242. utils.each(v, function(fn, eventName) {
  10243. editor.addListener(eventName, fn);
  10244. });
  10245. break;
  10246. case "bindmultievents":
  10247. utils.each(utils.isArray(v) ? v : [v], function(event) {
  10248. var types = utils.trim(event.type).split(/\s+/);
  10249. utils.each(types, function(eventName) {
  10250. editor.addListener(eventName, event.handler);
  10251. });
  10252. });
  10253. break;
  10254. case "commands":
  10255. utils.each(v, function(execFn, execName) {
  10256. editor.commands[execName] = execFn;
  10257. });
  10258. break;
  10259. case "outputrule":
  10260. editor.addOutputRule(v);
  10261. break;
  10262. case "inputrule":
  10263. editor.addInputRule(v);
  10264. break;
  10265. case "defaultoptions":
  10266. editor.setOpt(v);
  10267. }
  10268. });
  10269. }
  10270. } else if (plugin.afterDisabled) {
  10271. plugin.afterDisabled.call(editor);
  10272. }
  10273. });
  10274. //向下兼容
  10275. utils.each(UE.plugins, function(plugin) {
  10276. plugin.call(editor);
  10277. });
  10278. },
  10279. run: function(pluginName, editor) {
  10280. var plugin = _plugins[pluginName];
  10281. if (plugin) {
  10282. plugin.exeFn.call(editor);
  10283. }
  10284. }
  10285. };
  10286. })();
  10287. // core/keymap.js
  10288. var keymap = (UE.keymap = {
  10289. Backspace: 8,
  10290. Tab: 9,
  10291. Enter: 13,
  10292. Shift: 16,
  10293. Control: 17,
  10294. Alt: 18,
  10295. CapsLock: 20,
  10296. Esc: 27,
  10297. Spacebar: 32,
  10298. PageUp: 33,
  10299. PageDown: 34,
  10300. End: 35,
  10301. Home: 36,
  10302. Left: 37,
  10303. Up: 38,
  10304. Right: 39,
  10305. Down: 40,
  10306. Insert: 45,
  10307. Del: 46,
  10308. NumLock: 144,
  10309. Cmd: 91,
  10310. "=": 187,
  10311. "-": 189,
  10312. b: 66,
  10313. i: 73,
  10314. //回退
  10315. z: 90,
  10316. y: 89,
  10317. //粘贴
  10318. v: 86,
  10319. x: 88,
  10320. s: 83,
  10321. n: 78
  10322. });
  10323. // core/localstorage.js
  10324. var LocalStorage = (UE.LocalStorage = (function () {
  10325. var storage = window.localStorage
  10326. return {
  10327. saveLocalData: function (key, data) {
  10328. // console.log('saveLocalData', key, data);
  10329. if (!storage) {
  10330. return false;
  10331. }
  10332. storage.setItem(key, data);
  10333. return true;
  10334. },
  10335. getLocalData: function (key) {
  10336. // console.log('getLocalData', key);
  10337. if (!storage) {
  10338. return null;
  10339. }
  10340. return storage.getItem(key) || null;
  10341. },
  10342. removeItem: function (key) {
  10343. // console.log('removeItem', key);
  10344. storage && storage.removeItem(key);
  10345. }
  10346. };
  10347. })());
  10348. (function () {
  10349. var ROOT_KEY = "UEditorPlusPref";
  10350. UE.Editor.prototype.setPreferences = function (key, value) {
  10351. // console.log('setPreferences', key, value);
  10352. var obj = {};
  10353. if (utils.isString(key)) {
  10354. obj[key] = value;
  10355. } else {
  10356. obj = key;
  10357. }
  10358. var data = LocalStorage.getLocalData(ROOT_KEY);
  10359. if (data && (data = utils.str2json(data))) {
  10360. utils.extend(data, obj);
  10361. } else {
  10362. data = obj;
  10363. }
  10364. data && LocalStorage.saveLocalData(ROOT_KEY, utils.json2str(data));
  10365. };
  10366. UE.Editor.prototype.getPreferences = function (key) {
  10367. // console.log('getPreferences', key);
  10368. var data = LocalStorage.getLocalData(ROOT_KEY);
  10369. if (data && (data = utils.str2json(data))) {
  10370. return key ? data[key] : data;
  10371. }
  10372. return null;
  10373. };
  10374. UE.Editor.prototype.removePreferences = function (key) {
  10375. // console.log('removePreferences', key);
  10376. var data = LocalStorage.getLocalData(ROOT_KEY);
  10377. if (data && (data = utils.str2json(data))) {
  10378. data[key] = undefined;
  10379. delete data[key];
  10380. }
  10381. data && LocalStorage.saveLocalData(ROOT_KEY, utils.json2str(data));
  10382. };
  10383. })();
  10384. // plugins/defaultfilter.js
  10385. ///import core
  10386. ///plugin 编辑器默认的过滤转换机制
  10387. UE.plugins["defaultfilter"] = function() {
  10388. var me = this;
  10389. me.setOpt({
  10390. allowDivTransToP: true,
  10391. disabledTableInTable: true,
  10392. rgb2Hex: true
  10393. });
  10394. //默认的过滤处理
  10395. //进入编辑器的内容处理
  10396. me.addInputRule(function(root) {
  10397. var allowDivTransToP = this.options.allowDivTransToP;
  10398. var val;
  10399. function tdParent(node) {
  10400. while (node && node.type == "element") {
  10401. if (node.tagName == "td") {
  10402. return true;
  10403. }
  10404. node = node.parentNode;
  10405. }
  10406. return false;
  10407. }
  10408. //进行默认的处理
  10409. root.traversal(function(node) {
  10410. if (node.type == "element") {
  10411. if (
  10412. !dtd.$cdata[node.tagName] &&
  10413. me.options.autoClearEmptyNode &&
  10414. dtd.$inline[node.tagName] &&
  10415. !dtd.$empty[node.tagName] &&
  10416. (!node.attrs || utils.isEmptyObject(node.attrs))
  10417. ) {
  10418. if (!node.firstChild()) node.parentNode.removeChild(node);
  10419. else if (
  10420. node.tagName == "span" &&
  10421. (!node.attrs || utils.isEmptyObject(node.attrs))
  10422. ) {
  10423. node.parentNode.removeChild(node, true);
  10424. }
  10425. return;
  10426. }
  10427. switch (node.tagName) {
  10428. case "style":
  10429. case "script":
  10430. node.setAttr({
  10431. cdata_tag: node.tagName,
  10432. cdata_data: node.innerHTML() || "",
  10433. _ue_custom_node_: "true"
  10434. });
  10435. node.tagName = "div";
  10436. node.innerHTML("");
  10437. break;
  10438. case "a":
  10439. if ((val = node.getAttr("href"))) {
  10440. node.setAttr("_href", val);
  10441. }
  10442. break;
  10443. case "img":
  10444. //todo base64暂时去掉,后边做远程图片上传后,干掉这个
  10445. if ((val = node.getAttr("src"))) {
  10446. if (/^data:/.test(val)) {
  10447. node.parentNode.removeChild(node);
  10448. break;
  10449. }
  10450. }
  10451. node.setAttr("_src", node.getAttr("src"));
  10452. break;
  10453. case "span":
  10454. if (browser.webkit && (val = node.getStyle("white-space"))) {
  10455. if (/nowrap|normal/.test(val)) {
  10456. node.setStyle("white-space", "");
  10457. if (
  10458. me.options.autoClearEmptyNode &&
  10459. utils.isEmptyObject(node.attrs)
  10460. ) {
  10461. node.parentNode.removeChild(node, true);
  10462. }
  10463. }
  10464. }
  10465. val = node.getAttr("id");
  10466. if (val && /^_baidu_bookmark_/i.test(val)) {
  10467. node.parentNode.removeChild(node);
  10468. }
  10469. break;
  10470. case "p":
  10471. if ((val = node.getAttr("align"))) {
  10472. node.setAttr("align");
  10473. node.setStyle("text-align", val);
  10474. }
  10475. //trace:3431
  10476. // var cssStyle = node.getAttr('style');
  10477. // if (cssStyle) {
  10478. // cssStyle = cssStyle.replace(/(margin|padding)[^;]+/g, '');
  10479. // node.setAttr('style', cssStyle)
  10480. //
  10481. // }
  10482. //p标签不允许嵌套
  10483. utils.each(node.children, function(n) {
  10484. if (n.type == "element" && n.tagName == "p") {
  10485. var next = n.nextSibling();
  10486. node.parentNode.insertAfter(n, node);
  10487. var last = n;
  10488. while (next) {
  10489. var tmp = next.nextSibling();
  10490. node.parentNode.insertAfter(next, last);
  10491. last = next;
  10492. next = tmp;
  10493. }
  10494. return false;
  10495. }
  10496. });
  10497. if (!node.firstChild()) {
  10498. node.innerHTML(browser.ie ? "&nbsp;" : "<br/>");
  10499. }
  10500. break;
  10501. case "div":
  10502. if (node.getAttr("cdata_tag")) {
  10503. break;
  10504. }
  10505. //针对代码这里不处理插入代码的div
  10506. val = node.getAttr("class");
  10507. if (val && /^line number\d+/.test(val)) {
  10508. break;
  10509. }
  10510. if (!allowDivTransToP) {
  10511. break;
  10512. }
  10513. var tmpNode,
  10514. p = UE.uNode.createElement("p");
  10515. while ((tmpNode = node.firstChild())) {
  10516. if (
  10517. tmpNode.type == "text" ||
  10518. !UE.dom.dtd.$block[tmpNode.tagName]
  10519. ) {
  10520. p.appendChild(tmpNode);
  10521. } else {
  10522. if (p.firstChild()) {
  10523. node.parentNode.insertBefore(p, node);
  10524. p = UE.uNode.createElement("p");
  10525. } else {
  10526. node.parentNode.insertBefore(tmpNode, node);
  10527. }
  10528. }
  10529. }
  10530. if (p.firstChild()) {
  10531. node.parentNode.insertBefore(p, node);
  10532. }
  10533. node.parentNode.removeChild(node);
  10534. break;
  10535. case "dl":
  10536. node.tagName = "ul";
  10537. break;
  10538. case "dt":
  10539. case "dd":
  10540. node.tagName = "li";
  10541. break;
  10542. case "li":
  10543. var className = node.getAttr("class");
  10544. if (!className || !/list\-/.test(className)) {
  10545. node.setAttr();
  10546. }
  10547. var tmpNodes = node.getNodesByTagName("ol ul");
  10548. UE.utils.each(tmpNodes, function(n) {
  10549. node.parentNode.insertAfter(n, node);
  10550. });
  10551. break;
  10552. case "td":
  10553. case "th":
  10554. case "caption":
  10555. if (!node.children || !node.children.length) {
  10556. node.appendChild(
  10557. browser.ie11below
  10558. ? UE.uNode.createText(" ")
  10559. : UE.uNode.createElement("br")
  10560. );
  10561. }
  10562. break;
  10563. case "table":
  10564. if (me.options.disabledTableInTable && tdParent(node)) {
  10565. node.parentNode.insertBefore(
  10566. UE.uNode.createText(node.innerText()),
  10567. node
  10568. );
  10569. node.parentNode.removeChild(node);
  10570. }
  10571. }
  10572. }
  10573. // if(node.type == 'comment'){
  10574. // node.parentNode.removeChild(node);
  10575. // }
  10576. });
  10577. });
  10578. //从编辑器出去的内容处理
  10579. me.addOutputRule(function(root) {
  10580. var val;
  10581. root.traversal(function(node) {
  10582. if (node.type == "element") {
  10583. if (
  10584. me.options.autoClearEmptyNode &&
  10585. dtd.$inline[node.tagName] &&
  10586. !dtd.$empty[node.tagName] &&
  10587. (!node.attrs || utils.isEmptyObject(node.attrs))
  10588. ) {
  10589. if (!node.firstChild()) node.parentNode.removeChild(node);
  10590. else if (
  10591. node.tagName == "span" &&
  10592. (!node.attrs || utils.isEmptyObject(node.attrs))
  10593. ) {
  10594. node.parentNode.removeChild(node, true);
  10595. }
  10596. return;
  10597. }
  10598. switch (node.tagName) {
  10599. case "div":
  10600. if ((val = node.getAttr("cdata_tag"))) {
  10601. node.tagName = val;
  10602. node.appendChild(UE.uNode.createText(node.getAttr("cdata_data")));
  10603. node.setAttr({
  10604. cdata_tag: "",
  10605. cdata_data: "",
  10606. _ue_custom_node_: ""
  10607. });
  10608. }
  10609. break;
  10610. case "a":
  10611. if ((val = node.getAttr("_href"))) {
  10612. node.setAttr({
  10613. href: utils.html(val),
  10614. _href: ""
  10615. });
  10616. }
  10617. break;
  10618. break;
  10619. case "span":
  10620. val = node.getAttr("id");
  10621. if (val && /^_baidu_bookmark_/i.test(val)) {
  10622. node.parentNode.removeChild(node);
  10623. }
  10624. //将color的rgb格式转换为#16进制格式
  10625. if (me.getOpt("rgb2Hex")) {
  10626. var cssStyle = node.getAttr("style");
  10627. if (cssStyle) {
  10628. node.setAttr(
  10629. "style",
  10630. cssStyle.replace(/rgba?\(([\d,\s]+)\)/g, function(a, value) {
  10631. var array = value.split(",");
  10632. if (array.length > 3) return "";
  10633. value = "#";
  10634. for (var i = 0, color; (color = array[i++]); ) {
  10635. color = parseInt(
  10636. color.replace(/[^\d]/gi, ""),
  10637. 10
  10638. ).toString(16);
  10639. value += color.length == 1 ? "0" + color : color;
  10640. }
  10641. return value.toUpperCase();
  10642. })
  10643. );
  10644. }
  10645. }
  10646. break;
  10647. case "img":
  10648. if ((val = node.getAttr("_src"))) {
  10649. node.setAttr({
  10650. src: node.getAttr("_src"),
  10651. _src: ""
  10652. });
  10653. }
  10654. }
  10655. }
  10656. });
  10657. });
  10658. };
  10659. // plugins/inserthtml.js
  10660. /**
  10661. * 插入html字符串插件
  10662. * @file
  10663. * @since 1.2.6.1
  10664. */
  10665. /**
  10666. * 插入html代码
  10667. * @command inserthtml
  10668. * @method execCommand
  10669. * @param { String } cmd 命令字符串
  10670. * @param { String } html 插入的html字符串
  10671. * @remaind 插入的标签内容是在当前的选区位置上插入,如果当前是闭合状态,那直接插入内容, 如果当前是选中状态,将先清除当前选中内容后,再做插入
  10672. * @warning 注意:该命令会对当前选区的位置,对插入的内容进行过滤转换处理。 过滤的规则遵循html语意化的原则。
  10673. * @example
  10674. * ```javascript
  10675. * //xxx[BB]xxx 当前选区为非闭合选区,选中BB这两个文本
  10676. * //执行命令,插入<b>CC</b>
  10677. * //插入后的效果 xxx<b>CC</b>xxx
  10678. * //<p>xx|xxx</p> 当前选区为闭合状态
  10679. * //插入<p>CC</p>
  10680. * //结果 <p>xx</p><p>CC</p><p>xxx</p>
  10681. * //<p>xxxx</p>|</p>xxx</p> 当前选区在两个p标签之间
  10682. * //插入 xxxx
  10683. * //结果 <p>xxxx</p><p>xxxx</p></p>xxx</p>
  10684. * ```
  10685. */
  10686. UE.commands["inserthtml"] = {
  10687. execCommand: function(command, html, notNeedFilter) {
  10688. var me = this,
  10689. range,
  10690. div;
  10691. if (!html) {
  10692. return;
  10693. }
  10694. if (me.fireEvent("beforeinserthtml", html) === true) {
  10695. return;
  10696. }
  10697. range = me.selection.getRange();
  10698. div = range.document.createElement("div");
  10699. div.style.display = "inline";
  10700. if (!notNeedFilter) {
  10701. var root = UE.htmlparser(html);
  10702. //如果给了过滤规则就先进行过滤
  10703. if (me.options.filterRules) {
  10704. UE.filterNode(root, me.options.filterRules);
  10705. }
  10706. //执行默认的处理
  10707. me.filterInputRule(root);
  10708. html = root.toHtml();
  10709. }
  10710. div.innerHTML = utils.trim(html);
  10711. if (!range.collapsed) {
  10712. var tmpNode = range.startContainer;
  10713. if (domUtils.isFillChar(tmpNode)) {
  10714. range.setStartBefore(tmpNode);
  10715. }
  10716. tmpNode = range.endContainer;
  10717. if (domUtils.isFillChar(tmpNode)) {
  10718. range.setEndAfter(tmpNode);
  10719. }
  10720. range.txtToElmBoundary();
  10721. //结束边界可能放到了br的前边,要把br包含进来
  10722. // x[xxx]<br/>
  10723. if (range.endContainer && range.endContainer.nodeType == 1) {
  10724. tmpNode = range.endContainer.childNodes[range.endOffset];
  10725. if (tmpNode && domUtils.isBr(tmpNode)) {
  10726. range.setEndAfter(tmpNode);
  10727. }
  10728. }
  10729. if (range.startOffset == 0) {
  10730. tmpNode = range.startContainer;
  10731. if (domUtils.isBoundaryNode(tmpNode, "firstChild")) {
  10732. tmpNode = range.endContainer;
  10733. if (
  10734. range.endOffset ==
  10735. (tmpNode.nodeType == 3
  10736. ? tmpNode.nodeValue.length
  10737. : tmpNode.childNodes.length) &&
  10738. domUtils.isBoundaryNode(tmpNode, "lastChild")
  10739. ) {
  10740. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  10741. range.setStart(me.body.firstChild, 0).collapse(true);
  10742. }
  10743. }
  10744. }
  10745. !range.collapsed && range.deleteContents();
  10746. if (range.startContainer.nodeType == 1) {
  10747. var child = range.startContainer.childNodes[range.startOffset],
  10748. pre;
  10749. if (
  10750. child &&
  10751. domUtils.isBlockElm(child) &&
  10752. (pre = child.previousSibling) &&
  10753. domUtils.isBlockElm(pre)
  10754. ) {
  10755. range.setEnd(pre, pre.childNodes.length).collapse();
  10756. while (child.firstChild) {
  10757. pre.appendChild(child.firstChild);
  10758. }
  10759. domUtils.remove(child);
  10760. }
  10761. }
  10762. }
  10763. var child,
  10764. parent,
  10765. pre,
  10766. tmp,
  10767. hadBreak = 0,
  10768. nextNode;
  10769. //如果当前位置选中了fillchar要干掉,要不会产生空行
  10770. if (range.inFillChar()) {
  10771. child = range.startContainer;
  10772. if (domUtils.isFillChar(child)) {
  10773. range.setStartBefore(child).collapse(true);
  10774. domUtils.remove(child);
  10775. } else if (domUtils.isFillChar(child, true)) {
  10776. child.nodeValue = child.nodeValue.replace(fillCharReg, "");
  10777. range.startOffset--;
  10778. range.collapsed && range.collapse(true);
  10779. }
  10780. }
  10781. //列表单独处理
  10782. var li = domUtils.findParentByTagName(range.startContainer, "li", true);
  10783. if (li) {
  10784. var next, last;
  10785. while ((child = div.firstChild)) {
  10786. //针对hr单独处理一下先
  10787. while (
  10788. child &&
  10789. (child.nodeType == 3 ||
  10790. !domUtils.isBlockElm(child) ||
  10791. child.tagName == "HR")
  10792. ) {
  10793. next = child.nextSibling;
  10794. range.insertNode(child).collapse();
  10795. last = child;
  10796. child = next;
  10797. }
  10798. if (child) {
  10799. if (/^(ol|ul)$/i.test(child.tagName)) {
  10800. while (child.firstChild) {
  10801. last = child.firstChild;
  10802. domUtils.insertAfter(li, child.firstChild);
  10803. li = li.nextSibling;
  10804. }
  10805. domUtils.remove(child);
  10806. } else {
  10807. var tmpLi;
  10808. next = child.nextSibling;
  10809. tmpLi = me.document.createElement("li");
  10810. domUtils.insertAfter(li, tmpLi);
  10811. tmpLi.appendChild(child);
  10812. last = child;
  10813. child = next;
  10814. li = tmpLi;
  10815. }
  10816. }
  10817. }
  10818. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  10819. if (domUtils.isEmptyBlock(li)) {
  10820. domUtils.remove(li);
  10821. }
  10822. if (last) {
  10823. range.setStartAfter(last).collapse(true).select(true);
  10824. }
  10825. } else {
  10826. while ((child = div.firstChild)) {
  10827. if (hadBreak) {
  10828. var p = me.document.createElement("p");
  10829. while (child && (child.nodeType == 3 || !dtd.$block[child.tagName])) {
  10830. nextNode = child.nextSibling;
  10831. p.appendChild(child);
  10832. child = nextNode;
  10833. }
  10834. if (p.firstChild) {
  10835. child = p;
  10836. }
  10837. }
  10838. range.insertNode(child);
  10839. nextNode = child.nextSibling;
  10840. if (
  10841. !hadBreak &&
  10842. child.nodeType == domUtils.NODE_ELEMENT &&
  10843. domUtils.isBlockElm(child)
  10844. ) {
  10845. parent = domUtils.findParent(child, function(node) {
  10846. return domUtils.isBlockElm(node);
  10847. });
  10848. if (
  10849. parent &&
  10850. parent.tagName.toLowerCase() != "body" &&
  10851. !(
  10852. dtd[parent.tagName][child.nodeName] && child.parentNode === parent
  10853. )
  10854. ) {
  10855. if (!dtd[parent.tagName][child.nodeName]) {
  10856. pre = parent;
  10857. } else {
  10858. tmp = child.parentNode;
  10859. while (tmp !== parent) {
  10860. pre = tmp;
  10861. tmp = tmp.parentNode;
  10862. }
  10863. }
  10864. domUtils.breakParent(child, pre || tmp);
  10865. //去掉break后前一个多余的节点 <p>|<[p> ==> <p></p><div></div><p>|</p>
  10866. var pre = child.previousSibling;
  10867. domUtils.trimWhiteTextNode(pre);
  10868. if (!pre.childNodes.length) {
  10869. domUtils.remove(pre);
  10870. }
  10871. //trace:2012,在非ie的情况,切开后剩下的节点有可能不能点入光标添加br占位
  10872. if (
  10873. !browser.ie &&
  10874. (next = child.nextSibling) &&
  10875. domUtils.isBlockElm(next) &&
  10876. next.lastChild &&
  10877. !domUtils.isBr(next.lastChild)
  10878. ) {
  10879. next.appendChild(me.document.createElement("br"));
  10880. }
  10881. hadBreak = 1;
  10882. }
  10883. }
  10884. var next = child.nextSibling;
  10885. if (!div.firstChild && next && domUtils.isBlockElm(next)) {
  10886. range.setStart(next, 0).collapse(true);
  10887. break;
  10888. }
  10889. range.setEndAfter(child).collapse();
  10890. }
  10891. child = range.startContainer;
  10892. if (nextNode && domUtils.isBr(nextNode)) {
  10893. domUtils.remove(nextNode);
  10894. }
  10895. //用chrome可能有空白展位符
  10896. if (domUtils.isBlockElm(child) && domUtils.isEmptyNode(child)) {
  10897. if ((nextNode = child.nextSibling)) {
  10898. domUtils.remove(child);
  10899. if (nextNode.nodeType == 1 && dtd.$block[nextNode.tagName]) {
  10900. range.setStart(nextNode, 0).collapse(true).shrinkBoundary();
  10901. }
  10902. } else {
  10903. try {
  10904. child.innerHTML = browser.ie ? domUtils.fillChar : "<br/>";
  10905. } catch (e) {
  10906. range.setStartBefore(child);
  10907. domUtils.remove(child);
  10908. }
  10909. }
  10910. }
  10911. //加上true因为在删除表情等时会删两次,第一次是删的fillData
  10912. try {
  10913. range.select(true);
  10914. } catch (e) {}
  10915. }
  10916. setTimeout(function() {
  10917. range = me.selection.getRange();
  10918. range.scrollToView(
  10919. me.autoHeightEnabled,
  10920. me.autoHeightEnabled ? domUtils.getXY(me.iframe).y : 0
  10921. );
  10922. me.fireEvent("afterinserthtml", html);
  10923. }, 200);
  10924. }
  10925. };
  10926. // plugins/autotypeset.js
  10927. /**
  10928. * 自动排版
  10929. * @file
  10930. * @since 1.2.6.1
  10931. */
  10932. /**
  10933. * 对当前编辑器的内容执行自动排版, 排版的行为根据config配置文件里的“autotypeset”选项进行控制。
  10934. * @command autotypeset
  10935. * @method execCommand
  10936. * @param { String } cmd 命令字符串
  10937. * @example
  10938. * ```javascript
  10939. * editor.execCommand( 'autotypeset' );
  10940. * ```
  10941. */
  10942. UE.plugins["autotypeset"] = function() {
  10943. this.setOpt({
  10944. // 自动排版参数
  10945. autotypeset: {
  10946. // 合并空行
  10947. mergeEmptyline: true,
  10948. // 去掉冗余的class
  10949. removeClass: true,
  10950. // 去掉空行
  10951. removeEmptyline: false,
  10952. // 段落的排版方式,可以是 left,right,center,justify 去掉这个属性表示不执行排版
  10953. textAlign: "left",
  10954. // 图片的浮动方式,独占一行剧中,左右浮动,默认: center,left,right,none 去掉这个属性表示不执行排版
  10955. imageBlockLine: "center",
  10956. // 根据规则过滤没事粘贴进来的内容
  10957. pasteFilter: false,
  10958. // 去掉所有的内嵌字号,使用编辑器默认的字号
  10959. clearFontSize: false,
  10960. // 去掉所有的内嵌字体,使用编辑器默认的字体
  10961. clearFontFamily: false,
  10962. // 去掉空节点
  10963. removeEmptyNode: false,
  10964. // 可以去掉的标签
  10965. removeTagNames: utils.extend({ div: 1 }, dtd.$removeEmpty),
  10966. // 行首缩进
  10967. indent: false,
  10968. // 行首缩进的大小
  10969. indentValue: "2em",
  10970. // 全角转半角
  10971. bdc2sb: false,
  10972. // 半角转全角
  10973. tobdc: false
  10974. }
  10975. });
  10976. var me = this,
  10977. opt = me.options.autotypeset,
  10978. remainClass = {
  10979. selectTdClass: 1,
  10980. pagebreak: 1,
  10981. anchorclass: 1
  10982. },
  10983. remainTag = {
  10984. li: 1
  10985. },
  10986. tags = {
  10987. div: 1,
  10988. p: 1,
  10989. //trace:2183 这些也认为是行
  10990. blockquote: 1,
  10991. center: 1,
  10992. h1: 1,
  10993. h2: 1,
  10994. h3: 1,
  10995. h4: 1,
  10996. h5: 1,
  10997. h6: 1,
  10998. span: 1
  10999. },
  11000. highlightCont;
  11001. //升级了版本,但配置项目里没有autotypeset
  11002. if (!opt) {
  11003. return;
  11004. }
  11005. readLocalOpts();
  11006. function isLine(node, notEmpty) {
  11007. if (!node || node.nodeType == 3) return 0;
  11008. if (domUtils.isBr(node)) return 1;
  11009. if (node && node.parentNode && tags[node.tagName.toLowerCase()]) {
  11010. if (
  11011. (highlightCont && highlightCont.contains(node)) ||
  11012. node.getAttribute("pagebreak")
  11013. ) {
  11014. return 0;
  11015. }
  11016. return notEmpty
  11017. ? !domUtils.isEmptyBlock(node)
  11018. : domUtils.isEmptyBlock(
  11019. node,
  11020. new RegExp("[\\s" + domUtils.fillChar + "]", "g")
  11021. );
  11022. }
  11023. }
  11024. function removeNotAttributeSpan(node) {
  11025. if (!node.style.cssText) {
  11026. domUtils.removeAttributes(node, ["style"]);
  11027. if (
  11028. node.tagName.toLowerCase() == "span" &&
  11029. domUtils.hasNoAttributes(node)
  11030. ) {
  11031. domUtils.remove(node, true);
  11032. }
  11033. }
  11034. }
  11035. function autotype(type, html) {
  11036. var me = this,
  11037. cont;
  11038. if (html) {
  11039. if (!opt.pasteFilter) {
  11040. return;
  11041. }
  11042. cont = me.document.createElement("div");
  11043. cont.innerHTML = html.html;
  11044. } else {
  11045. cont = me.document.body;
  11046. }
  11047. var nodes = domUtils.getElementsByTagName(cont, "*");
  11048. // 行首缩进,段落方向,段间距,段内间距
  11049. for (var i = 0, ci; (ci = nodes[i++]); ) {
  11050. if (me.fireEvent("excludeNodeinautotype", ci) === true) {
  11051. continue;
  11052. }
  11053. //font-size
  11054. if (opt.clearFontSize && ci.style.fontSize) {
  11055. domUtils.removeStyle(ci, "font-size");
  11056. removeNotAttributeSpan(ci);
  11057. }
  11058. //font-family
  11059. if (opt.clearFontFamily && ci.style.fontFamily) {
  11060. domUtils.removeStyle(ci, "font-family");
  11061. removeNotAttributeSpan(ci);
  11062. }
  11063. if (isLine(ci)) {
  11064. //合并空行
  11065. if (opt.mergeEmptyline) {
  11066. var next = ci.nextSibling,
  11067. tmpNode,
  11068. isBr = domUtils.isBr(ci);
  11069. while (isLine(next)) {
  11070. tmpNode = next;
  11071. next = tmpNode.nextSibling;
  11072. if (isBr && (!next || (next && !domUtils.isBr(next)))) {
  11073. break;
  11074. }
  11075. domUtils.remove(tmpNode);
  11076. }
  11077. }
  11078. //去掉空行,保留占位的空行
  11079. if (
  11080. opt.removeEmptyline &&
  11081. domUtils.inDoc(ci, cont) &&
  11082. !remainTag[ci.parentNode.tagName.toLowerCase()]
  11083. ) {
  11084. if (domUtils.isBr(ci)) {
  11085. next = ci.nextSibling;
  11086. if (next && !domUtils.isBr(next)) {
  11087. continue;
  11088. }
  11089. }
  11090. domUtils.remove(ci);
  11091. continue;
  11092. }
  11093. }
  11094. if (isLine(ci, true) && ci.tagName != "SPAN") {
  11095. if (opt.indent) {
  11096. ci.style.textIndent = opt.indentValue;
  11097. }
  11098. if (opt.textAlign) {
  11099. ci.style.textAlign = opt.textAlign;
  11100. }
  11101. // if(opt.lineHeight)
  11102. // ci.style.lineHeight = opt.lineHeight + 'cm';
  11103. }
  11104. //去掉class,保留的class不去掉
  11105. if (
  11106. opt.removeClass &&
  11107. ci.className &&
  11108. !remainClass[ci.className.toLowerCase()]
  11109. ) {
  11110. if (highlightCont && highlightCont.contains(ci)) {
  11111. continue;
  11112. }
  11113. domUtils.removeAttributes(ci, ["class"]);
  11114. }
  11115. //表情不处理
  11116. if (
  11117. opt.imageBlockLine &&
  11118. ci.tagName.toLowerCase() == "img" &&
  11119. !ci.getAttribute("emotion")
  11120. ) {
  11121. if (html) {
  11122. var img = ci;
  11123. switch (opt.imageBlockLine) {
  11124. case "left":
  11125. case "right":
  11126. case "none":
  11127. var pN = img.parentNode,
  11128. tmpNode,
  11129. pre,
  11130. next;
  11131. while (dtd.$inline[pN.tagName] || pN.tagName == "A") {
  11132. pN = pN.parentNode;
  11133. }
  11134. tmpNode = pN;
  11135. if (
  11136. tmpNode.tagName == "P" &&
  11137. domUtils.getStyle(tmpNode, "text-align") == "center"
  11138. ) {
  11139. if (
  11140. !domUtils.isBody(tmpNode) &&
  11141. domUtils.getChildCount(tmpNode, function(node) {
  11142. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11143. }) == 1
  11144. ) {
  11145. pre = tmpNode.previousSibling;
  11146. next = tmpNode.nextSibling;
  11147. if (
  11148. pre &&
  11149. next &&
  11150. pre.nodeType == 1 &&
  11151. next.nodeType == 1 &&
  11152. pre.tagName == next.tagName &&
  11153. domUtils.isBlockElm(pre)
  11154. ) {
  11155. pre.appendChild(tmpNode.firstChild);
  11156. while (next.firstChild) {
  11157. pre.appendChild(next.firstChild);
  11158. }
  11159. domUtils.remove(tmpNode);
  11160. domUtils.remove(next);
  11161. } else {
  11162. domUtils.setStyle(tmpNode, "text-align", "");
  11163. }
  11164. }
  11165. }
  11166. domUtils.setStyle(img, "float", opt.imageBlockLine);
  11167. break;
  11168. case "center":
  11169. if (me.queryCommandValue("imagefloat") != "center") {
  11170. pN = img.parentNode;
  11171. domUtils.setStyle(img, "float", "none");
  11172. tmpNode = img;
  11173. while (
  11174. pN &&
  11175. domUtils.getChildCount(pN, function(node) {
  11176. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11177. }) == 1 &&
  11178. (dtd.$inline[pN.tagName] || pN.tagName == "A")
  11179. ) {
  11180. tmpNode = pN;
  11181. pN = pN.parentNode;
  11182. }
  11183. var pNode = me.document.createElement("p");
  11184. domUtils.setAttributes(pNode, {
  11185. style: "text-align:center"
  11186. });
  11187. tmpNode.parentNode.insertBefore(pNode, tmpNode);
  11188. pNode.appendChild(tmpNode);
  11189. domUtils.setStyle(tmpNode, "float", "");
  11190. }
  11191. }
  11192. } else {
  11193. var range = me.selection.getRange();
  11194. range.selectNode(ci).select();
  11195. me.execCommand("imagefloat", opt.imageBlockLine);
  11196. }
  11197. }
  11198. //去掉冗余的标签
  11199. if (opt.removeEmptyNode) {
  11200. if (
  11201. opt.removeTagNames[ci.tagName.toLowerCase()] &&
  11202. domUtils.hasNoAttributes(ci) &&
  11203. domUtils.isEmptyBlock(ci)
  11204. ) {
  11205. domUtils.remove(ci);
  11206. }
  11207. }
  11208. }
  11209. if (opt.tobdc) {
  11210. var root = UE.htmlparser(cont.innerHTML);
  11211. root.traversal(function(node) {
  11212. if (node.type == "text") {
  11213. node.data = ToDBC(node.data);
  11214. }
  11215. });
  11216. cont.innerHTML = root.toHtml();
  11217. }
  11218. if (opt.bdc2sb) {
  11219. var root = UE.htmlparser(cont.innerHTML);
  11220. root.traversal(function(node) {
  11221. if (node.type == "text") {
  11222. node.data = DBC2SB(node.data);
  11223. }
  11224. });
  11225. cont.innerHTML = root.toHtml();
  11226. }
  11227. if (html) {
  11228. html.html = cont.innerHTML;
  11229. }
  11230. }
  11231. if (opt.pasteFilter) {
  11232. me.addListener("beforepaste", autotype);
  11233. }
  11234. function DBC2SB(str) {
  11235. var result = "";
  11236. for (var i = 0; i < str.length; i++) {
  11237. var code = str.charCodeAt(i); //获取当前字符的unicode编码
  11238. if (code >= 65281 && code <= 65373) {
  11239. //在这个unicode编码范围中的是所有的英文字母已经各种字符
  11240. result += String.fromCharCode(str.charCodeAt(i) - 65248); //把全角字符的unicode编码转换为对应半角字符的unicode码
  11241. } else if (code == 12288) {
  11242. //空格
  11243. result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
  11244. } else {
  11245. result += str.charAt(i);
  11246. }
  11247. }
  11248. return result;
  11249. }
  11250. function ToDBC(txtstring) {
  11251. txtstring = utils.html(txtstring);
  11252. var tmp = "";
  11253. var mark = ""; /*用于判断,如果是html尖括里的标记,则不进行全角的转换*/
  11254. for (var i = 0; i < txtstring.length; i++) {
  11255. if (txtstring.charCodeAt(i) == 32) {
  11256. tmp = tmp + String.fromCharCode(12288);
  11257. } else if (txtstring.charCodeAt(i) < 127) {
  11258. tmp = tmp + String.fromCharCode(txtstring.charCodeAt(i) + 65248);
  11259. } else {
  11260. tmp += txtstring.charAt(i);
  11261. }
  11262. }
  11263. return tmp;
  11264. }
  11265. function readLocalOpts() {
  11266. var cookieOpt = me.getPreferences("autotypeset");
  11267. utils.extend(me.options.autotypeset, cookieOpt);
  11268. }
  11269. me.commands["autotypeset"] = {
  11270. execCommand: function() {
  11271. me.removeListener("beforepaste", autotype);
  11272. if (opt.pasteFilter) {
  11273. me.addListener("beforepaste", autotype);
  11274. }
  11275. autotype.call(me);
  11276. }
  11277. };
  11278. };
  11279. // plugins/autosubmit.js
  11280. /**
  11281. * 快捷键提交
  11282. * @file
  11283. * @since 1.2.6.1
  11284. */
  11285. /**
  11286. * 提交表单
  11287. * @command autosubmit
  11288. * @method execCommand
  11289. * @param { String } cmd 命令字符串
  11290. * @example
  11291. * ```javascript
  11292. * editor.execCommand( 'autosubmit' );
  11293. * ```
  11294. */
  11295. UE.plugin.register("autosubmit", function() {
  11296. return {
  11297. shortcutkey: {
  11298. autosubmit: "ctrl+13" //手动提交
  11299. },
  11300. commands: {
  11301. autosubmit: {
  11302. execCommand: function() {
  11303. var me = this,
  11304. form = domUtils.findParentByTagName(me.iframe, "form", false);
  11305. if (form) {
  11306. if (me.fireEvent("beforesubmit") === false) {
  11307. return;
  11308. }
  11309. me.sync();
  11310. form.submit();
  11311. }
  11312. }
  11313. }
  11314. }
  11315. };
  11316. });
  11317. // plugins/background.js
  11318. /**
  11319. * 背景插件,为UEditor提供设置背景功能
  11320. * @file
  11321. * @since 1.2.6.1
  11322. */
  11323. UE.plugin.register("background", function() {
  11324. var me = this,
  11325. cssRuleId = "editor_background",
  11326. isSetColored,
  11327. reg = new RegExp("body[\\s]*\\{(.+)\\}", "i");
  11328. function stringToObj(str) {
  11329. var obj = {},
  11330. styles = str.split(";");
  11331. utils.each(styles, function(v) {
  11332. var index = v.indexOf(":"),
  11333. key = utils.trim(v.substr(0, index)).toLowerCase();
  11334. key && (obj[key] = utils.trim(v.substr(index + 1) || ""));
  11335. });
  11336. return obj;
  11337. }
  11338. function setBackground(obj) {
  11339. if (obj) {
  11340. var styles = [];
  11341. for (var name in obj) {
  11342. if (obj.hasOwnProperty(name)) {
  11343. styles.push(name + ":" + obj[name] + "; ");
  11344. }
  11345. }
  11346. utils.cssRule(
  11347. cssRuleId,
  11348. styles.length ? "body{" + styles.join("") + "}" : "",
  11349. me.document
  11350. );
  11351. } else {
  11352. utils.cssRule(cssRuleId, "", me.document);
  11353. }
  11354. }
  11355. //重写editor.hasContent方法
  11356. var orgFn = me.hasContents;
  11357. me.hasContents = function() {
  11358. if (me.queryCommandValue("background")) {
  11359. return true;
  11360. }
  11361. return orgFn.apply(me, arguments);
  11362. };
  11363. return {
  11364. bindEvents: {
  11365. getAllHtml: function(type, headHtml) {
  11366. var body = this.body,
  11367. su = domUtils.getComputedStyle(body, "background-image"),
  11368. url = "";
  11369. if (su.indexOf(me.options.imagePath) > 0) {
  11370. url = su
  11371. .substring(su.indexOf(me.options.imagePath), su.length - 1)
  11372. .replace(/"|\(|\)/gi, "");
  11373. } else {
  11374. url = su != "none" ? su.replace(/url\("?|"?\)/gi, "") : "";
  11375. }
  11376. var html = '<style type="text/css">body{';
  11377. var bgObj = {
  11378. "background-color":
  11379. domUtils.getComputedStyle(body, "background-color") || "#ffffff",
  11380. "background-image": url ? "url(" + url + ")" : "",
  11381. "background-repeat":
  11382. domUtils.getComputedStyle(body, "background-repeat") || "",
  11383. "background-position": browser.ie
  11384. ? domUtils.getComputedStyle(body, "background-position-x") +
  11385. " " +
  11386. domUtils.getComputedStyle(body, "background-position-y")
  11387. : domUtils.getComputedStyle(body, "background-position"),
  11388. height: domUtils.getComputedStyle(body, "height")
  11389. };
  11390. for (var name in bgObj) {
  11391. if (bgObj.hasOwnProperty(name)) {
  11392. html += name + ":" + bgObj[name] + "; ";
  11393. }
  11394. }
  11395. html += "}</style> ";
  11396. headHtml.push(html);
  11397. },
  11398. aftersetcontent: function() {
  11399. if (isSetColored == false) setBackground();
  11400. }
  11401. },
  11402. inputRule: function(root) {
  11403. isSetColored = false;
  11404. utils.each(root.getNodesByTagName("p"), function(p) {
  11405. var styles = p.getAttr("data-background");
  11406. if (styles) {
  11407. isSetColored = true;
  11408. setBackground(stringToObj(styles));
  11409. p.parentNode.removeChild(p);
  11410. }
  11411. });
  11412. },
  11413. outputRule: function(root) {
  11414. var me = this,
  11415. styles = (utils.cssRule(cssRuleId, me.document) || "")
  11416. .replace(/[\n\r]+/g, "")
  11417. .match(reg);
  11418. if (styles) {
  11419. root.appendChild(
  11420. UE.uNode.createElement(
  11421. '<p style="display:none;" data-background="' +
  11422. utils.trim(styles[1].replace(/"/g, "").replace(/[\s]+/g, " ")) +
  11423. '"><br/></p>'
  11424. )
  11425. );
  11426. }
  11427. },
  11428. commands: {
  11429. background: {
  11430. execCommand: function(cmd, obj) {
  11431. setBackground(obj);
  11432. },
  11433. queryCommandValue: function() {
  11434. var me = this,
  11435. styles = (utils.cssRule(cssRuleId, me.document) || "")
  11436. .replace(/[\n\r]+/g, "")
  11437. .match(reg);
  11438. return styles ? stringToObj(styles[1]) : null;
  11439. },
  11440. notNeedUndo: true
  11441. }
  11442. }
  11443. };
  11444. });
  11445. // plugins/image.js
  11446. /**
  11447. * 图片插入、排版插件
  11448. * @file
  11449. * @since 1.2.6.1
  11450. */
  11451. /**
  11452. * 图片对齐方式
  11453. * @command imagefloat
  11454. * @method execCommand
  11455. * @remind 值center为独占一行居中
  11456. * @param { String } cmd 命令字符串
  11457. * @param { String } align 对齐方式,可传left、right、none、center
  11458. * @remaind center表示图片独占一行
  11459. * @example
  11460. * ```javascript
  11461. * editor.execCommand( 'imagefloat', 'center' );
  11462. * ```
  11463. */
  11464. /**
  11465. * 如果选区所在位置是图片区域
  11466. * @command imagefloat
  11467. * @method queryCommandValue
  11468. * @param { String } cmd 命令字符串
  11469. * @return { String } 返回图片对齐方式
  11470. * @example
  11471. * ```javascript
  11472. * editor.queryCommandValue( 'imagefloat' );
  11473. * ```
  11474. */
  11475. UE.commands["imagefloat"] = {
  11476. execCommand: function(cmd, align) {
  11477. var me = this,
  11478. range = me.selection.getRange();
  11479. if (!range.collapsed) {
  11480. var img = range.getClosedNode();
  11481. if (img && img.tagName == "IMG") {
  11482. switch (align) {
  11483. case "left":
  11484. case "right":
  11485. case "none":
  11486. var pN = img.parentNode,
  11487. tmpNode,
  11488. pre,
  11489. next;
  11490. while (dtd.$inline[pN.tagName] || pN.tagName == "A") {
  11491. pN = pN.parentNode;
  11492. }
  11493. tmpNode = pN;
  11494. if (
  11495. tmpNode.tagName == "P" &&
  11496. domUtils.getStyle(tmpNode, "text-align") == "center"
  11497. ) {
  11498. if (
  11499. !domUtils.isBody(tmpNode) &&
  11500. domUtils.getChildCount(tmpNode, function(node) {
  11501. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11502. }) == 1
  11503. ) {
  11504. pre = tmpNode.previousSibling;
  11505. next = tmpNode.nextSibling;
  11506. if (
  11507. pre &&
  11508. next &&
  11509. pre.nodeType == 1 &&
  11510. next.nodeType == 1 &&
  11511. pre.tagName == next.tagName &&
  11512. domUtils.isBlockElm(pre)
  11513. ) {
  11514. pre.appendChild(tmpNode.firstChild);
  11515. while (next.firstChild) {
  11516. pre.appendChild(next.firstChild);
  11517. }
  11518. domUtils.remove(tmpNode);
  11519. domUtils.remove(next);
  11520. } else {
  11521. domUtils.setStyle(tmpNode, "text-align", "");
  11522. }
  11523. }
  11524. range.selectNode(img).select();
  11525. }
  11526. domUtils.setStyle(img, "float", align == "none" ? "" : align);
  11527. if (align == "none") {
  11528. domUtils.removeAttributes(img, "align");
  11529. }
  11530. break;
  11531. case "center":
  11532. if (me.queryCommandValue("imagefloat") != "center") {
  11533. pN = img.parentNode;
  11534. domUtils.setStyle(img, "float", "");
  11535. domUtils.removeAttributes(img, "align");
  11536. tmpNode = img;
  11537. while (
  11538. pN &&
  11539. domUtils.getChildCount(pN, function(node) {
  11540. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  11541. }) == 1 &&
  11542. (dtd.$inline[pN.tagName] || pN.tagName == "A")
  11543. ) {
  11544. tmpNode = pN;
  11545. pN = pN.parentNode;
  11546. }
  11547. range.setStartBefore(tmpNode).setCursor(false);
  11548. pN = me.document.createElement("div");
  11549. pN.appendChild(tmpNode);
  11550. domUtils.setStyle(tmpNode, "float", "");
  11551. me.execCommand(
  11552. "insertHtml",
  11553. '<p id="_img_parent_tmp" style="text-align:center">' +
  11554. pN.innerHTML +
  11555. "</p>"
  11556. );
  11557. tmpNode = me.document.getElementById("_img_parent_tmp");
  11558. tmpNode.removeAttribute("id");
  11559. tmpNode = tmpNode.firstChild;
  11560. range.selectNode(tmpNode).select();
  11561. //去掉后边多余的元素
  11562. next = tmpNode.parentNode.nextSibling;
  11563. if (next && domUtils.isEmptyNode(next)) {
  11564. domUtils.remove(next);
  11565. }
  11566. }
  11567. break;
  11568. }
  11569. }
  11570. }
  11571. },
  11572. queryCommandValue: function() {
  11573. var range = this.selection.getRange(),
  11574. startNode,
  11575. floatStyle;
  11576. if (range.collapsed) {
  11577. return "none";
  11578. }
  11579. startNode = range.getClosedNode();
  11580. if (startNode && startNode.nodeType == 1 && startNode.tagName == "IMG") {
  11581. floatStyle =
  11582. domUtils.getComputedStyle(startNode, "float") ||
  11583. startNode.getAttribute("align");
  11584. if (floatStyle == "none") {
  11585. floatStyle = domUtils.getComputedStyle(
  11586. startNode.parentNode,
  11587. "text-align"
  11588. ) == "center"
  11589. ? "center"
  11590. : floatStyle;
  11591. }
  11592. return {
  11593. left: 1,
  11594. right: 1,
  11595. center: 1
  11596. }[floatStyle]
  11597. ? floatStyle
  11598. : "none";
  11599. }
  11600. return "none";
  11601. },
  11602. queryCommandState: function() {
  11603. var range = this.selection.getRange(),
  11604. startNode;
  11605. if (range.collapsed) return -1;
  11606. startNode = range.getClosedNode();
  11607. if (startNode && startNode.nodeType == 1 && startNode.tagName == "IMG") {
  11608. return 0;
  11609. }
  11610. return -1;
  11611. }
  11612. };
  11613. /**
  11614. * 插入图片
  11615. * @command insertimage
  11616. * @method execCommand
  11617. * @param { String } cmd 命令字符串
  11618. * @param { Object } opt 属性键值对,这些属性都将被复制到当前插入图片
  11619. * @remind 该命令第二个参数可接受一个图片配置项对象的数组,可以插入多张图片,
  11620. * 此时数组的每一个元素都是一个Object类型的图片属性集合。
  11621. * @example
  11622. * ```javascript
  11623. * editor.execCommand( 'insertimage', {
  11624. * src:'a/b/c.jpg',
  11625. * width:'100',
  11626. * height:'100'
  11627. * } );
  11628. * ```
  11629. * @example
  11630. * ```javascript
  11631. * editor.execCommand( 'insertimage', [{
  11632. * src:'a/b/c.jpg',
  11633. * width:'100',
  11634. * height:'100'
  11635. * },{
  11636. * src:'a/b/d.jpg',
  11637. * width:'100',
  11638. * height:'100'
  11639. * }] );
  11640. * ```
  11641. */
  11642. UE.commands["insertimage"] = {
  11643. execCommand: function(cmd, opt) {
  11644. opt = utils.isArray(opt) ? opt : [opt];
  11645. if (!opt.length) {
  11646. return;
  11647. }
  11648. var me = this,
  11649. range = me.selection.getRange(),
  11650. img = range.getClosedNode();
  11651. if (me.fireEvent("beforeinsertimage", opt) === true) {
  11652. return;
  11653. }
  11654. if (
  11655. img &&
  11656. /img/i.test(img.tagName) &&
  11657. (img.className != "edui-faked-video" ||
  11658. img.className.indexOf("edui-upload-video") != -1) &&
  11659. !img.getAttribute("data-word-image")
  11660. ) {
  11661. var first = opt.shift();
  11662. var floatStyle = first["floatStyle"];
  11663. delete first["floatStyle"];
  11664. //// img.style.border = (first.border||0) +"px solid #000";
  11665. //// img.style.margin = (first.margin||0) +"px";
  11666. // img.style.cssText += ';margin:' + (first.margin||0) +"px;" + 'border:' + (first.border||0) +"px solid #000";
  11667. domUtils.setAttributes(img, first);
  11668. me.execCommand("imagefloat", floatStyle);
  11669. if (opt.length > 0) {
  11670. range.setStartAfter(img).setCursor(false, true);
  11671. me.execCommand("insertimage", opt);
  11672. }
  11673. } else {
  11674. var html = [],
  11675. str = "",
  11676. ci;
  11677. ci = opt[0];
  11678. if (opt.length == 1) {
  11679. str =
  11680. '<img src="' +
  11681. ci.src +
  11682. '" ' +
  11683. (ci._src ? ' _src="' + ci._src + '" ' : "") +
  11684. (ci.width ? 'width="' + ci.width + '" ' : "") +
  11685. (ci.height ? ' height="' + ci.height + '" ' : "") +
  11686. (ci["floatStyle"] == "left" || ci["floatStyle"] == "right"
  11687. ? ' style="float:' + ci["floatStyle"] + ';"'
  11688. : "") +
  11689. (ci.title && ci.title != "" ? ' title="' + ci.title + '"' : "") +
  11690. (ci.border && ci.border != "0" ? ' border="' + ci.border + '"' : "") +
  11691. (ci.alt && ci.alt != "" ? ' alt="' + ci.alt + '"' : "") +
  11692. (ci.hspace && ci.hspace != "0"
  11693. ? ' hspace = "' + ci.hspace + '"'
  11694. : "") +
  11695. (ci.vspace && ci.vspace != "0"
  11696. ? ' vspace = "' + ci.vspace + '"'
  11697. : "") +
  11698. "/>";
  11699. if (ci["floatStyle"] == "center") {
  11700. str = '<p style="text-align: center">' + str + "</p>";
  11701. }
  11702. html.push(str);
  11703. } else {
  11704. for (var i = 0; (ci = opt[i++]); ) {
  11705. str =
  11706. "<p " +
  11707. (ci["floatStyle"] == "center"
  11708. ? 'style="text-align: center" '
  11709. : "") +
  11710. '><img src="' +
  11711. ci.src +
  11712. '" ' +
  11713. (ci.width ? 'width="' + ci.width + '" ' : "") +
  11714. (ci._src ? ' _src="' + ci._src + '" ' : "") +
  11715. (ci.height ? ' height="' + ci.height + '" ' : "") +
  11716. ' style="' +
  11717. (ci["floatStyle"] && ci["floatStyle"] != "center"
  11718. ? "float:" + ci["floatStyle"] + ";"
  11719. : "") +
  11720. (ci.border || "") +
  11721. '" ' +
  11722. (ci.title ? ' title="' + ci.title + '"' : "") +
  11723. " /></p>";
  11724. html.push(str);
  11725. }
  11726. }
  11727. me.execCommand("insertHtml", html.join(""));
  11728. }
  11729. me.fireEvent("afterinsertimage", opt);
  11730. }
  11731. };
  11732. // plugins/justify.js
  11733. /**
  11734. * 段落格式
  11735. * @file
  11736. * @since 1.2.6.1
  11737. */
  11738. /**
  11739. * 段落对齐方式
  11740. * @command justify
  11741. * @method execCommand
  11742. * @param { String } cmd 命令字符串
  11743. * @param { String } align 对齐方式:left => 居左,right => 居右,center => 居中,justify => 两端对齐
  11744. * @example
  11745. * ```javascript
  11746. * editor.execCommand( 'justify', 'center' );
  11747. * ```
  11748. */
  11749. /**
  11750. * 如果选区所在位置是段落区域,返回当前段落对齐方式
  11751. * @command justify
  11752. * @method queryCommandValue
  11753. * @param { String } cmd 命令字符串
  11754. * @return { String } 返回段落对齐方式
  11755. * @example
  11756. * ```javascript
  11757. * editor.queryCommandValue( 'justify' );
  11758. * ```
  11759. */
  11760. UE.plugins["justify"] = function() {
  11761. var me = this,
  11762. block = domUtils.isBlockElm,
  11763. defaultValue = {
  11764. left: 1,
  11765. right: 1,
  11766. center: 1,
  11767. justify: 1
  11768. },
  11769. doJustify = function(range, style) {
  11770. var bookmark = range.createBookmark(),
  11771. filterFn = function(node) {
  11772. return node.nodeType == 1
  11773. ? node.tagName.toLowerCase() != "br" &&
  11774. !domUtils.isBookmarkNode(node)
  11775. : !domUtils.isWhitespace(node);
  11776. };
  11777. range.enlarge(true);
  11778. var bookmark2 = range.createBookmark(),
  11779. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  11780. tmpRange = range.cloneRange(),
  11781. tmpNode;
  11782. while (
  11783. current &&
  11784. !(
  11785. domUtils.getPosition(current, bookmark2.end) &
  11786. domUtils.POSITION_FOLLOWING
  11787. )
  11788. ) {
  11789. if (current.nodeType == 3 || !block(current)) {
  11790. tmpRange.setStartBefore(current);
  11791. while (current && current !== bookmark2.end && !block(current)) {
  11792. tmpNode = current;
  11793. current = domUtils.getNextDomNode(current, false, null, function(
  11794. node
  11795. ) {
  11796. return !block(node);
  11797. });
  11798. }
  11799. tmpRange.setEndAfter(tmpNode);
  11800. var common = tmpRange.getCommonAncestor();
  11801. if (!domUtils.isBody(common) && block(common)) {
  11802. domUtils.setStyles(
  11803. common,
  11804. utils.isString(style) ? { "text-align": style } : style
  11805. );
  11806. current = common;
  11807. } else {
  11808. var p = range.document.createElement("p");
  11809. domUtils.setStyles(
  11810. p,
  11811. utils.isString(style) ? { "text-align": style } : style
  11812. );
  11813. var frag = tmpRange.extractContents();
  11814. p.appendChild(frag);
  11815. tmpRange.insertNode(p);
  11816. current = p;
  11817. }
  11818. current = domUtils.getNextDomNode(current, false, filterFn);
  11819. } else {
  11820. current = domUtils.getNextDomNode(current, true, filterFn);
  11821. }
  11822. }
  11823. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  11824. };
  11825. UE.commands["justify"] = {
  11826. execCommand: function(cmdName, align) {
  11827. var range = this.selection.getRange(),
  11828. txt;
  11829. //闭合时单独处理
  11830. if (range.collapsed) {
  11831. txt = this.document.createTextNode("p");
  11832. range.insertNode(txt);
  11833. }
  11834. doJustify(range, align);
  11835. if (txt) {
  11836. range.setStartBefore(txt).collapse(true);
  11837. domUtils.remove(txt);
  11838. }
  11839. range.select();
  11840. return true;
  11841. },
  11842. queryCommandValue: function() {
  11843. var startNode = this.selection.getStart(),
  11844. value = domUtils.getComputedStyle(startNode, "text-align");
  11845. return defaultValue[value] ? value : "left";
  11846. },
  11847. queryCommandState: function() {
  11848. var start = this.selection.getStart(),
  11849. cell =
  11850. start &&
  11851. domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  11852. return cell ? -1 : 0;
  11853. }
  11854. };
  11855. };
  11856. // plugins/font.js
  11857. /**
  11858. * 字体颜色,背景色,字号,字体,下划线,删除线
  11859. * @file
  11860. * @since 1.2.6.1
  11861. */
  11862. /**
  11863. * 字体颜色
  11864. * @command forecolor
  11865. * @method execCommand
  11866. * @param { String } cmd 命令字符串
  11867. * @param { String } value 色值(必须十六进制)
  11868. * @example
  11869. * ```javascript
  11870. * editor.execCommand( 'forecolor', '#000' );
  11871. * ```
  11872. */
  11873. /**
  11874. * 返回选区字体颜色
  11875. * @command forecolor
  11876. * @method queryCommandValue
  11877. * @param { String } cmd 命令字符串
  11878. * @return { String } 返回字体颜色
  11879. * @example
  11880. * ```javascript
  11881. * editor.queryCommandValue( 'forecolor' );
  11882. * ```
  11883. */
  11884. /**
  11885. * 字体背景颜色
  11886. * @command backcolor
  11887. * @method execCommand
  11888. * @param { String } cmd 命令字符串
  11889. * @param { String } value 色值(必须十六进制)
  11890. * @example
  11891. * ```javascript
  11892. * editor.execCommand( 'backcolor', '#000' );
  11893. * ```
  11894. */
  11895. /**
  11896. * 返回选区字体颜色
  11897. * @command backcolor
  11898. * @method queryCommandValue
  11899. * @param { String } cmd 命令字符串
  11900. * @return { String } 返回字体背景颜色
  11901. * @example
  11902. * ```javascript
  11903. * editor.queryCommandValue( 'backcolor' );
  11904. * ```
  11905. */
  11906. /**
  11907. * 字体大小
  11908. * @command fontsize
  11909. * @method execCommand
  11910. * @param { String } cmd 命令字符串
  11911. * @param { String } value 字体大小
  11912. * @example
  11913. * ```javascript
  11914. * editor.execCommand( 'fontsize', '14px' );
  11915. * ```
  11916. */
  11917. /**
  11918. * 返回选区字体大小
  11919. * @command fontsize
  11920. * @method queryCommandValue
  11921. * @param { String } cmd 命令字符串
  11922. * @return { String } 返回字体大小
  11923. * @example
  11924. * ```javascript
  11925. * editor.queryCommandValue( 'fontsize' );
  11926. * ```
  11927. */
  11928. /**
  11929. * 字体样式
  11930. * @command fontfamily
  11931. * @method execCommand
  11932. * @param { String } cmd 命令字符串
  11933. * @param { String } value 字体样式
  11934. * @example
  11935. * ```javascript
  11936. * editor.execCommand( 'fontfamily', '微软雅黑' );
  11937. * ```
  11938. */
  11939. /**
  11940. * 返回选区字体样式
  11941. * @command fontfamily
  11942. * @method queryCommandValue
  11943. * @param { String } cmd 命令字符串
  11944. * @return { String } 返回字体样式
  11945. * @example
  11946. * ```javascript
  11947. * editor.queryCommandValue( 'fontfamily' );
  11948. * ```
  11949. */
  11950. /**
  11951. * 字体下划线,与删除线互斥
  11952. * @command underline
  11953. * @method execCommand
  11954. * @param { String } cmd 命令字符串
  11955. * @example
  11956. * ```javascript
  11957. * editor.execCommand( 'underline' );
  11958. * ```
  11959. */
  11960. /**
  11961. * 字体删除线,与下划线互斥
  11962. * @command strikethrough
  11963. * @method execCommand
  11964. * @param { String } cmd 命令字符串
  11965. * @example
  11966. * ```javascript
  11967. * editor.execCommand( 'strikethrough' );
  11968. * ```
  11969. */
  11970. /**
  11971. * 字体边框
  11972. * @command fontborder
  11973. * @method execCommand
  11974. * @param { String } cmd 命令字符串
  11975. * @example
  11976. * ```javascript
  11977. * editor.execCommand( 'fontborder' );
  11978. * ```
  11979. */
  11980. UE.plugins["font"] = function() {
  11981. var me = this,
  11982. fonts = {
  11983. forecolor: "color",
  11984. backcolor: "background-color",
  11985. fontsize: "font-size",
  11986. fontfamily: "font-family",
  11987. underline: "text-decoration",
  11988. strikethrough: "text-decoration",
  11989. fontborder: "border"
  11990. },
  11991. lang = me.getLang(),
  11992. needCmd = { underline: 1, strikethrough: 1, fontborder: 1 },
  11993. needSetChild = {
  11994. forecolor: "color",
  11995. backcolor: "background-color",
  11996. fontsize: "font-size",
  11997. fontfamily: "font-family"
  11998. };
  11999. me.setOpt({
  12000. fontfamily: [
  12001. { name: "default", val: "default" },
  12002. { name: "songti", val: "宋体,SimSun" },
  12003. { name: "yahei", val: "微软雅黑,Microsoft YaHei" },
  12004. { name: "kaiti", val: "楷体,楷体_GB2312,SimKai" },
  12005. { name: "heiti", val: "黑体,SimHei" },
  12006. { name: "lishu", val: "隶书,SimLi" },
  12007. // { name: "andaleMono", val: "andale mono" },
  12008. { name: "arial", val: "arial,helvetica,sans-serif" },
  12009. // { name: "arialBlack", val: "arial black,avant garde" },
  12010. // { name: "comicSansMs", val: "comic sans ms" },
  12011. // { name: "impact", val: "impact,chicago" },
  12012. { name: "timesNewRoman", val: "times new roman" }
  12013. ],
  12014. fontsize: [10, 11, 12, 14, 16, 18, 20, 24, 36]
  12015. });
  12016. function mergeWithParent(node) {
  12017. var parent;
  12018. while ((parent = node.parentNode)) {
  12019. if (
  12020. parent.tagName == "SPAN" &&
  12021. domUtils.getChildCount(parent, function(child) {
  12022. return !domUtils.isBookmarkNode(child) && !domUtils.isBr(child);
  12023. }) == 1
  12024. ) {
  12025. parent.style.cssText += node.style.cssText;
  12026. domUtils.remove(node, true);
  12027. node = parent;
  12028. } else {
  12029. break;
  12030. }
  12031. }
  12032. }
  12033. function mergeChild(rng, cmdName, value) {
  12034. if (needSetChild[cmdName]) {
  12035. rng.adjustmentBoundary();
  12036. if (!rng.collapsed && rng.startContainer.nodeType == 1) {
  12037. var start = rng.startContainer.childNodes[rng.startOffset];
  12038. if (start && domUtils.isTagNode(start, "span")) {
  12039. var bk = rng.createBookmark();
  12040. utils.each(domUtils.getElementsByTagName(start, "span"), function(
  12041. span
  12042. ) {
  12043. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  12044. if (
  12045. cmdName == "backcolor" &&
  12046. domUtils
  12047. .getComputedStyle(span, "background-color")
  12048. .toLowerCase() === value
  12049. ) {
  12050. return;
  12051. }
  12052. domUtils.removeStyle(span, needSetChild[cmdName]);
  12053. if (span.style.cssText.replace(/^\s+$/, "").length == 0) {
  12054. domUtils.remove(span, true);
  12055. }
  12056. });
  12057. rng.moveToBookmark(bk);
  12058. }
  12059. }
  12060. }
  12061. }
  12062. function mergesibling(rng, cmdName, value) {
  12063. var collapsed = rng.collapsed,
  12064. bk = rng.createBookmark(),
  12065. common;
  12066. if (collapsed) {
  12067. common = bk.start.parentNode;
  12068. while (dtd.$inline[common.tagName]) {
  12069. common = common.parentNode;
  12070. }
  12071. } else {
  12072. common = domUtils.getCommonAncestor(bk.start, bk.end);
  12073. }
  12074. utils.each(domUtils.getElementsByTagName(common, "span"), function(span) {
  12075. if (!span.parentNode || domUtils.isBookmarkNode(span)) return;
  12076. if (/\s*border\s*:\s*none;?\s*/i.test(span.style.cssText)) {
  12077. if (/^\s*border\s*:\s*none;?\s*$/.test(span.style.cssText)) {
  12078. domUtils.remove(span, true);
  12079. } else {
  12080. domUtils.removeStyle(span, "border");
  12081. }
  12082. return;
  12083. }
  12084. if (
  12085. /border/i.test(span.style.cssText) &&
  12086. span.parentNode.tagName == "SPAN" &&
  12087. /border/i.test(span.parentNode.style.cssText)
  12088. ) {
  12089. span.style.cssText = span.style.cssText.replace(
  12090. /border[^:]*:[^;]+;?/gi,
  12091. ""
  12092. );
  12093. }
  12094. if (!(cmdName == "fontborder" && value == "none")) {
  12095. var next = span.nextSibling;
  12096. while (next && next.nodeType == 1 && next.tagName == "SPAN") {
  12097. if (domUtils.isBookmarkNode(next) && cmdName == "fontborder") {
  12098. span.appendChild(next);
  12099. next = span.nextSibling;
  12100. continue;
  12101. }
  12102. if (next.style.cssText == span.style.cssText) {
  12103. domUtils.moveChild(next, span);
  12104. domUtils.remove(next);
  12105. }
  12106. if (span.nextSibling === next) break;
  12107. next = span.nextSibling;
  12108. }
  12109. }
  12110. mergeWithParent(span);
  12111. if (browser.ie && browser.version > 8) {
  12112. //拷贝父亲们的特别的属性,这里只做背景颜色的处理
  12113. var parent = domUtils.findParent(span, function(n) {
  12114. return (
  12115. n.tagName == "SPAN" && /background-color/.test(n.style.cssText)
  12116. );
  12117. });
  12118. if (parent && !/background-color/.test(span.style.cssText)) {
  12119. span.style.backgroundColor = parent.style.backgroundColor;
  12120. }
  12121. }
  12122. });
  12123. rng.moveToBookmark(bk);
  12124. mergeChild(rng, cmdName, value);
  12125. }
  12126. me.addInputRule(function(root) {
  12127. utils.each(root.getNodesByTagName("u s del font strike"), function(node) {
  12128. if (node.tagName == "font") {
  12129. var cssStyle = [];
  12130. for (var p in node.attrs) {
  12131. switch (p) {
  12132. case "size":
  12133. cssStyle.push(
  12134. "font-size:" +
  12135. ({
  12136. "1": "10",
  12137. "2": "12",
  12138. "3": "16",
  12139. "4": "18",
  12140. "5": "24",
  12141. "6": "32",
  12142. "7": "48"
  12143. }[node.attrs[p]] || node.attrs[p]) +
  12144. "px"
  12145. );
  12146. break;
  12147. case "color":
  12148. cssStyle.push("color:" + node.attrs[p]);
  12149. break;
  12150. case "face":
  12151. cssStyle.push("font-family:" + node.attrs[p]);
  12152. break;
  12153. case "style":
  12154. cssStyle.push(node.attrs[p]);
  12155. }
  12156. }
  12157. node.attrs = {
  12158. style: cssStyle.join(";")
  12159. };
  12160. } else {
  12161. var val = node.tagName == "u" ? "underline" : "line-through";
  12162. node.attrs = {
  12163. style: (node.getAttr("style") || "") + "text-decoration:" + val + ";"
  12164. };
  12165. }
  12166. node.tagName = "span";
  12167. });
  12168. // utils.each(root.getNodesByTagName('span'), function (node) {
  12169. // var val;
  12170. // if(val = node.getAttr('class')){
  12171. // if(/fontstrikethrough/.test(val)){
  12172. // node.setStyle('text-decoration','line-through');
  12173. // if(node.attrs['class']){
  12174. // node.attrs['class'] = node.attrs['class'].replace(/fontstrikethrough/,'');
  12175. // }else{
  12176. // node.setAttr('class')
  12177. // }
  12178. // }
  12179. // if(/fontborder/.test(val)){
  12180. // node.setStyle('border','1px solid #000');
  12181. // if(node.attrs['class']){
  12182. // node.attrs['class'] = node.attrs['class'].replace(/fontborder/,'');
  12183. // }else{
  12184. // node.setAttr('class')
  12185. // }
  12186. // }
  12187. // }
  12188. // });
  12189. });
  12190. // me.addOutputRule(function(root){
  12191. // utils.each(root.getNodesByTagName('span'), function (node) {
  12192. // var val;
  12193. // if(val = node.getStyle('text-decoration')){
  12194. // if(/line-through/.test(val)){
  12195. // if(node.attrs['class']){
  12196. // node.attrs['class'] += ' fontstrikethrough';
  12197. // }else{
  12198. // node.setAttr('class','fontstrikethrough')
  12199. // }
  12200. // }
  12201. //
  12202. // node.setStyle('text-decoration')
  12203. // }
  12204. // if(val = node.getStyle('border')){
  12205. // if(/1px/.test(val) && /solid/.test(val)){
  12206. // if(node.attrs['class']){
  12207. // node.attrs['class'] += ' fontborder';
  12208. //
  12209. // }else{
  12210. // node.setAttr('class','fontborder')
  12211. // }
  12212. // }
  12213. // node.setStyle('border')
  12214. //
  12215. // }
  12216. // });
  12217. // });
  12218. for (var p in fonts) {
  12219. (function(cmd, style) {
  12220. UE.commands[cmd] = {
  12221. execCommand: function(cmdName, value) {
  12222. value =
  12223. value ||
  12224. (this.queryCommandState(cmdName)
  12225. ? "none"
  12226. : cmdName === "underline"
  12227. ? "underline"
  12228. : cmdName === "fontborder" ? "1px solid #000" : "line-through");
  12229. var me = this,
  12230. range = this.selection.getRange(),
  12231. text;
  12232. if (value === "default") {
  12233. if (range.collapsed) {
  12234. text = me.document.createTextNode("font");
  12235. range.insertNode(text).select();
  12236. }
  12237. me.execCommand("removeFormat", "span,a", style);
  12238. if (text) {
  12239. range.setStartBefore(text).collapse(true);
  12240. domUtils.remove(text);
  12241. }
  12242. mergesibling(range, cmdName, value);
  12243. range.select();
  12244. } else {
  12245. if (!range.collapsed) {
  12246. if (needCmd[cmd] && me.queryCommandValue(cmd)) {
  12247. me.execCommand("removeFormat", "span,a", style);
  12248. }
  12249. range = me.selection.getRange();
  12250. range.applyInlineStyle("span", { style: style + ":" + value });
  12251. mergesibling(range, cmdName, value);
  12252. range.select();
  12253. } else {
  12254. var span = domUtils.findParentByTagName(
  12255. range.startContainer,
  12256. "span",
  12257. true
  12258. );
  12259. text = me.document.createTextNode("font");
  12260. if (
  12261. span &&
  12262. !span.children.length &&
  12263. !span[browser.ie ? "innerText" : "textContent"].replace(
  12264. fillCharReg,
  12265. ""
  12266. ).length
  12267. ) {
  12268. //for ie hack when enter
  12269. range.insertNode(text);
  12270. if (needCmd[cmd]) {
  12271. range.selectNode(text).select();
  12272. me.execCommand("removeFormat", "span,a", style, null);
  12273. span = domUtils.findParentByTagName(text, "span", true);
  12274. range.setStartBefore(text);
  12275. }
  12276. span && (span.style.cssText += ";" + style + ":" + value);
  12277. range.collapse(true).select();
  12278. } else {
  12279. range.insertNode(text);
  12280. range.selectNode(text).select();
  12281. span = range.document.createElement("span");
  12282. if (needCmd[cmd]) {
  12283. //a标签内的不处理跳过
  12284. if (domUtils.findParentByTagName(text, "a", true)) {
  12285. range.setStartBefore(text).setCursor();
  12286. domUtils.remove(text);
  12287. return;
  12288. }
  12289. me.execCommand("removeFormat", "span,a", style);
  12290. }
  12291. span.style.cssText = style + ":" + value;
  12292. text.parentNode.insertBefore(span, text);
  12293. //修复,span套span 但样式不继承的问题
  12294. if (!browser.ie || (browser.ie && browser.version == 9)) {
  12295. var spanParent = span.parentNode;
  12296. while (!domUtils.isBlockElm(spanParent)) {
  12297. if (spanParent.tagName == "SPAN") {
  12298. //opera合并style不会加入";"
  12299. span.style.cssText =
  12300. spanParent.style.cssText + ";" + span.style.cssText;
  12301. }
  12302. spanParent = spanParent.parentNode;
  12303. }
  12304. }
  12305. if (opera) {
  12306. setTimeout(function() {
  12307. range.setStart(span, 0).collapse(true);
  12308. mergesibling(range, cmdName, value);
  12309. range.select();
  12310. });
  12311. } else {
  12312. range.setStart(span, 0).collapse(true);
  12313. mergesibling(range, cmdName, value);
  12314. range.select();
  12315. }
  12316. //trace:981
  12317. //domUtils.mergeToParent(span)
  12318. }
  12319. domUtils.remove(text);
  12320. }
  12321. }
  12322. return true;
  12323. },
  12324. queryCommandValue: function(cmdName) {
  12325. var startNode = this.selection.getStart();
  12326. var styleVal;
  12327. //trace:946
  12328. if (cmdName === "underline" || cmdName === "strikethrough") {
  12329. var tmpNode = startNode,
  12330. value;
  12331. while (
  12332. tmpNode &&
  12333. !domUtils.isBlockElm(tmpNode) &&
  12334. !domUtils.isBody(tmpNode)
  12335. ) {
  12336. if (tmpNode.nodeType == 1) {
  12337. value = domUtils.getComputedStyle(tmpNode, style);
  12338. if (value != "none") {
  12339. return value;
  12340. }
  12341. }
  12342. tmpNode = tmpNode.parentNode;
  12343. }
  12344. return "none";
  12345. }
  12346. else if (cmdName === "fontborder") {
  12347. var tmp = startNode,
  12348. val;
  12349. while (tmp && dtd.$inline[tmp.tagName]) {
  12350. if ((val = domUtils.getComputedStyle(tmp, "border"))) {
  12351. if (/1px/.test(val) && /solid/.test(val)) {
  12352. return val;
  12353. }
  12354. }
  12355. tmp = tmp.parentNode;
  12356. }
  12357. return "";
  12358. }
  12359. else if (cmdName === "FontSize") {
  12360. styleVal = domUtils.getComputedStyle(startNode, style);
  12361. tmp = /^([\d\.]+)(\w+)$/.exec(styleVal);
  12362. if (tmp) {
  12363. return Math.floor(tmp[1]) + tmp[2];
  12364. }
  12365. return styleVal;
  12366. }
  12367. else if(cmdName === 'FontFamily'){
  12368. styleVal = domUtils.getComputedStyle(startNode, style)
  12369. // 移除左右引号
  12370. styleVal = styleVal.replace(/['"]/g, '');
  12371. let fontFamily = lang.fontfamily.default;
  12372. for(var v of me.options["fontfamily"] || []){
  12373. // console.log('FontFamily',styleVal, v.val);
  12374. if(v.val === styleVal){
  12375. fontFamily = styleVal;
  12376. break;
  12377. }
  12378. }
  12379. // console.log('FontFamily',styleVal, fontFamily);
  12380. return fontFamily;
  12381. }
  12382. value = domUtils.getComputedStyle(startNode, style);
  12383. return value;
  12384. },
  12385. queryCommandState: function(cmdName) {
  12386. if (!needCmd[cmdName]) return 0;
  12387. var val = this.queryCommandValue(cmdName);
  12388. if (cmdName == "fontborder") {
  12389. return /1px/.test(val) && /solid/.test(val);
  12390. } else {
  12391. return cmdName == "underline"
  12392. ? /underline/.test(val)
  12393. : /line\-through/.test(val);
  12394. }
  12395. }
  12396. };
  12397. })(p, fonts[p]);
  12398. }
  12399. };
  12400. // plugins/link.js
  12401. /**
  12402. * 超链接
  12403. * @file
  12404. * @since 1.2.6.1
  12405. */
  12406. /**
  12407. * 插入超链接
  12408. * @command link
  12409. * @method execCommand
  12410. * @param { String } cmd 命令字符串
  12411. * @param { Object } options 设置自定义属性,例如:url、title、target
  12412. * @example
  12413. * ```javascript
  12414. * editor.execCommand( 'link', '{
  12415. * url:'ueditor.baidu.com',
  12416. * title:'ueditor',
  12417. * target:'_blank'
  12418. * }' );
  12419. * ```
  12420. */
  12421. /**
  12422. * 返回当前选中的第一个超链接节点
  12423. * @command link
  12424. * @method queryCommandValue
  12425. * @param { String } cmd 命令字符串
  12426. * @return { Element } 超链接节点
  12427. * @example
  12428. * ```javascript
  12429. * editor.queryCommandValue( 'link' );
  12430. * ```
  12431. */
  12432. /**
  12433. * 取消超链接
  12434. * @command unlink
  12435. * @method execCommand
  12436. * @param { String } cmd 命令字符串
  12437. * @example
  12438. * ```javascript
  12439. * editor.execCommand( 'unlink');
  12440. * ```
  12441. */
  12442. UE.plugins["link"] = function() {
  12443. function optimize(range) {
  12444. var start = range.startContainer,
  12445. end = range.endContainer;
  12446. if ((start = domUtils.findParentByTagName(start, "a", true))) {
  12447. range.setStartBefore(start);
  12448. }
  12449. if ((end = domUtils.findParentByTagName(end, "a", true))) {
  12450. range.setEndAfter(end);
  12451. }
  12452. }
  12453. UE.commands["unlink"] = {
  12454. execCommand: function() {
  12455. var range = this.selection.getRange(),
  12456. bookmark;
  12457. if (
  12458. range.collapsed &&
  12459. !domUtils.findParentByTagName(range.startContainer, "a", true)
  12460. ) {
  12461. return;
  12462. }
  12463. bookmark = range.createBookmark();
  12464. optimize(range);
  12465. range.removeInlineStyle("a").moveToBookmark(bookmark).select();
  12466. },
  12467. queryCommandState: function() {
  12468. return !this.highlight && this.queryCommandValue("link") ? 0 : -1;
  12469. }
  12470. };
  12471. function doLink(range, opt, me) {
  12472. var rngClone = range.cloneRange(),
  12473. link = me.queryCommandValue("link");
  12474. optimize((range = range.adjustmentBoundary()));
  12475. var start = range.startContainer;
  12476. if (start.nodeType == 1 && link) {
  12477. start = start.childNodes[range.startOffset];
  12478. if (
  12479. start &&
  12480. start.nodeType == 1 &&
  12481. start.tagName == "A" &&
  12482. /^(?:https?|ftp|file)\s*:\s*\/\//.test(
  12483. start[browser.ie ? "innerText" : "textContent"]
  12484. )
  12485. ) {
  12486. start[browser.ie ? "innerText" : "textContent"] = utils.html(
  12487. opt.textValue || opt.href
  12488. );
  12489. }
  12490. }
  12491. if (!rngClone.collapsed || link) {
  12492. range.removeInlineStyle("a");
  12493. rngClone = range.cloneRange();
  12494. }
  12495. if (rngClone.collapsed) {
  12496. var a = range.document.createElement("a"),
  12497. text = "";
  12498. if (opt.textValue) {
  12499. text = utils.html(opt.textValue);
  12500. delete opt.textValue;
  12501. } else {
  12502. text = utils.html(opt.href);
  12503. }
  12504. domUtils.setAttributes(a, opt);
  12505. start = domUtils.findParentByTagName(rngClone.startContainer, "a", true);
  12506. if (start && domUtils.isInNodeEndBoundary(rngClone, start)) {
  12507. range.setStartAfter(start).collapse(true);
  12508. }
  12509. a[browser.ie ? "innerText" : "textContent"] = text;
  12510. range.insertNode(a).selectNode(a);
  12511. } else {
  12512. range.applyInlineStyle("a", opt);
  12513. }
  12514. }
  12515. UE.commands["link"] = {
  12516. execCommand: function(cmdName, opt) {
  12517. var range;
  12518. opt._href && (opt._href = utils.unhtml(opt._href, /[<">]/g));
  12519. opt.href && (opt.href = utils.unhtml(opt.href, /[<">]/g));
  12520. opt.textValue && (opt.textValue = utils.unhtml(opt.textValue, /[<">]/g));
  12521. doLink((range = this.selection.getRange()), opt, this);
  12522. //闭合都不加占位符,如果加了会在a后边多个占位符节点,导致a是图片背景组成的列表,出现空白问题
  12523. range.collapse().select(true);
  12524. },
  12525. queryCommandValue: function() {
  12526. var range = this.selection.getRange(),
  12527. node;
  12528. if (range.collapsed) {
  12529. // node = this.selection.getStart();
  12530. //在ie下getstart()取值偏上了
  12531. node = range.startContainer;
  12532. node = node.nodeType == 1 ? node : node.parentNode;
  12533. if (
  12534. node &&
  12535. (node = domUtils.findParentByTagName(node, "a", true)) &&
  12536. !domUtils.isInNodeEndBoundary(range, node)
  12537. ) {
  12538. return node;
  12539. }
  12540. } else {
  12541. //trace:1111 如果是<p><a>xx</a></p> startContainer是p就会找不到a
  12542. range.shrinkBoundary();
  12543. var start = range.startContainer.nodeType == 3 ||
  12544. !range.startContainer.childNodes[range.startOffset]
  12545. ? range.startContainer
  12546. : range.startContainer.childNodes[range.startOffset],
  12547. end = range.endContainer.nodeType == 3 || range.endOffset == 0
  12548. ? range.endContainer
  12549. : range.endContainer.childNodes[range.endOffset - 1],
  12550. common = range.getCommonAncestor();
  12551. node = domUtils.findParentByTagName(common, "a", true);
  12552. if (!node && common.nodeType == 1) {
  12553. var as = common.getElementsByTagName("a"),
  12554. ps,
  12555. pe;
  12556. for (var i = 0, ci; (ci = as[i++]); ) {
  12557. (ps = domUtils.getPosition(ci, start)), (pe = domUtils.getPosition(
  12558. ci,
  12559. end
  12560. ));
  12561. if (
  12562. (ps & domUtils.POSITION_FOLLOWING ||
  12563. ps & domUtils.POSITION_CONTAINS) &&
  12564. (pe & domUtils.POSITION_PRECEDING ||
  12565. pe & domUtils.POSITION_CONTAINS)
  12566. ) {
  12567. node = ci;
  12568. break;
  12569. }
  12570. }
  12571. }
  12572. return node;
  12573. }
  12574. },
  12575. queryCommandState: function() {
  12576. //判断如果是视频的话连接不可用
  12577. //fix 853
  12578. var img = this.selection.getRange().getClosedNode(),
  12579. flag =
  12580. img &&
  12581. (img.className == "edui-faked-video" ||
  12582. img.className.indexOf("edui-upload-video") != -1);
  12583. return flag ? -1 : 0;
  12584. }
  12585. };
  12586. };
  12587. // plugins/iframe.js
  12588. ///import core
  12589. ///import plugins\inserthtml.js
  12590. ///commands 插入框架
  12591. ///commandsName InsertFrame
  12592. ///commandsTitle 插入Iframe
  12593. ///commandsDialog dialogs\insertframe
  12594. UE.plugins["insertframe"] = function() {
  12595. var me = this;
  12596. function deleteIframe() {
  12597. me._iframe && delete me._iframe;
  12598. }
  12599. me.addListener("selectionchange", function() {
  12600. deleteIframe();
  12601. });
  12602. };
  12603. // plugins/scrawl.js
  12604. ///import core
  12605. ///commands 涂鸦
  12606. ///commandsName Scrawl
  12607. ///commandsTitle 涂鸦
  12608. ///commandsDialog dialogs\scrawl
  12609. UE.commands["scrawl"] = {
  12610. queryCommandState: function() {
  12611. return browser.ie && browser.version <= 8 ? -1 : 0;
  12612. }
  12613. };
  12614. // plugins/removeformat.js
  12615. /**
  12616. * 清除格式
  12617. * @file
  12618. * @since 1.2.6.1
  12619. */
  12620. /**
  12621. * 清除文字样式
  12622. * @command removeformat
  12623. * @method execCommand
  12624. * @param { String } cmd 命令字符串
  12625. * @param {String} tags 以逗号隔开的标签。如:strong
  12626. * @param {String} style 样式如:color
  12627. * @param {String} attrs 属性如:width
  12628. * @example
  12629. * ```javascript
  12630. * editor.execCommand( 'removeformat', 'strong','color','width' );
  12631. * ```
  12632. */
  12633. UE.plugins["removeformat"] = function() {
  12634. var me = this;
  12635. me.setOpt({
  12636. removeFormatTags:
  12637. "b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var",
  12638. removeFormatAttributes: "class,style,lang,width,height,align,hspace,valign"
  12639. });
  12640. me.commands["removeformat"] = {
  12641. execCommand: function(cmdName, tags, style, attrs, notIncludeA) {
  12642. var tagReg = new RegExp(
  12643. "^(?:" +
  12644. (tags || this.options.removeFormatTags).replace(/,/g, "|") +
  12645. ")$",
  12646. "i"
  12647. ),
  12648. removeFormatAttributes = style
  12649. ? []
  12650. : (attrs || this.options.removeFormatAttributes).split(","),
  12651. range = new dom.Range(this.document),
  12652. bookmark,
  12653. node,
  12654. parent,
  12655. filter = function(node) {
  12656. return node.nodeType == 1;
  12657. };
  12658. function isRedundantSpan(node) {
  12659. if (node.nodeType == 3 || node.tagName.toLowerCase() != "span") {
  12660. return 0;
  12661. }
  12662. if (browser.ie) {
  12663. //ie 下判断实效,所以只能简单用style来判断
  12664. //return node.style.cssText == '' ? 1 : 0;
  12665. var attrs = node.attributes;
  12666. if (attrs.length) {
  12667. for (var i = 0, l = attrs.length; i < l; i++) {
  12668. if (attrs[i].specified) {
  12669. return 0;
  12670. }
  12671. }
  12672. return 1;
  12673. }
  12674. }
  12675. return !node.attributes.length;
  12676. }
  12677. function doRemove(range) {
  12678. var bookmark1 = range.createBookmark();
  12679. if (range.collapsed) {
  12680. range.enlarge(true);
  12681. }
  12682. //不能把a标签切了
  12683. if (!notIncludeA) {
  12684. var aNode = domUtils.findParentByTagName(
  12685. range.startContainer,
  12686. "a",
  12687. true
  12688. );
  12689. if (aNode) {
  12690. range.setStartBefore(aNode);
  12691. }
  12692. aNode = domUtils.findParentByTagName(range.endContainer, "a", true);
  12693. if (aNode) {
  12694. range.setEndAfter(aNode);
  12695. }
  12696. }
  12697. bookmark = range.createBookmark();
  12698. node = bookmark.start;
  12699. //切开始
  12700. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12701. domUtils.breakParent(node, parent);
  12702. domUtils.clearEmptySibling(node);
  12703. }
  12704. if (bookmark.end) {
  12705. //切结束
  12706. node = bookmark.end;
  12707. while ((parent = node.parentNode) && !domUtils.isBlockElm(parent)) {
  12708. domUtils.breakParent(node, parent);
  12709. domUtils.clearEmptySibling(node);
  12710. }
  12711. //开始去除样式
  12712. var current = domUtils.getNextDomNode(bookmark.start, false, filter),
  12713. next;
  12714. while (current) {
  12715. if (current == bookmark.end) {
  12716. break;
  12717. }
  12718. next = domUtils.getNextDomNode(current, true, filter);
  12719. if (
  12720. !dtd.$empty[current.tagName.toLowerCase()] &&
  12721. !domUtils.isBookmarkNode(current)
  12722. ) {
  12723. if (tagReg.test(current.tagName)) {
  12724. if (style) {
  12725. domUtils.removeStyle(current, style);
  12726. if (isRedundantSpan(current) && style != "text-decoration") {
  12727. domUtils.remove(current, true);
  12728. }
  12729. } else {
  12730. domUtils.remove(current, true);
  12731. }
  12732. } else {
  12733. //trace:939 不能把list上的样式去掉
  12734. if (
  12735. !dtd.$tableContent[current.tagName] &&
  12736. !dtd.$list[current.tagName]
  12737. ) {
  12738. domUtils.removeAttributes(current, removeFormatAttributes);
  12739. if (isRedundantSpan(current)) {
  12740. domUtils.remove(current, true);
  12741. }
  12742. }
  12743. }
  12744. }
  12745. current = next;
  12746. }
  12747. }
  12748. //trace:1035
  12749. //trace:1096 不能把td上的样式去掉,比如边框
  12750. var pN = bookmark.start.parentNode;
  12751. if (
  12752. domUtils.isBlockElm(pN) &&
  12753. !dtd.$tableContent[pN.tagName] &&
  12754. !dtd.$list[pN.tagName]
  12755. ) {
  12756. domUtils.removeAttributes(pN, removeFormatAttributes);
  12757. }
  12758. pN = bookmark.end.parentNode;
  12759. if (
  12760. bookmark.end &&
  12761. domUtils.isBlockElm(pN) &&
  12762. !dtd.$tableContent[pN.tagName] &&
  12763. !dtd.$list[pN.tagName]
  12764. ) {
  12765. domUtils.removeAttributes(pN, removeFormatAttributes);
  12766. }
  12767. range.moveToBookmark(bookmark).moveToBookmark(bookmark1);
  12768. //清除冗余的代码 <b><bookmark></b>
  12769. var node = range.startContainer,
  12770. tmp,
  12771. collapsed = range.collapsed;
  12772. while (
  12773. node.nodeType == 1 &&
  12774. domUtils.isEmptyNode(node) &&
  12775. dtd.$removeEmpty[node.tagName]
  12776. ) {
  12777. tmp = node.parentNode;
  12778. range.setStartBefore(node);
  12779. //trace:937
  12780. //更新结束边界
  12781. if (range.startContainer === range.endContainer) {
  12782. range.endOffset--;
  12783. }
  12784. domUtils.remove(node);
  12785. node = tmp;
  12786. }
  12787. if (!collapsed) {
  12788. node = range.endContainer;
  12789. while (
  12790. node.nodeType == 1 &&
  12791. domUtils.isEmptyNode(node) &&
  12792. dtd.$removeEmpty[node.tagName]
  12793. ) {
  12794. tmp = node.parentNode;
  12795. range.setEndBefore(node);
  12796. domUtils.remove(node);
  12797. node = tmp;
  12798. }
  12799. }
  12800. }
  12801. range = this.selection.getRange();
  12802. doRemove(range);
  12803. range.select();
  12804. }
  12805. };
  12806. };
  12807. // plugins/blockquote.js
  12808. /**
  12809. * 添加引用
  12810. * @file
  12811. * @since 1.2.6.1
  12812. */
  12813. /**
  12814. * 添加引用
  12815. * @command blockquote
  12816. * @method execCommand
  12817. * @param { String } cmd 命令字符串
  12818. * @example
  12819. * ```javascript
  12820. * editor.execCommand( 'blockquote' );
  12821. * ```
  12822. */
  12823. /**
  12824. * 添加引用
  12825. * @command blockquote
  12826. * @method execCommand
  12827. * @param { String } cmd 命令字符串
  12828. * @param { Object } attrs 节点属性
  12829. * @example
  12830. * ```javascript
  12831. * editor.execCommand( 'blockquote',{
  12832. * style: "color: red;"
  12833. * } );
  12834. * ```
  12835. */
  12836. UE.plugins["blockquote"] = function() {
  12837. var me = this;
  12838. function getObj(editor) {
  12839. return domUtils.filterNodeList(
  12840. editor.selection.getStartElementPath(),
  12841. "blockquote"
  12842. );
  12843. }
  12844. me.commands["blockquote"] = {
  12845. execCommand: function(cmdName, attrs) {
  12846. var range = this.selection.getRange(),
  12847. obj = getObj(this),
  12848. blockquote = dtd.blockquote,
  12849. bookmark = range.createBookmark();
  12850. if (obj) {
  12851. var start = range.startContainer,
  12852. startBlock = domUtils.isBlockElm(start)
  12853. ? start
  12854. : domUtils.findParent(start, function(node) {
  12855. return domUtils.isBlockElm(node);
  12856. }),
  12857. end = range.endContainer,
  12858. endBlock = domUtils.isBlockElm(end)
  12859. ? end
  12860. : domUtils.findParent(end, function(node) {
  12861. return domUtils.isBlockElm(node);
  12862. });
  12863. //处理一下li
  12864. startBlock =
  12865. domUtils.findParentByTagName(startBlock, "li", true) || startBlock;
  12866. endBlock =
  12867. domUtils.findParentByTagName(endBlock, "li", true) || endBlock;
  12868. if (
  12869. startBlock.tagName == "LI" ||
  12870. startBlock.tagName == "TD" ||
  12871. startBlock === obj ||
  12872. domUtils.isBody(startBlock)
  12873. ) {
  12874. domUtils.remove(obj, true);
  12875. } else {
  12876. domUtils.breakParent(startBlock, obj);
  12877. }
  12878. if (startBlock !== endBlock) {
  12879. obj = domUtils.findParentByTagName(endBlock, "blockquote");
  12880. if (obj) {
  12881. if (
  12882. endBlock.tagName == "LI" ||
  12883. endBlock.tagName == "TD" ||
  12884. domUtils.isBody(endBlock)
  12885. ) {
  12886. obj.parentNode && domUtils.remove(obj, true);
  12887. } else {
  12888. domUtils.breakParent(endBlock, obj);
  12889. }
  12890. }
  12891. }
  12892. var blockquotes = domUtils.getElementsByTagName(
  12893. this.document,
  12894. "blockquote"
  12895. );
  12896. for (var i = 0, bi; (bi = blockquotes[i++]); ) {
  12897. if (!bi.childNodes.length) {
  12898. domUtils.remove(bi);
  12899. } else if (
  12900. domUtils.getPosition(bi, startBlock) &
  12901. domUtils.POSITION_FOLLOWING &&
  12902. domUtils.getPosition(bi, endBlock) & domUtils.POSITION_PRECEDING
  12903. ) {
  12904. domUtils.remove(bi, true);
  12905. }
  12906. }
  12907. } else {
  12908. var tmpRange = range.cloneRange(),
  12909. node = tmpRange.startContainer.nodeType == 1
  12910. ? tmpRange.startContainer
  12911. : tmpRange.startContainer.parentNode,
  12912. preNode = node,
  12913. doEnd = 1;
  12914. //调整开始
  12915. while (1) {
  12916. if (domUtils.isBody(node)) {
  12917. if (preNode !== node) {
  12918. if (range.collapsed) {
  12919. tmpRange.selectNode(preNode);
  12920. doEnd = 0;
  12921. } else {
  12922. tmpRange.setStartBefore(preNode);
  12923. }
  12924. } else {
  12925. tmpRange.setStart(node, 0);
  12926. }
  12927. break;
  12928. }
  12929. if (!blockquote[node.tagName]) {
  12930. if (range.collapsed) {
  12931. tmpRange.selectNode(preNode);
  12932. } else {
  12933. tmpRange.setStartBefore(preNode);
  12934. }
  12935. break;
  12936. }
  12937. preNode = node;
  12938. node = node.parentNode;
  12939. }
  12940. //调整结束
  12941. if (doEnd) {
  12942. preNode = node = node = tmpRange.endContainer.nodeType == 1
  12943. ? tmpRange.endContainer
  12944. : tmpRange.endContainer.parentNode;
  12945. while (1) {
  12946. if (domUtils.isBody(node)) {
  12947. if (preNode !== node) {
  12948. tmpRange.setEndAfter(preNode);
  12949. } else {
  12950. tmpRange.setEnd(node, node.childNodes.length);
  12951. }
  12952. break;
  12953. }
  12954. if (!blockquote[node.tagName]) {
  12955. tmpRange.setEndAfter(preNode);
  12956. break;
  12957. }
  12958. preNode = node;
  12959. node = node.parentNode;
  12960. }
  12961. }
  12962. node = range.document.createElement("blockquote");
  12963. domUtils.setAttributes(node, attrs);
  12964. node.appendChild(tmpRange.extractContents());
  12965. tmpRange.insertNode(node);
  12966. //去除重复的
  12967. var childs = domUtils.getElementsByTagName(node, "blockquote");
  12968. for (var i = 0, ci; (ci = childs[i++]); ) {
  12969. if (ci.parentNode) {
  12970. domUtils.remove(ci, true);
  12971. }
  12972. }
  12973. }
  12974. range.moveToBookmark(bookmark).select();
  12975. },
  12976. queryCommandState: function() {
  12977. return getObj(this) ? 1 : 0;
  12978. }
  12979. };
  12980. };
  12981. // plugins/convertcase.js
  12982. /**
  12983. * 大小写转换
  12984. * @file
  12985. * @since 1.2.6.1
  12986. */
  12987. /**
  12988. * 把选区内文本变大写,与“tolowercase”命令互斥
  12989. * @command touppercase
  12990. * @method execCommand
  12991. * @param { String } cmd 命令字符串
  12992. * @example
  12993. * ```javascript
  12994. * editor.execCommand( 'touppercase' );
  12995. * ```
  12996. */
  12997. /**
  12998. * 把选区内文本变小写,与“touppercase”命令互斥
  12999. * @command tolowercase
  13000. * @method execCommand
  13001. * @param { String } cmd 命令字符串
  13002. * @example
  13003. * ```javascript
  13004. * editor.execCommand( 'tolowercase' );
  13005. * ```
  13006. */
  13007. UE.commands["touppercase"] = UE.commands["tolowercase"] = {
  13008. execCommand: function(cmd) {
  13009. var me = this;
  13010. var rng = me.selection.getRange();
  13011. if (rng.collapsed) {
  13012. return rng;
  13013. }
  13014. var bk = rng.createBookmark(),
  13015. bkEnd = bk.end,
  13016. filterFn = function(node) {
  13017. return !domUtils.isBr(node) && !domUtils.isWhitespace(node);
  13018. },
  13019. curNode = domUtils.getNextDomNode(bk.start, false, filterFn);
  13020. while (
  13021. curNode &&
  13022. domUtils.getPosition(curNode, bkEnd) & domUtils.POSITION_PRECEDING
  13023. ) {
  13024. if (curNode.nodeType == 3) {
  13025. curNode.nodeValue = curNode.nodeValue[
  13026. cmd == "touppercase" ? "toUpperCase" : "toLowerCase"
  13027. ]();
  13028. }
  13029. curNode = domUtils.getNextDomNode(curNode, true, filterFn);
  13030. if (curNode === bkEnd) {
  13031. break;
  13032. }
  13033. }
  13034. rng.moveToBookmark(bk).select();
  13035. }
  13036. };
  13037. // plugins/indent.js
  13038. /**
  13039. * 首行缩进
  13040. * @file
  13041. * @since 1.2.6.1
  13042. */
  13043. /**
  13044. * 缩进
  13045. * @command indent
  13046. * @method execCommand
  13047. * @param { String } cmd 命令字符串
  13048. * @example
  13049. * ```javascript
  13050. * editor.execCommand( 'indent' );
  13051. * ```
  13052. */
  13053. UE.commands["indent"] = {
  13054. execCommand: function() {
  13055. var me = this,
  13056. value = me.queryCommandState("indent")
  13057. ? "0em"
  13058. : me.options.indentValue || "2em";
  13059. me.execCommand("Paragraph", "p", { style: "text-indent:" + value });
  13060. },
  13061. queryCommandState: function() {
  13062. var pN = domUtils.filterNodeList(
  13063. this.selection.getStartElementPath(),
  13064. "p h1 h2 h3 h4 h5 h6"
  13065. );
  13066. return pN && pN.style.textIndent && parseInt(pN.style.textIndent) ? 1 : 0;
  13067. }
  13068. };
  13069. // plugins/print.js
  13070. /**
  13071. * 打印
  13072. * @file
  13073. * @since 1.2.6.1
  13074. */
  13075. /**
  13076. * 打印
  13077. * @command print
  13078. * @method execCommand
  13079. * @param { String } cmd 命令字符串
  13080. * @example
  13081. * ```javascript
  13082. * editor.execCommand( 'print' );
  13083. * ```
  13084. */
  13085. UE.commands["print"] = {
  13086. execCommand: function() {
  13087. this.window.print();
  13088. },
  13089. notNeedUndo: 1
  13090. };
  13091. // plugins/preview.js
  13092. /**
  13093. * 预览
  13094. * @file
  13095. * @since 1.2.6.1
  13096. */
  13097. /**
  13098. * 预览
  13099. * @command preview
  13100. * @method execCommand
  13101. * @param { String } cmd 命令字符串
  13102. * @example
  13103. * ```javascript
  13104. * editor.execCommand( 'preview' );
  13105. * ```
  13106. */
  13107. UE.commands["preview"] = {
  13108. execCommand: function() {
  13109. var w = window.open("", "_blank", ""),
  13110. d = w.document;
  13111. d.open();
  13112. d.write(
  13113. '<!DOCTYPE html><html><head><meta charset="utf-8"/><script src="' +
  13114. this.options.UEDITOR_HOME_URL +
  13115. 'ueditor.parse.js"></script><script>' +
  13116. "setTimeout(function(){uParse('div',{rootPath: '" +
  13117. this.options.UEDITOR_HOME_URL +
  13118. "'})},300)" +
  13119. "</script></head><body><div>" +
  13120. this.getContent(null, null, true) +
  13121. "</div></body></html>"
  13122. );
  13123. d.close();
  13124. },
  13125. notNeedUndo: 1
  13126. };
  13127. // plugins/selectall.js
  13128. /**
  13129. * 全选
  13130. * @file
  13131. * @since 1.2.6.1
  13132. */
  13133. /**
  13134. * 选中所有内容
  13135. * @command selectall
  13136. * @method execCommand
  13137. * @param { String } cmd 命令字符串
  13138. * @example
  13139. * ```javascript
  13140. * editor.execCommand( 'selectall' );
  13141. * ```
  13142. */
  13143. UE.plugins["selectall"] = function() {
  13144. var me = this;
  13145. me.commands["selectall"] = {
  13146. execCommand: function() {
  13147. //去掉了原生的selectAll,因为会出现报错和当内容为空时,不能出现闭合状态的光标
  13148. var me = this,
  13149. body = me.body,
  13150. range = me.selection.getRange();
  13151. range.selectNodeContents(body);
  13152. if (domUtils.isEmptyBlock(body)) {
  13153. //opera不能自动合并到元素的里边,要手动处理一下
  13154. if (browser.opera && body.firstChild && body.firstChild.nodeType == 1) {
  13155. range.setStartAtFirst(body.firstChild);
  13156. }
  13157. range.collapse(true);
  13158. }
  13159. range.select(true);
  13160. },
  13161. notNeedUndo: 1
  13162. };
  13163. //快捷键
  13164. me.addshortcutkey({
  13165. selectAll: "ctrl+65"
  13166. });
  13167. };
  13168. // plugins/paragraph.js
  13169. /**
  13170. * 段落样式
  13171. * @file
  13172. * @since 1.2.6.1
  13173. */
  13174. /**
  13175. * 段落格式
  13176. * @command paragraph
  13177. * @method execCommand
  13178. * @param { String } cmd 命令字符串
  13179. * @param {String} style 标签值为:'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'
  13180. * @param {Object} attrs 标签的属性
  13181. * @example
  13182. * ```javascript
  13183. * editor.execCommand( 'Paragraph','h1','{
  13184. * class:'test'
  13185. * }' );
  13186. * ```
  13187. */
  13188. /**
  13189. * 返回选区内节点标签名
  13190. * @command paragraph
  13191. * @method queryCommandValue
  13192. * @param { String } cmd 命令字符串
  13193. * @return { String } 节点标签名
  13194. * @example
  13195. * ```javascript
  13196. * editor.queryCommandValue( 'Paragraph' );
  13197. * ```
  13198. */
  13199. UE.plugins["paragraph"] = function() {
  13200. var me = this,
  13201. block = domUtils.isBlockElm,
  13202. notExchange = ["TD", "LI", "PRE"],
  13203. doParagraph = function(range, style, attrs, sourceCmdName) {
  13204. var bookmark = range.createBookmark(),
  13205. filterFn = function(node) {
  13206. return node.nodeType == 1
  13207. ? node.tagName.toLowerCase() != "br" &&
  13208. !domUtils.isBookmarkNode(node)
  13209. : !domUtils.isWhitespace(node);
  13210. },
  13211. para;
  13212. range.enlarge(true);
  13213. var bookmark2 = range.createBookmark(),
  13214. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13215. tmpRange = range.cloneRange(),
  13216. tmpNode;
  13217. while (
  13218. current &&
  13219. !(
  13220. domUtils.getPosition(current, bookmark2.end) &
  13221. domUtils.POSITION_FOLLOWING
  13222. )
  13223. ) {
  13224. if (current.nodeType === 3 || !block(current)) {
  13225. tmpRange.setStartBefore(current);
  13226. while (current && current !== bookmark2.end && !block(current)) {
  13227. tmpNode = current;
  13228. current = domUtils.getNextDomNode(current, false, null, function(
  13229. node
  13230. ) {
  13231. return !block(node);
  13232. });
  13233. }
  13234. tmpRange.setEndAfter(tmpNode);
  13235. para = range.document.createElement(style);
  13236. if (attrs) {
  13237. domUtils.setAttributes(para, attrs);
  13238. if (
  13239. sourceCmdName &&
  13240. sourceCmdName === "customstyle" &&
  13241. attrs.style
  13242. ) {
  13243. para.style.cssText = attrs.style;
  13244. }
  13245. }
  13246. para.appendChild(tmpRange.extractContents());
  13247. //需要内容占位
  13248. if (domUtils.isEmptyNode(para)) {
  13249. domUtils.fillChar(range.document, para);
  13250. }
  13251. tmpRange.insertNode(para);
  13252. var parent = para.parentNode;
  13253. //如果para上一级是一个block元素且不是body,td就删除它
  13254. if (
  13255. block(parent) &&
  13256. !domUtils.isBody(para.parentNode) &&
  13257. utils.indexOf(notExchange, parent.tagName) === -1
  13258. ) {
  13259. //存储dir,style
  13260. if (!(sourceCmdName && sourceCmdName === "customstyle")) {
  13261. parent.getAttribute("dir") &&
  13262. para.setAttribute("dir", parent.getAttribute("dir"));
  13263. //trace:1070
  13264. parent.style.cssText &&
  13265. (para.style.cssText =
  13266. parent.style.cssText + ";" + para.style.cssText);
  13267. //trace:1030
  13268. parent.style.textAlign &&
  13269. !para.style.textAlign &&
  13270. (para.style.textAlign = parent.style.textAlign);
  13271. parent.style.textIndent &&
  13272. !para.style.textIndent &&
  13273. (para.style.textIndent = parent.style.textIndent);
  13274. parent.style.padding &&
  13275. !para.style.padding &&
  13276. (para.style.padding = parent.style.padding);
  13277. }
  13278. //trace:1706 选择的就是h1-6要删除
  13279. if (
  13280. attrs &&
  13281. /h\d/i.test(parent.tagName) &&
  13282. !/h\d/i.test(para.tagName)
  13283. ) {
  13284. domUtils.setAttributes(parent, attrs);
  13285. if (
  13286. sourceCmdName &&
  13287. sourceCmdName === "customstyle" &&
  13288. attrs.style
  13289. ) {
  13290. parent.style.cssText = attrs.style;
  13291. }
  13292. domUtils.remove(para.parentNode, true);
  13293. para = parent;
  13294. } else {
  13295. domUtils.remove(para.parentNode, true);
  13296. }
  13297. }
  13298. if (utils.indexOf(notExchange, parent.tagName) !== -1) {
  13299. current = parent;
  13300. } else {
  13301. current = para;
  13302. }
  13303. current = domUtils.getNextDomNode(current, false, filterFn);
  13304. } else {
  13305. current = domUtils.getNextDomNode(current, true, filterFn);
  13306. }
  13307. }
  13308. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  13309. };
  13310. me.setOpt("paragraph", {
  13311. p: "",
  13312. h1: "",
  13313. h2: "",
  13314. h3: "",
  13315. h4: "",
  13316. h5: "",
  13317. h6: ""
  13318. });
  13319. me.commands["paragraph"] = {
  13320. execCommand: function(cmdName, style, attrs, sourceCmdName) {
  13321. var range = this.selection.getRange();
  13322. //闭合时单独处理
  13323. if (range.collapsed) {
  13324. var txt = this.document.createTextNode("p");
  13325. range.insertNode(txt);
  13326. //去掉冗余的fillchar
  13327. if (browser.ie) {
  13328. var node = txt.previousSibling;
  13329. if (node && domUtils.isWhitespace(node)) {
  13330. domUtils.remove(node);
  13331. }
  13332. node = txt.nextSibling;
  13333. if (node && domUtils.isWhitespace(node)) {
  13334. domUtils.remove(node);
  13335. }
  13336. }
  13337. }
  13338. range = doParagraph(range, style, attrs, sourceCmdName);
  13339. if (txt) {
  13340. range.setStartBefore(txt).collapse(true);
  13341. pN = txt.parentNode;
  13342. domUtils.remove(txt);
  13343. if (domUtils.isBlockElm(pN) && domUtils.isEmptyNode(pN)) {
  13344. domUtils.fillNode(this.document, pN);
  13345. }
  13346. }
  13347. if (
  13348. browser.gecko &&
  13349. range.collapsed &&
  13350. range.startContainer.nodeType === 1
  13351. ) {
  13352. var child = range.startContainer.childNodes[range.startOffset];
  13353. if (
  13354. child &&
  13355. child.nodeType === 1 &&
  13356. child.tagName.toLowerCase() === style
  13357. ) {
  13358. range.setStart(child, 0).collapse(true);
  13359. }
  13360. }
  13361. //trace:1097 原来有true,原因忘了,但去了就不能清除多余的占位符了
  13362. range.select();
  13363. return true;
  13364. },
  13365. queryCommandValue: function() {
  13366. var node = domUtils.filterNodeList(
  13367. this.selection.getStartElementPath(),
  13368. "p h1 h2 h3 h4 h5 h6"
  13369. );
  13370. return node ? node.tagName.toLowerCase() : "";
  13371. }
  13372. };
  13373. };
  13374. // plugins/directionality.js
  13375. /**
  13376. * 设置文字输入的方向的插件
  13377. * @file
  13378. * @since 1.2.6.1
  13379. */
  13380. (function() {
  13381. var block = domUtils.isBlockElm,
  13382. getObj = function(editor) {
  13383. // var startNode = editor.selection.getStart(),
  13384. // parents;
  13385. // if ( startNode ) {
  13386. // //查找所有的是block的父亲节点
  13387. // parents = domUtils.findParents( startNode, true, block, true );
  13388. // for ( var i = 0,ci; ci = parents[i++]; ) {
  13389. // if ( ci.getAttribute( 'dir' ) ) {
  13390. // return ci;
  13391. // }
  13392. // }
  13393. // }
  13394. return domUtils.filterNodeList(
  13395. editor.selection.getStartElementPath(),
  13396. function(n) {
  13397. return n && n.nodeType == 1 && n.getAttribute("dir");
  13398. }
  13399. );
  13400. },
  13401. doDirectionality = function(range, editor, forward) {
  13402. var bookmark,
  13403. filterFn = function(node) {
  13404. return node.nodeType == 1
  13405. ? !domUtils.isBookmarkNode(node)
  13406. : !domUtils.isWhitespace(node);
  13407. },
  13408. obj = getObj(editor);
  13409. if (obj && range.collapsed) {
  13410. obj.setAttribute("dir", forward);
  13411. return range;
  13412. }
  13413. bookmark = range.createBookmark();
  13414. range.enlarge(true);
  13415. var bookmark2 = range.createBookmark(),
  13416. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn),
  13417. tmpRange = range.cloneRange(),
  13418. tmpNode;
  13419. while (
  13420. current &&
  13421. !(
  13422. domUtils.getPosition(current, bookmark2.end) &
  13423. domUtils.POSITION_FOLLOWING
  13424. )
  13425. ) {
  13426. if (current.nodeType == 3 || !block(current)) {
  13427. tmpRange.setStartBefore(current);
  13428. while (current && current !== bookmark2.end && !block(current)) {
  13429. tmpNode = current;
  13430. current = domUtils.getNextDomNode(current, false, null, function(
  13431. node
  13432. ) {
  13433. return !block(node);
  13434. });
  13435. }
  13436. tmpRange.setEndAfter(tmpNode);
  13437. var common = tmpRange.getCommonAncestor();
  13438. if (!domUtils.isBody(common) && block(common)) {
  13439. //遍历到了block节点
  13440. common.setAttribute("dir", forward);
  13441. current = common;
  13442. } else {
  13443. //没有遍历到,添加一个block节点
  13444. var p = range.document.createElement("p");
  13445. p.setAttribute("dir", forward);
  13446. var frag = tmpRange.extractContents();
  13447. p.appendChild(frag);
  13448. tmpRange.insertNode(p);
  13449. current = p;
  13450. }
  13451. current = domUtils.getNextDomNode(current, false, filterFn);
  13452. } else {
  13453. current = domUtils.getNextDomNode(current, true, filterFn);
  13454. }
  13455. }
  13456. return range.moveToBookmark(bookmark2).moveToBookmark(bookmark);
  13457. };
  13458. /**
  13459. * 文字输入方向
  13460. * @command directionality
  13461. * @method execCommand
  13462. * @param { String } cmdName 命令字符串
  13463. * @param { String } forward 传入'ltr'表示从左向右输入,传入'rtl'表示从右向左输入
  13464. * @example
  13465. * ```javascript
  13466. * editor.execCommand( 'directionality', 'ltr');
  13467. * ```
  13468. */
  13469. /**
  13470. * 查询当前选区的文字输入方向
  13471. * @command directionality
  13472. * @method queryCommandValue
  13473. * @param { String } cmdName 命令字符串
  13474. * @return { String } 返回'ltr'表示从左向右输入,返回'rtl'表示从右向左输入
  13475. * @example
  13476. * ```javascript
  13477. * editor.queryCommandValue( 'directionality');
  13478. * ```
  13479. */
  13480. UE.commands["directionality"] = {
  13481. execCommand: function(cmdName, forward) {
  13482. var range = this.selection.getRange();
  13483. //闭合时单独处理
  13484. if (range.collapsed) {
  13485. var txt = this.document.createTextNode("d");
  13486. range.insertNode(txt);
  13487. }
  13488. doDirectionality(range, this, forward);
  13489. if (txt) {
  13490. range.setStartBefore(txt).collapse(true);
  13491. domUtils.remove(txt);
  13492. }
  13493. range.select();
  13494. return true;
  13495. },
  13496. queryCommandValue: function() {
  13497. var node = getObj(this);
  13498. return node ? node.getAttribute("dir") : "ltr";
  13499. }
  13500. };
  13501. })();
  13502. // plugins/horizontal.js
  13503. /**
  13504. * 插入分割线插件
  13505. * @file
  13506. * @since 1.2.6.1
  13507. */
  13508. /**
  13509. * 插入分割线
  13510. * @command horizontal
  13511. * @method execCommand
  13512. * @param { String } cmdName 命令字符串
  13513. * @example
  13514. * ```javascript
  13515. * editor.execCommand( 'horizontal' );
  13516. * ```
  13517. */
  13518. UE.plugins["horizontal"] = function() {
  13519. var me = this;
  13520. me.commands["horizontal"] = {
  13521. execCommand: function(cmdName) {
  13522. var me = this;
  13523. if (me.queryCommandState(cmdName) !== -1) {
  13524. me.execCommand("insertHtml", "<hr>");
  13525. var range = me.selection.getRange(),
  13526. start = range.startContainer;
  13527. if (start.nodeType == 1 && !start.childNodes[range.startOffset]) {
  13528. var tmp;
  13529. if ((tmp = start.childNodes[range.startOffset - 1])) {
  13530. if (tmp.nodeType == 1 && tmp.tagName == "HR") {
  13531. if (me.options.enterTag == "p") {
  13532. tmp = me.document.createElement("p");
  13533. range.insertNode(tmp);
  13534. range.setStart(tmp, 0).setCursor();
  13535. } else {
  13536. tmp = me.document.createElement("br");
  13537. range.insertNode(tmp);
  13538. range.setStartBefore(tmp).setCursor();
  13539. }
  13540. }
  13541. }
  13542. }
  13543. return true;
  13544. }
  13545. },
  13546. //边界在table里不能加分隔线
  13547. queryCommandState: function() {
  13548. return domUtils.filterNodeList(
  13549. this.selection.getStartElementPath(),
  13550. "table"
  13551. )
  13552. ? -1
  13553. : 0;
  13554. }
  13555. };
  13556. // me.addListener('delkeyup',function(){
  13557. // var rng = this.selection.getRange();
  13558. // if(browser.ie && browser.version > 8){
  13559. // rng.txtToElmBoundary(true);
  13560. // if(domUtils.isStartInblock(rng)){
  13561. // var tmpNode = rng.startContainer;
  13562. // var pre = tmpNode.previousSibling;
  13563. // if(pre && domUtils.isTagNode(pre,'hr')){
  13564. // domUtils.remove(pre);
  13565. // rng.select();
  13566. // return;
  13567. // }
  13568. // }
  13569. // }
  13570. // if(domUtils.isBody(rng.startContainer)){
  13571. // var hr = rng.startContainer.childNodes[rng.startOffset -1];
  13572. // if(hr && hr.nodeName == 'HR'){
  13573. // var next = hr.nextSibling;
  13574. // if(next){
  13575. // rng.setStart(next,0)
  13576. // }else if(hr.previousSibling){
  13577. // rng.setStartAtLast(hr.previousSibling)
  13578. // }else{
  13579. // var p = this.document.createElement('p');
  13580. // hr.parentNode.insertBefore(p,hr);
  13581. // domUtils.fillNode(this.document,p);
  13582. // rng.setStart(p,0);
  13583. // }
  13584. // domUtils.remove(hr);
  13585. // rng.setCursor(false,true);
  13586. // }
  13587. // }
  13588. // })
  13589. me.addListener("delkeydown", function(name, evt) {
  13590. var rng = this.selection.getRange();
  13591. rng.txtToElmBoundary(true);
  13592. if (domUtils.isStartInblock(rng)) {
  13593. var tmpNode = rng.startContainer;
  13594. var pre = tmpNode.previousSibling;
  13595. if (pre && domUtils.isTagNode(pre, "hr")) {
  13596. domUtils.remove(pre);
  13597. rng.select();
  13598. domUtils.preventDefault(evt);
  13599. return true;
  13600. }
  13601. }
  13602. });
  13603. };
  13604. // plugins/time.js
  13605. /**
  13606. * 插入时间和日期
  13607. * @file
  13608. * @since 1.2.6.1
  13609. */
  13610. /**
  13611. * 插入时间,默认格式:12:59:59
  13612. * @command time
  13613. * @method execCommand
  13614. * @param { String } cmd 命令字符串
  13615. * @example
  13616. * ```javascript
  13617. * editor.execCommand( 'time');
  13618. * ```
  13619. */
  13620. /**
  13621. * 插入日期,默认格式:2013-08-30
  13622. * @command date
  13623. * @method execCommand
  13624. * @param { String } cmd 命令字符串
  13625. * @example
  13626. * ```javascript
  13627. * editor.execCommand( 'date');
  13628. * ```
  13629. */
  13630. UE.commands["time"] = UE.commands["date"] = {
  13631. execCommand: function(cmd, format) {
  13632. var date = new Date();
  13633. function formatTime(date, format) {
  13634. var hh = ("0" + date.getHours()).slice(-2),
  13635. ii = ("0" + date.getMinutes()).slice(-2),
  13636. ss = ("0" + date.getSeconds()).slice(-2);
  13637. format = format || "hh:ii:ss";
  13638. return format.replace(/hh/gi, hh).replace(/ii/gi, ii).replace(/ss/gi, ss);
  13639. }
  13640. function formatDate(date, format) {
  13641. var yyyy = ("000" + date.getFullYear()).slice(-4),
  13642. yy = yyyy.slice(-2),
  13643. mm = ("0" + (date.getMonth() + 1)).slice(-2),
  13644. dd = ("0" + date.getDate()).slice(-2);
  13645. format = format || "yyyy-mm-dd";
  13646. return format
  13647. .replace(/yyyy/gi, yyyy)
  13648. .replace(/yy/gi, yy)
  13649. .replace(/mm/gi, mm)
  13650. .replace(/dd/gi, dd);
  13651. }
  13652. this.execCommand(
  13653. "insertHtml",
  13654. cmd == "time" ? formatTime(date, format) : formatDate(date, format)
  13655. );
  13656. }
  13657. };
  13658. // plugins/rowspacing.js
  13659. /**
  13660. * 段前段后间距插件
  13661. * @file
  13662. * @since 1.2.6.1
  13663. */
  13664. /**
  13665. * 设置段间距
  13666. * @command rowspacing
  13667. * @method execCommand
  13668. * @param { String } cmd 命令字符串
  13669. * @param { String } value 段间距的值,以px为单位
  13670. * @param { String } dir 间距位置,top或bottom,分别表示段前和段后
  13671. * @example
  13672. * ```javascript
  13673. * editor.execCommand( 'rowspacing', '10', 'top' );
  13674. * ```
  13675. */
  13676. UE.plugins["rowspacing"] = function() {
  13677. var me = this;
  13678. me.setOpt({
  13679. rowspacingtop: ["5", "10", "15", "20", "25"],
  13680. rowspacingbottom: ["5", "10", "15", "20", "25"]
  13681. });
  13682. me.commands["rowspacing"] = {
  13683. execCommand: function(cmdName, value, dir) {
  13684. this.execCommand("paragraph", "p", {
  13685. style: "margin-" + dir + ":" + value + "px"
  13686. });
  13687. return true;
  13688. },
  13689. queryCommandValue: function(cmdName, dir) {
  13690. var pN = domUtils.filterNodeList(
  13691. this.selection.getStartElementPath(),
  13692. function(node) {
  13693. return domUtils.isBlockElm(node);
  13694. }
  13695. ),
  13696. value;
  13697. //trace:1026
  13698. if (pN) {
  13699. value = domUtils
  13700. .getComputedStyle(pN, "margin-" + dir)
  13701. .replace(/[^\d]/g, "");
  13702. return !value ? 0 : value;
  13703. }
  13704. return 0;
  13705. }
  13706. };
  13707. };
  13708. // plugins/lineheight.js
  13709. /**
  13710. * 设置行内间距
  13711. * @file
  13712. * @since 1.2.6.1
  13713. */
  13714. UE.plugins["lineheight"] = function() {
  13715. var me = this;
  13716. me.setOpt({ lineheight: ["1", "1.5", "1.75", "2", "3", "4", "5"] });
  13717. /**
  13718. * 行距
  13719. * @command lineheight
  13720. * @method execCommand
  13721. * @param { String } cmdName 命令字符串
  13722. * @param { String } value 传入的行高值, 该值是当前字体的倍数, 例如: 1.5, 1.75
  13723. * @example
  13724. * ```javascript
  13725. * editor.execCommand( 'lineheight', 1.5);
  13726. * ```
  13727. */
  13728. /**
  13729. * 查询当前选区内容的行高大小
  13730. * @command lineheight
  13731. * @method queryCommandValue
  13732. * @param { String } cmd 命令字符串
  13733. * @return { String } 返回当前行高大小
  13734. * @example
  13735. * ```javascript
  13736. * editor.queryCommandValue( 'lineheight' );
  13737. * ```
  13738. */
  13739. me.commands["lineheight"] = {
  13740. execCommand: function(cmdName, value) {
  13741. this.execCommand("paragraph", "p", {
  13742. style: "line-height:" + (value == "1" ? "normal" : value + "em")
  13743. });
  13744. return true;
  13745. },
  13746. queryCommandValue: function() {
  13747. var pN = domUtils.filterNodeList(
  13748. this.selection.getStartElementPath(),
  13749. function(node) {
  13750. return domUtils.isBlockElm(node);
  13751. }
  13752. );
  13753. if (pN) {
  13754. var value = domUtils.getComputedStyle(pN, "line-height");
  13755. return value == "normal" ? 1 : value.replace(/[^\d.]*/gi, "");
  13756. }
  13757. }
  13758. };
  13759. };
  13760. // plugins/insertcode.js
  13761. /**
  13762. * 插入代码插件
  13763. * @file
  13764. * @since 1.2.6.1
  13765. */
  13766. UE.plugins["insertcode"] = function() {
  13767. var me = this;
  13768. me.setOpt("insertcode", {
  13769. as3: "ActionScript3",
  13770. bash: "Bash/Shell",
  13771. cpp: "C/C++",
  13772. css: "Css",
  13773. cf: "CodeFunction",
  13774. "c#": "C#",
  13775. delphi: "Delphi",
  13776. diff: "Diff",
  13777. erlang: "Erlang",
  13778. groovy: "Groovy",
  13779. html: "Html",
  13780. java: "Java",
  13781. jfx: "JavaFx",
  13782. js: "Javascript",
  13783. pl: "Perl",
  13784. php: "Php",
  13785. plain: "Plain Text",
  13786. ps: "PowerShell",
  13787. python: "Python",
  13788. ruby: "Ruby",
  13789. scala: "Scala",
  13790. sql: "Sql",
  13791. vb: "Vb",
  13792. xml: "Xml"
  13793. });
  13794. /**
  13795. * 插入代码
  13796. * @command insertcode
  13797. * @method execCommand
  13798. * @param { String } cmd 命令字符串
  13799. * @param { String } lang 插入代码的语言
  13800. * @example
  13801. * ```javascript
  13802. * editor.execCommand( 'insertcode', 'javascript' );
  13803. * ```
  13804. */
  13805. /**
  13806. * 如果选区所在位置是插入插入代码区域,返回代码的语言
  13807. * @command insertcode
  13808. * @method queryCommandValue
  13809. * @param { String } cmd 命令字符串
  13810. * @return { String } 返回代码的语言
  13811. * @example
  13812. * ```javascript
  13813. * editor.queryCommandValue( 'insertcode' );
  13814. * ```
  13815. */
  13816. me.commands["insertcode"] = {
  13817. execCommand: function(cmd, lang) {
  13818. var me = this,
  13819. rng = me.selection.getRange(),
  13820. pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  13821. if (pre) {
  13822. pre.className = "brush:" + lang + ";toolbar:false;";
  13823. } else {
  13824. var code = "";
  13825. if (rng.collapsed) {
  13826. code = browser.ie && browser.ie11below
  13827. ? browser.version <= 8 ? "&nbsp;" : ""
  13828. : "<br/>";
  13829. } else {
  13830. var frag = rng.extractContents();
  13831. var div = me.document.createElement("div");
  13832. div.appendChild(frag);
  13833. utils.each(
  13834. UE.filterNode(
  13835. UE.htmlparser(div.innerHTML.replace(/[\r\t]/g, "")),
  13836. me.options.filterTxtRules
  13837. ).children,
  13838. function(node) {
  13839. if (browser.ie && browser.ie11below && browser.version > 8) {
  13840. if (node.type == "element") {
  13841. if (node.tagName == "br") {
  13842. code += "\n";
  13843. } else if (!dtd.$empty[node.tagName]) {
  13844. utils.each(node.children, function(cn) {
  13845. if (cn.type == "element") {
  13846. if (cn.tagName == "br") {
  13847. code += "\n";
  13848. } else if (!dtd.$empty[node.tagName]) {
  13849. code += cn.innerText();
  13850. }
  13851. } else {
  13852. code += cn.data;
  13853. }
  13854. });
  13855. if (!/\n$/.test(code)) {
  13856. code += "\n";
  13857. }
  13858. }
  13859. } else {
  13860. code += node.data + "\n";
  13861. }
  13862. if (!node.nextSibling() && /\n$/.test(code)) {
  13863. code = code.replace(/\n$/, "");
  13864. }
  13865. } else {
  13866. if (browser.ie && browser.ie11below) {
  13867. if (node.type == "element") {
  13868. if (node.tagName == "br") {
  13869. code += "<br>";
  13870. } else if (!dtd.$empty[node.tagName]) {
  13871. utils.each(node.children, function(cn) {
  13872. if (cn.type == "element") {
  13873. if (cn.tagName == "br") {
  13874. code += "<br>";
  13875. } else if (!dtd.$empty[node.tagName]) {
  13876. code += cn.innerText();
  13877. }
  13878. } else {
  13879. code += cn.data;
  13880. }
  13881. });
  13882. if (!/br>$/.test(code)) {
  13883. code += "<br>";
  13884. }
  13885. }
  13886. } else {
  13887. code += node.data + "<br>";
  13888. }
  13889. if (!node.nextSibling() && /<br>$/.test(code)) {
  13890. code = code.replace(/<br>$/, "");
  13891. }
  13892. } else {
  13893. code += node.type == "element"
  13894. ? dtd.$empty[node.tagName] ? "" : node.innerText()
  13895. : node.data;
  13896. if (!/br\/?\s*>$/.test(code)) {
  13897. if (!node.nextSibling()) return;
  13898. code += "<br>";
  13899. }
  13900. }
  13901. }
  13902. }
  13903. );
  13904. }
  13905. me.execCommand(
  13906. "inserthtml",
  13907. '<pre id="coder"class="brush:' +
  13908. lang +
  13909. ';toolbar:false">' +
  13910. code +
  13911. "</pre>",
  13912. true
  13913. );
  13914. pre = me.document.getElementById("coder");
  13915. domUtils.removeAttributes(pre, "id");
  13916. var tmpNode = pre.previousSibling;
  13917. if (
  13918. tmpNode &&
  13919. ((tmpNode.nodeType == 3 &&
  13920. tmpNode.nodeValue.length == 1 &&
  13921. browser.ie &&
  13922. browser.version == 6) ||
  13923. domUtils.isEmptyBlock(tmpNode))
  13924. ) {
  13925. domUtils.remove(tmpNode);
  13926. }
  13927. var rng = me.selection.getRange();
  13928. if (domUtils.isEmptyBlock(pre)) {
  13929. rng.setStart(pre, 0).setCursor(false, true);
  13930. } else {
  13931. rng.selectNodeContents(pre).select();
  13932. }
  13933. }
  13934. },
  13935. queryCommandValue: function() {
  13936. var path = this.selection.getStartElementPath();
  13937. var lang = "";
  13938. utils.each(path, function(node) {
  13939. if (node.nodeName == "PRE") {
  13940. var match = node.className.match(/brush:([^;]+)/);
  13941. lang = match && match[1] ? match[1] : "";
  13942. return false;
  13943. }
  13944. });
  13945. return lang;
  13946. }
  13947. };
  13948. me.addInputRule(function(root) {
  13949. utils.each(root.getNodesByTagName("pre"), function(pre) {
  13950. var brs = pre.getNodesByTagName("br");
  13951. if (brs.length) {
  13952. browser.ie &&
  13953. browser.ie11below &&
  13954. browser.version > 8 &&
  13955. utils.each(brs, function(br) {
  13956. var txt = UE.uNode.createText("\n");
  13957. br.parentNode.insertBefore(txt, br);
  13958. br.parentNode.removeChild(br);
  13959. });
  13960. return;
  13961. }
  13962. if (browser.ie && browser.ie11below && browser.version > 8) return;
  13963. var code = pre.innerText().split(/\n/);
  13964. pre.innerHTML("");
  13965. utils.each(code, function(c) {
  13966. if (c.length) {
  13967. pre.appendChild(UE.uNode.createText(c));
  13968. }
  13969. pre.appendChild(UE.uNode.createElement("br"));
  13970. });
  13971. });
  13972. });
  13973. me.addOutputRule(function(root) {
  13974. utils.each(root.getNodesByTagName("pre"), function(pre) {
  13975. var code = "";
  13976. utils.each(pre.children, function(n) {
  13977. if (n.type == "text") {
  13978. //在ie下文本内容有可能末尾带有\n要去掉
  13979. //trace:3396
  13980. code += n.data.replace(/[ ]/g, "&nbsp;").replace(/\n$/, "");
  13981. } else {
  13982. if (n.tagName == "br") {
  13983. code += "\n";
  13984. } else {
  13985. code += !dtd.$empty[n.tagName] ? "" : n.innerText();
  13986. }
  13987. }
  13988. });
  13989. pre.innerText(code.replace(/(&nbsp;|\n)+$/, ""));
  13990. });
  13991. });
  13992. //不需要判断highlight的command列表
  13993. me.notNeedCodeQuery = {
  13994. help: 1,
  13995. undo: 1,
  13996. redo: 1,
  13997. source: 1,
  13998. print: 1,
  13999. searchreplace: 1,
  14000. fullscreen: 1,
  14001. preview: 1,
  14002. insertparagraph: 1,
  14003. elementpath: 1,
  14004. insertcode: 1,
  14005. inserthtml: 1,
  14006. selectall: 1
  14007. };
  14008. //将queyCommamndState重置
  14009. var orgQuery = me.queryCommandState;
  14010. me.queryCommandState = function(cmd) {
  14011. var me = this;
  14012. if (
  14013. !me.notNeedCodeQuery[cmd.toLowerCase()] &&
  14014. me.selection &&
  14015. me.queryCommandValue("insertcode")
  14016. ) {
  14017. return -1;
  14018. }
  14019. return UE.Editor.prototype.queryCommandState.apply(this, arguments);
  14020. };
  14021. me.addListener("beforeenterkeydown", function() {
  14022. var rng = me.selection.getRange();
  14023. var pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  14024. if (pre) {
  14025. me.fireEvent("saveScene");
  14026. if (!rng.collapsed) {
  14027. rng.deleteContents();
  14028. }
  14029. if (!browser.ie || browser.ie9above) {
  14030. var tmpNode = me.document.createElement("br"),
  14031. pre;
  14032. rng.insertNode(tmpNode).setStartAfter(tmpNode).collapse(true);
  14033. var next = tmpNode.nextSibling;
  14034. if (!next && (!browser.ie || browser.version > 10)) {
  14035. rng.insertNode(tmpNode.cloneNode(false));
  14036. } else {
  14037. rng.setStartAfter(tmpNode);
  14038. }
  14039. pre = tmpNode.previousSibling;
  14040. var tmp;
  14041. while (pre) {
  14042. tmp = pre;
  14043. pre = pre.previousSibling;
  14044. if (!pre || pre.nodeName == "BR") {
  14045. pre = tmp;
  14046. break;
  14047. }
  14048. }
  14049. if (pre) {
  14050. var str = "";
  14051. while (
  14052. pre &&
  14053. pre.nodeName != "BR" &&
  14054. new RegExp("^[\\s" + domUtils.fillChar + "]*$").test(pre.nodeValue)
  14055. ) {
  14056. str += pre.nodeValue;
  14057. pre = pre.nextSibling;
  14058. }
  14059. if (pre.nodeName != "BR") {
  14060. var match = pre.nodeValue.match(
  14061. new RegExp("^([\\s" + domUtils.fillChar + "]+)")
  14062. );
  14063. if (match && match[1]) {
  14064. str += match[1];
  14065. }
  14066. }
  14067. if (str) {
  14068. str = me.document.createTextNode(str);
  14069. rng.insertNode(str).setStartAfter(str);
  14070. }
  14071. }
  14072. rng.collapse(true).select(true);
  14073. } else {
  14074. if (browser.version > 8) {
  14075. var txt = me.document.createTextNode("\n");
  14076. var start = rng.startContainer;
  14077. if (rng.startOffset == 0) {
  14078. var preNode = start.previousSibling;
  14079. if (preNode) {
  14080. rng.insertNode(txt);
  14081. var fillchar = me.document.createTextNode(" ");
  14082. rng
  14083. .setStartAfter(txt)
  14084. .insertNode(fillchar)
  14085. .setStart(fillchar, 0)
  14086. .collapse(true)
  14087. .select(true);
  14088. }
  14089. } else {
  14090. rng.insertNode(txt).setStartAfter(txt);
  14091. var fillchar = me.document.createTextNode(" ");
  14092. start = rng.startContainer.childNodes[rng.startOffset];
  14093. if (start && !/^\n/.test(start.nodeValue)) {
  14094. rng.setStartBefore(txt);
  14095. }
  14096. rng
  14097. .insertNode(fillchar)
  14098. .setStart(fillchar, 0)
  14099. .collapse(true)
  14100. .select(true);
  14101. }
  14102. } else {
  14103. var tmpNode = me.document.createElement("br");
  14104. rng.insertNode(tmpNode);
  14105. rng.insertNode(me.document.createTextNode(domUtils.fillChar));
  14106. rng.setStartAfter(tmpNode);
  14107. pre = tmpNode.previousSibling;
  14108. var tmp;
  14109. while (pre) {
  14110. tmp = pre;
  14111. pre = pre.previousSibling;
  14112. if (!pre || pre.nodeName == "BR") {
  14113. pre = tmp;
  14114. break;
  14115. }
  14116. }
  14117. if (pre) {
  14118. var str = "";
  14119. while (
  14120. pre &&
  14121. pre.nodeName != "BR" &&
  14122. new RegExp("^[ " + domUtils.fillChar + "]*$").test(pre.nodeValue)
  14123. ) {
  14124. str += pre.nodeValue;
  14125. pre = pre.nextSibling;
  14126. }
  14127. if (pre.nodeName != "BR") {
  14128. var match = pre.nodeValue.match(
  14129. new RegExp("^([ " + domUtils.fillChar + "]+)")
  14130. );
  14131. if (match && match[1]) {
  14132. str += match[1];
  14133. }
  14134. }
  14135. str = me.document.createTextNode(str);
  14136. rng.insertNode(str).setStartAfter(str);
  14137. }
  14138. rng.collapse(true).select();
  14139. }
  14140. }
  14141. me.fireEvent("saveScene");
  14142. return true;
  14143. }
  14144. });
  14145. me.addListener("tabkeydown", function(cmd, evt) {
  14146. var rng = me.selection.getRange();
  14147. var pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  14148. if (pre) {
  14149. me.fireEvent("saveScene");
  14150. if (evt.shiftKey) {
  14151. } else {
  14152. if (!rng.collapsed) {
  14153. var bk = rng.createBookmark();
  14154. var start = bk.start.previousSibling;
  14155. while (start) {
  14156. if (pre.firstChild === start && !domUtils.isBr(start)) {
  14157. pre.insertBefore(me.document.createTextNode(" "), start);
  14158. break;
  14159. }
  14160. if (domUtils.isBr(start)) {
  14161. pre.insertBefore(
  14162. me.document.createTextNode(" "),
  14163. start.nextSibling
  14164. );
  14165. break;
  14166. }
  14167. start = start.previousSibling;
  14168. }
  14169. var end = bk.end;
  14170. start = bk.start.nextSibling;
  14171. if (pre.firstChild === bk.start) {
  14172. pre.insertBefore(
  14173. me.document.createTextNode(" "),
  14174. start.nextSibling
  14175. );
  14176. }
  14177. while (start && start !== end) {
  14178. if (domUtils.isBr(start) && start.nextSibling) {
  14179. if (start.nextSibling === end) {
  14180. break;
  14181. }
  14182. pre.insertBefore(
  14183. me.document.createTextNode(" "),
  14184. start.nextSibling
  14185. );
  14186. }
  14187. start = start.nextSibling;
  14188. }
  14189. rng.moveToBookmark(bk).select();
  14190. } else {
  14191. var tmpNode = me.document.createTextNode(" ");
  14192. rng
  14193. .insertNode(tmpNode)
  14194. .setStartAfter(tmpNode)
  14195. .collapse(true)
  14196. .select(true);
  14197. }
  14198. }
  14199. me.fireEvent("saveScene");
  14200. return true;
  14201. }
  14202. });
  14203. me.addListener("beforeinserthtml", function(evtName, html) {
  14204. var me = this,
  14205. rng = me.selection.getRange(),
  14206. pre = domUtils.findParentByTagName(rng.startContainer, "pre", true);
  14207. if (pre) {
  14208. if (!rng.collapsed) {
  14209. rng.deleteContents();
  14210. }
  14211. var htmlstr = "";
  14212. if (browser.ie && browser.version > 8) {
  14213. utils.each(
  14214. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14215. .children,
  14216. function(node) {
  14217. if (node.type == "element") {
  14218. if (node.tagName == "br") {
  14219. htmlstr += "\n";
  14220. } else if (!dtd.$empty[node.tagName]) {
  14221. utils.each(node.children, function(cn) {
  14222. if (cn.type == "element") {
  14223. if (cn.tagName == "br") {
  14224. htmlstr += "\n";
  14225. } else if (!dtd.$empty[node.tagName]) {
  14226. htmlstr += cn.innerText();
  14227. }
  14228. } else {
  14229. htmlstr += cn.data;
  14230. }
  14231. });
  14232. if (!/\n$/.test(htmlstr)) {
  14233. htmlstr += "\n";
  14234. }
  14235. }
  14236. } else {
  14237. htmlstr += node.data + "\n";
  14238. }
  14239. if (!node.nextSibling() && /\n$/.test(htmlstr)) {
  14240. htmlstr = htmlstr.replace(/\n$/, "");
  14241. }
  14242. }
  14243. );
  14244. var tmpNode = me.document.createTextNode(
  14245. utils.html(htmlstr.replace(/&nbsp;/g, " "))
  14246. );
  14247. rng.insertNode(tmpNode).selectNode(tmpNode).select();
  14248. } else {
  14249. var frag = me.document.createDocumentFragment();
  14250. utils.each(
  14251. UE.filterNode(UE.htmlparser(html), me.options.filterTxtRules)
  14252. .children,
  14253. function(node) {
  14254. if (node.type == "element") {
  14255. if (node.tagName == "br") {
  14256. frag.appendChild(me.document.createElement("br"));
  14257. } else if (!dtd.$empty[node.tagName]) {
  14258. utils.each(node.children, function(cn) {
  14259. if (cn.type == "element") {
  14260. if (cn.tagName == "br") {
  14261. frag.appendChild(me.document.createElement("br"));
  14262. } else if (!dtd.$empty[node.tagName]) {
  14263. frag.appendChild(
  14264. me.document.createTextNode(
  14265. utils.html(cn.innerText().replace(/&nbsp;/g, " "))
  14266. )
  14267. );
  14268. }
  14269. } else {
  14270. frag.appendChild(
  14271. me.document.createTextNode(
  14272. utils.html(cn.data.replace(/&nbsp;/g, " "))
  14273. )
  14274. );
  14275. }
  14276. });
  14277. if (frag.lastChild.nodeName != "BR") {
  14278. frag.appendChild(me.document.createElement("br"));
  14279. }
  14280. }
  14281. } else {
  14282. frag.appendChild(
  14283. me.document.createTextNode(
  14284. utils.html(node.data.replace(/&nbsp;/g, " "))
  14285. )
  14286. );
  14287. }
  14288. if (!node.nextSibling() && frag.lastChild.nodeName == "BR") {
  14289. frag.removeChild(frag.lastChild);
  14290. }
  14291. }
  14292. );
  14293. rng.insertNode(frag).select();
  14294. }
  14295. return true;
  14296. }
  14297. });
  14298. //方向键的处理
  14299. me.addListener("keydown", function(cmd, evt) {
  14300. var me = this,
  14301. keyCode = evt.keyCode || evt.which;
  14302. if (keyCode == 40) {
  14303. var rng = me.selection.getRange(),
  14304. pre,
  14305. start = rng.startContainer;
  14306. if (
  14307. rng.collapsed &&
  14308. (pre = domUtils.findParentByTagName(rng.startContainer, "pre", true)) &&
  14309. !pre.nextSibling
  14310. ) {
  14311. var last = pre.lastChild;
  14312. while (last && last.nodeName == "BR") {
  14313. last = last.previousSibling;
  14314. }
  14315. if (
  14316. last === start ||
  14317. (rng.startContainer === pre &&
  14318. rng.startOffset == pre.childNodes.length)
  14319. ) {
  14320. me.execCommand("insertparagraph");
  14321. domUtils.preventDefault(evt);
  14322. }
  14323. }
  14324. }
  14325. });
  14326. //trace:3395
  14327. me.addListener("delkeydown", function(type, evt) {
  14328. var rng = this.selection.getRange();
  14329. rng.txtToElmBoundary(true);
  14330. var start = rng.startContainer;
  14331. if (
  14332. domUtils.isTagNode(start, "pre") &&
  14333. rng.collapsed &&
  14334. domUtils.isStartInblock(rng)
  14335. ) {
  14336. var p = me.document.createElement("p");
  14337. domUtils.fillNode(me.document, p);
  14338. start.parentNode.insertBefore(p, start);
  14339. domUtils.remove(start);
  14340. rng.setStart(p, 0).setCursor(false, true);
  14341. domUtils.preventDefault(evt);
  14342. return true;
  14343. }
  14344. });
  14345. };
  14346. // plugins/cleardoc.js
  14347. /**
  14348. * 清空文档插件
  14349. * @file
  14350. * @since 1.2.6.1
  14351. */
  14352. /**
  14353. * 清空文档
  14354. * @command cleardoc
  14355. * @method execCommand
  14356. * @param { String } cmd 命令字符串
  14357. * @example
  14358. * ```javascript
  14359. * //editor 是编辑器实例
  14360. * editor.execCommand('cleardoc');
  14361. * ```
  14362. */
  14363. UE.commands["cleardoc"] = {
  14364. execCommand: function(cmdName) {
  14365. var me = this,
  14366. enterTag = me.options.enterTag,
  14367. range = me.selection.getRange();
  14368. if (enterTag == "br") {
  14369. me.body.innerHTML = "<br/>";
  14370. range.setStart(me.body, 0).setCursor();
  14371. } else {
  14372. me.body.innerHTML = "<p>" + (ie ? "" : "<br/>") + "</p>";
  14373. range.setStart(me.body.firstChild, 0).setCursor(false, true);
  14374. }
  14375. setTimeout(function() {
  14376. me.fireEvent("clearDoc");
  14377. }, 0);
  14378. }
  14379. };
  14380. // plugins/anchor.js
  14381. /**
  14382. * 锚点插件,为UEditor提供插入锚点支持
  14383. * @file
  14384. * @since 1.2.6.1
  14385. */
  14386. UE.plugin.register("anchor", function() {
  14387. return {
  14388. bindEvents: {
  14389. ready: function() {
  14390. utils.cssRule(
  14391. "anchor",
  14392. ".anchorclass{background: url('" +
  14393. this.options.themePath +
  14394. this.options.theme +
  14395. "/images/anchor.gif') no-repeat scroll left center transparent;cursor: auto;display: inline-block;height: 16px;width: 15px;}",
  14396. this.document
  14397. );
  14398. }
  14399. },
  14400. outputRule: function(root) {
  14401. utils.each(root.getNodesByTagName("img"), function(a) {
  14402. var val;
  14403. if ((val = a.getAttr("anchorname"))) {
  14404. a.tagName = "a";
  14405. a.setAttr({
  14406. anchorname: "",
  14407. name: val,
  14408. class: ""
  14409. });
  14410. }
  14411. });
  14412. },
  14413. inputRule: function(root) {
  14414. utils.each(root.getNodesByTagName("a"), function(a) {
  14415. var val;
  14416. if ((val = a.getAttr("name")) && !a.getAttr("href")) {
  14417. //过滤掉word冗余标签
  14418. //_Toc\d+有可能勿命中
  14419. if (/^\_Toc\d+$/.test(val)) {
  14420. a.parentNode.removeChild(a);
  14421. return;
  14422. }
  14423. a.tagName = "img";
  14424. a.setAttr({
  14425. anchorname: a.getAttr("name"),
  14426. class: "anchorclass"
  14427. });
  14428. a.setAttr("name");
  14429. }
  14430. });
  14431. },
  14432. commands: {
  14433. /**
  14434. * 插入锚点
  14435. * @command anchor
  14436. * @method execCommand
  14437. * @param { String } cmd 命令字符串
  14438. * @param { String } name 锚点名称字符串
  14439. * @example
  14440. * ```javascript
  14441. * //editor 是编辑器实例
  14442. * editor.execCommand('anchor', 'anchor1');
  14443. * ```
  14444. */
  14445. anchor: {
  14446. execCommand: function(cmd, name) {
  14447. var range = this.selection.getRange(),
  14448. img = range.getClosedNode();
  14449. if (img && img.getAttribute("anchorname")) {
  14450. if (name) {
  14451. img.setAttribute("anchorname", name);
  14452. } else {
  14453. range.setStartBefore(img).setCursor();
  14454. domUtils.remove(img);
  14455. }
  14456. } else {
  14457. if (name) {
  14458. //只在选区的开始插入
  14459. var anchor = this.document.createElement("img");
  14460. range.collapse(true);
  14461. domUtils.setAttributes(anchor, {
  14462. anchorname: name,
  14463. class: "anchorclass"
  14464. });
  14465. range
  14466. .insertNode(anchor)
  14467. .setStartAfter(anchor)
  14468. .setCursor(false, true);
  14469. }
  14470. }
  14471. }
  14472. }
  14473. }
  14474. };
  14475. });
  14476. // plugins/wordcount.js
  14477. ///import core
  14478. ///commands 字数统计
  14479. ///commandsName WordCount,wordCount
  14480. ///commandsTitle 字数统计
  14481. /*
  14482. * Created by JetBrains WebStorm.
  14483. * User: taoqili
  14484. * Date: 11-9-7
  14485. * Time: 下午8:18
  14486. * To change this template use File | Settings | File Templates.
  14487. */
  14488. UE.plugins["wordcount"] = function() {
  14489. var me = this;
  14490. me.setOpt("wordCount", true);
  14491. me.addListener("contentchange", function() {
  14492. me.fireEvent("wordcount");
  14493. });
  14494. var timer;
  14495. me.addListener("ready", function() {
  14496. var me = this;
  14497. domUtils.on(me.body, "keyup", function(evt) {
  14498. var code = evt.keyCode || evt.which,
  14499. //忽略的按键,ctr,alt,shift,方向键
  14500. ignores = {
  14501. "16": 1,
  14502. "18": 1,
  14503. "20": 1,
  14504. "37": 1,
  14505. "38": 1,
  14506. "39": 1,
  14507. "40": 1
  14508. };
  14509. if (code in ignores) return;
  14510. clearTimeout(timer);
  14511. timer = setTimeout(function() {
  14512. me.fireEvent("wordcount");
  14513. }, 200);
  14514. });
  14515. });
  14516. };
  14517. // plugins/pagebreak.js
  14518. /**
  14519. * 分页功能插件
  14520. * @file
  14521. * @since 1.2.6.1
  14522. */
  14523. UE.plugins["pagebreak"] = function() {
  14524. var me = this,
  14525. notBreakTags = ["td"];
  14526. me.setOpt("pageBreakTag", "_ueditor_page_break_tag_");
  14527. function fillNode(node) {
  14528. if (domUtils.isEmptyBlock(node)) {
  14529. var firstChild = node.firstChild,
  14530. tmpNode;
  14531. while (
  14532. firstChild &&
  14533. firstChild.nodeType == 1 &&
  14534. domUtils.isEmptyBlock(firstChild)
  14535. ) {
  14536. tmpNode = firstChild;
  14537. firstChild = firstChild.firstChild;
  14538. }
  14539. !tmpNode && (tmpNode = node);
  14540. domUtils.fillNode(me.document, tmpNode);
  14541. }
  14542. }
  14543. //分页符样式添加
  14544. me.ready(function() {
  14545. utils.cssRule(
  14546. "pagebreak",
  14547. ".pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}",
  14548. me.document
  14549. );
  14550. });
  14551. function isHr(node) {
  14552. return (
  14553. node &&
  14554. node.nodeType == 1 &&
  14555. node.tagName == "HR" &&
  14556. node.className == "pagebreak"
  14557. );
  14558. }
  14559. me.addInputRule(function(root) {
  14560. root.traversal(function(node) {
  14561. if (node.type == "text" && node.data == me.options.pageBreakTag) {
  14562. var hr = UE.uNode.createElement(
  14563. '<hr class="pagebreak" noshade="noshade" size="5" style="-webkit-user-select: none;">'
  14564. );
  14565. node.parentNode.insertBefore(hr, node);
  14566. node.parentNode.removeChild(node);
  14567. }
  14568. });
  14569. });
  14570. me.addOutputRule(function(node) {
  14571. utils.each(node.getNodesByTagName("hr"), function(n) {
  14572. if (n.getAttr("class") == "pagebreak") {
  14573. var txt = UE.uNode.createText(me.options.pageBreakTag);
  14574. n.parentNode.insertBefore(txt, n);
  14575. n.parentNode.removeChild(n);
  14576. }
  14577. });
  14578. });
  14579. /**
  14580. * 插入分页符
  14581. * @command pagebreak
  14582. * @method execCommand
  14583. * @param { String } cmd 命令字符串
  14584. * @remind 在表格中插入分页符会把表格切分成两部分
  14585. * @remind 获取编辑器内的数据时, 编辑器会把分页符转换成“_ueditor_page_break_tag_”字符串,
  14586. * 以便于提交数据到服务器端后处理分页。
  14587. * @example
  14588. * ```javascript
  14589. * editor.execCommand( 'pagebreak'); //插入一个hr标签,带有样式类名pagebreak
  14590. * ```
  14591. */
  14592. me.commands["pagebreak"] = {
  14593. execCommand: function() {
  14594. var range = me.selection.getRange(),
  14595. hr = me.document.createElement("hr");
  14596. domUtils.setAttributes(hr, {
  14597. class: "pagebreak",
  14598. noshade: "noshade",
  14599. size: "5"
  14600. });
  14601. domUtils.unSelectable(hr);
  14602. //table单独处理
  14603. var node = domUtils.findParentByTagName(
  14604. range.startContainer,
  14605. notBreakTags,
  14606. true
  14607. ),
  14608. parents = [],
  14609. pN;
  14610. if (node) {
  14611. switch (node.tagName) {
  14612. case "TD":
  14613. pN = node.parentNode;
  14614. if (!pN.previousSibling) {
  14615. var table = domUtils.findParentByTagName(pN, "table");
  14616. // var tableWrapDiv = table.parentNode;
  14617. // if(tableWrapDiv && tableWrapDiv.nodeType == 1
  14618. // && tableWrapDiv.tagName == 'DIV'
  14619. // && tableWrapDiv.getAttribute('dropdrag')
  14620. // ){
  14621. // domUtils.remove(tableWrapDiv,true);
  14622. // }
  14623. table.parentNode.insertBefore(hr, table);
  14624. parents = domUtils.findParents(hr, true);
  14625. } else {
  14626. pN.parentNode.insertBefore(hr, pN);
  14627. parents = domUtils.findParents(hr);
  14628. }
  14629. pN = parents[1];
  14630. if (hr !== pN) {
  14631. domUtils.breakParent(hr, pN);
  14632. }
  14633. //table要重写绑定一下拖拽
  14634. me.fireEvent("afteradjusttable", me.document);
  14635. }
  14636. } else {
  14637. if (!range.collapsed) {
  14638. range.deleteContents();
  14639. var start = range.startContainer;
  14640. while (
  14641. !domUtils.isBody(start) &&
  14642. domUtils.isBlockElm(start) &&
  14643. domUtils.isEmptyNode(start)
  14644. ) {
  14645. range.setStartBefore(start).collapse(true);
  14646. domUtils.remove(start);
  14647. start = range.startContainer;
  14648. }
  14649. }
  14650. range.insertNode(hr);
  14651. var pN = hr.parentNode,
  14652. nextNode;
  14653. while (!domUtils.isBody(pN)) {
  14654. domUtils.breakParent(hr, pN);
  14655. nextNode = hr.nextSibling;
  14656. if (nextNode && domUtils.isEmptyBlock(nextNode)) {
  14657. domUtils.remove(nextNode);
  14658. }
  14659. pN = hr.parentNode;
  14660. }
  14661. nextNode = hr.nextSibling;
  14662. var pre = hr.previousSibling;
  14663. if (isHr(pre)) {
  14664. domUtils.remove(pre);
  14665. } else {
  14666. pre && fillNode(pre);
  14667. }
  14668. if (!nextNode) {
  14669. var p = me.document.createElement("p");
  14670. hr.parentNode.appendChild(p);
  14671. domUtils.fillNode(me.document, p);
  14672. range.setStart(p, 0).collapse(true);
  14673. } else {
  14674. if (isHr(nextNode)) {
  14675. domUtils.remove(nextNode);
  14676. } else {
  14677. fillNode(nextNode);
  14678. }
  14679. range.setEndAfter(hr).collapse(false);
  14680. }
  14681. range.select(true);
  14682. }
  14683. }
  14684. };
  14685. };
  14686. // plugins/wordimage.js
  14687. ///import core
  14688. ///commands 本地图片引导上传
  14689. ///commandsName WordImage
  14690. ///commandsTitle 本地图片引导上传
  14691. ///commandsDialog dialogs\wordimage
  14692. UE.plugin.register("wordimage", function() {
  14693. var me = this,
  14694. images = [];
  14695. this.addListener("click", function (type, evt) {
  14696. var el = evt.target || evt.srcElement;
  14697. if ('IMG' == el.tagName && el.getAttribute('data-word-image')) {
  14698. me.ui._dialogs.wordimageDialog && me.ui._dialogs.wordimageDialog.open();
  14699. }
  14700. });
  14701. return {
  14702. commands: {
  14703. wordimage: {
  14704. execCommand: function() {
  14705. var images = domUtils.getElementsByTagName(me.body, "img");
  14706. var urlList = [];
  14707. for (var i = 0, ci; (ci = images[i++]); ) {
  14708. var url = ci.getAttribute("data-word-image");
  14709. url && urlList.push(url);
  14710. }
  14711. return urlList;
  14712. },
  14713. queryCommandState: function() {
  14714. images = domUtils.getElementsByTagName(me.body, "img");
  14715. for (var i = 0, ci; (ci = images[i++]); ) {
  14716. if (ci.getAttribute("data-word-image")) {
  14717. return 1;
  14718. }
  14719. }
  14720. return -1;
  14721. },
  14722. notNeedUndo: true
  14723. }
  14724. },
  14725. inputRule: function(root) {
  14726. utils.each(root.getNodesByTagName("img"), function(img) {
  14727. var attrs = img.attrs,
  14728. flag = parseInt(attrs.width) < 128 || parseInt(attrs.height) < 43,
  14729. opt = me.options,
  14730. src = opt.UEDITOR_HOME_URL + "themes/default/images/spacer.gif";
  14731. if (attrs["src"] && /^(?:(file:\/+))/.test(attrs["src"])) {
  14732. img.setAttr({
  14733. width: attrs.width,
  14734. height: attrs.height,
  14735. alt: attrs.alt,
  14736. 'data-word-image': attrs.src,
  14737. src: src,
  14738. style:
  14739. "background:url(" +
  14740. (flag
  14741. ? opt.themePath + opt.theme + "/images/word.gif"
  14742. : opt.langPath + opt.lang + "/images/localimage.png") +
  14743. ") no-repeat center center;border:1px solid #ddd"
  14744. });
  14745. }
  14746. });
  14747. }
  14748. };
  14749. });
  14750. // plugins/autosave.js
  14751. UE.plugin.register("autosave", function () {
  14752. var me = this, saveKey = null;
  14753. function save(editor) {
  14754. var saveData;
  14755. if (!editor.hasContents()) {
  14756. //这里不能调用命令来删除, 会造成事件死循环
  14757. saveKey && me.removePreferences(saveKey);
  14758. return;
  14759. }
  14760. editor._autoSaveTimer = null;
  14761. saveData = me.body.innerHTML;
  14762. if (
  14763. editor.fireEvent("beforeautosave", {
  14764. content: saveData
  14765. }) === false
  14766. ) {
  14767. return;
  14768. }
  14769. // console.log('autosave', saveKey, saveData);
  14770. me.setPreferences(saveKey, saveData);
  14771. editor.fireEvent("afterautosave", {
  14772. content: saveData
  14773. });
  14774. }
  14775. return {
  14776. defaultOptions: {
  14777. autoSaveEnable: true,
  14778. autoSaveRestore: false,
  14779. autoSaveKey: null,
  14780. },
  14781. bindEvents: {
  14782. ready: function () {
  14783. saveKey = me.getOpt('autoSaveKey');
  14784. if (!saveKey) {
  14785. var _suffix = "_DraftsData", key = null;
  14786. if (me.key) {
  14787. key = me.key + _suffix;
  14788. } else {
  14789. key = (me.container.parentNode.id || "ue-common") + _suffix;
  14790. }
  14791. saveKey = (location.protocol + location.host + location.pathname).replace(
  14792. /[.:\/]/g,
  14793. "_"
  14794. ) + key;
  14795. }
  14796. if (me.getOpt('autoSaveRestore')) {
  14797. var data = me.getPreferences(saveKey);
  14798. // console.log('saveKey', saveKey, data);
  14799. if (data) {
  14800. me.body.innerHTML = data;
  14801. me.fireEvent('showmessage',{
  14802. type:'info',
  14803. content:me.getLang('autosave').autoRestoreTip
  14804. })
  14805. }
  14806. }
  14807. // console.log('saveKey', saveKey);
  14808. },
  14809. beforesubmit: function(){
  14810. if (!me.getOpt("autoSaveEnable") || !saveKey) {
  14811. return;
  14812. }
  14813. me.execCommand('clear_auto_save_content');
  14814. },
  14815. contentchange: function () {
  14816. if(!me.isReady){
  14817. return;
  14818. }
  14819. if (!me.getOpt("autoSaveEnable") || !saveKey) {
  14820. return;
  14821. }
  14822. if (me._autoSaveTimer) {
  14823. window.clearTimeout(me._autoSaveTimer);
  14824. }
  14825. me._autoSaveTimer = window.setTimeout(function () {
  14826. save(me);
  14827. }, 1000);
  14828. }
  14829. },
  14830. commands: {
  14831. clear_auto_save_content: {
  14832. execCommand: function (cmd, name) {
  14833. if (saveKey && me.getPreferences(saveKey)) {
  14834. me.removePreferences(saveKey);
  14835. }
  14836. },
  14837. notNeedUndo: true,
  14838. ignoreContentChange: true
  14839. },
  14840. set_auto_save_content: {
  14841. execCommand: function (cmd, name) {
  14842. save(me);
  14843. },
  14844. notNeedUndo: true,
  14845. ignoreContentChange: true
  14846. },
  14847. get_auto_save_content: {
  14848. execCommand: function (cmd, name) {
  14849. return me.getPreferences(saveKey) || "";
  14850. },
  14851. notNeedUndo: true,
  14852. ignoreContentChange: true
  14853. },
  14854. auto_save_restore: {
  14855. execCommand: function (cmd, name) {
  14856. if (saveKey) {
  14857. me.body.innerHTML =
  14858. me.getPreferences(saveKey) || "<p>" + domUtils.fillHtml + "</p>";
  14859. me.focus(true);
  14860. }
  14861. },
  14862. queryCommandState: function () {
  14863. return saveKey ? (me.getPreferences(saveKey) === null ? -1 : 0) : -1;
  14864. },
  14865. notNeedUndo: true,
  14866. ignoreContentChange: true
  14867. }
  14868. }
  14869. };
  14870. });
  14871. // plugins/formula.js
  14872. UE.plugin.register("formula", function () {
  14873. var me = this, images = [];
  14874. return {
  14875. commands: {
  14876. formula: {
  14877. execCommand: function (cmdName, value) {
  14878. var range = me.selection.getRange(),
  14879. img = range.getClosedNode();
  14880. value = encodeURIComponent(value);
  14881. var formulaConfig = me.getOpt('formulaConfig');
  14882. var src = formulaConfig.imageUrlTemplate.replace(/\{\}/, value);
  14883. if (img) {
  14884. img.setAttribute("src", src);
  14885. } else {
  14886. me.execCommand("insertHtml", '<img src="' + src + '" data-formula-image="' + value + '" />');
  14887. }
  14888. },
  14889. }
  14890. },
  14891. };
  14892. });
  14893. // plugins/dragdrop.js
  14894. UE.plugins["dragdrop"] = function() {
  14895. var me = this;
  14896. me.ready(function() {
  14897. domUtils.on(this.body, "dragend", function() {
  14898. var rng = me.selection.getRange();
  14899. var node = rng.getClosedNode() || me.selection.getStart();
  14900. if (node && node.tagName == "IMG") {
  14901. var pre = node.previousSibling,
  14902. next;
  14903. while ((next = node.nextSibling)) {
  14904. if (
  14905. next.nodeType == 1 &&
  14906. next.tagName == "SPAN" &&
  14907. !next.firstChild
  14908. ) {
  14909. domUtils.remove(next);
  14910. } else {
  14911. break;
  14912. }
  14913. }
  14914. if (
  14915. ((pre && pre.nodeType == 1 && !domUtils.isEmptyBlock(pre)) || !pre) &&
  14916. (!next || (next && !domUtils.isEmptyBlock(next)))
  14917. ) {
  14918. if (pre && pre.tagName == "P" && !domUtils.isEmptyBlock(pre)) {
  14919. pre.appendChild(node);
  14920. domUtils.moveChild(next, pre);
  14921. domUtils.remove(next);
  14922. } else if (
  14923. next &&
  14924. next.tagName == "P" &&
  14925. !domUtils.isEmptyBlock(next)
  14926. ) {
  14927. next.insertBefore(node, next.firstChild);
  14928. }
  14929. if (pre && pre.tagName == "P" && domUtils.isEmptyBlock(pre)) {
  14930. domUtils.remove(pre);
  14931. }
  14932. if (next && next.tagName == "P" && domUtils.isEmptyBlock(next)) {
  14933. domUtils.remove(next);
  14934. }
  14935. rng.selectNode(node).select();
  14936. me.fireEvent("saveScene");
  14937. }
  14938. }
  14939. });
  14940. });
  14941. me.addListener("keyup", function(type, evt) {
  14942. var keyCode = evt.keyCode || evt.which;
  14943. if (keyCode == 13) {
  14944. var rng = me.selection.getRange(),
  14945. node;
  14946. if (
  14947. (node = domUtils.findParentByTagName(rng.startContainer, "p", true))
  14948. ) {
  14949. if (domUtils.getComputedStyle(node, "text-align") == "center") {
  14950. domUtils.removeStyle(node, "text-align");
  14951. }
  14952. }
  14953. }
  14954. });
  14955. };
  14956. // plugins/undo.js
  14957. /**
  14958. * undo redo
  14959. * @file
  14960. * @since 1.2.6.1
  14961. */
  14962. /**
  14963. * 撤销上一次执行的命令
  14964. * @command undo
  14965. * @method execCommand
  14966. * @param { String } cmd 命令字符串
  14967. * @example
  14968. * ```javascript
  14969. * editor.execCommand( 'undo' );
  14970. * ```
  14971. */
  14972. /**
  14973. * 重做上一次执行的命令
  14974. * @command redo
  14975. * @method execCommand
  14976. * @param { String } cmd 命令字符串
  14977. * @example
  14978. * ```javascript
  14979. * editor.execCommand( 'redo' );
  14980. * ```
  14981. */
  14982. UE.plugins["undo"] = function() {
  14983. var saveSceneTimer;
  14984. var me = this,
  14985. maxUndoCount = me.options.maxUndoCount || 20,
  14986. maxInputCount = me.options.maxInputCount || 20,
  14987. fillchar = new RegExp(domUtils.fillChar + "|</hr>", "gi"); // ie会产生多余的</hr>
  14988. var noNeedFillCharTags = {
  14989. ol: 1,
  14990. ul: 1,
  14991. table: 1,
  14992. tbody: 1,
  14993. tr: 1,
  14994. body: 1
  14995. };
  14996. var orgState = me.options.autoClearEmptyNode;
  14997. function compareAddr(indexA, indexB) {
  14998. if (indexA.length != indexB.length) return 0;
  14999. for (var i = 0, l = indexA.length; i < l; i++) {
  15000. if (indexA[i] != indexB[i]) return 0;
  15001. }
  15002. return 1;
  15003. }
  15004. function compareRangeAddress(rngAddrA, rngAddrB) {
  15005. if (rngAddrA.collapsed != rngAddrB.collapsed) {
  15006. return 0;
  15007. }
  15008. if (
  15009. !compareAddr(rngAddrA.startAddress, rngAddrB.startAddress) ||
  15010. !compareAddr(rngAddrA.endAddress, rngAddrB.endAddress)
  15011. ) {
  15012. return 0;
  15013. }
  15014. return 1;
  15015. }
  15016. function UndoManager() {
  15017. this.list = [];
  15018. this.index = 0;
  15019. this.hasUndo = false;
  15020. this.hasRedo = false;
  15021. this.undo = function() {
  15022. if (this.hasUndo) {
  15023. if (!this.list[this.index - 1] && this.list.length == 1) {
  15024. this.reset();
  15025. return;
  15026. }
  15027. while (
  15028. this.list[this.index].content == this.list[this.index - 1].content
  15029. ) {
  15030. this.index--;
  15031. if (this.index == 0) {
  15032. return this.restore(0);
  15033. }
  15034. }
  15035. this.restore(--this.index);
  15036. }
  15037. };
  15038. this.redo = function() {
  15039. if (this.hasRedo) {
  15040. while (
  15041. this.list[this.index].content == this.list[this.index + 1].content
  15042. ) {
  15043. this.index++;
  15044. if (this.index == this.list.length - 1) {
  15045. return this.restore(this.index);
  15046. }
  15047. }
  15048. this.restore(++this.index);
  15049. }
  15050. };
  15051. this.restore = function() {
  15052. var me = this.editor;
  15053. var scene = this.list[this.index];
  15054. var root = UE.htmlparser(scene.content.replace(fillchar, ""));
  15055. me.options.autoClearEmptyNode = false;
  15056. me.filterInputRule(root);
  15057. me.options.autoClearEmptyNode = orgState;
  15058. //trace:873
  15059. //去掉展位符
  15060. me.document.body.innerHTML = root.toHtml();
  15061. me.fireEvent("afterscencerestore");
  15062. //处理undo后空格不展位的问题
  15063. if (browser.ie) {
  15064. utils.each(
  15065. domUtils.getElementsByTagName(me.document, "td th caption p"),
  15066. function(node) {
  15067. if (domUtils.isEmptyNode(node)) {
  15068. domUtils.fillNode(me.document, node);
  15069. }
  15070. }
  15071. );
  15072. }
  15073. try {
  15074. var rng = new dom.Range(me.document).moveToAddress(scene.address);
  15075. rng.select(
  15076. noNeedFillCharTags[rng.startContainer.nodeName.toLowerCase()]
  15077. );
  15078. } catch (e) {}
  15079. this.update();
  15080. this.clearKey();
  15081. //不能把自己reset了
  15082. me.fireEvent("reset", true);
  15083. };
  15084. this.getScene = function() {
  15085. var me = this.editor;
  15086. var rng = me.selection.getRange(),
  15087. rngAddress = rng.createAddress(false, true);
  15088. me.fireEvent("beforegetscene");
  15089. var root = UE.htmlparser(me.body.innerHTML);
  15090. me.options.autoClearEmptyNode = false;
  15091. me.filterOutputRule(root);
  15092. me.options.autoClearEmptyNode = orgState;
  15093. var cont = root.toHtml();
  15094. //trace:3461
  15095. //这个会引起回退时导致空格丢失的情况
  15096. // browser.ie && (cont = cont.replace(/>&nbsp;</g, '><').replace(/\s*</g, '<').replace(/>\s*/g, '>'));
  15097. me.fireEvent("aftergetscene");
  15098. return {
  15099. address: rngAddress,
  15100. content: cont
  15101. };
  15102. };
  15103. this.save = function(notCompareRange, notSetCursor) {
  15104. clearTimeout(saveSceneTimer);
  15105. var currentScene = this.getScene(notSetCursor),
  15106. lastScene = this.list[this.index];
  15107. if (lastScene && lastScene.content != currentScene.content) {
  15108. me.trigger("contentchange");
  15109. }
  15110. //内容相同位置相同不存
  15111. if (
  15112. lastScene &&
  15113. lastScene.content == currentScene.content &&
  15114. (notCompareRange
  15115. ? 1
  15116. : compareRangeAddress(lastScene.address, currentScene.address))
  15117. ) {
  15118. return;
  15119. }
  15120. this.list = this.list.slice(0, this.index + 1);
  15121. this.list.push(currentScene);
  15122. //如果大于最大数量了,就把最前的剔除
  15123. if (this.list.length > maxUndoCount) {
  15124. this.list.shift();
  15125. }
  15126. this.index = this.list.length - 1;
  15127. this.clearKey();
  15128. //跟新undo/redo状态
  15129. this.update();
  15130. };
  15131. this.update = function() {
  15132. this.hasRedo = !!this.list[this.index + 1];
  15133. this.hasUndo = !!this.list[this.index - 1];
  15134. };
  15135. this.reset = function() {
  15136. this.list = [];
  15137. this.index = 0;
  15138. this.hasUndo = false;
  15139. this.hasRedo = false;
  15140. this.clearKey();
  15141. };
  15142. this.clearKey = function() {
  15143. keycont = 0;
  15144. lastKeyCode = null;
  15145. };
  15146. }
  15147. me.undoManger = new UndoManager();
  15148. me.undoManger.editor = me;
  15149. function saveScene() {
  15150. this.undoManger.save();
  15151. }
  15152. me.addListener("saveScene", function() {
  15153. var args = Array.prototype.splice.call(arguments, 1);
  15154. this.undoManger.save.apply(this.undoManger, args);
  15155. });
  15156. // me.addListener('beforeexeccommand', saveScene);
  15157. // me.addListener('afterexeccommand', saveScene);
  15158. me.addListener("reset", function(type, exclude) {
  15159. if (!exclude) {
  15160. this.undoManger.reset();
  15161. }
  15162. });
  15163. me.commands["redo"] = me.commands["undo"] = {
  15164. execCommand: function(cmdName) {
  15165. this.undoManger[cmdName]();
  15166. },
  15167. queryCommandState: function(cmdName) {
  15168. return this.undoManger[
  15169. "has" + (cmdName.toLowerCase() == "undo" ? "Undo" : "Redo")
  15170. ]
  15171. ? 0
  15172. : -1;
  15173. },
  15174. notNeedUndo: 1
  15175. };
  15176. var keys = {
  15177. // /*Backspace*/ 8:1, /*Delete*/ 46:1,
  15178. /*Shift*/ 16: 1,
  15179. /*Ctrl*/ 17: 1,
  15180. /*Alt*/ 18: 1,
  15181. 37: 1,
  15182. 38: 1,
  15183. 39: 1,
  15184. 40: 1
  15185. },
  15186. keycont = 0,
  15187. lastKeyCode;
  15188. //输入法状态下不计算字符数
  15189. var inputType = false;
  15190. me.addListener("ready", function() {
  15191. domUtils.on(this.body, "compositionstart", function() {
  15192. inputType = true;
  15193. });
  15194. domUtils.on(this.body, "compositionend", function() {
  15195. inputType = false;
  15196. });
  15197. });
  15198. //快捷键
  15199. me.addshortcutkey({
  15200. Undo: "ctrl+90", //undo
  15201. Redo: "ctrl+89" //redo
  15202. });
  15203. var isCollapsed = true;
  15204. me.addListener("keydown", function(type, evt) {
  15205. var me = this;
  15206. var keyCode = evt.keyCode || evt.which;
  15207. if (
  15208. !keys[keyCode] &&
  15209. !evt.ctrlKey &&
  15210. !evt.metaKey &&
  15211. !evt.shiftKey &&
  15212. !evt.altKey
  15213. ) {
  15214. if (inputType) return;
  15215. if (!me.selection.getRange().collapsed) {
  15216. me.undoManger.save(false, true);
  15217. isCollapsed = false;
  15218. return;
  15219. }
  15220. if (me.undoManger.list.length == 0) {
  15221. me.undoManger.save(true);
  15222. }
  15223. clearTimeout(saveSceneTimer);
  15224. function save(cont) {
  15225. cont.undoManger.save(false, true);
  15226. cont.fireEvent("selectionchange");
  15227. }
  15228. saveSceneTimer = setTimeout(function() {
  15229. if (inputType) {
  15230. var interalTimer = setInterval(function() {
  15231. if (!inputType) {
  15232. save(me);
  15233. clearInterval(interalTimer);
  15234. }
  15235. }, 300);
  15236. return;
  15237. }
  15238. save(me);
  15239. }, 200);
  15240. lastKeyCode = keyCode;
  15241. keycont++;
  15242. if (keycont >= maxInputCount) {
  15243. save(me);
  15244. }
  15245. }
  15246. });
  15247. me.addListener("keyup", function(type, evt) {
  15248. var keyCode = evt.keyCode || evt.which;
  15249. if (
  15250. !keys[keyCode] &&
  15251. !evt.ctrlKey &&
  15252. !evt.metaKey &&
  15253. !evt.shiftKey &&
  15254. !evt.altKey
  15255. ) {
  15256. if (inputType) return;
  15257. if (!isCollapsed) {
  15258. this.undoManger.save(false, true);
  15259. isCollapsed = true;
  15260. }
  15261. }
  15262. });
  15263. //扩展实例,添加关闭和开启命令undo
  15264. me.stopCmdUndo = function() {
  15265. me.__hasEnterExecCommand = true;
  15266. };
  15267. me.startCmdUndo = function() {
  15268. me.__hasEnterExecCommand = false;
  15269. };
  15270. };
  15271. // plugins/copy.js
  15272. UE.plugin.register("copy", function() {
  15273. var me = this;
  15274. function initZeroClipboard() {
  15275. ZeroClipboard.config({
  15276. debug: false,
  15277. swfPath:
  15278. me.options.UEDITOR_HOME_URL +
  15279. "third-party/zeroclipboard/ZeroClipboard.swf"
  15280. });
  15281. var client = (me.zeroclipboard = new ZeroClipboard());
  15282. // 复制内容
  15283. client.on("copy", function(e) {
  15284. var client = e.client,
  15285. rng = me.selection.getRange(),
  15286. div = document.createElement("div");
  15287. div.appendChild(rng.cloneContents());
  15288. client.setText(div.innerText || div.textContent);
  15289. client.setHtml(div.innerHTML);
  15290. rng.select();
  15291. });
  15292. // hover事件传递到target
  15293. client.on("mouseover mouseout", function(e) {
  15294. var target = e.target;
  15295. if (target) {
  15296. if (e.type == "mouseover") {
  15297. domUtils.addClass(target, "edui-state-hover");
  15298. } else if (e.type == "mouseout") {
  15299. domUtils.removeClasses(target, "edui-state-hover");
  15300. }
  15301. }
  15302. });
  15303. // flash加载不成功
  15304. client.on("wrongflash noflash", function() {
  15305. ZeroClipboard.destroy();
  15306. });
  15307. // 触发事件
  15308. me.fireEvent("zeroclipboardready", client);
  15309. }
  15310. return {
  15311. bindEvents: {
  15312. ready: function() {
  15313. if (!browser.ie) {
  15314. if (window.ZeroClipboard) {
  15315. initZeroClipboard();
  15316. } else {
  15317. utils.loadFile(
  15318. document,
  15319. {
  15320. src:
  15321. me.options.UEDITOR_HOME_URL +
  15322. "third-party/zeroclipboard/ZeroClipboard.js",
  15323. tag: "script",
  15324. type: "text/javascript",
  15325. defer: "defer"
  15326. },
  15327. function() {
  15328. initZeroClipboard();
  15329. }
  15330. );
  15331. }
  15332. }
  15333. }
  15334. },
  15335. commands: {
  15336. copy: {
  15337. execCommand: function(cmd) {
  15338. if (!me.document.execCommand("copy")) {
  15339. alert(me.getLang("copymsg"));
  15340. }
  15341. }
  15342. }
  15343. }
  15344. };
  15345. });
  15346. // plugins/paste.js
  15347. ///import core
  15348. ///import plugins/inserthtml.js
  15349. ///import plugins/undo.js
  15350. ///import plugins/serialize.js
  15351. ///commands 粘贴
  15352. ///commandsName PastePlain
  15353. ///commandsTitle 纯文本粘贴模式
  15354. /**
  15355. * @description 粘贴
  15356. * @author zhanyi
  15357. */
  15358. UE.plugins["paste"] = function() {
  15359. function getClipboardData(callback) {
  15360. var doc = this.document;
  15361. if (doc.getElementById("baidu_pastebin")) {
  15362. return;
  15363. }
  15364. var range = this.selection.getRange(),
  15365. bk = range.createBookmark(),
  15366. //创建剪贴的容器div
  15367. pastebin = doc.createElement("div");
  15368. pastebin.id = "baidu_pastebin";
  15369. // Safari 要求div必须有内容,才能粘贴内容进来
  15370. browser.webkit &&
  15371. pastebin.appendChild(
  15372. doc.createTextNode(domUtils.fillChar + domUtils.fillChar)
  15373. );
  15374. doc.body.appendChild(pastebin);
  15375. //trace:717 隐藏的span不能得到top
  15376. //bk.start.innerHTML = '&nbsp;';
  15377. bk.start.style.display = "";
  15378. pastebin.style.cssText =
  15379. "position:absolute;width:1px;height:1px;overflow:hidden;left:-1000px;white-space:nowrap;top:" +
  15380. //要在现在光标平行的位置加入,否则会出现跳动的问题
  15381. domUtils.getXY(bk.start).y +
  15382. "px";
  15383. range.selectNodeContents(pastebin).select(true);
  15384. setTimeout(function() {
  15385. if (browser.webkit) {
  15386. for (
  15387. var i = 0, pastebins = doc.querySelectorAll("#baidu_pastebin"), pi;
  15388. (pi = pastebins[i++]);
  15389. ) {
  15390. if (domUtils.isEmptyNode(pi)) {
  15391. domUtils.remove(pi);
  15392. } else {
  15393. pastebin = pi;
  15394. break;
  15395. }
  15396. }
  15397. }
  15398. try {
  15399. pastebin.parentNode.removeChild(pastebin);
  15400. } catch (e) {}
  15401. range.moveToBookmark(bk).select(true);
  15402. callback(pastebin);
  15403. }, 0);
  15404. }
  15405. var me = this;
  15406. me.setOpt({
  15407. retainOnlyLabelPasted: false
  15408. });
  15409. var txtContent, htmlContent, address;
  15410. function getPureHtml(html) {
  15411. return html.replace(/<(\/?)([\w\-]+)([^>]*)>/gi, function(
  15412. a,
  15413. b,
  15414. tagName,
  15415. attrs
  15416. ) {
  15417. tagName = tagName.toLowerCase();
  15418. if ({ img: 1 }[tagName]) {
  15419. return a;
  15420. }
  15421. attrs = attrs.replace(
  15422. /([\w\-]*?)\s*=\s*(("([^"]*)")|('([^']*)')|([^\s>]+))/gi,
  15423. function(str, atr, val) {
  15424. if (
  15425. {
  15426. src: 1,
  15427. href: 1,
  15428. name: 1
  15429. }[atr.toLowerCase()]
  15430. ) {
  15431. return atr + "=" + val + " ";
  15432. }
  15433. return "";
  15434. }
  15435. );
  15436. if (
  15437. {
  15438. span: 1,
  15439. div: 1
  15440. }[tagName]
  15441. ) {
  15442. return "";
  15443. } else {
  15444. return "<" + b + tagName + " " + utils.trim(attrs) + ">";
  15445. }
  15446. });
  15447. }
  15448. function filter(div) {
  15449. var html;
  15450. if (div.firstChild) {
  15451. //去掉cut中添加的边界值
  15452. var nodes = domUtils.getElementsByTagName(div, "span");
  15453. for (var i = 0, ni; (ni = nodes[i++]); ) {
  15454. if (ni.id == "_baidu_cut_start" || ni.id == "_baidu_cut_end") {
  15455. domUtils.remove(ni);
  15456. }
  15457. }
  15458. if (browser.webkit) {
  15459. var brs = div.querySelectorAll("div br");
  15460. for (var i = 0, bi; (bi = brs[i++]); ) {
  15461. var pN = bi.parentNode;
  15462. if (pN.tagName == "DIV" && pN.childNodes.length == 1) {
  15463. pN.innerHTML = "<p><br/></p>";
  15464. domUtils.remove(pN);
  15465. }
  15466. }
  15467. var divs = div.querySelectorAll("#baidu_pastebin");
  15468. for (var i = 0, di; (di = divs[i++]); ) {
  15469. var tmpP = me.document.createElement("p");
  15470. di.parentNode.insertBefore(tmpP, di);
  15471. while (di.firstChild) {
  15472. tmpP.appendChild(di.firstChild);
  15473. }
  15474. domUtils.remove(di);
  15475. }
  15476. var metas = div.querySelectorAll("meta");
  15477. for (var i = 0, ci; (ci = metas[i++]); ) {
  15478. domUtils.remove(ci);
  15479. }
  15480. var brs = div.querySelectorAll("br");
  15481. for (i = 0; (ci = brs[i++]); ) {
  15482. if (/^apple-/i.test(ci.className)) {
  15483. domUtils.remove(ci);
  15484. }
  15485. }
  15486. }
  15487. if (browser.gecko) {
  15488. var dirtyNodes = div.querySelectorAll("[_moz_dirty]");
  15489. for (i = 0; (ci = dirtyNodes[i++]); ) {
  15490. ci.removeAttribute("_moz_dirty");
  15491. }
  15492. }
  15493. if (!browser.ie) {
  15494. var spans = div.querySelectorAll("span.Apple-style-span");
  15495. for (var i = 0, ci; (ci = spans[i++]); ) {
  15496. domUtils.remove(ci, true);
  15497. }
  15498. }
  15499. //ie下使用innerHTML会产生多余的\r\n字符,也会产生&nbsp;这里过滤掉
  15500. html = div.innerHTML; //.replace(/>(?:(\s|&nbsp;)*?)</g,'><');
  15501. //过滤word粘贴过来的冗余属性
  15502. html = UE.filterWord(html);
  15503. //取消了忽略空白的第二个参数,粘贴过来的有些是有空白的,会被套上相关的标签
  15504. var root = UE.htmlparser(html);
  15505. //如果给了过滤规则就先进行过滤
  15506. if (me.options.filterRules) {
  15507. UE.filterNode(root, me.options.filterRules);
  15508. }
  15509. //执行默认的处理
  15510. me.filterInputRule(root);
  15511. //针对chrome的处理
  15512. if (browser.webkit) {
  15513. var br = root.lastChild();
  15514. if (br && br.type == "element" && br.tagName == "br") {
  15515. root.removeChild(br);
  15516. }
  15517. utils.each(me.body.querySelectorAll("div"), function(node) {
  15518. if (domUtils.isEmptyBlock(node)) {
  15519. domUtils.remove(node, true);
  15520. }
  15521. });
  15522. }
  15523. html = { html: root.toHtml() };
  15524. me.fireEvent("beforepaste", html, root);
  15525. //抢了默认的粘贴,那后边的内容就不执行了,比如表格粘贴
  15526. if (!html.html) {
  15527. return;
  15528. }
  15529. root = UE.htmlparser(html.html, true);
  15530. //如果开启了纯文本模式
  15531. if (me.queryCommandState("pasteplain") === 1) {
  15532. me.execCommand(
  15533. "insertHtml",
  15534. UE.filterNode(root, me.options.filterTxtRules).toHtml(),
  15535. true
  15536. );
  15537. } else {
  15538. //文本模式
  15539. UE.filterNode(root, me.options.filterTxtRules);
  15540. txtContent = root.toHtml();
  15541. //完全模式
  15542. htmlContent = html.html;
  15543. address = me.selection.getRange().createAddress(true);
  15544. me.execCommand(
  15545. "insertHtml",
  15546. me.getOpt("retainOnlyLabelPasted") === true
  15547. ? getPureHtml(htmlContent)
  15548. : htmlContent,
  15549. true
  15550. );
  15551. }
  15552. me.fireEvent("afterpaste", html);
  15553. }
  15554. }
  15555. me.addListener("pasteTransfer", function(cmd, plainType) {
  15556. if (address && txtContent && htmlContent && txtContent != htmlContent) {
  15557. var range = me.selection.getRange();
  15558. range.moveToAddress(address, true);
  15559. if (!range.collapsed) {
  15560. while (!domUtils.isBody(range.startContainer)) {
  15561. var start = range.startContainer;
  15562. if (start.nodeType == 1) {
  15563. start = start.childNodes[range.startOffset];
  15564. if (!start) {
  15565. range.setStartBefore(range.startContainer);
  15566. continue;
  15567. }
  15568. var pre = start.previousSibling;
  15569. if (
  15570. pre &&
  15571. pre.nodeType == 3 &&
  15572. new RegExp("^[\n\r\t " + domUtils.fillChar + "]*$").test(
  15573. pre.nodeValue
  15574. )
  15575. ) {
  15576. range.setStartBefore(pre);
  15577. }
  15578. }
  15579. if (range.startOffset == 0) {
  15580. range.setStartBefore(range.startContainer);
  15581. } else {
  15582. break;
  15583. }
  15584. }
  15585. while (!domUtils.isBody(range.endContainer)) {
  15586. var end = range.endContainer;
  15587. if (end.nodeType == 1) {
  15588. end = end.childNodes[range.endOffset];
  15589. if (!end) {
  15590. range.setEndAfter(range.endContainer);
  15591. continue;
  15592. }
  15593. var next = end.nextSibling;
  15594. if (
  15595. next &&
  15596. next.nodeType == 3 &&
  15597. new RegExp("^[\n\r\t" + domUtils.fillChar + "]*$").test(
  15598. next.nodeValue
  15599. )
  15600. ) {
  15601. range.setEndAfter(next);
  15602. }
  15603. }
  15604. if (
  15605. range.endOffset ==
  15606. range.endContainer[
  15607. range.endContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  15608. ].length
  15609. ) {
  15610. range.setEndAfter(range.endContainer);
  15611. } else {
  15612. break;
  15613. }
  15614. }
  15615. }
  15616. range.deleteContents();
  15617. range.select(true);
  15618. me.__hasEnterExecCommand = true;
  15619. var html = htmlContent;
  15620. if (plainType === 2) {
  15621. html = getPureHtml(html);
  15622. } else if (plainType) {
  15623. html = txtContent;
  15624. }
  15625. me.execCommand("inserthtml", html, true);
  15626. me.__hasEnterExecCommand = false;
  15627. var rng = me.selection.getRange();
  15628. while (
  15629. !domUtils.isBody(rng.startContainer) &&
  15630. !rng.startOffset &&
  15631. rng.startContainer[
  15632. rng.startContainer.nodeType == 3 ? "nodeValue" : "childNodes"
  15633. ].length
  15634. ) {
  15635. rng.setStartBefore(rng.startContainer);
  15636. }
  15637. var tmpAddress = rng.createAddress(true);
  15638. address.endAddress = tmpAddress.startAddress;
  15639. }
  15640. });
  15641. me.addListener("ready", function() {
  15642. domUtils.on(me.body, "cut", function() {
  15643. var range = me.selection.getRange();
  15644. if (!range.collapsed && me.undoManger) {
  15645. me.undoManger.save();
  15646. }
  15647. });
  15648. //ie下beforepaste在点击右键时也会触发,所以用监控键盘才处理
  15649. domUtils.on(
  15650. me.body,
  15651. browser.ie || browser.opera ? "keydown" : "paste",
  15652. function(e) {
  15653. if (
  15654. (browser.ie || browser.opera) &&
  15655. ((!e.ctrlKey && !e.metaKey) || e.keyCode != "86")
  15656. ) {
  15657. return;
  15658. }
  15659. getClipboardData.call(me, function(div) {
  15660. filter(div);
  15661. });
  15662. }
  15663. );
  15664. });
  15665. me.commands["paste"] = {
  15666. execCommand: function(cmd) {
  15667. if (browser.ie) {
  15668. getClipboardData.call(me, function(div) {
  15669. filter(div);
  15670. });
  15671. me.document.execCommand("paste");
  15672. } else {
  15673. alert(me.getLang("pastemsg"));
  15674. }
  15675. }
  15676. };
  15677. };
  15678. // plugins/puretxtpaste.js
  15679. /**
  15680. * 纯文本粘贴插件
  15681. * @file
  15682. * @since 1.2.6.1
  15683. */
  15684. UE.plugins["pasteplain"] = function() {
  15685. var me = this;
  15686. me.setOpt({
  15687. pasteplain: false,
  15688. filterTxtRules: (function() {
  15689. function transP(node) {
  15690. node.tagName = "p";
  15691. node.setStyle();
  15692. }
  15693. function removeNode(node) {
  15694. node.parentNode.removeChild(node, true);
  15695. }
  15696. return {
  15697. //直接删除及其字节点内容
  15698. "-": "script style object iframe embed input select",
  15699. p: { $: {} },
  15700. br: { $: {} },
  15701. div: function(node) {
  15702. var tmpNode,
  15703. p = UE.uNode.createElement("p");
  15704. while ((tmpNode = node.firstChild())) {
  15705. if (tmpNode.type == "text" || !UE.dom.dtd.$block[tmpNode.tagName]) {
  15706. p.appendChild(tmpNode);
  15707. } else {
  15708. if (p.firstChild()) {
  15709. node.parentNode.insertBefore(p, node);
  15710. p = UE.uNode.createElement("p");
  15711. } else {
  15712. node.parentNode.insertBefore(tmpNode, node);
  15713. }
  15714. }
  15715. }
  15716. if (p.firstChild()) {
  15717. node.parentNode.insertBefore(p, node);
  15718. }
  15719. node.parentNode.removeChild(node);
  15720. },
  15721. ol: removeNode,
  15722. ul: removeNode,
  15723. dl: removeNode,
  15724. dt: removeNode,
  15725. dd: removeNode,
  15726. li: removeNode,
  15727. caption: transP,
  15728. th: transP,
  15729. tr: transP,
  15730. h1: transP,
  15731. h2: transP,
  15732. h3: transP,
  15733. h4: transP,
  15734. h5: transP,
  15735. h6: transP,
  15736. td: function(node) {
  15737. //没有内容的td直接删掉
  15738. var txt = !!node.innerText();
  15739. if (txt) {
  15740. node.parentNode.insertAfter(
  15741. UE.uNode.createText(" &nbsp; &nbsp;"),
  15742. node
  15743. );
  15744. }
  15745. node.parentNode.removeChild(node, node.innerText());
  15746. }
  15747. };
  15748. })()
  15749. });
  15750. //暂时这里支持一下老版本的属性
  15751. var pasteplain = me.options.pasteplain;
  15752. /**
  15753. * 启用或取消纯文本粘贴模式
  15754. * @command pasteplain
  15755. * @method execCommand
  15756. * @param { String } cmd 命令字符串
  15757. * @example
  15758. * ```javascript
  15759. * editor.queryCommandState( 'pasteplain' );
  15760. * ```
  15761. */
  15762. /**
  15763. * 查询当前是否处于纯文本粘贴模式
  15764. * @command pasteplain
  15765. * @method queryCommandState
  15766. * @param { String } cmd 命令字符串
  15767. * @return { int } 如果处于纯文本模式,返回1,否则,返回0
  15768. * @example
  15769. * ```javascript
  15770. * editor.queryCommandState( 'pasteplain' );
  15771. * ```
  15772. */
  15773. me.commands["pasteplain"] = {
  15774. queryCommandState: function() {
  15775. return pasteplain ? 1 : 0;
  15776. },
  15777. execCommand: function() {
  15778. pasteplain = !pasteplain | 0;
  15779. },
  15780. notNeedUndo: 1
  15781. };
  15782. };
  15783. // plugins/list.js
  15784. /**
  15785. * 有序列表,无序列表插件
  15786. * @file
  15787. * @since 1.2.6.1
  15788. */
  15789. UE.plugins["list"] = function() {
  15790. var me = this,
  15791. notExchange = {
  15792. TD: 1,
  15793. PRE: 1,
  15794. BLOCKQUOTE: 1
  15795. };
  15796. var customStyle = {
  15797. cn: "cn-1-",
  15798. cn1: "cn-2-",
  15799. cn2: "cn-3-",
  15800. num: "num-1-",
  15801. num1: "num-2-",
  15802. num2: "num-3-",
  15803. dash: "dash",
  15804. dot: "dot"
  15805. };
  15806. me.setOpt({
  15807. autoTransWordToList: false,
  15808. insertorderedlist: {
  15809. num: "",
  15810. num1: "",
  15811. num2: "",
  15812. cn: "",
  15813. cn1: "",
  15814. cn2: "",
  15815. decimal: "",
  15816. "lower-alpha": "",
  15817. "lower-roman": "",
  15818. "upper-alpha": "",
  15819. "upper-roman": ""
  15820. },
  15821. insertunorderedlist: {
  15822. circle: "",
  15823. disc: "",
  15824. square: "",
  15825. dash: "",
  15826. dot: ""
  15827. },
  15828. listDefaultPaddingLeft: "30",
  15829. listiconpath: "http://bs.baidu.com/listicon/",
  15830. maxListLevel: -1, //-1不限制
  15831. disablePInList: false
  15832. });
  15833. function listToArray(list) {
  15834. var arr = [];
  15835. for (var p in list) {
  15836. arr.push(p);
  15837. }
  15838. return arr;
  15839. }
  15840. var listStyle = {
  15841. OL: listToArray(me.options.insertorderedlist),
  15842. UL: listToArray(me.options.insertunorderedlist)
  15843. };
  15844. var liiconpath = me.options.listiconpath;
  15845. //根据用户配置,调整customStyle
  15846. for (var s in customStyle) {
  15847. if (
  15848. !me.options.insertorderedlist.hasOwnProperty(s) &&
  15849. !me.options.insertunorderedlist.hasOwnProperty(s)
  15850. ) {
  15851. delete customStyle[s];
  15852. }
  15853. }
  15854. me.ready(function() {
  15855. var customCss = [];
  15856. for (var p in customStyle) {
  15857. if (p == "dash" || p == "dot") {
  15858. customCss.push(
  15859. "li.list-" +
  15860. customStyle[p] +
  15861. "{background-image:url(" +
  15862. liiconpath +
  15863. customStyle[p] +
  15864. ".gif)}"
  15865. );
  15866. customCss.push(
  15867. "ul.custom_" +
  15868. p +
  15869. "{list-style:none;}ul.custom_" +
  15870. p +
  15871. " li{background-position:0 3px;background-repeat:no-repeat}"
  15872. );
  15873. } else {
  15874. for (var i = 0; i < 99; i++) {
  15875. customCss.push(
  15876. "li.list-" +
  15877. customStyle[p] +
  15878. i +
  15879. "{background-image:url(" +
  15880. liiconpath +
  15881. "list-" +
  15882. customStyle[p] +
  15883. i +
  15884. ".gif)}"
  15885. );
  15886. }
  15887. customCss.push(
  15888. "ol.custom_" +
  15889. p +
  15890. "{list-style:none;}ol.custom_" +
  15891. p +
  15892. " li{background-position:0 3px;background-repeat:no-repeat}"
  15893. );
  15894. }
  15895. switch (p) {
  15896. case "cn":
  15897. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:25px}");
  15898. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:40px}");
  15899. customCss.push("li.list-" + p + "-paddingleft-3{padding-left:55px}");
  15900. break;
  15901. case "cn1":
  15902. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:30px}");
  15903. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:40px}");
  15904. customCss.push("li.list-" + p + "-paddingleft-3{padding-left:55px}");
  15905. break;
  15906. case "cn2":
  15907. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:40px}");
  15908. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:55px}");
  15909. customCss.push("li.list-" + p + "-paddingleft-3{padding-left:68px}");
  15910. break;
  15911. case "num":
  15912. case "num1":
  15913. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:25px}");
  15914. break;
  15915. case "num2":
  15916. customCss.push("li.list-" + p + "-paddingleft-1{padding-left:35px}");
  15917. customCss.push("li.list-" + p + "-paddingleft-2{padding-left:40px}");
  15918. break;
  15919. case "dash":
  15920. customCss.push("li.list-" + p + "-paddingleft{padding-left:35px}");
  15921. break;
  15922. case "dot":
  15923. customCss.push("li.list-" + p + "-paddingleft{padding-left:20px}");
  15924. }
  15925. }
  15926. customCss.push(".list-paddingleft-1{padding-left:0}");
  15927. customCss.push(
  15928. ".list-paddingleft-2{padding-left:" +
  15929. me.options.listDefaultPaddingLeft +
  15930. "px}"
  15931. );
  15932. customCss.push(
  15933. ".list-paddingleft-3{padding-left:" +
  15934. me.options.listDefaultPaddingLeft * 2 +
  15935. "px}"
  15936. );
  15937. //如果不给宽度会在自定应样式里出现滚动条
  15938. utils.cssRule(
  15939. "list",
  15940. "ol,ul{margin:0;pading:0;" +
  15941. (browser.ie ? "" : "width:95%") +
  15942. "}li{clear:both;}" +
  15943. customCss.join("\n"),
  15944. me.document
  15945. );
  15946. });
  15947. //单独处理剪切的问题
  15948. me.ready(function() {
  15949. domUtils.on(me.body, "cut", function() {
  15950. setTimeout(function() {
  15951. var rng = me.selection.getRange(),
  15952. li;
  15953. //trace:3416
  15954. if (!rng.collapsed) {
  15955. if (
  15956. (li = domUtils.findParentByTagName(rng.startContainer, "li", true))
  15957. ) {
  15958. if (!li.nextSibling && domUtils.isEmptyBlock(li)) {
  15959. var pn = li.parentNode,
  15960. node;
  15961. if ((node = pn.previousSibling)) {
  15962. domUtils.remove(pn);
  15963. rng.setStartAtLast(node).collapse(true);
  15964. rng.select(true);
  15965. } else if ((node = pn.nextSibling)) {
  15966. domUtils.remove(pn);
  15967. rng.setStartAtFirst(node).collapse(true);
  15968. rng.select(true);
  15969. } else {
  15970. var tmpNode = me.document.createElement("p");
  15971. domUtils.fillNode(me.document, tmpNode);
  15972. pn.parentNode.insertBefore(tmpNode, pn);
  15973. domUtils.remove(pn);
  15974. rng.setStart(tmpNode, 0).collapse(true);
  15975. rng.select(true);
  15976. }
  15977. }
  15978. }
  15979. }
  15980. });
  15981. });
  15982. });
  15983. function getStyle(node) {
  15984. var cls = node.className;
  15985. if (domUtils.hasClass(node, /custom_/)) {
  15986. return cls.match(/custom_(\w+)/)[1];
  15987. }
  15988. return domUtils.getStyle(node, "list-style-type");
  15989. }
  15990. me.addListener("beforepaste", function(type, html) {
  15991. var me = this,
  15992. rng = me.selection.getRange(),
  15993. li;
  15994. var root = UE.htmlparser(html.html, true);
  15995. if ((li = domUtils.findParentByTagName(rng.startContainer, "li", true))) {
  15996. var list = li.parentNode,
  15997. tagName = list.tagName === "OL" ? "ul" : "ol";
  15998. utils.each(root.getNodesByTagName(tagName), function(n) {
  15999. n.tagName = list.tagName;
  16000. n.setAttr();
  16001. if (n.parentNode === root) {
  16002. type = getStyle(list) || (list.tagName == "OL" ? "decimal" : "disc");
  16003. } else {
  16004. var className = n.parentNode.getAttr("class");
  16005. if (className && /custom_/.test(className)) {
  16006. type = className.match(/custom_(\w+)/)[1];
  16007. } else {
  16008. type = n.parentNode.getStyle("list-style-type");
  16009. }
  16010. if (!type) {
  16011. type = list.tagName === "OL" ? "decimal" : "disc";
  16012. }
  16013. }
  16014. var index = utils.indexOf(listStyle[list.tagName], type);
  16015. if (n.parentNode !== root)
  16016. index = index + 1 === listStyle[list.tagName].length ? 0 : index + 1;
  16017. var currentStyle = listStyle[list.tagName][index];
  16018. if (customStyle[currentStyle]) {
  16019. n.setAttr("class", "custom_" + currentStyle);
  16020. } else {
  16021. n.setStyle("list-style-type", currentStyle);
  16022. }
  16023. });
  16024. }
  16025. html.html = root.toHtml();
  16026. });
  16027. //导出时,去掉p标签
  16028. me.getOpt("disablePInList") === true &&
  16029. me.addOutputRule(function(root) {
  16030. utils.each(root.getNodesByTagName("li"), function(li) {
  16031. var newChildrens = [],
  16032. index = 0;
  16033. utils.each(li.children, function(n) {
  16034. if (n.tagName == "p") {
  16035. var tmpNode;
  16036. while ((tmpNode = n.children.pop())) {
  16037. newChildrens.splice(index, 0, tmpNode);
  16038. tmpNode.parentNode = li;
  16039. lastNode = tmpNode;
  16040. }
  16041. tmpNode = newChildrens[newChildrens.length - 1];
  16042. if (
  16043. !tmpNode ||
  16044. tmpNode.type !== "element" ||
  16045. tmpNode.tagName !== "br"
  16046. ) {
  16047. var br = UE.uNode.createElement("br");
  16048. br.parentNode = li;
  16049. newChildrens.push(br);
  16050. }
  16051. index = newChildrens.length;
  16052. }
  16053. });
  16054. if (newChildrens.length) {
  16055. li.children = newChildrens;
  16056. }
  16057. });
  16058. });
  16059. //进入编辑器的li要套p标签
  16060. me.addInputRule(function(root) {
  16061. utils.each(root.getNodesByTagName("li"), function(li) {
  16062. var tmpP = UE.uNode.createElement("p");
  16063. for (var i = 0, ci; (ci = li.children[i]); ) {
  16064. if (ci.type === "text" || dtd.p[ci.tagName]) {
  16065. tmpP.appendChild(ci);
  16066. } else {
  16067. if (tmpP.firstChild()) {
  16068. li.insertBefore(tmpP, ci);
  16069. tmpP = UE.uNode.createElement("p");
  16070. i = i + 2;
  16071. } else {
  16072. i++;
  16073. }
  16074. }
  16075. }
  16076. if ((tmpP.firstChild() && !tmpP.parentNode) || !li.firstChild()) {
  16077. li.appendChild(tmpP);
  16078. }
  16079. //trace:3357
  16080. //p不能为空
  16081. if (!tmpP.firstChild()) {
  16082. tmpP.innerHTML(browser.ie ? "&nbsp;" : "<br/>");
  16083. }
  16084. //去掉末尾的空白
  16085. var p = li.firstChild();
  16086. var lastChild = p.lastChild();
  16087. if (
  16088. lastChild &&
  16089. lastChild.type === "text" &&
  16090. /^\s*$/.test(lastChild.data)
  16091. ) {
  16092. p.removeChild(lastChild);
  16093. }
  16094. });
  16095. if (me.options.autoTransWordToList) {
  16096. var orderlisttype = {
  16097. num1: /^\d+\)/,
  16098. decimal: /^\d+\./,
  16099. "lower-alpha": /^[a-z]+\)/,
  16100. "upper-alpha": /^[A-Z]+\./,
  16101. cn: /^[\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+[\u3001]/,
  16102. cn2: /^\([\u4E00\u4E8C\u4E09\u56DB\u516d\u4e94\u4e03\u516b\u4e5d]+\)/
  16103. },
  16104. unorderlisttype = {
  16105. square: "n"
  16106. };
  16107. function checkListType(content, container) {
  16108. var span = container.firstChild();
  16109. if (
  16110. span &&
  16111. span.type === "element" &&
  16112. span.tagName === "span" &&
  16113. /Wingdings|Symbol/.test(span.getStyle("font-family"))
  16114. ) {
  16115. for (var p in unorderlisttype) {
  16116. if (unorderlisttype[p] == span.data) {
  16117. return p;
  16118. }
  16119. }
  16120. return "disc";
  16121. }
  16122. for (var p in orderlisttype) {
  16123. if (orderlisttype[p].test(content)) {
  16124. return p;
  16125. }
  16126. }
  16127. }
  16128. utils.each(root.getNodesByTagName("p"), function(node) {
  16129. if (node.getAttr("class") !== "MsoListParagraph") {
  16130. return;
  16131. }
  16132. //word粘贴过来的会带有margin要去掉,但这样也可能会误命中一些央视
  16133. node.setStyle("margin", "");
  16134. node.setStyle("margin-left", "");
  16135. node.setAttr("class", "");
  16136. function appendLi(list, p, type) {
  16137. if (list.tagName === "ol") {
  16138. if (browser.ie) {
  16139. var first = p.firstChild();
  16140. if (
  16141. first.type === "element" &&
  16142. first.tagName === "span" &&
  16143. orderlisttype[type].test(first.innerText())
  16144. ) {
  16145. p.removeChild(first);
  16146. }
  16147. } else {
  16148. p.innerHTML(p.innerHTML().replace(orderlisttype[type], ""));
  16149. }
  16150. } else {
  16151. p.removeChild(p.firstChild());
  16152. }
  16153. var li = UE.uNode.createElement("li");
  16154. li.appendChild(p);
  16155. list.appendChild(li);
  16156. }
  16157. var tmp = node,
  16158. type,
  16159. cacheNode = node;
  16160. if (
  16161. node.parentNode.tagName !== "li" &&
  16162. (type = checkListType(node.innerText(), node))
  16163. ) {
  16164. var list = UE.uNode.createElement(
  16165. me.options.insertorderedlist.hasOwnProperty(type) ? "ol" : "ul"
  16166. );
  16167. if (customStyle[type]) {
  16168. list.setAttr("class", "custom_" + type);
  16169. } else {
  16170. list.setStyle("list-style-type", type);
  16171. }
  16172. while (
  16173. node &&
  16174. node.parentNode.tagName !== "li" &&
  16175. checkListType(node.innerText(), node)
  16176. ) {
  16177. tmp = node.nextSibling();
  16178. if (!tmp) {
  16179. node.parentNode.insertBefore(list, node);
  16180. }
  16181. appendLi(list, node, type);
  16182. node = tmp;
  16183. }
  16184. if (!list.parentNode && node && node.parentNode) {
  16185. node.parentNode.insertBefore(list, node);
  16186. }
  16187. }
  16188. var span = cacheNode.firstChild();
  16189. if (
  16190. span &&
  16191. span.type == "element" &&
  16192. span.tagName == "span" &&
  16193. /^\s*(&nbsp;)+\s*$/.test(span.innerText())
  16194. ) {
  16195. span.parentNode.removeChild(span);
  16196. }
  16197. });
  16198. }
  16199. });
  16200. //调整索引标签
  16201. me.addListener("contentchange", function() {
  16202. adjustListStyle(me.document);
  16203. });
  16204. function adjustListStyle(doc, ignore) {
  16205. utils.each(domUtils.getElementsByTagName(doc, "ol ul"), function(node) {
  16206. if (!domUtils.inDoc(node, doc)) return;
  16207. var parent = node.parentNode;
  16208. if (parent.tagName === node.tagName) {
  16209. var nodeStyleType =
  16210. getStyle(node) || (node.tagName === "OL" ? "decimal" : "disc"),
  16211. parentStyleType =
  16212. getStyle(parent) || (parent.tagName === "OL" ? "decimal" : "disc");
  16213. if (nodeStyleType === parentStyleType) {
  16214. var styleIndex = utils.indexOf(
  16215. listStyle[node.tagName],
  16216. nodeStyleType
  16217. );
  16218. styleIndex = styleIndex + 1 === listStyle[node.tagName].length
  16219. ? 0
  16220. : styleIndex + 1;
  16221. setListStyle(node, listStyle[node.tagName][styleIndex]);
  16222. }
  16223. }
  16224. var index = 0,
  16225. type = 2;
  16226. if (domUtils.hasClass(node, /custom_/)) {
  16227. if (
  16228. !(
  16229. /[ou]l/i.test(parent.tagName) &&
  16230. domUtils.hasClass(parent, /custom_/)
  16231. )
  16232. ) {
  16233. type = 1;
  16234. }
  16235. } else {
  16236. if (
  16237. /[ou]l/i.test(parent.tagName) &&
  16238. domUtils.hasClass(parent, /custom_/)
  16239. ) {
  16240. type = 3;
  16241. }
  16242. }
  16243. var style = domUtils.getStyle(node, "list-style-type");
  16244. style && (node.style.cssText = "list-style-type:" + style);
  16245. node.className =
  16246. utils.trim(node.className.replace(/list-paddingleft-\w+/, "")) +
  16247. " list-paddingleft-" +
  16248. type;
  16249. utils.each(domUtils.getElementsByTagName(node, "li"), function(li) {
  16250. li.style.cssText && (li.style.cssText = "");
  16251. if (!li.firstChild) {
  16252. domUtils.remove(li);
  16253. return;
  16254. }
  16255. if (li.parentNode !== node) {
  16256. return;
  16257. }
  16258. index++;
  16259. if (domUtils.hasClass(node, /custom_/)) {
  16260. var paddingLeft = 1,
  16261. currentStyle = getStyle(node);
  16262. if (node.tagName === "OL") {
  16263. if (currentStyle) {
  16264. switch (currentStyle) {
  16265. case "cn":
  16266. case "cn1":
  16267. case "cn2":
  16268. if (
  16269. index > 10 &&
  16270. (index % 10 === 0 || (index > 10 && index < 20))
  16271. ) {
  16272. paddingLeft = 2;
  16273. } else if (index > 20) {
  16274. paddingLeft = 3;
  16275. }
  16276. break;
  16277. case "num2":
  16278. if (index > 9) {
  16279. paddingLeft = 2;
  16280. }
  16281. }
  16282. }
  16283. li.className =
  16284. "list-" +
  16285. customStyle[currentStyle] +
  16286. index +
  16287. " " +
  16288. "list-" +
  16289. currentStyle +
  16290. "-paddingleft-" +
  16291. paddingLeft;
  16292. } else {
  16293. li.className =
  16294. "list-" +
  16295. customStyle[currentStyle] +
  16296. " " +
  16297. "list-" +
  16298. currentStyle +
  16299. "-paddingleft";
  16300. }
  16301. } else {
  16302. li.className = li.className.replace(/list-[\w\-]+/gi, "");
  16303. }
  16304. var className = li.getAttribute("class");
  16305. if (className !== null && !className.replace(/\s/g, "")) {
  16306. domUtils.removeAttributes(li, "class");
  16307. }
  16308. });
  16309. !ignore &&
  16310. adjustList(
  16311. node,
  16312. node.tagName.toLowerCase(),
  16313. getStyle(node) || domUtils.getStyle(node, "list-style-type"),
  16314. true
  16315. );
  16316. });
  16317. }
  16318. function adjustList(list, tag, style, ignoreEmpty) {
  16319. var nextList = list.nextSibling;
  16320. if (
  16321. nextList &&
  16322. nextList.nodeType === 1 &&
  16323. nextList.tagName.toLowerCase() === tag &&
  16324. (getStyle(nextList) ||
  16325. domUtils.getStyle(nextList, "list-style-type") ||
  16326. (tag == "ol" ? "decimal" : "disc")) == style
  16327. ) {
  16328. domUtils.moveChild(nextList, list);
  16329. if (nextList.childNodes.length === 0) {
  16330. domUtils.remove(nextList);
  16331. }
  16332. }
  16333. if (nextList && domUtils.isFillChar(nextList)) {
  16334. domUtils.remove(nextList);
  16335. }
  16336. var preList = list.previousSibling;
  16337. if (
  16338. preList &&
  16339. preList.nodeType === 1 &&
  16340. preList.tagName.toLowerCase() == tag &&
  16341. (getStyle(preList) ||
  16342. domUtils.getStyle(preList, "list-style-type") ||
  16343. (tag == "ol" ? "decimal" : "disc")) === style
  16344. ) {
  16345. domUtils.moveChild(list, preList);
  16346. }
  16347. if (preList && domUtils.isFillChar(preList)) {
  16348. domUtils.remove(preList);
  16349. }
  16350. !ignoreEmpty && domUtils.isEmptyBlock(list) && domUtils.remove(list);
  16351. if (getStyle(list)) {
  16352. adjustListStyle(list.ownerDocument, true);
  16353. }
  16354. }
  16355. function setListStyle(list, style) {
  16356. if (customStyle[style]) {
  16357. list.className = "custom_" + style;
  16358. }
  16359. try {
  16360. domUtils.setStyle(list, "list-style-type", style);
  16361. } catch (e) {}
  16362. }
  16363. function clearEmptySibling(node) {
  16364. var tmpNode = node.previousSibling;
  16365. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16366. domUtils.remove(tmpNode);
  16367. }
  16368. tmpNode = node.nextSibling;
  16369. if (tmpNode && domUtils.isEmptyBlock(tmpNode)) {
  16370. domUtils.remove(tmpNode);
  16371. }
  16372. }
  16373. me.addListener("keydown", function(type, evt) {
  16374. function preventAndSave() {
  16375. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  16376. me.fireEvent("contentchange");
  16377. me.undoManger && me.undoManger.save();
  16378. }
  16379. function findList(node, filterFn) {
  16380. while (node && !domUtils.isBody(node)) {
  16381. if (filterFn(node)) {
  16382. return null;
  16383. }
  16384. if (node.nodeType === 1 && /[ou]l/i.test(node.tagName)) {
  16385. return node;
  16386. }
  16387. node = node.parentNode;
  16388. }
  16389. return null;
  16390. }
  16391. var keyCode = evt.keyCode || evt.which;
  16392. if (keyCode === 13 && !evt.shiftKey) {
  16393. //回车
  16394. var rng = me.selection.getRange(),
  16395. parent = domUtils.findParent(
  16396. rng.startContainer,
  16397. function(node) {
  16398. return domUtils.isBlockElm(node);
  16399. },
  16400. true
  16401. ),
  16402. li = domUtils.findParentByTagName(rng.startContainer, "li", true);
  16403. if (parent && parent.tagName !== "PRE" && !li) {
  16404. var html = parent.innerHTML.replace(
  16405. new RegExp(domUtils.fillChar, "g"),
  16406. ""
  16407. );
  16408. if (/^\s*1\s*\.[^\d]/.test(html)) {
  16409. parent.innerHTML = html.replace(/^\s*1\s*\./, "");
  16410. rng.setStartAtLast(parent).collapse(true).select();
  16411. me.__hasEnterExecCommand = true;
  16412. me.execCommand("insertorderedlist");
  16413. me.__hasEnterExecCommand = false;
  16414. }
  16415. }
  16416. var range = me.selection.getRange(),
  16417. start = findList(range.startContainer, function(node) {
  16418. return node.tagName === "TABLE";
  16419. }),
  16420. end = range.collapsed
  16421. ? start
  16422. : findList(range.endContainer, function(node) {
  16423. return node.tagName === "TABLE";
  16424. });
  16425. if (start && end && start === end) {
  16426. if (!range.collapsed) {
  16427. start = domUtils.findParentByTagName(
  16428. range.startContainer,
  16429. "li",
  16430. true
  16431. );
  16432. end = domUtils.findParentByTagName(range.endContainer, "li", true);
  16433. if (start && end && start === end) {
  16434. range.deleteContents();
  16435. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16436. if (li && domUtils.isEmptyBlock(li)) {
  16437. pre = li.previousSibling;
  16438. next = li.nextSibling;
  16439. p = me.document.createElement("p");
  16440. domUtils.fillNode(me.document, p);
  16441. parentList = li.parentNode;
  16442. if (pre && next) {
  16443. range.setStart(next, 0).collapse(true).select(true);
  16444. domUtils.remove(li);
  16445. } else {
  16446. if ((!pre && !next) || !pre) {
  16447. parentList.parentNode.insertBefore(p, parentList);
  16448. } else {
  16449. li.parentNode.parentNode.insertBefore(
  16450. p,
  16451. parentList.nextSibling
  16452. );
  16453. }
  16454. domUtils.remove(li);
  16455. if (!parentList.firstChild) {
  16456. domUtils.remove(parentList);
  16457. }
  16458. range.setStart(p, 0).setCursor();
  16459. }
  16460. preventAndSave();
  16461. return;
  16462. }
  16463. } else {
  16464. var tmpRange = range.cloneRange(),
  16465. bk = tmpRange.collapse(false).createBookmark();
  16466. range.deleteContents();
  16467. tmpRange.moveToBookmark(bk);
  16468. var li = domUtils.findParentByTagName(
  16469. tmpRange.startContainer,
  16470. "li",
  16471. true
  16472. );
  16473. clearEmptySibling(li);
  16474. tmpRange.select();
  16475. preventAndSave();
  16476. return;
  16477. }
  16478. }
  16479. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16480. if (li) {
  16481. if (domUtils.isEmptyBlock(li)) {
  16482. bk = range.createBookmark();
  16483. var parentList = li.parentNode;
  16484. if (li !== parentList.lastChild) {
  16485. domUtils.breakParent(li, parentList);
  16486. clearEmptySibling(li);
  16487. } else {
  16488. parentList.parentNode.insertBefore(li, parentList.nextSibling);
  16489. if (domUtils.isEmptyNode(parentList)) {
  16490. domUtils.remove(parentList);
  16491. }
  16492. }
  16493. //嵌套不处理
  16494. if (!dtd.$list[li.parentNode.tagName]) {
  16495. if (!domUtils.isBlockElm(li.firstChild)) {
  16496. p = me.document.createElement("p");
  16497. li.parentNode.insertBefore(p, li);
  16498. while (li.firstChild) {
  16499. p.appendChild(li.firstChild);
  16500. }
  16501. domUtils.remove(li);
  16502. } else {
  16503. domUtils.remove(li, true);
  16504. }
  16505. }
  16506. range.moveToBookmark(bk).select();
  16507. } else {
  16508. var first = li.firstChild;
  16509. if (!first || !domUtils.isBlockElm(first)) {
  16510. var p = me.document.createElement("p");
  16511. !li.firstChild && domUtils.fillNode(me.document, p);
  16512. while (li.firstChild) {
  16513. p.appendChild(li.firstChild);
  16514. }
  16515. li.appendChild(p);
  16516. first = p;
  16517. }
  16518. var span = me.document.createElement("span");
  16519. range.insertNode(span);
  16520. domUtils.breakParent(span, li);
  16521. var nextLi = span.nextSibling;
  16522. first = nextLi.firstChild;
  16523. if (!first) {
  16524. p = me.document.createElement("p");
  16525. domUtils.fillNode(me.document, p);
  16526. nextLi.appendChild(p);
  16527. first = p;
  16528. }
  16529. if (domUtils.isEmptyNode(first)) {
  16530. first.innerHTML = "";
  16531. domUtils.fillNode(me.document, first);
  16532. }
  16533. range.setStart(first, 0).collapse(true).shrinkBoundary().select();
  16534. domUtils.remove(span);
  16535. var pre = nextLi.previousSibling;
  16536. if (pre && domUtils.isEmptyBlock(pre)) {
  16537. pre.innerHTML = "<p></p>";
  16538. domUtils.fillNode(me.document, pre.firstChild);
  16539. }
  16540. }
  16541. // }
  16542. preventAndSave();
  16543. }
  16544. }
  16545. }
  16546. if (keyCode === 8) {
  16547. //修中ie中li下的问题
  16548. range = me.selection.getRange();
  16549. if (range.collapsed && domUtils.isStartInblock(range)) {
  16550. tmpRange = range.cloneRange().trimBoundary();
  16551. li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16552. //要在li的最左边,才能处理
  16553. if (li && domUtils.isStartInblock(tmpRange)) {
  16554. start = domUtils.findParentByTagName(range.startContainer, "p", true);
  16555. if (start && start !== li.firstChild) {
  16556. var parentList = domUtils.findParentByTagName(start, ["ol", "ul"]);
  16557. domUtils.breakParent(start, parentList);
  16558. clearEmptySibling(start);
  16559. me.fireEvent("contentchange");
  16560. range.setStart(start, 0).setCursor(false, true);
  16561. me.fireEvent("saveScene");
  16562. domUtils.preventDefault(evt);
  16563. return;
  16564. }
  16565. if (li && (pre = li.previousSibling)) {
  16566. if (keyCode === 46 && li.childNodes.length) {
  16567. return;
  16568. }
  16569. //有可能上边的兄弟节点是个2级菜单,要追加到2级菜单的最后的li
  16570. if (dtd.$list[pre.tagName]) {
  16571. pre = pre.lastChild;
  16572. }
  16573. me.undoManger && me.undoManger.save();
  16574. first = li.firstChild;
  16575. if (domUtils.isBlockElm(first)) {
  16576. if (domUtils.isEmptyNode(first)) {
  16577. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16578. pre.appendChild(first);
  16579. range.setStart(first, 0).setCursor(false, true);
  16580. //first不是唯一的节点
  16581. while (li.firstChild) {
  16582. pre.appendChild(li.firstChild);
  16583. }
  16584. } else {
  16585. span = me.document.createElement("span");
  16586. range.insertNode(span);
  16587. //判断pre是否是空的节点,如果是<p><br/></p>类型的空节点,干掉p标签防止它占位
  16588. if (domUtils.isEmptyBlock(pre)) {
  16589. pre.innerHTML = "";
  16590. }
  16591. domUtils.moveChild(li, pre);
  16592. range.setStartBefore(span).collapse(true).select(true);
  16593. domUtils.remove(span);
  16594. }
  16595. } else {
  16596. if (domUtils.isEmptyNode(li)) {
  16597. var p = me.document.createElement("p");
  16598. pre.appendChild(p);
  16599. range.setStart(p, 0).setCursor();
  16600. // range.setEnd(pre, pre.childNodes.length).shrinkBoundary().collapse().select(true);
  16601. } else {
  16602. range
  16603. .setEnd(pre, pre.childNodes.length)
  16604. .collapse()
  16605. .select(true);
  16606. while (li.firstChild) {
  16607. pre.appendChild(li.firstChild);
  16608. }
  16609. }
  16610. }
  16611. domUtils.remove(li);
  16612. me.fireEvent("contentchange");
  16613. me.fireEvent("saveScene");
  16614. domUtils.preventDefault(evt);
  16615. return;
  16616. }
  16617. //trace:980
  16618. if (li && !li.previousSibling) {
  16619. var parentList = li.parentNode;
  16620. var bk = range.createBookmark();
  16621. if (domUtils.isTagNode(parentList.parentNode, "ol ul")) {
  16622. parentList.parentNode.insertBefore(li, parentList);
  16623. if (domUtils.isEmptyNode(parentList)) {
  16624. domUtils.remove(parentList);
  16625. }
  16626. } else {
  16627. while (li.firstChild) {
  16628. parentList.parentNode.insertBefore(li.firstChild, parentList);
  16629. }
  16630. domUtils.remove(li);
  16631. if (domUtils.isEmptyNode(parentList)) {
  16632. domUtils.remove(parentList);
  16633. }
  16634. }
  16635. range.moveToBookmark(bk).setCursor(false, true);
  16636. me.fireEvent("contentchange");
  16637. me.fireEvent("saveScene");
  16638. domUtils.preventDefault(evt);
  16639. return;
  16640. }
  16641. }
  16642. }
  16643. }
  16644. });
  16645. me.addListener("keyup", function(type, evt) {
  16646. var keyCode = evt.keyCode || evt.which;
  16647. if (keyCode == 8) {
  16648. var rng = me.selection.getRange(),
  16649. list;
  16650. if (
  16651. (list = domUtils.findParentByTagName(
  16652. rng.startContainer,
  16653. ["ol", "ul"],
  16654. true
  16655. ))
  16656. ) {
  16657. adjustList(
  16658. list,
  16659. list.tagName.toLowerCase(),
  16660. getStyle(list) || domUtils.getComputedStyle(list, "list-style-type"),
  16661. true
  16662. );
  16663. }
  16664. }
  16665. });
  16666. //处理tab键
  16667. me.addListener("tabkeydown", function() {
  16668. var range = me.selection.getRange();
  16669. //控制级数
  16670. function checkLevel(li) {
  16671. if (me.options.maxListLevel != -1) {
  16672. var level = li.parentNode,
  16673. levelNum = 0;
  16674. while (/[ou]l/i.test(level.tagName)) {
  16675. levelNum++;
  16676. level = level.parentNode;
  16677. }
  16678. if (levelNum >= me.options.maxListLevel) {
  16679. return true;
  16680. }
  16681. }
  16682. }
  16683. //只以开始为准
  16684. //todo 后续改进
  16685. var li = domUtils.findParentByTagName(range.startContainer, "li", true);
  16686. if (li) {
  16687. var bk;
  16688. if (range.collapsed) {
  16689. if (checkLevel(li)) return true;
  16690. var parentLi = li.parentNode,
  16691. list = me.document.createElement(parentLi.tagName),
  16692. index = utils.indexOf(
  16693. listStyle[list.tagName],
  16694. getStyle(parentLi) ||
  16695. domUtils.getComputedStyle(parentLi, "list-style-type")
  16696. );
  16697. index = index + 1 == listStyle[list.tagName].length ? 0 : index + 1;
  16698. var currentStyle = listStyle[list.tagName][index];
  16699. setListStyle(list, currentStyle);
  16700. if (domUtils.isStartInblock(range)) {
  16701. me.fireEvent("saveScene");
  16702. bk = range.createBookmark();
  16703. parentLi.insertBefore(list, li);
  16704. list.appendChild(li);
  16705. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  16706. me.fireEvent("contentchange");
  16707. range.moveToBookmark(bk).select(true);
  16708. return true;
  16709. }
  16710. } else {
  16711. me.fireEvent("saveScene");
  16712. bk = range.createBookmark();
  16713. for (
  16714. var i = 0, closeList, parents = domUtils.findParents(li), ci;
  16715. (ci = parents[i++]);
  16716. ) {
  16717. if (domUtils.isTagNode(ci, "ol ul")) {
  16718. closeList = ci;
  16719. break;
  16720. }
  16721. }
  16722. var current = li;
  16723. if (bk.end) {
  16724. while (
  16725. current &&
  16726. !(
  16727. domUtils.getPosition(current, bk.end) &
  16728. domUtils.POSITION_FOLLOWING
  16729. )
  16730. ) {
  16731. if (checkLevel(current)) {
  16732. current = domUtils.getNextDomNode(current, false, null, function(
  16733. node
  16734. ) {
  16735. return node !== closeList;
  16736. });
  16737. continue;
  16738. }
  16739. var parentLi = current.parentNode,
  16740. list = me.document.createElement(parentLi.tagName),
  16741. index = utils.indexOf(
  16742. listStyle[list.tagName],
  16743. getStyle(parentLi) ||
  16744. domUtils.getComputedStyle(parentLi, "list-style-type")
  16745. );
  16746. var currentIndex = index + 1 == listStyle[list.tagName].length
  16747. ? 0
  16748. : index + 1;
  16749. var currentStyle = listStyle[list.tagName][currentIndex];
  16750. setListStyle(list, currentStyle);
  16751. parentLi.insertBefore(list, current);
  16752. while (
  16753. current &&
  16754. !(
  16755. domUtils.getPosition(current, bk.end) &
  16756. domUtils.POSITION_FOLLOWING
  16757. )
  16758. ) {
  16759. li = current.nextSibling;
  16760. list.appendChild(current);
  16761. if (!li || domUtils.isTagNode(li, "ol ul")) {
  16762. if (li) {
  16763. while ((li = li.firstChild)) {
  16764. if (li.tagName == "LI") {
  16765. break;
  16766. }
  16767. }
  16768. } else {
  16769. li = domUtils.getNextDomNode(current, false, null, function(
  16770. node
  16771. ) {
  16772. return node !== closeList;
  16773. });
  16774. }
  16775. break;
  16776. }
  16777. current = li;
  16778. }
  16779. adjustList(list, list.tagName.toLowerCase(), currentStyle);
  16780. current = li;
  16781. }
  16782. }
  16783. me.fireEvent("contentchange");
  16784. range.moveToBookmark(bk).select();
  16785. return true;
  16786. }
  16787. }
  16788. });
  16789. function getLi(start) {
  16790. while (start && !domUtils.isBody(start)) {
  16791. if (start.nodeName == "TABLE") {
  16792. return null;
  16793. }
  16794. if (start.nodeName == "LI") {
  16795. return start;
  16796. }
  16797. start = start.parentNode;
  16798. }
  16799. }
  16800. /**
  16801. * 有序列表,与“insertunorderedlist”命令互斥
  16802. * @command insertorderedlist
  16803. * @method execCommand
  16804. * @param { String } command 命令字符串
  16805. * @param { String } style 插入的有序列表类型,值为:decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16806. * @example
  16807. * ```javascript
  16808. * editor.execCommand( 'insertorderedlist','decimal');
  16809. * ```
  16810. */
  16811. /**
  16812. * 查询当前选区内容是否有序列表
  16813. * @command insertorderedlist
  16814. * @method queryCommandState
  16815. * @param { String } cmd 命令字符串
  16816. * @return { int } 如果当前选区是有序列表返回1,否则返回0
  16817. * @example
  16818. * ```javascript
  16819. * editor.queryCommandState( 'insertorderedlist' );
  16820. * ```
  16821. */
  16822. /**
  16823. * 查询当前选区内容是否有序列表
  16824. * @command insertorderedlist
  16825. * @method queryCommandValue
  16826. * @param { String } cmd 命令字符串
  16827. * @return { String } 返回当前有序列表的类型,值为null或decimal,lower-alpha,lower-roman,upper-alpha,upper-roman,cn,cn1,cn2,num,num1,num2
  16828. * @example
  16829. * ```javascript
  16830. * editor.queryCommandValue( 'insertorderedlist' );
  16831. * ```
  16832. */
  16833. /**
  16834. * 无序列表,与“insertorderedlist”命令互斥
  16835. * @command insertunorderedlist
  16836. * @method execCommand
  16837. * @param { String } command 命令字符串
  16838. * @param { String } style 插入的无序列表类型,值为:circle,disc,square,dash,dot
  16839. * @example
  16840. * ```javascript
  16841. * editor.execCommand( 'insertunorderedlist','circle');
  16842. * ```
  16843. */
  16844. /**
  16845. * 查询当前是否有word文档粘贴进来的图片
  16846. * @command insertunorderedlist
  16847. * @method insertunorderedlist
  16848. * @param { String } command 命令字符串
  16849. * @return { int } 如果当前选区是无序列表返回1,否则返回0
  16850. * @example
  16851. * ```javascript
  16852. * editor.queryCommandState( 'insertunorderedlist' );
  16853. * ```
  16854. */
  16855. /**
  16856. * 查询当前选区内容是否有序列表
  16857. * @command insertunorderedlist
  16858. * @method queryCommandValue
  16859. * @param { String } command 命令字符串
  16860. * @return { String } 返回当前无序列表的类型,值为null或circle,disc,square,dash,dot
  16861. * @example
  16862. * ```javascript
  16863. * editor.queryCommandValue( 'insertunorderedlist' );
  16864. * ```
  16865. */
  16866. me.commands["insertorderedlist"] = me.commands["insertunorderedlist"] = {
  16867. execCommand: function(command, style) {
  16868. if (!style) {
  16869. style = command.toLowerCase() == "insertorderedlist"
  16870. ? "decimal"
  16871. : "disc";
  16872. }
  16873. var me = this,
  16874. range = this.selection.getRange(),
  16875. filterFn = function(node) {
  16876. return node.nodeType == 1
  16877. ? node.tagName.toLowerCase() != "br"
  16878. : !domUtils.isWhitespace(node);
  16879. },
  16880. tag = command.toLowerCase() == "insertorderedlist" ? "ol" : "ul",
  16881. frag = me.document.createDocumentFragment();
  16882. //去掉是因为会出现选到末尾,导致adjustmentBoundary缩到ol/ul的位置
  16883. //range.shrinkBoundary();//.adjustmentBoundary();
  16884. range.adjustmentBoundary().shrinkBoundary();
  16885. var bko = range.createBookmark(true),
  16886. start = getLi(me.document.getElementById(bko.start)),
  16887. modifyStart = 0,
  16888. end = getLi(me.document.getElementById(bko.end)),
  16889. modifyEnd = 0,
  16890. startParent,
  16891. endParent,
  16892. list,
  16893. tmp;
  16894. if (start || end) {
  16895. start && (startParent = start.parentNode);
  16896. if (!bko.end) {
  16897. end = start;
  16898. }
  16899. end && (endParent = end.parentNode);
  16900. if (startParent === endParent) {
  16901. while (start !== end) {
  16902. tmp = start;
  16903. start = start.nextSibling;
  16904. if (!domUtils.isBlockElm(tmp.firstChild)) {
  16905. var p = me.document.createElement("p");
  16906. while (tmp.firstChild) {
  16907. p.appendChild(tmp.firstChild);
  16908. }
  16909. tmp.appendChild(p);
  16910. }
  16911. frag.appendChild(tmp);
  16912. }
  16913. tmp = me.document.createElement("span");
  16914. startParent.insertBefore(tmp, end);
  16915. if (!domUtils.isBlockElm(end.firstChild)) {
  16916. p = me.document.createElement("p");
  16917. while (end.firstChild) {
  16918. p.appendChild(end.firstChild);
  16919. }
  16920. end.appendChild(p);
  16921. }
  16922. frag.appendChild(end);
  16923. domUtils.breakParent(tmp, startParent);
  16924. if (domUtils.isEmptyNode(tmp.previousSibling)) {
  16925. domUtils.remove(tmp.previousSibling);
  16926. }
  16927. if (domUtils.isEmptyNode(tmp.nextSibling)) {
  16928. domUtils.remove(tmp.nextSibling);
  16929. }
  16930. var nodeStyle =
  16931. getStyle(startParent) ||
  16932. domUtils.getComputedStyle(startParent, "list-style-type") ||
  16933. (command.toLowerCase() == "insertorderedlist" ? "decimal" : "disc");
  16934. if (startParent.tagName.toLowerCase() == tag && nodeStyle == style) {
  16935. for (
  16936. var i = 0, ci, tmpFrag = me.document.createDocumentFragment();
  16937. (ci = frag.firstChild);
  16938. ) {
  16939. if (domUtils.isTagNode(ci, "ol ul")) {
  16940. // 删除时,子列表不处理
  16941. // utils.each(domUtils.getElementsByTagName(ci,'li'),function(li){
  16942. // while(li.firstChild){
  16943. // tmpFrag.appendChild(li.firstChild);
  16944. // }
  16945. //
  16946. // });
  16947. tmpFrag.appendChild(ci);
  16948. } else {
  16949. while (ci.firstChild) {
  16950. tmpFrag.appendChild(ci.firstChild);
  16951. domUtils.remove(ci);
  16952. }
  16953. }
  16954. }
  16955. tmp.parentNode.insertBefore(tmpFrag, tmp);
  16956. } else {
  16957. list = me.document.createElement(tag);
  16958. setListStyle(list, style);
  16959. list.appendChild(frag);
  16960. tmp.parentNode.insertBefore(list, tmp);
  16961. }
  16962. domUtils.remove(tmp);
  16963. list && adjustList(list, tag, style);
  16964. range.moveToBookmark(bko).select();
  16965. return;
  16966. }
  16967. //开始
  16968. if (start) {
  16969. while (start) {
  16970. tmp = start.nextSibling;
  16971. if (domUtils.isTagNode(start, "ol ul")) {
  16972. frag.appendChild(start);
  16973. } else {
  16974. var tmpfrag = me.document.createDocumentFragment(),
  16975. hasBlock = 0;
  16976. while (start.firstChild) {
  16977. if (domUtils.isBlockElm(start.firstChild)) {
  16978. hasBlock = 1;
  16979. }
  16980. tmpfrag.appendChild(start.firstChild);
  16981. }
  16982. if (!hasBlock) {
  16983. var tmpP = me.document.createElement("p");
  16984. tmpP.appendChild(tmpfrag);
  16985. frag.appendChild(tmpP);
  16986. } else {
  16987. frag.appendChild(tmpfrag);
  16988. }
  16989. domUtils.remove(start);
  16990. }
  16991. start = tmp;
  16992. }
  16993. startParent.parentNode.insertBefore(frag, startParent.nextSibling);
  16994. if (domUtils.isEmptyNode(startParent)) {
  16995. range.setStartBefore(startParent);
  16996. domUtils.remove(startParent);
  16997. } else {
  16998. range.setStartAfter(startParent);
  16999. }
  17000. modifyStart = 1;
  17001. }
  17002. if (end && domUtils.inDoc(endParent, me.document)) {
  17003. //结束
  17004. start = endParent.firstChild;
  17005. while (start && start !== end) {
  17006. tmp = start.nextSibling;
  17007. if (domUtils.isTagNode(start, "ol ul")) {
  17008. frag.appendChild(start);
  17009. } else {
  17010. tmpfrag = me.document.createDocumentFragment();
  17011. hasBlock = 0;
  17012. while (start.firstChild) {
  17013. if (domUtils.isBlockElm(start.firstChild)) {
  17014. hasBlock = 1;
  17015. }
  17016. tmpfrag.appendChild(start.firstChild);
  17017. }
  17018. if (!hasBlock) {
  17019. tmpP = me.document.createElement("p");
  17020. tmpP.appendChild(tmpfrag);
  17021. frag.appendChild(tmpP);
  17022. } else {
  17023. frag.appendChild(tmpfrag);
  17024. }
  17025. domUtils.remove(start);
  17026. }
  17027. start = tmp;
  17028. }
  17029. var tmpDiv = domUtils.createElement(me.document, "div", {
  17030. tmpDiv: 1
  17031. });
  17032. domUtils.moveChild(end, tmpDiv);
  17033. frag.appendChild(tmpDiv);
  17034. domUtils.remove(end);
  17035. endParent.parentNode.insertBefore(frag, endParent);
  17036. range.setEndBefore(endParent);
  17037. if (domUtils.isEmptyNode(endParent)) {
  17038. domUtils.remove(endParent);
  17039. }
  17040. modifyEnd = 1;
  17041. }
  17042. }
  17043. if (!modifyStart) {
  17044. range.setStartBefore(me.document.getElementById(bko.start));
  17045. }
  17046. if (bko.end && !modifyEnd) {
  17047. range.setEndAfter(me.document.getElementById(bko.end));
  17048. }
  17049. range.enlarge(true, function(node) {
  17050. return notExchange[node.tagName];
  17051. });
  17052. frag = me.document.createDocumentFragment();
  17053. var bk = range.createBookmark(),
  17054. current = domUtils.getNextDomNode(bk.start, false, filterFn),
  17055. tmpRange = range.cloneRange(),
  17056. tmpNode,
  17057. block = domUtils.isBlockElm;
  17058. while (
  17059. current &&
  17060. current !== bk.end &&
  17061. domUtils.getPosition(current, bk.end) & domUtils.POSITION_PRECEDING
  17062. ) {
  17063. if (current.nodeType == 3 || dtd.li[current.tagName]) {
  17064. if (current.nodeType == 1 && dtd.$list[current.tagName]) {
  17065. while (current.firstChild) {
  17066. frag.appendChild(current.firstChild);
  17067. }
  17068. tmpNode = domUtils.getNextDomNode(current, false, filterFn);
  17069. domUtils.remove(current);
  17070. current = tmpNode;
  17071. continue;
  17072. }
  17073. tmpNode = current;
  17074. tmpRange.setStartBefore(current);
  17075. while (
  17076. current &&
  17077. current !== bk.end &&
  17078. (!block(current) || domUtils.isBookmarkNode(current))
  17079. ) {
  17080. tmpNode = current;
  17081. current = domUtils.getNextDomNode(current, false, null, function(
  17082. node
  17083. ) {
  17084. return !notExchange[node.tagName];
  17085. });
  17086. }
  17087. if (current && block(current)) {
  17088. tmp = domUtils.getNextDomNode(tmpNode, false, filterFn);
  17089. if (tmp && domUtils.isBookmarkNode(tmp)) {
  17090. current = domUtils.getNextDomNode(tmp, false, filterFn);
  17091. tmpNode = tmp;
  17092. }
  17093. }
  17094. tmpRange.setEndAfter(tmpNode);
  17095. current = domUtils.getNextDomNode(tmpNode, false, filterFn);
  17096. var li = range.document.createElement("li");
  17097. li.appendChild(tmpRange.extractContents());
  17098. if (domUtils.isEmptyNode(li)) {
  17099. var tmpNode = range.document.createElement("p");
  17100. while (li.firstChild) {
  17101. tmpNode.appendChild(li.firstChild);
  17102. }
  17103. li.appendChild(tmpNode);
  17104. }
  17105. frag.appendChild(li);
  17106. } else {
  17107. current = domUtils.getNextDomNode(current, true, filterFn);
  17108. }
  17109. }
  17110. range.moveToBookmark(bk).collapse(true);
  17111. list = me.document.createElement(tag);
  17112. setListStyle(list, style);
  17113. list.appendChild(frag);
  17114. range.insertNode(list);
  17115. //当前list上下看能否合并
  17116. adjustList(list, tag, style);
  17117. //去掉冗余的tmpDiv
  17118. for (
  17119. var i = 0, ci, tmpDivs = domUtils.getElementsByTagName(list, "div");
  17120. (ci = tmpDivs[i++]);
  17121. ) {
  17122. if (ci.getAttribute("tmpDiv")) {
  17123. domUtils.remove(ci, true);
  17124. }
  17125. }
  17126. range.moveToBookmark(bko).select();
  17127. },
  17128. queryCommandState: function(command) {
  17129. var tag = command.toLowerCase() == "insertorderedlist" ? "ol" : "ul";
  17130. var path = this.selection.getStartElementPath();
  17131. for (var i = 0, ci; (ci = path[i++]); ) {
  17132. if (ci.nodeName == "TABLE") {
  17133. return 0;
  17134. }
  17135. if (tag == ci.nodeName.toLowerCase()) {
  17136. return 1;
  17137. }
  17138. }
  17139. return 0;
  17140. },
  17141. queryCommandValue: function(command) {
  17142. var tag = command.toLowerCase() == "insertorderedlist" ? "ol" : "ul";
  17143. var path = this.selection.getStartElementPath(),
  17144. node;
  17145. for (var i = 0, ci; (ci = path[i++]); ) {
  17146. if (ci.nodeName == "TABLE") {
  17147. node = null;
  17148. break;
  17149. }
  17150. if (tag == ci.nodeName.toLowerCase()) {
  17151. node = ci;
  17152. break;
  17153. }
  17154. }
  17155. return node
  17156. ? getStyle(node) || domUtils.getComputedStyle(node, "list-style-type")
  17157. : null;
  17158. }
  17159. };
  17160. };
  17161. // plugins/source.js
  17162. /**
  17163. * 源码编辑插件
  17164. * @file
  17165. * @since 1.2.6.1
  17166. */
  17167. (function() {
  17168. var sourceEditors = {
  17169. textarea: function(editor, holder) {
  17170. var textarea = holder.ownerDocument.createElement("textarea");
  17171. textarea.style.cssText =
  17172. "position:absolute;resize:none;width:100%;height:100%;border:0;padding:0;margin:0;overflow-y:auto;";
  17173. // todo: IE下只有onresize属性可用... 很纠结
  17174. if (browser.ie && browser.version < 8) {
  17175. textarea.style.width = holder.offsetWidth + "px";
  17176. textarea.style.height = holder.offsetHeight + "px";
  17177. holder.onresize = function() {
  17178. textarea.style.width = holder.offsetWidth + "px";
  17179. textarea.style.height = holder.offsetHeight + "px";
  17180. };
  17181. }
  17182. holder.appendChild(textarea);
  17183. return {
  17184. setContent: function(content) {
  17185. textarea.value = content;
  17186. },
  17187. getContent: function() {
  17188. return textarea.value;
  17189. },
  17190. select: function() {
  17191. var range;
  17192. if (browser.ie) {
  17193. range = textarea.createTextRange();
  17194. range.collapse(true);
  17195. range.select();
  17196. } else {
  17197. //todo: chrome下无法设置焦点
  17198. textarea.setSelectionRange(0, 0);
  17199. textarea.focus();
  17200. }
  17201. },
  17202. dispose: function() {
  17203. holder.removeChild(textarea);
  17204. // todo
  17205. holder.onresize = null;
  17206. textarea = null;
  17207. holder = null;
  17208. },
  17209. focus: function (){
  17210. textarea.focus();
  17211. },
  17212. blur: function (){
  17213. textarea.blur();
  17214. }
  17215. };
  17216. },
  17217. codemirror: function(editor, holder) {
  17218. var codeEditor = window.CodeMirror(holder, {
  17219. mode: "text/html",
  17220. tabMode: "indent",
  17221. lineNumbers: true,
  17222. lineWrapping: true,
  17223. onChange: function(v){
  17224. editor.sync();
  17225. // console.log('CodeMirror.onChange',v.getValue());
  17226. }
  17227. });
  17228. // console.log('sourceEditor',codeEditor);
  17229. var dom = codeEditor.getWrapperElement();
  17230. dom.style.cssText =
  17231. 'position:absolute;left:0;top:0;width:100%;height:100%;font-family:consolas,"Courier new",monospace;font-size:13px;';
  17232. codeEditor.getScrollerElement().style.cssText =
  17233. "position:absolute;left:0;top:0;width:100%;height:100%;";
  17234. codeEditor.refresh();
  17235. return {
  17236. getCodeMirror: function() {
  17237. return codeEditor;
  17238. },
  17239. setContent: function(content) {
  17240. codeEditor.setValue(content);
  17241. },
  17242. getContent: function() {
  17243. return codeEditor.getValue();
  17244. },
  17245. select: function() {
  17246. codeEditor.focus();
  17247. },
  17248. dispose: function() {
  17249. holder.removeChild(dom);
  17250. dom = null;
  17251. codeEditor = null;
  17252. },
  17253. focus: function (){
  17254. codeEditor.focus();
  17255. },
  17256. blur: function (){
  17257. // codeEditor.blur();
  17258. // since codemirror not support blur()
  17259. codeEditor.setOption('readOnly', true);
  17260. codeEditor.setOption('readOnly', false);
  17261. }
  17262. };
  17263. }
  17264. };
  17265. UE.plugins["source"] = function() {
  17266. var me = this;
  17267. var opt = this.options;
  17268. var sourceMode = false;
  17269. var sourceEditor;
  17270. var orgSetContent;
  17271. var orgFocus;
  17272. var orgBlur;
  17273. opt.sourceEditor = browser.ie
  17274. ? "textarea"
  17275. : opt.sourceEditor || "codemirror";
  17276. me.setOpt({
  17277. sourceEditorFirst: false
  17278. });
  17279. function createSourceEditor(holder) {
  17280. return sourceEditors[
  17281. opt.sourceEditor == "codemirror" && window.CodeMirror
  17282. ? "codemirror"
  17283. : "textarea"
  17284. ](me, holder);
  17285. }
  17286. var bakCssText;
  17287. //解决在源码模式下getContent不能得到最新的内容问题
  17288. var oldGetContent, bakAddress;
  17289. /**
  17290. * 切换源码模式和编辑模式
  17291. * @command source
  17292. * @method execCommand
  17293. * @param { String } cmd 命令字符串
  17294. * @example
  17295. * ```javascript
  17296. * editor.execCommand( 'source');
  17297. * ```
  17298. */
  17299. /**
  17300. * 查询当前编辑区域的状态是源码模式还是可视化模式
  17301. * @command source
  17302. * @method queryCommandState
  17303. * @param { String } cmd 命令字符串
  17304. * @return { int } 如果当前是源码编辑模式,返回1,否则返回0
  17305. * @example
  17306. * ```javascript
  17307. * editor.queryCommandState( 'source' );
  17308. * ```
  17309. */
  17310. me.commands["source"] = {
  17311. execCommand: function() {
  17312. sourceMode = !sourceMode;
  17313. if (sourceMode) {
  17314. bakAddress = me.selection.getRange().createAddress(false, true);
  17315. me.undoManger && me.undoManger.save(true);
  17316. if (browser.gecko) {
  17317. me.body.contentEditable = false;
  17318. }
  17319. bakCssText = me.iframe.style.cssText;
  17320. me.iframe.style.cssText +=
  17321. "position:absolute;left:-32768px;top:-32768px;";
  17322. me.fireEvent("beforegetcontent");
  17323. var root = UE.htmlparser(me.body.innerHTML);
  17324. me.filterOutputRule(root);
  17325. root.traversal(function(node) {
  17326. if (node.type == "element") {
  17327. switch (node.tagName) {
  17328. case "td":
  17329. case "th":
  17330. case "caption":
  17331. if (node.children && node.children.length == 1) {
  17332. if (node.firstChild().tagName == "br") {
  17333. node.removeChild(node.firstChild());
  17334. }
  17335. }
  17336. break;
  17337. case "pre":
  17338. node.innerText(node.innerText().replace(/&nbsp;/g, " "));
  17339. }
  17340. }
  17341. });
  17342. me.fireEvent("aftergetcontent");
  17343. var content = root.toHtml(true);
  17344. sourceEditor = createSourceEditor(me.iframe.parentNode);
  17345. sourceEditor.setContent(content);
  17346. orgSetContent = me.setContent;
  17347. me.setContent = function(html) {
  17348. //这里暂时不触发事件,防止报错
  17349. var root = UE.htmlparser(html);
  17350. me.filterInputRule(root);
  17351. html = root.toHtml();
  17352. sourceEditor.setContent(html);
  17353. };
  17354. setTimeout(function() {
  17355. sourceEditor.select();
  17356. me.addListener("fullscreenchanged", function() {
  17357. try {
  17358. sourceEditor.getCodeMirror().refresh();
  17359. } catch (e) {}
  17360. });
  17361. });
  17362. //重置getContent,源码模式下取值也能是最新的数据
  17363. oldGetContent = me.getContent;
  17364. me.getContent = function() {
  17365. return (
  17366. sourceEditor.getContent() ||
  17367. "<p>" + (browser.ie ? "" : "<br/>") + "</p>"
  17368. );
  17369. };
  17370. orgFocus = me.focus;
  17371. orgBlur = me.blur;
  17372. me.focus = function(){
  17373. sourceEditor.focus();
  17374. };
  17375. me.blur = function(){
  17376. orgBlur.call(me);
  17377. sourceEditor.blur();
  17378. };
  17379. } else {
  17380. me.iframe.style.cssText = bakCssText;
  17381. var cont =
  17382. sourceEditor.getContent() ||
  17383. "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  17384. //处理掉block节点前后的空格,有可能会误命中,暂时不考虑
  17385. cont = cont.replace(
  17386. new RegExp("[\\r\\t\\n ]*</?(\\w+)\\s*(?:[^>]*)>", "g"),
  17387. function(a, b) {
  17388. if (b && !dtd.$inlineWithA[b.toLowerCase()]) {
  17389. return a.replace(/(^[\n\r\t ]*)|([\n\r\t ]*$)/g, "");
  17390. }
  17391. return a.replace(/(^[\n\r\t]*)|([\n\r\t]*$)/g, "");
  17392. }
  17393. );
  17394. me.setContent = orgSetContent;
  17395. me.setContent(cont);
  17396. sourceEditor.dispose();
  17397. sourceEditor = null;
  17398. //还原getContent方法
  17399. me.getContent = oldGetContent;
  17400. me.focus = orgFocus;
  17401. me.blur = orgBlur;
  17402. var first = me.body.firstChild;
  17403. //trace:1106 都删除空了,下边会报错,所以补充一个p占位
  17404. if (!first) {
  17405. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  17406. first = me.body.firstChild;
  17407. }
  17408. //要在ifm为显示时ff才能取到selection,否则报错
  17409. //这里不能比较位置了
  17410. me.undoManger && me.undoManger.save(true);
  17411. if (browser.gecko) {
  17412. var input = document.createElement("input");
  17413. input.style.cssText = "position:absolute;left:0;top:-32768px";
  17414. document.body.appendChild(input);
  17415. me.body.contentEditable = false;
  17416. setTimeout(function() {
  17417. domUtils.setViewportOffset(input, { left: -32768, top: 0 });
  17418. input.focus();
  17419. setTimeout(function() {
  17420. me.body.contentEditable = true;
  17421. me.selection.getRange().moveToAddress(bakAddress).select(true);
  17422. domUtils.remove(input);
  17423. });
  17424. });
  17425. } else {
  17426. //ie下有可能报错,比如在代码顶头的情况
  17427. try {
  17428. me.selection.getRange().moveToAddress(bakAddress).select(true);
  17429. } catch (e) {}
  17430. }
  17431. }
  17432. this.fireEvent("sourcemodechanged", sourceMode);
  17433. },
  17434. queryCommandState: function() {
  17435. return sourceMode | 0;
  17436. },
  17437. notNeedUndo: 1
  17438. };
  17439. var oldQueryCommandState = me.queryCommandState;
  17440. me.queryCommandState = function(cmdName) {
  17441. cmdName = cmdName.toLowerCase();
  17442. if (sourceMode) {
  17443. //源码模式下可以开启的命令
  17444. return cmdName in
  17445. {
  17446. source: 1,
  17447. fullscreen: 1
  17448. }
  17449. ? 1
  17450. : -1;
  17451. }
  17452. return oldQueryCommandState.apply(this, arguments);
  17453. };
  17454. if (opt.sourceEditor == "codemirror") {
  17455. me.addListener("ready", function() {
  17456. utils.loadFile(
  17457. document,
  17458. {
  17459. src:
  17460. opt.codeMirrorJsUrl ||
  17461. opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.js",
  17462. tag: "script",
  17463. type: "text/javascript",
  17464. defer: "defer"
  17465. },
  17466. function() {
  17467. if (opt.sourceEditorFirst) {
  17468. setTimeout(function() {
  17469. me.execCommand("source");
  17470. }, 0);
  17471. }
  17472. }
  17473. );
  17474. utils.loadFile(document, {
  17475. tag: "link",
  17476. rel: "stylesheet",
  17477. type: "text/css",
  17478. href:
  17479. opt.codeMirrorCssUrl ||
  17480. opt.UEDITOR_HOME_URL + "third-party/codemirror/codemirror.css?221123"
  17481. });
  17482. });
  17483. }
  17484. };
  17485. })();
  17486. // plugins/enterkey.js
  17487. ///import core
  17488. ///import plugins/undo.js
  17489. ///commands 设置回车标签p或br
  17490. ///commandsName EnterKey
  17491. ///commandsTitle 设置回车标签p或br
  17492. /**
  17493. * @description 处理回车
  17494. * @author zhanyi
  17495. */
  17496. UE.plugins["enterkey"] = function() {
  17497. var hTag,
  17498. me = this,
  17499. tag = me.options.enterTag;
  17500. me.addListener("keyup", function(type, evt) {
  17501. var keyCode = evt.keyCode || evt.which;
  17502. if (keyCode == 13) {
  17503. var range = me.selection.getRange(),
  17504. start = range.startContainer,
  17505. doSave;
  17506. //修正在h1-h6里边回车后不能嵌套p的问题
  17507. if (!browser.ie) {
  17508. if (/h\d/i.test(hTag)) {
  17509. if (browser.gecko) {
  17510. var h = domUtils.findParentByTagName(
  17511. start,
  17512. [
  17513. "h1",
  17514. "h2",
  17515. "h3",
  17516. "h4",
  17517. "h5",
  17518. "h6",
  17519. "blockquote",
  17520. "caption",
  17521. "table"
  17522. ],
  17523. true
  17524. );
  17525. if (!h) {
  17526. me.document.execCommand("formatBlock", false, "<p>");
  17527. doSave = 1;
  17528. }
  17529. } else {
  17530. //chrome remove div
  17531. if (start.nodeType == 1) {
  17532. var tmp = me.document.createTextNode(""),
  17533. div;
  17534. range.insertNode(tmp);
  17535. div = domUtils.findParentByTagName(tmp, "div", true);
  17536. if (div) {
  17537. var p = me.document.createElement("p");
  17538. while (div.firstChild) {
  17539. p.appendChild(div.firstChild);
  17540. }
  17541. div.parentNode.insertBefore(p, div);
  17542. domUtils.remove(div);
  17543. range.setStartBefore(tmp).setCursor();
  17544. doSave = 1;
  17545. }
  17546. domUtils.remove(tmp);
  17547. }
  17548. }
  17549. if (me.undoManger && doSave) {
  17550. me.undoManger.save();
  17551. }
  17552. }
  17553. //没有站位符,会出现多行的问题
  17554. browser.opera && range.select();
  17555. } else {
  17556. me.fireEvent("saveScene", true, true);
  17557. }
  17558. }
  17559. });
  17560. me.addListener("keydown", function(type, evt) {
  17561. var keyCode = evt.keyCode || evt.which;
  17562. if (keyCode == 13) {
  17563. //回车
  17564. if (me.fireEvent("beforeenterkeydown")) {
  17565. domUtils.preventDefault(evt);
  17566. return;
  17567. }
  17568. me.fireEvent("saveScene", true, true);
  17569. hTag = "";
  17570. var range = me.selection.getRange();
  17571. if (!range.collapsed) {
  17572. //跨td不能删
  17573. var start = range.startContainer,
  17574. end = range.endContainer,
  17575. startTd = domUtils.findParentByTagName(start, "td", true),
  17576. endTd = domUtils.findParentByTagName(end, "td", true);
  17577. if (
  17578. (startTd && endTd && startTd !== endTd) ||
  17579. (!startTd && endTd) ||
  17580. (startTd && !endTd)
  17581. ) {
  17582. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  17583. return;
  17584. }
  17585. }
  17586. if (tag == "p") {
  17587. if (!browser.ie) {
  17588. start = domUtils.findParentByTagName(
  17589. range.startContainer,
  17590. [
  17591. "ol",
  17592. "ul",
  17593. "p",
  17594. "h1",
  17595. "h2",
  17596. "h3",
  17597. "h4",
  17598. "h5",
  17599. "h6",
  17600. "blockquote",
  17601. "caption"
  17602. ],
  17603. true
  17604. );
  17605. //opera下执行formatblock会在table的场景下有问题,回车在opera原生支持很好,所以暂时在opera去掉调用这个原生的command
  17606. //trace:2431
  17607. if (!start && !browser.opera) {
  17608. me.document.execCommand("formatBlock", false, "<p>");
  17609. if (browser.gecko) {
  17610. range = me.selection.getRange();
  17611. start = domUtils.findParentByTagName(
  17612. range.startContainer,
  17613. "p",
  17614. true
  17615. );
  17616. start && domUtils.removeDirtyAttr(start);
  17617. }
  17618. } else {
  17619. hTag = start.tagName;
  17620. start.tagName.toLowerCase() == "p" &&
  17621. browser.gecko &&
  17622. domUtils.removeDirtyAttr(start);
  17623. }
  17624. }
  17625. } else {
  17626. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  17627. if (!range.collapsed) {
  17628. range.deleteContents();
  17629. start = range.startContainer;
  17630. if (
  17631. start.nodeType == 1 &&
  17632. (start = start.childNodes[range.startOffset])
  17633. ) {
  17634. while (start.nodeType == 1) {
  17635. if (dtd.$empty[start.tagName]) {
  17636. range.setStartBefore(start).setCursor();
  17637. if (me.undoManger) {
  17638. me.undoManger.save();
  17639. }
  17640. return false;
  17641. }
  17642. if (!start.firstChild) {
  17643. var br = range.document.createElement("br");
  17644. start.appendChild(br);
  17645. range.setStart(start, 0).setCursor();
  17646. if (me.undoManger) {
  17647. me.undoManger.save();
  17648. }
  17649. return false;
  17650. }
  17651. start = start.firstChild;
  17652. }
  17653. if (start === range.startContainer.childNodes[range.startOffset]) {
  17654. br = range.document.createElement("br");
  17655. range.insertNode(br).setCursor();
  17656. } else {
  17657. range.setStart(start, 0).setCursor();
  17658. }
  17659. } else {
  17660. br = range.document.createElement("br");
  17661. range.insertNode(br).setStartAfter(br).setCursor();
  17662. }
  17663. } else {
  17664. br = range.document.createElement("br");
  17665. range.insertNode(br);
  17666. var parent = br.parentNode;
  17667. if (parent.lastChild === br) {
  17668. br.parentNode.insertBefore(br.cloneNode(true), br);
  17669. range.setStartBefore(br);
  17670. } else {
  17671. range.setStartAfter(br);
  17672. }
  17673. range.setCursor();
  17674. }
  17675. }
  17676. }
  17677. });
  17678. };
  17679. // plugins/keystrokes.js
  17680. /* 处理特殊键的兼容性问题 */
  17681. UE.plugins["keystrokes"] = function() {
  17682. var me = this;
  17683. var collapsed = true;
  17684. me.addListener("keydown", function(type, evt) {
  17685. var keyCode = evt.keyCode || evt.which,
  17686. rng = me.selection.getRange();
  17687. //处理全选的情况
  17688. if (
  17689. !rng.collapsed &&
  17690. !(evt.ctrlKey || evt.shiftKey || evt.altKey || evt.metaKey) &&
  17691. ((keyCode >= 65 && keyCode <= 90) ||
  17692. (keyCode >= 48 && keyCode <= 57) ||
  17693. (keyCode >= 96 && keyCode <= 111) ||
  17694. {
  17695. 13: 1,
  17696. 8: 1,
  17697. 46: 1
  17698. }[keyCode])
  17699. ) {
  17700. var tmpNode = rng.startContainer;
  17701. if (domUtils.isFillChar(tmpNode)) {
  17702. rng.setStartBefore(tmpNode);
  17703. }
  17704. tmpNode = rng.endContainer;
  17705. if (domUtils.isFillChar(tmpNode)) {
  17706. rng.setEndAfter(tmpNode);
  17707. }
  17708. rng.txtToElmBoundary();
  17709. //结束边界可能放到了br的前边,要把br包含进来
  17710. // x[xxx]<br/>
  17711. if (rng.endContainer && rng.endContainer.nodeType == 1) {
  17712. tmpNode = rng.endContainer.childNodes[rng.endOffset];
  17713. if (tmpNode && domUtils.isBr(tmpNode)) {
  17714. rng.setEndAfter(tmpNode);
  17715. }
  17716. }
  17717. if (rng.startOffset == 0) {
  17718. tmpNode = rng.startContainer;
  17719. if (domUtils.isBoundaryNode(tmpNode, "firstChild")) {
  17720. tmpNode = rng.endContainer;
  17721. if (
  17722. rng.endOffset ==
  17723. (tmpNode.nodeType == 3
  17724. ? tmpNode.nodeValue.length
  17725. : tmpNode.childNodes.length) &&
  17726. domUtils.isBoundaryNode(tmpNode, "lastChild")
  17727. ) {
  17728. me.fireEvent("saveScene");
  17729. me.body.innerHTML = "<p>" + (browser.ie ? "" : "<br/>") + "</p>";
  17730. rng.setStart(me.body.firstChild, 0).setCursor(false, true);
  17731. me._selectionChange();
  17732. return;
  17733. }
  17734. }
  17735. }
  17736. }
  17737. //处理backspace
  17738. if (keyCode == keymap.Backspace) {
  17739. rng = me.selection.getRange();
  17740. collapsed = rng.collapsed;
  17741. if (me.fireEvent("delkeydown", evt)) {
  17742. return;
  17743. }
  17744. var start, end;
  17745. //避免按两次删除才能生效的问题
  17746. if (rng.collapsed && rng.inFillChar()) {
  17747. start = rng.startContainer;
  17748. if (domUtils.isFillChar(start)) {
  17749. rng.setStartBefore(start).shrinkBoundary(true).collapse(true);
  17750. domUtils.remove(start);
  17751. } else {
  17752. start.nodeValue = start.nodeValue.replace(
  17753. new RegExp("^" + domUtils.fillChar),
  17754. ""
  17755. );
  17756. rng.startOffset--;
  17757. rng.collapse(true).select(true);
  17758. }
  17759. }
  17760. //解决选中control元素不能删除的问题
  17761. if ((start = rng.getClosedNode())) {
  17762. me.fireEvent("saveScene");
  17763. rng.setStartBefore(start);
  17764. domUtils.remove(start);
  17765. rng.setCursor();
  17766. me.fireEvent("saveScene");
  17767. domUtils.preventDefault(evt);
  17768. return;
  17769. }
  17770. //阻止在table上的删除
  17771. if (!browser.ie) {
  17772. start = domUtils.findParentByTagName(rng.startContainer, "table", true);
  17773. end = domUtils.findParentByTagName(rng.endContainer, "table", true);
  17774. if ((start && !end) || (!start && end) || start !== end) {
  17775. evt.preventDefault();
  17776. return;
  17777. }
  17778. }
  17779. }
  17780. //处理tab键的逻辑
  17781. if (keyCode == keymap.Tab) {
  17782. //不处理以下标签
  17783. var excludeTagNameForTabKey = {
  17784. ol: 1,
  17785. ul: 1,
  17786. table: 1
  17787. };
  17788. //处理组件里的tab按下事件
  17789. if (me.fireEvent("tabkeydown", evt)) {
  17790. domUtils.preventDefault(evt);
  17791. return;
  17792. }
  17793. var range = me.selection.getRange();
  17794. me.fireEvent("saveScene");
  17795. for (
  17796. var i = 0,
  17797. txt = "",
  17798. tabSize = me.options.tabSize || 4,
  17799. tabNode = me.options.tabNode || "&nbsp;";
  17800. i < tabSize;
  17801. i++
  17802. ) {
  17803. txt += tabNode;
  17804. }
  17805. var span = me.document.createElement("span");
  17806. span.innerHTML = txt + domUtils.fillChar;
  17807. if (range.collapsed) {
  17808. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  17809. } else {
  17810. var filterFn = function(node) {
  17811. return (
  17812. domUtils.isBlockElm(node) &&
  17813. !excludeTagNameForTabKey[node.tagName.toLowerCase()]
  17814. );
  17815. };
  17816. //普通的情况
  17817. start = domUtils.findParent(range.startContainer, filterFn, true);
  17818. end = domUtils.findParent(range.endContainer, filterFn, true);
  17819. if (start && end && start === end) {
  17820. range.deleteContents();
  17821. range.insertNode(span.cloneNode(true).firstChild).setCursor(true);
  17822. } else {
  17823. var bookmark = range.createBookmark();
  17824. range.enlarge(true);
  17825. var bookmark2 = range.createBookmark(),
  17826. current = domUtils.getNextDomNode(bookmark2.start, false, filterFn);
  17827. while (
  17828. current &&
  17829. !(
  17830. domUtils.getPosition(current, bookmark2.end) &
  17831. domUtils.POSITION_FOLLOWING
  17832. )
  17833. ) {
  17834. current.insertBefore(
  17835. span.cloneNode(true).firstChild,
  17836. current.firstChild
  17837. );
  17838. current = domUtils.getNextDomNode(current, false, filterFn);
  17839. }
  17840. range.moveToBookmark(bookmark2).moveToBookmark(bookmark).select();
  17841. }
  17842. }
  17843. domUtils.preventDefault(evt);
  17844. }
  17845. //trace:1634
  17846. //ff的del键在容器空的时候,也会删除
  17847. if (browser.gecko && keyCode == 46) {
  17848. range = me.selection.getRange();
  17849. if (range.collapsed) {
  17850. start = range.startContainer;
  17851. if (domUtils.isEmptyBlock(start)) {
  17852. var parent = start.parentNode;
  17853. while (
  17854. domUtils.getChildCount(parent) == 1 &&
  17855. !domUtils.isBody(parent)
  17856. ) {
  17857. start = parent;
  17858. parent = parent.parentNode;
  17859. }
  17860. if (start === parent.lastChild) evt.preventDefault();
  17861. return;
  17862. }
  17863. }
  17864. }
  17865. /* 修复在编辑区域快捷键 (Mac:meta+alt+I; Win:ctrl+shift+I) 打不开 chrome 控制台的问题 */
  17866. browser.chrome &&
  17867. me.on("keydown", function(type, e) {
  17868. var keyCode = e.keyCode || e.which;
  17869. if (
  17870. ((e.metaKey && e.altKey) || (e.ctrlKey && e.shiftKey)) &&
  17871. keyCode == 73
  17872. ) {
  17873. return true;
  17874. }
  17875. });
  17876. });
  17877. me.addListener("keyup", function(type, evt) {
  17878. var keyCode = evt.keyCode || evt.which,
  17879. rng,
  17880. me = this;
  17881. if (keyCode == keymap.Backspace) {
  17882. if (me.fireEvent("delkeyup")) {
  17883. return;
  17884. }
  17885. rng = me.selection.getRange();
  17886. if (rng.collapsed) {
  17887. var tmpNode,
  17888. autoClearTagName = ["h1", "h2", "h3", "h4", "h5", "h6"];
  17889. if (
  17890. (tmpNode = domUtils.findParentByTagName(
  17891. rng.startContainer,
  17892. autoClearTagName,
  17893. true
  17894. ))
  17895. ) {
  17896. if (domUtils.isEmptyBlock(tmpNode)) {
  17897. var pre = tmpNode.previousSibling;
  17898. if (pre && pre.nodeName != "TABLE") {
  17899. domUtils.remove(tmpNode);
  17900. rng.setStartAtLast(pre).setCursor(false, true);
  17901. return;
  17902. } else {
  17903. var next = tmpNode.nextSibling;
  17904. if (next && next.nodeName != "TABLE") {
  17905. domUtils.remove(tmpNode);
  17906. rng.setStartAtFirst(next).setCursor(false, true);
  17907. return;
  17908. }
  17909. }
  17910. }
  17911. }
  17912. //处理当删除到body时,要重新给p标签展位
  17913. if (domUtils.isBody(rng.startContainer)) {
  17914. var tmpNode = domUtils.createElement(me.document, "p", {
  17915. innerHTML: browser.ie ? domUtils.fillChar : "<br/>"
  17916. });
  17917. rng.insertNode(tmpNode).setStart(tmpNode, 0).setCursor(false, true);
  17918. }
  17919. }
  17920. //chrome下如果删除了inline标签,浏览器会有记忆,在输入文字还是会套上刚才删除的标签,所以这里再选一次就不会了
  17921. if (
  17922. !collapsed &&
  17923. (rng.startContainer.nodeType == 3 ||
  17924. (rng.startContainer.nodeType == 1 &&
  17925. domUtils.isEmptyBlock(rng.startContainer)))
  17926. ) {
  17927. if (browser.ie) {
  17928. var span = rng.document.createElement("span");
  17929. rng.insertNode(span).setStartBefore(span).collapse(true);
  17930. rng.select();
  17931. domUtils.remove(span);
  17932. } else {
  17933. rng.select();
  17934. }
  17935. }
  17936. }
  17937. });
  17938. };
  17939. // plugins/fiximgclick.js
  17940. ///import core
  17941. ///commands 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17942. ///commandsName FixImgClick
  17943. ///commandsTitle 修复chrome下图片不能点击的问题,出现八个角可改变大小
  17944. //修复chrome下图片不能点击的问题,出现八个角可改变大小
  17945. UE.plugins["fiximgclick"] = (function() {
  17946. var elementUpdated = false;
  17947. function Scale() {
  17948. this.editor = null;
  17949. this.resizer = null;
  17950. this.cover = null;
  17951. this.doc = document;
  17952. this.prePos = { x: 0, y: 0 };
  17953. this.startPos = { x: 0, y: 0 };
  17954. }
  17955. (function() {
  17956. var rect = [
  17957. //[left, top, width, height]
  17958. [0, 0, -1, -1],
  17959. [0, 0, 0, -1],
  17960. [0, 0, 1, -1],
  17961. [0, 0, -1, 0],
  17962. [0, 0, 1, 0],
  17963. [0, 0, -1, 1],
  17964. [0, 0, 0, 1],
  17965. [0, 0, 1, 1]
  17966. ];
  17967. Scale.prototype = {
  17968. init: function(editor) {
  17969. var me = this;
  17970. me.editor = editor;
  17971. me.startPos = this.prePos = { x: 0, y: 0 };
  17972. me.dragId = -1;
  17973. var hands = [],
  17974. cover = (me.cover = document.createElement("div")),
  17975. resizer = (me.resizer = document.createElement("div"));
  17976. cover.id = me.editor.ui.id + "_imagescale_cover";
  17977. cover.style.cssText =
  17978. "position:absolute;display:none;z-index:" +
  17979. me.editor.options.zIndex +
  17980. ";filter:alpha(opacity=0); opacity:0;background:#CCC;";
  17981. domUtils.on(cover, "mousedown click", function() {
  17982. me.hide();
  17983. });
  17984. for (i = 0; i < 8; i++) {
  17985. hands.push(
  17986. '<span class="edui-editor-imagescale-hand' + i + '"></span>'
  17987. );
  17988. }
  17989. resizer.id = me.editor.ui.id + "_imagescale";
  17990. resizer.className = "edui-editor-imagescale";
  17991. resizer.innerHTML = hands.join("");
  17992. resizer.style.cssText +=
  17993. ";display:none;border:1px solid #3b77ff;z-index:" +
  17994. me.editor.options.zIndex +
  17995. ";";
  17996. me.editor.ui.getDom().appendChild(cover);
  17997. me.editor.ui.getDom().appendChild(resizer);
  17998. me.initStyle();
  17999. me.initEvents();
  18000. },
  18001. initStyle: function() {
  18002. utils.cssRule(
  18003. "imagescale",
  18004. ".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;}" +
  18005. ".edui-editor-imagescale span{position:absolute;width:6px;height:6px;overflow:hidden;font-size:0px;display:block;background-color:#3C9DD0;}" +
  18006. ".edui-editor-imagescale .edui-editor-imagescale-hand0{cursor:nw-resize;top:0;margin-top:-4px;left:0;margin-left:-4px;}" +
  18007. ".edui-editor-imagescale .edui-editor-imagescale-hand1{cursor:n-resize;top:0;margin-top:-4px;left:50%;margin-left:-4px;}" +
  18008. ".edui-editor-imagescale .edui-editor-imagescale-hand2{cursor:ne-resize;top:0;margin-top:-4px;left:100%;margin-left:-3px;}" +
  18009. ".edui-editor-imagescale .edui-editor-imagescale-hand3{cursor:w-resize;top:50%;margin-top:-4px;left:0;margin-left:-4px;}" +
  18010. ".edui-editor-imagescale .edui-editor-imagescale-hand4{cursor:e-resize;top:50%;margin-top:-4px;left:100%;margin-left:-3px;}" +
  18011. ".edui-editor-imagescale .edui-editor-imagescale-hand5{cursor:sw-resize;top:100%;margin-top:-3px;left:0;margin-left:-4px;}" +
  18012. ".edui-editor-imagescale .edui-editor-imagescale-hand6{cursor:s-resize;top:100%;margin-top:-3px;left:50%;margin-left:-4px;}" +
  18013. ".edui-editor-imagescale .edui-editor-imagescale-hand7{cursor:se-resize;top:100%;margin-top:-3px;left:100%;margin-left:-3px;}"
  18014. );
  18015. },
  18016. initEvents: function() {
  18017. var me = this;
  18018. me.startPos.x = me.startPos.y = 0;
  18019. me.isDraging = false;
  18020. },
  18021. _eventHandler: function(e) {
  18022. var me = this;
  18023. switch (e.type) {
  18024. case "mousedown":
  18025. var hand = e.target || e.srcElement,
  18026. hand;
  18027. if (
  18028. hand.className.indexOf("edui-editor-imagescale-hand") != -1 &&
  18029. me.dragId == -1
  18030. ) {
  18031. me.dragId = hand.className.slice(-1);
  18032. me.startPos.x = me.prePos.x = e.clientX;
  18033. me.startPos.y = me.prePos.y = e.clientY;
  18034. domUtils.on(me.doc, "mousemove", me.proxy(me._eventHandler, me));
  18035. }
  18036. break;
  18037. case "mousemove":
  18038. if (me.dragId != -1) {
  18039. me.updateContainerStyle(me.dragId, {
  18040. x: e.clientX - me.prePos.x,
  18041. y: e.clientY - me.prePos.y
  18042. });
  18043. me.prePos.x = e.clientX;
  18044. me.prePos.y = e.clientY;
  18045. elementUpdated = true;
  18046. me.updateTargetElement();
  18047. }
  18048. break;
  18049. case "mouseup":
  18050. if (me.dragId != -1) {
  18051. me.updateContainerStyle(me.dragId, {
  18052. x: e.clientX - me.prePos.x,
  18053. y: e.clientY - me.prePos.y
  18054. });
  18055. me.updateTargetElement();
  18056. if (me.target.parentNode) me.attachTo(me.target);
  18057. me.dragId = -1;
  18058. }
  18059. domUtils.un(me.doc, "mousemove", me.proxy(me._eventHandler, me));
  18060. //修复只是点击挪动点,但没有改变大小,不应该触发contentchange
  18061. if (elementUpdated) {
  18062. elementUpdated = false;
  18063. me.editor.fireEvent("contentchange");
  18064. }
  18065. break;
  18066. default:
  18067. break;
  18068. }
  18069. },
  18070. updateTargetElement: function() {
  18071. var me = this;
  18072. domUtils.setStyles(me.target, {
  18073. width: me.resizer.style.width,
  18074. height: me.resizer.style.height
  18075. });
  18076. me.target.width = parseInt(me.resizer.style.width);
  18077. me.target.height = parseInt(me.resizer.style.height);
  18078. me.attachTo(me.target);
  18079. },
  18080. updateContainerStyle: function(dir, offset) {
  18081. var me = this,
  18082. dom = me.resizer,
  18083. tmp;
  18084. if (rect[dir][0] != 0) {
  18085. tmp = parseInt(dom.style.left) + offset.x;
  18086. dom.style.left = me._validScaledProp("left", tmp) + "px";
  18087. }
  18088. if (rect[dir][1] != 0) {
  18089. tmp = parseInt(dom.style.top) + offset.y;
  18090. dom.style.top = me._validScaledProp("top", tmp) + "px";
  18091. }
  18092. if (rect[dir][2] != 0) {
  18093. tmp = dom.clientWidth + rect[dir][2] * offset.x;
  18094. dom.style.width = me._validScaledProp("width", tmp) + "px";
  18095. }
  18096. if (rect[dir][3] != 0) {
  18097. tmp = dom.clientHeight + rect[dir][3] * offset.y;
  18098. dom.style.height = me._validScaledProp("height", tmp) + "px";
  18099. }
  18100. },
  18101. _validScaledProp: function(prop, value) {
  18102. var ele = this.resizer,
  18103. wrap = document;
  18104. value = isNaN(value) ? 0 : value;
  18105. switch (prop) {
  18106. case "left":
  18107. return value < 0
  18108. ? 0
  18109. : value + ele.clientWidth > wrap.clientWidth
  18110. ? wrap.clientWidth - ele.clientWidth
  18111. : value;
  18112. case "top":
  18113. return value < 0
  18114. ? 0
  18115. : value + ele.clientHeight > wrap.clientHeight
  18116. ? wrap.clientHeight - ele.clientHeight
  18117. : value;
  18118. case "width":
  18119. return value <= 0
  18120. ? 1
  18121. : value + ele.offsetLeft > wrap.clientWidth
  18122. ? wrap.clientWidth - ele.offsetLeft
  18123. : value;
  18124. case "height":
  18125. return value <= 0
  18126. ? 1
  18127. : value + ele.offsetTop > wrap.clientHeight
  18128. ? wrap.clientHeight - ele.offsetTop
  18129. : value;
  18130. }
  18131. },
  18132. hideCover: function() {
  18133. this.cover.style.display = "none";
  18134. },
  18135. showCover: function() {
  18136. var me = this,
  18137. editorPos = domUtils.getXY(me.editor.ui.getDom()),
  18138. iframePos = domUtils.getXY(me.editor.iframe);
  18139. domUtils.setStyles(me.cover, {
  18140. width: me.editor.iframe.offsetWidth + "px",
  18141. height: me.editor.iframe.offsetHeight + "px",
  18142. top: iframePos.y - editorPos.y + "px",
  18143. left: iframePos.x - editorPos.x + "px",
  18144. position: "absolute",
  18145. display: ""
  18146. });
  18147. },
  18148. show: function(targetObj) {
  18149. var me = this;
  18150. me.resizer.style.display = "block";
  18151. if (targetObj) me.attachTo(targetObj);
  18152. domUtils.on(this.resizer, "mousedown", me.proxy(me._eventHandler, me));
  18153. domUtils.on(me.doc, "mouseup", me.proxy(me._eventHandler, me));
  18154. me.showCover();
  18155. me.editor.fireEvent("afterscaleshow", me);
  18156. me.editor.fireEvent("saveScene");
  18157. },
  18158. hide: function() {
  18159. var me = this;
  18160. me.hideCover();
  18161. me.resizer.style.display = "none";
  18162. domUtils.un(me.resizer, "mousedown", me.proxy(me._eventHandler, me));
  18163. domUtils.un(me.doc, "mouseup", me.proxy(me._eventHandler, me));
  18164. me.editor.fireEvent("afterscalehide", me);
  18165. },
  18166. proxy: function(fn, context) {
  18167. return function(e) {
  18168. return fn.apply(context || this, arguments);
  18169. };
  18170. },
  18171. attachTo: function(targetObj) {
  18172. var me = this,
  18173. target = (me.target = targetObj),
  18174. resizer = this.resizer,
  18175. imgPos = domUtils.getXY(target),
  18176. iframePos = domUtils.getXY(me.editor.iframe),
  18177. editorPos = domUtils.getXY(resizer.parentNode);
  18178. domUtils.setStyles(resizer, {
  18179. width: target.width + "px",
  18180. height: target.height + "px",
  18181. left:
  18182. iframePos.x +
  18183. imgPos.x -
  18184. me.editor.document.body.scrollLeft -
  18185. editorPos.x -
  18186. parseInt(resizer.style.borderLeftWidth) +
  18187. "px",
  18188. top:
  18189. iframePos.y +
  18190. imgPos.y -
  18191. me.editor.document.body.scrollTop -
  18192. editorPos.y -
  18193. parseInt(resizer.style.borderTopWidth) +
  18194. "px"
  18195. });
  18196. }
  18197. };
  18198. })();
  18199. return function() {
  18200. var me = this,
  18201. imageScale;
  18202. me.setOpt("imageScaleEnabled", true);
  18203. if (!browser.ie && me.options.imageScaleEnabled) {
  18204. me.addListener("click", function(type, e) {
  18205. var range = me.selection.getRange(),
  18206. img = range.getClosedNode();
  18207. if (img && img.tagName == "IMG" && me.body.contentEditable != "false") {
  18208. if (
  18209. img.getAttribute("anchorname") ||
  18210. domUtils.hasClass(img, "loadingclass") ||
  18211. domUtils.hasClass(img, "loaderrorclass")
  18212. ) {
  18213. return;
  18214. }
  18215. if (!imageScale) {
  18216. imageScale = new Scale();
  18217. imageScale.init(me);
  18218. me.ui.getDom().appendChild(imageScale.resizer);
  18219. var _keyDownHandler = function(e) {
  18220. imageScale.hide();
  18221. if (imageScale.target)
  18222. me.selection.getRange().selectNode(imageScale.target).select();
  18223. },
  18224. _mouseDownHandler = function(e) {
  18225. var ele = e.target || e.srcElement;
  18226. if (
  18227. ele &&
  18228. (ele.className === undefined ||
  18229. ele.className.indexOf("edui-editor-imagescale") == -1)
  18230. ) {
  18231. _keyDownHandler(e);
  18232. }
  18233. },
  18234. timer;
  18235. me.addListener("afterscaleshow", function(e) {
  18236. me.addListener("beforekeydown", _keyDownHandler);
  18237. me.addListener("beforemousedown", _mouseDownHandler);
  18238. domUtils.on(document, "keydown", _keyDownHandler);
  18239. domUtils.on(document, "mousedown", _mouseDownHandler);
  18240. me.selection.getNative().removeAllRanges();
  18241. });
  18242. me.addListener("afterscalehide", function(e) {
  18243. me.removeListener("beforekeydown", _keyDownHandler);
  18244. me.removeListener("beforemousedown", _mouseDownHandler);
  18245. domUtils.un(document, "keydown", _keyDownHandler);
  18246. domUtils.un(document, "mousedown", _mouseDownHandler);
  18247. var target = imageScale.target;
  18248. if (target.parentNode) {
  18249. me.selection.getRange().selectNode(target).select();
  18250. }
  18251. });
  18252. //TODO 有iframe的情况,mousedown不能往下传。。
  18253. domUtils.on(imageScale.resizer, "mousedown", function(e) {
  18254. me.selection.getNative().removeAllRanges();
  18255. var ele = e.target || e.srcElement;
  18256. if (
  18257. ele &&
  18258. ele.className.indexOf("edui-editor-imagescale-hand") == -1
  18259. ) {
  18260. timer = setTimeout(function() {
  18261. imageScale.hide();
  18262. if (imageScale.target)
  18263. me.selection.getRange().selectNode(ele).select();
  18264. }, 200);
  18265. }
  18266. });
  18267. domUtils.on(imageScale.resizer, "mouseup", function(e) {
  18268. var ele = e.target || e.srcElement;
  18269. if (
  18270. ele &&
  18271. ele.className.indexOf("edui-editor-imagescale-hand") == -1
  18272. ) {
  18273. clearTimeout(timer);
  18274. }
  18275. });
  18276. }
  18277. imageScale.show(img);
  18278. } else {
  18279. if (imageScale && imageScale.resizer.style.display != "none")
  18280. imageScale.hide();
  18281. }
  18282. });
  18283. }
  18284. if (browser.webkit) {
  18285. me.addListener("click", function(type, e) {
  18286. if (e.target.tagName == "IMG" && me.body.contentEditable != "false") {
  18287. var range = new dom.Range(me.document);
  18288. range.selectNode(e.target).select();
  18289. }
  18290. });
  18291. }
  18292. };
  18293. })();
  18294. // plugins/autolink.js
  18295. ///import core
  18296. ///commands 为非ie浏览器自动添加a标签
  18297. ///commandsName AutoLink
  18298. ///commandsTitle 自动增加链接
  18299. /**
  18300. * @description 为非ie浏览器自动添加a标签
  18301. * @author zhanyi
  18302. */
  18303. UE.plugin.register(
  18304. "autolink",
  18305. function() {
  18306. var cont = 0;
  18307. return !browser.ie
  18308. ? {
  18309. bindEvents: {
  18310. reset: function() {
  18311. cont = 0;
  18312. },
  18313. keydown: function(type, evt) {
  18314. var me = this;
  18315. var keyCode = evt.keyCode || evt.which;
  18316. if (keyCode == 32 || keyCode == 13) {
  18317. var sel = me.selection.getNative(),
  18318. range = sel.getRangeAt(0).cloneRange(),
  18319. offset,
  18320. charCode;
  18321. var start = range.startContainer;
  18322. while (start.nodeType == 1 && range.startOffset > 0) {
  18323. start =
  18324. range.startContainer.childNodes[range.startOffset - 1];
  18325. if (!start) {
  18326. break;
  18327. }
  18328. range.setStart(
  18329. start,
  18330. start.nodeType == 1
  18331. ? start.childNodes.length
  18332. : start.nodeValue.length
  18333. );
  18334. range.collapse(true);
  18335. start = range.startContainer;
  18336. }
  18337. do {
  18338. if (range.startOffset == 0) {
  18339. start = range.startContainer.previousSibling;
  18340. while (start && start.nodeType == 1) {
  18341. start = start.lastChild;
  18342. }
  18343. if (!start || domUtils.isFillChar(start)) {
  18344. break;
  18345. }
  18346. offset = start.nodeValue.length;
  18347. } else {
  18348. start = range.startContainer;
  18349. offset = range.startOffset;
  18350. }
  18351. range.setStart(start, offset - 1);
  18352. charCode = range.toString().charCodeAt(0);
  18353. } while (charCode != 160 && charCode != 32);
  18354. if (
  18355. range
  18356. .toString()
  18357. .replace(new RegExp(domUtils.fillChar, "g"), "")
  18358. .match(/(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i)
  18359. ) {
  18360. while (range.toString().length) {
  18361. if (
  18362. /^(?:https?:\/\/|ssh:\/\/|ftp:\/\/|file:\/|www\.)/i.test(
  18363. range.toString()
  18364. )
  18365. ) {
  18366. break;
  18367. }
  18368. try {
  18369. range.setStart(
  18370. range.startContainer,
  18371. range.startOffset + 1
  18372. );
  18373. } catch (e) {
  18374. //trace:2121
  18375. var start = range.startContainer;
  18376. while (!(next = start.nextSibling)) {
  18377. if (domUtils.isBody(start)) {
  18378. return;
  18379. }
  18380. start = start.parentNode;
  18381. }
  18382. range.setStart(next, 0);
  18383. }
  18384. }
  18385. //range的开始边界已经在a标签里的不再处理
  18386. if (
  18387. domUtils.findParentByTagName(
  18388. range.startContainer,
  18389. "a",
  18390. true
  18391. )
  18392. ) {
  18393. return;
  18394. }
  18395. var a = me.document.createElement("a"),
  18396. text = me.document.createTextNode(" "),
  18397. href;
  18398. me.undoManger && me.undoManger.save();
  18399. a.appendChild(range.extractContents());
  18400. a.href = a.innerHTML = a.innerHTML.replace(/<[^>]+>/g, "");
  18401. href = a
  18402. .getAttribute("href")
  18403. .replace(new RegExp(domUtils.fillChar, "g"), "");
  18404. href = /^(?:https?:\/\/)/gi.test(href)
  18405. ? href
  18406. : "http://" + href;
  18407. a.setAttribute("_src", utils.html(href));
  18408. a.href = utils.html(href);
  18409. range.insertNode(a);
  18410. a.parentNode.insertBefore(text, a.nextSibling);
  18411. range.setStart(text, 0);
  18412. range.collapse(true);
  18413. sel.removeAllRanges();
  18414. sel.addRange(range);
  18415. me.undoManger && me.undoManger.save();
  18416. }
  18417. }
  18418. }
  18419. }
  18420. }
  18421. : {};
  18422. },
  18423. function() {
  18424. var keyCodes = {
  18425. 37: 1,
  18426. 38: 1,
  18427. 39: 1,
  18428. 40: 1,
  18429. 13: 1,
  18430. 32: 1
  18431. };
  18432. function checkIsCludeLink(node) {
  18433. if (node.nodeType == 3) {
  18434. return null;
  18435. }
  18436. if (node.nodeName == "A") {
  18437. return node;
  18438. }
  18439. var lastChild = node.lastChild;
  18440. while (lastChild) {
  18441. if (lastChild.nodeName == "A") {
  18442. return lastChild;
  18443. }
  18444. if (lastChild.nodeType == 3) {
  18445. if (domUtils.isWhitespace(lastChild)) {
  18446. lastChild = lastChild.previousSibling;
  18447. continue;
  18448. }
  18449. return null;
  18450. }
  18451. lastChild = lastChild.lastChild;
  18452. }
  18453. }
  18454. browser.ie &&
  18455. this.addListener("keyup", function(cmd, evt) {
  18456. var me = this,
  18457. keyCode = evt.keyCode;
  18458. if (keyCodes[keyCode]) {
  18459. var rng = me.selection.getRange();
  18460. var start = rng.startContainer;
  18461. if (keyCode == 13) {
  18462. while (
  18463. start &&
  18464. !domUtils.isBody(start) &&
  18465. !domUtils.isBlockElm(start)
  18466. ) {
  18467. start = start.parentNode;
  18468. }
  18469. if (start && !domUtils.isBody(start) && start.nodeName == "P") {
  18470. var pre = start.previousSibling;
  18471. if (pre && pre.nodeType == 1) {
  18472. var pre = checkIsCludeLink(pre);
  18473. if (pre && !pre.getAttribute("_href")) {
  18474. domUtils.remove(pre, true);
  18475. }
  18476. }
  18477. }
  18478. } else if (keyCode == 32) {
  18479. if (start.nodeType == 3 && /^\s$/.test(start.nodeValue)) {
  18480. start = start.previousSibling;
  18481. if (
  18482. start &&
  18483. start.nodeName == "A" &&
  18484. !start.getAttribute("_href")
  18485. ) {
  18486. domUtils.remove(start, true);
  18487. }
  18488. }
  18489. } else {
  18490. start = domUtils.findParentByTagName(start, "a", true);
  18491. if (start && !start.getAttribute("_href")) {
  18492. var bk = rng.createBookmark();
  18493. domUtils.remove(start, true);
  18494. rng.moveToBookmark(bk).select(true);
  18495. }
  18496. }
  18497. }
  18498. });
  18499. }
  18500. );
  18501. // plugins/autoheight.js
  18502. ///import core
  18503. ///commands 当输入内容超过编辑器高度时,编辑器自动增高
  18504. ///commandsName AutoHeight,autoHeightEnabled
  18505. ///commandsTitle 自动增高
  18506. /**
  18507. * @description 自动伸展
  18508. * @author zhanyi
  18509. */
  18510. UE.plugins["autoheight"] = function() {
  18511. var me = this;
  18512. //提供开关,就算加载也可以关闭
  18513. me.autoHeightEnabled = me.options.autoHeightEnabled !== false;
  18514. if (!me.autoHeightEnabled) {
  18515. return;
  18516. }
  18517. var bakOverflow,
  18518. lastHeight = 0,
  18519. options = me.options,
  18520. currentHeight,
  18521. timer;
  18522. function adjustHeight() {
  18523. var me = this;
  18524. clearTimeout(timer);
  18525. if (isFullscreen) return;
  18526. if (
  18527. !me.queryCommandState ||
  18528. (me.queryCommandState && me.queryCommandState("source") != 1)
  18529. ) {
  18530. timer = setTimeout(function() {
  18531. var node = me.body.lastChild;
  18532. while (node && node.nodeType != 1) {
  18533. node = node.previousSibling;
  18534. }
  18535. if (node && node.nodeType == 1) {
  18536. node.style.clear = "both";
  18537. currentHeight = Math.max(
  18538. domUtils.getXY(node).y + node.offsetHeight + 25,
  18539. Math.max(options.minFrameHeight, options.initialFrameHeight)
  18540. );
  18541. if (currentHeight !== lastHeight) {
  18542. me.iframe.parentNode.style.transition = 'width 0.3s, height 0.3s, easy-in-out';
  18543. if (currentHeight !== parseInt(me.iframe.parentNode.style.height)) {
  18544. me.iframe.parentNode.style.height = currentHeight + "px";
  18545. }
  18546. me.body.style.height = currentHeight + "px";
  18547. lastHeight = currentHeight;
  18548. }
  18549. domUtils.removeStyle(node, "clear");
  18550. }
  18551. }, 50);
  18552. }
  18553. }
  18554. var isFullscreen;
  18555. me.addListener("fullscreenchanged", function(cmd, f) {
  18556. isFullscreen = f;
  18557. });
  18558. me.addListener("destroy", function() {
  18559. domUtils.un(me.window, "scroll", fixedScrollTop);
  18560. me.removeListener(
  18561. "contentchange afterinserthtml keyup mouseup",
  18562. adjustHeight
  18563. );
  18564. });
  18565. me.enableAutoHeight = function() {
  18566. var me = this;
  18567. if (!me.autoHeightEnabled) {
  18568. return;
  18569. }
  18570. var doc = me.document;
  18571. me.autoHeightEnabled = true;
  18572. bakOverflow = doc.body.style.overflowY;
  18573. doc.body.style.overflowY = "hidden";
  18574. me.addListener("contentchange afterinserthtml keyup mouseup", adjustHeight);
  18575. //ff不给事件算得不对
  18576. setTimeout(function() {
  18577. adjustHeight.call(me);
  18578. }, browser.gecko ? 100 : 0);
  18579. me.fireEvent("autoheightchanged", me.autoHeightEnabled);
  18580. };
  18581. me.disableAutoHeight = function() {
  18582. me.body.style.overflowY = bakOverflow || "";
  18583. me.removeListener("contentchange", adjustHeight);
  18584. me.removeListener("keyup", adjustHeight);
  18585. me.removeListener("mouseup", adjustHeight);
  18586. me.autoHeightEnabled = false;
  18587. me.fireEvent("autoheightchanged", me.autoHeightEnabled);
  18588. };
  18589. me.on("setHeight", function() {
  18590. me.disableAutoHeight();
  18591. });
  18592. me.addListener("ready", function() {
  18593. me.enableAutoHeight();
  18594. //trace:1764
  18595. var timer;
  18596. domUtils.on(
  18597. browser.ie ? me.body : me.document,
  18598. browser.webkit ? "dragover" : "drop",
  18599. function() {
  18600. clearTimeout(timer);
  18601. timer = setTimeout(function() {
  18602. //trace:3681
  18603. adjustHeight.call(me);
  18604. }, 100);
  18605. }
  18606. );
  18607. //修复内容过多时,回到顶部,顶部内容被工具栏遮挡问题
  18608. domUtils.on(me.window, "scroll", fixedScrollTop);
  18609. });
  18610. var lastScrollY;
  18611. function fixedScrollTop() {
  18612. if (!me.window) return;
  18613. if (lastScrollY === null) {
  18614. lastScrollY = me.window.scrollY;
  18615. } else if (me.window.scrollY == 0 && lastScrollY != 0) {
  18616. me.window.scrollTo(0, 0);
  18617. lastScrollY = null;
  18618. }
  18619. }
  18620. };
  18621. // plugins/autofloat.js
  18622. ///import core
  18623. ///commands 悬浮工具栏
  18624. ///commandsName AutoFloat,autoFloatEnabled
  18625. ///commandsTitle 悬浮工具栏
  18626. /**
  18627. * modified by chengchao01
  18628. * 注意: 引入此功能后,在IE6下会将body的背景图片覆盖掉!
  18629. */
  18630. UE.plugins["autofloat"] = function() {
  18631. var me = this,
  18632. lang = me.getLang();
  18633. me.setOpt({
  18634. topOffset: 0
  18635. });
  18636. var optsAutoFloatEnabled = me.options.autoFloatEnabled !== false,
  18637. topOffset = me.options.topOffset;
  18638. //如果不固定toolbar的位置,则直接退出
  18639. if (!optsAutoFloatEnabled) {
  18640. return;
  18641. }
  18642. var uiUtils = UE.ui.uiUtils,
  18643. LteIE6 = browser.ie && browser.version <= 6,
  18644. quirks = browser.quirks;
  18645. function checkHasUI() {
  18646. if (!UE.ui) {
  18647. alert(lang.autofloatMsg);
  18648. return 0;
  18649. }
  18650. return 1;
  18651. }
  18652. function fixIE6FixedPos() {
  18653. var docStyle = document.body.style;
  18654. docStyle.backgroundImage = 'url("about:blank")';
  18655. docStyle.backgroundAttachment = "fixed";
  18656. }
  18657. var bakCssText,
  18658. placeHolder = document.createElement("div"),
  18659. toolbarBox,
  18660. orgTop,
  18661. getPosition,
  18662. flag = true; //ie7模式下需要偏移
  18663. function setFloating() {
  18664. var toobarBoxPos = domUtils.getXY(toolbarBox),
  18665. origalFloat = domUtils.getComputedStyle(toolbarBox, "position"),
  18666. origalLeft = domUtils.getComputedStyle(toolbarBox, "left");
  18667. toolbarBox.style.width = toolbarBox.offsetWidth + "px";
  18668. toolbarBox.style.zIndex = me.options.zIndex * 1 + 1;
  18669. toolbarBox.parentNode.insertBefore(placeHolder, toolbarBox);
  18670. if (LteIE6 || (quirks && browser.ie)) {
  18671. if (toolbarBox.style.position != "absolute") {
  18672. toolbarBox.style.position = "absolute";
  18673. }
  18674. toolbarBox.style.top =
  18675. (document.body.scrollTop || document.documentElement.scrollTop) -
  18676. orgTop +
  18677. topOffset +
  18678. "px";
  18679. } else {
  18680. if (browser.ie7Compat && flag) {
  18681. flag = false;
  18682. toolbarBox.style.left =
  18683. domUtils.getXY(toolbarBox).x -
  18684. document.documentElement.getBoundingClientRect().left +
  18685. 2 +
  18686. "px";
  18687. }
  18688. if (toolbarBox.style.position != "fixed") {
  18689. toolbarBox.style.position = "fixed";
  18690. toolbarBox.style.top = topOffset + "px";
  18691. (origalFloat == "absolute" || origalFloat == "relative") &&
  18692. parseFloat(origalLeft) &&
  18693. (toolbarBox.style.left = toobarBoxPos.x + "px");
  18694. }
  18695. }
  18696. }
  18697. function unsetFloating() {
  18698. flag = true;
  18699. if (placeHolder.parentNode) {
  18700. placeHolder.parentNode.removeChild(placeHolder);
  18701. }
  18702. toolbarBox.style.cssText = bakCssText;
  18703. }
  18704. me.unsetFloating = unsetFloating;
  18705. function updateFloating() {
  18706. var rect3 = getPosition(me.container);
  18707. var offset = me.options.toolbarTopOffset || 0;
  18708. if (rect3.top < 0 && rect3.bottom - toolbarBox.offsetHeight > offset) {
  18709. setFloating();
  18710. } else {
  18711. unsetFloating();
  18712. }
  18713. }
  18714. var defer_updateFloating = utils.defer(
  18715. function() {
  18716. updateFloating();
  18717. },
  18718. browser.ie ? 200 : 100,
  18719. true
  18720. );
  18721. me.addListener("destroy", function() {
  18722. domUtils.un(window, ["scroll", "resize"], updateFloating);
  18723. me.removeListener("keydown", defer_updateFloating);
  18724. });
  18725. me.addListener("ready", function() {
  18726. if (checkHasUI(me)) {
  18727. //加载了ui组件,但在new时,没有加载ui,导致编辑器实例上没有ui类,所以这里做判断
  18728. if (!me.ui) {
  18729. return;
  18730. }
  18731. getPosition = uiUtils.getClientRect;
  18732. toolbarBox = me.ui.getDom("toolbarbox");
  18733. orgTop = getPosition(toolbarBox).top;
  18734. bakCssText = toolbarBox.style.cssText;
  18735. placeHolder.style.height = toolbarBox.offsetHeight + "px";
  18736. if (LteIE6) {
  18737. fixIE6FixedPos();
  18738. }
  18739. domUtils.on(window, ["scroll", "resize"], updateFloating);
  18740. me.addListener("keydown", defer_updateFloating);
  18741. me.addListener("beforefullscreenchange", function(t, enabled) {
  18742. if (enabled) {
  18743. unsetFloating();
  18744. }
  18745. });
  18746. me.addListener("fullscreenchanged", function(t, enabled) {
  18747. if (!enabled) {
  18748. updateFloating();
  18749. }
  18750. });
  18751. me.addListener("sourcemodechanged", function(t, enabled) {
  18752. setTimeout(function() {
  18753. updateFloating();
  18754. }, 0);
  18755. });
  18756. me.addListener("clearDoc", function() {
  18757. setTimeout(function() {
  18758. updateFloating();
  18759. }, 0);
  18760. });
  18761. }
  18762. });
  18763. };
  18764. // plugins/video.js
  18765. /**
  18766. * video插件, 为UEditor提供视频插入支持
  18767. * @file
  18768. * @since 1.2.6.1
  18769. */
  18770. UE.plugins["video"] = function() {
  18771. var me = this;
  18772. /**
  18773. * 创建插入视频字符窜
  18774. * @param url 视频地址
  18775. * @param width 视频宽度
  18776. * @param height 视频高度
  18777. * @param align 视频对齐
  18778. * @param toEmbed 是否以flash代替显示
  18779. * @param addParagraph 是否需要添加P 标签
  18780. */
  18781. function creatInsertStr(url, width, height, id, align, classname, type) {
  18782. var str;
  18783. switch (type) {
  18784. case 'iframe':
  18785. str = '<iframe class="' + classname + '" ' +
  18786. ' src="' + utils.html(url) + '" width="' + width + '" height="' + height + '"' +
  18787. ' frameborder=0 allowfullscreen>';
  18788. break;
  18789. case "image":
  18790. str =
  18791. "<img " +
  18792. (id ? 'id="' + id + '"' : "") +
  18793. ' width="' +
  18794. width +
  18795. '" height="' +
  18796. height +
  18797. '" _url="' +
  18798. url +
  18799. '" class="' +
  18800. '"' +
  18801. ' src="' +
  18802. me.options.UEDITOR_HOME_URL +
  18803. 'themes/default/images/spacer.gif" style="background:url(' +
  18804. me.options.UEDITOR_HOME_URL +
  18805. "themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;" +
  18806. (align ? "float:" + align + ";" : "") +
  18807. '" />';
  18808. break;
  18809. case "embed":
  18810. str =
  18811. '<embed type="application/x-shockwave-flash" class="' +
  18812. classname +
  18813. '" pluginspage="http://www.macromedia.com/go/getflashplayer"' +
  18814. ' src="' +
  18815. utils.html(url) +
  18816. '" width="' +
  18817. width +
  18818. '" height="' +
  18819. height +
  18820. '"' +
  18821. (align ? ' style="float:' + align + '"' : "") +
  18822. ' wmode="transparent" play="true" loop="false" menu="false" allowscriptaccess="never" allowfullscreen="true" >';
  18823. break;
  18824. case "video":
  18825. var ext = url.substr(url.lastIndexOf(".") + 1);
  18826. if (ext == "ogv") ext = "ogg";
  18827. str =
  18828. "<video" +
  18829. (id ? ' id="' + id + '"' : "") +
  18830. ' class="' +
  18831. classname +
  18832. '" ' +
  18833. (align ? ' style="float:' + align + '"' : "") +
  18834. ' controls preload="none" width="' +
  18835. width +
  18836. '" height="' +
  18837. height +
  18838. '" src="' +
  18839. url +
  18840. '" data-setup="{}">' +
  18841. '<source src="' +
  18842. url +
  18843. '" type="video/' +
  18844. ext +
  18845. '" /></video>';
  18846. break;
  18847. }
  18848. return str;
  18849. }
  18850. function switchImgAndVideo(root, img2video) {
  18851. utils.each(
  18852. root.getNodesByTagName(img2video ? "img" : "embed video"),
  18853. function(node) {
  18854. var className = node.getAttr("class");
  18855. if (className && className.indexOf("edui-faked-video") != -1) {
  18856. var html = creatInsertStr(
  18857. img2video ? node.getAttr("_url") : node.getAttr("src"),
  18858. node.getAttr("width"),
  18859. node.getAttr("height"),
  18860. null,
  18861. node.getStyle("float") || "",
  18862. className,
  18863. img2video ? "embed" : "image"
  18864. );
  18865. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  18866. }
  18867. if (className && className.indexOf("edui-upload-video") != -1) {
  18868. var html = creatInsertStr(
  18869. img2video ? node.getAttr("_url") : node.getAttr("src"),
  18870. node.getAttr("width"),
  18871. node.getAttr("height"),
  18872. null,
  18873. node.getStyle("float") || "",
  18874. className,
  18875. img2video ? "video" : "image"
  18876. );
  18877. node.parentNode.replaceChild(UE.uNode.createElement(html), node);
  18878. }
  18879. }
  18880. );
  18881. }
  18882. me.addOutputRule(function(root) {
  18883. switchImgAndVideo(root, true);
  18884. });
  18885. me.addInputRule(function(root) {
  18886. switchImgAndVideo(root);
  18887. });
  18888. /**
  18889. * 插入视频
  18890. * @command insertvideo
  18891. * @method execCommand
  18892. * @param { String } cmd 命令字符串
  18893. * @param { Object } videoAttr 键值对对象, 描述一个视频的所有属性
  18894. * @example
  18895. * ```javascript
  18896. *
  18897. * var videoAttr = {
  18898. * //视频地址
  18899. * url: 'http://www.youku.com/xxx',
  18900. * //视频宽高值, 单位px
  18901. * width: 200,
  18902. * height: 100
  18903. * };
  18904. *
  18905. * //editor 是编辑器实例
  18906. * //向编辑器插入单个视频
  18907. * editor.execCommand( 'insertvideo', videoAttr );
  18908. * ```
  18909. */
  18910. /**
  18911. * 插入视频
  18912. * @command insertvideo
  18913. * @method execCommand
  18914. * @param { String } cmd 命令字符串
  18915. * @param { Array } videoArr 需要插入的视频的数组, 其中的每一个元素都是一个键值对对象, 描述了一个视频的所有属性
  18916. * @example
  18917. * ```javascript
  18918. *
  18919. * var videoAttr1 = {
  18920. * //视频地址
  18921. * url: 'http://www.youku.com/xxx',
  18922. * //视频宽高值, 单位px
  18923. * width: 200,
  18924. * height: 100
  18925. * },
  18926. * videoAttr2 = {
  18927. * //视频地址
  18928. * url: 'http://www.youku.com/xxx',
  18929. * //视频宽高值, 单位px
  18930. * width: 200,
  18931. * height: 100
  18932. * }
  18933. *
  18934. * //editor 是编辑器实例
  18935. * //该方法将会向编辑器内插入两个视频
  18936. * editor.execCommand( 'insertvideo', [ videoAttr1, videoAttr2 ] );
  18937. * ```
  18938. */
  18939. /**
  18940. * 查询当前光标所在处是否是一个视频
  18941. * @command insertvideo
  18942. * @method queryCommandState
  18943. * @param { String } cmd 需要查询的命令字符串
  18944. * @return { int } 如果当前光标所在处的元素是一个视频对象, 则返回1,否则返回0
  18945. * @example
  18946. * ```javascript
  18947. *
  18948. * //editor 是编辑器实例
  18949. * editor.queryCommandState( 'insertvideo' );
  18950. * ```
  18951. */
  18952. me.commands["insertvideo"] = {
  18953. execCommand: function(cmd, videoObjs, type) {
  18954. videoObjs = utils.isArray(videoObjs) ? videoObjs : [videoObjs];
  18955. if (me.fireEvent("beforeinsertvideo", videoObjs) === true) {
  18956. return;
  18957. }
  18958. var html = [],
  18959. id = "tmpVideo",
  18960. cl;
  18961. for (var i = 0, vi, len = videoObjs.length; i < len; i++) {
  18962. vi = videoObjs[i];
  18963. var videoType = 'iframe';
  18964. if(vi.url.match(/.mp4$/)){
  18965. videoType = 'video';
  18966. }
  18967. cl = videoType == "iframe"
  18968. ? "edui-video-iframe"
  18969. : "edui-video-video";
  18970. html.push(
  18971. creatInsertStr(
  18972. vi.url,
  18973. vi.width || 420,
  18974. vi.height || 280,
  18975. id + i,
  18976. null,
  18977. cl,
  18978. videoType
  18979. )
  18980. );
  18981. }
  18982. me.execCommand("inserthtml", html.join(""), true);
  18983. var rng = this.selection.getRange();
  18984. // for (var i = 0, len = videoObjs.length; i < len; i++) {
  18985. // var img = this.document.getElementById("tmpVideo" + i);
  18986. // domUtils.removeAttributes(img, "id");
  18987. // rng.selectNode(img).select();
  18988. // me.execCommand("imagefloat", videoObjs[i].align);
  18989. // }
  18990. me.fireEvent("afterinsertvideo", videoObjs);
  18991. },
  18992. queryCommandState: function() {
  18993. var img = me.selection.getRange().getClosedNode(),
  18994. flag =
  18995. img &&
  18996. (img.className == "edui-video-iframe" ||
  18997. img.className.indexOf("edui-video-iframe") != -1 ||
  18998. img.className == "edui-video-video" ||
  18999. img.className.indexOf("edui-video-video") != -1);
  19000. return flag ? 1 : 0;
  19001. }
  19002. };
  19003. };
  19004. // plugins/table.core.js
  19005. /**
  19006. * Created with JetBrains WebStorm.
  19007. * User: taoqili
  19008. * Date: 13-1-18
  19009. * Time: 上午11:09
  19010. * To change this template use File | Settings | File Templates.
  19011. */
  19012. /**
  19013. * UE表格操作类
  19014. * @param table
  19015. * @constructor
  19016. */
  19017. (function() {
  19018. var UETable = (UE.UETable = function(table) {
  19019. this.table = table;
  19020. this.indexTable = [];
  19021. this.selectedTds = [];
  19022. this.cellsRange = {};
  19023. this.update(table);
  19024. });
  19025. //===以下为静态工具方法===
  19026. UETable.removeSelectedClass = function(cells) {
  19027. utils.each(cells, function(cell) {
  19028. domUtils.removeClasses(cell, "selectTdClass");
  19029. });
  19030. };
  19031. UETable.addSelectedClass = function(cells) {
  19032. utils.each(cells, function(cell) {
  19033. domUtils.addClass(cell, "selectTdClass");
  19034. });
  19035. };
  19036. UETable.isEmptyBlock = function(node) {
  19037. var reg = new RegExp(domUtils.fillChar, "g");
  19038. if (
  19039. node[browser.ie ? "innerText" : "textContent"]
  19040. .replace(/^\s*$/, "")
  19041. .replace(reg, "").length > 0
  19042. ) {
  19043. return 0;
  19044. }
  19045. for (var i in dtd.$isNotEmpty)
  19046. if (dtd.$isNotEmpty.hasOwnProperty(i)) {
  19047. if (node.getElementsByTagName(i).length) {
  19048. return 0;
  19049. }
  19050. }
  19051. return 1;
  19052. };
  19053. UETable.getWidth = function(cell) {
  19054. if (!cell) return 0;
  19055. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  19056. };
  19057. /**
  19058. * 获取单元格或者单元格组的“对齐”状态。 如果当前的检测对象是一个单元格组, 只有在满足所有单元格的 水平和竖直 对齐属性都相同的
  19059. * 条件时才会返回其状态值,否则将返回null; 如果当前只检测了一个单元格, 则直接返回当前单元格的对齐状态;
  19060. * @param table cell or table cells , 支持单个单元格dom对象 或者 单元格dom对象数组
  19061. * @return { align: 'left' || 'right' || 'center', valign: 'top' || 'middle' || 'bottom' } 或者 null
  19062. */
  19063. UETable.getTableCellAlignState = function(cells) {
  19064. !utils.isArray(cells) && (cells = [cells]);
  19065. var result = {},
  19066. status = ["align", "valign"],
  19067. tempStatus = null,
  19068. isSame = true; //状态是否相同
  19069. utils.each(cells, function(cellNode) {
  19070. utils.each(status, function(currentState) {
  19071. tempStatus = cellNode.getAttribute(currentState);
  19072. if (!result[currentState] && tempStatus) {
  19073. result[currentState] = tempStatus;
  19074. } else if (
  19075. !result[currentState] ||
  19076. tempStatus !== result[currentState]
  19077. ) {
  19078. isSame = false;
  19079. return false;
  19080. }
  19081. });
  19082. return isSame;
  19083. });
  19084. return isSame ? result : null;
  19085. };
  19086. /**
  19087. * 根据当前选区获取相关的table信息
  19088. * @return {Object}
  19089. */
  19090. UETable.getTableItemsByRange = function(editor) {
  19091. var start = editor.selection.getStart();
  19092. //ff下会选中bookmark
  19093. if (
  19094. start &&
  19095. start.id &&
  19096. start.id.indexOf("_baidu_bookmark_start_") === 0 &&
  19097. start.nextSibling
  19098. ) {
  19099. start = start.nextSibling;
  19100. }
  19101. //在table或者td边缘有可能存在选中tr的情况
  19102. var cell = start && domUtils.findParentByTagName(start, ["td", "th"], true),
  19103. tr = cell && cell.parentNode,
  19104. table = tr && domUtils.findParentByTagName(tr, ["table"]),
  19105. caption = table && table.getElementsByTagName("caption")[0];
  19106. return {
  19107. cell: cell,
  19108. tr: tr,
  19109. table: table,
  19110. caption: caption
  19111. };
  19112. };
  19113. UETable.getUETableBySelected = function(editor) {
  19114. var table = UETable.getTableItemsByRange(editor).table;
  19115. if (table && table.ueTable && table.ueTable.selectedTds.length) {
  19116. return table.ueTable;
  19117. }
  19118. return null;
  19119. };
  19120. UETable.getDefaultValue = function(editor, table) {
  19121. var borderMap = {
  19122. thin: "0px",
  19123. medium: "1px",
  19124. thick: "2px"
  19125. },
  19126. tableBorder,
  19127. tdPadding,
  19128. tdBorder,
  19129. tmpValue;
  19130. if (!table) {
  19131. table = editor.document.createElement("table");
  19132. table.insertRow(0).insertCell(0).innerHTML = "xxx";
  19133. editor.body.appendChild(table);
  19134. var td = table.getElementsByTagName("td")[0];
  19135. tmpValue = domUtils.getComputedStyle(table, "border-left-width");
  19136. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  19137. tmpValue = domUtils.getComputedStyle(td, "padding-left");
  19138. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  19139. tmpValue = domUtils.getComputedStyle(td, "border-left-width");
  19140. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  19141. domUtils.remove(table);
  19142. return {
  19143. tableBorder: tableBorder,
  19144. tdPadding: tdPadding,
  19145. tdBorder: tdBorder
  19146. };
  19147. } else {
  19148. td = table.getElementsByTagName("td")[0];
  19149. tmpValue = domUtils.getComputedStyle(table, "border-left-width");
  19150. tableBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  19151. tmpValue = domUtils.getComputedStyle(td, "padding-left");
  19152. tdPadding = parseInt(borderMap[tmpValue] || tmpValue, 10);
  19153. tmpValue = domUtils.getComputedStyle(td, "border-left-width");
  19154. tdBorder = parseInt(borderMap[tmpValue] || tmpValue, 10);
  19155. return {
  19156. tableBorder: tableBorder,
  19157. tdPadding: tdPadding,
  19158. tdBorder: tdBorder
  19159. };
  19160. }
  19161. };
  19162. /**
  19163. * 根据当前点击的td或者table获取索引对象
  19164. * @param tdOrTable
  19165. */
  19166. UETable.getUETable = function(tdOrTable) {
  19167. var tag = tdOrTable.tagName.toLowerCase();
  19168. tdOrTable = tag == "td" || tag == "th" || tag == "caption"
  19169. ? domUtils.findParentByTagName(tdOrTable, "table", true)
  19170. : tdOrTable;
  19171. if (!tdOrTable.ueTable) {
  19172. tdOrTable.ueTable = new UETable(tdOrTable);
  19173. }
  19174. return tdOrTable.ueTable;
  19175. };
  19176. UETable.cloneCell = function(cell, ignoreMerge, keepPro) {
  19177. if (!cell || utils.isString(cell)) {
  19178. return this.table.ownerDocument.createElement(cell || "td");
  19179. }
  19180. var flag = domUtils.hasClass(cell, "selectTdClass");
  19181. flag && domUtils.removeClasses(cell, "selectTdClass");
  19182. var tmpCell = cell.cloneNode(true);
  19183. if (ignoreMerge) {
  19184. tmpCell.rowSpan = tmpCell.colSpan = 1;
  19185. }
  19186. //去掉宽高
  19187. !keepPro && domUtils.removeAttributes(tmpCell, "width height");
  19188. !keepPro && domUtils.removeAttributes(tmpCell, "style");
  19189. tmpCell.style.borderLeftStyle = "";
  19190. tmpCell.style.borderTopStyle = "";
  19191. tmpCell.style.borderLeftColor = cell.style.borderRightColor;
  19192. tmpCell.style.borderLeftWidth = cell.style.borderRightWidth;
  19193. tmpCell.style.borderTopColor = cell.style.borderBottomColor;
  19194. tmpCell.style.borderTopWidth = cell.style.borderBottomWidth;
  19195. flag && domUtils.addClass(cell, "selectTdClass");
  19196. return tmpCell;
  19197. };
  19198. UETable.prototype = {
  19199. getMaxRows: function() {
  19200. var rows = this.table.rows,
  19201. maxLen = 1;
  19202. for (var i = 0, row; (row = rows[i]); i++) {
  19203. var currentMax = 1;
  19204. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  19205. currentMax = Math.max(cj.rowSpan || 1, currentMax);
  19206. }
  19207. maxLen = Math.max(currentMax + i, maxLen);
  19208. }
  19209. return maxLen;
  19210. },
  19211. /**
  19212. * 获取当前表格的最大列数
  19213. */
  19214. getMaxCols: function() {
  19215. var rows = this.table.rows,
  19216. maxLen = 0,
  19217. cellRows = {};
  19218. for (var i = 0, row; (row = rows[i]); i++) {
  19219. var cellsNum = 0;
  19220. for (var j = 0, cj; (cj = row.cells[j++]); ) {
  19221. cellsNum += cj.colSpan || 1;
  19222. if (cj.rowSpan && cj.rowSpan > 1) {
  19223. for (var k = 1; k < cj.rowSpan; k++) {
  19224. if (!cellRows["row_" + (i + k)]) {
  19225. cellRows["row_" + (i + k)] = cj.colSpan || 1;
  19226. } else {
  19227. cellRows["row_" + (i + k)]++;
  19228. }
  19229. }
  19230. }
  19231. }
  19232. cellsNum += cellRows["row_" + i] || 0;
  19233. maxLen = Math.max(cellsNum, maxLen);
  19234. }
  19235. return maxLen;
  19236. },
  19237. getCellColIndex: function(cell) {},
  19238. /**
  19239. * 获取当前cell旁边的单元格,
  19240. * @param cell
  19241. * @param right
  19242. */
  19243. getHSideCell: function(cell, right) {
  19244. try {
  19245. var cellInfo = this.getCellInfo(cell),
  19246. previewRowIndex,
  19247. previewColIndex;
  19248. var len = this.selectedTds.length,
  19249. range = this.cellsRange;
  19250. //首行或者首列没有前置单元格
  19251. if (
  19252. (!right && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19253. (right &&
  19254. (!len
  19255. ? cellInfo.colIndex == this.colsNum - 1
  19256. : range.endColIndex == this.colsNum - 1))
  19257. )
  19258. return null;
  19259. previewRowIndex = !len ? cellInfo.rowIndex : range.beginRowIndex;
  19260. previewColIndex = !right
  19261. ? !len
  19262. ? cellInfo.colIndex < 1 ? 0 : cellInfo.colIndex - 1
  19263. : range.beginColIndex - 1
  19264. : !len ? cellInfo.colIndex + 1 : range.endColIndex + 1;
  19265. return this.getCell(
  19266. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19267. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19268. );
  19269. } catch (e) {
  19270. showError(e);
  19271. }
  19272. },
  19273. getTabNextCell: function(cell, preRowIndex) {
  19274. var cellInfo = this.getCellInfo(cell),
  19275. rowIndex = preRowIndex || cellInfo.rowIndex,
  19276. colIndex = cellInfo.colIndex + 1 + (cellInfo.colSpan - 1),
  19277. nextCell;
  19278. try {
  19279. nextCell = this.getCell(
  19280. this.indexTable[rowIndex][colIndex].rowIndex,
  19281. this.indexTable[rowIndex][colIndex].cellIndex
  19282. );
  19283. } catch (e) {
  19284. try {
  19285. rowIndex = rowIndex * 1 + 1;
  19286. colIndex = 0;
  19287. nextCell = this.getCell(
  19288. this.indexTable[rowIndex][colIndex].rowIndex,
  19289. this.indexTable[rowIndex][colIndex].cellIndex
  19290. );
  19291. } catch (e) {}
  19292. }
  19293. return nextCell;
  19294. },
  19295. /**
  19296. * 获取视觉上的后置单元格
  19297. * @param cell
  19298. * @param bottom
  19299. */
  19300. getVSideCell: function(cell, bottom, ignoreRange) {
  19301. try {
  19302. var cellInfo = this.getCellInfo(cell),
  19303. nextRowIndex,
  19304. nextColIndex;
  19305. var len = this.selectedTds.length && !ignoreRange,
  19306. range = this.cellsRange;
  19307. //末行或者末列没有后置单元格
  19308. if (
  19309. (!bottom && cellInfo.rowIndex == 0) ||
  19310. (bottom &&
  19311. (!len
  19312. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19313. : range.endRowIndex == this.rowsNum - 1))
  19314. )
  19315. return null;
  19316. nextRowIndex = !bottom
  19317. ? !len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1
  19318. : !len ? cellInfo.rowIndex + cellInfo.rowSpan : range.endRowIndex + 1;
  19319. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  19320. return this.getCell(
  19321. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19322. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19323. );
  19324. } catch (e) {
  19325. showError(e);
  19326. }
  19327. },
  19328. /**
  19329. * 获取相同结束位置的单元格,xOrY指代了是获取x轴相同还是y轴相同
  19330. */
  19331. getSameEndPosCells: function(cell, xOrY) {
  19332. try {
  19333. var flag = xOrY.toLowerCase() === "x",
  19334. end =
  19335. domUtils.getXY(cell)[flag ? "x" : "y"] +
  19336. cell["offset" + (flag ? "Width" : "Height")],
  19337. rows = this.table.rows,
  19338. cells = null,
  19339. returns = [];
  19340. for (var i = 0; i < this.rowsNum; i++) {
  19341. cells = rows[i].cells;
  19342. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19343. var tmpEnd =
  19344. domUtils.getXY(tmpCell)[flag ? "x" : "y"] +
  19345. tmpCell["offset" + (flag ? "Width" : "Height")];
  19346. //对应行的td已经被上面行rowSpan了
  19347. if (tmpEnd > end && flag) break;
  19348. if (cell == tmpCell || end == tmpEnd) {
  19349. //只获取单一的单元格
  19350. //todo 仅获取单一单元格在特定情况下会造成returns为空,从而影响后续的拖拽实现,修正这个。需考虑性能
  19351. if (tmpCell[flag ? "colSpan" : "rowSpan"] == 1) {
  19352. returns.push(tmpCell);
  19353. }
  19354. if (flag) break;
  19355. }
  19356. }
  19357. }
  19358. return returns;
  19359. } catch (e) {
  19360. showError(e);
  19361. }
  19362. },
  19363. setCellContent: function(cell, content) {
  19364. cell.innerHTML = content || (browser.ie ? domUtils.fillChar : "<br />");
  19365. },
  19366. cloneCell: UETable.cloneCell,
  19367. /**
  19368. * 获取跟当前单元格的右边竖线为左边的所有未合并单元格
  19369. */
  19370. getSameStartPosXCells: function(cell) {
  19371. try {
  19372. var start = domUtils.getXY(cell).x + cell.offsetWidth,
  19373. rows = this.table.rows,
  19374. cells,
  19375. returns = [];
  19376. for (var i = 0; i < this.rowsNum; i++) {
  19377. cells = rows[i].cells;
  19378. for (var j = 0, tmpCell; (tmpCell = cells[j++]); ) {
  19379. var tmpStart = domUtils.getXY(tmpCell).x;
  19380. if (tmpStart > start) break;
  19381. if (tmpStart == start && tmpCell.colSpan == 1) {
  19382. returns.push(tmpCell);
  19383. break;
  19384. }
  19385. }
  19386. }
  19387. return returns;
  19388. } catch (e) {
  19389. showError(e);
  19390. }
  19391. },
  19392. /**
  19393. * 更新table对应的索引表
  19394. */
  19395. update: function(table) {
  19396. this.table = table || this.table;
  19397. this.selectedTds = [];
  19398. this.cellsRange = {};
  19399. this.indexTable = [];
  19400. var rows = this.table.rows,
  19401. rowsNum = this.getMaxRows(),
  19402. dNum = rowsNum - rows.length,
  19403. colsNum = this.getMaxCols();
  19404. while (dNum--) {
  19405. this.table.insertRow(rows.length);
  19406. }
  19407. this.rowsNum = rowsNum;
  19408. this.colsNum = colsNum;
  19409. for (var i = 0, len = rows.length; i < len; i++) {
  19410. this.indexTable[i] = new Array(colsNum);
  19411. }
  19412. //填充索引表
  19413. for (var rowIndex = 0, row; (row = rows[rowIndex]); rowIndex++) {
  19414. for (
  19415. var cellIndex = 0, cell, cells = row.cells;
  19416. (cell = cells[cellIndex]);
  19417. cellIndex++
  19418. ) {
  19419. //修正整行被rowSpan时导致的行数计算错误
  19420. if (cell.rowSpan > rowsNum) {
  19421. cell.rowSpan = rowsNum;
  19422. }
  19423. var colIndex = cellIndex,
  19424. rowSpan = cell.rowSpan || 1,
  19425. colSpan = cell.colSpan || 1;
  19426. //当已经被上一行rowSpan或者被前一列colSpan了,则跳到下一个单元格进行
  19427. while (this.indexTable[rowIndex][colIndex]) colIndex++;
  19428. for (var j = 0; j < rowSpan; j++) {
  19429. for (var k = 0; k < colSpan; k++) {
  19430. this.indexTable[rowIndex + j][colIndex + k] = {
  19431. rowIndex: rowIndex,
  19432. cellIndex: cellIndex,
  19433. colIndex: colIndex,
  19434. rowSpan: rowSpan,
  19435. colSpan: colSpan
  19436. };
  19437. }
  19438. }
  19439. }
  19440. }
  19441. //修复残缺td
  19442. for (j = 0; j < rowsNum; j++) {
  19443. for (k = 0; k < colsNum; k++) {
  19444. if (this.indexTable[j][k] === undefined) {
  19445. row = rows[j];
  19446. cell = row.cells[row.cells.length - 1];
  19447. cell = cell
  19448. ? cell.cloneNode(true)
  19449. : this.table.ownerDocument.createElement("td");
  19450. this.setCellContent(cell);
  19451. if (cell.colSpan !== 1) cell.colSpan = 1;
  19452. if (cell.rowSpan !== 1) cell.rowSpan = 1;
  19453. row.appendChild(cell);
  19454. this.indexTable[j][k] = {
  19455. rowIndex: j,
  19456. cellIndex: cell.cellIndex,
  19457. colIndex: k,
  19458. rowSpan: 1,
  19459. colSpan: 1
  19460. };
  19461. }
  19462. }
  19463. }
  19464. //当框选后删除行或者列后撤销,需要重建选区。
  19465. var tds = domUtils.getElementsByTagName(this.table, "td"),
  19466. selectTds = [];
  19467. utils.each(tds, function(td) {
  19468. if (domUtils.hasClass(td, "selectTdClass")) {
  19469. selectTds.push(td);
  19470. }
  19471. });
  19472. if (selectTds.length) {
  19473. var start = selectTds[0],
  19474. end = selectTds[selectTds.length - 1],
  19475. startInfo = this.getCellInfo(start),
  19476. endInfo = this.getCellInfo(end);
  19477. this.selectedTds = selectTds;
  19478. this.cellsRange = {
  19479. beginRowIndex: startInfo.rowIndex,
  19480. beginColIndex: startInfo.colIndex,
  19481. endRowIndex: endInfo.rowIndex + endInfo.rowSpan - 1,
  19482. endColIndex: endInfo.colIndex + endInfo.colSpan - 1
  19483. };
  19484. }
  19485. //给第一行设置firstRow的样式名称,在排序图标的样式上使用到
  19486. if (!domUtils.hasClass(this.table.rows[0], "firstRow")) {
  19487. domUtils.addClass(this.table.rows[0], "firstRow");
  19488. for (var i = 1; i < this.table.rows.length; i++) {
  19489. domUtils.removeClasses(this.table.rows[i], "firstRow");
  19490. }
  19491. }
  19492. },
  19493. /**
  19494. * 获取单元格的索引信息
  19495. */
  19496. getCellInfo: function(cell) {
  19497. if (!cell) return;
  19498. var cellIndex = cell.cellIndex,
  19499. rowIndex = cell.parentNode.rowIndex,
  19500. rowInfo = this.indexTable[rowIndex],
  19501. numCols = this.colsNum;
  19502. for (var colIndex = cellIndex; colIndex < numCols; colIndex++) {
  19503. var cellInfo = rowInfo[colIndex];
  19504. if (
  19505. cellInfo.rowIndex === rowIndex &&
  19506. cellInfo.cellIndex === cellIndex
  19507. ) {
  19508. return cellInfo;
  19509. }
  19510. }
  19511. },
  19512. /**
  19513. * 根据行列号获取单元格
  19514. */
  19515. getCell: function(rowIndex, cellIndex) {
  19516. return (
  19517. (rowIndex < this.rowsNum &&
  19518. this.table.rows[rowIndex].cells[cellIndex]) ||
  19519. null
  19520. );
  19521. },
  19522. /**
  19523. * 删除单元格
  19524. */
  19525. deleteCell: function(cell, rowIndex) {
  19526. rowIndex = typeof rowIndex == "number"
  19527. ? rowIndex
  19528. : cell.parentNode.rowIndex;
  19529. var row = this.table.rows[rowIndex];
  19530. row.deleteCell(cell.cellIndex);
  19531. },
  19532. /**
  19533. * 根据始末两个单元格获取被框选的所有单元格范围
  19534. */
  19535. getCellsRange: function(cellA, cellB) {
  19536. function checkRange(
  19537. beginRowIndex,
  19538. beginColIndex,
  19539. endRowIndex,
  19540. endColIndex
  19541. ) {
  19542. var tmpBeginRowIndex = beginRowIndex,
  19543. tmpBeginColIndex = beginColIndex,
  19544. tmpEndRowIndex = endRowIndex,
  19545. tmpEndColIndex = endColIndex,
  19546. cellInfo,
  19547. colIndex,
  19548. rowIndex;
  19549. // 通过indexTable检查是否存在超出TableRange上边界的情况
  19550. if (beginRowIndex > 0) {
  19551. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19552. cellInfo = me.indexTable[beginRowIndex][colIndex];
  19553. rowIndex = cellInfo.rowIndex;
  19554. if (rowIndex < beginRowIndex) {
  19555. tmpBeginRowIndex = Math.min(rowIndex, tmpBeginRowIndex);
  19556. }
  19557. }
  19558. }
  19559. // 通过indexTable检查是否存在超出TableRange右边界的情况
  19560. if (endColIndex < me.colsNum) {
  19561. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19562. cellInfo = me.indexTable[rowIndex][endColIndex];
  19563. colIndex = cellInfo.colIndex + cellInfo.colSpan - 1;
  19564. if (colIndex > endColIndex) {
  19565. tmpEndColIndex = Math.max(colIndex, tmpEndColIndex);
  19566. }
  19567. }
  19568. }
  19569. // 检查是否有超出TableRange下边界的情况
  19570. if (endRowIndex < me.rowsNum) {
  19571. for (colIndex = beginColIndex; colIndex < endColIndex; colIndex++) {
  19572. cellInfo = me.indexTable[endRowIndex][colIndex];
  19573. rowIndex = cellInfo.rowIndex + cellInfo.rowSpan - 1;
  19574. if (rowIndex > endRowIndex) {
  19575. tmpEndRowIndex = Math.max(rowIndex, tmpEndRowIndex);
  19576. }
  19577. }
  19578. }
  19579. // 检查是否有超出TableRange左边界的情况
  19580. if (beginColIndex > 0) {
  19581. for (rowIndex = beginRowIndex; rowIndex < endRowIndex; rowIndex++) {
  19582. cellInfo = me.indexTable[rowIndex][beginColIndex];
  19583. colIndex = cellInfo.colIndex;
  19584. if (colIndex < beginColIndex) {
  19585. tmpBeginColIndex = Math.min(cellInfo.colIndex, tmpBeginColIndex);
  19586. }
  19587. }
  19588. }
  19589. //递归调用直至所有完成所有框选单元格的扩展
  19590. if (
  19591. tmpBeginRowIndex != beginRowIndex ||
  19592. tmpBeginColIndex != beginColIndex ||
  19593. tmpEndRowIndex != endRowIndex ||
  19594. tmpEndColIndex != endColIndex
  19595. ) {
  19596. return checkRange(
  19597. tmpBeginRowIndex,
  19598. tmpBeginColIndex,
  19599. tmpEndRowIndex,
  19600. tmpEndColIndex
  19601. );
  19602. } else {
  19603. // 不需要扩展TableRange的情况
  19604. return {
  19605. beginRowIndex: beginRowIndex,
  19606. beginColIndex: beginColIndex,
  19607. endRowIndex: endRowIndex,
  19608. endColIndex: endColIndex
  19609. };
  19610. }
  19611. }
  19612. try {
  19613. var me = this,
  19614. cellAInfo = me.getCellInfo(cellA);
  19615. if (cellA === cellB) {
  19616. return {
  19617. beginRowIndex: cellAInfo.rowIndex,
  19618. beginColIndex: cellAInfo.colIndex,
  19619. endRowIndex: cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19620. endColIndex: cellAInfo.colIndex + cellAInfo.colSpan - 1
  19621. };
  19622. }
  19623. var cellBInfo = me.getCellInfo(cellB);
  19624. // 计算TableRange的四个边
  19625. var beginRowIndex = Math.min(cellAInfo.rowIndex, cellBInfo.rowIndex),
  19626. beginColIndex = Math.min(cellAInfo.colIndex, cellBInfo.colIndex),
  19627. endRowIndex = Math.max(
  19628. cellAInfo.rowIndex + cellAInfo.rowSpan - 1,
  19629. cellBInfo.rowIndex + cellBInfo.rowSpan - 1
  19630. ),
  19631. endColIndex = Math.max(
  19632. cellAInfo.colIndex + cellAInfo.colSpan - 1,
  19633. cellBInfo.colIndex + cellBInfo.colSpan - 1
  19634. );
  19635. return checkRange(
  19636. beginRowIndex,
  19637. beginColIndex,
  19638. endRowIndex,
  19639. endColIndex
  19640. );
  19641. } catch (e) {
  19642. //throw e;
  19643. }
  19644. },
  19645. /**
  19646. * 依据cellsRange获取对应的单元格集合
  19647. */
  19648. getCells: function(range) {
  19649. //每次获取cells之前必须先清除上次的选择,否则会对后续获取操作造成影响
  19650. this.clearSelected();
  19651. var beginRowIndex = range.beginRowIndex,
  19652. beginColIndex = range.beginColIndex,
  19653. endRowIndex = range.endRowIndex,
  19654. endColIndex = range.endColIndex,
  19655. cellInfo,
  19656. rowIndex,
  19657. colIndex,
  19658. tdHash = {},
  19659. returnTds = [];
  19660. for (var i = beginRowIndex; i <= endRowIndex; i++) {
  19661. for (var j = beginColIndex; j <= endColIndex; j++) {
  19662. cellInfo = this.indexTable[i][j];
  19663. rowIndex = cellInfo.rowIndex;
  19664. colIndex = cellInfo.colIndex;
  19665. // 如果Cells里已经包含了此Cell则跳过
  19666. var key = rowIndex + "|" + colIndex;
  19667. if (tdHash[key]) continue;
  19668. tdHash[key] = 1;
  19669. if (
  19670. rowIndex < i ||
  19671. colIndex < j ||
  19672. rowIndex + cellInfo.rowSpan - 1 > endRowIndex ||
  19673. colIndex + cellInfo.colSpan - 1 > endColIndex
  19674. ) {
  19675. return null;
  19676. }
  19677. returnTds.push(this.getCell(rowIndex, cellInfo.cellIndex));
  19678. }
  19679. }
  19680. return returnTds;
  19681. },
  19682. /**
  19683. * 清理已经选中的单元格
  19684. */
  19685. clearSelected: function() {
  19686. UETable.removeSelectedClass(this.selectedTds);
  19687. this.selectedTds = [];
  19688. this.cellsRange = {};
  19689. },
  19690. /**
  19691. * 根据range设置已经选中的单元格
  19692. */
  19693. setSelected: function(range) {
  19694. var cells = this.getCells(range);
  19695. UETable.addSelectedClass(cells);
  19696. this.selectedTds = cells;
  19697. this.cellsRange = range;
  19698. },
  19699. isFullRow: function() {
  19700. var range = this.cellsRange;
  19701. return range.endColIndex - range.beginColIndex + 1 == this.colsNum;
  19702. },
  19703. isFullCol: function() {
  19704. var range = this.cellsRange,
  19705. table = this.table,
  19706. ths = table.getElementsByTagName("th"),
  19707. rows = range.endRowIndex - range.beginRowIndex + 1;
  19708. return !ths.length
  19709. ? rows == this.rowsNum
  19710. : rows == this.rowsNum || rows == this.rowsNum - 1;
  19711. },
  19712. /**
  19713. * 获取视觉上的前置单元格,默认是左边,top传入时
  19714. * @param cell
  19715. * @param top
  19716. */
  19717. getNextCell: function(cell, bottom, ignoreRange) {
  19718. try {
  19719. var cellInfo = this.getCellInfo(cell),
  19720. nextRowIndex,
  19721. nextColIndex;
  19722. var len = this.selectedTds.length && !ignoreRange,
  19723. range = this.cellsRange;
  19724. //末行或者末列没有后置单元格
  19725. if (
  19726. (!bottom && cellInfo.rowIndex == 0) ||
  19727. (bottom &&
  19728. (!len
  19729. ? cellInfo.rowIndex + cellInfo.rowSpan > this.rowsNum - 1
  19730. : range.endRowIndex == this.rowsNum - 1))
  19731. )
  19732. return null;
  19733. nextRowIndex = !bottom
  19734. ? !len ? cellInfo.rowIndex - 1 : range.beginRowIndex - 1
  19735. : !len ? cellInfo.rowIndex + cellInfo.rowSpan : range.endRowIndex + 1;
  19736. nextColIndex = !len ? cellInfo.colIndex : range.beginColIndex;
  19737. return this.getCell(
  19738. this.indexTable[nextRowIndex][nextColIndex].rowIndex,
  19739. this.indexTable[nextRowIndex][nextColIndex].cellIndex
  19740. );
  19741. } catch (e) {
  19742. showError(e);
  19743. }
  19744. },
  19745. getPreviewCell: function(cell, top) {
  19746. try {
  19747. var cellInfo = this.getCellInfo(cell),
  19748. previewRowIndex,
  19749. previewColIndex;
  19750. var len = this.selectedTds.length,
  19751. range = this.cellsRange;
  19752. //首行或者首列没有前置单元格
  19753. if (
  19754. (!top && (!len ? !cellInfo.colIndex : !range.beginColIndex)) ||
  19755. (top &&
  19756. (!len
  19757. ? cellInfo.rowIndex > this.colsNum - 1
  19758. : range.endColIndex == this.colsNum - 1))
  19759. )
  19760. return null;
  19761. previewRowIndex = !top
  19762. ? !len ? cellInfo.rowIndex : range.beginRowIndex
  19763. : !len
  19764. ? cellInfo.rowIndex < 1 ? 0 : cellInfo.rowIndex - 1
  19765. : range.beginRowIndex;
  19766. previewColIndex = !top
  19767. ? !len
  19768. ? cellInfo.colIndex < 1 ? 0 : cellInfo.colIndex - 1
  19769. : range.beginColIndex - 1
  19770. : !len ? cellInfo.colIndex : range.endColIndex + 1;
  19771. return this.getCell(
  19772. this.indexTable[previewRowIndex][previewColIndex].rowIndex,
  19773. this.indexTable[previewRowIndex][previewColIndex].cellIndex
  19774. );
  19775. } catch (e) {
  19776. showError(e);
  19777. }
  19778. },
  19779. /**
  19780. * 移动单元格中的内容
  19781. */
  19782. moveContent: function(cellTo, cellFrom) {
  19783. if (UETable.isEmptyBlock(cellFrom)) return;
  19784. if (UETable.isEmptyBlock(cellTo)) {
  19785. cellTo.innerHTML = cellFrom.innerHTML;
  19786. return;
  19787. }
  19788. var child = cellTo.lastChild;
  19789. if (child.nodeType == 3 || !dtd.$block[child.tagName]) {
  19790. cellTo.appendChild(cellTo.ownerDocument.createElement("br"));
  19791. }
  19792. while ((child = cellFrom.firstChild)) {
  19793. cellTo.appendChild(child);
  19794. }
  19795. },
  19796. /**
  19797. * 向右合并单元格
  19798. */
  19799. mergeRight: function(cell) {
  19800. var cellInfo = this.getCellInfo(cell),
  19801. rightColIndex = cellInfo.colIndex + cellInfo.colSpan,
  19802. rightCellInfo = this.indexTable[cellInfo.rowIndex][rightColIndex],
  19803. rightCell = this.getCell(
  19804. rightCellInfo.rowIndex,
  19805. rightCellInfo.cellIndex
  19806. );
  19807. //合并
  19808. cell.colSpan = cellInfo.colSpan + rightCellInfo.colSpan;
  19809. //被合并的单元格不应存在宽度属性
  19810. cell.removeAttribute("width");
  19811. //移动内容
  19812. this.moveContent(cell, rightCell);
  19813. //删掉被合并的Cell
  19814. this.deleteCell(rightCell, rightCellInfo.rowIndex);
  19815. this.update();
  19816. },
  19817. /**
  19818. * 向下合并单元格
  19819. */
  19820. mergeDown: function(cell) {
  19821. var cellInfo = this.getCellInfo(cell),
  19822. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan,
  19823. downCellInfo = this.indexTable[downRowIndex][cellInfo.colIndex],
  19824. downCell = this.getCell(downCellInfo.rowIndex, downCellInfo.cellIndex);
  19825. cell.rowSpan = cellInfo.rowSpan + downCellInfo.rowSpan;
  19826. cell.removeAttribute("height");
  19827. this.moveContent(cell, downCell);
  19828. this.deleteCell(downCell, downCellInfo.rowIndex);
  19829. this.update();
  19830. },
  19831. /**
  19832. * 合并整个range中的内容
  19833. */
  19834. mergeRange: function() {
  19835. //由于合并操作可以在任意时刻进行,所以无法通过鼠标位置等信息实时生成range,只能通过缓存实例中的cellsRange对象来访问
  19836. var range = this.cellsRange,
  19837. leftTopCell = this.getCell(
  19838. range.beginRowIndex,
  19839. this.indexTable[range.beginRowIndex][range.beginColIndex].cellIndex
  19840. );
  19841. // 这段关于行表头或者列表头的特殊处理会导致表头合并范围错误
  19842. // 为什么有这段代码的原因未明,暂且注释掉,希望原作者看到后出面说明下
  19843. // if (
  19844. // leftTopCell.tagName == "TH" &&
  19845. // range.endRowIndex !== range.beginRowIndex
  19846. // ) {
  19847. // var index = this.indexTable,
  19848. // info = this.getCellInfo(leftTopCell);
  19849. // leftTopCell = this.getCell(1, index[1][info.colIndex].cellIndex);
  19850. // range = this.getCellsRange(
  19851. // leftTopCell,
  19852. // this.getCell(
  19853. // index[this.rowsNum - 1][info.colIndex].rowIndex,
  19854. // index[this.rowsNum - 1][info.colIndex].cellIndex
  19855. // )
  19856. // );
  19857. // }
  19858. // 删除剩余的Cells
  19859. var cells = this.getCells(range);
  19860. for (var i = 0, ci; (ci = cells[i++]); ) {
  19861. if (ci !== leftTopCell) {
  19862. this.moveContent(leftTopCell, ci);
  19863. this.deleteCell(ci);
  19864. }
  19865. }
  19866. // 修改左上角Cell的rowSpan和colSpan,并调整宽度属性设置
  19867. leftTopCell.rowSpan = range.endRowIndex - range.beginRowIndex + 1;
  19868. leftTopCell.rowSpan > 1 && leftTopCell.removeAttribute("height");
  19869. leftTopCell.colSpan = range.endColIndex - range.beginColIndex + 1;
  19870. leftTopCell.colSpan > 1 && leftTopCell.removeAttribute("width");
  19871. if (leftTopCell.rowSpan == this.rowsNum && leftTopCell.colSpan != 1) {
  19872. leftTopCell.colSpan = 1;
  19873. }
  19874. if (leftTopCell.colSpan == this.colsNum && leftTopCell.rowSpan != 1) {
  19875. var rowIndex = leftTopCell.parentNode.rowIndex;
  19876. //解决IE下的表格操作问题
  19877. if (this.table.deleteRow) {
  19878. for (
  19879. var i = rowIndex + 1,
  19880. curIndex = rowIndex + 1,
  19881. len = leftTopCell.rowSpan;
  19882. i < len;
  19883. i++
  19884. ) {
  19885. this.table.deleteRow(curIndex);
  19886. }
  19887. } else {
  19888. for (var i = 0, len = leftTopCell.rowSpan - 1; i < len; i++) {
  19889. var row = this.table.rows[rowIndex + 1];
  19890. row.parentNode.removeChild(row);
  19891. }
  19892. }
  19893. leftTopCell.rowSpan = 1;
  19894. }
  19895. this.update();
  19896. },
  19897. /**
  19898. * 插入一行单元格
  19899. */
  19900. insertRow: function(rowIndex, sourceCell) {
  19901. var numCols = this.colsNum,
  19902. table = this.table,
  19903. row = table.insertRow(rowIndex),
  19904. cell,
  19905. thead = null,
  19906. isInsertTitle =
  19907. typeof sourceCell == "string" && sourceCell.toUpperCase() == "TH";
  19908. function replaceTdToTh(colIndex, cell, tableRow) {
  19909. if (colIndex == 0) {
  19910. var tr = tableRow.nextSibling || tableRow.previousSibling,
  19911. th = tr.cells[colIndex];
  19912. if (th.tagName == "TH") {
  19913. th = cell.ownerDocument.createElement("th");
  19914. th.appendChild(cell.firstChild);
  19915. tableRow.insertBefore(th, cell);
  19916. domUtils.remove(cell);
  19917. }
  19918. } else {
  19919. if (cell.tagName == "TH") {
  19920. var td = cell.ownerDocument.createElement("td");
  19921. td.appendChild(cell.firstChild);
  19922. tableRow.insertBefore(td, cell);
  19923. domUtils.remove(cell);
  19924. }
  19925. }
  19926. }
  19927. //首行直接插入,无需考虑部分单元格被rowspan的情况
  19928. if (rowIndex == 0 || rowIndex == this.rowsNum) {
  19929. for (var colIndex = 0; colIndex < numCols; colIndex++) {
  19930. cell = this.cloneCell(sourceCell, true);
  19931. this.setCellContent(cell);
  19932. cell.getAttribute("vAlign") &&
  19933. cell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  19934. row.appendChild(cell);
  19935. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  19936. }
  19937. if (isInsertTitle) {
  19938. thead = table.createTHead();
  19939. thead.insertBefore(row, thead.firstChild);
  19940. }
  19941. } else {
  19942. var infoRow = this.indexTable[rowIndex],
  19943. cellIndex = 0;
  19944. for (colIndex = 0; colIndex < numCols; colIndex++) {
  19945. var cellInfo = infoRow[colIndex];
  19946. //如果存在某个单元格的rowspan穿过待插入行的位置,则修改该单元格的rowspan即可,无需插入单元格
  19947. if (cellInfo.rowIndex < rowIndex) {
  19948. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19949. cell.rowSpan = cellInfo.rowSpan + 1;
  19950. } else {
  19951. cell = this.cloneCell(sourceCell, true);
  19952. this.setCellContent(cell);
  19953. row.appendChild(cell);
  19954. }
  19955. if (!isInsertTitle) replaceTdToTh(colIndex, cell, row);
  19956. }
  19957. }
  19958. //框选时插入不触发contentchange,需要手动更新索引。
  19959. this.update();
  19960. return row;
  19961. },
  19962. /**
  19963. * 删除一行单元格
  19964. * @param rowIndex
  19965. */
  19966. deleteRow: function(rowIndex) {
  19967. var row = this.table.rows[rowIndex],
  19968. infoRow = this.indexTable[rowIndex],
  19969. colsNum = this.colsNum,
  19970. count = 0; //处理计数
  19971. for (var colIndex = 0; colIndex < colsNum; ) {
  19972. var cellInfo = infoRow[colIndex],
  19973. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  19974. if (cell.rowSpan > 1) {
  19975. if (cellInfo.rowIndex == rowIndex) {
  19976. var clone = cell.cloneNode(true);
  19977. clone.rowSpan = cell.rowSpan - 1;
  19978. clone.innerHTML = "";
  19979. cell.rowSpan = 1;
  19980. var nextRowIndex = rowIndex + 1,
  19981. nextRow = this.table.rows[nextRowIndex],
  19982. insertCellIndex,
  19983. preMerged =
  19984. this.getPreviewMergedCellsNum(nextRowIndex, colIndex) - count;
  19985. if (preMerged < colIndex) {
  19986. insertCellIndex = colIndex - preMerged - 1;
  19987. //nextRow.insertCell(insertCellIndex);
  19988. domUtils.insertAfter(nextRow.cells[insertCellIndex], clone);
  19989. } else {
  19990. if (nextRow.cells.length)
  19991. nextRow.insertBefore(clone, nextRow.cells[0]);
  19992. }
  19993. count += 1;
  19994. //cell.parentNode.removeChild(cell);
  19995. }
  19996. }
  19997. colIndex += cell.colSpan || 1;
  19998. }
  19999. var deleteTds = [],
  20000. cacheMap = {};
  20001. for (colIndex = 0; colIndex < colsNum; colIndex++) {
  20002. var tmpRowIndex = infoRow[colIndex].rowIndex,
  20003. tmpCellIndex = infoRow[colIndex].cellIndex,
  20004. key = tmpRowIndex + "_" + tmpCellIndex;
  20005. if (cacheMap[key]) continue;
  20006. cacheMap[key] = 1;
  20007. cell = this.getCell(tmpRowIndex, tmpCellIndex);
  20008. deleteTds.push(cell);
  20009. }
  20010. var mergeTds = [];
  20011. utils.each(deleteTds, function(td) {
  20012. if (td.rowSpan == 1) {
  20013. td.parentNode.removeChild(td);
  20014. } else {
  20015. mergeTds.push(td);
  20016. }
  20017. });
  20018. utils.each(mergeTds, function(td) {
  20019. td.rowSpan--;
  20020. });
  20021. row.parentNode.removeChild(row);
  20022. //浏览器方法本身存在bug,采用自定义方法删除
  20023. //this.table.deleteRow(rowIndex);
  20024. this.update();
  20025. },
  20026. insertCol: function(colIndex, sourceCell, defaultValue) {
  20027. var rowsNum = this.rowsNum,
  20028. rowIndex = 0,
  20029. tableRow,
  20030. cell,
  20031. backWidth = parseInt(
  20032. (this.table.offsetWidth -
  20033. (this.colsNum + 1) * 20 -
  20034. (this.colsNum + 1)) /
  20035. (this.colsNum + 1),
  20036. 10
  20037. ),
  20038. isInsertTitleCol =
  20039. typeof sourceCell == "string" && sourceCell.toUpperCase() == "TH";
  20040. function replaceTdToTh(rowIndex, cell, tableRow) {
  20041. if (rowIndex == 0) {
  20042. var th = cell.nextSibling || cell.previousSibling;
  20043. if (th.tagName == "TH") {
  20044. th = cell.ownerDocument.createElement("th");
  20045. th.appendChild(cell.firstChild);
  20046. tableRow.insertBefore(th, cell);
  20047. domUtils.remove(cell);
  20048. }
  20049. } else {
  20050. if (cell.tagName == "TH") {
  20051. var td = cell.ownerDocument.createElement("td");
  20052. td.appendChild(cell.firstChild);
  20053. tableRow.insertBefore(td, cell);
  20054. domUtils.remove(cell);
  20055. }
  20056. }
  20057. }
  20058. var preCell;
  20059. if (colIndex == 0 || colIndex == this.colsNum) {
  20060. for (; rowIndex < rowsNum; rowIndex++) {
  20061. tableRow = this.table.rows[rowIndex];
  20062. preCell =
  20063. tableRow.cells[colIndex == 0 ? colIndex : tableRow.cells.length];
  20064. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(colIndex == 0 ? colIndex : tableRow.cells.length);
  20065. this.setCellContent(cell);
  20066. cell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  20067. preCell && cell.setAttribute("width", preCell.getAttribute("width"));
  20068. if (!colIndex) {
  20069. tableRow.insertBefore(cell, tableRow.cells[0]);
  20070. } else {
  20071. domUtils.insertAfter(
  20072. tableRow.cells[tableRow.cells.length - 1],
  20073. cell
  20074. );
  20075. }
  20076. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow);
  20077. }
  20078. } else {
  20079. for (; rowIndex < rowsNum; rowIndex++) {
  20080. var cellInfo = this.indexTable[rowIndex][colIndex];
  20081. if (cellInfo.colIndex < colIndex) {
  20082. cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  20083. cell.colSpan = cellInfo.colSpan + 1;
  20084. } else {
  20085. tableRow = this.table.rows[rowIndex];
  20086. preCell = tableRow.cells[cellInfo.cellIndex];
  20087. cell = this.cloneCell(sourceCell, true); //tableRow.insertCell(cellInfo.cellIndex);
  20088. this.setCellContent(cell);
  20089. cell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  20090. preCell &&
  20091. cell.setAttribute("width", preCell.getAttribute("width"));
  20092. //防止IE下报错
  20093. preCell
  20094. ? tableRow.insertBefore(cell, preCell)
  20095. : tableRow.appendChild(cell);
  20096. }
  20097. if (!isInsertTitleCol) replaceTdToTh(rowIndex, cell, tableRow);
  20098. }
  20099. }
  20100. //框选时插入不触发contentchange,需要手动更新索引
  20101. this.update();
  20102. this.updateWidth(
  20103. backWidth,
  20104. defaultValue || { tdPadding: 10, tdBorder: 1 }
  20105. );
  20106. },
  20107. updateWidth: function(width, defaultValue) {
  20108. var table = this.table,
  20109. tmpWidth =
  20110. UETable.getWidth(table) -
  20111. defaultValue.tdPadding * 2 -
  20112. defaultValue.tdBorder +
  20113. width;
  20114. if (tmpWidth < table.ownerDocument.body.offsetWidth) {
  20115. table.setAttribute("width", tmpWidth);
  20116. return;
  20117. }
  20118. var tds = domUtils.getElementsByTagName(this.table, "td th");
  20119. utils.each(tds, function(td) {
  20120. td.setAttribute("width", width);
  20121. });
  20122. },
  20123. deleteCol: function(colIndex) {
  20124. var indexTable = this.indexTable,
  20125. tableRows = this.table.rows,
  20126. backTableWidth = this.table.getAttribute("width"),
  20127. backTdWidth = 0,
  20128. rowsNum = this.rowsNum,
  20129. cacheMap = {};
  20130. for (var rowIndex = 0; rowIndex < rowsNum; ) {
  20131. var infoRow = indexTable[rowIndex],
  20132. cellInfo = infoRow[colIndex],
  20133. key = cellInfo.rowIndex + "_" + cellInfo.colIndex;
  20134. // 跳过已经处理过的Cell
  20135. if (cacheMap[key]) continue;
  20136. cacheMap[key] = 1;
  20137. var cell = this.getCell(cellInfo.rowIndex, cellInfo.cellIndex);
  20138. if (!backTdWidth)
  20139. backTdWidth =
  20140. cell && parseInt(cell.offsetWidth / cell.colSpan, 10).toFixed(0);
  20141. // 如果Cell的colSpan大于1, 就修改colSpan, 否则就删掉这个Cell
  20142. if (cell.colSpan > 1) {
  20143. cell.colSpan--;
  20144. } else {
  20145. tableRows[rowIndex].deleteCell(cellInfo.cellIndex);
  20146. }
  20147. rowIndex += cellInfo.rowSpan || 1;
  20148. }
  20149. this.table.setAttribute("width", backTableWidth - backTdWidth);
  20150. this.update();
  20151. },
  20152. splitToCells: function(cell) {
  20153. var me = this,
  20154. cells = this.splitToRows(cell);
  20155. utils.each(cells, function(cell) {
  20156. me.splitToCols(cell);
  20157. });
  20158. },
  20159. splitToRows: function(cell) {
  20160. var cellInfo = this.getCellInfo(cell),
  20161. rowIndex = cellInfo.rowIndex,
  20162. colIndex = cellInfo.colIndex,
  20163. results = [];
  20164. // 修改Cell的rowSpan
  20165. cell.rowSpan = 1;
  20166. results.push(cell);
  20167. // 补齐单元格
  20168. for (
  20169. var i = rowIndex, endRow = rowIndex + cellInfo.rowSpan;
  20170. i < endRow;
  20171. i++
  20172. ) {
  20173. if (i == rowIndex) continue;
  20174. var tableRow = this.table.rows[i],
  20175. tmpCell = tableRow.insertCell(
  20176. colIndex - this.getPreviewMergedCellsNum(i, colIndex)
  20177. );
  20178. tmpCell.colSpan = cellInfo.colSpan;
  20179. this.setCellContent(tmpCell);
  20180. tmpCell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  20181. tmpCell.setAttribute("align", cell.getAttribute("align"));
  20182. if (cell.style.cssText) {
  20183. tmpCell.style.cssText = cell.style.cssText;
  20184. }
  20185. results.push(tmpCell);
  20186. }
  20187. this.update();
  20188. return results;
  20189. },
  20190. getPreviewMergedCellsNum: function(rowIndex, colIndex) {
  20191. var indexRow = this.indexTable[rowIndex],
  20192. num = 0;
  20193. for (var i = 0; i < colIndex; ) {
  20194. var colSpan = indexRow[i].colSpan,
  20195. tmpRowIndex = indexRow[i].rowIndex;
  20196. num += colSpan - (tmpRowIndex == rowIndex ? 1 : 0);
  20197. i += colSpan;
  20198. }
  20199. return num;
  20200. },
  20201. splitToCols: function(cell) {
  20202. var backWidth = (cell.offsetWidth / cell.colSpan - 22).toFixed(0),
  20203. cellInfo = this.getCellInfo(cell),
  20204. rowIndex = cellInfo.rowIndex,
  20205. colIndex = cellInfo.colIndex,
  20206. results = [];
  20207. // 修改Cell的rowSpan
  20208. cell.colSpan = 1;
  20209. cell.setAttribute("width", backWidth);
  20210. results.push(cell);
  20211. // 补齐单元格
  20212. for (
  20213. var j = colIndex, endCol = colIndex + cellInfo.colSpan;
  20214. j < endCol;
  20215. j++
  20216. ) {
  20217. if (j == colIndex) continue;
  20218. var tableRow = this.table.rows[rowIndex],
  20219. tmpCell = tableRow.insertCell(
  20220. this.indexTable[rowIndex][j].cellIndex + 1
  20221. );
  20222. tmpCell.rowSpan = cellInfo.rowSpan;
  20223. this.setCellContent(tmpCell);
  20224. tmpCell.setAttribute("vAlign", cell.getAttribute("vAlign"));
  20225. tmpCell.setAttribute("align", cell.getAttribute("align"));
  20226. tmpCell.setAttribute("width", backWidth);
  20227. if (cell.style.cssText) {
  20228. tmpCell.style.cssText = cell.style.cssText;
  20229. }
  20230. //处理th的情况
  20231. if (cell.tagName == "TH") {
  20232. var th = cell.ownerDocument.createElement("th");
  20233. th.appendChild(tmpCell.firstChild);
  20234. th.setAttribute("vAlign", cell.getAttribute("vAlign"));
  20235. th.rowSpan = tmpCell.rowSpan;
  20236. tableRow.insertBefore(th, tmpCell);
  20237. domUtils.remove(tmpCell);
  20238. }
  20239. results.push(tmpCell);
  20240. }
  20241. this.update();
  20242. return results;
  20243. },
  20244. isLastCell: function(cell, rowsNum, colsNum) {
  20245. rowsNum = rowsNum || this.rowsNum;
  20246. colsNum = colsNum || this.colsNum;
  20247. var cellInfo = this.getCellInfo(cell);
  20248. return (
  20249. cellInfo.rowIndex + cellInfo.rowSpan == rowsNum &&
  20250. cellInfo.colIndex + cellInfo.colSpan == colsNum
  20251. );
  20252. },
  20253. getLastCell: function(cells) {
  20254. cells = cells || this.table.getElementsByTagName("td");
  20255. var firstInfo = this.getCellInfo(cells[0]);
  20256. var me = this,
  20257. last = cells[0],
  20258. tr = last.parentNode,
  20259. cellsNum = 0,
  20260. cols = 0,
  20261. rows;
  20262. utils.each(cells, function(cell) {
  20263. if (cell.parentNode == tr) cols += cell.colSpan || 1;
  20264. cellsNum += cell.rowSpan * cell.colSpan || 1;
  20265. });
  20266. rows = cellsNum / cols;
  20267. utils.each(cells, function(cell) {
  20268. if (me.isLastCell(cell, rows, cols)) {
  20269. last = cell;
  20270. return false;
  20271. }
  20272. });
  20273. return last;
  20274. },
  20275. selectRow: function(rowIndex) {
  20276. var indexRow = this.indexTable[rowIndex],
  20277. start = this.getCell(indexRow[0].rowIndex, indexRow[0].cellIndex),
  20278. end = this.getCell(
  20279. indexRow[this.colsNum - 1].rowIndex,
  20280. indexRow[this.colsNum - 1].cellIndex
  20281. ),
  20282. range = this.getCellsRange(start, end);
  20283. this.setSelected(range);
  20284. },
  20285. selectTable: function() {
  20286. var tds = this.table.getElementsByTagName("td"),
  20287. range = this.getCellsRange(tds[0], tds[tds.length - 1]);
  20288. this.setSelected(range);
  20289. },
  20290. setBackground: function(cells, value) {
  20291. if (typeof value === "string") {
  20292. utils.each(cells, function(cell) {
  20293. cell.style.backgroundColor = value;
  20294. });
  20295. } else if (typeof value === "object") {
  20296. value = utils.extend(
  20297. {
  20298. repeat: true,
  20299. colorList: ["#ddd", "#fff"]
  20300. },
  20301. value
  20302. );
  20303. var rowIndex = this.getCellInfo(cells[0]).rowIndex,
  20304. count = 0,
  20305. colors = value.colorList,
  20306. getColor = function(list, index, repeat) {
  20307. return list[index]
  20308. ? list[index]
  20309. : repeat ? list[index % list.length] : "";
  20310. };
  20311. for (var i = 0, cell; (cell = cells[i++]); ) {
  20312. var cellInfo = this.getCellInfo(cell);
  20313. cell.style.backgroundColor = getColor(
  20314. colors,
  20315. rowIndex + count == cellInfo.rowIndex ? count : ++count,
  20316. value.repeat
  20317. );
  20318. }
  20319. }
  20320. },
  20321. removeBackground: function(cells) {
  20322. utils.each(cells, function(cell) {
  20323. cell.style.backgroundColor = "";
  20324. });
  20325. }
  20326. };
  20327. function showError(e) {}
  20328. })();
  20329. // plugins/table.cmds.js
  20330. /**
  20331. * Created with JetBrains PhpStorm.
  20332. * User: taoqili
  20333. * Date: 13-2-20
  20334. * Time: 下午6:25
  20335. * To change this template use File | Settings | File Templates.
  20336. */
  20337. (function() {
  20338. var UT = UE.UETable,
  20339. getTableItemsByRange = function(editor) {
  20340. return UT.getTableItemsByRange(editor);
  20341. },
  20342. getUETableBySelected = function(editor) {
  20343. return UT.getUETableBySelected(editor);
  20344. },
  20345. getDefaultValue = function(editor, table) {
  20346. return UT.getDefaultValue(editor, table);
  20347. },
  20348. getUETable = function(tdOrTable) {
  20349. return UT.getUETable(tdOrTable);
  20350. };
  20351. UE.commands["inserttable"] = {
  20352. queryCommandState: function() {
  20353. return getTableItemsByRange(this).table ? -1 : 0;
  20354. },
  20355. execCommand: function(cmd, opt) {
  20356. function createTable(opt, tdWidth) {
  20357. var html = [],
  20358. rowsNum = opt.numRows,
  20359. colsNum = opt.numCols;
  20360. for (var r = 0; r < rowsNum; r++) {
  20361. html.push("<tr" + (r == 0 ? ' class="firstRow"' : "") + ">");
  20362. for (var c = 0; c < colsNum; c++) {
  20363. html.push(
  20364. '<td width="' +
  20365. tdWidth +
  20366. '" vAlign="' +
  20367. opt.tdvalign +
  20368. '" >' +
  20369. (browser.ie && browser.version < 11
  20370. ? domUtils.fillChar
  20371. : "<br/>") +
  20372. "</td>"
  20373. );
  20374. }
  20375. html.push("</tr>");
  20376. }
  20377. //禁止指定table-width
  20378. return "<table><tbody>" + html.join("") + "</tbody></table>";
  20379. }
  20380. if (!opt) {
  20381. opt = utils.extend(
  20382. {},
  20383. {
  20384. numCols: this.options.defaultCols,
  20385. numRows: this.options.defaultRows,
  20386. tdvalign: this.options.tdvalign
  20387. }
  20388. );
  20389. }
  20390. var me = this;
  20391. var range = this.selection.getRange(),
  20392. start = range.startContainer,
  20393. firstParentBlock =
  20394. domUtils.findParent(
  20395. start,
  20396. function(node) {
  20397. return domUtils.isBlockElm(node);
  20398. },
  20399. true
  20400. ) || me.body;
  20401. var defaultValue = getDefaultValue(me),
  20402. tableWidth = firstParentBlock.offsetWidth,
  20403. tdWidth = Math.floor(
  20404. tableWidth / opt.numCols -
  20405. defaultValue.tdPadding * 2 -
  20406. defaultValue.tdBorder
  20407. );
  20408. //todo其他属性
  20409. !opt.tdvalign && (opt.tdvalign = me.options.tdvalign);
  20410. me.execCommand("inserthtml", createTable(opt, tdWidth));
  20411. }
  20412. };
  20413. UE.commands["insertparagraphbeforetable"] = {
  20414. queryCommandState: function() {
  20415. return getTableItemsByRange(this).cell ? 0 : -1;
  20416. },
  20417. execCommand: function() {
  20418. var table = getTableItemsByRange(this).table;
  20419. if (table) {
  20420. var p = this.document.createElement("p");
  20421. p.innerHTML = browser.ie ? "&nbsp;" : "<br />";
  20422. table.parentNode.insertBefore(p, table);
  20423. this.selection.getRange().setStart(p, 0).setCursor();
  20424. }
  20425. }
  20426. };
  20427. UE.commands["deletetable"] = {
  20428. queryCommandState: function() {
  20429. var rng = this.selection.getRange();
  20430. return domUtils.findParentByTagName(rng.startContainer, "table", true)
  20431. ? 0
  20432. : -1;
  20433. },
  20434. execCommand: function(cmd, table) {
  20435. var rng = this.selection.getRange();
  20436. table =
  20437. table ||
  20438. domUtils.findParentByTagName(rng.startContainer, "table", true);
  20439. if (table) {
  20440. var next = table.nextSibling;
  20441. if (!next) {
  20442. next = domUtils.createElement(this.document, "p", {
  20443. innerHTML: browser.ie ? domUtils.fillChar : "<br/>"
  20444. });
  20445. table.parentNode.insertBefore(next, table);
  20446. }
  20447. domUtils.remove(table);
  20448. rng = this.selection.getRange();
  20449. if (next.nodeType == 3) {
  20450. rng.setStartBefore(next);
  20451. } else {
  20452. rng.setStart(next, 0);
  20453. }
  20454. rng.setCursor(false, true);
  20455. this.fireEvent("tablehasdeleted");
  20456. }
  20457. }
  20458. };
  20459. UE.commands["cellalign"] = {
  20460. queryCommandState: function() {
  20461. return getSelectedArr(this).length ? 0 : -1;
  20462. },
  20463. execCommand: function(cmd, align) {
  20464. var selectedTds = getSelectedArr(this);
  20465. if (selectedTds.length) {
  20466. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20467. ci.setAttribute("align", align);
  20468. }
  20469. }
  20470. }
  20471. };
  20472. UE.commands["cellvalign"] = {
  20473. queryCommandState: function() {
  20474. return getSelectedArr(this).length ? 0 : -1;
  20475. },
  20476. execCommand: function(cmd, valign) {
  20477. var selectedTds = getSelectedArr(this);
  20478. if (selectedTds.length) {
  20479. for (var i = 0, ci; (ci = selectedTds[i++]); ) {
  20480. ci.setAttribute("vAlign", valign);
  20481. }
  20482. }
  20483. }
  20484. };
  20485. UE.commands["insertcaption"] = {
  20486. queryCommandState: function() {
  20487. var table = getTableItemsByRange(this).table;
  20488. if (table) {
  20489. return table.getElementsByTagName("caption").length == 0 ? 1 : -1;
  20490. }
  20491. return -1;
  20492. },
  20493. execCommand: function() {
  20494. var table = getTableItemsByRange(this).table;
  20495. if (table) {
  20496. var caption = this.document.createElement("caption");
  20497. caption.innerHTML = browser.ie ? domUtils.fillChar : "<br/>";
  20498. table.insertBefore(caption, table.firstChild);
  20499. var range = this.selection.getRange();
  20500. range.setStart(caption, 0).setCursor();
  20501. }
  20502. }
  20503. };
  20504. UE.commands["deletecaption"] = {
  20505. queryCommandState: function() {
  20506. var rng = this.selection.getRange(),
  20507. table = domUtils.findParentByTagName(rng.startContainer, "table");
  20508. if (table) {
  20509. return table.getElementsByTagName("caption").length == 0 ? -1 : 1;
  20510. }
  20511. return -1;
  20512. },
  20513. execCommand: function() {
  20514. var rng = this.selection.getRange(),
  20515. table = domUtils.findParentByTagName(rng.startContainer, "table");
  20516. if (table) {
  20517. domUtils.remove(table.getElementsByTagName("caption")[0]);
  20518. var range = this.selection.getRange();
  20519. range.setStart(table.rows[0].cells[0], 0).setCursor();
  20520. }
  20521. }
  20522. };
  20523. UE.commands["inserttitle"] = {
  20524. queryCommandState: function() {
  20525. var table = getTableItemsByRange(this).table;
  20526. if (table) {
  20527. var firstRow = table.rows[0];
  20528. return firstRow.cells[
  20529. firstRow.cells.length - 1
  20530. ].tagName.toLowerCase() != "th"
  20531. ? 0
  20532. : -1;
  20533. }
  20534. return -1;
  20535. },
  20536. execCommand: function() {
  20537. var table = getTableItemsByRange(this).table;
  20538. if (table) {
  20539. getUETable(table).insertRow(0, "th");
  20540. }
  20541. var th = table.getElementsByTagName("th")[0];
  20542. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  20543. }
  20544. };
  20545. UE.commands["deletetitle"] = {
  20546. queryCommandState: function() {
  20547. var table = getTableItemsByRange(this).table;
  20548. if (table) {
  20549. var firstRow = table.rows[0];
  20550. return firstRow.cells[
  20551. firstRow.cells.length - 1
  20552. ].tagName.toLowerCase() == "th"
  20553. ? 0
  20554. : -1;
  20555. }
  20556. return -1;
  20557. },
  20558. execCommand: function() {
  20559. var table = getTableItemsByRange(this).table;
  20560. if (table) {
  20561. domUtils.remove(table.rows[0]);
  20562. }
  20563. var td = table.getElementsByTagName("td")[0];
  20564. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  20565. }
  20566. };
  20567. UE.commands["inserttitlecol"] = {
  20568. queryCommandState: function() {
  20569. var table = getTableItemsByRange(this).table;
  20570. if (table) {
  20571. var lastRow = table.rows[table.rows.length - 1];
  20572. return lastRow.getElementsByTagName("th").length ? -1 : 0;
  20573. }
  20574. return -1;
  20575. },
  20576. execCommand: function(cmd) {
  20577. var table = getTableItemsByRange(this).table;
  20578. if (table) {
  20579. getUETable(table).insertCol(0, "th");
  20580. }
  20581. resetTdWidth(table, this);
  20582. var th = table.getElementsByTagName("th")[0];
  20583. this.selection.getRange().setStart(th, 0).setCursor(false, true);
  20584. }
  20585. };
  20586. UE.commands["deletetitlecol"] = {
  20587. queryCommandState: function() {
  20588. var table = getTableItemsByRange(this).table;
  20589. if (table) {
  20590. var lastRow = table.rows[table.rows.length - 1];
  20591. return lastRow.getElementsByTagName("th").length ? 0 : -1;
  20592. }
  20593. return -1;
  20594. },
  20595. execCommand: function() {
  20596. var table = getTableItemsByRange(this).table;
  20597. if (table) {
  20598. for (var i = 0; i < table.rows.length; i++) {
  20599. domUtils.remove(table.rows[i].children[0]);
  20600. }
  20601. }
  20602. resetTdWidth(table, this);
  20603. var td = table.getElementsByTagName("td")[0];
  20604. this.selection.getRange().setStart(td, 0).setCursor(false, true);
  20605. }
  20606. };
  20607. UE.commands["mergeright"] = {
  20608. queryCommandState: function(cmd) {
  20609. var tableItems = getTableItemsByRange(this),
  20610. table = tableItems.table,
  20611. cell = tableItems.cell;
  20612. if (!table || !cell) return -1;
  20613. var ut = getUETable(table);
  20614. if (ut.selectedTds.length) return -1;
  20615. var cellInfo = ut.getCellInfo(cell),
  20616. rightColIndex = cellInfo.colIndex + cellInfo.colSpan;
  20617. if (rightColIndex >= ut.colsNum) return -1; // 如果处于最右边则不能向右合并
  20618. var rightCellInfo = ut.indexTable[cellInfo.rowIndex][rightColIndex],
  20619. rightCell =
  20620. table.rows[rightCellInfo.rowIndex].cells[rightCellInfo.cellIndex];
  20621. if (!rightCell || cell.tagName != rightCell.tagName) return -1; // TH和TD不能相互合并
  20622. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20623. return rightCellInfo.rowIndex == cellInfo.rowIndex &&
  20624. rightCellInfo.rowSpan == cellInfo.rowSpan
  20625. ? 0
  20626. : -1;
  20627. },
  20628. execCommand: function(cmd) {
  20629. var rng = this.selection.getRange(),
  20630. bk = rng.createBookmark(true);
  20631. var cell = getTableItemsByRange(this).cell,
  20632. ut = getUETable(cell);
  20633. ut.mergeRight(cell);
  20634. rng.moveToBookmark(bk).select();
  20635. }
  20636. };
  20637. UE.commands["mergedown"] = {
  20638. queryCommandState: function(cmd) {
  20639. var tableItems = getTableItemsByRange(this),
  20640. table = tableItems.table,
  20641. cell = tableItems.cell;
  20642. if (!table || !cell) return -1;
  20643. var ut = getUETable(table);
  20644. if (ut.selectedTds.length) return -1;
  20645. var cellInfo = ut.getCellInfo(cell),
  20646. downRowIndex = cellInfo.rowIndex + cellInfo.rowSpan;
  20647. if (downRowIndex >= ut.rowsNum) return -1; // 如果处于最下边则不能向下合并
  20648. var downCellInfo = ut.indexTable[downRowIndex][cellInfo.colIndex],
  20649. downCell =
  20650. table.rows[downCellInfo.rowIndex].cells[downCellInfo.cellIndex];
  20651. if (!downCell || cell.tagName != downCell.tagName) return -1; // TH和TD不能相互合并
  20652. // 当且仅当两个Cell的开始列号和结束列号一致时能进行合并
  20653. return downCellInfo.colIndex == cellInfo.colIndex &&
  20654. downCellInfo.colSpan == cellInfo.colSpan
  20655. ? 0
  20656. : -1;
  20657. },
  20658. execCommand: function() {
  20659. var rng = this.selection.getRange(),
  20660. bk = rng.createBookmark(true);
  20661. var cell = getTableItemsByRange(this).cell,
  20662. ut = getUETable(cell);
  20663. ut.mergeDown(cell);
  20664. rng.moveToBookmark(bk).select();
  20665. }
  20666. };
  20667. UE.commands["mergecells"] = {
  20668. queryCommandState: function() {
  20669. return getUETableBySelected(this) ? 0 : -1;
  20670. },
  20671. execCommand: function() {
  20672. var ut = getUETableBySelected(this);
  20673. if (ut && ut.selectedTds.length) {
  20674. var cell = ut.selectedTds[0];
  20675. ut.mergeRange();
  20676. var rng = this.selection.getRange();
  20677. if (domUtils.isEmptyBlock(cell)) {
  20678. rng.setStart(cell, 0).collapse(true);
  20679. } else {
  20680. rng.selectNodeContents(cell);
  20681. }
  20682. rng.select();
  20683. }
  20684. }
  20685. };
  20686. UE.commands["insertrow"] = {
  20687. queryCommandState: function() {
  20688. var tableItems = getTableItemsByRange(this),
  20689. cell = tableItems.cell;
  20690. return cell &&
  20691. (cell.tagName == "TD" ||
  20692. (cell.tagName == "TH" &&
  20693. tableItems.tr !== tableItems.table.rows[0])) &&
  20694. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20695. ? 0
  20696. : -1;
  20697. },
  20698. execCommand: function() {
  20699. var rng = this.selection.getRange(),
  20700. bk = rng.createBookmark(true);
  20701. var tableItems = getTableItemsByRange(this),
  20702. cell = tableItems.cell,
  20703. table = tableItems.table,
  20704. ut = getUETable(table),
  20705. cellInfo = ut.getCellInfo(cell);
  20706. //ut.insertRow(!ut.selectedTds.length ? cellInfo.rowIndex:ut.cellsRange.beginRowIndex,'');
  20707. if (!ut.selectedTds.length) {
  20708. ut.insertRow(cellInfo.rowIndex, cell);
  20709. } else {
  20710. var range = ut.cellsRange;
  20711. for (
  20712. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20713. i < len;
  20714. i++
  20715. ) {
  20716. ut.insertRow(range.beginRowIndex, cell);
  20717. }
  20718. }
  20719. rng.moveToBookmark(bk).select();
  20720. if (table.getAttribute("interlaced") === "enabled")
  20721. this.fireEvent("interlacetable", table);
  20722. }
  20723. };
  20724. //后插入行
  20725. UE.commands["insertrownext"] = {
  20726. queryCommandState: function() {
  20727. var tableItems = getTableItemsByRange(this),
  20728. cell = tableItems.cell;
  20729. return cell &&
  20730. cell.tagName == "TD" &&
  20731. getUETable(tableItems.table).rowsNum < this.options.maxRowNum
  20732. ? 0
  20733. : -1;
  20734. },
  20735. execCommand: function() {
  20736. var rng = this.selection.getRange(),
  20737. bk = rng.createBookmark(true);
  20738. var tableItems = getTableItemsByRange(this),
  20739. cell = tableItems.cell,
  20740. table = tableItems.table,
  20741. ut = getUETable(table),
  20742. cellInfo = ut.getCellInfo(cell);
  20743. //ut.insertRow(!ut.selectedTds.length? cellInfo.rowIndex + cellInfo.rowSpan : ut.cellsRange.endRowIndex + 1,'');
  20744. if (!ut.selectedTds.length) {
  20745. ut.insertRow(cellInfo.rowIndex + cellInfo.rowSpan, cell);
  20746. } else {
  20747. var range = ut.cellsRange;
  20748. for (
  20749. var i = 0, len = range.endRowIndex - range.beginRowIndex + 1;
  20750. i < len;
  20751. i++
  20752. ) {
  20753. ut.insertRow(range.endRowIndex + 1, cell);
  20754. }
  20755. }
  20756. rng.moveToBookmark(bk).select();
  20757. if (table.getAttribute("interlaced") === "enabled")
  20758. this.fireEvent("interlacetable", table);
  20759. }
  20760. };
  20761. UE.commands["deleterow"] = {
  20762. queryCommandState: function() {
  20763. var tableItems = getTableItemsByRange(this);
  20764. return tableItems.cell ? 0 : -1;
  20765. },
  20766. execCommand: function() {
  20767. var cell = getTableItemsByRange(this).cell,
  20768. ut = getUETable(cell),
  20769. cellsRange = ut.cellsRange,
  20770. cellInfo = ut.getCellInfo(cell),
  20771. preCell = ut.getVSideCell(cell),
  20772. nextCell = ut.getVSideCell(cell, true),
  20773. rng = this.selection.getRange();
  20774. if (utils.isEmptyObject(cellsRange)) {
  20775. ut.deleteRow(cellInfo.rowIndex);
  20776. } else {
  20777. for (
  20778. var i = cellsRange.beginRowIndex;
  20779. i < cellsRange.endRowIndex + 1;
  20780. i++
  20781. ) {
  20782. ut.deleteRow(cellsRange.beginRowIndex);
  20783. }
  20784. }
  20785. var table = ut.table;
  20786. if (!table.getElementsByTagName("td").length) {
  20787. var nextSibling = table.nextSibling;
  20788. domUtils.remove(table);
  20789. if (nextSibling) {
  20790. rng.setStart(nextSibling, 0).setCursor(false, true);
  20791. }
  20792. } else {
  20793. if (
  20794. cellInfo.rowSpan == 1 ||
  20795. cellInfo.rowSpan ==
  20796. cellsRange.endRowIndex - cellsRange.beginRowIndex + 1
  20797. ) {
  20798. if (nextCell || preCell)
  20799. rng.selectNodeContents(nextCell || preCell).setCursor(false, true);
  20800. } else {
  20801. var newCell = ut.getCell(
  20802. cellInfo.rowIndex,
  20803. ut.indexTable[cellInfo.rowIndex][cellInfo.colIndex].cellIndex
  20804. );
  20805. if (newCell) rng.selectNodeContents(newCell).setCursor(false, true);
  20806. }
  20807. }
  20808. if (table.getAttribute("interlaced") === "enabled")
  20809. this.fireEvent("interlacetable", table);
  20810. }
  20811. };
  20812. UE.commands["insertcol"] = {
  20813. queryCommandState: function(cmd) {
  20814. var tableItems = getTableItemsByRange(this),
  20815. cell = tableItems.cell;
  20816. return cell &&
  20817. (cell.tagName == "TD" ||
  20818. (cell.tagName == "TH" && cell !== tableItems.tr.cells[0])) &&
  20819. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20820. ? 0
  20821. : -1;
  20822. },
  20823. execCommand: function(cmd) {
  20824. var rng = this.selection.getRange(),
  20825. bk = rng.createBookmark(true);
  20826. if (this.queryCommandState(cmd) == -1) return;
  20827. var cell = getTableItemsByRange(this).cell,
  20828. ut = getUETable(cell),
  20829. cellInfo = ut.getCellInfo(cell);
  20830. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex:ut.cellsRange.beginColIndex);
  20831. if (!ut.selectedTds.length) {
  20832. ut.insertCol(cellInfo.colIndex, cell);
  20833. } else {
  20834. var range = ut.cellsRange;
  20835. for (
  20836. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20837. i < len;
  20838. i++
  20839. ) {
  20840. ut.insertCol(range.beginColIndex, cell);
  20841. }
  20842. }
  20843. rng.moveToBookmark(bk).select(true);
  20844. }
  20845. };
  20846. UE.commands["insertcolnext"] = {
  20847. queryCommandState: function() {
  20848. var tableItems = getTableItemsByRange(this),
  20849. cell = tableItems.cell;
  20850. return cell &&
  20851. getUETable(tableItems.table).colsNum < this.options.maxColNum
  20852. ? 0
  20853. : -1;
  20854. },
  20855. execCommand: function() {
  20856. var rng = this.selection.getRange(),
  20857. bk = rng.createBookmark(true);
  20858. var cell = getTableItemsByRange(this).cell,
  20859. ut = getUETable(cell),
  20860. cellInfo = ut.getCellInfo(cell);
  20861. //ut.insertCol(!ut.selectedTds.length ? cellInfo.colIndex + cellInfo.colSpan:ut.cellsRange.endColIndex +1);
  20862. if (!ut.selectedTds.length) {
  20863. ut.insertCol(cellInfo.colIndex + cellInfo.colSpan, cell);
  20864. } else {
  20865. var range = ut.cellsRange;
  20866. for (
  20867. var i = 0, len = range.endColIndex - range.beginColIndex + 1;
  20868. i < len;
  20869. i++
  20870. ) {
  20871. ut.insertCol(range.endColIndex + 1, cell);
  20872. }
  20873. }
  20874. rng.moveToBookmark(bk).select();
  20875. }
  20876. };
  20877. UE.commands["deletecol"] = {
  20878. queryCommandState: function() {
  20879. var tableItems = getTableItemsByRange(this);
  20880. return tableItems.cell ? 0 : -1;
  20881. },
  20882. execCommand: function() {
  20883. var cell = getTableItemsByRange(this).cell,
  20884. ut = getUETable(cell),
  20885. range = ut.cellsRange,
  20886. cellInfo = ut.getCellInfo(cell),
  20887. preCell = ut.getHSideCell(cell),
  20888. nextCell = ut.getHSideCell(cell, true);
  20889. if (utils.isEmptyObject(range)) {
  20890. ut.deleteCol(cellInfo.colIndex);
  20891. } else {
  20892. for (var i = range.beginColIndex; i < range.endColIndex + 1; i++) {
  20893. ut.deleteCol(range.beginColIndex);
  20894. }
  20895. }
  20896. var table = ut.table,
  20897. rng = this.selection.getRange();
  20898. if (!table.getElementsByTagName("td").length) {
  20899. var nextSibling = table.nextSibling;
  20900. domUtils.remove(table);
  20901. if (nextSibling) {
  20902. rng.setStart(nextSibling, 0).setCursor(false, true);
  20903. }
  20904. } else {
  20905. if (domUtils.inDoc(cell, this.document)) {
  20906. rng.setStart(cell, 0).setCursor(false, true);
  20907. } else {
  20908. if (nextCell && domUtils.inDoc(nextCell, this.document)) {
  20909. rng.selectNodeContents(nextCell).setCursor(false, true);
  20910. } else {
  20911. if (preCell && domUtils.inDoc(preCell, this.document)) {
  20912. rng.selectNodeContents(preCell).setCursor(true, true);
  20913. }
  20914. }
  20915. }
  20916. }
  20917. }
  20918. };
  20919. UE.commands["splittocells"] = {
  20920. queryCommandState: function() {
  20921. var tableItems = getTableItemsByRange(this),
  20922. cell = tableItems.cell;
  20923. if (!cell) return -1;
  20924. var ut = getUETable(tableItems.table);
  20925. if (ut.selectedTds.length > 0) return -1;
  20926. return cell && (cell.colSpan > 1 || cell.rowSpan > 1) ? 0 : -1;
  20927. },
  20928. execCommand: function() {
  20929. var rng = this.selection.getRange(),
  20930. bk = rng.createBookmark(true);
  20931. var cell = getTableItemsByRange(this).cell,
  20932. ut = getUETable(cell);
  20933. ut.splitToCells(cell);
  20934. rng.moveToBookmark(bk).select();
  20935. }
  20936. };
  20937. UE.commands["splittorows"] = {
  20938. queryCommandState: function() {
  20939. var tableItems = getTableItemsByRange(this),
  20940. cell = tableItems.cell;
  20941. if (!cell) return -1;
  20942. var ut = getUETable(tableItems.table);
  20943. if (ut.selectedTds.length > 0) return -1;
  20944. return cell && cell.rowSpan > 1 ? 0 : -1;
  20945. },
  20946. execCommand: function() {
  20947. var rng = this.selection.getRange(),
  20948. bk = rng.createBookmark(true);
  20949. var cell = getTableItemsByRange(this).cell,
  20950. ut = getUETable(cell);
  20951. ut.splitToRows(cell);
  20952. rng.moveToBookmark(bk).select();
  20953. }
  20954. };
  20955. UE.commands["splittocols"] = {
  20956. queryCommandState: function() {
  20957. var tableItems = getTableItemsByRange(this),
  20958. cell = tableItems.cell;
  20959. if (!cell) return -1;
  20960. var ut = getUETable(tableItems.table);
  20961. if (ut.selectedTds.length > 0) return -1;
  20962. return cell && cell.colSpan > 1 ? 0 : -1;
  20963. },
  20964. execCommand: function() {
  20965. var rng = this.selection.getRange(),
  20966. bk = rng.createBookmark(true);
  20967. var cell = getTableItemsByRange(this).cell,
  20968. ut = getUETable(cell);
  20969. ut.splitToCols(cell);
  20970. rng.moveToBookmark(bk).select();
  20971. }
  20972. };
  20973. UE.commands["adaptbytext"] = UE.commands["adaptbywindow"] = {
  20974. queryCommandState: function() {
  20975. return getTableItemsByRange(this).table ? 0 : -1;
  20976. },
  20977. execCommand: function(cmd) {
  20978. var tableItems = getTableItemsByRange(this),
  20979. table = tableItems.table;
  20980. if (table) {
  20981. if (cmd == "adaptbywindow") {
  20982. resetTdWidth(table, this);
  20983. } else {
  20984. var cells = domUtils.getElementsByTagName(table, "td th");
  20985. utils.each(cells, function(cell) {
  20986. cell.removeAttribute("width");
  20987. });
  20988. table.removeAttribute("width");
  20989. }
  20990. }
  20991. }
  20992. };
  20993. //平均分配各列
  20994. UE.commands["averagedistributecol"] = {
  20995. queryCommandState: function() {
  20996. var ut = getUETableBySelected(this);
  20997. if (!ut) return -1;
  20998. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  20999. },
  21000. execCommand: function(cmd) {
  21001. var me = this,
  21002. ut = getUETableBySelected(me);
  21003. function getAverageWidth() {
  21004. var tb = ut.table,
  21005. averageWidth,
  21006. sumWidth = 0,
  21007. colsNum = 0,
  21008. tbAttr = getDefaultValue(me, tb);
  21009. if (ut.isFullRow()) {
  21010. sumWidth = tb.offsetWidth;
  21011. colsNum = ut.colsNum;
  21012. } else {
  21013. var begin = ut.cellsRange.beginColIndex,
  21014. end = ut.cellsRange.endColIndex,
  21015. node;
  21016. for (var i = begin; i <= end; ) {
  21017. node = ut.selectedTds[i];
  21018. sumWidth += node.offsetWidth;
  21019. i += node.colSpan;
  21020. colsNum += 1;
  21021. }
  21022. }
  21023. averageWidth =
  21024. Math.ceil(sumWidth / colsNum) -
  21025. tbAttr.tdBorder * 2 -
  21026. tbAttr.tdPadding * 2;
  21027. return averageWidth;
  21028. }
  21029. function setAverageWidth(averageWidth) {
  21030. utils.each(domUtils.getElementsByTagName(ut.table, "th"), function(
  21031. node
  21032. ) {
  21033. node.setAttribute("width", "");
  21034. });
  21035. var cells = ut.isFullRow()
  21036. ? domUtils.getElementsByTagName(ut.table, "td")
  21037. : ut.selectedTds;
  21038. utils.each(cells, function(node) {
  21039. if (node.colSpan == 1) {
  21040. node.setAttribute("width", averageWidth);
  21041. }
  21042. });
  21043. }
  21044. if (ut && ut.selectedTds.length) {
  21045. setAverageWidth(getAverageWidth());
  21046. }
  21047. }
  21048. };
  21049. //平均分配各行
  21050. UE.commands["averagedistributerow"] = {
  21051. queryCommandState: function() {
  21052. var ut = getUETableBySelected(this);
  21053. if (!ut) return -1;
  21054. if (ut.selectedTds && /th/gi.test(ut.selectedTds[0].tagName)) return -1;
  21055. return ut.isFullRow() || ut.isFullCol() ? 0 : -1;
  21056. },
  21057. execCommand: function(cmd) {
  21058. var me = this,
  21059. ut = getUETableBySelected(me);
  21060. function getAverageHeight() {
  21061. var averageHeight,
  21062. rowNum,
  21063. sumHeight = 0,
  21064. tb = ut.table,
  21065. tbAttr = getDefaultValue(me, tb),
  21066. tdpadding = parseInt(
  21067. domUtils.getComputedStyle(
  21068. tb.getElementsByTagName("td")[0],
  21069. "padding-top"
  21070. )
  21071. );
  21072. if (ut.isFullCol()) {
  21073. var captionArr = domUtils.getElementsByTagName(tb, "caption"),
  21074. thArr = domUtils.getElementsByTagName(tb, "th"),
  21075. captionHeight,
  21076. thHeight;
  21077. if (captionArr.length > 0) {
  21078. captionHeight = captionArr[0].offsetHeight;
  21079. }
  21080. if (thArr.length > 0) {
  21081. thHeight = thArr[0].offsetHeight;
  21082. }
  21083. sumHeight = tb.offsetHeight - (captionHeight || 0) - (thHeight || 0);
  21084. rowNum = thArr.length == 0 ? ut.rowsNum : ut.rowsNum - 1;
  21085. } else {
  21086. var begin = ut.cellsRange.beginRowIndex,
  21087. end = ut.cellsRange.endRowIndex,
  21088. count = 0,
  21089. trs = domUtils.getElementsByTagName(tb, "tr");
  21090. for (var i = begin; i <= end; i++) {
  21091. sumHeight += trs[i].offsetHeight;
  21092. count += 1;
  21093. }
  21094. rowNum = count;
  21095. }
  21096. //ie8下是混杂模式
  21097. if (browser.ie && browser.version < 9) {
  21098. averageHeight = Math.ceil(sumHeight / rowNum);
  21099. } else {
  21100. averageHeight =
  21101. Math.ceil(sumHeight / rowNum) - tbAttr.tdBorder * 2 - tdpadding * 2;
  21102. }
  21103. return averageHeight;
  21104. }
  21105. function setAverageHeight(averageHeight) {
  21106. var cells = ut.isFullCol()
  21107. ? domUtils.getElementsByTagName(ut.table, "td")
  21108. : ut.selectedTds;
  21109. utils.each(cells, function(node) {
  21110. if (node.rowSpan == 1) {
  21111. node.setAttribute("height", averageHeight);
  21112. }
  21113. });
  21114. }
  21115. if (ut && ut.selectedTds.length) {
  21116. setAverageHeight(getAverageHeight());
  21117. }
  21118. }
  21119. };
  21120. //单元格对齐方式
  21121. UE.commands["cellalignment"] = {
  21122. queryCommandState: function() {
  21123. return getTableItemsByRange(this).table ? 0 : -1;
  21124. },
  21125. execCommand: function(cmd, data) {
  21126. var me = this,
  21127. ut = getUETableBySelected(me);
  21128. if (!ut) {
  21129. var start = me.selection.getStart(),
  21130. cell =
  21131. start &&
  21132. domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  21133. if (!/caption/gi.test(cell.tagName)) {
  21134. domUtils.setAttributes(cell, data);
  21135. } else {
  21136. cell.style.textAlign = data.align;
  21137. cell.style.verticalAlign = data.vAlign;
  21138. }
  21139. me.selection.getRange().setCursor(true);
  21140. } else {
  21141. utils.each(ut.selectedTds, function(cell) {
  21142. domUtils.setAttributes(cell, data);
  21143. });
  21144. }
  21145. },
  21146. /**
  21147. * 查询当前点击的单元格的对齐状态, 如果当前已经选择了多个单元格, 则会返回所有单元格经过统一协调过后的状态
  21148. * @see UE.UETable.getTableCellAlignState
  21149. */
  21150. queryCommandValue: function(cmd) {
  21151. var activeMenuCell = getTableItemsByRange(this).cell;
  21152. if (!activeMenuCell) {
  21153. activeMenuCell = getSelectedArr(this)[0];
  21154. }
  21155. if (!activeMenuCell) {
  21156. return null;
  21157. } else {
  21158. //获取同时选中的其他单元格
  21159. var cells = UE.UETable.getUETable(activeMenuCell).selectedTds;
  21160. !cells.length && (cells = activeMenuCell);
  21161. return UE.UETable.getTableCellAlignState(cells);
  21162. }
  21163. }
  21164. };
  21165. //表格对齐方式
  21166. UE.commands["tablealignment"] = {
  21167. queryCommandState: function() {
  21168. if (browser.ie && browser.version < 8) {
  21169. return -1;
  21170. }
  21171. return getTableItemsByRange(this).table ? 0 : -1;
  21172. },
  21173. execCommand: function(cmd, value) {
  21174. var me = this,
  21175. start = me.selection.getStart(),
  21176. table = start && domUtils.findParentByTagName(start, ["table"], true);
  21177. if (table) {
  21178. table.setAttribute("align", value);
  21179. }
  21180. }
  21181. };
  21182. //表格属性
  21183. UE.commands["edittable"] = {
  21184. queryCommandState: function() {
  21185. return getTableItemsByRange(this).table ? 0 : -1;
  21186. },
  21187. execCommand: function(cmd, color) {
  21188. var rng = this.selection.getRange(),
  21189. table = domUtils.findParentByTagName(rng.startContainer, "table");
  21190. if (table) {
  21191. var arr = domUtils
  21192. .getElementsByTagName(table, "td")
  21193. .concat(
  21194. domUtils.getElementsByTagName(table, "th"),
  21195. domUtils.getElementsByTagName(table, "caption")
  21196. );
  21197. utils.each(arr, function(node) {
  21198. node.style.borderColor = color;
  21199. });
  21200. }
  21201. }
  21202. };
  21203. //单元格属性
  21204. UE.commands["edittd"] = {
  21205. queryCommandState: function() {
  21206. return getTableItemsByRange(this).table ? 0 : -1;
  21207. },
  21208. execCommand: function(cmd, bkColor) {
  21209. var me = this,
  21210. ut = getUETableBySelected(me);
  21211. if (!ut) {
  21212. var start = me.selection.getStart(),
  21213. cell =
  21214. start &&
  21215. domUtils.findParentByTagName(start, ["td", "th", "caption"], true);
  21216. if (cell) {
  21217. cell.style.backgroundColor = bkColor;
  21218. }
  21219. } else {
  21220. utils.each(ut.selectedTds, function(cell) {
  21221. cell.style.backgroundColor = bkColor;
  21222. });
  21223. }
  21224. }
  21225. };
  21226. UE.commands["settablebackground"] = {
  21227. queryCommandState: function() {
  21228. return getSelectedArr(this).length > 1 ? 0 : -1;
  21229. },
  21230. execCommand: function(cmd, value) {
  21231. var cells, ut;
  21232. cells = getSelectedArr(this);
  21233. ut = getUETable(cells[0]);
  21234. ut.setBackground(cells, value);
  21235. }
  21236. };
  21237. UE.commands["cleartablebackground"] = {
  21238. queryCommandState: function() {
  21239. var cells = getSelectedArr(this);
  21240. if (!cells.length) return -1;
  21241. for (var i = 0, cell; (cell = cells[i++]); ) {
  21242. if (cell.style.backgroundColor !== "") return 0;
  21243. }
  21244. return -1;
  21245. },
  21246. execCommand: function() {
  21247. var cells = getSelectedArr(this),
  21248. ut = getUETable(cells[0]);
  21249. ut.removeBackground(cells);
  21250. }
  21251. };
  21252. UE.commands["interlacetable"] = UE.commands["uninterlacetable"] = {
  21253. queryCommandState: function(cmd) {
  21254. var table = getTableItemsByRange(this).table;
  21255. if (!table) return -1;
  21256. var interlaced = table.getAttribute("interlaced");
  21257. if (cmd == "interlacetable") {
  21258. //TODO 待定
  21259. //是否需要待定,如果设置,则命令只能单次执行成功,但反射具备toggle效果;否则可以覆盖前次命令,但反射将不存在toggle效果
  21260. return interlaced === "enabled" ? -1 : 0;
  21261. } else {
  21262. return !interlaced || interlaced === "disabled" ? -1 : 0;
  21263. }
  21264. },
  21265. execCommand: function(cmd, classList) {
  21266. var table = getTableItemsByRange(this).table;
  21267. if (cmd == "interlacetable") {
  21268. table.setAttribute("interlaced", "enabled");
  21269. this.fireEvent("interlacetable", table, classList);
  21270. } else {
  21271. table.setAttribute("interlaced", "disabled");
  21272. this.fireEvent("uninterlacetable", table);
  21273. }
  21274. }
  21275. };
  21276. UE.commands["setbordervisible"] = {
  21277. queryCommandState: function(cmd) {
  21278. var table = getTableItemsByRange(this).table;
  21279. if (!table) return -1;
  21280. return 0;
  21281. },
  21282. execCommand: function() {
  21283. var table = getTableItemsByRange(this).table;
  21284. utils.each(domUtils.getElementsByTagName(table, "td"), function(td) {
  21285. td.style.borderWidth = "1px";
  21286. td.style.borderStyle = "solid";
  21287. });
  21288. }
  21289. };
  21290. function resetTdWidth(table, editor) {
  21291. var tds = domUtils.getElementsByTagName(table, "td th");
  21292. utils.each(tds, function(td) {
  21293. td.removeAttribute("width");
  21294. });
  21295. table.setAttribute(
  21296. "width",
  21297. getTableWidth(editor, true, getDefaultValue(editor, table))
  21298. );
  21299. var tdsWidths = [];
  21300. setTimeout(function() {
  21301. utils.each(tds, function(td) {
  21302. td.colSpan == 1 && tdsWidths.push(td.offsetWidth);
  21303. });
  21304. utils.each(tds, function(td, i) {
  21305. td.colSpan == 1 && td.setAttribute("width", tdsWidths[i] + "");
  21306. });
  21307. }, 0);
  21308. }
  21309. function getTableWidth(editor, needIEHack, defaultValue) {
  21310. var body = editor.body;
  21311. return (
  21312. body.offsetWidth -
  21313. (needIEHack
  21314. ? parseInt(domUtils.getComputedStyle(body, "margin-left"), 10) * 2
  21315. : 0) -
  21316. defaultValue.tableBorder * 2 -
  21317. (editor.options.offsetWidth || 0)
  21318. );
  21319. }
  21320. function getSelectedArr(editor) {
  21321. var cell = getTableItemsByRange(editor).cell;
  21322. if (cell) {
  21323. var ut = getUETable(cell);
  21324. return ut.selectedTds.length ? ut.selectedTds : [cell];
  21325. } else {
  21326. return [];
  21327. }
  21328. }
  21329. })();
  21330. // plugins/table.action.js
  21331. /**
  21332. * Created with JetBrains PhpStorm.
  21333. * User: taoqili
  21334. * Date: 12-10-12
  21335. * Time: 上午10:05
  21336. * To change this template use File | Settings | File Templates.
  21337. */
  21338. UE.plugins["table"] = function() {
  21339. var me = this,
  21340. tabTimer = null,
  21341. //拖动计时器
  21342. tableDragTimer = null,
  21343. //双击计时器
  21344. tableResizeTimer = null,
  21345. //单元格最小宽度
  21346. cellMinWidth = 5,
  21347. isInResizeBuffer = false,
  21348. //单元格边框大小
  21349. cellBorderWidth = 5,
  21350. //鼠标偏移距离
  21351. offsetOfTableCell = 10,
  21352. //记录在有限时间内的点击状态, 共有3个取值, 0, 1, 2。 0代表未初始化, 1代表单击了1次,2代表2次
  21353. singleClickState = 0,
  21354. userActionStatus = null,
  21355. //双击允许的时间范围
  21356. dblclickTime = 200,
  21357. UT = UE.UETable,
  21358. getUETable = function(tdOrTable) {
  21359. return UT.getUETable(tdOrTable);
  21360. },
  21361. getUETableBySelected = function(editor) {
  21362. return UT.getUETableBySelected(editor);
  21363. },
  21364. getDefaultValue = function(editor, table) {
  21365. return UT.getDefaultValue(editor, table);
  21366. },
  21367. removeSelectedClass = function(cells) {
  21368. return UT.removeSelectedClass(cells);
  21369. };
  21370. function showError(e) {
  21371. // throw e;
  21372. }
  21373. me.ready(function() {
  21374. var me = this;
  21375. var orgGetText = me.selection.getText;
  21376. me.selection.getText = function() {
  21377. var table = getUETableBySelected(me);
  21378. if (table) {
  21379. var str = "";
  21380. utils.each(table.selectedTds, function(td) {
  21381. str += td[browser.ie ? "innerText" : "textContent"];
  21382. });
  21383. return str;
  21384. } else {
  21385. return orgGetText.call(me.selection);
  21386. }
  21387. };
  21388. });
  21389. //处理拖动及框选相关方法
  21390. var startTd = null, //鼠标按下时的锚点td
  21391. currentTd = null, //当前鼠标经过时的td
  21392. onDrag = "", //指示当前拖动状态,其值可为"","h","v" ,分别表示未拖动状态,横向拖动状态,纵向拖动状态,用于鼠标移动过程中的判断
  21393. onBorder = false, //检测鼠标按下时是否处在单元格边缘位置
  21394. dragButton = null,
  21395. dragOver = false,
  21396. dragLine = null, //模拟的拖动线
  21397. dragTd = null; //发生拖动的目标td
  21398. var mousedown = false,
  21399. //todo 判断混乱模式
  21400. needIEHack = true;
  21401. me.setOpt({
  21402. maxColNum: 20,
  21403. maxRowNum: 100,
  21404. defaultCols: 5,
  21405. defaultRows: 5,
  21406. tdvalign: "top",
  21407. cursorpath: me.options.UEDITOR_HOME_URL + "themes/default/images/cursor_",
  21408. tableDragable: false,
  21409. classList: [
  21410. "ue-table-interlace-color-single",
  21411. "ue-table-interlace-color-double"
  21412. ]
  21413. });
  21414. me.getUETable = getUETable;
  21415. var commands = {
  21416. deletetable: 1,
  21417. inserttable: 1,
  21418. cellvalign: 1,
  21419. insertcaption: 1,
  21420. deletecaption: 1,
  21421. inserttitle: 1,
  21422. deletetitle: 1,
  21423. mergeright: 1,
  21424. mergedown: 1,
  21425. mergecells: 1,
  21426. insertrow: 1,
  21427. insertrownext: 1,
  21428. deleterow: 1,
  21429. insertcol: 1,
  21430. insertcolnext: 1,
  21431. deletecol: 1,
  21432. splittocells: 1,
  21433. splittorows: 1,
  21434. splittocols: 1,
  21435. adaptbytext: 1,
  21436. adaptbywindow: 1,
  21437. adaptbycustomer: 1,
  21438. insertparagraph: 1,
  21439. insertparagraphbeforetable: 1,
  21440. averagedistributecol: 1,
  21441. averagedistributerow: 1
  21442. };
  21443. me.ready(function() {
  21444. utils.cssRule(
  21445. "table",
  21446. //选中的td上的样式
  21447. ".selectTdClass{background-color:#edf5fa !important}" +
  21448. "table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #ddd !important}" +
  21449. //插入的表格的默认样式
  21450. "table{margin-bottom:10px;border-collapse:collapse;display:table;}" +
  21451. "td,th{padding: 5px 10px;border: 1px solid #DDD;}" +
  21452. "caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}" +
  21453. "th{border-top:1px solid #BBB;background-color:#F7F7F7;}" +
  21454. "table tr.firstRow th{border-top-width:2px;}" +
  21455. ".ue-table-interlace-color-single{ background-color: #fcfcfc; } .ue-table-interlace-color-double{ background-color: #f7faff; }" +
  21456. "td p{margin:0;padding:0;}",
  21457. me.document
  21458. );
  21459. var tableCopyList, isFullCol, isFullRow;
  21460. //注册del/backspace事件
  21461. me.addListener("keydown", function(cmd, evt) {
  21462. var me = this;
  21463. var keyCode = evt.keyCode || evt.which;
  21464. if (keyCode == 8) {
  21465. var ut = getUETableBySelected(me);
  21466. if (ut && ut.selectedTds.length) {
  21467. if (ut.isFullCol()) {
  21468. me.execCommand("deletecol");
  21469. } else if (ut.isFullRow()) {
  21470. me.execCommand("deleterow");
  21471. } else {
  21472. me.fireEvent("delcells");
  21473. }
  21474. domUtils.preventDefault(evt);
  21475. }
  21476. var caption = domUtils.findParentByTagName(
  21477. me.selection.getStart(),
  21478. "caption",
  21479. true
  21480. ),
  21481. range = me.selection.getRange();
  21482. if (range.collapsed && caption && isEmptyBlock(caption)) {
  21483. me.fireEvent("saveScene");
  21484. var table = caption.parentNode;
  21485. domUtils.remove(caption);
  21486. if (table) {
  21487. range.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  21488. }
  21489. me.fireEvent("saveScene");
  21490. }
  21491. }
  21492. if (keyCode == 46) {
  21493. ut = getUETableBySelected(me);
  21494. if (ut) {
  21495. me.fireEvent("saveScene");
  21496. for (var i = 0, ci; (ci = ut.selectedTds[i++]); ) {
  21497. domUtils.fillNode(me.document, ci);
  21498. }
  21499. me.fireEvent("saveScene");
  21500. domUtils.preventDefault(evt);
  21501. }
  21502. }
  21503. if (keyCode == 13) {
  21504. var rng = me.selection.getRange(),
  21505. caption = domUtils.findParentByTagName(
  21506. rng.startContainer,
  21507. "caption",
  21508. true
  21509. );
  21510. if (caption) {
  21511. var table = domUtils.findParentByTagName(caption, "table");
  21512. if (!rng.collapsed) {
  21513. rng.deleteContents();
  21514. me.fireEvent("saveScene");
  21515. } else {
  21516. if (caption) {
  21517. rng.setStart(table.rows[0].cells[0], 0).setCursor(false, true);
  21518. }
  21519. }
  21520. domUtils.preventDefault(evt);
  21521. return;
  21522. }
  21523. if (rng.collapsed) {
  21524. var table = domUtils.findParentByTagName(rng.startContainer, "table");
  21525. if (table) {
  21526. var cell = table.rows[0].cells[0],
  21527. start = domUtils.findParentByTagName(
  21528. me.selection.getStart(),
  21529. ["td", "th"],
  21530. true
  21531. ),
  21532. preNode = table.previousSibling;
  21533. if (
  21534. cell === start &&
  21535. (!preNode ||
  21536. (preNode.nodeType == 1 && preNode.tagName == "TABLE")) &&
  21537. domUtils.isStartInblock(rng)
  21538. ) {
  21539. var first = domUtils.findParent(
  21540. me.selection.getStart(),
  21541. function(n) {
  21542. return domUtils.isBlockElm(n);
  21543. },
  21544. true
  21545. );
  21546. if (
  21547. first &&
  21548. (/t(h|d)/i.test(first.tagName) || first === start.firstChild)
  21549. ) {
  21550. me.execCommand("insertparagraphbeforetable");
  21551. domUtils.preventDefault(evt);
  21552. }
  21553. }
  21554. }
  21555. }
  21556. }
  21557. if ((evt.ctrlKey || evt.metaKey) && evt.keyCode == "67") {
  21558. tableCopyList = null;
  21559. var ut = getUETableBySelected(me);
  21560. if (ut) {
  21561. var tds = ut.selectedTds;
  21562. isFullCol = ut.isFullCol();
  21563. isFullRow = ut.isFullRow();
  21564. tableCopyList = [[ut.cloneCell(tds[0], null, true)]];
  21565. for (var i = 1, ci; (ci = tds[i]); i++) {
  21566. if (ci.parentNode !== tds[i - 1].parentNode) {
  21567. tableCopyList.push([ut.cloneCell(ci, null, true)]);
  21568. } else {
  21569. tableCopyList[tableCopyList.length - 1].push(
  21570. ut.cloneCell(ci, null, true)
  21571. );
  21572. }
  21573. }
  21574. }
  21575. }
  21576. });
  21577. me.addListener("tablehasdeleted", function() {
  21578. toggleDraggableState(this, false, "", null);
  21579. if (dragButton) domUtils.remove(dragButton);
  21580. });
  21581. me.addListener("beforepaste", function(cmd, html) {
  21582. var me = this;
  21583. var rng = me.selection.getRange();
  21584. if (domUtils.findParentByTagName(rng.startContainer, "caption", true)) {
  21585. var div = me.document.createElement("div");
  21586. div.innerHTML = html.html;
  21587. //trace:3729
  21588. html.html = div[browser.ie9below ? "innerText" : "textContent"];
  21589. return;
  21590. }
  21591. var table = getUETableBySelected(me);
  21592. if (tableCopyList) {
  21593. me.fireEvent("saveScene");
  21594. var rng = me.selection.getRange();
  21595. var td = domUtils.findParentByTagName(
  21596. rng.startContainer,
  21597. ["td", "th"],
  21598. true
  21599. ),
  21600. tmpNode,
  21601. preNode;
  21602. if (td) {
  21603. var ut = getUETable(td);
  21604. if (isFullRow) {
  21605. var rowIndex = ut.getCellInfo(td).rowIndex;
  21606. if (td.tagName == "TH") {
  21607. rowIndex++;
  21608. }
  21609. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21610. var tr = ut.insertRow(rowIndex++, "td");
  21611. for (var j = 0, cj; (cj = ci[j]); j++) {
  21612. var cell = tr.cells[j];
  21613. if (!cell) {
  21614. cell = tr.insertCell(j);
  21615. }
  21616. cell.innerHTML = cj.innerHTML;
  21617. cj.getAttribute("width") &&
  21618. cell.setAttribute("width", cj.getAttribute("width"));
  21619. cj.getAttribute("vAlign") &&
  21620. cell.setAttribute("vAlign", cj.getAttribute("vAlign"));
  21621. cj.getAttribute("align") &&
  21622. cell.setAttribute("align", cj.getAttribute("align"));
  21623. cj.style.cssText && (cell.style.cssText = cj.style.cssText);
  21624. }
  21625. for (var j = 0, cj; (cj = tr.cells[j]); j++) {
  21626. if (!ci[j]) break;
  21627. cj.innerHTML = ci[j].innerHTML;
  21628. ci[j].getAttribute("width") &&
  21629. cj.setAttribute("width", ci[j].getAttribute("width"));
  21630. ci[j].getAttribute("vAlign") &&
  21631. cj.setAttribute("vAlign", ci[j].getAttribute("vAlign"));
  21632. ci[j].getAttribute("align") &&
  21633. cj.setAttribute("align", ci[j].getAttribute("align"));
  21634. ci[j].style.cssText && (cj.style.cssText = ci[j].style.cssText);
  21635. }
  21636. }
  21637. } else {
  21638. if (isFullCol) {
  21639. cellInfo = ut.getCellInfo(td);
  21640. var maxColNum = 0;
  21641. for (var j = 0, ci = tableCopyList[0], cj; (cj = ci[j++]); ) {
  21642. maxColNum += cj.colSpan || 1;
  21643. }
  21644. me.__hasEnterExecCommand = true;
  21645. for (i = 0; i < maxColNum; i++) {
  21646. me.execCommand("insertcol");
  21647. }
  21648. me.__hasEnterExecCommand = false;
  21649. td = ut.table.rows[0].cells[cellInfo.cellIndex];
  21650. if (td.tagName == "TH") {
  21651. td = ut.table.rows[1].cells[cellInfo.cellIndex];
  21652. }
  21653. }
  21654. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21655. tmpNode = td;
  21656. for (var j = 0, cj; (cj = ci[j++]); ) {
  21657. if (td) {
  21658. td.innerHTML = cj.innerHTML;
  21659. //todo 定制处理
  21660. cj.getAttribute("width") &&
  21661. td.setAttribute("width", cj.getAttribute("width"));
  21662. cj.getAttribute("vAlign") &&
  21663. td.setAttribute("vAlign", cj.getAttribute("vAlign"));
  21664. cj.getAttribute("align") &&
  21665. td.setAttribute("align", cj.getAttribute("align"));
  21666. cj.style.cssText && (td.style.cssText = cj.style.cssText);
  21667. preNode = td;
  21668. td = td.nextSibling;
  21669. } else {
  21670. var cloneTd = cj.cloneNode(true);
  21671. domUtils.removeAttributes(cloneTd, [
  21672. "class",
  21673. "rowSpan",
  21674. "colSpan"
  21675. ]);
  21676. preNode.parentNode.appendChild(cloneTd);
  21677. }
  21678. }
  21679. td = ut.getNextCell(tmpNode, true, true);
  21680. if (!tableCopyList[i]) break;
  21681. if (!td) {
  21682. var cellInfo = ut.getCellInfo(tmpNode);
  21683. ut.table.insertRow(ut.table.rows.length);
  21684. ut.update();
  21685. td = ut.getVSideCell(tmpNode, true);
  21686. }
  21687. }
  21688. }
  21689. ut.update();
  21690. } else {
  21691. table = me.document.createElement("table");
  21692. for (var i = 0, ci; (ci = tableCopyList[i++]); ) {
  21693. var tr = table.insertRow(table.rows.length);
  21694. for (var j = 0, cj; (cj = ci[j++]); ) {
  21695. cloneTd = UT.cloneCell(cj, null, true);
  21696. domUtils.removeAttributes(cloneTd, ["class"]);
  21697. tr.appendChild(cloneTd);
  21698. }
  21699. if (j == 2 && cloneTd.rowSpan > 1) {
  21700. cloneTd.rowSpan = 1;
  21701. }
  21702. }
  21703. var defaultValue = getDefaultValue(me),
  21704. width =
  21705. me.body.offsetWidth -
  21706. (needIEHack
  21707. ? parseInt(
  21708. domUtils.getComputedStyle(me.body, "margin-left"),
  21709. 10
  21710. ) * 2
  21711. : 0) -
  21712. defaultValue.tableBorder * 2 -
  21713. (me.options.offsetWidth || 0);
  21714. me.execCommand(
  21715. "insertHTML",
  21716. "<table " +
  21717. (isFullCol && isFullRow ? 'width="' + width + '"' : "") +
  21718. ">" +
  21719. table.innerHTML
  21720. .replace(/>\s*</g, "><")
  21721. .replace(/\bth\b/gi, "td") +
  21722. "</table>"
  21723. );
  21724. }
  21725. me.fireEvent("contentchange");
  21726. me.fireEvent("saveScene");
  21727. html.html = "";
  21728. return true;
  21729. } else {
  21730. var div = me.document.createElement("div"),
  21731. tables;
  21732. div.innerHTML = html.html;
  21733. tables = div.getElementsByTagName("table");
  21734. if (domUtils.findParentByTagName(me.selection.getStart(), "table")) {
  21735. utils.each(tables, function(t) {
  21736. domUtils.remove(t);
  21737. });
  21738. if (
  21739. domUtils.findParentByTagName(
  21740. me.selection.getStart(),
  21741. "caption",
  21742. true
  21743. )
  21744. ) {
  21745. div.innerHTML = div[browser.ie ? "innerText" : "textContent"];
  21746. }
  21747. } else {
  21748. utils.each(tables, function(table) {
  21749. removeStyleSize(table, true);
  21750. domUtils.removeAttributes(table, ["style", "border"]);
  21751. utils.each(domUtils.getElementsByTagName(table, "td"), function(
  21752. td
  21753. ) {
  21754. if (isEmptyBlock(td)) {
  21755. domUtils.fillNode(me.document, td);
  21756. }
  21757. removeStyleSize(td, true);
  21758. // domUtils.removeAttributes(td, ['style'])
  21759. });
  21760. });
  21761. }
  21762. html.html = div.innerHTML;
  21763. }
  21764. });
  21765. me.addListener("afterpaste", function() {
  21766. utils.each(domUtils.getElementsByTagName(me.body, "table"), function(
  21767. table
  21768. ) {
  21769. if (table.offsetWidth > me.body.offsetWidth) {
  21770. var defaultValue = getDefaultValue(me, table);
  21771. table.style.width =
  21772. me.body.offsetWidth -
  21773. (needIEHack
  21774. ? parseInt(
  21775. domUtils.getComputedStyle(me.body, "margin-left"),
  21776. 10
  21777. ) * 2
  21778. : 0) -
  21779. defaultValue.tableBorder * 2 -
  21780. (me.options.offsetWidth || 0) +
  21781. "px";
  21782. }
  21783. });
  21784. });
  21785. me.addListener("blur", function() {
  21786. tableCopyList = null;
  21787. });
  21788. var timer;
  21789. me.addListener("keydown", function() {
  21790. clearTimeout(timer);
  21791. timer = setTimeout(function() {
  21792. var rng = me.selection.getRange(),
  21793. cell = domUtils.findParentByTagName(
  21794. rng.startContainer,
  21795. ["th", "td"],
  21796. true
  21797. );
  21798. if (cell) {
  21799. var table = cell.parentNode.parentNode.parentNode;
  21800. if (table.offsetWidth > table.getAttribute("width")) {
  21801. cell.style.wordBreak = "break-all";
  21802. }
  21803. }
  21804. }, 100);
  21805. });
  21806. me.addListener("selectionchange", function() {
  21807. toggleDraggableState(me, false, "", null);
  21808. });
  21809. //内容变化时触发索引更新
  21810. //todo 可否考虑标记检测,如果不涉及表格的变化就不进行索引重建和更新
  21811. me.addListener("contentchange", function() {
  21812. var me = this;
  21813. //尽可能排除一些不需要更新的状况
  21814. hideDragLine(me);
  21815. if (getUETableBySelected(me)) return;
  21816. var rng = me.selection.getRange();
  21817. var start = rng.startContainer;
  21818. start = domUtils.findParentByTagName(start, ["td", "th"], true);
  21819. utils.each(domUtils.getElementsByTagName(me.document, "table"), function(
  21820. table
  21821. ) {
  21822. if (me.fireEvent("excludetable", table) === true) return;
  21823. table.ueTable = new UT(table);
  21824. //trace:3742
  21825. // utils.each(domUtils.getElementsByTagName(me.document, 'td'), function (td) {
  21826. //
  21827. // if (domUtils.isEmptyBlock(td) && td !== start) {
  21828. // domUtils.fillNode(me.document, td);
  21829. // if (browser.ie && browser.version == 6) {
  21830. // td.innerHTML = '&nbsp;'
  21831. // }
  21832. // }
  21833. // });
  21834. // utils.each(domUtils.getElementsByTagName(me.document, 'th'), function (th) {
  21835. // if (domUtils.isEmptyBlock(th) && th !== start) {
  21836. // domUtils.fillNode(me.document, th);
  21837. // if (browser.ie && browser.version == 6) {
  21838. // th.innerHTML = '&nbsp;'
  21839. // }
  21840. // }
  21841. // });
  21842. table.onmouseover = function() {
  21843. me.fireEvent("tablemouseover", table);
  21844. };
  21845. table.onmousemove = function() {
  21846. me.fireEvent("tablemousemove", table);
  21847. me.options.tableDragable && toggleDragButton(true, this, me);
  21848. utils.defer(function() {
  21849. me.fireEvent("contentchange", 50);
  21850. }, true);
  21851. };
  21852. table.onmouseout = function() {
  21853. me.fireEvent("tablemouseout", table);
  21854. toggleDraggableState(me, false, "", null);
  21855. hideDragLine(me);
  21856. };
  21857. table.onclick = function(evt) {
  21858. evt = me.window.event || evt;
  21859. var target = getParentTdOrTh(evt.target || evt.srcElement);
  21860. if (!target) return;
  21861. var ut = getUETable(target),
  21862. table = ut.table,
  21863. cellInfo = ut.getCellInfo(target),
  21864. cellsRange,
  21865. rng = me.selection.getRange();
  21866. // if ("topLeft" == inPosition(table, mouseCoords(evt))) {
  21867. // cellsRange = ut.getCellsRange(ut.table.rows[0].cells[0], ut.getLastCell());
  21868. // ut.setSelected(cellsRange);
  21869. // return;
  21870. // }
  21871. // if ("bottomRight" == inPosition(table, mouseCoords(evt))) {
  21872. //
  21873. // return;
  21874. // }
  21875. if (inTableSide(table, target, evt, true)) {
  21876. var endTdCol = ut.getCell(
  21877. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].rowIndex,
  21878. ut.indexTable[ut.rowsNum - 1][cellInfo.colIndex].cellIndex
  21879. );
  21880. if (evt.shiftKey && ut.selectedTds.length) {
  21881. if (ut.selectedTds[0] !== endTdCol) {
  21882. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdCol);
  21883. ut.setSelected(cellsRange);
  21884. } else {
  21885. rng && rng.selectNodeContents(endTdCol).select();
  21886. }
  21887. } else {
  21888. if (target !== endTdCol) {
  21889. cellsRange = ut.getCellsRange(target, endTdCol);
  21890. ut.setSelected(cellsRange);
  21891. } else {
  21892. rng && rng.selectNodeContents(endTdCol).select();
  21893. }
  21894. }
  21895. return;
  21896. }
  21897. if (inTableSide(table, target, evt)) {
  21898. var endTdRow = ut.getCell(
  21899. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].rowIndex,
  21900. ut.indexTable[cellInfo.rowIndex][ut.colsNum - 1].cellIndex
  21901. );
  21902. if (evt.shiftKey && ut.selectedTds.length) {
  21903. if (ut.selectedTds[0] !== endTdRow) {
  21904. cellsRange = ut.getCellsRange(ut.selectedTds[0], endTdRow);
  21905. ut.setSelected(cellsRange);
  21906. } else {
  21907. rng && rng.selectNodeContents(endTdRow).select();
  21908. }
  21909. } else {
  21910. if (target !== endTdRow) {
  21911. cellsRange = ut.getCellsRange(target, endTdRow);
  21912. ut.setSelected(cellsRange);
  21913. } else {
  21914. rng && rng.selectNodeContents(endTdRow).select();
  21915. }
  21916. }
  21917. }
  21918. };
  21919. });
  21920. switchBorderColor(me, true);
  21921. });
  21922. domUtils.on(me.document, "mousemove", mouseMoveEvent);
  21923. domUtils.on(me.document, "mouseout", function(evt) {
  21924. var target = evt.target || evt.srcElement;
  21925. if (target.tagName == "TABLE") {
  21926. toggleDraggableState(me, false, "", null);
  21927. }
  21928. });
  21929. /**
  21930. * 表格隔行变色
  21931. */
  21932. me.addListener("interlacetable", function(type, table, classList) {
  21933. if (!table) return;
  21934. var me = this,
  21935. rows = table.rows,
  21936. len = rows.length,
  21937. getClass = function(list, index, repeat) {
  21938. return list[index]
  21939. ? list[index]
  21940. : repeat ? list[index % list.length] : "";
  21941. };
  21942. for (var i = 0; i < len; i++) {
  21943. rows[i].className = getClass(
  21944. classList || me.options.classList,
  21945. i,
  21946. true
  21947. );
  21948. }
  21949. });
  21950. me.addListener("uninterlacetable", function(type, table) {
  21951. if (!table) return;
  21952. var me = this,
  21953. rows = table.rows,
  21954. classList = me.options.classList,
  21955. len = rows.length;
  21956. for (var i = 0; i < len; i++) {
  21957. domUtils.removeClasses(rows[i], classList);
  21958. }
  21959. });
  21960. me.addListener("mousedown", mouseDownEvent);
  21961. me.addListener("mouseup", mouseUpEvent);
  21962. //拖动的时候触发mouseup
  21963. domUtils.on(me.body, "dragstart", function(evt) {
  21964. mouseUpEvent.call(me, "dragstart", evt);
  21965. });
  21966. me.addOutputRule(function(root) {
  21967. utils.each(root.getNodesByTagName("div"), function(n) {
  21968. if (n.getAttr("id") == "ue_tableDragLine") {
  21969. n.parentNode.removeChild(n);
  21970. }
  21971. });
  21972. });
  21973. var currentRowIndex = 0;
  21974. me.addListener("mousedown", function() {
  21975. currentRowIndex = 0;
  21976. });
  21977. me.addListener("tabkeydown", function() {
  21978. var range = this.selection.getRange(),
  21979. common = range.getCommonAncestor(true, true),
  21980. table = domUtils.findParentByTagName(common, "table");
  21981. if (table) {
  21982. if (domUtils.findParentByTagName(common, "caption", true)) {
  21983. var cell = domUtils.getElementsByTagName(table, "th td");
  21984. if (cell && cell.length) {
  21985. range.setStart(cell[0], 0).setCursor(false, true);
  21986. }
  21987. } else {
  21988. var cell = domUtils.findParentByTagName(common, ["td", "th"], true),
  21989. ua = getUETable(cell);
  21990. currentRowIndex = cell.rowSpan > 1
  21991. ? currentRowIndex
  21992. : ua.getCellInfo(cell).rowIndex;
  21993. var nextCell = ua.getTabNextCell(cell, currentRowIndex);
  21994. if (nextCell) {
  21995. if (isEmptyBlock(nextCell)) {
  21996. range.setStart(nextCell, 0).setCursor(false, true);
  21997. } else {
  21998. range.selectNodeContents(nextCell).select();
  21999. }
  22000. } else {
  22001. me.fireEvent("saveScene");
  22002. me.__hasEnterExecCommand = true;
  22003. this.execCommand("insertrownext");
  22004. me.__hasEnterExecCommand = false;
  22005. range = this.selection.getRange();
  22006. range
  22007. .setStart(table.rows[table.rows.length - 1].cells[0], 0)
  22008. .setCursor();
  22009. me.fireEvent("saveScene");
  22010. }
  22011. }
  22012. return true;
  22013. }
  22014. });
  22015. browser.ie &&
  22016. me.addListener("selectionchange", function() {
  22017. toggleDraggableState(this, false, "", null);
  22018. });
  22019. me.addListener("keydown", function(type, evt) {
  22020. var me = this;
  22021. //处理在表格的最后一个输入tab产生新的表格
  22022. var keyCode = evt.keyCode || evt.which;
  22023. if (keyCode == 8 || keyCode == 46) {
  22024. return;
  22025. }
  22026. var notCtrlKey =
  22027. !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey;
  22028. notCtrlKey &&
  22029. removeSelectedClass(domUtils.getElementsByTagName(me.body, "td"));
  22030. var ut = getUETableBySelected(me);
  22031. if (!ut) return;
  22032. notCtrlKey && ut.clearSelected();
  22033. });
  22034. me.addListener("beforegetcontent", function() {
  22035. switchBorderColor(this, false);
  22036. browser.ie &&
  22037. utils.each(this.document.getElementsByTagName("caption"), function(ci) {
  22038. if (domUtils.isEmptyNode(ci)) {
  22039. ci.innerHTML = "&nbsp;";
  22040. }
  22041. });
  22042. });
  22043. me.addListener("aftergetcontent", function() {
  22044. switchBorderColor(this, true);
  22045. });
  22046. me.addListener("getAllHtml", function() {
  22047. removeSelectedClass(me.document.getElementsByTagName("td"));
  22048. });
  22049. //修正全屏状态下插入的表格宽度在非全屏状态下撑开编辑器的情况
  22050. me.addListener("fullscreenchanged", function(type, fullscreen) {
  22051. if (!fullscreen) {
  22052. var ratio = this.body.offsetWidth / document.body.offsetWidth,
  22053. tables = domUtils.getElementsByTagName(this.body, "table");
  22054. utils.each(tables, function(table) {
  22055. if (table.offsetWidth < me.body.offsetWidth) return false;
  22056. var tds = domUtils.getElementsByTagName(table, "td"),
  22057. backWidths = [];
  22058. utils.each(tds, function(td) {
  22059. backWidths.push(td.offsetWidth);
  22060. });
  22061. for (var i = 0, td; (td = tds[i]); i++) {
  22062. td.setAttribute("width", Math.floor(backWidths[i] * ratio));
  22063. }
  22064. table.setAttribute(
  22065. "width",
  22066. Math.floor(getTableWidth(me, needIEHack, getDefaultValue(me)))
  22067. );
  22068. });
  22069. }
  22070. });
  22071. //重写execCommand命令,用于处理框选时的处理
  22072. var oldExecCommand = me.execCommand;
  22073. me.execCommand = function(cmd, datatat) {
  22074. var me = this,
  22075. args = arguments;
  22076. cmd = cmd.toLowerCase();
  22077. var ut = getUETableBySelected(me),
  22078. tds,
  22079. range = new dom.Range(me.document),
  22080. cmdFun = me.commands[cmd] || UE.commands[cmd],
  22081. result;
  22082. if (!cmdFun) return;
  22083. if (
  22084. ut &&
  22085. !commands[cmd] &&
  22086. !cmdFun.notNeedUndo &&
  22087. !me.__hasEnterExecCommand
  22088. ) {
  22089. me.__hasEnterExecCommand = true;
  22090. me.fireEvent("beforeexeccommand", cmd);
  22091. tds = ut.selectedTds;
  22092. var lastState = -2,
  22093. lastValue = -2,
  22094. value,
  22095. state;
  22096. for (var i = 0, td; (td = tds[i]); i++) {
  22097. if (isEmptyBlock(td)) {
  22098. range.setStart(td, 0).setCursor(false, true);
  22099. } else {
  22100. range.selectNode(td).select(true);
  22101. }
  22102. state = me.queryCommandState(cmd);
  22103. value = me.queryCommandValue(cmd);
  22104. if (state != -1) {
  22105. if (lastState !== state || lastValue !== value) {
  22106. me._ignoreContentChange = true;
  22107. result = oldExecCommand.apply(me, arguments);
  22108. me._ignoreContentChange = false;
  22109. }
  22110. lastState = me.queryCommandState(cmd);
  22111. lastValue = me.queryCommandValue(cmd);
  22112. if (domUtils.isEmptyBlock(td)) {
  22113. domUtils.fillNode(me.document, td);
  22114. }
  22115. }
  22116. }
  22117. range.setStart(tds[0], 0).shrinkBoundary(true).setCursor(false, true);
  22118. me.fireEvent("contentchange");
  22119. me.fireEvent("afterexeccommand", cmd);
  22120. me.__hasEnterExecCommand = false;
  22121. me._selectionChange();
  22122. } else {
  22123. result = oldExecCommand.apply(me, arguments);
  22124. }
  22125. return result;
  22126. };
  22127. });
  22128. /**
  22129. * 删除obj的宽高style,改成属性宽高
  22130. * @param obj
  22131. * @param replaceToProperty
  22132. */
  22133. function removeStyleSize(obj, replaceToProperty) {
  22134. removeStyle(obj, "width", true);
  22135. removeStyle(obj, "height", true);
  22136. }
  22137. function removeStyle(obj, styleName, replaceToProperty) {
  22138. if (obj.style[styleName]) {
  22139. replaceToProperty &&
  22140. obj.setAttribute(styleName, parseInt(obj.style[styleName], 10));
  22141. obj.style[styleName] = "";
  22142. }
  22143. }
  22144. function getParentTdOrTh(ele) {
  22145. if (ele.tagName == "TD" || ele.tagName == "TH") return ele;
  22146. var td;
  22147. if (
  22148. (td =
  22149. domUtils.findParentByTagName(ele, "td", true) ||
  22150. domUtils.findParentByTagName(ele, "th", true))
  22151. )
  22152. return td;
  22153. return null;
  22154. }
  22155. function isEmptyBlock(node) {
  22156. var reg = new RegExp(domUtils.fillChar, "g");
  22157. if (
  22158. node[browser.ie ? "innerText" : "textContent"]
  22159. .replace(/^\s*$/, "")
  22160. .replace(reg, "").length > 0
  22161. ) {
  22162. return 0;
  22163. }
  22164. for (var n in dtd.$isNotEmpty) {
  22165. if (node.getElementsByTagName(n).length) {
  22166. return 0;
  22167. }
  22168. }
  22169. return 1;
  22170. }
  22171. function mouseCoords(evt) {
  22172. if (evt.pageX || evt.pageY) {
  22173. return { x: evt.pageX, y: evt.pageY };
  22174. }
  22175. return {
  22176. x:
  22177. evt.clientX + me.document.body.scrollLeft - me.document.body.clientLeft,
  22178. y: evt.clientY + me.document.body.scrollTop - me.document.body.clientTop
  22179. };
  22180. }
  22181. function mouseMoveEvent(evt) {
  22182. if (isEditorDisabled()) {
  22183. return;
  22184. }
  22185. try {
  22186. //普通状态下鼠标移动
  22187. var target = getParentTdOrTh(evt.target || evt.srcElement),
  22188. pos;
  22189. //区分用户的行为是拖动还是双击
  22190. if (isInResizeBuffer) {
  22191. me.body.style.webkitUserSelect = "none";
  22192. if (
  22193. Math.abs(userActionStatus.x - evt.clientX) > offsetOfTableCell ||
  22194. Math.abs(userActionStatus.y - evt.clientY) > offsetOfTableCell
  22195. ) {
  22196. clearTableDragTimer();
  22197. isInResizeBuffer = false;
  22198. singleClickState = 0;
  22199. //drag action
  22200. tableBorderDrag(evt);
  22201. }
  22202. }
  22203. //修改单元格大小时的鼠标移动
  22204. if (onDrag && dragTd) {
  22205. singleClickState = 0;
  22206. me.body.style.webkitUserSelect = "none";
  22207. me.selection.getNative()[
  22208. browser.ie9below ? "empty" : "removeAllRanges"
  22209. ]();
  22210. pos = mouseCoords(evt);
  22211. toggleDraggableState(me, true, onDrag, pos, target);
  22212. if (onDrag == "h") {
  22213. dragLine.style.left = getPermissionX(dragTd, evt) + "px";
  22214. } else if (onDrag == "v") {
  22215. dragLine.style.top = getPermissionY(dragTd, evt) + "px";
  22216. }
  22217. return;
  22218. }
  22219. //当鼠标处于table上时,修改移动过程中的光标状态
  22220. if (target) {
  22221. //针对使用table作为容器的组件不触发拖拽效果
  22222. if (me.fireEvent("excludetable", target) === true) return;
  22223. pos = mouseCoords(evt);
  22224. var state = getRelation(target, pos),
  22225. table = domUtils.findParentByTagName(target, "table", true);
  22226. if (inTableSide(table, target, evt, true)) {
  22227. if (me.fireEvent("excludetable", table) === true) return;
  22228. me.body.style.cursor =
  22229. "url(" + me.options.cursorpath + "h.png),pointer";
  22230. } else if (inTableSide(table, target, evt)) {
  22231. if (me.fireEvent("excludetable", table) === true) return;
  22232. me.body.style.cursor =
  22233. "url(" + me.options.cursorpath + "v.png),pointer";
  22234. } else {
  22235. me.body.style.cursor = "text";
  22236. var curCell = target;
  22237. if (/\d/.test(state)) {
  22238. state = state.replace(/\d/, "");
  22239. target = getUETable(target).getPreviewCell(target, state == "v");
  22240. }
  22241. //位于第一行的顶部或者第一列的左边时不可拖动
  22242. toggleDraggableState(
  22243. me,
  22244. target ? !!state : false,
  22245. target ? state : "",
  22246. pos,
  22247. target
  22248. );
  22249. }
  22250. } else {
  22251. toggleDragButton(false, table, me);
  22252. }
  22253. } catch (e) {
  22254. showError(e);
  22255. }
  22256. }
  22257. var dragButtonTimer;
  22258. function toggleDragButton(show, table, editor) {
  22259. if (!show) {
  22260. if (dragOver) return;
  22261. dragButtonTimer = setTimeout(function() {
  22262. !dragOver &&
  22263. dragButton &&
  22264. dragButton.parentNode &&
  22265. dragButton.parentNode.removeChild(dragButton);
  22266. }, 2000);
  22267. } else {
  22268. createDragButton(table, editor);
  22269. }
  22270. }
  22271. function createDragButton(table, editor) {
  22272. var pos = domUtils.getXY(table),
  22273. doc = table.ownerDocument;
  22274. if (dragButton && dragButton.parentNode) return dragButton;
  22275. dragButton = doc.createElement("div");
  22276. dragButton.contentEditable = false;
  22277. dragButton.innerHTML = "";
  22278. dragButton.style.cssText =
  22279. "width:15px;height:15px;background-image:url(" +
  22280. editor.options.UEDITOR_HOME_URL +
  22281. "dialogs/table/dragicon.png);position: absolute;cursor:move;top:" +
  22282. (pos.y - 15) +
  22283. "px;left:" +
  22284. pos.x +
  22285. "px;";
  22286. domUtils.unSelectable(dragButton);
  22287. dragButton.onmouseover = function(evt) {
  22288. dragOver = true;
  22289. };
  22290. dragButton.onmouseout = function(evt) {
  22291. dragOver = false;
  22292. };
  22293. domUtils.on(dragButton, "click", function(type, evt) {
  22294. doClick(evt, this);
  22295. });
  22296. domUtils.on(dragButton, "dblclick", function(type, evt) {
  22297. doDblClick(evt);
  22298. });
  22299. domUtils.on(dragButton, "dragstart", function(type, evt) {
  22300. domUtils.preventDefault(evt);
  22301. });
  22302. var timer;
  22303. function doClick(evt, button) {
  22304. // 部分浏览器下需要清理
  22305. clearTimeout(timer);
  22306. timer = setTimeout(function() {
  22307. editor.fireEvent("tableClicked", table, button);
  22308. }, 300);
  22309. }
  22310. function doDblClick(evt) {
  22311. clearTimeout(timer);
  22312. var ut = getUETable(table),
  22313. start = table.rows[0].cells[0],
  22314. end = ut.getLastCell(),
  22315. range = ut.getCellsRange(start, end);
  22316. editor.selection.getRange().setStart(start, 0).setCursor(false, true);
  22317. ut.setSelected(range);
  22318. }
  22319. doc.body.appendChild(dragButton);
  22320. }
  22321. // function inPosition(table, pos) {
  22322. // var tablePos = domUtils.getXY(table),
  22323. // width = table.offsetWidth,
  22324. // height = table.offsetHeight;
  22325. // if (pos.x - tablePos.x < 5 && pos.y - tablePos.y < 5) {
  22326. // return "topLeft";
  22327. // } else if (tablePos.x + width - pos.x < 5 && tablePos.y + height - pos.y < 5) {
  22328. // return "bottomRight";
  22329. // }
  22330. // }
  22331. function inTableSide(table, cell, evt, top) {
  22332. var pos = mouseCoords(evt),
  22333. state = getRelation(cell, pos);
  22334. if (top) {
  22335. var caption = table.getElementsByTagName("caption")[0],
  22336. capHeight = caption ? caption.offsetHeight : 0;
  22337. return state == "v1" && pos.y - domUtils.getXY(table).y - capHeight < 8;
  22338. } else {
  22339. return state == "h1" && pos.x - domUtils.getXY(table).x < 8;
  22340. }
  22341. }
  22342. /**
  22343. * 获取拖动时允许的X轴坐标
  22344. * @param dragTd
  22345. * @param evt
  22346. */
  22347. function getPermissionX(dragTd, evt) {
  22348. var ut = getUETable(dragTd);
  22349. if (ut) {
  22350. var preTd = ut.getSameEndPosCells(dragTd, "x")[0],
  22351. nextTd = ut.getSameStartPosXCells(dragTd)[0],
  22352. mouseX = mouseCoords(evt).x,
  22353. left =
  22354. (preTd ? domUtils.getXY(preTd).x : domUtils.getXY(ut.table).x) + 20,
  22355. right = nextTd
  22356. ? domUtils.getXY(nextTd).x + nextTd.offsetWidth - 20
  22357. : me.body.offsetWidth + 5 ||
  22358. parseInt(domUtils.getComputedStyle(me.body, "width"), 10);
  22359. left += cellMinWidth;
  22360. right -= cellMinWidth;
  22361. return mouseX < left ? left : mouseX > right ? right : mouseX;
  22362. }
  22363. }
  22364. /**
  22365. * 获取拖动时允许的Y轴坐标
  22366. */
  22367. function getPermissionY(dragTd, evt) {
  22368. try {
  22369. var top = domUtils.getXY(dragTd).y,
  22370. mousePosY = mouseCoords(evt).y;
  22371. return mousePosY < top ? top : mousePosY;
  22372. } catch (e) {
  22373. showError(e);
  22374. }
  22375. }
  22376. /**
  22377. * 移动状态切换
  22378. */
  22379. function toggleDraggableState(editor, draggable, dir, mousePos, cell) {
  22380. try {
  22381. editor.body.style.cursor = dir == "h"
  22382. ? "col-resize"
  22383. : dir == "v" ? "row-resize" : "text";
  22384. if (browser.ie) {
  22385. if (dir && !mousedown && !getUETableBySelected(editor)) {
  22386. getDragLine(editor, editor.document);
  22387. showDragLineAt(dir, cell);
  22388. } else {
  22389. hideDragLine(editor);
  22390. }
  22391. }
  22392. onBorder = draggable;
  22393. } catch (e) {
  22394. showError(e);
  22395. }
  22396. }
  22397. /**
  22398. * 获取与UETable相关的resize line
  22399. * @param uetable UETable对象
  22400. */
  22401. function getResizeLineByUETable() {
  22402. var lineId = "_UETableResizeLine",
  22403. line = this.document.getElementById(lineId);
  22404. if (!line) {
  22405. line = this.document.createElement("div");
  22406. line.id = lineId;
  22407. line.contnetEditable = false;
  22408. line.setAttribute("unselectable", "on");
  22409. var styles = {
  22410. width: 2 * cellBorderWidth + 1 + "px",
  22411. position: "absolute",
  22412. "z-index": 100000,
  22413. cursor: "col-resize",
  22414. background: "red",
  22415. display: "none"
  22416. };
  22417. //切换状态
  22418. line.onmouseout = function() {
  22419. this.style.display = "none";
  22420. };
  22421. utils.extend(line.style, styles);
  22422. this.document.body.appendChild(line);
  22423. }
  22424. return line;
  22425. }
  22426. /**
  22427. * 更新resize-line
  22428. */
  22429. function updateResizeLine(cell, uetable) {
  22430. var line = getResizeLineByUETable.call(this),
  22431. table = uetable.table,
  22432. styles = {
  22433. top: domUtils.getXY(table).y + "px",
  22434. left:
  22435. domUtils.getXY(cell).x + cell.offsetWidth - cellBorderWidth + "px",
  22436. display: "block",
  22437. height: table.offsetHeight + "px"
  22438. };
  22439. utils.extend(line.style, styles);
  22440. }
  22441. /**
  22442. * 显示resize-line
  22443. */
  22444. function showResizeLine(cell) {
  22445. var uetable = getUETable(cell);
  22446. updateResizeLine.call(this, cell, uetable);
  22447. }
  22448. /**
  22449. * 获取鼠标与当前单元格的相对位置
  22450. * @param ele
  22451. * @param mousePos
  22452. */
  22453. function getRelation(ele, mousePos) {
  22454. var elePos = domUtils.getXY(ele);
  22455. if (!elePos) {
  22456. return "";
  22457. }
  22458. if (elePos.x + ele.offsetWidth - mousePos.x < cellBorderWidth) {
  22459. return "h";
  22460. }
  22461. if (mousePos.x - elePos.x < cellBorderWidth) {
  22462. return "h1";
  22463. }
  22464. if (elePos.y + ele.offsetHeight - mousePos.y < cellBorderWidth) {
  22465. return "v";
  22466. }
  22467. if (mousePos.y - elePos.y < cellBorderWidth) {
  22468. return "v1";
  22469. }
  22470. return "";
  22471. }
  22472. function mouseDownEvent(type, evt) {
  22473. if (isEditorDisabled()) {
  22474. return;
  22475. }
  22476. userActionStatus = {
  22477. x: evt.clientX,
  22478. y: evt.clientY
  22479. };
  22480. //右键菜单单独处理
  22481. if (evt.button == 2) {
  22482. var ut = getUETableBySelected(me),
  22483. flag = false;
  22484. if (ut) {
  22485. var td = getTargetTd(me, evt);
  22486. utils.each(ut.selectedTds, function(ti) {
  22487. if (ti === td) {
  22488. flag = true;
  22489. }
  22490. });
  22491. if (!flag) {
  22492. removeSelectedClass(domUtils.getElementsByTagName(me.body, "th td"));
  22493. ut.clearSelected();
  22494. } else {
  22495. td = ut.selectedTds[0];
  22496. setTimeout(function() {
  22497. me.selection.getRange().setStart(td, 0).setCursor(false, true);
  22498. }, 0);
  22499. }
  22500. }
  22501. } else {
  22502. tableClickHander(evt);
  22503. }
  22504. }
  22505. //清除表格的计时器
  22506. function clearTableTimer() {
  22507. tabTimer && clearTimeout(tabTimer);
  22508. tabTimer = null;
  22509. }
  22510. //双击收缩
  22511. function tableDbclickHandler(evt) {
  22512. singleClickState = 0;
  22513. evt = evt || me.window.event;
  22514. var target = getParentTdOrTh(evt.target || evt.srcElement);
  22515. if (target) {
  22516. var h;
  22517. if ((h = getRelation(target, mouseCoords(evt)))) {
  22518. hideDragLine(me);
  22519. if (h == "h1") {
  22520. h = "h";
  22521. if (
  22522. inTableSide(
  22523. domUtils.findParentByTagName(target, "table"),
  22524. target,
  22525. evt
  22526. )
  22527. ) {
  22528. me.execCommand("adaptbywindow");
  22529. } else {
  22530. target = getUETable(target).getPreviewCell(target);
  22531. if (target) {
  22532. var rng = me.selection.getRange();
  22533. rng.selectNodeContents(target).setCursor(true, true);
  22534. }
  22535. }
  22536. }
  22537. if (h == "h") {
  22538. var ut = getUETable(target),
  22539. table = ut.table,
  22540. cells = getCellsByMoveBorder(target, table, true);
  22541. cells = extractArray(cells, "left");
  22542. ut.width = ut.offsetWidth;
  22543. var oldWidth = [],
  22544. newWidth = [];
  22545. utils.each(cells, function(cell) {
  22546. oldWidth.push(cell.offsetWidth);
  22547. });
  22548. utils.each(cells, function(cell) {
  22549. cell.removeAttribute("width");
  22550. });
  22551. window.setTimeout(function() {
  22552. //是否允许改变
  22553. var changeable = true;
  22554. utils.each(cells, function(cell, index) {
  22555. var width = cell.offsetWidth;
  22556. if (width > oldWidth[index]) {
  22557. changeable = false;
  22558. return false;
  22559. }
  22560. newWidth.push(width);
  22561. });
  22562. var change = changeable ? newWidth : oldWidth;
  22563. utils.each(cells, function(cell, index) {
  22564. cell.width = change[index] - getTabcellSpace();
  22565. });
  22566. }, 0);
  22567. // minWidth -= cellMinWidth;
  22568. //
  22569. // table.removeAttribute("width");
  22570. // utils.each(cells, function (cell) {
  22571. // cell.style.width = "";
  22572. // cell.width -= minWidth;
  22573. // });
  22574. }
  22575. }
  22576. }
  22577. }
  22578. function tableClickHander(evt) {
  22579. removeSelectedClass(domUtils.getElementsByTagName(me.body, "td th"));
  22580. //trace:3113
  22581. //选中单元格,点击table外部,不会清掉table上挂的ueTable,会引起getUETableBySelected方法返回值
  22582. utils.each(me.document.getElementsByTagName("table"), function(t) {
  22583. t.ueTable = null;
  22584. });
  22585. startTd = getTargetTd(me, evt);
  22586. if (!startTd) return;
  22587. var table = domUtils.findParentByTagName(startTd, "table", true);
  22588. ut = getUETable(table);
  22589. ut && ut.clearSelected();
  22590. //判断当前鼠标状态
  22591. if (!onBorder) {
  22592. me.document.body.style.webkitUserSelect = "";
  22593. mousedown = true;
  22594. me.addListener("mouseover", mouseOverEvent);
  22595. } else {
  22596. //边框上的动作处理
  22597. borderActionHandler(evt);
  22598. }
  22599. }
  22600. //处理表格边框上的动作, 这里做延时处理,避免两种动作互相影响
  22601. function borderActionHandler(evt) {
  22602. if (browser.ie) {
  22603. evt = reconstruct(evt);
  22604. }
  22605. clearTableDragTimer();
  22606. //是否正在等待resize的缓冲中
  22607. isInResizeBuffer = true;
  22608. tableDragTimer = setTimeout(function() {
  22609. tableBorderDrag(evt);
  22610. }, dblclickTime);
  22611. }
  22612. function extractArray(originArr, key) {
  22613. var result = [],
  22614. tmp = null;
  22615. for (var i = 0, len = originArr.length; i < len; i++) {
  22616. tmp = originArr[i][key];
  22617. if (tmp) {
  22618. result.push(tmp);
  22619. }
  22620. }
  22621. return result;
  22622. }
  22623. function clearTableDragTimer() {
  22624. tableDragTimer && clearTimeout(tableDragTimer);
  22625. tableDragTimer = null;
  22626. }
  22627. function reconstruct(obj) {
  22628. var attrs = [
  22629. "pageX",
  22630. "pageY",
  22631. "clientX",
  22632. "clientY",
  22633. "srcElement",
  22634. "target"
  22635. ],
  22636. newObj = {};
  22637. if (obj) {
  22638. for (var i = 0, key, val; (key = attrs[i]); i++) {
  22639. val = obj[key];
  22640. val && (newObj[key] = val);
  22641. }
  22642. }
  22643. return newObj;
  22644. }
  22645. //边框拖动
  22646. function tableBorderDrag(evt) {
  22647. isInResizeBuffer = false;
  22648. startTd = evt.target || evt.srcElement;
  22649. if (!startTd) return;
  22650. var state = getRelation(startTd, mouseCoords(evt));
  22651. if (/\d/.test(state)) {
  22652. state = state.replace(/\d/, "");
  22653. startTd = getUETable(startTd).getPreviewCell(startTd, state == "v");
  22654. }
  22655. hideDragLine(me);
  22656. getDragLine(me, me.document);
  22657. me.fireEvent("saveScene");
  22658. showDragLineAt(state, startTd);
  22659. mousedown = true;
  22660. //拖动开始
  22661. onDrag = state;
  22662. dragTd = startTd;
  22663. }
  22664. function mouseUpEvent(type, evt) {
  22665. if (isEditorDisabled()) {
  22666. return;
  22667. }
  22668. clearTableDragTimer();
  22669. isInResizeBuffer = false;
  22670. if (onBorder) {
  22671. singleClickState = ++singleClickState % 3;
  22672. userActionStatus = {
  22673. x: evt.clientX,
  22674. y: evt.clientY
  22675. };
  22676. tableResizeTimer = setTimeout(function() {
  22677. singleClickState > 0 && singleClickState--;
  22678. }, dblclickTime);
  22679. if (singleClickState === 2) {
  22680. singleClickState = 0;
  22681. tableDbclickHandler(evt);
  22682. return;
  22683. }
  22684. }
  22685. if (evt.button == 2) return;
  22686. var me = this;
  22687. //清除表格上原生跨选问题
  22688. var range = me.selection.getRange(),
  22689. start = domUtils.findParentByTagName(range.startContainer, "table", true),
  22690. end = domUtils.findParentByTagName(range.endContainer, "table", true);
  22691. if (start || end) {
  22692. if (start === end) {
  22693. start = domUtils.findParentByTagName(
  22694. range.startContainer,
  22695. ["td", "th", "caption"],
  22696. true
  22697. );
  22698. end = domUtils.findParentByTagName(
  22699. range.endContainer,
  22700. ["td", "th", "caption"],
  22701. true
  22702. );
  22703. if (start !== end) {
  22704. me.selection.clearRange();
  22705. }
  22706. } else {
  22707. me.selection.clearRange();
  22708. }
  22709. }
  22710. mousedown = false;
  22711. me.document.body.style.webkitUserSelect = "";
  22712. //拖拽状态下的mouseUP
  22713. if (onDrag && dragTd) {
  22714. me.selection.getNative()[
  22715. browser.ie9below ? "empty" : "removeAllRanges"
  22716. ]();
  22717. singleClickState = 0;
  22718. dragLine = me.document.getElementById("ue_tableDragLine");
  22719. // trace 3973
  22720. if (dragLine) {
  22721. var dragTdPos = domUtils.getXY(dragTd),
  22722. dragLinePos = domUtils.getXY(dragLine);
  22723. switch (onDrag) {
  22724. case "h":
  22725. changeColWidth(dragTd, dragLinePos.x - dragTdPos.x);
  22726. break;
  22727. case "v":
  22728. changeRowHeight(
  22729. dragTd,
  22730. dragLinePos.y - dragTdPos.y - dragTd.offsetHeight
  22731. );
  22732. break;
  22733. default:
  22734. }
  22735. onDrag = "";
  22736. dragTd = null;
  22737. hideDragLine(me);
  22738. me.fireEvent("saveScene");
  22739. return;
  22740. }
  22741. }
  22742. //正常状态下的mouseup
  22743. if (!startTd) {
  22744. var target = domUtils.findParentByTagName(
  22745. evt.target || evt.srcElement,
  22746. "td",
  22747. true
  22748. );
  22749. if (!target)
  22750. target = domUtils.findParentByTagName(
  22751. evt.target || evt.srcElement,
  22752. "th",
  22753. true
  22754. );
  22755. if (target && (target.tagName == "TD" || target.tagName == "TH")) {
  22756. if (me.fireEvent("excludetable", target) === true) return;
  22757. range = new dom.Range(me.document);
  22758. range.setStart(target, 0).setCursor(false, true);
  22759. }
  22760. } else {
  22761. var ut = getUETable(startTd),
  22762. cell = ut ? ut.selectedTds[0] : null;
  22763. if (cell) {
  22764. range = new dom.Range(me.document);
  22765. if (domUtils.isEmptyBlock(cell)) {
  22766. range.setStart(cell, 0).setCursor(false, true);
  22767. } else {
  22768. range
  22769. .selectNodeContents(cell)
  22770. .shrinkBoundary()
  22771. .setCursor(false, true);
  22772. }
  22773. } else {
  22774. range = me.selection.getRange().shrinkBoundary();
  22775. if (!range.collapsed) {
  22776. var start = domUtils.findParentByTagName(
  22777. range.startContainer,
  22778. ["td", "th"],
  22779. true
  22780. ),
  22781. end = domUtils.findParentByTagName(
  22782. range.endContainer,
  22783. ["td", "th"],
  22784. true
  22785. );
  22786. //在table里边的不能清除
  22787. if (
  22788. (start && !end) ||
  22789. (!start && end) ||
  22790. (start && end && start !== end)
  22791. ) {
  22792. range.setCursor(false, true);
  22793. }
  22794. }
  22795. }
  22796. startTd = null;
  22797. me.removeListener("mouseover", mouseOverEvent);
  22798. }
  22799. me._selectionChange(250, evt);
  22800. }
  22801. function mouseOverEvent(type, evt) {
  22802. if (isEditorDisabled()) {
  22803. return;
  22804. }
  22805. var me = this,
  22806. tar = evt.target || evt.srcElement;
  22807. currentTd =
  22808. domUtils.findParentByTagName(tar, "td", true) ||
  22809. domUtils.findParentByTagName(tar, "th", true);
  22810. //需要判断两个TD是否位于同一个表格内
  22811. if (
  22812. startTd &&
  22813. currentTd &&
  22814. ((startTd.tagName == "TD" && currentTd.tagName == "TD") ||
  22815. (startTd.tagName == "TH" && currentTd.tagName == "TH")) &&
  22816. domUtils.findParentByTagName(startTd, "table") ==
  22817. domUtils.findParentByTagName(currentTd, "table")
  22818. ) {
  22819. var ut = getUETable(currentTd);
  22820. if (startTd != currentTd) {
  22821. me.document.body.style.webkitUserSelect = "none";
  22822. me.selection.getNative()[
  22823. browser.ie9below ? "empty" : "removeAllRanges"
  22824. ]();
  22825. var range = ut.getCellsRange(startTd, currentTd);
  22826. ut.setSelected(range);
  22827. } else {
  22828. me.document.body.style.webkitUserSelect = "";
  22829. ut.clearSelected();
  22830. }
  22831. }
  22832. evt.preventDefault ? evt.preventDefault() : (evt.returnValue = false);
  22833. }
  22834. function setCellHeight(cell, height, backHeight) {
  22835. var lineHight = parseInt(
  22836. domUtils.getComputedStyle(cell, "line-height"),
  22837. 10
  22838. ),
  22839. tmpHeight = backHeight + height;
  22840. height = tmpHeight < lineHight ? lineHight : tmpHeight;
  22841. if (cell.style.height) cell.style.height = "";
  22842. cell.rowSpan == 1
  22843. ? cell.setAttribute("height", height)
  22844. : cell.removeAttribute && cell.removeAttribute("height");
  22845. }
  22846. function getWidth(cell) {
  22847. if (!cell) return 0;
  22848. return parseInt(domUtils.getComputedStyle(cell, "width"), 10);
  22849. }
  22850. function changeColWidth(cell, changeValue) {
  22851. var ut = getUETable(cell);
  22852. if (ut) {
  22853. //根据当前移动的边框获取相关的单元格
  22854. var table = ut.table,
  22855. cells = getCellsByMoveBorder(cell, table);
  22856. table.style.width = "";
  22857. table.removeAttribute("width");
  22858. //修正改变量
  22859. changeValue = correctChangeValue(changeValue, cell, cells);
  22860. if (cell.nextSibling) {
  22861. var i = 0;
  22862. utils.each(cells, function(cellGroup) {
  22863. cellGroup.left.width = +cellGroup.left.width + changeValue;
  22864. cellGroup.right &&
  22865. (cellGroup.right.width = +cellGroup.right.width - changeValue);
  22866. });
  22867. } else {
  22868. utils.each(cells, function(cellGroup) {
  22869. cellGroup.left.width -= -changeValue;
  22870. });
  22871. }
  22872. }
  22873. }
  22874. function isEditorDisabled() {
  22875. return me.body.contentEditable === "false";
  22876. }
  22877. function changeRowHeight(td, changeValue) {
  22878. if (Math.abs(changeValue) < 10) return;
  22879. var ut = getUETable(td);
  22880. if (ut) {
  22881. var cells = ut.getSameEndPosCells(td, "y"),
  22882. //备份需要连带变化的td的原始高度,否则后期无法获取正确的值
  22883. backHeight = cells[0] ? cells[0].offsetHeight : 0;
  22884. for (var i = 0, cell; (cell = cells[i++]); ) {
  22885. setCellHeight(cell, changeValue, backHeight);
  22886. }
  22887. }
  22888. }
  22889. /**
  22890. * 获取调整单元格大小的相关单元格
  22891. * @isContainMergeCell 返回的结果中是否包含发生合并后的单元格
  22892. */
  22893. function getCellsByMoveBorder(cell, table, isContainMergeCell) {
  22894. if (!table) {
  22895. table = domUtils.findParentByTagName(cell, "table");
  22896. }
  22897. if (!table) {
  22898. return null;
  22899. }
  22900. //获取到该单元格所在行的序列号
  22901. var index = domUtils.getNodeIndex(cell),
  22902. temp = cell,
  22903. rows = table.rows,
  22904. colIndex = 0;
  22905. while (temp) {
  22906. //获取到当前单元格在未发生单元格合并时的序列
  22907. if (temp.nodeType === 1) {
  22908. colIndex += temp.colSpan || 1;
  22909. }
  22910. temp = temp.previousSibling;
  22911. }
  22912. temp = null;
  22913. //记录想关的单元格
  22914. var borderCells = [];
  22915. utils.each(rows, function(tabRow) {
  22916. var cells = tabRow.cells,
  22917. currIndex = 0;
  22918. utils.each(cells, function(tabCell) {
  22919. currIndex += tabCell.colSpan || 1;
  22920. if (currIndex === colIndex) {
  22921. borderCells.push({
  22922. left: tabCell,
  22923. right: tabCell.nextSibling || null
  22924. });
  22925. return false;
  22926. } else if (currIndex > colIndex) {
  22927. if (isContainMergeCell) {
  22928. borderCells.push({
  22929. left: tabCell
  22930. });
  22931. }
  22932. return false;
  22933. }
  22934. });
  22935. });
  22936. return borderCells;
  22937. }
  22938. /**
  22939. * 通过给定的单元格集合获取最小的单元格width
  22940. */
  22941. function getMinWidthByTableCells(cells) {
  22942. var minWidth = Number.MAX_VALUE;
  22943. for (var i = 0, curCell; (curCell = cells[i]); i++) {
  22944. minWidth = Math.min(
  22945. minWidth,
  22946. curCell.width || getTableCellWidth(curCell)
  22947. );
  22948. }
  22949. return minWidth;
  22950. }
  22951. function correctChangeValue(changeValue, relatedCell, cells) {
  22952. //为单元格的paading预留空间
  22953. changeValue -= getTabcellSpace();
  22954. if (changeValue < 0) {
  22955. return 0;
  22956. }
  22957. changeValue -= getTableCellWidth(relatedCell);
  22958. //确定方向
  22959. var direction = changeValue < 0 ? "left" : "right";
  22960. changeValue = Math.abs(changeValue);
  22961. //只关心非最后一个单元格就可以
  22962. utils.each(cells, function(cellGroup) {
  22963. var curCell = cellGroup[direction];
  22964. //为单元格保留最小空间
  22965. if (curCell) {
  22966. changeValue = Math.min(
  22967. changeValue,
  22968. getTableCellWidth(curCell) - cellMinWidth
  22969. );
  22970. }
  22971. });
  22972. //修正越界
  22973. changeValue = changeValue < 0 ? 0 : changeValue;
  22974. return direction === "left" ? -changeValue : changeValue;
  22975. }
  22976. function getTableCellWidth(cell) {
  22977. var width = 0,
  22978. //偏移纠正量
  22979. offset = 0,
  22980. width = cell.offsetWidth - getTabcellSpace();
  22981. //最后一个节点纠正一下
  22982. if (!cell.nextSibling) {
  22983. width -= getTableCellOffset(cell);
  22984. }
  22985. width = width < 0 ? 0 : width;
  22986. try {
  22987. cell.width = width;
  22988. } catch (e) {}
  22989. return width;
  22990. }
  22991. /**
  22992. * 获取单元格所在表格的最末单元格的偏移量
  22993. */
  22994. function getTableCellOffset(cell) {
  22995. tab = domUtils.findParentByTagName(cell, "table", false);
  22996. if (tab.offsetVal === undefined) {
  22997. var prev = cell.previousSibling;
  22998. if (prev) {
  22999. //最后一个单元格和前一个单元格的width diff结果 如果恰好为一个border width, 则条件成立
  23000. tab.offsetVal = cell.offsetWidth - prev.offsetWidth === UT.borderWidth
  23001. ? UT.borderWidth
  23002. : 0;
  23003. } else {
  23004. tab.offsetVal = 0;
  23005. }
  23006. }
  23007. return tab.offsetVal;
  23008. }
  23009. function getTabcellSpace() {
  23010. if (UT.tabcellSpace === undefined) {
  23011. var cell = null,
  23012. tab = me.document.createElement("table"),
  23013. tbody = me.document.createElement("tbody"),
  23014. trow = me.document.createElement("tr"),
  23015. tabcell = me.document.createElement("td"),
  23016. mirror = null;
  23017. tabcell.style.cssText = "border: 0;";
  23018. tabcell.width = 1;
  23019. trow.appendChild(tabcell);
  23020. trow.appendChild((mirror = tabcell.cloneNode(false)));
  23021. tbody.appendChild(trow);
  23022. tab.appendChild(tbody);
  23023. tab.style.cssText = "visibility: hidden;";
  23024. me.body.appendChild(tab);
  23025. UT.paddingSpace = tabcell.offsetWidth - 1;
  23026. var tmpTabWidth = tab.offsetWidth;
  23027. tabcell.style.cssText = "";
  23028. mirror.style.cssText = "";
  23029. UT.borderWidth = (tab.offsetWidth - tmpTabWidth) / 3;
  23030. UT.tabcellSpace = UT.paddingSpace + UT.borderWidth;
  23031. me.body.removeChild(tab);
  23032. }
  23033. getTabcellSpace = function() {
  23034. return UT.tabcellSpace;
  23035. };
  23036. return UT.tabcellSpace;
  23037. }
  23038. function getDragLine(editor, doc) {
  23039. if (mousedown) return;
  23040. dragLine = editor.document.createElement("div");
  23041. domUtils.setAttributes(dragLine, {
  23042. id: "ue_tableDragLine",
  23043. unselectable: "on",
  23044. contenteditable: false,
  23045. onresizestart: "return false",
  23046. ondragstart: "return false",
  23047. onselectstart: "return false",
  23048. style:
  23049. "background-color:blue;position:absolute;padding:0;margin:0;background-image:none;border:0px none;opacity:0;filter:alpha(opacity=0)"
  23050. });
  23051. editor.body.appendChild(dragLine);
  23052. }
  23053. function hideDragLine(editor) {
  23054. if (mousedown) return;
  23055. var line;
  23056. while ((line = editor.document.getElementById("ue_tableDragLine"))) {
  23057. domUtils.remove(line);
  23058. }
  23059. }
  23060. /**
  23061. * 依据state(v|h)在cell位置显示横线
  23062. * @param state
  23063. * @param cell
  23064. */
  23065. function showDragLineAt(state, cell) {
  23066. if (!cell) return;
  23067. var table = domUtils.findParentByTagName(cell, "table"),
  23068. caption = table.getElementsByTagName("caption"),
  23069. width = table.offsetWidth,
  23070. height =
  23071. table.offsetHeight - (caption.length > 0 ? caption[0].offsetHeight : 0),
  23072. tablePos = domUtils.getXY(table),
  23073. cellPos = domUtils.getXY(cell),
  23074. css;
  23075. switch (state) {
  23076. case "h":
  23077. css =
  23078. "height:" +
  23079. height +
  23080. "px;top:" +
  23081. (tablePos.y + (caption.length > 0 ? caption[0].offsetHeight : 0)) +
  23082. "px;left:" +
  23083. (cellPos.x + cell.offsetWidth);
  23084. dragLine.style.cssText =
  23085. css +
  23086. "px;position: absolute;display:block;background-color:blue;width:1px;border:0; color:blue;opacity:.3;filter:alpha(opacity=30)";
  23087. break;
  23088. case "v":
  23089. css =
  23090. "width:" +
  23091. width +
  23092. "px;left:" +
  23093. tablePos.x +
  23094. "px;top:" +
  23095. (cellPos.y + cell.offsetHeight);
  23096. //必须加上border:0和color:blue,否则低版ie不支持背景色显示
  23097. dragLine.style.cssText =
  23098. css +
  23099. "px;overflow:hidden;position: absolute;display:block;background-color:blue;height:1px;border:0;color:blue;opacity:.2;filter:alpha(opacity=20)";
  23100. break;
  23101. default:
  23102. }
  23103. }
  23104. /**
  23105. * 当表格边框颜色为白色时设置为虚线,true为添加虚线
  23106. * @param editor
  23107. * @param flag
  23108. */
  23109. function switchBorderColor(editor, flag) {
  23110. var tableArr = domUtils.getElementsByTagName(editor.body, "table"),
  23111. color;
  23112. for (var i = 0, node; (node = tableArr[i++]); ) {
  23113. var td = domUtils.getElementsByTagName(node, "td");
  23114. if (td[0]) {
  23115. if (flag) {
  23116. color = td[0].style.borderColor.replace(/\s/g, "");
  23117. if (/(#ffffff)|(rgb\(255,255,255\))/gi.test(color))
  23118. domUtils.addClass(node, "noBorderTable");
  23119. } else {
  23120. domUtils.removeClasses(node, "noBorderTable");
  23121. }
  23122. }
  23123. }
  23124. }
  23125. function getTableWidth(editor, needIEHack, defaultValue) {
  23126. var body = editor.body;
  23127. return (
  23128. body.offsetWidth -
  23129. (needIEHack
  23130. ? parseInt(domUtils.getComputedStyle(body, "margin-left"), 10) * 2
  23131. : 0) -
  23132. defaultValue.tableBorder * 2 -
  23133. (editor.options.offsetWidth || 0)
  23134. );
  23135. }
  23136. /**
  23137. * 获取当前拖动的单元格
  23138. */
  23139. function getTargetTd(editor, evt) {
  23140. var target = domUtils.findParentByTagName(
  23141. evt.target || evt.srcElement,
  23142. ["td", "th"],
  23143. true
  23144. ),
  23145. dir = null;
  23146. if (!target) {
  23147. return null;
  23148. }
  23149. dir = getRelation(target, mouseCoords(evt));
  23150. //如果有前一个节点, 需要做一个修正, 否则可能会得到一个错误的td
  23151. if (!target) {
  23152. return null;
  23153. }
  23154. if (dir === "h1" && target.previousSibling) {
  23155. var position = domUtils.getXY(target),
  23156. cellWidth = target.offsetWidth;
  23157. if (Math.abs(position.x + cellWidth - evt.clientX) > cellWidth / 3) {
  23158. target = target.previousSibling;
  23159. }
  23160. } else if (dir === "v1" && target.parentNode.previousSibling) {
  23161. var position = domUtils.getXY(target),
  23162. cellHeight = target.offsetHeight;
  23163. if (Math.abs(position.y + cellHeight - evt.clientY) > cellHeight / 3) {
  23164. target = target.parentNode.previousSibling.firstChild;
  23165. }
  23166. }
  23167. //排除了非td内部以及用于代码高亮部分的td
  23168. return target && !(editor.fireEvent("excludetable", target) === true)
  23169. ? target
  23170. : null;
  23171. }
  23172. };
  23173. // plugins/table.sort.js
  23174. /**
  23175. * Created with JetBrains PhpStorm.
  23176. * User: Jinqn
  23177. * Date: 13-10-12
  23178. * Time: 上午10:20
  23179. * To change this template use File | Settings | File Templates.
  23180. */
  23181. UE.UETable.prototype.sortTable = function(sortByCellIndex, compareFn) {
  23182. var table = this.table,
  23183. rows = table.rows,
  23184. trArray = [],
  23185. flag = rows[0].cells[0].tagName === "TH",
  23186. lastRowIndex = 0;
  23187. if (this.selectedTds.length) {
  23188. var range = this.cellsRange,
  23189. len = range.endRowIndex + 1;
  23190. for (var i = range.beginRowIndex; i < len; i++) {
  23191. trArray[i] = rows[i];
  23192. }
  23193. trArray.splice(0, range.beginRowIndex);
  23194. lastRowIndex = range.endRowIndex + 1 === this.rowsNum
  23195. ? 0
  23196. : range.endRowIndex + 1;
  23197. } else {
  23198. for (var i = 0, len = rows.length; i < len; i++) {
  23199. trArray[i] = rows[i];
  23200. }
  23201. }
  23202. var Fn = {
  23203. reversecurrent: function(td1, td2) {
  23204. return 1;
  23205. },
  23206. orderbyasc: function(td1, td2) {
  23207. var value1 = td1.innerText || td1.textContent,
  23208. value2 = td2.innerText || td2.textContent;
  23209. return value1.localeCompare(value2);
  23210. },
  23211. reversebyasc: function(td1, td2) {
  23212. var value1 = td1.innerHTML,
  23213. value2 = td2.innerHTML;
  23214. return value2.localeCompare(value1);
  23215. },
  23216. orderbynum: function(td1, td2) {
  23217. var value1 = td1[browser.ie ? "innerText" : "textContent"].match(/\d+/),
  23218. value2 = td2[browser.ie ? "innerText" : "textContent"].match(/\d+/);
  23219. if (value1) value1 = +value1[0];
  23220. if (value2) value2 = +value2[0];
  23221. return (value1 || 0) - (value2 || 0);
  23222. },
  23223. reversebynum: function(td1, td2) {
  23224. var value1 = td1[browser.ie ? "innerText" : "textContent"].match(/\d+/),
  23225. value2 = td2[browser.ie ? "innerText" : "textContent"].match(/\d+/);
  23226. if (value1) value1 = +value1[0];
  23227. if (value2) value2 = +value2[0];
  23228. return (value2 || 0) - (value1 || 0);
  23229. }
  23230. };
  23231. //对表格设置排序的标记data-sort-type
  23232. table.setAttribute(
  23233. "data-sort-type",
  23234. compareFn && typeof compareFn === "string" && Fn[compareFn] ? compareFn : ""
  23235. );
  23236. //th不参与排序
  23237. flag && trArray.splice(0, 1);
  23238. trArray = utils.sort(trArray, function(tr1, tr2) {
  23239. var result;
  23240. if (compareFn && typeof compareFn === "function") {
  23241. result = compareFn.call(
  23242. this,
  23243. tr1.cells[sortByCellIndex],
  23244. tr2.cells[sortByCellIndex]
  23245. );
  23246. } else if (compareFn && typeof compareFn === "number") {
  23247. result = 1;
  23248. } else if (compareFn && typeof compareFn === "string" && Fn[compareFn]) {
  23249. result = Fn[compareFn].call(
  23250. this,
  23251. tr1.cells[sortByCellIndex],
  23252. tr2.cells[sortByCellIndex]
  23253. );
  23254. } else {
  23255. result = Fn["orderbyasc"].call(
  23256. this,
  23257. tr1.cells[sortByCellIndex],
  23258. tr2.cells[sortByCellIndex]
  23259. );
  23260. }
  23261. return result;
  23262. });
  23263. var fragment = table.ownerDocument.createDocumentFragment();
  23264. for (var j = 0, len = trArray.length; j < len; j++) {
  23265. fragment.appendChild(trArray[j]);
  23266. }
  23267. var tbody = table.getElementsByTagName("tbody")[0];
  23268. if (!lastRowIndex) {
  23269. tbody.appendChild(fragment);
  23270. } else {
  23271. tbody.insertBefore(
  23272. fragment,
  23273. rows[lastRowIndex - range.endRowIndex + range.beginRowIndex - 1]
  23274. );
  23275. }
  23276. };
  23277. UE.plugins["tablesort"] = function() {
  23278. var me = this,
  23279. UT = UE.UETable,
  23280. getUETable = function(tdOrTable) {
  23281. return UT.getUETable(tdOrTable);
  23282. },
  23283. getTableItemsByRange = function(editor) {
  23284. return UT.getTableItemsByRange(editor);
  23285. };
  23286. me.ready(function() {
  23287. //添加表格可排序的样式
  23288. utils.cssRule(
  23289. "tablesort",
  23290. "table.sortEnabled tr.firstRow th,table.sortEnabled tr.firstRow td{padding-right:20px;background-repeat: no-repeat;background-position: center right;" +
  23291. " background-image:url(" +
  23292. me.options.themePath +
  23293. me.options.theme +
  23294. "/images/sortable.png);}",
  23295. me.document
  23296. );
  23297. //做单元格合并操作时,清除可排序标识
  23298. me.addListener("afterexeccommand", function(type, cmd) {
  23299. if (cmd == "mergeright" || cmd == "mergedown" || cmd == "mergecells") {
  23300. this.execCommand("disablesort");
  23301. }
  23302. });
  23303. });
  23304. //表格排序
  23305. UE.commands["sorttable"] = {
  23306. queryCommandState: function() {
  23307. var me = this,
  23308. tableItems = getTableItemsByRange(me);
  23309. if (!tableItems.cell) return -1;
  23310. var table = tableItems.table,
  23311. cells = table.getElementsByTagName("td");
  23312. for (var i = 0, cell; (cell = cells[i++]); ) {
  23313. if (cell.rowSpan != 1 || cell.colSpan != 1) return -1;
  23314. }
  23315. return 0;
  23316. },
  23317. execCommand: function(cmd, fn) {
  23318. var me = this,
  23319. range = me.selection.getRange(),
  23320. bk = range.createBookmark(true),
  23321. tableItems = getTableItemsByRange(me),
  23322. cell = tableItems.cell,
  23323. ut = getUETable(tableItems.table),
  23324. cellInfo = ut.getCellInfo(cell);
  23325. ut.sortTable(cellInfo.cellIndex, fn);
  23326. range.moveToBookmark(bk);
  23327. try {
  23328. range.select();
  23329. } catch (e) {}
  23330. }
  23331. };
  23332. //设置表格可排序,清除表格可排序
  23333. UE.commands["enablesort"] = UE.commands["disablesort"] = {
  23334. queryCommandState: function(cmd) {
  23335. var table = getTableItemsByRange(this).table;
  23336. if (table && cmd == "enablesort") {
  23337. var cells = domUtils.getElementsByTagName(table, "th td");
  23338. for (var i = 0; i < cells.length; i++) {
  23339. if (
  23340. cells[i].getAttribute("colspan") > 1 ||
  23341. cells[i].getAttribute("rowspan") > 1
  23342. )
  23343. return -1;
  23344. }
  23345. }
  23346. return !table
  23347. ? -1
  23348. : (cmd == "enablesort") ^
  23349. (table.getAttribute("data-sort") != "sortEnabled")
  23350. ? -1
  23351. : 0;
  23352. },
  23353. execCommand: function(cmd) {
  23354. var table = getTableItemsByRange(this).table;
  23355. table.setAttribute(
  23356. "data-sort",
  23357. cmd == "enablesort" ? "sortEnabled" : "sortDisabled"
  23358. );
  23359. cmd == "enablesort"
  23360. ? domUtils.addClass(table, "sortEnabled")
  23361. : domUtils.removeClasses(table, "sortEnabled");
  23362. }
  23363. };
  23364. };
  23365. // plugins/contextmenu.js
  23366. ///import core
  23367. ///commands 右键菜单
  23368. ///commandsName ContextMenu
  23369. ///commandsTitle 右键菜单
  23370. /**
  23371. * 右键菜单
  23372. * @function
  23373. * @name baidu.editor.plugins.contextmenu
  23374. * @author zhanyi
  23375. */
  23376. UE.plugins["contextmenu"] = function() {
  23377. var me = this;
  23378. me.setOpt("enableContextMenu", me.getOpt("enableContextMenu") || true);
  23379. if (me.getOpt("enableContextMenu") === false) {
  23380. return;
  23381. }
  23382. var lang = me.getLang("contextMenu"),
  23383. menu,
  23384. items = me.options.contextMenu || [
  23385. { label: lang["selectall"], cmdName: "selectall" },
  23386. {
  23387. label: lang.cleardoc,
  23388. cmdName: "cleardoc",
  23389. exec: function() {
  23390. if (confirm(lang.confirmclear)) {
  23391. this.execCommand("cleardoc");
  23392. }
  23393. }
  23394. },
  23395. "-",
  23396. {
  23397. label: lang.unlink,
  23398. cmdName: "unlink"
  23399. },
  23400. "-",
  23401. {
  23402. group: lang.paragraph,
  23403. icon: "justifyjustify",
  23404. subMenu: [
  23405. {
  23406. label: lang.justifyleft,
  23407. cmdName: "justify",
  23408. value: "left"
  23409. },
  23410. {
  23411. label: lang.justifyright,
  23412. cmdName: "justify",
  23413. value: "right"
  23414. },
  23415. {
  23416. label: lang.justifycenter,
  23417. cmdName: "justify",
  23418. value: "center"
  23419. },
  23420. {
  23421. label: lang.justifyjustify,
  23422. cmdName: "justify",
  23423. value: "justify"
  23424. }
  23425. ]
  23426. },
  23427. "-",
  23428. {
  23429. group: lang.table,
  23430. icon: "table",
  23431. subMenu: [
  23432. {
  23433. label: lang.inserttable,
  23434. cmdName: "inserttable"
  23435. },
  23436. {
  23437. label: lang.deletetable,
  23438. cmdName: "deletetable"
  23439. },
  23440. "-",
  23441. {
  23442. label: lang.deleterow,
  23443. cmdName: "deleterow"
  23444. },
  23445. {
  23446. label: lang.deletecol,
  23447. cmdName: "deletecol"
  23448. },
  23449. {
  23450. label: lang.insertcol,
  23451. cmdName: "insertcol"
  23452. },
  23453. {
  23454. label: lang.insertcolnext,
  23455. cmdName: "insertcolnext"
  23456. },
  23457. {
  23458. label: lang.insertrow,
  23459. cmdName: "insertrow"
  23460. },
  23461. {
  23462. label: lang.insertrownext,
  23463. cmdName: "insertrownext"
  23464. },
  23465. "-",
  23466. {
  23467. label: lang.insertcaption,
  23468. cmdName: "insertcaption"
  23469. },
  23470. {
  23471. label: lang.deletecaption,
  23472. cmdName: "deletecaption"
  23473. },
  23474. {
  23475. label: lang.inserttitle,
  23476. cmdName: "inserttitle"
  23477. },
  23478. {
  23479. label: lang.deletetitle,
  23480. cmdName: "deletetitle"
  23481. },
  23482. {
  23483. label: lang.inserttitlecol,
  23484. cmdName: "inserttitlecol"
  23485. },
  23486. {
  23487. label: lang.deletetitlecol,
  23488. cmdName: "deletetitlecol"
  23489. },
  23490. "-",
  23491. {
  23492. label: lang.mergecells,
  23493. cmdName: "mergecells"
  23494. },
  23495. {
  23496. label: lang.mergeright,
  23497. cmdName: "mergeright"
  23498. },
  23499. {
  23500. label: lang.mergedown,
  23501. cmdName: "mergedown"
  23502. },
  23503. "-",
  23504. {
  23505. label: lang.splittorows,
  23506. cmdName: "splittorows"
  23507. },
  23508. {
  23509. label: lang.splittocols,
  23510. cmdName: "splittocols"
  23511. },
  23512. {
  23513. label: lang.splittocells,
  23514. cmdName: "splittocells"
  23515. },
  23516. "-",
  23517. {
  23518. label: lang.averageDiseRow,
  23519. cmdName: "averagedistributerow"
  23520. },
  23521. {
  23522. label: lang.averageDisCol,
  23523. cmdName: "averagedistributecol"
  23524. },
  23525. "-",
  23526. {
  23527. label: lang.edittd,
  23528. cmdName: "edittd",
  23529. exec: function() {
  23530. if (UE.ui["edittd"]) {
  23531. new UE.ui["edittd"](this);
  23532. }
  23533. this.getDialog("edittd").open();
  23534. }
  23535. },
  23536. {
  23537. label: lang.edittable,
  23538. cmdName: "edittable",
  23539. exec: function() {
  23540. if (UE.ui["edittable"]) {
  23541. new UE.ui["edittable"](this);
  23542. }
  23543. this.getDialog("edittable").open();
  23544. }
  23545. },
  23546. {
  23547. label: lang.setbordervisible,
  23548. cmdName: "setbordervisible"
  23549. }
  23550. ]
  23551. },
  23552. {
  23553. group: lang.tablesort,
  23554. icon: "tablesort",
  23555. subMenu: [
  23556. {
  23557. label: lang.enablesort,
  23558. cmdName: "enablesort"
  23559. },
  23560. {
  23561. label: lang.disablesort,
  23562. cmdName: "disablesort"
  23563. },
  23564. "-",
  23565. {
  23566. label: lang.reversecurrent,
  23567. cmdName: "sorttable",
  23568. value: "reversecurrent"
  23569. },
  23570. {
  23571. label: lang.orderbyasc,
  23572. cmdName: "sorttable",
  23573. value: "orderbyasc"
  23574. },
  23575. {
  23576. label: lang.reversebyasc,
  23577. cmdName: "sorttable",
  23578. value: "reversebyasc"
  23579. },
  23580. {
  23581. label: lang.orderbynum,
  23582. cmdName: "sorttable",
  23583. value: "orderbynum"
  23584. },
  23585. {
  23586. label: lang.reversebynum,
  23587. cmdName: "sorttable",
  23588. value: "reversebynum"
  23589. }
  23590. ]
  23591. },
  23592. {
  23593. group: lang.borderbk,
  23594. icon: "borderBack",
  23595. subMenu: [
  23596. {
  23597. label: lang.setcolor,
  23598. cmdName: "interlacetable",
  23599. exec: function() {
  23600. this.execCommand("interlacetable");
  23601. }
  23602. },
  23603. {
  23604. label: lang.unsetcolor,
  23605. cmdName: "uninterlacetable",
  23606. exec: function() {
  23607. this.execCommand("uninterlacetable");
  23608. }
  23609. },
  23610. {
  23611. label: lang.setbackground,
  23612. cmdName: "settablebackground",
  23613. exec: function() {
  23614. this.execCommand("settablebackground", {
  23615. repeat: true,
  23616. colorList: ["#bbb", "#ccc"]
  23617. });
  23618. }
  23619. },
  23620. {
  23621. label: lang.unsetbackground,
  23622. cmdName: "cleartablebackground",
  23623. exec: function() {
  23624. this.execCommand("cleartablebackground");
  23625. }
  23626. },
  23627. {
  23628. label: lang.redandblue,
  23629. cmdName: "settablebackground",
  23630. exec: function() {
  23631. this.execCommand("settablebackground", {
  23632. repeat: true,
  23633. colorList: ["red", "blue"]
  23634. });
  23635. }
  23636. },
  23637. {
  23638. label: lang.threecolorgradient,
  23639. cmdName: "settablebackground",
  23640. exec: function() {
  23641. this.execCommand("settablebackground", {
  23642. repeat: true,
  23643. colorList: ["#aaa", "#bbb", "#ccc"]
  23644. });
  23645. }
  23646. }
  23647. ]
  23648. },
  23649. {
  23650. group: lang.aligntd,
  23651. icon: "aligntd",
  23652. subMenu: [
  23653. {
  23654. cmdName: "cellalignment",
  23655. value: { align: "left", vAlign: "top" }
  23656. },
  23657. {
  23658. cmdName: "cellalignment",
  23659. value: { align: "center", vAlign: "top" }
  23660. },
  23661. {
  23662. cmdName: "cellalignment",
  23663. value: { align: "right", vAlign: "top" }
  23664. },
  23665. {
  23666. cmdName: "cellalignment",
  23667. value: { align: "left", vAlign: "middle" }
  23668. },
  23669. {
  23670. cmdName: "cellalignment",
  23671. value: { align: "center", vAlign: "middle" }
  23672. },
  23673. {
  23674. cmdName: "cellalignment",
  23675. value: { align: "right", vAlign: "middle" }
  23676. },
  23677. {
  23678. cmdName: "cellalignment",
  23679. value: { align: "left", vAlign: "bottom" }
  23680. },
  23681. {
  23682. cmdName: "cellalignment",
  23683. value: { align: "center", vAlign: "bottom" }
  23684. },
  23685. {
  23686. cmdName: "cellalignment",
  23687. value: { align: "right", vAlign: "bottom" }
  23688. }
  23689. ]
  23690. },
  23691. {
  23692. group: lang.aligntable,
  23693. icon: "aligntable",
  23694. subMenu: [
  23695. {
  23696. cmdName: "tablealignment",
  23697. className: "left",
  23698. label: lang.tableleft,
  23699. value: "left"
  23700. },
  23701. {
  23702. cmdName: "tablealignment",
  23703. className: "center",
  23704. label: lang.tablecenter,
  23705. value: "center"
  23706. },
  23707. {
  23708. cmdName: "tablealignment",
  23709. className: "right",
  23710. label: lang.tableright,
  23711. value: "right"
  23712. }
  23713. ]
  23714. },
  23715. "-",
  23716. {
  23717. label: lang.insertparagraphbefore,
  23718. cmdName: "insertparagraph",
  23719. value: true
  23720. },
  23721. {
  23722. label: lang.insertparagraphafter,
  23723. cmdName: "insertparagraph"
  23724. },
  23725. {
  23726. label: lang["copy"],
  23727. cmdName: "copy"
  23728. },
  23729. {
  23730. label: lang["paste"],
  23731. cmdName: "paste"
  23732. }
  23733. ];
  23734. if (!items.length) {
  23735. return;
  23736. }
  23737. var uiUtils = UE.ui.uiUtils;
  23738. me.addListener("contextmenu", function(type, evt) {
  23739. var offset = uiUtils.getViewportOffsetByEvent(evt);
  23740. me.fireEvent("beforeselectionchange");
  23741. if (menu) {
  23742. menu.destroy();
  23743. }
  23744. for (var i = 0, ti, contextItems = []; (ti = items[i]); i++) {
  23745. var last;
  23746. (function(item) {
  23747. if (item == "-") {
  23748. if ((last = contextItems[contextItems.length - 1]) && last !== "-") {
  23749. contextItems.push("-");
  23750. }
  23751. } else if (item.hasOwnProperty("group")) {
  23752. for (var j = 0, cj, subMenu = []; (cj = item.subMenu[j]); j++) {
  23753. (function(subItem) {
  23754. if (subItem == "-") {
  23755. if ((last = subMenu[subMenu.length - 1]) && last !== "-") {
  23756. subMenu.push("-");
  23757. } else {
  23758. subMenu.splice(subMenu.length - 1);
  23759. }
  23760. } else {
  23761. if (
  23762. (me.commands[subItem.cmdName] ||
  23763. UE.commands[subItem.cmdName] ||
  23764. subItem.query) &&
  23765. (subItem.query
  23766. ? subItem.query()
  23767. : me.queryCommandState(subItem.cmdName)) > -1
  23768. ) {
  23769. subMenu.push({
  23770. label:
  23771. subItem.label ||
  23772. me.getLang(
  23773. "contextMenu." +
  23774. subItem.cmdName +
  23775. (subItem.value || "")
  23776. ) ||
  23777. "",
  23778. className:
  23779. "edui-for-" +
  23780. subItem.cmdName +
  23781. (subItem.className
  23782. ? " edui-for-" +
  23783. subItem.cmdName +
  23784. "-" +
  23785. subItem.className
  23786. : ""),
  23787. onclick: subItem.exec
  23788. ? function() {
  23789. subItem.exec.call(me);
  23790. }
  23791. : function() {
  23792. me.execCommand(subItem.cmdName, subItem.value);
  23793. }
  23794. });
  23795. }
  23796. }
  23797. })(cj);
  23798. }
  23799. if (subMenu.length) {
  23800. function getLabel() {
  23801. switch (item.icon) {
  23802. case "table":
  23803. return me.getLang("contextMenu.table");
  23804. case "justifyjustify":
  23805. return me.getLang("contextMenu.paragraph");
  23806. case "aligntd":
  23807. return me.getLang("contextMenu.aligntd");
  23808. case "aligntable":
  23809. return me.getLang("contextMenu.aligntable");
  23810. case "tablesort":
  23811. return lang.tablesort;
  23812. case "borderBack":
  23813. return lang.borderbk;
  23814. default:
  23815. return "";
  23816. }
  23817. }
  23818. contextItems.push({
  23819. //todo 修正成自动获取方式
  23820. label: getLabel(),
  23821. className: "edui-for-" + item.icon,
  23822. subMenu: {
  23823. items: subMenu,
  23824. editor: me
  23825. }
  23826. });
  23827. }
  23828. } else {
  23829. //有可能commmand没有加载右键不能出来,或者没有command也想能展示出来添加query方法
  23830. if (
  23831. (me.commands[item.cmdName] ||
  23832. UE.commands[item.cmdName] ||
  23833. item.query) &&
  23834. (item.query
  23835. ? item.query.call(me)
  23836. : me.queryCommandState(item.cmdName)) > -1
  23837. ) {
  23838. contextItems.push({
  23839. label: item.label || me.getLang("contextMenu." + item.cmdName),
  23840. className:
  23841. "edui-for-" +
  23842. (item.icon ? item.icon : item.cmdName + (item.value || "")),
  23843. onclick: item.exec
  23844. ? function() {
  23845. item.exec.call(me);
  23846. }
  23847. : function() {
  23848. me.execCommand(item.cmdName, item.value);
  23849. }
  23850. });
  23851. }
  23852. }
  23853. })(ti);
  23854. }
  23855. if (contextItems[contextItems.length - 1] == "-") {
  23856. contextItems.pop();
  23857. }
  23858. menu = new UE.ui.Menu({
  23859. items: contextItems,
  23860. className: "edui-contextmenu",
  23861. editor: me
  23862. });
  23863. menu.render();
  23864. menu.showAt(offset);
  23865. me.fireEvent("aftershowcontextmenu", menu);
  23866. domUtils.preventDefault(evt);
  23867. if (browser.ie) {
  23868. var ieRange;
  23869. try {
  23870. ieRange = me.selection.getNative().createRange();
  23871. } catch (e) {
  23872. return;
  23873. }
  23874. if (ieRange.item) {
  23875. var range = new dom.Range(me.document);
  23876. range.selectNode(ieRange.item(0)).select(true, true);
  23877. }
  23878. }
  23879. });
  23880. // 添加复制的flash按钮
  23881. me.addListener("aftershowcontextmenu", function(type, menu) {
  23882. if (me.zeroclipboard) {
  23883. var items = menu.items;
  23884. for (var key in items) {
  23885. if (items[key].className == "edui-for-copy") {
  23886. me.zeroclipboard.clip(items[key].getDom());
  23887. }
  23888. }
  23889. }
  23890. });
  23891. };
  23892. // plugins/shortcutmenu.js
  23893. ///import core
  23894. ///commands 弹出菜单
  23895. // commandsName popupmenu
  23896. ///commandsTitle 弹出菜单
  23897. /**
  23898. * 弹出菜单
  23899. * @function
  23900. * @name baidu.editor.plugins.popupmenu
  23901. * @author xuheng
  23902. */
  23903. UE.plugins["shortcutmenu"] = function() {
  23904. var me = this,
  23905. menu,
  23906. items = me.options.shortcutMenu || [];
  23907. if (!items.length) {
  23908. return;
  23909. }
  23910. me.addListener("contextmenu mouseup", function(type, e) {
  23911. var me = this,
  23912. customEvt = {
  23913. type: type,
  23914. target: e.target || e.srcElement,
  23915. screenX: e.screenX,
  23916. screenY: e.screenY,
  23917. clientX: e.clientX,
  23918. clientY: e.clientY
  23919. };
  23920. setTimeout(function() {
  23921. var rng = me.selection.getRange();
  23922. if (rng.collapsed === false || type == "contextmenu") {
  23923. // 未选中文字情况下不显示
  23924. if(!me.selection.getText()){
  23925. return
  23926. }
  23927. if (!menu) {
  23928. menu = new baidu.editor.ui.ShortCutMenu({
  23929. editor: me,
  23930. items: items,
  23931. theme: me.options.theme,
  23932. className: "edui-shortcutmenu"
  23933. });
  23934. menu.render();
  23935. me.fireEvent("afterrendershortcutmenu", menu);
  23936. }
  23937. menu.show(customEvt, !!UE.plugins["contextmenu"]);
  23938. }
  23939. });
  23940. if (type == "contextmenu") {
  23941. domUtils.preventDefault(e);
  23942. if (browser.ie9below) {
  23943. var ieRange;
  23944. try {
  23945. ieRange = me.selection.getNative().createRange();
  23946. } catch (e) {
  23947. return;
  23948. }
  23949. if (ieRange.item) {
  23950. var range = new dom.Range(me.document);
  23951. range.selectNode(ieRange.item(0)).select(true, true);
  23952. }
  23953. }
  23954. }
  23955. });
  23956. me.addListener("keydown", function(type) {
  23957. if (type == "keydown") {
  23958. menu && !menu.isHidden && menu.hide();
  23959. }
  23960. });
  23961. };
  23962. // plugins/basestyle.js
  23963. /**
  23964. * B、I、sub、super命令支持
  23965. * @file
  23966. * @since 1.2.6.1
  23967. */
  23968. UE.plugins["basestyle"] = function() {
  23969. /**
  23970. * 字体加粗
  23971. * @command bold
  23972. * @param { String } cmd 命令字符串
  23973. * @remind 对已加粗的文本内容执行该命令, 将取消加粗
  23974. * @method execCommand
  23975. * @example
  23976. * ```javascript
  23977. * //editor是编辑器实例
  23978. * //对当前选中的文本内容执行加粗操作
  23979. * //第一次执行, 文本内容加粗
  23980. * editor.execCommand( 'bold' );
  23981. *
  23982. * //第二次执行, 文本内容取消加粗
  23983. * editor.execCommand( 'bold' );
  23984. * ```
  23985. */
  23986. /**
  23987. * 字体倾斜
  23988. * @command italic
  23989. * @method execCommand
  23990. * @param { String } cmd 命令字符串
  23991. * @remind 对已倾斜的文本内容执行该命令, 将取消倾斜
  23992. * @example
  23993. * ```javascript
  23994. * //editor是编辑器实例
  23995. * //对当前选中的文本内容执行斜体操作
  23996. * //第一次操作, 文本内容将变成斜体
  23997. * editor.execCommand( 'italic' );
  23998. *
  23999. * //再次对同一文本内容执行, 则文本内容将恢复正常
  24000. * editor.execCommand( 'italic' );
  24001. * ```
  24002. */
  24003. /**
  24004. * 下标文本,与“superscript”命令互斥
  24005. * @command subscript
  24006. * @method execCommand
  24007. * @remind 把选中的文本内容切换成下标文本, 如果当前选中的文本已经是下标, 则该操作会把文本内容还原成正常文本
  24008. * @param { String } cmd 命令字符串
  24009. * @example
  24010. * ```javascript
  24011. * //editor是编辑器实例
  24012. * //对当前选中的文本内容执行下标操作
  24013. * //第一次操作, 文本内容将变成下标文本
  24014. * editor.execCommand( 'subscript' );
  24015. *
  24016. * //再次对同一文本内容执行, 则文本内容将恢复正常
  24017. * editor.execCommand( 'subscript' );
  24018. * ```
  24019. */
  24020. /**
  24021. * 上标文本,与“subscript”命令互斥
  24022. * @command superscript
  24023. * @method execCommand
  24024. * @remind 把选中的文本内容切换成上标文本, 如果当前选中的文本已经是上标, 则该操作会把文本内容还原成正常文本
  24025. * @param { String } cmd 命令字符串
  24026. * @example
  24027. * ```javascript
  24028. * //editor是编辑器实例
  24029. * //对当前选中的文本内容执行上标操作
  24030. * //第一次操作, 文本内容将变成上标文本
  24031. * editor.execCommand( 'superscript' );
  24032. *
  24033. * //再次对同一文本内容执行, 则文本内容将恢复正常
  24034. * editor.execCommand( 'superscript' );
  24035. * ```
  24036. */
  24037. var basestyles = {
  24038. bold: ["strong", "b"],
  24039. italic: ["em", "i"],
  24040. subscript: ["sub"],
  24041. superscript: ["sup"]
  24042. },
  24043. getObj = function(editor, tagNames) {
  24044. return domUtils.filterNodeList(
  24045. editor.selection.getStartElementPath(),
  24046. tagNames
  24047. );
  24048. },
  24049. me = this;
  24050. //添加快捷键
  24051. me.addshortcutkey({
  24052. Bold: "ctrl+66", //^B
  24053. Italic: "ctrl+73", //^I
  24054. Underline: "ctrl+85" //^U
  24055. });
  24056. me.addInputRule(function(root) {
  24057. utils.each(root.getNodesByTagName("b i"), function(node) {
  24058. switch (node.tagName) {
  24059. case "b":
  24060. node.tagName = "strong";
  24061. break;
  24062. case "i":
  24063. node.tagName = "em";
  24064. }
  24065. });
  24066. });
  24067. for (var style in basestyles) {
  24068. (function(cmd, tagNames) {
  24069. me.commands[cmd] = {
  24070. execCommand: function(cmdName) {
  24071. var range = me.selection.getRange(),
  24072. obj = getObj(this, tagNames);
  24073. if (range.collapsed) {
  24074. if (obj) {
  24075. var tmpText = me.document.createTextNode("");
  24076. range.insertNode(tmpText).removeInlineStyle(tagNames);
  24077. range.setStartBefore(tmpText);
  24078. domUtils.remove(tmpText);
  24079. } else {
  24080. var tmpNode = range.document.createElement(tagNames[0]);
  24081. if (cmdName == "superscript" || cmdName == "subscript") {
  24082. tmpText = me.document.createTextNode("");
  24083. range
  24084. .insertNode(tmpText)
  24085. .removeInlineStyle(["sub", "sup"])
  24086. .setStartBefore(tmpText)
  24087. .collapse(true);
  24088. }
  24089. range.insertNode(tmpNode).setStart(tmpNode, 0);
  24090. }
  24091. range.collapse(true);
  24092. } else {
  24093. if (cmdName == "superscript" || cmdName == "subscript") {
  24094. if (!obj || obj.tagName.toLowerCase() != cmdName) {
  24095. range.removeInlineStyle(["sub", "sup"]);
  24096. }
  24097. }
  24098. obj
  24099. ? range.removeInlineStyle(tagNames)
  24100. : range.applyInlineStyle(tagNames[0]);
  24101. }
  24102. range.select();
  24103. },
  24104. queryCommandState: function() {
  24105. return getObj(this, tagNames) ? 1 : 0;
  24106. }
  24107. };
  24108. })(style, basestyles[style]);
  24109. }
  24110. };
  24111. // plugins/elementpath.js
  24112. /**
  24113. * 选取路径命令
  24114. * @file
  24115. */
  24116. UE.plugins["elementpath"] = function() {
  24117. var currentLevel,
  24118. tagNames,
  24119. me = this;
  24120. me.setOpt("elementPathEnabled", true);
  24121. if (!me.options.elementPathEnabled) {
  24122. return;
  24123. }
  24124. me.commands["elementpath"] = {
  24125. execCommand: function(cmdName, level) {
  24126. var start = tagNames[level],
  24127. range = me.selection.getRange();
  24128. currentLevel = level * 1;
  24129. range.selectNode(start).select();
  24130. },
  24131. queryCommandValue: function() {
  24132. //产生一个副本,不能修改原来的startElementPath;
  24133. var parents = [].concat(this.selection.getStartElementPath()).reverse(),
  24134. names = [];
  24135. tagNames = parents;
  24136. for (var i = 0, ci; (ci = parents[i]); i++) {
  24137. if (ci.nodeType == 3) {
  24138. continue;
  24139. }
  24140. var name = ci.tagName.toLowerCase();
  24141. if (name == "img" && ci.getAttribute("anchorname")) {
  24142. name = "anchor";
  24143. }
  24144. names[i] = name;
  24145. if (currentLevel == i) {
  24146. currentLevel = -1;
  24147. break;
  24148. }
  24149. }
  24150. return names;
  24151. }
  24152. };
  24153. };
  24154. // plugins/formatmatch.js
  24155. /**
  24156. * 格式刷,只格式inline的
  24157. * @file
  24158. * @since 1.2.6.1
  24159. */
  24160. /**
  24161. * 格式刷
  24162. * @command formatmatch
  24163. * @method execCommand
  24164. * @remind 该操作不能复制段落格式
  24165. * @param { String } cmd 命令字符串
  24166. * @example
  24167. * ```javascript
  24168. * //editor是编辑器实例
  24169. * //获取格式刷
  24170. * editor.execCommand( 'formatmatch' );
  24171. * ```
  24172. */
  24173. UE.plugins["formatmatch"] = function() {
  24174. var me = this,
  24175. list = [],
  24176. img,
  24177. flag = 0;
  24178. me.addListener("reset", function() {
  24179. list = [];
  24180. flag = 0;
  24181. });
  24182. function addList(type, evt) {
  24183. if (browser.webkit) {
  24184. var target = evt.target.tagName == "IMG" ? evt.target : null;
  24185. }
  24186. function addFormat(range) {
  24187. if (text) {
  24188. range.selectNode(text);
  24189. }
  24190. return range.applyInlineStyle(list[list.length - 1].tagName, null, list);
  24191. }
  24192. me.undoManger && me.undoManger.save();
  24193. var range = me.selection.getRange(),
  24194. imgT = target || range.getClosedNode();
  24195. if (img && imgT && imgT.tagName == "IMG") {
  24196. //trace:964
  24197. imgT.style.cssText +=
  24198. ";float:" +
  24199. (img.style.cssFloat || img.style.styleFloat || "none") +
  24200. ";display:" +
  24201. (img.style.display || "inline");
  24202. img = null;
  24203. } else {
  24204. if (!img) {
  24205. var collapsed = range.collapsed;
  24206. if (collapsed) {
  24207. var text = me.document.createTextNode("match");
  24208. range.insertNode(text).select();
  24209. }
  24210. me.__hasEnterExecCommand = true;
  24211. //不能把block上的属性干掉
  24212. //trace:1553
  24213. var removeFormatAttributes = me.options.removeFormatAttributes;
  24214. me.options.removeFormatAttributes = "";
  24215. me.execCommand("removeformat");
  24216. me.options.removeFormatAttributes = removeFormatAttributes;
  24217. me.__hasEnterExecCommand = false;
  24218. //trace:969
  24219. range = me.selection.getRange();
  24220. if (list.length) {
  24221. addFormat(range);
  24222. }
  24223. if (text) {
  24224. range.setStartBefore(text).collapse(true);
  24225. }
  24226. range.select();
  24227. text && domUtils.remove(text);
  24228. }
  24229. }
  24230. me.undoManger && me.undoManger.save();
  24231. me.removeListener("mouseup", addList);
  24232. flag = 0;
  24233. }
  24234. me.commands["formatmatch"] = {
  24235. execCommand: function(cmdName) {
  24236. if (flag) {
  24237. flag = 0;
  24238. list = [];
  24239. me.removeListener("mouseup", addList);
  24240. return;
  24241. }
  24242. var range = me.selection.getRange();
  24243. img = range.getClosedNode();
  24244. if (!img || img.tagName != "IMG") {
  24245. range.collapse(true).shrinkBoundary();
  24246. var start = range.startContainer;
  24247. list = domUtils.findParents(start, true, function(node) {
  24248. return !domUtils.isBlockElm(node) && node.nodeType == 1;
  24249. });
  24250. //a不能加入格式刷, 并且克隆节点
  24251. for (var i = 0, ci; (ci = list[i]); i++) {
  24252. if (ci.tagName == "A") {
  24253. list.splice(i, 1);
  24254. break;
  24255. }
  24256. }
  24257. }
  24258. me.addListener("mouseup", addList);
  24259. flag = 1;
  24260. },
  24261. queryCommandState: function() {
  24262. return flag;
  24263. },
  24264. notNeedUndo: 1
  24265. };
  24266. };
  24267. // plugins/searchreplace.js
  24268. ///import core
  24269. ///commands 查找替换
  24270. ///commandsName SearchReplace
  24271. ///commandsTitle 查询替换
  24272. ///commandsDialog dialogs\searchreplace
  24273. /**
  24274. * @description 查找替换
  24275. * @author zhanyi
  24276. */
  24277. UE.plugin.register("searchreplace", function() {
  24278. var me = this;
  24279. var _blockElm = { table: 1, tbody: 1, tr: 1, ol: 1, ul: 1 };
  24280. var lastRng = null;
  24281. function getText(node) {
  24282. var text = node.nodeType == 3
  24283. ? node.nodeValue
  24284. : node[browser.ie ? "innerText" : "textContent"];
  24285. return text.replace(domUtils.fillChar, "");
  24286. }
  24287. function findTextInString(textContent, opt, currentIndex) {
  24288. var str = opt.searchStr;
  24289. var reg = new RegExp(str, "g" + (opt.casesensitive ? "" : "i")),
  24290. match;
  24291. if (opt.dir == -1) {
  24292. textContent = textContent.substr(0, currentIndex);
  24293. textContent = textContent.split("").reverse().join("");
  24294. str = str.split("").reverse().join("");
  24295. match = reg.exec(textContent);
  24296. if (match) {
  24297. return currentIndex - match.index - str.length;
  24298. }
  24299. } else {
  24300. textContent = textContent.substr(currentIndex);
  24301. match = reg.exec(textContent);
  24302. if (match) {
  24303. return match.index + currentIndex;
  24304. }
  24305. }
  24306. return -1;
  24307. }
  24308. function findTextBlockElm(node, currentIndex, opt) {
  24309. var textContent,
  24310. index,
  24311. methodName = opt.all || opt.dir == 1 ? "getNextDomNode" : "getPreDomNode";
  24312. if (domUtils.isBody(node)) {
  24313. node = node.firstChild;
  24314. }
  24315. var first = 1;
  24316. while (node) {
  24317. textContent = getText(node);
  24318. index = findTextInString(textContent, opt, currentIndex);
  24319. first = 0;
  24320. if (index != -1) {
  24321. return {
  24322. node: node,
  24323. index: index
  24324. };
  24325. }
  24326. node = domUtils[methodName](node);
  24327. while (node && _blockElm[node.nodeName.toLowerCase()]) {
  24328. node = domUtils[methodName](node, true);
  24329. }
  24330. if (node) {
  24331. currentIndex = opt.dir == -1 ? getText(node).length : 0;
  24332. }
  24333. }
  24334. }
  24335. function findNTextInBlockElm(node, index, str) {
  24336. var currentIndex = 0,
  24337. currentNode = node.firstChild,
  24338. currentNodeLength = 0,
  24339. result;
  24340. while (currentNode) {
  24341. if (currentNode.nodeType == 3) {
  24342. currentNodeLength = getText(currentNode).replace(
  24343. /(^[\t\r\n]+)|([\t\r\n]+$)/,
  24344. ""
  24345. ).length;
  24346. currentIndex += currentNodeLength;
  24347. if (currentIndex >= index) {
  24348. return {
  24349. node: currentNode,
  24350. index: currentNodeLength - (currentIndex - index)
  24351. };
  24352. }
  24353. } else if (!dtd.$empty[currentNode.tagName]) {
  24354. currentNodeLength = getText(currentNode).replace(
  24355. /(^[\t\r\n]+)|([\t\r\n]+$)/,
  24356. ""
  24357. ).length;
  24358. currentIndex += currentNodeLength;
  24359. if (currentIndex >= index) {
  24360. result = findNTextInBlockElm(
  24361. currentNode,
  24362. currentNodeLength - (currentIndex - index),
  24363. str
  24364. );
  24365. if (result) {
  24366. return result;
  24367. }
  24368. }
  24369. }
  24370. currentNode = domUtils.getNextDomNode(currentNode);
  24371. }
  24372. }
  24373. function searchReplace(me, opt) {
  24374. var rng = lastRng || me.selection.getRange(),
  24375. startBlockNode,
  24376. searchStr = opt.searchStr,
  24377. span = me.document.createElement("span");
  24378. span.innerHTML = "$$ueditor_searchreplace_key$$";
  24379. rng.shrinkBoundary(true);
  24380. //判断是不是第一次选中
  24381. if (!rng.collapsed) {
  24382. rng.select();
  24383. var rngText = me.selection.getText();
  24384. if (
  24385. new RegExp(
  24386. "^" + opt.searchStr + "$",
  24387. opt.casesensitive ? "" : "i"
  24388. ).test(rngText)
  24389. ) {
  24390. if (opt.replaceStr != undefined) {
  24391. replaceText(rng, opt.replaceStr);
  24392. rng.select();
  24393. return true;
  24394. } else {
  24395. rng.collapse(opt.dir == -1);
  24396. }
  24397. }
  24398. }
  24399. rng.insertNode(span);
  24400. rng.enlargeToBlockElm(true);
  24401. startBlockNode = rng.startContainer;
  24402. var currentIndex = getText(startBlockNode).indexOf(
  24403. "$$ueditor_searchreplace_key$$"
  24404. );
  24405. rng.setStartBefore(span);
  24406. domUtils.remove(span);
  24407. var result = findTextBlockElm(startBlockNode, currentIndex, opt);
  24408. if (result) {
  24409. var rngStart = findNTextInBlockElm(result.node, result.index, searchStr);
  24410. var rngEnd = findNTextInBlockElm(
  24411. result.node,
  24412. result.index + searchStr.length,
  24413. searchStr
  24414. );
  24415. rng
  24416. .setStart(rngStart.node, rngStart.index)
  24417. .setEnd(rngEnd.node, rngEnd.index);
  24418. if (opt.replaceStr !== undefined) {
  24419. replaceText(rng, opt.replaceStr);
  24420. }
  24421. rng.select();
  24422. return true;
  24423. } else {
  24424. rng.setCursor();
  24425. }
  24426. }
  24427. function replaceText(rng, str) {
  24428. str = me.document.createTextNode(str);
  24429. rng.deleteContents().insertNode(str);
  24430. }
  24431. return {
  24432. commands: {
  24433. searchreplace: {
  24434. execCommand: function(cmdName, opt) {
  24435. utils.extend(
  24436. opt,
  24437. {
  24438. all: false,
  24439. casesensitive: false,
  24440. dir: 1
  24441. },
  24442. true
  24443. );
  24444. var num = 0;
  24445. if (opt.all) {
  24446. lastRng = null;
  24447. var rng = me.selection.getRange(),
  24448. first = me.body.firstChild;
  24449. if (first && first.nodeType == 1) {
  24450. rng.setStart(first, 0);
  24451. rng.shrinkBoundary(true);
  24452. } else if (first.nodeType == 3) {
  24453. rng.setStartBefore(first);
  24454. }
  24455. rng.collapse(true).select(true);
  24456. if (opt.replaceStr !== undefined) {
  24457. me.fireEvent("saveScene");
  24458. }
  24459. while (searchReplace(this, opt)) {
  24460. num++;
  24461. lastRng = me.selection.getRange();
  24462. lastRng.collapse(opt.dir == -1);
  24463. }
  24464. if (num) {
  24465. me.fireEvent("saveScene");
  24466. }
  24467. } else {
  24468. if (opt.replaceStr !== undefined) {
  24469. me.fireEvent("saveScene");
  24470. }
  24471. if (searchReplace(this, opt)) {
  24472. num++;
  24473. lastRng = me.selection.getRange();
  24474. lastRng.collapse(opt.dir == -1);
  24475. }
  24476. if (num) {
  24477. me.fireEvent("saveScene");
  24478. }
  24479. }
  24480. return num;
  24481. },
  24482. notNeedUndo: 1
  24483. }
  24484. },
  24485. bindEvents: {
  24486. clearlastSearchResult: function() {
  24487. lastRng = null;
  24488. }
  24489. }
  24490. };
  24491. });
  24492. // plugins/customstyle.js
  24493. /**
  24494. * 自定义样式
  24495. * @file
  24496. * @since 1.2.6.1
  24497. */
  24498. /**
  24499. * 根据config配置文件里“customstyle”选项的值对匹配的标签执行样式替换。
  24500. * @command customstyle
  24501. * @method execCommand
  24502. * @param { String } cmd 命令字符串
  24503. * @example
  24504. * ```javascript
  24505. * editor.execCommand( 'customstyle' );
  24506. * ```
  24507. */
  24508. UE.plugins["customstyle"] = function() {
  24509. var me = this;
  24510. me.setOpt({
  24511. customstyle: [
  24512. {
  24513. tag: "h1",
  24514. name: "tc",
  24515. style:
  24516. "font-size:32px;line-height:40px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:center;"
  24517. },
  24518. {
  24519. tag: "h1",
  24520. name: "tl",
  24521. style:
  24522. "font-size:32px;line-height:40px;font-weight:bold;border-bottom:#ccc 2px solid;padding:0 4px 0 0;text-align:left;"
  24523. },
  24524. {
  24525. tag: "span",
  24526. name: "im",
  24527. style:
  24528. "font-size:16px;font-style:italic;font-weight:bold;line-height:18px;"
  24529. },
  24530. {
  24531. tag: "span",
  24532. name: "hi",
  24533. style:
  24534. "font-size:16px;font-style:italic;font-weight:bold;color:rgb(51, 153, 204);line-height:18px;"
  24535. }
  24536. ]
  24537. });
  24538. me.commands["customstyle"] = {
  24539. execCommand: function(cmdName, obj) {
  24540. var me = this,
  24541. tagName = obj.tag,
  24542. node = domUtils.findParent(
  24543. me.selection.getStart(),
  24544. function(node) {
  24545. return node.getAttribute("label");
  24546. },
  24547. true
  24548. ),
  24549. range,
  24550. bk,
  24551. tmpObj = {};
  24552. for (var p in obj) {
  24553. if (obj[p] !== undefined) tmpObj[p] = obj[p];
  24554. }
  24555. delete tmpObj.tag;
  24556. if (node && node.getAttribute("label") == obj.label) {
  24557. range = this.selection.getRange();
  24558. bk = range.createBookmark();
  24559. if (range.collapsed) {
  24560. //trace:1732 删掉自定义标签,要有p来回填站位
  24561. if (dtd.$block[node.tagName]) {
  24562. var fillNode = me.document.createElement("p");
  24563. domUtils.moveChild(node, fillNode);
  24564. node.parentNode.insertBefore(fillNode, node);
  24565. domUtils.remove(node);
  24566. } else {
  24567. domUtils.remove(node, true);
  24568. }
  24569. } else {
  24570. var common = domUtils.getCommonAncestor(bk.start, bk.end),
  24571. nodes = domUtils.getElementsByTagName(common, tagName);
  24572. if (new RegExp(tagName, "i").test(common.tagName)) {
  24573. nodes.push(common);
  24574. }
  24575. for (var i = 0, ni; (ni = nodes[i++]); ) {
  24576. if (ni.getAttribute("label") == obj.label) {
  24577. var ps = domUtils.getPosition(ni, bk.start),
  24578. pe = domUtils.getPosition(ni, bk.end);
  24579. if (
  24580. (ps & domUtils.POSITION_FOLLOWING ||
  24581. ps & domUtils.POSITION_CONTAINS) &&
  24582. (pe & domUtils.POSITION_PRECEDING ||
  24583. pe & domUtils.POSITION_CONTAINS)
  24584. )
  24585. if (dtd.$block[tagName]) {
  24586. var fillNode = me.document.createElement("p");
  24587. domUtils.moveChild(ni, fillNode);
  24588. ni.parentNode.insertBefore(fillNode, ni);
  24589. }
  24590. domUtils.remove(ni, true);
  24591. }
  24592. }
  24593. node = domUtils.findParent(
  24594. common,
  24595. function(node) {
  24596. return node.getAttribute("label") == obj.label;
  24597. },
  24598. true
  24599. );
  24600. if (node) {
  24601. domUtils.remove(node, true);
  24602. }
  24603. }
  24604. range.moveToBookmark(bk).select();
  24605. } else {
  24606. if (dtd.$block[tagName]) {
  24607. this.execCommand("paragraph", tagName, tmpObj, "customstyle");
  24608. range = me.selection.getRange();
  24609. if (!range.collapsed) {
  24610. range.collapse();
  24611. node = domUtils.findParent(
  24612. me.selection.getStart(),
  24613. function(node) {
  24614. return node.getAttribute("label") == obj.label;
  24615. },
  24616. true
  24617. );
  24618. var pNode = me.document.createElement("p");
  24619. domUtils.insertAfter(node, pNode);
  24620. domUtils.fillNode(me.document, pNode);
  24621. range.setStart(pNode, 0).setCursor();
  24622. }
  24623. } else {
  24624. range = me.selection.getRange();
  24625. if (range.collapsed) {
  24626. node = me.document.createElement(tagName);
  24627. domUtils.setAttributes(node, tmpObj);
  24628. range.insertNode(node).setStart(node, 0).setCursor();
  24629. return;
  24630. }
  24631. bk = range.createBookmark();
  24632. range.applyInlineStyle(tagName, tmpObj).moveToBookmark(bk).select();
  24633. }
  24634. }
  24635. },
  24636. queryCommandValue: function() {
  24637. var parent = domUtils.filterNodeList(
  24638. this.selection.getStartElementPath(),
  24639. function(node) {
  24640. return node.getAttribute("label");
  24641. }
  24642. );
  24643. return parent ? parent.getAttribute("label") : "";
  24644. }
  24645. };
  24646. //当去掉customstyle是,如果是块元素,用p代替
  24647. me.addListener("keyup", function(type, evt) {
  24648. var keyCode = evt.keyCode || evt.which;
  24649. if (keyCode == 32 || keyCode == 13) {
  24650. var range = me.selection.getRange();
  24651. if (range.collapsed) {
  24652. var node = domUtils.findParent(
  24653. me.selection.getStart(),
  24654. function(node) {
  24655. return node.getAttribute("label");
  24656. },
  24657. true
  24658. );
  24659. if (node && dtd.$block[node.tagName] && domUtils.isEmptyNode(node)) {
  24660. var p = me.document.createElement("p");
  24661. domUtils.insertAfter(node, p);
  24662. domUtils.fillNode(me.document, p);
  24663. domUtils.remove(node);
  24664. range.setStart(p, 0).setCursor();
  24665. }
  24666. }
  24667. }
  24668. });
  24669. };
  24670. // plugins/catchremoteimage.js
  24671. ///import core
  24672. ///commands 远程图片抓取
  24673. ///commandsName catchRemoteImage,catchremoteimageenable
  24674. ///commandsTitle 远程图片抓取
  24675. /**
  24676. * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
  24677. */
  24678. UE.plugins["catchremoteimage"] = function () {
  24679. var me = this,
  24680. ajax = UE.ajax;
  24681. /* 设置默认值 */
  24682. if (me.options.catchRemoteImageEnable === false) {
  24683. return;
  24684. }
  24685. me.setOpt({
  24686. catchRemoteImageEnable: false
  24687. });
  24688. var catcherLocalDomain = me.getOpt("catcherLocalDomain"),
  24689. catcherActionUrl = me.getActionUrl(me.getOpt("catcherActionName")),
  24690. catcherUrlPrefix = me.getOpt("catcherUrlPrefix"),
  24691. catcherFieldName = me.getOpt("catcherFieldName");
  24692. me.addListener('serverConfigLoaded', function () {
  24693. catcherLocalDomain = me.getOpt("catcherLocalDomain");
  24694. catcherActionUrl = me.getActionUrl(me.getOpt("catcherActionName"));
  24695. catcherUrlPrefix = me.getOpt("catcherUrlPrefix");
  24696. catcherFieldName = me.getOpt("catcherFieldName");
  24697. });
  24698. me.addListener("afterpaste", function () {
  24699. me.fireEvent("catchremoteimage");
  24700. });
  24701. var catchRemoteImageCatching = false;
  24702. function sendApi(imgs, callbacks) {
  24703. var params = utils.serializeParam(me.queryCommandValue("serverparam")) || "",
  24704. url = utils.formatUrl(
  24705. catcherActionUrl +
  24706. (catcherActionUrl.indexOf("?") === -1 ? "?" : "&") +
  24707. params
  24708. ),
  24709. isJsonp = utils.isCrossDomainUrl(url),
  24710. opt = {
  24711. method: "POST",
  24712. dataType: isJsonp ? "jsonp" : "",
  24713. timeout: 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
  24714. headers: me.options.serverHeaders || {},
  24715. onsuccess: callbacks["success"],
  24716. onerror: callbacks["error"]
  24717. };
  24718. opt[catcherFieldName] = imgs;
  24719. ajax.request(url, opt);
  24720. }
  24721. function catchElement(type, ele, imageUrl) {
  24722. sendApi([imageUrl], {
  24723. //成功抓取
  24724. success: function (r) {
  24725. try {
  24726. var info = r.state !== undefined
  24727. ? r
  24728. : eval("(" + r.responseText + ")");
  24729. } catch (e) {
  24730. return;
  24731. }
  24732. /* 获取源路径和新路径 */
  24733. var oldSrc,
  24734. newSrc,
  24735. oldBgIMG,
  24736. newBgIMG,
  24737. list = info.list;
  24738. var catchFailList = [];
  24739. var catchSuccessList = [];
  24740. var failIMG = me.options.themePath + me.options.theme + '/images/img-cracked.png';
  24741. var loadingIMG = me.options.themePath + me.options.theme + '/images/spacer.gif';
  24742. var cj = list[0];
  24743. switch (type) {
  24744. case 'image':
  24745. oldSrc = ele.getAttribute("_src") || ele.src || "";
  24746. if (cj.state === "SUCCESS") {
  24747. newSrc = catcherUrlPrefix + cj.url;
  24748. // 上传成功是删除uploading动画
  24749. domUtils.removeClasses(ele, "loadingclass");
  24750. domUtils.setAttributes(ele, {
  24751. "src": newSrc,
  24752. "_src": newSrc,
  24753. "data-catch-result": "success"
  24754. });
  24755. catchSuccessList.push(ele);
  24756. } else {
  24757. // 替换成统一的失败图片
  24758. domUtils.removeClasses(ele, "loadingclass");
  24759. domUtils.setAttributes(ele, {
  24760. "src": failIMG,
  24761. "_src": failIMG,
  24762. "data-catch-result": "fail" // 添加catch失败标记
  24763. });
  24764. catchFailList.push(ele);
  24765. }
  24766. break;
  24767. case 'background':
  24768. oldBgIMG = ele.getAttribute("data-background") || "";
  24769. if (cj.state === "SUCCESS") {
  24770. newBgIMG = catcherUrlPrefix + cj.url;
  24771. ele.style.cssText = ele.style.cssText.replace(loadingIMG, newBgIMG);
  24772. domUtils.removeAttributes(ele, "data-background");
  24773. domUtils.setAttributes(ele, {
  24774. "data-catch-result": "success" // 添加catch成功标记
  24775. });
  24776. catchSuccessList.push(ele);
  24777. } else {
  24778. ele.style.cssText = ele.style.cssText.replace(loadingIMG, failIMG);
  24779. domUtils.removeAttributes(ele, "data-background");
  24780. domUtils.setAttributes(ele, {
  24781. "data-catch-result": "fail" // 添加catch失败标记
  24782. });
  24783. catchFailList.push(ele);
  24784. }
  24785. break;
  24786. }
  24787. // 监听事件添加成功抓取和抓取失败的dom列表参数
  24788. me.fireEvent('catchremotesuccess', catchSuccessList, catchFailList);
  24789. catchRemoteImageCatching = false;
  24790. setTimeout(function () {
  24791. me.fireEvent('catchremoteimage');
  24792. }, 0);
  24793. },
  24794. //回调失败,本次请求超时
  24795. error: function () {
  24796. me.fireEvent('catchremoteerror');
  24797. catchRemoteImageCatching = false;
  24798. setTimeout(function () {
  24799. me.fireEvent('catchremoteimage');
  24800. }, 0);
  24801. }
  24802. });
  24803. }
  24804. function catchRemoteImage() {
  24805. if (catchRemoteImageCatching) {
  24806. return;
  24807. }
  24808. catchRemoteImageCatching = true;
  24809. var loadingIMG = me.options.themePath + me.options.theme + '/images/spacer.gif',
  24810. imgs = me.document.querySelectorAll('[style*="url"],img'),
  24811. test = function (src, urls) {
  24812. if (src.indexOf(location.host) !== -1 || /(^\.)|(^\/)/.test(src)) {
  24813. return true;
  24814. }
  24815. if (urls) {
  24816. for (var j = 0, url; (url = urls[j++]);) {
  24817. if (src.indexOf(url) !== -1) {
  24818. return true;
  24819. }
  24820. }
  24821. }
  24822. return false;
  24823. };
  24824. for (var i = 0, ci; (ci = imgs[i++]);) {
  24825. if (ci.getAttribute("data-word-image") || ci.getAttribute('data-catch-result')) {
  24826. continue;
  24827. }
  24828. if (ci.nodeName === "IMG") {
  24829. var src = ci.getAttribute("_src") || ci.src || "";
  24830. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  24831. catchElement('image', ci, src);
  24832. domUtils.setAttributes(ci, {
  24833. class: "loadingclass",
  24834. _src: src,
  24835. src: loadingIMG
  24836. })
  24837. return;
  24838. }
  24839. } else {
  24840. var backgroundImageurl = ci.style.cssText.replace(/.*\s?url\([\'\"]?/, '').replace(/[\'\"]?\).*/, '');
  24841. if (/^(https?|ftp):/i.test(backgroundImageurl) && !test(backgroundImageurl, catcherLocalDomain)) {
  24842. catchElement('background', ci, backgroundImageurl);
  24843. ci.style.cssText = ci.style.cssText.replace(backgroundImageurl, loadingIMG);
  24844. domUtils.setAttributes(ci, {
  24845. "data-background": backgroundImageurl
  24846. })
  24847. return;
  24848. }
  24849. }
  24850. }
  24851. };
  24852. me.addListener("catchremoteimage", function () {
  24853. catchRemoteImage();
  24854. });
  24855. };
  24856. // plugins/insertparagraph.js
  24857. /**
  24858. * 插入段落
  24859. * @file
  24860. * @since 1.2.6.1
  24861. */
  24862. /**
  24863. * 插入段落
  24864. * @command insertparagraph
  24865. * @method execCommand
  24866. * @param { String } cmd 命令字符串
  24867. * @example
  24868. * ```javascript
  24869. * //editor是编辑器实例
  24870. * editor.execCommand( 'insertparagraph' );
  24871. * ```
  24872. */
  24873. UE.commands["insertparagraph"] = {
  24874. execCommand: function(cmdName, front) {
  24875. var me = this,
  24876. range = me.selection.getRange(),
  24877. start = range.startContainer,
  24878. tmpNode;
  24879. while (start) {
  24880. if (domUtils.isBody(start)) {
  24881. break;
  24882. }
  24883. tmpNode = start;
  24884. start = start.parentNode;
  24885. }
  24886. if (tmpNode) {
  24887. var p = me.document.createElement("p");
  24888. if (front) {
  24889. tmpNode.parentNode.insertBefore(p, tmpNode);
  24890. } else {
  24891. tmpNode.parentNode.insertBefore(p, tmpNode.nextSibling);
  24892. }
  24893. domUtils.fillNode(me.document, p);
  24894. range.setStart(p, 0).setCursor(false, true);
  24895. }
  24896. }
  24897. };
  24898. // plugins/template.js
  24899. ///import core
  24900. ///import plugins\inserthtml.js
  24901. ///import plugins\cleardoc.js
  24902. ///commands 模板
  24903. ///commandsName template
  24904. ///commandsTitle 模板
  24905. ///commandsDialog dialogs\template
  24906. UE.plugins["template"] = function() {
  24907. UE.commands["template"] = {
  24908. execCommand: function(cmd, obj) {
  24909. obj.html && this.execCommand("inserthtml", obj.html);
  24910. }
  24911. };
  24912. this.addListener("click", function(type, evt) {
  24913. var el = evt.target || evt.srcElement,
  24914. range = this.selection.getRange();
  24915. var tnode = domUtils.findParent(
  24916. el,
  24917. function(node) {
  24918. if (node.className && domUtils.hasClass(node, "ue_t")) {
  24919. return node;
  24920. }
  24921. },
  24922. true
  24923. );
  24924. tnode && range.selectNode(tnode).shrinkBoundary().select();
  24925. });
  24926. this.addListener("keydown", function(type, evt) {
  24927. var range = this.selection.getRange();
  24928. if (!range.collapsed) {
  24929. if (!evt.ctrlKey && !evt.metaKey && !evt.shiftKey && !evt.altKey) {
  24930. var tnode = domUtils.findParent(
  24931. range.startContainer,
  24932. function(node) {
  24933. if (node.className && domUtils.hasClass(node, "ue_t")) {
  24934. return node;
  24935. }
  24936. },
  24937. true
  24938. );
  24939. if (tnode) {
  24940. domUtils.removeClasses(tnode, ["ue_t"]);
  24941. }
  24942. }
  24943. }
  24944. });
  24945. };
  24946. // plugins/autoupload.js
  24947. /**
  24948. * @description
  24949. * 1.拖放文件到编辑区域,自动上传并插入到选区
  24950. * 2.插入粘贴板的图片,自动上传并插入到选区
  24951. * @author Jinqn
  24952. * @date 2013-10-14
  24953. */
  24954. UE.plugin.register("autoupload", function() {
  24955. function sendAndInsertFile(file, editor) {
  24956. var me = editor;
  24957. //模拟数据
  24958. var fieldName,
  24959. urlPrefix,
  24960. maxSize,
  24961. allowFiles,
  24962. actionUrl,
  24963. loadingHtml,
  24964. errorHandler,
  24965. successHandler,
  24966. filetype = /image\/\w+/i.test(file.type) ? "image" : "file",
  24967. loadingId = "loading_" + (+new Date()).toString(36);
  24968. fieldName = me.getOpt(filetype + "FieldName");
  24969. urlPrefix = me.getOpt(filetype + "UrlPrefix");
  24970. maxSize = me.getOpt(filetype + "MaxSize");
  24971. allowFiles = me.getOpt(filetype + "AllowFiles");
  24972. actionUrl = me.getActionUrl(me.getOpt(filetype + "ActionName"));
  24973. errorHandler = function(title) {
  24974. var loader = me.document.getElementById(loadingId);
  24975. loader && domUtils.remove(loader);
  24976. me.fireEvent("showmessage", {
  24977. id: loadingId,
  24978. content: title,
  24979. type: "error",
  24980. timeout: 4000
  24981. });
  24982. };
  24983. if (filetype == "image") {
  24984. loadingHtml =
  24985. '<img class="loadingclass" id="' +
  24986. loadingId +
  24987. '" src="' +
  24988. me.options.themePath +
  24989. me.options.theme +
  24990. '/images/spacer.gif">';
  24991. successHandler = function(data) {
  24992. var link = urlPrefix + data.url,
  24993. loader = me.document.getElementById(loadingId);
  24994. if (loader) {
  24995. domUtils.removeClasses(loader, "loadingclass");
  24996. loader.setAttribute("src", link);
  24997. loader.setAttribute("_src", link);
  24998. loader.setAttribute("alt", data.original || "");
  24999. loader.removeAttribute("id");
  25000. me.trigger("contentchange", loader);
  25001. }
  25002. };
  25003. } else {
  25004. loadingHtml =
  25005. "<p>" +
  25006. '<img class="loadingclass" id="' +
  25007. loadingId +
  25008. '" src="' +
  25009. me.options.themePath +
  25010. me.options.theme +
  25011. '/images/spacer.gif">' +
  25012. "</p>";
  25013. successHandler = function(data) {
  25014. var link = urlPrefix + data.url,
  25015. loader = me.document.getElementById(loadingId);
  25016. var rng = me.selection.getRange(),
  25017. bk = rng.createBookmark();
  25018. rng.selectNode(loader).select();
  25019. me.execCommand("insertfile", { url: link });
  25020. rng.moveToBookmark(bk).select();
  25021. };
  25022. }
  25023. /* 插入loading的占位符 */
  25024. me.execCommand("inserthtml", loadingHtml);
  25025. /* 判断后端配置是否没有加载成功 */
  25026. if (!me.getOpt(filetype + "ActionName")) {
  25027. errorHandler(me.getLang("autoupload.errorLoadConfig"));
  25028. return;
  25029. }
  25030. /* 判断文件大小是否超出限制 */
  25031. if (file.size > maxSize) {
  25032. errorHandler(me.getLang("autoupload.exceedSizeError"));
  25033. return;
  25034. }
  25035. /* 判断文件格式是否超出允许 */
  25036. var fileext = file.name ? file.name.substr(file.name.lastIndexOf(".")) : "";
  25037. if (
  25038. (fileext && filetype != "image") ||
  25039. (allowFiles &&
  25040. (allowFiles.join("") + ".").indexOf(fileext.toLowerCase() + ".") == -1)
  25041. ) {
  25042. errorHandler(me.getLang("autoupload.exceedTypeError"));
  25043. return;
  25044. }
  25045. /* 创建Ajax并提交 */
  25046. var xhr = new XMLHttpRequest(),
  25047. fd = new FormData(),
  25048. params = utils.serializeParam(me.queryCommandValue("serverparam")) || "",
  25049. url = utils.formatUrl(
  25050. actionUrl + (actionUrl.indexOf("?") == -1 ? "?" : "&") + params
  25051. );
  25052. fd.append(
  25053. fieldName,
  25054. file,
  25055. file.name || "blob." + file.type.substr("image/".length)
  25056. );
  25057. fd.append("type", "ajax");
  25058. xhr.open("post", url, true);
  25059. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  25060. xhr.addEventListener("load", function(e) {
  25061. try {
  25062. var json = new Function("return " + utils.trim(e.target.response))();
  25063. if (json.state == "SUCCESS" && json.url) {
  25064. successHandler(json);
  25065. } else {
  25066. errorHandler(json.state);
  25067. }
  25068. } catch (er) {
  25069. errorHandler(me.getLang("autoupload.loadError"));
  25070. }
  25071. });
  25072. xhr.send(fd);
  25073. }
  25074. function getPasteImage(e) {
  25075. return e.clipboardData &&
  25076. e.clipboardData.items &&
  25077. e.clipboardData.items.length == 1 &&
  25078. /^image\//.test(e.clipboardData.items[0].type)
  25079. ? e.clipboardData.items
  25080. : null;
  25081. }
  25082. function getDropImage(e) {
  25083. return e.dataTransfer && e.dataTransfer.files ? e.dataTransfer.files : null;
  25084. }
  25085. return {
  25086. outputRule: function(root) {
  25087. utils.each(root.getNodesByTagName("img"), function(n) {
  25088. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr("class"))) {
  25089. n.parentNode.removeChild(n);
  25090. }
  25091. });
  25092. utils.each(root.getNodesByTagName("p"), function(n) {
  25093. if (/\bloadpara\b/.test(n.getAttr("class"))) {
  25094. n.parentNode.removeChild(n);
  25095. }
  25096. });
  25097. },
  25098. bindEvents: {
  25099. defaultOptions: {
  25100. //默认间隔时间
  25101. enableDragUpload: true,
  25102. enablePasteUpload: true
  25103. },
  25104. //插入粘贴板的图片,拖放插入图片
  25105. ready: function(e) {
  25106. var me = this;
  25107. if (window.FormData && window.FileReader) {
  25108. var handler = function(e) {
  25109. var hasImg = false,
  25110. items;
  25111. //获取粘贴板文件列表或者拖放文件列表
  25112. items = e.type == "paste" ? getPasteImage(e) : getDropImage(e);
  25113. if (items) {
  25114. var len = items.length,
  25115. file;
  25116. while (len--) {
  25117. file = items[len];
  25118. if (file.getAsFile) file = file.getAsFile();
  25119. if (file && file.size > 0) {
  25120. sendAndInsertFile(file, me);
  25121. hasImg = true;
  25122. }
  25123. }
  25124. hasImg && e.preventDefault();
  25125. }
  25126. };
  25127. if (me.getOpt("enablePasteUpload") !== false) {
  25128. domUtils.on(me.body, "paste ", handler);
  25129. }
  25130. if (me.getOpt("enableDragUpload") !== false) {
  25131. domUtils.on(me.body, "drop", handler);
  25132. //取消拖放图片时出现的文字光标位置提示
  25133. domUtils.on(me.body, "dragover", function(e) {
  25134. if (e.dataTransfer.types[0] == "Files") {
  25135. e.preventDefault();
  25136. }
  25137. });
  25138. } else {
  25139. if (browser.gecko) {
  25140. domUtils.on(me.body, "drop", function(e) {
  25141. if (getDropImage(e)) {
  25142. e.preventDefault();
  25143. }
  25144. });
  25145. }
  25146. }
  25147. //设置loading的样式
  25148. utils.cssRule(
  25149. "loading",
  25150. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  25151. this.options.themePath +
  25152. this.options.theme +
  25153. "/images/loading.gif') no-repeat center center transparent;border-radius:3px;outline:1px solid #EEE;margin-left:1px;height:22px;width:22px;}\n" +
  25154. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  25155. this.options.themePath +
  25156. this.options.theme +
  25157. "/images/loaderror.png') no-repeat center center transparent;border-radius:3px;outline:1px solid #EEE;margin-right:1px;height:22px;width:22px;" +
  25158. "}",
  25159. this.document
  25160. );
  25161. }
  25162. }
  25163. }
  25164. };
  25165. });
  25166. // plugins/section.js
  25167. /**
  25168. * 目录大纲支持插件
  25169. * @file
  25170. * @since 1.3.0
  25171. */
  25172. UE.plugin.register("section", function() {
  25173. /* 目录节点对象 */
  25174. function Section(option) {
  25175. this.tag = "";
  25176. (this.level = -1), (this.dom = null);
  25177. this.nextSection = null;
  25178. this.previousSection = null;
  25179. this.parentSection = null;
  25180. this.startAddress = [];
  25181. this.endAddress = [];
  25182. this.children = [];
  25183. }
  25184. function getSection(option) {
  25185. var section = new Section();
  25186. return utils.extend(section, option);
  25187. }
  25188. function getNodeFromAddress(startAddress, root) {
  25189. var current = root;
  25190. for (var i = 0; i < startAddress.length; i++) {
  25191. if (!current.childNodes) return null;
  25192. current = current.childNodes[startAddress[i]];
  25193. }
  25194. return current;
  25195. }
  25196. var me = this;
  25197. return {
  25198. bindMultiEvents: {
  25199. type: "aftersetcontent afterscencerestore",
  25200. handler: function() {
  25201. me.fireEvent("updateSections");
  25202. }
  25203. },
  25204. bindEvents: {
  25205. /* 初始化、拖拽、粘贴、执行setcontent之后 */
  25206. ready: function() {
  25207. me.fireEvent("updateSections");
  25208. domUtils.on(me.body, "drop paste", function() {
  25209. me.fireEvent("updateSections");
  25210. });
  25211. },
  25212. /* 执行paragraph命令之后 */
  25213. afterexeccommand: function(type, cmd) {
  25214. if (cmd == "paragraph") {
  25215. me.fireEvent("updateSections");
  25216. }
  25217. },
  25218. /* 部分键盘操作,触发updateSections事件 */
  25219. keyup: function(type, e) {
  25220. var me = this,
  25221. range = me.selection.getRange();
  25222. if (range.collapsed != true) {
  25223. me.fireEvent("updateSections");
  25224. } else {
  25225. var keyCode = e.keyCode || e.which;
  25226. if (keyCode == 13 || keyCode == 8 || keyCode == 46) {
  25227. me.fireEvent("updateSections");
  25228. }
  25229. }
  25230. }
  25231. },
  25232. commands: {
  25233. getsections: {
  25234. execCommand: function(cmd, levels) {
  25235. var levelFn = levels || ["h1", "h2", "h3", "h4", "h5", "h6"];
  25236. for (var i = 0; i < levelFn.length; i++) {
  25237. if (typeof levelFn[i] == "string") {
  25238. levelFn[i] = (function(fn) {
  25239. return function(node) {
  25240. return node.tagName == fn.toUpperCase();
  25241. };
  25242. })(levelFn[i]);
  25243. } else if (typeof levelFn[i] != "function") {
  25244. levelFn[i] = function(node) {
  25245. return null;
  25246. };
  25247. }
  25248. }
  25249. function getSectionLevel(node) {
  25250. for (var i = 0; i < levelFn.length; i++) {
  25251. if (levelFn[i](node)) return i;
  25252. }
  25253. return -1;
  25254. }
  25255. var me = this,
  25256. Directory = getSection({ level: -1, title: "root" }),
  25257. previous = Directory;
  25258. function traversal(node, Directory) {
  25259. var level,
  25260. tmpSection = null,
  25261. parent,
  25262. child,
  25263. children = node.childNodes;
  25264. for (var i = 0, len = children.length; i < len; i++) {
  25265. child = children[i];
  25266. level = getSectionLevel(child);
  25267. if (level >= 0) {
  25268. var address = me.selection
  25269. .getRange()
  25270. .selectNode(child)
  25271. .createAddress(true).startAddress,
  25272. current = getSection({
  25273. tag: child.tagName,
  25274. title: child.innerText || child.textContent || "",
  25275. level: level,
  25276. dom: child,
  25277. startAddress: utils.clone(address, []),
  25278. endAddress: utils.clone(address, []),
  25279. children: []
  25280. });
  25281. previous.nextSection = current;
  25282. current.previousSection = previous;
  25283. parent = previous;
  25284. while (level <= parent.level) {
  25285. parent = parent.parentSection;
  25286. }
  25287. current.parentSection = parent;
  25288. parent.children.push(current);
  25289. tmpSection = previous = current;
  25290. } else {
  25291. child.nodeType === 1 && traversal(child, Directory);
  25292. tmpSection &&
  25293. tmpSection.endAddress[tmpSection.endAddress.length - 1]++;
  25294. }
  25295. }
  25296. }
  25297. traversal(me.body, Directory);
  25298. return Directory;
  25299. },
  25300. notNeedUndo: true
  25301. },
  25302. movesection: {
  25303. execCommand: function(cmd, sourceSection, targetSection, isAfter) {
  25304. var me = this,
  25305. targetAddress,
  25306. target;
  25307. if (!sourceSection || !targetSection || targetSection.level == -1)
  25308. return;
  25309. targetAddress = isAfter
  25310. ? targetSection.endAddress
  25311. : targetSection.startAddress;
  25312. target = getNodeFromAddress(targetAddress, me.body);
  25313. /* 判断目标地址是否被源章节包含 */
  25314. if (
  25315. !targetAddress ||
  25316. !target ||
  25317. isContainsAddress(
  25318. sourceSection.startAddress,
  25319. sourceSection.endAddress,
  25320. targetAddress
  25321. )
  25322. )
  25323. return;
  25324. var startNode = getNodeFromAddress(
  25325. sourceSection.startAddress,
  25326. me.body
  25327. ),
  25328. endNode = getNodeFromAddress(sourceSection.endAddress, me.body),
  25329. current,
  25330. nextNode;
  25331. if (isAfter) {
  25332. current = endNode;
  25333. while (
  25334. current &&
  25335. !(
  25336. domUtils.getPosition(startNode, current) &
  25337. domUtils.POSITION_FOLLOWING
  25338. )
  25339. ) {
  25340. nextNode = current.previousSibling;
  25341. domUtils.insertAfter(target, current);
  25342. if (current == startNode) break;
  25343. current = nextNode;
  25344. }
  25345. } else {
  25346. current = startNode;
  25347. while (
  25348. current &&
  25349. !(
  25350. domUtils.getPosition(current, endNode) &
  25351. domUtils.POSITION_FOLLOWING
  25352. )
  25353. ) {
  25354. nextNode = current.nextSibling;
  25355. target.parentNode.insertBefore(current, target);
  25356. if (current == endNode) break;
  25357. current = nextNode;
  25358. }
  25359. }
  25360. me.fireEvent("updateSections");
  25361. /* 获取地址的包含关系 */
  25362. function isContainsAddress(startAddress, endAddress, addressTarget) {
  25363. var isAfterStartAddress = false,
  25364. isBeforeEndAddress = false;
  25365. for (var i = 0; i < startAddress.length; i++) {
  25366. if (i >= addressTarget.length) break;
  25367. if (addressTarget[i] > startAddress[i]) {
  25368. isAfterStartAddress = true;
  25369. break;
  25370. } else if (addressTarget[i] < startAddress[i]) {
  25371. break;
  25372. }
  25373. }
  25374. for (var i = 0; i < endAddress.length; i++) {
  25375. if (i >= addressTarget.length) break;
  25376. if (addressTarget[i] < startAddress[i]) {
  25377. isBeforeEndAddress = true;
  25378. break;
  25379. } else if (addressTarget[i] > startAddress[i]) {
  25380. break;
  25381. }
  25382. }
  25383. return isAfterStartAddress && isBeforeEndAddress;
  25384. }
  25385. }
  25386. },
  25387. deletesection: {
  25388. execCommand: function(cmd, section, keepChildren) {
  25389. var me = this;
  25390. if (!section) return;
  25391. function getNodeFromAddress(startAddress) {
  25392. var current = me.body;
  25393. for (var i = 0; i < startAddress.length; i++) {
  25394. if (!current.childNodes) return null;
  25395. current = current.childNodes[startAddress[i]];
  25396. }
  25397. return current;
  25398. }
  25399. var startNode = getNodeFromAddress(section.startAddress),
  25400. endNode = getNodeFromAddress(section.endAddress),
  25401. current = startNode,
  25402. nextNode;
  25403. if (!keepChildren) {
  25404. while (
  25405. current &&
  25406. domUtils.inDoc(endNode, me.document) &&
  25407. !(
  25408. domUtils.getPosition(current, endNode) &
  25409. domUtils.POSITION_FOLLOWING
  25410. )
  25411. ) {
  25412. nextNode = current.nextSibling;
  25413. domUtils.remove(current);
  25414. current = nextNode;
  25415. }
  25416. } else {
  25417. domUtils.remove(current);
  25418. }
  25419. me.fireEvent("updateSections");
  25420. }
  25421. },
  25422. selectsection: {
  25423. execCommand: function(cmd, section) {
  25424. if (!section && !section.dom) return false;
  25425. var me = this,
  25426. range = me.selection.getRange(),
  25427. address = {
  25428. startAddress: utils.clone(section.startAddress, []),
  25429. endAddress: utils.clone(section.endAddress, [])
  25430. };
  25431. address.endAddress[address.endAddress.length - 1]++;
  25432. range.moveToAddress(address).select().scrollToView();
  25433. return true;
  25434. },
  25435. notNeedUndo: true
  25436. },
  25437. scrolltosection: {
  25438. execCommand: function(cmd, section) {
  25439. if (!section && !section.dom) return false;
  25440. var me = this,
  25441. range = me.selection.getRange(),
  25442. address = {
  25443. startAddress: section.startAddress,
  25444. endAddress: section.endAddress
  25445. };
  25446. address.endAddress[address.endAddress.length - 1]++;
  25447. range.moveToAddress(address).scrollToView();
  25448. return true;
  25449. },
  25450. notNeedUndo: true
  25451. }
  25452. }
  25453. };
  25454. });
  25455. // plugins/simpleupload.js
  25456. /**
  25457. * @description
  25458. * 简单上传:点击按钮,直接选择文件上传
  25459. * @author Jinqn
  25460. * @date 2014-03-31
  25461. */
  25462. UE.plugin.register("simpleupload", function () {
  25463. var me = this,
  25464. isLoaded = false,
  25465. containerBtn;
  25466. function initUploadBtn() {
  25467. var w = containerBtn.offsetWidth || 20,
  25468. h = containerBtn.offsetHeight || 20,
  25469. btnIframe = document.createElement("iframe"),
  25470. btnStyle =
  25471. "display:block;width:" +
  25472. w +
  25473. "px;height:" +
  25474. h +
  25475. "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;";
  25476. domUtils.on(btnIframe, "load", function () {
  25477. var timestrap = (+new Date()).toString(36),
  25478. wrapper,
  25479. btnIframeDoc,
  25480. btnIframeBody;
  25481. var imageCompressEnable = me.getOpt('imageCompressEnable'),
  25482. imageMaxSize = me.getOpt('imageMaxSize'),
  25483. imageCompressBorder = me.getOpt('imageCompressBorder');
  25484. // console.log('simpleupload.compress',imageCompressEnable, imageMaxSize, imageCompressBorder);
  25485. btnIframeDoc =
  25486. btnIframe.contentDocument || btnIframe.contentWindow.document;
  25487. btnIframeBody = btnIframeDoc.body;
  25488. wrapper = btnIframeDoc.createElement("div");
  25489. wrapper.innerHTML =
  25490. '<form id="edui_form_' +
  25491. timestrap +
  25492. '" target="edui_iframe_' +
  25493. timestrap +
  25494. '" method="POST" enctype="multipart/form-data" action="' +
  25495. me.getOpt("serverUrl") +
  25496. '" ' +
  25497. 'style="' +
  25498. btnStyle +
  25499. '">' +
  25500. '<input id="edui_input_' +
  25501. timestrap +
  25502. '" type="file" accept="image/*" name="' +
  25503. me.options.imageFieldName +
  25504. '" ' +
  25505. 'style="' +
  25506. btnStyle +
  25507. '">' +
  25508. "</form>" +
  25509. '<iframe id="edui_iframe_' +
  25510. timestrap +
  25511. '" name="edui_iframe_' +
  25512. timestrap +
  25513. '" style="display:none;width:0;height:0;border:0;margin:0;padding:0;position:absolute;"></iframe>';
  25514. wrapper.className = "edui-" + me.options.theme;
  25515. wrapper.id = me.ui.id + "_iframeupload";
  25516. btnIframeBody.style.cssText = btnStyle;
  25517. btnIframeBody.style.width = w + "px";
  25518. btnIframeBody.style.height = h + "px";
  25519. btnIframeBody.appendChild(wrapper);
  25520. if (btnIframeBody.parentNode) {
  25521. btnIframeBody.parentNode.style.width = w + "px";
  25522. btnIframeBody.parentNode.style.height = w + "px";
  25523. }
  25524. var form = btnIframeDoc.getElementById("edui_form_" + timestrap);
  25525. var input = btnIframeDoc.getElementById("edui_input_" + timestrap);
  25526. var iframe = btnIframeDoc.getElementById("edui_iframe_" + timestrap);
  25527. domUtils.on(input, "change", function () {
  25528. if (!input.value) return;
  25529. var loadingId = "loading_" + (+new Date()).toString(36);
  25530. var params = utils.serializeParam(me.queryCommandValue("serverparam")) || "";
  25531. var imageActionUrl = me.getActionUrl(me.getOpt("imageActionName"));
  25532. var allowFiles = me.getOpt("imageAllowFiles");
  25533. var targetActionUrl = utils.formatUrl(imageActionUrl + (imageActionUrl.indexOf("?") === -1 ? "?" : "&") + params);
  25534. me.focus();
  25535. me.execCommand(
  25536. "inserthtml",
  25537. '<img class="loadingclass" id="' +
  25538. loadingId +
  25539. '" src="' +
  25540. me.options.themePath +
  25541. me.options.theme +
  25542. '/images/spacer.gif">'
  25543. );
  25544. function showErrorLoader(title) {
  25545. if (loadingId) {
  25546. var loader = me.document.getElementById(loadingId);
  25547. loader && domUtils.remove(loader);
  25548. me.fireEvent("showmessage", {
  25549. id: loadingId,
  25550. content: title,
  25551. type: "error",
  25552. timeout: 4000
  25553. });
  25554. }
  25555. alert(title);
  25556. }
  25557. // 判断后端配置是否没有加载成功
  25558. if (!me.getOpt("imageActionName")) {
  25559. showErrorLoader(me.getLang("autoupload.errorLoadConfig"));
  25560. return;
  25561. }
  25562. // 判断文件格式是否错误
  25563. var filename = input.value, fileext = filename ? filename.substr(filename.lastIndexOf(".")) : "";
  25564. if (
  25565. !fileext ||
  25566. (allowFiles &&
  25567. (allowFiles.join("") + ".").indexOf(fileext.toLowerCase() + ".") === -1)
  25568. ) {
  25569. showErrorLoader(me.getLang("simpleupload.exceedTypeError"));
  25570. return;
  25571. }
  25572. function callback(json, cb) {
  25573. try {
  25574. var link = me.options.imageUrlPrefix + json.url;
  25575. if (json.state === "SUCCESS" && json.url) {
  25576. var loader = me.document.getElementById(loadingId);
  25577. domUtils.removeClasses(loader, "loadingclass");
  25578. loader.setAttribute("src", link);
  25579. loader.setAttribute("_src", link);
  25580. loader.setAttribute("alt", json.original || "");
  25581. loader.removeAttribute("id");
  25582. me.fireEvent("contentchange");
  25583. } else {
  25584. showErrorLoader && showErrorLoader(json.state);
  25585. }
  25586. } catch (er) {
  25587. showErrorLoader &&
  25588. showErrorLoader(me.getLang("simpleupload.loadError"));
  25589. }
  25590. form.reset();
  25591. cb && cb();
  25592. }
  25593. function uploadDirect() {
  25594. domUtils.on(iframe, "load", function () {
  25595. var body = (iframe.contentDocument || iframe.contentWindow.document).body;
  25596. var result = body.innerText || body.textContent || "";
  25597. var json;
  25598. try {
  25599. json = new Function("return " + result)();
  25600. if (!json) {
  25601. json = {state: 'ERROR:UPLOAD_RESULT_EMPTY'};
  25602. }
  25603. } catch (e) {
  25604. json = {state: 'ERROR:UPLOAD_FAIL'};
  25605. }
  25606. callback(json, function () {
  25607. domUtils.un(iframe, "load", callback);
  25608. });
  25609. });
  25610. form.action = targetActionUrl;
  25611. form.submit();
  25612. }
  25613. var compressSupport = false;
  25614. if (imageCompressEnable) {
  25615. compressSupport = true;
  25616. }
  25617. // 类型映射
  25618. var imageMimeMap = {
  25619. '.jpg': 'image/jpeg',
  25620. '.jpeg': 'image/jpeg',
  25621. '.png': 'image/png',
  25622. '.gif': 'image/gif',
  25623. }
  25624. var imageMimeType
  25625. if (fileext.toLowerCase() in imageMimeMap) {
  25626. imageMimeType = imageMimeMap[fileext.toLowerCase()]
  25627. } else {
  25628. compressSupport = false;
  25629. }
  25630. var fileBaseName = input.files[0].name;
  25631. if (compressSupport) {
  25632. var img = new Image();
  25633. img.onload = function () {
  25634. var w = img.width, h = img.height, rate;
  25635. if (w > imageCompressBorder) {
  25636. rate = w / imageCompressBorder;
  25637. w = imageCompressBorder;
  25638. h = Math.round(h / rate);
  25639. } else if (h > imageCompressBorder) {
  25640. rate = h / imageCompressBorder;
  25641. h = imageCompressBorder;
  25642. w = Math.round(w / rate);
  25643. }
  25644. var canvas = document.createElement('canvas');
  25645. var ctx = canvas.getContext('2d');
  25646. canvas.setAttribute('width', w);
  25647. canvas.setAttribute('height', h);
  25648. ctx.drawImage(img, 0, 0, w, h);
  25649. canvas.toBlob(function (blob) {
  25650. var xhr = new XMLHttpRequest(), fd = new FormData()
  25651. fd.append(me.options.imageFieldName, blob, fileBaseName);
  25652. xhr.open("post", targetActionUrl, true);
  25653. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  25654. xhr.addEventListener("load", function (e) {
  25655. try {
  25656. var json = new Function("return " + utils.trim(e.target.response))();
  25657. if (json.state === "SUCCESS" && json.url) {
  25658. callback(json);
  25659. } else {
  25660. showErrorLoader(json.state);
  25661. }
  25662. } catch (er) {
  25663. showErrorLoader(me.getLang("autoupload.loadError"));
  25664. }
  25665. });
  25666. xhr.send(fd);
  25667. }, imageMimeType, 0.8);
  25668. };
  25669. img.src = URL.createObjectURL(input.files[0]);
  25670. } else {
  25671. // 无需压缩直接上传
  25672. uploadDirect();
  25673. }
  25674. });
  25675. var stateTimer;
  25676. me.addListener("selectionchange", function () {
  25677. clearTimeout(stateTimer);
  25678. stateTimer = setTimeout(function () {
  25679. var state = me.queryCommandState("simpleupload");
  25680. if (state == -1) {
  25681. input.disabled = "disabled";
  25682. } else {
  25683. input.disabled = false;
  25684. }
  25685. }, 400);
  25686. });
  25687. isLoaded = true;
  25688. });
  25689. btnIframe.style.cssText = btnStyle;
  25690. containerBtn.appendChild(btnIframe);
  25691. }
  25692. return {
  25693. bindEvents: {
  25694. ready: function () {
  25695. //设置loading的样式
  25696. utils.cssRule(
  25697. "loading",
  25698. ".loadingclass{display:inline-block;cursor:default;background: url('" +
  25699. this.options.themePath +
  25700. this.options.theme +
  25701. "/images/loading.gif') no-repeat center center transparent;border-radius:3px;outline:1px solid #EEE;margin-right:1px;height:22px;width:22px;}\n" +
  25702. ".loaderrorclass{display:inline-block;cursor:default;background: url('" +
  25703. this.options.themePath +
  25704. this.options.theme +
  25705. "/images/loaderror.png') no-repeat center center transparent;border-radius:3px;outline:1px solid #EEE;margin-right:1px;height:22px;width:22px;" +
  25706. "}",
  25707. this.document
  25708. );
  25709. },
  25710. /* 初始化简单上传按钮 */
  25711. simpleuploadbtnready: function (type, container) {
  25712. containerBtn = container;
  25713. me.afterConfigReady(initUploadBtn);
  25714. }
  25715. },
  25716. outputRule: function (root) {
  25717. utils.each(root.getNodesByTagName("img"), function (n) {
  25718. if (/\b(loaderrorclass)|(bloaderrorclass)\b/.test(n.getAttr("class"))) {
  25719. n.parentNode.removeChild(n);
  25720. }
  25721. });
  25722. },
  25723. commands: {
  25724. simpleupload: {
  25725. queryCommandState: function () {
  25726. return isLoaded ? 0 : -1;
  25727. }
  25728. }
  25729. }
  25730. };
  25731. });
  25732. // plugins/serverparam.js
  25733. /**
  25734. * 服务器提交的额外参数列表设置插件
  25735. * @file
  25736. * @since 1.2.6.1
  25737. */
  25738. UE.plugin.register("serverparam", function() {
  25739. var me = this,
  25740. serverParam = {};
  25741. return {
  25742. commands: {
  25743. /**
  25744. * 修改服务器提交的额外参数列表,清除所有项
  25745. * @command serverparam
  25746. * @method execCommand
  25747. * @param { String } cmd 命令字符串
  25748. * @example
  25749. * ```javascript
  25750. * editor.execCommand('serverparam');
  25751. * editor.queryCommandValue('serverparam'); //返回空
  25752. * ```
  25753. */
  25754. /**
  25755. * 修改服务器提交的额外参数列表,删除指定项
  25756. * @command serverparam
  25757. * @method execCommand
  25758. * @param { String } cmd 命令字符串
  25759. * @param { String } key 要清除的属性
  25760. * @example
  25761. * ```javascript
  25762. * editor.execCommand('serverparam', 'name'); //删除属性name
  25763. * ```
  25764. */
  25765. /**
  25766. * 修改服务器提交的额外参数列表,使用键值添加项
  25767. * @command serverparam
  25768. * @method execCommand
  25769. * @param { String } cmd 命令字符串
  25770. * @param { String } key 要添加的属性
  25771. * @param { String } value 要添加属性的值
  25772. * @example
  25773. * ```javascript
  25774. * editor.execCommand('serverparam', 'name', 'hello');
  25775. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  25776. * ```
  25777. */
  25778. /**
  25779. * 修改服务器提交的额外参数列表,传入键值对对象添加多项
  25780. * @command serverparam
  25781. * @method execCommand
  25782. * @param { String } cmd 命令字符串
  25783. * @param { Object } key 传入的键值对对象
  25784. * @example
  25785. * ```javascript
  25786. * editor.execCommand('serverparam', {'name': 'hello'});
  25787. * editor.queryCommandValue('serverparam'); //返回对象 {'name': 'hello'}
  25788. * ```
  25789. */
  25790. /**
  25791. * 修改服务器提交的额外参数列表,使用自定义函数添加多项
  25792. * @command serverparam
  25793. * @method execCommand
  25794. * @param { String } cmd 命令字符串
  25795. * @param { Function } key 自定义获取参数的函数
  25796. * @example
  25797. * ```javascript
  25798. * editor.execCommand('serverparam', function(editor){
  25799. * return {'key': 'value'};
  25800. * });
  25801. * editor.queryCommandValue('serverparam'); //返回对象 {'key': 'value'}
  25802. * ```
  25803. */
  25804. /**
  25805. * 获取服务器提交的额外参数列表
  25806. * @command serverparam
  25807. * @method queryCommandValue
  25808. * @param { String } cmd 命令字符串
  25809. * @example
  25810. * ```javascript
  25811. * editor.queryCommandValue( 'serverparam' ); //返回对象 {'key': 'value'}
  25812. * ```
  25813. */
  25814. serverparam: {
  25815. execCommand: function(cmd, key, value) {
  25816. if (key === undefined || key === null) {
  25817. //不传参数,清空列表
  25818. serverParam = {};
  25819. } else if (utils.isString(key)) {
  25820. //传入键值
  25821. if (value === undefined || value === null) {
  25822. delete serverParam[key];
  25823. } else {
  25824. serverParam[key] = value;
  25825. }
  25826. } else if (utils.isObject(key)) {
  25827. //传入对象,覆盖列表项
  25828. utils.extend(serverParam, key, false);
  25829. } else if (utils.isFunction(key)) {
  25830. //传入函数,添加列表项
  25831. utils.extend(serverParam, key(), false);
  25832. }
  25833. },
  25834. queryCommandValue: function() {
  25835. return serverParam || {};
  25836. }
  25837. }
  25838. }
  25839. };
  25840. });
  25841. // plugins/insertfile.js
  25842. /**
  25843. * 插入附件
  25844. */
  25845. UE.plugin.register("insertfile", function() {
  25846. var me = this;
  25847. function getFileIcon(url) {
  25848. var ext = url.substr(url.lastIndexOf(".") + 1).toLowerCase(),
  25849. maps = {
  25850. "doc":"doc.svg",
  25851. "docx":"docx.svg",
  25852. "gif":"gif.svg",
  25853. "jpeg":"jpeg.svg",
  25854. "jpg":"jpg.svg",
  25855. "mp3":"mp3.svg",
  25856. "mp4":"mp4.svg",
  25857. "pdf":"pdf.svg",
  25858. "png":"png.svg",
  25859. "ppt":"ppt.svg",
  25860. "pptx":"pptx.svg",
  25861. "rar":"rar.svg",
  25862. "torrent":"torrent.svg",
  25863. "txt":"txt.svg",
  25864. "unknown":"unknown.svg",
  25865. "xls":"xls.svg",
  25866. "xlsx":"xlsx.svg",
  25867. "zip":"zip.svg",
  25868. };
  25869. return maps[ext] ? maps[ext] : maps["unknown"];
  25870. }
  25871. return {
  25872. commands: {
  25873. insertfile: {
  25874. execCommand: function(command, filelist) {
  25875. filelist = utils.isArray(filelist) ? filelist : [filelist];
  25876. if (me.fireEvent("beforeinsertfile", filelist) === true) {
  25877. return;
  25878. }
  25879. //console.log('themePath', );
  25880. var i,
  25881. item,
  25882. icon,
  25883. title,
  25884. html = "",
  25885. URL = me.getOpt("UEDITOR_HOME_URL"),
  25886. iconDir = me.options.themePath + me.options.theme + "/exts/";
  25887. for (i = 0; i < filelist.length; i++) {
  25888. item = filelist[i];
  25889. icon = iconDir + getFileIcon(item.url);
  25890. title =
  25891. item.title || item.url.substr(item.url.lastIndexOf("/") + 1);
  25892. html +=
  25893. '<p>' +
  25894. '<a style="background:#EEE;padding:10px;border-radius:5px;line-height:1.5em;display:inline-flex;align-items:center;" href="' +
  25895. item.url +
  25896. '" title="' +
  25897. title +
  25898. '" target="_blank">' +
  25899. '<img style="vertical-align:middle;margin-right:0.5em;height:1.5em;" src="' + icon + '" _src="' + icon + '" />' +
  25900. '<span style="color:#111111;line-height:1.5em;flex-grow:1;">' +
  25901. title +
  25902. "</span>" +
  25903. "</a>" +
  25904. "</p>";
  25905. }
  25906. me.execCommand("insertHtml", html);
  25907. me.fireEvent("afterinsertfile", filelist);
  25908. }
  25909. }
  25910. }
  25911. };
  25912. });
  25913. // plugins/markdown-shortcut.js
  25914. UE.plugins["markdown-shortcut"] = function () {
  25915. let me = this;
  25916. const uiUtils = UE.ui.uiUtils;
  25917. const getCleanHtml = function (node) {
  25918. let html = node.innerHTML
  25919. html = html.replace(/[\u200b]*/g, '')
  25920. return html
  25921. }
  25922. let shortCuts = [];
  25923. for (let i = 1; i <= 6; i++) {
  25924. let command = 'h' + i;
  25925. const regExp = new RegExp('^\\t?' + Array(i + 1).join('#') + '(\\s|&nbsp;)');
  25926. shortCuts.push({
  25927. tagName: ['P'],
  25928. key: [' '],
  25929. offset: [i + 1, i + 2],
  25930. match: [regExp],
  25931. callback: function (param) {
  25932. me.__hasEnterExecCommand = true;
  25933. me.execCommand('paragraph', command);
  25934. setTimeout(function () {
  25935. let range = me.selection.getRange();
  25936. let node = range.startContainer;
  25937. // safari 下不会自动选中Hx标签
  25938. if (node.tagName !== 'H' + i) {
  25939. node = node.parentNode
  25940. }
  25941. let html = getCleanHtml(node)
  25942. html = html.replace(regExp, '');
  25943. if (!html) {
  25944. html = domUtils.fillChar;
  25945. }
  25946. node.innerHTML = html;
  25947. me.__hasEnterExecCommand = false;
  25948. }, 0);
  25949. }
  25950. })
  25951. }
  25952. me.on("ready", function () {
  25953. // let quickOperate = null
  25954. // domUtils.on(me.body, "mouseover", function (evt) {
  25955. // const node = evt.target
  25956. // const rect = node.getBoundingClientRect();
  25957. // const offset = uiUtils.getClientRect(node)
  25958. // offset.left = offset.left - 60
  25959. // console.log('mouseover', rect, node, offset);
  25960. // // var offset = uiUtils.getViewportOffsetByEvent(evt);
  25961. // if (quickOperate) {
  25962. // quickOperate.destroy();
  25963. // }
  25964. // quickOperate = new UE.ui.QuickOperate({
  25965. // // items: contextItems,
  25966. // className: "edui-quick-operate",
  25967. // editor: me
  25968. // });
  25969. // // console.log('quickOperate', quickOperate);
  25970. // quickOperate.render();
  25971. // quickOperate.showAt(offset);
  25972. // });
  25973. domUtils.on(me.body, "keyup", function (e) {
  25974. let range = me.selection.getRange();
  25975. if (range.endOffset !== range.startOffset) {
  25976. return;
  25977. }
  25978. let key = e.key;
  25979. let offset = range.startOffset;
  25980. const node = range.startContainer.parentNode;
  25981. let html = getCleanHtml(node);
  25982. let tagName = node.tagName;
  25983. // console.log('keyup', [node, range, tagName, key, offset, html]);
  25984. for (let s of shortCuts) {
  25985. if (!s.tagName.includes(tagName)) {
  25986. continue;
  25987. }
  25988. if (!s.key.includes(key)) {
  25989. continue;
  25990. }
  25991. if (!s.offset.includes(offset)) {
  25992. continue;
  25993. }
  25994. for (let m of s.match) {
  25995. let match = html.match(m);
  25996. // console.log('keyup', [html, m, match]);
  25997. if (match) {
  25998. s.callback({
  25999. node: node,
  26000. });
  26001. break;
  26002. }
  26003. }
  26004. }
  26005. });
  26006. });
  26007. };
  26008. // ui/ui.js
  26009. var baidu = baidu || {};
  26010. baidu.editor = baidu.editor || {};
  26011. UE.ui = baidu.editor.ui = {};
  26012. // ui/uiutils.js
  26013. (function() {
  26014. var browser = baidu.editor.browser,
  26015. domUtils = baidu.editor.dom.domUtils;
  26016. var magic = "$EDITORUI";
  26017. var root = (window[magic] = {});
  26018. var uidMagic = "ID" + magic;
  26019. var uidCount = 0;
  26020. var uiUtils = (baidu.editor.ui.uiUtils = {
  26021. uid: function(obj) {
  26022. return obj ? obj[uidMagic] || (obj[uidMagic] = ++uidCount) : ++uidCount;
  26023. },
  26024. hook: function(fn, callback) {
  26025. var dg;
  26026. if (fn && fn._callbacks) {
  26027. dg = fn;
  26028. } else {
  26029. dg = function() {
  26030. var q;
  26031. if (fn) {
  26032. q = fn.apply(this, arguments);
  26033. }
  26034. var callbacks = dg._callbacks;
  26035. var k = callbacks.length;
  26036. while (k--) {
  26037. var r = callbacks[k].apply(this, arguments);
  26038. if (q === undefined) {
  26039. q = r;
  26040. }
  26041. }
  26042. return q;
  26043. };
  26044. dg._callbacks = [];
  26045. }
  26046. dg._callbacks.push(callback);
  26047. return dg;
  26048. },
  26049. createElementByHtml: function(html) {
  26050. var el = document.createElement("div");
  26051. el.innerHTML = html;
  26052. el = el.firstChild;
  26053. el.parentNode.removeChild(el);
  26054. return el;
  26055. },
  26056. getViewportElement: function() {
  26057. return browser.ie && browser.quirks
  26058. ? document.body
  26059. : document.documentElement;
  26060. },
  26061. getClientRect: function(element) {
  26062. var bcr;
  26063. //trace IE6下在控制编辑器显隐时可能会报错,catch一下
  26064. try {
  26065. bcr = element.getBoundingClientRect();
  26066. } catch (e) {
  26067. bcr = { left: 0, top: 0, height: 0, width: 0 };
  26068. }
  26069. var rect = {
  26070. left: Math.round(bcr.left),
  26071. top: Math.round(bcr.top),
  26072. height: Math.round(bcr.bottom - bcr.top),
  26073. width: Math.round(bcr.right - bcr.left)
  26074. };
  26075. var doc;
  26076. while (
  26077. (doc = element.ownerDocument) !== document &&
  26078. (element = domUtils.getWindow(doc).frameElement)
  26079. ) {
  26080. bcr = element.getBoundingClientRect();
  26081. rect.left += bcr.left;
  26082. rect.top += bcr.top;
  26083. }
  26084. rect.bottom = rect.top + rect.height;
  26085. rect.right = rect.left + rect.width;
  26086. return rect;
  26087. },
  26088. getViewportRect: function() {
  26089. var viewportEl = uiUtils.getViewportElement();
  26090. var width = (window.innerWidth || viewportEl.clientWidth) | 0;
  26091. var height = (window.innerHeight || viewportEl.clientHeight) | 0;
  26092. return {
  26093. left: 0,
  26094. top: 0,
  26095. height: height,
  26096. width: width,
  26097. bottom: height,
  26098. right: width
  26099. };
  26100. },
  26101. setViewportOffset: function(element, offset) {
  26102. var rect;
  26103. var fixedLayer = uiUtils.getFixedLayer();
  26104. if (element.parentNode === fixedLayer) {
  26105. element.style.left = offset.left + "px";
  26106. element.style.top = offset.top + "px";
  26107. } else {
  26108. domUtils.setViewportOffset(element, offset);
  26109. }
  26110. },
  26111. getEventOffset: function(evt) {
  26112. var el = evt.target || evt.srcElement;
  26113. var rect = uiUtils.getClientRect(el);
  26114. var offset = uiUtils.getViewportOffsetByEvent(evt);
  26115. return {
  26116. left: offset.left - rect.left,
  26117. top: offset.top - rect.top
  26118. };
  26119. },
  26120. getViewportOffsetByEvent: function(evt) {
  26121. var el = evt.target || evt.srcElement;
  26122. var frameEl = domUtils.getWindow(el).frameElement;
  26123. var offset = {
  26124. left: evt.clientX,
  26125. top: evt.clientY
  26126. };
  26127. if (frameEl && el.ownerDocument !== document) {
  26128. var rect = uiUtils.getClientRect(frameEl);
  26129. offset.left += rect.left;
  26130. offset.top += rect.top;
  26131. }
  26132. return offset;
  26133. },
  26134. setGlobal: function(id, obj) {
  26135. root[id] = obj;
  26136. return magic + '["' + id + '"]';
  26137. },
  26138. unsetGlobal: function(id) {
  26139. delete root[id];
  26140. },
  26141. copyAttributes: function(tgt, src) {
  26142. var attributes = src.attributes;
  26143. var k = attributes.length;
  26144. while (k--) {
  26145. var attrNode = attributes[k];
  26146. if (
  26147. attrNode.nodeName != "style" &&
  26148. attrNode.nodeName != "class" &&
  26149. (!browser.ie || attrNode.specified)
  26150. ) {
  26151. tgt.setAttribute(attrNode.nodeName, attrNode.nodeValue);
  26152. }
  26153. }
  26154. if (src.className) {
  26155. domUtils.addClass(tgt, src.className);
  26156. }
  26157. if (src.style.cssText) {
  26158. tgt.style.cssText += ";" + src.style.cssText;
  26159. }
  26160. },
  26161. removeStyle: function(el, styleName) {
  26162. if (el.style.removeProperty) {
  26163. el.style.removeProperty(styleName);
  26164. } else if (el.style.removeAttribute) {
  26165. el.style.removeAttribute(styleName);
  26166. } else throw "";
  26167. },
  26168. contains: function(elA, elB) {
  26169. return (
  26170. elA &&
  26171. elB &&
  26172. (elA === elB
  26173. ? false
  26174. : elA.contains
  26175. ? elA.contains(elB)
  26176. : elA.compareDocumentPosition(elB) & 16)
  26177. );
  26178. },
  26179. startDrag: function(evt, callbacks, doc) {
  26180. var doc = doc || document;
  26181. var startX = evt.clientX;
  26182. var startY = evt.clientY;
  26183. function handleMouseMove(evt) {
  26184. var x = evt.clientX - startX;
  26185. var y = evt.clientY - startY;
  26186. callbacks.ondragmove(x, y, evt);
  26187. if (evt.stopPropagation) {
  26188. evt.stopPropagation();
  26189. } else {
  26190. evt.cancelBubble = true;
  26191. }
  26192. }
  26193. if (doc.addEventListener) {
  26194. function handleMouseUp(evt) {
  26195. doc.removeEventListener("mousemove", handleMouseMove, true);
  26196. doc.removeEventListener("mouseup", handleMouseUp, true);
  26197. window.removeEventListener("mouseup", handleMouseUp, true);
  26198. callbacks.ondragstop();
  26199. }
  26200. doc.addEventListener("mousemove", handleMouseMove, true);
  26201. doc.addEventListener("mouseup", handleMouseUp, true);
  26202. window.addEventListener("mouseup", handleMouseUp, true);
  26203. evt.preventDefault();
  26204. } else {
  26205. var elm = evt.srcElement;
  26206. elm.setCapture();
  26207. function releaseCaptrue() {
  26208. elm.releaseCapture();
  26209. elm.detachEvent("onmousemove", handleMouseMove);
  26210. elm.detachEvent("onmouseup", releaseCaptrue);
  26211. elm.detachEvent("onlosecaptrue", releaseCaptrue);
  26212. callbacks.ondragstop();
  26213. }
  26214. elm.attachEvent("onmousemove", handleMouseMove);
  26215. elm.attachEvent("onmouseup", releaseCaptrue);
  26216. elm.attachEvent("onlosecaptrue", releaseCaptrue);
  26217. evt.returnValue = false;
  26218. }
  26219. callbacks.ondragstart();
  26220. },
  26221. getFixedLayer: function() {
  26222. var layer = document.getElementById("edui_fixedlayer");
  26223. if (layer == null) {
  26224. layer = document.createElement("div");
  26225. layer.id = "edui_fixedlayer";
  26226. document.body.appendChild(layer);
  26227. if (browser.ie && browser.version <= 8) {
  26228. layer.style.position = "absolute";
  26229. bindFixedLayer();
  26230. setTimeout(updateFixedOffset);
  26231. } else {
  26232. layer.style.position = "fixed";
  26233. }
  26234. layer.style.left = "0";
  26235. layer.style.top = "0";
  26236. layer.style.width = "0";
  26237. layer.style.height = "0";
  26238. layer.style.margin = "0";
  26239. }
  26240. return layer;
  26241. },
  26242. makeUnselectable: function(element) {
  26243. if (browser.opera || (browser.ie && browser.version < 9)) {
  26244. element.unselectable = "on";
  26245. if (element.hasChildNodes()) {
  26246. for (var i = 0; i < element.childNodes.length; i++) {
  26247. if (element.childNodes[i].nodeType == 1) {
  26248. uiUtils.makeUnselectable(element.childNodes[i]);
  26249. }
  26250. }
  26251. }
  26252. } else {
  26253. if (element.style.MozUserSelect !== undefined) {
  26254. element.style.MozUserSelect = "none";
  26255. } else if (element.style.WebkitUserSelect !== undefined) {
  26256. element.style.WebkitUserSelect = "none";
  26257. } else if (element.style.KhtmlUserSelect !== undefined) {
  26258. element.style.KhtmlUserSelect = "none";
  26259. }
  26260. }
  26261. }
  26262. });
  26263. function updateFixedOffset() {
  26264. var layer = document.getElementById("edui_fixedlayer");
  26265. uiUtils.setViewportOffset(layer, {
  26266. left: 0,
  26267. top: 0
  26268. });
  26269. // layer.style.display = 'none';
  26270. // layer.style.display = 'block';
  26271. //#trace: 1354
  26272. // setTimeout(updateFixedOffset);
  26273. }
  26274. function bindFixedLayer(adjOffset) {
  26275. domUtils.on(window, "scroll", updateFixedOffset);
  26276. domUtils.on(
  26277. window,
  26278. "resize",
  26279. baidu.editor.utils.defer(updateFixedOffset, 0, true)
  26280. );
  26281. }
  26282. })();
  26283. // ui/uibase.js
  26284. (function() {
  26285. var utils = baidu.editor.utils,
  26286. uiUtils = baidu.editor.ui.uiUtils,
  26287. EventBase = baidu.editor.EventBase,
  26288. UIBase = (baidu.editor.ui.UIBase = function() {});
  26289. UIBase.prototype = {
  26290. className: "",
  26291. uiName: "",
  26292. initOptions: function(options) {
  26293. var me = this;
  26294. for (var k in options) {
  26295. me[k] = options[k];
  26296. }
  26297. this.id = this.id || "edui" + uiUtils.uid();
  26298. },
  26299. initUIBase: function() {
  26300. this._globalKey = utils.unhtml(uiUtils.setGlobal(this.id, this));
  26301. },
  26302. render: function(holder) {
  26303. var html = this.renderHtml();
  26304. var el = uiUtils.createElementByHtml(html);
  26305. //by xuheng 给每个node添加class
  26306. var list = domUtils.getElementsByTagName(el, "*");
  26307. var theme = "edui-" + (this.theme || this.editor.options.theme);
  26308. var layer = document.getElementById("edui_fixedlayer");
  26309. for (var i = 0, node; (node = list[i++]); ) {
  26310. domUtils.addClass(node, theme);
  26311. }
  26312. domUtils.addClass(el, theme);
  26313. if (layer) {
  26314. layer.className = "";
  26315. domUtils.addClass(layer, theme);
  26316. }
  26317. var seatEl = this.getDom();
  26318. if (seatEl != null) {
  26319. seatEl.parentNode.replaceChild(el, seatEl);
  26320. uiUtils.copyAttributes(el, seatEl);
  26321. } else {
  26322. if (typeof holder == "string") {
  26323. holder = document.getElementById(holder);
  26324. }
  26325. holder = holder || uiUtils.getFixedLayer();
  26326. // console.log('Uibase.render',holder,el);
  26327. domUtils.addClass(holder, theme);
  26328. holder.appendChild(el);
  26329. }
  26330. this.postRender();
  26331. },
  26332. getDom: function(name) {
  26333. if (!name) {
  26334. return document.getElementById(this.id);
  26335. } else {
  26336. return document.getElementById(this.id + "_" + name);
  26337. }
  26338. },
  26339. postRender: function() {
  26340. this.fireEvent("postrender");
  26341. },
  26342. getHtmlTpl: function() {
  26343. return "";
  26344. },
  26345. formatHtml: function(tpl) {
  26346. var prefix = "edui-" + this.uiName;
  26347. return tpl
  26348. .replace(/##/g, this.id)
  26349. .replace(/%%-/g, this.uiName ? prefix + "-" : "")
  26350. .replace(/%%/g, (this.uiName ? prefix : "") + " " + this.className)
  26351. .replace(/\$\$/g, this._globalKey);
  26352. },
  26353. renderHtml: function() {
  26354. return this.formatHtml(this.getHtmlTpl());
  26355. },
  26356. dispose: function() {
  26357. var box = this.getDom();
  26358. if (box) baidu.editor.dom.domUtils.remove(box);
  26359. uiUtils.unsetGlobal(this.id);
  26360. }
  26361. };
  26362. utils.inherits(UIBase, EventBase);
  26363. })();
  26364. // ui/separator.js
  26365. (function() {
  26366. var utils = baidu.editor.utils,
  26367. UIBase = baidu.editor.ui.UIBase,
  26368. Separator = (baidu.editor.ui.Separator = function(options) {
  26369. this.initOptions(options);
  26370. this.initSeparator();
  26371. });
  26372. Separator.prototype = {
  26373. uiName: "separator",
  26374. initSeparator: function() {
  26375. this.initUIBase();
  26376. },
  26377. getHtmlTpl: function() {
  26378. return '<div id="##" class="edui-box %%"></div>';
  26379. }
  26380. };
  26381. utils.inherits(Separator, UIBase);
  26382. })();
  26383. // ui/mask.js
  26384. ///import core
  26385. ///import uicore
  26386. (function() {
  26387. var utils = baidu.editor.utils,
  26388. domUtils = baidu.editor.dom.domUtils,
  26389. UIBase = baidu.editor.ui.UIBase,
  26390. uiUtils = baidu.editor.ui.uiUtils;
  26391. var Mask = (baidu.editor.ui.Mask = function(options) {
  26392. this.initOptions(options);
  26393. this.initUIBase();
  26394. });
  26395. Mask.prototype = {
  26396. getHtmlTpl: function() {
  26397. return '<div id="##" class="edui-mask %%" onclick="return $$._onClick(event, this);" onmousedown="return $$._onMouseDown(event, this);"></div>';
  26398. },
  26399. postRender: function() {
  26400. var me = this;
  26401. domUtils.on(window, "resize", function() {
  26402. setTimeout(function() {
  26403. if (!me.isHidden()) {
  26404. me._fill();
  26405. }
  26406. });
  26407. });
  26408. },
  26409. show: function(zIndex) {
  26410. this._fill();
  26411. this.getDom().style.display = "";
  26412. this.getDom().style.zIndex = zIndex;
  26413. },
  26414. hide: function() {
  26415. this.getDom().style.display = "none";
  26416. this.getDom().style.zIndex = "";
  26417. },
  26418. isHidden: function() {
  26419. return this.getDom().style.display == "none";
  26420. },
  26421. _onMouseDown: function() {
  26422. return false;
  26423. },
  26424. _onClick: function(e, target) {
  26425. this.fireEvent("click", e, target);
  26426. },
  26427. _fill: function() {
  26428. var el = this.getDom();
  26429. var vpRect = uiUtils.getViewportRect();
  26430. el.style.width = vpRect.width + "px";
  26431. el.style.height = vpRect.height + "px";
  26432. }
  26433. };
  26434. utils.inherits(Mask, UIBase);
  26435. })();
  26436. // ui/popup.js
  26437. ///import core
  26438. ///import uicore
  26439. (function() {
  26440. var utils = baidu.editor.utils,
  26441. uiUtils = baidu.editor.ui.uiUtils,
  26442. domUtils = baidu.editor.dom.domUtils,
  26443. UIBase = baidu.editor.ui.UIBase,
  26444. Popup = (baidu.editor.ui.Popup = function(options) {
  26445. this.initOptions(options);
  26446. this.initPopup();
  26447. });
  26448. var allPopups = [];
  26449. function closeAllPopup(evt, el) {
  26450. for (var i = 0; i < allPopups.length; i++) {
  26451. var pop = allPopups[i];
  26452. if (!pop.isHidden()) {
  26453. if (pop.queryAutoHide(el) !== false) {
  26454. if (
  26455. evt &&
  26456. /scroll/gi.test(evt.type) &&
  26457. pop.className == "edui-wordpastepop"
  26458. )
  26459. return;
  26460. pop.hide();
  26461. }
  26462. }
  26463. }
  26464. if (allPopups.length) pop.editor.fireEvent("afterhidepop");
  26465. }
  26466. Popup.postHide = closeAllPopup;
  26467. var ANCHOR_CLASSES = [
  26468. "edui-anchor-topleft",
  26469. "edui-anchor-topright",
  26470. "edui-anchor-bottomleft",
  26471. "edui-anchor-bottomright"
  26472. ];
  26473. Popup.prototype = {
  26474. SHADOW_RADIUS: 5,
  26475. content: null,
  26476. _hidden: false,
  26477. autoRender: true,
  26478. canSideLeft: true,
  26479. canSideUp: true,
  26480. initPopup: function() {
  26481. this.initUIBase();
  26482. allPopups.push(this);
  26483. },
  26484. getHtmlTpl: function() {
  26485. return (
  26486. '<div id="##" class="edui-popup %%" onmousedown="return false;">' +
  26487. ' <div id="##_body" class="edui-popup-body">' +
  26488. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  26489. ' <div class="edui-shadow"></div>' +
  26490. ' <div id="##_content" class="edui-popup-content">' +
  26491. this.getContentHtmlTpl() +
  26492. " </div>" +
  26493. " </div>" +
  26494. "</div>"
  26495. );
  26496. },
  26497. getContentHtmlTpl: function() {
  26498. if (this.content) {
  26499. if (typeof this.content == "string") {
  26500. return this.content;
  26501. }
  26502. return this.content.renderHtml();
  26503. } else {
  26504. return "";
  26505. }
  26506. },
  26507. _UIBase_postRender: UIBase.prototype.postRender,
  26508. postRender: function() {
  26509. if (this.content instanceof UIBase) {
  26510. this.content.postRender();
  26511. }
  26512. //捕获鼠标滚轮
  26513. if (this.captureWheel && !this.captured) {
  26514. this.captured = true;
  26515. var winHeight =
  26516. (document.documentElement.clientHeight ||
  26517. document.body.clientHeight) - 80,
  26518. _height = this.getDom().offsetHeight,
  26519. _top = uiUtils.getClientRect(this.combox.getDom()).top,
  26520. content = this.getDom("content"),
  26521. ifr = this.getDom("body").getElementsByTagName("iframe"),
  26522. me = this;
  26523. ifr.length && (ifr = ifr[0]);
  26524. while (_top + _height > winHeight) {
  26525. _height -= 30;
  26526. }
  26527. content.style.height = _height + "px";
  26528. //同步更改iframe高度
  26529. ifr && (ifr.style.height = _height + "px");
  26530. //阻止在combox上的鼠标滚轮事件, 防止用户的正常操作被误解
  26531. domUtils.on(
  26532. content,
  26533. "onmousewheel" in document.body ? "mousewheel" : "DOMMouseScroll",
  26534. function(e) {
  26535. if (e.preventDefault) {
  26536. e.preventDefault();
  26537. } else {
  26538. e.returnValue = false;
  26539. }
  26540. if (e.wheelDelta) {
  26541. content.scrollTop -= e.wheelDelta / 120 * 60;
  26542. } else {
  26543. content.scrollTop -= e.detail / -3 * 60;
  26544. }
  26545. }
  26546. );
  26547. }
  26548. this.fireEvent("postRenderAfter");
  26549. this.hide(true);
  26550. this._UIBase_postRender();
  26551. },
  26552. _doAutoRender: function() {
  26553. if (!this.getDom() && this.autoRender) {
  26554. this.render();
  26555. }
  26556. },
  26557. mesureSize: function() {
  26558. var box = this.getDom("content");
  26559. return uiUtils.getClientRect(box);
  26560. },
  26561. fitSize: function() {
  26562. // console.log('fitSize.popup')
  26563. if (this.captureWheel && this.sized) {
  26564. return this.__size;
  26565. }
  26566. this.sized = true;
  26567. var popBodyEl = this.getDom("body");
  26568. popBodyEl.style.width = "";
  26569. popBodyEl.style.height = "";
  26570. var size = this.mesureSize();
  26571. if (this.captureWheel) {
  26572. popBodyEl.style.width = -(-20 - size.width) + "px";
  26573. var height = parseInt(this.getDom("content").style.height, 10);
  26574. !window.isNaN(height) && (size.height = height);
  26575. } else {
  26576. popBodyEl.style.width = size.width + "px";
  26577. }
  26578. popBodyEl.style.height = size.height + "px";
  26579. this.__size = size;
  26580. this.captureWheel && (this.getDom("content").style.overflow = "auto");
  26581. return size;
  26582. },
  26583. showAnchor: function(element, hoz) {
  26584. this.showAnchorRect(uiUtils.getClientRect(element), hoz);
  26585. },
  26586. showAnchorRect: function(rect, hoz, adj) {
  26587. this._doAutoRender();
  26588. var vpRect = uiUtils.getViewportRect();
  26589. this.getDom().style.visibility = "hidden";
  26590. this._show();
  26591. var popSize = this.fitSize();
  26592. var sideLeft, sideUp, left, top;
  26593. if (hoz) {
  26594. sideLeft =
  26595. this.canSideLeft &&
  26596. (rect.right + popSize.width > vpRect.right &&
  26597. rect.left > popSize.width);
  26598. sideUp =
  26599. this.canSideUp &&
  26600. (rect.top + popSize.height > vpRect.bottom &&
  26601. rect.bottom > popSize.height);
  26602. left = sideLeft ? rect.left - popSize.width : rect.right;
  26603. top = sideUp ? rect.bottom - popSize.height : rect.top;
  26604. } else {
  26605. sideLeft =
  26606. this.canSideLeft &&
  26607. (rect.right + popSize.width > vpRect.right &&
  26608. rect.left > popSize.width);
  26609. sideUp =
  26610. this.canSideUp &&
  26611. (rect.top + popSize.height > vpRect.bottom &&
  26612. rect.bottom > popSize.height);
  26613. left = sideLeft ? rect.right - popSize.width : rect.left;
  26614. top = sideUp ? rect.top - popSize.height : rect.bottom;
  26615. }
  26616. if(!sideUp){
  26617. if(top + popSize.height > vpRect.bottom){
  26618. top = vpRect.bottom - popSize.height
  26619. }
  26620. }
  26621. // console.log('popup.showAnchorRect', vpRect, rect, hoz, sideUp, sideLeft, left, top);
  26622. var popEl = this.getDom();
  26623. uiUtils.setViewportOffset(popEl, {
  26624. left: left,
  26625. top: top
  26626. });
  26627. domUtils.removeClasses(popEl, ANCHOR_CLASSES);
  26628. popEl.className +=
  26629. " " + ANCHOR_CLASSES[(sideUp ? 1 : 0) * 2 + (sideLeft ? 1 : 0)];
  26630. if (this.editor) {
  26631. popEl.style.zIndex = this.editor.container.style.zIndex * 1 + 10;
  26632. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  26633. popEl.style.zIndex - 1;
  26634. }
  26635. this.getDom().style.visibility = "visible";
  26636. },
  26637. showAt: function(offset) {
  26638. var left = offset.left;
  26639. var top = offset.top;
  26640. var rect = {
  26641. left: left,
  26642. top: top,
  26643. right: left,
  26644. bottom: top,
  26645. height: 0,
  26646. width: 0
  26647. };
  26648. this.showAnchorRect(rect, false, true);
  26649. },
  26650. _show: function() {
  26651. if (this._hidden) {
  26652. var box = this.getDom();
  26653. box.style.display = "";
  26654. this._hidden = false;
  26655. // if (box.setActive) {
  26656. // box.setActive();
  26657. // }
  26658. this.fireEvent("show");
  26659. }
  26660. },
  26661. isHidden: function() {
  26662. return this._hidden;
  26663. },
  26664. show: function() {
  26665. this._doAutoRender();
  26666. this._show();
  26667. },
  26668. hide: function(notNofity) {
  26669. if (!this._hidden && this.getDom()) {
  26670. this.getDom().style.display = "none";
  26671. this._hidden = true;
  26672. if (!notNofity) {
  26673. this.fireEvent("hide");
  26674. }
  26675. }
  26676. },
  26677. queryAutoHide: function(el) {
  26678. return !el || !uiUtils.contains(this.getDom(), el);
  26679. }
  26680. };
  26681. utils.inherits(Popup, UIBase);
  26682. domUtils.on(document, "mousedown", function(evt) {
  26683. var el = evt.target || evt.srcElement;
  26684. closeAllPopup(evt, el);
  26685. });
  26686. domUtils.on(window, "scroll", function(evt, el) {
  26687. closeAllPopup(evt, el);
  26688. });
  26689. })();
  26690. // ui/colorpicker.js
  26691. ///import core
  26692. ///import uicore
  26693. (function() {
  26694. var utils = baidu.editor.utils,
  26695. UIBase = baidu.editor.ui.UIBase,
  26696. ColorPicker = (baidu.editor.ui.ColorPicker = function(options) {
  26697. this.initOptions(options);
  26698. this.noColorText = this.noColorText || this.editor.getLang("clearColor");
  26699. this.initUIBase();
  26700. });
  26701. ColorPicker.prototype = {
  26702. getHtmlTpl: function() {
  26703. return genColorPicker(this.noColorText, this.editor);
  26704. },
  26705. _onTableClick: function(evt) {
  26706. var tgt = evt.target || evt.srcElement;
  26707. var color = tgt.getAttribute("data-color");
  26708. if (color) {
  26709. this.fireEvent("pickcolor", color);
  26710. }
  26711. },
  26712. _onTableOver: function(evt) {
  26713. var tgt = evt.target || evt.srcElement;
  26714. var color = tgt.getAttribute("data-color");
  26715. if (color) {
  26716. this.getDom("preview").style.backgroundColor = color;
  26717. }
  26718. },
  26719. _onTableOut: function() {
  26720. this.getDom("preview").style.backgroundColor = "";
  26721. },
  26722. _onPickNoColor: function() {
  26723. this.fireEvent("picknocolor");
  26724. },
  26725. _onColorSelect: function(evt) {
  26726. var input = evt.target || evt.srcElement;
  26727. var color = input.value;
  26728. if (color) {
  26729. this.fireEvent("pickcolor", color);
  26730. }
  26731. }
  26732. };
  26733. utils.inherits(ColorPicker, UIBase);
  26734. var COLORS = ("ffffff,000000,eeece1,1f497d,4f81bd,c0504d,9bbb59,8064a2,4bacc6,f79646," +
  26735. "f2f2f2,7f7f7f,ddd9c3,c6d9f0,dbe5f1,f2dcdb,ebf1dd,e5e0ec,dbeef3,fdeada," +
  26736. "d8d8d8,595959,c4bd97,8db3e2,b8cce4,e5b9b7,d7e3bc,ccc1d9,b7dde8,fbd5b5," +
  26737. "bfbfbf,3f3f3f,938953,548dd4,95b3d7,d99694,c3d69b,b2a2c7,92cddc,fac08f," +
  26738. "a5a5a5,262626,494429,17365d,366092,953734,76923c,5f497a,31859b,e36c09," +
  26739. "7f7f7f,0c0c0c,1d1b10,0f243e,244061,632423,4f6128,3f3151,205867,974806," +
  26740. "c00000,ff0000,ffc000,ffff00,92d050,00b050,00b0f0,0070c0,002060,7030a0,").split(
  26741. ","
  26742. );
  26743. function genColorPicker(noColorText, editor) {
  26744. var html =
  26745. '<div id="##" class="edui-colorpicker %%">' +
  26746. '<div class="edui-colorpicker-topbar edui-clearfix">' +
  26747. // '<div unselectable="on" id="##_preview" class="edui-colorpicker-preview"></div>' +
  26748. '<div id="##_preview" class="edui-colorpicker-preview"><input type="color" id="##_input" onchange="$$._onColorSelect(event,this);" /></div>' +
  26749. '<div unselectable="on" class="edui-colorpicker-nocolor" onclick="$$._onPickNoColor(event, this);">' +
  26750. noColorText +
  26751. "</div>" +
  26752. "</div>" +
  26753. '<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">' +
  26754. '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;padding-top: 2px"><td colspan="10">' +
  26755. editor.getLang("themeColor") +
  26756. "</td> </tr>" +
  26757. '<tr class="edui-colorpicker-tablefirstrow" >';
  26758. for (var i = 0; i < COLORS.length; i++) {
  26759. if (i && i % 10 === 0) {
  26760. html +=
  26761. "</tr>" +
  26762. (i == 60
  26763. ? '<tr style="border-bottom: 1px solid #ddd;font-size: 13px;line-height: 25px;color:#39C;"><td colspan="10">' +
  26764. editor.getLang("standardColor") +
  26765. "</td></tr>"
  26766. : "") +
  26767. "<tr" +
  26768. (i == 60 ? ' class="edui-colorpicker-tablefirstrow"' : "") +
  26769. ">";
  26770. }
  26771. html += i < 70
  26772. ? '<td style="padding:2px 2px;"><a hidefocus title="' +
  26773. COLORS[i] +
  26774. '" onclick="return false;" href="javascript:" unselectable="on" class="edui-box edui-colorpicker-colorcell"' +
  26775. ' data-color="#' +
  26776. COLORS[i] +
  26777. '"' +
  26778. ' style="background-color:#' +
  26779. COLORS[i] +
  26780. ";border:solid #ccc 1px;" +
  26781. '"' +
  26782. "></a></td>"
  26783. : "";
  26784. }
  26785. html += "</tr>";
  26786. html += "</table></div>";
  26787. return html;
  26788. }
  26789. })();
  26790. // ui/tablepicker.js
  26791. ///import core
  26792. ///import uicore
  26793. (function() {
  26794. var utils = baidu.editor.utils,
  26795. uiUtils = baidu.editor.ui.uiUtils,
  26796. UIBase = baidu.editor.ui.UIBase;
  26797. var TablePicker = (baidu.editor.ui.TablePicker = function(options) {
  26798. this.initOptions(options);
  26799. this.initTablePicker();
  26800. });
  26801. TablePicker.prototype = {
  26802. defaultNumRows: 10,
  26803. defaultNumCols: 10,
  26804. maxNumRows: 20,
  26805. maxNumCols: 20,
  26806. numRows: 10,
  26807. numCols: 10,
  26808. lengthOfCellSide: 22,
  26809. initTablePicker: function() {
  26810. this.initUIBase();
  26811. },
  26812. getHtmlTpl: function() {
  26813. var me = this;
  26814. return (
  26815. '<div id="##" class="edui-tablepicker %%">' +
  26816. '<div class="edui-tablepicker-body">' +
  26817. '<div class="edui-infoarea">' +
  26818. '<span id="##_label" class="edui-label"></span>' +
  26819. "</div>" +
  26820. '<div class="edui-pickarea"' +
  26821. ' onmousemove="$$._onMouseMove(event, this);"' +
  26822. ' onmouseover="$$._onMouseOver(event, this);"' +
  26823. ' onmouseout="$$._onMouseOut(event, this);"' +
  26824. ' onclick="$$._onClick(event, this);"' +
  26825. ">" +
  26826. '<div id="##_overlay" class="edui-overlay"></div>' +
  26827. "</div>" +
  26828. "</div>" +
  26829. "</div>"
  26830. );
  26831. },
  26832. _UIBase_render: UIBase.prototype.render,
  26833. render: function(holder) {
  26834. this._UIBase_render(holder);
  26835. this.getDom("label").innerHTML =
  26836. "0" +
  26837. this.editor.getLang("t_row") +
  26838. " x 0" +
  26839. this.editor.getLang("t_col");
  26840. },
  26841. _track: function(numCols, numRows) {
  26842. var style = this.getDom("overlay").style;
  26843. var sideLen = this.lengthOfCellSide;
  26844. style.width = numCols * sideLen + "px";
  26845. style.height = numRows * sideLen + "px";
  26846. var label = this.getDom("label");
  26847. label.innerHTML =
  26848. numCols +
  26849. this.editor.getLang("t_col") +
  26850. " x " +
  26851. numRows +
  26852. this.editor.getLang("t_row");
  26853. this.numCols = numCols;
  26854. this.numRows = numRows;
  26855. },
  26856. _onMouseOver: function(evt, el) {
  26857. var rel = evt.relatedTarget || evt.fromElement;
  26858. if (!uiUtils.contains(el, rel) && el !== rel) {
  26859. this.getDom("label").innerHTML =
  26860. "0" +
  26861. this.editor.getLang("t_col") +
  26862. " x 0" +
  26863. this.editor.getLang("t_row");
  26864. this.getDom("overlay").style.visibility = "";
  26865. }
  26866. },
  26867. _onMouseOut: function(evt, el) {
  26868. var rel = evt.relatedTarget || evt.toElement;
  26869. if (!uiUtils.contains(el, rel) && el !== rel) {
  26870. this.getDom("label").innerHTML =
  26871. "0" +
  26872. this.editor.getLang("t_col") +
  26873. " x 0" +
  26874. this.editor.getLang("t_row");
  26875. this.getDom("overlay").style.visibility = "hidden";
  26876. }
  26877. },
  26878. _onMouseMove: function(evt, el) {
  26879. var style = this.getDom("overlay").style;
  26880. var offset = uiUtils.getEventOffset(evt);
  26881. var sideLen = this.lengthOfCellSide;
  26882. var numCols = Math.ceil(offset.left / sideLen);
  26883. var numRows = Math.ceil(offset.top / sideLen);
  26884. this._track(numCols, numRows);
  26885. },
  26886. _onClick: function() {
  26887. this.fireEvent("picktable", this.numCols, this.numRows);
  26888. }
  26889. };
  26890. utils.inherits(TablePicker, UIBase);
  26891. })();
  26892. // ui/stateful.js
  26893. (function() {
  26894. var browser = baidu.editor.browser,
  26895. domUtils = baidu.editor.dom.domUtils,
  26896. uiUtils = baidu.editor.ui.uiUtils;
  26897. var TPL_STATEFUL =
  26898. 'onmousedown="$$.Stateful_onMouseDown(event, this);"' +
  26899. ' onmouseup="$$.Stateful_onMouseUp(event, this);"' +
  26900. (browser.ie
  26901. ? ' onmouseenter="$$.Stateful_onMouseEnter(event, this);"' +
  26902. ' onmouseleave="$$.Stateful_onMouseLeave(event, this);"'
  26903. : ' onmouseover="$$.Stateful_onMouseOver(event, this);"' +
  26904. ' onmouseout="$$.Stateful_onMouseOut(event, this);"');
  26905. baidu.editor.ui.Stateful = {
  26906. alwalysHoverable: false,
  26907. target: null, //目标元素和this指向dom不一样
  26908. Stateful_init: function() {
  26909. this._Stateful_dGetHtmlTpl = this.getHtmlTpl;
  26910. this.getHtmlTpl = this.Stateful_getHtmlTpl;
  26911. },
  26912. Stateful_getHtmlTpl: function() {
  26913. var tpl = this._Stateful_dGetHtmlTpl();
  26914. // 使用function避免$转义
  26915. return tpl.replace(/stateful/g, function() {
  26916. return TPL_STATEFUL;
  26917. });
  26918. },
  26919. Stateful_onMouseEnter: function(evt, el) {
  26920. this.target = el;
  26921. if (!this.isDisabled() || this.alwalysHoverable) {
  26922. this.addState("hover");
  26923. this.fireEvent("over");
  26924. }
  26925. },
  26926. Stateful_onMouseLeave: function(evt, el) {
  26927. if (!this.isDisabled() || this.alwalysHoverable) {
  26928. this.removeState("hover");
  26929. this.removeState("active");
  26930. this.fireEvent("out");
  26931. }
  26932. },
  26933. Stateful_onMouseOver: function(evt, el) {
  26934. var rel = evt.relatedTarget;
  26935. if (!uiUtils.contains(el, rel) && el !== rel) {
  26936. this.Stateful_onMouseEnter(evt, el);
  26937. }
  26938. },
  26939. Stateful_onMouseOut: function(evt, el) {
  26940. var rel = evt.relatedTarget;
  26941. if (!uiUtils.contains(el, rel) && el !== rel) {
  26942. this.Stateful_onMouseLeave(evt, el);
  26943. }
  26944. },
  26945. Stateful_onMouseDown: function(evt, el) {
  26946. if (!this.isDisabled()) {
  26947. this.addState("active");
  26948. }
  26949. },
  26950. Stateful_onMouseUp: function(evt, el) {
  26951. if (!this.isDisabled()) {
  26952. this.removeState("active");
  26953. }
  26954. },
  26955. Stateful_postRender: function() {
  26956. if (this.disabled && !this.hasState("disabled")) {
  26957. this.addState("disabled");
  26958. }
  26959. },
  26960. hasState: function(state) {
  26961. return domUtils.hasClass(this.getStateDom(), "edui-state-" + state);
  26962. },
  26963. addState: function(state) {
  26964. if (!this.hasState(state)) {
  26965. this.getStateDom().className += " edui-state-" + state;
  26966. }
  26967. },
  26968. removeState: function(state) {
  26969. if (this.hasState(state)) {
  26970. domUtils.removeClasses(this.getStateDom(), ["edui-state-" + state]);
  26971. }
  26972. },
  26973. getStateDom: function() {
  26974. return this.getDom("state");
  26975. },
  26976. isChecked: function() {
  26977. return this.hasState("checked");
  26978. },
  26979. setChecked: function(checked) {
  26980. if (!this.isDisabled() && checked) {
  26981. this.addState("checked");
  26982. } else {
  26983. this.removeState("checked");
  26984. }
  26985. },
  26986. isDisabled: function() {
  26987. return this.hasState("disabled");
  26988. },
  26989. setDisabled: function(disabled) {
  26990. if (disabled) {
  26991. this.removeState("hover");
  26992. this.removeState("checked");
  26993. this.removeState("active");
  26994. this.addState("disabled");
  26995. } else {
  26996. this.removeState("disabled");
  26997. }
  26998. }
  26999. };
  27000. })();
  27001. // ui/button.js
  27002. ///import core
  27003. ///import uicore
  27004. ///import ui/stateful.js
  27005. (function() {
  27006. var utils = baidu.editor.utils,
  27007. UIBase = baidu.editor.ui.UIBase,
  27008. Stateful = baidu.editor.ui.Stateful,
  27009. Button = (baidu.editor.ui.Button = function(options) {
  27010. if (options.name) {
  27011. var btnName = options.name;
  27012. var cssRules = options.cssRules;
  27013. if (!options.className) {
  27014. options.className = "edui-for-" + btnName;
  27015. }
  27016. options.cssRules =
  27017. ".edui-" +
  27018. (options.theme || "default") +
  27019. " .edui-toolbar .edui-button.edui-for-" +
  27020. btnName +
  27021. " .edui-icon {" +
  27022. cssRules +
  27023. "}";
  27024. }
  27025. this.initOptions(options);
  27026. this.initButton();
  27027. });
  27028. Button.prototype = {
  27029. uiName: "button",
  27030. label: "",
  27031. title: "",
  27032. showIcon: true,
  27033. showText: true,
  27034. cssRules: "",
  27035. initButton: function() {
  27036. this.initUIBase();
  27037. this.Stateful_init();
  27038. if (this.cssRules) {
  27039. utils.cssRule("edui-customize-" + this.name + "-style", this.cssRules);
  27040. }
  27041. },
  27042. getHtmlTpl: function() {
  27043. return (
  27044. '<div id="##" class="edui-box %%">' +
  27045. '<div id="##_state" stateful>' +
  27046. '<div class="%%-wrap"><div id="##_body" unselectable="on" ' +
  27047. (this.title ? 'title="' + this.title + '"' : "") +
  27048. ' class="%%-body" onmousedown="return $$._onMouseDown(event, this);" onclick="return $$._onClick(event, this);">' +
  27049. (this.showIcon ? '<div class="edui-box edui-icon"></div>' : "") +
  27050. (this.showText
  27051. ? '<div class="edui-box edui-label">' + this.label + "</div>"
  27052. : "") +
  27053. "</div>" +
  27054. "</div>" +
  27055. "</div></div>"
  27056. );
  27057. },
  27058. postRender: function() {
  27059. this.Stateful_postRender();
  27060. this.setDisabled(this.disabled);
  27061. },
  27062. _onMouseDown: function(e) {
  27063. var target = e.target || e.srcElement,
  27064. tagName = target && target.tagName && target.tagName.toLowerCase();
  27065. if (tagName == "input" || tagName == "object" || tagName == "object") {
  27066. return false;
  27067. }
  27068. },
  27069. _onClick: function() {
  27070. if (!this.isDisabled()) {
  27071. this.fireEvent("click");
  27072. }
  27073. },
  27074. setTitle: function(text) {
  27075. var label = this.getDom("label");
  27076. label.innerHTML = text;
  27077. }
  27078. };
  27079. utils.inherits(Button, UIBase);
  27080. utils.extend(Button.prototype, Stateful);
  27081. })();
  27082. // ui/splitbutton.js
  27083. ///import core
  27084. ///import uicore
  27085. ///import ui/stateful.js
  27086. (function() {
  27087. var utils = baidu.editor.utils,
  27088. uiUtils = baidu.editor.ui.uiUtils,
  27089. domUtils = baidu.editor.dom.domUtils,
  27090. UIBase = baidu.editor.ui.UIBase,
  27091. Stateful = baidu.editor.ui.Stateful,
  27092. SplitButton = (baidu.editor.ui.SplitButton = function(options) {
  27093. this.initOptions(options);
  27094. this.initSplitButton();
  27095. });
  27096. SplitButton.prototype = {
  27097. popup: null,
  27098. uiName: "splitbutton",
  27099. title: "",
  27100. initSplitButton: function() {
  27101. this.initUIBase();
  27102. this.Stateful_init();
  27103. var me = this;
  27104. if (this.popup != null) {
  27105. var popup = this.popup;
  27106. this.popup = null;
  27107. this.setPopup(popup);
  27108. }
  27109. },
  27110. _UIBase_postRender: UIBase.prototype.postRender,
  27111. postRender: function() {
  27112. this.Stateful_postRender();
  27113. this._UIBase_postRender();
  27114. },
  27115. setPopup: function(popup) {
  27116. if (this.popup === popup) return;
  27117. if (this.popup != null) {
  27118. this.popup.dispose();
  27119. }
  27120. popup.addListener("show", utils.bind(this._onPopupShow, this));
  27121. popup.addListener("hide", utils.bind(this._onPopupHide, this));
  27122. popup.addListener(
  27123. "postrender",
  27124. utils.bind(function() {
  27125. popup
  27126. .getDom("body")
  27127. .appendChild(
  27128. uiUtils.createElementByHtml(
  27129. '<div id="' +
  27130. this.popup.id +
  27131. '_bordereraser" class="edui-bordereraser edui-background" style="width:' +
  27132. (uiUtils.getClientRect(this.getDom()).width + 20) +
  27133. 'px"></div>'
  27134. )
  27135. );
  27136. popup.getDom().className += " " + this.className;
  27137. }, this)
  27138. );
  27139. this.popup = popup;
  27140. },
  27141. _onPopupShow: function() {
  27142. this.addState("opened");
  27143. },
  27144. _onPopupHide: function() {
  27145. this.removeState("opened");
  27146. },
  27147. getHtmlTpl: function() {
  27148. return (
  27149. '<div id="##" class="edui-box %%">' +
  27150. "<div " +
  27151. (this.title ? 'title="' + this.title + '"' : "") +
  27152. ' id="##_state" stateful><div class="%%-body">' +
  27153. '<div id="##_button_body" class="edui-box edui-button-body" onclick="$$._onButtonClick(event, this);">' +
  27154. '<div class="edui-box edui-icon"></div>' +
  27155. "</div>" +
  27156. '<div class="edui-box edui-splitborder"></div>' +
  27157. '<div class="edui-box edui-arrow" onclick="$$._onArrowClick();"></div>' +
  27158. "</div></div></div>"
  27159. );
  27160. },
  27161. showPopup: function() {
  27162. // 当popup往上弹出的时候,做特殊处理
  27163. var rect = uiUtils.getClientRect(this.getDom());
  27164. rect.top -= this.popup.SHADOW_RADIUS;
  27165. rect.height += this.popup.SHADOW_RADIUS;
  27166. this.popup.showAnchorRect(rect);
  27167. },
  27168. _onArrowClick: function(event, el) {
  27169. if (!this.isDisabled()) {
  27170. this.showPopup();
  27171. }
  27172. },
  27173. _onButtonClick: function() {
  27174. if (!this.isDisabled()) {
  27175. this.fireEvent("buttonclick");
  27176. }
  27177. }
  27178. };
  27179. utils.inherits(SplitButton, UIBase);
  27180. utils.extend(SplitButton.prototype, Stateful, true);
  27181. })();
  27182. // ui/colorbutton.js
  27183. ///import core
  27184. ///import uicore
  27185. ///import ui/colorpicker.js
  27186. ///import ui/popup.js
  27187. ///import ui/splitbutton.js
  27188. (function() {
  27189. var utils = baidu.editor.utils,
  27190. uiUtils = baidu.editor.ui.uiUtils,
  27191. ColorPicker = baidu.editor.ui.ColorPicker,
  27192. Popup = baidu.editor.ui.Popup,
  27193. SplitButton = baidu.editor.ui.SplitButton,
  27194. ColorButton = (baidu.editor.ui.ColorButton = function(options) {
  27195. this.initOptions(options);
  27196. this.initColorButton();
  27197. });
  27198. ColorButton.prototype = {
  27199. initColorButton: function() {
  27200. var me = this;
  27201. this.popup = new Popup({
  27202. content: new ColorPicker({
  27203. noColorText: me.editor.getLang("clearColor"),
  27204. editor: me.editor,
  27205. onpickcolor: function(t, color) {
  27206. me._onPickColor(color);
  27207. },
  27208. onpicknocolor: function(t, color) {
  27209. me._onPickNoColor(color);
  27210. }
  27211. }),
  27212. editor: me.editor
  27213. });
  27214. this.initSplitButton();
  27215. },
  27216. _SplitButton_postRender: SplitButton.prototype.postRender,
  27217. postRender: function() {
  27218. this._SplitButton_postRender();
  27219. this.getDom("button_body").appendChild(
  27220. uiUtils.createElementByHtml(
  27221. '<div id="' + this.id + '_colorlump" class="edui-colorlump"></div>'
  27222. )
  27223. );
  27224. this.getDom().className += " edui-colorbutton";
  27225. },
  27226. setColor: function(color) {
  27227. this.getDom("colorlump").style.backgroundColor = color;
  27228. this.color = color;
  27229. },
  27230. _onPickColor: function(color) {
  27231. if (this.fireEvent("pickcolor", color) !== false) {
  27232. this.setColor(color);
  27233. this.popup.hide();
  27234. }
  27235. },
  27236. _onPickNoColor: function(color) {
  27237. if (this.fireEvent("picknocolor") !== false) {
  27238. this.popup.hide();
  27239. }
  27240. }
  27241. };
  27242. utils.inherits(ColorButton, SplitButton);
  27243. })();
  27244. // ui/tablebutton.js
  27245. ///import core
  27246. ///import uicore
  27247. ///import ui/popup.js
  27248. ///import ui/tablepicker.js
  27249. ///import ui/splitbutton.js
  27250. (function() {
  27251. var utils = baidu.editor.utils,
  27252. Popup = baidu.editor.ui.Popup,
  27253. TablePicker = baidu.editor.ui.TablePicker,
  27254. SplitButton = baidu.editor.ui.SplitButton,
  27255. TableButton = (baidu.editor.ui.TableButton = function(options) {
  27256. this.initOptions(options);
  27257. this.initTableButton();
  27258. });
  27259. TableButton.prototype = {
  27260. initTableButton: function() {
  27261. var me = this;
  27262. this.popup = new Popup({
  27263. content: new TablePicker({
  27264. editor: me.editor,
  27265. onpicktable: function(t, numCols, numRows) {
  27266. me._onPickTable(numCols, numRows);
  27267. }
  27268. }),
  27269. editor: me.editor
  27270. });
  27271. this.initSplitButton();
  27272. },
  27273. _onPickTable: function(numCols, numRows) {
  27274. if (this.fireEvent("picktable", numCols, numRows) !== false) {
  27275. this.popup.hide();
  27276. }
  27277. }
  27278. };
  27279. utils.inherits(TableButton, SplitButton);
  27280. })();
  27281. // ui/autotypesetpicker.js
  27282. ///import core
  27283. ///import uicore
  27284. (function() {
  27285. var utils = baidu.editor.utils,
  27286. UIBase = baidu.editor.ui.UIBase;
  27287. var AutoTypeSetPicker = (baidu.editor.ui.AutoTypeSetPicker = function(
  27288. options
  27289. ) {
  27290. this.initOptions(options);
  27291. this.initAutoTypeSetPicker();
  27292. });
  27293. AutoTypeSetPicker.prototype = {
  27294. initAutoTypeSetPicker: function() {
  27295. this.initUIBase();
  27296. },
  27297. getHtmlTpl: function() {
  27298. var me = this.editor,
  27299. opt = me.options.autotypeset,
  27300. lang = me.getLang("autoTypeSet");
  27301. var textAlignInputName = "textAlignValue" + me.uid,
  27302. imageBlockInputName = "imageBlockLineValue" + me.uid,
  27303. symbolConverInputName = "symbolConverValue" + me.uid;
  27304. return (
  27305. '<div id="##" class="edui-autotypesetpicker %%">' +
  27306. '<div class="edui-autotypesetpicker-body">' +
  27307. "<table >" +
  27308. '<tr><td nowrap><input type="checkbox" name="mergeEmptyline" ' +
  27309. (opt["mergeEmptyline"] ? "checked" : "") +
  27310. ">" +
  27311. lang.mergeLine +
  27312. '</td><td colspan="2"><input type="checkbox" name="removeEmptyline" ' +
  27313. (opt["removeEmptyline"] ? "checked" : "") +
  27314. ">" +
  27315. lang.delLine +
  27316. "</td></tr>" +
  27317. '<tr><td nowrap><input type="checkbox" name="removeClass" ' +
  27318. (opt["removeClass"] ? "checked" : "") +
  27319. ">" +
  27320. lang.removeFormat +
  27321. '</td><td colspan="2"><input type="checkbox" name="indent" ' +
  27322. (opt["indent"] ? "checked" : "") +
  27323. ">" +
  27324. lang.indent +
  27325. "</td></tr>" +
  27326. "<tr>" +
  27327. '<td nowrap><input type="checkbox" name="textAlign" ' +
  27328. (opt["textAlign"] ? "checked" : "") +
  27329. ">" +
  27330. lang.alignment +
  27331. "</td>" +
  27332. '<td colspan="2" id="' +
  27333. textAlignInputName +
  27334. '">' +
  27335. '<input type="radio" name="' +
  27336. textAlignInputName +
  27337. '" value="left" ' +
  27338. (opt["textAlign"] && opt["textAlign"] == "left" ? "checked" : "") +
  27339. ">" +
  27340. me.getLang("justifyleft") +
  27341. '<input type="radio" name="' +
  27342. textAlignInputName +
  27343. '" value="center" ' +
  27344. (opt["textAlign"] && opt["textAlign"] == "center" ? "checked" : "") +
  27345. ">" +
  27346. me.getLang("justifycenter") +
  27347. '<input type="radio" name="' +
  27348. textAlignInputName +
  27349. '" value="right" ' +
  27350. (opt["textAlign"] && opt["textAlign"] == "right" ? "checked" : "") +
  27351. ">" +
  27352. me.getLang("justifyright") +
  27353. "</td>" +
  27354. "</tr>" +
  27355. "<tr>" +
  27356. '<td nowrap><input type="checkbox" name="imageBlockLine" ' +
  27357. (opt["imageBlockLine"] ? "checked" : "") +
  27358. ">" +
  27359. lang.imageFloat +
  27360. "</td>" +
  27361. '<td nowrap id="' +
  27362. imageBlockInputName +
  27363. '">' +
  27364. '<input type="radio" name="' +
  27365. imageBlockInputName +
  27366. '" value="none" ' +
  27367. (opt["imageBlockLine"] && opt["imageBlockLine"] == "none"
  27368. ? "checked"
  27369. : "") +
  27370. ">" +
  27371. me.getLang("default") +
  27372. '<input type="radio" name="' +
  27373. imageBlockInputName +
  27374. '" value="left" ' +
  27375. (opt["imageBlockLine"] && opt["imageBlockLine"] == "left"
  27376. ? "checked"
  27377. : "") +
  27378. ">" +
  27379. me.getLang("justifyleft") +
  27380. '<input type="radio" name="' +
  27381. imageBlockInputName +
  27382. '" value="center" ' +
  27383. (opt["imageBlockLine"] && opt["imageBlockLine"] == "center"
  27384. ? "checked"
  27385. : "") +
  27386. ">" +
  27387. me.getLang("justifycenter") +
  27388. '<input type="radio" name="' +
  27389. imageBlockInputName +
  27390. '" value="right" ' +
  27391. (opt["imageBlockLine"] && opt["imageBlockLine"] == "right"
  27392. ? "checked"
  27393. : "") +
  27394. ">" +
  27395. me.getLang("justifyright") +
  27396. "</td>" +
  27397. "</tr>" +
  27398. '<tr><td nowrap><input type="checkbox" name="clearFontSize" ' +
  27399. (opt["clearFontSize"] ? "checked" : "") +
  27400. ">" +
  27401. lang.removeFontsize +
  27402. '</td><td colspan="2"><input type="checkbox" name="clearFontFamily" ' +
  27403. (opt["clearFontFamily"] ? "checked" : "") +
  27404. ">" +
  27405. lang.removeFontFamily +
  27406. "</td></tr>" +
  27407. '<tr><td nowrap colspan="3"><input type="checkbox" name="removeEmptyNode" ' +
  27408. (opt["removeEmptyNode"] ? "checked" : "") +
  27409. ">" +
  27410. lang.removeHtml +
  27411. "</td></tr>" +
  27412. '<tr><td nowrap colspan="3"><input type="checkbox" name="pasteFilter" ' +
  27413. (opt["pasteFilter"] ? "checked" : "") +
  27414. ">" +
  27415. lang.pasteFilter +
  27416. "</td></tr>" +
  27417. "<tr>" +
  27418. '<td nowrap><input type="checkbox" name="symbolConver" ' +
  27419. (opt["bdc2sb"] || opt["tobdc"] ? "checked" : "") +
  27420. ">" +
  27421. lang.symbol +
  27422. "</td>" +
  27423. '<td id="' +
  27424. symbolConverInputName +
  27425. '">' +
  27426. '<input type="radio" name="bdc" value="bdc2sb" ' +
  27427. (opt["bdc2sb"] ? "checked" : "") +
  27428. ">" +
  27429. lang.bdc2sb +
  27430. '<input type="radio" name="bdc" value="tobdc" ' +
  27431. (opt["tobdc"] ? "checked" : "") +
  27432. ">" +
  27433. lang.tobdc +
  27434. "" +
  27435. "</td>" +
  27436. '<td nowrap align="right"><button >' +
  27437. lang.run +
  27438. "</button></td>" +
  27439. "</tr>" +
  27440. "</table>" +
  27441. "</div>" +
  27442. "</div>"
  27443. );
  27444. },
  27445. _UIBase_render: UIBase.prototype.render
  27446. };
  27447. utils.inherits(AutoTypeSetPicker, UIBase);
  27448. })();
  27449. // ui/autotypesetbutton.js
  27450. ///import core
  27451. ///import uicore
  27452. ///import ui/popup.js
  27453. ///import ui/autotypesetpicker.js
  27454. ///import ui/splitbutton.js
  27455. (function() {
  27456. var utils = baidu.editor.utils,
  27457. Popup = baidu.editor.ui.Popup,
  27458. AutoTypeSetPicker = baidu.editor.ui.AutoTypeSetPicker,
  27459. SplitButton = baidu.editor.ui.SplitButton,
  27460. AutoTypeSetButton = (baidu.editor.ui.AutoTypeSetButton = function(options) {
  27461. this.initOptions(options);
  27462. this.initAutoTypeSetButton();
  27463. });
  27464. function getPara(me) {
  27465. var opt = {},
  27466. cont = me.getDom(),
  27467. editorId = me.editor.uid,
  27468. inputType = null,
  27469. attrName = null,
  27470. ipts = domUtils.getElementsByTagName(cont, "input");
  27471. for (var i = ipts.length - 1, ipt; (ipt = ipts[i--]); ) {
  27472. inputType = ipt.getAttribute("type");
  27473. if (inputType == "checkbox") {
  27474. attrName = ipt.getAttribute("name");
  27475. opt[attrName] && delete opt[attrName];
  27476. if (ipt.checked) {
  27477. var attrValue = document.getElementById(
  27478. attrName + "Value" + editorId
  27479. );
  27480. if (attrValue) {
  27481. if (/input/gi.test(attrValue.tagName)) {
  27482. opt[attrName] = attrValue.value;
  27483. } else {
  27484. var iptChilds = attrValue.getElementsByTagName("input");
  27485. for (
  27486. var j = iptChilds.length - 1, iptchild;
  27487. (iptchild = iptChilds[j--]);
  27488. ) {
  27489. if (iptchild.checked) {
  27490. opt[attrName] = iptchild.value;
  27491. break;
  27492. }
  27493. }
  27494. }
  27495. } else {
  27496. opt[attrName] = true;
  27497. }
  27498. } else {
  27499. opt[attrName] = false;
  27500. }
  27501. } else {
  27502. opt[ipt.getAttribute("value")] = ipt.checked;
  27503. }
  27504. }
  27505. var selects = domUtils.getElementsByTagName(cont, "select");
  27506. for (var i = 0, si; (si = selects[i++]); ) {
  27507. var attr = si.getAttribute("name");
  27508. opt[attr] = opt[attr] ? si.value : "";
  27509. }
  27510. utils.extend(me.editor.options.autotypeset, opt);
  27511. me.editor.setPreferences("autotypeset", opt);
  27512. }
  27513. AutoTypeSetButton.prototype = {
  27514. initAutoTypeSetButton: function() {
  27515. var me = this;
  27516. this.popup = new Popup({
  27517. //传入配置参数
  27518. content: new AutoTypeSetPicker({ editor: me.editor }),
  27519. editor: me.editor,
  27520. hide: function() {
  27521. if (!this._hidden && this.getDom()) {
  27522. getPara(this);
  27523. this.getDom().style.display = "none";
  27524. this._hidden = true;
  27525. this.fireEvent("hide");
  27526. }
  27527. }
  27528. });
  27529. var flag = 0;
  27530. this.popup.addListener("postRenderAfter", function() {
  27531. var popupUI = this;
  27532. if (flag) return;
  27533. var cont = this.getDom(),
  27534. btn = cont.getElementsByTagName("button")[0];
  27535. btn.onclick = function() {
  27536. getPara(popupUI);
  27537. me.editor.execCommand("autotypeset");
  27538. popupUI.hide();
  27539. };
  27540. domUtils.on(cont, "click", function(e) {
  27541. var target = e.target || e.srcElement,
  27542. editorId = me.editor.uid;
  27543. if (target && target.tagName == "INPUT") {
  27544. // 点击图片浮动的checkbox,去除对应的radio
  27545. if (
  27546. target.name == "imageBlockLine" ||
  27547. target.name == "textAlign" ||
  27548. target.name == "symbolConver"
  27549. ) {
  27550. var checked = target.checked,
  27551. radioTd = document.getElementById(
  27552. target.name + "Value" + editorId
  27553. ),
  27554. radios = radioTd.getElementsByTagName("input"),
  27555. defalutSelect = {
  27556. imageBlockLine: "none",
  27557. textAlign: "left",
  27558. symbolConver: "tobdc"
  27559. };
  27560. for (var i = 0; i < radios.length; i++) {
  27561. if (checked) {
  27562. if (radios[i].value == defalutSelect[target.name]) {
  27563. radios[i].checked = "checked";
  27564. }
  27565. } else {
  27566. radios[i].checked = false;
  27567. }
  27568. }
  27569. }
  27570. // 点击radio,选中对应的checkbox
  27571. if (
  27572. target.name == "imageBlockLineValue" + editorId ||
  27573. target.name == "textAlignValue" + editorId ||
  27574. target.name == "bdc"
  27575. ) {
  27576. var checkboxs = target.parentNode.previousSibling.getElementsByTagName(
  27577. "input"
  27578. );
  27579. checkboxs && (checkboxs[0].checked = true);
  27580. }
  27581. getPara(popupUI);
  27582. }
  27583. });
  27584. flag = 1;
  27585. });
  27586. this.initSplitButton();
  27587. }
  27588. };
  27589. utils.inherits(AutoTypeSetButton, SplitButton);
  27590. })();
  27591. // ui/cellalignpicker.js
  27592. ///import core
  27593. ///import uicore
  27594. (function() {
  27595. var utils = baidu.editor.utils,
  27596. Popup = baidu.editor.ui.Popup,
  27597. Stateful = baidu.editor.ui.Stateful,
  27598. UIBase = baidu.editor.ui.UIBase;
  27599. /**
  27600. * 该参数将新增一个参数: selected, 参数类型为一个Object, 形如{ 'align': 'center', 'valign': 'top' }, 表示单元格的初始
  27601. * 对齐状态为: 竖直居上,水平居中; 其中 align的取值为:'center', 'left', 'right'; valign的取值为: 'top', 'middle', 'bottom'
  27602. * @update 2013/4/2 hancong03@baidu.com
  27603. */
  27604. var CellAlignPicker = (baidu.editor.ui.CellAlignPicker = function(options) {
  27605. this.initOptions(options);
  27606. this.initSelected();
  27607. this.initCellAlignPicker();
  27608. });
  27609. CellAlignPicker.prototype = {
  27610. //初始化选中状态, 该方法将根据传递进来的参数获取到应该选中的对齐方式图标的索引
  27611. initSelected: function() {
  27612. var status = {
  27613. valign: {
  27614. top: 0,
  27615. middle: 1,
  27616. bottom: 2
  27617. },
  27618. align: {
  27619. left: 0,
  27620. center: 1,
  27621. right: 2
  27622. },
  27623. count: 3
  27624. },
  27625. result = -1;
  27626. if (this.selected) {
  27627. this.selectedIndex =
  27628. status.valign[this.selected.valign] * status.count +
  27629. status.align[this.selected.align];
  27630. }
  27631. },
  27632. initCellAlignPicker: function() {
  27633. this.initUIBase();
  27634. this.Stateful_init();
  27635. },
  27636. getHtmlTpl: function() {
  27637. var alignType = ["left", "center", "right"],
  27638. COUNT = 9,
  27639. tempClassName = null,
  27640. tempIndex = -1,
  27641. tmpl = [];
  27642. for (var i = 0; i < COUNT; i++) {
  27643. tempClassName = this.selectedIndex === i
  27644. ? ' class="edui-cellalign-selected" '
  27645. : "";
  27646. tempIndex = i % 3;
  27647. tempIndex === 0 && tmpl.push("<tr>");
  27648. tmpl.push(
  27649. '<td index="' +
  27650. i +
  27651. '" ' +
  27652. tempClassName +
  27653. ' stateful><div class="edui-icon edui-' +
  27654. alignType[tempIndex] +
  27655. '"></div></td>'
  27656. );
  27657. tempIndex === 2 && tmpl.push("</tr>");
  27658. }
  27659. return (
  27660. '<div id="##" class="edui-cellalignpicker %%">' +
  27661. '<div class="edui-cellalignpicker-body">' +
  27662. '<table onclick="$$._onClick(event);">' +
  27663. tmpl.join("") +
  27664. "</table>" +
  27665. "</div>" +
  27666. "</div>"
  27667. );
  27668. },
  27669. getStateDom: function() {
  27670. return this.target;
  27671. },
  27672. _onClick: function(evt) {
  27673. var target = evt.target || evt.srcElement;
  27674. if (/icon/.test(target.className)) {
  27675. this.items[target.parentNode.getAttribute("index")].onclick();
  27676. Popup.postHide(evt);
  27677. }
  27678. },
  27679. _UIBase_render: UIBase.prototype.render
  27680. };
  27681. utils.inherits(CellAlignPicker, UIBase);
  27682. utils.extend(CellAlignPicker.prototype, Stateful, true);
  27683. })();
  27684. // ui/pastepicker.js
  27685. ///import core
  27686. ///import uicore
  27687. (function() {
  27688. var utils = baidu.editor.utils,
  27689. Stateful = baidu.editor.ui.Stateful,
  27690. uiUtils = baidu.editor.ui.uiUtils,
  27691. UIBase = baidu.editor.ui.UIBase;
  27692. var PastePicker = (baidu.editor.ui.PastePicker = function(options) {
  27693. this.initOptions(options);
  27694. this.initPastePicker();
  27695. });
  27696. PastePicker.prototype = {
  27697. initPastePicker: function() {
  27698. this.initUIBase();
  27699. this.Stateful_init();
  27700. },
  27701. getHtmlTpl: function() {
  27702. return (
  27703. '<div class="edui-pasteicon" onclick="$$._onClick(this)"></div>' +
  27704. '<div class="edui-pastecontainer">' +
  27705. '<div class="edui-title">' +
  27706. this.editor.getLang("pasteOpt") +
  27707. "</div>" +
  27708. '<div class="edui-button">' +
  27709. '<div title="' +
  27710. this.editor.getLang("pasteSourceFormat") +
  27711. '" onclick="$$.format(false)" stateful>' +
  27712. '<div class="edui-richtxticon"></div></div>' +
  27713. '<div title="' +
  27714. this.editor.getLang("tagFormat") +
  27715. '" onclick="$$.format(2)" stateful>' +
  27716. '<div class="edui-tagicon"></div></div>' +
  27717. '<div title="' +
  27718. this.editor.getLang("pasteTextFormat") +
  27719. '" onclick="$$.format(true)" stateful>' +
  27720. '<div class="edui-plaintxticon"></div></div>' +
  27721. "</div>" +
  27722. "</div>" +
  27723. "</div>"
  27724. );
  27725. },
  27726. getStateDom: function() {
  27727. return this.target;
  27728. },
  27729. format: function(param) {
  27730. this.editor.ui._isTransfer = true;
  27731. this.editor.fireEvent("pasteTransfer", param);
  27732. },
  27733. _onClick: function(cur) {
  27734. var node = domUtils.getNextDomNode(cur),
  27735. screenHt = uiUtils.getViewportRect().height,
  27736. subPop = uiUtils.getClientRect(node);
  27737. if (subPop.top + subPop.height > screenHt)
  27738. node.style.top = -subPop.height - cur.offsetHeight + "px";
  27739. else node.style.top = "";
  27740. if (/hidden/gi.test(domUtils.getComputedStyle(node, "visibility"))) {
  27741. node.style.visibility = "visible";
  27742. domUtils.addClass(cur, "edui-state-opened");
  27743. } else {
  27744. node.style.visibility = "hidden";
  27745. domUtils.removeClasses(cur, "edui-state-opened");
  27746. }
  27747. },
  27748. _UIBase_render: UIBase.prototype.render
  27749. };
  27750. utils.inherits(PastePicker, UIBase);
  27751. utils.extend(PastePicker.prototype, Stateful, true);
  27752. })();
  27753. // ui/toolbar.js
  27754. (function() {
  27755. var utils = baidu.editor.utils,
  27756. uiUtils = baidu.editor.ui.uiUtils,
  27757. UIBase = baidu.editor.ui.UIBase,
  27758. Toolbar = (baidu.editor.ui.Toolbar = function(options) {
  27759. this.initOptions(options);
  27760. this.initToolbar();
  27761. });
  27762. Toolbar.prototype = {
  27763. items: null,
  27764. initToolbar: function() {
  27765. this.items = this.items || [];
  27766. this.initUIBase();
  27767. },
  27768. add: function(item, index) {
  27769. if (index === undefined) {
  27770. this.items.push(item);
  27771. } else {
  27772. this.items.splice(index, 0, item);
  27773. }
  27774. },
  27775. getHtmlTpl: function() {
  27776. var buff = [];
  27777. for (var i = 0; i < this.items.length; i++) {
  27778. buff[i] = this.items[i].renderHtml();
  27779. }
  27780. return (
  27781. '<div id="##" class="edui-toolbar %%" onselectstart="return false;" onmousedown="return $$._onMouseDown(event, this);">' +
  27782. buff.join("") +
  27783. "</div>"
  27784. );
  27785. },
  27786. postRender: function() {
  27787. var box = this.getDom();
  27788. for (var i = 0; i < this.items.length; i++) {
  27789. this.items[i].postRender();
  27790. }
  27791. uiUtils.makeUnselectable(box);
  27792. },
  27793. _onMouseDown: function(e) {
  27794. var target = e.target || e.srcElement,
  27795. tagName = target && target.tagName && target.tagName.toLowerCase();
  27796. if (tagName == "input" || tagName == "object" || tagName == "object") {
  27797. return false;
  27798. }
  27799. }
  27800. };
  27801. utils.inherits(Toolbar, UIBase);
  27802. })();
  27803. // ui/quick-operate.js
  27804. ///import core
  27805. ///import uicore
  27806. ///import ui\popup.js
  27807. ///import ui\stateful.js
  27808. (function () {
  27809. var utils = baidu.editor.utils,
  27810. domUtils = baidu.editor.dom.domUtils,
  27811. uiUtils = baidu.editor.ui.uiUtils,
  27812. UIBase = baidu.editor.ui.UIBase,
  27813. Popup = baidu.editor.ui.Popup,
  27814. Stateful = baidu.editor.ui.Stateful,
  27815. CellAlignPicker = baidu.editor.ui.CellAlignPicker,
  27816. QuickOperate = (baidu.editor.ui.QuickOperate = function (options) {
  27817. this.initOptions(options);
  27818. // this.initMenu();
  27819. });
  27820. // var menuSeparator = {
  27821. // renderHtml: function() {
  27822. // return '<div class="edui-menuitem edui-menuseparator"><div class="edui-menuseparator-inner"></div></div>';
  27823. // },
  27824. // postRender: function() {},
  27825. // queryAutoHide: function() {
  27826. // return true;
  27827. // }
  27828. // };
  27829. QuickOperate.prototype = {
  27830. // items: null,
  27831. uiName: "quick-operate",
  27832. // initMenu: function() {
  27833. // this.items = this.items || [];
  27834. // this.initPopup();
  27835. // this.initItems();
  27836. // },
  27837. // initItems: function() {
  27838. // for (var i = 0; i < this.items.length; i++) {
  27839. // var item = this.items[i];
  27840. // if (item == "-") {
  27841. // this.items[i] = this.getSeparator();
  27842. // } else if (!(item instanceof MenuItem)) {
  27843. // item.editor = this.editor;
  27844. // item.theme = this.editor.options.theme;
  27845. // this.items[i] = this.createItem(item);
  27846. // }
  27847. // }
  27848. // },
  27849. // getSeparator: function() {
  27850. // return menuSeparator;
  27851. // },
  27852. // createItem: function(item) {
  27853. // //新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  27854. // item.menu = this;
  27855. // return new MenuItem(item);
  27856. // },
  27857. _Popup_getContentHtmlTpl: Popup.prototype.getContentHtmlTpl,
  27858. getContentHtmlTpl: function () {
  27859. // if (this.items.length == 0) {
  27860. // return this._Popup_getContentHtmlTpl();
  27861. // }
  27862. // var buff = [];
  27863. // for (var i = 0; i < this.items.length; i++) {
  27864. // var item = this.items[i];
  27865. // buff[i] = item.renderHtml();
  27866. // }
  27867. // return '<div class="%%-body">' + buff.join("") + "</div>";
  27868. return '<div></div>'
  27869. },
  27870. // _Popup_postRender: Popup.prototype.postRender,
  27871. // postRender: function() {
  27872. // var me = this;
  27873. // for (var i = 0; i < this.items.length; i++) {
  27874. // var item = this.items[i];
  27875. // item.ownerMenu = this;
  27876. // item.postRender();
  27877. // }
  27878. // domUtils.on(this.getDom(), "mouseover", function(evt) {
  27879. // evt = evt || event;
  27880. // var rel = evt.relatedTarget || evt.fromElement;
  27881. // var el = me.getDom();
  27882. // if (!uiUtils.contains(el, rel) && el !== rel) {
  27883. // me.fireEvent("over");
  27884. // }
  27885. // });
  27886. // this._Popup_postRender();
  27887. // },
  27888. // queryAutoHide: function(el) {
  27889. // if (el) {
  27890. // if (uiUtils.contains(this.getDom(), el)) {
  27891. // return false;
  27892. // }
  27893. // for (var i = 0; i < this.items.length; i++) {
  27894. // var item = this.items[i];
  27895. // if (item.queryAutoHide(el) === false) {
  27896. // return false;
  27897. // }
  27898. // }
  27899. // }
  27900. // },
  27901. // clearItems: function() {
  27902. // for (var i = 0; i < this.items.length; i++) {
  27903. // var item = this.items[i];
  27904. // clearTimeout(item._showingTimer);
  27905. // clearTimeout(item._closingTimer);
  27906. // if (item.subMenu) {
  27907. // item.subMenu.destroy();
  27908. // }
  27909. // }
  27910. // this.items = [];
  27911. // },
  27912. destroy: function () {
  27913. if (this.getDom()) {
  27914. domUtils.remove(this.getDom());
  27915. }
  27916. // this.clearItems();
  27917. },
  27918. dispose: function () {
  27919. this.destroy();
  27920. }
  27921. };
  27922. utils.inherits(QuickOperate, Popup);
  27923. //
  27924. // /**
  27925. // * @update 2013/04/03 hancong03 新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  27926. // * @type {Function}
  27927. // */
  27928. // var MenuItem = (baidu.editor.ui.MenuItem = function(options) {
  27929. // this.initOptions(options);
  27930. // this.initUIBase();
  27931. // this.Stateful_init();
  27932. // if (this.subMenu && !(this.subMenu instanceof QuickOperate)) {
  27933. // if (options.className && options.className.indexOf("aligntd") != -1) {
  27934. // var me = this;
  27935. //
  27936. // //获取单元格对齐初始状态
  27937. // this.subMenu.selected = this.editor.queryCommandValue("cellalignment");
  27938. //
  27939. // this.subMenu = new Popup({
  27940. // content: new CellAlignPicker(this.subMenu),
  27941. // parentMenu: me,
  27942. // editor: me.editor,
  27943. // destroy: function() {
  27944. // if (this.getDom()) {
  27945. // domUtils.remove(this.getDom());
  27946. // }
  27947. // }
  27948. // });
  27949. // this.subMenu.addListener("postRenderAfter", function() {
  27950. // domUtils.on(this.getDom(), "mouseover", function() {
  27951. // me.addState("opened");
  27952. // });
  27953. // });
  27954. // } else {
  27955. // this.subMenu = new QuickOperate(this.subMenu);
  27956. // }
  27957. // }
  27958. // });
  27959. // MenuItem.prototype = {
  27960. // label: "",
  27961. // subMenu: null,
  27962. // ownerMenu: null,
  27963. // uiName: "menuitem",
  27964. // alwalysHoverable: true,
  27965. // getHtmlTpl: function() {
  27966. // return (
  27967. // '<div id="##" class="%%" stateful onclick="$$._onClick(event, this);">' +
  27968. // '<div class="%%-body">' +
  27969. // this.renderLabelHtml() +
  27970. // "</div>" +
  27971. // "</div>"
  27972. // );
  27973. // },
  27974. // postRender: function() {
  27975. // var me = this;
  27976. // this.addListener("over", function() {
  27977. // me.ownerMenu.fireEvent("submenuover", me);
  27978. // if (me.subMenu) {
  27979. // me.delayShowSubMenu();
  27980. // }
  27981. // });
  27982. // if (this.subMenu) {
  27983. // this.getDom().className += " edui-hassubmenu";
  27984. // this.subMenu.render();
  27985. // this.addListener("out", function() {
  27986. // me.delayHideSubMenu();
  27987. // });
  27988. // this.subMenu.addListener("over", function() {
  27989. // clearTimeout(me._closingTimer);
  27990. // me._closingTimer = null;
  27991. // me.addState("opened");
  27992. // });
  27993. // this.ownerMenu.addListener("hide", function() {
  27994. // me.hideSubMenu();
  27995. // });
  27996. // this.ownerMenu.addListener("submenuover", function(t, subMenu) {
  27997. // if (subMenu !== me) {
  27998. // me.delayHideSubMenu();
  27999. // }
  28000. // });
  28001. // this.subMenu._bakQueryAutoHide = this.subMenu.queryAutoHide;
  28002. // this.subMenu.queryAutoHide = function(el) {
  28003. // if (el && uiUtils.contains(me.getDom(), el)) {
  28004. // return false;
  28005. // }
  28006. // return this._bakQueryAutoHide(el);
  28007. // };
  28008. // }
  28009. // this.getDom().style.tabIndex = "-1";
  28010. // uiUtils.makeUnselectable(this.getDom());
  28011. // this.Stateful_postRender();
  28012. // },
  28013. // delayShowSubMenu: function() {
  28014. // var me = this;
  28015. // if (!me.isDisabled()) {
  28016. // me.addState("opened");
  28017. // clearTimeout(me._showingTimer);
  28018. // clearTimeout(me._closingTimer);
  28019. // me._closingTimer = null;
  28020. // me._showingTimer = setTimeout(function() {
  28021. // me.showSubMenu();
  28022. // }, 250);
  28023. // }
  28024. // },
  28025. // delayHideSubMenu: function() {
  28026. // var me = this;
  28027. // if (!me.isDisabled()) {
  28028. // me.removeState("opened");
  28029. // clearTimeout(me._showingTimer);
  28030. // if (!me._closingTimer) {
  28031. // me._closingTimer = setTimeout(function() {
  28032. // if (!me.hasState("opened")) {
  28033. // me.hideSubMenu();
  28034. // }
  28035. // me._closingTimer = null;
  28036. // }, 400);
  28037. // }
  28038. // }
  28039. // },
  28040. // renderLabelHtml: function() {
  28041. // return (
  28042. // '<div class="edui-arrow"></div>' +
  28043. // '<div class="edui-box edui-icon"></div>' +
  28044. // '<div class="edui-box edui-label %%-label">' +
  28045. // (this.label || "") +
  28046. // "</div>"
  28047. // );
  28048. // },
  28049. // getStateDom: function() {
  28050. // return this.getDom();
  28051. // },
  28052. // queryAutoHide: function(el) {
  28053. // if (this.subMenu && this.hasState("opened")) {
  28054. // return this.subMenu.queryAutoHide(el);
  28055. // }
  28056. // },
  28057. // _onClick: function(event, this_) {
  28058. // if (this.hasState("disabled")) return;
  28059. // if (this.fireEvent("click", event, this_) !== false) {
  28060. // if (this.subMenu) {
  28061. // this.showSubMenu();
  28062. // } else {
  28063. // Popup.postHide(event);
  28064. // }
  28065. // }
  28066. // },
  28067. // showSubMenu: function() {
  28068. // var rect = uiUtils.getClientRect(this.getDom());
  28069. // rect.right -= 5;
  28070. // rect.left += 2;
  28071. // rect.width -= 7;
  28072. // rect.top -= 4;
  28073. // rect.bottom += 4;
  28074. // rect.height += 8;
  28075. // this.subMenu.showAnchorRect(rect, true, true);
  28076. // },
  28077. // hideSubMenu: function() {
  28078. // this.subMenu.hide();
  28079. // }
  28080. // };
  28081. // utils.inherits(MenuItem, UIBase);
  28082. // utils.extend(MenuItem.prototype, Stateful, true);
  28083. })();
  28084. // ui/menu.js
  28085. ///import core
  28086. ///import uicore
  28087. ///import ui\popup.js
  28088. ///import ui\stateful.js
  28089. (function() {
  28090. var utils = baidu.editor.utils,
  28091. domUtils = baidu.editor.dom.domUtils,
  28092. uiUtils = baidu.editor.ui.uiUtils,
  28093. UIBase = baidu.editor.ui.UIBase,
  28094. Popup = baidu.editor.ui.Popup,
  28095. Stateful = baidu.editor.ui.Stateful,
  28096. CellAlignPicker = baidu.editor.ui.CellAlignPicker,
  28097. Menu = (baidu.editor.ui.Menu = function(options) {
  28098. this.initOptions(options);
  28099. this.initMenu();
  28100. });
  28101. var menuSeparator = {
  28102. renderHtml: function() {
  28103. return '<div class="edui-menuitem edui-menuseparator"><div class="edui-menuseparator-inner"></div></div>';
  28104. },
  28105. postRender: function() {},
  28106. queryAutoHide: function() {
  28107. return true;
  28108. }
  28109. };
  28110. Menu.prototype = {
  28111. items: null,
  28112. uiName: "menu",
  28113. initMenu: function() {
  28114. this.items = this.items || [];
  28115. this.initPopup();
  28116. this.initItems();
  28117. },
  28118. initItems: function() {
  28119. for (var i = 0; i < this.items.length; i++) {
  28120. var item = this.items[i];
  28121. if (item == "-") {
  28122. this.items[i] = this.getSeparator();
  28123. } else if (!(item instanceof MenuItem)) {
  28124. item.editor = this.editor;
  28125. item.theme = this.editor.options.theme;
  28126. this.items[i] = this.createItem(item);
  28127. }
  28128. }
  28129. },
  28130. getSeparator: function() {
  28131. return menuSeparator;
  28132. },
  28133. createItem: function(item) {
  28134. //新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28135. item.menu = this;
  28136. return new MenuItem(item);
  28137. },
  28138. _Popup_getContentHtmlTpl: Popup.prototype.getContentHtmlTpl,
  28139. getContentHtmlTpl: function() {
  28140. if (this.items.length == 0) {
  28141. return this._Popup_getContentHtmlTpl();
  28142. }
  28143. var buff = [];
  28144. for (var i = 0; i < this.items.length; i++) {
  28145. var item = this.items[i];
  28146. buff[i] = item.renderHtml();
  28147. }
  28148. return '<div class="%%-body">' + buff.join("") + "</div>";
  28149. },
  28150. _Popup_postRender: Popup.prototype.postRender,
  28151. postRender: function() {
  28152. var me = this;
  28153. for (var i = 0; i < this.items.length; i++) {
  28154. var item = this.items[i];
  28155. item.ownerMenu = this;
  28156. item.postRender();
  28157. }
  28158. domUtils.on(this.getDom(), "mouseover", function(evt) {
  28159. evt = evt || event;
  28160. var rel = evt.relatedTarget || evt.fromElement;
  28161. var el = me.getDom();
  28162. if (!uiUtils.contains(el, rel) && el !== rel) {
  28163. me.fireEvent("over");
  28164. }
  28165. });
  28166. this._Popup_postRender();
  28167. },
  28168. queryAutoHide: function(el) {
  28169. if (el) {
  28170. if (uiUtils.contains(this.getDom(), el)) {
  28171. return false;
  28172. }
  28173. for (var i = 0; i < this.items.length; i++) {
  28174. var item = this.items[i];
  28175. if (item.queryAutoHide(el) === false) {
  28176. return false;
  28177. }
  28178. }
  28179. }
  28180. },
  28181. clearItems: function() {
  28182. for (var i = 0; i < this.items.length; i++) {
  28183. var item = this.items[i];
  28184. clearTimeout(item._showingTimer);
  28185. clearTimeout(item._closingTimer);
  28186. if (item.subMenu) {
  28187. item.subMenu.destroy();
  28188. }
  28189. }
  28190. this.items = [];
  28191. },
  28192. destroy: function() {
  28193. if (this.getDom()) {
  28194. domUtils.remove(this.getDom());
  28195. }
  28196. this.clearItems();
  28197. },
  28198. dispose: function() {
  28199. this.destroy();
  28200. }
  28201. };
  28202. utils.inherits(Menu, Popup);
  28203. /**
  28204. * @update 2013/04/03 hancong03 新增一个参数menu, 该参数存储了menuItem所对应的menu引用
  28205. * @type {Function}
  28206. */
  28207. var MenuItem = (baidu.editor.ui.MenuItem = function(options) {
  28208. this.initOptions(options);
  28209. this.initUIBase();
  28210. this.Stateful_init();
  28211. if (this.subMenu && !(this.subMenu instanceof Menu)) {
  28212. if (options.className && options.className.indexOf("aligntd") != -1) {
  28213. var me = this;
  28214. //获取单元格对齐初始状态
  28215. this.subMenu.selected = this.editor.queryCommandValue("cellalignment");
  28216. this.subMenu = new Popup({
  28217. content: new CellAlignPicker(this.subMenu),
  28218. parentMenu: me,
  28219. editor: me.editor,
  28220. destroy: function() {
  28221. if (this.getDom()) {
  28222. domUtils.remove(this.getDom());
  28223. }
  28224. }
  28225. });
  28226. this.subMenu.addListener("postRenderAfter", function() {
  28227. domUtils.on(this.getDom(), "mouseover", function() {
  28228. me.addState("opened");
  28229. });
  28230. });
  28231. } else {
  28232. this.subMenu = new Menu(this.subMenu);
  28233. }
  28234. }
  28235. });
  28236. MenuItem.prototype = {
  28237. label: "",
  28238. subMenu: null,
  28239. ownerMenu: null,
  28240. uiName: "menuitem",
  28241. alwalysHoverable: true,
  28242. getHtmlTpl: function() {
  28243. return (
  28244. '<div id="##" class="%%" stateful onclick="$$._onClick(event, this);">' +
  28245. '<div class="%%-body">' +
  28246. this.renderLabelHtml() +
  28247. "</div>" +
  28248. "</div>"
  28249. );
  28250. },
  28251. postRender: function() {
  28252. var me = this;
  28253. this.addListener("over", function() {
  28254. me.ownerMenu.fireEvent("submenuover", me);
  28255. if (me.subMenu) {
  28256. me.delayShowSubMenu();
  28257. }
  28258. });
  28259. if (this.subMenu) {
  28260. this.getDom().className += " edui-hassubmenu";
  28261. this.subMenu.render();
  28262. this.addListener("out", function() {
  28263. me.delayHideSubMenu();
  28264. });
  28265. this.subMenu.addListener("over", function() {
  28266. clearTimeout(me._closingTimer);
  28267. me._closingTimer = null;
  28268. me.addState("opened");
  28269. });
  28270. this.ownerMenu.addListener("hide", function() {
  28271. me.hideSubMenu();
  28272. });
  28273. this.ownerMenu.addListener("submenuover", function(t, subMenu) {
  28274. if (subMenu !== me) {
  28275. me.delayHideSubMenu();
  28276. }
  28277. });
  28278. this.subMenu._bakQueryAutoHide = this.subMenu.queryAutoHide;
  28279. this.subMenu.queryAutoHide = function(el) {
  28280. if (el && uiUtils.contains(me.getDom(), el)) {
  28281. return false;
  28282. }
  28283. return this._bakQueryAutoHide(el);
  28284. };
  28285. }
  28286. this.getDom().style.tabIndex = "-1";
  28287. uiUtils.makeUnselectable(this.getDom());
  28288. this.Stateful_postRender();
  28289. },
  28290. delayShowSubMenu: function() {
  28291. var me = this;
  28292. if (!me.isDisabled()) {
  28293. me.addState("opened");
  28294. clearTimeout(me._showingTimer);
  28295. clearTimeout(me._closingTimer);
  28296. me._closingTimer = null;
  28297. me._showingTimer = setTimeout(function() {
  28298. me.showSubMenu();
  28299. }, 250);
  28300. }
  28301. },
  28302. delayHideSubMenu: function() {
  28303. var me = this;
  28304. if (!me.isDisabled()) {
  28305. me.removeState("opened");
  28306. clearTimeout(me._showingTimer);
  28307. if (!me._closingTimer) {
  28308. me._closingTimer = setTimeout(function() {
  28309. if (!me.hasState("opened")) {
  28310. me.hideSubMenu();
  28311. }
  28312. me._closingTimer = null;
  28313. }, 400);
  28314. }
  28315. }
  28316. },
  28317. renderLabelHtml: function() {
  28318. return (
  28319. '<div class="edui-arrow"></div>' +
  28320. '<div class="edui-box edui-icon"></div>' +
  28321. '<div class="edui-box edui-label %%-label">' +
  28322. (this.label || "") +
  28323. "</div>"
  28324. );
  28325. },
  28326. getStateDom: function() {
  28327. return this.getDom();
  28328. },
  28329. queryAutoHide: function(el) {
  28330. if (this.subMenu && this.hasState("opened")) {
  28331. return this.subMenu.queryAutoHide(el);
  28332. }
  28333. },
  28334. _onClick: function(event, this_) {
  28335. if (this.hasState("disabled")) return;
  28336. if (this.fireEvent("click", event, this_) !== false) {
  28337. if (this.subMenu) {
  28338. this.showSubMenu();
  28339. } else {
  28340. Popup.postHide(event);
  28341. }
  28342. }
  28343. },
  28344. showSubMenu: function() {
  28345. var rect = uiUtils.getClientRect(this.getDom());
  28346. rect.right -= 5;
  28347. rect.left += 2;
  28348. rect.width -= 7;
  28349. rect.top -= 4;
  28350. rect.bottom += 4;
  28351. rect.height += 8;
  28352. this.subMenu.showAnchorRect(rect, true, true);
  28353. },
  28354. hideSubMenu: function() {
  28355. this.subMenu.hide();
  28356. }
  28357. };
  28358. utils.inherits(MenuItem, UIBase);
  28359. utils.extend(MenuItem.prototype, Stateful, true);
  28360. })();
  28361. // ui/combox.js
  28362. ///import core
  28363. ///import uicore
  28364. ///import ui/menu.js
  28365. ///import ui/splitbutton.js
  28366. (function() {
  28367. // todo: menu和item提成通用list
  28368. var utils = baidu.editor.utils,
  28369. uiUtils = baidu.editor.ui.uiUtils,
  28370. Menu = baidu.editor.ui.Menu,
  28371. SplitButton = baidu.editor.ui.SplitButton,
  28372. Combox = (baidu.editor.ui.Combox = function(options) {
  28373. this.initOptions(options);
  28374. this.initCombox();
  28375. });
  28376. Combox.prototype = {
  28377. uiName: "combox",
  28378. onbuttonclick: function() {
  28379. this.showPopup();
  28380. },
  28381. initCombox: function() {
  28382. var me = this;
  28383. this.items = this.items || [];
  28384. for (var i = 0; i < this.items.length; i++) {
  28385. var item = this.items[i];
  28386. item.uiName = "listitem";
  28387. item.index = i;
  28388. item.onclick = function() {
  28389. me.selectByIndex(this.index);
  28390. };
  28391. }
  28392. this.popup = new Menu({
  28393. items: this.items,
  28394. uiName: "list",
  28395. editor: this.editor,
  28396. captureWheel: true,
  28397. combox: this
  28398. });
  28399. this.initSplitButton();
  28400. },
  28401. _SplitButton_postRender: SplitButton.prototype.postRender,
  28402. postRender: function() {
  28403. this._SplitButton_postRender();
  28404. this.setLabel(this.label || "");
  28405. this.setValue(this.initValue || "");
  28406. },
  28407. showPopup: function() {
  28408. var rect = uiUtils.getClientRect(this.getDom());
  28409. rect.top += 1;
  28410. rect.bottom -= 1;
  28411. rect.height -= 2;
  28412. this.popup.showAnchorRect(rect);
  28413. },
  28414. getValue: function() {
  28415. return this.value;
  28416. },
  28417. setValue: function(value) {
  28418. var index = this.indexByValue(value);
  28419. if (index != -1) {
  28420. this.selectedIndex = index;
  28421. this.setLabel(this.items[index].label);
  28422. this.value = this.items[index].value;
  28423. } else {
  28424. this.selectedIndex = -1;
  28425. this.setLabel(this.getLabelForUnknowValue(value));
  28426. this.value = value;
  28427. }
  28428. },
  28429. setLabel: function(label) {
  28430. this.getDom("button_body").innerHTML = label;
  28431. this.label = label;
  28432. },
  28433. getLabelForUnknowValue: function(value) {
  28434. return value;
  28435. },
  28436. indexByValue: function(value) {
  28437. for (var i = 0; i < this.items.length; i++) {
  28438. if (value == this.items[i].value) {
  28439. return i;
  28440. }
  28441. }
  28442. return -1;
  28443. },
  28444. getItem: function(index) {
  28445. return this.items[index];
  28446. },
  28447. selectByIndex: function(index) {
  28448. if (
  28449. index < this.items.length &&
  28450. this.fireEvent("select", index) !== false
  28451. ) {
  28452. this.selectedIndex = index;
  28453. this.value = this.items[index].value;
  28454. this.setLabel(this.items[index].label);
  28455. }
  28456. }
  28457. };
  28458. utils.inherits(Combox, SplitButton);
  28459. })();
  28460. // ui/dialog.js
  28461. ///import core
  28462. ///import uicore
  28463. ///import ui/mask.js
  28464. ///import ui/button.js
  28465. (function() {
  28466. var utils = baidu.editor.utils,
  28467. domUtils = baidu.editor.dom.domUtils,
  28468. uiUtils = baidu.editor.ui.uiUtils,
  28469. Mask = baidu.editor.ui.Mask,
  28470. UIBase = baidu.editor.ui.UIBase,
  28471. Button = baidu.editor.ui.Button,
  28472. Dialog = (baidu.editor.ui.Dialog = function(options) {
  28473. if (options.name) {
  28474. var name = options.name;
  28475. var cssRules = options.cssRules;
  28476. if (!options.className) {
  28477. options.className = "edui-for-" + name;
  28478. }
  28479. if (cssRules) {
  28480. options.cssRules =
  28481. ".edui-for-" + name + " .edui-dialog-content {" + cssRules + "}";
  28482. }
  28483. }
  28484. this.initOptions(
  28485. utils.extend(
  28486. {
  28487. autoReset: true,
  28488. draggable: true,
  28489. onok: function() {},
  28490. oncancel: function() {},
  28491. onclose: function(t, ok) {
  28492. return ok ? this.onok() : this.oncancel();
  28493. },
  28494. //是否控制dialog中的scroll事件, 默认为不阻止
  28495. holdScroll: false
  28496. },
  28497. options
  28498. )
  28499. );
  28500. this.initDialog();
  28501. });
  28502. var modalMask;
  28503. var dragMask;
  28504. var activeDialog;
  28505. Dialog.prototype = {
  28506. draggable: false,
  28507. uiName: "dialog",
  28508. initDialog: function() {
  28509. var me = this,
  28510. theme = this.editor.options.theme;
  28511. if (this.cssRules) {
  28512. this.cssRules = ".edui-" + theme + " " + this.cssRules;
  28513. utils.cssRule("edui-customize-" + this.name + "-style", this.cssRules);
  28514. }
  28515. this.initUIBase();
  28516. this.modalMask =
  28517. modalMask ||
  28518. (modalMask = new Mask({
  28519. className: "edui-dialog-modalmask",
  28520. theme: theme,
  28521. onclick: function() {
  28522. activeDialog && activeDialog.close(false);
  28523. }
  28524. }));
  28525. this.dragMask =
  28526. dragMask ||
  28527. (dragMask = new Mask({
  28528. className: "edui-dialog-dragmask",
  28529. theme: theme
  28530. }));
  28531. this.closeButton = new Button({
  28532. className: "edui-dialog-closebutton",
  28533. title: me.closeDialog,
  28534. theme: theme,
  28535. onclick: function() {
  28536. me.close(false);
  28537. }
  28538. });
  28539. this.fullscreen && this.initResizeEvent();
  28540. if (this.buttons) {
  28541. for (var i = 0; i < this.buttons.length; i++) {
  28542. if (!(this.buttons[i] instanceof Button)) {
  28543. this.buttons[i] = new Button(
  28544. utils.extend(
  28545. this.buttons[i],
  28546. {
  28547. editor: this.editor
  28548. },
  28549. true
  28550. )
  28551. );
  28552. }
  28553. }
  28554. }
  28555. },
  28556. initResizeEvent: function() {
  28557. var me = this;
  28558. domUtils.on(window, "resize", function() {
  28559. if (me._hidden || me._hidden === undefined) {
  28560. return;
  28561. }
  28562. if (me.__resizeTimer) {
  28563. window.clearTimeout(me.__resizeTimer);
  28564. }
  28565. me.__resizeTimer = window.setTimeout(function() {
  28566. me.__resizeTimer = null;
  28567. var dialogWrapNode = me.getDom(),
  28568. contentNode = me.getDom("content"),
  28569. wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28570. contentRect = UE.ui.uiUtils.getClientRect(contentNode),
  28571. vpRect = uiUtils.getViewportRect();
  28572. contentNode.style.width =
  28573. vpRect.width - wrapRect.width + contentRect.width + "px";
  28574. contentNode.style.height =
  28575. vpRect.height - wrapRect.height + contentRect.height + "px";
  28576. dialogWrapNode.style.width = vpRect.width + "px";
  28577. dialogWrapNode.style.height = vpRect.height + "px";
  28578. me.fireEvent("resize");
  28579. }, 100);
  28580. });
  28581. },
  28582. fitSize: function() {
  28583. // console.log('fitSize.dialog')
  28584. var popBodyEl = this.getDom("body");
  28585. var $foot = popBodyEl.querySelector('.edui-dialog-foot');
  28586. var heightWithoutBody = 70;
  28587. if(!$foot){
  28588. heightWithoutBody = 30;
  28589. }
  28590. var size = this.mesureSize();
  28591. var winSize = uiUtils.getViewportRect();
  28592. var width = size.width;
  28593. var height = size.height - heightWithoutBody;
  28594. var maxWidth = winSize.width - 2;
  28595. var maxHeight = winSize.height - heightWithoutBody - 2;
  28596. if(width > maxWidth){
  28597. height = height * maxWidth / width;
  28598. width = maxWidth;
  28599. }
  28600. if(height > maxHeight){
  28601. width = width * maxHeight / height;
  28602. height = maxHeight;
  28603. }
  28604. var scale = (width / size.width);
  28605. // console.log('size', {sizeWidth:size.width,sizeHeight:size.height,width,height,scale});
  28606. // console.log('popBodyEl',popBodyEl, popBodyEl.querySelector('.edui-dialog-foot'));
  28607. // window._xxx = popBodyEl;
  28608. var $content = popBodyEl.querySelector('.edui-dialog-content');
  28609. if(!$content.dataset.dialogScaled){
  28610. $content.dataset.dialogScaled = true
  28611. $content.style.width = (width)+'px';
  28612. $content.style.height = (height)+'px';
  28613. var $iframe = popBodyEl.querySelector('.edui-dialog-content iframe');
  28614. $iframe.style.width = (size.width) +'px';
  28615. $iframe.style.height = (size.height) +'px';
  28616. $iframe.style.transformOrigin = '0 0';
  28617. $iframe.style.transform = 'scale('+scale+')';
  28618. size.width = width
  28619. size.height = height + heightWithoutBody
  28620. }
  28621. popBodyEl.style.width = size.width + "px";
  28622. popBodyEl.style.height = size.height + "px";
  28623. return size;
  28624. },
  28625. safeSetOffset: function(offset) {
  28626. var me = this;
  28627. var el = me.getDom();
  28628. var vpRect = uiUtils.getViewportRect();
  28629. var rect = uiUtils.getClientRect(el);
  28630. var left = offset.left;
  28631. if (left + rect.width > vpRect.right) {
  28632. left = vpRect.right - rect.width;
  28633. }
  28634. var top = offset.top;
  28635. if (top + rect.height > vpRect.bottom) {
  28636. top = vpRect.bottom - rect.height;
  28637. }
  28638. el.style.left = Math.max(left, 0) + "px";
  28639. el.style.top = Math.max(top, 0) + "px";
  28640. },
  28641. showAtCenter: function() {
  28642. var vpRect = uiUtils.getViewportRect();
  28643. if (!this.fullscreen) {
  28644. this.getDom().style.display = "";
  28645. var popSize = this.fitSize();
  28646. var titleHeight = this.getDom("titlebar").offsetHeight | 0;
  28647. var left = vpRect.width / 2 - popSize.width / 2;
  28648. var top =
  28649. vpRect.height / 2 - (popSize.height - titleHeight) / 2 - titleHeight;
  28650. var popEl = this.getDom();
  28651. this.safeSetOffset({
  28652. left: Math.max(left | 0, 0),
  28653. top: Math.max(top | 0, 0)
  28654. });
  28655. if (!domUtils.hasClass(popEl, "edui-state-centered")) {
  28656. popEl.className += " edui-state-centered";
  28657. }
  28658. } else {
  28659. var dialogWrapNode = this.getDom(),
  28660. contentNode = this.getDom("content");
  28661. dialogWrapNode.style.display = "block";
  28662. var wrapRect = UE.ui.uiUtils.getClientRect(dialogWrapNode),
  28663. contentRect = UE.ui.uiUtils.getClientRect(contentNode);
  28664. dialogWrapNode.style.left = "-100000px";
  28665. contentNode.style.width =
  28666. vpRect.width - wrapRect.width + contentRect.width + "px";
  28667. contentNode.style.height =
  28668. vpRect.height - wrapRect.height + contentRect.height + "px";
  28669. dialogWrapNode.style.width = vpRect.width + "px";
  28670. dialogWrapNode.style.height = vpRect.height + "px";
  28671. dialogWrapNode.style.left = 0;
  28672. //保存环境的overflow值
  28673. this._originalContext = {
  28674. html: {
  28675. overflowX: document.documentElement.style.overflowX,
  28676. overflowY: document.documentElement.style.overflowY
  28677. },
  28678. body: {
  28679. overflowX: document.body.style.overflowX,
  28680. overflowY: document.body.style.overflowY
  28681. }
  28682. };
  28683. document.documentElement.style.overflowX = "hidden";
  28684. document.documentElement.style.overflowY = "hidden";
  28685. document.body.style.overflowX = "hidden";
  28686. document.body.style.overflowY = "hidden";
  28687. }
  28688. this._show();
  28689. },
  28690. getContentHtml: function() {
  28691. var contentHtml = "";
  28692. if (typeof this.content == "string") {
  28693. contentHtml = this.content;
  28694. } else if (this.iframeUrl) {
  28695. contentHtml =
  28696. '<span id="' +
  28697. this.id +
  28698. '_contmask" class="dialogcontmask"></span><iframe id="' +
  28699. this.id +
  28700. '_iframe" class="%%-iframe" height="100%" width="100%" frameborder="0" src="' +
  28701. this.iframeUrl +
  28702. '"></iframe>';
  28703. }
  28704. return contentHtml;
  28705. },
  28706. getHtmlTpl: function() {
  28707. var footHtml = "";
  28708. if (this.buttons) {
  28709. var buff = [];
  28710. for (var i = 0; i < this.buttons.length; i++) {
  28711. buff[i] = this.buttons[i].renderHtml();
  28712. }
  28713. footHtml =
  28714. '<div class="%%-foot">' +
  28715. '<div id="##_buttons" class="%%-buttons">' +
  28716. buff.join("") +
  28717. "</div>" +
  28718. "</div>";
  28719. }
  28720. return (
  28721. '<div id="##" class="%%"><div ' +
  28722. (!this.fullscreen
  28723. ? 'class="%%"'
  28724. : 'class="%%-wrap edui-dialog-fullscreen-flag"') +
  28725. '><div id="##_body" class="%%-body">' +
  28726. '<div class="%%-shadow"></div>' +
  28727. '<div id="##_titlebar" class="%%-titlebar">' +
  28728. '<div class="%%-draghandle" onmousedown="$$._onTitlebarMouseDown(event, this);">' +
  28729. '<span class="%%-caption">' +
  28730. (this.title || "") +
  28731. "</span>" +
  28732. "</div>" +
  28733. this.closeButton.renderHtml() +
  28734. "</div>" +
  28735. '<div id="##_content" class="%%-content">' +
  28736. (this.autoReset ? "" : this.getContentHtml()) +
  28737. "</div>" +
  28738. footHtml +
  28739. "</div></div></div>"
  28740. );
  28741. },
  28742. postRender: function() {
  28743. // todo: 保持居中/记住上次关闭位置选项
  28744. if (!this.modalMask.getDom()) {
  28745. this.modalMask.render();
  28746. this.modalMask.hide();
  28747. }
  28748. if (!this.dragMask.getDom()) {
  28749. this.dragMask.render();
  28750. this.dragMask.hide();
  28751. }
  28752. var me = this;
  28753. this.addListener("show", function() {
  28754. me.modalMask.show(this.getDom().style.zIndex - 2);
  28755. });
  28756. this.addListener("hide", function() {
  28757. me.modalMask.hide();
  28758. });
  28759. if (this.buttons) {
  28760. for (var i = 0; i < this.buttons.length; i++) {
  28761. this.buttons[i].postRender();
  28762. }
  28763. }
  28764. domUtils.on(window, "resize", function() {
  28765. setTimeout(function() {
  28766. if (!me.isHidden()) {
  28767. me.safeSetOffset(uiUtils.getClientRect(me.getDom()));
  28768. }
  28769. });
  28770. });
  28771. //hold住scroll事件,防止dialog的滚动影响页面
  28772. // if( this.holdScroll ) {
  28773. //
  28774. // if( !me.iframeUrl ) {
  28775. // domUtils.on( document.getElementById( me.id + "_iframe"), !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  28776. // domUtils.preventDefault(e);
  28777. // } );
  28778. // } else {
  28779. // me.addListener('dialogafterreset', function(){
  28780. // window.setTimeout(function(){
  28781. // var iframeWindow = document.getElementById( me.id + "_iframe").contentWindow;
  28782. //
  28783. // if( browser.ie ) {
  28784. //
  28785. // var timer = window.setInterval(function(){
  28786. //
  28787. // if( iframeWindow.document && iframeWindow.document.body ) {
  28788. // window.clearInterval( timer );
  28789. // timer = null;
  28790. // domUtils.on( iframeWindow.document.body, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  28791. // domUtils.preventDefault(e);
  28792. // } );
  28793. // }
  28794. //
  28795. // }, 100);
  28796. //
  28797. // } else {
  28798. // domUtils.on( iframeWindow, !browser.gecko ? "mousewheel" : "DOMMouseScroll", function(e){
  28799. // domUtils.preventDefault(e);
  28800. // } );
  28801. // }
  28802. //
  28803. // }, 1);
  28804. // });
  28805. // }
  28806. //
  28807. // }
  28808. this._hide();
  28809. },
  28810. mesureSize: function() {
  28811. var body = this.getDom("body");
  28812. var width = uiUtils.getClientRect(this.getDom("content")).width;
  28813. var dialogBodyStyle = body.style;
  28814. dialogBodyStyle.width = width;
  28815. return uiUtils.getClientRect(body);
  28816. },
  28817. _onTitlebarMouseDown: function(evt, el) {
  28818. if (this.draggable) {
  28819. var rect;
  28820. var vpRect = uiUtils.getViewportRect();
  28821. var me = this;
  28822. uiUtils.startDrag(evt, {
  28823. ondragstart: function() {
  28824. rect = uiUtils.getClientRect(me.getDom());
  28825. me.getDom("contmask").style.visibility = "visible";
  28826. me.dragMask.show(me.getDom().style.zIndex - 1);
  28827. },
  28828. ondragmove: function(x, y) {
  28829. var left = rect.left + x;
  28830. var top = rect.top + y;
  28831. me.safeSetOffset({
  28832. left: left,
  28833. top: top
  28834. });
  28835. },
  28836. ondragstop: function() {
  28837. me.getDom("contmask").style.visibility = "hidden";
  28838. domUtils.removeClasses(me.getDom(), ["edui-state-centered"]);
  28839. me.dragMask.hide();
  28840. }
  28841. });
  28842. }
  28843. },
  28844. reset: function() {
  28845. this.getDom("content").innerHTML = this.getContentHtml();
  28846. this.fireEvent("dialogafterreset");
  28847. },
  28848. _show: function() {
  28849. if (this._hidden) {
  28850. this.getDom().style.display = "";
  28851. //要高过编辑器的zindxe
  28852. this.editor.container.style.zIndex &&
  28853. (this.getDom().style.zIndex =
  28854. this.editor.container.style.zIndex * 1 + 10);
  28855. this._hidden = false;
  28856. this.fireEvent("show");
  28857. baidu.editor.ui.uiUtils.getFixedLayer().style.zIndex =
  28858. this.getDom().style.zIndex - 4;
  28859. }
  28860. },
  28861. isHidden: function() {
  28862. return this._hidden;
  28863. },
  28864. _hide: function() {
  28865. if (!this._hidden) {
  28866. var wrapNode = this.getDom();
  28867. wrapNode.style.display = "none";
  28868. wrapNode.style.zIndex = "";
  28869. wrapNode.style.width = "";
  28870. wrapNode.style.height = "";
  28871. this._hidden = true;
  28872. this.fireEvent("hide");
  28873. }
  28874. },
  28875. open: function() {
  28876. if (this.autoReset) {
  28877. //有可能还没有渲染
  28878. try {
  28879. this.reset();
  28880. } catch (e) {
  28881. this.render();
  28882. this.open();
  28883. }
  28884. }
  28885. this.showAtCenter();
  28886. if (this.iframeUrl) {
  28887. try {
  28888. this.getDom("iframe").focus();
  28889. } catch (ex) {}
  28890. }
  28891. activeDialog = this;
  28892. },
  28893. _onCloseButtonClick: function(evt, el) {
  28894. this.close(false);
  28895. },
  28896. close: function(ok) {
  28897. if (this.fireEvent("close", ok) !== false) {
  28898. //还原环境
  28899. if (this.fullscreen) {
  28900. document.documentElement.style.overflowX = this._originalContext.html.overflowX;
  28901. document.documentElement.style.overflowY = this._originalContext.html.overflowY;
  28902. document.body.style.overflowX = this._originalContext.body.overflowX;
  28903. document.body.style.overflowY = this._originalContext.body.overflowY;
  28904. delete this._originalContext;
  28905. }
  28906. this._hide();
  28907. //销毁content
  28908. var content = this.getDom("content");
  28909. var iframe = this.getDom("iframe");
  28910. if (content && iframe) {
  28911. var doc = iframe.contentDocument || iframe.contentWindow.document;
  28912. doc && (doc.body.innerHTML = "");
  28913. domUtils.remove(content);
  28914. }
  28915. }
  28916. }
  28917. };
  28918. utils.inherits(Dialog, UIBase);
  28919. })();
  28920. // ui/menubutton.js
  28921. ///import core
  28922. ///import uicore
  28923. ///import ui/menu.js
  28924. ///import ui/splitbutton.js
  28925. (function() {
  28926. var utils = baidu.editor.utils,
  28927. Menu = baidu.editor.ui.Menu,
  28928. SplitButton = baidu.editor.ui.SplitButton,
  28929. MenuButton = (baidu.editor.ui.MenuButton = function(options) {
  28930. this.initOptions(options);
  28931. this.initMenuButton();
  28932. });
  28933. MenuButton.prototype = {
  28934. initMenuButton: function() {
  28935. var me = this;
  28936. this.uiName = "menubutton";
  28937. this.popup = new Menu({
  28938. items: me.items,
  28939. className: me.className,
  28940. editor: me.editor
  28941. });
  28942. this.popup.addListener("show", function() {
  28943. var list = this;
  28944. for (var i = 0; i < list.items.length; i++) {
  28945. list.items[i].removeState("checked");
  28946. if (list.items[i].value == me._value) {
  28947. list.items[i].addState("checked");
  28948. this.value = me._value;
  28949. }
  28950. }
  28951. });
  28952. this.initSplitButton();
  28953. },
  28954. setValue: function(value) {
  28955. this._value = value;
  28956. }
  28957. };
  28958. utils.inherits(MenuButton, SplitButton);
  28959. })();
  28960. // ui/multiMenu.js
  28961. ///import core
  28962. ///import uicore
  28963. ///commands 表情
  28964. (function() {
  28965. var utils = baidu.editor.utils,
  28966. Popup = baidu.editor.ui.Popup,
  28967. SplitButton = baidu.editor.ui.SplitButton,
  28968. MultiMenuPop = (baidu.editor.ui.MultiMenuPop = function(options) {
  28969. this.initOptions(options);
  28970. this.initMultiMenu();
  28971. });
  28972. MultiMenuPop.prototype = {
  28973. initMultiMenu: function() {
  28974. var me = this;
  28975. this.popup = new Popup({
  28976. content: "",
  28977. editor: me.editor,
  28978. iframe_rendered: false,
  28979. onshow: function() {
  28980. if (!this.iframe_rendered) {
  28981. this.iframe_rendered = true;
  28982. this.getDom("content").innerHTML =
  28983. '<iframe id="' +
  28984. me.id +
  28985. '_iframe" src="' +
  28986. me.iframeUrl +
  28987. '" frameborder="0"></iframe>';
  28988. me.editor.container.style.zIndex &&
  28989. (this.getDom().style.zIndex =
  28990. me.editor.container.style.zIndex * 1 + 1);
  28991. }
  28992. }
  28993. // canSideUp:false,
  28994. // canSideLeft:false
  28995. });
  28996. this.onbuttonclick = function() {
  28997. this.showPopup();
  28998. };
  28999. this.initSplitButton();
  29000. }
  29001. };
  29002. utils.inherits(MultiMenuPop, SplitButton);
  29003. })();
  29004. // ui/shortcutmenu.js
  29005. (function() {
  29006. var UI = baidu.editor.ui,
  29007. UIBase = UI.UIBase,
  29008. uiUtils = UI.uiUtils,
  29009. utils = baidu.editor.utils,
  29010. domUtils = baidu.editor.dom.domUtils;
  29011. var allMenus = [], //存储所有快捷菜单
  29012. timeID,
  29013. isSubMenuShow = false; //是否有子pop显示
  29014. var ShortCutMenu = (UI.ShortCutMenu = function(options) {
  29015. this.initOptions(options);
  29016. this.initShortCutMenu();
  29017. });
  29018. ShortCutMenu.postHide = hideAllMenu;
  29019. ShortCutMenu.prototype = {
  29020. isHidden: true,
  29021. SPACE: 5,
  29022. initShortCutMenu: function() {
  29023. this.items = this.items || [];
  29024. this.initUIBase();
  29025. this.initItems();
  29026. this.initEvent();
  29027. allMenus.push(this);
  29028. },
  29029. initEvent: function() {
  29030. var me = this,
  29031. doc = me.editor.document;
  29032. /*
  29033. domUtils.on(doc, "mousemove", function(e) {
  29034. if (me.isHidden === false) {
  29035. //有pop显示就不隐藏快捷菜单
  29036. if (me.getSubMenuMark() || me.eventType == "contextmenu") return;
  29037. var flag = true,
  29038. el = me.getDom(),
  29039. wt = el.offsetWidth,
  29040. ht = el.offsetHeight,
  29041. distanceX = wt / 2 + me.SPACE, //距离中心X标准
  29042. distanceY = ht / 2, //距离中心Y标准
  29043. x = Math.abs(e.screenX - me.left), //离中心距离横坐标
  29044. y = Math.abs(e.screenY - me.top); //离中心距离纵坐标
  29045. clearTimeout(timeID);
  29046. timeID = setTimeout(function() {
  29047. if (y > 0 && y < distanceY) {
  29048. me.setOpacity(el, "1");
  29049. } else if (y > distanceY && y < distanceY + 70) {
  29050. me.setOpacity(el, "0.5");
  29051. flag = false;
  29052. } else if (y > distanceY + 70 && y < distanceY + 140) {
  29053. me.hide();
  29054. }
  29055. if (flag && x > 0 && x < distanceX) {
  29056. me.setOpacity(el, "1");
  29057. } else if (x > distanceX && x < distanceX + 70) {
  29058. me.setOpacity(el, "0.5");
  29059. } else if (x > distanceX + 70 && x < distanceX + 140) {
  29060. console.log('hide')
  29061. me.hide();
  29062. }
  29063. });
  29064. }
  29065. });
  29066. */
  29067. //ie\ff下 mouseout不准
  29068. /*
  29069. if (browser.chrome) {
  29070. domUtils.on(doc, "mouseout", function(e) {
  29071. var relatedTgt = e.relatedTarget || e.toElement;
  29072. if (relatedTgt == null || relatedTgt.tagName == "HTML") {
  29073. me.hide();
  29074. }
  29075. });
  29076. }
  29077. */
  29078. me.editor.addListener("afterhidepop", function() {
  29079. if (!me.isHidden) {
  29080. isSubMenuShow = true;
  29081. }
  29082. });
  29083. },
  29084. initItems: function() {
  29085. if (utils.isArray(this.items)) {
  29086. for (var i = 0, len = this.items.length; i < len; i++) {
  29087. var item = this.items[i].toLowerCase();
  29088. if (UI[item]) {
  29089. this.items[i] = new UI[item](this.editor);
  29090. this.items[i].className += " edui-shortcutsubmenu ";
  29091. }
  29092. }
  29093. }
  29094. },
  29095. setOpacity: function(el, value) {
  29096. if (browser.ie && browser.version < 9) {
  29097. el.style.filter = "alpha(opacity = " + parseFloat(value) * 100 + ");";
  29098. } else {
  29099. el.style.opacity = value;
  29100. }
  29101. },
  29102. getSubMenuMark: function() {
  29103. isSubMenuShow = false;
  29104. var layerEle = uiUtils.getFixedLayer();
  29105. var list = domUtils.getElementsByTagName(layerEle, "div", function(node) {
  29106. return domUtils.hasClass(node, "edui-shortcutsubmenu edui-popup");
  29107. });
  29108. for (var i = 0, node; (node = list[i++]); ) {
  29109. if (node.style.display != "none") {
  29110. isSubMenuShow = true;
  29111. }
  29112. }
  29113. return isSubMenuShow;
  29114. },
  29115. show: function(e, hasContextmenu) {
  29116. var me = this,
  29117. offset = {},
  29118. el = this.getDom(),
  29119. fixedlayer = uiUtils.getFixedLayer();
  29120. function setPos(offset) {
  29121. if (offset.left < 0) {
  29122. offset.left = 0;
  29123. }
  29124. if (offset.top < 0) {
  29125. offset.top = 0;
  29126. }
  29127. el.style.cssText =
  29128. "position:absolute;left:" +
  29129. offset.left +
  29130. "px;top:" +
  29131. offset.top +
  29132. "px;";
  29133. }
  29134. function setPosByCxtMenu(menu) {
  29135. if (!menu.tagName) {
  29136. menu = menu.getDom();
  29137. }
  29138. offset.left = parseInt(menu.style.left);
  29139. offset.top = parseInt(menu.style.top);
  29140. offset.top -= el.offsetHeight + 15;
  29141. setPos(offset);
  29142. }
  29143. me.eventType = e.type;
  29144. el.style.cssText = "display:block;left:-9999px";
  29145. if (e.type == "contextmenu" && hasContextmenu) {
  29146. var menu = domUtils.getElementsByTagName(
  29147. fixedlayer,
  29148. "div",
  29149. "edui-contextmenu"
  29150. )[0];
  29151. if (menu) {
  29152. setPosByCxtMenu(menu);
  29153. } else {
  29154. me.editor.addListener("aftershowcontextmenu", function(type, menu) {
  29155. setPosByCxtMenu(menu);
  29156. });
  29157. }
  29158. } else {
  29159. offset = uiUtils.getViewportOffsetByEvent(e);
  29160. offset.top -= el.offsetHeight + me.SPACE;
  29161. offset.left += me.SPACE + 20;
  29162. setPos(offset);
  29163. me.setOpacity(el, 1);
  29164. }
  29165. me.isHidden = false;
  29166. me.left = e.screenX + el.offsetWidth / 2 - me.SPACE;
  29167. me.top = e.screenY - el.offsetHeight / 2 - me.SPACE;
  29168. if (me.editor) {
  29169. el.style.zIndex = me.editor.container.style.zIndex * 1 + 10;
  29170. fixedlayer.style.zIndex = el.style.zIndex - 1;
  29171. }
  29172. },
  29173. hide: function() {
  29174. if (this.getDom()) {
  29175. this.getDom().style.display = "none";
  29176. }
  29177. this.isHidden = true;
  29178. },
  29179. postRender: function() {
  29180. if (utils.isArray(this.items)) {
  29181. for (var i = 0, item; (item = this.items[i++]); ) {
  29182. item.postRender();
  29183. }
  29184. }
  29185. },
  29186. getHtmlTpl: function() {
  29187. var buff;
  29188. if (utils.isArray(this.items)) {
  29189. buff = [];
  29190. for (var i = 0; i < this.items.length; i++) {
  29191. buff[i] = this.items[i].renderHtml();
  29192. }
  29193. buff = buff.join("");
  29194. } else {
  29195. buff = this.items;
  29196. }
  29197. return (
  29198. '<div id="##" class="%% edui-toolbar" data-src="shortcutmenu" onmousedown="return false;" onselectstart="return false;" >' +
  29199. buff +
  29200. "</div>"
  29201. );
  29202. }
  29203. };
  29204. utils.inherits(ShortCutMenu, UIBase);
  29205. function hideAllMenu(e) {
  29206. var tgt = e.target || e.srcElement,
  29207. cur = domUtils.findParent(
  29208. tgt,
  29209. function(node) {
  29210. return (
  29211. domUtils.hasClass(node, "edui-shortcutmenu") ||
  29212. domUtils.hasClass(node, "edui-popup")
  29213. );
  29214. },
  29215. true
  29216. );
  29217. if (!cur) {
  29218. for (var i = 0, menu; (menu = allMenus[i++]); ) {
  29219. menu.hide();
  29220. }
  29221. }
  29222. }
  29223. domUtils.on(document, "mousedown", function(e) {
  29224. hideAllMenu(e);
  29225. });
  29226. domUtils.on(window, "scroll", function(e) {
  29227. hideAllMenu(e);
  29228. });
  29229. })();
  29230. // ui/breakline.js
  29231. (function() {
  29232. var utils = baidu.editor.utils,
  29233. UIBase = baidu.editor.ui.UIBase,
  29234. Breakline = (baidu.editor.ui.Breakline = function(options) {
  29235. this.initOptions(options);
  29236. this.initSeparator();
  29237. });
  29238. Breakline.prototype = {
  29239. uiName: "Breakline",
  29240. initSeparator: function() {
  29241. this.initUIBase();
  29242. },
  29243. getHtmlTpl: function() {
  29244. return "<br/>";
  29245. }
  29246. };
  29247. utils.inherits(Breakline, UIBase);
  29248. })();
  29249. // ui/message.js
  29250. ///import core
  29251. ///import uicore
  29252. (function() {
  29253. var utils = baidu.editor.utils,
  29254. domUtils = baidu.editor.dom.domUtils,
  29255. UIBase = baidu.editor.ui.UIBase,
  29256. Message = (baidu.editor.ui.Message = function(options) {
  29257. this.initOptions(options);
  29258. this.initMessage();
  29259. });
  29260. Message.prototype = {
  29261. initMessage: function() {
  29262. this.initUIBase();
  29263. },
  29264. getHtmlTpl: function() {
  29265. return (
  29266. '<div id="##" class="edui-message %%">' +
  29267. ' <div id="##_closer" class="edui-message-closer">×</div>' +
  29268. ' <div id="##_body" class="edui-message-body edui-message-type-info">' +
  29269. ' <iframe style="position:absolute;z-index:-1;left:0;top:0;background-color: transparent;" frameborder="0" width="100%" height="100%" src="about:blank"></iframe>' +
  29270. ' <div class="edui-shadow"></div>' +
  29271. ' <div id="##_content" class="edui-message-content">' +
  29272. " </div>" +
  29273. " </div>" +
  29274. "</div>"
  29275. );
  29276. },
  29277. reset: function(opt) {
  29278. var me = this;
  29279. if (!opt.keepshow) {
  29280. clearTimeout(this.timer);
  29281. me.timer = setTimeout(function() {
  29282. me.hide();
  29283. }, opt.timeout || 4000);
  29284. }
  29285. opt.content !== undefined && me.setContent(opt.content);
  29286. opt.type !== undefined && me.setType(opt.type);
  29287. me.show();
  29288. },
  29289. postRender: function() {
  29290. var me = this,
  29291. closer = this.getDom("closer");
  29292. closer &&
  29293. domUtils.on(closer, "click", function() {
  29294. me.hide();
  29295. });
  29296. },
  29297. setContent: function(content) {
  29298. this.getDom("content").innerHTML = content;
  29299. },
  29300. setType: function(type) {
  29301. type = type || "info";
  29302. var body = this.getDom("body");
  29303. body.className = body.className.replace(
  29304. /edui-message-type-[\w-]+/,
  29305. "edui-message-type-" + type
  29306. );
  29307. },
  29308. getContent: function() {
  29309. return this.getDom("content").innerHTML;
  29310. },
  29311. getType: function() {
  29312. var arr = this.getDom("body").match(/edui-message-type-([\w-]+)/);
  29313. return arr ? arr[1] : "";
  29314. },
  29315. show: function() {
  29316. this.getDom().style.display = "block";
  29317. },
  29318. hide: function() {
  29319. var dom = this.getDom();
  29320. if (dom) {
  29321. dom.style.display = "none";
  29322. dom.parentNode && dom.parentNode.removeChild(dom);
  29323. }
  29324. }
  29325. };
  29326. utils.inherits(Message, UIBase);
  29327. })();
  29328. // adapter/editorui.js
  29329. //ui跟编辑器的适配層
  29330. //那个按钮弹出是dialog,是下拉筐等都是在这个js中配置
  29331. //自己写的ui也要在这里配置,放到baidu.editor.ui下边,当编辑器实例化的时候会根据ueditor.config中的toolbars找到相应的进行实例化
  29332. (function() {
  29333. var utils = baidu.editor.utils;
  29334. var editorui = baidu.editor.ui;
  29335. var _Dialog = editorui.Dialog;
  29336. editorui.buttons = {};
  29337. editorui.Dialog = function(options) {
  29338. var dialog = new _Dialog(options);
  29339. dialog.addListener("hide", function() {
  29340. if (dialog.editor) {
  29341. var editor = dialog.editor;
  29342. try {
  29343. if (browser.gecko) {
  29344. var y = editor.window.scrollY,
  29345. x = editor.window.scrollX;
  29346. editor.body.focus();
  29347. editor.window.scrollTo(x, y);
  29348. } else {
  29349. editor.focus();
  29350. }
  29351. } catch (ex) {}
  29352. }
  29353. });
  29354. return dialog;
  29355. };
  29356. var iframeUrlMap = {
  29357. anchor: "~/dialogs/anchor/anchor.html?20220503",
  29358. insertimage: "~/dialogs/image/image.html?20220503",
  29359. link: "~/dialogs/link/link.html?20220503",
  29360. spechars: "~/dialogs/spechars/spechars.html?20220503",
  29361. searchreplace: "~/dialogs/searchreplace/searchreplace.html?20220503",
  29362. insertvideo: "~/dialogs/video/video.html?20220503",
  29363. help: "~/dialogs/help/help.html?20220503",
  29364. preview: "~/dialogs/preview/preview.html?20220503",
  29365. emotion: "~/dialogs/emotion/emotion.html?20220503",
  29366. wordimage: "~/dialogs/wordimage/wordimage.html?20220902",
  29367. formula: "~/dialogs/formula/formula.html?20220902",
  29368. attachment: "~/dialogs/attachment/attachment.html?20220503",
  29369. insertframe: "~/dialogs/insertframe/insertframe.html?20220503",
  29370. edittip: "~/dialogs/table/edittip.html?20220503",
  29371. edittable: "~/dialogs/table/edittable.html?20220503",
  29372. edittd: "~/dialogs/table/edittd.html?20220503",
  29373. scrawl: "~/dialogs/scrawl/scrawl.html?20220503",
  29374. template: "~/dialogs/template/template.html?20220503",
  29375. background: "~/dialogs/background/background.html?20220503",
  29376. };
  29377. //为工具栏添加按钮,以下都是统一的按钮触发命令,所以写在一起
  29378. var btnCmds = [
  29379. "undo",
  29380. "redo",
  29381. "formatmatch",
  29382. "bold",
  29383. "italic",
  29384. "underline",
  29385. "fontborder",
  29386. "touppercase",
  29387. "tolowercase",
  29388. "strikethrough",
  29389. "subscript",
  29390. "superscript",
  29391. "source",
  29392. "indent",
  29393. "outdent",
  29394. "blockquote",
  29395. "pasteplain",
  29396. "pagebreak",
  29397. "selectall",
  29398. "print",
  29399. "horizontal",
  29400. "removeformat",
  29401. "time",
  29402. "date",
  29403. "unlink",
  29404. "insertparagraphbeforetable",
  29405. "insertrow",
  29406. "insertcol",
  29407. "mergeright",
  29408. "mergedown",
  29409. "deleterow",
  29410. "deletecol",
  29411. "splittorows",
  29412. "splittocols",
  29413. "splittocells",
  29414. "mergecells",
  29415. "deletetable",
  29416. ];
  29417. for (var i = 0, ci; (ci = btnCmds[i++]); ) {
  29418. ci = ci.toLowerCase();
  29419. editorui[ci] = (function(cmd) {
  29420. return function(editor) {
  29421. var ui = new editorui.Button({
  29422. className: "edui-for-" + cmd,
  29423. title:
  29424. editor.options.labelMap[cmd] ||
  29425. editor.getLang("labelMap." + cmd) ||
  29426. "",
  29427. onclick: function() {
  29428. editor.execCommand(cmd);
  29429. },
  29430. theme: editor.options.theme,
  29431. showText: false
  29432. });
  29433. editorui.buttons[cmd] = ui;
  29434. editor.addListener("selectionchange", function(
  29435. type,
  29436. causeByUi,
  29437. uiReady
  29438. ) {
  29439. var state = editor.queryCommandState(cmd);
  29440. // console.log('selectionchange',cmd,uiReady,state);
  29441. if (state == -1) {
  29442. ui.setDisabled(true);
  29443. ui.setChecked(false);
  29444. } else {
  29445. if (!uiReady) {
  29446. ui.setDisabled(false);
  29447. ui.setChecked(state);
  29448. }
  29449. }
  29450. });
  29451. return ui;
  29452. };
  29453. })(ci);
  29454. }
  29455. //清除文档
  29456. editorui.cleardoc = function(editor) {
  29457. var ui = new editorui.Button({
  29458. className: "edui-for-cleardoc",
  29459. title:
  29460. editor.options.labelMap.cleardoc ||
  29461. editor.getLang("labelMap.cleardoc") ||
  29462. "",
  29463. theme: editor.options.theme,
  29464. onclick: function() {
  29465. if (confirm(editor.getLang("confirmClear"))) {
  29466. editor.execCommand("cleardoc");
  29467. }
  29468. }
  29469. });
  29470. editorui.buttons["cleardoc"] = ui;
  29471. editor.addListener("selectionchange", function() {
  29472. ui.setDisabled(editor.queryCommandState("cleardoc") == -1);
  29473. });
  29474. return ui;
  29475. };
  29476. //排版,图片排版,文字方向
  29477. var typeset = {
  29478. justify: ["left", "right", "center", "justify"],
  29479. imagefloat: ["none", "left", "center", "right"],
  29480. directionality: ["ltr", "rtl"]
  29481. };
  29482. for (var p in typeset) {
  29483. (function(cmd, val) {
  29484. for (var i = 0, ci; (ci = val[i++]); ) {
  29485. (function(cmd2) {
  29486. editorui[cmd.replace("float", "") + cmd2] = function(editor) {
  29487. var ui = new editorui.Button({
  29488. className: "edui-for-" + cmd.replace("float", "") + cmd2,
  29489. title:
  29490. editor.options.labelMap[cmd.replace("float", "") + cmd2] ||
  29491. editor.getLang(
  29492. "labelMap." + cmd.replace("float", "") + cmd2
  29493. ) ||
  29494. "",
  29495. theme: editor.options.theme,
  29496. onclick: function() {
  29497. editor.execCommand(cmd, cmd2);
  29498. }
  29499. });
  29500. editorui.buttons[cmd] = ui;
  29501. editor.addListener("selectionchange", function(
  29502. type,
  29503. causeByUi,
  29504. uiReady
  29505. ) {
  29506. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  29507. ui.setChecked(editor.queryCommandValue(cmd) == cmd2 && !uiReady);
  29508. });
  29509. return ui;
  29510. };
  29511. })(ci);
  29512. }
  29513. })(p, typeset[p]);
  29514. }
  29515. //字体颜色和背景颜色
  29516. for (var i = 0, ci; (ci = ["backcolor", "forecolor"][i++]); ) {
  29517. editorui[ci] = (function(cmd) {
  29518. return function(editor) {
  29519. var ui = new editorui.ColorButton({
  29520. className: "edui-for-" + cmd,
  29521. color: "default",
  29522. title:
  29523. editor.options.labelMap[cmd] ||
  29524. editor.getLang("labelMap." + cmd) ||
  29525. "",
  29526. editor: editor,
  29527. onpickcolor: function(t, color) {
  29528. editor.execCommand(cmd, color);
  29529. },
  29530. onpicknocolor: function() {
  29531. editor.execCommand(cmd, "default");
  29532. this.setColor("transparent");
  29533. this.color = "default";
  29534. },
  29535. onbuttonclick: function() {
  29536. editor.execCommand(cmd, this.color);
  29537. }
  29538. });
  29539. editorui.buttons[cmd] = ui;
  29540. editor.addListener("selectionchange", function() {
  29541. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  29542. });
  29543. return ui;
  29544. };
  29545. })(ci);
  29546. }
  29547. var dialogBtns = {
  29548. noOk: ["searchreplace", "help", "spechars", "preview"],
  29549. ok: [
  29550. "attachment",
  29551. "anchor",
  29552. "link",
  29553. "insertimage",
  29554. "insertframe",
  29555. "wordimage",
  29556. "insertvideo",
  29557. "insertframe",
  29558. "edittip",
  29559. "edittable",
  29560. "edittd",
  29561. "scrawl",
  29562. "template",
  29563. "formula",
  29564. "background",
  29565. ]
  29566. };
  29567. for (var p in dialogBtns) {
  29568. (function(type, vals) {
  29569. for (var i = 0, ci; (ci = vals[i++]); ) {
  29570. //todo opera下存在问题
  29571. if (browser.opera && ci === "searchreplace") {
  29572. continue;
  29573. }
  29574. (function(cmd) {
  29575. editorui[cmd] = function(editor, iframeUrl, title) {
  29576. iframeUrl =
  29577. iframeUrl ||
  29578. (editor.options.iframeUrlMap || {})[cmd] ||
  29579. iframeUrlMap[cmd];
  29580. title =
  29581. editor.options.labelMap[cmd] ||
  29582. editor.getLang("labelMap." + cmd) ||
  29583. "";
  29584. var dialog;
  29585. //没有iframeUrl不创建dialog
  29586. if (iframeUrl) {
  29587. dialog = new editorui.Dialog(
  29588. utils.extend(
  29589. {
  29590. iframeUrl: editor.ui.mapUrl(iframeUrl),
  29591. editor: editor,
  29592. className: "edui-for-" + cmd,
  29593. title: title,
  29594. holdScroll: cmd === "insertimage",
  29595. fullscreen: /preview/.test(cmd),
  29596. closeDialog: editor.getLang("closeDialog")
  29597. },
  29598. type == "ok"
  29599. ? {
  29600. buttons: [
  29601. {
  29602. className: "edui-okbutton",
  29603. label: editor.getLang("ok"),
  29604. editor: editor,
  29605. onclick: function() {
  29606. dialog.close(true);
  29607. }
  29608. },
  29609. {
  29610. className: "edui-cancelbutton",
  29611. label: editor.getLang("cancel"),
  29612. editor: editor,
  29613. onclick: function() {
  29614. dialog.close(false);
  29615. }
  29616. }
  29617. ]
  29618. }
  29619. : {}
  29620. )
  29621. );
  29622. editor.ui._dialogs[cmd + "Dialog"] = dialog;
  29623. }
  29624. var ui = new editorui.Button({
  29625. className: "edui-for-" + cmd,
  29626. title: title,
  29627. onclick: function() {
  29628. if(editor.options.toolbarCallback){
  29629. if(true === editor.options.toolbarCallback(cmd,editor)){
  29630. return;
  29631. }
  29632. }
  29633. if (dialog) {
  29634. switch (cmd) {
  29635. case "wordimage":
  29636. var images = editor.execCommand("wordimage");
  29637. if (images && images.length) {
  29638. dialog.render();
  29639. dialog.open();
  29640. }
  29641. break;
  29642. case "scrawl":
  29643. if (editor.queryCommandState("scrawl") != -1) {
  29644. dialog.render();
  29645. dialog.open();
  29646. }
  29647. break;
  29648. default:
  29649. dialog.render();
  29650. dialog.open();
  29651. }
  29652. }
  29653. },
  29654. theme: editor.options.theme,
  29655. disabled:
  29656. (cmd == "scrawl" && editor.queryCommandState("scrawl") == -1)
  29657. });
  29658. editorui.buttons[cmd] = ui;
  29659. editor.addListener("selectionchange", function() {
  29660. //只存在于右键菜单而无工具栏按钮的ui不需要检测状态
  29661. var unNeedCheckState = { edittable: 1 };
  29662. if (cmd in unNeedCheckState) return;
  29663. var state = editor.queryCommandState(cmd);
  29664. if (ui.getDom()) {
  29665. ui.setDisabled(state == -1);
  29666. ui.setChecked(state);
  29667. }
  29668. });
  29669. return ui;
  29670. };
  29671. })(ci.toLowerCase());
  29672. }
  29673. })(p, dialogBtns[p]);
  29674. }
  29675. editorui.insertcode = function(editor, list, title) {
  29676. list = editor.options["insertcode"] || [];
  29677. title =
  29678. editor.options.labelMap["insertcode"] ||
  29679. editor.getLang("labelMap.insertcode") ||
  29680. "";
  29681. // if (!list.length) return;
  29682. var items = [];
  29683. utils.each(list, function(key, val) {
  29684. items.push({
  29685. label: key,
  29686. value: val,
  29687. theme: editor.options.theme,
  29688. renderLabelHtml: function() {
  29689. return (
  29690. '<div class="edui-label %%-label" >' + (this.label || "") + "</div>"
  29691. );
  29692. }
  29693. });
  29694. });
  29695. var ui = new editorui.Combox({
  29696. editor: editor,
  29697. items: items,
  29698. onselect: function(t, index) {
  29699. editor.execCommand("insertcode", this.items[index].value);
  29700. },
  29701. onbuttonclick: function() {
  29702. this.showPopup();
  29703. },
  29704. title: title,
  29705. initValue: title,
  29706. className: "edui-for-insertcode",
  29707. indexByValue: function(value) {
  29708. if (value) {
  29709. for (var i = 0, ci; (ci = this.items[i]); i++) {
  29710. if (ci.value.indexOf(value) != -1) return i;
  29711. }
  29712. }
  29713. return -1;
  29714. }
  29715. });
  29716. editorui.buttons["insertcode"] = ui;
  29717. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29718. if (!uiReady) {
  29719. var state = editor.queryCommandState("insertcode");
  29720. if (state == -1) {
  29721. ui.setDisabled(true);
  29722. } else {
  29723. ui.setDisabled(false);
  29724. var value = editor.queryCommandValue("insertcode");
  29725. if (!value) {
  29726. ui.setValue(title);
  29727. return;
  29728. }
  29729. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  29730. value && (value = value.replace(/['"]/g, "").split(",")[0]);
  29731. ui.setValue(value);
  29732. }
  29733. }
  29734. });
  29735. return ui;
  29736. };
  29737. editorui.fontfamily = function(editor, list, title) {
  29738. list = editor.options["fontfamily"] || [];
  29739. title =
  29740. editor.options.labelMap["fontfamily"] ||
  29741. editor.getLang("labelMap.fontfamily") ||
  29742. "";
  29743. if (!list.length) return;
  29744. for (var i = 0, ci, items = []; (ci = list[i]); i++) {
  29745. var langLabel = editor.getLang("fontfamily")[ci.name] || "";
  29746. (function(key, val) {
  29747. items.push({
  29748. label: key,
  29749. value: val,
  29750. theme: editor.options.theme,
  29751. renderLabelHtml: function() {
  29752. return (
  29753. '<div class="edui-label %%-label" style="font-family:' +
  29754. utils.unhtml(this.value) +
  29755. '">' +
  29756. (this.label || "") +
  29757. "</div>"
  29758. );
  29759. }
  29760. });
  29761. })(ci.label || langLabel, ci.val);
  29762. }
  29763. var ui = new editorui.Combox({
  29764. editor: editor,
  29765. items: items,
  29766. onselect: function(t, index) {
  29767. editor.execCommand("FontFamily", this.items[index].value);
  29768. },
  29769. onbuttonclick: function() {
  29770. this.showPopup();
  29771. },
  29772. title: title,
  29773. initValue: title,
  29774. className: "edui-for-fontfamily",
  29775. indexByValue: function(value) {
  29776. if (value) {
  29777. for (var i = 0, ci; (ci = this.items[i]); i++) {
  29778. if (ci.value.indexOf(value) != -1) return i;
  29779. }
  29780. }
  29781. return -1;
  29782. }
  29783. });
  29784. editorui.buttons["fontfamily"] = ui;
  29785. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29786. if (!uiReady) {
  29787. var state = editor.queryCommandState("FontFamily");
  29788. if (state == -1) {
  29789. ui.setDisabled(true);
  29790. } else {
  29791. ui.setDisabled(false);
  29792. var value = editor.queryCommandValue("FontFamily");
  29793. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  29794. value && (value = value.replace(/['"]/g, "").split(",")[0]);
  29795. ui.setValue(value);
  29796. }
  29797. }
  29798. });
  29799. return ui;
  29800. };
  29801. editorui.fontsize = function(editor, list, title) {
  29802. title =
  29803. editor.options.labelMap["fontsize"] ||
  29804. editor.getLang("labelMap.fontsize") ||
  29805. "";
  29806. list = list || editor.options["fontsize"] || [];
  29807. if (!list.length) return;
  29808. var items = [];
  29809. for (var i = 0; i < list.length; i++) {
  29810. var size = list[i] + "px";
  29811. items.push({
  29812. label: size,
  29813. value: size,
  29814. theme: editor.options.theme,
  29815. renderLabelHtml: function() {
  29816. return (
  29817. '<div class="edui-label %%-label" style="line-height:1;font-size:' +
  29818. this.value +
  29819. '">' +
  29820. (this.label || "") +
  29821. "</div>"
  29822. );
  29823. }
  29824. });
  29825. }
  29826. var ui = new editorui.Combox({
  29827. editor: editor,
  29828. items: items,
  29829. title: title,
  29830. initValue: title,
  29831. onselect: function(t, index) {
  29832. editor.execCommand("FontSize", this.items[index].value);
  29833. },
  29834. onbuttonclick: function() {
  29835. this.showPopup();
  29836. },
  29837. className: "edui-for-fontsize"
  29838. });
  29839. editorui.buttons["fontsize"] = ui;
  29840. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29841. if (!uiReady) {
  29842. var state = editor.queryCommandState("FontSize");
  29843. if (state == -1) {
  29844. ui.setDisabled(true);
  29845. } else {
  29846. ui.setDisabled(false);
  29847. ui.setValue(editor.queryCommandValue("FontSize"));
  29848. }
  29849. }
  29850. });
  29851. return ui;
  29852. };
  29853. editorui.paragraph = function(editor, list, title) {
  29854. title =
  29855. editor.options.labelMap["paragraph"] ||
  29856. editor.getLang("labelMap.paragraph") ||
  29857. "";
  29858. list = editor.options["paragraph"] || [];
  29859. if (utils.isEmptyObject(list)) return;
  29860. var items = [];
  29861. for (var i in list) {
  29862. items.push({
  29863. value: i,
  29864. label: list[i] || editor.getLang("paragraph")[i],
  29865. theme: editor.options.theme,
  29866. renderLabelHtml: function() {
  29867. return (
  29868. '<div class="edui-label %%-label"><span class="edui-for-' +
  29869. this.value +
  29870. '">' +
  29871. (this.label || "") +
  29872. "</span></div>"
  29873. );
  29874. }
  29875. });
  29876. }
  29877. var ui = new editorui.Combox({
  29878. editor: editor,
  29879. items: items,
  29880. title: title,
  29881. initValue: title,
  29882. className: "edui-for-paragraph",
  29883. onselect: function(t, index) {
  29884. editor.execCommand("Paragraph", this.items[index].value);
  29885. },
  29886. onbuttonclick: function() {
  29887. this.showPopup();
  29888. }
  29889. });
  29890. editorui.buttons["paragraph"] = ui;
  29891. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29892. if (!uiReady) {
  29893. var state = editor.queryCommandState("Paragraph");
  29894. if (state == -1) {
  29895. ui.setDisabled(true);
  29896. } else {
  29897. ui.setDisabled(false);
  29898. var value = editor.queryCommandValue("Paragraph");
  29899. var index = ui.indexByValue(value);
  29900. if (index != -1) {
  29901. ui.setValue(value);
  29902. } else {
  29903. ui.setValue(ui.initValue);
  29904. }
  29905. }
  29906. }
  29907. });
  29908. return ui;
  29909. };
  29910. //自定义标题
  29911. editorui.customstyle = function(editor) {
  29912. var list = editor.options["customstyle"] || [],
  29913. title =
  29914. editor.options.labelMap["customstyle"] ||
  29915. editor.getLang("labelMap.customstyle") ||
  29916. "";
  29917. if (!list.length) return;
  29918. var langCs = editor.getLang("customstyle");
  29919. for (var i = 0, items = [], t; (t = list[i++]); ) {
  29920. (function(t) {
  29921. var ck = {};
  29922. ck.label = t.label ? t.label : langCs[t.name];
  29923. ck.style = t.style;
  29924. ck.className = t.className;
  29925. ck.tag = t.tag;
  29926. items.push({
  29927. label: ck.label,
  29928. value: ck,
  29929. theme: editor.options.theme,
  29930. renderLabelHtml: function() {
  29931. return (
  29932. '<div class="edui-label %%-label">' +
  29933. "<" +
  29934. ck.tag +
  29935. " " +
  29936. (ck.className ? ' class="' + ck.className + '"' : "") +
  29937. (ck.style ? ' style="' + ck.style + '"' : "") +
  29938. ">" +
  29939. ck.label +
  29940. "</" +
  29941. ck.tag +
  29942. ">" +
  29943. "</div>"
  29944. );
  29945. }
  29946. });
  29947. })(t);
  29948. }
  29949. var ui = new editorui.Combox({
  29950. editor: editor,
  29951. items: items,
  29952. title: title,
  29953. initValue: title,
  29954. className: "edui-for-customstyle",
  29955. onselect: function(t, index) {
  29956. editor.execCommand("customstyle", this.items[index].value);
  29957. },
  29958. onbuttonclick: function() {
  29959. this.showPopup();
  29960. },
  29961. indexByValue: function(value) {
  29962. for (var i = 0, ti; (ti = this.items[i++]); ) {
  29963. if (ti.label == value) {
  29964. return i - 1;
  29965. }
  29966. }
  29967. return -1;
  29968. }
  29969. });
  29970. editorui.buttons["customstyle"] = ui;
  29971. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  29972. if (!uiReady) {
  29973. var state = editor.queryCommandState("customstyle");
  29974. if (state == -1) {
  29975. ui.setDisabled(true);
  29976. } else {
  29977. ui.setDisabled(false);
  29978. var value = editor.queryCommandValue("customstyle");
  29979. var index = ui.indexByValue(value);
  29980. if (index != -1) {
  29981. ui.setValue(value);
  29982. } else {
  29983. ui.setValue(ui.initValue);
  29984. }
  29985. }
  29986. }
  29987. });
  29988. return ui;
  29989. };
  29990. editorui.inserttable = function(editor, iframeUrl, title) {
  29991. title =
  29992. editor.options.labelMap["inserttable"] ||
  29993. editor.getLang("labelMap.inserttable") ||
  29994. "";
  29995. var ui = new editorui.TableButton({
  29996. editor: editor,
  29997. title: title,
  29998. className: "edui-for-inserttable",
  29999. onpicktable: function(t, numCols, numRows) {
  30000. editor.execCommand("InsertTable", {
  30001. numRows: numRows,
  30002. numCols: numCols,
  30003. border: 1
  30004. });
  30005. },
  30006. onbuttonclick: function() {
  30007. this.showPopup();
  30008. }
  30009. });
  30010. editorui.buttons["inserttable"] = ui;
  30011. editor.addListener("selectionchange", function() {
  30012. ui.setDisabled(editor.queryCommandState("inserttable") == -1);
  30013. });
  30014. return ui;
  30015. };
  30016. editorui.lineheight = function(editor) {
  30017. var val = editor.options.lineheight || [];
  30018. if (!val.length) return;
  30019. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  30020. items.push({
  30021. //todo:写死了
  30022. label: ci,
  30023. value: ci,
  30024. theme: editor.options.theme,
  30025. onclick: function() {
  30026. editor.execCommand("lineheight", this.value);
  30027. }
  30028. });
  30029. }
  30030. var ui = new editorui.MenuButton({
  30031. editor: editor,
  30032. className: "edui-for-lineheight",
  30033. title:
  30034. editor.options.labelMap["lineheight"] ||
  30035. editor.getLang("labelMap.lineheight") ||
  30036. "",
  30037. items: items,
  30038. onbuttonclick: function() {
  30039. var value = editor.queryCommandValue("LineHeight") || this.value;
  30040. editor.execCommand("LineHeight", value);
  30041. }
  30042. });
  30043. editorui.buttons["lineheight"] = ui;
  30044. editor.addListener("selectionchange", function() {
  30045. var state = editor.queryCommandState("LineHeight");
  30046. if (state == -1) {
  30047. ui.setDisabled(true);
  30048. } else {
  30049. ui.setDisabled(false);
  30050. var value = editor.queryCommandValue("LineHeight");
  30051. value && ui.setValue((value + "").replace(/cm/, ""));
  30052. ui.setChecked(state);
  30053. }
  30054. });
  30055. return ui;
  30056. };
  30057. var rowspacings = ["top", "bottom"];
  30058. for (var r = 0, ri; (ri = rowspacings[r++]); ) {
  30059. (function(cmd) {
  30060. editorui["rowspacing" + cmd] = function(editor) {
  30061. var val = editor.options["rowspacing" + cmd] || [];
  30062. if (!val.length) return null;
  30063. for (var i = 0, ci, items = []; (ci = val[i++]); ) {
  30064. items.push({
  30065. label: ci,
  30066. value: ci,
  30067. theme: editor.options.theme,
  30068. onclick: function() {
  30069. editor.execCommand("rowspacing", this.value, cmd);
  30070. }
  30071. });
  30072. }
  30073. var ui = new editorui.MenuButton({
  30074. editor: editor,
  30075. className: "edui-for-rowspacing" + cmd,
  30076. title:
  30077. editor.options.labelMap["rowspacing" + cmd] ||
  30078. editor.getLang("labelMap.rowspacing" + cmd) ||
  30079. "",
  30080. items: items,
  30081. onbuttonclick: function() {
  30082. var value =
  30083. editor.queryCommandValue("rowspacing", cmd) || this.value;
  30084. editor.execCommand("rowspacing", value, cmd);
  30085. }
  30086. });
  30087. editorui.buttons[cmd] = ui;
  30088. editor.addListener("selectionchange", function() {
  30089. var state = editor.queryCommandState("rowspacing", cmd);
  30090. if (state == -1) {
  30091. ui.setDisabled(true);
  30092. } else {
  30093. ui.setDisabled(false);
  30094. var value = editor.queryCommandValue("rowspacing", cmd);
  30095. value && ui.setValue((value + "").replace(/%/, ""));
  30096. ui.setChecked(state);
  30097. }
  30098. });
  30099. return ui;
  30100. };
  30101. })(ri);
  30102. }
  30103. //有序,无序列表
  30104. var lists = ["insertorderedlist", "insertunorderedlist"];
  30105. for (var l = 0, cl; (cl = lists[l++]); ) {
  30106. (function(cmd) {
  30107. editorui[cmd] = function(editor) {
  30108. var vals = editor.options[cmd],
  30109. _onMenuClick = function() {
  30110. editor.execCommand(cmd, this.value);
  30111. },
  30112. items = [];
  30113. for (var i in vals) {
  30114. items.push({
  30115. label: vals[i] || editor.getLang()[cmd][i] || "",
  30116. value: i,
  30117. theme: editor.options.theme,
  30118. onclick: _onMenuClick
  30119. });
  30120. }
  30121. var ui = new editorui.MenuButton({
  30122. editor: editor,
  30123. className: "edui-for-" + cmd,
  30124. title: editor.getLang("labelMap." + cmd) || "",
  30125. items: items,
  30126. onbuttonclick: function() {
  30127. var value = editor.queryCommandValue(cmd) || this.value;
  30128. editor.execCommand(cmd, value);
  30129. }
  30130. });
  30131. editorui.buttons[cmd] = ui;
  30132. editor.addListener("selectionchange", function() {
  30133. var state = editor.queryCommandState(cmd);
  30134. if (state == -1) {
  30135. ui.setDisabled(true);
  30136. } else {
  30137. ui.setDisabled(false);
  30138. var value = editor.queryCommandValue(cmd);
  30139. ui.setValue(value);
  30140. ui.setChecked(state);
  30141. }
  30142. });
  30143. return ui;
  30144. };
  30145. })(cl);
  30146. }
  30147. editorui.fullscreen = function(editor, title) {
  30148. title =
  30149. editor.options.labelMap["fullscreen"] ||
  30150. editor.getLang("labelMap.fullscreen") ||
  30151. "";
  30152. var ui = new editorui.Button({
  30153. className: "edui-for-fullscreen",
  30154. title: title,
  30155. theme: editor.options.theme,
  30156. onclick: function() {
  30157. if (editor.ui) {
  30158. editor.ui.setFullScreen(!editor.ui.isFullScreen());
  30159. }
  30160. this.setChecked(editor.ui.isFullScreen());
  30161. }
  30162. });
  30163. editorui.buttons["fullscreen"] = ui;
  30164. editor.addListener("selectionchange", function() {
  30165. var state = editor.queryCommandState("fullscreen");
  30166. ui.setDisabled(state == -1);
  30167. ui.setChecked(editor.ui.isFullScreen());
  30168. });
  30169. return ui;
  30170. };
  30171. // 表情
  30172. editorui["emotion"] = function(editor, iframeUrl) {
  30173. var cmd = "emotion";
  30174. var ui = new editorui.MultiMenuPop({
  30175. title:
  30176. editor.options.labelMap[cmd] ||
  30177. editor.getLang("labelMap." + cmd + "") ||
  30178. "",
  30179. editor: editor,
  30180. className: "edui-for-" + cmd,
  30181. iframeUrl: editor.ui.mapUrl(
  30182. iframeUrl ||
  30183. (editor.options.iframeUrlMap || {})[cmd] ||
  30184. iframeUrlMap[cmd]
  30185. )
  30186. });
  30187. editorui.buttons[cmd] = ui;
  30188. editor.addListener("selectionchange", function() {
  30189. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  30190. });
  30191. return ui;
  30192. };
  30193. editorui.autotypeset = function(editor) {
  30194. var ui = new editorui.AutoTypeSetButton({
  30195. editor: editor,
  30196. title:
  30197. editor.options.labelMap["autotypeset"] ||
  30198. editor.getLang("labelMap.autotypeset") ||
  30199. "",
  30200. className: "edui-for-autotypeset",
  30201. onbuttonclick: function() {
  30202. editor.execCommand("autotypeset");
  30203. }
  30204. });
  30205. editorui.buttons["autotypeset"] = ui;
  30206. editor.addListener("selectionchange", function() {
  30207. ui.setDisabled(editor.queryCommandState("autotypeset") == -1);
  30208. });
  30209. return ui;
  30210. };
  30211. /* 简单上传插件 */
  30212. editorui["simpleupload"] = function(editor) {
  30213. var name = "simpleupload",
  30214. ui = new editorui.Button({
  30215. className: "edui-for-" + name,
  30216. title:
  30217. editor.options.labelMap[name] ||
  30218. editor.getLang("labelMap." + name) ||
  30219. "",
  30220. onclick: function() {},
  30221. theme: editor.options.theme,
  30222. showText: false
  30223. });
  30224. editorui.buttons[name] = ui;
  30225. editor.addListener("ready", function() {
  30226. var b = ui.getDom("body"),
  30227. iconSpan = b.children[0];
  30228. editor.fireEvent("simpleuploadbtnready", iconSpan);
  30229. });
  30230. editor.addListener("selectionchange", function(type, causeByUi, uiReady) {
  30231. var state = editor.queryCommandState(name);
  30232. if (state == -1) {
  30233. ui.setDisabled(true);
  30234. ui.setChecked(false);
  30235. } else {
  30236. if (!uiReady) {
  30237. ui.setDisabled(false);
  30238. ui.setChecked(state);
  30239. }
  30240. }
  30241. });
  30242. return ui;
  30243. };
  30244. })();
  30245. // adapter/editor.js
  30246. ///import core
  30247. ///commands 全屏
  30248. ///commandsName FullScreen
  30249. ///commandsTitle 全屏
  30250. (function () {
  30251. var utils = baidu.editor.utils,
  30252. uiUtils = baidu.editor.ui.uiUtils,
  30253. UIBase = baidu.editor.ui.UIBase,
  30254. domUtils = baidu.editor.dom.domUtils;
  30255. var nodeStack = [];
  30256. function EditorUI(options) {
  30257. this.initOptions(options);
  30258. this.initEditorUI();
  30259. }
  30260. EditorUI.prototype = {
  30261. uiName: "editor",
  30262. initEditorUI: function () {
  30263. this.editor.ui = this;
  30264. this._dialogs = {};
  30265. this.initUIBase();
  30266. this._initToolbars();
  30267. var editor = this.editor,
  30268. me = this;
  30269. editor.addListener("ready", function () {
  30270. //提供getDialog方法
  30271. editor.getDialog = function (name) {
  30272. return editor.ui._dialogs[name + "Dialog"];
  30273. };
  30274. domUtils.on(editor.window, "scroll", function (evt) {
  30275. baidu.editor.ui.Popup.postHide(evt);
  30276. });
  30277. //提供编辑器实时宽高(全屏时宽高不变化)
  30278. editor.ui._actualFrameWidth = editor.options.initialFrameWidth;
  30279. UE.browser.ie &&
  30280. UE.browser.version === 6 &&
  30281. editor.container.ownerDocument.execCommand(
  30282. "BackgroundImageCache",
  30283. false,
  30284. true
  30285. );
  30286. //display bottom-bar label based on config
  30287. if (editor.options.elementPathEnabled) {
  30288. editor.ui.getDom("elementpath").innerHTML =
  30289. '<div class="edui-editor-breadcrumb">' +
  30290. editor.getLang("elementPathTip") +
  30291. ":</div>";
  30292. }
  30293. if (editor.options.wordCount) {
  30294. function countFn() {
  30295. setCount(editor, me);
  30296. domUtils.un(editor.document, "click", arguments.callee);
  30297. }
  30298. domUtils.on(editor.document, "click", countFn);
  30299. editor.ui.getDom("wordcount").innerHTML = editor.getLang(
  30300. "wordCountTip"
  30301. );
  30302. }
  30303. editor.ui._scale();
  30304. if (editor.options.scaleEnabled) {
  30305. if (editor.autoHeightEnabled) {
  30306. editor.disableAutoHeight();
  30307. }
  30308. me.enableScale();
  30309. } else {
  30310. me.disableScale();
  30311. }
  30312. if (
  30313. !editor.options.elementPathEnabled &&
  30314. !editor.options.wordCount &&
  30315. !editor.options.scaleEnabled
  30316. ) {
  30317. editor.ui.getDom("elementpath").style.display = "none";
  30318. editor.ui.getDom("wordcount").style.display = "none";
  30319. editor.ui.getDom("scale").style.display = "none";
  30320. }
  30321. if (!editor.selection.isFocus()) return;
  30322. editor.fireEvent("selectionchange", false, true);
  30323. });
  30324. editor.addListener("mousedown", function (t, evt) {
  30325. var el = evt.target || evt.srcElement;
  30326. baidu.editor.ui.Popup.postHide(evt, el);
  30327. baidu.editor.ui.ShortCutMenu.postHide(evt);
  30328. });
  30329. editor.addListener("delcells", function () {
  30330. if (UE.ui["edittip"]) {
  30331. new UE.ui["edittip"](editor);
  30332. }
  30333. editor.getDialog("edittip").open();
  30334. });
  30335. var pastePop,
  30336. isPaste = false,
  30337. timer;
  30338. editor.addListener("afterpaste", function () {
  30339. if (editor.queryCommandState("pasteplain")) return;
  30340. if (baidu.editor.ui.PastePicker) {
  30341. pastePop = new baidu.editor.ui.Popup({
  30342. content: new baidu.editor.ui.PastePicker({editor: editor}),
  30343. editor: editor,
  30344. className: "edui-wordpastepop"
  30345. });
  30346. pastePop.render();
  30347. }
  30348. isPaste = true;
  30349. });
  30350. editor.addListener("afterinserthtml", function () {
  30351. clearTimeout(timer);
  30352. timer = setTimeout(function () {
  30353. if (pastePop && (isPaste || editor.ui._isTransfer)) {
  30354. if (pastePop.isHidden()) {
  30355. var span = domUtils.createElement(editor.document, "span", {
  30356. style: "line-height:0px;",
  30357. innerHTML: "\ufeff"
  30358. }),
  30359. range = editor.selection.getRange();
  30360. range.insertNode(span);
  30361. var tmp = getDomNode(span, "firstChild", "previousSibling");
  30362. tmp &&
  30363. pastePop.showAnchor(tmp.nodeType == 3 ? tmp.parentNode : tmp);
  30364. domUtils.remove(span);
  30365. } else {
  30366. pastePop.show();
  30367. }
  30368. delete editor.ui._isTransfer;
  30369. isPaste = false;
  30370. }
  30371. }, 200);
  30372. });
  30373. editor.addListener("contextmenu", function (t, evt) {
  30374. baidu.editor.ui.Popup.postHide(evt);
  30375. });
  30376. editor.addListener("keydown", function (t, evt) {
  30377. if (pastePop) pastePop.dispose(evt);
  30378. var keyCode = evt.keyCode || evt.which;
  30379. if (evt.altKey && keyCode == 90) {
  30380. UE.ui.buttons["fullscreen"].onclick();
  30381. }
  30382. });
  30383. editor.addListener("wordcount", function (type) {
  30384. setCount(this, me);
  30385. });
  30386. function setCount(editor, ui) {
  30387. editor.setOpt({
  30388. wordCount: true,
  30389. maximumWords: 10000,
  30390. wordCountMsg:
  30391. editor.options.wordCountMsg || editor.getLang("wordCountMsg"),
  30392. wordOverFlowMsg:
  30393. editor.options.wordOverFlowMsg || editor.getLang("wordOverFlowMsg")
  30394. });
  30395. var opt = editor.options,
  30396. max = opt.maximumWords,
  30397. msg = opt.wordCountMsg,
  30398. errMsg = opt.wordOverFlowMsg,
  30399. countDom = ui.getDom("wordcount");
  30400. if (!opt.wordCount) {
  30401. return;
  30402. }
  30403. var count = editor.getContentLength(true);
  30404. if (count > max) {
  30405. countDom.innerHTML = errMsg;
  30406. editor.fireEvent("wordcountoverflow");
  30407. } else {
  30408. countDom.innerHTML = msg
  30409. .replace("{#leave}", max - count)
  30410. .replace("{#count}", count);
  30411. }
  30412. }
  30413. editor.addListener("selectionchange", function () {
  30414. if (editor.options.elementPathEnabled) {
  30415. me[
  30416. (editor.queryCommandState("elementpath") == -1 ? "dis" : "en") +
  30417. "ableElementPath"
  30418. ]();
  30419. }
  30420. if (editor.options.scaleEnabled) {
  30421. me[
  30422. (editor.queryCommandState("scale") == -1 ? "dis" : "en") +
  30423. "ableScale"
  30424. ]();
  30425. }
  30426. });
  30427. var popup = new baidu.editor.ui.Popup({
  30428. editor: editor,
  30429. content: "",
  30430. className: "edui-bubble",
  30431. _onEditButtonClick: function () {
  30432. this.hide();
  30433. editor.ui._dialogs.linkDialog.open();
  30434. },
  30435. _onImgEditButtonClick: function (name) {
  30436. this.hide();
  30437. editor.ui._dialogs[name] && editor.ui._dialogs[name].open();
  30438. },
  30439. _onImgSetFloat: function (value) {
  30440. this.hide();
  30441. editor.execCommand("imagefloat", value);
  30442. },
  30443. _setIframeAlign: function (value) {
  30444. var frame = popup.anchorEl;
  30445. var newFrame = frame.cloneNode(true);
  30446. switch (value) {
  30447. case -2:
  30448. newFrame.setAttribute("align", "");
  30449. break;
  30450. case -1:
  30451. newFrame.setAttribute("align", "left");
  30452. break;
  30453. case 1:
  30454. newFrame.setAttribute("align", "right");
  30455. break;
  30456. }
  30457. frame.parentNode.insertBefore(newFrame, frame);
  30458. domUtils.remove(frame);
  30459. popup.anchorEl = newFrame;
  30460. popup.showAnchor(popup.anchorEl);
  30461. },
  30462. _updateIframe: function () {
  30463. var frame = (editor._iframe = popup.anchorEl);
  30464. if (domUtils.hasClass(frame, "ueditor_baidumap")) {
  30465. editor.selection.getRange().selectNode(frame).select();
  30466. editor.ui._dialogs.mapDialog.open();
  30467. popup.hide();
  30468. } else {
  30469. editor.ui._dialogs.insertframeDialog.open();
  30470. popup.hide();
  30471. }
  30472. },
  30473. _onRemoveButtonClick: function (cmdName) {
  30474. editor.execCommand(cmdName);
  30475. this.hide();
  30476. },
  30477. queryAutoHide: function (el) {
  30478. if (el && el.ownerDocument == editor.document) {
  30479. if (
  30480. el.tagName.toLowerCase() == "img" ||
  30481. domUtils.findParentByTagName(el, "a", true)
  30482. ) {
  30483. return el !== popup.anchorEl;
  30484. }
  30485. }
  30486. return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el);
  30487. }
  30488. });
  30489. popup.render();
  30490. if (editor.options.imagePopup) {
  30491. editor.addListener("mouseover", function (t, evt) {
  30492. evt = evt || window.event;
  30493. var el = evt.target || evt.srcElement;
  30494. if (
  30495. editor.ui._dialogs.insertframeDialog &&
  30496. /iframe/gi.test(el.tagName)
  30497. ) {
  30498. var html = popup.formatHtml(
  30499. "<nobr>" +
  30500. '<span onclick=$$._setIframeAlign(-2) class="edui-clickable">' +
  30501. editor.getLang("default") +
  30502. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(-1) class="edui-clickable">' +
  30503. editor.getLang("justifyleft") +
  30504. '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(1) class="edui-clickable">' +
  30505. editor.getLang("justifyright") +
  30506. "</span>&nbsp;&nbsp;" +
  30507. ' <span onclick="$$._updateIframe( this);" class="edui-clickable">' +
  30508. editor.getLang("modify") +
  30509. "</span></nobr>"
  30510. );
  30511. if (html) {
  30512. popup.getDom("content").innerHTML = html;
  30513. popup.anchorEl = el;
  30514. popup.showAnchor(popup.anchorEl);
  30515. } else {
  30516. popup.hide();
  30517. }
  30518. }
  30519. });
  30520. editor.addListener("selectionchange", function (t, causeByUi) {
  30521. if (!causeByUi) return;
  30522. var html = "",
  30523. str = "",
  30524. img = editor.selection.getRange().getClosedNode(),
  30525. dialogs = editor.ui._dialogs;
  30526. if (img && img.tagName == "IMG") {
  30527. var dialogName = "insertimageDialog";
  30528. if (
  30529. img.className.indexOf("edui-faked-video") != -1 ||
  30530. img.className.indexOf("edui-upload-video") != -1
  30531. ) {
  30532. dialogName = "insertvideoDialog";
  30533. }
  30534. if (img.getAttribute("anchorname")) {
  30535. dialogName = "anchorDialog";
  30536. html = popup.formatHtml(
  30537. "<nobr>" +
  30538. '<span onclick=$$._onImgEditButtonClick("anchorDialog") class="edui-clickable">' +
  30539. editor.getLang("modify") +
  30540. "</span>&nbsp;&nbsp;" +
  30541. "<span onclick=$$._onRemoveButtonClick('anchor') class=\"edui-clickable\">" +
  30542. editor.getLang("delete") +
  30543. "</span></nobr>"
  30544. );
  30545. }
  30546. // if (img.getAttribute("data-word-image")) {
  30547. // //todo 放到dialog去做查询
  30548. // editor['data-word-image'] = [img.getAttribute("data-word-image")];
  30549. // dialogName = "wordimageDialog";
  30550. // }
  30551. if (
  30552. domUtils.hasClass(img, "loadingclass") ||
  30553. domUtils.hasClass(img, "loaderrorclass")
  30554. ) {
  30555. dialogName = "";
  30556. }
  30557. if (!dialogs[dialogName]) {
  30558. return;
  30559. }
  30560. var actions = [];
  30561. actions.push('<nobr />');
  30562. actions.push('<span onclick=$$._onImgSetFloat("none") class="edui-clickable edui-popup-action-item">' +
  30563. editor.getLang("default") +
  30564. "</span>");
  30565. actions.push('<span onclick=$$._onImgSetFloat("left") class="edui-clickable edui-popup-action-item">' +
  30566. editor.getLang("justifyleft") +
  30567. "</span>");
  30568. actions.push('<span onclick=$$._onImgSetFloat("right") class="edui-clickable edui-popup-action-item">' +
  30569. editor.getLang("justifyright") +
  30570. "</span>");
  30571. actions.push('<span onclick=$$._onImgSetFloat("center") class="edui-clickable edui-popup-action-item">' +
  30572. editor.getLang("justifycenter") +
  30573. "</span>");
  30574. if (img.getAttribute('data-formula-image') !== null) {
  30575. actions.push("<span onclick=\"$$._onImgEditButtonClick('formulaDialog');\" class='edui-clickable edui-popup-action-item'>" +
  30576. editor.getLang("formulaedit") + "</span>");
  30577. }
  30578. if (img.getAttribute("data-word-image")) {
  30579. actions.push("<span onclick=\"$$._onImgEditButtonClick('wordimageDialog');\" class='edui-clickable edui-popup-action-item'>" +
  30580. editor.getLang("save") +
  30581. "</span>");
  30582. } else {
  30583. actions.push("<span onclick=\"$$._onImgEditButtonClick('" + dialogName + '\');" class="edui-clickable edui-popup-action-item">' +
  30584. editor.getLang("modify") +
  30585. "</span>");
  30586. }
  30587. actions.push("</nobr>");
  30588. !html && (html = popup.formatHtml(actions.join("")));
  30589. }
  30590. if (editor.ui._dialogs.linkDialog) {
  30591. var link = editor.queryCommandValue("link");
  30592. var url;
  30593. if (
  30594. link &&
  30595. (url = link.getAttribute("_href") || link.getAttribute("href", 2))
  30596. ) {
  30597. var txt = url;
  30598. if (url.length > 30) {
  30599. txt = url.substring(0, 20) + "...";
  30600. }
  30601. if (html) {
  30602. html += '<div style="height:5px;"></div>';
  30603. }
  30604. html += popup.formatHtml(
  30605. "<nobr>" +
  30606. editor.getLang("anchorMsg") +
  30607. ': <a target="_blank" href="' +
  30608. url +
  30609. '" title="' +
  30610. url +
  30611. '" >' +
  30612. txt +
  30613. "</a>" +
  30614. ' <span class="edui-clickable" onclick="$$._onEditButtonClick();">' +
  30615. editor.getLang("modify") +
  30616. "</span>" +
  30617. ' <span class="edui-clickable" onclick="$$._onRemoveButtonClick(\'unlink\');"> ' +
  30618. editor.getLang("clear") +
  30619. "</span></nobr>"
  30620. );
  30621. popup.showAnchor(link);
  30622. }
  30623. }
  30624. if (html) {
  30625. popup.getDom("content").innerHTML = html;
  30626. popup.anchorEl = img || link;
  30627. popup.showAnchor(popup.anchorEl);
  30628. } else {
  30629. popup.hide();
  30630. }
  30631. });
  30632. }
  30633. },
  30634. _initToolbars: function () {
  30635. var editor = this.editor;
  30636. var toolbars = this.toolbars || [];
  30637. if(toolbars[0]){
  30638. toolbars[0].unshift(
  30639. 'message'
  30640. );
  30641. }
  30642. var toolbarUis = [];
  30643. var extraUIs = [];
  30644. for (var i = 0; i < toolbars.length; i++) {
  30645. var toolbar = toolbars[i];
  30646. var toolbarUi = new baidu.editor.ui.Toolbar({
  30647. theme: editor.options.theme
  30648. });
  30649. for (var j = 0; j < toolbar.length; j++) {
  30650. var toolbarItem = toolbar[j];
  30651. var toolbarItemUi = null;
  30652. if (typeof toolbarItem == "string") {
  30653. toolbarItem = toolbarItem.toLowerCase();
  30654. if (toolbarItem === "|") {
  30655. toolbarItem = "Separator";
  30656. }
  30657. if (toolbarItem === "||") {
  30658. toolbarItem = "Breakline";
  30659. }
  30660. var ui = baidu.editor.ui[toolbarItem];
  30661. if (ui) {
  30662. if (utils.isFunction(ui)) {
  30663. toolbarItemUi = new baidu.editor.ui[toolbarItem](editor);
  30664. } else {
  30665. if (ui.id && ui.id !== editor.key) {
  30666. continue;
  30667. }
  30668. var itemUI = ui.execFn.call(editor, editor, toolbarItem);
  30669. if (itemUI) {
  30670. if (ui.index === undefined) {
  30671. toolbarUi.add(itemUI);
  30672. continue;
  30673. } else {
  30674. extraUIs.push({
  30675. index: ui.index,
  30676. itemUI: itemUI
  30677. });
  30678. }
  30679. }
  30680. }
  30681. }
  30682. //fullscreen这里单独处理一下,放到首行去
  30683. if (toolbarItem === "fullscreen") {
  30684. if (toolbarUis && toolbarUis[0]) {
  30685. toolbarUis[0].items.splice(0, 0, toolbarItemUi);
  30686. } else {
  30687. toolbarItemUi && toolbarUi.items.splice(0, 0, toolbarItemUi);
  30688. }
  30689. continue;
  30690. }
  30691. } else {
  30692. toolbarItemUi = toolbarItem;
  30693. }
  30694. if (toolbarItemUi && toolbarItemUi.id) {
  30695. toolbarUi.add(toolbarItemUi);
  30696. }
  30697. }
  30698. toolbarUis[i] = toolbarUi;
  30699. }
  30700. //接受外部定制的UI
  30701. utils.each(extraUIs, function (obj) {
  30702. toolbarUi.add(obj.itemUI, obj.index);
  30703. });
  30704. this.toolbars = toolbarUis;
  30705. },
  30706. getHtmlTpl: function () {
  30707. return (
  30708. '<div id="##" class="%%">' +
  30709. '<div id="##_toolbarbox" class="%%-toolbarbox">' +
  30710. (this.toolbars.length
  30711. ? '<div id="##_toolbarboxouter" class="%%-toolbarboxouter"><div class="%%-toolbarboxinner">' +
  30712. this.renderToolbarBoxHtml() +
  30713. "</div></div>"
  30714. : "") +
  30715. '<div id="##_toolbarmsg" class="%%-toolbarmsg" style="display:none;">' +
  30716. '<div id = "##_upload_dialog" class="%%-toolbarmsg-upload" onclick="$$.showWordImageDialog();">' +
  30717. this.editor.getLang("clickToUpload") +
  30718. "</div>" +
  30719. '<div class="%%-toolbarmsg-close" onclick="$$.hideToolbarMsg();">x</div>' +
  30720. '<div id="##_toolbarmsg_label" class="%%-toolbarmsg-label"></div>' +
  30721. '<div style="height:0;overflow:hidden;clear:both;"></div>' +
  30722. "</div>" +
  30723. '<div id="##_message_holder" class="%%-messageholder"></div>' +
  30724. "</div>" +
  30725. '<div id="##_iframeholder" class="%%-iframeholder">' +
  30726. "</div>" +
  30727. //modify wdcount by matao
  30728. '<div id="##_bottombar" class="%%-bottomContainer"><table><tr>' +
  30729. '<td id="##_elementpath" class="%%-bottombar"></td>' +
  30730. '<td id="##_wordcount" class="%%-wordcount"></td>' +
  30731. '<td id="##_scale" class="%%-scale"><div class="%%-icon"></div></td>' +
  30732. "</tr></table></div>" +
  30733. '<div id="##_scalelayer"></div>' +
  30734. "</div>"
  30735. );
  30736. },
  30737. showWordImageDialog: function () {
  30738. this._dialogs["wordimageDialog"].open();
  30739. },
  30740. renderToolbarBoxHtml: function () {
  30741. var buff = [];
  30742. for (var i = 0; i < this.toolbars.length; i++) {
  30743. buff.push(this.toolbars[i].renderHtml());
  30744. }
  30745. return buff.join("");
  30746. },
  30747. setFullScreen: function (fullscreen) {
  30748. var editor = this.editor,
  30749. container = editor.container.parentNode.parentNode;
  30750. if (this._fullscreen != fullscreen) {
  30751. this._fullscreen = fullscreen;
  30752. this.editor.fireEvent("beforefullscreenchange", fullscreen);
  30753. if (baidu.editor.browser.gecko) {
  30754. var bk = editor.selection.getRange().createBookmark();
  30755. }
  30756. if (fullscreen) {
  30757. while (container.tagName != "BODY") {
  30758. var position = baidu.editor.dom.domUtils.getComputedStyle(
  30759. container,
  30760. "position"
  30761. );
  30762. nodeStack.push(position);
  30763. container.style.position = "static";
  30764. container = container.parentNode;
  30765. }
  30766. this._bakHtmlOverflow = document.documentElement.style.overflow;
  30767. this._bakBodyOverflow = document.body.style.overflow;
  30768. this._bakAutoHeight = this.editor.autoHeightEnabled;
  30769. this._bakScrollTop = Math.max(
  30770. document.documentElement.scrollTop,
  30771. document.body.scrollTop
  30772. );
  30773. this._bakEditorContaninerWidth = editor.iframe.parentNode.offsetWidth;
  30774. if (this._bakAutoHeight) {
  30775. //当全屏时不能执行自动长高
  30776. editor.autoHeightEnabled = false;
  30777. this.editor.disableAutoHeight();
  30778. }
  30779. document.documentElement.style.overflow = "hidden";
  30780. //修复,滚动条不收起的问题
  30781. window.scrollTo(0, window.scrollY);
  30782. this._bakCssText = this.getDom().style.cssText;
  30783. this._bakCssText1 = this.getDom("iframeholder").style.cssText;
  30784. editor.iframe.parentNode.style.width = "";
  30785. this._updateFullScreen();
  30786. } else {
  30787. while (container.tagName != "BODY") {
  30788. container.style.position = nodeStack.shift();
  30789. container = container.parentNode;
  30790. }
  30791. this.getDom().style.cssText = this._bakCssText;
  30792. this.getDom("iframeholder").style.cssText = this._bakCssText1;
  30793. if (this._bakAutoHeight) {
  30794. editor.autoHeightEnabled = true;
  30795. this.editor.enableAutoHeight();
  30796. }
  30797. document.documentElement.style.overflow = this._bakHtmlOverflow;
  30798. document.body.style.overflow = this._bakBodyOverflow;
  30799. editor.iframe.parentNode.style.width =
  30800. this._bakEditorContaninerWidth + "px";
  30801. window.scrollTo(0, this._bakScrollTop);
  30802. }
  30803. if (browser.gecko && editor.body.contentEditable === "true") {
  30804. var input = document.createElement("input");
  30805. document.body.appendChild(input);
  30806. editor.body.contentEditable = false;
  30807. setTimeout(function () {
  30808. input.focus();
  30809. setTimeout(function () {
  30810. editor.body.contentEditable = true;
  30811. editor.fireEvent("fullscreenchanged", fullscreen);
  30812. editor.selection.getRange().moveToBookmark(bk).select(true);
  30813. baidu.editor.dom.domUtils.remove(input);
  30814. fullscreen && window.scroll(0, 0);
  30815. }, 0);
  30816. }, 0);
  30817. }
  30818. if (editor.body.contentEditable === "true") {
  30819. this.editor.fireEvent("fullscreenchanged", fullscreen);
  30820. this.triggerLayout();
  30821. }
  30822. }
  30823. },
  30824. _updateFullScreen: function () {
  30825. if (this._fullscreen) {
  30826. var vpRect = uiUtils.getViewportRect();
  30827. this.getDom().style.cssText =
  30828. "border:0;position:absolute;left:0;top:" +
  30829. (this.editor.options.topOffset || 0) +
  30830. "px;width:" +
  30831. vpRect.width +
  30832. "px;height:" +
  30833. vpRect.height +
  30834. "px;z-index:" +
  30835. (this.getDom().style.zIndex * 1 + 100);
  30836. uiUtils.setViewportOffset(this.getDom(), {
  30837. left: 0,
  30838. top: this.editor.options.topOffset || 0
  30839. });
  30840. this.editor.setHeight(
  30841. vpRect.height -
  30842. this.getDom("toolbarbox").offsetHeight -
  30843. this.getDom("bottombar").offsetHeight -
  30844. (this.editor.options.topOffset || 0),
  30845. true
  30846. );
  30847. //不手动调一下,会导致全屏失效
  30848. if (browser.gecko) {
  30849. try {
  30850. window.onresize();
  30851. } catch (e) {
  30852. }
  30853. }
  30854. }
  30855. },
  30856. _updateElementPath: function () {
  30857. var bottom = this.getDom("elementpath"),
  30858. list;
  30859. if (
  30860. this.elementPathEnabled &&
  30861. (list = this.editor.queryCommandValue("elementpath"))
  30862. ) {
  30863. var buff = [];
  30864. for (var i = 0, ci; (ci = list[i]); i++) {
  30865. buff[i] = this.formatHtml(
  30866. '<span unselectable="on" onclick="$$.editor.execCommand(&quot;elementpath&quot;, &quot;' +
  30867. i +
  30868. '&quot;);">' +
  30869. ci +
  30870. "</span>"
  30871. );
  30872. }
  30873. bottom.innerHTML =
  30874. '<div class="edui-editor-breadcrumb" onmousedown="return false;">' +
  30875. this.editor.getLang("elementPathTip") +
  30876. ": " +
  30877. buff.join(" &gt; ") +
  30878. "</div>";
  30879. } else {
  30880. bottom.style.display = "none";
  30881. }
  30882. },
  30883. disableElementPath: function () {
  30884. var bottom = this.getDom("elementpath");
  30885. bottom.innerHTML = "";
  30886. bottom.style.display = "none";
  30887. this.elementPathEnabled = false;
  30888. },
  30889. enableElementPath: function () {
  30890. var bottom = this.getDom("elementpath");
  30891. bottom.style.display = "";
  30892. this.elementPathEnabled = true;
  30893. this._updateElementPath();
  30894. },
  30895. _scale: function () {
  30896. var doc = document,
  30897. editor = this.editor,
  30898. editorHolder = editor.container,
  30899. editorDocument = editor.document,
  30900. toolbarBox = this.getDom("toolbarbox"),
  30901. bottombar = this.getDom("bottombar"),
  30902. scale = this.getDom("scale"),
  30903. scalelayer = this.getDom("scalelayer");
  30904. var isMouseMove = false,
  30905. position = null,
  30906. minEditorHeight = 0,
  30907. minEditorWidth = editor.options.minFrameWidth,
  30908. pageX = 0,
  30909. pageY = 0,
  30910. scaleWidth = 0,
  30911. scaleHeight = 0;
  30912. function down() {
  30913. position = domUtils.getXY(editorHolder);
  30914. if (!minEditorHeight) {
  30915. minEditorHeight =
  30916. editor.options.minFrameHeight +
  30917. toolbarBox.offsetHeight +
  30918. bottombar.offsetHeight;
  30919. }
  30920. scalelayer.style.cssText =
  30921. "position:absolute;left:0;display:;top:0;background-color:#41ABFF;opacity:0.4;filter: Alpha(opacity=40);width:" +
  30922. editorHolder.offsetWidth +
  30923. "px;height:" +
  30924. editorHolder.offsetHeight +
  30925. "px;z-index:" +
  30926. (editor.options.zIndex + 1);
  30927. domUtils.on(doc, "mousemove", move);
  30928. domUtils.on(editorDocument, "mouseup", up);
  30929. domUtils.on(doc, "mouseup", up);
  30930. }
  30931. var me = this;
  30932. //by xuheng 全屏时关掉缩放
  30933. this.editor.addListener("fullscreenchanged", function (e, fullScreen) {
  30934. if (fullScreen) {
  30935. me.disableScale();
  30936. } else {
  30937. if (me.editor.options.scaleEnabled) {
  30938. me.enableScale();
  30939. var tmpNode = me.editor.document.createElement("span");
  30940. me.editor.body.appendChild(tmpNode);
  30941. me.editor.body.style.height =
  30942. Math.max(
  30943. domUtils.getXY(tmpNode).y,
  30944. me.editor.iframe.offsetHeight - 20
  30945. ) + "px";
  30946. domUtils.remove(tmpNode);
  30947. }
  30948. }
  30949. });
  30950. function move(event) {
  30951. clearSelection();
  30952. var e = event || window.event;
  30953. pageX = e.pageX || doc.documentElement.scrollLeft + e.clientX;
  30954. pageY = e.pageY || doc.documentElement.scrollTop + e.clientY;
  30955. scaleWidth = pageX - position.x;
  30956. scaleHeight = pageY - position.y;
  30957. if (scaleWidth >= minEditorWidth) {
  30958. isMouseMove = true;
  30959. scalelayer.style.width = scaleWidth + "px";
  30960. }
  30961. if (scaleHeight >= minEditorHeight) {
  30962. isMouseMove = true;
  30963. scalelayer.style.height = scaleHeight + "px";
  30964. }
  30965. }
  30966. function up() {
  30967. if (isMouseMove) {
  30968. isMouseMove = false;
  30969. editor.ui._actualFrameWidth = scalelayer.offsetWidth - 2;
  30970. editorHolder.style.width = editor.ui._actualFrameWidth + "px";
  30971. editor.setHeight(
  30972. scalelayer.offsetHeight -
  30973. bottombar.offsetHeight -
  30974. toolbarBox.offsetHeight -
  30975. 2,
  30976. true
  30977. );
  30978. }
  30979. if (scalelayer) {
  30980. scalelayer.style.display = "none";
  30981. }
  30982. clearSelection();
  30983. domUtils.un(doc, "mousemove", move);
  30984. domUtils.un(editorDocument, "mouseup", up);
  30985. domUtils.un(doc, "mouseup", up);
  30986. }
  30987. function clearSelection() {
  30988. if (browser.ie) doc.selection.clear();
  30989. else window.getSelection().removeAllRanges();
  30990. }
  30991. this.enableScale = function () {
  30992. //trace:2868
  30993. if (editor.queryCommandState("source") == 1) return;
  30994. scale.style.display = "";
  30995. this.scaleEnabled = true;
  30996. domUtils.on(scale, "mousedown", down);
  30997. };
  30998. this.disableScale = function () {
  30999. scale.style.display = "none";
  31000. this.scaleEnabled = false;
  31001. domUtils.un(scale, "mousedown", down);
  31002. };
  31003. },
  31004. isFullScreen: function () {
  31005. return this._fullscreen;
  31006. },
  31007. postRender: function () {
  31008. UIBase.prototype.postRender.call(this);
  31009. for (var i = 0; i < this.toolbars.length; i++) {
  31010. this.toolbars[i].postRender();
  31011. }
  31012. var me = this;
  31013. var timerId,
  31014. domUtils = baidu.editor.dom.domUtils,
  31015. updateFullScreenTime = function () {
  31016. clearTimeout(timerId);
  31017. timerId = setTimeout(function () {
  31018. me._updateFullScreen();
  31019. });
  31020. };
  31021. domUtils.on(window, "resize", updateFullScreenTime);
  31022. me.addListener("destroy", function () {
  31023. domUtils.un(window, "resize", updateFullScreenTime);
  31024. clearTimeout(timerId);
  31025. });
  31026. },
  31027. showToolbarMsg: function (msg, flag) {
  31028. this.getDom("toolbarmsg_label").innerHTML = msg;
  31029. this.getDom("toolbarmsg").style.display = "";
  31030. //
  31031. if (!flag) {
  31032. var w = this.getDom("upload_dialog");
  31033. w.style.display = "none";
  31034. }
  31035. },
  31036. hideToolbarMsg: function () {
  31037. this.getDom("toolbarmsg").style.display = "none";
  31038. },
  31039. mapUrl: function (url) {
  31040. return url
  31041. ? url.replace("~/", this.editor.options.UEDITOR_CORS_URL || "")
  31042. : "";
  31043. },
  31044. triggerLayout: function () {
  31045. var dom = this.getDom();
  31046. if (dom.style.zoom == "1") {
  31047. dom.style.zoom = "100%";
  31048. } else {
  31049. dom.style.zoom = "1";
  31050. }
  31051. }
  31052. };
  31053. utils.inherits(EditorUI, baidu.editor.ui.UIBase);
  31054. var instances = {};
  31055. UE.ui.Editor = function (options) {
  31056. var editor = new UE.Editor(options);
  31057. editor.options.editor = editor;
  31058. utils.loadFile(document, {
  31059. href:
  31060. editor.options.themePath + editor.options.theme + "/css/ueditor.css?20230319",
  31061. tag: "link",
  31062. type: "text/css",
  31063. rel: "stylesheet"
  31064. });
  31065. var oldRender = editor.render;
  31066. editor.render = function (holder) {
  31067. if (holder.constructor === String) {
  31068. editor.key = holder;
  31069. instances[holder] = editor;
  31070. }
  31071. utils.domReady(function () {
  31072. editor.langIsReady
  31073. ? renderUI()
  31074. : editor.addListener("langReady", renderUI);
  31075. function renderUI() {
  31076. editor.setOpt({
  31077. labelMap: editor.options.labelMap || editor.getLang("labelMap")
  31078. });
  31079. new EditorUI(editor.options);
  31080. if (holder) {
  31081. if (holder.constructor === String) {
  31082. holder = document.getElementById(holder);
  31083. }
  31084. holder &&
  31085. holder.getAttribute("name") &&
  31086. (editor.options.textarea = holder.getAttribute("name"));
  31087. if (holder && /script|textarea/gi.test(holder.tagName)) {
  31088. var newDiv = document.createElement("div");
  31089. holder.parentNode.insertBefore(newDiv, holder);
  31090. var cont = holder.value || holder.innerHTML;
  31091. editor.options.initialContent = /^[\t\r\n ]*$/.test(cont)
  31092. ? editor.options.initialContent
  31093. : cont
  31094. .replace(/>[\n\r\t]+([ ]{4})+/g, ">")
  31095. .replace(/[\n\r\t]+([ ]{4})+</g, "<")
  31096. .replace(/>[\n\r\t]+</g, "><");
  31097. holder.className && (newDiv.className = holder.className);
  31098. holder.style.cssText &&
  31099. (newDiv.style.cssText = holder.style.cssText);
  31100. if (/textarea/i.test(holder.tagName)) {
  31101. editor.textarea = holder;
  31102. editor.textarea.style.display = "none";
  31103. } else {
  31104. holder.parentNode.removeChild(holder);
  31105. }
  31106. if (holder.id) {
  31107. newDiv.id = holder.id;
  31108. domUtils.removeAttributes(holder, "id");
  31109. }
  31110. holder = newDiv;
  31111. holder.innerHTML = "";
  31112. }
  31113. }
  31114. domUtils.addClass(holder, "edui-" + editor.options.theme);
  31115. editor.ui.render(holder);
  31116. var opt = editor.options;
  31117. //给实例添加一个编辑器的容器引用
  31118. editor.container = editor.ui.getDom();
  31119. var parents = domUtils.findParents(holder, true);
  31120. var displays = [];
  31121. for (var i = 0, ci; (ci = parents[i]); i++) {
  31122. displays[i] = ci.style.display;
  31123. ci.style.display = "block";
  31124. }
  31125. if (opt.initialFrameWidth) {
  31126. opt.minFrameWidth = opt.initialFrameWidth;
  31127. } else {
  31128. opt.minFrameWidth = opt.initialFrameWidth = holder.offsetWidth;
  31129. var styleWidth = holder.style.width;
  31130. if (/%$/.test(styleWidth)) {
  31131. opt.initialFrameWidth = styleWidth;
  31132. }
  31133. }
  31134. if (opt.initialFrameHeight) {
  31135. opt.minFrameHeight = opt.initialFrameHeight;
  31136. } else {
  31137. opt.initialFrameHeight = opt.minFrameHeight = holder.offsetHeight;
  31138. }
  31139. for (var i = 0, ci; (ci = parents[i]); i++) {
  31140. ci.style.display = displays[i];
  31141. }
  31142. //编辑器最外容器设置了高度,会导致,编辑器不占位
  31143. //todo 先去掉,没有找到原因
  31144. if (holder.style.height) {
  31145. holder.style.height = "";
  31146. }
  31147. editor.container.style.width =
  31148. opt.initialFrameWidth +
  31149. (/%$/.test(opt.initialFrameWidth) ? "" : "px");
  31150. editor.container.style.zIndex = opt.zIndex;
  31151. oldRender.call(editor, editor.ui.getDom("iframeholder"));
  31152. editor.fireEvent("afteruiready");
  31153. }
  31154. });
  31155. };
  31156. return editor;
  31157. };
  31158. /**
  31159. * @file
  31160. * @name UE
  31161. * @short UE
  31162. * @desc UEditor的顶部命名空间
  31163. */
  31164. /**
  31165. * @name getEditor
  31166. * @since 1.2.4+
  31167. * @grammar UE.getEditor(id,[opt]) => Editor实例
  31168. * @desc 提供一个全局的方法得到编辑器实例
  31169. *
  31170. * * ''id'' 放置编辑器的容器id, 如果容器下的编辑器已经存在,就直接返回
  31171. * * ''opt'' 编辑器的可选参数
  31172. * @example
  31173. * UE.getEditor('containerId',{onready:function(){//创建一个编辑器实例
  31174. * this.setContent('hello')
  31175. * }});
  31176. * UE.getEditor('containerId'); //返回刚创建的实例
  31177. *
  31178. */
  31179. UE.getEditor = function (id, opt) {
  31180. var editor = instances[id];
  31181. if (!editor) {
  31182. editor = instances[id] = new UE.ui.Editor(opt);
  31183. editor.render(id);
  31184. }
  31185. return editor;
  31186. };
  31187. UE.delEditor = function (id) {
  31188. var editor;
  31189. if ((editor = instances[id])) {
  31190. editor.key && editor.destroy();
  31191. delete instances[id];
  31192. }
  31193. };
  31194. UE.registerUI = function (uiName, fn, index, editorId) {
  31195. utils.each(uiName.split(/\s+/), function (name) {
  31196. baidu.editor.ui[name] = {
  31197. id: editorId,
  31198. execFn: fn,
  31199. index: index
  31200. };
  31201. });
  31202. };
  31203. })();
  31204. // adapter/message.js
  31205. UE.registerUI("message", function(editor) {
  31206. var editorui = baidu.editor.ui;
  31207. var Message = editorui.Message;
  31208. var holder;
  31209. var _messageItems = [];
  31210. var me = editor;
  31211. me.setOpt("enableMessageShow", true);
  31212. if (me.getOpt("enableMessageShow") === false) {
  31213. return;
  31214. }
  31215. me.addListener("ready", function() {
  31216. holder = document.getElementById(me.ui.id + "_message_holder");
  31217. updateHolderPos();
  31218. setTimeout(function() {
  31219. updateHolderPos();
  31220. }, 500);
  31221. });
  31222. me.addListener("showmessage", function(type, opt) {
  31223. opt = utils.isString(opt)
  31224. ? {
  31225. content: opt
  31226. }
  31227. : opt;
  31228. var message = new Message({
  31229. timeout: opt.timeout,
  31230. type: opt.type,
  31231. content: opt.content,
  31232. keepshow: opt.keepshow,
  31233. editor: me
  31234. }),
  31235. mid = opt.id || "msg_" + (+new Date()).toString(36);
  31236. message.render(holder);
  31237. _messageItems[mid] = message;
  31238. message.reset(opt);
  31239. updateHolderPos();
  31240. return mid;
  31241. });
  31242. me.addListener("updatemessage", function(type, id, opt) {
  31243. opt = utils.isString(opt)
  31244. ? {
  31245. content: opt
  31246. }
  31247. : opt;
  31248. var message = _messageItems[id];
  31249. message.render(holder);
  31250. message && message.reset(opt);
  31251. });
  31252. me.addListener("hidemessage", function(type, id) {
  31253. var message = _messageItems[id];
  31254. message && message.hide();
  31255. });
  31256. function updateHolderPos() {
  31257. if (!holder || !me.ui) return;
  31258. var toolbarbox = me.ui.getDom("toolbarbox");
  31259. if (toolbarbox) {
  31260. holder.style.top = toolbarbox.offsetHeight + 3 + "px";
  31261. }
  31262. holder.style.zIndex =
  31263. Math.max(me.options.zIndex, me.iframe.style.zIndex) + 1;
  31264. }
  31265. });
  31266. })();