summernote.js 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597
  1. /**
  2. * Super simple wysiwyg editor on Bootstrap v0.5.2
  3. * http://hackerwins.github.io/summernote/
  4. *
  5. * summernote.js
  6. * Copyright 2013 Alan Hong. and outher contributors
  7. * summernote may be freely distributed under the MIT license./
  8. *
  9. * Date: 2014-05-05T12:16Z
  10. */
  11. (function (factory) {
  12. /* global define */
  13. if (typeof define === 'function' && define.amd) {
  14. // AMD. Register as an anonymous module.
  15. define(['jquery', 'codemirror'], factory);
  16. } else {
  17. // Browser globals: jQuery, CodeMirror
  18. factory(window.jQuery, window.CodeMirror);
  19. }
  20. }(function ($, CodeMirror) {
  21. if ('function' !== typeof Array.prototype.reduce) {
  22. /**
  23. * Array.prototype.reduce fallback
  24. *
  25. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
  26. */
  27. Array.prototype.reduce = function (callback, optInitialValue) {
  28. var idx, value, length = this.length >>> 0, isValueSet = false;
  29. if (1 < arguments.length) {
  30. value = optInitialValue;
  31. isValueSet = true;
  32. }
  33. for (idx = 0; length > idx; ++idx) {
  34. if (this.hasOwnProperty(idx)) {
  35. if (isValueSet) {
  36. value = callback(value, this[idx], idx, this);
  37. } else {
  38. value = this[idx];
  39. isValueSet = true;
  40. }
  41. }
  42. }
  43. if (!isValueSet) {
  44. throw new TypeError('Reduce of empty array with no initial value');
  45. }
  46. return value;
  47. };
  48. }
  49. /**
  50. * Object which check platform and agent
  51. */
  52. var agent = {
  53. bMac: navigator.appVersion.indexOf('Mac') > -1,
  54. bMSIE: navigator.userAgent.indexOf('MSIE') > -1 || navigator.userAgent.indexOf('Trident') > -1,
  55. bFF: navigator.userAgent.indexOf('Firefox') > -1,
  56. jqueryVersion: parseFloat($.fn.jquery),
  57. bCodeMirror: !!CodeMirror
  58. };
  59. /**
  60. * func utils (for high-order func's arg)
  61. */
  62. var func = (function () {
  63. var eq = function (elA) {
  64. return function (elB) {
  65. return elA === elB;
  66. };
  67. };
  68. var eq2 = function (elA, elB) {
  69. return elA === elB;
  70. };
  71. var ok = function () {
  72. return true;
  73. };
  74. var fail = function () {
  75. return false;
  76. };
  77. var not = function (f) {
  78. return function () {
  79. return !f.apply(f, arguments);
  80. };
  81. };
  82. var self = function (a) {
  83. return a;
  84. };
  85. return {
  86. eq: eq,
  87. eq2: eq2,
  88. ok: ok,
  89. fail: fail,
  90. not: not,
  91. self: self
  92. };
  93. })();
  94. /**
  95. * list utils
  96. */
  97. var list = (function () {
  98. /**
  99. * returns the first element of an array.
  100. * @param {Array} array
  101. */
  102. var head = function (array) {
  103. return array[0];
  104. };
  105. /**
  106. * returns the last element of an array.
  107. * @param {Array} array
  108. */
  109. var last = function (array) {
  110. return array[array.length - 1];
  111. };
  112. /**
  113. * returns everything but the last entry of the array.
  114. * @param {Array} array
  115. */
  116. var initial = function (array) {
  117. return array.slice(0, array.length - 1);
  118. };
  119. /**
  120. * returns the rest of the elements in an array.
  121. * @param {Array} array
  122. */
  123. var tail = function (array) {
  124. return array.slice(1);
  125. };
  126. /**
  127. * returns next item.
  128. * @param {Array} array
  129. */
  130. var next = function (array, item) {
  131. var idx = array.indexOf(item);
  132. if (idx === -1) { return null; }
  133. return array[idx + 1];
  134. };
  135. /**
  136. * returns prev item.
  137. * @param {Array} array
  138. */
  139. var prev = function (array, item) {
  140. var idx = array.indexOf(item);
  141. if (idx === -1) { return null; }
  142. return array[idx - 1];
  143. };
  144. /**
  145. * get sum from a list
  146. * @param {Array} array - array
  147. * @param {Function} fn - iterator
  148. */
  149. var sum = function (array, fn) {
  150. fn = fn || func.self;
  151. return array.reduce(function (memo, v) {
  152. return memo + fn(v);
  153. }, 0);
  154. };
  155. /**
  156. * returns a copy of the collection with array type.
  157. * @param {Collection} collection - collection eg) node.childNodes, ...
  158. */
  159. var from = function (collection) {
  160. var result = [], idx = -1, length = collection.length;
  161. while (++idx < length) {
  162. result[idx] = collection[idx];
  163. }
  164. return result;
  165. };
  166. /**
  167. * cluster elements by predicate function.
  168. * @param {Array} array - array
  169. * @param {Function} fn - predicate function for cluster rule
  170. * @param {Array[]}
  171. */
  172. var clusterBy = function (array, fn) {
  173. if (array.length === 0) { return []; }
  174. var aTail = tail(array);
  175. return aTail.reduce(function (memo, v) {
  176. var aLast = last(memo);
  177. if (fn(last(aLast), v)) {
  178. aLast[aLast.length] = v;
  179. } else {
  180. memo[memo.length] = [v];
  181. }
  182. return memo;
  183. }, [[head(array)]]);
  184. };
  185. /**
  186. * returns a copy of the array with all falsy values removed
  187. * @param {Array} array - array
  188. * @param {Function} fn - predicate function for cluster rule
  189. */
  190. var compact = function (array) {
  191. var aResult = [];
  192. for (var idx = 0, sz = array.length; idx < sz; idx ++) {
  193. if (array[idx]) { aResult.push(array[idx]); }
  194. }
  195. return aResult;
  196. };
  197. return { head: head, last: last, initial: initial, tail: tail,
  198. prev: prev, next: next, sum: sum, from: from,
  199. compact: compact, clusterBy: clusterBy };
  200. })();
  201. /**
  202. * Dom functions
  203. */
  204. var dom = (function () {
  205. /**
  206. * returns whether node is `note-editable` or not.
  207. *
  208. * @param {Element} node
  209. * @return {Boolean}
  210. */
  211. var isEditable = function (node) {
  212. return node && $(node).hasClass('note-editable');
  213. };
  214. var isControlSizing = function (node) {
  215. return node && $(node).hasClass('note-control-sizing');
  216. };
  217. /**
  218. * build layoutInfo from $editor(.note-editor)
  219. *
  220. * @param {jQuery} $editor
  221. * @return {Object}
  222. */
  223. var buildLayoutInfo = function ($editor) {
  224. var makeFinder = function (sClassName) {
  225. return function () { return $editor.find(sClassName); };
  226. };
  227. return {
  228. editor: function () { return $editor; },
  229. dropzone: makeFinder('.note-dropzone'),
  230. toolbar: makeFinder('.note-toolbar'),
  231. editable: makeFinder('.note-editable'),
  232. codable: makeFinder('.note-codable'),
  233. statusbar: makeFinder('.note-statusbar'),
  234. popover: makeFinder('.note-popover'),
  235. handle: makeFinder('.note-handle'),
  236. dialog: makeFinder('.note-dialog')
  237. };
  238. };
  239. /**
  240. * returns predicate which judge whether nodeName is same
  241. * @param {String} sNodeName
  242. */
  243. var makePredByNodeName = function (sNodeName) {
  244. // nodeName is always uppercase.
  245. return function (node) {
  246. return node && node.nodeName === sNodeName;
  247. };
  248. };
  249. var isPara = function (node) {
  250. // Chrome(v31.0), FF(v25.0.1) use DIV for paragraph
  251. return node && /^DIV|^P|^LI|^H[1-7]/.test(node.nodeName);
  252. };
  253. var isList = function (node) {
  254. return node && /^UL|^OL/.test(node.nodeName);
  255. };
  256. var isCell = function (node) {
  257. return node && /^TD|^TH/.test(node.nodeName);
  258. };
  259. /**
  260. * find nearest ancestor predicate hit
  261. *
  262. * @param {Element} node
  263. * @param {Function} pred - predicate function
  264. */
  265. var ancestor = function (node, pred) {
  266. while (node) {
  267. if (pred(node)) { return node; }
  268. if (isEditable(node)) { break; }
  269. node = node.parentNode;
  270. }
  271. return null;
  272. };
  273. /**
  274. * returns new array of ancestor nodes (until predicate hit).
  275. *
  276. * @param {Element} node
  277. * @param {Function} [optional] pred - predicate function
  278. */
  279. var listAncestor = function (node, pred) {
  280. pred = pred || func.fail;
  281. var aAncestor = [];
  282. ancestor(node, function (el) {
  283. aAncestor.push(el);
  284. return pred(el);
  285. });
  286. return aAncestor;
  287. };
  288. /**
  289. * returns common ancestor node between two nodes.
  290. *
  291. * @param {Element} nodeA
  292. * @param {Element} nodeB
  293. */
  294. var commonAncestor = function (nodeA, nodeB) {
  295. var aAncestor = listAncestor(nodeA);
  296. for (var n = nodeB; n; n = n.parentNode) {
  297. if ($.inArray(n, aAncestor) > -1) { return n; }
  298. }
  299. return null; // difference document area
  300. };
  301. /**
  302. * listing all Nodes between two nodes.
  303. * FIXME: nodeA and nodeB must be sorted, use comparePoints later.
  304. *
  305. * @param {Element} nodeA
  306. * @param {Element} nodeB
  307. */
  308. var listBetween = function (nodeA, nodeB) {
  309. var aNode = [];
  310. var bStart = false, bEnd = false;
  311. // DFS(depth first search) with commonAcestor.
  312. (function fnWalk(node) {
  313. if (!node) { return; } // traverse fisnish
  314. if (node === nodeA) { bStart = true; } // start point
  315. if (bStart && !bEnd) { aNode.push(node); } // between
  316. if (node === nodeB) { bEnd = true; return; } // end point
  317. for (var idx = 0, sz = node.childNodes.length; idx < sz; idx++) {
  318. fnWalk(node.childNodes[idx]);
  319. }
  320. })(commonAncestor(nodeA, nodeB));
  321. return aNode;
  322. };
  323. /**
  324. * listing all previous siblings (until predicate hit).
  325. * @param {Element} node
  326. * @param {Function} [optional] pred - predicate function
  327. */
  328. var listPrev = function (node, pred) {
  329. pred = pred || func.fail;
  330. var aNext = [];
  331. while (node) {
  332. aNext.push(node);
  333. if (pred(node)) { break; }
  334. node = node.previousSibling;
  335. }
  336. return aNext;
  337. };
  338. /**
  339. * listing next siblings (until predicate hit).
  340. *
  341. * @param {Element} node
  342. * @param {Function} [pred] - predicate function
  343. */
  344. var listNext = function (node, pred) {
  345. pred = pred || func.fail;
  346. var aNext = [];
  347. while (node) {
  348. aNext.push(node);
  349. if (pred(node)) { break; }
  350. node = node.nextSibling;
  351. }
  352. return aNext;
  353. };
  354. /**
  355. * listing descendant nodes
  356. *
  357. * @param {Element} node
  358. * @param {Function} [pred] - predicate function
  359. */
  360. var listDescendant = function (node, pred) {
  361. var aDescendant = [];
  362. pred = pred || func.ok;
  363. // start DFS(depth first search) with node
  364. (function fnWalk(current) {
  365. if (node !== current && pred(current)) {
  366. aDescendant.push(current);
  367. }
  368. for (var idx = 0, sz = current.childNodes.length; idx < sz; idx++) {
  369. fnWalk(current.childNodes[idx]);
  370. }
  371. })(node);
  372. return aDescendant;
  373. };
  374. /**
  375. * insert node after preceding
  376. *
  377. * @param {Element} node
  378. * @param {Element} preceding - predicate function
  379. */
  380. var insertAfter = function (node, preceding) {
  381. var next = preceding.nextSibling, parent = preceding.parentNode;
  382. if (next) {
  383. parent.insertBefore(node, next);
  384. } else {
  385. parent.appendChild(node);
  386. }
  387. return node;
  388. };
  389. /**
  390. * append elements.
  391. *
  392. * @param {Element} node
  393. * @param {Collection} aChild
  394. */
  395. var appends = function (node, aChild) {
  396. $.each(aChild, function (idx, child) {
  397. node.appendChild(child);
  398. });
  399. return node;
  400. };
  401. var isText = makePredByNodeName('#text');
  402. /**
  403. * returns #text's text size or element's childNodes size
  404. *
  405. * @param {Element} node
  406. */
  407. var length = function (node) {
  408. if (isText(node)) { return node.nodeValue.length; }
  409. return node.childNodes.length;
  410. };
  411. /**
  412. * returns offset from parent.
  413. *
  414. * @param {Element} node
  415. */
  416. var position = function (node) {
  417. var offset = 0;
  418. while ((node = node.previousSibling)) { offset += 1; }
  419. return offset;
  420. };
  421. /**
  422. * return offsetPath(array of offset) from ancestor
  423. *
  424. * @param {Element} ancestor - ancestor node
  425. * @param {Element} node
  426. */
  427. var makeOffsetPath = function (ancestor, node) {
  428. var aAncestor = list.initial(listAncestor(node, func.eq(ancestor)));
  429. return $.map(aAncestor, position).reverse();
  430. };
  431. /**
  432. * return element from offsetPath(array of offset)
  433. *
  434. * @param {Element} ancestor - ancestor node
  435. * @param {array} aOffset - offsetPath
  436. */
  437. var fromOffsetPath = function (ancestor, aOffset) {
  438. var current = ancestor;
  439. for (var i = 0, sz = aOffset.length; i < sz; i++) {
  440. current = current.childNodes[aOffset[i]];
  441. }
  442. return current;
  443. };
  444. /**
  445. * split element or #text
  446. *
  447. * @param {Element} node
  448. * @param {Number} offset
  449. */
  450. var splitData = function (node, offset) {
  451. if (offset === 0) { return node; }
  452. if (offset >= length(node)) { return node.nextSibling; }
  453. // splitText
  454. if (isText(node)) { return node.splitText(offset); }
  455. // splitElement
  456. var child = node.childNodes[offset];
  457. node = insertAfter(node.cloneNode(false), node);
  458. return appends(node, listNext(child));
  459. };
  460. /**
  461. * split dom tree by boundaryPoint(pivot and offset)
  462. *
  463. * @param {Element} root
  464. * @param {Element} pivot - this will be boundaryPoint's node
  465. * @param {Number} offset - this will be boundaryPoint's offset
  466. */
  467. var split = function (root, pivot, offset) {
  468. var aAncestor = listAncestor(pivot, func.eq(root));
  469. if (aAncestor.length === 1) { return splitData(pivot, offset); }
  470. return aAncestor.reduce(function (node, parent) {
  471. var clone = parent.cloneNode(false);
  472. insertAfter(clone, parent);
  473. if (node === pivot) {
  474. node = splitData(node, offset);
  475. }
  476. appends(clone, listNext(node));
  477. return clone;
  478. });
  479. };
  480. /**
  481. * remove node, (bRemoveChild: remove child or not)
  482. * @param {Element} node
  483. * @param {Boolean} bRemoveChild
  484. */
  485. var remove = function (node, bRemoveChild) {
  486. if (!node || !node.parentNode) { return; }
  487. if (node.removeNode) { return node.removeNode(bRemoveChild); }
  488. var elParent = node.parentNode;
  489. if (!bRemoveChild) {
  490. var aNode = [];
  491. var i, sz;
  492. for (i = 0, sz = node.childNodes.length; i < sz; i++) {
  493. aNode.push(node.childNodes[i]);
  494. }
  495. for (i = 0, sz = aNode.length; i < sz; i++) {
  496. elParent.insertBefore(aNode[i], node);
  497. }
  498. }
  499. elParent.removeChild(node);
  500. };
  501. var html = function ($node) {
  502. return dom.isTextarea($node[0]) ? $node.val() : $node.html();
  503. };
  504. return {
  505. blank: agent.bMSIE ? '&nbsp;' : '<br/>',
  506. emptyPara: '<p><br/></p>',
  507. isEditable: isEditable,
  508. isControlSizing: isControlSizing,
  509. buildLayoutInfo: buildLayoutInfo,
  510. isText: isText,
  511. isPara: isPara,
  512. isList: isList,
  513. isTable: makePredByNodeName('TABLE'),
  514. isCell: isCell,
  515. isAnchor: makePredByNodeName('A'),
  516. isDiv: makePredByNodeName('DIV'),
  517. isLi: makePredByNodeName('LI'),
  518. isSpan: makePredByNodeName('SPAN'),
  519. isB: makePredByNodeName('B'),
  520. isU: makePredByNodeName('U'),
  521. isS: makePredByNodeName('S'),
  522. isI: makePredByNodeName('I'),
  523. isImg: makePredByNodeName('IMG'),
  524. isTextarea: makePredByNodeName('TEXTAREA'),
  525. ancestor: ancestor,
  526. listAncestor: listAncestor,
  527. listNext: listNext,
  528. listPrev: listPrev,
  529. listDescendant: listDescendant,
  530. commonAncestor: commonAncestor,
  531. listBetween: listBetween,
  532. insertAfter: insertAfter,
  533. position: position,
  534. makeOffsetPath: makeOffsetPath,
  535. fromOffsetPath: fromOffsetPath,
  536. split: split,
  537. remove: remove,
  538. html: html
  539. };
  540. })();
  541. var settings = {
  542. // version
  543. version: '0.5.2',
  544. /**
  545. * options
  546. */
  547. options: {
  548. width: null, // set editor width
  549. height: null, // set editable height, ex) 300
  550. focus: false, // set focus after initilize summernote
  551. tabsize: 4, // size of tab ex) 2 or 4
  552. styleWithSpan: true, // style with span (Chrome and FF only)
  553. disableLinkTarget: false, // hide link Target Checkbox
  554. disableDragAndDrop: false, // disable drag and drop event
  555. codemirror: { // codemirror options
  556. mode: 'text/html',
  557. lineNumbers: true
  558. },
  559. // language
  560. lang: 'en-US', // language 'en-US', 'ko-KR', ...
  561. direction: null, // text direction, ex) 'rtl'
  562. // default toolbar
  563. toolbar: [
  564. ['style', ['style']],
  565. ['font', ['bold', 'italic', 'underline', 'clear']],
  566. ['fontname', ['fontname']],
  567. // ['fontsize', ['fontsize']], // Still buggy
  568. ['color', ['color']],
  569. ['para', ['ul', 'ol', 'paragraph']],
  570. ['height', ['height']],
  571. ['table', ['table']],
  572. ['insert', ['link', 'picture', 'video']],
  573. ['view', ['fullscreen', 'codeview']],
  574. ['help', ['help']]
  575. ],
  576. // style tag
  577. styleTags: ['p', 'blockquote', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
  578. // default fontName
  579. defaultFontName: 'Arial',
  580. // fontName
  581. fontNames: [
  582. 'Serif', 'Sans', 'Arial', 'Arial Black', 'Courier',
  583. 'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande',
  584. 'Lucida Sans', 'Tahoma', 'Times', 'Times New Roman', 'Verdana'
  585. ],
  586. // pallete colors(n x n)
  587. colors: [
  588. ['#000000', '#424242', '#636363', '#9C9C94', '#CEC6CE', '#EFEFEF', '#F7F7F7', '#FFFFFF'],
  589. ['#FF0000', '#FF9C00', '#FFFF00', '#00FF00', '#00FFFF', '#0000FF', '#9C00FF', '#FF00FF'],
  590. ['#F7C6CE', '#FFE7CE', '#FFEFC6', '#D6EFD6', '#CEDEE7', '#CEE7F7', '#D6D6E7', '#E7D6DE'],
  591. ['#E79C9C', '#FFC69C', '#FFE79C', '#B5D6A5', '#A5C6CE', '#9CC6EF', '#B5A5D6', '#D6A5BD'],
  592. ['#E76363', '#F7AD6B', '#FFD663', '#94BD7B', '#73A5AD', '#6BADDE', '#8C7BC6', '#C67BA5'],
  593. ['#CE0000', '#E79439', '#EFC631', '#6BA54A', '#4A7B8C', '#3984C6', '#634AA5', '#A54A7B'],
  594. ['#9C0000', '#B56308', '#BD9400', '#397B21', '#104A5A', '#085294', '#311873', '#731842'],
  595. ['#630000', '#7B3900', '#846300', '#295218', '#083139', '#003163', '#21104A', '#4A1031']
  596. ],
  597. // fontSize
  598. fontSizes: ['8', '9', '10', '11', '12', '14', '18', '24', '36'],
  599. // lineHeight
  600. lineHeights: ['1.0', '1.2', '1.4', '1.5', '1.6', '1.8', '2.0', '3.0'],
  601. // callbacks
  602. oninit: null, // initialize
  603. onfocus: null, // editable has focus
  604. onblur: null, // editable out of focus
  605. onenter: null, // enter key pressed
  606. onkeyup: null, // keyup
  607. onkeydown: null, // keydown
  608. onImageUpload: null, // imageUploadHandler
  609. onImageUploadError: null, // imageUploadErrorHandler
  610. onToolbarClick: null,
  611. keyMap: {
  612. pc: {
  613. 'CTRL+Z': 'undo',
  614. 'CTRL+Y': 'redo',
  615. 'TAB': 'tab',
  616. 'SHIFT+TAB': 'untab',
  617. 'CTRL+B': 'bold',
  618. 'CTRL+I': 'italic',
  619. 'CTRL+U': 'underline',
  620. 'CTRL+SHIFT+S': 'strikethrough',
  621. 'CTRL+BACKSLASH': 'removeFormat',
  622. 'CTRL+SHIFT+L': 'justifyLeft',
  623. 'CTRL+SHIFT+E': 'justifyCenter',
  624. 'CTRL+SHIFT+R': 'justifyRight',
  625. 'CTRL+SHIFT+J': 'justifyFull',
  626. 'CTRL+SHIFT+NUM7': 'insertUnorderedList',
  627. 'CTRL+SHIFT+NUM8': 'insertOrderedList',
  628. 'CTRL+LEFTBRACKET': 'outdent',
  629. 'CTRL+RIGHTBRACKET': 'indent',
  630. 'CTRL+NUM0': 'formatPara',
  631. 'CTRL+NUM1': 'formatH1',
  632. 'CTRL+NUM2': 'formatH2',
  633. 'CTRL+NUM3': 'formatH3',
  634. 'CTRL+NUM4': 'formatH4',
  635. 'CTRL+NUM5': 'formatH5',
  636. 'CTRL+NUM6': 'formatH6',
  637. 'CTRL+ENTER': 'insertHorizontalRule'
  638. },
  639. mac: {
  640. 'CMD+Z': 'undo',
  641. 'CMD+SHIFT+Z': 'redo',
  642. 'TAB': 'tab',
  643. 'SHIFT+TAB': 'untab',
  644. 'CMD+B': 'bold',
  645. 'CMD+I': 'italic',
  646. 'CMD+U': 'underline',
  647. 'CMD+SHIFT+S': 'strikethrough',
  648. 'CMD+BACKSLASH': 'removeFormat',
  649. 'CMD+SHIFT+L': 'justifyLeft',
  650. 'CMD+SHIFT+E': 'justifyCenter',
  651. 'CMD+SHIFT+R': 'justifyRight',
  652. 'CMD+SHIFT+J': 'justifyFull',
  653. 'CMD+SHIFT+NUM7': 'insertUnorderedList',
  654. 'CMD+SHIFT+NUM8': 'insertOrderedList',
  655. 'CMD+LEFTBRACKET': 'outdent',
  656. 'CMD+RIGHTBRACKET': 'indent',
  657. 'CMD+NUM0': 'formatPara',
  658. 'CMD+NUM1': 'formatH1',
  659. 'CMD+NUM2': 'formatH2',
  660. 'CMD+NUM3': 'formatH3',
  661. 'CMD+NUM4': 'formatH4',
  662. 'CMD+NUM5': 'formatH5',
  663. 'CMD+NUM6': 'formatH6',
  664. 'CMD+ENTER': 'insertHorizontalRule'
  665. }
  666. }
  667. },
  668. // default language: en-US
  669. lang: {
  670. 'en-US': {
  671. font: {
  672. bold: 'Bold',
  673. italic: 'Italic',
  674. underline: 'Underline',
  675. strike: 'Strike',
  676. clear: 'Remove Font Style',
  677. height: 'Line Height',
  678. name: 'Font Family',
  679. size: 'Font Size'
  680. },
  681. image: {
  682. image: 'Picture',
  683. insert: 'Insert Image',
  684. resizeFull: 'Resize Full',
  685. resizeHalf: 'Resize Half',
  686. resizeQuarter: 'Resize Quarter',
  687. floatLeft: 'Float Left',
  688. floatRight: 'Float Right',
  689. floatNone: 'Float None',
  690. dragImageHere: 'Drag an image here',
  691. selectFromFiles: 'Select from files',
  692. url: 'Image URL',
  693. remove: 'Remove Image'
  694. },
  695. link: {
  696. link: 'Link',
  697. insert: 'Insert Link',
  698. unlink: 'Unlink',
  699. edit: 'Edit',
  700. textToDisplay: 'Text to display',
  701. url: 'To what URL should this link go?',
  702. openInNewWindow: 'Open in new window'
  703. },
  704. video: {
  705. video: 'Video',
  706. videoLink: 'Video Link',
  707. insert: 'Insert Video',
  708. url: 'Video URL?',
  709. providers: '(YouTube, Vimeo, Vine, Instagram, or DailyMotion)'
  710. },
  711. table: {
  712. table: 'Table'
  713. },
  714. hr: {
  715. insert: 'Insert Horizontal Rule'
  716. },
  717. style: {
  718. style: 'Style',
  719. normal: 'Normal',
  720. blockquote: 'Quote',
  721. pre: 'Code',
  722. h1: 'Header 1',
  723. h2: 'Header 2',
  724. h3: 'Header 3',
  725. h4: 'Header 4',
  726. h5: 'Header 5',
  727. h6: 'Header 6'
  728. },
  729. lists: {
  730. unordered: 'Unordered list',
  731. ordered: 'Ordered list'
  732. },
  733. options: {
  734. help: 'Help',
  735. fullscreen: 'Full Screen',
  736. codeview: 'Code View'
  737. },
  738. paragraph: {
  739. paragraph: 'Paragraph',
  740. outdent: 'Outdent',
  741. indent: 'Indent',
  742. left: 'Align left',
  743. center: 'Align center',
  744. right: 'Align right',
  745. justify: 'Justify full'
  746. },
  747. color: {
  748. recent: 'Recent Color',
  749. more: 'More Color',
  750. background: 'BackColor',
  751. foreground: 'FontColor',
  752. transparent: 'Transparent',
  753. setTransparent: 'Set transparent',
  754. reset: 'Reset',
  755. resetToDefault: 'Reset to default'
  756. },
  757. shortcut: {
  758. shortcuts: 'Keyboard shortcuts',
  759. close: 'Close',
  760. textFormatting: 'Text formatting',
  761. action: 'Action',
  762. paragraphFormatting: 'Paragraph formatting',
  763. documentStyle: 'Document Style'
  764. },
  765. history: {
  766. undo: 'Undo',
  767. redo: 'Redo'
  768. }
  769. }
  770. }
  771. };
  772. /**
  773. * Async functions which returns `Promise`
  774. */
  775. var async = (function () {
  776. /**
  777. * read contents of file as representing URL
  778. *
  779. * @param {File} file
  780. * @return {Promise} - then: sDataUrl
  781. */
  782. var readFileAsDataURL = function (file) {
  783. return $.Deferred(function (deferred) {
  784. $.extend(new FileReader(), {
  785. onload: function (e) {
  786. var sDataURL = e.target.result;
  787. deferred.resolve(sDataURL);
  788. },
  789. onerror: function () {
  790. deferred.reject(this);
  791. }
  792. }).readAsDataURL(file);
  793. }).promise();
  794. };
  795. /**
  796. * create `<image>` from url string
  797. *
  798. * @param {String} sUrl
  799. * @return {Promise} - then: $image
  800. */
  801. var createImage = function (sUrl) {
  802. return $.Deferred(function (deferred) {
  803. $('<img>').one('load', function () {
  804. deferred.resolve($(this));
  805. }).one('error abort', function () {
  806. deferred.reject($(this));
  807. }).css({
  808. display: 'none'
  809. }).appendTo(document.body).attr('src', sUrl);
  810. }).promise();
  811. };
  812. return {
  813. readFileAsDataURL: readFileAsDataURL,
  814. createImage: createImage
  815. };
  816. })();
  817. /**
  818. * Object for keycodes.
  819. */
  820. var key = {
  821. isEdit: function (keyCode) {
  822. return [8, 9, 13, 32].indexOf(keyCode) !== -1;
  823. },
  824. nameFromCode: {
  825. '8': 'BACKSPACE',
  826. '9': 'TAB',
  827. '13': 'ENTER',
  828. '32': 'SPACE',
  829. // Number: 0-9
  830. '48': 'NUM0',
  831. '49': 'NUM1',
  832. '50': 'NUM2',
  833. '51': 'NUM3',
  834. '52': 'NUM4',
  835. '53': 'NUM5',
  836. '54': 'NUM6',
  837. '55': 'NUM7',
  838. '56': 'NUM8',
  839. // Alphabet: a-z
  840. '66': 'B',
  841. '69': 'E',
  842. '73': 'I',
  843. '74': 'J',
  844. '75': 'K',
  845. '76': 'L',
  846. '82': 'R',
  847. '83': 'S',
  848. '85': 'U',
  849. '89': 'Y',
  850. '90': 'Z',
  851. '191': 'SLASH',
  852. '219': 'LEFTBRACKET',
  853. '220': 'BACKSLASH',
  854. '221': 'RIGHTBRACKET'
  855. }
  856. };
  857. /**
  858. * Style
  859. * @class
  860. */
  861. var Style = function () {
  862. /**
  863. * passing an array of style properties to .css()
  864. * will result in an object of property-value pairs.
  865. * (compability with version < 1.9)
  866. *
  867. * @param {jQuery} $obj
  868. * @param {Array} propertyNames - An array of one or more CSS properties.
  869. * @returns {Object}
  870. */
  871. var jQueryCSS = function ($obj, propertyNames) {
  872. if (agent.jqueryVersion < 1.9) {
  873. var result = {};
  874. $.each(propertyNames, function (idx, propertyName) {
  875. result[propertyName] = $obj.css(propertyName);
  876. });
  877. return result;
  878. }
  879. return $obj.css.call($obj, propertyNames);
  880. };
  881. /**
  882. * paragraph level style
  883. *
  884. * @param {WrappedRange} rng
  885. * @param {Object} oStyle
  886. */
  887. this.stylePara = function (rng, oStyle) {
  888. $.each(rng.nodes(dom.isPara), function (idx, elPara) {
  889. $(elPara).css(oStyle);
  890. });
  891. };
  892. /**
  893. * get current style on cursor
  894. *
  895. * @param {WrappedRange} rng
  896. * @param {Element} elTarget - target element on event
  897. * @return {Object} - object contains style properties.
  898. */
  899. this.current = function (rng, elTarget) {
  900. var $cont = $(dom.isText(rng.sc) ? rng.sc.parentNode : rng.sc);
  901. var properties = ['font-family', 'font-size', 'text-align', 'list-style-type', 'line-height'];
  902. var oStyle = jQueryCSS($cont, properties) || {};
  903. oStyle['font-size'] = parseInt(oStyle['font-size'], 10);
  904. // document.queryCommandState for toggle state
  905. oStyle['font-bold'] = document.queryCommandState('bold') ? 'bold' : 'normal';
  906. oStyle['font-italic'] = document.queryCommandState('italic') ? 'italic' : 'normal';
  907. oStyle['font-underline'] = document.queryCommandState('underline') ? 'underline' : 'normal';
  908. oStyle['font-strikethrough'] = document.queryCommandState('strikeThrough') ? 'strikethrough' : 'normal';
  909. // list-style-type to list-style(unordered, ordered)
  910. if (!rng.isOnList()) {
  911. oStyle['list-style'] = 'none';
  912. } else {
  913. var aOrderedType = ['circle', 'disc', 'disc-leading-zero', 'square'];
  914. var bUnordered = $.inArray(oStyle['list-style-type'], aOrderedType) > -1;
  915. oStyle['list-style'] = bUnordered ? 'unordered' : 'ordered';
  916. }
  917. var elPara = dom.ancestor(rng.sc, dom.isPara);
  918. if (elPara && elPara.style['line-height']) {
  919. oStyle['line-height'] = elPara.style.lineHeight;
  920. } else {
  921. var lineHeight = parseInt(oStyle['line-height'], 10) / parseInt(oStyle['font-size'], 10);
  922. oStyle['line-height'] = lineHeight.toFixed(1);
  923. }
  924. oStyle.image = dom.isImg(elTarget) && elTarget;
  925. oStyle.anchor = rng.isOnAnchor() && dom.ancestor(rng.sc, dom.isAnchor);
  926. oStyle.aAncestor = dom.listAncestor(rng.sc, dom.isEditable);
  927. return oStyle;
  928. };
  929. };
  930. /**
  931. * range module
  932. */
  933. var range = (function () {
  934. var bW3CRangeSupport = !!document.createRange;
  935. /**
  936. * return boundaryPoint from TextRange, inspired by Andy Na's HuskyRange.js
  937. * @param {TextRange} textRange
  938. * @param {Boolean} bStart
  939. * @return {BoundaryPoint}
  940. */
  941. var textRange2bp = function (textRange, bStart) {
  942. var elCont = textRange.parentElement(), nOffset;
  943. var tester = document.body.createTextRange(), elPrevCont;
  944. var aChild = list.from(elCont.childNodes);
  945. for (nOffset = 0; nOffset < aChild.length; nOffset++) {
  946. if (dom.isText(aChild[nOffset])) { continue; }
  947. tester.moveToElementText(aChild[nOffset]);
  948. if (tester.compareEndPoints('StartToStart', textRange) >= 0) { break; }
  949. elPrevCont = aChild[nOffset];
  950. }
  951. if (nOffset !== 0 && dom.isText(aChild[nOffset - 1])) {
  952. var textRangeStart = document.body.createTextRange(), elCurText = null;
  953. textRangeStart.moveToElementText(elPrevCont || elCont);
  954. textRangeStart.collapse(!elPrevCont);
  955. elCurText = elPrevCont ? elPrevCont.nextSibling : elCont.firstChild;
  956. var pointTester = textRange.duplicate();
  957. pointTester.setEndPoint('StartToStart', textRangeStart);
  958. var nTextCount = pointTester.text.replace(/[\r\n]/g, '').length;
  959. while (nTextCount > elCurText.nodeValue.length && elCurText.nextSibling) {
  960. nTextCount -= elCurText.nodeValue.length;
  961. elCurText = elCurText.nextSibling;
  962. }
  963. /* jshint ignore:start */
  964. var sDummy = elCurText.nodeValue; //enforce IE to re-reference elCurText, hack
  965. /* jshint ignore:end */
  966. if (bStart && elCurText.nextSibling && dom.isText(elCurText.nextSibling) &&
  967. nTextCount === elCurText.nodeValue.length) {
  968. nTextCount -= elCurText.nodeValue.length;
  969. elCurText = elCurText.nextSibling;
  970. }
  971. elCont = elCurText;
  972. nOffset = nTextCount;
  973. }
  974. return {cont: elCont, offset: nOffset};
  975. };
  976. /**
  977. * return TextRange from boundary point (inspired by google closure-library)
  978. * @param {BoundaryPoint} bp
  979. * @return {TextRange}
  980. */
  981. var bp2textRange = function (bp) {
  982. var textRangeInfo = function (elCont, nOffset) {
  983. var elNode, bCollapseToStart;
  984. if (dom.isText(elCont)) {
  985. var aPrevText = dom.listPrev(elCont, func.not(dom.isText));
  986. var elPrevCont = list.last(aPrevText).previousSibling;
  987. elNode = elPrevCont || elCont.parentNode;
  988. nOffset += list.sum(list.tail(aPrevText), dom.length);
  989. bCollapseToStart = !elPrevCont;
  990. } else {
  991. elNode = elCont.childNodes[nOffset] || elCont;
  992. if (dom.isText(elNode)) {
  993. return textRangeInfo(elNode, nOffset);
  994. }
  995. nOffset = 0;
  996. bCollapseToStart = false;
  997. }
  998. return {cont: elNode, collapseToStart: bCollapseToStart, offset: nOffset};
  999. };
  1000. var textRange = document.body.createTextRange();
  1001. var info = textRangeInfo(bp.cont, bp.offset);
  1002. textRange.moveToElementText(info.cont);
  1003. textRange.collapse(info.collapseToStart);
  1004. textRange.moveStart('character', info.offset);
  1005. return textRange;
  1006. };
  1007. /**
  1008. * Wrapped Range
  1009. *
  1010. * @param {Element} sc - start container
  1011. * @param {Number} so - start offset
  1012. * @param {Element} ec - end container
  1013. * @param {Number} eo - end offset
  1014. */
  1015. var WrappedRange = function (sc, so, ec, eo) {
  1016. this.sc = sc;
  1017. this.so = so;
  1018. this.ec = ec;
  1019. this.eo = eo;
  1020. // nativeRange: get nativeRange from sc, so, ec, eo
  1021. var nativeRange = function () {
  1022. if (bW3CRangeSupport) {
  1023. var w3cRange = document.createRange();
  1024. w3cRange.setStart(sc, so);
  1025. w3cRange.setEnd(ec, eo);
  1026. return w3cRange;
  1027. } else {
  1028. var textRange = bp2textRange({cont: sc, offset: so});
  1029. textRange.setEndPoint('EndToEnd', bp2textRange({cont: ec, offset: eo}));
  1030. return textRange;
  1031. }
  1032. };
  1033. /**
  1034. * select update visible range
  1035. */
  1036. this.select = function () {
  1037. var nativeRng = nativeRange();
  1038. if (bW3CRangeSupport) {
  1039. var selection = document.getSelection();
  1040. if (selection.rangeCount > 0) { selection.removeAllRanges(); }
  1041. selection.addRange(nativeRng);
  1042. } else {
  1043. nativeRng.select();
  1044. }
  1045. };
  1046. /**
  1047. * returns matched nodes on range
  1048. *
  1049. * @param {Function} pred - predicate function
  1050. * @return {Element[]}
  1051. */
  1052. this.nodes = function (pred) {
  1053. var aNode = dom.listBetween(sc, ec);
  1054. var aMatched = list.compact($.map(aNode, function (node) {
  1055. return dom.ancestor(node, pred);
  1056. }));
  1057. return $.map(list.clusterBy(aMatched, func.eq2), list.head);
  1058. };
  1059. /**
  1060. * returns commonAncestor of range
  1061. * @return {Element} - commonAncestor
  1062. */
  1063. this.commonAncestor = function () {
  1064. return dom.commonAncestor(sc, ec);
  1065. };
  1066. /**
  1067. * makeIsOn: return isOn(pred) function
  1068. */
  1069. var makeIsOn = function (pred) {
  1070. return function () {
  1071. var elAncestor = dom.ancestor(sc, pred);
  1072. return !!elAncestor && (elAncestor === dom.ancestor(ec, pred));
  1073. };
  1074. };
  1075. // isOnEditable: judge whether range is on editable or not
  1076. this.isOnEditable = makeIsOn(dom.isEditable);
  1077. // isOnList: judge whether range is on list node or not
  1078. this.isOnList = makeIsOn(dom.isList);
  1079. // isOnAnchor: judge whether range is on anchor node or not
  1080. this.isOnAnchor = makeIsOn(dom.isAnchor);
  1081. // isOnAnchor: judge whether range is on cell node or not
  1082. this.isOnCell = makeIsOn(dom.isCell);
  1083. // isCollapsed: judge whether range was collapsed
  1084. this.isCollapsed = function () { return sc === ec && so === eo; };
  1085. /**
  1086. * insert node at current cursor
  1087. * @param {Element} node
  1088. */
  1089. this.insertNode = function (node) {
  1090. var nativeRng = nativeRange();
  1091. if (bW3CRangeSupport) {
  1092. nativeRng.insertNode(node);
  1093. } else {
  1094. nativeRng.pasteHTML(node.outerHTML); // NOTE: missing node reference.
  1095. }
  1096. };
  1097. this.toString = function () {
  1098. var nativeRng = nativeRange();
  1099. return bW3CRangeSupport ? nativeRng.toString() : nativeRng.text;
  1100. };
  1101. // bookmark: offsetPath bookmark
  1102. this.bookmark = function (elEditable) {
  1103. return {
  1104. s: { path: dom.makeOffsetPath(elEditable, sc), offset: so },
  1105. e: { path: dom.makeOffsetPath(elEditable, ec), offset: eo }
  1106. };
  1107. };
  1108. };
  1109. return {
  1110. /**
  1111. * create Range Object From arguments or Browser Selection
  1112. *
  1113. * @param {Element} sc - start container
  1114. * @param {Number} so - start offset
  1115. * @param {Element} ec - end container
  1116. * @param {Number} eo - end offset
  1117. */
  1118. create : function (sc, so, ec, eo) {
  1119. if (arguments.length === 0) { // from Browser Selection
  1120. if (bW3CRangeSupport) { // webkit, firefox
  1121. var selection = document.getSelection();
  1122. if (selection.rangeCount === 0) { return null; }
  1123. var nativeRng = selection.getRangeAt(0);
  1124. sc = nativeRng.startContainer;
  1125. so = nativeRng.startOffset;
  1126. ec = nativeRng.endContainer;
  1127. eo = nativeRng.endOffset;
  1128. } else { // IE8: TextRange
  1129. var textRange = document.selection.createRange();
  1130. var textRangeEnd = textRange.duplicate();
  1131. textRangeEnd.collapse(false);
  1132. var textRangeStart = textRange;
  1133. textRangeStart.collapse(true);
  1134. var bpStart = textRange2bp(textRangeStart, true),
  1135. bpEnd = textRange2bp(textRangeEnd, false);
  1136. sc = bpStart.cont;
  1137. so = bpStart.offset;
  1138. ec = bpEnd.cont;
  1139. eo = bpEnd.offset;
  1140. }
  1141. } else if (arguments.length === 2) { //collapsed
  1142. ec = sc;
  1143. eo = so;
  1144. }
  1145. return new WrappedRange(sc, so, ec, eo);
  1146. },
  1147. /**
  1148. * create WrappedRange from node
  1149. *
  1150. * @param {Element} node
  1151. * @return {WrappedRange}
  1152. */
  1153. createFromNode: function (node) {
  1154. return this.create(node, 0, node, 1);
  1155. },
  1156. /**
  1157. * create WrappedRange from Bookmark
  1158. *
  1159. * @param {Element} elEditable
  1160. * @param {Obkect} bookmark
  1161. * @return {WrappedRange}
  1162. */
  1163. createFromBookmark : function (elEditable, bookmark) {
  1164. var sc = dom.fromOffsetPath(elEditable, bookmark.s.path);
  1165. var so = bookmark.s.offset;
  1166. var ec = dom.fromOffsetPath(elEditable, bookmark.e.path);
  1167. var eo = bookmark.e.offset;
  1168. return new WrappedRange(sc, so, ec, eo);
  1169. }
  1170. };
  1171. })();
  1172. /**
  1173. * Table
  1174. * @class
  1175. */
  1176. var Table = function () {
  1177. /**
  1178. * handle tab key
  1179. *
  1180. * @param {WrappedRange} rng
  1181. * @param {Boolean} bShift
  1182. */
  1183. this.tab = function (rng, bShift) {
  1184. var elCell = dom.ancestor(rng.commonAncestor(), dom.isCell);
  1185. var elTable = dom.ancestor(elCell, dom.isTable);
  1186. var aCell = dom.listDescendant(elTable, dom.isCell);
  1187. var elNext = list[bShift ? 'prev' : 'next'](aCell, elCell);
  1188. if (elNext) {
  1189. range.create(elNext, 0).select();
  1190. }
  1191. };
  1192. /**
  1193. * create empty table element
  1194. *
  1195. * @param {Number} nRow
  1196. * @param {Number} nCol
  1197. */
  1198. this.createTable = function (nCol, nRow) {
  1199. var aTD = [], sTD;
  1200. for (var idxCol = 0; idxCol < nCol; idxCol++) {
  1201. aTD.push('<td>' + dom.blank + '</td>');
  1202. }
  1203. sTD = aTD.join('');
  1204. var aTR = [], sTR;
  1205. for (var idxRow = 0; idxRow < nRow; idxRow++) {
  1206. aTR.push('<tr>' + sTD + '</tr>');
  1207. }
  1208. sTR = aTR.join('');
  1209. var sTable = '<table class="table table-bordered">' + sTR + '</table>';
  1210. return $(sTable)[0];
  1211. };
  1212. };
  1213. /**
  1214. * Editor
  1215. * @class
  1216. */
  1217. var Editor = function () {
  1218. var style = new Style();
  1219. var table = new Table();
  1220. /**
  1221. * save current range
  1222. *
  1223. * @param {jQuery} $editable
  1224. */
  1225. this.saveRange = function ($editable) {
  1226. $editable.data('range', range.create());
  1227. };
  1228. /**
  1229. * restore lately range
  1230. *
  1231. * @param {jQuery} $editable
  1232. */
  1233. this.restoreRange = function ($editable) {
  1234. var rng = $editable.data('range');
  1235. if (rng) { rng.select(); }
  1236. };
  1237. /**
  1238. * current style
  1239. * @param {Element} elTarget
  1240. */
  1241. this.currentStyle = function (elTarget) {
  1242. var rng = range.create();
  1243. return rng.isOnEditable() && style.current(rng, elTarget);
  1244. };
  1245. /**
  1246. * undo
  1247. * @param {jQuery} $editable
  1248. */
  1249. this.undo = function ($editable) {
  1250. $editable.data('NoteHistory').undo($editable);
  1251. };
  1252. /**
  1253. * redo
  1254. * @param {jQuery} $editable
  1255. */
  1256. this.redo = function ($editable) {
  1257. $editable.data('NoteHistory').redo($editable);
  1258. };
  1259. /**
  1260. * record Undo
  1261. * @param {jQuery} $editable
  1262. */
  1263. var recordUndo = this.recordUndo = function ($editable) {
  1264. $editable.data('NoteHistory').recordUndo($editable);
  1265. };
  1266. /* jshint ignore:start */
  1267. // native commands(with execCommand), generate function for execCommand
  1268. var aCmd = ['bold', 'italic', 'underline', 'strikethrough',
  1269. 'justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull',
  1270. 'insertOrderedList', 'insertUnorderedList',
  1271. 'indent', 'outdent', 'formatBlock', 'removeFormat',
  1272. 'backColor', 'foreColor', 'insertHorizontalRule', 'fontName'];
  1273. for (var idx = 0, len = aCmd.length; idx < len; idx ++) {
  1274. this[aCmd[idx]] = (function (sCmd) {
  1275. return function ($editable, sValue) {
  1276. recordUndo($editable);
  1277. document.execCommand(sCmd, false, sValue);
  1278. };
  1279. })(aCmd[idx]);
  1280. }
  1281. /* jshint ignore:end */
  1282. /**
  1283. * @param {jQuery} $editable
  1284. * @param {WrappedRange} rng
  1285. * @param {Number} nTabsize
  1286. */
  1287. var insertTab = function ($editable, rng, nTabsize) {
  1288. recordUndo($editable);
  1289. var sNbsp = new Array(nTabsize + 1).join('&nbsp;');
  1290. rng.insertNode($('<span id="noteTab">' + sNbsp + '</span>')[0]);
  1291. var $tab = $('#noteTab').removeAttr('id');
  1292. rng = range.create($tab[0], 1);
  1293. rng.select();
  1294. dom.remove($tab[0]);
  1295. };
  1296. /**
  1297. * handle tab key
  1298. * @param {jQuery} $editable
  1299. * @param {Number} nTabsize
  1300. * @param {Boolean} bShift
  1301. */
  1302. this.tab = function ($editable, options) {
  1303. var rng = range.create();
  1304. if (rng.isCollapsed() && rng.isOnCell()) {
  1305. table.tab(rng);
  1306. } else {
  1307. insertTab($editable, rng, options.tabsize);
  1308. }
  1309. };
  1310. /**
  1311. * handle shift+tab key
  1312. */
  1313. this.untab = function () {
  1314. var rng = range.create();
  1315. if (rng.isCollapsed() && rng.isOnCell()) {
  1316. table.tab(rng, true);
  1317. }
  1318. };
  1319. /**
  1320. * insert image
  1321. *
  1322. * @param {jQuery} $editable
  1323. * @param {String} sUrl
  1324. */
  1325. this.insertImage = function ($editable, sUrl) {
  1326. async.createImage(sUrl).then(function ($image) {
  1327. recordUndo($editable);
  1328. $image.css({
  1329. display: '',
  1330. width: Math.min($editable.width(), $image.width())
  1331. });
  1332. range.create().insertNode($image[0]);
  1333. }).fail(function () {
  1334. var callbacks = $editable.data('callbacks');
  1335. if (callbacks.onImageUploadError) {
  1336. callbacks.onImageUploadError();
  1337. }
  1338. });
  1339. };
  1340. /**
  1341. * insert video
  1342. * @param {jQuery} $editable
  1343. * @param {String} sUrl
  1344. */
  1345. this.insertVideo = function ($editable, sUrl) {
  1346. recordUndo($editable);
  1347. // video url patterns(youtube, instagram, vimeo, dailymotion)
  1348. var ytRegExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
  1349. var ytMatch = sUrl.match(ytRegExp);
  1350. var igRegExp = /\/\/instagram.com\/p\/(.[a-zA-Z0-9]*)/;
  1351. var igMatch = sUrl.match(igRegExp);
  1352. var vRegExp = /\/\/vine.co\/v\/(.[a-zA-Z0-9]*)/;
  1353. var vMatch = sUrl.match(vRegExp);
  1354. var vimRegExp = /\/\/(player.)?vimeo.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/;
  1355. var vimMatch = sUrl.match(vimRegExp);
  1356. var dmRegExp = /.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/;
  1357. var dmMatch = sUrl.match(dmRegExp);
  1358. var $video;
  1359. if (ytMatch && ytMatch[2].length === 11) {
  1360. var youtubeId = ytMatch[2];
  1361. $video = $('<iframe>')
  1362. .attr('src', '//www.youtube.com/embed/' + youtubeId)
  1363. .attr('width', '640').attr('height', '360');
  1364. } else if (igMatch && igMatch[0].length > 0) {
  1365. $video = $('<iframe>')
  1366. .attr('src', igMatch[0] + '/embed/')
  1367. .attr('width', '612').attr('height', '710')
  1368. .attr('scrolling', 'no')
  1369. .attr('allowtransparency', 'true');
  1370. } else if (vMatch && vMatch[0].length > 0) {
  1371. $video = $('<iframe>')
  1372. .attr('src', vMatch[0] + '/embed/simple')
  1373. .attr('width', '600').attr('height', '600')
  1374. .attr('class', 'vine-embed');
  1375. } else if (vimMatch && vimMatch[3].length > 0) {
  1376. $video = $('<iframe webkitallowfullscreen mozallowfullscreen allowfullscreen>')
  1377. .attr('src', '//player.vimeo.com/video/' + vimMatch[3])
  1378. .attr('width', '640').attr('height', '360');
  1379. } else if (dmMatch && dmMatch[2].length > 0) {
  1380. $video = $('<iframe>')
  1381. .attr('src', '//www.dailymotion.com/embed/video/' + dmMatch[2])
  1382. .attr('width', '640').attr('height', '360');
  1383. } else {
  1384. // this is not a known video link. Now what, Cat? Now what?
  1385. }
  1386. if ($video) {
  1387. $video.attr('frameborder', 0);
  1388. range.create().insertNode($video[0]);
  1389. }
  1390. };
  1391. /**
  1392. * formatBlock
  1393. *
  1394. * @param {jQuery} $editable
  1395. * @param {String} sTagName
  1396. */
  1397. this.formatBlock = function ($editable, sTagName) {
  1398. recordUndo($editable);
  1399. sTagName = agent.bMSIE ? '<' + sTagName + '>' : sTagName;
  1400. document.execCommand('FormatBlock', false, sTagName);
  1401. };
  1402. this.formatPara = function ($editable) {
  1403. this.formatBlock($editable, 'P');
  1404. };
  1405. /* jshint ignore:start */
  1406. for (var idx = 1; idx <= 6; idx ++) {
  1407. this['formatH' + idx] = function (idx) {
  1408. return function ($editable) {
  1409. this.formatBlock($editable, 'H' + idx);
  1410. };
  1411. }(idx);
  1412. };
  1413. /* jshint ignore:end */
  1414. /**
  1415. * fontsize
  1416. * FIXME: Still buggy
  1417. *
  1418. * @param {jQuery} $editable
  1419. * @param {String} sValue - px
  1420. */
  1421. this.fontSize = function ($editable, sValue) {
  1422. recordUndo($editable);
  1423. document.execCommand('fontSize', false, 3);
  1424. if (agent.bFF) {
  1425. // firefox: <font size="3"> to <span style='font-size={sValue}px;'>, buggy
  1426. $editable.find('font[size=3]').removeAttr('size').css('font-size', sValue + 'px');
  1427. } else {
  1428. // chrome: <span style="font-size: medium"> to <span style='font-size={sValue}px;'>
  1429. $editable.find('span').filter(function () {
  1430. return this.style.fontSize === 'medium';
  1431. }).css('font-size', sValue + 'px');
  1432. }
  1433. };
  1434. /**
  1435. * lineHeight
  1436. * @param {jQuery} $editable
  1437. * @param {String} sValue
  1438. */
  1439. this.lineHeight = function ($editable, sValue) {
  1440. recordUndo($editable);
  1441. style.stylePara(range.create(), {lineHeight: sValue});
  1442. };
  1443. /**
  1444. * unlink
  1445. * @param {jQuery} $editable
  1446. */
  1447. this.unlink = function ($editable) {
  1448. var rng = range.create();
  1449. if (rng.isOnAnchor()) {
  1450. recordUndo($editable);
  1451. var elAnchor = dom.ancestor(rng.sc, dom.isAnchor);
  1452. rng = range.createFromNode(elAnchor);
  1453. rng.select();
  1454. document.execCommand('unlink');
  1455. }
  1456. };
  1457. /**
  1458. * create link
  1459. *
  1460. * @param {jQuery} $editable
  1461. * @param {String} sLinkUrl
  1462. * @param {Boolean} bNewWindow
  1463. */
  1464. this.createLink = function ($editable, sLinkUrl, bNewWindow) {
  1465. var rng = range.create();
  1466. recordUndo($editable);
  1467. // protocol
  1468. var sLinkUrlWithProtocol = sLinkUrl;
  1469. if (sLinkUrl.indexOf('@') !== -1 && sLinkUrl.indexOf(':') === -1) {
  1470. sLinkUrlWithProtocol = 'mailto:' + sLinkUrl;
  1471. } else if (sLinkUrl.indexOf('://') === -1) {
  1472. sLinkUrlWithProtocol = 'http://' + sLinkUrl;
  1473. }
  1474. // createLink when range collapsed (IE, Firefox).
  1475. if ((agent.bMSIE || agent.bFF) && rng.isCollapsed()) {
  1476. rng.insertNode($('<A id="linkAnchor">' + sLinkUrl + '</A>')[0]);
  1477. var $anchor = $('#linkAnchor').attr('href', sLinkUrlWithProtocol).removeAttr('id');
  1478. rng = range.createFromNode($anchor[0]);
  1479. rng.select();
  1480. } else {
  1481. document.execCommand('createlink', false, sLinkUrlWithProtocol);
  1482. rng = range.create();
  1483. }
  1484. // target
  1485. $.each(rng.nodes(dom.isAnchor), function (idx, elAnchor) {
  1486. if (bNewWindow) {
  1487. $(elAnchor).attr('target', '_blank');
  1488. } else {
  1489. $(elAnchor).removeAttr('target');
  1490. }
  1491. });
  1492. };
  1493. /**
  1494. * get link info
  1495. *
  1496. * @return {Promise}
  1497. */
  1498. this.getLinkInfo = function () {
  1499. var rng = range.create();
  1500. var bNewWindow = true;
  1501. var sUrl = '';
  1502. // If range on anchor expand range on anchor(for edit link).
  1503. if (rng.isOnAnchor()) {
  1504. var elAnchor = dom.ancestor(rng.sc, dom.isAnchor);
  1505. rng = range.createFromNode(elAnchor);
  1506. bNewWindow = $(elAnchor).attr('target') === '_blank';
  1507. sUrl = elAnchor.href;
  1508. }
  1509. return {
  1510. text: rng.toString(),
  1511. url: sUrl,
  1512. newWindow: bNewWindow
  1513. };
  1514. };
  1515. /**
  1516. * get video info
  1517. *
  1518. * @return {Object}
  1519. */
  1520. this.getVideoInfo = function () {
  1521. var rng = range.create();
  1522. if (rng.isOnAnchor()) {
  1523. var elAnchor = dom.ancestor(rng.sc, dom.isAnchor);
  1524. rng = range.createFromNode(elAnchor);
  1525. }
  1526. return {
  1527. text: rng.toString()
  1528. };
  1529. };
  1530. this.color = function ($editable, sObjColor) {
  1531. var oColor = JSON.parse(sObjColor);
  1532. var foreColor = oColor.foreColor, backColor = oColor.backColor;
  1533. recordUndo($editable);
  1534. if (foreColor) { document.execCommand('foreColor', false, foreColor); }
  1535. if (backColor) { document.execCommand('backColor', false, backColor); }
  1536. };
  1537. this.insertTable = function ($editable, sDim) {
  1538. recordUndo($editable);
  1539. var aDim = sDim.split('x');
  1540. range.create().insertNode(table.createTable(aDim[0], aDim[1]));
  1541. };
  1542. /**
  1543. * @param {jQuery} $editable
  1544. * @param {String} sValue
  1545. * @param {jQuery} $target
  1546. */
  1547. this.floatMe = function ($editable, sValue, $target) {
  1548. recordUndo($editable);
  1549. $target.css('float', sValue);
  1550. };
  1551. /**
  1552. * resize overlay element
  1553. * @param {jQuery} $editable
  1554. * @param {String} sValue
  1555. * @param {jQuery} $target - target element
  1556. */
  1557. this.resize = function ($editable, sValue, $target) {
  1558. recordUndo($editable);
  1559. $target.css({
  1560. width: $editable.width() * sValue + 'px',
  1561. height: ''
  1562. });
  1563. };
  1564. /**
  1565. * @param {Position} pos
  1566. * @param {jQuery} $target - target element
  1567. * @param {Boolean} [bKeepRatio] - keep ratio
  1568. */
  1569. this.resizeTo = function (pos, $target, bKeepRatio) {
  1570. var szImage;
  1571. if (bKeepRatio) {
  1572. var newRatio = pos.y / pos.x;
  1573. var ratio = $target.data('ratio');
  1574. szImage = {
  1575. width: ratio > newRatio ? pos.x : pos.y / ratio,
  1576. height: ratio > newRatio ? pos.x * ratio : pos.y
  1577. };
  1578. } else {
  1579. szImage = {
  1580. width: pos.x,
  1581. height: pos.y
  1582. };
  1583. }
  1584. $target.css(szImage);
  1585. };
  1586. /**
  1587. * remove media object
  1588. *
  1589. * @param {jQuery} $editable
  1590. * @param {String} sValue - dummy argument (for keep interface)
  1591. * @param {jQuery} $target - target element
  1592. */
  1593. this.removeMedia = function ($editable, sValue, $target) {
  1594. recordUndo($editable);
  1595. $target.detach();
  1596. };
  1597. };
  1598. /**
  1599. * History
  1600. * @class
  1601. */
  1602. var History = function () {
  1603. var aUndo = [], aRedo = [];
  1604. var makeSnap = function ($editable) {
  1605. var elEditable = $editable[0], rng = range.create();
  1606. return {
  1607. contents: $editable.html(),
  1608. bookmark: rng.bookmark(elEditable),
  1609. scrollTop: $editable.scrollTop()
  1610. };
  1611. };
  1612. var applySnap = function ($editable, oSnap) {
  1613. $editable.html(oSnap.contents).scrollTop(oSnap.scrollTop);
  1614. range.createFromBookmark($editable[0], oSnap.bookmark).select();
  1615. };
  1616. this.undo = function ($editable) {
  1617. var oSnap = makeSnap($editable);
  1618. if (aUndo.length === 0) { return; }
  1619. applySnap($editable, aUndo.pop());
  1620. aRedo.push(oSnap);
  1621. };
  1622. this.redo = function ($editable) {
  1623. var oSnap = makeSnap($editable);
  1624. if (aRedo.length === 0) { return; }
  1625. applySnap($editable, aRedo.pop());
  1626. aUndo.push(oSnap);
  1627. };
  1628. this.recordUndo = function ($editable) {
  1629. aRedo = [];
  1630. aUndo.push(makeSnap($editable));
  1631. };
  1632. };
  1633. /**
  1634. * Toolbar
  1635. */
  1636. var Toolbar = function () {
  1637. /**
  1638. * update button status
  1639. *
  1640. * @param {jQuery} $toolbar
  1641. * @param {Object} oStyle
  1642. */
  1643. this.update = function ($toolbar, oStyle) {
  1644. /**
  1645. * handle dropdown's check mark (for fontname, fontsize, lineHeight).
  1646. * @param {jQuery} $btn
  1647. * @param {Number} nValue
  1648. */
  1649. var checkDropdownMenu = function ($btn, nValue) {
  1650. $btn.find('.dropdown-menu li a').each(function () {
  1651. // always compare string to avoid creating another func.
  1652. var bChecked = ($(this).data('value') + '') === (nValue + '');
  1653. this.className = bChecked ? 'checked' : '';
  1654. });
  1655. };
  1656. /**
  1657. * update button state(active or not).
  1658. *
  1659. * @param {String} sSelector
  1660. * @param {Function} pred
  1661. */
  1662. var btnState = function (sSelector, pred) {
  1663. var $btn = $toolbar.find(sSelector);
  1664. $btn.toggleClass('active', pred());
  1665. };
  1666. // fontname
  1667. var $fontname = $toolbar.find('.note-fontname');
  1668. if ($fontname.length > 0) {
  1669. var selectedFont = oStyle['font-family'];
  1670. if (!!selectedFont) {
  1671. selectedFont = list.head(selectedFont.split(','));
  1672. selectedFont = selectedFont.replace(/\'/g, '');
  1673. $fontname.find('.note-current-fontname').text(selectedFont);
  1674. checkDropdownMenu($fontname, selectedFont);
  1675. }
  1676. }
  1677. // fontsize
  1678. var $fontsize = $toolbar.find('.note-fontsize');
  1679. $fontsize.find('.note-current-fontsize').text(oStyle['font-size']);
  1680. checkDropdownMenu($fontsize, parseFloat(oStyle['font-size']));
  1681. // lineheight
  1682. var $lineHeight = $toolbar.find('.note-height');
  1683. checkDropdownMenu($lineHeight, parseFloat(oStyle['line-height']));
  1684. btnState('button[data-event="bold"]', function () {
  1685. return oStyle['font-bold'] === 'bold';
  1686. });
  1687. btnState('button[data-event="italic"]', function () {
  1688. return oStyle['font-italic'] === 'italic';
  1689. });
  1690. btnState('button[data-event="underline"]', function () {
  1691. return oStyle['font-underline'] === 'underline';
  1692. });
  1693. btnState('button[data-event="strikethrough"]', function () {
  1694. return oStyle['font-strikethrough'] === 'strikethrough';
  1695. });
  1696. btnState('button[data-event="justifyLeft"]', function () {
  1697. return oStyle['text-align'] === 'left' || oStyle['text-align'] === 'start';
  1698. });
  1699. btnState('button[data-event="justifyCenter"]', function () {
  1700. return oStyle['text-align'] === 'center';
  1701. });
  1702. btnState('button[data-event="justifyRight"]', function () {
  1703. return oStyle['text-align'] === 'right';
  1704. });
  1705. btnState('button[data-event="justifyFull"]', function () {
  1706. return oStyle['text-align'] === 'justify';
  1707. });
  1708. btnState('button[data-event="insertUnorderedList"]', function () {
  1709. return oStyle['list-style'] === 'unordered';
  1710. });
  1711. btnState('button[data-event="insertOrderedList"]', function () {
  1712. return oStyle['list-style'] === 'ordered';
  1713. });
  1714. };
  1715. /**
  1716. * update recent color
  1717. *
  1718. * @param {Element} elBtn
  1719. * @param {String} sEvent
  1720. * @param {sValue} sValue
  1721. */
  1722. this.updateRecentColor = function (elBtn, sEvent, sValue) {
  1723. var $color = $(elBtn).closest('.note-color');
  1724. var $recentColor = $color.find('.note-recent-color');
  1725. var oColor = JSON.parse($recentColor.attr('data-value'));
  1726. oColor[sEvent] = sValue;
  1727. $recentColor.attr('data-value', JSON.stringify(oColor));
  1728. var sKey = sEvent === 'backColor' ? 'background-color' : 'color';
  1729. $recentColor.find('i').css(sKey, sValue);
  1730. };
  1731. this.updateFullscreen = function ($toolbar, bFullscreen) {
  1732. var $btn = $toolbar.find('button[data-event="fullscreen"]');
  1733. $btn.toggleClass('active', bFullscreen);
  1734. };
  1735. this.updateCodeview = function ($toolbar, bCodeview) {
  1736. var $btn = $toolbar.find('button[data-event="codeview"]');
  1737. $btn.toggleClass('active', bCodeview);
  1738. };
  1739. /**
  1740. * activate buttons exclude codeview
  1741. * @param {jQuery} $toolbar
  1742. */
  1743. this.activate = function ($toolbar) {
  1744. $toolbar.find('button').not('button[data-event="codeview"]').removeClass('disabled');
  1745. };
  1746. /**
  1747. * deactivate buttons exclude codeview
  1748. * @param {jQuery} $toolbar
  1749. */
  1750. this.deactivate = function ($toolbar) {
  1751. $toolbar.find('button').not('button[data-event="codeview"]').addClass('disabled');
  1752. };
  1753. };
  1754. /**
  1755. * Popover (http://getbootstrap.com/javascript/#popovers)
  1756. */
  1757. var Popover = function () {
  1758. /**
  1759. * show popover
  1760. * @param {jQuery} popover
  1761. * @param {Element} elPlaceholder - placeholder for popover
  1762. */
  1763. var showPopover = function ($popover, elPlaceholder) {
  1764. var $placeholder = $(elPlaceholder);
  1765. var pos = $placeholder.position(), height = $placeholder.height();
  1766. // display popover below placeholder.
  1767. $popover.css({
  1768. display: 'block',
  1769. left: pos.left,
  1770. top: pos.top + height
  1771. });
  1772. };
  1773. /**
  1774. * update current state
  1775. * @param {jQuery} $popover - popover container
  1776. * @param {Object} oStyle - style object
  1777. */
  1778. this.update = function ($popover, oStyle) {
  1779. var $linkPopover = $popover.find('.note-link-popover');
  1780. if (oStyle.anchor) {
  1781. var $anchor = $linkPopover.find('a');
  1782. $anchor.attr('href', oStyle.anchor.href).html(oStyle.anchor.href);
  1783. showPopover($linkPopover, oStyle.anchor);
  1784. } else {
  1785. $linkPopover.hide();
  1786. }
  1787. var $imagePopover = $popover.find('.note-image-popover');
  1788. if (oStyle.image) {
  1789. showPopover($imagePopover, oStyle.image);
  1790. } else {
  1791. $imagePopover.hide();
  1792. }
  1793. };
  1794. /**
  1795. * hide all popovers
  1796. * @param {jQuery} $popover - popover contaienr
  1797. */
  1798. this.hide = function ($popover) {
  1799. $popover.children().hide();
  1800. };
  1801. };
  1802. /**
  1803. * Handle
  1804. */
  1805. var Handle = function () {
  1806. /**
  1807. * update handle
  1808. * @param {jQuery} $handle
  1809. * @param {Object} oStyle
  1810. */
  1811. this.update = function ($handle, oStyle) {
  1812. var $selection = $handle.find('.note-control-selection');
  1813. if (oStyle.image) {
  1814. var $image = $(oStyle.image);
  1815. var pos = $image.position();
  1816. var szImage = {w: $image.width(), h: $image.height()};
  1817. $selection.css({
  1818. display: 'block',
  1819. left: pos.left,
  1820. top: pos.top,
  1821. width: szImage.w,
  1822. height: szImage.h
  1823. }).data('target', oStyle.image); // save current image element.
  1824. var sSizing = szImage.w + 'x' + szImage.h;
  1825. $selection.find('.note-control-selection-info').text(sSizing);
  1826. } else {
  1827. $selection.hide();
  1828. }
  1829. };
  1830. this.hide = function ($handle) {
  1831. $handle.children().hide();
  1832. };
  1833. };
  1834. /**
  1835. * Dialog
  1836. *
  1837. * @class
  1838. */
  1839. var Dialog = function () {
  1840. /**
  1841. * toggle button status
  1842. *
  1843. * @param {jQuery} $btn
  1844. * @param {Boolean} bEnable
  1845. */
  1846. var toggleBtn = function ($btn, bEnable) {
  1847. $btn.toggleClass('disabled', !bEnable);
  1848. $btn.attr('disabled', !bEnable);
  1849. };
  1850. /**
  1851. * show image dialog
  1852. *
  1853. * @param {jQuery} $editable
  1854. * @param {jQuery} $dialog
  1855. * @return {Promise}
  1856. */
  1857. this.showImageDialog = function ($editable, $dialog) {
  1858. return $.Deferred(function (deferred) {
  1859. var $imageDialog = $dialog.find('.note-image-dialog');
  1860. var $imageInput = $dialog.find('.note-image-input'),
  1861. $imageUrl = $dialog.find('.note-image-url'),
  1862. $imageBtn = $dialog.find('.note-image-btn');
  1863. $imageDialog.one('shown.bs.modal', function () {
  1864. // Cloning imageInput to clear element.
  1865. $imageInput.replaceWith($imageInput.clone()
  1866. .on('change', function () {
  1867. $imageDialog.modal('hide');
  1868. deferred.resolve(this.files);
  1869. })
  1870. );
  1871. $imageBtn.click(function (event) {
  1872. event.preventDefault();
  1873. $imageDialog.modal('hide');
  1874. deferred.resolve($imageUrl.val());
  1875. });
  1876. $imageUrl.keyup(function () {
  1877. toggleBtn($imageBtn, $imageUrl.val());
  1878. }).val('').focus();
  1879. }).one('hidden.bs.modal', function () {
  1880. $editable.focus();
  1881. $imageInput.off('change');
  1882. $imageUrl.off('keyup');
  1883. $imageBtn.off('click');
  1884. }).modal('show');
  1885. });
  1886. };
  1887. /**
  1888. * Show video dialog and set event handlers on dialog controls.
  1889. *
  1890. * @param {jQuery} $dialog
  1891. * @param {Object} videoInfo
  1892. * @return {Promise}
  1893. */
  1894. this.showVideoDialog = function ($editable, $dialog, videoInfo) {
  1895. return $.Deferred(function (deferred) {
  1896. var $videoDialog = $dialog.find('.note-video-dialog');
  1897. var $videoUrl = $videoDialog.find('.note-video-url'),
  1898. $videoBtn = $videoDialog.find('.note-video-btn');
  1899. $videoDialog.one('shown.bs.modal', function () {
  1900. $videoUrl.val(videoInfo.text).keyup(function () {
  1901. toggleBtn($videoBtn, $videoUrl.val());
  1902. }).trigger('keyup').trigger('focus');
  1903. $videoBtn.click(function (event) {
  1904. event.preventDefault();
  1905. $videoDialog.modal('hide');
  1906. deferred.resolve($videoUrl.val());
  1907. });
  1908. }).one('hidden.bs.modal', function () {
  1909. $editable.focus();
  1910. $videoUrl.off('keyup');
  1911. $videoBtn.off('click');
  1912. }).modal('show');
  1913. });
  1914. };
  1915. /**
  1916. * Show link dialog and set event handlers on dialog controls.
  1917. *
  1918. * @param {jQuery} $dialog
  1919. * @param {Object} linkInfo
  1920. * @return {Promise}
  1921. */
  1922. this.showLinkDialog = function ($editable, $dialog, linkInfo) {
  1923. return $.Deferred(function (deferred) {
  1924. var $linkDialog = $dialog.find('.note-link-dialog');
  1925. var $linkText = $linkDialog.find('.note-link-text'),
  1926. $linkUrl = $linkDialog.find('.note-link-url'),
  1927. $linkBtn = $linkDialog.find('.note-link-btn'),
  1928. $openInNewWindow = $linkDialog.find('input[type=checkbox]');
  1929. $linkDialog.one('shown.bs.modal', function () {
  1930. $linkText.val(linkInfo.text);
  1931. $linkUrl.keyup(function () {
  1932. toggleBtn($linkBtn, $linkUrl.val());
  1933. // display same link on `Text to display` input
  1934. // when create a new link
  1935. if (!linkInfo.text) {
  1936. $linkText.val($linkUrl.val());
  1937. }
  1938. }).val(linkInfo.url).trigger('focus');
  1939. $openInNewWindow.prop('checked', linkInfo.newWindow);
  1940. $linkBtn.one('click', function (event) {
  1941. event.preventDefault();
  1942. $linkDialog.modal('hide');
  1943. deferred.resolve($linkUrl.val(), $openInNewWindow.is(':checked'));
  1944. });
  1945. }).one('hidden.bs.modal', function () {
  1946. $editable.focus();
  1947. $linkUrl.off('keyup');
  1948. }).modal('show');
  1949. }).promise();
  1950. };
  1951. /**
  1952. * show help dialog
  1953. *
  1954. * @param {jQuery} $dialog
  1955. */
  1956. this.showHelpDialog = function ($editable, $dialog) {
  1957. var $helpDialog = $dialog.find('.note-help-dialog');
  1958. $helpDialog.one('hidden.bs.modal', function () {
  1959. $editable.focus();
  1960. }).modal('show');
  1961. };
  1962. };
  1963. /**
  1964. * EventHandler
  1965. */
  1966. var EventHandler = function () {
  1967. var editor = new Editor();
  1968. var toolbar = new Toolbar(), popover = new Popover();
  1969. var handle = new Handle(), dialog = new Dialog();
  1970. /**
  1971. * returns makeLayoutInfo from editor's descendant node.
  1972. *
  1973. * @param {Element} descendant
  1974. * @returns {Object}
  1975. */
  1976. var makeLayoutInfo = function (descendant) {
  1977. var $editor = $(descendant).closest('.note-editor');
  1978. return $editor.length > 0 && dom.buildLayoutInfo($editor);
  1979. };
  1980. /**
  1981. * insert Images from file array.
  1982. *
  1983. * @param {jQuery} $editable
  1984. * @param {File[]} files
  1985. */
  1986. var insertImages = function ($editable, files) {
  1987. editor.restoreRange($editable);
  1988. var callbacks = $editable.data('callbacks');
  1989. // If onImageUpload options setted
  1990. if (callbacks.onImageUpload) {
  1991. callbacks.onImageUpload(files, editor, $editable);
  1992. // else insert Image as dataURL
  1993. } else {
  1994. $.each(files, function (idx, file) {
  1995. async.readFileAsDataURL(file).then(function (sDataURL) {
  1996. editor.insertImage($editable, sDataURL);
  1997. }).fail(function () {
  1998. if (callbacks.onImageUploadError) {
  1999. callbacks.onImageUploadError();
  2000. }
  2001. });
  2002. });
  2003. }
  2004. };
  2005. var hMousedown = function (event) {
  2006. //preventDefault Selection for FF, IE8+
  2007. if (dom.isImg(event.target)) { event.preventDefault(); }
  2008. };
  2009. var hToolbarAndPopoverUpdate = function (event) {
  2010. var oLayoutInfo = makeLayoutInfo(event.currentTarget || event.target);
  2011. var oStyle = editor.currentStyle(event.target);
  2012. if (!oStyle) { return; }
  2013. toolbar.update(oLayoutInfo.toolbar(), oStyle);
  2014. popover.update(oLayoutInfo.popover(), oStyle);
  2015. handle.update(oLayoutInfo.handle(), oStyle);
  2016. };
  2017. var hScroll = function (event) {
  2018. var oLayoutInfo = makeLayoutInfo(event.currentTarget || event.target);
  2019. //hide popover and handle when scrolled
  2020. popover.hide(oLayoutInfo.popover());
  2021. handle.hide(oLayoutInfo.handle());
  2022. };
  2023. /**
  2024. * paste clipboard image
  2025. *
  2026. * @param {Event} event
  2027. */
  2028. var hPasteClipboardImage = function (event) {
  2029. var originalEvent = event.originalEvent;
  2030. if (!originalEvent.clipboardData ||
  2031. !originalEvent.clipboardData.items ||
  2032. !originalEvent.clipboardData.items.length) {
  2033. return;
  2034. }
  2035. var oLayoutInfo = makeLayoutInfo(event.currentTarget || event.target);
  2036. var item = list.head(originalEvent.clipboardData.items);
  2037. var bClipboardImage = item.kind === 'file' && item.type.indexOf('image/') !== -1;
  2038. if (bClipboardImage) {
  2039. insertImages(oLayoutInfo.editable(), [item.getAsFile()]);
  2040. }
  2041. };
  2042. /**
  2043. * `mousedown` event handler on $handle
  2044. * - controlSizing: resize image
  2045. *
  2046. * @param {MouseEvent} event
  2047. */
  2048. var hHandleMousedown = function (event) {
  2049. if (dom.isControlSizing(event.target)) {
  2050. event.preventDefault();
  2051. event.stopPropagation();
  2052. var oLayoutInfo = makeLayoutInfo(event.target),
  2053. $handle = oLayoutInfo.handle(), $popover = oLayoutInfo.popover(),
  2054. $editable = oLayoutInfo.editable(), $editor = oLayoutInfo.editor();
  2055. var elTarget = $handle.find('.note-control-selection').data('target'),
  2056. $target = $(elTarget), posStart = $target.offset(),
  2057. scrollTop = $(document).scrollTop();
  2058. $editor.on('mousemove', function (event) {
  2059. editor.resizeTo({
  2060. x: event.clientX - posStart.left,
  2061. y: event.clientY - (posStart.top - scrollTop)
  2062. }, $target, !event.shiftKey);
  2063. handle.update($handle, {image: elTarget});
  2064. popover.update($popover, {image: elTarget});
  2065. }).one('mouseup', function () {
  2066. $editor.off('mousemove');
  2067. });
  2068. if (!$target.data('ratio')) { // original ratio.
  2069. $target.data('ratio', $target.height() / $target.width());
  2070. }
  2071. editor.recordUndo($editable);
  2072. }
  2073. };
  2074. var hToolbarAndPopoverMousedown = function (event) {
  2075. // prevent default event when insertTable (FF, Webkit)
  2076. var $btn = $(event.target).closest('[data-event]');
  2077. if ($btn.length > 0) { event.preventDefault(); }
  2078. };
  2079. var hToolbarAndPopoverClick = function (event) {
  2080. var $btn = $(event.target).closest('[data-event]');
  2081. if ($btn.length > 0) {
  2082. var sEvent = $btn.attr('data-event'),
  2083. sValue = $btn.attr('data-value');
  2084. var oLayoutInfo = makeLayoutInfo(event.target);
  2085. var $editor = oLayoutInfo.editor(),
  2086. $toolbar = oLayoutInfo.toolbar(),
  2087. $dialog = oLayoutInfo.dialog(),
  2088. $editable = oLayoutInfo.editable(),
  2089. $codable = oLayoutInfo.codable();
  2090. var server;
  2091. var cmEditor;
  2092. var options = $editor.data('options');
  2093. // before command: detect control selection element($target)
  2094. var $target;
  2095. if ($.inArray(sEvent, ['resize', 'floatMe', 'removeMedia']) !== -1) {
  2096. var $handle = oLayoutInfo.handle();
  2097. var $selection = $handle.find('.note-control-selection');
  2098. $target = $($selection.data('target'));
  2099. }
  2100. if (editor[sEvent]) { // on command
  2101. $editable.trigger('focus');
  2102. editor[sEvent]($editable, sValue, $target);
  2103. }
  2104. // after command
  2105. if ($.inArray(sEvent, ['backColor', 'foreColor']) !== -1) {
  2106. toolbar.updateRecentColor($btn[0], sEvent, sValue);
  2107. } else if (sEvent === 'showLinkDialog') { // popover to dialog
  2108. $editable.focus();
  2109. var linkInfo = editor.getLinkInfo();
  2110. editor.saveRange($editable);
  2111. dialog.showLinkDialog($editable, $dialog, linkInfo).then(function (sLinkUrl, bNewWindow) {
  2112. editor.restoreRange($editable);
  2113. editor.createLink($editable, sLinkUrl, bNewWindow);
  2114. });
  2115. } else if (sEvent === 'showImageDialog') {
  2116. $editable.focus();
  2117. dialog.showImageDialog($editable, $dialog).then(function (data) {
  2118. if (typeof data === 'string') {
  2119. editor.restoreRange($editable);
  2120. editor.insertImage($editable, data);
  2121. } else {
  2122. insertImages($editable, data);
  2123. }
  2124. });
  2125. } else if (sEvent === 'showVideoDialog') {
  2126. $editable.focus();
  2127. var videoInfo = editor.getVideoInfo();
  2128. editor.saveRange($editable);
  2129. dialog.showVideoDialog($editable, $dialog, videoInfo).then(function (sUrl) {
  2130. editor.restoreRange($editable);
  2131. editor.insertVideo($editable, sUrl);
  2132. });
  2133. } else if (sEvent === 'showHelpDialog') {
  2134. dialog.showHelpDialog($editable, $dialog);
  2135. } else if (sEvent === 'fullscreen') {
  2136. var $scrollbar = $('html, body');
  2137. var resize = function (size) {
  2138. $editor.css('width', size.w);
  2139. $editable.css('height', size.h);
  2140. $codable.css('height', size.h);
  2141. if ($codable.data('cmEditor')) {
  2142. $codable.data('cmEditor').setSize(null, size.h);
  2143. }
  2144. };
  2145. $editor.toggleClass('fullscreen');
  2146. var isFullscreen = $editor.hasClass('fullscreen');
  2147. if (isFullscreen) {
  2148. $editable.data('orgHeight', $editable.css('height'));
  2149. $(window).on('resize', function () {
  2150. resize({
  2151. w: $(window).width(),
  2152. h: $(window).height() - $toolbar.outerHeight()
  2153. });
  2154. }).trigger('resize');
  2155. $scrollbar.css('overflow', 'hidden');
  2156. } else {
  2157. $(window).off('resize');
  2158. resize({
  2159. w: options.width || '',
  2160. h: $editable.data('orgHeight')
  2161. });
  2162. $scrollbar.css('overflow', 'auto');
  2163. }
  2164. toolbar.updateFullscreen($toolbar, isFullscreen);
  2165. } else if (sEvent === 'codeview') {
  2166. $editor.toggleClass('codeview');
  2167. var bCodeview = $editor.hasClass('codeview');
  2168. if (bCodeview) {
  2169. $codable.val($editable.html());
  2170. $codable.height($editable.height());
  2171. toolbar.deactivate($toolbar);
  2172. $codable.focus();
  2173. // activate CodeMirror as codable
  2174. if (agent.bCodeMirror) {
  2175. cmEditor = CodeMirror.fromTextArea($codable[0], options.codemirror);
  2176. // CodeMirror TernServer
  2177. if (options.codemirror.tern) {
  2178. server = new CodeMirror.TernServer(options.codemirror.tern);
  2179. cmEditor.ternServer = server;
  2180. cmEditor.on('cursorActivity', function (cm) {
  2181. server.updateArgHints(cm);
  2182. });
  2183. }
  2184. // CodeMirror hasn't Padding.
  2185. cmEditor.setSize(null, $editable.outerHeight());
  2186. // autoFormatRange If formatting included
  2187. if (cmEditor.autoFormatRange) {
  2188. cmEditor.autoFormatRange({line: 0, ch: 0}, {
  2189. line: cmEditor.lineCount(),
  2190. ch: cmEditor.getTextArea().value.length
  2191. });
  2192. }
  2193. $codable.data('cmEditor', cmEditor);
  2194. }
  2195. } else {
  2196. // deactivate CodeMirror as codable
  2197. if (agent.bCodeMirror) {
  2198. cmEditor = $codable.data('cmEditor');
  2199. $codable.val(cmEditor.getValue());
  2200. cmEditor.toTextArea();
  2201. }
  2202. $editable.html($codable.val() || dom.emptyPara);
  2203. $editable.height(options.height ? $codable.height() : 'auto');
  2204. toolbar.activate($toolbar);
  2205. $editable.focus();
  2206. }
  2207. toolbar.updateCodeview(oLayoutInfo.toolbar(), bCodeview);
  2208. }
  2209. hToolbarAndPopoverUpdate(event);
  2210. }
  2211. };
  2212. var EDITABLE_PADDING = 24;
  2213. /**
  2214. * `mousedown` event handler on statusbar
  2215. *
  2216. * @param {MouseEvent} event
  2217. */
  2218. var hStatusbarMousedown = function (event) {
  2219. var $document = $(document);
  2220. var $editable = makeLayoutInfo(event.target).editable();
  2221. var nEditableTop = $editable.offset().top - $document.scrollTop();
  2222. $document.on('mousemove', function (event) {
  2223. var nHeight = event.clientY - (nEditableTop + EDITABLE_PADDING);
  2224. $editable.height(nHeight);
  2225. }).one('mouseup', function () {
  2226. $document.off('mousemove');
  2227. });
  2228. event.stopPropagation();
  2229. event.preventDefault();
  2230. };
  2231. var PX_PER_EM = 18;
  2232. var hDimensionPickerMove = function (event) {
  2233. var $picker = $(event.target.parentNode); // target is mousecatcher
  2234. var $dimensionDisplay = $picker.next();
  2235. var $catcher = $picker.find('.note-dimension-picker-mousecatcher');
  2236. var $highlighted = $picker.find('.note-dimension-picker-highlighted');
  2237. var $unhighlighted = $picker.find('.note-dimension-picker-unhighlighted');
  2238. var posOffset;
  2239. // HTML5 with jQuery - e.offsetX is undefined in Firefox
  2240. if (event.offsetX === undefined) {
  2241. var posCatcher = $(event.target).offset();
  2242. posOffset = {
  2243. x: event.pageX - posCatcher.left,
  2244. y: event.pageY - posCatcher.top
  2245. };
  2246. } else {
  2247. posOffset = {
  2248. x: event.offsetX,
  2249. y: event.offsetY
  2250. };
  2251. }
  2252. var dim = {
  2253. c: Math.ceil(posOffset.x / PX_PER_EM) || 1,
  2254. r: Math.ceil(posOffset.y / PX_PER_EM) || 1
  2255. };
  2256. $highlighted.css({ width: dim.c + 'em', height: dim.r + 'em' });
  2257. $catcher.attr('data-value', dim.c + 'x' + dim.r);
  2258. if (3 < dim.c && dim.c < 10) { // 5~10
  2259. $unhighlighted.css({ width: dim.c + 1 + 'em'});
  2260. }
  2261. if (3 < dim.r && dim.r < 10) { // 5~10
  2262. $unhighlighted.css({ height: dim.r + 1 + 'em'});
  2263. }
  2264. $dimensionDisplay.html(dim.c + ' x ' + dim.r);
  2265. };
  2266. /**
  2267. * attach Drag and Drop Events
  2268. *
  2269. * @param {Object} oLayoutInfo - layout Informations
  2270. */
  2271. var attachDragAndDropEvent = function (oLayoutInfo) {
  2272. var collection = $(), $dropzone = oLayoutInfo.dropzone,
  2273. $dropzoneMessage = oLayoutInfo.dropzone.find('.note-dropzone-message');
  2274. // show dropzone on dragenter when dragging a object to document.
  2275. $(document).on('dragenter', function (e) {
  2276. var bCodeview = oLayoutInfo.editor.hasClass('codeview');
  2277. if (!bCodeview && collection.length === 0) {
  2278. oLayoutInfo.editor.addClass('dragover');
  2279. $dropzone.width(oLayoutInfo.editor.width());
  2280. $dropzone.height(oLayoutInfo.editor.height());
  2281. $dropzoneMessage.text('Drag Image Here');
  2282. }
  2283. collection = collection.add(e.target);
  2284. }).on('dragleave', function (e) {
  2285. collection = collection.not(e.target);
  2286. if (collection.length === 0) {
  2287. oLayoutInfo.editor.removeClass('dragover');
  2288. }
  2289. }).on('drop', function () {
  2290. collection = $();
  2291. oLayoutInfo.editor.removeClass('dragover');
  2292. });
  2293. // change dropzone's message on hover.
  2294. $dropzone.on('dragenter', function () {
  2295. $dropzone.addClass('hover');
  2296. $dropzoneMessage.text('Drop Image');
  2297. }).on('dragleave', function () {
  2298. $dropzone.removeClass('hover');
  2299. $dropzoneMessage.text('Drag Image Here');
  2300. });
  2301. // attach dropImage
  2302. $dropzone.on('drop', function (event) {
  2303. var dataTransfer = event.originalEvent.dataTransfer;
  2304. if (dataTransfer && dataTransfer.files) {
  2305. var oLayoutInfo = makeLayoutInfo(event.currentTarget || event.target);
  2306. oLayoutInfo.editable().focus();
  2307. insertImages(oLayoutInfo.editable(), dataTransfer.files);
  2308. }
  2309. event.preventDefault();
  2310. }).on('dragover', false); // prevent default dragover event
  2311. };
  2312. /**
  2313. * bind KeyMap on keydown
  2314. *
  2315. * @param {Object} oLayoutInfo
  2316. * @param {Object} keyMap
  2317. */
  2318. this.bindKeyMap = function (oLayoutInfo, keyMap) {
  2319. var $editor = oLayoutInfo.editor;
  2320. var $editable = oLayoutInfo.editable;
  2321. $editable.on('keydown', function (event) {
  2322. var aKey = [];
  2323. // modifier
  2324. if (event.metaKey) { aKey.push('CMD'); }
  2325. if (event.ctrlKey) { aKey.push('CTRL'); }
  2326. if (event.shiftKey) { aKey.push('SHIFT'); }
  2327. // keycode
  2328. var keyName = key.nameFromCode[event.keyCode];
  2329. if (keyName) { aKey.push(keyName); }
  2330. var handler = keyMap[aKey.join('+')];
  2331. if (handler) {
  2332. event.preventDefault();
  2333. editor[handler]($editable, $editor.data('options'));
  2334. } else if (key.isEdit(event.keyCode)) {
  2335. editor.recordUndo($editable);
  2336. }
  2337. });
  2338. };
  2339. /**
  2340. * attach eventhandler
  2341. *
  2342. * @param {Object} oLayoutInfo - layout Informations
  2343. * @param {Object} options - user options include custom event handlers
  2344. * @param {Function} options.enter - enter key handler
  2345. */
  2346. this.attach = function (oLayoutInfo, options) {
  2347. // handlers for editable
  2348. this.bindKeyMap(oLayoutInfo, options.keyMap[agent.bMac ? 'mac' : 'pc']);
  2349. oLayoutInfo.editable.on('mousedown', hMousedown);
  2350. oLayoutInfo.editable.on('keyup mouseup', hToolbarAndPopoverUpdate);
  2351. oLayoutInfo.editable.on('scroll', hScroll);
  2352. oLayoutInfo.editable.on('paste', hPasteClipboardImage);
  2353. // handler for drag and drop
  2354. if (!options.disableDragAndDrop) { attachDragAndDropEvent(oLayoutInfo); }
  2355. // handler for handle(sizing image handle)
  2356. oLayoutInfo.handle.on('mousedown', hHandleMousedown);
  2357. // handler for toolbar, popover and statusbar
  2358. oLayoutInfo.toolbar.on('click', hToolbarAndPopoverClick);
  2359. oLayoutInfo.popover.on('click', hToolbarAndPopoverClick);
  2360. oLayoutInfo.toolbar.on('mousedown', hToolbarAndPopoverMousedown);
  2361. oLayoutInfo.popover.on('mousedown', hToolbarAndPopoverMousedown);
  2362. oLayoutInfo.statusbar.on('mousedown', hStatusbarMousedown);
  2363. // handler for toolbar's table dimension
  2364. var $toolbar = oLayoutInfo.toolbar;
  2365. var $catcher = $toolbar.find('.note-dimension-picker-mousecatcher');
  2366. $catcher.on('mousemove', hDimensionPickerMove);
  2367. // save selection when focusout
  2368. oLayoutInfo.editable.on('blur', function () {
  2369. editor.saveRange(oLayoutInfo.editable);
  2370. });
  2371. // save options on editor
  2372. oLayoutInfo.editor.data('options', options);
  2373. // ret styleWithCSS for backColor / foreColor clearing with 'inherit'.
  2374. if (options.styleWithSpan && !agent.bMSIE) {
  2375. // protect FF Error: NS_ERROR_FAILURE: Failure
  2376. setTimeout(function () {
  2377. document.execCommand('styleWithCSS', 0, true);
  2378. });
  2379. }
  2380. // History
  2381. oLayoutInfo.editable.data('NoteHistory', new History());
  2382. // basic event callbacks (lowercase)
  2383. // enter, focus, blur, keyup, keydown
  2384. if (options.onenter) {
  2385. oLayoutInfo.editable.keypress(function (event) {
  2386. if (event.keyCode === key.ENTER) { options.onenter(event); }
  2387. });
  2388. }
  2389. if (options.onfocus) { oLayoutInfo.editable.focus(options.onfocus); }
  2390. if (options.onblur) { oLayoutInfo.editable.blur(options.onblur); }
  2391. if (options.onkeyup) { oLayoutInfo.editable.keyup(options.onkeyup); }
  2392. if (options.onkeydown) { oLayoutInfo.editable.keydown(options.onkeydown); }
  2393. if (options.onpaste) { oLayoutInfo.editable.on('paste', options.onpaste); }
  2394. // callbacks for advanced features (camel)
  2395. if (options.onToolbarClick) { oLayoutInfo.toolbar.click(options.onToolbarClick); }
  2396. if (options.onChange) {
  2397. var handler = function () {
  2398. options.onChange(oLayoutInfo.editable, oLayoutInfo.editable.html());
  2399. };
  2400. if (agent.bMSIE) {
  2401. oLayoutInfo.editable.on('DOMCharacterDataModified, DOMSubtreeModified, DOMNodeInserted', handler);
  2402. } else {
  2403. oLayoutInfo.editable.on('input', handler);
  2404. }
  2405. }
  2406. // All editor status will be saved on editable with jquery's data
  2407. // for support multiple editor with singleton object.
  2408. oLayoutInfo.editable.data('callbacks', {
  2409. onAutoSave: options.onAutoSave,
  2410. onImageUpload: options.onImageUpload,
  2411. onImageUploadError: options.onImageUploadError,
  2412. onFileUpload: options.onFileUpload,
  2413. onFileUploadError: options.onFileUpload
  2414. });
  2415. };
  2416. this.dettach = function (oLayoutInfo) {
  2417. oLayoutInfo.editable.off();
  2418. oLayoutInfo.dropzone.off();
  2419. oLayoutInfo.toolbar.off();
  2420. oLayoutInfo.statusbar.off();
  2421. oLayoutInfo.popover.off();
  2422. oLayoutInfo.handle.off();
  2423. oLayoutInfo.dialog.off();
  2424. };
  2425. };
  2426. /**
  2427. * renderer
  2428. *
  2429. * rendering toolbar and editable
  2430. */
  2431. var Renderer = function () {
  2432. /**
  2433. * bootstrap button template
  2434. *
  2435. * @param {String} sLabel
  2436. * @param {Object} [options]
  2437. * @param {String} [options.event]
  2438. * @param {String} [options.value]
  2439. * @param {String} [options.title]
  2440. * @param {String} [options.dropdown]
  2441. */
  2442. var tplButton = function (sLabel, options) {
  2443. var event = options.event;
  2444. var value = options.value;
  2445. var title = options.title;
  2446. var className = options.className;
  2447. var dropdown = options.dropdown;
  2448. return '<button type="button"' +
  2449. ' class="btn btn-default btn-sm btn-small' +
  2450. (className ? ' ' + className : '') +
  2451. (dropdown ? ' dropdown-toggle' : '') +
  2452. '"' +
  2453. (dropdown ? ' data-toggle="dropdown"' : '') +
  2454. (title ? ' title="' + title + '"' : '') +
  2455. (event ? ' data-event="' + event + '"' : '') +
  2456. (value ? ' data-value=\'' + value + '\'' : '') +
  2457. ' tabindex="-1">' +
  2458. sLabel +
  2459. (dropdown ? ' <span class="caret"></span>' : '') +
  2460. '</button>' +
  2461. (dropdown || '');
  2462. };
  2463. /**
  2464. * bootstrap icon button template
  2465. *
  2466. * @param {String} sIconClass
  2467. * @param {Object} [options]
  2468. * @param {String} [options.event]
  2469. * @param {String} [options.value]
  2470. * @param {String} [options.title]
  2471. * @param {String} [options.dropdown]
  2472. */
  2473. var tplIconButton = function (sIconClass, options) {
  2474. var sLabel = '<i class="' + sIconClass + '"></i>';
  2475. return tplButton(sLabel, options);
  2476. };
  2477. /**
  2478. * bootstrap popover template
  2479. *
  2480. * @param {String} className
  2481. * @param {String} content
  2482. */
  2483. var tplPopover = function (className, content) {
  2484. return '<div class="' + className + ' popover bottom in" style="display: none;">' +
  2485. '<div class="arrow"></div>' +
  2486. '<div class="popover-content">' +
  2487. content +
  2488. '</div>' +
  2489. '</div>';
  2490. };
  2491. /**
  2492. * bootstrap dialog template
  2493. *
  2494. * @param {String} className
  2495. * @param {String} [title]
  2496. * @param {String} body
  2497. * @param {String} [footer]
  2498. */
  2499. var tplDialog = function (className, title, body, footer) {
  2500. return '<div class="' + className + ' modal" aria-hidden="false">' +
  2501. '<div class="modal-dialog">' +
  2502. '<div class="modal-content">' +
  2503. (title ?
  2504. '<div class="modal-header">' +
  2505. '<button type="button" class="close" aria-hidden="true" tabindex="-1">&times;</button>' +
  2506. '<h4>' + title + '</h4>' +
  2507. '</div>' : ''
  2508. ) +
  2509. '<div class="modal-body">' +
  2510. '<div class="row-fluid">' + body + '</div>' +
  2511. '</div>' +
  2512. (footer ?
  2513. '<div class="modal-footer">' + footer + '</div>' : ''
  2514. ) +
  2515. '</div>' +
  2516. '</div>' +
  2517. '</div>';
  2518. };
  2519. var tplToolbarInfo = {
  2520. picture: function (lang) {
  2521. return tplIconButton('fa fa-picture-o icon-picture', {
  2522. event: 'showImageDialog',
  2523. title: lang.image.image
  2524. });
  2525. },
  2526. link: function (lang) {
  2527. return tplIconButton('fa fa-link icon-link', {
  2528. event: 'showLinkDialog',
  2529. title: lang.link.link
  2530. });
  2531. },
  2532. video: function (lang) {
  2533. return tplIconButton('fa fa-youtube-play icon-play', {
  2534. event: 'showVideoDialog',
  2535. title: lang.link.link
  2536. });
  2537. },
  2538. table: function (lang) {
  2539. var dropdown = '<ul class="dropdown-menu">' +
  2540. '<div class="note-dimension-picker">' +
  2541. '<div class="note-dimension-picker-mousecatcher" data-event="insertTable" data-value="1x1"></div>' +
  2542. '<div class="note-dimension-picker-highlighted"></div>' +
  2543. '<div class="note-dimension-picker-unhighlighted"></div>' +
  2544. '</div>' +
  2545. '<div class="note-dimension-display"> 1 x 1 </div>' +
  2546. '</ul>';
  2547. return tplIconButton('fa fa-table icon-table', {
  2548. title: lang.table.table,
  2549. dropdown: dropdown
  2550. });
  2551. },
  2552. style: function (lang, options) {
  2553. var items = options.styleTags.reduce(function (memo, v) {
  2554. var label = lang.style[v === 'p' ? 'normal' : v];
  2555. return memo + '<li><a data-event="formatBlock" data-value="' + v + '">' +
  2556. (
  2557. (v === 'p' || v === 'pre') ? label :
  2558. '<' + v + '>' + label + '</' + v + '>'
  2559. ) +
  2560. '</a></li>';
  2561. }, '');
  2562. return tplIconButton('fa fa-magic icon-magic', {
  2563. title: lang.style.style,
  2564. dropdown: '<ul class="dropdown-menu">' + items + '</ul>'
  2565. });
  2566. },
  2567. fontname: function (lang, options) {
  2568. var items = options.fontNames.reduce(function (memo, v) {
  2569. return memo + '<li><a data-event="fontName" data-value="' + v + '">' +
  2570. '<i class="fa fa-check icon-ok"></i> ' + v +
  2571. '</a></li>';
  2572. }, '');
  2573. var sLabel = '<span class="note-current-fontname">' +
  2574. options.defaultFontName +
  2575. '</span>';
  2576. return tplButton(sLabel, {
  2577. title: lang.font.name,
  2578. dropdown: '<ul class="dropdown-menu">' + items + '</ul>'
  2579. });
  2580. },
  2581. fontsize: function (lang, options) {
  2582. var items = options.fontSizes.reduce(function (memo, v) {
  2583. return memo + '<li><a data-event="fontSize" data-value="' + v + '">' +
  2584. '<i class="fa fa-check icon-ok"></i> ' + v +
  2585. '</a></li>';
  2586. }, '');
  2587. var sLabel = '<span class="note-current-fontsize">11</span>';
  2588. return tplButton(sLabel, {
  2589. title: lang.font.size,
  2590. dropdown: '<ul class="dropdown-menu">' + items + '</ul>'
  2591. });
  2592. },
  2593. color: function (lang) {
  2594. var colorButtonLabel = '<i class="fa fa-font icon-font" style="color:black;background-color:yellow;"></i>';
  2595. var colorButton = tplButton(colorButtonLabel, {
  2596. className: 'note-recent-color',
  2597. title: lang.color.recent,
  2598. event: 'color',
  2599. value: '{"backColor":"yellow"}'
  2600. });
  2601. var dropdown = '<ul class="dropdown-menu">' +
  2602. '<li>' +
  2603. '<div class="btn-group">' +
  2604. '<div class="note-palette-title">' + lang.color.background + '</div>' +
  2605. '<div class="note-color-reset" data-event="backColor"' +
  2606. ' data-value="inherit" title="' + lang.color.transparent + '">' +
  2607. lang.color.setTransparent +
  2608. '</div>' +
  2609. '<div class="note-color-palette" data-target-event="backColor"></div>' +
  2610. '</div>' +
  2611. '<div class="btn-group">' +
  2612. '<div class="note-palette-title">' + lang.color.foreground + '</div>' +
  2613. '<div class="note-color-reset" data-event="foreColor" data-value="inherit" title="' + lang.color.reset + '">' +
  2614. lang.color.resetToDefault +
  2615. '</div>' +
  2616. '<div class="note-color-palette" data-target-event="foreColor"></div>' +
  2617. '</div>' +
  2618. '</li>' +
  2619. '</ul>';
  2620. var moreButton = tplButton('', {
  2621. title: lang.color.more,
  2622. dropdown: dropdown
  2623. });
  2624. return colorButton + moreButton;
  2625. },
  2626. bold: function (lang) {
  2627. return tplIconButton('fa fa-bold icon-bold', {
  2628. event: 'bold',
  2629. title: lang.font.bold
  2630. });
  2631. },
  2632. italic: function (lang) {
  2633. return tplIconButton('fa fa-italic icon-italic', {
  2634. event: 'italic',
  2635. title: lang.font.italic
  2636. });
  2637. },
  2638. underline: function (lang) {
  2639. return tplIconButton('fa fa-underline icon-underline', {
  2640. event: 'underline',
  2641. title: lang.font.underline
  2642. });
  2643. },
  2644. strike: function (lang) {
  2645. return tplIconButton('fa fa-strikethrough icon-strikethrough', {
  2646. event: 'strikethrough',
  2647. title: lang.font.strike
  2648. });
  2649. },
  2650. clear: function (lang) {
  2651. return tplIconButton('fa fa-eraser icon-eraser', {
  2652. event: 'removeFormat',
  2653. title: lang.font.clear
  2654. });
  2655. },
  2656. ul: function (lang) {
  2657. return tplIconButton('fa fa-list-ul icon-list-ul', {
  2658. event: 'insertUnorderedList',
  2659. title: lang.lists.unordered
  2660. });
  2661. },
  2662. ol: function (lang) {
  2663. return tplIconButton('fa fa-list-ol icon-list-ol', {
  2664. event: 'insertOrderedList',
  2665. title: lang.lists.ordered
  2666. });
  2667. },
  2668. paragraph: function (lang) {
  2669. var leftButton = tplIconButton('fa fa-align-left icon-align-left', {
  2670. title: lang.paragraph.left,
  2671. event: 'justifyLeft'
  2672. });
  2673. var centerButton = tplIconButton('fa fa-align-center icon-align-center', {
  2674. title: lang.paragraph.center,
  2675. event: 'justifyCenter'
  2676. });
  2677. var rightButton = tplIconButton('fa fa-align-right icon-align-right', {
  2678. title: lang.paragraph.right,
  2679. event: 'justifyRight'
  2680. });
  2681. var justifyButton = tplIconButton('fa fa-align-justify icon-align-justify', {
  2682. title: lang.paragraph.justify,
  2683. event: 'justifyFull'
  2684. });
  2685. var outdentButton = tplIconButton('fa fa-outdent icon-indent-left', {
  2686. title: lang.paragraph.outdent,
  2687. event: 'outdent'
  2688. });
  2689. var indentButton = tplIconButton('fa fa-indent icon-indent-right', {
  2690. title: lang.paragraph.indent,
  2691. event: 'indent'
  2692. });
  2693. var dropdown = '<div class="dropdown-menu">' +
  2694. '<div class="note-align btn-group">' +
  2695. leftButton + centerButton + rightButton + justifyButton +
  2696. '</div>' +
  2697. '<div class="note-list btn-group">' +
  2698. indentButton + outdentButton +
  2699. '</div>' +
  2700. '</div>';
  2701. return tplIconButton('fa fa-align-left icon-align-left', {
  2702. title: lang.paragraph.paragraph,
  2703. dropdown: dropdown
  2704. });
  2705. },
  2706. height: function (lang, options) {
  2707. var items = options.lineHeights.reduce(function (memo, v) {
  2708. return memo + '<li><a data-event="lineHeight" data-value="' + v + '">' +
  2709. '<i class="fa fa-check icon-ok"></i> ' + v +
  2710. '</a></li>';
  2711. }, '');
  2712. return tplIconButton('fa fa-text-height icon-text-height', {
  2713. title: lang.font.height,
  2714. dropdown: '<ul class="dropdown-menu">' + items + '</ul>'
  2715. });
  2716. },
  2717. help: function (lang) {
  2718. return tplIconButton('fa fa-question icon-question', {
  2719. event: 'showHelpDialog',
  2720. title: lang.options.help
  2721. });
  2722. },
  2723. fullscreen: function (lang) {
  2724. return tplIconButton('fa fa-arrows-alt icon-fullscreen', {
  2725. event: 'fullscreen',
  2726. title: lang.options.fullscreen
  2727. });
  2728. },
  2729. codeview: function (lang) {
  2730. return tplIconButton('fa fa-code icon-code', {
  2731. event: 'codeview',
  2732. title: lang.options.codeview
  2733. });
  2734. },
  2735. undo: function (lang) {
  2736. return tplIconButton('fa fa-undo icon-undo', {
  2737. event: 'undo',
  2738. title: lang.history.undo
  2739. });
  2740. },
  2741. redo: function (lang) {
  2742. return tplIconButton('fa fa-repeat icon-repeat', {
  2743. event: 'redo',
  2744. title: lang.history.redo
  2745. });
  2746. }
  2747. };
  2748. var tplPopovers = function (lang) {
  2749. var tplLinkPopover = function () {
  2750. var linkButton = tplIconButton('fa fa-edit icon-edit', {
  2751. title: lang.link.edit,
  2752. event: 'showLinkDialog'
  2753. });
  2754. var unlinkButton = tplIconButton('fa fa-unlink icon-unlink', {
  2755. title: lang.link.unlink,
  2756. event: 'unlink'
  2757. });
  2758. var content = '<a href="http://www.google.com" target="_blank">www.google.com</a>&nbsp;&nbsp;' +
  2759. '<div class="note-insert btn-group">' +
  2760. linkButton + unlinkButton +
  2761. '</div>';
  2762. return tplPopover('note-link-popover', content);
  2763. };
  2764. var tplImagePopover = function () {
  2765. var fullButton = tplButton('<span class="note-fontsize-10">100%</span>', {
  2766. title: lang.image.resizeFull,
  2767. event: 'resize',
  2768. value: '1'
  2769. });
  2770. var halfButton = tplButton('<span class="note-fontsize-10">50%</span>', {
  2771. title: lang.image.resizeHalf,
  2772. event: 'resize',
  2773. value: '0.5'
  2774. });
  2775. var quarterButton = tplButton('<span class="note-fontsize-10">25%</span>', {
  2776. title: lang.image.resizeQuarter,
  2777. event: 'resize',
  2778. value: '0.25'
  2779. });
  2780. var leftButton = tplIconButton('fa fa-align-left icon-align-left', {
  2781. title: lang.image.floatLeft,
  2782. event: 'floatMe',
  2783. value: 'left'
  2784. });
  2785. var rightButton = tplIconButton('fa fa-align-left icon-align-right', {
  2786. title: lang.image.floatRight,
  2787. event: 'floatMe',
  2788. value: 'right'
  2789. });
  2790. var justifyButton = tplIconButton('fa fa-align-justify icon-align-justify', {
  2791. title: lang.image.floatNone,
  2792. event: 'floatMe',
  2793. value: 'none'
  2794. });
  2795. var removeButton = tplIconButton('fa fa-trash-o icon-trash', {
  2796. title: lang.image.remove,
  2797. event: 'removeMedia',
  2798. value: 'none'
  2799. });
  2800. var content = '<div class="btn-group">' + fullButton + halfButton + quarterButton + '</div>' +
  2801. '<div class="btn-group">' + leftButton + rightButton + justifyButton + '</div>' +
  2802. '<div class="btn-group">' + removeButton + '</div>';
  2803. return tplPopover('note-image-popover', content);
  2804. };
  2805. return '<div class="note-popover">' + tplLinkPopover() + tplImagePopover() + '</div>';
  2806. };
  2807. var tplHandles = function () {
  2808. return '<div class="note-handle">' +
  2809. '<div class="note-control-selection">' +
  2810. '<div class="note-control-selection-bg"></div>' +
  2811. '<div class="note-control-holder note-control-nw"></div>' +
  2812. '<div class="note-control-holder note-control-ne"></div>' +
  2813. '<div class="note-control-holder note-control-sw"></div>' +
  2814. '<div class="note-control-sizing note-control-se"></div>' +
  2815. '<div class="note-control-selection-info"></div>' +
  2816. '</div>' +
  2817. '</div>';
  2818. };
  2819. /**
  2820. * shortcut table template
  2821. * @param {String} title
  2822. * @param {String} body
  2823. */
  2824. var tplShortcut = function (title, body) {
  2825. return '<table class="note-shortcut">' +
  2826. '<thead>' +
  2827. '<tr><th></th><th>' + title + '</th></tr>' +
  2828. '</thead>' +
  2829. '<tbody>' + body + '</tbody>' +
  2830. '</table>';
  2831. };
  2832. var tplShortcutText = function (lang) {
  2833. var body = '<tr><td>⌘ + B</td><td>' + lang.font.bold + '</td></tr>' +
  2834. '<tr><td>⌘ + I</td><td>' + lang.font.italic + '</td></tr>' +
  2835. '<tr><td>⌘ + U</td><td>' + lang.font.underline + '</td></tr>' +
  2836. '<tr><td>⌘ + ⇧ + S</td><td>' + lang.font.strike + '</td></tr>' +
  2837. '<tr><td>⌘ + \\</td><td>' + lang.font.clear + '</td></tr>';
  2838. return tplShortcut(lang.shortcut.textFormatting, body);
  2839. };
  2840. var tplShortcutAction = function (lang) {
  2841. var body = '<tr><td>⌘ + Z</td><td>' + lang.history.undo + '</td></tr>' +
  2842. '<tr><td>⌘ + ⇧ + Z</td><td>' + lang.history.redo + '</td></tr>' +
  2843. '<tr><td>⌘ + ]</td><td>' + lang.paragraph.indent + '</td></tr>' +
  2844. '<tr><td>⌘ + [</td><td>' + lang.paragraph.outdent + '</td></tr>' +
  2845. '<tr><td>⌘ + ENTER</td><td>' + lang.hr.insert + '</td></tr>';
  2846. return tplShortcut(lang.shortcut.action, body);
  2847. };
  2848. var tplShortcutPara = function (lang) {
  2849. var body = '<tr><td>⌘ + ⇧ + L</td><td>' + lang.paragraph.left + '</td></tr>' +
  2850. '<tr><td>⌘ + ⇧ + E</td><td>' + lang.paragraph.center + '</td></tr>' +
  2851. '<tr><td>⌘ + ⇧ + R</td><td>' + lang.paragraph.right + '</td></tr>' +
  2852. '<tr><td>⌘ + ⇧ + J</td><td>' + lang.paragraph.justify + '</td></tr>' +
  2853. '<tr><td>⌘ + ⇧ + NUM7</td><td>' + lang.lists.ordered + '</td></tr>' +
  2854. '<tr><td>⌘ + ⇧ + NUM8</td><td>' + lang.lists.unordered + '</td></tr>';
  2855. return tplShortcut(lang.shortcut.paragraphFormatting, body);
  2856. };
  2857. var tplShortcutStyle = function (lang) {
  2858. var body = '<tr><td>⌘ + NUM0</td><td>' + lang.style.normal + '</td></tr>' +
  2859. '<tr><td>⌘ + NUM1</td><td>' + lang.style.h1 + '</td></tr>' +
  2860. '<tr><td>⌘ + NUM2</td><td>' + lang.style.h2 + '</td></tr>' +
  2861. '<tr><td>⌘ + NUM3</td><td>' + lang.style.h3 + '</td></tr>' +
  2862. '<tr><td>⌘ + NUM4</td><td>' + lang.style.h4 + '</td></tr>' +
  2863. '<tr><td>⌘ + NUM5</td><td>' + lang.style.h5 + '</td></tr>' +
  2864. '<tr><td>⌘ + NUM6</td><td>' + lang.style.h6 + '</td></tr>';
  2865. return tplShortcut(lang.shortcut.documentStyle, body);
  2866. };
  2867. var tplExtraShortcuts = function (lang, options) {
  2868. var extraKeys = options.extraKeys;
  2869. var body = '';
  2870. for (var key in extraKeys) {
  2871. if (extraKeys.hasOwnProperty(key)) {
  2872. body += '<tr><td>' + key + '</td><td>' + extraKeys[key] + '</td></tr>';
  2873. }
  2874. }
  2875. return tplShortcut(lang.shortcut.extraKeys, body);
  2876. };
  2877. var tplShortcutTable = function (lang, options) {
  2878. var template = '<table class="note-shortcut-layout">' +
  2879. '<tbody>' +
  2880. '<tr><td>' + tplShortcutAction(lang, options) + '</td><td>' + tplShortcutText(lang, options) + '</td></tr>' +
  2881. '<tr><td>' + tplShortcutStyle(lang, options) + '</td><td>' + tplShortcutPara(lang, options) + '</td></tr>';
  2882. if (options.extraKeys) {
  2883. template += '<tr><td colspan="2">' + tplExtraShortcuts(lang, options) + '</td></tr>';
  2884. }
  2885. template += '</tbody</table>';
  2886. return template;
  2887. };
  2888. var replaceMacKeys = function (sHtml) {
  2889. return sHtml.replace(/⌘/g, 'Ctrl').replace(/⇧/g, 'Shift');
  2890. };
  2891. var tplDialogs = function (lang, options) {
  2892. var tplImageDialog = function () {
  2893. var body = '<h5>' + lang.image.selectFromFiles + '</h5>' +
  2894. '<input class="note-image-input" type="file" name="files" accept="image/*" />' +
  2895. '<h5>' + lang.image.url + '</h5>' +
  2896. '<input class="note-image-url form-control span12" type="text" />';
  2897. var footer = '<button href="#" class="btn btn-primary note-image-btn disabled" disabled>' + lang.image.insert + '</button>';
  2898. return tplDialog('note-image-dialog', lang.image.insert, body, footer);
  2899. };
  2900. var tplLinkDialog = function () {
  2901. var body = '<div class="form-group">' +
  2902. '<label>' + lang.link.textToDisplay + '</label>' +
  2903. '<input class="note-link-text form-control span12" type="text" disabled />' +
  2904. '</div>' +
  2905. '<div class="form-group">' +
  2906. '<label>' + lang.link.url + '</label>' +
  2907. '<input class="note-link-url form-control span12" type="text" />' +
  2908. '</div>' +
  2909. (!options.disableLinkTarget ?
  2910. '<div class="checkbox">' +
  2911. '<label>' + '<input type="checkbox" checked> ' +
  2912. lang.link.openInNewWindow +
  2913. '</label>' +
  2914. '</div>' : ''
  2915. );
  2916. var footer = '<button href="#" class="btn btn-primary note-link-btn disabled" disabled>' + lang.link.insert + '</button>';
  2917. return tplDialog('note-link-dialog', lang.link.insert, body, footer);
  2918. };
  2919. var tplVideoDialog = function () {
  2920. var body = '<div class="form-group">' +
  2921. '<label>' + lang.video.url + '</label>&nbsp;<small class="text-muted">' + lang.video.providers + '</small>' +
  2922. '<input class="note-video-url form-control span12" type="text" />' +
  2923. '</div>';
  2924. var footer = '<button href="#" class="btn btn-primary note-video-btn disabled" disabled>' + lang.video.insert + '</button>';
  2925. return tplDialog('note-video-dialog', lang.video.insert, body, footer);
  2926. };
  2927. var tplHelpDialog = function () {
  2928. var body = '<a class="modal-close pull-right" aria-hidden="true" tabindex="-1">' + lang.shortcut.close + '</a>' +
  2929. '<div class="title">' + lang.shortcut.shortcuts + '</div>' +
  2930. (agent.bMac ? tplShortcutTable(lang, options) : replaceMacKeys(tplShortcutTable(lang, options))) +
  2931. '<p class="text-center">' +
  2932. '<a href="//hackerwins.github.io/summernote/" target="_blank">Summernote 0.5.2</a> · ' +
  2933. '<a href="//github.com/HackerWins/summernote" target="_blank">Project</a> · ' +
  2934. '<a href="//github.com/HackerWins/summernote/issues" target="_blank">Issues</a>' +
  2935. '</p>';
  2936. return tplDialog('note-help-dialog', '', body, '');
  2937. };
  2938. return '<div class="note-dialog">' +
  2939. tplImageDialog() +
  2940. tplLinkDialog() +
  2941. tplVideoDialog() +
  2942. tplHelpDialog() +
  2943. '</div>';
  2944. };
  2945. var tplStatusbar = function () {
  2946. return '<div class="note-resizebar">' +
  2947. '<div class="note-icon-bar"></div>' +
  2948. '<div class="note-icon-bar"></div>' +
  2949. '<div class="note-icon-bar"></div>' +
  2950. '</div>';
  2951. };
  2952. var invertObject = function (obj) {
  2953. var inverted = {};
  2954. for (var key in obj) {
  2955. if (obj.hasOwnProperty(key)) {
  2956. inverted[obj[key]] = key;
  2957. }
  2958. }
  2959. return inverted;
  2960. };
  2961. var representShortcut = function (str) {
  2962. if (agent.bMac) {
  2963. str = str.replace('CMD', '⌘').replace('SHIFT', '⇧');
  2964. }
  2965. return str.replace('BACKSLASH', '\\')
  2966. .replace('SLASH', '/')
  2967. .replace('LEFTBRACKET', '[')
  2968. .replace('RIGHTBRACKET', ']');
  2969. };
  2970. /**
  2971. * createTooltip
  2972. *
  2973. * @param {jQuery} $container
  2974. * @param {Object} keyMap
  2975. * @param {String} [sPlacement]
  2976. */
  2977. var createTooltip = function ($container, keyMap, sPlacement) {
  2978. var invertedKeyMap = invertObject(keyMap);
  2979. var $buttons = $container.find('button');
  2980. $buttons.each(function (i, elBtn) {
  2981. var $btn = $(elBtn);
  2982. var sShortcut = invertedKeyMap[$btn.data('event')];
  2983. if (sShortcut) {
  2984. $btn.attr('title', function (i, v) {
  2985. return v + ' (' + representShortcut(sShortcut) + ')';
  2986. });
  2987. }
  2988. // bootstrap tooltip on btn-group bug
  2989. // https://github.com/twitter/bootstrap/issues/5687
  2990. }).tooltip({
  2991. container: 'body',
  2992. trigger: 'hover',
  2993. placement: sPlacement || 'top'
  2994. }).on('click', function () {
  2995. $(this).tooltip('hide');
  2996. });
  2997. };
  2998. // createPalette
  2999. var createPalette = function ($container, options) {
  3000. var aaColor = options.colors;
  3001. $container.find('.note-color-palette').each(function () {
  3002. var $palette = $(this), sEvent = $palette.attr('data-target-event');
  3003. var aPaletteContents = [];
  3004. for (var row = 0, szRow = aaColor.length; row < szRow; row++) {
  3005. var aColor = aaColor[row];
  3006. var aButton = [];
  3007. for (var col = 0, szCol = aColor.length; col < szCol; col++) {
  3008. var sColor = aColor[col];
  3009. aButton.push(['<button type="button" class="note-color-btn" style="background-color:', sColor,
  3010. ';" data-event="', sEvent,
  3011. '" data-value="', sColor,
  3012. '" title="', sColor,
  3013. '" data-toggle="button" tabindex="-1"></button>'].join(''));
  3014. }
  3015. aPaletteContents.push('<div>' + aButton.join('') + '</div>');
  3016. }
  3017. $palette.html(aPaletteContents.join(''));
  3018. });
  3019. };
  3020. /**
  3021. * create summernote layout
  3022. *
  3023. * @param {jQuery} $holder
  3024. * @param {Object} options
  3025. */
  3026. this.createLayout = function ($holder, options) {
  3027. //already created
  3028. var next = $holder.next();
  3029. if (next && next.hasClass('note-editor')) { return; }
  3030. //01. create Editor
  3031. var $editor = $('<div class="note-editor"></div>');
  3032. if (options.width) {
  3033. $editor.width(options.width);
  3034. }
  3035. //02. statusbar (resizebar)
  3036. if (options.height > 0) {
  3037. $('<div class="note-statusbar">' + tplStatusbar() + '</div>').prependTo($editor);
  3038. }
  3039. //03. create Editable
  3040. var isContentEditable = !$holder.is(':disabled');
  3041. var $editable = $('<div class="note-editable" contentEditable="' + isContentEditable + '"></div>')
  3042. .prependTo($editor);
  3043. if (options.height) {
  3044. $editable.height(options.height);
  3045. }
  3046. if (options.direction) {
  3047. $editable.attr('dir', options.direction);
  3048. }
  3049. $editable.html(dom.html($holder) || dom.emptyPara);
  3050. //031. create codable
  3051. $('<textarea class="note-codable"></textarea>').prependTo($editor);
  3052. var langInfo = $.summernote.lang[options.lang];
  3053. //04. create Toolbar
  3054. var sToolbar = '';
  3055. for (var idx = 0, sz = options.toolbar.length; idx < sz; idx ++) {
  3056. var group = options.toolbar[idx];
  3057. sToolbar += '<div class="note-' + group[0] + ' btn-group">';
  3058. for (var i = 0, szGroup = group[1].length; i < szGroup; i++) {
  3059. sToolbar += tplToolbarInfo[group[1][i]](langInfo, options);
  3060. }
  3061. sToolbar += '</div>';
  3062. }
  3063. sToolbar = '<div class="note-toolbar btn-toolbar">' + sToolbar + '</div>';
  3064. var $toolbar = $(sToolbar).prependTo($editor);
  3065. var keyMap = options.keyMap[agent.bMac ? 'mac' : 'pc'];
  3066. createPalette($toolbar, options);
  3067. createTooltip($toolbar, keyMap, 'bottom');
  3068. //05. create Popover
  3069. var $popover = $(tplPopovers(langInfo)).prependTo($editor);
  3070. createTooltip($popover, keyMap);
  3071. //06. handle(control selection, ...)
  3072. $(tplHandles()).prependTo($editor);
  3073. //07. create Dialog
  3074. var $dialog = $(tplDialogs(langInfo, options)).prependTo($editor);
  3075. $dialog.find('button.close, a.modal-close').click(function () {
  3076. $(this).closest('.modal').modal('hide');
  3077. });
  3078. //08. create Dropzone
  3079. $('<div class="note-dropzone"><div class="note-dropzone-message"></div></div>').prependTo($editor);
  3080. //09. Editor/Holder switch
  3081. $editor.insertAfter($holder);
  3082. $holder.hide();
  3083. };
  3084. /**
  3085. * returns layoutInfo from holder
  3086. *
  3087. * @param {jQuery} $holder - placeholder
  3088. * @returns {Object}
  3089. */
  3090. this.layoutInfoFromHolder = function ($holder) {
  3091. var $editor = $holder.next();
  3092. if (!$editor.hasClass('note-editor')) { return; }
  3093. var layoutInfo = dom.buildLayoutInfo($editor);
  3094. // cache all properties.
  3095. for (var key in layoutInfo) {
  3096. if (layoutInfo.hasOwnProperty(key)) {
  3097. layoutInfo[key] = layoutInfo[key].call();
  3098. }
  3099. }
  3100. return layoutInfo;
  3101. };
  3102. /**
  3103. * removeLayout
  3104. *
  3105. * @param {jQuery} $holder - placeholder
  3106. */
  3107. this.removeLayout = function ($holder) {
  3108. var info = this.layoutInfoFromHolder($holder);
  3109. if (!info) { return; }
  3110. $holder.html(info.editable.html());
  3111. info.editor.remove();
  3112. $holder.show();
  3113. };
  3114. };
  3115. // jQuery namespace for summernote
  3116. $.summernote = $.summernote || {};
  3117. // extends default `settings`
  3118. $.extend($.summernote, settings);
  3119. var renderer = new Renderer();
  3120. var eventHandler = new EventHandler();
  3121. /**
  3122. * extend jquery fn
  3123. */
  3124. $.fn.extend({
  3125. /**
  3126. * initialize summernote
  3127. * - create editor layout and attach Mouse and keyboard events.
  3128. *
  3129. * @param {Object} options
  3130. * @returns {this}
  3131. */
  3132. summernote: function (options) {
  3133. // extend default options
  3134. options = $.extend({}, $.summernote.options, options);
  3135. this.each(function (idx, elHolder) {
  3136. var $holder = $(elHolder);
  3137. // createLayout with options
  3138. renderer.createLayout($holder, options);
  3139. var info = renderer.layoutInfoFromHolder($holder);
  3140. eventHandler.attach(info, options);
  3141. // Textarea: auto filling the code before form submit.
  3142. if (dom.isTextarea($holder[0])) {
  3143. $holder.closest('form').submit(function () {
  3144. $holder.html($holder.code());
  3145. });
  3146. }
  3147. });
  3148. // focus on first editable element
  3149. if (this.first() && options.focus) {
  3150. var info = renderer.layoutInfoFromHolder(this.first());
  3151. info.editable.focus();
  3152. }
  3153. // callback on init
  3154. if (this.length > 0 && options.oninit) {
  3155. options.oninit();
  3156. }
  3157. return this;
  3158. },
  3159. //
  3160. /**
  3161. * get the HTML contents of note or set the HTML contents of note.
  3162. *
  3163. * @param {String} [sHTML] - HTML contents(optional, set)
  3164. * @returns {this|String} - context(set) or HTML contents of note(get).
  3165. */
  3166. code: function (sHTML) {
  3167. // get the HTML contents of note
  3168. if (sHTML === undefined) {
  3169. var $holder = this.first();
  3170. if ($holder.length === 0) { return; }
  3171. var info = renderer.layoutInfoFromHolder($holder);
  3172. if (!!(info && info.editable)) {
  3173. var bCodeview = info.editor.hasClass('codeview');
  3174. if (bCodeview && agent.bCodeMirror) {
  3175. info.codable.data('cmEditor').save();
  3176. }
  3177. return bCodeview ? info.codable.val() : info.editable.html();
  3178. }
  3179. return $holder.html();
  3180. }
  3181. // set the HTML contents of note
  3182. this.each(function (i, elHolder) {
  3183. var info = renderer.layoutInfoFromHolder($(elHolder));
  3184. if (info && info.editable) { info.editable.html(sHTML); }
  3185. });
  3186. return this;
  3187. },
  3188. /**
  3189. * destroy Editor Layout and dettach Key and Mouse Event
  3190. * @returns {this}
  3191. */
  3192. destroy: function () {
  3193. this.each(function (idx, elHolder) {
  3194. var $holder = $(elHolder);
  3195. var info = renderer.layoutInfoFromHolder($holder);
  3196. if (!info || !info.editable) { return; }
  3197. eventHandler.dettach(info);
  3198. renderer.removeLayout($holder);
  3199. });
  3200. return this;
  3201. }
  3202. });
  3203. }));