wangEditor.js 160 KB

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