wangEditor.up.js 173 KB

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