Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | /** |
| 2 | * Super simple wysiwyg editor on Bootstrap v0.5.10 |
||
| 3 | * http://hackerwins.github.io/summernote/ |
||
| 4 | * |
||
| 5 | * summernote.js |
||
| 6 | * Copyright 2013-2014 Alan Hong. and other contributors |
||
| 7 | * summernote may be freely distributed under the MIT license./ |
||
| 8 | * |
||
| 9 | * Date: 2014-10-03T06:12Z |
||
| 10 | */ |
||
| 11 | (function (factory) { |
||
| 12 | /* global define */ |
||
| 13 | if (typeof define === 'function' && define.amd) { |
||
| 14 | // AMD. Register as an anonymous module. |
||
| 15 | define(['jquery'], factory); |
||
| 16 | } else { |
||
| 17 | // Browser globals: jQuery |
||
| 18 | factory(window.jQuery); |
||
| 19 | } |
||
| 20 | }(function ($) { |
||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | if ('function' !== typeof Array.prototype.reduce) { |
||
| 25 | /** |
||
| 26 | * Array.prototype.reduce fallback |
||
| 27 | * |
||
| 28 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce |
||
| 29 | */ |
||
| 30 | Array.prototype.reduce = function (callback, optInitialValue) { |
||
| 31 | var idx, value, length = this.length >>> 0, isValueSet = false; |
||
| 32 | if (1 < arguments.length) { |
||
| 33 | value = optInitialValue; |
||
| 34 | isValueSet = true; |
||
| 35 | } |
||
| 36 | for (idx = 0; length > idx; ++idx) { |
||
| 37 | if (this.hasOwnProperty(idx)) { |
||
| 38 | if (isValueSet) { |
||
| 39 | value = callback(value, this[idx], idx, this); |
||
| 40 | } else { |
||
| 41 | value = this[idx]; |
||
| 42 | isValueSet = true; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | } |
||
| 46 | if (!isValueSet) { |
||
| 47 | throw new TypeError('Reduce of empty array with no initial value'); |
||
| 48 | } |
||
| 49 | return value; |
||
| 50 | }; |
||
| 51 | } |
||
| 52 | |||
| 53 | if ('function' !== typeof Array.prototype.filter) { |
||
| 54 | Array.prototype.filter = function (fun/*, thisArg*/) { |
||
| 55 | if (this === void 0 || this === null) { |
||
| 56 | throw new TypeError(); |
||
| 57 | } |
||
| 58 | |||
| 59 | var t = Object(this); |
||
| 60 | var len = t.length >>> 0; |
||
| 61 | if (typeof fun !== 'function') { |
||
| 62 | throw new TypeError(); |
||
| 63 | } |
||
| 64 | |||
| 65 | var res = []; |
||
| 66 | var thisArg = arguments.length >= 2 ? arguments[1] : void 0; |
||
| 67 | for (var i = 0; i < len; i++) { |
||
| 68 | if (i in t) { |
||
| 69 | var val = t[i]; |
||
| 70 | if (fun.call(thisArg, val, i, t)) { |
||
| 71 | res.push(val); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | return res; |
||
| 77 | }; |
||
| 78 | } |
||
| 79 | |||
| 80 | var isSupportAmd = typeof define === 'function' && define.amd; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * returns whether font is installed or not. |
||
| 84 | * @param {String} fontName |
||
| 85 | * @return {Boolean} |
||
| 86 | */ |
||
| 87 | var isFontInstalled = function (fontName) { |
||
| 88 | var testFontName = fontName === 'Comic Sans MS' ? 'Courier New' : 'Comic Sans MS'; |
||
| 89 | var $tester = $('<div>').css({ |
||
| 90 | position: 'absolute', |
||
| 91 | left: '-9999px', |
||
| 92 | top: '-9999px', |
||
| 93 | fontSize: '200px' |
||
| 94 | }).text('mmmmmmmmmwwwwwww').appendTo(document.body); |
||
| 95 | |||
| 96 | var originalWidth = $tester.css('fontFamily', testFontName).width(); |
||
| 97 | var width = $tester.css('fontFamily', fontName + ',' + testFontName).width(); |
||
| 98 | |||
| 99 | $tester.remove(); |
||
| 100 | |||
| 101 | return originalWidth !== width; |
||
| 102 | }; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Object which check platform and agent |
||
| 106 | */ |
||
| 107 | var agent = { |
||
| 108 | isMac: navigator.appVersion.indexOf('Mac') > -1, |
||
| 109 | isMSIE: navigator.userAgent.indexOf('MSIE') > -1 || navigator.userAgent.indexOf('Trident') > -1, |
||
| 110 | isFF: navigator.userAgent.indexOf('Firefox') > -1, |
||
| 111 | jqueryVersion: parseFloat($.fn.jquery), |
||
| 112 | isSupportAmd: isSupportAmd, |
||
| 113 | hasCodeMirror: isSupportAmd ? require.specified('CodeMirror') : !!window.CodeMirror, |
||
| 114 | isFontInstalled: isFontInstalled, |
||
| 115 | isW3CRangeSupport: !!document.createRange |
||
| 116 | }; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * func utils (for high-order func's arg) |
||
| 120 | */ |
||
| 121 | var func = (function () { |
||
| 122 | var eq = function (itemA) { |
||
| 123 | return function (itemB) { |
||
| 124 | return itemA === itemB; |
||
| 125 | }; |
||
| 126 | }; |
||
| 127 | |||
| 128 | var eq2 = function (itemA, itemB) { |
||
| 129 | return itemA === itemB; |
||
| 130 | }; |
||
| 131 | |||
| 132 | var peq2 = function (propName) { |
||
| 133 | return function (itemA, itemB) { |
||
| 134 | return itemA[propName] === itemB[propName]; |
||
| 135 | }; |
||
| 136 | }; |
||
| 137 | |||
| 138 | var ok = function () { |
||
| 139 | return true; |
||
| 140 | }; |
||
| 141 | |||
| 142 | var fail = function () { |
||
| 143 | return false; |
||
| 144 | }; |
||
| 145 | |||
| 146 | var not = function (f) { |
||
| 147 | return function () { |
||
| 148 | return !f.apply(f, arguments); |
||
| 149 | }; |
||
| 150 | }; |
||
| 151 | |||
| 152 | var and = function (fA, fB) { |
||
| 153 | return function (item) { |
||
| 154 | return fA(item) && fB(item); |
||
| 155 | }; |
||
| 156 | }; |
||
| 157 | |||
| 158 | var self = function (a) { |
||
| 159 | return a; |
||
| 160 | }; |
||
| 161 | |||
| 162 | var idCounter = 0; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * generate a globally-unique id |
||
| 166 | * |
||
| 167 | * @param {String} [prefix] |
||
| 168 | */ |
||
| 169 | var uniqueId = function (prefix) { |
||
| 170 | var id = ++idCounter + ''; |
||
| 171 | return prefix ? prefix + id : id; |
||
| 172 | }; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * returns bnd (bounds) from rect |
||
| 176 | * |
||
| 177 | * - IE Compatability Issue: http://goo.gl/sRLOAo |
||
| 178 | * - Scroll Issue: http://goo.gl/sNjUc |
||
| 179 | * |
||
| 180 | * @param {Rect} rect |
||
| 181 | * @return {Object} bounds |
||
| 182 | * @return {Number} bounds.top |
||
| 183 | * @return {Number} bounds.left |
||
| 184 | * @return {Number} bounds.width |
||
| 185 | * @return {Number} bounds.height |
||
| 186 | */ |
||
| 187 | var rect2bnd = function (rect) { |
||
| 188 | var $document = $(document); |
||
| 189 | return { |
||
| 190 | top: rect.top + $document.scrollTop(), |
||
| 191 | left: rect.left + $document.scrollLeft(), |
||
| 192 | width: rect.right - rect.left, |
||
| 193 | height: rect.bottom - rect.top |
||
| 194 | }; |
||
| 195 | }; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * returns a copy of the object where the keys have become the values and the values the keys. |
||
| 199 | * @param {Object} obj |
||
| 200 | * @return {Object} |
||
| 201 | */ |
||
| 202 | var invertObject = function (obj) { |
||
| 203 | var inverted = {}; |
||
| 204 | for (var key in obj) { |
||
| 205 | if (obj.hasOwnProperty(key)) { |
||
| 206 | inverted[obj[key]] = key; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | return inverted; |
||
| 210 | }; |
||
| 211 | |||
| 212 | return { |
||
| 213 | eq: eq, |
||
| 214 | eq2: eq2, |
||
| 215 | peq2: peq2, |
||
| 216 | ok: ok, |
||
| 217 | fail: fail, |
||
| 218 | self: self, |
||
| 219 | not: not, |
||
| 220 | and: and, |
||
| 221 | uniqueId: uniqueId, |
||
| 222 | rect2bnd: rect2bnd, |
||
| 223 | invertObject: invertObject |
||
| 224 | }; |
||
| 225 | })(); |
||
| 226 | |||
| 227 | /** |
||
| 228 | * list utils |
||
| 229 | */ |
||
| 230 | var list = (function () { |
||
| 231 | /** |
||
| 232 | * returns the first item of an array. |
||
| 233 | * |
||
| 234 | * @param {Array} array |
||
| 235 | */ |
||
| 236 | var head = function (array) { |
||
| 237 | return array[0]; |
||
| 238 | }; |
||
| 239 | |||
| 240 | /** |
||
| 241 | * returns the last item of an array. |
||
| 242 | * |
||
| 243 | * @param {Array} array |
||
| 244 | */ |
||
| 245 | var last = function (array) { |
||
| 246 | return array[array.length - 1]; |
||
| 247 | }; |
||
| 248 | |||
| 249 | /** |
||
| 250 | * returns everything but the last entry of the array. |
||
| 251 | * |
||
| 252 | * @param {Array} array |
||
| 253 | */ |
||
| 254 | var initial = function (array) { |
||
| 255 | return array.slice(0, array.length - 1); |
||
| 256 | }; |
||
| 257 | |||
| 258 | /** |
||
| 259 | * returns the rest of the items in an array. |
||
| 260 | * |
||
| 261 | * @param {Array} array |
||
| 262 | */ |
||
| 263 | var tail = function (array) { |
||
| 264 | return array.slice(1); |
||
| 265 | }; |
||
| 266 | |||
| 267 | /** |
||
| 268 | * returns item of array |
||
| 269 | */ |
||
| 270 | var find = function (array, pred) { |
||
| 271 | for (var idx = 0, len = array.length; idx < len; idx ++) { |
||
| 272 | var item = array[idx]; |
||
| 273 | if (pred(item)) { |
||
| 274 | return item; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | }; |
||
| 278 | |||
| 279 | /** |
||
| 280 | * returns true if all of the values in the array pass the predicate truth test. |
||
| 281 | */ |
||
| 282 | var all = function (array, pred) { |
||
| 283 | for (var idx = 0, len = array.length; idx < len; idx ++) { |
||
| 284 | if (!pred(array[idx])) { |
||
| 285 | return false; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | return true; |
||
| 289 | }; |
||
| 290 | |||
| 291 | /** |
||
| 292 | * returns true if the value is present in the list. |
||
| 293 | */ |
||
| 294 | var contains = function (array, item) { |
||
| 295 | return $.inArray(item, array) !== -1; |
||
| 296 | }; |
||
| 297 | |||
| 298 | /** |
||
| 299 | * get sum from a list |
||
| 300 | * |
||
| 301 | * @param {Array} array - array |
||
| 302 | * @param {Function} fn - iterator |
||
| 303 | */ |
||
| 304 | var sum = function (array, fn) { |
||
| 305 | fn = fn || func.self; |
||
| 306 | return array.reduce(function (memo, v) { |
||
| 307 | return memo + fn(v); |
||
| 308 | }, 0); |
||
| 309 | }; |
||
| 310 | |||
| 311 | /** |
||
| 312 | * returns a copy of the collection with array type. |
||
| 313 | * @param {Collection} collection - collection eg) node.childNodes, ... |
||
| 314 | */ |
||
| 315 | var from = function (collection) { |
||
| 316 | var result = [], idx = -1, length = collection.length; |
||
| 317 | while (++idx < length) { |
||
| 318 | result[idx] = collection[idx]; |
||
| 319 | } |
||
| 320 | return result; |
||
| 321 | }; |
||
| 322 | |||
| 323 | /** |
||
| 324 | * cluster elements by predicate function. |
||
| 325 | * |
||
| 326 | * @param {Array} array - array |
||
| 327 | * @param {Function} fn - predicate function for cluster rule |
||
| 328 | * @param {Array[]} |
||
| 329 | */ |
||
| 330 | var clusterBy = function (array, fn) { |
||
| 331 | if (!array.length) { return []; } |
||
| 332 | var aTail = tail(array); |
||
| 333 | return aTail.reduce(function (memo, v) { |
||
| 334 | var aLast = last(memo); |
||
| 335 | if (fn(last(aLast), v)) { |
||
| 336 | aLast[aLast.length] = v; |
||
| 337 | } else { |
||
| 338 | memo[memo.length] = [v]; |
||
| 339 | } |
||
| 340 | return memo; |
||
| 341 | }, [[head(array)]]); |
||
| 342 | }; |
||
| 343 | |||
| 344 | /** |
||
| 345 | * returns a copy of the array with all falsy values removed |
||
| 346 | * |
||
| 347 | * @param {Array} array - array |
||
| 348 | * @param {Function} fn - predicate function for cluster rule |
||
| 349 | */ |
||
| 350 | var compact = function (array) { |
||
| 351 | var aResult = []; |
||
| 352 | for (var idx = 0, len = array.length; idx < len; idx ++) { |
||
| 353 | if (array[idx]) { aResult.push(array[idx]); } |
||
| 354 | } |
||
| 355 | return aResult; |
||
| 356 | }; |
||
| 357 | |||
| 358 | /** |
||
| 359 | * produces a duplicate-free version of the array |
||
| 360 | * |
||
| 361 | * @param {Array} array |
||
| 362 | */ |
||
| 363 | var unique = function (array) { |
||
| 364 | var results = []; |
||
| 365 | |||
| 366 | for (var idx = 0, len = array.length; idx < len; idx ++) { |
||
| 367 | if (!contains(results, array[idx])) { |
||
| 368 | results.push(array[idx]); |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | return results; |
||
| 373 | }; |
||
| 374 | |||
| 375 | return { head: head, last: last, initial: initial, tail: tail, |
||
| 376 | find: find, contains: contains, |
||
| 377 | all: all, sum: sum, from: from, |
||
| 378 | clusterBy: clusterBy, compact: compact, unique: unique }; |
||
| 379 | })(); |
||
| 380 | |||
| 381 | |||
| 382 | var NBSP_CHAR = String.fromCharCode(160); |
||
| 383 | var ZERO_WIDTH_NBSP_CHAR = '\ufeff'; |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Dom functions |
||
| 387 | */ |
||
| 388 | var dom = (function () { |
||
| 389 | /** |
||
| 390 | * returns whether node is `note-editable` or not. |
||
| 391 | * |
||
| 392 | * @param {Node} node |
||
| 393 | * @return {Boolean} |
||
| 394 | */ |
||
| 395 | var isEditable = function (node) { |
||
| 396 | return node && $(node).hasClass('note-editable'); |
||
| 397 | }; |
||
| 398 | |||
| 399 | /** |
||
| 400 | * returns whether node is `note-control-sizing` or not. |
||
| 401 | * |
||
| 402 | * @param {Node} node |
||
| 403 | * @return {Boolean} |
||
| 404 | */ |
||
| 405 | var isControlSizing = function (node) { |
||
| 406 | return node && $(node).hasClass('note-control-sizing'); |
||
| 407 | }; |
||
| 408 | |||
| 409 | /** |
||
| 410 | * build layoutInfo from $editor(.note-editor) |
||
| 411 | * |
||
| 412 | * @param {jQuery} $editor |
||
| 413 | * @return {Object} |
||
| 414 | */ |
||
| 415 | var buildLayoutInfo = function ($editor) { |
||
| 416 | var makeFinder; |
||
| 417 | |||
| 418 | // air mode |
||
| 419 | if ($editor.hasClass('note-air-editor')) { |
||
| 420 | var id = list.last($editor.attr('id').split('-')); |
||
| 421 | makeFinder = function (sIdPrefix) { |
||
| 422 | return function () { return $(sIdPrefix + id); }; |
||
| 423 | }; |
||
| 424 | |||
| 425 | return { |
||
| 426 | editor: function () { return $editor; }, |
||
| 427 | editable: function () { return $editor; }, |
||
| 428 | popover: makeFinder('#note-popover-'), |
||
| 429 | handle: makeFinder('#note-handle-'), |
||
| 430 | dialog: makeFinder('#note-dialog-') |
||
| 431 | }; |
||
| 432 | |||
| 433 | // frame mode |
||
| 434 | } else { |
||
| 435 | makeFinder = function (sClassName) { |
||
| 436 | return function () { return $editor.find(sClassName); }; |
||
| 437 | }; |
||
| 438 | return { |
||
| 439 | editor: function () { return $editor; }, |
||
| 440 | dropzone: makeFinder('.note-dropzone'), |
||
| 441 | toolbar: makeFinder('.note-toolbar'), |
||
| 442 | editable: makeFinder('.note-editable'), |
||
| 443 | codable: makeFinder('.note-codable'), |
||
| 444 | statusbar: makeFinder('.note-statusbar'), |
||
| 445 | popover: makeFinder('.note-popover'), |
||
| 446 | handle: makeFinder('.note-handle'), |
||
| 447 | dialog: makeFinder('.note-dialog') |
||
| 448 | }; |
||
| 449 | } |
||
| 450 | }; |
||
| 451 | |||
| 452 | /** |
||
| 453 | * returns predicate which judge whether nodeName is same |
||
| 454 | * |
||
| 455 | * @param {String} nodeName |
||
| 456 | * @return {String} |
||
| 457 | */ |
||
| 458 | var makePredByNodeName = function (nodeName) { |
||
| 459 | nodeName = nodeName.toUpperCase(); |
||
| 460 | return function (node) { |
||
| 461 | return node && node.nodeName.toUpperCase() === nodeName; |
||
| 462 | }; |
||
| 463 | }; |
||
| 464 | |||
| 465 | var isText = function (node) { |
||
| 466 | return node && node.nodeType === 3; |
||
| 467 | }; |
||
| 468 | |||
| 469 | /** |
||
| 470 | * ex) br, col, embed, hr, img, input, ... |
||
| 471 | * @see http://www.w3.org/html/wg/drafts/html/master/syntax.html#void-elements |
||
| 472 | */ |
||
| 473 | var isVoid = function (node) { |
||
| 474 | return node && /^BR|^IMG|^HR/.test(node.nodeName.toUpperCase()); |
||
| 475 | }; |
||
| 476 | |||
| 477 | var isPara = function (node) { |
||
| 478 | if (isEditable(node)) { |
||
| 479 | return false; |
||
| 480 | } |
||
| 481 | |||
| 482 | // Chrome(v31.0), FF(v25.0.1) use DIV for paragraph |
||
| 483 | return node && /^DIV|^P|^LI|^H[1-7]/.test(node.nodeName.toUpperCase()); |
||
| 484 | }; |
||
| 485 | |||
| 486 | var isLi = makePredByNodeName('LI'); |
||
| 487 | |||
| 488 | var isPurePara = function (node) { |
||
| 489 | return isPara(node) && !isLi(node); |
||
| 490 | }; |
||
| 491 | |||
| 492 | var isInline = function (node) { |
||
| 493 | return !isBodyContainer(node) && !isList(node) && !isPara(node); |
||
| 494 | }; |
||
| 495 | |||
| 496 | var isList = function (node) { |
||
| 497 | return node && /^UL|^OL/.test(node.nodeName.toUpperCase()); |
||
| 498 | }; |
||
| 499 | |||
| 500 | var isCell = function (node) { |
||
| 501 | return node && /^TD|^TH/.test(node.nodeName.toUpperCase()); |
||
| 502 | }; |
||
| 503 | |||
| 504 | var isBlockquote = makePredByNodeName('BLOCKQUOTE'); |
||
| 505 | |||
| 506 | var isBodyContainer = function (node) { |
||
| 507 | return isCell(node) || isBlockquote(node) || isEditable(node); |
||
| 508 | }; |
||
| 509 | |||
| 510 | var isAnchor = makePredByNodeName('A'); |
||
| 511 | |||
| 512 | var isParaInline = function (node) { |
||
| 513 | return isInline(node) && !!ancestor(node, isPara); |
||
| 514 | }; |
||
| 515 | |||
| 516 | var isBodyInline = function (node) { |
||
| 517 | return isInline(node) && !ancestor(node, isPara); |
||
| 518 | }; |
||
| 519 | |||
| 520 | var isBody = makePredByNodeName('BODY'); |
||
| 521 | |||
| 522 | /** |
||
| 523 | * blank HTML for cursor position |
||
| 524 | */ |
||
| 525 | var blankHTML = agent.isMSIE ? ' ' : '<br>'; |
||
| 526 | |||
| 527 | /** |
||
| 528 | * returns #text's text size or element's childNodes size |
||
| 529 | * |
||
| 530 | * @param {Node} node |
||
| 531 | */ |
||
| 532 | var nodeLength = function (node) { |
||
| 533 | if (isText(node)) { |
||
| 534 | return node.nodeValue.length; |
||
| 535 | } |
||
| 536 | |||
| 537 | return node.childNodes.length; |
||
| 538 | }; |
||
| 539 | |||
| 540 | /** |
||
| 541 | * returns whether node is empty or not. |
||
| 542 | * |
||
| 543 | * @param {Node} node |
||
| 544 | * @return {Boolean} |
||
| 545 | */ |
||
| 546 | var isEmpty = function (node) { |
||
| 547 | var len = nodeLength(node); |
||
| 548 | |||
| 549 | if (len === 0) { |
||
| 550 | return true; |
||
| 551 | } else if (!dom.isText(node) && len === 1 && node.innerHTML === blankHTML) { |
||
| 552 | // ex) <p><br></p>, <span><br></span> |
||
| 553 | return true; |
||
| 554 | } |
||
| 555 | |||
| 556 | return false; |
||
| 557 | }; |
||
| 558 | |||
| 559 | /** |
||
| 560 | * padding blankHTML if node is empty (for cursor position) |
||
| 561 | */ |
||
| 562 | var paddingBlankHTML = function (node) { |
||
| 563 | if (!isVoid(node) && !nodeLength(node)) { |
||
| 564 | node.innerHTML = blankHTML; |
||
| 565 | } |
||
| 566 | }; |
||
| 567 | |||
| 568 | /** |
||
| 569 | * find nearest ancestor predicate hit |
||
| 570 | * |
||
| 571 | * @param {Node} node |
||
| 572 | * @param {Function} pred - predicate function |
||
| 573 | */ |
||
| 574 | var ancestor = function (node, pred) { |
||
| 575 | while (node) { |
||
| 576 | if (pred(node)) { return node; } |
||
| 577 | if (isEditable(node)) { break; } |
||
| 578 | |||
| 579 | node = node.parentNode; |
||
| 580 | } |
||
| 581 | return null; |
||
| 582 | }; |
||
| 583 | |||
| 584 | /** |
||
| 585 | * returns new array of ancestor nodes (until predicate hit). |
||
| 586 | * |
||
| 587 | * @param {Node} node |
||
| 588 | * @param {Function} [optional] pred - predicate function |
||
| 589 | */ |
||
| 590 | var listAncestor = function (node, pred) { |
||
| 591 | pred = pred || func.fail; |
||
| 592 | |||
| 593 | var ancestors = []; |
||
| 594 | ancestor(node, function (el) { |
||
| 595 | if (!isEditable(el)) { |
||
| 596 | ancestors.push(el); |
||
| 597 | } |
||
| 598 | |||
| 599 | return pred(el); |
||
| 600 | }); |
||
| 601 | return ancestors; |
||
| 602 | }; |
||
| 603 | |||
| 604 | /** |
||
| 605 | * find farthest ancestor predicate hit |
||
| 606 | */ |
||
| 607 | var lastAncestor = function (node, pred) { |
||
| 608 | var ancestors = listAncestor(node); |
||
| 609 | return list.last(ancestors.filter(pred)); |
||
| 610 | }; |
||
| 611 | |||
| 612 | /** |
||
| 613 | * returns common ancestor node between two nodes. |
||
| 614 | * |
||
| 615 | * @param {Node} nodeA |
||
| 616 | * @param {Node} nodeB |
||
| 617 | */ |
||
| 618 | var commonAncestor = function (nodeA, nodeB) { |
||
| 619 | var ancestors = listAncestor(nodeA); |
||
| 620 | for (var n = nodeB; n; n = n.parentNode) { |
||
| 621 | if ($.inArray(n, ancestors) > -1) { return n; } |
||
| 622 | } |
||
| 623 | return null; // difference document area |
||
| 624 | }; |
||
| 625 | |||
| 626 | /** |
||
| 627 | * listing all previous siblings (until predicate hit). |
||
| 628 | * |
||
| 629 | * @param {Node} node |
||
| 630 | * @param {Function} [optional] pred - predicate function |
||
| 631 | */ |
||
| 632 | var listPrev = function (node, pred) { |
||
| 633 | pred = pred || func.fail; |
||
| 634 | |||
| 635 | var nodes = []; |
||
| 636 | while (node) { |
||
| 637 | if (pred(node)) { break; } |
||
| 638 | nodes.push(node); |
||
| 639 | node = node.previousSibling; |
||
| 640 | } |
||
| 641 | return nodes; |
||
| 642 | }; |
||
| 643 | |||
| 644 | /** |
||
| 645 | * listing next siblings (until predicate hit). |
||
| 646 | * |
||
| 647 | * @param {Node} node |
||
| 648 | * @param {Function} [pred] - predicate function |
||
| 649 | */ |
||
| 650 | var listNext = function (node, pred) { |
||
| 651 | pred = pred || func.fail; |
||
| 652 | |||
| 653 | var nodes = []; |
||
| 654 | while (node) { |
||
| 655 | if (pred(node)) { break; } |
||
| 656 | nodes.push(node); |
||
| 657 | node = node.nextSibling; |
||
| 658 | } |
||
| 659 | return nodes; |
||
| 660 | }; |
||
| 661 | |||
| 662 | /** |
||
| 663 | * listing descendant nodes |
||
| 664 | * |
||
| 665 | * @param {Node} node |
||
| 666 | * @param {Function} [pred] - predicate function |
||
| 667 | */ |
||
| 668 | var listDescendant = function (node, pred) { |
||
| 669 | var descendents = []; |
||
| 670 | pred = pred || func.ok; |
||
| 671 | |||
| 672 | // start DFS(depth first search) with node |
||
| 673 | (function fnWalk(current) { |
||
| 674 | if (node !== current && pred(current)) { |
||
| 675 | descendents.push(current); |
||
| 676 | } |
||
| 677 | for (var idx = 0, len = current.childNodes.length; idx < len; idx++) { |
||
| 678 | fnWalk(current.childNodes[idx]); |
||
| 679 | } |
||
| 680 | })(node); |
||
| 681 | |||
| 682 | return descendents; |
||
| 683 | }; |
||
| 684 | |||
| 685 | /** |
||
| 686 | * wrap node with new tag. |
||
| 687 | * |
||
| 688 | * @param {Node} node |
||
| 689 | * @param {Node} tagName of wrapper |
||
| 690 | * @return {Node} - wrapper |
||
| 691 | */ |
||
| 692 | var wrap = function (node, wrapperName) { |
||
| 693 | var parent = node.parentNode; |
||
| 694 | var wrapper = $('<' + wrapperName + '>')[0]; |
||
| 695 | |||
| 696 | parent.insertBefore(wrapper, node); |
||
| 697 | wrapper.appendChild(node); |
||
| 698 | |||
| 699 | return wrapper; |
||
| 700 | }; |
||
| 701 | |||
| 702 | /** |
||
| 703 | * insert node after preceding |
||
| 704 | * |
||
| 705 | * @param {Node} node |
||
| 706 | * @param {Node} preceding - predicate function |
||
| 707 | */ |
||
| 708 | var insertAfter = function (node, preceding) { |
||
| 709 | var next = preceding.nextSibling, parent = preceding.parentNode; |
||
| 710 | if (next) { |
||
| 711 | parent.insertBefore(node, next); |
||
| 712 | } else { |
||
| 713 | parent.appendChild(node); |
||
| 714 | } |
||
| 715 | return node; |
||
| 716 | }; |
||
| 717 | |||
| 718 | /** |
||
| 719 | * append elements. |
||
| 720 | * |
||
| 721 | * @param {Node} node |
||
| 722 | * @param {Collection} aChild |
||
| 723 | */ |
||
| 724 | var appendChildNodes = function (node, aChild) { |
||
| 725 | $.each(aChild, function (idx, child) { |
||
| 726 | node.appendChild(child); |
||
| 727 | }); |
||
| 728 | return node; |
||
| 729 | }; |
||
| 730 | |||
| 731 | /** |
||
| 732 | * returns whether boundaryPoint is left edge or not. |
||
| 733 | * |
||
| 734 | * @param {BoundaryPoint} point |
||
| 735 | * @return {Boolean} |
||
| 736 | */ |
||
| 737 | var isLeftEdgePoint = function (point) { |
||
| 738 | return point.offset === 0; |
||
| 739 | }; |
||
| 740 | |||
| 741 | /** |
||
| 742 | * returns whether boundaryPoint is right edge or not. |
||
| 743 | * |
||
| 744 | * @param {BoundaryPoint} point |
||
| 745 | * @return {Boolean} |
||
| 746 | */ |
||
| 747 | var isRightEdgePoint = function (point) { |
||
| 748 | return point.offset === nodeLength(point.node); |
||
| 749 | }; |
||
| 750 | |||
| 751 | /** |
||
| 752 | * returns whether boundaryPoint is edge or not. |
||
| 753 | * |
||
| 754 | * @param {BoundaryPoint} point |
||
| 755 | * @return {Boolean} |
||
| 756 | */ |
||
| 757 | var isEdgePoint = function (point) { |
||
| 758 | return isLeftEdgePoint(point) || isRightEdgePoint(point); |
||
| 759 | }; |
||
| 760 | |||
| 761 | /** |
||
| 762 | * returns wheter node is left edge of ancestor or not. |
||
| 763 | * |
||
| 764 | * @param {Node} node |
||
| 765 | * @param {Node} ancestor |
||
| 766 | * @return {Boolean} |
||
| 767 | */ |
||
| 768 | var isLeftEdgeOf = function (node, ancestor) { |
||
| 769 | while (node && node !== ancestor) { |
||
| 770 | if (position(node) !== 0) { |
||
| 771 | return false; |
||
| 772 | } |
||
| 773 | node = node.parentNode; |
||
| 774 | } |
||
| 775 | |||
| 776 | return true; |
||
| 777 | }; |
||
| 778 | |||
| 779 | /** |
||
| 780 | * returns whether node is right edge of ancestor or not. |
||
| 781 | * |
||
| 782 | * @param {Node} node |
||
| 783 | * @param {Node} ancestor |
||
| 784 | * @return {Boolean} |
||
| 785 | */ |
||
| 786 | var isRightEdgeOf = function (node, ancestor) { |
||
| 787 | while (node && node !== ancestor) { |
||
| 788 | if (position(node) !== nodeLength(node.parentNode) - 1) { |
||
| 789 | return false; |
||
| 790 | } |
||
| 791 | node = node.parentNode; |
||
| 792 | } |
||
| 793 | |||
| 794 | return true; |
||
| 795 | }; |
||
| 796 | |||
| 797 | /** |
||
| 798 | * returns offset from parent. |
||
| 799 | * |
||
| 800 | * @param {Node} node |
||
| 801 | */ |
||
| 802 | var position = function (node) { |
||
| 803 | var offset = 0; |
||
| 804 | while ((node = node.previousSibling)) { |
||
| 805 | offset += 1; |
||
| 806 | } |
||
| 807 | return offset; |
||
| 808 | }; |
||
| 809 | |||
| 810 | var hasChildren = function (node) { |
||
| 811 | return !!(node && node.childNodes && node.childNodes.length); |
||
| 812 | }; |
||
| 813 | |||
| 814 | /** |
||
| 815 | * returns previous boundaryPoint |
||
| 816 | * |
||
| 817 | * @param {BoundaryPoint} point |
||
| 818 | * @param {Boolean} isSkipInnerOffset |
||
| 819 | * @return {BoundaryPoint} |
||
| 820 | */ |
||
| 821 | var prevPoint = function (point, isSkipInnerOffset) { |
||
| 822 | var node, offset; |
||
| 823 | |||
| 824 | if (point.offset === 0) { |
||
| 825 | if (isEditable(point.node)) { |
||
| 826 | return null; |
||
| 827 | } |
||
| 828 | |||
| 829 | node = point.node.parentNode; |
||
| 830 | offset = position(point.node); |
||
| 831 | } else if (hasChildren(point.node)) { |
||
| 832 | node = point.node.childNodes[point.offset - 1]; |
||
| 833 | offset = nodeLength(node); |
||
| 834 | } else { |
||
| 835 | node = point.node; |
||
| 836 | offset = isSkipInnerOffset ? 0 : point.offset - 1; |
||
| 837 | } |
||
| 838 | |||
| 839 | return { |
||
| 840 | node: node, |
||
| 841 | offset: offset |
||
| 842 | }; |
||
| 843 | }; |
||
| 844 | |||
| 845 | /** |
||
| 846 | * returns next boundaryPoint |
||
| 847 | * |
||
| 848 | * @param {BoundaryPoint} point |
||
| 849 | * @param {Boolean} isSkipInnerOffset |
||
| 850 | * @return {BoundaryPoint} |
||
| 851 | */ |
||
| 852 | var nextPoint = function (point, isSkipInnerOffset) { |
||
| 853 | var node, offset; |
||
| 854 | |||
| 855 | if (nodeLength(point.node) === point.offset) { |
||
| 856 | if (isEditable(point.node)) { |
||
| 857 | return null; |
||
| 858 | } |
||
| 859 | |||
| 860 | node = point.node.parentNode; |
||
| 861 | offset = position(point.node) + 1; |
||
| 862 | } else if (hasChildren(point.node)) { |
||
| 863 | node = point.node.childNodes[point.offset]; |
||
| 864 | offset = 0; |
||
| 865 | } else { |
||
| 866 | node = point.node; |
||
| 867 | offset = isSkipInnerOffset ? nodeLength(point.node) : point.offset + 1; |
||
| 868 | } |
||
| 869 | |||
| 870 | return { |
||
| 871 | node: node, |
||
| 872 | offset: offset |
||
| 873 | }; |
||
| 874 | }; |
||
| 875 | |||
| 876 | /** |
||
| 877 | * returns whether pointA and pointB is same or not. |
||
| 878 | * |
||
| 879 | * @param {BoundaryPoint} pointA |
||
| 880 | * @param {BoundaryPoint} pointB |
||
| 881 | * @return {Boolean} |
||
| 882 | */ |
||
| 883 | var isSamePoint = function (pointA, pointB) { |
||
| 884 | return pointA.node === pointB.node && pointA.offset === pointB.offset; |
||
| 885 | }; |
||
| 886 | |||
| 887 | /** |
||
| 888 | * returns whether point is visible (can set cursor) or not. |
||
| 889 | * |
||
| 890 | * @param {BoundaryPoint} point |
||
| 891 | * @return {Boolean} |
||
| 892 | */ |
||
| 893 | var isVisiblePoint = function (point) { |
||
| 894 | if (isText(point.node) || !hasChildren(point.node) || isEmpty(point.node)) { |
||
| 895 | return true; |
||
| 896 | } |
||
| 897 | |||
| 898 | var leftNode = point.node.childNodes[point.offset - 1]; |
||
| 899 | var rightNode = point.node.childNodes[point.offset]; |
||
| 900 | if ((!leftNode || isVoid(leftNode)) && (!rightNode || isVoid(rightNode))) { |
||
| 901 | return true; |
||
| 902 | } |
||
| 903 | |||
| 904 | return false; |
||
| 905 | }; |
||
| 906 | |||
| 907 | /** |
||
| 908 | * @param {BoundaryPoint} point |
||
| 909 | * @param {Function} pred |
||
| 910 | * @return {BoundaryPoint} |
||
| 911 | */ |
||
| 912 | var prevPointUntil = function (point, pred) { |
||
| 913 | while (point) { |
||
| 914 | if (pred(point)) { |
||
| 915 | return point; |
||
| 916 | } |
||
| 917 | |||
| 918 | point = prevPoint(point); |
||
| 919 | } |
||
| 920 | |||
| 921 | return null; |
||
| 922 | }; |
||
| 923 | |||
| 924 | /** |
||
| 925 | * @param {BoundaryPoint} point |
||
| 926 | * @param {Function} pred |
||
| 927 | * @return {BoundaryPoint} |
||
| 928 | */ |
||
| 929 | var nextPointUntil = function (point, pred) { |
||
| 930 | while (point) { |
||
| 931 | if (pred(point)) { |
||
| 932 | return point; |
||
| 933 | } |
||
| 934 | |||
| 935 | point = nextPoint(point); |
||
| 936 | } |
||
| 937 | |||
| 938 | return null; |
||
| 939 | }; |
||
| 940 | |||
| 941 | /** |
||
| 942 | * @param {BoundaryPoint} startPoint |
||
| 943 | * @param {BoundaryPoint} endPoint |
||
| 944 | * @param {Function} handler |
||
| 945 | * @param {Boolean} isSkipInnerOffset |
||
| 946 | */ |
||
| 947 | var walkPoint = function (startPoint, endPoint, handler, isSkipInnerOffset) { |
||
| 948 | var point = startPoint; |
||
| 949 | |||
| 950 | while (point) { |
||
| 951 | handler(point); |
||
| 952 | |||
| 953 | if (isSamePoint(point, endPoint)) { |
||
| 954 | break; |
||
| 955 | } |
||
| 956 | |||
| 957 | var isSkipOffset = isSkipInnerOffset && |
||
| 958 | startPoint.node !== point.node && |
||
| 959 | endPoint.node !== point.node; |
||
| 960 | point = nextPoint(point, isSkipOffset); |
||
| 961 | } |
||
| 962 | }; |
||
| 963 | |||
| 964 | /** |
||
| 965 | * return offsetPath(array of offset) from ancestor |
||
| 966 | * |
||
| 967 | * @param {Node} ancestor - ancestor node |
||
| 968 | * @param {Node} node |
||
| 969 | */ |
||
| 970 | var makeOffsetPath = function (ancestor, node) { |
||
| 971 | var ancestors = listAncestor(node, func.eq(ancestor)); |
||
| 972 | return $.map(ancestors, position).reverse(); |
||
| 973 | }; |
||
| 974 | |||
| 975 | /** |
||
| 976 | * return element from offsetPath(array of offset) |
||
| 977 | * |
||
| 978 | * @param {Node} ancestor - ancestor node |
||
| 979 | * @param {array} aOffset - offsetPath |
||
| 980 | */ |
||
| 981 | var fromOffsetPath = function (ancestor, aOffset) { |
||
| 982 | var current = ancestor; |
||
| 983 | for (var i = 0, len = aOffset.length; i < len; i++) { |
||
| 984 | if (current.childNodes.length <= aOffset[i]) { |
||
| 985 | current = current.childNodes[current.childNodes.length - 1]; |
||
| 986 | } else { |
||
| 987 | current = current.childNodes[aOffset[i]]; |
||
| 988 | } |
||
| 989 | } |
||
| 990 | return current; |
||
| 991 | }; |
||
| 992 | |||
| 993 | /** |
||
| 994 | * split element or #text |
||
| 995 | * |
||
| 996 | * @param {BoundaryPoint} point |
||
| 997 | * @param {Boolean} [isSkipPaddingBlankHTML] |
||
| 998 | * @return {Node} right node of boundaryPoint |
||
| 999 | */ |
||
| 1000 | var splitNode = function (point, isSkipPaddingBlankHTML) { |
||
| 1001 | // split #text |
||
| 1002 | if (isText(point.node)) { |
||
| 1003 | // edge case |
||
| 1004 | if (isLeftEdgePoint(point)) { |
||
| 1005 | return point.node; |
||
| 1006 | } else if (isRightEdgePoint(point)) { |
||
| 1007 | return point.node.nextSibling; |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | return point.node.splitText(point.offset); |
||
| 1011 | } |
||
| 1012 | |||
| 1013 | // split element |
||
| 1014 | var childNode = point.node.childNodes[point.offset]; |
||
| 1015 | var clone = insertAfter(point.node.cloneNode(false), point.node); |
||
| 1016 | appendChildNodes(clone, listNext(childNode)); |
||
| 1017 | |||
| 1018 | if (!isSkipPaddingBlankHTML) { |
||
| 1019 | paddingBlankHTML(point.node); |
||
| 1020 | paddingBlankHTML(clone); |
||
| 1021 | } |
||
| 1022 | |||
| 1023 | return clone; |
||
| 1024 | }; |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * split tree by point |
||
| 1028 | * |
||
| 1029 | * @param {Node} root - split root |
||
| 1030 | * @param {BoundaryPoint} point |
||
| 1031 | * @param {Boolean} [isSkipPaddingBlankHTML] |
||
| 1032 | * @return {Node} right node of boundaryPoint |
||
| 1033 | */ |
||
| 1034 | var splitTree = function (root, point, isSkipPaddingBlankHTML) { |
||
| 1035 | // ex) [#text, <span>, <p>] |
||
| 1036 | var ancestors = listAncestor(point.node, func.eq(root)); |
||
| 1037 | |||
| 1038 | if (!ancestors.length) { |
||
| 1039 | return null; |
||
| 1040 | } else if (ancestors.length === 1) { |
||
| 1041 | return splitNode(point, isSkipPaddingBlankHTML); |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | return ancestors.reduce(function (node, parent) { |
||
| 1045 | var clone = insertAfter(parent.cloneNode(false), parent); |
||
| 1046 | |||
| 1047 | if (node === point.node) { |
||
| 1048 | node = splitNode(point, isSkipPaddingBlankHTML); |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | appendChildNodes(clone, listNext(node)); |
||
| 1052 | |||
| 1053 | if (!isSkipPaddingBlankHTML) { |
||
| 1054 | paddingBlankHTML(parent); |
||
| 1055 | paddingBlankHTML(clone); |
||
| 1056 | } |
||
| 1057 | return clone; |
||
| 1058 | }); |
||
| 1059 | }; |
||
| 1060 | |||
| 1061 | var create = function (nodeName) { |
||
| 1062 | return document.createElement(nodeName); |
||
| 1063 | }; |
||
| 1064 | |||
| 1065 | var createText = function (text) { |
||
| 1066 | return document.createTextNode(text); |
||
| 1067 | }; |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * remove node, (isRemoveChild: remove child or not) |
||
| 1071 | * @param {Node} node |
||
| 1072 | * @param {Boolean} isRemoveChild |
||
| 1073 | */ |
||
| 1074 | var remove = function (node, isRemoveChild) { |
||
| 1075 | if (!node || !node.parentNode) { return; } |
||
| 1076 | if (node.removeNode) { return node.removeNode(isRemoveChild); } |
||
| 1077 | |||
| 1078 | var parent = node.parentNode; |
||
| 1079 | if (!isRemoveChild) { |
||
| 1080 | var nodes = []; |
||
| 1081 | var i, len; |
||
| 1082 | for (i = 0, len = node.childNodes.length; i < len; i++) { |
||
| 1083 | nodes.push(node.childNodes[i]); |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | for (i = 0, len = nodes.length; i < len; i++) { |
||
| 1087 | parent.insertBefore(nodes[i], node); |
||
| 1088 | } |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | parent.removeChild(node); |
||
| 1092 | }; |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * @param {Node} node |
||
| 1096 | * @param {Function} pred |
||
| 1097 | */ |
||
| 1098 | var removeWhile = function (node, pred) { |
||
| 1099 | while (node) { |
||
| 1100 | if (isEditable(node) || !pred(node)) { |
||
| 1101 | break; |
||
| 1102 | } |
||
| 1103 | |||
| 1104 | var parent = node.parentNode; |
||
| 1105 | remove(node); |
||
| 1106 | node = parent; |
||
| 1107 | } |
||
| 1108 | }; |
||
| 1109 | |||
| 1110 | /** |
||
| 1111 | * replace node with provided nodeName |
||
| 1112 | * |
||
| 1113 | * @param {Node} node |
||
| 1114 | * @param {String} nodeName |
||
| 1115 | * @return {Node} - new node |
||
| 1116 | */ |
||
| 1117 | var replace = function (node, nodeName) { |
||
| 1118 | if (node.nodeName.toUpperCase() === nodeName.toUpperCase()) { |
||
| 1119 | return node; |
||
| 1120 | } |
||
| 1121 | |||
| 1122 | var newNode = create(nodeName); |
||
| 1123 | |||
| 1124 | if (node.style.cssText) { |
||
| 1125 | newNode.style.cssText = node.style.cssText; |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | appendChildNodes(newNode, list.from(node.childNodes)); |
||
| 1129 | insertAfter(newNode, node); |
||
| 1130 | remove(node); |
||
| 1131 | |||
| 1132 | return newNode; |
||
| 1133 | }; |
||
| 1134 | |||
| 1135 | var isTextarea = makePredByNodeName('TEXTAREA'); |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * get the HTML contents of node |
||
| 1139 | * |
||
| 1140 | * @param {jQuery} $node |
||
| 1141 | * @param {Boolean} [isNewlineOnBlock] |
||
| 1142 | */ |
||
| 1143 | var html = function ($node, isNewlineOnBlock) { |
||
| 1144 | var markup = isTextarea($node[0]) ? $node.val() : $node.html(); |
||
| 1145 | |||
| 1146 | if (isNewlineOnBlock) { |
||
| 1147 | var regexTag = /<(\/?)(\b(?!!)[^>\s]*)(.*?)(\s*\/?>)/g; |
||
| 1148 | markup = markup.replace(regexTag, function (match, endSlash, name) { |
||
| 1149 | name = name.toUpperCase(); |
||
| 1150 | var isEndOfInlineContainer = /^DIV|^TD|^TH|^P|^LI|^H[1-7]/.test(name) && |
||
| 1151 | !!endSlash; |
||
| 1152 | var isBlockNode = /^BLOCKQUOTE|^TABLE|^TBODY|^TR|^HR|^UL|^OL/.test(name); |
||
| 1153 | |||
| 1154 | return match + ((isEndOfInlineContainer || isBlockNode) ? '\n' : ''); |
||
| 1155 | }); |
||
| 1156 | markup = $.trim(markup); |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | return markup; |
||
| 1160 | }; |
||
| 1161 | |||
| 1162 | var value = function ($textarea) { |
||
| 1163 | var val = $textarea.val(); |
||
| 1164 | // strip line breaks |
||
| 1165 | return val.replace(/[\n\r]/g, ''); |
||
| 1166 | }; |
||
| 1167 | |||
| 1168 | return { |
||
| 1169 | NBSP_CHAR: NBSP_CHAR, |
||
| 1170 | ZERO_WIDTH_NBSP_CHAR: ZERO_WIDTH_NBSP_CHAR, |
||
| 1171 | blank: blankHTML, |
||
| 1172 | emptyPara: '<p>' + blankHTML + '</p>', |
||
| 1173 | isEditable: isEditable, |
||
| 1174 | isControlSizing: isControlSizing, |
||
| 1175 | buildLayoutInfo: buildLayoutInfo, |
||
| 1176 | isText: isText, |
||
| 1177 | isPara: isPara, |
||
| 1178 | isPurePara: isPurePara, |
||
| 1179 | isInline: isInline, |
||
| 1180 | isBodyInline: isBodyInline, |
||
| 1181 | isBody: isBody, |
||
| 1182 | isParaInline: isParaInline, |
||
| 1183 | isList: isList, |
||
| 1184 | isTable: makePredByNodeName('TABLE'), |
||
| 1185 | isCell: isCell, |
||
| 1186 | isBlockquote: isBlockquote, |
||
| 1187 | isBodyContainer: isBodyContainer, |
||
| 1188 | isAnchor: isAnchor, |
||
| 1189 | isDiv: makePredByNodeName('DIV'), |
||
| 1190 | isLi: isLi, |
||
| 1191 | isSpan: makePredByNodeName('SPAN'), |
||
| 1192 | isB: makePredByNodeName('B'), |
||
| 1193 | isU: makePredByNodeName('U'), |
||
| 1194 | isS: makePredByNodeName('S'), |
||
| 1195 | isI: makePredByNodeName('I'), |
||
| 1196 | isImg: makePredByNodeName('IMG'), |
||
| 1197 | isTextarea: isTextarea, |
||
| 1198 | isEmpty: isEmpty, |
||
| 1199 | isEmptyAnchor: func.and(isAnchor, isEmpty), |
||
| 1200 | nodeLength: nodeLength, |
||
| 1201 | isLeftEdgePoint: isLeftEdgePoint, |
||
| 1202 | isRightEdgePoint: isRightEdgePoint, |
||
| 1203 | isEdgePoint: isEdgePoint, |
||
| 1204 | isLeftEdgeOf: isLeftEdgeOf, |
||
| 1205 | isRightEdgeOf: isRightEdgeOf, |
||
| 1206 | prevPoint: prevPoint, |
||
| 1207 | nextPoint: nextPoint, |
||
| 1208 | isSamePoint: isSamePoint, |
||
| 1209 | isVisiblePoint: isVisiblePoint, |
||
| 1210 | prevPointUntil: prevPointUntil, |
||
| 1211 | nextPointUntil: nextPointUntil, |
||
| 1212 | walkPoint: walkPoint, |
||
| 1213 | ancestor: ancestor, |
||
| 1214 | listAncestor: listAncestor, |
||
| 1215 | lastAncestor: lastAncestor, |
||
| 1216 | listNext: listNext, |
||
| 1217 | listPrev: listPrev, |
||
| 1218 | listDescendant: listDescendant, |
||
| 1219 | commonAncestor: commonAncestor, |
||
| 1220 | wrap: wrap, |
||
| 1221 | insertAfter: insertAfter, |
||
| 1222 | appendChildNodes: appendChildNodes, |
||
| 1223 | position: position, |
||
| 1224 | hasChildren: hasChildren, |
||
| 1225 | makeOffsetPath: makeOffsetPath, |
||
| 1226 | fromOffsetPath: fromOffsetPath, |
||
| 1227 | splitTree: splitTree, |
||
| 1228 | create: create, |
||
| 1229 | createText: createText, |
||
| 1230 | remove: remove, |
||
| 1231 | removeWhile: removeWhile, |
||
| 1232 | replace: replace, |
||
| 1233 | html: html, |
||
| 1234 | value: value |
||
| 1235 | }; |
||
| 1236 | })(); |
||
| 1237 | |||
| 1238 | var settings = { |
||
| 1239 | // version |
||
| 1240 | version: '0.5.10', |
||
| 1241 | |||
| 1242 | /** |
||
| 1243 | * options |
||
| 1244 | */ |
||
| 1245 | options: { |
||
| 1246 | width: null, // set editor width |
||
| 1247 | height: null, // set editor height, ex) 300 |
||
| 1248 | |||
| 1249 | minHeight: null, // set minimum height of editor |
||
| 1250 | maxHeight: null, // set maximum height of editor |
||
| 1251 | |||
| 1252 | focus: false, // set focus to editable area after initializing summernote |
||
| 1253 | |||
| 1254 | tabsize: 4, // size of tab ex) 2 or 4 |
||
| 1255 | styleWithSpan: true, // style with span (Chrome and FF only) |
||
| 1256 | |||
| 1257 | disableLinkTarget: false, // hide link Target Checkbox |
||
| 1258 | disableDragAndDrop: false, // disable drag and drop event |
||
| 1259 | disableResizeEditor: false, // disable resizing editor |
||
| 1260 | |||
| 1261 | codemirror: { // codemirror options |
||
| 1262 | mode: 'text/html', |
||
| 1263 | htmlMode: true, |
||
| 1264 | lineNumbers: true |
||
| 1265 | }, |
||
| 1266 | |||
| 1267 | // language |
||
| 1268 | lang: 'en-US', // language 'en-US', 'ko-KR', ... |
||
| 1269 | direction: null, // text direction, ex) 'rtl' |
||
| 1270 | |||
| 1271 | // toolbar |
||
| 1272 | toolbar: [ |
||
| 1273 | ['style', ['style']], |
||
| 1274 | ['font', ['bold', 'italic', 'underline', 'superscript', 'subscript', 'strikethrough', 'clear']], |
||
| 1275 | ['fontname', ['fontname']], |
||
| 1276 | // ['fontsize', ['fontsize']], // Still buggy |
||
| 1277 | ['color', ['color']], |
||
| 1278 | ['para', ['ul', 'ol', 'paragraph']], |
||
| 1279 | ['height', ['height']], |
||
| 1280 | ['table', ['table']], |
||
| 1281 | ['insert', ['link', 'picture', 'video', 'hr']], |
||
| 1282 | ['view', ['fullscreen', 'codeview']], |
||
| 1283 | ['help', ['help']] |
||
| 1284 | ], |
||
| 1285 | |||
| 1286 | // air mode: inline editor |
||
| 1287 | airMode: false, |
||
| 1288 | // airPopover: [ |
||
| 1289 | // ['style', ['style']], |
||
| 1290 | // ['font', ['bold', 'italic', 'underline', 'clear']], |
||
| 1291 | // ['fontname', ['fontname']], |
||
| 1292 | // ['fontsize', ['fontsize']], // Still buggy |
||
| 1293 | // ['color', ['color']], |
||
| 1294 | // ['para', ['ul', 'ol', 'paragraph']], |
||
| 1295 | // ['height', ['height']], |
||
| 1296 | // ['table', ['table']], |
||
| 1297 | // ['insert', ['link', 'picture', 'video']], |
||
| 1298 | // ['help', ['help']] |
||
| 1299 | // ], |
||
| 1300 | airPopover: [ |
||
| 1301 | ['color', ['color']], |
||
| 1302 | ['font', ['bold', 'underline', 'clear']], |
||
| 1303 | ['para', ['ul', 'paragraph']], |
||
| 1304 | ['table', ['table']], |
||
| 1305 | ['insert', ['link', 'picture']] |
||
| 1306 | ], |
||
| 1307 | |||
| 1308 | // style tag |
||
| 1309 | styleTags: ['p', 'blockquote', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'], |
||
| 1310 | |||
| 1311 | // default fontName |
||
| 1312 | defaultFontName: 'Helvetica Neue', |
||
| 1313 | |||
| 1314 | // fontName |
||
| 1315 | fontNames: [ |
||
| 1316 | 'Arial', 'Arial Black', 'Comic Sans MS', 'Courier New', |
||
| 1317 | 'Helvetica Neue', 'Impact', 'Lucida Grande', |
||
| 1318 | 'Tahoma', 'Times New Roman', 'Verdana' |
||
| 1319 | ], |
||
| 1320 | |||
| 1321 | // pallete colors(n x n) |
||
| 1322 | colors: [ |
||
| 1323 | ['#000000', '#424242', '#636363', '#9C9C94', '#CEC6CE', '#EFEFEF', '#F7F7F7', '#FFFFFF'], |
||
| 1324 | ['#FF0000', '#FF9C00', '#FFFF00', '#00FF00', '#00FFFF', '#0000FF', '#9C00FF', '#FF00FF'], |
||
| 1325 | ['#F7C6CE', '#FFE7CE', '#FFEFC6', '#D6EFD6', '#CEDEE7', '#CEE7F7', '#D6D6E7', '#E7D6DE'], |
||
| 1326 | ['#E79C9C', '#FFC69C', '#FFE79C', '#B5D6A5', '#A5C6CE', '#9CC6EF', '#B5A5D6', '#D6A5BD'], |
||
| 1327 | ['#E76363', '#F7AD6B', '#FFD663', '#94BD7B', '#73A5AD', '#6BADDE', '#8C7BC6', '#C67BA5'], |
||
| 1328 | ['#CE0000', '#E79439', '#EFC631', '#6BA54A', '#4A7B8C', '#3984C6', '#634AA5', '#A54A7B'], |
||
| 1329 | ['#9C0000', '#B56308', '#BD9400', '#397B21', '#104A5A', '#085294', '#311873', '#731842'], |
||
| 1330 | ['#630000', '#7B3900', '#846300', '#295218', '#083139', '#003163', '#21104A', '#4A1031'] |
||
| 1331 | ], |
||
| 1332 | |||
| 1333 | // fontSize |
||
| 1334 | fontSizes: ['8', '9', '10', '11', '12', '14', '18', '24', '36'], |
||
| 1335 | |||
| 1336 | // lineHeight |
||
| 1337 | lineHeights: ['1.0', '1.2', '1.4', '1.5', '1.6', '1.8', '2.0', '3.0'], |
||
| 1338 | |||
| 1339 | // insertTable max size |
||
| 1340 | insertTableMaxSize: { |
||
| 1341 | col: 10, |
||
| 1342 | row: 10 |
||
| 1343 | }, |
||
| 1344 | |||
| 1345 | // callbacks |
||
| 1346 | oninit: null, // initialize |
||
| 1347 | onfocus: null, // editable has focus |
||
| 1348 | onblur: null, // editable out of focus |
||
| 1349 | onenter: null, // enter key pressed |
||
| 1350 | onkeyup: null, // keyup |
||
| 1351 | onkeydown: null, // keydown |
||
| 1352 | onImageUpload: null, // imageUpload |
||
| 1353 | onImageUploadError: null, // imageUploadError |
||
| 1354 | onToolbarClick: null, |
||
| 1355 | |||
| 1356 | /** |
||
| 1357 | * manipulate link address when user create link |
||
| 1358 | * @param {String} sLinkUrl |
||
| 1359 | * @return {String} |
||
| 1360 | */ |
||
| 1361 | onCreateLink: function (sLinkUrl) { |
||
| 1362 | if (sLinkUrl.indexOf('@') !== -1 && sLinkUrl.indexOf(':') === -1) { |
||
| 1363 | sLinkUrl = 'mailto:' + sLinkUrl; |
||
| 1364 | } else if (sLinkUrl.indexOf('://') === -1) { |
||
| 1365 | sLinkUrl = 'http://' + sLinkUrl; |
||
| 1366 | } |
||
| 1367 | |||
| 1368 | return sLinkUrl; |
||
| 1369 | }, |
||
| 1370 | |||
| 1371 | keyMap: { |
||
| 1372 | pc: { |
||
| 1373 | 'ENTER': 'insertParagraph', |
||
| 1374 | 'CTRL+Z': 'undo', |
||
| 1375 | 'CTRL+Y': 'redo', |
||
| 1376 | 'TAB': 'tab', |
||
| 1377 | 'SHIFT+TAB': 'untab', |
||
| 1378 | 'CTRL+B': 'bold', |
||
| 1379 | 'CTRL+I': 'italic', |
||
| 1380 | 'CTRL+U': 'underline', |
||
| 1381 | 'CTRL+SHIFT+S': 'strikethrough', |
||
| 1382 | 'CTRL+BACKSLASH': 'removeFormat', |
||
| 1383 | 'CTRL+SHIFT+L': 'justifyLeft', |
||
| 1384 | 'CTRL+SHIFT+E': 'justifyCenter', |
||
| 1385 | 'CTRL+SHIFT+R': 'justifyRight', |
||
| 1386 | 'CTRL+SHIFT+J': 'justifyFull', |
||
| 1387 | 'CTRL+SHIFT+NUM7': 'insertUnorderedList', |
||
| 1388 | 'CTRL+SHIFT+NUM8': 'insertOrderedList', |
||
| 1389 | 'CTRL+LEFTBRACKET': 'outdent', |
||
| 1390 | 'CTRL+RIGHTBRACKET': 'indent', |
||
| 1391 | 'CTRL+NUM0': 'formatPara', |
||
| 1392 | 'CTRL+NUM1': 'formatH1', |
||
| 1393 | 'CTRL+NUM2': 'formatH2', |
||
| 1394 | 'CTRL+NUM3': 'formatH3', |
||
| 1395 | 'CTRL+NUM4': 'formatH4', |
||
| 1396 | 'CTRL+NUM5': 'formatH5', |
||
| 1397 | 'CTRL+NUM6': 'formatH6', |
||
| 1398 | 'CTRL+ENTER': 'insertHorizontalRule', |
||
| 1399 | 'CTRL+K': 'showLinkDialog' |
||
| 1400 | }, |
||
| 1401 | |||
| 1402 | mac: { |
||
| 1403 | 'ENTER': 'insertParagraph', |
||
| 1404 | 'CMD+Z': 'undo', |
||
| 1405 | 'CMD+SHIFT+Z': 'redo', |
||
| 1406 | 'TAB': 'tab', |
||
| 1407 | 'SHIFT+TAB': 'untab', |
||
| 1408 | 'CMD+B': 'bold', |
||
| 1409 | 'CMD+I': 'italic', |
||
| 1410 | 'CMD+U': 'underline', |
||
| 1411 | 'CMD+SHIFT+S': 'strikethrough', |
||
| 1412 | 'CMD+BACKSLASH': 'removeFormat', |
||
| 1413 | 'CMD+SHIFT+L': 'justifyLeft', |
||
| 1414 | 'CMD+SHIFT+E': 'justifyCenter', |
||
| 1415 | 'CMD+SHIFT+R': 'justifyRight', |
||
| 1416 | 'CMD+SHIFT+J': 'justifyFull', |
||
| 1417 | 'CMD+SHIFT+NUM7': 'insertUnorderedList', |
||
| 1418 | 'CMD+SHIFT+NUM8': 'insertOrderedList', |
||
| 1419 | 'CMD+LEFTBRACKET': 'outdent', |
||
| 1420 | 'CMD+RIGHTBRACKET': 'indent', |
||
| 1421 | 'CMD+NUM0': 'formatPara', |
||
| 1422 | 'CMD+NUM1': 'formatH1', |
||
| 1423 | 'CMD+NUM2': 'formatH2', |
||
| 1424 | 'CMD+NUM3': 'formatH3', |
||
| 1425 | 'CMD+NUM4': 'formatH4', |
||
| 1426 | 'CMD+NUM5': 'formatH5', |
||
| 1427 | 'CMD+NUM6': 'formatH6', |
||
| 1428 | 'CMD+ENTER': 'insertHorizontalRule', |
||
| 1429 | 'CMD+K': 'showLinkDialog' |
||
| 1430 | } |
||
| 1431 | } |
||
| 1432 | }, |
||
| 1433 | |||
| 1434 | // default language: en-US |
||
| 1435 | lang: { |
||
| 1436 | 'en-US': { |
||
| 1437 | font: { |
||
| 1438 | bold: 'Bold', |
||
| 1439 | italic: 'Italic', |
||
| 1440 | underline: 'Underline', |
||
| 1441 | strikethrough: 'Strikethrough', |
||
| 1442 | subscript: 'Subscript', |
||
| 1443 | superscript: 'Superscript', |
||
| 1444 | clear: 'Remove Font Style', |
||
| 1445 | height: 'Line Height', |
||
| 1446 | name: 'Font Family', |
||
| 1447 | size: 'Font Size' |
||
| 1448 | }, |
||
| 1449 | image: { |
||
| 1450 | image: 'Picture', |
||
| 1451 | insert: 'Insert Image', |
||
| 1452 | resizeFull: 'Resize Full', |
||
| 1453 | resizeHalf: 'Resize Half', |
||
| 1454 | resizeQuarter: 'Resize Quarter', |
||
| 1455 | floatLeft: 'Float Left', |
||
| 1456 | floatRight: 'Float Right', |
||
| 1457 | floatNone: 'Float None', |
||
| 1458 | shapeRounded: 'Shape: Rounded', |
||
| 1459 | shapeCircle: 'Shape: Circle', |
||
| 1460 | shapeThumbnail: 'Shape: Thumbnail', |
||
| 1461 | shapeNone: 'Shape: None', |
||
| 1462 | dragImageHere: 'Drag an image here', |
||
| 1463 | selectFromFiles: 'Select from files', |
||
| 1464 | url: 'Image URL', |
||
| 1465 | remove: 'Remove Image' |
||
| 1466 | }, |
||
| 1467 | link: { |
||
| 1468 | link: 'Link', |
||
| 1469 | insert: 'Insert Link', |
||
| 1470 | unlink: 'Unlink', |
||
| 1471 | edit: 'Edit', |
||
| 1472 | textToDisplay: 'Text to display', |
||
| 1473 | url: 'To what URL should this link go?', |
||
| 1474 | openInNewWindow: 'Open in new window' |
||
| 1475 | }, |
||
| 1476 | video: { |
||
| 1477 | video: 'Video', |
||
| 1478 | videoLink: 'Video Link', |
||
| 1479 | insert: 'Insert Video', |
||
| 1480 | url: 'Video URL?', |
||
| 1481 | providers: '(YouTube, Vimeo, Vine, Instagram, DailyMotion or Youku)' |
||
| 1482 | }, |
||
| 1483 | table: { |
||
| 1484 | table: 'Table' |
||
| 1485 | }, |
||
| 1486 | hr: { |
||
| 1487 | insert: 'Insert Horizontal Rule' |
||
| 1488 | }, |
||
| 1489 | style: { |
||
| 1490 | style: 'Style', |
||
| 1491 | normal: 'Normal', |
||
| 1492 | blockquote: 'Quote', |
||
| 1493 | pre: 'Code', |
||
| 1494 | h1: 'Header 1', |
||
| 1495 | h2: 'Header 2', |
||
| 1496 | h3: 'Header 3', |
||
| 1497 | h4: 'Header 4', |
||
| 1498 | h5: 'Header 5', |
||
| 1499 | h6: 'Header 6' |
||
| 1500 | }, |
||
| 1501 | lists: { |
||
| 1502 | unordered: 'Unordered list', |
||
| 1503 | ordered: 'Ordered list' |
||
| 1504 | }, |
||
| 1505 | options: { |
||
| 1506 | help: 'Help', |
||
| 1507 | fullscreen: 'Full Screen', |
||
| 1508 | codeview: 'Code View' |
||
| 1509 | }, |
||
| 1510 | paragraph: { |
||
| 1511 | paragraph: 'Paragraph', |
||
| 1512 | outdent: 'Outdent', |
||
| 1513 | indent: 'Indent', |
||
| 1514 | left: 'Align left', |
||
| 1515 | center: 'Align center', |
||
| 1516 | right: 'Align right', |
||
| 1517 | justify: 'Justify full' |
||
| 1518 | }, |
||
| 1519 | color: { |
||
| 1520 | recent: 'Recent Color', |
||
| 1521 | more: 'More Color', |
||
| 1522 | background: 'Background Color', |
||
| 1523 | foreground: 'Foreground Color', |
||
| 1524 | transparent: 'Transparent', |
||
| 1525 | setTransparent: 'Set transparent', |
||
| 1526 | reset: 'Reset', |
||
| 1527 | resetToDefault: 'Reset to default' |
||
| 1528 | }, |
||
| 1529 | shortcut: { |
||
| 1530 | shortcuts: 'Keyboard shortcuts', |
||
| 1531 | close: 'Close', |
||
| 1532 | textFormatting: 'Text formatting', |
||
| 1533 | action: 'Action', |
||
| 1534 | paragraphFormatting: 'Paragraph formatting', |
||
| 1535 | documentStyle: 'Document Style' |
||
| 1536 | }, |
||
| 1537 | history: { |
||
| 1538 | undo: 'Undo', |
||
| 1539 | redo: 'Redo' |
||
| 1540 | } |
||
| 1541 | } |
||
| 1542 | } |
||
| 1543 | }; |
||
| 1544 | |||
| 1545 | /** |
||
| 1546 | * Async functions which returns `Promise` |
||
| 1547 | */ |
||
| 1548 | var async = (function () { |
||
| 1549 | /** |
||
| 1550 | * read contents of file as representing URL |
||
| 1551 | * |
||
| 1552 | * @param {File} file |
||
| 1553 | * @return {Promise} - then: sDataUrl |
||
| 1554 | */ |
||
| 1555 | var readFileAsDataURL = function (file) { |
||
| 1556 | return $.Deferred(function (deferred) { |
||
| 1557 | $.extend(new FileReader(), { |
||
| 1558 | onload: function (e) { |
||
| 1559 | var sDataURL = e.target.result; |
||
| 1560 | deferred.resolve(sDataURL); |
||
| 1561 | }, |
||
| 1562 | onerror: function () { |
||
| 1563 | deferred.reject(this); |
||
| 1564 | } |
||
| 1565 | }).readAsDataURL(file); |
||
| 1566 | }).promise(); |
||
| 1567 | }; |
||
| 1568 | |||
| 1569 | /** |
||
| 1570 | * create `<image>` from url string |
||
| 1571 | * |
||
| 1572 | * @param {String} sUrl |
||
| 1573 | * @return {Promise} - then: $image |
||
| 1574 | */ |
||
| 1575 | var createImage = function (sUrl, filename) { |
||
| 1576 | return $.Deferred(function (deferred) { |
||
| 1577 | $('<img>').one('load', function () { |
||
| 1578 | deferred.resolve($(this)); |
||
| 1579 | }).one('error abort', function () { |
||
| 1580 | deferred.reject($(this)); |
||
| 1581 | }).css({ |
||
| 1582 | display: 'none' |
||
| 1583 | }).appendTo(document.body) |
||
| 1584 | .attr('src', sUrl) |
||
| 1585 | .attr('data-filename', filename); |
||
| 1586 | }).promise(); |
||
| 1587 | }; |
||
| 1588 | |||
| 1589 | return { |
||
| 1590 | readFileAsDataURL: readFileAsDataURL, |
||
| 1591 | createImage: createImage |
||
| 1592 | }; |
||
| 1593 | })(); |
||
| 1594 | |||
| 1595 | /** |
||
| 1596 | * Object for keycodes. |
||
| 1597 | */ |
||
| 1598 | var key = { |
||
| 1599 | isEdit: function (keyCode) { |
||
| 1600 | return list.contains([8, 9, 13, 32], keyCode); |
||
| 1601 | }, |
||
| 1602 | nameFromCode: { |
||
| 1603 | '8': 'BACKSPACE', |
||
| 1604 | '9': 'TAB', |
||
| 1605 | '13': 'ENTER', |
||
| 1606 | '32': 'SPACE', |
||
| 1607 | |||
| 1608 | // Number: 0-9 |
||
| 1609 | '48': 'NUM0', |
||
| 1610 | '49': 'NUM1', |
||
| 1611 | '50': 'NUM2', |
||
| 1612 | '51': 'NUM3', |
||
| 1613 | '52': 'NUM4', |
||
| 1614 | '53': 'NUM5', |
||
| 1615 | '54': 'NUM6', |
||
| 1616 | '55': 'NUM7', |
||
| 1617 | '56': 'NUM8', |
||
| 1618 | |||
| 1619 | // Alphabet: a-z |
||
| 1620 | '66': 'B', |
||
| 1621 | '69': 'E', |
||
| 1622 | '73': 'I', |
||
| 1623 | '74': 'J', |
||
| 1624 | '75': 'K', |
||
| 1625 | '76': 'L', |
||
| 1626 | '82': 'R', |
||
| 1627 | '83': 'S', |
||
| 1628 | '85': 'U', |
||
| 1629 | '89': 'Y', |
||
| 1630 | '90': 'Z', |
||
| 1631 | |||
| 1632 | '191': 'SLASH', |
||
| 1633 | '219': 'LEFTBRACKET', |
||
| 1634 | '220': 'BACKSLASH', |
||
| 1635 | '221': 'RIGHTBRACKET' |
||
| 1636 | } |
||
| 1637 | }; |
||
| 1638 | |||
| 1639 | /** |
||
| 1640 | * Style |
||
| 1641 | * @class |
||
| 1642 | */ |
||
| 1643 | var Style = function () { |
||
| 1644 | /** |
||
| 1645 | * passing an array of style properties to .css() |
||
| 1646 | * will result in an object of property-value pairs. |
||
| 1647 | * (compability with version < 1.9) |
||
| 1648 | * |
||
| 1649 | * @param {jQuery} $obj |
||
| 1650 | * @param {Array} propertyNames - An array of one or more CSS properties. |
||
| 1651 | * @returns {Object} |
||
| 1652 | */ |
||
| 1653 | var jQueryCSS = function ($obj, propertyNames) { |
||
| 1654 | if (agent.jqueryVersion < 1.9) { |
||
| 1655 | var result = {}; |
||
| 1656 | $.each(propertyNames, function (idx, propertyName) { |
||
| 1657 | result[propertyName] = $obj.css(propertyName); |
||
| 1658 | }); |
||
| 1659 | return result; |
||
| 1660 | } |
||
| 1661 | return $obj.css.call($obj, propertyNames); |
||
| 1662 | }; |
||
| 1663 | |||
| 1664 | /** |
||
| 1665 | * paragraph level style |
||
| 1666 | * |
||
| 1667 | * @param {WrappedRange} rng |
||
| 1668 | * @param {Object} styleInfo |
||
| 1669 | */ |
||
| 1670 | this.stylePara = function (rng, styleInfo) { |
||
| 1671 | $.each(rng.nodes(dom.isPara, { |
||
| 1672 | includeAncestor: true |
||
| 1673 | }), function (idx, para) { |
||
| 1674 | $(para).css(styleInfo); |
||
| 1675 | }); |
||
| 1676 | }; |
||
| 1677 | |||
| 1678 | /** |
||
| 1679 | * get current style on cursor |
||
| 1680 | * |
||
| 1681 | * @param {WrappedRange} rng |
||
| 1682 | * @param {Node} target - target element on event |
||
| 1683 | * @return {Object} - object contains style properties. |
||
| 1684 | */ |
||
| 1685 | this.current = function (rng, target) { |
||
| 1686 | var $cont = $(dom.isText(rng.sc) ? rng.sc.parentNode : rng.sc); |
||
| 1687 | var properties = ['font-family', 'font-size', 'text-align', 'list-style-type', 'line-height']; |
||
| 1688 | var styleInfo = jQueryCSS($cont, properties) || {}; |
||
| 1689 | |||
| 1690 | styleInfo['font-size'] = parseInt(styleInfo['font-size'], 10); |
||
| 1691 | |||
| 1692 | // document.queryCommandState for toggle state |
||
| 1693 | styleInfo['font-bold'] = document.queryCommandState('bold') ? 'bold' : 'normal'; |
||
| 1694 | styleInfo['font-italic'] = document.queryCommandState('italic') ? 'italic' : 'normal'; |
||
| 1695 | styleInfo['font-underline'] = document.queryCommandState('underline') ? 'underline' : 'normal'; |
||
| 1696 | styleInfo['font-strikethrough'] = document.queryCommandState('strikeThrough') ? 'strikethrough' : 'normal'; |
||
| 1697 | styleInfo['font-superscript'] = document.queryCommandState('superscript') ? 'superscript' : 'normal'; |
||
| 1698 | styleInfo['font-subscript'] = document.queryCommandState('subscript') ? 'subscript' : 'normal'; |
||
| 1699 | |||
| 1700 | // list-style-type to list-style(unordered, ordered) |
||
| 1701 | if (!rng.isOnList()) { |
||
| 1702 | styleInfo['list-style'] = 'none'; |
||
| 1703 | } else { |
||
| 1704 | var aOrderedType = ['circle', 'disc', 'disc-leading-zero', 'square']; |
||
| 1705 | var isUnordered = $.inArray(styleInfo['list-style-type'], aOrderedType) > -1; |
||
| 1706 | styleInfo['list-style'] = isUnordered ? 'unordered' : 'ordered'; |
||
| 1707 | } |
||
| 1708 | |||
| 1709 | var para = dom.ancestor(rng.sc, dom.isPara); |
||
| 1710 | if (para && para.style['line-height']) { |
||
| 1711 | styleInfo['line-height'] = para.style.lineHeight; |
||
| 1712 | } else { |
||
| 1713 | var lineHeight = parseInt(styleInfo['line-height'], 10) / parseInt(styleInfo['font-size'], 10); |
||
| 1714 | styleInfo['line-height'] = lineHeight.toFixed(1); |
||
| 1715 | } |
||
| 1716 | |||
| 1717 | styleInfo.image = dom.isImg(target) && target; |
||
| 1718 | styleInfo.anchor = rng.isOnAnchor() && dom.ancestor(rng.sc, dom.isAnchor); |
||
| 1719 | styleInfo.ancestors = dom.listAncestor(rng.sc, dom.isEditable); |
||
| 1720 | styleInfo.range = rng; |
||
| 1721 | |||
| 1722 | return styleInfo; |
||
| 1723 | }; |
||
| 1724 | }; |
||
| 1725 | |||
| 1726 | |||
| 1727 | /** |
||
| 1728 | * Data structure |
||
| 1729 | * - {BoundaryPoint}: a point of dom tree |
||
| 1730 | * - {BoundaryPoints}: two boundaryPoints corresponding to the start and the end of the Range |
||
| 1731 | * |
||
| 1732 | * @see http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html#Level-2-Range-Position |
||
| 1733 | */ |
||
| 1734 | var range = (function () { |
||
| 1735 | |||
| 1736 | /** |
||
| 1737 | * return boundaryPoint from TextRange, inspired by Andy Na's HuskyRange.js |
||
| 1738 | * |
||
| 1739 | * @param {TextRange} textRange |
||
| 1740 | * @param {Boolean} isStart |
||
| 1741 | * @return {BoundaryPoint} |
||
| 1742 | * |
||
| 1743 | * @see http://msdn.microsoft.com/en-us/library/ie/ms535872(v=vs.85).aspx |
||
| 1744 | */ |
||
| 1745 | var textRangeToPoint = function (textRange, isStart) { |
||
| 1746 | var container = textRange.parentElement(), offset; |
||
| 1747 | |||
| 1748 | var tester = document.body.createTextRange(), prevContainer; |
||
| 1749 | var childNodes = list.from(container.childNodes); |
||
| 1750 | for (offset = 0; offset < childNodes.length; offset++) { |
||
| 1751 | if (dom.isText(childNodes[offset])) { |
||
| 1752 | continue; |
||
| 1753 | } |
||
| 1754 | tester.moveToElementText(childNodes[offset]); |
||
| 1755 | if (tester.compareEndPoints('StartToStart', textRange) >= 0) { |
||
| 1756 | break; |
||
| 1757 | } |
||
| 1758 | prevContainer = childNodes[offset]; |
||
| 1759 | } |
||
| 1760 | |||
| 1761 | if (offset !== 0 && dom.isText(childNodes[offset - 1])) { |
||
| 1762 | var textRangeStart = document.body.createTextRange(), curTextNode = null; |
||
| 1763 | textRangeStart.moveToElementText(prevContainer || container); |
||
| 1764 | textRangeStart.collapse(!prevContainer); |
||
| 1765 | curTextNode = prevContainer ? prevContainer.nextSibling : container.firstChild; |
||
| 1766 | |||
| 1767 | var pointTester = textRange.duplicate(); |
||
| 1768 | pointTester.setEndPoint('StartToStart', textRangeStart); |
||
| 1769 | var textCount = pointTester.text.replace(/[\r\n]/g, '').length; |
||
| 1770 | |||
| 1771 | while (textCount > curTextNode.nodeValue.length && curTextNode.nextSibling) { |
||
| 1772 | textCount -= curTextNode.nodeValue.length; |
||
| 1773 | curTextNode = curTextNode.nextSibling; |
||
| 1774 | } |
||
| 1775 | |||
| 1776 | /* jshint ignore:start */ |
||
| 1777 | var dummy = curTextNode.nodeValue; // enforce IE to re-reference curTextNode, hack |
||
| 1778 | /* jshint ignore:end */ |
||
| 1779 | |||
| 1780 | if (isStart && curTextNode.nextSibling && dom.isText(curTextNode.nextSibling) && |
||
| 1781 | textCount === curTextNode.nodeValue.length) { |
||
| 1782 | textCount -= curTextNode.nodeValue.length; |
||
| 1783 | curTextNode = curTextNode.nextSibling; |
||
| 1784 | } |
||
| 1785 | |||
| 1786 | container = curTextNode; |
||
| 1787 | offset = textCount; |
||
| 1788 | } |
||
| 1789 | |||
| 1790 | return { |
||
| 1791 | cont: container, |
||
| 1792 | offset: offset |
||
| 1793 | }; |
||
| 1794 | }; |
||
| 1795 | |||
| 1796 | /** |
||
| 1797 | * return TextRange from boundary point (inspired by google closure-library) |
||
| 1798 | * @param {BoundaryPoint} point |
||
| 1799 | * @return {TextRange} |
||
| 1800 | */ |
||
| 1801 | var pointToTextRange = function (point) { |
||
| 1802 | var textRangeInfo = function (container, offset) { |
||
| 1803 | var node, isCollapseToStart; |
||
| 1804 | |||
| 1805 | if (dom.isText(container)) { |
||
| 1806 | var prevTextNodes = dom.listPrev(container, func.not(dom.isText)); |
||
| 1807 | var prevContainer = list.last(prevTextNodes).previousSibling; |
||
| 1808 | node = prevContainer || container.parentNode; |
||
| 1809 | offset += list.sum(list.tail(prevTextNodes), dom.nodeLength); |
||
| 1810 | isCollapseToStart = !prevContainer; |
||
| 1811 | } else { |
||
| 1812 | node = container.childNodes[offset] || container; |
||
| 1813 | if (dom.isText(node)) { |
||
| 1814 | return textRangeInfo(node, 0); |
||
| 1815 | } |
||
| 1816 | |||
| 1817 | offset = 0; |
||
| 1818 | isCollapseToStart = false; |
||
| 1819 | } |
||
| 1820 | |||
| 1821 | return { |
||
| 1822 | node: node, |
||
| 1823 | collapseToStart: isCollapseToStart, |
||
| 1824 | offset: offset |
||
| 1825 | }; |
||
| 1826 | }; |
||
| 1827 | |||
| 1828 | var textRange = document.body.createTextRange(); |
||
| 1829 | var info = textRangeInfo(point.node, point.offset); |
||
| 1830 | |||
| 1831 | textRange.moveToElementText(info.node); |
||
| 1832 | textRange.collapse(info.collapseToStart); |
||
| 1833 | textRange.moveStart('character', info.offset); |
||
| 1834 | return textRange; |
||
| 1835 | }; |
||
| 1836 | |||
| 1837 | /** |
||
| 1838 | * Wrapped Range |
||
| 1839 | * |
||
| 1840 | * @param {Node} sc - start container |
||
| 1841 | * @param {Number} so - start offset |
||
| 1842 | * @param {Node} ec - end container |
||
| 1843 | * @param {Number} eo - end offset |
||
| 1844 | */ |
||
| 1845 | var WrappedRange = function (sc, so, ec, eo) { |
||
| 1846 | this.sc = sc; |
||
| 1847 | this.so = so; |
||
| 1848 | this.ec = ec; |
||
| 1849 | this.eo = eo; |
||
| 1850 | |||
| 1851 | // nativeRange: get nativeRange from sc, so, ec, eo |
||
| 1852 | var nativeRange = function () { |
||
| 1853 | if (agent.isW3CRangeSupport) { |
||
| 1854 | var w3cRange = document.createRange(); |
||
| 1855 | w3cRange.setStart(sc, so); |
||
| 1856 | w3cRange.setEnd(ec, eo); |
||
| 1857 | |||
| 1858 | return w3cRange; |
||
| 1859 | } else { |
||
| 1860 | var textRange = pointToTextRange({ |
||
| 1861 | node: sc, |
||
| 1862 | offset: so |
||
| 1863 | }); |
||
| 1864 | |||
| 1865 | textRange.setEndPoint('EndToEnd', pointToTextRange({ |
||
| 1866 | node: ec, |
||
| 1867 | offset: eo |
||
| 1868 | })); |
||
| 1869 | |||
| 1870 | return textRange; |
||
| 1871 | } |
||
| 1872 | }; |
||
| 1873 | |||
| 1874 | this.getPoints = function () { |
||
| 1875 | return { |
||
| 1876 | sc: sc, |
||
| 1877 | so: so, |
||
| 1878 | ec: ec, |
||
| 1879 | eo: eo |
||
| 1880 | }; |
||
| 1881 | }; |
||
| 1882 | |||
| 1883 | this.getStartPoint = function () { |
||
| 1884 | return { |
||
| 1885 | node: sc, |
||
| 1886 | offset: so |
||
| 1887 | }; |
||
| 1888 | }; |
||
| 1889 | |||
| 1890 | this.getEndPoint = function () { |
||
| 1891 | return { |
||
| 1892 | node: ec, |
||
| 1893 | offset: eo |
||
| 1894 | }; |
||
| 1895 | }; |
||
| 1896 | |||
| 1897 | /** |
||
| 1898 | * select update visible range |
||
| 1899 | */ |
||
| 1900 | this.select = function () { |
||
| 1901 | var nativeRng = nativeRange(); |
||
| 1902 | if (agent.isW3CRangeSupport) { |
||
| 1903 | var selection = document.getSelection(); |
||
| 1904 | if (selection.rangeCount > 0) { |
||
| 1905 | selection.removeAllRanges(); |
||
| 1906 | } |
||
| 1907 | selection.addRange(nativeRng); |
||
| 1908 | } else { |
||
| 1909 | nativeRng.select(); |
||
| 1910 | } |
||
| 1911 | }; |
||
| 1912 | |||
| 1913 | /** |
||
| 1914 | * @return {WrappedRange} |
||
| 1915 | */ |
||
| 1916 | this.normalize = function () { |
||
| 1917 | var getVisiblePoint = function (point) { |
||
| 1918 | if (!dom.isVisiblePoint(point)) { |
||
| 1919 | if (dom.isLeftEdgePoint(point)) { |
||
| 1920 | point = dom.nextPointUntil(point, dom.isVisiblePoint); |
||
| 1921 | } else if (dom.isRightEdgePoint(point)) { |
||
| 1922 | point = dom.prevPointUntil(point, dom.isVisiblePoint); |
||
| 1923 | } |
||
| 1924 | } |
||
| 1925 | return point; |
||
| 1926 | }; |
||
| 1927 | |||
| 1928 | var startPoint = getVisiblePoint(this.getStartPoint()); |
||
| 1929 | var endPoint = getVisiblePoint(this.getStartPoint()); |
||
| 1930 | |||
| 1931 | return new WrappedRange( |
||
| 1932 | startPoint.node, |
||
| 1933 | startPoint.offset, |
||
| 1934 | endPoint.node, |
||
| 1935 | endPoint.offset |
||
| 1936 | ); |
||
| 1937 | }; |
||
| 1938 | |||
| 1939 | /** |
||
| 1940 | * returns matched nodes on range |
||
| 1941 | * |
||
| 1942 | * @param {Function} [pred] - predicate function |
||
| 1943 | * @param {Object} [options] |
||
| 1944 | * @param {Boolean} [options.includeAncestor] |
||
| 1945 | * @param {Boolean} [options.fullyContains] |
||
| 1946 | * @return {Node[]} |
||
| 1947 | */ |
||
| 1948 | this.nodes = function (pred, options) { |
||
| 1949 | pred = pred || func.ok; |
||
| 1950 | |||
| 1951 | var includeAncestor = options && options.includeAncestor; |
||
| 1952 | var fullyContains = options && options.fullyContains; |
||
| 1953 | |||
| 1954 | // TODO compare points and sort |
||
| 1955 | var startPoint = this.getStartPoint(); |
||
| 1956 | var endPoint = this.getEndPoint(); |
||
| 1957 | |||
| 1958 | var nodes = []; |
||
| 1959 | var leftEdgeNodes = []; |
||
| 1960 | |||
| 1961 | dom.walkPoint(startPoint, endPoint, function (point) { |
||
| 1962 | if (dom.isEditable(point.node)) { |
||
| 1963 | return; |
||
| 1964 | } |
||
| 1965 | |||
| 1966 | var node; |
||
| 1967 | if (fullyContains) { |
||
| 1968 | if (dom.isLeftEdgePoint(point)) { |
||
| 1969 | leftEdgeNodes.push(point.node); |
||
| 1970 | } |
||
| 1971 | if (dom.isRightEdgePoint(point) && list.contains(leftEdgeNodes, point.node)) { |
||
| 1972 | node = point.node; |
||
| 1973 | } |
||
| 1974 | } else if (includeAncestor) { |
||
| 1975 | node = dom.ancestor(point.node, pred); |
||
| 1976 | } else { |
||
| 1977 | node = point.node; |
||
| 1978 | } |
||
| 1979 | |||
| 1980 | if (node && pred(node)) { |
||
| 1981 | nodes.push(node); |
||
| 1982 | } |
||
| 1983 | }, true); |
||
| 1984 | |||
| 1985 | return list.unique(nodes); |
||
| 1986 | }; |
||
| 1987 | |||
| 1988 | /** |
||
| 1989 | * returns commonAncestor of range |
||
| 1990 | * @return {Element} - commonAncestor |
||
| 1991 | */ |
||
| 1992 | this.commonAncestor = function () { |
||
| 1993 | return dom.commonAncestor(sc, ec); |
||
| 1994 | }; |
||
| 1995 | |||
| 1996 | /** |
||
| 1997 | * returns expanded range by pred |
||
| 1998 | * |
||
| 1999 | * @param {Function} pred - predicate function |
||
| 2000 | * @return {WrappedRange} |
||
| 2001 | */ |
||
| 2002 | this.expand = function (pred) { |
||
| 2003 | var startAncestor = dom.ancestor(sc, pred); |
||
| 2004 | var endAncestor = dom.ancestor(ec, pred); |
||
| 2005 | |||
| 2006 | if (!startAncestor && !endAncestor) { |
||
| 2007 | return new WrappedRange(sc, so, ec, eo); |
||
| 2008 | } |
||
| 2009 | |||
| 2010 | var boundaryPoints = this.getPoints(); |
||
| 2011 | |||
| 2012 | if (startAncestor) { |
||
| 2013 | boundaryPoints.sc = startAncestor; |
||
| 2014 | boundaryPoints.so = 0; |
||
| 2015 | } |
||
| 2016 | |||
| 2017 | if (endAncestor) { |
||
| 2018 | boundaryPoints.ec = endAncestor; |
||
| 2019 | boundaryPoints.eo = dom.nodeLength(endAncestor); |
||
| 2020 | } |
||
| 2021 | |||
| 2022 | return new WrappedRange( |
||
| 2023 | boundaryPoints.sc, |
||
| 2024 | boundaryPoints.so, |
||
| 2025 | boundaryPoints.ec, |
||
| 2026 | boundaryPoints.eo |
||
| 2027 | ); |
||
| 2028 | }; |
||
| 2029 | |||
| 2030 | /** |
||
| 2031 | * @param {Boolean} isCollapseToStart |
||
| 2032 | * @return {WrappedRange} |
||
| 2033 | */ |
||
| 2034 | this.collapse = function (isCollapseToStart) { |
||
| 2035 | if (isCollapseToStart) { |
||
| 2036 | return new WrappedRange(sc, so, sc, so); |
||
| 2037 | } else { |
||
| 2038 | return new WrappedRange(ec, eo, ec, eo); |
||
| 2039 | } |
||
| 2040 | }; |
||
| 2041 | |||
| 2042 | /** |
||
| 2043 | * splitText on range |
||
| 2044 | */ |
||
| 2045 | this.splitText = function () { |
||
| 2046 | var isSameContainer = sc === ec; |
||
| 2047 | var boundaryPoints = this.getPoints(); |
||
| 2048 | |||
| 2049 | if (dom.isText(ec) && !dom.isEdgePoint(this.getEndPoint())) { |
||
| 2050 | ec.splitText(eo); |
||
| 2051 | } |
||
| 2052 | |||
| 2053 | if (dom.isText(sc) && !dom.isEdgePoint(this.getStartPoint())) { |
||
| 2054 | boundaryPoints.sc = sc.splitText(so); |
||
| 2055 | boundaryPoints.so = 0; |
||
| 2056 | |||
| 2057 | if (isSameContainer) { |
||
| 2058 | boundaryPoints.ec = boundaryPoints.sc; |
||
| 2059 | boundaryPoints.eo = eo - so; |
||
| 2060 | } |
||
| 2061 | } |
||
| 2062 | |||
| 2063 | return new WrappedRange( |
||
| 2064 | boundaryPoints.sc, |
||
| 2065 | boundaryPoints.so, |
||
| 2066 | boundaryPoints.ec, |
||
| 2067 | boundaryPoints.eo |
||
| 2068 | ); |
||
| 2069 | }; |
||
| 2070 | |||
| 2071 | /** |
||
| 2072 | * delete contents on range |
||
| 2073 | * @return {WrappedRange} |
||
| 2074 | */ |
||
| 2075 | this.deleteContents = function () { |
||
| 2076 | if (this.isCollapsed()) { |
||
| 2077 | return this; |
||
| 2078 | } |
||
| 2079 | |||
| 2080 | var rng = this.splitText(); |
||
| 2081 | var nodes = rng.nodes(null, { |
||
| 2082 | fullyContains: true |
||
| 2083 | }); |
||
| 2084 | |||
| 2085 | var point = dom.prevPointUntil(rng.getStartPoint(), function (point) { |
||
| 2086 | return !list.contains(nodes, point.node); |
||
| 2087 | }); |
||
| 2088 | |||
| 2089 | var emptyParents = []; |
||
| 2090 | $.each(nodes, function (idx, node) { |
||
| 2091 | // find empty parents |
||
| 2092 | var parent = node.parentNode; |
||
| 2093 | if (point.node !== parent && dom.nodeLength(parent) === 1) { |
||
| 2094 | emptyParents.push(parent); |
||
| 2095 | } |
||
| 2096 | dom.remove(node, false); |
||
| 2097 | }); |
||
| 2098 | |||
| 2099 | // remove empty parents |
||
| 2100 | $.each(emptyParents, function (idx, node) { |
||
| 2101 | dom.remove(node, false); |
||
| 2102 | }); |
||
| 2103 | |||
| 2104 | return new WrappedRange( |
||
| 2105 | point.node, |
||
| 2106 | point.offset, |
||
| 2107 | point.node, |
||
| 2108 | point.offset |
||
| 2109 | ); |
||
| 2110 | }; |
||
| 2111 | |||
| 2112 | /** |
||
| 2113 | * makeIsOn: return isOn(pred) function |
||
| 2114 | */ |
||
| 2115 | var makeIsOn = function (pred) { |
||
| 2116 | return function () { |
||
| 2117 | var ancestor = dom.ancestor(sc, pred); |
||
| 2118 | return !!ancestor && (ancestor === dom.ancestor(ec, pred)); |
||
| 2119 | }; |
||
| 2120 | }; |
||
| 2121 | |||
| 2122 | // isOnEditable: judge whether range is on editable or not |
||
| 2123 | this.isOnEditable = makeIsOn(dom.isEditable); |
||
| 2124 | // isOnList: judge whether range is on list node or not |
||
| 2125 | this.isOnList = makeIsOn(dom.isList); |
||
| 2126 | // isOnAnchor: judge whether range is on anchor node or not |
||
| 2127 | this.isOnAnchor = makeIsOn(dom.isAnchor); |
||
| 2128 | // isOnAnchor: judge whether range is on cell node or not |
||
| 2129 | this.isOnCell = makeIsOn(dom.isCell); |
||
| 2130 | |||
| 2131 | /** |
||
| 2132 | * @param {Function} pred |
||
| 2133 | * @return {Boolean} |
||
| 2134 | */ |
||
| 2135 | this.isLeftEdgeOf = function (pred) { |
||
| 2136 | if (!dom.isLeftEdgePoint(this.getStartPoint())) { |
||
| 2137 | return false; |
||
| 2138 | } |
||
| 2139 | |||
| 2140 | var node = dom.ancestor(this.sc, pred); |
||
| 2141 | return node && dom.isLeftEdgeOf(this.sc, node); |
||
| 2142 | }; |
||
| 2143 | |||
| 2144 | /** |
||
| 2145 | * returns whether range was collapsed or not |
||
| 2146 | */ |
||
| 2147 | this.isCollapsed = function () { |
||
| 2148 | return sc === ec && so === eo; |
||
| 2149 | }; |
||
| 2150 | |||
| 2151 | /** |
||
| 2152 | * wrap inline nodes which children of body with paragraph |
||
| 2153 | * |
||
| 2154 | * @return {WrappedRange} |
||
| 2155 | */ |
||
| 2156 | this.wrapBodyInlineWithPara = function () { |
||
| 2157 | if (dom.isBodyContainer(sc) && dom.isEmpty(sc)) { |
||
| 2158 | sc.innerHTML = dom.emptyPara; |
||
| 2159 | return new WrappedRange(sc.firstChild, 0); |
||
| 2160 | } else if (!dom.isInline(sc) || dom.isParaInline(sc)) { |
||
| 2161 | return this; |
||
| 2162 | } |
||
| 2163 | |||
| 2164 | // find inline top ancestor |
||
| 2165 | var ancestors = dom.listAncestor(sc, func.not(dom.isInline)); |
||
| 2166 | var topAncestor = list.last(ancestors); |
||
| 2167 | if (!dom.isInline(topAncestor)) { |
||
| 2168 | topAncestor = ancestors[ancestors.length - 2] || sc.childNodes[so]; |
||
| 2169 | } |
||
| 2170 | |||
| 2171 | // siblings not in paragraph |
||
| 2172 | var inlineSiblings = dom.listPrev(topAncestor, dom.isParaInline).reverse(); |
||
| 2173 | inlineSiblings = inlineSiblings.concat(dom.listNext(topAncestor.nextSibling, dom.isParaInline)); |
||
| 2174 | |||
| 2175 | // wrap with paragraph |
||
| 2176 | if (inlineSiblings.length) { |
||
| 2177 | var para = dom.wrap(list.head(inlineSiblings), 'p'); |
||
| 2178 | dom.appendChildNodes(para, list.tail(inlineSiblings)); |
||
| 2179 | } |
||
| 2180 | |||
| 2181 | return this; |
||
| 2182 | }; |
||
| 2183 | |||
| 2184 | /** |
||
| 2185 | * insert node at current cursor |
||
| 2186 | * |
||
| 2187 | * @param {Node} node |
||
| 2188 | * @param {Boolean} [isInline] |
||
| 2189 | * @return {Node} |
||
| 2190 | */ |
||
| 2191 | this.insertNode = function (node, isInline) { |
||
| 2192 | var rng = this.wrapBodyInlineWithPara(); |
||
| 2193 | var point = rng.getStartPoint(); |
||
| 2194 | |||
| 2195 | var splitRoot, container, pivot; |
||
| 2196 | if (isInline) { |
||
| 2197 | container = dom.isPara(point.node) ? point.node : point.node.parentNode; |
||
| 2198 | if (dom.isPara(point.node)) { |
||
| 2199 | pivot = point.node.childNodes[point.offset]; |
||
| 2200 | } else { |
||
| 2201 | pivot = dom.splitTree(point.node, point); |
||
| 2202 | } |
||
| 2203 | } else { |
||
| 2204 | // splitRoot will be childNode of container |
||
| 2205 | var ancestors = dom.listAncestor(point.node, dom.isBodyContainer); |
||
| 2206 | var topAncestor = list.last(ancestors) || point.node; |
||
| 2207 | |||
| 2208 | if (dom.isBodyContainer(topAncestor)) { |
||
| 2209 | splitRoot = ancestors[ancestors.length - 2]; |
||
| 2210 | container = topAncestor; |
||
| 2211 | } else { |
||
| 2212 | splitRoot = topAncestor; |
||
| 2213 | container = splitRoot.parentNode; |
||
| 2214 | } |
||
| 2215 | pivot = splitRoot && dom.splitTree(splitRoot, point); |
||
| 2216 | } |
||
| 2217 | |||
| 2218 | if (pivot) { |
||
| 2219 | pivot.parentNode.insertBefore(node, pivot); |
||
| 2220 | } else { |
||
| 2221 | container.appendChild(node); |
||
| 2222 | } |
||
| 2223 | |||
| 2224 | return node; |
||
| 2225 | }; |
||
| 2226 | |||
| 2227 | this.toString = function () { |
||
| 2228 | var nativeRng = nativeRange(); |
||
| 2229 | return agent.isW3CRangeSupport ? nativeRng.toString() : nativeRng.text; |
||
| 2230 | }; |
||
| 2231 | |||
| 2232 | /** |
||
| 2233 | * create offsetPath bookmark |
||
| 2234 | * @param {Node} editable |
||
| 2235 | */ |
||
| 2236 | this.bookmark = function (editable) { |
||
| 2237 | return { |
||
| 2238 | s: { |
||
| 2239 | path: dom.makeOffsetPath(editable, sc), |
||
| 2240 | offset: so |
||
| 2241 | }, |
||
| 2242 | e: { |
||
| 2243 | path: dom.makeOffsetPath(editable, ec), |
||
| 2244 | offset: eo |
||
| 2245 | } |
||
| 2246 | }; |
||
| 2247 | }; |
||
| 2248 | |||
| 2249 | /** |
||
| 2250 | * getClientRects |
||
| 2251 | * @return {Rect[]} |
||
| 2252 | */ |
||
| 2253 | this.getClientRects = function () { |
||
| 2254 | var nativeRng = nativeRange(); |
||
| 2255 | return nativeRng.getClientRects(); |
||
| 2256 | }; |
||
| 2257 | }; |
||
| 2258 | |||
| 2259 | return { |
||
| 2260 | /** |
||
| 2261 | * create Range Object From arguments or Browser Selection |
||
| 2262 | * |
||
| 2263 | * @param {Node} sc - start container |
||
| 2264 | * @param {Number} so - start offset |
||
| 2265 | * @param {Node} ec - end container |
||
| 2266 | * @param {Number} eo - end offset |
||
| 2267 | */ |
||
| 2268 | create : function (sc, so, ec, eo) { |
||
| 2269 | if (!arguments.length) { // from Browser Selection |
||
| 2270 | if (agent.isW3CRangeSupport) { |
||
| 2271 | var selection = document.getSelection(); |
||
| 2272 | if (selection.rangeCount === 0) { |
||
| 2273 | return null; |
||
| 2274 | } else if (dom.isBody(selection.anchorNode)) { |
||
| 2275 | // Firefox: returns entire body as range on initialization. We won't never need it. |
||
| 2276 | return null; |
||
| 2277 | } |
||
| 2278 | |||
| 2279 | var nativeRng = selection.getRangeAt(0); |
||
| 2280 | sc = nativeRng.startContainer; |
||
| 2281 | so = nativeRng.startOffset; |
||
| 2282 | ec = nativeRng.endContainer; |
||
| 2283 | eo = nativeRng.endOffset; |
||
| 2284 | } else { // IE8: TextRange |
||
| 2285 | var textRange = document.selection.createRange(); |
||
| 2286 | var textRangeEnd = textRange.duplicate(); |
||
| 2287 | textRangeEnd.collapse(false); |
||
| 2288 | var textRangeStart = textRange; |
||
| 2289 | textRangeStart.collapse(true); |
||
| 2290 | |||
| 2291 | var startPoint = textRangeToPoint(textRangeStart, true), |
||
| 2292 | endPoint = textRangeToPoint(textRangeEnd, false); |
||
| 2293 | |||
| 2294 | // same visible point case: range was collapsed. |
||
| 2295 | if (dom.isText(startPoint.node) && dom.isLeftEdgePoint(startPoint) && |
||
| 2296 | dom.isTextNode(endPoint.node) && dom.isRightEdgePoint(endPoint) && |
||
| 2297 | endPoint.node.nextSibling === startPoint.node) { |
||
| 2298 | startPoint = endPoint; |
||
| 2299 | } |
||
| 2300 | |||
| 2301 | sc = startPoint.cont; |
||
| 2302 | so = startPoint.offset; |
||
| 2303 | ec = endPoint.cont; |
||
| 2304 | eo = endPoint.offset; |
||
| 2305 | } |
||
| 2306 | } else if (arguments.length === 2) { //collapsed |
||
| 2307 | ec = sc; |
||
| 2308 | eo = so; |
||
| 2309 | } |
||
| 2310 | return new WrappedRange(sc, so, ec, eo); |
||
| 2311 | }, |
||
| 2312 | |||
| 2313 | /** |
||
| 2314 | * create WrappedRange from node |
||
| 2315 | * |
||
| 2316 | * @param {Node} node |
||
| 2317 | * @return {WrappedRange} |
||
| 2318 | */ |
||
| 2319 | createFromNode: function (node) { |
||
| 2320 | return this.create(node, 0, node, 1); |
||
| 2321 | }, |
||
| 2322 | |||
| 2323 | /** |
||
| 2324 | * create WrappedRange from Bookmark |
||
| 2325 | * |
||
| 2326 | * @param {Node} editable |
||
| 2327 | * @param {Obkect} bookmark |
||
| 2328 | * @return {WrappedRange} |
||
| 2329 | */ |
||
| 2330 | createFromBookmark : function (editable, bookmark) { |
||
| 2331 | var sc = dom.fromOffsetPath(editable, bookmark.s.path); |
||
| 2332 | var so = bookmark.s.offset; |
||
| 2333 | var ec = dom.fromOffsetPath(editable, bookmark.e.path); |
||
| 2334 | var eo = bookmark.e.offset; |
||
| 2335 | return new WrappedRange(sc, so, ec, eo); |
||
| 2336 | } |
||
| 2337 | }; |
||
| 2338 | })(); |
||
| 2339 | |||
| 2340 | |||
| 2341 | var Typing = function () { |
||
| 2342 | |||
| 2343 | /** |
||
| 2344 | * @param {jQuery} $editable |
||
| 2345 | * @param {WrappedRange} rng |
||
| 2346 | * @param {Number} tabsize |
||
| 2347 | */ |
||
| 2348 | this.insertTab = function ($editable, rng, tabsize) { |
||
| 2349 | var tab = dom.createText(new Array(tabsize + 1).join(dom.NBSP_CHAR)); |
||
| 2350 | rng = rng.deleteContents(); |
||
| 2351 | rng.insertNode(tab, true); |
||
| 2352 | |||
| 2353 | rng = range.create(tab, tabsize); |
||
| 2354 | rng.select(); |
||
| 2355 | }; |
||
| 2356 | |||
| 2357 | /** |
||
| 2358 | * insert paragraph |
||
| 2359 | */ |
||
| 2360 | this.insertParagraph = function () { |
||
| 2361 | var rng = range.create(); |
||
| 2362 | |||
| 2363 | // deleteContents on range. |
||
| 2364 | rng = rng.deleteContents(); |
||
| 2365 | |||
| 2366 | // Wrap range if it needs to be wrapped by paragraph |
||
| 2367 | rng = rng.wrapBodyInlineWithPara(); |
||
| 2368 | |||
| 2369 | // finding paragraph |
||
| 2370 | var splitRoot = dom.ancestor(rng.sc, dom.isPara); |
||
| 2371 | |||
| 2372 | var nextPara; |
||
| 2373 | // on paragraph: split paragraph |
||
| 2374 | if (splitRoot) { |
||
| 2375 | nextPara = dom.splitTree(splitRoot, rng.getStartPoint()); |
||
| 2376 | |||
| 2377 | var emptyAnchors = dom.listDescendant(splitRoot, dom.isEmptyAnchor); |
||
| 2378 | emptyAnchors = emptyAnchors.concat(dom.listDescendant(nextPara, dom.isEmptyAnchor)); |
||
| 2379 | |||
| 2380 | $.each(emptyAnchors, function (idx, anchor) { |
||
| 2381 | dom.remove(anchor); |
||
| 2382 | }); |
||
| 2383 | // no paragraph: insert empty paragraph |
||
| 2384 | } else { |
||
| 2385 | var next = rng.sc.childNodes[rng.so]; |
||
| 2386 | nextPara = $(dom.emptyPara)[0]; |
||
| 2387 | if (next) { |
||
| 2388 | rng.sc.insertBefore(nextPara, next); |
||
| 2389 | } else { |
||
| 2390 | rng.sc.appendChild(nextPara); |
||
| 2391 | } |
||
| 2392 | } |
||
| 2393 | |||
| 2394 | range.create(nextPara, 0).normalize().select(); |
||
| 2395 | }; |
||
| 2396 | |||
| 2397 | }; |
||
| 2398 | |||
| 2399 | /** |
||
| 2400 | * Table |
||
| 2401 | * @class |
||
| 2402 | */ |
||
| 2403 | var Table = function () { |
||
| 2404 | /** |
||
| 2405 | * handle tab key |
||
| 2406 | * |
||
| 2407 | * @param {WrappedRange} rng |
||
| 2408 | * @param {Boolean} isShift |
||
| 2409 | */ |
||
| 2410 | this.tab = function (rng, isShift) { |
||
| 2411 | var cell = dom.ancestor(rng.commonAncestor(), dom.isCell); |
||
| 2412 | var table = dom.ancestor(cell, dom.isTable); |
||
| 2413 | var cells = dom.listDescendant(table, dom.isCell); |
||
| 2414 | |||
| 2415 | var nextCell = list[isShift ? 'prev' : 'next'](cells, cell); |
||
| 2416 | if (nextCell) { |
||
| 2417 | range.create(nextCell, 0).select(); |
||
| 2418 | } |
||
| 2419 | }; |
||
| 2420 | |||
| 2421 | /** |
||
| 2422 | * create empty table element |
||
| 2423 | * |
||
| 2424 | * @param {Number} rowCount |
||
| 2425 | * @param {Number} colCount |
||
| 2426 | * @return {Node} |
||
| 2427 | */ |
||
| 2428 | this.createTable = function (colCount, rowCount) { |
||
| 2429 | var tds = [], tdHTML; |
||
| 2430 | for (var idxCol = 0; idxCol < colCount; idxCol++) { |
||
| 2431 | tds.push('<td>' + dom.blank + '</td>'); |
||
| 2432 | } |
||
| 2433 | tdHTML = tds.join(''); |
||
| 2434 | |||
| 2435 | var trs = [], trHTML; |
||
| 2436 | for (var idxRow = 0; idxRow < rowCount; idxRow++) { |
||
| 2437 | trs.push('<tr>' + tdHTML + '</tr>'); |
||
| 2438 | } |
||
| 2439 | trHTML = trs.join(''); |
||
| 2440 | return $('<table class="table table-bordered">' + trHTML + '</table>')[0]; |
||
| 2441 | }; |
||
| 2442 | }; |
||
| 2443 | |||
| 2444 | |||
| 2445 | var Bullet = function () { |
||
| 2446 | /** |
||
| 2447 | * toggle ordered list |
||
| 2448 | * @type command |
||
| 2449 | */ |
||
| 2450 | this.insertOrderedList = function () { |
||
| 2451 | this.toggleList('OL'); |
||
| 2452 | }; |
||
| 2453 | |||
| 2454 | /** |
||
| 2455 | * toggle unordered list |
||
| 2456 | * @type command |
||
| 2457 | */ |
||
| 2458 | this.insertUnorderedList = function () { |
||
| 2459 | this.toggleList('UL'); |
||
| 2460 | }; |
||
| 2461 | |||
| 2462 | /** |
||
| 2463 | * indent |
||
| 2464 | * @type command |
||
| 2465 | */ |
||
| 2466 | this.indent = function () { |
||
| 2467 | var self = this; |
||
| 2468 | var rng = range.create().wrapBodyInlineWithPara(); |
||
| 2469 | |||
| 2470 | var paras = rng.nodes(dom.isPara, { includeAncestor: true }); |
||
| 2471 | var clustereds = list.clusterBy(paras, func.peq2('parentNode')); |
||
| 2472 | |||
| 2473 | $.each(clustereds, function (idx, paras) { |
||
| 2474 | var head = list.head(paras); |
||
| 2475 | if (dom.isLi(head)) { |
||
| 2476 | self.wrapList(paras, head.parentNode.nodeName); |
||
| 2477 | } else { |
||
| 2478 | $.each(paras, function (idx, para) { |
||
| 2479 | $(para).css('marginLeft', function (idx, val) { |
||
| 2480 | return (parseInt(val, 10) || 0) + 25; |
||
| 2481 | }); |
||
| 2482 | }); |
||
| 2483 | } |
||
| 2484 | }); |
||
| 2485 | |||
| 2486 | rng.select(); |
||
| 2487 | }; |
||
| 2488 | |||
| 2489 | /** |
||
| 2490 | * outdent |
||
| 2491 | * @type command |
||
| 2492 | */ |
||
| 2493 | this.outdent = function () { |
||
| 2494 | var self = this; |
||
| 2495 | var rng = range.create().wrapBodyInlineWithPara(); |
||
| 2496 | |||
| 2497 | var paras = rng.nodes(dom.isPara, { includeAncestor: true }); |
||
| 2498 | var clustereds = list.clusterBy(paras, func.peq2('parentNode')); |
||
| 2499 | |||
| 2500 | $.each(clustereds, function (idx, paras) { |
||
| 2501 | var head = list.head(paras); |
||
| 2502 | if (dom.isLi(head)) { |
||
| 2503 | self.releaseList([paras]); |
||
| 2504 | } else { |
||
| 2505 | $.each(paras, function (idx, para) { |
||
| 2506 | $(para).css('marginLeft', function (idx, val) { |
||
| 2507 | val = (parseInt(val, 10) || 0); |
||
| 2508 | return val > 25 ? val - 25 : ''; |
||
| 2509 | }); |
||
| 2510 | }); |
||
| 2511 | } |
||
| 2512 | }); |
||
| 2513 | |||
| 2514 | rng.select(); |
||
| 2515 | }; |
||
| 2516 | |||
| 2517 | /** |
||
| 2518 | * toggle list |
||
| 2519 | * @param {String} listName - OL or UL |
||
| 2520 | */ |
||
| 2521 | this.toggleList = function (listName) { |
||
| 2522 | var self = this; |
||
| 2523 | var rng = range.create().wrapBodyInlineWithPara(); |
||
| 2524 | |||
| 2525 | var paras = rng.nodes(dom.isPara, { includeAncestor: true }); |
||
| 2526 | var clustereds = list.clusterBy(paras, func.peq2('parentNode')); |
||
| 2527 | |||
| 2528 | // paragraph to list |
||
| 2529 | if (list.find(paras, dom.isPurePara)) { |
||
| 2530 | $.each(clustereds, function (idx, paras) { |
||
| 2531 | self.wrapList(paras, listName); |
||
| 2532 | }); |
||
| 2533 | // list to paragraph or change list style |
||
| 2534 | } else { |
||
| 2535 | var diffLists = rng.nodes(dom.isList, { |
||
| 2536 | includeAncestor: true |
||
| 2537 | }).filter(function (listNode) { |
||
| 2538 | return !$.nodeName(listNode, listName); |
||
| 2539 | }); |
||
| 2540 | |||
| 2541 | if (diffLists.length) { |
||
| 2542 | $.each(diffLists, function (idx, listNode) { |
||
| 2543 | dom.replace(listNode, listName); |
||
| 2544 | }); |
||
| 2545 | } else { |
||
| 2546 | this.releaseList(clustereds, true); |
||
| 2547 | } |
||
| 2548 | } |
||
| 2549 | |||
| 2550 | rng.select(); |
||
| 2551 | }; |
||
| 2552 | |||
| 2553 | /** |
||
| 2554 | * @param {Node[]} paras |
||
| 2555 | * @param {String} listName |
||
| 2556 | */ |
||
| 2557 | this.wrapList = function (paras, listName) { |
||
| 2558 | var head = list.head(paras); |
||
| 2559 | var last = list.last(paras); |
||
| 2560 | |||
| 2561 | var prevList = dom.isList(head.previousSibling) && head.previousSibling; |
||
| 2562 | var nextList = dom.isList(last.nextSibling) && last.nextSibling; |
||
| 2563 | |||
| 2564 | var listNode = prevList || dom.insertAfter(dom.create(listName || 'UL'), last); |
||
| 2565 | |||
| 2566 | // P to LI |
||
| 2567 | paras = $.map(paras, function (para) { |
||
| 2568 | return dom.isPurePara(para) ? dom.replace(para, 'LI') : para; |
||
| 2569 | }); |
||
| 2570 | |||
| 2571 | // append to list(<ul>, <ol>) |
||
| 2572 | dom.appendChildNodes(listNode, paras); |
||
| 2573 | |||
| 2574 | if (nextList) { |
||
| 2575 | dom.appendChildNodes(listNode, list.from(nextList.childNodes)); |
||
| 2576 | dom.remove(nextList); |
||
| 2577 | } |
||
| 2578 | }; |
||
| 2579 | |||
| 2580 | /** |
||
| 2581 | * @param {Array[]} clustereds |
||
| 2582 | * @param {Boolean} isEscapseToBody |
||
| 2583 | * @return {Node[]} |
||
| 2584 | */ |
||
| 2585 | this.releaseList = function (clustereds, isEscapseToBody) { |
||
| 2586 | var releasedParas = []; |
||
| 2587 | |||
| 2588 | $.each(clustereds, function (idx, paras) { |
||
| 2589 | var head = list.head(paras); |
||
| 2590 | var last = list.last(paras); |
||
| 2591 | |||
| 2592 | var headList = isEscapseToBody ? dom.lastAncestor(head, dom.isList) : |
||
| 2593 | head.parentNode; |
||
| 2594 | var lastList = headList.childNodes.length > 1 ? dom.splitTree(headList, { |
||
| 2595 | node: last.parentNode, |
||
| 2596 | offset: dom.position(last) + 1 |
||
| 2597 | }, true) : null; |
||
| 2598 | |||
| 2599 | var middleList = dom.splitTree(headList, { |
||
| 2600 | node: head.parentNode, |
||
| 2601 | offset: dom.position(head) |
||
| 2602 | }, true); |
||
| 2603 | |||
| 2604 | paras = isEscapseToBody ? dom.listDescendant(middleList, dom.isLi) : |
||
| 2605 | list.from(middleList.childNodes).filter(dom.isLi); |
||
| 2606 | |||
| 2607 | // LI to P |
||
| 2608 | if (isEscapseToBody || !dom.isList(headList.parentNode)) { |
||
| 2609 | paras = $.map(paras, function (para) { |
||
| 2610 | return dom.replace(para, 'P'); |
||
| 2611 | }); |
||
| 2612 | } |
||
| 2613 | |||
| 2614 | $.each(list.from(paras).reverse(), function (idx, para) { |
||
| 2615 | dom.insertAfter(para, headList); |
||
| 2616 | }); |
||
| 2617 | |||
| 2618 | // remove empty lists |
||
| 2619 | var rootLists = list.compact([headList, middleList, lastList]); |
||
| 2620 | $.each(rootLists, function (idx, rootList) { |
||
| 2621 | var listNodes = [rootList].concat(dom.listDescendant(rootList, dom.isList)); |
||
| 2622 | $.each(listNodes.reverse(), function (idx, listNode) { |
||
| 2623 | if (!dom.nodeLength(listNode)) { |
||
| 2624 | dom.remove(listNode, true); |
||
| 2625 | } |
||
| 2626 | }); |
||
| 2627 | }); |
||
| 2628 | |||
| 2629 | releasedParas = releasedParas.concat(paras); |
||
| 2630 | }); |
||
| 2631 | |||
| 2632 | return releasedParas; |
||
| 2633 | }; |
||
| 2634 | }; |
||
| 2635 | |||
| 2636 | /** |
||
| 2637 | * Editor |
||
| 2638 | * @class |
||
| 2639 | */ |
||
| 2640 | var Editor = function () { |
||
| 2641 | |||
| 2642 | var style = new Style(); |
||
| 2643 | var table = new Table(); |
||
| 2644 | var typing = new Typing(); |
||
| 2645 | var bullet = new Bullet(); |
||
| 2646 | |||
| 2647 | /** |
||
| 2648 | * save current range |
||
| 2649 | * |
||
| 2650 | * @param {jQuery} $editable |
||
| 2651 | */ |
||
| 2652 | this.saveRange = function ($editable, thenCollapse) { |
||
| 2653 | $editable.focus(); |
||
| 2654 | $editable.data('range', range.create()); |
||
| 2655 | if (thenCollapse) { |
||
| 2656 | range.create().collapse().select(); |
||
| 2657 | } |
||
| 2658 | }; |
||
| 2659 | |||
| 2660 | /** |
||
| 2661 | * restore lately range |
||
| 2662 | * |
||
| 2663 | * @param {jQuery} $editable |
||
| 2664 | */ |
||
| 2665 | this.restoreRange = function ($editable) { |
||
| 2666 | var rng = $editable.data('range'); |
||
| 2667 | if (rng) { |
||
| 2668 | rng.select(); |
||
| 2669 | $editable.focus(); |
||
| 2670 | } |
||
| 2671 | }; |
||
| 2672 | |||
| 2673 | /** |
||
| 2674 | * current style |
||
| 2675 | * @param {Node} target |
||
| 2676 | */ |
||
| 2677 | this.currentStyle = function (target) { |
||
| 2678 | var rng = range.create(); |
||
| 2679 | return rng ? rng.isOnEditable() && style.current(rng, target) : false; |
||
| 2680 | }; |
||
| 2681 | |||
| 2682 | var triggerOnChange = this.triggerOnChange = function ($editable) { |
||
| 2683 | var onChange = $editable.data('callbacks').onChange; |
||
| 2684 | if (onChange) { |
||
| 2685 | onChange($editable.html(), $editable); |
||
| 2686 | } |
||
| 2687 | }; |
||
| 2688 | |||
| 2689 | /** |
||
| 2690 | * undo |
||
| 2691 | * @param {jQuery} $editable |
||
| 2692 | */ |
||
| 2693 | this.undo = function ($editable) { |
||
| 2694 | $editable.data('NoteHistory').undo(); |
||
| 2695 | triggerOnChange($editable); |
||
| 2696 | }; |
||
| 2697 | |||
| 2698 | /** |
||
| 2699 | * redo |
||
| 2700 | * @param {jQuery} $editable |
||
| 2701 | */ |
||
| 2702 | this.redo = function ($editable) { |
||
| 2703 | $editable.data('NoteHistory').redo(); |
||
| 2704 | triggerOnChange($editable); |
||
| 2705 | }; |
||
| 2706 | |||
| 2707 | /** |
||
| 2708 | * after command |
||
| 2709 | * @param {jQuery} $editable |
||
| 2710 | */ |
||
| 2711 | var afterCommand = this.afterCommand = function ($editable) { |
||
| 2712 | $editable.data('NoteHistory').recordUndo(); |
||
| 2713 | triggerOnChange($editable); |
||
| 2714 | }; |
||
| 2715 | |||
| 2716 | /* jshint ignore:start */ |
||
| 2717 | // native commands(with execCommand), generate function for execCommand |
||
| 2718 | var commands = ['bold', 'italic', 'underline', 'strikethrough', 'superscript', 'subscript', |
||
| 2719 | 'justifyLeft', 'justifyCenter', 'justifyRight', 'justifyFull', |
||
| 2720 | 'formatBlock', 'removeFormat', |
||
| 2721 | 'backColor', 'foreColor', 'insertHorizontalRule', 'fontName']; |
||
| 2722 | |||
| 2723 | for (var idx = 0, len = commands.length; idx < len; idx ++) { |
||
| 2724 | this[commands[idx]] = (function (sCmd) { |
||
| 2725 | return function ($editable, value) { |
||
| 2726 | document.execCommand(sCmd, false, value); |
||
| 2727 | |||
| 2728 | afterCommand($editable); |
||
| 2729 | }; |
||
| 2730 | })(commands[idx]); |
||
| 2731 | } |
||
| 2732 | /* jshint ignore:end */ |
||
| 2733 | |||
| 2734 | /** |
||
| 2735 | * handle tab key |
||
| 2736 | * |
||
| 2737 | * @param {jQuery} $editable |
||
| 2738 | * @param {Object} options |
||
| 2739 | */ |
||
| 2740 | this.tab = function ($editable, options) { |
||
| 2741 | var rng = range.create(); |
||
| 2742 | if (rng.isCollapsed() && rng.isOnCell()) { |
||
| 2743 | table.tab(rng); |
||
| 2744 | } else { |
||
| 2745 | typing.insertTab($editable, rng, options.tabsize); |
||
| 2746 | afterCommand($editable); |
||
| 2747 | } |
||
| 2748 | }; |
||
| 2749 | |||
| 2750 | /** |
||
| 2751 | * handle shift+tab key |
||
| 2752 | */ |
||
| 2753 | this.untab = function () { |
||
| 2754 | var rng = range.create(); |
||
| 2755 | if (rng.isCollapsed() && rng.isOnCell()) { |
||
| 2756 | table.tab(rng, true); |
||
| 2757 | } |
||
| 2758 | }; |
||
| 2759 | |||
| 2760 | /** |
||
| 2761 | * insert paragraph |
||
| 2762 | * |
||
| 2763 | * @param {Node} $editable |
||
| 2764 | */ |
||
| 2765 | this.insertParagraph = function ($editable) { |
||
| 2766 | typing.insertParagraph($editable); |
||
| 2767 | afterCommand($editable); |
||
| 2768 | }; |
||
| 2769 | |||
| 2770 | /** |
||
| 2771 | * @param {jQuery} $editable |
||
| 2772 | */ |
||
| 2773 | this.insertOrderedList = function ($editable) { |
||
| 2774 | bullet.insertOrderedList($editable); |
||
| 2775 | afterCommand($editable); |
||
| 2776 | }; |
||
| 2777 | |||
| 2778 | /** |
||
| 2779 | * @param {jQuery} $editable |
||
| 2780 | */ |
||
| 2781 | this.insertUnorderedList = function ($editable) { |
||
| 2782 | bullet.insertUnorderedList($editable); |
||
| 2783 | afterCommand($editable); |
||
| 2784 | }; |
||
| 2785 | |||
| 2786 | /** |
||
| 2787 | * @param {jQuery} $editable |
||
| 2788 | */ |
||
| 2789 | this.indent = function ($editable) { |
||
| 2790 | bullet.indent($editable); |
||
| 2791 | afterCommand($editable); |
||
| 2792 | }; |
||
| 2793 | |||
| 2794 | /** |
||
| 2795 | * @param {jQuery} $editable |
||
| 2796 | */ |
||
| 2797 | this.outdent = function ($editable) { |
||
| 2798 | bullet.outdent($editable); |
||
| 2799 | afterCommand($editable); |
||
| 2800 | }; |
||
| 2801 | |||
| 2802 | /** |
||
| 2803 | * insert image |
||
| 2804 | * |
||
| 2805 | * @param {jQuery} $editable |
||
| 2806 | * @param {String} sUrl |
||
| 2807 | */ |
||
| 2808 | this.insertImage = function ($editable, sUrl, filename) { |
||
| 2809 | async.createImage(sUrl, filename).then(function ($image) { |
||
| 2810 | $image.css({ |
||
| 2811 | display: '', |
||
| 2812 | width: Math.min($editable.width(), $image.width()) |
||
| 2813 | }); |
||
| 2814 | range.create().insertNode($image[0]); |
||
| 2815 | afterCommand($editable); |
||
| 2816 | }).fail(function () { |
||
| 2817 | var callbacks = $editable.data('callbacks'); |
||
| 2818 | if (callbacks.onImageUploadError) { |
||
| 2819 | callbacks.onImageUploadError(); |
||
| 2820 | } |
||
| 2821 | }); |
||
| 2822 | }; |
||
| 2823 | |||
| 2824 | /** |
||
| 2825 | * insert video |
||
| 2826 | * @param {jQuery} $editable |
||
| 2827 | * @param {String} sUrl |
||
| 2828 | */ |
||
| 2829 | this.insertVideo = function ($editable, sUrl) { |
||
| 2830 | // video url patterns(youtube, instagram, vimeo, dailymotion, youku) |
||
| 2831 | var ytRegExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; |
||
| 2832 | var ytMatch = sUrl.match(ytRegExp); |
||
| 2833 | |||
| 2834 | var igRegExp = /\/\/instagram.com\/p\/(.[a-zA-Z0-9]*)/; |
||
| 2835 | var igMatch = sUrl.match(igRegExp); |
||
| 2836 | |||
| 2837 | var vRegExp = /\/\/vine.co\/v\/(.[a-zA-Z0-9]*)/; |
||
| 2838 | var vMatch = sUrl.match(vRegExp); |
||
| 2839 | |||
| 2840 | var vimRegExp = /\/\/(player.)?vimeo.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/; |
||
| 2841 | var vimMatch = sUrl.match(vimRegExp); |
||
| 2842 | |||
| 2843 | var dmRegExp = /.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/; |
||
| 2844 | var dmMatch = sUrl.match(dmRegExp); |
||
| 2845 | |||
| 2846 | var youkuRegExp = /\/\/v\.youku\.com\/v_show\/id_(\w+)\.html/; |
||
| 2847 | var youkuMatch = sUrl.match(youkuRegExp); |
||
| 2848 | |||
| 2849 | var $video; |
||
| 2850 | if (ytMatch && ytMatch[2].length === 11) { |
||
| 2851 | var youtubeId = ytMatch[2]; |
||
| 2852 | $video = $('<iframe>') |
||
| 2853 | .attr('src', '//www.youtube.com/embed/' + youtubeId) |
||
| 2854 | .attr('width', '640').attr('height', '360'); |
||
| 2855 | } else if (igMatch && igMatch[0].length) { |
||
| 2856 | $video = $('<iframe>') |
||
| 2857 | .attr('src', igMatch[0] + '/embed/') |
||
| 2858 | .attr('width', '612').attr('height', '710') |
||
| 2859 | .attr('scrolling', 'no') |
||
| 2860 | .attr('allowtransparency', 'true'); |
||
| 2861 | } else if (vMatch && vMatch[0].length) { |
||
| 2862 | $video = $('<iframe>') |
||
| 2863 | .attr('src', vMatch[0] + '/embed/simple') |
||
| 2864 | .attr('width', '600').attr('height', '600') |
||
| 2865 | .attr('class', 'vine-embed'); |
||
| 2866 | } else if (vimMatch && vimMatch[3].length) { |
||
| 2867 | $video = $('<iframe webkitallowfullscreen mozallowfullscreen allowfullscreen>') |
||
| 2868 | .attr('src', '//player.vimeo.com/video/' + vimMatch[3]) |
||
| 2869 | .attr('width', '640').attr('height', '360'); |
||
| 2870 | } else if (dmMatch && dmMatch[2].length) { |
||
| 2871 | $video = $('<iframe>') |
||
| 2872 | .attr('src', '//www.dailymotion.com/embed/video/' + dmMatch[2]) |
||
| 2873 | .attr('width', '640').attr('height', '360'); |
||
| 2874 | } else if (youkuMatch && youkuMatch[1].length) { |
||
| 2875 | $video = $('<iframe webkitallowfullscreen mozallowfullscreen allowfullscreen>') |
||
| 2876 | .attr('height', '498') |
||
| 2877 | .attr('width', '510') |
||
| 2878 | .attr('src', '//player.youku.com/embed/' + youkuMatch[1]); |
||
| 2879 | } else { |
||
| 2880 | // this is not a known video link. Now what, Cat? Now what? |
||
| 2881 | } |
||
| 2882 | |||
| 2883 | if ($video) { |
||
| 2884 | $video.attr('frameborder', 0); |
||
| 2885 | range.create().insertNode($video[0]); |
||
| 2886 | afterCommand($editable); |
||
| 2887 | } |
||
| 2888 | }; |
||
| 2889 | |||
| 2890 | /** |
||
| 2891 | * formatBlock |
||
| 2892 | * |
||
| 2893 | * @param {jQuery} $editable |
||
| 2894 | * @param {String} tagName |
||
| 2895 | */ |
||
| 2896 | this.formatBlock = function ($editable, tagName) { |
||
| 2897 | tagName = agent.isMSIE ? '<' + tagName + '>' : tagName; |
||
| 2898 | document.execCommand('FormatBlock', false, tagName); |
||
| 2899 | afterCommand($editable); |
||
| 2900 | }; |
||
| 2901 | |||
| 2902 | this.formatPara = function ($editable) { |
||
| 2903 | this.formatBlock($editable, 'P'); |
||
| 2904 | afterCommand($editable); |
||
| 2905 | }; |
||
| 2906 | |||
| 2907 | /* jshint ignore:start */ |
||
| 2908 | for (var idx = 1; idx <= 6; idx ++) { |
||
| 2909 | this['formatH' + idx] = function (idx) { |
||
| 2910 | return function ($editable) { |
||
| 2911 | this.formatBlock($editable, 'H' + idx); |
||
| 2912 | }; |
||
| 2913 | }(idx); |
||
| 2914 | }; |
||
| 2915 | /* jshint ignore:end */ |
||
| 2916 | |||
| 2917 | /** |
||
| 2918 | * fontsize |
||
| 2919 | * FIXME: Still buggy |
||
| 2920 | * |
||
| 2921 | * @param {jQuery} $editable |
||
| 2922 | * @param {String} value - px |
||
| 2923 | */ |
||
| 2924 | this.fontSize = function ($editable, value) { |
||
| 2925 | document.execCommand('fontSize', false, 3); |
||
| 2926 | if (agent.isFF) { |
||
| 2927 | // firefox: <font size="3"> to <span style='font-size={value}px;'>, buggy |
||
| 2928 | $editable.find('font[size=3]').removeAttr('size').css('font-size', value + 'px'); |
||
| 2929 | } else { |
||
| 2930 | // chrome: <span style="font-size: medium"> to <span style='font-size={value}px;'> |
||
| 2931 | $editable.find('span').filter(function () { |
||
| 2932 | return this.style.fontSize === 'medium'; |
||
| 2933 | }).css('font-size', value + 'px'); |
||
| 2934 | } |
||
| 2935 | |||
| 2936 | afterCommand($editable); |
||
| 2937 | }; |
||
| 2938 | |||
| 2939 | /** |
||
| 2940 | * lineHeight |
||
| 2941 | * @param {jQuery} $editable |
||
| 2942 | * @param {String} value |
||
| 2943 | */ |
||
| 2944 | this.lineHeight = function ($editable, value) { |
||
| 2945 | style.stylePara(range.create(), { |
||
| 2946 | lineHeight: value |
||
| 2947 | }); |
||
| 2948 | afterCommand($editable); |
||
| 2949 | }; |
||
| 2950 | |||
| 2951 | /** |
||
| 2952 | * unlink |
||
| 2953 | * |
||
| 2954 | * @type command |
||
| 2955 | * |
||
| 2956 | * @param {jQuery} $editable |
||
| 2957 | */ |
||
| 2958 | this.unlink = function ($editable) { |
||
| 2959 | var rng = range.create(); |
||
| 2960 | if (rng.isOnAnchor()) { |
||
| 2961 | var anchor = dom.ancestor(rng.sc, dom.isAnchor); |
||
| 2962 | rng = range.createFromNode(anchor); |
||
| 2963 | rng.select(); |
||
| 2964 | document.execCommand('unlink'); |
||
| 2965 | |||
| 2966 | afterCommand($editable); |
||
| 2967 | } |
||
| 2968 | }; |
||
| 2969 | |||
| 2970 | /** |
||
| 2971 | * create link |
||
| 2972 | * |
||
| 2973 | * @type command |
||
| 2974 | * |
||
| 2975 | * @param {jQuery} $editable |
||
| 2976 | * @param {Object} linkInfo |
||
| 2977 | * @param {Object} options |
||
| 2978 | */ |
||
| 2979 | this.createLink = function ($editable, linkInfo, options) { |
||
| 2980 | var linkUrl = linkInfo.url; |
||
| 2981 | var linkText = linkInfo.text; |
||
| 2982 | var isNewWindow = linkInfo.newWindow; |
||
| 2983 | var rng = linkInfo.range; |
||
| 2984 | |||
| 2985 | if (options.onCreateLink) { |
||
| 2986 | linkUrl = options.onCreateLink(linkUrl); |
||
| 2987 | } |
||
| 2988 | |||
| 2989 | rng = rng.deleteContents(); |
||
| 2990 | |||
| 2991 | // Create a new link when there is no anchor on range. |
||
| 2992 | var anchor = rng.insertNode($('<A>' + linkText + '</A>')[0], true); |
||
| 2993 | $(anchor).attr({ |
||
| 2994 | href: linkUrl, |
||
| 2995 | target: isNewWindow ? '_blank' : '' |
||
| 2996 | }); |
||
| 2997 | |||
| 2998 | range.createFromNode(anchor).select(); |
||
| 2999 | afterCommand($editable); |
||
| 3000 | }; |
||
| 3001 | |||
| 3002 | /** |
||
| 3003 | * returns link info |
||
| 3004 | * |
||
| 3005 | * @return {Object} |
||
| 3006 | */ |
||
| 3007 | this.getLinkInfo = function ($editable) { |
||
| 3008 | $editable.focus(); |
||
| 3009 | |||
| 3010 | var rng = range.create().expand(dom.isAnchor); |
||
| 3011 | |||
| 3012 | // Get the first anchor on range(for edit). |
||
| 3013 | var $anchor = $(list.head(rng.nodes(dom.isAnchor))); |
||
| 3014 | |||
| 3015 | return { |
||
| 3016 | range: rng, |
||
| 3017 | text: rng.toString(), |
||
| 3018 | isNewWindow: $anchor.length ? $anchor.attr('target') === '_blank' : true, |
||
| 3019 | url: $anchor.length ? $anchor.attr('href') : '' |
||
| 3020 | }; |
||
| 3021 | }; |
||
| 3022 | |||
| 3023 | /** |
||
| 3024 | * get video info |
||
| 3025 | * |
||
| 3026 | * @param {jQuery} $editable |
||
| 3027 | * @return {Object} |
||
| 3028 | */ |
||
| 3029 | this.getVideoInfo = function ($editable) { |
||
| 3030 | $editable.focus(); |
||
| 3031 | |||
| 3032 | var rng = range.create(); |
||
| 3033 | |||
| 3034 | if (rng.isOnAnchor()) { |
||
| 3035 | var anchor = dom.ancestor(rng.sc, dom.isAnchor); |
||
| 3036 | rng = range.createFromNode(anchor); |
||
| 3037 | } |
||
| 3038 | |||
| 3039 | return { |
||
| 3040 | text: rng.toString() |
||
| 3041 | }; |
||
| 3042 | }; |
||
| 3043 | |||
| 3044 | this.color = function ($editable, sObjColor) { |
||
| 3045 | var oColor = JSON.parse(sObjColor); |
||
| 3046 | var foreColor = oColor.foreColor, backColor = oColor.backColor; |
||
| 3047 | |||
| 3048 | if (foreColor) { document.execCommand('foreColor', false, foreColor); } |
||
| 3049 | if (backColor) { document.execCommand('backColor', false, backColor); } |
||
| 3050 | |||
| 3051 | afterCommand($editable); |
||
| 3052 | }; |
||
| 3053 | |||
| 3054 | this.insertTable = function ($editable, sDim) { |
||
| 3055 | var dimension = sDim.split('x'); |
||
| 3056 | var rng = range.create(); |
||
| 3057 | rng = rng.deleteContents(); |
||
| 3058 | rng.insertNode(table.createTable(dimension[0], dimension[1])); |
||
| 3059 | afterCommand($editable); |
||
| 3060 | }; |
||
| 3061 | |||
| 3062 | /** |
||
| 3063 | * @param {jQuery} $editable |
||
| 3064 | * @param {String} value |
||
| 3065 | * @param {jQuery} $target |
||
| 3066 | */ |
||
| 3067 | this.floatMe = function ($editable, value, $target) { |
||
| 3068 | $target.css('float', value); |
||
| 3069 | afterCommand($editable); |
||
| 3070 | }; |
||
| 3071 | |||
| 3072 | this.imageShape = function ($editable, value, $target) { |
||
| 3073 | $target.removeClass('img-rounded img-circle img-thumbnail'); |
||
| 3074 | |||
| 3075 | if (value) { |
||
| 3076 | $target.addClass(value); |
||
| 3077 | } |
||
| 3078 | }; |
||
| 3079 | |||
| 3080 | /** |
||
| 3081 | * resize overlay element |
||
| 3082 | * @param {jQuery} $editable |
||
| 3083 | * @param {String} value |
||
| 3084 | * @param {jQuery} $target - target element |
||
| 3085 | */ |
||
| 3086 | this.resize = function ($editable, value, $target) { |
||
| 3087 | $target.css({ |
||
| 3088 | width: value * 100 + '%', |
||
| 3089 | height: '' |
||
| 3090 | }); |
||
| 3091 | |||
| 3092 | afterCommand($editable); |
||
| 3093 | }; |
||
| 3094 | |||
| 3095 | /** |
||
| 3096 | * @param {Position} pos |
||
| 3097 | * @param {jQuery} $target - target element |
||
| 3098 | * @param {Boolean} [bKeepRatio] - keep ratio |
||
| 3099 | */ |
||
| 3100 | this.resizeTo = function (pos, $target, bKeepRatio) { |
||
| 3101 | var imageSize; |
||
| 3102 | if (bKeepRatio) { |
||
| 3103 | var newRatio = pos.y / pos.x; |
||
| 3104 | var ratio = $target.data('ratio'); |
||
| 3105 | imageSize = { |
||
| 3106 | width: ratio > newRatio ? pos.x : pos.y / ratio, |
||
| 3107 | height: ratio > newRatio ? pos.x * ratio : pos.y |
||
| 3108 | }; |
||
| 3109 | } else { |
||
| 3110 | imageSize = { |
||
| 3111 | width: pos.x, |
||
| 3112 | height: pos.y |
||
| 3113 | }; |
||
| 3114 | } |
||
| 3115 | |||
| 3116 | $target.css(imageSize); |
||
| 3117 | }; |
||
| 3118 | |||
| 3119 | /** |
||
| 3120 | * remove media object |
||
| 3121 | * |
||
| 3122 | * @param {jQuery} $editable |
||
| 3123 | * @param {String} value - dummy argument (for keep interface) |
||
| 3124 | * @param {jQuery} $target - target element |
||
| 3125 | */ |
||
| 3126 | this.removeMedia = function ($editable, value, $target) { |
||
| 3127 | $target.detach(); |
||
| 3128 | |||
| 3129 | afterCommand($editable); |
||
| 3130 | }; |
||
| 3131 | }; |
||
| 3132 | |||
| 3133 | /** |
||
| 3134 | * History |
||
| 3135 | * @class |
||
| 3136 | */ |
||
| 3137 | var History = function ($editable) { |
||
| 3138 | var stack = [], stackOffset = -1; |
||
| 3139 | var editable = $editable[0]; |
||
| 3140 | |||
| 3141 | var makeSnapshot = function () { |
||
| 3142 | var rng = range.create(); |
||
| 3143 | var emptyBookmark = {s: {path: [0], offset: 0}, e: {path: [0], offset: 0}}; |
||
| 3144 | |||
| 3145 | return { |
||
| 3146 | contents: $editable.html(), |
||
| 3147 | bookmark: (rng ? rng.bookmark(editable) : emptyBookmark) |
||
| 3148 | }; |
||
| 3149 | }; |
||
| 3150 | |||
| 3151 | var applySnapshot = function (snapshot) { |
||
| 3152 | if (snapshot.contents !== null) { |
||
| 3153 | $editable.html(snapshot.contents); |
||
| 3154 | } |
||
| 3155 | if (snapshot.bookmark !== null) { |
||
| 3156 | range.createFromBookmark(editable, snapshot.bookmark).select(); |
||
| 3157 | } |
||
| 3158 | }; |
||
| 3159 | |||
| 3160 | this.undo = function () { |
||
| 3161 | if (0 < stackOffset) { |
||
| 3162 | stackOffset--; |
||
| 3163 | applySnapshot(stack[stackOffset]); |
||
| 3164 | } |
||
| 3165 | }; |
||
| 3166 | |||
| 3167 | this.redo = function () { |
||
| 3168 | if (stack.length - 1 > stackOffset) { |
||
| 3169 | stackOffset++; |
||
| 3170 | applySnapshot(stack[stackOffset]); |
||
| 3171 | } |
||
| 3172 | }; |
||
| 3173 | |||
| 3174 | this.recordUndo = function () { |
||
| 3175 | stackOffset++; |
||
| 3176 | |||
| 3177 | // Wash out stack after stackOffset |
||
| 3178 | if (stack.length > stackOffset) { |
||
| 3179 | stack = stack.slice(0, stackOffset); |
||
| 3180 | } |
||
| 3181 | |||
| 3182 | // Create new snapshot and push it to the end |
||
| 3183 | stack.push(makeSnapshot()); |
||
| 3184 | }; |
||
| 3185 | |||
| 3186 | // Create first undo stack |
||
| 3187 | this.recordUndo(); |
||
| 3188 | }; |
||
| 3189 | |||
| 3190 | /** |
||
| 3191 | * Button |
||
| 3192 | */ |
||
| 3193 | var Button = function () { |
||
| 3194 | /** |
||
| 3195 | * update button status |
||
| 3196 | * |
||
| 3197 | * @param {jQuery} $container |
||
| 3198 | * @param {Object} styleInfo |
||
| 3199 | */ |
||
| 3200 | this.update = function ($container, styleInfo) { |
||
| 3201 | /** |
||
| 3202 | * handle dropdown's check mark (for fontname, fontsize, lineHeight). |
||
| 3203 | * @param {jQuery} $btn |
||
| 3204 | * @param {Number} value |
||
| 3205 | */ |
||
| 3206 | var checkDropdownMenu = function ($btn, value) { |
||
| 3207 | $btn.find('.dropdown-menu li a').each(function () { |
||
| 3208 | // always compare string to avoid creating another func. |
||
| 3209 | var isChecked = ($(this).data('value') + '') === (value + ''); |
||
| 3210 | this.className = isChecked ? 'checked' : ''; |
||
| 3211 | }); |
||
| 3212 | }; |
||
| 3213 | |||
| 3214 | /** |
||
| 3215 | * update button state(active or not). |
||
| 3216 | * |
||
| 3217 | * @param {String} selector |
||
| 3218 | * @param {Function} pred |
||
| 3219 | */ |
||
| 3220 | var btnState = function (selector, pred) { |
||
| 3221 | var $btn = $container.find(selector); |
||
| 3222 | $btn.toggleClass('active', pred()); |
||
| 3223 | }; |
||
| 3224 | |||
| 3225 | // fontname |
||
| 3226 | var $fontname = $container.find('.note-fontname'); |
||
| 3227 | if ($fontname.length) { |
||
| 3228 | var selectedFont = styleInfo['font-family']; |
||
| 3229 | if (!!selectedFont) { |
||
| 3230 | selectedFont = list.head(selectedFont.split(',')); |
||
| 3231 | selectedFont = selectedFont.replace(/\'/g, ''); |
||
| 3232 | $fontname.find('.note-current-fontname').text(selectedFont); |
||
| 3233 | checkDropdownMenu($fontname, selectedFont); |
||
| 3234 | } |
||
| 3235 | } |
||
| 3236 | |||
| 3237 | // fontsize |
||
| 3238 | var $fontsize = $container.find('.note-fontsize'); |
||
| 3239 | $fontsize.find('.note-current-fontsize').text(styleInfo['font-size']); |
||
| 3240 | checkDropdownMenu($fontsize, parseFloat(styleInfo['font-size'])); |
||
| 3241 | |||
| 3242 | // lineheight |
||
| 3243 | var $lineHeight = $container.find('.note-height'); |
||
| 3244 | checkDropdownMenu($lineHeight, parseFloat(styleInfo['line-height'])); |
||
| 3245 | |||
| 3246 | btnState('button[data-event="bold"]', function () { |
||
| 3247 | return styleInfo['font-bold'] === 'bold'; |
||
| 3248 | }); |
||
| 3249 | btnState('button[data-event="italic"]', function () { |
||
| 3250 | return styleInfo['font-italic'] === 'italic'; |
||
| 3251 | }); |
||
| 3252 | btnState('button[data-event="underline"]', function () { |
||
| 3253 | return styleInfo['font-underline'] === 'underline'; |
||
| 3254 | }); |
||
| 3255 | btnState('button[data-event="strikethrough"]', function () { |
||
| 3256 | return styleInfo['font-strikethrough'] === 'strikethrough'; |
||
| 3257 | }); |
||
| 3258 | btnState('button[data-event="superscript"]', function () { |
||
| 3259 | return styleInfo['font-superscript'] === 'superscript'; |
||
| 3260 | }); |
||
| 3261 | btnState('button[data-event="subscript"]', function () { |
||
| 3262 | return styleInfo['font-subscript'] === 'subscript'; |
||
| 3263 | }); |
||
| 3264 | btnState('button[data-event="justifyLeft"]', function () { |
||
| 3265 | return styleInfo['text-align'] === 'left' || styleInfo['text-align'] === 'start'; |
||
| 3266 | }); |
||
| 3267 | btnState('button[data-event="justifyCenter"]', function () { |
||
| 3268 | return styleInfo['text-align'] === 'center'; |
||
| 3269 | }); |
||
| 3270 | btnState('button[data-event="justifyRight"]', function () { |
||
| 3271 | return styleInfo['text-align'] === 'right'; |
||
| 3272 | }); |
||
| 3273 | btnState('button[data-event="justifyFull"]', function () { |
||
| 3274 | return styleInfo['text-align'] === 'justify'; |
||
| 3275 | }); |
||
| 3276 | btnState('button[data-event="insertUnorderedList"]', function () { |
||
| 3277 | return styleInfo['list-style'] === 'unordered'; |
||
| 3278 | }); |
||
| 3279 | btnState('button[data-event="insertOrderedList"]', function () { |
||
| 3280 | return styleInfo['list-style'] === 'ordered'; |
||
| 3281 | }); |
||
| 3282 | }; |
||
| 3283 | |||
| 3284 | /** |
||
| 3285 | * update recent color |
||
| 3286 | * |
||
| 3287 | * @param {Node} button |
||
| 3288 | * @param {String} eventName |
||
| 3289 | * @param {value} value |
||
| 3290 | */ |
||
| 3291 | this.updateRecentColor = function (button, eventName, value) { |
||
| 3292 | var $color = $(button).closest('.note-color'); |
||
| 3293 | var $recentColor = $color.find('.note-recent-color'); |
||
| 3294 | var colorInfo = JSON.parse($recentColor.attr('data-value')); |
||
| 3295 | colorInfo[eventName] = value; |
||
| 3296 | $recentColor.attr('data-value', JSON.stringify(colorInfo)); |
||
| 3297 | var sKey = eventName === 'backColor' ? 'background-color' : 'color'; |
||
| 3298 | $recentColor.find('i').css(sKey, value); |
||
| 3299 | }; |
||
| 3300 | }; |
||
| 3301 | |||
| 3302 | /** |
||
| 3303 | * Toolbar |
||
| 3304 | */ |
||
| 3305 | var Toolbar = function () { |
||
| 3306 | var button = new Button(); |
||
| 3307 | |||
| 3308 | this.update = function ($toolbar, styleInfo) { |
||
| 3309 | button.update($toolbar, styleInfo); |
||
| 3310 | }; |
||
| 3311 | |||
| 3312 | /** |
||
| 3313 | * @param {Node} button |
||
| 3314 | * @param {String} eventName |
||
| 3315 | * @param {String} value |
||
| 3316 | */ |
||
| 3317 | this.updateRecentColor = function (buttonNode, eventName, value) { |
||
| 3318 | button.updateRecentColor(buttonNode, eventName, value); |
||
| 3319 | }; |
||
| 3320 | |||
| 3321 | /** |
||
| 3322 | * activate buttons exclude codeview |
||
| 3323 | * @param {jQuery} $toolbar |
||
| 3324 | */ |
||
| 3325 | this.activate = function ($toolbar) { |
||
| 3326 | $toolbar.find('button') |
||
| 3327 | .not('button[data-event="codeview"]') |
||
| 3328 | .removeClass('disabled'); |
||
| 3329 | }; |
||
| 3330 | |||
| 3331 | /** |
||
| 3332 | * deactivate buttons exclude codeview |
||
| 3333 | * @param {jQuery} $toolbar |
||
| 3334 | */ |
||
| 3335 | this.deactivate = function ($toolbar) { |
||
| 3336 | $toolbar.find('button') |
||
| 3337 | .not('button[data-event="codeview"]') |
||
| 3338 | .addClass('disabled'); |
||
| 3339 | }; |
||
| 3340 | |||
| 3341 | this.updateFullscreen = function ($container, bFullscreen) { |
||
| 3342 | var $btn = $container.find('button[data-event="fullscreen"]'); |
||
| 3343 | $btn.toggleClass('active', bFullscreen); |
||
| 3344 | }; |
||
| 3345 | |||
| 3346 | this.updateCodeview = function ($container, isCodeview) { |
||
| 3347 | var $btn = $container.find('button[data-event="codeview"]'); |
||
| 3348 | $btn.toggleClass('active', isCodeview); |
||
| 3349 | }; |
||
| 3350 | }; |
||
| 3351 | |||
| 3352 | /** |
||
| 3353 | * Popover (http://getbootstrap.com/javascript/#popovers) |
||
| 3354 | */ |
||
| 3355 | var Popover = function () { |
||
| 3356 | var button = new Button(); |
||
| 3357 | |||
| 3358 | /** |
||
| 3359 | * returns position from placeholder |
||
| 3360 | * @param {Node} placeholder |
||
| 3361 | * @param {Boolean} isAirMode |
||
| 3362 | */ |
||
| 3363 | var posFromPlaceholder = function (placeholder, isAirMode) { |
||
| 3364 | var $placeholder = $(placeholder); |
||
| 3365 | var pos = isAirMode ? $placeholder.offset() : $placeholder.position(); |
||
| 3366 | var height = $placeholder.outerHeight(true); // include margin |
||
| 3367 | |||
| 3368 | // popover below placeholder. |
||
| 3369 | return { |
||
| 3370 | left: pos.left, |
||
| 3371 | top: pos.top + height |
||
| 3372 | }; |
||
| 3373 | }; |
||
| 3374 | |||
| 3375 | /** |
||
| 3376 | * show popover |
||
| 3377 | * @param {jQuery} popover |
||
| 3378 | * @param {Position} pos |
||
| 3379 | */ |
||
| 3380 | var showPopover = function ($popover, pos) { |
||
| 3381 | $popover.css({ |
||
| 3382 | display: 'block', |
||
| 3383 | left: pos.left, |
||
| 3384 | top: pos.top |
||
| 3385 | }); |
||
| 3386 | }; |
||
| 3387 | |||
| 3388 | var PX_POPOVER_ARROW_OFFSET_X = 20; |
||
| 3389 | |||
| 3390 | /** |
||
| 3391 | * update current state |
||
| 3392 | * @param {jQuery} $popover - popover container |
||
| 3393 | * @param {Object} styleInfo - style object |
||
| 3394 | * @param {Boolean} isAirMode |
||
| 3395 | */ |
||
| 3396 | this.update = function ($popover, styleInfo, isAirMode) { |
||
| 3397 | button.update($popover, styleInfo); |
||
| 3398 | |||
| 3399 | var $linkPopover = $popover.find('.note-link-popover'); |
||
| 3400 | if (styleInfo.anchor) { |
||
| 3401 | var $anchor = $linkPopover.find('a'); |
||
| 3402 | var href = $(styleInfo.anchor).attr('href'); |
||
| 3403 | $anchor.attr('href', href).html(href); |
||
| 3404 | showPopover($linkPopover, posFromPlaceholder(styleInfo.anchor, isAirMode)); |
||
| 3405 | } else { |
||
| 3406 | $linkPopover.hide(); |
||
| 3407 | } |
||
| 3408 | |||
| 3409 | var $imagePopover = $popover.find('.note-image-popover'); |
||
| 3410 | if (styleInfo.image) { |
||
| 3411 | showPopover($imagePopover, posFromPlaceholder(styleInfo.image, isAirMode)); |
||
| 3412 | } else { |
||
| 3413 | $imagePopover.hide(); |
||
| 3414 | } |
||
| 3415 | |||
| 3416 | var $airPopover = $popover.find('.note-air-popover'); |
||
| 3417 | if (isAirMode && !styleInfo.range.isCollapsed()) { |
||
| 3418 | var bnd = func.rect2bnd(list.last(styleInfo.range.getClientRects())); |
||
| 3419 | showPopover($airPopover, { |
||
| 3420 | left: Math.max(bnd.left + bnd.width / 2 - PX_POPOVER_ARROW_OFFSET_X, 0), |
||
| 3421 | top: bnd.top + bnd.height |
||
| 3422 | }); |
||
| 3423 | } else { |
||
| 3424 | $airPopover.hide(); |
||
| 3425 | } |
||
| 3426 | }; |
||
| 3427 | |||
| 3428 | /** |
||
| 3429 | * @param {Node} button |
||
| 3430 | * @param {String} eventName |
||
| 3431 | * @param {String} value |
||
| 3432 | */ |
||
| 3433 | this.updateRecentColor = function (button, eventName, value) { |
||
| 3434 | button.updateRecentColor(button, eventName, value); |
||
| 3435 | }; |
||
| 3436 | |||
| 3437 | /** |
||
| 3438 | * hide all popovers |
||
| 3439 | * @param {jQuery} $popover - popover contaienr |
||
| 3440 | */ |
||
| 3441 | this.hide = function ($popover) { |
||
| 3442 | $popover.children().hide(); |
||
| 3443 | }; |
||
| 3444 | }; |
||
| 3445 | |||
| 3446 | /** |
||
| 3447 | * Handle |
||
| 3448 | */ |
||
| 3449 | var Handle = function () { |
||
| 3450 | /** |
||
| 3451 | * update handle |
||
| 3452 | * @param {jQuery} $handle |
||
| 3453 | * @param {Object} styleInfo |
||
| 3454 | * @param {Boolean} isAirMode |
||
| 3455 | */ |
||
| 3456 | this.update = function ($handle, styleInfo, isAirMode) { |
||
| 3457 | var $selection = $handle.find('.note-control-selection'); |
||
| 3458 | if (styleInfo.image) { |
||
| 3459 | var $image = $(styleInfo.image); |
||
| 3460 | var pos = isAirMode ? $image.offset() : $image.position(); |
||
| 3461 | |||
| 3462 | // include margin |
||
| 3463 | var imageSize = { |
||
| 3464 | w: $image.outerWidth(true), |
||
| 3465 | h: $image.outerHeight(true) |
||
| 3466 | }; |
||
| 3467 | |||
| 3468 | $selection.css({ |
||
| 3469 | display: 'block', |
||
| 3470 | left: pos.left, |
||
| 3471 | top: pos.top, |
||
| 3472 | width: imageSize.w, |
||
| 3473 | height: imageSize.h |
||
| 3474 | }).data('target', styleInfo.image); // save current image element. |
||
| 3475 | var sizingText = imageSize.w + 'x' + imageSize.h; |
||
| 3476 | $selection.find('.note-control-selection-info').text(sizingText); |
||
| 3477 | } else { |
||
| 3478 | $selection.hide(); |
||
| 3479 | } |
||
| 3480 | }; |
||
| 3481 | |||
| 3482 | this.hide = function ($handle) { |
||
| 3483 | $handle.children().hide(); |
||
| 3484 | }; |
||
| 3485 | }; |
||
| 3486 | |||
| 3487 | /** |
||
| 3488 | * Dialog |
||
| 3489 | * |
||
| 3490 | * @class |
||
| 3491 | */ |
||
| 3492 | var Dialog = function () { |
||
| 3493 | |||
| 3494 | /** |
||
| 3495 | * toggle button status |
||
| 3496 | * |
||
| 3497 | * @param {jQuery} $btn |
||
| 3498 | * @param {Boolean} isEnable |
||
| 3499 | */ |
||
| 3500 | var toggleBtn = function ($btn, isEnable) { |
||
| 3501 | $btn.toggleClass('disabled', !isEnable); |
||
| 3502 | $btn.attr('disabled', !isEnable); |
||
| 3503 | }; |
||
| 3504 | |||
| 3505 | /** |
||
| 3506 | * show image dialog |
||
| 3507 | * |
||
| 3508 | * @param {jQuery} $editable |
||
| 3509 | * @param {jQuery} $dialog |
||
| 3510 | * @return {Promise} |
||
| 3511 | */ |
||
| 3512 | this.showImageDialog = function ($editable, $dialog) { |
||
| 3513 | return $.Deferred(function (deferred) { |
||
| 3514 | var $imageDialog = $dialog.find('.note-image-dialog'); |
||
| 3515 | |||
| 3516 | var $imageInput = $dialog.find('.note-image-input'), |
||
| 3517 | $imageUrl = $dialog.find('.note-image-url'), |
||
| 3518 | $imageBtn = $dialog.find('.note-image-btn'); |
||
| 3519 | |||
| 3520 | $imageDialog.one('shown.bs.modal', function () { |
||
| 3521 | // Cloning imageInput to clear element. |
||
| 3522 | $imageInput.replaceWith($imageInput.clone() |
||
| 3523 | .on('change', function () { |
||
| 3524 | deferred.resolve(this.files); |
||
| 3525 | $imageDialog.modal('hide'); |
||
| 3526 | }) |
||
| 3527 | .val('') |
||
| 3528 | ); |
||
| 3529 | |||
| 3530 | $imageBtn.click(function (event) { |
||
| 3531 | event.preventDefault(); |
||
| 3532 | |||
| 3533 | deferred.resolve($imageUrl.val()); |
||
| 3534 | $imageDialog.modal('hide'); |
||
| 3535 | }); |
||
| 3536 | |||
| 3537 | $imageUrl.on('keyup paste', function (event) { |
||
| 3538 | var url; |
||
| 3539 | |||
| 3540 | if (event.type === 'paste') { |
||
| 3541 | url = event.originalEvent.clipboardData.getData('text'); |
||
| 3542 | } else { |
||
| 3543 | url = $imageUrl.val(); |
||
| 3544 | } |
||
| 3545 | |||
| 3546 | toggleBtn($imageBtn, url); |
||
| 3547 | }).val('').trigger('focus'); |
||
| 3548 | }).one('hidden.bs.modal', function () { |
||
| 3549 | $imageInput.off('change'); |
||
| 3550 | $imageUrl.off('keyup paste'); |
||
| 3551 | $imageBtn.off('click'); |
||
| 3552 | |||
| 3553 | if (deferred.state() === 'pending') { |
||
| 3554 | deferred.reject(); |
||
| 3555 | } |
||
| 3556 | }).modal('show'); |
||
| 3557 | }); |
||
| 3558 | }; |
||
| 3559 | |||
| 3560 | /** |
||
| 3561 | * Show video dialog and set event handlers on dialog controls. |
||
| 3562 | * |
||
| 3563 | * @param {jQuery} $dialog |
||
| 3564 | * @param {Object} videoInfo |
||
| 3565 | * @return {Promise} |
||
| 3566 | */ |
||
| 3567 | this.showVideoDialog = function ($editable, $dialog, videoInfo) { |
||
| 3568 | return $.Deferred(function (deferred) { |
||
| 3569 | var $videoDialog = $dialog.find('.note-video-dialog'); |
||
| 3570 | var $videoUrl = $videoDialog.find('.note-video-url'), |
||
| 3571 | $videoBtn = $videoDialog.find('.note-video-btn'); |
||
| 3572 | |||
| 3573 | $videoDialog.one('shown.bs.modal', function () { |
||
| 3574 | $videoUrl.val(videoInfo.text).keyup(function () { |
||
| 3575 | toggleBtn($videoBtn, $videoUrl.val()); |
||
| 3576 | }).trigger('keyup').trigger('focus'); |
||
| 3577 | |||
| 3578 | $videoBtn.click(function (event) { |
||
| 3579 | event.preventDefault(); |
||
| 3580 | |||
| 3581 | deferred.resolve($videoUrl.val()); |
||
| 3582 | $videoDialog.modal('hide'); |
||
| 3583 | }); |
||
| 3584 | }).one('hidden.bs.modal', function () { |
||
| 3585 | // dettach events |
||
| 3586 | $videoUrl.off('keyup'); |
||
| 3587 | $videoBtn.off('click'); |
||
| 3588 | |||
| 3589 | if (deferred.state() === 'pending') { |
||
| 3590 | deferred.reject(); |
||
| 3591 | } |
||
| 3592 | }).modal('show'); |
||
| 3593 | }); |
||
| 3594 | }; |
||
| 3595 | |||
| 3596 | /** |
||
| 3597 | * Show link dialog and set event handlers on dialog controls. |
||
| 3598 | * |
||
| 3599 | * @param {jQuery} $dialog |
||
| 3600 | * @param {Object} linkInfo |
||
| 3601 | * @return {Promise} |
||
| 3602 | */ |
||
| 3603 | this.showLinkDialog = function ($editable, $dialog, linkInfo) { |
||
| 3604 | return $.Deferred(function (deferred) { |
||
| 3605 | var $linkDialog = $dialog.find('.note-link-dialog'); |
||
| 3606 | |||
| 3607 | var $linkText = $linkDialog.find('.note-link-text'), |
||
| 3608 | $linkUrl = $linkDialog.find('.note-link-url'), |
||
| 3609 | $linkBtn = $linkDialog.find('.note-link-btn'), |
||
| 3610 | $openInNewWindow = $linkDialog.find('input[type=checkbox]'); |
||
| 3611 | |||
| 3612 | $linkDialog.one('shown.bs.modal', function () { |
||
| 3613 | $linkText.val(linkInfo.text); |
||
| 3614 | |||
| 3615 | $linkText.keyup(function () { |
||
| 3616 | // if linktext was modified by keyup, |
||
| 3617 | // stop cloning text from linkUrl |
||
| 3618 | linkInfo.text = $linkText.val(); |
||
| 3619 | }); |
||
| 3620 | |||
| 3621 | // if no url was given, copy text to url |
||
| 3622 | if (!linkInfo.url) { |
||
| 3623 | linkInfo.url = linkInfo.text; |
||
| 3624 | toggleBtn($linkBtn, linkInfo.text); |
||
| 3625 | } |
||
| 3626 | |||
| 3627 | $linkUrl.keyup(function () { |
||
| 3628 | toggleBtn($linkBtn, $linkUrl.val()); |
||
| 3629 | // display same link on `Text to display` input |
||
| 3630 | // when create a new link |
||
| 3631 | if (!linkInfo.text) { |
||
| 3632 | $linkText.val($linkUrl.val()); |
||
| 3633 | } |
||
| 3634 | }).val(linkInfo.url).trigger('focus').trigger('select'); |
||
| 3635 | |||
| 3636 | $openInNewWindow.prop('checked', linkInfo.newWindow); |
||
| 3637 | |||
| 3638 | $linkBtn.one('click', function (event) { |
||
| 3639 | event.preventDefault(); |
||
| 3640 | |||
| 3641 | deferred.resolve({ |
||
| 3642 | range: linkInfo.range, |
||
| 3643 | url: $linkUrl.val(), |
||
| 3644 | text: $linkText.val(), |
||
| 3645 | newWindow: $openInNewWindow.is(':checked') |
||
| 3646 | }); |
||
| 3647 | $linkDialog.modal('hide'); |
||
| 3648 | }); |
||
| 3649 | }).one('hidden.bs.modal', function () { |
||
| 3650 | // dettach events |
||
| 3651 | $linkText.off('keyup'); |
||
| 3652 | $linkUrl.off('keyup'); |
||
| 3653 | $linkBtn.off('click'); |
||
| 3654 | |||
| 3655 | if (deferred.state() === 'pending') { |
||
| 3656 | deferred.reject(); |
||
| 3657 | } |
||
| 3658 | }).modal('show'); |
||
| 3659 | }).promise(); |
||
| 3660 | }; |
||
| 3661 | |||
| 3662 | /** |
||
| 3663 | * show help dialog |
||
| 3664 | * |
||
| 3665 | * @param {jQuery} $dialog |
||
| 3666 | */ |
||
| 3667 | this.showHelpDialog = function ($editable, $dialog) { |
||
| 3668 | return $.Deferred(function (deferred) { |
||
| 3669 | var $helpDialog = $dialog.find('.note-help-dialog'); |
||
| 3670 | |||
| 3671 | $helpDialog.one('hidden.bs.modal', function () { |
||
| 3672 | deferred.resolve(); |
||
| 3673 | }).modal('show'); |
||
| 3674 | }).promise(); |
||
| 3675 | }; |
||
| 3676 | }; |
||
| 3677 | |||
| 3678 | |||
| 3679 | var CodeMirror; |
||
| 3680 | if (agent.hasCodeMirror) { |
||
| 3681 | if (agent.isSupportAmd) { |
||
| 3682 | require(['CodeMirror'], function (cm) { |
||
| 3683 | CodeMirror = cm; |
||
| 3684 | }); |
||
| 3685 | } else { |
||
| 3686 | CodeMirror = window.CodeMirror; |
||
| 3687 | } |
||
| 3688 | } |
||
| 3689 | |||
| 3690 | /** |
||
| 3691 | * EventHandler |
||
| 3692 | */ |
||
| 3693 | var EventHandler = function () { |
||
| 3694 | var $window = $(window); |
||
| 3695 | var $document = $(document); |
||
| 3696 | var $scrollbar = $('html, body'); |
||
| 3697 | |||
| 3698 | var editor = new Editor(); |
||
| 3699 | var toolbar = new Toolbar(), popover = new Popover(); |
||
| 3700 | var handle = new Handle(), dialog = new Dialog(); |
||
| 3701 | |||
| 3702 | /** |
||
| 3703 | * returns makeLayoutInfo from editor's descendant node. |
||
| 3704 | * |
||
| 3705 | * @param {Node} descendant |
||
| 3706 | * @returns {Object} |
||
| 3707 | */ |
||
| 3708 | var makeLayoutInfo = function (descendant) { |
||
| 3709 | var $target = $(descendant).closest('.note-editor, .note-air-editor, .note-air-layout'); |
||
| 3710 | |||
| 3711 | if (!$target.length) { return null; } |
||
| 3712 | |||
| 3713 | var $editor; |
||
| 3714 | if ($target.is('.note-editor, .note-air-editor')) { |
||
| 3715 | $editor = $target; |
||
| 3716 | } else { |
||
| 3717 | $editor = $('#note-editor-' + list.last($target.attr('id').split('-'))); |
||
| 3718 | } |
||
| 3719 | |||
| 3720 | return dom.buildLayoutInfo($editor); |
||
| 3721 | }; |
||
| 3722 | |||
| 3723 | /** |
||
| 3724 | * insert Images from file array. |
||
| 3725 | * |
||
| 3726 | * @param {jQuery} $editable |
||
| 3727 | * @param {File[]} files |
||
| 3728 | */ |
||
| 3729 | var insertImages = function ($editable, files) { |
||
| 3730 | var callbacks = $editable.data('callbacks'); |
||
| 3731 | |||
| 3732 | // If onImageUpload options setted |
||
| 3733 | if (callbacks.onImageUpload) { |
||
| 3734 | callbacks.onImageUpload(files, editor, $editable); |
||
| 3735 | // else insert Image as dataURL |
||
| 3736 | } else { |
||
| 3737 | $.each(files, function (idx, file) { |
||
| 3738 | var filename = file.name; |
||
| 3739 | async.readFileAsDataURL(file).then(function (sDataURL) { |
||
| 3740 | editor.insertImage($editable, sDataURL, filename); |
||
| 3741 | }).fail(function () { |
||
| 3742 | if (callbacks.onImageUploadError) { |
||
| 3743 | callbacks.onImageUploadError(); |
||
| 3744 | } |
||
| 3745 | }); |
||
| 3746 | }); |
||
| 3747 | } |
||
| 3748 | }; |
||
| 3749 | |||
| 3750 | var commands = { |
||
| 3751 | /** |
||
| 3752 | * @param {Object} layoutInfo |
||
| 3753 | */ |
||
| 3754 | showLinkDialog: function (layoutInfo) { |
||
| 3755 | var $editor = layoutInfo.editor(), |
||
| 3756 | $dialog = layoutInfo.dialog(), |
||
| 3757 | $editable = layoutInfo.editable(), |
||
| 3758 | linkInfo = editor.getLinkInfo($editable); |
||
| 3759 | |||
| 3760 | var options = $editor.data('options'); |
||
| 3761 | |||
| 3762 | editor.saveRange($editable); |
||
| 3763 | dialog.showLinkDialog($editable, $dialog, linkInfo).then(function (linkInfo) { |
||
| 3764 | editor.restoreRange($editable); |
||
| 3765 | editor.createLink($editable, linkInfo, options); |
||
| 3766 | // hide popover after creating link |
||
| 3767 | popover.hide(layoutInfo.popover()); |
||
| 3768 | }).fail(function () { |
||
| 3769 | editor.restoreRange($editable); |
||
| 3770 | }); |
||
| 3771 | }, |
||
| 3772 | |||
| 3773 | /** |
||
| 3774 | * @param {Object} layoutInfo |
||
| 3775 | */ |
||
| 3776 | showImageDialog: function (layoutInfo) { |
||
| 3777 | var $dialog = layoutInfo.dialog(), |
||
| 3778 | $editable = layoutInfo.editable(); |
||
| 3779 | |||
| 3780 | editor.saveRange($editable); |
||
| 3781 | dialog.showImageDialog($editable, $dialog).then(function (data) { |
||
| 3782 | editor.restoreRange($editable); |
||
| 3783 | |||
| 3784 | if (typeof data === 'string') { |
||
| 3785 | // image url |
||
| 3786 | editor.insertImage($editable, data); |
||
| 3787 | } else { |
||
| 3788 | // array of files |
||
| 3789 | insertImages($editable, data); |
||
| 3790 | } |
||
| 3791 | }).fail(function () { |
||
| 3792 | editor.restoreRange($editable); |
||
| 3793 | }); |
||
| 3794 | }, |
||
| 3795 | |||
| 3796 | /** |
||
| 3797 | * @param {Object} layoutInfo |
||
| 3798 | */ |
||
| 3799 | showVideoDialog: function (layoutInfo) { |
||
| 3800 | var $dialog = layoutInfo.dialog(), |
||
| 3801 | $editable = layoutInfo.editable(), |
||
| 3802 | videoInfo = editor.getVideoInfo($editable); |
||
| 3803 | |||
| 3804 | editor.saveRange($editable); |
||
| 3805 | dialog.showVideoDialog($editable, $dialog, videoInfo).then(function (sUrl) { |
||
| 3806 | editor.restoreRange($editable); |
||
| 3807 | editor.insertVideo($editable, sUrl); |
||
| 3808 | }).fail(function () { |
||
| 3809 | editor.restoreRange($editable); |
||
| 3810 | }); |
||
| 3811 | }, |
||
| 3812 | |||
| 3813 | /** |
||
| 3814 | * @param {Object} layoutInfo |
||
| 3815 | */ |
||
| 3816 | showHelpDialog: function (layoutInfo) { |
||
| 3817 | var $dialog = layoutInfo.dialog(), |
||
| 3818 | $editable = layoutInfo.editable(); |
||
| 3819 | |||
| 3820 | editor.saveRange($editable, true); |
||
| 3821 | dialog.showHelpDialog($editable, $dialog).then(function () { |
||
| 3822 | editor.restoreRange($editable); |
||
| 3823 | }); |
||
| 3824 | }, |
||
| 3825 | |||
| 3826 | fullscreen: function (layoutInfo) { |
||
| 3827 | var $editor = layoutInfo.editor(), |
||
| 3828 | $toolbar = layoutInfo.toolbar(), |
||
| 3829 | $editable = layoutInfo.editable(), |
||
| 3830 | $codable = layoutInfo.codable(); |
||
| 3831 | |||
| 3832 | var options = $editor.data('options'); |
||
| 3833 | |||
| 3834 | var resize = function (size) { |
||
| 3835 | $editor.css('width', size.w); |
||
| 3836 | $editable.css('height', size.h); |
||
| 3837 | $codable.css('height', size.h); |
||
| 3838 | if ($codable.data('cmeditor')) { |
||
| 3839 | $codable.data('cmeditor').setsize(null, size.h); |
||
| 3840 | } |
||
| 3841 | }; |
||
| 3842 | |||
| 3843 | $editor.toggleClass('fullscreen'); |
||
| 3844 | var isFullscreen = $editor.hasClass('fullscreen'); |
||
| 3845 | if (isFullscreen) { |
||
| 3846 | $editable.data('orgheight', $editable.css('height')); |
||
| 3847 | |||
| 3848 | $window.on('resize', function () { |
||
| 3849 | resize({ |
||
| 3850 | w: $window.width(), |
||
| 3851 | h: $window.height() - $toolbar.outerHeight() |
||
| 3852 | }); |
||
| 3853 | }).trigger('resize'); |
||
| 3854 | |||
| 3855 | $scrollbar.css('overflow', 'hidden'); |
||
| 3856 | } else { |
||
| 3857 | $window.off('resize'); |
||
| 3858 | resize({ |
||
| 3859 | w: options.width || '', |
||
| 3860 | h: $editable.data('orgheight') |
||
| 3861 | }); |
||
| 3862 | $scrollbar.css('overflow', 'visible'); |
||
| 3863 | } |
||
| 3864 | |||
| 3865 | toolbar.updateFullscreen($toolbar, isFullscreen); |
||
| 3866 | }, |
||
| 3867 | |||
| 3868 | codeview: function (layoutInfo) { |
||
| 3869 | var $editor = layoutInfo.editor(), |
||
| 3870 | $toolbar = layoutInfo.toolbar(), |
||
| 3871 | $editable = layoutInfo.editable(), |
||
| 3872 | $codable = layoutInfo.codable(), |
||
| 3873 | $popover = layoutInfo.popover(); |
||
| 3874 | |||
| 3875 | var options = $editor.data('options'); |
||
| 3876 | |||
| 3877 | var cmEditor, server; |
||
| 3878 | |||
| 3879 | $editor.toggleClass('codeview'); |
||
| 3880 | |||
| 3881 | var isCodeview = $editor.hasClass('codeview'); |
||
| 3882 | if (isCodeview) { |
||
| 3883 | $codable.val(dom.html($editable, true)); |
||
| 3884 | $codable.height($editable.height()); |
||
| 3885 | toolbar.deactivate($toolbar); |
||
| 3886 | popover.hide($popover); |
||
| 3887 | $codable.focus(); |
||
| 3888 | |||
| 3889 | // activate CodeMirror as codable |
||
| 3890 | if (agent.hasCodeMirror) { |
||
| 3891 | cmEditor = CodeMirror.fromTextArea($codable[0], options.codemirror); |
||
| 3892 | |||
| 3893 | // CodeMirror TernServer |
||
| 3894 | if (options.codemirror.tern) { |
||
| 3895 | server = new CodeMirror.TernServer(options.codemirror.tern); |
||
| 3896 | cmEditor.ternServer = server; |
||
| 3897 | cmEditor.on('cursorActivity', function (cm) { |
||
| 3898 | server.updateArgHints(cm); |
||
| 3899 | }); |
||
| 3900 | } |
||
| 3901 | |||
| 3902 | // CodeMirror hasn't Padding. |
||
| 3903 | cmEditor.setSize(null, $editable.outerHeight()); |
||
| 3904 | $codable.data('cmEditor', cmEditor); |
||
| 3905 | } |
||
| 3906 | } else { |
||
| 3907 | // deactivate CodeMirror as codable |
||
| 3908 | if (agent.hasCodeMirror) { |
||
| 3909 | cmEditor = $codable.data('cmEditor'); |
||
| 3910 | $codable.val(cmEditor.getValue()); |
||
| 3911 | cmEditor.toTextArea(); |
||
| 3912 | } |
||
| 3913 | |||
| 3914 | $editable.html(dom.value($codable) || dom.emptyPara); |
||
| 3915 | $editable.height(options.height ? $codable.height() : 'auto'); |
||
| 3916 | |||
| 3917 | toolbar.activate($toolbar); |
||
| 3918 | $editable.focus(); |
||
| 3919 | } |
||
| 3920 | |||
| 3921 | toolbar.updateCodeview(layoutInfo.toolbar(), isCodeview); |
||
| 3922 | } |
||
| 3923 | }; |
||
| 3924 | |||
| 3925 | var hMousedown = function (event) { |
||
| 3926 | //preventDefault Selection for FF, IE8+ |
||
| 3927 | if (dom.isImg(event.target)) { |
||
| 3928 | event.preventDefault(); |
||
| 3929 | } |
||
| 3930 | }; |
||
| 3931 | |||
| 3932 | var hToolbarAndPopoverUpdate = function (event) { |
||
| 3933 | // delay for range after mouseup |
||
| 3934 | setTimeout(function () { |
||
| 3935 | var layoutInfo = makeLayoutInfo(event.currentTarget || event.target); |
||
| 3936 | var styleInfo = editor.currentStyle(event.target); |
||
| 3937 | if (!styleInfo) { return; } |
||
| 3938 | |||
| 3939 | var isAirMode = layoutInfo.editor().data('options').airMode; |
||
| 3940 | if (!isAirMode) { |
||
| 3941 | toolbar.update(layoutInfo.toolbar(), styleInfo); |
||
| 3942 | } |
||
| 3943 | |||
| 3944 | popover.update(layoutInfo.popover(), styleInfo, isAirMode); |
||
| 3945 | handle.update(layoutInfo.handle(), styleInfo, isAirMode); |
||
| 3946 | }, 0); |
||
| 3947 | }; |
||
| 3948 | |||
| 3949 | var hScroll = function (event) { |
||
| 3950 | var layoutInfo = makeLayoutInfo(event.currentTarget || event.target); |
||
| 3951 | //hide popover and handle when scrolled |
||
| 3952 | popover.hide(layoutInfo.popover()); |
||
| 3953 | handle.hide(layoutInfo.handle()); |
||
| 3954 | }; |
||
| 3955 | |||
| 3956 | /** |
||
| 3957 | * paste clipboard image |
||
| 3958 | * |
||
| 3959 | * @param {Event} event |
||
| 3960 | */ |
||
| 3961 | var hPasteClipboardImage = function (event) { |
||
| 3962 | var clipboardData = event.originalEvent.clipboardData; |
||
| 3963 | if (!clipboardData || !clipboardData.items || !clipboardData.items.length) { |
||
| 3964 | return; |
||
| 3965 | } |
||
| 3966 | |||
| 3967 | var layoutInfo = makeLayoutInfo(event.currentTarget || event.target), |
||
| 3968 | $editable = layoutInfo.editable(); |
||
| 3969 | |||
| 3970 | var item = list.head(clipboardData.items); |
||
| 3971 | var isClipboardImage = item.kind === 'file' && item.type.indexOf('image/') !== -1; |
||
| 3972 | |||
| 3973 | if (isClipboardImage) { |
||
| 3974 | insertImages($editable, [item.getAsFile()]); |
||
| 3975 | } |
||
| 3976 | |||
| 3977 | editor.afterCommand($editable); |
||
| 3978 | }; |
||
| 3979 | |||
| 3980 | /** |
||
| 3981 | * `mousedown` event handler on $handle |
||
| 3982 | * - controlSizing: resize image |
||
| 3983 | * |
||
| 3984 | * @param {MouseEvent} event |
||
| 3985 | */ |
||
| 3986 | var hHandleMousedown = function (event) { |
||
| 3987 | if (dom.isControlSizing(event.target)) { |
||
| 3988 | event.preventDefault(); |
||
| 3989 | event.stopPropagation(); |
||
| 3990 | |||
| 3991 | var layoutInfo = makeLayoutInfo(event.target), |
||
| 3992 | $handle = layoutInfo.handle(), $popover = layoutInfo.popover(), |
||
| 3993 | $editable = layoutInfo.editable(), |
||
| 3994 | $editor = layoutInfo.editor(); |
||
| 3995 | |||
| 3996 | var target = $handle.find('.note-control-selection').data('target'), |
||
| 3997 | $target = $(target), posStart = $target.offset(), |
||
| 3998 | scrollTop = $document.scrollTop(); |
||
| 3999 | |||
| 4000 | var isAirMode = $editor.data('options').airMode; |
||
| 4001 | |||
| 4002 | $document.on('mousemove', function (event) { |
||
| 4003 | editor.resizeTo({ |
||
| 4004 | x: event.clientX - posStart.left, |
||
| 4005 | y: event.clientY - (posStart.top - scrollTop) |
||
| 4006 | }, $target, !event.shiftKey); |
||
| 4007 | |||
| 4008 | handle.update($handle, {image: target}, isAirMode); |
||
| 4009 | popover.update($popover, {image: target}, isAirMode); |
||
| 4010 | }).one('mouseup', function () { |
||
| 4011 | $document.off('mousemove'); |
||
| 4012 | }); |
||
| 4013 | |||
| 4014 | if (!$target.data('ratio')) { // original ratio. |
||
| 4015 | $target.data('ratio', $target.height() / $target.width()); |
||
| 4016 | } |
||
| 4017 | |||
| 4018 | editor.afterCommand($editable); |
||
| 4019 | } |
||
| 4020 | }; |
||
| 4021 | |||
| 4022 | var hToolbarAndPopoverMousedown = function (event) { |
||
| 4023 | // prevent default event when insertTable (FF, Webkit) |
||
| 4024 | var $btn = $(event.target).closest('[data-event]'); |
||
| 4025 | if ($btn.length) { |
||
| 4026 | event.preventDefault(); |
||
| 4027 | } |
||
| 4028 | }; |
||
| 4029 | |||
| 4030 | var hToolbarAndPopoverClick = function (event) { |
||
| 4031 | var $btn = $(event.target).closest('[data-event]'); |
||
| 4032 | |||
| 4033 | if ($btn.length) { |
||
| 4034 | var eventName = $btn.attr('data-event'), |
||
| 4035 | value = $btn.attr('data-value'), |
||
| 4036 | hide = $btn.attr('data-hide'); |
||
| 4037 | |||
| 4038 | var layoutInfo = makeLayoutInfo(event.target); |
||
| 4039 | |||
| 4040 | event.preventDefault(); |
||
| 4041 | |||
| 4042 | // before command: detect control selection element($target) |
||
| 4043 | var $target; |
||
| 4044 | if ($.inArray(eventName, ['resize', 'floatMe', 'removeMedia', 'imageShape']) !== -1) { |
||
| 4045 | var $selection = layoutInfo.handle().find('.note-control-selection'); |
||
| 4046 | $target = $($selection.data('target')); |
||
| 4047 | } |
||
| 4048 | |||
| 4049 | // If requested, hide the popover when the button is clicked. |
||
| 4050 | // Useful for things like showHelpDialog. |
||
| 4051 | if (hide) { |
||
| 4052 | $btn.parents('.popover').hide(); |
||
| 4053 | } |
||
| 4054 | |||
| 4055 | if (editor[eventName]) { // on command |
||
| 4056 | var $editable = layoutInfo.editable(); |
||
| 4057 | $editable.trigger('focus'); |
||
| 4058 | editor[eventName]($editable, value, $target); |
||
| 4059 | } else if (commands[eventName]) { |
||
| 4060 | commands[eventName].call(this, layoutInfo); |
||
| 4061 | } |
||
| 4062 | |||
| 4063 | // after command |
||
| 4064 | if ($.inArray(eventName, ['backColor', 'foreColor']) !== -1) { |
||
| 4065 | var options = layoutInfo.editor().data('options', options); |
||
| 4066 | var module = options.airMode ? popover : toolbar; |
||
| 4067 | module.updateRecentColor(list.head($btn), eventName, value); |
||
| 4068 | } |
||
| 4069 | |||
| 4070 | hToolbarAndPopoverUpdate(event); |
||
| 4071 | } |
||
| 4072 | }; |
||
| 4073 | |||
| 4074 | var EDITABLE_PADDING = 24; |
||
| 4075 | /** |
||
| 4076 | * `mousedown` event handler on statusbar |
||
| 4077 | * |
||
| 4078 | * @param {MouseEvent} event |
||
| 4079 | */ |
||
| 4080 | var hStatusbarMousedown = function (event) { |
||
| 4081 | event.preventDefault(); |
||
| 4082 | event.stopPropagation(); |
||
| 4083 | |||
| 4084 | var $editable = makeLayoutInfo(event.target).editable(); |
||
| 4085 | var nEditableTop = $editable.offset().top - $document.scrollTop(); |
||
| 4086 | |||
| 4087 | var layoutInfo = makeLayoutInfo(event.currentTarget || event.target); |
||
| 4088 | var options = layoutInfo.editor().data('options'); |
||
| 4089 | |||
| 4090 | $document.on('mousemove', function (event) { |
||
| 4091 | var nHeight = event.clientY - (nEditableTop + EDITABLE_PADDING); |
||
| 4092 | |||
| 4093 | nHeight = (options.minHeight > 0) ? Math.max(nHeight, options.minHeight) : nHeight; |
||
| 4094 | nHeight = (options.maxHeight > 0) ? Math.min(nHeight, options.maxHeight) : nHeight; |
||
| 4095 | |||
| 4096 | $editable.height(nHeight); |
||
| 4097 | }).one('mouseup', function () { |
||
| 4098 | $document.off('mousemove'); |
||
| 4099 | }); |
||
| 4100 | }; |
||
| 4101 | |||
| 4102 | var PX_PER_EM = 18; |
||
| 4103 | var hDimensionPickerMove = function (event, options) { |
||
| 4104 | var $picker = $(event.target.parentNode); // target is mousecatcher |
||
| 4105 | var $dimensionDisplay = $picker.next(); |
||
| 4106 | var $catcher = $picker.find('.note-dimension-picker-mousecatcher'); |
||
| 4107 | var $highlighted = $picker.find('.note-dimension-picker-highlighted'); |
||
| 4108 | var $unhighlighted = $picker.find('.note-dimension-picker-unhighlighted'); |
||
| 4109 | |||
| 4110 | var posOffset; |
||
| 4111 | // HTML5 with jQuery - e.offsetX is undefined in Firefox |
||
| 4112 | if (event.offsetX === undefined) { |
||
| 4113 | var posCatcher = $(event.target).offset(); |
||
| 4114 | posOffset = { |
||
| 4115 | x: event.pageX - posCatcher.left, |
||
| 4116 | y: event.pageY - posCatcher.top |
||
| 4117 | }; |
||
| 4118 | } else { |
||
| 4119 | posOffset = { |
||
| 4120 | x: event.offsetX, |
||
| 4121 | y: event.offsetY |
||
| 4122 | }; |
||
| 4123 | } |
||
| 4124 | |||
| 4125 | var dim = { |
||
| 4126 | c: Math.ceil(posOffset.x / PX_PER_EM) || 1, |
||
| 4127 | r: Math.ceil(posOffset.y / PX_PER_EM) || 1 |
||
| 4128 | }; |
||
| 4129 | |||
| 4130 | $highlighted.css({ width: dim.c + 'em', height: dim.r + 'em' }); |
||
| 4131 | $catcher.attr('data-value', dim.c + 'x' + dim.r); |
||
| 4132 | |||
| 4133 | if (3 < dim.c && dim.c < options.insertTableMaxSize.col) { |
||
| 4134 | $unhighlighted.css({ width: dim.c + 1 + 'em'}); |
||
| 4135 | } |
||
| 4136 | |||
| 4137 | if (3 < dim.r && dim.r < options.insertTableMaxSize.row) { |
||
| 4138 | $unhighlighted.css({ height: dim.r + 1 + 'em'}); |
||
| 4139 | } |
||
| 4140 | |||
| 4141 | $dimensionDisplay.html(dim.c + ' x ' + dim.r); |
||
| 4142 | }; |
||
| 4143 | |||
| 4144 | /** |
||
| 4145 | * Drag and Drop Events |
||
| 4146 | * |
||
| 4147 | * @param {Object} layoutInfo - layout Informations |
||
| 4148 | * @param {Boolean} disableDragAndDrop |
||
| 4149 | */ |
||
| 4150 | var handleDragAndDropEvent = function (layoutInfo, disableDragAndDrop) { |
||
| 4151 | if (disableDragAndDrop) { |
||
| 4152 | // prevent default drop event |
||
| 4153 | $document.on('drop', function (e) { |
||
| 4154 | e.preventDefault(); |
||
| 4155 | }); |
||
| 4156 | } else { |
||
| 4157 | attachDragAndDropEvent(layoutInfo); |
||
| 4158 | } |
||
| 4159 | }; |
||
| 4160 | |||
| 4161 | /** |
||
| 4162 | * attach Drag and Drop Events |
||
| 4163 | * |
||
| 4164 | * @param {Object} layoutInfo - layout Informations |
||
| 4165 | */ |
||
| 4166 | var attachDragAndDropEvent = function (layoutInfo) { |
||
| 4167 | var collection = $(), |
||
| 4168 | $dropzone = layoutInfo.dropzone, |
||
| 4169 | $dropzoneMessage = layoutInfo.dropzone.find('.note-dropzone-message'); |
||
| 4170 | |||
| 4171 | // show dropzone on dragenter when dragging a object to document. |
||
| 4172 | $document.on('dragenter', function (e) { |
||
| 4173 | var isCodeview = layoutInfo.editor.hasClass('codeview'); |
||
| 4174 | if (!isCodeview && !collection.length) { |
||
| 4175 | layoutInfo.editor.addClass('dragover'); |
||
| 4176 | $dropzone.width(layoutInfo.editor.width()); |
||
| 4177 | $dropzone.height(layoutInfo.editor.height()); |
||
| 4178 | $dropzoneMessage.text('Drag Image Here'); |
||
| 4179 | } |
||
| 4180 | collection = collection.add(e.target); |
||
| 4181 | }).on('dragleave', function (e) { |
||
| 4182 | collection = collection.not(e.target); |
||
| 4183 | if (!collection.length) { |
||
| 4184 | layoutInfo.editor.removeClass('dragover'); |
||
| 4185 | } |
||
| 4186 | }).on('drop', function () { |
||
| 4187 | collection = $(); |
||
| 4188 | layoutInfo.editor.removeClass('dragover'); |
||
| 4189 | }); |
||
| 4190 | |||
| 4191 | // change dropzone's message on hover. |
||
| 4192 | $dropzone.on('dragenter', function () { |
||
| 4193 | $dropzone.addClass('hover'); |
||
| 4194 | $dropzoneMessage.text('Drop Image'); |
||
| 4195 | }).on('dragleave', function () { |
||
| 4196 | $dropzone.removeClass('hover'); |
||
| 4197 | $dropzoneMessage.text('Drag Image Here'); |
||
| 4198 | }); |
||
| 4199 | |||
| 4200 | // attach dropImage |
||
| 4201 | $dropzone.on('drop', function (event) { |
||
| 4202 | event.preventDefault(); |
||
| 4203 | |||
| 4204 | var dataTransfer = event.originalEvent.dataTransfer; |
||
| 4205 | if (dataTransfer && dataTransfer.files) { |
||
| 4206 | var layoutInfo = makeLayoutInfo(event.currentTarget || event.target); |
||
| 4207 | layoutInfo.editable().focus(); |
||
| 4208 | insertImages(layoutInfo.editable(), dataTransfer.files); |
||
| 4209 | } |
||
| 4210 | }).on('dragover', false); // prevent default dragover event |
||
| 4211 | }; |
||
| 4212 | |||
| 4213 | |||
| 4214 | /** |
||
| 4215 | * bind KeyMap on keydown |
||
| 4216 | * |
||
| 4217 | * @param {Object} layoutInfo |
||
| 4218 | * @param {Object} keyMap |
||
| 4219 | */ |
||
| 4220 | this.bindKeyMap = function (layoutInfo, keyMap) { |
||
| 4221 | var $editor = layoutInfo.editor; |
||
| 4222 | var $editable = layoutInfo.editable; |
||
| 4223 | |||
| 4224 | layoutInfo = makeLayoutInfo($editable); |
||
| 4225 | |||
| 4226 | $editable.on('keydown', function (event) { |
||
| 4227 | var aKey = []; |
||
| 4228 | |||
| 4229 | // modifier |
||
| 4230 | if (event.metaKey) { aKey.push('CMD'); } |
||
| 4231 | if (event.ctrlKey && !event.altKey) { aKey.push('CTRL'); } |
||
| 4232 | if (event.shiftKey) { aKey.push('SHIFT'); } |
||
| 4233 | |||
| 4234 | // keycode |
||
| 4235 | var keyName = key.nameFromCode[event.keyCode]; |
||
| 4236 | if (keyName) { aKey.push(keyName); } |
||
| 4237 | |||
| 4238 | var eventName = keyMap[aKey.join('+')]; |
||
| 4239 | if (eventName) { |
||
| 4240 | event.preventDefault(); |
||
| 4241 | |||
| 4242 | if (editor[eventName]) { |
||
| 4243 | editor[eventName]($editable, $editor.data('options')); |
||
| 4244 | } else if (commands[eventName]) { |
||
| 4245 | commands[eventName].call(this, layoutInfo); |
||
| 4246 | } |
||
| 4247 | } else if (key.isEdit(event.keyCode)) { |
||
| 4248 | editor.afterCommand($editable); |
||
| 4249 | } |
||
| 4250 | }); |
||
| 4251 | }; |
||
| 4252 | |||
| 4253 | /** |
||
| 4254 | * attach eventhandler |
||
| 4255 | * |
||
| 4256 | * @param {Object} layoutInfo - layout Informations |
||
| 4257 | * @param {Object} options - user options include custom event handlers |
||
| 4258 | * @param {Function} options.enter - enter key handler |
||
| 4259 | */ |
||
| 4260 | this.attach = function (layoutInfo, options) { |
||
| 4261 | // handlers for editable |
||
| 4262 | this.bindKeyMap(layoutInfo, options.keyMap[agent.isMac ? 'mac' : 'pc']); |
||
| 4263 | layoutInfo.editable.on('mousedown', hMousedown); |
||
| 4264 | layoutInfo.editable.on('keyup mouseup', hToolbarAndPopoverUpdate); |
||
| 4265 | layoutInfo.editable.on('scroll', hScroll); |
||
| 4266 | layoutInfo.editable.on('paste', hPasteClipboardImage); |
||
| 4267 | |||
| 4268 | // handler for handle and popover |
||
| 4269 | layoutInfo.handle.on('mousedown', hHandleMousedown); |
||
| 4270 | layoutInfo.popover.on('click', hToolbarAndPopoverClick); |
||
| 4271 | layoutInfo.popover.on('mousedown', hToolbarAndPopoverMousedown); |
||
| 4272 | |||
| 4273 | // handlers for frame mode (toolbar, statusbar) |
||
| 4274 | if (!options.airMode) { |
||
| 4275 | // handler for drag and drop |
||
| 4276 | handleDragAndDropEvent(layoutInfo, options.disableDragAndDrop); |
||
| 4277 | |||
| 4278 | // handler for toolbar |
||
| 4279 | layoutInfo.toolbar.on('click', hToolbarAndPopoverClick); |
||
| 4280 | layoutInfo.toolbar.on('mousedown', hToolbarAndPopoverMousedown); |
||
| 4281 | |||
| 4282 | // handler for statusbar |
||
| 4283 | if (!options.disableResizeEditor) { |
||
| 4284 | layoutInfo.statusbar.on('mousedown', hStatusbarMousedown); |
||
| 4285 | } |
||
| 4286 | } |
||
| 4287 | |||
| 4288 | // handler for table dimension |
||
| 4289 | var $catcherContainer = options.airMode ? layoutInfo.popover : |
||
| 4290 | layoutInfo.toolbar; |
||
| 4291 | var $catcher = $catcherContainer.find('.note-dimension-picker-mousecatcher'); |
||
| 4292 | $catcher.css({ |
||
| 4293 | width: options.insertTableMaxSize.col + 'em', |
||
| 4294 | height: options.insertTableMaxSize.row + 'em' |
||
| 4295 | }).on('mousemove', function (event) { |
||
| 4296 | hDimensionPickerMove(event, options); |
||
| 4297 | }); |
||
| 4298 | |||
| 4299 | // save options on editor |
||
| 4300 | layoutInfo.editor.data('options', options); |
||
| 4301 | |||
| 4302 | // ret styleWithCSS for backColor / foreColor clearing with 'inherit'. |
||
| 4303 | if (options.styleWithSpan && !agent.isMSIE) { |
||
| 4304 | // protect FF Error: NS_ERROR_FAILURE: Failure |
||
| 4305 | setTimeout(function () { |
||
| 4306 | document.execCommand('styleWithCSS', 0, true); |
||
| 4307 | }, 0); |
||
| 4308 | } |
||
| 4309 | |||
| 4310 | // History |
||
| 4311 | var history = new History(layoutInfo.editable); |
||
| 4312 | layoutInfo.editable.data('NoteHistory', history); |
||
| 4313 | |||
| 4314 | // basic event callbacks (lowercase) |
||
| 4315 | // enter, focus, blur, keyup, keydown |
||
| 4316 | if (options.onenter) { |
||
| 4317 | layoutInfo.editable.keypress(function (event) { |
||
| 4318 | if (event.keyCode === key.ENTER) { options.onenter(event); } |
||
| 4319 | }); |
||
| 4320 | } |
||
| 4321 | |||
| 4322 | if (options.onfocus) { layoutInfo.editable.focus(options.onfocus); } |
||
| 4323 | if (options.onblur) { layoutInfo.editable.blur(options.onblur); } |
||
| 4324 | if (options.onkeyup) { layoutInfo.editable.keyup(options.onkeyup); } |
||
| 4325 | if (options.onkeydown) { layoutInfo.editable.keydown(options.onkeydown); } |
||
| 4326 | if (options.onpaste) { layoutInfo.editable.on('paste', options.onpaste); } |
||
| 4327 | |||
| 4328 | // callbacks for advanced features (camel) |
||
| 4329 | if (options.onToolbarClick) { layoutInfo.toolbar.click(options.onToolbarClick); } |
||
| 4330 | if (options.onChange) { |
||
| 4331 | var hChange = function () { |
||
| 4332 | editor.triggerOnChange(layoutInfo.editable); |
||
| 4333 | }; |
||
| 4334 | |||
| 4335 | if (agent.isMSIE) { |
||
| 4336 | var sDomEvents = 'DOMCharacterDataModified DOMSubtreeModified DOMNodeInserted'; |
||
| 4337 | layoutInfo.editable.on(sDomEvents, hChange); |
||
| 4338 | } else { |
||
| 4339 | layoutInfo.editable.on('input', hChange); |
||
| 4340 | } |
||
| 4341 | } |
||
| 4342 | |||
| 4343 | // All editor status will be saved on editable with jquery's data |
||
| 4344 | // for support multiple editor with singleton object. |
||
| 4345 | layoutInfo.editable.data('callbacks', { |
||
| 4346 | onChange: options.onChange, |
||
| 4347 | onAutoSave: options.onAutoSave, |
||
| 4348 | onImageUpload: options.onImageUpload, |
||
| 4349 | onImageUploadError: options.onImageUploadError, |
||
| 4350 | onFileUpload: options.onFileUpload, |
||
| 4351 | onFileUploadError: options.onFileUpload |
||
| 4352 | }); |
||
| 4353 | }; |
||
| 4354 | |||
| 4355 | this.dettach = function (layoutInfo, options) { |
||
| 4356 | layoutInfo.editable.off(); |
||
| 4357 | |||
| 4358 | layoutInfo.popover.off(); |
||
| 4359 | layoutInfo.handle.off(); |
||
| 4360 | layoutInfo.dialog.off(); |
||
| 4361 | |||
| 4362 | if (!options.airMode) { |
||
| 4363 | layoutInfo.dropzone.off(); |
||
| 4364 | layoutInfo.toolbar.off(); |
||
| 4365 | layoutInfo.statusbar.off(); |
||
| 4366 | } |
||
| 4367 | }; |
||
| 4368 | }; |
||
| 4369 | |||
| 4370 | /** |
||
| 4371 | * renderer |
||
| 4372 | * |
||
| 4373 | * rendering toolbar and editable |
||
| 4374 | */ |
||
| 4375 | var Renderer = function () { |
||
| 4376 | |||
| 4377 | /** |
||
| 4378 | * bootstrap button template |
||
| 4379 | * |
||
| 4380 | * @param {String} label |
||
| 4381 | * @param {Object} [options] |
||
| 4382 | * @param {String} [options.event] |
||
| 4383 | * @param {String} [options.value] |
||
| 4384 | * @param {String} [options.title] |
||
| 4385 | * @param {String} [options.dropdown] |
||
| 4386 | * @param {String} [options.hide] |
||
| 4387 | */ |
||
| 4388 | var tplButton = function (label, options) { |
||
| 4389 | var event = options.event; |
||
| 4390 | var value = options.value; |
||
| 4391 | var title = options.title; |
||
| 4392 | var className = options.className; |
||
| 4393 | var dropdown = options.dropdown; |
||
| 4394 | var hide = options.hide; |
||
| 4395 | |||
| 4396 | return '<button type="button"' + |
||
| 4397 | ' class="btn btn-default btn-sm btn-small' + |
||
| 4398 | (className ? ' ' + className : '') + |
||
| 4399 | (dropdown ? ' dropdown-toggle' : '') + |
||
| 4400 | '"' + |
||
| 4401 | (dropdown ? ' data-toggle="dropdown"' : '') + |
||
| 4402 | (title ? ' title="' + title + '"' : '') + |
||
| 4403 | (event ? ' data-event="' + event + '"' : '') + |
||
| 4404 | (value ? ' data-value=\'' + value + '\'' : '') + |
||
| 4405 | (hide ? ' data-hide=\'' + hide + '\'' : '') + |
||
| 4406 | ' tabindex="-1">' + |
||
| 4407 | label + |
||
| 4408 | (dropdown ? ' <span class="caret"></span>' : '') + |
||
| 4409 | '</button>' + |
||
| 4410 | (dropdown || ''); |
||
| 4411 | }; |
||
| 4412 | |||
| 4413 | /** |
||
| 4414 | * bootstrap icon button template |
||
| 4415 | * |
||
| 4416 | * @param {String} iconClassName |
||
| 4417 | * @param {Object} [options] |
||
| 4418 | * @param {String} [options.event] |
||
| 4419 | * @param {String} [options.value] |
||
| 4420 | * @param {String} [options.title] |
||
| 4421 | * @param {String} [options.dropdown] |
||
| 4422 | */ |
||
| 4423 | var tplIconButton = function (iconClassName, options) { |
||
| 4424 | var label = '<i class="' + iconClassName + '"></i>'; |
||
| 4425 | return tplButton(label, options); |
||
| 4426 | }; |
||
| 4427 | |||
| 4428 | /** |
||
| 4429 | * bootstrap popover template |
||
| 4430 | * |
||
| 4431 | * @param {String} className |
||
| 4432 | * @param {String} content |
||
| 4433 | */ |
||
| 4434 | var tplPopover = function (className, content) { |
||
| 4435 | return '<div class="' + className + ' popover bottom in" style="display: none;">' + |
||
| 4436 | '<div class="arrow"></div>' + |
||
| 4437 | '<div class="popover-content">' + |
||
| 4438 | content + |
||
| 4439 | '</div>' + |
||
| 4440 | '</div>'; |
||
| 4441 | }; |
||
| 4442 | |||
| 4443 | /** |
||
| 4444 | * bootstrap dialog template |
||
| 4445 | * |
||
| 4446 | * @param {String} className |
||
| 4447 | * @param {String} [title] |
||
| 4448 | * @param {String} body |
||
| 4449 | * @param {String} [footer] |
||
| 4450 | */ |
||
| 4451 | var tplDialog = function (className, title, body, footer) { |
||
| 4452 | return '<div class="' + className + ' modal" aria-hidden="false">' + |
||
| 4453 | '<div class="modal-dialog">' + |
||
| 4454 | '<div class="modal-content">' + |
||
| 4455 | (title ? |
||
| 4456 | '<div class="modal-header">' + |
||
| 4457 | '<button type="button" class="close" aria-hidden="true" tabindex="-1">×</button>' + |
||
| 4458 | '<h4 class="modal-title">' + title + '</h4>' + |
||
| 4459 | '</div>' : '' |
||
| 4460 | ) + |
||
| 4461 | '<form class="note-modal-form">' + |
||
| 4462 | '<div class="modal-body">' + |
||
| 4463 | '<div class="row-fluid">' + body + '</div>' + |
||
| 4464 | '</div>' + |
||
| 4465 | (footer ? |
||
| 4466 | '<div class="modal-footer">' + footer + '</div>' : '' |
||
| 4467 | ) + |
||
| 4468 | '</form>' + |
||
| 4469 | '</div>' + |
||
| 4470 | '</div>' + |
||
| 4471 | '</div>'; |
||
| 4472 | }; |
||
| 4473 | |||
| 4474 | var tplButtonInfo = { |
||
| 4475 | picture: function (lang) { |
||
| 4476 | return tplIconButton('fa fa-picture-o icon-picture', { |
||
| 4477 | event: 'showImageDialog', |
||
| 4478 | title: lang.image.image, |
||
| 4479 | hide: true |
||
| 4480 | }); |
||
| 4481 | }, |
||
| 4482 | link: function (lang) { |
||
| 4483 | return tplIconButton('fa fa-link icon-link', { |
||
| 4484 | event: 'showLinkDialog', |
||
| 4485 | title: lang.link.link, |
||
| 4486 | hide: true |
||
| 4487 | }); |
||
| 4488 | }, |
||
| 4489 | video: function (lang) { |
||
| 4490 | return tplIconButton('fa fa-youtube-play icon-play', { |
||
| 4491 | event: 'showVideoDialog', |
||
| 4492 | title: lang.video.video, |
||
| 4493 | hide: true |
||
| 4494 | }); |
||
| 4495 | }, |
||
| 4496 | table: function (lang) { |
||
| 4497 | var dropdown = '<ul class="note-table dropdown-menu">' + |
||
| 4498 | '<div class="note-dimension-picker">' + |
||
| 4499 | '<div class="note-dimension-picker-mousecatcher" data-event="insertTable" data-value="1x1"></div>' + |
||
| 4500 | '<div class="note-dimension-picker-highlighted"></div>' + |
||
| 4501 | '<div class="note-dimension-picker-unhighlighted"></div>' + |
||
| 4502 | '</div>' + |
||
| 4503 | '<div class="note-dimension-display"> 1 x 1 </div>' + |
||
| 4504 | '</ul>'; |
||
| 4505 | return tplIconButton('fa fa-table icon-table', { |
||
| 4506 | title: lang.table.table, |
||
| 4507 | dropdown: dropdown |
||
| 4508 | }); |
||
| 4509 | }, |
||
| 4510 | style: function (lang, options) { |
||
| 4511 | var items = options.styleTags.reduce(function (memo, v) { |
||
| 4512 | var label = lang.style[v === 'p' ? 'normal' : v]; |
||
| 4513 | return memo + '<li><a data-event="formatBlock" href="#" data-value="' + v + '">' + |
||
| 4514 | ( |
||
| 4515 | (v === 'p' || v === 'pre') ? label : |
||
| 4516 | '<' + v + '>' + label + '</' + v + '>' |
||
| 4517 | ) + |
||
| 4518 | '</a></li>'; |
||
| 4519 | }, ''); |
||
| 4520 | |||
| 4521 | return tplIconButton('fa fa-magic icon-magic', { |
||
| 4522 | title: lang.style.style, |
||
| 4523 | dropdown: '<ul class="dropdown-menu">' + items + '</ul>' |
||
| 4524 | }); |
||
| 4525 | }, |
||
| 4526 | fontname: function (lang, options) { |
||
| 4527 | var items = options.fontNames.reduce(function (memo, v) { |
||
| 4528 | if (!agent.isFontInstalled(v)) { return memo; } |
||
| 4529 | return memo + '<li><a data-event="fontName" href="#" data-value="' + v + '">' + |
||
| 4530 | '<i class="fa fa-check icon-ok"></i> ' + v + |
||
| 4531 | '</a></li>'; |
||
| 4532 | }, ''); |
||
| 4533 | var label = '<span class="note-current-fontname">' + |
||
| 4534 | options.defaultFontName + |
||
| 4535 | '</span>'; |
||
| 4536 | return tplButton(label, { |
||
| 4537 | title: lang.font.name, |
||
| 4538 | dropdown: '<ul class="dropdown-menu">' + items + '</ul>' |
||
| 4539 | }); |
||
| 4540 | }, |
||
| 4541 | fontsize: function (lang, options) { |
||
| 4542 | var items = options.fontSizes.reduce(function (memo, v) { |
||
| 4543 | return memo + '<li><a data-event="fontSize" href="#" data-value="' + v + '">' + |
||
| 4544 | '<i class="fa fa-check icon-ok"></i> ' + v + |
||
| 4545 | '</a></li>'; |
||
| 4546 | }, ''); |
||
| 4547 | |||
| 4548 | var label = '<span class="note-current-fontsize">11</span>'; |
||
| 4549 | return tplButton(label, { |
||
| 4550 | title: lang.font.size, |
||
| 4551 | dropdown: '<ul class="dropdown-menu">' + items + '</ul>' |
||
| 4552 | }); |
||
| 4553 | }, |
||
| 4554 | |||
| 4555 | color: function (lang) { |
||
| 4556 | var colorButtonLabel = '<i class="fa fa-font icon-font" style="color:black;background-color:yellow;"></i>'; |
||
| 4557 | var colorButton = tplButton(colorButtonLabel, { |
||
| 4558 | className: 'note-recent-color', |
||
| 4559 | title: lang.color.recent, |
||
| 4560 | event: 'color', |
||
| 4561 | value: '{"backColor":"yellow"}' |
||
| 4562 | }); |
||
| 4563 | |||
| 4564 | var dropdown = '<ul class="dropdown-menu">' + |
||
| 4565 | '<li>' + |
||
| 4566 | '<div class="btn-group">' + |
||
| 4567 | '<div class="note-palette-title">' + lang.color.background + '</div>' + |
||
| 4568 | '<div class="note-color-reset" data-event="backColor"' + |
||
| 4569 | ' data-value="inherit" title="' + lang.color.transparent + '">' + |
||
| 4570 | lang.color.setTransparent + |
||
| 4571 | '</div>' + |
||
| 4572 | '<div class="note-color-palette" data-target-event="backColor"></div>' + |
||
| 4573 | '</div>' + |
||
| 4574 | '<div class="btn-group">' + |
||
| 4575 | '<div class="note-palette-title">' + lang.color.foreground + '</div>' + |
||
| 4576 | '<div class="note-color-reset" data-event="foreColor" data-value="inherit" title="' + lang.color.reset + '">' + |
||
| 4577 | lang.color.resetToDefault + |
||
| 4578 | '</div>' + |
||
| 4579 | '<div class="note-color-palette" data-target-event="foreColor"></div>' + |
||
| 4580 | '</div>' + |
||
| 4581 | '</li>' + |
||
| 4582 | '</ul>'; |
||
| 4583 | |||
| 4584 | var moreButton = tplButton('', { |
||
| 4585 | title: lang.color.more, |
||
| 4586 | dropdown: dropdown |
||
| 4587 | }); |
||
| 4588 | |||
| 4589 | return colorButton + moreButton; |
||
| 4590 | }, |
||
| 4591 | bold: function (lang) { |
||
| 4592 | return tplIconButton('fa fa-bold icon-bold', { |
||
| 4593 | event: 'bold', |
||
| 4594 | title: lang.font.bold |
||
| 4595 | }); |
||
| 4596 | }, |
||
| 4597 | italic: function (lang) { |
||
| 4598 | return tplIconButton('fa fa-italic icon-italic', { |
||
| 4599 | event: 'italic', |
||
| 4600 | title: lang.font.italic |
||
| 4601 | }); |
||
| 4602 | }, |
||
| 4603 | underline: function (lang) { |
||
| 4604 | return tplIconButton('fa fa-underline icon-underline', { |
||
| 4605 | event: 'underline', |
||
| 4606 | title: lang.font.underline |
||
| 4607 | }); |
||
| 4608 | }, |
||
| 4609 | strikethrough: function (lang) { |
||
| 4610 | return tplIconButton('fa fa-strikethrough icon-strikethrough', { |
||
| 4611 | event: 'strikethrough', |
||
| 4612 | title: lang.font.strikethrough |
||
| 4613 | }); |
||
| 4614 | }, |
||
| 4615 | superscript: function (lang) { |
||
| 4616 | return tplIconButton('fa fa-superscript icon-superscript', { |
||
| 4617 | event: 'superscript', |
||
| 4618 | title: lang.font.superscript |
||
| 4619 | }); |
||
| 4620 | }, |
||
| 4621 | subscript: function (lang) { |
||
| 4622 | return tplIconButton('fa fa-subscript icon-subscript', { |
||
| 4623 | event: 'subscript', |
||
| 4624 | title: lang.font.subscript |
||
| 4625 | }); |
||
| 4626 | }, |
||
| 4627 | clear: function (lang) { |
||
| 4628 | return tplIconButton('fa fa-eraser icon-eraser', { |
||
| 4629 | event: 'removeFormat', |
||
| 4630 | title: lang.font.clear |
||
| 4631 | }); |
||
| 4632 | }, |
||
| 4633 | ul: function (lang) { |
||
| 4634 | return tplIconButton('fa fa-list-ul icon-list-ul', { |
||
| 4635 | event: 'insertUnorderedList', |
||
| 4636 | title: lang.lists.unordered |
||
| 4637 | }); |
||
| 4638 | }, |
||
| 4639 | ol: function (lang) { |
||
| 4640 | return tplIconButton('fa fa-list-ol icon-list-ol', { |
||
| 4641 | event: 'insertOrderedList', |
||
| 4642 | title: lang.lists.ordered |
||
| 4643 | }); |
||
| 4644 | }, |
||
| 4645 | paragraph: function (lang) { |
||
| 4646 | var leftButton = tplIconButton('fa fa-align-left icon-align-left', { |
||
| 4647 | title: lang.paragraph.left, |
||
| 4648 | event: 'justifyLeft' |
||
| 4649 | }); |
||
| 4650 | var centerButton = tplIconButton('fa fa-align-center icon-align-center', { |
||
| 4651 | title: lang.paragraph.center, |
||
| 4652 | event: 'justifyCenter' |
||
| 4653 | }); |
||
| 4654 | var rightButton = tplIconButton('fa fa-align-right icon-align-right', { |
||
| 4655 | title: lang.paragraph.right, |
||
| 4656 | event: 'justifyRight' |
||
| 4657 | }); |
||
| 4658 | var justifyButton = tplIconButton('fa fa-align-justify icon-align-justify', { |
||
| 4659 | title: lang.paragraph.justify, |
||
| 4660 | event: 'justifyFull' |
||
| 4661 | }); |
||
| 4662 | |||
| 4663 | var outdentButton = tplIconButton('fa fa-outdent icon-indent-left', { |
||
| 4664 | title: lang.paragraph.outdent, |
||
| 4665 | event: 'outdent' |
||
| 4666 | }); |
||
| 4667 | var indentButton = tplIconButton('fa fa-indent icon-indent-right', { |
||
| 4668 | title: lang.paragraph.indent, |
||
| 4669 | event: 'indent' |
||
| 4670 | }); |
||
| 4671 | |||
| 4672 | var dropdown = '<div class="dropdown-menu">' + |
||
| 4673 | '<div class="note-align btn-group">' + |
||
| 4674 | leftButton + centerButton + rightButton + justifyButton + |
||
| 4675 | '</div>' + |
||
| 4676 | '<div class="note-list btn-group">' + |
||
| 4677 | indentButton + outdentButton + |
||
| 4678 | '</div>' + |
||
| 4679 | '</div>'; |
||
| 4680 | |||
| 4681 | return tplIconButton('fa fa-align-left icon-align-left', { |
||
| 4682 | title: lang.paragraph.paragraph, |
||
| 4683 | dropdown: dropdown |
||
| 4684 | }); |
||
| 4685 | }, |
||
| 4686 | height: function (lang, options) { |
||
| 4687 | var items = options.lineHeights.reduce(function (memo, v) { |
||
| 4688 | return memo + '<li><a data-event="lineHeight" href="#" data-value="' + parseFloat(v) + '">' + |
||
| 4689 | '<i class="fa fa-check icon-ok"></i> ' + v + |
||
| 4690 | '</a></li>'; |
||
| 4691 | }, ''); |
||
| 4692 | |||
| 4693 | return tplIconButton('fa fa-text-height icon-text-height', { |
||
| 4694 | title: lang.font.height, |
||
| 4695 | dropdown: '<ul class="dropdown-menu">' + items + '</ul>' |
||
| 4696 | }); |
||
| 4697 | |||
| 4698 | }, |
||
| 4699 | help: function (lang) { |
||
| 4700 | return tplIconButton('fa fa-question icon-question', { |
||
| 4701 | event: 'showHelpDialog', |
||
| 4702 | title: lang.options.help, |
||
| 4703 | hide: true |
||
| 4704 | }); |
||
| 4705 | }, |
||
| 4706 | fullscreen: function (lang) { |
||
| 4707 | return tplIconButton('fa fa-arrows-alt icon-fullscreen', { |
||
| 4708 | event: 'fullscreen', |
||
| 4709 | title: lang.options.fullscreen |
||
| 4710 | }); |
||
| 4711 | }, |
||
| 4712 | codeview: function (lang) { |
||
| 4713 | return tplIconButton('fa fa-code icon-code', { |
||
| 4714 | event: 'codeview', |
||
| 4715 | title: lang.options.codeview |
||
| 4716 | }); |
||
| 4717 | }, |
||
| 4718 | undo: function (lang) { |
||
| 4719 | return tplIconButton('fa fa-undo icon-undo', { |
||
| 4720 | event: 'undo', |
||
| 4721 | title: lang.history.undo |
||
| 4722 | }); |
||
| 4723 | }, |
||
| 4724 | redo: function (lang) { |
||
| 4725 | return tplIconButton('fa fa-repeat icon-repeat', { |
||
| 4726 | event: 'redo', |
||
| 4727 | title: lang.history.redo |
||
| 4728 | }); |
||
| 4729 | }, |
||
| 4730 | hr: function (lang) { |
||
| 4731 | return tplIconButton('fa fa-minus icon-hr', { |
||
| 4732 | event: 'insertHorizontalRule', |
||
| 4733 | title: lang.hr.insert |
||
| 4734 | }); |
||
| 4735 | } |
||
| 4736 | }; |
||
| 4737 | |||
| 4738 | var tplPopovers = function (lang, options) { |
||
| 4739 | var tplLinkPopover = function () { |
||
| 4740 | var linkButton = tplIconButton('fa fa-edit icon-edit', { |
||
| 4741 | title: lang.link.edit, |
||
| 4742 | event: 'showLinkDialog', |
||
| 4743 | hide: true |
||
| 4744 | }); |
||
| 4745 | var unlinkButton = tplIconButton('fa fa-unlink icon-unlink', { |
||
| 4746 | title: lang.link.unlink, |
||
| 4747 | event: 'unlink' |
||
| 4748 | }); |
||
| 4749 | var content = '<a href="http://www.google.com" target="_blank">www.google.com</a> ' + |
||
| 4750 | '<div class="note-insert btn-group">' + |
||
| 4751 | linkButton + unlinkButton + |
||
| 4752 | '</div>'; |
||
| 4753 | return tplPopover('note-link-popover', content); |
||
| 4754 | }; |
||
| 4755 | |||
| 4756 | var tplImagePopover = function () { |
||
| 4757 | var fullButton = tplButton('<span class="note-fontsize-10">100%</span>', { |
||
| 4758 | title: lang.image.resizeFull, |
||
| 4759 | event: 'resize', |
||
| 4760 | value: '1' |
||
| 4761 | }); |
||
| 4762 | var halfButton = tplButton('<span class="note-fontsize-10">50%</span>', { |
||
| 4763 | title: lang.image.resizeHalf, |
||
| 4764 | event: 'resize', |
||
| 4765 | value: '0.5' |
||
| 4766 | }); |
||
| 4767 | var quarterButton = tplButton('<span class="note-fontsize-10">25%</span>', { |
||
| 4768 | title: lang.image.resizeQuarter, |
||
| 4769 | event: 'resize', |
||
| 4770 | value: '0.25' |
||
| 4771 | }); |
||
| 4772 | |||
| 4773 | var leftButton = tplIconButton('fa fa-align-left icon-align-left', { |
||
| 4774 | title: lang.image.floatLeft, |
||
| 4775 | event: 'floatMe', |
||
| 4776 | value: 'left' |
||
| 4777 | }); |
||
| 4778 | var rightButton = tplIconButton('fa fa-align-right icon-align-right', { |
||
| 4779 | title: lang.image.floatRight, |
||
| 4780 | event: 'floatMe', |
||
| 4781 | value: 'right' |
||
| 4782 | }); |
||
| 4783 | var justifyButton = tplIconButton('fa fa-align-justify icon-align-justify', { |
||
| 4784 | title: lang.image.floatNone, |
||
| 4785 | event: 'floatMe', |
||
| 4786 | value: 'none' |
||
| 4787 | }); |
||
| 4788 | |||
| 4789 | var roundedButton = tplIconButton('fa fa-square icon-unchecked', { |
||
| 4790 | title: lang.image.shapeRounded, |
||
| 4791 | event: 'imageShape', |
||
| 4792 | value: 'img-rounded' |
||
| 4793 | }); |
||
| 4794 | var circleButton = tplIconButton('fa fa-circle-o icon-circle-blank', { |
||
| 4795 | title: lang.image.shapeCircle, |
||
| 4796 | event: 'imageShape', |
||
| 4797 | value: 'img-circle' |
||
| 4798 | }); |
||
| 4799 | var thumbnailButton = tplIconButton('fa fa-picture-o icon-picture', { |
||
| 4800 | title: lang.image.shapeThumbnail, |
||
| 4801 | event: 'imageShape', |
||
| 4802 | value: 'img-thumbnail' |
||
| 4803 | }); |
||
| 4804 | var noneButton = tplIconButton('fa fa-times icon-times', { |
||
| 4805 | title: lang.image.shapeNone, |
||
| 4806 | event: 'imageShape', |
||
| 4807 | value: '' |
||
| 4808 | }); |
||
| 4809 | |||
| 4810 | var removeButton = tplIconButton('fa fa-trash-o icon-trash', { |
||
| 4811 | title: lang.image.remove, |
||
| 4812 | event: 'removeMedia', |
||
| 4813 | value: 'none' |
||
| 4814 | }); |
||
| 4815 | |||
| 4816 | var content = '<div class="btn-group">' + fullButton + halfButton + quarterButton + '</div>' + |
||
| 4817 | '<div class="btn-group">' + leftButton + rightButton + justifyButton + '</div>' + |
||
| 4818 | '<div class="btn-group">' + roundedButton + circleButton + thumbnailButton + noneButton + '</div>' + |
||
| 4819 | '<div class="btn-group">' + removeButton + '</div>'; |
||
| 4820 | return tplPopover('note-image-popover', content); |
||
| 4821 | }; |
||
| 4822 | |||
| 4823 | var tplAirPopover = function () { |
||
| 4824 | var content = ''; |
||
| 4825 | for (var idx = 0, len = options.airPopover.length; idx < len; idx ++) { |
||
| 4826 | var group = options.airPopover[idx]; |
||
| 4827 | content += '<div class="note-' + group[0] + ' btn-group">'; |
||
| 4828 | for (var i = 0, lenGroup = group[1].length; i < lenGroup; i++) { |
||
| 4829 | content += tplButtonInfo[group[1][i]](lang, options); |
||
| 4830 | } |
||
| 4831 | content += '</div>'; |
||
| 4832 | } |
||
| 4833 | |||
| 4834 | return tplPopover('note-air-popover', content); |
||
| 4835 | }; |
||
| 4836 | |||
| 4837 | return '<div class="note-popover">' + |
||
| 4838 | tplLinkPopover() + |
||
| 4839 | tplImagePopover() + |
||
| 4840 | (options.airMode ? tplAirPopover() : '') + |
||
| 4841 | '</div>'; |
||
| 4842 | }; |
||
| 4843 | |||
| 4844 | var tplHandles = function () { |
||
| 4845 | return '<div class="note-handle">' + |
||
| 4846 | '<div class="note-control-selection">' + |
||
| 4847 | '<div class="note-control-selection-bg"></div>' + |
||
| 4848 | '<div class="note-control-holder note-control-nw"></div>' + |
||
| 4849 | '<div class="note-control-holder note-control-ne"></div>' + |
||
| 4850 | '<div class="note-control-holder note-control-sw"></div>' + |
||
| 4851 | '<div class="note-control-sizing note-control-se"></div>' + |
||
| 4852 | '<div class="note-control-selection-info"></div>' + |
||
| 4853 | '</div>' + |
||
| 4854 | '</div>'; |
||
| 4855 | }; |
||
| 4856 | |||
| 4857 | /** |
||
| 4858 | * shortcut table template |
||
| 4859 | * @param {String} title |
||
| 4860 | * @param {String} body |
||
| 4861 | */ |
||
| 4862 | var tplShortcut = function (title, body) { |
||
| 4863 | return '<table class="note-shortcut">' + |
||
| 4864 | '<thead>' + |
||
| 4865 | '<tr><th></th><th>' + title + '</th></tr>' + |
||
| 4866 | '</thead>' + |
||
| 4867 | '<tbody>' + body + '</tbody>' + |
||
| 4868 | '</table>'; |
||
| 4869 | }; |
||
| 4870 | |||
| 4871 | var tplShortcutText = function (lang) { |
||
| 4872 | var body = '<tr><td>⌘ + B</td><td>' + lang.font.bold + '</td></tr>' + |
||
| 4873 | '<tr><td>⌘ + I</td><td>' + lang.font.italic + '</td></tr>' + |
||
| 4874 | '<tr><td>⌘ + U</td><td>' + lang.font.underline + '</td></tr>' + |
||
| 4875 | '<tr><td>⌘ + ⇧ + S</td><td>' + lang.font.strikethrough + '</td></tr>' + |
||
| 4876 | '<tr><td>⌘ + \\</td><td>' + lang.font.clear + '</td></tr>'; |
||
| 4877 | |||
| 4878 | return tplShortcut(lang.shortcut.textFormatting, body); |
||
| 4879 | }; |
||
| 4880 | |||
| 4881 | var tplShortcutAction = function (lang) { |
||
| 4882 | var body = '<tr><td>⌘ + Z</td><td>' + lang.history.undo + '</td></tr>' + |
||
| 4883 | '<tr><td>⌘ + ⇧ + Z</td><td>' + lang.history.redo + '</td></tr>' + |
||
| 4884 | '<tr><td>⌘ + ]</td><td>' + lang.paragraph.indent + '</td></tr>' + |
||
| 4885 | '<tr><td>⌘ + [</td><td>' + lang.paragraph.outdent + '</td></tr>' + |
||
| 4886 | '<tr><td>⌘ + ENTER</td><td>' + lang.hr.insert + '</td></tr>'; |
||
| 4887 | |||
| 4888 | return tplShortcut(lang.shortcut.action, body); |
||
| 4889 | }; |
||
| 4890 | |||
| 4891 | var tplShortcutPara = function (lang) { |
||
| 4892 | var body = '<tr><td>⌘ + ⇧ + L</td><td>' + lang.paragraph.left + '</td></tr>' + |
||
| 4893 | '<tr><td>⌘ + ⇧ + E</td><td>' + lang.paragraph.center + '</td></tr>' + |
||
| 4894 | '<tr><td>⌘ + ⇧ + R</td><td>' + lang.paragraph.right + '</td></tr>' + |
||
| 4895 | '<tr><td>⌘ + ⇧ + J</td><td>' + lang.paragraph.justify + '</td></tr>' + |
||
| 4896 | '<tr><td>⌘ + ⇧ + NUM7</td><td>' + lang.lists.ordered + '</td></tr>' + |
||
| 4897 | '<tr><td>⌘ + ⇧ + NUM8</td><td>' + lang.lists.unordered + '</td></tr>'; |
||
| 4898 | |||
| 4899 | return tplShortcut(lang.shortcut.paragraphFormatting, body); |
||
| 4900 | }; |
||
| 4901 | |||
| 4902 | var tplShortcutStyle = function (lang) { |
||
| 4903 | var body = '<tr><td>⌘ + NUM0</td><td>' + lang.style.normal + '</td></tr>' + |
||
| 4904 | '<tr><td>⌘ + NUM1</td><td>' + lang.style.h1 + '</td></tr>' + |
||
| 4905 | '<tr><td>⌘ + NUM2</td><td>' + lang.style.h2 + '</td></tr>' + |
||
| 4906 | '<tr><td>⌘ + NUM3</td><td>' + lang.style.h3 + '</td></tr>' + |
||
| 4907 | '<tr><td>⌘ + NUM4</td><td>' + lang.style.h4 + '</td></tr>' + |
||
| 4908 | '<tr><td>⌘ + NUM5</td><td>' + lang.style.h5 + '</td></tr>' + |
||
| 4909 | '<tr><td>⌘ + NUM6</td><td>' + lang.style.h6 + '</td></tr>'; |
||
| 4910 | |||
| 4911 | return tplShortcut(lang.shortcut.documentStyle, body); |
||
| 4912 | }; |
||
| 4913 | |||
| 4914 | var tplExtraShortcuts = function (lang, options) { |
||
| 4915 | var extraKeys = options.extraKeys; |
||
| 4916 | var body = ''; |
||
| 4917 | for (var key in extraKeys) { |
||
| 4918 | if (extraKeys.hasOwnProperty(key)) { |
||
| 4919 | body += '<tr><td>' + key + '</td><td>' + extraKeys[key] + '</td></tr>'; |
||
| 4920 | } |
||
| 4921 | } |
||
| 4922 | |||
| 4923 | return tplShortcut(lang.shortcut.extraKeys, body); |
||
| 4924 | }; |
||
| 4925 | |||
| 4926 | var tplShortcutTable = function (lang, options) { |
||
| 4927 | var template = '<table class="note-shortcut-layout">' + |
||
| 4928 | '<tbody>' + |
||
| 4929 | '<tr><td>' + tplShortcutAction(lang, options) + '</td><td>' + tplShortcutText(lang, options) + '</td></tr>' + |
||
| 4930 | '<tr><td>' + tplShortcutStyle(lang, options) + '</td><td>' + tplShortcutPara(lang, options) + '</td></tr>'; |
||
| 4931 | if (options.extraKeys) { |
||
| 4932 | template += '<tr><td colspan="2">' + tplExtraShortcuts(lang, options) + '</td></tr>'; |
||
| 4933 | } |
||
| 4934 | template += '</tbody></table>'; |
||
| 4935 | return template; |
||
| 4936 | }; |
||
| 4937 | |||
| 4938 | var replaceMacKeys = function (sHtml) { |
||
| 4939 | return sHtml.replace(/⌘/g, 'Ctrl').replace(/⇧/g, 'Shift'); |
||
| 4940 | }; |
||
| 4941 | |||
| 4942 | var tplDialogs = function (lang, options) { |
||
| 4943 | var tplImageDialog = function () { |
||
| 4944 | var body = |
||
| 4945 | '<div class="note-group-select-from-files">' + |
||
| 4946 | '<h5>' + lang.image.selectFromFiles + '</h5>' + |
||
| 4947 | '<input class="note-image-input" type="file" name="files" accept="image/*" />' + |
||
| 4948 | '</div>' + |
||
| 4949 | '<h5>' + lang.image.url + '</h5>' + |
||
| 4950 | '<input class="note-image-url form-control span12" type="text" />'; |
||
| 4951 | var footer = '<button href="#" class="btn btn-primary note-image-btn disabled" disabled>' + lang.image.insert + '</button>'; |
||
| 4952 | return tplDialog('note-image-dialog', lang.image.insert, body, footer); |
||
| 4953 | }; |
||
| 4954 | |||
| 4955 | var tplLinkDialog = function () { |
||
| 4956 | var body = '<div class="form-group">' + |
||
| 4957 | '<label>' + lang.link.textToDisplay + '</label>' + |
||
| 4958 | '<input class="note-link-text form-control span12" type="text" />' + |
||
| 4959 | '</div>' + |
||
| 4960 | '<div class="form-group">' + |
||
| 4961 | '<label>' + lang.link.url + '</label>' + |
||
| 4962 | '<input class="note-link-url form-control span12" type="text" />' + |
||
| 4963 | '</div>' + |
||
| 4964 | (!options.disableLinkTarget ? |
||
| 4965 | '<div class="checkbox">' + |
||
| 4966 | '<label>' + '<input type="checkbox" checked> ' + |
||
| 4967 | lang.link.openInNewWindow + |
||
| 4968 | '</label>' + |
||
| 4969 | '</div>' : '' |
||
| 4970 | ); |
||
| 4971 | var footer = '<button href="#" class="btn btn-primary note-link-btn disabled" disabled>' + lang.link.insert + '</button>'; |
||
| 4972 | return tplDialog('note-link-dialog', lang.link.insert, body, footer); |
||
| 4973 | }; |
||
| 4974 | |||
| 4975 | var tplVideoDialog = function () { |
||
| 4976 | var body = '<div class="form-group">' + |
||
| 4977 | '<label>' + lang.video.url + '</label> <small class="text-muted">' + lang.video.providers + '</small>' + |
||
| 4978 | '<input class="note-video-url form-control span12" type="text" />' + |
||
| 4979 | '</div>'; |
||
| 4980 | var footer = '<button href="#" class="btn btn-primary note-video-btn disabled" disabled>' + lang.video.insert + '</button>'; |
||
| 4981 | return tplDialog('note-video-dialog', lang.video.insert, body, footer); |
||
| 4982 | }; |
||
| 4983 | |||
| 4984 | var tplHelpDialog = function () { |
||
| 4985 | var body = '<a class="modal-close pull-right" aria-hidden="true" tabindex="-1">' + lang.shortcut.close + '</a>' + |
||
| 4986 | '<div class="title">' + lang.shortcut.shortcuts + '</div>' + |
||
| 4987 | (agent.isMac ? tplShortcutTable(lang, options) : replaceMacKeys(tplShortcutTable(lang, options))) + |
||
| 4988 | '<p class="text-center">' + |
||
| 4989 | '<a href="//hackerwins.github.io/summernote/" target="_blank">Summernote 0.5.10</a> · ' + |
||
| 4990 | '<a href="//github.com/HackerWins/summernote" target="_blank">Project</a> · ' + |
||
| 4991 | '<a href="//github.com/HackerWins/summernote/issues" target="_blank">Issues</a>' + |
||
| 4992 | '</p>'; |
||
| 4993 | return tplDialog('note-help-dialog', '', body, ''); |
||
| 4994 | }; |
||
| 4995 | |||
| 4996 | return '<div class="note-dialog">' + |
||
| 4997 | tplImageDialog() + |
||
| 4998 | tplLinkDialog() + |
||
| 4999 | tplVideoDialog() + |
||
| 5000 | tplHelpDialog() + |
||
| 5001 | '</div>'; |
||
| 5002 | }; |
||
| 5003 | |||
| 5004 | var tplStatusbar = function () { |
||
| 5005 | return '<div class="note-resizebar">' + |
||
| 5006 | '<div class="note-icon-bar"></div>' + |
||
| 5007 | '<div class="note-icon-bar"></div>' + |
||
| 5008 | '<div class="note-icon-bar"></div>' + |
||
| 5009 | '</div>'; |
||
| 5010 | }; |
||
| 5011 | |||
| 5012 | var representShortcut = function (str) { |
||
| 5013 | if (agent.isMac) { |
||
| 5014 | str = str.replace('CMD', '⌘').replace('SHIFT', '⇧'); |
||
| 5015 | } |
||
| 5016 | |||
| 5017 | return str.replace('BACKSLASH', '\\') |
||
| 5018 | .replace('SLASH', '/') |
||
| 5019 | .replace('LEFTBRACKET', '[') |
||
| 5020 | .replace('RIGHTBRACKET', ']'); |
||
| 5021 | }; |
||
| 5022 | |||
| 5023 | /** |
||
| 5024 | * createTooltip |
||
| 5025 | * |
||
| 5026 | * @param {jQuery} $container |
||
| 5027 | * @param {Object} keyMap |
||
| 5028 | * @param {String} [sPlacement] |
||
| 5029 | */ |
||
| 5030 | var createTooltip = function ($container, keyMap, sPlacement) { |
||
| 5031 | var invertedKeyMap = func.invertObject(keyMap); |
||
| 5032 | var $buttons = $container.find('button'); |
||
| 5033 | |||
| 5034 | $buttons.each(function (i, elBtn) { |
||
| 5035 | var $btn = $(elBtn); |
||
| 5036 | var sShortcut = invertedKeyMap[$btn.data('event')]; |
||
| 5037 | if (sShortcut) { |
||
| 5038 | $btn.attr('title', function (i, v) { |
||
| 5039 | return v + ' (' + representShortcut(sShortcut) + ')'; |
||
| 5040 | }); |
||
| 5041 | } |
||
| 5042 | // bootstrap tooltip on btn-group bug |
||
| 5043 | // https://github.com/twbs/bootstrap/issues/5687 |
||
| 5044 | }).tooltip({ |
||
| 5045 | container: 'body', |
||
| 5046 | trigger: 'hover', |
||
| 5047 | placement: sPlacement || 'top' |
||
| 5048 | }).on('click', function () { |
||
| 5049 | $(this).tooltip('hide'); |
||
| 5050 | }); |
||
| 5051 | }; |
||
| 5052 | |||
| 5053 | // createPalette |
||
| 5054 | var createPalette = function ($container, options) { |
||
| 5055 | var colorInfo = options.colors; |
||
| 5056 | $container.find('.note-color-palette').each(function () { |
||
| 5057 | var $palette = $(this), eventName = $palette.attr('data-target-event'); |
||
| 5058 | var paletteContents = []; |
||
| 5059 | for (var row = 0, lenRow = colorInfo.length; row < lenRow; row++) { |
||
| 5060 | var colors = colorInfo[row]; |
||
| 5061 | var buttons = []; |
||
| 5062 | for (var col = 0, lenCol = colors.length; col < lenCol; col++) { |
||
| 5063 | var color = colors[col]; |
||
| 5064 | buttons.push(['<button type="button" class="note-color-btn" style="background-color:', color, |
||
| 5065 | ';" data-event="', eventName, |
||
| 5066 | '" data-value="', color, |
||
| 5067 | '" title="', color, |
||
| 5068 | '" data-toggle="button" tabindex="-1"></button>'].join('')); |
||
| 5069 | } |
||
| 5070 | paletteContents.push('<div class="note-color-row">' + buttons.join('') + '</div>'); |
||
| 5071 | } |
||
| 5072 | $palette.html(paletteContents.join('')); |
||
| 5073 | }); |
||
| 5074 | }; |
||
| 5075 | |||
| 5076 | /** |
||
| 5077 | * create summernote layout (air mode) |
||
| 5078 | * |
||
| 5079 | * @param {jQuery} $holder |
||
| 5080 | * @param {Object} options |
||
| 5081 | */ |
||
| 5082 | this.createLayoutByAirMode = function ($holder, options) { |
||
| 5083 | var keyMap = options.keyMap[agent.isMac ? 'mac' : 'pc']; |
||
| 5084 | var langInfo = $.extend($.summernote.lang['en-US'], $.summernote.lang[options.lang]); |
||
| 5085 | |||
| 5086 | var id = func.uniqueId(); |
||
| 5087 | |||
| 5088 | $holder.addClass('note-air-editor note-editable'); |
||
| 5089 | $holder.attr({ |
||
| 5090 | 'id': 'note-editor-' + id, |
||
| 5091 | 'contentEditable': true |
||
| 5092 | }); |
||
| 5093 | |||
| 5094 | var body = document.body; |
||
| 5095 | |||
| 5096 | // create Popover |
||
| 5097 | var $popover = $(tplPopovers(langInfo, options)); |
||
| 5098 | $popover.addClass('note-air-layout'); |
||
| 5099 | $popover.attr('id', 'note-popover-' + id); |
||
| 5100 | $popover.appendTo(body); |
||
| 5101 | createTooltip($popover, keyMap); |
||
| 5102 | createPalette($popover, options); |
||
| 5103 | |||
| 5104 | // create Handle |
||
| 5105 | var $handle = $(tplHandles()); |
||
| 5106 | $handle.addClass('note-air-layout'); |
||
| 5107 | $handle.attr('id', 'note-handle-' + id); |
||
| 5108 | $handle.appendTo(body); |
||
| 5109 | |||
| 5110 | // create Dialog |
||
| 5111 | var $dialog = $(tplDialogs(langInfo, options)); |
||
| 5112 | $dialog.addClass('note-air-layout'); |
||
| 5113 | $dialog.attr('id', 'note-dialog-' + id); |
||
| 5114 | $dialog.find('button.close, a.modal-close').click(function () { |
||
| 5115 | $(this).closest('.modal').modal('hide'); |
||
| 5116 | }); |
||
| 5117 | $dialog.appendTo(body); |
||
| 5118 | }; |
||
| 5119 | |||
| 5120 | /** |
||
| 5121 | * create summernote layout (normal mode) |
||
| 5122 | * |
||
| 5123 | * @param {jQuery} $holder |
||
| 5124 | * @param {Object} options |
||
| 5125 | */ |
||
| 5126 | this.createLayoutByFrame = function ($holder, options) { |
||
| 5127 | //01. create Editor |
||
| 5128 | var $editor = $('<div class="note-editor"></div>'); |
||
| 5129 | if (options.width) { |
||
| 5130 | $editor.width(options.width); |
||
| 5131 | } |
||
| 5132 | |||
| 5133 | //02. statusbar (resizebar) |
||
| 5134 | if (options.height > 0) { |
||
| 5135 | $('<div class="note-statusbar">' + (options.disableResizeEditor ? '' : tplStatusbar()) + '</div>').prependTo($editor); |
||
| 5136 | } |
||
| 5137 | |||
| 5138 | //03. create Editable |
||
| 5139 | var isContentEditable = !$holder.is(':disabled'); |
||
| 5140 | var $editable = $('<div class="note-editable" contentEditable="' + isContentEditable + '"></div>') |
||
| 5141 | .prependTo($editor); |
||
| 5142 | if (options.height) { |
||
| 5143 | $editable.height(options.height); |
||
| 5144 | } |
||
| 5145 | if (options.direction) { |
||
| 5146 | $editable.attr('dir', options.direction); |
||
| 5147 | } |
||
| 5148 | |||
| 5149 | $editable.html(dom.html($holder) || dom.emptyPara); |
||
| 5150 | |||
| 5151 | //031. create codable |
||
| 5152 | $('<textarea class="note-codable"></textarea>').prependTo($editor); |
||
| 5153 | |||
| 5154 | var langInfo = $.extend($.summernote.lang['en-US'], $.summernote.lang[options.lang]); |
||
| 5155 | |||
| 5156 | //04. create Toolbar |
||
| 5157 | var toolbarHTML = ''; |
||
| 5158 | for (var idx = 0, len = options.toolbar.length; idx < len; idx ++) { |
||
| 5159 | var groupName = options.toolbar[idx][0]; |
||
| 5160 | var groupButtons = options.toolbar[idx][1]; |
||
| 5161 | |||
| 5162 | toolbarHTML += '<div class="note-' + groupName + ' btn-group">'; |
||
| 5163 | for (var i = 0, btnLength = groupButtons.length; i < btnLength; i++) { |
||
| 5164 | // continue creating toolbar even if a button doesn't exist |
||
| 5165 | if (!$.isFunction(tplButtonInfo[groupButtons[i]])) { continue; } |
||
| 5166 | toolbarHTML += tplButtonInfo[groupButtons[i]](langInfo, options); |
||
| 5167 | } |
||
| 5168 | toolbarHTML += '</div>'; |
||
| 5169 | } |
||
| 5170 | |||
| 5171 | toolbarHTML = '<div class="note-toolbar btn-toolbar">' + toolbarHTML + '</div>'; |
||
| 5172 | |||
| 5173 | var $toolbar = $(toolbarHTML).prependTo($editor); |
||
| 5174 | var keyMap = options.keyMap[agent.isMac ? 'mac' : 'pc']; |
||
| 5175 | createPalette($toolbar, options); |
||
| 5176 | createTooltip($toolbar, keyMap, 'bottom'); |
||
| 5177 | |||
| 5178 | //05. create Popover |
||
| 5179 | var $popover = $(tplPopovers(langInfo, options)).prependTo($editor); |
||
| 5180 | createPalette($popover, options); |
||
| 5181 | createTooltip($popover, keyMap); |
||
| 5182 | |||
| 5183 | //06. handle(control selection, ...) |
||
| 5184 | $(tplHandles()).prependTo($editor); |
||
| 5185 | |||
| 5186 | //07. create Dialog |
||
| 5187 | var $dialog = $(tplDialogs(langInfo, options)).prependTo($editor); |
||
| 5188 | $dialog.find('button.close, a.modal-close').click(function () { |
||
| 5189 | $(this).closest('.modal').modal('hide'); |
||
| 5190 | }); |
||
| 5191 | |||
| 5192 | //08. create Dropzone |
||
| 5193 | $('<div class="note-dropzone"><div class="note-dropzone-message"></div></div>').prependTo($editor); |
||
| 5194 | |||
| 5195 | //09. Editor/Holder switch |
||
| 5196 | $editor.insertAfter($holder); |
||
| 5197 | $holder.hide(); |
||
| 5198 | }; |
||
| 5199 | |||
| 5200 | this.noteEditorFromHolder = function ($holder) { |
||
| 5201 | if ($holder.hasClass('note-air-editor')) { |
||
| 5202 | return $holder; |
||
| 5203 | } else if ($holder.next().hasClass('note-editor')) { |
||
| 5204 | return $holder.next(); |
||
| 5205 | } else { |
||
| 5206 | return $(); |
||
| 5207 | } |
||
| 5208 | }; |
||
| 5209 | |||
| 5210 | /** |
||
| 5211 | * create summernote layout |
||
| 5212 | * |
||
| 5213 | * @param {jQuery} $holder |
||
| 5214 | * @param {Object} options |
||
| 5215 | */ |
||
| 5216 | this.createLayout = function ($holder, options) { |
||
| 5217 | if (this.noteEditorFromHolder($holder).length) { |
||
| 5218 | return; |
||
| 5219 | } |
||
| 5220 | |||
| 5221 | if (options.airMode) { |
||
| 5222 | this.createLayoutByAirMode($holder, options); |
||
| 5223 | } else { |
||
| 5224 | this.createLayoutByFrame($holder, options); |
||
| 5225 | } |
||
| 5226 | }; |
||
| 5227 | |||
| 5228 | /** |
||
| 5229 | * returns layoutInfo from holder |
||
| 5230 | * |
||
| 5231 | * @param {jQuery} $holder - placeholder |
||
| 5232 | * @returns {Object} |
||
| 5233 | */ |
||
| 5234 | this.layoutInfoFromHolder = function ($holder) { |
||
| 5235 | var $editor = this.noteEditorFromHolder($holder); |
||
| 5236 | if (!$editor.length) { return; } |
||
| 5237 | |||
| 5238 | var layoutInfo = dom.buildLayoutInfo($editor); |
||
| 5239 | // cache all properties. |
||
| 5240 | for (var key in layoutInfo) { |
||
| 5241 | if (layoutInfo.hasOwnProperty(key)) { |
||
| 5242 | layoutInfo[key] = layoutInfo[key].call(); |
||
| 5243 | } |
||
| 5244 | } |
||
| 5245 | return layoutInfo; |
||
| 5246 | }; |
||
| 5247 | |||
| 5248 | /** |
||
| 5249 | * removeLayout |
||
| 5250 | * |
||
| 5251 | * @param {jQuery} $holder - placeholder |
||
| 5252 | * @param {Object} layoutInfo |
||
| 5253 | * @param {Object} options |
||
| 5254 | * |
||
| 5255 | */ |
||
| 5256 | this.removeLayout = function ($holder, layoutInfo, options) { |
||
| 5257 | if (options.airMode) { |
||
| 5258 | $holder.removeClass('note-air-editor note-editable') |
||
| 5259 | .removeAttr('id contentEditable'); |
||
| 5260 | |||
| 5261 | layoutInfo.popover.remove(); |
||
| 5262 | layoutInfo.handle.remove(); |
||
| 5263 | layoutInfo.dialog.remove(); |
||
| 5264 | } else { |
||
| 5265 | $holder.html(layoutInfo.editable.html()); |
||
| 5266 | |||
| 5267 | layoutInfo.editor.remove(); |
||
| 5268 | $holder.show(); |
||
| 5269 | } |
||
| 5270 | }; |
||
| 5271 | }; |
||
| 5272 | |||
| 5273 | // jQuery namespace for summernote |
||
| 5274 | $.summernote = $.summernote || {}; |
||
| 5275 | |||
| 5276 | // extends default `settings` |
||
| 5277 | $.extend($.summernote, settings); |
||
| 5278 | |||
| 5279 | var renderer = new Renderer(); |
||
| 5280 | var eventHandler = new EventHandler(); |
||
| 5281 | |||
| 5282 | /** |
||
| 5283 | * extend jquery fn |
||
| 5284 | */ |
||
| 5285 | $.fn.extend({ |
||
| 5286 | /** |
||
| 5287 | * initialize summernote |
||
| 5288 | * - create editor layout and attach Mouse and keyboard events. |
||
| 5289 | * |
||
| 5290 | * @param {Object} options |
||
| 5291 | * @returns {this} |
||
| 5292 | */ |
||
| 5293 | summernote: function (options) { |
||
| 5294 | // extend default options |
||
| 5295 | options = $.extend({}, $.summernote.options, options); |
||
| 5296 | |||
| 5297 | this.each(function (idx, elHolder) { |
||
| 5298 | var $holder = $(elHolder); |
||
| 5299 | |||
| 5300 | // createLayout with options |
||
| 5301 | renderer.createLayout($holder, options); |
||
| 5302 | |||
| 5303 | var info = renderer.layoutInfoFromHolder($holder); |
||
| 5304 | eventHandler.attach(info, options); |
||
| 5305 | |||
| 5306 | // Textarea: auto filling the code before form submit. |
||
| 5307 | if (dom.isTextarea($holder[0])) { |
||
| 5308 | $holder.closest('form').submit(function () { |
||
| 5309 | $holder.val($holder.code()); |
||
| 5310 | }); |
||
| 5311 | } |
||
| 5312 | }); |
||
| 5313 | |||
| 5314 | // focus on first editable element |
||
| 5315 | if (this.first().length && options.focus) { |
||
| 5316 | var info = renderer.layoutInfoFromHolder(this.first()); |
||
| 5317 | info.editable.focus(); |
||
| 5318 | } |
||
| 5319 | |||
| 5320 | // callback on init |
||
| 5321 | if (this.length && options.oninit) { |
||
| 5322 | options.oninit(); |
||
| 5323 | } |
||
| 5324 | |||
| 5325 | return this; |
||
| 5326 | }, |
||
| 5327 | // |
||
| 5328 | |||
| 5329 | /** |
||
| 5330 | * get the HTML contents of note or set the HTML contents of note. |
||
| 5331 | * |
||
| 5332 | * @param {String} [sHTML] - HTML contents(optional, set) |
||
| 5333 | * @returns {this|String} - context(set) or HTML contents of note(get). |
||
| 5334 | */ |
||
| 5335 | code: function (sHTML) { |
||
| 5336 | // get the HTML contents of note |
||
| 5337 | if (sHTML === undefined) { |
||
| 5338 | var $holder = this.first(); |
||
| 5339 | if (!$holder.length) { return; } |
||
| 5340 | var info = renderer.layoutInfoFromHolder($holder); |
||
| 5341 | if (!!(info && info.editable)) { |
||
| 5342 | var isCodeview = info.editor.hasClass('codeview'); |
||
| 5343 | if (isCodeview && agent.hasCodeMirror) { |
||
| 5344 | info.codable.data('cmEditor').save(); |
||
| 5345 | } |
||
| 5346 | return isCodeview ? info.codable.val() : info.editable.html(); |
||
| 5347 | } |
||
| 5348 | return dom.isTextarea($holder[0]) ? $holder.val() : $holder.html(); |
||
| 5349 | } |
||
| 5350 | |||
| 5351 | // set the HTML contents of note |
||
| 5352 | this.each(function (i, elHolder) { |
||
| 5353 | var info = renderer.layoutInfoFromHolder($(elHolder)); |
||
| 5354 | if (info && info.editable) { info.editable.html(sHTML); } |
||
| 5355 | }); |
||
| 5356 | |||
| 5357 | return this; |
||
| 5358 | }, |
||
| 5359 | |||
| 5360 | /** |
||
| 5361 | * destroy Editor Layout and dettach Key and Mouse Event |
||
| 5362 | * @returns {this} |
||
| 5363 | */ |
||
| 5364 | destroy: function () { |
||
| 5365 | this.each(function (idx, elHolder) { |
||
| 5366 | var $holder = $(elHolder); |
||
| 5367 | |||
| 5368 | var info = renderer.layoutInfoFromHolder($holder); |
||
| 5369 | if (!info || !info.editable) { return; } |
||
| 5370 | |||
| 5371 | var options = info.editor.data('options'); |
||
| 5372 | |||
| 5373 | eventHandler.dettach(info, options); |
||
| 5374 | renderer.removeLayout($holder, info, options); |
||
| 5375 | }); |
||
| 5376 | |||
| 5377 | return this; |
||
| 5378 | } |
||
| 5379 | }); |
||
| 5380 | })); |