wangEditor.up.js 157 KB

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