wangEditor.js 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.wangEditor = factory());
  5. }(this, (function () { 'use strict';
  6. /*
  7. DOM 操作 API
  8. */
  9. // 根据 html 代码片段创建 dom 对象
  10. function createElemByHTML(html) {
  11. var div = void 0;
  12. div = document.createElement('div');
  13. div.innerHTML = html;
  14. return div.children;
  15. }
  16. // 是否是 DOM List
  17. function isDOMList(selector) {
  18. if (!selector) {
  19. return false;
  20. }
  21. if (selector instanceof HTMLCollection || selector instanceof NodeList) {
  22. return true;
  23. }
  24. return false;
  25. }
  26. // 封装 document.querySelectorAll
  27. function querySelectorAll(selector) {
  28. var result = document.querySelectorAll(selector);
  29. if (isDOMList(result)) {
  30. return result;
  31. } else {
  32. return [result];
  33. }
  34. }
  35. // 记录所有的事件绑定
  36. var eventList = [];
  37. // 创建构造函数
  38. function DomElement(selector) {
  39. if (!selector) {
  40. return;
  41. }
  42. // selector 本来就是 DomElement 对象,直接返回
  43. if (selector instanceof DomElement) {
  44. return selector;
  45. }
  46. this.selector = selector;
  47. var nodeType = selector.nodeType;
  48. // 根据 selector 得出的结果(如 DOM,DOM List)
  49. var selectorResult = [];
  50. if (nodeType === 9) {
  51. // document 节点
  52. selectorResult = [selector];
  53. } else if (nodeType === 1) {
  54. // 单个 DOM 节点
  55. selectorResult = [selector];
  56. } else if (isDOMList(selector) || selector instanceof Array) {
  57. // DOM List 或者数组
  58. selectorResult = selector;
  59. } else if (typeof selector === 'string') {
  60. // 字符串
  61. selector = selector.replace('/\n/mg', '').trim();
  62. if (selector.indexOf('<') === 0) {
  63. // 如 <div>
  64. selectorResult = createElemByHTML(selector);
  65. } else {
  66. // 如 #id .class
  67. selectorResult = querySelectorAll(selector);
  68. }
  69. }
  70. var length = selectorResult.length;
  71. if (!length) {
  72. // 空数组
  73. return this;
  74. }
  75. // 加入 DOM 节点
  76. var i = void 0;
  77. for (i = 0; i < length; i++) {
  78. this[i] = selectorResult[i];
  79. }
  80. this.length = length;
  81. }
  82. // 修改原型
  83. DomElement.prototype = {
  84. constructor: DomElement,
  85. // 类数组,forEach
  86. forEach: function forEach(fn) {
  87. var i = void 0;
  88. for (i = 0; i < this.length; i++) {
  89. var elem = this[i];
  90. var result = fn.call(elem, elem, i);
  91. if (result === false) {
  92. break;
  93. }
  94. }
  95. return this;
  96. },
  97. // clone
  98. clone: function clone(deep) {
  99. var cloneList = [];
  100. this.forEach(function (elem) {
  101. cloneList.push(elem.cloneNode(!!deep));
  102. });
  103. return $(cloneList);
  104. },
  105. // 获取第几个元素
  106. get: function get(index) {
  107. var length = this.length;
  108. if (index >= length) {
  109. index = index % length;
  110. }
  111. return $(this[index]);
  112. },
  113. // 第一个
  114. first: function first() {
  115. return this.get(0);
  116. },
  117. // 最后一个
  118. last: function last() {
  119. var length = this.length;
  120. return this.get(length - 1);
  121. },
  122. // 绑定事件
  123. on: function on(type, selector, fn) {
  124. // selector 不为空,证明绑定事件要加代理
  125. if (!fn) {
  126. fn = selector;
  127. selector = null;
  128. }
  129. // type 是否有多个
  130. var types = [];
  131. types = type.split(/\s+/);
  132. return this.forEach(function (elem) {
  133. types.forEach(function (type) {
  134. if (!type) {
  135. return;
  136. }
  137. // 记录下,方便后面解绑
  138. eventList.push({
  139. elem: elem,
  140. type: type,
  141. fn: fn
  142. });
  143. if (!selector) {
  144. // 无代理
  145. elem.addEventListener(type, fn);
  146. return;
  147. }
  148. // 有代理
  149. elem.addEventListener(type, function (e) {
  150. var target = e.target;
  151. if (target.matches(selector)) {
  152. fn.call(target, e);
  153. }
  154. });
  155. });
  156. });
  157. },
  158. // 取消事件绑定
  159. off: function off(type, fn) {
  160. return this.forEach(function (elem) {
  161. elem.removeEventListener(type, fn);
  162. });
  163. },
  164. // 获取/设置 属性
  165. attr: function attr(key, val) {
  166. if (val == null) {
  167. // 获取值
  168. return this[0].getAttribute(key);
  169. } else {
  170. // 设置值
  171. return this.forEach(function (elem) {
  172. elem.setAttribute(key, val);
  173. });
  174. }
  175. },
  176. // 添加 class
  177. addClass: function addClass(className) {
  178. if (!className) {
  179. return this;
  180. }
  181. return this.forEach(function (elem) {
  182. var arr = void 0;
  183. if (elem.className) {
  184. // 解析当前 className 转换为数组
  185. arr = elem.className.split(/\s/);
  186. arr = arr.filter(function (item) {
  187. return !!item.trim();
  188. });
  189. // 添加 class
  190. if (arr.indexOf(className) < 0) {
  191. arr.push(className);
  192. }
  193. // 修改 elem.class
  194. elem.className = arr.join(' ');
  195. } else {
  196. elem.className = className;
  197. }
  198. });
  199. },
  200. // 删除 class
  201. removeClass: function removeClass(className) {
  202. if (!className) {
  203. return this;
  204. }
  205. return this.forEach(function (elem) {
  206. var arr = void 0;
  207. if (elem.className) {
  208. // 解析当前 className 转换为数组
  209. arr = elem.className.split(/\s/);
  210. arr = arr.filter(function (item) {
  211. item = item.trim();
  212. // 删除 class
  213. if (!item || item === className) {
  214. return false;
  215. }
  216. return true;
  217. });
  218. // 修改 elem.class
  219. elem.className = arr.join(' ');
  220. }
  221. });
  222. },
  223. // 修改 css
  224. css: function css(key, val) {
  225. var currentStyle = key + ':' + val + ';';
  226. return this.forEach(function (elem) {
  227. var style = (elem.getAttribute('style') || '').trim();
  228. var styleArr = void 0,
  229. resultArr = [];
  230. if (style) {
  231. // 将 style 按照 ; 拆分为数组
  232. styleArr = style.split(';');
  233. styleArr.forEach(function (item) {
  234. // 对每项样式,按照 : 拆分为 key 和 value
  235. var arr = item.split(':').map(function (i) {
  236. return i.trim();
  237. });
  238. if (arr.length === 2) {
  239. resultArr.push(arr[0] + ':' + arr[1]);
  240. }
  241. });
  242. // 替换或者新增
  243. resultArr = resultArr.map(function (item) {
  244. if (item.indexOf(key) === 0) {
  245. return currentStyle;
  246. } else {
  247. return item;
  248. }
  249. });
  250. if (resultArr.indexOf(currentStyle) < 0) {
  251. resultArr.push(currentStyle);
  252. }
  253. // 结果
  254. elem.setAttribute('style', resultArr.join('; '));
  255. } else {
  256. // style 无值
  257. elem.setAttribute('style', currentStyle);
  258. }
  259. });
  260. },
  261. // 显示
  262. show: function show() {
  263. return this.css('display', 'block');
  264. },
  265. // 隐藏
  266. hide: function hide() {
  267. return this.css('display', 'none');
  268. },
  269. // 获取子节点
  270. children: function children() {
  271. var elem = this[0];
  272. if (!elem) {
  273. return null;
  274. }
  275. return $(elem.children);
  276. },
  277. // 获取子节点(包括文本节点)
  278. childNodes: function childNodes() {
  279. var elem = this[0];
  280. if (!elem) {
  281. return null;
  282. }
  283. return $(elem.childNodes);
  284. },
  285. // 增加子节点
  286. append: function append($children) {
  287. return this.forEach(function (elem) {
  288. $children.forEach(function (child) {
  289. elem.appendChild(child);
  290. });
  291. });
  292. },
  293. // 移除当前节点
  294. remove: function remove() {
  295. return this.forEach(function (elem) {
  296. if (elem.remove) {
  297. elem.remove();
  298. } else {
  299. var parent = elem.parentElement;
  300. parent && parent.removeChild(elem);
  301. }
  302. });
  303. },
  304. // 是否包含某个子节点
  305. isContain: function isContain($child) {
  306. var elem = this[0];
  307. var child = $child[0];
  308. return elem.contains(child);
  309. },
  310. // 尺寸数据
  311. getSizeData: function getSizeData() {
  312. var elem = this[0];
  313. return elem.getBoundingClientRect(); // 可得到 bottom height left right top width 的数据
  314. },
  315. // 封装 nodeName
  316. getNodeName: function getNodeName() {
  317. var elem = this[0];
  318. return elem.nodeName;
  319. },
  320. // 从当前元素查找
  321. find: function find(selector) {
  322. var elem = this[0];
  323. return $(elem.querySelectorAll(selector));
  324. },
  325. // 获取当前元素的 text
  326. text: function text(val) {
  327. if (!val) {
  328. // 获取 text
  329. var elem = this[0];
  330. return elem.innerHTML.replace(/<.*?>/g, function () {
  331. return '';
  332. });
  333. } else {
  334. // 设置 text
  335. return this.forEach(function (elem) {
  336. elem.innerHTML = val;
  337. });
  338. }
  339. },
  340. // 获取 html
  341. html: function html(value) {
  342. var elem = this[0];
  343. if (value == null) {
  344. return elem.innerHTML;
  345. } else {
  346. elem.innerHTML = value;
  347. return this;
  348. }
  349. },
  350. width: function width() {
  351. return this[0].offsetWidth;
  352. },
  353. // 获取 value
  354. val: function val() {
  355. var elem = this[0];
  356. return elem.value.trim();
  357. },
  358. // focus
  359. focus: function focus() {
  360. return this.forEach(function (elem) {
  361. elem.focus();
  362. });
  363. },
  364. // parent
  365. parent: function parent() {
  366. var elem = this[0];
  367. return $(elem.parentElement);
  368. },
  369. // parentUntil 找到符合 selector 的父节点
  370. parentUntil: function parentUntil(selector, _currentElem) {
  371. var results = document.querySelectorAll(selector);
  372. var length = results.length;
  373. if (!length) {
  374. // 传入的 selector 无效
  375. return null;
  376. }
  377. var elem = _currentElem || this[0];
  378. if (elem.nodeName === 'BODY') {
  379. return null;
  380. }
  381. var parent = elem.parentElement;
  382. var i = void 0;
  383. for (i = 0; i < length; i++) {
  384. if (parent === results[i]) {
  385. // 找到,并返回
  386. return $(parent);
  387. }
  388. }
  389. // 继续查找
  390. return this.parentUntil(selector, parent);
  391. },
  392. // 判断两个 elem 是否相等
  393. equal: function equal($elem) {
  394. if ($elem.nodeType === 1) {
  395. return this[0] === $elem;
  396. } else {
  397. return this[0] === $elem[0];
  398. }
  399. },
  400. // 将该元素插入到某个元素前面
  401. insertBefore: function insertBefore(selector) {
  402. var $referenceNode = $(selector);
  403. var referenceNode = $referenceNode[0];
  404. if (!referenceNode) {
  405. return this;
  406. }
  407. return this.forEach(function (elem) {
  408. var parent = referenceNode.parentNode;
  409. parent.insertBefore(elem, referenceNode);
  410. });
  411. },
  412. // 将该元素插入到某个元素后面
  413. insertAfter: function insertAfter(selector) {
  414. var $referenceNode = $(selector);
  415. var referenceNode = $referenceNode[0];
  416. if (!referenceNode) {
  417. return this;
  418. }
  419. return this.forEach(function (elem) {
  420. var parent = referenceNode.parentNode;
  421. if (parent.lastChild === referenceNode) {
  422. // 最后一个元素
  423. parent.appendChild(elem);
  424. } else {
  425. // 不是最后一个元素
  426. parent.insertBefore(elem, referenceNode.nextSibling);
  427. }
  428. });
  429. }
  430. // new 一个对象
  431. };function $(selector) {
  432. return new DomElement(selector);
  433. }
  434. // 解绑所有事件,用于销毁编辑器
  435. $.offAll = function () {
  436. eventList.forEach(function (item) {
  437. var elem = item.elem;
  438. var type = item.type;
  439. var fn = item.fn;
  440. // 解绑
  441. elem.removeEventListener(type, fn);
  442. });
  443. };
  444. /*
  445. 工具
  446. */
  447. // 和 UA 相关的属性
  448. var UA = {
  449. _ua: navigator.userAgent,
  450. // 是否 webkit
  451. isWebkit: function isWebkit() {
  452. var reg = /webkit/i;
  453. return reg.test(this._ua);
  454. },
  455. // 是否 IE
  456. isIE: function isIE() {
  457. return 'ActiveXObject' in window;
  458. }
  459. // 遍历对象
  460. };function objForEach(obj, fn) {
  461. var key = void 0,
  462. result = void 0;
  463. for (key in obj) {
  464. if (obj.hasOwnProperty(key)) {
  465. result = fn.call(obj, key, obj[key]);
  466. if (result === false) {
  467. break;
  468. }
  469. }
  470. }
  471. }
  472. // 遍历类数组
  473. function arrForEach(fakeArr, fn) {
  474. var i = void 0,
  475. item = void 0,
  476. result = void 0;
  477. var length = fakeArr.length || 0;
  478. for (i = 0; i < length; i++) {
  479. item = fakeArr[i];
  480. result = fn.call(fakeArr, item, i);
  481. if (result === false) {
  482. break;
  483. }
  484. }
  485. }
  486. // 获取随机数
  487. function getRandom(prefix) {
  488. return prefix + Math.random().toString().slice(2);
  489. }
  490. // 替换 html 特殊字符
  491. function replaceHtmlSymbol(html) {
  492. if (html == null) {
  493. return '';
  494. }
  495. return html.replace(/</gm, '&lt;').replace(/>/gm, '&gt;').replace(/"/gm, '&quot;').replace(/(\r\n|\r|\n)/g, '<br/>');
  496. }
  497. // 返回百分比的格式
  498. // 判断是不是 function
  499. function isFunction(fn) {
  500. return typeof fn === 'function';
  501. }
  502. /*
  503. 命令,封装 document.execCommand
  504. */
  505. // 构造函数
  506. function Command(editor) {
  507. this.editor = editor;
  508. }
  509. // 修改原型
  510. Command.prototype = {
  511. constructor: Command,
  512. // 执行命令
  513. do: function _do(name, value) {
  514. var editor = this.editor;
  515. // 使用 styleWithCSS
  516. if (!editor._useStyleWithCSS) {
  517. document.execCommand("styleWithCSS", null, true);
  518. editor._useStyleWithCSS = true;
  519. }
  520. // 如果无选区,忽略
  521. if (!editor.selection.getRange()) {
  522. return;
  523. }
  524. // 恢复选取
  525. editor.selection.restoreSelection();
  526. // 执行
  527. var _name = "_" + name;
  528. if (this[_name]) {
  529. // 有自定义事件
  530. this[_name](value);
  531. } else {
  532. // 默认 command
  533. this._execCommand(name, value);
  534. }
  535. // 修改菜单状态
  536. editor.menus.changeActive();
  537. // 最后,恢复选取保证光标在原来的位置闪烁
  538. editor.selection.saveRange();
  539. editor.selection.restoreSelection();
  540. // 触发 onchange
  541. editor.change && editor.change();
  542. },
  543. // 自定义 insertHTML 事件
  544. _insertHTML: function _insertHTML(html) {
  545. var editor = this.editor;
  546. var range = editor.selection.getRange();
  547. if (this.queryCommandSupported("insertHTML")) {
  548. // W3C
  549. this._execCommand("insertHTML", html);
  550. } else if (range.insertNode) {
  551. // IE
  552. range.deleteContents();
  553. range.insertNode($(html)[0]);
  554. } else if (range.pasteHTML) {
  555. // IE <= 10
  556. range.pasteHTML(html);
  557. }
  558. },
  559. // 插入 elem
  560. _insertElem: function _insertElem($elem) {
  561. var editor = this.editor;
  562. var range = editor.selection.getRange();
  563. if (range.insertNode) {
  564. range.deleteContents();
  565. range.insertNode($elem[0]);
  566. }
  567. },
  568. // 封装 execCommand
  569. _execCommand: function _execCommand(name, value) {
  570. document.execCommand(name, false, value);
  571. },
  572. // 封装 document.queryCommandValue
  573. queryCommandValue: function queryCommandValue(name) {
  574. return document.queryCommandValue(name);
  575. },
  576. // 封装 document.queryCommandState
  577. queryCommandState: function queryCommandState(name) {
  578. return document.queryCommandState(name);
  579. },
  580. // 封装 document.queryCommandSupported
  581. queryCommandSupported: function queryCommandSupported(name) {
  582. return document.queryCommandSupported(name);
  583. }
  584. };
  585. /*
  586. 配置信息
  587. */
  588. var config = {
  589. // 默认菜单配置
  590. menus: ["head", "bold", "fontSize", "fontName", "italic", "underline", "strikeThrough", "foreColor", "backColor", "link", "list", "justify", "quote", "emoticon", "image", "table", "video", "code", "undo", "redo", "composition", "formula"],
  591. fontNames: ["宋体", "微软雅黑", "Arial", "Tahoma", "Verdana"],
  592. colors: ["#000000", "#eeece0", "#1c487f", "#4d80bf", "#c24f4a", "#8baa4a", "#7b5ba1", "#46acc8", "#f9963b", "#ffffff"],
  593. // // 语言配置
  594. // lang: {
  595. // '设置标题': 'title',
  596. // '正文': 'p',
  597. // '链接文字': 'link text',
  598. // '链接': 'link',
  599. // '插入': 'insert',
  600. // '创建': 'init'
  601. // },
  602. // 表情
  603. emotions: [{
  604. // tab 的标题
  605. title: "默认",
  606. // type -> 'emoji' / 'image'
  607. type: "image",
  608. // content -> 数组
  609. content: [{
  610. alt: "[坏笑]",
  611. src: "http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/50/pcmoren_huaixiao_org.png"
  612. }, {
  613. alt: "[舔屏]",
  614. src: "http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/40/pcmoren_tian_org.png"
  615. }, {
  616. alt: "[污]",
  617. src: "http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/3c/pcmoren_wu_org.png"
  618. }]
  619. }, {
  620. // tab 的标题
  621. title: "新浪",
  622. // type -> 'emoji' / 'image'
  623. type: "image",
  624. // content -> 数组
  625. content: [{
  626. src: "http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/7a/shenshou_thumb.gif",
  627. alt: "[草泥马]"
  628. }, {
  629. src: "http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/60/horse2_thumb.gif",
  630. alt: "[神马]"
  631. }, {
  632. src: "http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/bc/fuyun_thumb.gif",
  633. alt: "[浮云]"
  634. }]
  635. }, {
  636. // tab 的标题
  637. title: "emoji",
  638. // type -> 'emoji' / 'image'
  639. type: "emoji",
  640. // content -> 数组
  641. content: "😀 😃 😄 😁 😆 😅 😂 😊 😇 🙂 🙃 😉 😓 😪 😴 🙄 🤔 😬 🤐".split(/\s/)
  642. // {
  643. // // tab 的标题
  644. // title: '手势',
  645. // // type -> 'emoji' / 'image'
  646. // type: 'emoji',
  647. // // content -> 数组
  648. // content: ['🙌', '👏', '👋', '👍', '👎', '👊', '✊', '️👌', '✋', '👐', '💪', '🙏', '️👆', '👇', '👈', '👉', '🖕', '🖐', '🤘']
  649. // }
  650. }],
  651. // 编辑区域的 z-index
  652. zIndex: 10000,
  653. // 是否开启 debug 模式(debug 模式下错误会 throw error 形式抛出)
  654. debug: false,
  655. // 插入链接时候的格式校验
  656. linkCheck: function linkCheck(text, link) {
  657. // text 是插入的文字
  658. // link 是插入的链接
  659. return true; // 返回 true 即表示成功
  660. // return '校验失败' // 返回字符串即表示失败的提示信息
  661. },
  662. // 插入网络图片的校验
  663. linkImgCheck: function linkImgCheck(src) {
  664. // src 即图片的地址
  665. return true; // 返回 true 即表示成功
  666. // return '校验失败' // 返回字符串即表示失败的提示信息
  667. },
  668. // 粘贴过滤样式,默认开启
  669. pasteFilterStyle: true,
  670. // 粘贴内容时,忽略图片。默认关闭
  671. pasteIgnoreImg: false,
  672. // 对粘贴的文字进行自定义处理,返回处理后的结果。编辑器会将处理后的结果粘贴到编辑区域中。
  673. // IE 暂时不支持
  674. pasteTextHandle: function pasteTextHandle(content) {
  675. // content 即粘贴过来的内容(html 或 纯文本),可进行自定义处理然后返回
  676. return content;
  677. },
  678. // onchange 事件
  679. // onchange: function (html) {
  680. // // html 即变化之后的内容
  681. // console.log(html)
  682. // },
  683. // 是否显示添加网络图片的 tab
  684. showLinkImg: true,
  685. // 插入网络图片的回调
  686. linkImgCallback: function linkImgCallback(url) {
  687. // console.log(url) // url 即插入图片的地址
  688. },
  689. // 默认上传图片 max size: 5M
  690. uploadImgMaxSize: 5 * 1024 * 1024,
  691. // 配置一次最多上传几个图片
  692. // uploadImgMaxLength: 5,
  693. // 上传图片,是否显示 base64 格式
  694. uploadImgShowBase64: false,
  695. // 上传图片,server 地址(如果有值,则 base64 格式的配置则失效)
  696. // uploadImgServer: '/upload',
  697. // 自定义配置 filename
  698. uploadFileName: "",
  699. // 上传图片的自定义参数
  700. uploadImgParams: {
  701. // token: 'abcdef12345'
  702. },
  703. // 上传图片的自定义header
  704. uploadImgHeaders: {
  705. // 'Accept': 'text/x-json'
  706. },
  707. // 配置 XHR withCredentials
  708. withCredentials: false,
  709. // 自定义上传图片超时时间 ms
  710. uploadImgTimeout: 10000,
  711. // 上传图片 hook
  712. uploadImgHooks: {
  713. // customInsert: function (insertLinkImg, result, editor) {
  714. // console.log('customInsert')
  715. // // 图片上传并返回结果,自定义插入图片的事件,而不是编辑器自动插入图片
  716. // const data = result.data1 || []
  717. // data.forEach(link => {
  718. // insertLinkImg(link)
  719. // })
  720. // },
  721. before: function before(xhr, editor, files) {
  722. // 图片上传之前触发
  723. // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
  724. // return {
  725. // prevent: true,
  726. // msg: '放弃上传'
  727. // }
  728. },
  729. success: function success(xhr, editor, result) {
  730. // 图片上传并返回结果,图片插入成功之后触发
  731. },
  732. fail: function fail(xhr, editor, result) {
  733. // 图片上传并返回结果,但图片插入错误时触发
  734. },
  735. error: function error(xhr, editor) {
  736. // 图片上传出错时触发
  737. },
  738. timeout: function timeout(xhr, editor) {
  739. // 图片上传超时时触发
  740. }
  741. },
  742. // 是否上传七牛云,默认为 false
  743. qiniu: false
  744. // 上传图片自定义提示方法
  745. // customAlert: function (info) {
  746. // // 自定义上传提示
  747. // },
  748. // // 自定义上传图片
  749. // customUploadImg: function (files, insert) {
  750. // // files 是 input 中选中的文件列表
  751. // // insert 是获取图片 url 后,插入到编辑器的方法
  752. // insert(imgUrl)
  753. // }
  754. };
  755. /*
  756. 替换多语言
  757. */
  758. var replaceLang = function (editor, str) {
  759. var langArgs = editor.config.langArgs || [];
  760. var result = str;
  761. langArgs.forEach(function (item) {
  762. var reg = item.reg;
  763. var val = item.val;
  764. if (reg.test(result)) {
  765. result = result.replace(reg, function () {
  766. return val;
  767. });
  768. }
  769. });
  770. return result;
  771. };
  772. /*
  773. droplist
  774. */
  775. var _emptyFn = function _emptyFn() {};
  776. // 构造函数
  777. function DropList(menu, opt) {
  778. var _this = this;
  779. // droplist 所依附的菜单
  780. var editor = menu.editor;
  781. this.menu = menu;
  782. this.opt = opt;
  783. // 容器
  784. var $container = $('<div class="w-e-droplist"></div>');
  785. // 标题
  786. var $title = opt.$title;
  787. var titleHtml = void 0;
  788. if ($title) {
  789. // 替换多语言
  790. titleHtml = $title.html();
  791. titleHtml = replaceLang(editor, titleHtml);
  792. $title.html(titleHtml);
  793. $title.addClass('w-e-dp-title');
  794. $container.append($title);
  795. }
  796. var list = opt.list || [];
  797. var type = opt.type || 'list'; // 'list' 列表形式(如“标题”菜单) / 'inline-block' 块状形式(如“颜色”菜单)
  798. var onClick = opt.onClick || _emptyFn;
  799. // 加入 DOM 并绑定事件
  800. var $list = $('<ul class="' + (type === 'list' ? 'w-e-list' : 'w-e-block') + '"></ul>');
  801. $container.append($list);
  802. list.forEach(function (item) {
  803. var $elem = item.$elem;
  804. // 替换多语言
  805. var elemHtml = $elem.html();
  806. elemHtml = replaceLang(editor, elemHtml);
  807. $elem.html(elemHtml);
  808. var value = item.value;
  809. var $li = $('<li class="w-e-item"></li>');
  810. if ($elem) {
  811. $li.append($elem);
  812. $list.append($li);
  813. $li.on('click', function (e) {
  814. onClick(value);
  815. // 隐藏
  816. _this.hideTimeoutId = setTimeout(function () {
  817. _this.hide();
  818. }, 0);
  819. });
  820. }
  821. });
  822. // 绑定隐藏事件
  823. $container.on('mouseleave', function (e) {
  824. _this.hideTimeoutId = setTimeout(function () {
  825. _this.hide();
  826. }, 0);
  827. });
  828. // 记录属性
  829. this.$container = $container;
  830. // 基本属性
  831. this._rendered = false;
  832. this._show = false;
  833. }
  834. // 原型
  835. DropList.prototype = {
  836. constructor: DropList,
  837. // 显示(插入DOM)
  838. show: function show() {
  839. if (this.hideTimeoutId) {
  840. // 清除之前的定时隐藏
  841. clearTimeout(this.hideTimeoutId);
  842. }
  843. var menu = this.menu;
  844. var $menuELem = menu.$elem;
  845. var $container = this.$container;
  846. if (this._show) {
  847. return;
  848. }
  849. if (this._rendered) {
  850. // 显示
  851. $container.show();
  852. } else {
  853. // 加入 DOM 之前先定位位置
  854. var menuHeight = $menuELem.getSizeData().height || 0;
  855. var width = this.opt.width || 100; // 默认为 100
  856. $container.css('margin-top', menuHeight + 'px').css('width', width + 'px');
  857. // 加入到 DOM
  858. $menuELem.append($container);
  859. this._rendered = true;
  860. }
  861. // 修改属性
  862. this._show = true;
  863. },
  864. // 隐藏(移除DOM)
  865. hide: function hide() {
  866. if (this.showTimeoutId) {
  867. // 清除之前的定时显示
  868. clearTimeout(this.showTimeoutId);
  869. }
  870. var $container = this.$container;
  871. if (!this._show) {
  872. return;
  873. }
  874. // 隐藏并需改属性
  875. $container.hide();
  876. this._show = false;
  877. }
  878. };
  879. /*
  880. menu - BackColor
  881. */
  882. // 构造函数
  883. function BackColor(editor) {
  884. var _this = this;
  885. this.editor = editor;
  886. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-paint-brush"></i></div>');
  887. this.type = 'droplist';
  888. // 获取配置的颜色
  889. var config = editor.config;
  890. var colors = config.colors || [];
  891. // 当前是否 active 状态
  892. this._active = false;
  893. // 初始化 droplist
  894. this.droplist = new DropList(this, {
  895. width: 120,
  896. $title: $('<p>背景色</p>'),
  897. type: 'inline-block', // droplist 内容以 block 形式展示
  898. list: colors.map(function (color) {
  899. return { $elem: $('<i style="color:' + color + ';" class="w-e-icon-paint-brush"></i>'), value: color };
  900. }),
  901. onClick: function onClick(value) {
  902. // 注意 this 是指向当前的 BackColor 对象
  903. _this._command(value);
  904. }
  905. });
  906. }
  907. // 原型
  908. BackColor.prototype = {
  909. constructor: BackColor,
  910. // 执行命令
  911. _command: function _command(value) {
  912. var editor = this.editor;
  913. editor.cmd.do('backColor', value);
  914. }
  915. };
  916. /*
  917. bold-menu
  918. */
  919. // 构造函数
  920. function Bold(editor) {
  921. this.editor = editor;
  922. this.$elem = $("<div class=\"w-e-menu\">\n <i class=\"w-e-icon-bold\"></i>\n </div>");
  923. this.type = "click";
  924. // 当前是否 active 状态
  925. this._active = false;
  926. }
  927. // 原型
  928. Bold.prototype = {
  929. constructor: Bold,
  930. // 点击事件
  931. onClick: function onClick(e) {
  932. // 点击菜单将触发这里
  933. var editor = this.editor;
  934. var isSeleEmpty = editor.selection.isSelectionEmpty();
  935. if (isSeleEmpty) {
  936. // 选区是空的,插入并选中一个“空白”
  937. editor.selection.createEmptyRange();
  938. }
  939. // 执行 bold 命令
  940. editor.cmd.do("bold");
  941. if (isSeleEmpty) {
  942. // 需要将选取折叠起来
  943. editor.selection.collapseRange();
  944. editor.selection.restoreSelection();
  945. }
  946. },
  947. // 试图改变 active 状态
  948. tryChangeActive: function tryChangeActive(e) {
  949. var editor = this.editor;
  950. var $elem = this.$elem;
  951. if (editor.cmd.queryCommandState("bold")) {
  952. this._active = true;
  953. $elem.addClass("w-e-active");
  954. } else {
  955. this._active = false;
  956. $elem.removeClass("w-e-active");
  957. }
  958. }
  959. };
  960. /*
  961. panel
  962. */
  963. var emptyFn = function emptyFn() {};
  964. // 记录已经显示 panel 的菜单
  965. var _isCreatedPanelMenus = [];
  966. // 构造函数
  967. function Panel(menu, opt) {
  968. this.menu = menu;
  969. this.opt = opt;
  970. }
  971. // 原型
  972. Panel.prototype = {
  973. constructor: Panel,
  974. // 显示(插入DOM)
  975. show: function show() {
  976. var _this = this;
  977. var menu = this.menu;
  978. if (_isCreatedPanelMenus.indexOf(menu) >= 0) {
  979. // 该菜单已经创建了 panel 不能再创建
  980. return;
  981. }
  982. var editor = menu.editor;
  983. var $body = $('body');
  984. var $textContainerElem = editor.$textContainerElem;
  985. var opt = this.opt;
  986. // panel 的容器
  987. var $container = $('<div class="w-e-panel-container"></div>');
  988. var width = opt.width || 300; // 默认 300px
  989. $container.css('width', width + 'px').css('margin-left', (0 - width) / 2 + 'px');
  990. // 添加关闭按钮
  991. var $closeBtn = $('<i class="w-e-icon-close w-e-panel-close"></i>');
  992. $container.append($closeBtn);
  993. $closeBtn.on('click', function () {
  994. _this.hide();
  995. });
  996. // 准备 tabs 容器
  997. var $tabTitleContainer = $('<ul class="w-e-panel-tab-title"></ul>');
  998. var $tabContentContainer = $('<div class="w-e-panel-tab-content"></div>');
  999. $container.append($tabTitleContainer).append($tabContentContainer);
  1000. // 设置高度
  1001. var height = opt.height;
  1002. if (height) {
  1003. $tabContentContainer.css('height', height + 'px').css('overflow-y', 'auto');
  1004. }
  1005. // tabs
  1006. var tabs = opt.tabs || [];
  1007. var tabTitleArr = [];
  1008. var tabContentArr = [];
  1009. tabs.forEach(function (tab, tabIndex) {
  1010. if (!tab) {
  1011. return;
  1012. }
  1013. var title = tab.title || '';
  1014. var tpl = tab.tpl || '';
  1015. // 替换多语言
  1016. title = replaceLang(editor, title);
  1017. tpl = replaceLang(editor, tpl);
  1018. // 添加到 DOM
  1019. var $title = $('<li class="w-e-item">' + title + '</li>');
  1020. $tabTitleContainer.append($title);
  1021. var $content = $(tpl);
  1022. $tabContentContainer.append($content);
  1023. // 记录到内存
  1024. $title._index = tabIndex;
  1025. tabTitleArr.push($title);
  1026. tabContentArr.push($content);
  1027. // 设置 active 项
  1028. if (tabIndex === 0) {
  1029. $title._active = true;
  1030. $title.addClass('w-e-active');
  1031. } else {
  1032. $content.hide();
  1033. }
  1034. // 绑定 tab 的事件
  1035. $title.on('click', function (e) {
  1036. if ($title._active) {
  1037. return;
  1038. }
  1039. // 隐藏所有的 tab
  1040. tabTitleArr.forEach(function ($title) {
  1041. $title._active = false;
  1042. $title.removeClass('w-e-active');
  1043. });
  1044. tabContentArr.forEach(function ($content) {
  1045. $content.hide();
  1046. });
  1047. // 显示当前的 tab
  1048. $title._active = true;
  1049. $title.addClass('w-e-active');
  1050. $content.show();
  1051. });
  1052. });
  1053. // 绑定关闭事件
  1054. $container.on('click', function (e) {
  1055. // 点击时阻止冒泡
  1056. e.stopPropagation();
  1057. });
  1058. $body.on('click', function (e) {
  1059. _this.hide();
  1060. });
  1061. // 添加到 DOM
  1062. $textContainerElem.append($container);
  1063. // 绑定 opt 的事件,只有添加到 DOM 之后才能绑定成功
  1064. tabs.forEach(function (tab, index) {
  1065. if (!tab) {
  1066. return;
  1067. }
  1068. var events = tab.events || [];
  1069. events.forEach(function (event) {
  1070. var selector = event.selector;
  1071. var type = event.type;
  1072. var fn = event.fn || emptyFn;
  1073. var $content = tabContentArr[index];
  1074. $content.find(selector).on(type, function (e) {
  1075. e.stopPropagation();
  1076. var needToHide = fn(e);
  1077. // 执行完事件之后,是否要关闭 panel
  1078. if (needToHide) {
  1079. _this.hide();
  1080. }
  1081. });
  1082. });
  1083. });
  1084. // focus 第一个 elem
  1085. var $inputs = $container.find('input[type=text],textarea');
  1086. if ($inputs.length) {
  1087. $inputs.get(0).focus();
  1088. }
  1089. // 添加到属性
  1090. this.$container = $container;
  1091. // 隐藏其他 panel
  1092. this._hideOtherPanels();
  1093. // 记录该 menu 已经创建了 panel
  1094. _isCreatedPanelMenus.push(menu);
  1095. },
  1096. // 隐藏(移除DOM)
  1097. hide: function hide() {
  1098. var menu = this.menu;
  1099. var $container = this.$container;
  1100. if ($container) {
  1101. $container.remove();
  1102. }
  1103. // 将该 menu 记录中移除
  1104. _isCreatedPanelMenus = _isCreatedPanelMenus.filter(function (item) {
  1105. if (item === menu) {
  1106. return false;
  1107. } else {
  1108. return true;
  1109. }
  1110. });
  1111. },
  1112. // 一个 panel 展示时,隐藏其他 panel
  1113. _hideOtherPanels: function _hideOtherPanels() {
  1114. if (!_isCreatedPanelMenus.length) {
  1115. return;
  1116. }
  1117. _isCreatedPanelMenus.forEach(function (menu) {
  1118. var panel = menu.panel || {};
  1119. if (panel.hide) {
  1120. panel.hide();
  1121. }
  1122. });
  1123. }
  1124. };
  1125. /*
  1126. menu - code
  1127. */
  1128. // 构造函数
  1129. function Code(editor) {
  1130. this.editor = editor;
  1131. this.$elem = $('<div class="w-e-menu">\n <i class="w-e-icon-terminal"></i>\n </div>');
  1132. this.type = 'panel';
  1133. // 当前是否 active 状态
  1134. this._active = false;
  1135. }
  1136. // 原型
  1137. Code.prototype = {
  1138. constructor: Code,
  1139. onClick: function onClick(e) {
  1140. var editor = this.editor;
  1141. var $startElem = editor.selection.getSelectionStartElem();
  1142. var $endElem = editor.selection.getSelectionEndElem();
  1143. var isSeleEmpty = editor.selection.isSelectionEmpty();
  1144. var selectionText = editor.selection.getSelectionText();
  1145. var $code = void 0;
  1146. if (!$startElem.equal($endElem)) {
  1147. // 跨元素选择,不做处理
  1148. editor.selection.restoreSelection();
  1149. return;
  1150. }
  1151. if (!isSeleEmpty) {
  1152. // 选取不是空,用 <code> 包裹即可
  1153. $code = $('<code>' + selectionText + '</code>');
  1154. editor.cmd.do('insertElem', $code);
  1155. editor.selection.createRangeByElem($code, false);
  1156. editor.selection.restoreSelection();
  1157. return;
  1158. }
  1159. // 选取是空,且没有夸元素选择,则插入 <pre><code></code></prev>
  1160. if (this._active) {
  1161. // 选中状态,将编辑内容
  1162. this._createPanel($startElem.html());
  1163. } else {
  1164. // 未选中状态,将创建内容
  1165. this._createPanel();
  1166. }
  1167. },
  1168. _createPanel: function _createPanel(value) {
  1169. var _this = this;
  1170. // value - 要编辑的内容
  1171. value = value || '';
  1172. var type = !value ? 'new' : 'edit';
  1173. var textId = getRandom('texxt');
  1174. var btnId = getRandom('btn');
  1175. var panel = new Panel(this, {
  1176. width: 500,
  1177. // 一个 Panel 包含多个 tab
  1178. tabs: [{
  1179. // 标题
  1180. title: '插入代码',
  1181. // 模板
  1182. tpl: '<div>\n <textarea id="' + textId + '" style="height:145px;;">' + value + '</textarea>\n <div class="w-e-button-container">\n <button id="' + btnId + '" class="right">\u63D2\u5165</button>\n </div>\n <div>',
  1183. // 事件绑定
  1184. events: [
  1185. // 插入代码
  1186. {
  1187. selector: '#' + btnId,
  1188. type: 'click',
  1189. fn: function fn() {
  1190. var $text = $('#' + textId);
  1191. var text = $text.val() || $text.html();
  1192. text = replaceHtmlSymbol(text);
  1193. if (type === 'new') {
  1194. // 新插入
  1195. _this._insertCode(text);
  1196. } else {
  1197. // 编辑更新
  1198. _this._updateCode(text);
  1199. }
  1200. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  1201. return true;
  1202. }
  1203. }] // first tab end
  1204. }] // tabs end
  1205. }); // new Panel end
  1206. // 显示 panel
  1207. panel.show();
  1208. // 记录属性
  1209. this.panel = panel;
  1210. },
  1211. // 插入代码
  1212. _insertCode: function _insertCode(value) {
  1213. var editor = this.editor;
  1214. editor.cmd.do('insertHTML', '<pre><code>' + value + '</code></pre><p><br></p>');
  1215. },
  1216. // 更新代码
  1217. _updateCode: function _updateCode(value) {
  1218. var editor = this.editor;
  1219. var $selectionELem = editor.selection.getSelectionContainerElem();
  1220. if (!$selectionELem) {
  1221. return;
  1222. }
  1223. $selectionELem.html(value);
  1224. editor.selection.restoreSelection();
  1225. },
  1226. // 试图改变 active 状态
  1227. tryChangeActive: function tryChangeActive(e) {
  1228. var editor = this.editor;
  1229. var $elem = this.$elem;
  1230. var $selectionELem = editor.selection.getSelectionContainerElem();
  1231. if (!$selectionELem) {
  1232. return;
  1233. }
  1234. var $parentElem = $selectionELem.parent();
  1235. if ($selectionELem.getNodeName() === 'CODE' && $parentElem.getNodeName() === 'PRE') {
  1236. this._active = true;
  1237. $elem.addClass('w-e-active');
  1238. } else {
  1239. this._active = false;
  1240. $elem.removeClass('w-e-active');
  1241. }
  1242. }
  1243. };
  1244. /*
  1245. menu - table
  1246. */
  1247. // 构造函数
  1248. function Composition(editor) {
  1249. this.editor = editor;
  1250. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-composition">作文</i></div>');
  1251. this.type = "panel";
  1252. //语文作文字数显示1500
  1253. //英语行数限制100
  1254. this.maxNum = {
  1255. en: 100,
  1256. zh: 1500
  1257. // 当前是否 active 状态
  1258. };this._active = false;
  1259. }
  1260. // 原型
  1261. Composition.prototype = {
  1262. constructor: Composition,
  1263. onClick: function onClick() {
  1264. // 插入新表格
  1265. this._createInsertPanel();
  1266. },
  1267. // 创建插入新表格的 panel
  1268. _createInsertPanel: function _createInsertPanel() {
  1269. var _this = this;
  1270. // 用到的 id
  1271. var btnInsertIdC = getRandom("compositionForC");
  1272. var btnInsertIdE = getRandom("compositionForE");
  1273. var chineseNum = getRandom("chinese");
  1274. var englishNum = getRandom("english");
  1275. var panel = new Panel(this, {
  1276. width: 250,
  1277. // panel 包含多个 tab
  1278. tabs: [{
  1279. // 标题
  1280. title: "语文作文",
  1281. // 模板
  1282. tpl: "<div>\n <p style=\"text-align:left; padding:5px 0;\">\n \u5B57\u6570\uFF1A<input type=\"text\" class=\"half\" id=\"" + chineseNum + "\" />\n <a href=\"javascript:;\" id=\"" + btnInsertIdC + "\">\u4F7F\u7528</a>\n </p>\n </div>",
  1283. // 事件绑定
  1284. events: [{
  1285. // 点击按钮,插入表格
  1286. selector: "#" + btnInsertIdC,
  1287. type: "click",
  1288. fn: function fn() {
  1289. var num = parseInt($("#" + chineseNum).val());
  1290. console.log('语文作文字数', num);
  1291. if (num <= _this.maxNum.zh) {
  1292. _this._insert('zh', num);
  1293. }
  1294. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  1295. return true;
  1296. }
  1297. }]
  1298. }, {
  1299. // 标题
  1300. title: "英语作文",
  1301. // 模板
  1302. tpl: "<div>\n <p style=\"text-align:left; padding:5px 0;\">\n \u884C\u6570\uFF1A<input type=\"text\" class=\"half\" id=\"" + englishNum + "\" />\n <a href=\"javascript:;\" id=\"" + btnInsertIdE + "\">\u4F7F\u7528</a>\n </p>\n </div>",
  1303. // 事件绑定
  1304. events: [{
  1305. // 点击按钮,插入表格
  1306. selector: "#" + btnInsertIdE,
  1307. type: "click",
  1308. fn: function fn() {
  1309. var row = parseInt($("#" + englishNum).val());
  1310. console.log('英语作文行数', row);
  1311. if (row <= _this.maxNum.en) {
  1312. _this._insert('en', row);
  1313. }
  1314. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  1315. return true;
  1316. }
  1317. }]
  1318. }] // tabs end
  1319. }); // panel end
  1320. // 展示 panel
  1321. panel.show();
  1322. // 记录属性
  1323. this.panel = panel;
  1324. },
  1325. // 插入表格
  1326. _insert: function _insert(type, num) {
  1327. if (type === 'zh') {
  1328. this._insertForZh(num);
  1329. } else {
  1330. this._insertForZhEn(num);
  1331. }
  1332. },
  1333. _insertForZh: function _insertForZh(num) {
  1334. var editor = this.editor;
  1335. var compositionHtml = '';
  1336. var w = 30;
  1337. var h = 30;
  1338. var contentWidth = editor.$textElem.width();
  1339. var rowLen = Math.floor(contentWidth / w);
  1340. //循环列数
  1341. var columns = Math.ceil(num / rowLen);
  1342. var totalNum = 0;
  1343. //每 markNum 数量标记一下文字个数
  1344. var markNum = 100;
  1345. for (var i = 0; i < columns; i++) {
  1346. var rowHtml = '';
  1347. for (var curNum = 0; curNum < rowLen; curNum++) {
  1348. totalNum++;
  1349. if (!(totalNum % markNum)) {
  1350. rowHtml += '<span><b>' + totalNum + '</b></span>';
  1351. } else {
  1352. rowHtml += '<span></span>';
  1353. }
  1354. }
  1355. compositionHtml += '<div class="composition-column">' + rowHtml + '</div>';
  1356. }
  1357. // 执行命令
  1358. editor.txt.html(compositionHtml);
  1359. $(editor.textSelector).css('height',totalNum*42);
  1360. var position = Print.getPositions();
  1361. localStorage.setItem('position', JSON.stringify(position))
  1362. Print.reInitPageForNewBuild();
  1363. },
  1364. _insertForZhEn: function _insertForZhEn(row) {
  1365. var editor = this.editor;
  1366. var compositionHtml = '';
  1367. //每 markNum 数量标记一下文字个数
  1368. var markNum = 100;
  1369. for (var i = 0; i < row; i++) {
  1370. compositionHtml += '<div class="composition-column en"></div>';
  1371. }
  1372. editor.txt.html(compositionHtml);
  1373. $(editor.textSelector).css('height',row*34);
  1374. var position = Print.getPositions();
  1375. localStorage.setItem('position', JSON.stringify(position))
  1376. Print.reInitPageForNewBuild();
  1377. }
  1378. };
  1379. function Hline(editor) {
  1380. this.editor = editor;
  1381. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-composition">横线</i></div>');
  1382. this.type = "panel";
  1383. //语文作文字数显示1500
  1384. //英语行数限制100
  1385. this.maxNum = {
  1386. en: 100,
  1387. zh: 1500
  1388. // 当前是否 active 状态
  1389. };this._active = false;
  1390. }
  1391. // 原型
  1392. Hline.prototype = {
  1393. constructor: Hline,
  1394. onClick: function onClick() {
  1395. // 插入新表格
  1396. this._createInsertPanel();
  1397. },
  1398. // 创建插入新表格的 panel
  1399. _createInsertPanel: function _createInsertPanel() {
  1400. var _this = this;
  1401. // 用到的 id
  1402. // var btnInsertIdC = getRandom("compositionForC");
  1403. var btnInsertIdE = getRandom("compositionForE");
  1404. // var chineseNum = getRandom("chinese");
  1405. var englishNum = getRandom("english");
  1406. var panel = new Panel(this, {
  1407. width: 250,
  1408. // panel 包含多个 tab
  1409. tabs: [ {
  1410. // 标题
  1411. title: "答题横线",
  1412. // 模板
  1413. tpl: "<div>\n <p style=\"text-align:left; padding:5px 0;\">\n \u884C\u6570\uFF1A<input type=\"text\" class=\"half\" id=\"" + englishNum + "\" />\n <a href=\"javascript:;\" id=\"" + btnInsertIdE + "\">\u4F7F\u7528</a>\n </p>\n </div>",
  1414. // 事件绑定
  1415. events: [{
  1416. // 点击按钮,插入表格
  1417. selector: "#" + btnInsertIdE,
  1418. type: "click",
  1419. fn: function fn() {
  1420. var row = parseInt($("#" + englishNum).val());
  1421. if (row <= _this.maxNum.en) {
  1422. _this._insert('en', row);
  1423. }
  1424. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  1425. return true;
  1426. }
  1427. }]
  1428. }] // tabs end
  1429. }); // panel end
  1430. // 展示 panel
  1431. panel.show();
  1432. // 记录属性
  1433. this.panel = panel;
  1434. },
  1435. // 插入表格
  1436. _insert: function _insert(type, num) {
  1437. if (type === 'zh') {
  1438. this._insertForZh(num);
  1439. } else {
  1440. this._insertForZhEn(num);
  1441. }
  1442. },
  1443. _insertForZhEn: function _insertForZhEn(row) {
  1444. var editor = this.editor;
  1445. var compositionHtml = '';
  1446. //每 markNum 数量标记一下文字个数
  1447. var markNum = 100;
  1448. for (var i = 0; i < row; i++) {
  1449. compositionHtml += '<div class="composition-column en"></div>';
  1450. }
  1451. editor.txt.html(compositionHtml);
  1452. $(editor.textSelector).css('height',row*34);
  1453. var position = Print.getPositions();
  1454. localStorage.setItem('position', JSON.stringify(position))
  1455. Print.reInitPageForNewBuild();
  1456. }
  1457. };
  1458. /*
  1459. menu - emoticon
  1460. */
  1461. // 构造函数
  1462. function Emoticon(editor) {
  1463. this.editor = editor;
  1464. this.$elem = $('<div class="w-e-menu">\n <i class="w-e-icon-happy"></i>\n </div>');
  1465. this.type = 'panel';
  1466. // 当前是否 active 状态
  1467. this._active = false;
  1468. }
  1469. // 原型
  1470. Emoticon.prototype = {
  1471. constructor: Emoticon,
  1472. onClick: function onClick() {
  1473. this._createPanel();
  1474. },
  1475. _createPanel: function _createPanel() {
  1476. var _this = this;
  1477. var editor = this.editor;
  1478. var config = editor.config;
  1479. // 获取表情配置
  1480. var emotions = config.emotions || [];
  1481. // 创建表情 dropPanel 的配置
  1482. var tabConfig = [];
  1483. emotions.forEach(function (emotData) {
  1484. var emotType = emotData.type;
  1485. var content = emotData.content || [];
  1486. // 这一组表情最终拼接出来的 html
  1487. var faceHtml = '';
  1488. // emoji 表情
  1489. if (emotType === 'emoji') {
  1490. content.forEach(function (item) {
  1491. if (item) {
  1492. faceHtml += '<span class="w-e-item">' + item + '</span>';
  1493. }
  1494. });
  1495. }
  1496. // 图片表情
  1497. if (emotType === 'image') {
  1498. content.forEach(function (item) {
  1499. var src = item.src;
  1500. var alt = item.alt;
  1501. if (src) {
  1502. // 加一个 data-w-e 属性,点击图片的时候不再提示编辑图片
  1503. faceHtml += '<span class="w-e-item"><img src="' + src + '" alt="' + alt + '" data-w-e="1"/></span>';
  1504. }
  1505. });
  1506. }
  1507. tabConfig.push({
  1508. title: emotData.title,
  1509. tpl: '<div class="w-e-emoticon-container">' + faceHtml + '</div>',
  1510. events: [{
  1511. selector: 'span.w-e-item',
  1512. type: 'click',
  1513. fn: function fn(e) {
  1514. var target = e.target;
  1515. var $target = $(target);
  1516. var nodeName = $target.getNodeName();
  1517. var insertHtml = void 0;
  1518. if (nodeName === 'IMG') {
  1519. // 插入图片
  1520. insertHtml = $target.parent().html();
  1521. } else {
  1522. // 插入 emoji
  1523. insertHtml = '<span>' + $target.html() + '</span>';
  1524. }
  1525. _this._insert(insertHtml);
  1526. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  1527. return true;
  1528. }
  1529. }]
  1530. });
  1531. });
  1532. var panel = new Panel(this, {
  1533. width: 300,
  1534. height: 200,
  1535. // 一个 Panel 包含多个 tab
  1536. tabs: tabConfig
  1537. });
  1538. // 显示 panel
  1539. panel.show();
  1540. // 记录属性
  1541. this.panel = panel;
  1542. },
  1543. // 插入表情
  1544. _insert: function _insert(emotHtml) {
  1545. var editor = this.editor;
  1546. editor.cmd.do('insertHTML', emotHtml);
  1547. }
  1548. };
  1549. /*
  1550. menu - fontName
  1551. */
  1552. // 构造函数
  1553. function FontName(editor) {
  1554. var _this = this;
  1555. this.editor = editor;
  1556. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-font"></i></div>');
  1557. this.type = 'droplist';
  1558. // 当前是否 active 状态
  1559. this._active = false;
  1560. // 获取配置的字体
  1561. var config = editor.config;
  1562. var fontNames = config.fontNames || [];
  1563. // 初始化 droplist
  1564. this.droplist = new DropList(this, {
  1565. width: 100,
  1566. $title: $('<p>字体</p>'),
  1567. type: 'list', // droplist 以列表形式展示
  1568. list: fontNames.map(function (fontName) {
  1569. return { $elem: $('<span style="font-family: ' + fontName + ';">' + fontName + '</span>'), value: fontName };
  1570. }),
  1571. onClick: function onClick(value) {
  1572. // 注意 this 是指向当前的 FontName 对象
  1573. _this._command(value);
  1574. }
  1575. });
  1576. }
  1577. // 原型
  1578. FontName.prototype = {
  1579. constructor: FontName,
  1580. _command: function _command(value) {
  1581. var editor = this.editor;
  1582. editor.cmd.do('fontName', value);
  1583. }
  1584. };
  1585. /*
  1586. menu - fontSize
  1587. */
  1588. // 构造函数
  1589. function FontSize(editor) {
  1590. var _this = this;
  1591. this.editor = editor;
  1592. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-text-heigh"></i></div>');
  1593. this.type = 'droplist';
  1594. // 当前是否 active 状态
  1595. this._active = false;
  1596. // 初始化 droplist
  1597. this.droplist = new DropList(this, {
  1598. width: 160,
  1599. $title: $('<p>字号</p>'),
  1600. type: 'list', // droplist 以列表形式展示
  1601. list: [{ $elem: $('<span style="font-size: x-small;">x-small</span>'), value: '1' }, { $elem: $('<span style="font-size: small;">small</span>'), value: '2' }, { $elem: $('<span>normal</span>'), value: '3' }, { $elem: $('<span style="font-size: large;">large</span>'), value: '4' }, { $elem: $('<span style="font-size: x-large;">x-large</span>'), value: '5' }, { $elem: $('<span style="font-size: xx-large;">xx-large</span>'), value: '6' }],
  1602. onClick: function onClick(value) {
  1603. // 注意 this 是指向当前的 FontSize 对象
  1604. _this._command(value);
  1605. }
  1606. });
  1607. }
  1608. // 原型
  1609. FontSize.prototype = {
  1610. constructor: FontSize,
  1611. // 执行命令
  1612. _command: function _command(value) {
  1613. var editor = this.editor;
  1614. editor.cmd.do('fontSize', value);
  1615. }
  1616. };
  1617. /*
  1618. menu - Forecolor
  1619. */
  1620. // 构造函数
  1621. function ForeColor(editor) {
  1622. var _this = this;
  1623. this.editor = editor;
  1624. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-pencil2"></i></div>');
  1625. this.type = 'droplist';
  1626. // 获取配置的颜色
  1627. var config = editor.config;
  1628. var colors = config.colors || [];
  1629. // 当前是否 active 状态
  1630. this._active = false;
  1631. // 初始化 droplist
  1632. this.droplist = new DropList(this, {
  1633. width: 120,
  1634. $title: $('<p>文字颜色</p>'),
  1635. type: 'inline-block', // droplist 内容以 block 形式展示
  1636. list: colors.map(function (color) {
  1637. return { $elem: $('<i style="color:' + color + ';" class="w-e-icon-pencil2"></i>'), value: color };
  1638. }),
  1639. onClick: function onClick(value) {
  1640. // 注意 this 是指向当前的 ForeColor 对象
  1641. _this._command(value);
  1642. }
  1643. });
  1644. }
  1645. // 原型
  1646. ForeColor.prototype = {
  1647. constructor: ForeColor,
  1648. // 执行命令
  1649. _command: function _command(value) {
  1650. var editor = this.editor;
  1651. editor.cmd.do('foreColor', value);
  1652. }
  1653. };
  1654. /*
  1655. menu - table
  1656. */
  1657. // 构造函数
  1658. function Formula(editor) {
  1659. this.editor = editor;
  1660. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-formula">∑</i></div>');
  1661. this.formulaBox = document.getElementById('formulaBox');
  1662. this.formulaContainer = document.getElementById('kfEditorContainer');
  1663. this.$ensureBtn = document.getElementById('kf-formula-ensure');
  1664. this.$cancelBtn = document.getElementById('kf-formula-cancel');
  1665. this.type = 'click';
  1666. // 当前是否 active 状态
  1667. this._active = false;
  1668. }
  1669. // 原型
  1670. Formula.prototype = {
  1671. constructor: Formula,
  1672. onClick: function onClick() {
  1673. var self = this;
  1674. //显示公式编辑器
  1675. self.formulaBox.style.display = 'block';
  1676. if (window.kfe) {
  1677. window.kfe.execCommand("render", null || "\\placeholder");
  1678. } else {
  1679. window.factory = window.kf.EditorFactory.create(self.formulaContainer, {
  1680. render: {
  1681. fontsize: 24
  1682. },
  1683. resource: {
  1684. path: "./kityformula/resource/"
  1685. }
  1686. });
  1687. window.factory.ready(function (KFEditor) {
  1688. KFEditor.execCommand("render", null || "\\placeholder");
  1689. KFEditor.execCommand("focus");
  1690. window.kfe = KFEditor;
  1691. });
  1692. }
  1693. self.$ensureBtn.onclick = function () {
  1694. window.kfe.execCommand('get.image.data', function (data) {
  1695. var latex = window.kfe.execCommand('get.source');
  1696. self.editor.cmd.do("insertHTML", '<img class="kfformula" src="' + data.img + '" data-latex="' + latex + '" />');
  1697. return true;
  1698. });
  1699. self.formulaBox.style.display = 'none';
  1700. return false;
  1701. };
  1702. self.$cancelBtn.onclick = function () {
  1703. self.formulaBox.style.display = 'none';
  1704. };
  1705. }
  1706. };
  1707. /*
  1708. menu - header
  1709. */
  1710. // 构造函数
  1711. function Head(editor) {
  1712. var _this = this;
  1713. this.editor = editor;
  1714. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-header"></i></div>');
  1715. this.type = 'droplist';
  1716. // 当前是否 active 状态
  1717. this._active = false;
  1718. // 初始化 droplist
  1719. this.droplist = new DropList(this, {
  1720. width: 100,
  1721. $title: $('<p>设置标题</p>'),
  1722. type: 'list', // droplist 以列表形式展示
  1723. list: [{ $elem: $('<h1>H1</h1>'), value: '<h1>' }, { $elem: $('<h2>H2</h2>'), value: '<h2>' }, { $elem: $('<h3>H3</h3>'), value: '<h3>' }, { $elem: $('<h4>H4</h4>'), value: '<h4>' }, { $elem: $('<h5>H5</h5>'), value: '<h5>' }, { $elem: $('<p>正文</p>'), value: '<p>' }],
  1724. onClick: function onClick(value) {
  1725. // 注意 this 是指向当前的 Head 对象
  1726. _this._command(value);
  1727. }
  1728. });
  1729. }
  1730. // 原型
  1731. Head.prototype = {
  1732. constructor: Head,
  1733. // 执行命令
  1734. _command: function _command(value) {
  1735. var editor = this.editor;
  1736. var $selectionElem = editor.selection.getSelectionContainerElem();
  1737. if (editor.$textElem.equal($selectionElem)) {
  1738. // 不能选中多行来设置标题,否则会出现问题
  1739. // 例如选中的是 <p>xxx</p><p>yyy</p> 来设置标题,设置之后会成为 <h1>xxx<br>yyy</h1> 不符合预期
  1740. return;
  1741. }
  1742. editor.cmd.do('formatBlock', value);
  1743. },
  1744. // 试图改变 active 状态
  1745. tryChangeActive: function tryChangeActive(e) {
  1746. var editor = this.editor;
  1747. var $elem = this.$elem;
  1748. var reg = /^h/i;
  1749. var cmdValue = editor.cmd.queryCommandValue('formatBlock');
  1750. if (reg.test(cmdValue)) {
  1751. this._active = true;
  1752. $elem.addClass('w-e-active');
  1753. } else {
  1754. this._active = false;
  1755. $elem.removeClass('w-e-active');
  1756. }
  1757. }
  1758. };
  1759. /*
  1760. menu - img
  1761. */
  1762. // 构造函数
  1763. function Image(editor) {
  1764. this.editor = editor;
  1765. var imgMenuId = getRandom('w-e-img');
  1766. this.$elem = $('<div class="w-e-menu" id="' + imgMenuId + '"><i class="w-e-icon-image"></i></div>');
  1767. editor.imgMenuId = imgMenuId;
  1768. this.type = 'panel';
  1769. // 当前是否 active 状态
  1770. this._active = false;
  1771. }
  1772. // 原型
  1773. Image.prototype = {
  1774. constructor: Image,
  1775. onClick: function onClick() {
  1776. var editor = this.editor;
  1777. var config = editor.config;
  1778. if (config.qiniu) {
  1779. return;
  1780. }
  1781. if (this._active) {
  1782. this._createEditPanel();
  1783. } else {
  1784. this._createInsertPanel();
  1785. }
  1786. },
  1787. _createEditPanel: function _createEditPanel() {
  1788. var editor = this.editor;
  1789. // id
  1790. var width30 = getRandom('width-30');
  1791. var width50 = getRandom('width-50');
  1792. var width100 = getRandom('width-100');
  1793. var delBtn = getRandom('del-btn');
  1794. // tab 配置
  1795. var tabsConfig = [{
  1796. title: '编辑图片',
  1797. tpl: '<div>\n <div class="w-e-button-container" style="border-bottom:1px solid #f1f1f1;padding-bottom:5px;margin-bottom:5px;">\n <span style="float:left;font-size:14px;margin:4px 5px 0 5px;color:#333;">\u6700\u5927\u5BBD\u5EA6\uFF1A</span>\n <button id="' + width30 + '" class="left">30%</button>\n <button id="' + width50 + '" class="left">50%</button>\n <button id="' + width100 + '" class="left">100%</button>\n </div>\n <div class="w-e-button-container">\n <button id="' + delBtn + '" class="gray left">\u5220\u9664\u56FE\u7247</button>\n </dv>\n </div>',
  1798. events: [{
  1799. selector: '#' + width30,
  1800. type: 'click',
  1801. fn: function fn() {
  1802. var $img = editor._selectedImg;
  1803. if ($img) {
  1804. $img.css('max-width', '30%');
  1805. }
  1806. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  1807. return true;
  1808. }
  1809. }, {
  1810. selector: '#' + width50,
  1811. type: 'click',
  1812. fn: function fn() {
  1813. var $img = editor._selectedImg;
  1814. if ($img) {
  1815. $img.css('max-width', '50%');
  1816. }
  1817. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  1818. return true;
  1819. }
  1820. }, {
  1821. selector: '#' + width100,
  1822. type: 'click',
  1823. fn: function fn() {
  1824. var $img = editor._selectedImg;
  1825. if ($img) {
  1826. $img.css('max-width', '100%');
  1827. }
  1828. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  1829. return true;
  1830. }
  1831. }, {
  1832. selector: '#' + delBtn,
  1833. type: 'click',
  1834. fn: function fn() {
  1835. var $img = editor._selectedImg;
  1836. if ($img) {
  1837. $img.remove();
  1838. }
  1839. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  1840. return true;
  1841. }
  1842. }]
  1843. }];
  1844. // 创建 panel 并显示
  1845. var panel = new Panel(this, {
  1846. width: 300,
  1847. tabs: tabsConfig
  1848. });
  1849. panel.show();
  1850. // 记录属性
  1851. this.panel = panel;
  1852. },
  1853. _createInsertPanel: function _createInsertPanel() {
  1854. var editor = this.editor;
  1855. var uploadImg = editor.uploadImg;
  1856. var config = editor.config;
  1857. // id
  1858. var upTriggerId = getRandom('up-trigger');
  1859. var upFileId = getRandom('up-file');
  1860. var linkUrlId = getRandom('link-url');
  1861. var linkBtnId = getRandom('link-btn');
  1862. // tabs 的配置
  1863. var tabsConfig = [{
  1864. title: '上传图片',
  1865. tpl: '<div class="w-e-up-img-container">\n <div id="' + upTriggerId + '" class="w-e-up-btn">\n <i class="w-e-icon-upload2"></i>\n </div>\n <div style="display:none;">\n <input id="' + upFileId + '" type="file" multiple="multiple" accept="image/*"/>\n </div>\n </div>',
  1866. events: [{
  1867. // 触发选择图片
  1868. selector: '#' + upTriggerId,
  1869. type: 'click',
  1870. fn: function fn() {
  1871. var $file = $('#' + upFileId);
  1872. var fileElem = $file[0];
  1873. if (fileElem) {
  1874. fileElem.click();
  1875. } else {
  1876. // 返回 true 可关闭 panel
  1877. return true;
  1878. }
  1879. }
  1880. }, {
  1881. // 选择图片完毕
  1882. selector: '#' + upFileId,
  1883. type: 'change',
  1884. fn: function fn() {
  1885. var $file = $('#' + upFileId);
  1886. var fileElem = $file[0];
  1887. if (!fileElem) {
  1888. // 返回 true 可关闭 panel
  1889. return true;
  1890. }
  1891. // 获取选中的 file 对象列表
  1892. var fileList = fileElem.files;
  1893. if (fileList.length) {
  1894. uploadImg.uploadImg(fileList);
  1895. }
  1896. // 返回 true 可关闭 panel
  1897. return true;
  1898. }
  1899. }]
  1900. }, // first tab end
  1901. {
  1902. title: '网络图片',
  1903. tpl: '<div>\n <input id="' + linkUrlId + '" type="text" class="block" placeholder="\u56FE\u7247\u94FE\u63A5"/></td>\n <div class="w-e-button-container">\n <button id="' + linkBtnId + '" class="right">\u63D2\u5165</button>\n </div>\n </div>',
  1904. events: [{
  1905. selector: '#' + linkBtnId,
  1906. type: 'click',
  1907. fn: function fn() {
  1908. var $linkUrl = $('#' + linkUrlId);
  1909. var url = $linkUrl.val().trim();
  1910. if (url) {
  1911. uploadImg.insertLinkImg(url);
  1912. }
  1913. // 返回 true 表示函数执行结束之后关闭 panel
  1914. return true;
  1915. }
  1916. }] // second tab end
  1917. }]; // tabs end
  1918. // 判断 tabs 的显示
  1919. var tabsConfigResult = [];
  1920. if ((config.uploadImgShowBase64 || config.uploadImgServer || config.customUploadImg) && window.FileReader) {
  1921. // 显示“上传图片”
  1922. tabsConfigResult.push(tabsConfig[0]);
  1923. }
  1924. if (config.showLinkImg) {
  1925. // 显示“网络图片”
  1926. tabsConfigResult.push(tabsConfig[1]);
  1927. }
  1928. // 创建 panel 并显示
  1929. var panel = new Panel(this, {
  1930. width: 300,
  1931. tabs: tabsConfigResult
  1932. });
  1933. panel.show();
  1934. // 记录属性
  1935. this.panel = panel;
  1936. },
  1937. // 试图改变 active 状态
  1938. tryChangeActive: function tryChangeActive(e) {
  1939. var editor = this.editor;
  1940. var $elem = this.$elem;
  1941. if (editor._selectedImg) {
  1942. this._active = true;
  1943. $elem.addClass('w-e-active');
  1944. } else {
  1945. this._active = false;
  1946. $elem.removeClass('w-e-active');
  1947. }
  1948. }
  1949. };
  1950. /*
  1951. italic-menu
  1952. */
  1953. // 构造函数
  1954. function Italic(editor) {
  1955. this.editor = editor;
  1956. this.$elem = $('<div class="w-e-menu">\n <i class="w-e-icon-italic"></i>\n </div>');
  1957. this.type = 'click';
  1958. // 当前是否 active 状态
  1959. this._active = false;
  1960. }
  1961. // 原型
  1962. Italic.prototype = {
  1963. constructor: Italic,
  1964. // 点击事件
  1965. onClick: function onClick(e) {
  1966. // 点击菜单将触发这里
  1967. var editor = this.editor;
  1968. var isSeleEmpty = editor.selection.isSelectionEmpty();
  1969. if (isSeleEmpty) {
  1970. // 选区是空的,插入并选中一个“空白”
  1971. editor.selection.createEmptyRange();
  1972. }
  1973. // 执行 italic 命令
  1974. editor.cmd.do('italic');
  1975. if (isSeleEmpty) {
  1976. // 需要将选取折叠起来
  1977. editor.selection.collapseRange();
  1978. editor.selection.restoreSelection();
  1979. }
  1980. },
  1981. // 试图改变 active 状态
  1982. tryChangeActive: function tryChangeActive(e) {
  1983. var editor = this.editor;
  1984. var $elem = this.$elem;
  1985. if (editor.cmd.queryCommandState('italic')) {
  1986. this._active = true;
  1987. $elem.addClass('w-e-active');
  1988. } else {
  1989. this._active = false;
  1990. $elem.removeClass('w-e-active');
  1991. }
  1992. }
  1993. };
  1994. /*
  1995. menu - justify
  1996. */
  1997. // 构造函数
  1998. function Justify(editor) {
  1999. var _this = this;
  2000. this.editor = editor;
  2001. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-paragraph-left"></i></div>');
  2002. this.type = 'droplist';
  2003. // 当前是否 active 状态
  2004. this._active = false;
  2005. // 初始化 droplist
  2006. this.droplist = new DropList(this, {
  2007. width: 100,
  2008. $title: $('<p>对齐方式</p>'),
  2009. type: 'list', // droplist 以列表形式展示
  2010. list: [{ $elem: $('<span><i class="w-e-icon-paragraph-left"></i> 靠左</span>'), value: 'justifyLeft' }, { $elem: $('<span><i class="w-e-icon-paragraph-center"></i> 居中</span>'), value: 'justifyCenter' }, { $elem: $('<span><i class="w-e-icon-paragraph-right"></i> 靠右</span>'), value: 'justifyRight' }],
  2011. onClick: function onClick(value) {
  2012. // 注意 this 是指向当前的 List 对象
  2013. _this._command(value);
  2014. }
  2015. });
  2016. }
  2017. // 原型
  2018. Justify.prototype = {
  2019. constructor: Justify,
  2020. // 执行命令
  2021. _command: function _command(value) {
  2022. var editor = this.editor;
  2023. editor.cmd.do(value);
  2024. }
  2025. };
  2026. /*
  2027. menu - link
  2028. */
  2029. // 构造函数
  2030. function Link(editor) {
  2031. this.editor = editor;
  2032. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-link"></i></div>');
  2033. this.type = 'panel';
  2034. // 当前是否 active 状态
  2035. this._active = false;
  2036. }
  2037. // 原型
  2038. Link.prototype = {
  2039. constructor: Link,
  2040. // 点击事件
  2041. onClick: function onClick(e) {
  2042. var editor = this.editor;
  2043. var $linkelem = void 0;
  2044. if (this._active) {
  2045. // 当前选区在链接里面
  2046. $linkelem = editor.selection.getSelectionContainerElem();
  2047. if (!$linkelem) {
  2048. return;
  2049. }
  2050. // 将该元素都包含在选取之内,以便后面整体替换
  2051. editor.selection.createRangeByElem($linkelem);
  2052. editor.selection.restoreSelection();
  2053. // 显示 panel
  2054. this._createPanel($linkelem.text(), $linkelem.attr('href'));
  2055. } else {
  2056. // 当前选区不在链接里面
  2057. if (editor.selection.isSelectionEmpty()) {
  2058. // 选区是空的,未选中内容
  2059. this._createPanel('', '');
  2060. } else {
  2061. // 选中内容了
  2062. this._createPanel(editor.selection.getSelectionText(), '');
  2063. }
  2064. }
  2065. },
  2066. // 创建 panel
  2067. _createPanel: function _createPanel(text, link) {
  2068. var _this = this;
  2069. // panel 中需要用到的id
  2070. var inputLinkId = getRandom('input-link');
  2071. var inputTextId = getRandom('input-text');
  2072. var btnOkId = getRandom('btn-ok');
  2073. var btnDelId = getRandom('btn-del');
  2074. // 是否显示“删除链接”
  2075. var delBtnDisplay = this._active ? 'inline-block' : 'none';
  2076. // 初始化并显示 panel
  2077. var panel = new Panel(this, {
  2078. width: 300,
  2079. // panel 中可包含多个 tab
  2080. tabs: [{
  2081. // tab 的标题
  2082. title: '链接',
  2083. // 模板
  2084. tpl: '<div>\n <input id="' + inputTextId + '" type="text" class="block" value="' + text + '" placeholder="\u94FE\u63A5\u6587\u5B57"/></td>\n <input id="' + inputLinkId + '" type="text" class="block" value="' + link + '" placeholder="http://..."/></td>\n <div class="w-e-button-container">\n <button id="' + btnOkId + '" class="right">\u63D2\u5165</button>\n <button id="' + btnDelId + '" class="gray right" style="display:' + delBtnDisplay + '">\u5220\u9664\u94FE\u63A5</button>\n </div>\n </div>',
  2085. // 事件绑定
  2086. events: [
  2087. // 插入链接
  2088. {
  2089. selector: '#' + btnOkId,
  2090. type: 'click',
  2091. fn: function fn() {
  2092. // 执行插入链接
  2093. var $link = $('#' + inputLinkId);
  2094. var $text = $('#' + inputTextId);
  2095. var link = $link.val();
  2096. var text = $text.val();
  2097. _this._insertLink(text, link);
  2098. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  2099. return true;
  2100. }
  2101. },
  2102. // 删除链接
  2103. {
  2104. selector: '#' + btnDelId,
  2105. type: 'click',
  2106. fn: function fn() {
  2107. // 执行删除链接
  2108. _this._delLink();
  2109. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  2110. return true;
  2111. }
  2112. }] // tab end
  2113. }] // tabs end
  2114. });
  2115. // 显示 panel
  2116. panel.show();
  2117. // 记录属性
  2118. this.panel = panel;
  2119. },
  2120. // 删除当前链接
  2121. _delLink: function _delLink() {
  2122. if (!this._active) {
  2123. return;
  2124. }
  2125. var editor = this.editor;
  2126. var $selectionELem = editor.selection.getSelectionContainerElem();
  2127. if (!$selectionELem) {
  2128. return;
  2129. }
  2130. var selectionText = editor.selection.getSelectionText();
  2131. editor.cmd.do('insertHTML', '<span>' + selectionText + '</span>');
  2132. },
  2133. // 插入链接
  2134. _insertLink: function _insertLink(text, link) {
  2135. var editor = this.editor;
  2136. var config = editor.config;
  2137. var linkCheck = config.linkCheck;
  2138. var checkResult = true; // 默认为 true
  2139. if (linkCheck && typeof linkCheck === 'function') {
  2140. checkResult = linkCheck(text, link);
  2141. }
  2142. if (checkResult === true) {
  2143. editor.cmd.do('insertHTML', '<a href="' + link + '" target="_blank">' + text + '</a>');
  2144. } else {
  2145. alert(checkResult);
  2146. }
  2147. },
  2148. // 试图改变 active 状态
  2149. tryChangeActive: function tryChangeActive(e) {
  2150. var editor = this.editor;
  2151. var $elem = this.$elem;
  2152. var $selectionELem = editor.selection.getSelectionContainerElem();
  2153. if (!$selectionELem) {
  2154. return;
  2155. }
  2156. if ($selectionELem.getNodeName() === 'A') {
  2157. this._active = true;
  2158. $elem.addClass('w-e-active');
  2159. } else {
  2160. this._active = false;
  2161. $elem.removeClass('w-e-active');
  2162. }
  2163. }
  2164. };
  2165. /*
  2166. menu - list
  2167. */
  2168. // 构造函数
  2169. function List(editor) {
  2170. var _this = this;
  2171. this.editor = editor;
  2172. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-list2"></i></div>');
  2173. this.type = 'droplist';
  2174. // 当前是否 active 状态
  2175. this._active = false;
  2176. // 初始化 droplist
  2177. this.droplist = new DropList(this, {
  2178. width: 120,
  2179. $title: $('<p>设置列表</p>'),
  2180. type: 'list', // droplist 以列表形式展示
  2181. list: [{ $elem: $('<span><i class="w-e-icon-list-numbered"></i> 有序列表</span>'), value: 'insertOrderedList' }, { $elem: $('<span><i class="w-e-icon-list2"></i> 无序列表</span>'), value: 'insertUnorderedList' }],
  2182. onClick: function onClick(value) {
  2183. // 注意 this 是指向当前的 List 对象
  2184. _this._command(value);
  2185. }
  2186. });
  2187. }
  2188. // 原型
  2189. List.prototype = {
  2190. constructor: List,
  2191. // 执行命令
  2192. _command: function _command(value) {
  2193. var editor = this.editor;
  2194. var $textElem = editor.$textElem;
  2195. editor.selection.restoreSelection();
  2196. if (editor.cmd.queryCommandState(value)) {
  2197. return;
  2198. }
  2199. editor.cmd.do(value);
  2200. // 验证列表是否被包裹在 <p> 之内
  2201. var $selectionElem = editor.selection.getSelectionContainerElem();
  2202. if ($selectionElem.getNodeName() === 'LI') {
  2203. $selectionElem = $selectionElem.parent();
  2204. }
  2205. if (/^ol|ul$/i.test($selectionElem.getNodeName()) === false) {
  2206. return;
  2207. }
  2208. if ($selectionElem.equal($textElem)) {
  2209. // 证明是顶级标签,没有被 <p> 包裹
  2210. return;
  2211. }
  2212. var $parent = $selectionElem.parent();
  2213. if ($parent.equal($textElem)) {
  2214. // $parent 是顶级标签,不能删除
  2215. return;
  2216. }
  2217. $selectionElem.insertAfter($parent);
  2218. $parent.remove();
  2219. },
  2220. // 试图改变 active 状态
  2221. tryChangeActive: function tryChangeActive(e) {
  2222. var editor = this.editor;
  2223. var $elem = this.$elem;
  2224. if (editor.cmd.queryCommandState('insertUnOrderedList') || editor.cmd.queryCommandState('insertOrderedList')) {
  2225. this._active = true;
  2226. $elem.addClass('w-e-active');
  2227. } else {
  2228. this._active = false;
  2229. $elem.removeClass('w-e-active');
  2230. }
  2231. }
  2232. };
  2233. /*
  2234. menu - quote
  2235. */
  2236. // 构造函数
  2237. function Quote(editor) {
  2238. this.editor = editor;
  2239. this.$elem = $('<div class="w-e-menu">\n <i class="w-e-icon-quotes-left"></i>\n </div>');
  2240. this.type = 'click';
  2241. // 当前是否 active 状态
  2242. this._active = false;
  2243. }
  2244. // 原型
  2245. Quote.prototype = {
  2246. constructor: Quote,
  2247. onClick: function onClick(e) {
  2248. var editor = this.editor;
  2249. var $selectionElem = editor.selection.getSelectionContainerElem();
  2250. var nodeName = $selectionElem.getNodeName();
  2251. if (!UA.isIE()) {
  2252. if (nodeName === 'BLOCKQUOTE') {
  2253. // 撤销 quote
  2254. editor.cmd.do('formatBlock', '<P>');
  2255. } else {
  2256. // 转换为 quote
  2257. editor.cmd.do('formatBlock', '<BLOCKQUOTE>');
  2258. }
  2259. return;
  2260. }
  2261. // IE 中不支持 formatBlock <BLOCKQUOTE> ,要用其他方式兼容
  2262. var content = void 0,
  2263. $targetELem = void 0;
  2264. if (nodeName === 'P') {
  2265. // 将 P 转换为 quote
  2266. content = $selectionElem.text();
  2267. $targetELem = $('<blockquote>' + content + '</blockquote>');
  2268. $targetELem.insertAfter($selectionElem);
  2269. $selectionElem.remove();
  2270. return;
  2271. }
  2272. if (nodeName === 'BLOCKQUOTE') {
  2273. // 撤销 quote
  2274. content = $selectionElem.text();
  2275. $targetELem = $('<p>' + content + '</p>');
  2276. $targetELem.insertAfter($selectionElem);
  2277. $selectionElem.remove();
  2278. }
  2279. },
  2280. tryChangeActive: function tryChangeActive(e) {
  2281. var editor = this.editor;
  2282. var $elem = this.$elem;
  2283. var reg = /^BLOCKQUOTE$/i;
  2284. var cmdValue = editor.cmd.queryCommandValue('formatBlock');
  2285. if (reg.test(cmdValue)) {
  2286. this._active = true;
  2287. $elem.addClass('w-e-active');
  2288. } else {
  2289. this._active = false;
  2290. $elem.removeClass('w-e-active');
  2291. }
  2292. }
  2293. };
  2294. /*
  2295. redo-menu
  2296. */
  2297. // 构造函数
  2298. function Redo(editor) {
  2299. this.editor = editor;
  2300. this.$elem = $('<div class="w-e-menu">\n <i class="w-e-icon-redo"></i>\n </div>');
  2301. this.type = 'click';
  2302. // 当前是否 active 状态
  2303. this._active = false;
  2304. }
  2305. // 原型
  2306. Redo.prototype = {
  2307. constructor: Redo,
  2308. // 点击事件
  2309. onClick: function onClick(e) {
  2310. // 点击菜单将触发这里
  2311. var editor = this.editor;
  2312. // 执行 redo 命令
  2313. editor.cmd.do('redo');
  2314. }
  2315. };
  2316. /*
  2317. strikeThrough-menu
  2318. */
  2319. // 构造函数
  2320. function StrikeThrough(editor) {
  2321. this.editor = editor;
  2322. this.$elem = $('<div class="w-e-menu">\n <i class="w-e-icon-strikethrough"></i>\n </div>');
  2323. this.type = 'click';
  2324. // 当前是否 active 状态
  2325. this._active = false;
  2326. }
  2327. // 原型
  2328. StrikeThrough.prototype = {
  2329. constructor: StrikeThrough,
  2330. // 点击事件
  2331. onClick: function onClick(e) {
  2332. // 点击菜单将触发这里
  2333. var editor = this.editor;
  2334. var isSeleEmpty = editor.selection.isSelectionEmpty();
  2335. if (isSeleEmpty) {
  2336. // 选区是空的,插入并选中一个“空白”
  2337. editor.selection.createEmptyRange();
  2338. }
  2339. // 执行 strikeThrough 命令
  2340. editor.cmd.do('strikeThrough');
  2341. if (isSeleEmpty) {
  2342. // 需要将选取折叠起来
  2343. editor.selection.collapseRange();
  2344. editor.selection.restoreSelection();
  2345. }
  2346. },
  2347. // 试图改变 active 状态
  2348. tryChangeActive: function tryChangeActive(e) {
  2349. var editor = this.editor;
  2350. var $elem = this.$elem;
  2351. if (editor.cmd.queryCommandState('strikeThrough')) {
  2352. this._active = true;
  2353. $elem.addClass('w-e-active');
  2354. } else {
  2355. this._active = false;
  2356. $elem.removeClass('w-e-active');
  2357. }
  2358. }
  2359. };
  2360. /*
  2361. menu - table
  2362. */
  2363. // 构造函数
  2364. function Table(editor) {
  2365. this.editor = editor;
  2366. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-table2"></i></div>');
  2367. this.type = "panel";
  2368. // 当前是否 active 状态
  2369. this._active = false;
  2370. }
  2371. // 原型
  2372. Table.prototype = {
  2373. constructor: Table,
  2374. onClick: function onClick() {
  2375. if (this._active) {
  2376. // 编辑现有表格
  2377. this._createEditPanel();
  2378. } else {
  2379. // 插入新表格
  2380. this._createInsertPanel();
  2381. }
  2382. },
  2383. // 创建插入新表格的 panel
  2384. _createInsertPanel: function _createInsertPanel() {
  2385. var _this = this;
  2386. // 用到的 id
  2387. var btnInsertId = getRandom("btn");
  2388. var textRowNum = getRandom("row");
  2389. var textColNum = getRandom("col");
  2390. var panel = new Panel(this, {
  2391. width: 250,
  2392. // panel 包含多个 tab
  2393. tabs: [{
  2394. // 标题
  2395. title: "插入表格",
  2396. // 模板
  2397. tpl: "<div>\n <p style=\"text-align:left; padding:5px 0;\">\n \u521B\u5EFA\n <input id=\"" + textRowNum + "\" type=\"text\" value=\"5\" style=\"width:40px;text-align:center;\"/>\n \u884C\n <input id=\"" + textColNum + "\" type=\"text\" value=\"5\" style=\"width:40px;text-align:center;\"/>\n \u5217\u7684\u8868\u683C\n </p>\n <div class=\"w-e-button-container\">\n <button id=\"" + btnInsertId + "\" class=\"right\">\u63D2\u5165</button>\n </div>\n </div>",
  2398. // 事件绑定
  2399. events: [{
  2400. // 点击按钮,插入表格
  2401. selector: "#" + btnInsertId,
  2402. type: "click",
  2403. fn: function fn() {
  2404. var rowNum = parseInt($("#" + textRowNum).val());
  2405. var colNum = parseInt($("#" + textColNum).val());
  2406. if (rowNum && colNum && rowNum > 0 && colNum > 0) {
  2407. // form 数据有效
  2408. _this._insert(rowNum, colNum);
  2409. }
  2410. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  2411. return true;
  2412. }
  2413. }] // first tab end
  2414. }] // tabs end
  2415. }); // panel end
  2416. // 展示 panel
  2417. panel.show();
  2418. // 记录属性
  2419. this.panel = panel;
  2420. },
  2421. // 插入表格
  2422. _insert: function _insert(rowNum, colNum) {
  2423. // 拼接 table 模板
  2424. var r = void 0,
  2425. c = void 0;
  2426. var html = '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
  2427. for (r = 0; r < rowNum; r++) {
  2428. html += "<tr>";
  2429. if (r === 0) {
  2430. for (c = 0; c < colNum; c++) {
  2431. html += "<th>&nbsp;</th>";
  2432. }
  2433. } else {
  2434. for (c = 0; c < colNum; c++) {
  2435. html += "<td>&nbsp;</td>";
  2436. }
  2437. }
  2438. html += "</tr>";
  2439. }
  2440. html += "</table><p><br></p>";
  2441. // 执行命令
  2442. var editor = this.editor;
  2443. editor.cmd.do("insertHTML", html);
  2444. // 防止 firefox 下出现 resize 的控制点
  2445. editor.cmd.do("enableObjectResizing", false);
  2446. editor.cmd.do("enableInlineTableEditing", false);
  2447. },
  2448. // 创建编辑表格的 panel
  2449. _createEditPanel: function _createEditPanel() {
  2450. var _this2 = this;
  2451. // 可用的 id
  2452. var addRowBtnId = getRandom("add-row");
  2453. var addColBtnId = getRandom("add-col");
  2454. var delRowBtnId = getRandom("del-row");
  2455. var delColBtnId = getRandom("del-col");
  2456. var delTableBtnId = getRandom("del-table");
  2457. // 创建 panel 对象
  2458. var panel = new Panel(this, {
  2459. width: 320,
  2460. // panel 包含多个 tab
  2461. tabs: [{
  2462. // 标题
  2463. title: "编辑表格",
  2464. // 模板
  2465. tpl: "<div>\n <div class=\"w-e-button-container\" style=\"border-bottom:1px solid #f1f1f1;padding-bottom:5px;margin-bottom:5px;\">\n <button id=\"" + addRowBtnId + "\" class=\"left\">\u589E\u52A0\u884C</button>\n <button id=\"" + delRowBtnId + "\" class=\"red left\">\u5220\u9664\u884C</button>\n <button id=\"" + addColBtnId + "\" class=\"left\">\u589E\u52A0\u5217</button>\n <button id=\"" + delColBtnId + "\" class=\"red left\">\u5220\u9664\u5217</button>\n </div>\n <div class=\"w-e-button-container\">\n <button id=\"" + delTableBtnId + "\" class=\"gray left\">\u5220\u9664\u8868\u683C</button>\n </dv>\n </div>",
  2466. // 事件绑定
  2467. events: [{
  2468. // 增加行
  2469. selector: "#" + addRowBtnId,
  2470. type: "click",
  2471. fn: function fn() {
  2472. _this2._addRow();
  2473. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  2474. return true;
  2475. }
  2476. }, {
  2477. // 增加列
  2478. selector: "#" + addColBtnId,
  2479. type: "click",
  2480. fn: function fn() {
  2481. _this2._addCol();
  2482. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  2483. return true;
  2484. }
  2485. }, {
  2486. // 删除行
  2487. selector: "#" + delRowBtnId,
  2488. type: "click",
  2489. fn: function fn() {
  2490. _this2._delRow();
  2491. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  2492. return true;
  2493. }
  2494. }, {
  2495. // 删除列
  2496. selector: "#" + delColBtnId,
  2497. type: "click",
  2498. fn: function fn() {
  2499. _this2._delCol();
  2500. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  2501. return true;
  2502. }
  2503. }, {
  2504. // 删除表格
  2505. selector: "#" + delTableBtnId,
  2506. type: "click",
  2507. fn: function fn() {
  2508. _this2._delTable();
  2509. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  2510. return true;
  2511. }
  2512. }]
  2513. }]
  2514. });
  2515. // 显示 panel
  2516. panel.show();
  2517. },
  2518. // 获取选中的单元格的位置信息
  2519. _getLocationData: function _getLocationData() {
  2520. var result = {};
  2521. var editor = this.editor;
  2522. var $selectionELem = editor.selection.getSelectionContainerElem();
  2523. if (!$selectionELem) {
  2524. return;
  2525. }
  2526. var nodeName = $selectionELem.getNodeName();
  2527. if (nodeName !== "TD" && nodeName !== "TH") {
  2528. return;
  2529. }
  2530. // 获取 td index
  2531. var $tr = $selectionELem.parent();
  2532. var $tds = $tr.children();
  2533. var tdLength = $tds.length;
  2534. $tds.forEach(function (td, index) {
  2535. if (td === $selectionELem[0]) {
  2536. // 记录并跳出循环
  2537. result.td = {
  2538. index: index,
  2539. elem: td,
  2540. length: tdLength
  2541. };
  2542. return false;
  2543. }
  2544. });
  2545. // 获取 tr index
  2546. var $tbody = $tr.parent();
  2547. var $trs = $tbody.children();
  2548. var trLength = $trs.length;
  2549. $trs.forEach(function (tr, index) {
  2550. if (tr === $tr[0]) {
  2551. // 记录并跳出循环
  2552. result.tr = {
  2553. index: index,
  2554. elem: tr,
  2555. length: trLength
  2556. };
  2557. return false;
  2558. }
  2559. });
  2560. // 返回结果
  2561. return result;
  2562. },
  2563. // 增加行
  2564. _addRow: function _addRow() {
  2565. // 获取当前单元格的位置信息
  2566. var locationData = this._getLocationData();
  2567. if (!locationData) {
  2568. return;
  2569. }
  2570. var trData = locationData.tr;
  2571. var $currentTr = $(trData.elem);
  2572. var tdData = locationData.td;
  2573. var tdLength = tdData.length;
  2574. // 拼接即将插入的字符串
  2575. var newTr = document.createElement("tr");
  2576. var tpl = "",
  2577. i = void 0;
  2578. for (i = 0; i < tdLength; i++) {
  2579. tpl += "<td>&nbsp;</td>";
  2580. }
  2581. newTr.innerHTML = tpl;
  2582. // 插入
  2583. $(newTr).insertAfter($currentTr);
  2584. },
  2585. // 增加列
  2586. _addCol: function _addCol() {
  2587. // 获取当前单元格的位置信息
  2588. var locationData = this._getLocationData();
  2589. if (!locationData) {
  2590. return;
  2591. }
  2592. var trData = locationData.tr;
  2593. var tdData = locationData.td;
  2594. var tdIndex = tdData.index;
  2595. var $currentTr = $(trData.elem);
  2596. var $trParent = $currentTr.parent();
  2597. var $trs = $trParent.children();
  2598. // 遍历所有行
  2599. $trs.forEach(function (tr) {
  2600. var $tr = $(tr);
  2601. var $tds = $tr.children();
  2602. var $currentTd = $tds.get(tdIndex);
  2603. var name = $currentTd.getNodeName().toLowerCase();
  2604. // new 一个 td,并插入
  2605. var newTd = document.createElement(name);
  2606. $(newTd).insertAfter($currentTd);
  2607. });
  2608. },
  2609. // 删除行
  2610. _delRow: function _delRow() {
  2611. // 获取当前单元格的位置信息
  2612. var locationData = this._getLocationData();
  2613. if (!locationData) {
  2614. return;
  2615. }
  2616. var trData = locationData.tr;
  2617. var $currentTr = $(trData.elem);
  2618. $currentTr.remove();
  2619. },
  2620. // 删除列
  2621. _delCol: function _delCol() {
  2622. // 获取当前单元格的位置信息
  2623. var locationData = this._getLocationData();
  2624. if (!locationData) {
  2625. return;
  2626. }
  2627. var trData = locationData.tr;
  2628. var tdData = locationData.td;
  2629. var tdIndex = tdData.index;
  2630. var $currentTr = $(trData.elem);
  2631. var $trParent = $currentTr.parent();
  2632. var $trs = $trParent.children();
  2633. // 遍历所有行
  2634. $trs.forEach(function (tr) {
  2635. var $tr = $(tr);
  2636. var $tds = $tr.children();
  2637. var $currentTd = $tds.get(tdIndex);
  2638. // 删除
  2639. $currentTd.remove();
  2640. });
  2641. },
  2642. // 删除表格
  2643. _delTable: function _delTable() {
  2644. var editor = this.editor;
  2645. var $selectionELem = editor.selection.getSelectionContainerElem();
  2646. if (!$selectionELem) {
  2647. return;
  2648. }
  2649. var $table = $selectionELem.parentUntil("table");
  2650. if (!$table) {
  2651. return;
  2652. }
  2653. $table.remove();
  2654. },
  2655. // 试图改变 active 状态
  2656. tryChangeActive: function tryChangeActive(e) {
  2657. var editor = this.editor;
  2658. var $elem = this.$elem;
  2659. var $selectionELem = editor.selection.getSelectionContainerElem();
  2660. if (!$selectionELem) {
  2661. return;
  2662. }
  2663. var nodeName = $selectionELem.getNodeName();
  2664. if (nodeName === "TD" || nodeName === "TH") {
  2665. this._active = true;
  2666. $elem.addClass("w-e-active");
  2667. } else {
  2668. this._active = false;
  2669. $elem.removeClass("w-e-active");
  2670. }
  2671. }
  2672. };
  2673. /*
  2674. underline-menu
  2675. */
  2676. // 构造函数
  2677. function Underline(editor) {
  2678. this.editor = editor;
  2679. this.$elem = $('<div class="w-e-menu">\n <i class="w-e-icon-underline"></i>\n </div>');
  2680. this.type = 'click';
  2681. // 当前是否 active 状态
  2682. this._active = false;
  2683. }
  2684. // 原型
  2685. Underline.prototype = {
  2686. constructor: Underline,
  2687. // 点击事件
  2688. onClick: function onClick(e) {
  2689. // 点击菜单将触发这里
  2690. var editor = this.editor;
  2691. var isSeleEmpty = editor.selection.isSelectionEmpty();
  2692. if (isSeleEmpty) {
  2693. // 选区是空的,插入并选中一个“空白”
  2694. editor.selection.createEmptyRange();
  2695. }
  2696. // 执行 underline 命令
  2697. editor.cmd.do('underline');
  2698. if (isSeleEmpty) {
  2699. // 需要将选取折叠起来
  2700. editor.selection.collapseRange();
  2701. editor.selection.restoreSelection();
  2702. }
  2703. },
  2704. // 试图改变 active 状态
  2705. tryChangeActive: function tryChangeActive(e) {
  2706. var editor = this.editor;
  2707. var $elem = this.$elem;
  2708. if (editor.cmd.queryCommandState('underline')) {
  2709. this._active = true;
  2710. $elem.addClass('w-e-active');
  2711. } else {
  2712. this._active = false;
  2713. $elem.removeClass('w-e-active');
  2714. }
  2715. }
  2716. };
  2717. /*
  2718. undo-menu
  2719. */
  2720. // 构造函数
  2721. function Undo(editor) {
  2722. this.editor = editor;
  2723. this.$elem = $('<div class="w-e-menu">\n <i class="w-e-icon-undo"></i>\n </div>');
  2724. this.type = 'click';
  2725. // 当前是否 active 状态
  2726. this._active = false;
  2727. }
  2728. // 原型
  2729. Undo.prototype = {
  2730. constructor: Undo,
  2731. // 点击事件
  2732. onClick: function onClick(e) {
  2733. // 点击菜单将触发这里
  2734. var editor = this.editor;
  2735. // 执行 undo 命令
  2736. editor.cmd.do('undo');
  2737. }
  2738. };
  2739. /*
  2740. menu - video
  2741. */
  2742. // 构造函数
  2743. function Video(editor) {
  2744. this.editor = editor;
  2745. this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-play"></i></div>');
  2746. this.type = 'panel';
  2747. // 当前是否 active 状态
  2748. this._active = false;
  2749. }
  2750. // 原型
  2751. Video.prototype = {
  2752. constructor: Video,
  2753. onClick: function onClick() {
  2754. this._createPanel();
  2755. },
  2756. _createPanel: function _createPanel() {
  2757. var _this = this;
  2758. // 创建 id
  2759. var textValId = getRandom('text-val');
  2760. var btnId = getRandom('btn');
  2761. // 创建 panel
  2762. var panel = new Panel(this, {
  2763. width: 350,
  2764. // 一个 panel 多个 tab
  2765. tabs: [{
  2766. // 标题
  2767. title: '插入视频',
  2768. // 模板
  2769. tpl: '<div>\n <input id="' + textValId + '" type="text" class="block" placeholder="\u683C\u5F0F\u5982\uFF1A<iframe src=... ></iframe>"/>\n <div class="w-e-button-container">\n <button id="' + btnId + '" class="right">\u63D2\u5165</button>\n </div>\n </div>',
  2770. // 事件绑定
  2771. events: [{
  2772. selector: '#' + btnId,
  2773. type: 'click',
  2774. fn: function fn() {
  2775. var $text = $('#' + textValId);
  2776. var val = $text.val().trim();
  2777. // 测试用视频地址
  2778. // <iframe height=498 width=510 src='http://player.youku.com/embed/XMjcwMzc3MzM3Mg==' frameborder=0 'allowfullscreen'></iframe>
  2779. if (val) {
  2780. // 插入视频
  2781. _this._insert(val);
  2782. }
  2783. // 返回 true,表示该事件执行完之后,panel 要关闭。否则 panel 不会关闭
  2784. return true;
  2785. }
  2786. }] // first tab end
  2787. }] // tabs end
  2788. }); // panel end
  2789. // 显示 panel
  2790. panel.show();
  2791. // 记录属性
  2792. this.panel = panel;
  2793. },
  2794. // 插入视频
  2795. _insert: function _insert(val) {
  2796. var editor = this.editor;
  2797. editor.cmd.do('insertHTML', val + '<p><br></p>');
  2798. }
  2799. };
  2800. /*
  2801. 所有菜单的汇总
  2802. */
  2803. // 存储菜单的构造函数
  2804. var MenuConstructors = {};
  2805. MenuConstructors.bold = Bold;
  2806. MenuConstructors.head = Head;
  2807. MenuConstructors.fontSize = FontSize;
  2808. MenuConstructors.fontName = FontName;
  2809. MenuConstructors.link = Link;
  2810. MenuConstructors.italic = Italic;
  2811. MenuConstructors.redo = Redo;
  2812. MenuConstructors.strikeThrough = StrikeThrough;
  2813. MenuConstructors.underline = Underline;
  2814. MenuConstructors.undo = Undo;
  2815. MenuConstructors.list = List;
  2816. MenuConstructors.justify = Justify;
  2817. MenuConstructors.foreColor = ForeColor;
  2818. MenuConstructors.backColor = BackColor;
  2819. MenuConstructors.quote = Quote;
  2820. MenuConstructors.code = Code;
  2821. MenuConstructors.emoticon = Emoticon;
  2822. MenuConstructors.table = Table;
  2823. MenuConstructors.composition = Composition;
  2824. MenuConstructors.Hline = Hline;
  2825. MenuConstructors.formula = Formula;
  2826. MenuConstructors.video = Video;
  2827. MenuConstructors.image = Image;
  2828. /*
  2829. 菜单集合
  2830. */
  2831. // 构造函数
  2832. function Menus(editor) {
  2833. this.editor = editor;
  2834. this.menus = {};
  2835. }
  2836. // 修改原型
  2837. Menus.prototype = {
  2838. constructor: Menus,
  2839. // 初始化菜单
  2840. init: function init() {
  2841. var _this = this;
  2842. var editor = this.editor;
  2843. var config = editor.config || {};
  2844. var configMenus = config.menus || []; // 获取配置中的菜单
  2845. // 根据配置信息,创建菜单
  2846. configMenus.forEach(function (menuKey) {
  2847. var MenuConstructor = MenuConstructors[menuKey];
  2848. if (MenuConstructor && typeof MenuConstructor === "function") {
  2849. // 创建单个菜单
  2850. _this.menus[menuKey] = new MenuConstructor(editor);
  2851. }
  2852. });
  2853. // 添加到菜单栏
  2854. this._addToToolbar();
  2855. // 绑定事件
  2856. this._bindEvent();
  2857. },
  2858. // 添加到菜单栏
  2859. _addToToolbar: function _addToToolbar() {
  2860. var editor = this.editor;
  2861. var $toolbarElem = editor.$toolbarElem;
  2862. var menus = this.menus;
  2863. var config = editor.config;
  2864. // config.zIndex 是配置的编辑区域的 z-index,菜单的 z-index 得在其基础上 +1
  2865. var zIndex = config.zIndex + 1;
  2866. objForEach(menus, function (key, menu) {
  2867. var $elem = menu.$elem;
  2868. if ($elem) {
  2869. // 设置 z-index
  2870. $elem.css("z-index", zIndex);
  2871. $toolbarElem.append($elem);
  2872. }
  2873. });
  2874. },
  2875. // 绑定菜单 click mouseenter 事件
  2876. _bindEvent: function _bindEvent() {
  2877. var menus = this.menus;
  2878. var editor = this.editor;
  2879. objForEach(menus, function (key, menu) {
  2880. var type = menu.type;
  2881. if (!type) {
  2882. return;
  2883. }
  2884. var $elem = menu.$elem;
  2885. var droplist = menu.droplist;
  2886. var panel = menu.panel;
  2887. // 点击类型,例如 bold
  2888. if (type === "click" && menu.onClick) {
  2889. $elem.on("click", function (e) {
  2890. if (editor.selection.getRange() == null) {
  2891. return;
  2892. }
  2893. menu.onClick(e);
  2894. });
  2895. }
  2896. // 下拉框,例如 head
  2897. if (type === "droplist" && droplist) {
  2898. $elem.on("mouseenter", function (e) {
  2899. if (editor.selection.getRange() == null) {
  2900. return;
  2901. }
  2902. // 显示
  2903. droplist.showTimeoutId = setTimeout(function () {
  2904. droplist.show();
  2905. }, 200);
  2906. }).on("mouseleave", function (e) {
  2907. // 隐藏
  2908. droplist.hideTimeoutId = setTimeout(function () {
  2909. droplist.hide();
  2910. }, 0);
  2911. });
  2912. }
  2913. // 弹框类型,例如 link
  2914. if (type === "panel" && menu.onClick) {
  2915. $elem.on("click", function (e) {
  2916. e.stopPropagation();
  2917. if (editor.selection.getRange() == null) {
  2918. return;
  2919. }
  2920. // 在自定义事件中显示 panel
  2921. menu.onClick(e);
  2922. });
  2923. }
  2924. });
  2925. },
  2926. // 尝试修改菜单状态
  2927. changeActive: function changeActive() {
  2928. var menus = this.menus;
  2929. objForEach(menus, function (key, menu) {
  2930. if (menu.tryChangeActive) {
  2931. setTimeout(function () {
  2932. menu.tryChangeActive();
  2933. }, 100);
  2934. }
  2935. });
  2936. }
  2937. };
  2938. /*
  2939. selection range API
  2940. */
  2941. // 构造函数
  2942. function API(editor) {
  2943. this.editor = editor;
  2944. this._currentRange = null;
  2945. }
  2946. // 修改原型
  2947. API.prototype = {
  2948. constructor: API,
  2949. // 获取 range 对象
  2950. getRange: function getRange() {
  2951. return this._currentRange;
  2952. },
  2953. // 保存选区
  2954. saveRange: function saveRange(_range) {
  2955. if (_range) {
  2956. // 保存已有选区
  2957. this._currentRange = _range;
  2958. return;
  2959. }
  2960. // 获取当前的选区
  2961. var selection = window.getSelection();
  2962. if (selection.rangeCount === 0) {
  2963. return;
  2964. }
  2965. var range = selection.getRangeAt(0);
  2966. // 判断选区内容是否在编辑内容之内
  2967. var $containerElem = this.getSelectionContainerElem(range);
  2968. if (!$containerElem) {
  2969. return;
  2970. }
  2971. // 判断选区内容是否在不可编辑区域之内
  2972. if ($containerElem.attr('contenteditable') === 'false' || $containerElem.parentUntil('[contenteditable=false]')) {
  2973. return;
  2974. }
  2975. var editor = this.editor;
  2976. var $textElem = editor.$textElem;
  2977. if ($textElem.isContain($containerElem)) {
  2978. // 是编辑内容之内的
  2979. this._currentRange = range;
  2980. }
  2981. },
  2982. // 折叠选区
  2983. collapseRange: function collapseRange(toStart) {
  2984. if (toStart == null) {
  2985. // 默认为 false
  2986. toStart = false;
  2987. }
  2988. var range = this._currentRange;
  2989. if (range) {
  2990. range.collapse(toStart);
  2991. }
  2992. },
  2993. // 选中区域的文字
  2994. getSelectionText: function getSelectionText() {
  2995. var range = this._currentRange;
  2996. if (range) {
  2997. return this._currentRange.toString();
  2998. } else {
  2999. return '';
  3000. }
  3001. },
  3002. // 选区的 $Elem
  3003. getSelectionContainerElem: function getSelectionContainerElem(range) {
  3004. range = range || this._currentRange;
  3005. var elem = void 0;
  3006. if (range) {
  3007. elem = range.commonAncestorContainer;
  3008. return $(elem.nodeType === 1 ? elem : elem.parentNode);
  3009. }
  3010. },
  3011. getSelectionStartElem: function getSelectionStartElem(range) {
  3012. range = range || this._currentRange;
  3013. var elem = void 0;
  3014. if (range) {
  3015. elem = range.startContainer;
  3016. return $(elem.nodeType === 1 ? elem : elem.parentNode);
  3017. }
  3018. },
  3019. getSelectionEndElem: function getSelectionEndElem(range) {
  3020. range = range || this._currentRange;
  3021. var elem = void 0;
  3022. if (range) {
  3023. elem = range.endContainer;
  3024. return $(elem.nodeType === 1 ? elem : elem.parentNode);
  3025. }
  3026. },
  3027. // 选区是否为空
  3028. isSelectionEmpty: function isSelectionEmpty() {
  3029. var range = this._currentRange;
  3030. if (range && range.startContainer) {
  3031. if (range.startContainer === range.endContainer) {
  3032. if (range.startOffset === range.endOffset) {
  3033. return true;
  3034. }
  3035. }
  3036. }
  3037. return false;
  3038. },
  3039. // 恢复选区
  3040. restoreSelection: function restoreSelection() {
  3041. var selection = window.getSelection();
  3042. selection.removeAllRanges();
  3043. selection.addRange(this._currentRange);
  3044. },
  3045. // 创建一个空白(即 &#8203 字符)选区
  3046. createEmptyRange: function createEmptyRange() {
  3047. var editor = this.editor;
  3048. var range = this.getRange();
  3049. var $elem = void 0;
  3050. if (!range) {
  3051. // 当前无 range
  3052. return;
  3053. }
  3054. if (!this.isSelectionEmpty()) {
  3055. // 当前选区必须没有内容才可以
  3056. return;
  3057. }
  3058. try {
  3059. // 目前只支持 webkit 内核
  3060. if (UA.isWebkit()) {
  3061. // 插入 &#8203
  3062. editor.cmd.do('insertHTML', '&#8203;');
  3063. // 修改 offset 位置
  3064. range.setEnd(range.endContainer, range.endOffset + 1);
  3065. // 存储
  3066. this.saveRange(range);
  3067. } else {
  3068. $elem = $('<strong>&#8203;</strong>');
  3069. editor.cmd.do('insertElem', $elem);
  3070. this.createRangeByElem($elem, true);
  3071. }
  3072. } catch (ex) {
  3073. // 部分情况下会报错,兼容一下
  3074. }
  3075. },
  3076. // 根据 $Elem 设置选区
  3077. createRangeByElem: function createRangeByElem($elem, toStart, isContent) {
  3078. // $elem - 经过封装的 elem
  3079. // toStart - true 开始位置,false 结束位置
  3080. // isContent - 是否选中Elem的内容
  3081. if (!$elem.length) {
  3082. return;
  3083. }
  3084. var elem = $elem[0];
  3085. var range = document.createRange();
  3086. if (isContent) {
  3087. range.selectNodeContents(elem);
  3088. } else {
  3089. range.selectNode(elem);
  3090. }
  3091. if (typeof toStart === 'boolean') {
  3092. range.collapse(toStart);
  3093. }
  3094. // 存储 range
  3095. this.saveRange(range);
  3096. }
  3097. };
  3098. /*
  3099. 粘贴信息的处理
  3100. */
  3101. // 获取粘贴的纯文本
  3102. function getPasteText(e) {
  3103. var clipboardData = e.clipboardData || e.originalEvent && e.originalEvent.clipboardData;
  3104. var pasteText = void 0;
  3105. if (clipboardData == null) {
  3106. pasteText = window.clipboardData && window.clipboardData.getData('text');
  3107. } else {
  3108. pasteText = clipboardData.getData('text/plain');
  3109. }
  3110. return replaceHtmlSymbol(pasteText);
  3111. }
  3112. // 获取粘贴的html
  3113. function getPasteHtml(e, filterStyle, ignoreImg) {
  3114. var clipboardData = e.clipboardData || e.originalEvent && e.originalEvent.clipboardData;
  3115. var pasteText = void 0,
  3116. pasteHtml = void 0;
  3117. if (clipboardData == null) {
  3118. pasteText = window.clipboardData && window.clipboardData.getData('text');
  3119. } else {
  3120. pasteText = clipboardData.getData('text/plain');
  3121. pasteHtml = clipboardData.getData('text/html');
  3122. }
  3123. if (!pasteHtml && pasteText) {
  3124. pasteHtml = '<p>' + replaceHtmlSymbol(pasteText) + '</p>';
  3125. }
  3126. if (!pasteHtml) {
  3127. return;
  3128. }
  3129. // 过滤word中状态过来的无用字符
  3130. var docSplitHtml = pasteHtml.split('</html>');
  3131. if (docSplitHtml.length === 2) {
  3132. pasteHtml = docSplitHtml[0];
  3133. }
  3134. // 过滤无用标签
  3135. pasteHtml = pasteHtml.replace(/<(meta|script|link).+?>/igm, '');
  3136. // 去掉注释
  3137. pasteHtml = pasteHtml.replace(/<!--.*?-->/mg, '');
  3138. // 过滤 data-xxx 属性
  3139. pasteHtml = pasteHtml.replace(/\s?data-.+?=('|").+?('|")/igm, '');
  3140. if (ignoreImg) {
  3141. // 忽略图片
  3142. pasteHtml = pasteHtml.replace(/<img.+?>/igm, '');
  3143. }
  3144. if (filterStyle) {
  3145. // 过滤样式
  3146. pasteHtml = pasteHtml.replace(/\s?(class|style)=('|").*?('|")/igm, '');
  3147. } else {
  3148. // 保留样式
  3149. pasteHtml = pasteHtml.replace(/\s?class=('|").*?('|")/igm, '');
  3150. }
  3151. return pasteHtml;
  3152. }
  3153. // 获取粘贴的图片文件
  3154. function getPasteImgs(e) {
  3155. var result = [];
  3156. var txt = getPasteText(e);
  3157. if (txt) {
  3158. // 有文字,就忽略图片
  3159. return result;
  3160. }
  3161. var clipboardData = e.clipboardData || e.originalEvent && e.originalEvent.clipboardData || {};
  3162. var items = clipboardData.items;
  3163. if (!items) {
  3164. return result;
  3165. }
  3166. objForEach(items, function (key, value) {
  3167. var type = value.type;
  3168. if (/image/i.test(type)) {
  3169. result.push(value.getAsFile());
  3170. }
  3171. });
  3172. return result;
  3173. }
  3174. /*
  3175. 编辑区域
  3176. */
  3177. // 获取一个 elem.childNodes 的 JSON 数据
  3178. function getChildrenJSON($elem) {
  3179. var result = [];
  3180. var $children = $elem.childNodes() || []; // 注意 childNodes() 可以获取文本节点
  3181. $children.forEach(function (curElem) {
  3182. var elemResult = void 0;
  3183. var nodeType = curElem.nodeType;
  3184. // 文本节点
  3185. if (nodeType === 3) {
  3186. elemResult = curElem.textContent;
  3187. elemResult = replaceHtmlSymbol(elemResult);
  3188. }
  3189. // 普通 DOM 节点
  3190. if (nodeType === 1) {
  3191. elemResult = {};
  3192. // tag
  3193. elemResult.tag = curElem.nodeName.toLowerCase();
  3194. // attr
  3195. var attrData = [];
  3196. var attrList = curElem.attributes || {};
  3197. var attrListLength = attrList.length || 0;
  3198. for (var i = 0; i < attrListLength; i++) {
  3199. var attr = attrList[i];
  3200. attrData.push({
  3201. name: attr.name,
  3202. value: attr.value
  3203. });
  3204. }
  3205. elemResult.attrs = attrData;
  3206. // children(递归)
  3207. elemResult.children = getChildrenJSON($(curElem));
  3208. }
  3209. result.push(elemResult);
  3210. });
  3211. return result;
  3212. }
  3213. // 构造函数
  3214. function Text(editor) {
  3215. this.editor = editor;
  3216. }
  3217. // 修改原型
  3218. Text.prototype = {
  3219. constructor: Text,
  3220. // 初始化
  3221. init: function init() {
  3222. // 绑定事件
  3223. this._bindEvent();
  3224. },
  3225. // 清空内容
  3226. clear: function clear() {
  3227. this.html('<p><br></p>');
  3228. },
  3229. // 获取 设置 html
  3230. html: function html(val) {
  3231. var editor = this.editor;
  3232. var $textElem = editor.$textElem;
  3233. var html = void 0;
  3234. if (val == null) {
  3235. html = $textElem.html();
  3236. // 未选中任何内容的时候点击“加粗”或者“斜体”等按钮,就得需要一个空的占位符 &#8203 ,这里替换掉
  3237. html = html.replace(/\u200b/gm, '');
  3238. return html;
  3239. } else {
  3240. $textElem.html(val);
  3241. // 初始化选取,将光标定位到内容尾部
  3242. editor.initSelection();
  3243. }
  3244. },
  3245. // 获取 JSON
  3246. getJSON: function getJSON() {
  3247. var editor = this.editor;
  3248. var $textElem = editor.$textElem;
  3249. return getChildrenJSON($textElem);
  3250. },
  3251. // 获取 设置 text
  3252. text: function text(val) {
  3253. var editor = this.editor;
  3254. var $textElem = editor.$textElem;
  3255. var text = void 0;
  3256. if (val == null) {
  3257. text = $textElem.text();
  3258. // 未选中任何内容的时候点击“加粗”或者“斜体”等按钮,就得需要一个空的占位符 &#8203 ,这里替换掉
  3259. text = text.replace(/\u200b/gm, '');
  3260. return text;
  3261. } else {
  3262. $textElem.text('<p>' + val + '</p>');
  3263. // 初始化选取,将光标定位到内容尾部
  3264. editor.initSelection();
  3265. }
  3266. },
  3267. // 追加内容
  3268. append: function append(html) {
  3269. var editor = this.editor;
  3270. var $textElem = editor.$textElem;
  3271. $textElem.append($(html));
  3272. // 初始化选取,将光标定位到内容尾部
  3273. editor.initSelection();
  3274. },
  3275. // 绑定事件
  3276. _bindEvent: function _bindEvent() {
  3277. // 实时保存选取
  3278. this._saveRangeRealTime();
  3279. // 按回车建时的特殊处理
  3280. this._enterKeyHandle();
  3281. // 清空时保留 <p><br></p>
  3282. this._clearHandle();
  3283. // 粘贴事件(粘贴文字,粘贴图片)
  3284. this._pasteHandle();
  3285. // tab 特殊处理
  3286. this._tabHandle();
  3287. // img 点击
  3288. this._imgHandle();
  3289. // 拖拽事件
  3290. this._dragHandle();
  3291. },
  3292. // 实时保存选取
  3293. _saveRangeRealTime: function _saveRangeRealTime() {
  3294. var editor = this.editor;
  3295. var $textElem = editor.$textElem;
  3296. // 保存当前的选区
  3297. function saveRange(e) {
  3298. // 随时保存选区
  3299. editor.selection.saveRange();
  3300. // 更新按钮 ative 状态
  3301. editor.menus.changeActive();
  3302. }
  3303. // 按键后保存
  3304. $textElem.on('keyup', saveRange);
  3305. $textElem.on('mousedown', function (e) {
  3306. // mousedown 状态下,鼠标滑动到编辑区域外面,也需要保存选区
  3307. $textElem.on('mouseleave', saveRange);
  3308. });
  3309. $textElem.on('mouseup', function (e) {
  3310. saveRange();
  3311. // 在编辑器区域之内完成点击,取消鼠标滑动到编辑区外面的事件
  3312. $textElem.off('mouseleave', saveRange);
  3313. });
  3314. },
  3315. // 按回车键时的特殊处理
  3316. _enterKeyHandle: function _enterKeyHandle() {
  3317. var editor = this.editor;
  3318. var $textElem = editor.$textElem;
  3319. function insertEmptyP($selectionElem) {
  3320. var $p = $('<p><br></p>');
  3321. $p.insertBefore($selectionElem);
  3322. editor.selection.createRangeByElem($p, true);
  3323. editor.selection.restoreSelection();
  3324. $selectionElem.remove();
  3325. }
  3326. // 将回车之后生成的非 <p> 的顶级标签,改为 <p>
  3327. function pHandle(e) {
  3328. var $selectionElem = editor.selection.getSelectionContainerElem();
  3329. var $parentElem = $selectionElem.parent();
  3330. if ($parentElem.html() === '<code><br></code>') {
  3331. // 回车之前光标所在一个 <p><code>.....</code></p> ,忽然回车生成一个空的 <p><code><br></code></p>
  3332. // 而且继续回车跳不出去,因此只能特殊处理
  3333. insertEmptyP($selectionElem);
  3334. return;
  3335. }
  3336. if (!$parentElem.equal($textElem)) {
  3337. // 不是顶级标签
  3338. return;
  3339. }
  3340. var nodeName = $selectionElem.getNodeName();
  3341. if (nodeName === 'P') {
  3342. // 当前的标签是 P ,不用做处理
  3343. return;
  3344. }
  3345. if ($selectionElem.text()) {
  3346. // 有内容,不做处理
  3347. return;
  3348. }
  3349. // 插入 <p> ,并将选取定位到 <p>,删除当前标签
  3350. insertEmptyP($selectionElem);
  3351. }
  3352. $textElem.on('keyup', function (e) {
  3353. if (e.keyCode !== 13) {
  3354. // 不是回车键
  3355. return;
  3356. }
  3357. // 将回车之后生成的非 <p> 的顶级标签,改为 <p>
  3358. pHandle(e);
  3359. });
  3360. // <pre><code></code></pre> 回车时 特殊处理
  3361. function codeHandle(e) {
  3362. var $selectionElem = editor.selection.getSelectionContainerElem();
  3363. if (!$selectionElem) {
  3364. return;
  3365. }
  3366. var $parentElem = $selectionElem.parent();
  3367. var selectionNodeName = $selectionElem.getNodeName();
  3368. var parentNodeName = $parentElem.getNodeName();
  3369. if (selectionNodeName !== 'CODE' || parentNodeName !== 'PRE') {
  3370. // 不符合要求 忽略
  3371. return;
  3372. }
  3373. if (!editor.cmd.queryCommandSupported('insertHTML')) {
  3374. // 必须原生支持 insertHTML 命令
  3375. return;
  3376. }
  3377. // 处理:光标定位到代码末尾,联系点击两次回车,即跳出代码块
  3378. if (editor._willBreakCode === true) {
  3379. // 此时可以跳出代码块
  3380. // 插入 <p> ,并将选取定位到 <p>
  3381. var $p = $('<p><br></p>');
  3382. $p.insertAfter($parentElem);
  3383. editor.selection.createRangeByElem($p, true);
  3384. editor.selection.restoreSelection();
  3385. // 修改状态
  3386. editor._willBreakCode = false;
  3387. e.preventDefault();
  3388. return;
  3389. }
  3390. var _startOffset = editor.selection.getRange().startOffset;
  3391. // 处理:回车时,不能插入 <br> 而是插入 \n ,因为是在 pre 标签里面
  3392. editor.cmd.do('insertHTML', '\n');
  3393. editor.selection.saveRange();
  3394. if (editor.selection.getRange().startOffset === _startOffset) {
  3395. // 没起作用,再来一遍
  3396. editor.cmd.do('insertHTML', '\n');
  3397. }
  3398. var codeLength = $selectionElem.html().length;
  3399. if (editor.selection.getRange().startOffset + 1 === codeLength) {
  3400. // 说明光标在代码最后的位置,执行了回车操作
  3401. // 记录下来,以便下次回车时候跳出 code
  3402. editor._willBreakCode = true;
  3403. }
  3404. // 阻止默认行为
  3405. e.preventDefault();
  3406. }
  3407. $textElem.on('keydown', function (e) {
  3408. if (e.keyCode !== 13) {
  3409. // 不是回车键
  3410. // 取消即将跳转代码块的记录
  3411. editor._willBreakCode = false;
  3412. return;
  3413. }
  3414. // <pre><code></code></pre> 回车时 特殊处理
  3415. codeHandle(e);
  3416. });
  3417. },
  3418. // 清空时保留 <p><br></p>
  3419. _clearHandle: function _clearHandle() {
  3420. var editor = this.editor;
  3421. var $textElem = editor.$textElem;
  3422. $textElem.on('keydown', function (e) {
  3423. if (e.keyCode !== 8) {
  3424. return;
  3425. }
  3426. var txtHtml = $textElem.html().toLowerCase().trim();
  3427. if (txtHtml === '<p><br></p>') {
  3428. // 最后剩下一个空行,就不再删除了
  3429. e.preventDefault();
  3430. return;
  3431. }
  3432. });
  3433. $textElem.on('keyup', function (e) {
  3434. if (e.keyCode !== 8) {
  3435. return;
  3436. }
  3437. var $p = void 0;
  3438. var txtHtml = $textElem.html().toLowerCase().trim();
  3439. // firefox 时用 txtHtml === '<br>' 判断,其他用 !txtHtml 判断
  3440. if (!txtHtml || txtHtml === '<br>') {
  3441. // 内容空了
  3442. $p = $('<p><br/></p>');
  3443. $textElem.html(''); // 一定要先清空,否则在 firefox 下有问题
  3444. $textElem.append($p);
  3445. editor.selection.createRangeByElem($p, false, true);
  3446. editor.selection.restoreSelection();
  3447. }
  3448. });
  3449. },
  3450. // 粘贴事件(粘贴文字 粘贴图片)
  3451. _pasteHandle: function _pasteHandle() {
  3452. var editor = this.editor;
  3453. var config = editor.config;
  3454. var pasteFilterStyle = config.pasteFilterStyle;
  3455. var pasteTextHandle = config.pasteTextHandle;
  3456. var ignoreImg = config.pasteIgnoreImg;
  3457. var $textElem = editor.$textElem;
  3458. // 粘贴图片、文本的事件,每次只能执行一个
  3459. // 判断该次粘贴事件是否可以执行
  3460. var pasteTime = 0;
  3461. function canDo() {
  3462. var now = Date.now();
  3463. var flag = false;
  3464. if (now - pasteTime >= 100) {
  3465. // 间隔大于 100 ms ,可以执行
  3466. flag = true;
  3467. }
  3468. pasteTime = now;
  3469. return flag;
  3470. }
  3471. function resetTime() {
  3472. pasteTime = 0;
  3473. }
  3474. // 粘贴文字
  3475. $textElem.on('paste', function (e) {
  3476. if (UA.isIE()) {
  3477. return;
  3478. } else {
  3479. // 阻止默认行为,使用 execCommand 的粘贴命令
  3480. e.preventDefault();
  3481. }
  3482. // 粘贴图片和文本,只能同时使用一个
  3483. if (!canDo()) {
  3484. return;
  3485. }
  3486. // 获取粘贴的文字
  3487. var pasteHtml = getPasteHtml(e, pasteFilterStyle, ignoreImg);
  3488. var pasteText = getPasteText(e);
  3489. pasteText = pasteText.replace(/\n/gm, '<br>');
  3490. var $selectionElem = editor.selection.getSelectionContainerElem();
  3491. if (!$selectionElem) {
  3492. return;
  3493. }
  3494. var nodeName = $selectionElem.getNodeName();
  3495. // code 中只能粘贴纯文本
  3496. if (nodeName === 'CODE' || nodeName === 'PRE') {
  3497. if (pasteTextHandle && isFunction(pasteTextHandle)) {
  3498. // 用户自定义过滤处理粘贴内容
  3499. pasteText = '' + (pasteTextHandle(pasteText) || '');
  3500. }
  3501. editor.cmd.do('insertHTML', '<p>' + pasteText + '</p>');
  3502. return;
  3503. }
  3504. // 先放开注释,有问题再追查 ————
  3505. // // 表格中忽略,可能会出现异常问题
  3506. // if (nodeName === 'TD' || nodeName === 'TH') {
  3507. // return
  3508. // }
  3509. if (!pasteHtml) {
  3510. // 没有内容,可继续执行下面的图片粘贴
  3511. resetTime();
  3512. return;
  3513. }
  3514. try {
  3515. // firefox 中,获取的 pasteHtml 可能是没有 <ul> 包裹的 <li>
  3516. // 因此执行 insertHTML 会报错
  3517. if (pasteTextHandle && isFunction(pasteTextHandle)) {
  3518. // 用户自定义过滤处理粘贴内容
  3519. pasteHtml = '' + (pasteTextHandle(pasteHtml) || '');
  3520. }
  3521. editor.cmd.do('insertHTML', pasteHtml);
  3522. } catch (ex) {
  3523. // 此时使用 pasteText 来兼容一下
  3524. if (pasteTextHandle && isFunction(pasteTextHandle)) {
  3525. // 用户自定义过滤处理粘贴内容
  3526. pasteText = '' + (pasteTextHandle(pasteText) || '');
  3527. }
  3528. editor.cmd.do('insertHTML', '<p>' + pasteText + '</p>');
  3529. }
  3530. });
  3531. // 粘贴图片
  3532. $textElem.on('paste', function (e) {
  3533. if (UA.isIE()) {
  3534. return;
  3535. } else {
  3536. e.preventDefault();
  3537. }
  3538. // 粘贴图片和文本,只能同时使用一个
  3539. if (!canDo()) {
  3540. return;
  3541. }
  3542. // 获取粘贴的图片
  3543. var pasteFiles = getPasteImgs(e);
  3544. if (!pasteFiles || !pasteFiles.length) {
  3545. return;
  3546. }
  3547. // 获取当前的元素
  3548. var $selectionElem = editor.selection.getSelectionContainerElem();
  3549. if (!$selectionElem) {
  3550. return;
  3551. }
  3552. var nodeName = $selectionElem.getNodeName();
  3553. // code 中粘贴忽略
  3554. if (nodeName === 'CODE' || nodeName === 'PRE') {
  3555. return;
  3556. }
  3557. // 上传图片
  3558. var uploadImg = editor.uploadImg;
  3559. uploadImg.uploadImg(pasteFiles);
  3560. });
  3561. },
  3562. // tab 特殊处理
  3563. _tabHandle: function _tabHandle() {
  3564. var editor = this.editor;
  3565. var $textElem = editor.$textElem;
  3566. $textElem.on('keydown', function (e) {
  3567. if (e.keyCode !== 9) {
  3568. return;
  3569. }
  3570. if (!editor.cmd.queryCommandSupported('insertHTML')) {
  3571. // 必须原生支持 insertHTML 命令
  3572. return;
  3573. }
  3574. var $selectionElem = editor.selection.getSelectionContainerElem();
  3575. if (!$selectionElem) {
  3576. return;
  3577. }
  3578. var $parentElem = $selectionElem.parent();
  3579. var selectionNodeName = $selectionElem.getNodeName();
  3580. var parentNodeName = $parentElem.getNodeName();
  3581. if (selectionNodeName === 'CODE' && parentNodeName === 'PRE') {
  3582. // <pre><code> 里面
  3583. editor.cmd.do('insertHTML', ' ');
  3584. } else {
  3585. // 普通文字
  3586. editor.cmd.do('insertHTML', '&nbsp;&nbsp;&nbsp;&nbsp;');
  3587. }
  3588. e.preventDefault();
  3589. });
  3590. },
  3591. // img 点击
  3592. _imgHandle: function _imgHandle() {
  3593. var editor = this.editor;
  3594. var $textElem = editor.$textElem;
  3595. // 为图片增加 selected 样式
  3596. $textElem.on('click', 'img', function (e) {
  3597. var img = this;
  3598. var $img = $(img);
  3599. if ($img.attr('data-w-e') === '1') {
  3600. // 是表情图片,忽略
  3601. return;
  3602. }
  3603. // 记录当前点击过的图片
  3604. editor._selectedImg = $img;
  3605. // 修改选区并 restore ,防止用户此时点击退格键,会删除其他内容
  3606. editor.selection.createRangeByElem($img);
  3607. editor.selection.restoreSelection();
  3608. });
  3609. // 去掉图片的 selected 样式
  3610. $textElem.on('click keyup', function (e) {
  3611. if (e.target.matches('img')) {
  3612. // 点击的是图片,忽略
  3613. return;
  3614. }
  3615. // 删除记录
  3616. editor._selectedImg = null;
  3617. });
  3618. },
  3619. // 拖拽事件
  3620. _dragHandle: function _dragHandle() {
  3621. var editor = this.editor;
  3622. // 禁用 document 拖拽事件
  3623. var $document = $(document);
  3624. $document.on('dragleave drop dragenter dragover', function (e) {
  3625. e.preventDefault();
  3626. });
  3627. // 添加编辑区域拖拽事件
  3628. var $textElem = editor.$textElem;
  3629. $textElem.on('drop', function (e) {
  3630. e.preventDefault();
  3631. var files = e.dataTransfer && e.dataTransfer.files;
  3632. if (!files || !files.length) {
  3633. return;
  3634. }
  3635. // 上传图片
  3636. var uploadImg = editor.uploadImg;
  3637. uploadImg.uploadImg(files);
  3638. });
  3639. }
  3640. };
  3641. /*
  3642. 上传进度条
  3643. */
  3644. function Progress(editor) {
  3645. this.editor = editor;
  3646. this._time = 0;
  3647. this._isShow = false;
  3648. this._isRender = false;
  3649. this._timeoutId = 0;
  3650. this.$textContainer = editor.$textContainerElem;
  3651. this.$bar = $('<div class="w-e-progress"></div>');
  3652. }
  3653. Progress.prototype = {
  3654. constructor: Progress,
  3655. show: function show(progress) {
  3656. var _this = this;
  3657. // 状态处理
  3658. if (this._isShow) {
  3659. return;
  3660. }
  3661. this._isShow = true;
  3662. // 渲染
  3663. var $bar = this.$bar;
  3664. if (!this._isRender) {
  3665. var $textContainer = this.$textContainer;
  3666. $textContainer.append($bar);
  3667. } else {
  3668. this._isRender = true;
  3669. }
  3670. // 改变进度(节流,100ms 渲染一次)
  3671. if (Date.now() - this._time > 100) {
  3672. if (progress <= 1) {
  3673. $bar.css('width', progress * 100 + '%');
  3674. this._time = Date.now();
  3675. }
  3676. }
  3677. // 隐藏
  3678. var timeoutId = this._timeoutId;
  3679. if (timeoutId) {
  3680. clearTimeout(timeoutId);
  3681. }
  3682. timeoutId = setTimeout(function () {
  3683. _this._hide();
  3684. }, 500);
  3685. },
  3686. _hide: function _hide() {
  3687. var $bar = this.$bar;
  3688. $bar.remove();
  3689. // 修改状态
  3690. this._time = 0;
  3691. this._isShow = false;
  3692. this._isRender = false;
  3693. }
  3694. };
  3695. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  3696. return typeof obj;
  3697. } : function (obj) {
  3698. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  3699. };
  3700. /*
  3701. 上传图片
  3702. */
  3703. // 构造函数
  3704. function UploadImg(editor) {
  3705. this.editor = editor;
  3706. }
  3707. // 原型
  3708. UploadImg.prototype = {
  3709. constructor: UploadImg,
  3710. // 根据 debug 弹出不同的信息
  3711. _alert: function _alert(alertInfo, debugInfo) {
  3712. var editor = this.editor;
  3713. var debug = editor.config.debug;
  3714. var customAlert = editor.config.customAlert;
  3715. if (debug) {
  3716. throw new Error('wangEditor: ' + (debugInfo || alertInfo));
  3717. } else {
  3718. if (customAlert && typeof customAlert === 'function') {
  3719. customAlert(alertInfo);
  3720. } else {
  3721. alert(alertInfo);
  3722. }
  3723. }
  3724. },
  3725. // 根据链接插入图片
  3726. insertLinkImg: function insertLinkImg(link) {
  3727. var _this2 = this;
  3728. if (!link) {
  3729. return;
  3730. }
  3731. var editor = this.editor;
  3732. var config = editor.config;
  3733. // 校验格式
  3734. var linkImgCheck = config.linkImgCheck;
  3735. var checkResult = void 0;
  3736. if (linkImgCheck && typeof linkImgCheck === 'function') {
  3737. checkResult = linkImgCheck(link);
  3738. if (typeof checkResult === 'string') {
  3739. // 校验失败,提示信息
  3740. alert(checkResult);
  3741. return;
  3742. }
  3743. }
  3744. editor.cmd.do('insertHTML', '<img src="' + link + '" style="max-width:100%;"/>');
  3745. // 验证图片 url 是否有效,无效的话给出提示
  3746. var img = document.createElement('img');
  3747. img.onload = function () {
  3748. var callback = config.linkImgCallback;
  3749. if (callback && typeof callback === 'function') {
  3750. callback(link);
  3751. }
  3752. img = null;
  3753. };
  3754. img.onerror = function () {
  3755. img = null;
  3756. // 无法成功下载图片
  3757. _this2._alert('插入图片错误', 'wangEditor: \u63D2\u5165\u56FE\u7247\u51FA\u9519\uFF0C\u56FE\u7247\u94FE\u63A5\u662F "' + link + '"\uFF0C\u4E0B\u8F7D\u8BE5\u94FE\u63A5\u5931\u8D25');
  3758. return;
  3759. };
  3760. img.onabort = function () {
  3761. img = null;
  3762. };
  3763. img.src = link;
  3764. },
  3765. // 上传图片
  3766. uploadImg: function uploadImg(files) {
  3767. var _this3 = this;
  3768. if (!files || !files.length) {
  3769. return;
  3770. }
  3771. // ------------------------------ 获取配置信息 ------------------------------
  3772. var editor = this.editor;
  3773. var config = editor.config;
  3774. var uploadImgServer = config.uploadImgServer;
  3775. var uploadImgShowBase64 = config.uploadImgShowBase64;
  3776. var maxSize = config.uploadImgMaxSize;
  3777. var maxSizeM = maxSize / 1024 / 1024;
  3778. var maxLength = config.uploadImgMaxLength || 10000;
  3779. var uploadFileName = config.uploadFileName || '';
  3780. var uploadImgParams = config.uploadImgParams || {};
  3781. var uploadImgParamsWithUrl = config.uploadImgParamsWithUrl;
  3782. var uploadImgHeaders = config.uploadImgHeaders || {};
  3783. var hooks = config.uploadImgHooks || {};
  3784. var timeout = config.uploadImgTimeout || 3000;
  3785. var withCredentials = config.withCredentials;
  3786. if (withCredentials == null) {
  3787. withCredentials = false;
  3788. }
  3789. var customUploadImg = config.customUploadImg;
  3790. if (!customUploadImg) {
  3791. // 没有 customUploadImg 的情况下,需要如下两个配置才能继续进行图片上传
  3792. if (!uploadImgServer && !uploadImgShowBase64) {
  3793. return;
  3794. }
  3795. }
  3796. // ------------------------------ 验证文件信息 ------------------------------
  3797. var resultFiles = [];
  3798. var errInfo = [];
  3799. arrForEach(files, function (file) {
  3800. var name = file.name;
  3801. var size = file.size;
  3802. // chrome 低版本 name === undefined
  3803. if (!name || !size) {
  3804. return;
  3805. }
  3806. if (/\.(jpg|jpeg|png|bmp|gif|webp)$/i.test(name) === false) {
  3807. // 后缀名不合法,不是图片
  3808. errInfo.push('\u3010' + name + '\u3011\u4E0D\u662F\u56FE\u7247');
  3809. return;
  3810. }
  3811. if (maxSize < size) {
  3812. // 上传图片过大
  3813. errInfo.push('\u3010' + name + '\u3011\u5927\u4E8E ' + maxSizeM + 'M');
  3814. return;
  3815. }
  3816. // 验证通过的加入结果列表
  3817. resultFiles.push(file);
  3818. });
  3819. // 抛出验证信息
  3820. if (errInfo.length) {
  3821. this._alert('图片验证未通过: \n' + errInfo.join('\n'));
  3822. return;
  3823. }
  3824. if (resultFiles.length > maxLength) {
  3825. this._alert('一次最多上传' + maxLength + '张图片');
  3826. return;
  3827. }
  3828. // ------------------------------ 自定义上传 ------------------------------
  3829. if (customUploadImg && typeof customUploadImg === 'function') {
  3830. customUploadImg(resultFiles, this.insertLinkImg.bind(this));
  3831. // 阻止以下代码执行
  3832. return;
  3833. }
  3834. // 添加图片数据
  3835. var formdata = new FormData();
  3836. arrForEach(resultFiles, function (file) {
  3837. var name = uploadFileName || file.name;
  3838. formdata.append(name, file);
  3839. });
  3840. // ------------------------------ 上传图片 ------------------------------
  3841. if (uploadImgServer && typeof uploadImgServer === 'string') {
  3842. // 添加参数
  3843. var uploadImgServerArr = uploadImgServer.split('#');
  3844. uploadImgServer = uploadImgServerArr[0];
  3845. var uploadImgServerHash = uploadImgServerArr[1] || '';
  3846. objForEach(uploadImgParams, function (key, val) {
  3847. // 因使用者反应,自定义参数不能默认 encode ,由 v3.1.1 版本开始注释掉
  3848. // val = encodeURIComponent(val)
  3849. // 第一,将参数拼接到 url 中
  3850. if (uploadImgParamsWithUrl) {
  3851. if (uploadImgServer.indexOf('?') > 0) {
  3852. uploadImgServer += '&';
  3853. } else {
  3854. uploadImgServer += '?';
  3855. }
  3856. uploadImgServer = uploadImgServer + key + '=' + val;
  3857. }
  3858. // 第二,将参数添加到 formdata 中
  3859. formdata.append(key, val);
  3860. });
  3861. if (uploadImgServerHash) {
  3862. uploadImgServer += '#' + uploadImgServerHash;
  3863. }
  3864. // 定义 xhr
  3865. var xhr = new XMLHttpRequest();
  3866. xhr.open('POST', uploadImgServer);
  3867. // 设置超时
  3868. xhr.timeout = timeout;
  3869. xhr.ontimeout = function () {
  3870. // hook - timeout
  3871. if (hooks.timeout && typeof hooks.timeout === 'function') {
  3872. hooks.timeout(xhr, editor);
  3873. }
  3874. _this3._alert('上传图片超时');
  3875. };
  3876. // 监控 progress
  3877. if (xhr.upload) {
  3878. xhr.upload.onprogress = function (e) {
  3879. var percent = void 0;
  3880. // 进度条
  3881. var progressBar = new Progress(editor);
  3882. if (e.lengthComputable) {
  3883. percent = e.loaded / e.total;
  3884. progressBar.show(percent);
  3885. }
  3886. };
  3887. }
  3888. // 返回数据
  3889. xhr.onreadystatechange = function () {
  3890. var result = void 0;
  3891. if (xhr.readyState === 4) {
  3892. if (xhr.status < 200 || xhr.status >= 300) {
  3893. // hook - error
  3894. if (hooks.error && typeof hooks.error === 'function') {
  3895. hooks.error(xhr, editor);
  3896. }
  3897. // xhr 返回状态错误
  3898. _this3._alert('上传图片发生错误', '\u4E0A\u4F20\u56FE\u7247\u53D1\u751F\u9519\u8BEF\uFF0C\u670D\u52A1\u5668\u8FD4\u56DE\u72B6\u6001\u662F ' + xhr.status);
  3899. return;
  3900. }
  3901. result = xhr.responseText;
  3902. if ((typeof result === 'undefined' ? 'undefined' : _typeof(result)) !== 'object') {
  3903. try {
  3904. result = JSON.parse(result);
  3905. } catch (ex) {
  3906. // hook - fail
  3907. if (hooks.fail && typeof hooks.fail === 'function') {
  3908. hooks.fail(xhr, editor, result);
  3909. }
  3910. _this3._alert('上传图片失败', '上传图片返回结果错误,返回结果是: ' + result);
  3911. return;
  3912. }
  3913. }
  3914. if (!hooks.customInsert && result.errno != '0') {
  3915. // hook - fail
  3916. if (hooks.fail && typeof hooks.fail === 'function') {
  3917. hooks.fail(xhr, editor, result);
  3918. }
  3919. // 数据错误
  3920. _this3._alert('上传图片失败', '上传图片返回结果错误,返回结果 errno=' + result.errno);
  3921. } else {
  3922. if (hooks.customInsert && typeof hooks.customInsert === 'function') {
  3923. // 使用者自定义插入方法
  3924. hooks.customInsert(_this3.insertLinkImg.bind(_this3), result, editor);
  3925. } else {
  3926. // 将图片插入编辑器
  3927. var data = result.data || [];
  3928. data.forEach(function (link) {
  3929. _this3.insertLinkImg(link);
  3930. });
  3931. }
  3932. // hook - success
  3933. if (hooks.success && typeof hooks.success === 'function') {
  3934. hooks.success(xhr, editor, result);
  3935. }
  3936. }
  3937. }
  3938. };
  3939. // hook - before
  3940. if (hooks.before && typeof hooks.before === 'function') {
  3941. var beforeResult = hooks.before(xhr, editor, resultFiles);
  3942. if (beforeResult && (typeof beforeResult === 'undefined' ? 'undefined' : _typeof(beforeResult)) === 'object') {
  3943. if (beforeResult.prevent) {
  3944. // 如果返回的结果是 {prevent: true, msg: 'xxxx'} 则表示用户放弃上传
  3945. this._alert(beforeResult.msg);
  3946. return;
  3947. }
  3948. }
  3949. }
  3950. // 自定义 headers
  3951. objForEach(uploadImgHeaders, function (key, val) {
  3952. xhr.setRequestHeader(key, val);
  3953. });
  3954. // 跨域传 cookie
  3955. xhr.withCredentials = withCredentials;
  3956. // 发送请求
  3957. xhr.send(formdata);
  3958. // 注意,要 return 。不去操作接下来的 base64 显示方式
  3959. return;
  3960. }
  3961. // ------------------------------ 显示 base64 格式 ------------------------------
  3962. if (uploadImgShowBase64) {
  3963. arrForEach(files, function (file) {
  3964. var _this = _this3;
  3965. var reader = new FileReader();
  3966. reader.readAsDataURL(file);
  3967. reader.onload = function () {
  3968. _this.insertLinkImg(this.result);
  3969. };
  3970. });
  3971. }
  3972. }
  3973. };
  3974. /*
  3975. 编辑器构造函数
  3976. */
  3977. // id,累加
  3978. var editorId = 1;
  3979. // 构造函数
  3980. function Editor(toolbarSelector, textSelector) {
  3981. if (toolbarSelector == null) {
  3982. // 没有传入任何参数,报错
  3983. throw new Error("错误:初始化编辑器时候未传入任何参数,请查阅文档");
  3984. }
  3985. // id,用以区分单个页面不同的编辑器对象
  3986. this.id = "wangEditor-" + editorId++;
  3987. this.toolbarSelector = toolbarSelector;
  3988. this.textSelector = textSelector;
  3989. // 自定义配置
  3990. this.customConfig = {};
  3991. }
  3992. // 修改原型
  3993. Editor.prototype = {
  3994. constructor: Editor,
  3995. // 初始化配置
  3996. _initConfig: function _initConfig() {
  3997. // _config 是默认配置,this.customConfig 是用户自定义配置,将它们 merge 之后再赋值
  3998. var target = {};
  3999. this.config = Object.assign(target, config, this.customConfig);
  4000. // 将语言配置,生成正则表达式
  4001. var langConfig = this.config.lang || {};
  4002. var langArgs = [];
  4003. objForEach(langConfig, function (key, val) {
  4004. // key 即需要生成正则表达式的规则,如“插入链接”
  4005. // val 即需要被替换成的语言,如“insert link”
  4006. langArgs.push({
  4007. reg: new RegExp(key, "img"),
  4008. val: val
  4009. });
  4010. });
  4011. this.config.langArgs = langArgs;
  4012. },
  4013. // 初始化 DOM
  4014. _initDom: function _initDom() {
  4015. var _this = this;
  4016. var toolbarSelector = this.toolbarSelector;
  4017. var $toolbarSelector = $(toolbarSelector);
  4018. var textSelector = this.textSelector;
  4019. var config$$1 = this.config;
  4020. var zIndex = config$$1.zIndex;
  4021. // 定义变量
  4022. var $toolbarElem = void 0,
  4023. $textContainerElem = void 0,
  4024. $textElem = void 0,
  4025. $children = void 0;
  4026. if (textSelector == null) {
  4027. // 只传入一个参数,即是容器的选择器或元素,toolbar 和 text 的元素自行创建
  4028. $toolbarElem = $("<div></div>");
  4029. $textContainerElem = $("<div></div>");
  4030. // 将编辑器区域原有的内容,暂存起来
  4031. $children = $toolbarSelector.children();
  4032. // 添加到 DOM 结构中
  4033. $toolbarSelector.append($toolbarElem).append($textContainerElem);
  4034. // 自行创建的,需要配置默认的样式
  4035. $toolbarElem.css("background-color", "#f1f1f1").css("border", "1px solid #ccc");
  4036. $textContainerElem.css("border", "1px solid #ccc").css("border-top", "none").css("height", "300px");
  4037. } else {
  4038. // toolbar 和 text 的选择器都有值,记录属性
  4039. $toolbarElem = $toolbarSelector;
  4040. $textContainerElem = $(textSelector);
  4041. // 将编辑器区域原有的内容,暂存起来
  4042. $children = $textContainerElem.children();
  4043. }
  4044. // 编辑区域
  4045. $textElem = $("<div></div>");
  4046. $textElem.attr("contenteditable", "true").css("width", "100%").css("height", "100%");
  4047. // 初始化编辑区域内容
  4048. if ($children && $children.length) {
  4049. $textElem.append($children);
  4050. } else {
  4051. $textElem.append($("<p><br></p>"));
  4052. }
  4053. // 编辑区域加入DOM
  4054. $textContainerElem.append($textElem);
  4055. // 设置通用的 class
  4056. $toolbarElem.addClass("w-e-toolbar");
  4057. $textContainerElem.addClass("w-e-text-container");
  4058. $textContainerElem.css("z-index", zIndex);
  4059. $textElem.addClass("w-e-text");
  4060. // 添加 ID
  4061. var toolbarElemId = getRandom("toolbar-elem");
  4062. $toolbarElem.attr("id", toolbarElemId);
  4063. var textElemId = getRandom("text-elem");
  4064. $textElem.attr("id", textElemId);
  4065. // 记录属性
  4066. this.$toolbarElem = $toolbarElem;
  4067. this.$textContainerElem = $textContainerElem;
  4068. this.$textElem = $textElem;
  4069. this.toolbarElemId = toolbarElemId;
  4070. this.textElemId = textElemId;
  4071. // 记录输入法的开始和结束
  4072. var compositionEnd = true;
  4073. $textContainerElem.on("compositionstart", function () {
  4074. // 输入法开始输入
  4075. compositionEnd = false;
  4076. });
  4077. $textContainerElem.on("compositionend", function () {
  4078. // 输入法结束输入
  4079. compositionEnd = true;
  4080. });
  4081. // 绑定 onchange
  4082. $textContainerElem.on("click keyup", function () {
  4083. // 输入法结束才出发 onchange
  4084. compositionEnd && _this.change && _this.change();
  4085. });
  4086. $toolbarElem.on("click", function () {
  4087. this.change && this.change();
  4088. });
  4089. //绑定 onfocus 与 onblur 事件
  4090. if (config$$1.onfocus || config$$1.onblur) {
  4091. // 当前编辑器是否是焦点状态
  4092. this.isFocus = false;
  4093. $(document).on("click", function (e) {
  4094. //判断当前点击元素是否在编辑器内
  4095. var isChild = $textElem.isContain($(e.target));
  4096. //判断当前点击元素是否为工具栏
  4097. var isToolbar = $toolbarElem.isContain($(e.target));
  4098. var isMenu = $toolbarElem[0] == e.target ? true : false;
  4099. if (!isChild) {
  4100. //若为选择工具栏中的功能,则不视为成blur操作
  4101. if (isToolbar && !isMenu) {
  4102. return;
  4103. }
  4104. if (_this.isFocus) {
  4105. _this.onblur && _this.onblur();
  4106. }
  4107. _this.isFocus = false;
  4108. } else {
  4109. if (!_this.isFocus) {
  4110. _this.onfocus && _this.onfocus();
  4111. }
  4112. _this.isFocus = true;
  4113. }
  4114. });
  4115. }
  4116. },
  4117. // 封装 command
  4118. _initCommand: function _initCommand() {
  4119. this.cmd = new Command(this);
  4120. },
  4121. // 封装 selection range API
  4122. _initSelectionAPI: function _initSelectionAPI() {
  4123. this.selection = new API(this);
  4124. },
  4125. // 添加图片上传
  4126. _initUploadImg: function _initUploadImg() {
  4127. this.uploadImg = new UploadImg(this);
  4128. },
  4129. // 初始化菜单
  4130. _initMenus: function _initMenus() {
  4131. this.menus = new Menus(this);
  4132. this.menus.init();
  4133. },
  4134. // 添加 text 区域
  4135. _initText: function _initText() {
  4136. this.txt = new Text(this);
  4137. this.txt.init();
  4138. },
  4139. // 初始化选区,将光标定位到内容尾部
  4140. initSelection: function initSelection(newLine) {
  4141. var $textElem = this.$textElem;
  4142. var $children = $textElem.children();
  4143. if (!$children.length) {
  4144. // 如果编辑器区域无内容,添加一个空行,重新设置选区
  4145. $textElem.append($("<p><br></p>"));
  4146. this.initSelection();
  4147. return;
  4148. }
  4149. var $last = $children.last();
  4150. if (newLine) {
  4151. // 新增一个空行
  4152. var html = $last.html().toLowerCase();
  4153. var nodeName = $last.getNodeName();
  4154. if (html !== "<br>" && html !== "<br/>" || nodeName !== "P") {
  4155. // 最后一个元素不是 <p><br></p>,添加一个空行,重新设置选区
  4156. $textElem.append($("<p><br></p>"));
  4157. this.initSelection();
  4158. return;
  4159. }
  4160. }
  4161. this.selection.createRangeByElem($last, false, true);
  4162. this.selection.restoreSelection();
  4163. },
  4164. // 绑定事件
  4165. _bindEvent: function _bindEvent() {
  4166. // -------- 绑定 onchange 事件 --------
  4167. var onChangeTimeoutId = 0;
  4168. var beforeChangeHtml = this.txt.html();
  4169. var config$$1 = this.config;
  4170. // onchange 触发延迟时间
  4171. var onchangeTimeout = config$$1.onchangeTimeout;
  4172. onchangeTimeout = parseInt(onchangeTimeout, 10);
  4173. if (!onchangeTimeout || onchangeTimeout <= 0) {
  4174. onchangeTimeout = 200;
  4175. }
  4176. var onchange = config$$1.onchange;
  4177. if (onchange && typeof onchange === "function") {
  4178. // 触发 change 的有三个场景:
  4179. // 1. $textContainerElem.on('click keyup')
  4180. // 2. $toolbarElem.on('click')
  4181. // 3. editor.cmd.do()
  4182. this.change = function () {
  4183. // 判断是否有变化
  4184. var currentHtml = this.txt.html();
  4185. if (currentHtml.length === beforeChangeHtml.length) {
  4186. // 需要比较每一个字符
  4187. if (currentHtml === beforeChangeHtml) {
  4188. return;
  4189. }
  4190. }
  4191. // 执行,使用节流
  4192. if (onChangeTimeoutId) {
  4193. clearTimeout(onChangeTimeoutId);
  4194. }
  4195. onChangeTimeoutId = setTimeout(function () {
  4196. // 触发配置的 onchange 函数
  4197. onchange(currentHtml);
  4198. beforeChangeHtml = currentHtml;
  4199. }, onchangeTimeout);
  4200. };
  4201. }
  4202. // -------- 绑定 onblur 事件 --------
  4203. var onblur = config$$1.onblur;
  4204. if (onblur && typeof onblur === "function") {
  4205. this.onblur = function () {
  4206. var currentHtml = this.txt.html();
  4207. onblur(currentHtml);
  4208. };
  4209. }
  4210. // -------- 绑定 onfocus 事件 --------
  4211. var onfocus = config$$1.onfocus;
  4212. if (onfocus && typeof onfocus === "function") {
  4213. this.onfocus = function () {
  4214. onfocus();
  4215. };
  4216. }
  4217. },
  4218. // 创建编辑器
  4219. create: function create() {
  4220. // 初始化配置信息
  4221. this._initConfig();
  4222. // 初始化 DOM
  4223. this._initDom();
  4224. // 封装 command API
  4225. this._initCommand();
  4226. // 封装 selection range API
  4227. this._initSelectionAPI();
  4228. // 添加 text
  4229. this._initText();
  4230. // 初始化菜单
  4231. this._initMenus();
  4232. // 添加 图片上传
  4233. this._initUploadImg();
  4234. // 初始化选区,将光标定位到内容尾部
  4235. this.initSelection(true);
  4236. // 绑定事件
  4237. this._bindEvent();
  4238. },
  4239. // 解绑所有事件(暂时不对外开放)
  4240. _offAllEvent: function _offAllEvent() {
  4241. $.offAll();
  4242. }
  4243. };
  4244. /*
  4245. poly-fill
  4246. */
  4247. var polyfill = function () {
  4248. // Object.assign
  4249. if (typeof Object.assign != 'function') {
  4250. Object.assign = function (target, varArgs) {
  4251. // .length of function is 2
  4252. if (target == null) {
  4253. // TypeError if undefined or null
  4254. throw new TypeError('Cannot convert undefined or null to object');
  4255. }
  4256. var to = Object(target);
  4257. for (var index = 1; index < arguments.length; index++) {
  4258. var nextSource = arguments[index];
  4259. if (nextSource != null) {
  4260. // Skip over if undefined or null
  4261. for (var nextKey in nextSource) {
  4262. // Avoid bugs when hasOwnProperty is shadowed
  4263. if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
  4264. to[nextKey] = nextSource[nextKey];
  4265. }
  4266. }
  4267. }
  4268. }
  4269. return to;
  4270. };
  4271. }
  4272. // IE 中兼容 Element.prototype.matches
  4273. if (!Element.prototype.matches) {
  4274. Element.prototype.matches = Element.prototype.matchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector || Element.prototype.webkitMatchesSelector || function (s) {
  4275. var matches = (this.document || this.ownerDocument).querySelectorAll(s),
  4276. i = matches.length;
  4277. while (--i >= 0 && matches.item(i) !== this) {}
  4278. return i > -1;
  4279. };
  4280. }
  4281. };
  4282. //import '../kityformula/js/jquery-1.11.0.min.js';
  4283. // 检验是否浏览器环境
  4284. try {
  4285. document;
  4286. } catch (ex) {
  4287. throw new Error("请在浏览器环境下运行");
  4288. }
  4289. // polyfill
  4290. polyfill();
  4291. // 这里的 `inlinecss` 将被替换成 css 代码的内容,详情可去 ./gulpfile.js 中搜索 `inlinecss` 关键字
  4292. var inlinecss = ".w-e-toolbar,.w-e-text-container,.w-e-menu-panel { padding: 0; margin: 0; box-sizing: border-box;}.w-e-toolbar *,.w-e-text-container *,.w-e-menu-panel * { padding: 0; margin: 0; box-sizing: border-box;}.w-e-clear-fix:after { content: \'\'; display: table; clear: both;}.w-e-toolbar .w-e-droplist { position: absolute; left: 0; top: 0; background-color: #fff; border: 1px solid #f1f1f1; border-right-color: #ccc; border-bottom-color: #ccc;}.w-e-toolbar .w-e-droplist .w-e-dp-title { text-align: center; color: #999; line-height: 2; border-bottom: 1px solid #f1f1f1; font-size: 13px;}.w-e-toolbar .w-e-droplist ul.w-e-list { list-style: none; line-height: 1;}.w-e-toolbar .w-e-droplist ul.w-e-list li.w-e-item { color: #333; padding: 5px 0;}.w-e-toolbar .w-e-droplist ul.w-e-list li.w-e-item:hover { background-color: #f1f1f1;}.w-e-toolbar .w-e-droplist ul.w-e-block { list-style: none; text-align: left; padding: 5px;}.w-e-toolbar .w-e-droplist ul.w-e-block li.w-e-item { display: inline-block; *display: inline; *zoom: 1; padding: 3px 5px;}.w-e-toolbar .w-e-droplist ul.w-e-block li.w-e-item:hover { background-color: #f1f1f1;}@font-face { font-family: \'w-e-icon\'; src: url(data:application/x-font-woff;charset=utf-8;base64,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) format(\'truetype\'); font-weight: normal; font-style: normal;}[class^=\'w-e-icon-\'],[class*=\' w-e-icon-\'] { /* use !important to prevent issues with browser extensions that change fonts */ font-family: \'w-e-icon\' !important; speak: none; font-style: normal; font-weight: normal; font-variant: normal; text-transform: none; line-height: 1; /* Better Font Rendering =========== */ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;}.w-e-icon-close:before { content: \'\\f00d\';}.w-e-icon-upload2:before { content: \'\\e9c6\';}.w-e-icon-trash-o:before { content: \'\\f014\';}.w-e-icon-header:before { content: \'\\f1dc\';}.w-e-icon-pencil2:before { content: \'\\e906\';}.w-e-icon-paint-brush:before { content: \'\\f1fc\';}.w-e-icon-image:before { content: \'\\e90d\';}.w-e-icon-play:before { content: \'\\e912\';}.w-e-icon-location:before { content: \'\\e947\';}.w-e-icon-undo:before { content: \'\\e965\';}.w-e-icon-redo:before { content: \'\\e966\';}.w-e-icon-quotes-left:before { content: \'\\e977\';}.w-e-icon-list-numbered:before { content: \'\\e9b9\';}.w-e-icon-list2:before { content: \'\\e9bb\';}.w-e-icon-link:before { content: \'\\e9cb\';}.w-e-icon-happy:before { content: \'\\e9df\';}.w-e-icon-bold:before { content: \'\\ea62\';}.w-e-icon-underline:before { content: \'\\ea63\';}.w-e-icon-italic:before { content: \'\\ea64\';}.w-e-icon-strikethrough:before { content: \'\\ea65\';}.w-e-icon-table2:before { content: \'\\ea71\';}.w-e-icon-paragraph-left:before { content: \'\\ea77\';}.w-e-icon-paragraph-center:before { content: \'\\ea78\';}.w-e-icon-paragraph-right:before { content: \'\\ea79\';}.w-e-icon-terminal:before { content: \'\\f120\';}.w-e-icon-page-break:before { content: \'\\ea68\';}.w-e-icon-cancel-circle:before { content: \'\\ea0d\';}.w-e-icon-font:before { content: \'\\ea5c\';}.w-e-icon-text-heigh:before { content: \'\\ea5f\';}.w-e-toolbar { display: -ms-flexbox; display: flex; padding: 0 5px; /* flex-wrap: wrap; */ /* 单个菜单 */}.w-e-toolbar .w-e-menu { position: relative; text-align: center; padding: 5px 10px; cursor: pointer;}.w-e-toolbar .w-e-menu i { color: #999;}.w-e-toolbar .w-e-menu .w-e-icon-composition { position: absolute; width: 100%; height: 100%; top: 0; left: 0;}.w-e-toolbar .w-e-menu:hover i { color: #333;}.w-e-toolbar .w-e-active i { color: #1e88e5;}.w-e-toolbar .w-e-active:hover i { color: #1e88e5;}.w-e-text-container .w-e-panel-container { position: absolute; top: 0; left: 50%; border: 1px solid #ccc; border-top: 0; box-shadow: 1px 1px 2px #ccc; color: #333; background-color: #fff; /* 为 emotion panel 定制的样式 */ /* 上传图片的 panel 定制样式 */}.w-e-text-container .w-e-panel-container .w-e-panel-close { position: absolute; right: 0; top: 0; padding: 5px; margin: 2px 5px 0 0; cursor: pointer; color: #999;}.w-e-text-container .w-e-panel-container .w-e-panel-close:hover { color: #333;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-title { list-style: none; display: -ms-flexbox; display: flex; font-size: 14px; margin: 2px 10px 0 10px; border-bottom: 1px solid #f1f1f1;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-title .w-e-item { padding: 3px 5px; color: #999; cursor: pointer; margin: 0 3px; position: relative; top: 1px;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-title .w-e-active { color: #333; border-bottom: 1px solid #333; cursor: default; font-weight: 700;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content { padding: 10px 15px 10px 15px; font-size: 16px; /* 输入框的样式 */ /* 按钮的样式 */}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content input:focus,.w-e-text-container .w-e-panel-container .w-e-panel-tab-content textarea:focus,.w-e-text-container .w-e-panel-container .w-e-panel-tab-content button:focus { outline: none;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content textarea { width: 100%; border: 1px solid #ccc; padding: 5px;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content textarea:focus { border-color: #1e88e5;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content input[type=text] { border: none; border-bottom: 1px solid #ccc; font-size: 14px; height: 20px; color: #333; text-align: left;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content input[type=text].small { width: 30px; text-align: center;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content input[type=text].half { width: 100px; text-align: left;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content input[type=text].block { display: block; width: 100%; margin: 10px 0;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content input[type=text]:focus { border-bottom: 2px solid #1e88e5;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content .w-e-button-container button { font-size: 14px; color: #1e88e5; border: none; padding: 5px 10px; background-color: #fff; cursor: pointer; border-radius: 3px;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content .w-e-button-container button.left { float: left; margin-right: 10px;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content .w-e-button-container button.right { float: right; margin-left: 10px;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content .w-e-button-container button.gray { color: #999;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content .w-e-button-container button.red { color: #c24f4a;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content .w-e-button-container button:hover { background-color: #f1f1f1;}.w-e-text-container .w-e-panel-container .w-e-panel-tab-content .w-e-button-container:after { content: \'\'; display: table; clear: both;}.w-e-text-container .w-e-panel-container .w-e-emoticon-container .w-e-item { cursor: pointer; font-size: 18px; padding: 0 3px; display: inline-block; *display: inline; *zoom: 1;}.w-e-text-container .w-e-panel-container .w-e-up-img-container { text-align: center;}.w-e-text-container .w-e-panel-container .w-e-up-img-container .w-e-up-btn { display: inline-block; *display: inline; *zoom: 1; color: #999; cursor: pointer; font-size: 60px; line-height: 1;}.w-e-text-container .w-e-panel-container .w-e-up-img-container .w-e-up-btn:hover { color: #333;}.w-e-text-container { position: relative;}.w-e-text-container .w-e-progress { position: absolute; background-color: #1e88e5; bottom: 0; left: 0; height: 1px;}.w-e-text { padding: 0 10px; overflow-y: scroll;}.w-e-text p,.w-e-text h1,.w-e-text h2,.w-e-text h3,.w-e-text h4,.w-e-text h5,.w-e-text table,.w-e-text pre { margin: 10px 0; line-height: 1.5;}.w-e-text ul,.w-e-text ol { margin: 10px 0 10px 20px;}.w-e-text blockquote { display: block; border-left: 8px solid #d0e5f2; padding: 5px 10px; margin: 10px 0; line-height: 1.4; font-size: 100%; background-color: #f1f1f1;}.w-e-text code { display: inline-block; *display: inline; *zoom: 1; background-color: #f1f1f1; border-radius: 3px; padding: 3px 5px; margin: 0 3px;}.w-e-text pre code { display: block;}.w-e-text table { border-top: 1px solid #ccc; border-left: 1px solid #ccc;}.w-e-text table td,.w-e-text table th { border-bottom: 1px solid #ccc; border-right: 1px solid #ccc; padding: 3px 5px;}.w-e-text table th { border-bottom: 2px solid #ccc; text-align: center;}.w-e-text:focus { outline: none;}.w-e-text img { cursor: pointer;}.w-e-text img:hover { box-shadow: 0 0 5px #333;}";
  4293. // 将 css 代码添加到 <style> 中
  4294. var style = document.createElement("style");
  4295. style.type = "text/css";
  4296. style.innerHTML = inlinecss;
  4297. document.getElementsByTagName("HEAD").item(0).appendChild(style);
  4298. // 返回
  4299. var index = window.wangEditor || Editor;
  4300. return index;
  4301. })));