You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1559 lines
48 KiB

  1. /* ===================================================
  2. * bootstrap-markdown.js v2.10.0
  3. * http://github.com/toopay/bootstrap-markdown
  4. * ===================================================
  5. * Copyright 2013-2016 Taufan Aditya
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ========================================================== */
  19. (function(factory) {
  20. if (typeof define === "function" && define.amd) {
  21. // RequireJS
  22. define(["jquery"], factory);
  23. } else if (typeof exports === 'object') {
  24. // Backbone.js
  25. factory(require('jquery'));
  26. } else {
  27. // jQuery plugin
  28. factory(jQuery);
  29. }
  30. }(function($) {
  31. "use strict";
  32. /* MARKDOWN CLASS DEFINITION
  33. * ========================== */
  34. var Markdown = function(element, options) {
  35. // @TODO : remove this BC on next major release
  36. // @see : https://github.com/toopay/bootstrap-markdown/issues/109
  37. var opts = ['autofocus', 'savable', 'hideable', 'width',
  38. 'height', 'resize', 'iconlibrary', 'language',
  39. 'footer', 'fullscreen', 'hiddenButtons', 'disabledButtons'
  40. ];
  41. $.each(opts, function(_, opt) {
  42. if (typeof $(element).data(opt) !== 'undefined') {
  43. options = typeof options == 'object' ? options : {};
  44. options[opt] = $(element).data(opt);
  45. }
  46. });
  47. // End BC
  48. // Class Properties
  49. this.$ns = 'bootstrap-markdown';
  50. this.$element = $(element);
  51. this.$editable = {
  52. el: null,
  53. type: null,
  54. attrKeys: [],
  55. attrValues: [],
  56. content: null
  57. };
  58. this.$options = $.extend(true, {}, $.fn.markdown.defaults, options, this.$element.data('options'));
  59. this.$oldContent = null;
  60. this.$isPreview = false;
  61. this.$isFullscreen = false;
  62. this.$editor = null;
  63. this.$textarea = null;
  64. this.$handler = [];
  65. this.$callback = [];
  66. this.$nextTab = [];
  67. this.showEditor();
  68. };
  69. Markdown.prototype = {
  70. constructor: Markdown,
  71. __alterButtons: function(name, alter) {
  72. var handler = this.$handler,
  73. isAll = (name == 'all'),
  74. that = this;
  75. $.each(handler, function(k, v) {
  76. var halt = true;
  77. if (isAll) {
  78. halt = false;
  79. } else {
  80. halt = v.indexOf(name) < 0;
  81. }
  82. if (halt === false) {
  83. alter(that.$editor.find('button[data-handler="' + v + '"]'));
  84. }
  85. });
  86. },
  87. __buildButtons: function(buttonsArray, container) {
  88. var i,
  89. ns = this.$ns,
  90. handler = this.$handler,
  91. callback = this.$callback;
  92. for (i = 0; i < buttonsArray.length; i++) {
  93. // Build each group container
  94. var y, btnGroups = buttonsArray[i];
  95. for (y = 0; y < btnGroups.length; y++) {
  96. // Build each button group
  97. var z,
  98. buttons = btnGroups[y].data,
  99. btnGroupContainer = $('<div/>', {
  100. 'class': 'btn-group'
  101. });
  102. for (z = 0; z < buttons.length; z++) {
  103. var button = buttons[z],
  104. buttonContainer, buttonIconContainer,
  105. buttonHandler = ns + '-' + button.name,
  106. buttonIcon = this.__getIcon(button),
  107. btnText = button.btnText ? button.btnText : '',
  108. btnClass = button.btnClass ? button.btnClass : 'btn',
  109. tabIndex = button.tabIndex ? button.tabIndex : '-1',
  110. hotkey = typeof button.hotkey !== 'undefined' ? button.hotkey : '',
  111. hotkeyCaption = typeof jQuery.hotkeys !== 'undefined' && hotkey !== '' ? ' (' + hotkey + ')' : '';
  112. // Construct the button object
  113. buttonContainer = $('<button></button>');
  114. buttonContainer.text(' ' + this.__localize(btnText)).addClass('btn-default btn-sm').addClass(btnClass);
  115. if (btnClass.match(/btn\-(primary|success|info|warning|danger|link)/)) {
  116. buttonContainer.removeClass('btn-default');
  117. }
  118. buttonContainer.attr({
  119. 'type': 'button',
  120. 'title': this.__localize(button.title) + hotkeyCaption,
  121. 'tabindex': tabIndex,
  122. 'data-provider': ns,
  123. 'data-handler': buttonHandler,
  124. 'data-hotkey': hotkey
  125. });
  126. if (button.toggle === true) {
  127. buttonContainer.attr('data-toggle', 'button');
  128. }
  129. buttonIconContainer = $('<span/>');
  130. buttonIconContainer.addClass(buttonIcon);
  131. buttonIconContainer.prependTo(buttonContainer);
  132. // Attach the button object
  133. btnGroupContainer.append(buttonContainer);
  134. // Register handler and callback
  135. handler.push(buttonHandler);
  136. callback.push(button.callback);
  137. }
  138. // Attach the button group into container DOM
  139. container.append(btnGroupContainer);
  140. }
  141. }
  142. return container;
  143. },
  144. __setListener: function() {
  145. // Set size and resizable Properties
  146. var hasRows = typeof this.$textarea.attr('rows') !== 'undefined',
  147. maxRows = this.$textarea.val().split("\n").length > 5 ? this.$textarea.val().split("\n").length : '5',
  148. rowsVal = hasRows ? this.$textarea.attr('rows') : maxRows;
  149. this.$textarea.attr('rows', rowsVal);
  150. if (this.$options.resize) {
  151. this.$textarea.css('resize', this.$options.resize);
  152. }
  153. // Re-attach markdown data
  154. this.$textarea.data('markdown', this);
  155. },
  156. __setEventListeners: function() {
  157. this.$textarea.on({
  158. 'focus': $.proxy(this.focus, this),
  159. 'keyup': $.proxy(this.keyup, this),
  160. 'change': $.proxy(this.change, this),
  161. 'select': $.proxy(this.select, this)
  162. });
  163. if (this.eventSupported('keydown')) {
  164. this.$textarea.on('keydown', $.proxy(this.keydown, this));
  165. }
  166. if (this.eventSupported('keypress')) {
  167. this.$textarea.on('keypress', $.proxy(this.keypress, this));
  168. }
  169. },
  170. __handle: function(e) {
  171. var target = $(e.currentTarget),
  172. handler = this.$handler,
  173. callback = this.$callback,
  174. handlerName = target.attr('data-handler'),
  175. callbackIndex = handler.indexOf(handlerName),
  176. callbackHandler = callback[callbackIndex];
  177. // Trigger the focusin
  178. $(e.currentTarget).focus();
  179. callbackHandler(this);
  180. // Trigger onChange for each button handle
  181. this.change(this);
  182. // Unless it was the save handler,
  183. // focusin the textarea
  184. if (handlerName.indexOf('cmdSave') < 0) {
  185. this.$textarea.focus();
  186. }
  187. e.preventDefault();
  188. },
  189. __localize: function(string) {
  190. var messages = $.fn.markdown.messages,
  191. language = this.$options.language;
  192. if (
  193. typeof messages !== 'undefined' &&
  194. typeof messages[language] !== 'undefined' &&
  195. typeof messages[language][string] !== 'undefined'
  196. ) {
  197. return messages[language][string];
  198. }
  199. return string;
  200. },
  201. __getIcon: function(src) {
  202. if(typeof src == 'object'){
  203. var customIcon = this.$options.customIcons[src.name];
  204. return typeof customIcon == 'undefined' ? src.icon[this.$options.iconlibrary] : customIcon;
  205. } else {
  206. return src;
  207. }
  208. },
  209. setFullscreen: function(mode) {
  210. var $editor = this.$editor,
  211. $textarea = this.$textarea;
  212. if (mode === true) {
  213. $editor.addClass('md-fullscreen-mode');
  214. $('body').addClass('md-nooverflow');
  215. this.$options.onFullscreen(this);
  216. } else {
  217. $editor.removeClass('md-fullscreen-mode');
  218. $('body').removeClass('md-nooverflow');
  219. this.$options.onFullscreenExit(this);
  220. if (this.$isPreview === true)
  221. this.hidePreview().showPreview();
  222. }
  223. this.$isFullscreen = mode;
  224. $textarea.focus();
  225. },
  226. showEditor: function() {
  227. var instance = this,
  228. textarea,
  229. ns = this.$ns,
  230. container = this.$element,
  231. originalHeigth = container.css('height'),
  232. originalWidth = container.css('width'),
  233. editable = this.$editable,
  234. handler = this.$handler,
  235. callback = this.$callback,
  236. options = this.$options,
  237. editor = $('<div/>', {
  238. 'class': 'md-editor',
  239. click: function() {
  240. instance.focus();
  241. }
  242. });
  243. // Prepare the editor
  244. if (this.$editor === null) {
  245. // Create the panel
  246. var editorHeader = $('<div/>', {
  247. 'class': 'md-header btn-toolbar'
  248. });
  249. // Merge the main & additional button groups together
  250. var allBtnGroups = [];
  251. if (options.buttons.length > 0) allBtnGroups = allBtnGroups.concat(options.buttons[0]);
  252. if (options.additionalButtons.length > 0) {
  253. // iterate the additional button groups
  254. $.each(options.additionalButtons[0], function(idx, buttonGroup) {
  255. // see if the group name of the additional group matches an existing group
  256. var matchingGroups = $.grep(allBtnGroups, function(allButtonGroup, allIdx) {
  257. return allButtonGroup.name === buttonGroup.name;
  258. });
  259. // if it matches add the additional buttons to that group, if not just add it to the all buttons group
  260. if (matchingGroups.length > 0) {
  261. matchingGroups[0].data = matchingGroups[0].data.concat(buttonGroup.data);
  262. } else {
  263. allBtnGroups.push(options.additionalButtons[0][idx]);
  264. }
  265. });
  266. }
  267. // Reduce and/or reorder the button groups
  268. if (options.reorderButtonGroups.length > 0) {
  269. allBtnGroups = allBtnGroups
  270. .filter(function(btnGroup) {
  271. return options.reorderButtonGroups.indexOf(btnGroup.name) > -1;
  272. })
  273. .sort(function(a, b) {
  274. if (options.reorderButtonGroups.indexOf(a.name) < options.reorderButtonGroups.indexOf(b.name)) return -1;
  275. if (options.reorderButtonGroups.indexOf(a.name) > options.reorderButtonGroups.indexOf(b.name)) return 1;
  276. return 0;
  277. });
  278. }
  279. // Build the buttons
  280. if (allBtnGroups.length > 0) {
  281. editorHeader = this.__buildButtons([allBtnGroups], editorHeader);
  282. }
  283. if (options.fullscreen.enable) {
  284. editorHeader.append('<div class="md-controls"><a class="md-control md-control-fullscreen" href="#"><span class="' + this.__getIcon(options.fullscreen.icons.fullscreenOn) + '"></span></a></div>').on('click', '.md-control-fullscreen', function(e) {
  285. e.preventDefault();
  286. instance.setFullscreen(true);
  287. });
  288. }
  289. editor.append(editorHeader);
  290. // Wrap the textarea
  291. if (container.is('textarea')) {
  292. container.before(editor);
  293. textarea = container;
  294. textarea.addClass('md-input');
  295. editor.append(textarea);
  296. } else {
  297. var rawContent = (typeof toMarkdown == 'function') ? toMarkdown(container.html()) : container.html(),
  298. currentContent = $.trim(rawContent);
  299. // This is some arbitrary content that could be edited
  300. textarea = $('<textarea/>', {
  301. 'class': 'md-input',
  302. 'val': currentContent
  303. });
  304. editor.append(textarea);
  305. // Save the editable
  306. editable.el = container;
  307. editable.type = container.prop('tagName').toLowerCase();
  308. editable.content = container.html();
  309. $(container[0].attributes).each(function() {
  310. editable.attrKeys.push(this.nodeName);
  311. editable.attrValues.push(this.nodeValue);
  312. });
  313. // Set editor to block the original container
  314. container.replaceWith(editor);
  315. }
  316. var editorFooter = $('<div/>', {
  317. 'class': 'md-footer'
  318. }),
  319. createFooter = false,
  320. footer = '';
  321. // Create the footer if savable
  322. if (options.savable) {
  323. createFooter = true;
  324. var saveHandler = 'cmdSave';
  325. // Register handler and callback
  326. handler.push(saveHandler);
  327. callback.push(options.onSave);
  328. editorFooter.append('<button class="btn btn-success" data-provider="' +
  329. ns +
  330. '" data-handler="' +
  331. saveHandler +
  332. '"><i class="icon icon-white icon-ok"></i> ' +
  333. this.__localize('Save') +
  334. '</button>');
  335. }
  336. footer = typeof options.footer === 'function' ? options.footer(this) : options.footer;
  337. if ($.trim(footer) !== '') {
  338. createFooter = true;
  339. editorFooter.append(footer);
  340. }
  341. if (createFooter) editor.append(editorFooter);
  342. // Set width
  343. if (options.width && options.width !== 'inherit') {
  344. if (jQuery.isNumeric(options.width)) {
  345. editor.css('display', 'table');
  346. textarea.css('width', options.width + 'px');
  347. } else {
  348. editor.addClass(options.width);
  349. }
  350. }
  351. // Set height
  352. if (options.height && options.height !== 'inherit') {
  353. if (jQuery.isNumeric(options.height)) {
  354. var height = options.height;
  355. if (editorHeader) height = Math.max(0, height - editorHeader.outerHeight());
  356. if (editorFooter) height = Math.max(0, height - editorFooter.outerHeight());
  357. textarea.css('height', height + 'px');
  358. } else {
  359. editor.addClass(options.height);
  360. }
  361. }
  362. // Reference
  363. this.$editor = editor;
  364. this.$textarea = textarea;
  365. this.$editable = editable;
  366. this.$oldContent = this.getContent();
  367. this.__setListener();
  368. this.__setEventListeners();
  369. // Set editor attributes, data short-hand API and listener
  370. this.$editor.attr('id', (new Date()).getTime());
  371. this.$editor.on('click', '[data-provider="bootstrap-markdown"]', $.proxy(this.__handle, this));
  372. if (this.$element.is(':disabled') || this.$element.is('[readonly]')) {
  373. this.$editor.addClass('md-editor-disabled');
  374. this.disableButtons('all');
  375. }
  376. if (this.eventSupported('keydown') && typeof jQuery.hotkeys === 'object') {
  377. editorHeader.find('[data-provider="bootstrap-markdown"]').each(function() {
  378. var $button = $(this),
  379. hotkey = $button.attr('data-hotkey');
  380. if (hotkey.toLowerCase() !== '') {
  381. textarea.bind('keydown', hotkey, function() {
  382. $button.trigger('click');
  383. return false;
  384. });
  385. }
  386. });
  387. }
  388. if (options.initialstate === 'preview') {
  389. this.showPreview();
  390. } else if (options.initialstate === 'fullscreen' && options.fullscreen.enable) {
  391. this.setFullscreen(true);
  392. }
  393. } else {
  394. this.$editor.show();
  395. }
  396. if (options.autofocus) {
  397. this.$textarea.focus();
  398. this.$editor.addClass('active');
  399. }
  400. if (options.fullscreen.enable && options.fullscreen !== false) {
  401. this.$editor.append('<div class="md-fullscreen-controls">' +
  402. '<a href="#" class="exit-fullscreen" title="Exit fullscreen"><span class="' + this.__getIcon(options.fullscreen.icons.fullscreenOff) + '">' +
  403. '</span></a>' +
  404. '</div>');
  405. this.$editor.on('click', '.exit-fullscreen', function(e) {
  406. e.preventDefault();
  407. instance.setFullscreen(false);
  408. });
  409. }
  410. // hide hidden buttons from options
  411. this.hideButtons(options.hiddenButtons);
  412. // disable disabled buttons from options
  413. this.disableButtons(options.disabledButtons);
  414. // enable dropZone if available and configured
  415. if (options.dropZoneOptions) {
  416. if (this.$editor.dropzone) {
  417. if(!options.dropZoneOptions.init) {
  418. options.dropZoneOptions.init = function() {
  419. var caretPos = 0;
  420. this.on('drop', function(e) {
  421. caretPos = textarea.prop('selectionStart');
  422. });
  423. this.on('success', function(file, path) {
  424. var text = textarea.val();
  425. textarea.val(text.substring(0, caretPos) + '\n![description](' + path + ')\n' + text.substring(caretPos));
  426. });
  427. this.on('error', function(file, error, xhr) {
  428. console.log('Error:', error);
  429. });
  430. };
  431. }
  432. this.$editor.addClass('dropzone');
  433. this.$editor.dropzone(options.dropZoneOptions);
  434. } else {
  435. console.log('dropZoneOptions was configured, but DropZone was not detected.');
  436. }
  437. }
  438. // enable data-uris via drag and drop
  439. if (options.enableDropDataUri === true) {
  440. this.$editor.on('drop', function(e) {
  441. var caretPos = textarea.prop('selectionStart');
  442. e.stopPropagation();
  443. e.preventDefault();
  444. $.each(e.originalEvent.dataTransfer.files, function(index, file){
  445. var fileReader = new FileReader();
  446. fileReader.onload = (function(file) {
  447. var type = file.type.split('/')[0];
  448. return function(e) {
  449. var text = textarea.val();
  450. if (type === 'image')
  451. textarea.val(text.substring(0, caretPos) + '\n<img src="'+ e.target.result +'" />\n' + text.substring(caretPos) );
  452. else
  453. textarea.val(text.substring(0, caretPos) + '\n<a href="'+ e.target.result +'">Download ' + file.name + '</a>\n' + text.substring(caretPos) );
  454. };
  455. })(file);
  456. fileReader.readAsDataURL(file);
  457. });
  458. });
  459. }
  460. // Trigger the onShow hook
  461. options.onShow(this);
  462. return this;
  463. },
  464. parseContent: function(val) {
  465. var content;
  466. // parse with supported markdown parser
  467. val = val || this.$textarea.val();
  468. if (this.$options.parser) {
  469. content = this.$options.parser(val);
  470. } else if (typeof markdown == 'object') {
  471. content = markdown.toHTML(val);
  472. } else if (typeof marked == 'function') {
  473. content = marked(val);
  474. } else {
  475. content = val;
  476. }
  477. return content;
  478. },
  479. showPreview: function() {
  480. var options = this.$options,
  481. container = this.$textarea,
  482. afterContainer = container.next(),
  483. replacementContainer = $('<div/>', {
  484. 'class': 'md-preview',
  485. 'data-provider': 'markdown-preview'
  486. }),
  487. content,
  488. callbackContent;
  489. if (this.$isPreview === true) {
  490. // Avoid sequenced element creation on misused scenario
  491. // @see https://github.com/toopay/bootstrap-markdown/issues/170
  492. return this;
  493. }
  494. // Give flag that tells the editor to enter preview mode
  495. this.$isPreview = true;
  496. // Disable all buttons
  497. this.disableButtons('all').enableButtons('cmdPreview');
  498. // Try to get the content from callback
  499. callbackContent = options.onPreview(this, replacementContainer);
  500. // Set the content based on the callback content if string, otherwise parse value from textarea
  501. content = typeof callbackContent == 'string' ? callbackContent : this.parseContent();
  502. // Build preview element
  503. replacementContainer.html(content);
  504. if (afterContainer && afterContainer.attr('class') == 'md-footer') {
  505. // If there is footer element, insert the preview container before it
  506. replacementContainer.insertBefore(afterContainer);
  507. } else {
  508. // Otherwise, just append it after textarea
  509. container.parent().append(replacementContainer);
  510. }
  511. // Set the preview element dimensions
  512. replacementContainer.css({
  513. "width": container.outerWidth() + 'px',
  514. "min-height": container.outerHeight() + 'px',
  515. "height": "auto"
  516. });
  517. if (this.$options.resize) {
  518. replacementContainer.css('resize', this.$options.resize);
  519. }
  520. // Hide the last-active textarea
  521. container.hide();
  522. // Attach the editor instances
  523. replacementContainer.data('markdown', this);
  524. if (this.$element.is(':disabled') || this.$element.is('[readonly]')) {
  525. this.$editor.addClass('md-editor-disabled');
  526. this.disableButtons('all');
  527. }
  528. return this;
  529. },
  530. hidePreview: function() {
  531. // Give flag that tells the editor to quit preview mode
  532. this.$isPreview = false;
  533. // Obtain the preview container
  534. var container = this.$editor.find('div[data-provider="markdown-preview"]');
  535. // Remove the preview container
  536. container.remove();
  537. // Enable all buttons
  538. this.enableButtons('all');
  539. // Disable configured disabled buttons
  540. this.disableButtons(this.$options.disabledButtons);
  541. // Perform any callbacks
  542. this.$options.onPreviewEnd(this);
  543. // Back to the editor
  544. this.$textarea.show();
  545. this.__setListener();
  546. return this;
  547. },
  548. isDirty: function() {
  549. return this.$oldContent != this.getContent();
  550. },
  551. getContent: function() {
  552. return this.$textarea.val();
  553. },
  554. setContent: function(content) {
  555. this.$textarea.val(content);
  556. return this;
  557. },
  558. findSelection: function(chunk) {
  559. var content = this.getContent(),
  560. startChunkPosition;
  561. if (startChunkPosition = content.indexOf(chunk), startChunkPosition >= 0 && chunk.length > 0) {
  562. var oldSelection = this.getSelection(),
  563. selection;
  564. this.setSelection(startChunkPosition, startChunkPosition + chunk.length);
  565. selection = this.getSelection();
  566. this.setSelection(oldSelection.start, oldSelection.end);
  567. return selection;
  568. } else {
  569. return null;
  570. }
  571. },
  572. getSelection: function() {
  573. var e = this.$textarea[0];
  574. return (
  575. ('selectionStart' in e && function() {
  576. var l = e.selectionEnd - e.selectionStart;
  577. return {
  578. start: e.selectionStart,
  579. end: e.selectionEnd,
  580. length: l,
  581. text: e.value.substr(e.selectionStart, l)
  582. };
  583. }) ||
  584. /* browser not supported */
  585. function() {
  586. return null;
  587. }
  588. )();
  589. },
  590. setSelection: function(start, end) {
  591. var e = this.$textarea[0];
  592. return (
  593. ('selectionStart' in e && function() {
  594. e.selectionStart = start;
  595. e.selectionEnd = end;
  596. return;
  597. }) ||
  598. /* browser not supported */
  599. function() {
  600. return null;
  601. }
  602. )();
  603. },
  604. replaceSelection: function(text) {
  605. var e = this.$textarea[0];
  606. return (
  607. ('selectionStart' in e && function() {
  608. e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
  609. // Set cursor to the last replacement end
  610. e.selectionStart = e.value.length;
  611. return this;
  612. }) ||
  613. /* browser not supported */
  614. function() {
  615. e.value += text;
  616. return jQuery(e);
  617. }
  618. )();
  619. },
  620. getNextTab: function() {
  621. // Shift the nextTab
  622. if (this.$nextTab.length === 0) {
  623. return null;
  624. } else {
  625. var nextTab, tab = this.$nextTab.shift();
  626. if (typeof tab == 'function') {
  627. nextTab = tab();
  628. } else if (typeof tab == 'object' && tab.length > 0) {
  629. nextTab = tab;
  630. }
  631. return nextTab;
  632. }
  633. },
  634. setNextTab: function(start, end) {
  635. // Push new selection into nextTab collections
  636. if (typeof start == 'string') {
  637. var that = this;
  638. this.$nextTab.push(function() {
  639. return that.findSelection(start);
  640. });
  641. } else if (typeof start == 'number' && typeof end == 'number') {
  642. var oldSelection = this.getSelection();
  643. this.setSelection(start, end);
  644. this.$nextTab.push(this.getSelection());
  645. this.setSelection(oldSelection.start, oldSelection.end);
  646. }
  647. return;
  648. },
  649. __parseButtonNameParam: function(names) {
  650. return typeof names == 'string' ?
  651. names.split(' ') :
  652. names;
  653. },
  654. enableButtons: function(name) {
  655. var buttons = this.__parseButtonNameParam(name),
  656. that = this;
  657. $.each(buttons, function(i, v) {
  658. that.__alterButtons(buttons[i], function(el) {
  659. el.removeAttr('disabled');
  660. });
  661. });
  662. return this;
  663. },
  664. disableButtons: function(name) {
  665. var buttons = this.__parseButtonNameParam(name),
  666. that = this;
  667. $.each(buttons, function(i, v) {
  668. that.__alterButtons(buttons[i], function(el) {
  669. el.attr('disabled', 'disabled');
  670. });
  671. });
  672. return this;
  673. },
  674. hideButtons: function(name) {
  675. var buttons = this.__parseButtonNameParam(name),
  676. that = this;
  677. $.each(buttons, function(i, v) {
  678. that.__alterButtons(buttons[i], function(el) {
  679. el.addClass('hidden');
  680. });
  681. });
  682. return this;
  683. },
  684. showButtons: function(name) {
  685. var buttons = this.__parseButtonNameParam(name),
  686. that = this;
  687. $.each(buttons, function(i, v) {
  688. that.__alterButtons(buttons[i], function(el) {
  689. el.removeClass('hidden');
  690. });
  691. });
  692. return this;
  693. },
  694. eventSupported: function(eventName) {
  695. var isSupported = eventName in this.$element;
  696. if (!isSupported) {
  697. this.$element.setAttribute(eventName, 'return;');
  698. isSupported = typeof this.$element[eventName] === 'function';
  699. }
  700. return isSupported;
  701. },
  702. keyup: function(e) {
  703. var blocked = false;
  704. switch (e.keyCode) {
  705. case 40: // down arrow
  706. case 38: // up arrow
  707. case 16: // shift
  708. case 17: // ctrl
  709. case 18: // alt
  710. break;
  711. case 9: // tab
  712. var nextTab;
  713. if (nextTab = this.getNextTab(), nextTab !== null) {
  714. // Get the nextTab if exists
  715. var that = this;
  716. setTimeout(function() {
  717. that.setSelection(nextTab.start, nextTab.end);
  718. }, 500);
  719. blocked = true;
  720. } else {
  721. // The next tab's memory contains nothing...
  722. // check the cursor position to determine tab action
  723. var cursor = this.getSelection();
  724. if (cursor.start == cursor.end && cursor.end == this.getContent().length) {
  725. // The cursor has reached the end of the content
  726. blocked = false;
  727. } else {
  728. // Put the cursor to the end
  729. this.setSelection(this.getContent().length, this.getContent().length);
  730. blocked = true;
  731. }
  732. }
  733. break;
  734. case 13: // enter
  735. blocked = false;
  736. var chars = this.getContent().split('');
  737. var enterIndex = this.getSelection().start;
  738. var priorNewlineIndex = -1; // initial line break at before index 0
  739. // traverse backwards through chars to check if last line break was num/bullet item
  740. for (var i = enterIndex - 2; i >= 0; i--) {
  741. if (chars[i] === '\n') {
  742. priorNewlineIndex = i;
  743. break;
  744. }
  745. }
  746. var charFollowingLastLineBreak = chars[priorNewlineIndex + 1];
  747. if (charFollowingLastLineBreak === '-') {
  748. this.addBullet(enterIndex);
  749. } else if ($.isNumeric(charFollowingLastLineBreak)) {
  750. var numBullet = this.getBulletNumber(priorNewlineIndex + 1);
  751. if (numBullet) {
  752. this.addNumberedBullet(enterIndex, numBullet);
  753. }
  754. }
  755. break;
  756. case 27: // escape
  757. if (this.$isFullscreen) this.setFullscreen(false);
  758. blocked = false;
  759. break;
  760. default:
  761. blocked = false;
  762. }
  763. if (blocked) {
  764. e.stopPropagation();
  765. e.preventDefault();
  766. }
  767. this.$options.onChange(this);
  768. },
  769. insertContent: function(index, content) {
  770. var firstHalf = this.getContent().slice(0, index);
  771. var secondHalf = this.getContent().slice(index + 1);
  772. this.setContent(firstHalf.concat(content).concat(secondHalf));
  773. },
  774. addBullet: function(index) {
  775. this.insertContent(index, '- \n');
  776. this.setSelection(index + 2, index + 2); // Put the cursor after the bullet
  777. },
  778. addNumberedBullet: function(index, num) {
  779. var numBullet = (num + 1) + '. \n';
  780. this.insertContent(index, numBullet);
  781. var prefixLength = num.toString().length + 2;
  782. this.setSelection(index + prefixLength, index + prefixLength); // Put the cursor after the number
  783. },
  784. getBulletNumber: function(startIndex) {
  785. var bulletNum = this.getContent().slice(startIndex).split('.')[0];
  786. return $.isNumeric(bulletNum) ? parseInt(bulletNum) : null;
  787. },
  788. change: function(e) {
  789. this.$options.onChange(this);
  790. return this;
  791. },
  792. select: function(e) {
  793. this.$options.onSelect(this);
  794. return this;
  795. },
  796. focus: function(e) {
  797. var options = this.$options,
  798. isHideable = options.hideable,
  799. editor = this.$editor;
  800. editor.addClass('active');
  801. // Blur other markdown(s)
  802. $(document).find('.md-editor').each(function() {
  803. if ($(this).attr('id') !== editor.attr('id')) {
  804. var attachedMarkdown;
  805. if (attachedMarkdown = $(this).find('textarea').data('markdown'),
  806. attachedMarkdown === null) {
  807. attachedMarkdown = $(this).find('div[data-provider="markdown-preview"]').data('markdown');
  808. }
  809. if (attachedMarkdown) {
  810. attachedMarkdown.blur();
  811. }
  812. }
  813. });
  814. // Trigger the onFocus hook
  815. options.onFocus(this);
  816. return this;
  817. },
  818. blur: function(e) {
  819. var options = this.$options,
  820. isHideable = options.hideable,
  821. editor = this.$editor,
  822. editable = this.$editable;
  823. if (editor.hasClass('active') || this.$element.parent().length === 0) {
  824. editor.removeClass('active');
  825. if (isHideable) {
  826. // Check for editable elements
  827. if (editable.el !== null) {
  828. // Build the original element
  829. var oldElement = $('<' + editable.type + '/>'),
  830. content = this.getContent(),
  831. currentContent = this.parseContent(content);
  832. $(editable.attrKeys).each(function(k, v) {
  833. oldElement.attr(editable.attrKeys[k], editable.attrValues[k]);
  834. });
  835. // Get the editor content
  836. oldElement.html(currentContent);
  837. editor.replaceWith(oldElement);
  838. } else {
  839. editor.hide();
  840. }
  841. }
  842. // Trigger the onBlur hook
  843. options.onBlur(this);
  844. }
  845. return this;
  846. }
  847. };
  848. /* MARKDOWN PLUGIN DEFINITION
  849. * ========================== */
  850. var old = $.fn.markdown;
  851. $.fn.markdown = function(option) {
  852. return this.each(function() {
  853. var $this = $(this),
  854. data = $this.data('markdown'),
  855. options = typeof option == 'object' && option;
  856. if (!data)
  857. $this.data('markdown', (data = new Markdown(this, options)));
  858. });
  859. };
  860. $.fn.markdown.messages = {};
  861. $.fn.markdown.defaults = {
  862. /* Editor Properties */
  863. autofocus: false,
  864. hideable: false,
  865. savable: false,
  866. width: 'inherit',
  867. height: 'inherit',
  868. resize: 'none',
  869. iconlibrary: 'glyph',
  870. language: 'en',
  871. initialstate: 'editor',
  872. parser: null,
  873. dropZoneOptions: null,
  874. enableDropDataUri: false,
  875. /* Buttons Properties */
  876. buttons: [
  877. [{
  878. name: 'groupFont',
  879. data: [{
  880. name: 'cmdBold',
  881. hotkey: 'Ctrl+B',
  882. title: 'Bold',
  883. icon: {
  884. glyph: 'glyphicon glyphicon-bold',
  885. fa: 'fa fa-bold',
  886. 'fa-3': 'icon-bold',
  887. octicons: 'octicon octicon-bold'
  888. },
  889. callback: function(e) {
  890. // Give/remove ** surround the selection
  891. var chunk, cursor, selected = e.getSelection(),
  892. content = e.getContent();
  893. if (selected.length === 0) {
  894. // Give extra word
  895. chunk = e.__localize('strong text');
  896. } else {
  897. chunk = selected.text;
  898. }
  899. // transform selection and set the cursor into chunked text
  900. if (content.substr(selected.start - 2, 2) === '**' &&
  901. content.substr(selected.end, 2) === '**') {
  902. e.setSelection(selected.start - 2, selected.end + 2);
  903. e.replaceSelection(chunk);
  904. cursor = selected.start - 2;
  905. } else {
  906. e.replaceSelection('**' + chunk + '**');
  907. cursor = selected.start + 2;
  908. }
  909. // Set the cursor
  910. e.setSelection(cursor, cursor + chunk.length);
  911. }
  912. }, {
  913. name: 'cmdItalic',
  914. title: 'Italic',
  915. hotkey: 'Ctrl+I',
  916. icon: {
  917. glyph: 'glyphicon glyphicon-italic',
  918. fa: 'fa fa-italic',
  919. 'fa-3': 'icon-italic',
  920. octicons: 'octicon octicon-italic'
  921. },
  922. callback: function(e) {
  923. // Give/remove * surround the selection
  924. var chunk, cursor, selected = e.getSelection(),
  925. content = e.getContent();
  926. if (selected.length === 0) {
  927. // Give extra word
  928. chunk = e.__localize('emphasized text');
  929. } else {
  930. chunk = selected.text;
  931. }
  932. // transform selection and set the cursor into chunked text
  933. if (content.substr(selected.start - 1, 1) === '_' &&
  934. content.substr(selected.end, 1) === '_') {
  935. e.setSelection(selected.start - 1, selected.end + 1);
  936. e.replaceSelection(chunk);
  937. cursor = selected.start - 1;
  938. } else {
  939. e.replaceSelection('_' + chunk + '_');
  940. cursor = selected.start + 1;
  941. }
  942. // Set the cursor
  943. e.setSelection(cursor, cursor + chunk.length);
  944. }
  945. }, {
  946. name: 'cmdHeading',
  947. title: 'Heading',
  948. hotkey: 'Ctrl+H',
  949. icon: {
  950. glyph: 'glyphicon glyphicon-header',
  951. fa: 'fa fa-header',
  952. 'fa-3': 'icon-font',
  953. octicons: 'octicon octicon-text-size'
  954. },
  955. callback: function(e) {
  956. // Append/remove ### surround the selection
  957. var chunk, cursor, selected = e.getSelection(),
  958. content = e.getContent(),
  959. pointer, prevChar;
  960. if (selected.length === 0) {
  961. // Give extra word
  962. chunk = e.__localize('heading text');
  963. } else {
  964. chunk = selected.text + '\n';
  965. }
  966. // transform selection and set the cursor into chunked text
  967. if ((pointer = 4, content.substr(selected.start - pointer, pointer) === '### ') ||
  968. (pointer = 3, content.substr(selected.start - pointer, pointer) === '###')) {
  969. e.setSelection(selected.start - pointer, selected.end);
  970. e.replaceSelection(chunk);
  971. cursor = selected.start - pointer;
  972. } else if (selected.start > 0 && (prevChar = content.substr(selected.start - 1, 1), !!prevChar && prevChar != '\n')) {
  973. e.replaceSelection('\n\n### ' + chunk);
  974. cursor = selected.start + 6;
  975. } else {
  976. // Empty string before element
  977. e.replaceSelection('### ' + chunk);
  978. cursor = selected.start + 4;
  979. }
  980. // Set the cursor
  981. e.setSelection(cursor, cursor + chunk.length);
  982. }
  983. }]
  984. }, {
  985. name: 'groupLink',
  986. data: [{
  987. name: 'cmdUrl',
  988. title: 'URL/Link',
  989. hotkey: 'Ctrl+L',
  990. icon: {
  991. glyph: 'glyphicon glyphicon-link',
  992. fa: 'fa fa-link',
  993. 'fa-3': 'icon-link',
  994. octicons: 'octicon octicon-link'
  995. },
  996. callback: function(e) {
  997. // Give [] surround the selection and prepend the link
  998. var chunk, cursor, selected = e.getSelection(),
  999. content = e.getContent(),
  1000. link;
  1001. if (selected.length === 0) {
  1002. // Give extra word
  1003. chunk = e.__localize('enter link description here');
  1004. } else {
  1005. chunk = selected.text;
  1006. }
  1007. link = prompt(e.__localize('Insert Hyperlink'), 'http://');
  1008. var urlRegex = new RegExp('^((http|https)://|(mailto:)|(//))[a-z0-9]', 'i');
  1009. if (link !== null && link !== '' && link !== 'http://' && urlRegex.test(link)) {
  1010. var sanitizedLink = $('<div>' + link + '</div>').text();
  1011. // transform selection and set the cursor into chunked text
  1012. e.replaceSelection('[' + chunk + '](' + sanitizedLink + ')');
  1013. cursor = selected.start + 1;
  1014. // Set the cursor
  1015. e.setSelection(cursor, cursor + chunk.length);
  1016. }
  1017. }
  1018. }, {
  1019. name: 'cmdImage',
  1020. title: 'Image',
  1021. hotkey: 'Ctrl+G',
  1022. icon: {
  1023. glyph: 'glyphicon glyphicon-picture',
  1024. fa: 'fa fa-picture-o',
  1025. 'fa-3': 'icon-picture',
  1026. octicons: 'octicon octicon-file-media'
  1027. },
  1028. callback: function(e) {
  1029. // Give ![] surround the selection and prepend the image link
  1030. var chunk, cursor, selected = e.getSelection(),
  1031. content = e.getContent(),
  1032. link;
  1033. if (selected.length === 0) {
  1034. // Give extra word
  1035. chunk = e.__localize('enter image description here');
  1036. } else {
  1037. chunk = selected.text;
  1038. }
  1039. link = prompt(e.__localize('Insert Image Hyperlink'), 'http://');
  1040. var urlRegex = new RegExp('^((http|https)://|(//))[a-z0-9]', 'i');
  1041. if (link !== null && link !== '' && link !== 'http://' && urlRegex.test(link)) {
  1042. var sanitizedLink = $('<div>' + link + '</div>').text();
  1043. // transform selection and set the cursor into chunked text
  1044. e.replaceSelection('![' + chunk + '](' + sanitizedLink + ' "' + e.__localize('enter image title here') + '")');
  1045. cursor = selected.start + 2;
  1046. // Set the next tab
  1047. e.setNextTab(e.__localize('enter image title here'));
  1048. // Set the cursor
  1049. e.setSelection(cursor, cursor + chunk.length);
  1050. }
  1051. }
  1052. }]
  1053. }, {
  1054. name: 'groupMisc',
  1055. data: [{
  1056. name: 'cmdList',
  1057. hotkey: 'Ctrl+U',
  1058. title: 'Unordered List',
  1059. icon: {
  1060. glyph: 'glyphicon glyphicon-list',
  1061. fa: 'fa fa-list',
  1062. 'fa-3': 'icon-list-ul',
  1063. octicons: 'octicon octicon-list-unordered'
  1064. },
  1065. callback: function(e) {
  1066. // Prepend/Give - surround the selection
  1067. var chunk, cursor, selected = e.getSelection(),
  1068. content = e.getContent();
  1069. // transform selection and set the cursor into chunked text
  1070. if (selected.length === 0) {
  1071. // Give extra word
  1072. chunk = e.__localize('list text here');
  1073. e.replaceSelection('- ' + chunk);
  1074. // Set the cursor
  1075. cursor = selected.start + 2;
  1076. } else {
  1077. if (selected.text.indexOf('\n') < 0) {
  1078. chunk = selected.text;
  1079. e.replaceSelection('- ' + chunk);
  1080. // Set the cursor
  1081. cursor = selected.start + 2;
  1082. } else {
  1083. var list = [];
  1084. list = selected.text.split('\n');
  1085. chunk = list[0];
  1086. $.each(list, function(k, v) {
  1087. list[k] = '- ' + v;
  1088. });
  1089. e.replaceSelection('\n\n' + list.join('\n'));
  1090. // Set the cursor
  1091. cursor = selected.start + 4;
  1092. }
  1093. }
  1094. // Set the cursor
  1095. e.setSelection(cursor, cursor + chunk.length);
  1096. }
  1097. }, {
  1098. name: 'cmdListO',
  1099. hotkey: 'Ctrl+O',
  1100. title: 'Ordered List',
  1101. icon: {
  1102. glyph: 'glyphicon glyphicon-th-list',
  1103. fa: 'fa fa-list-ol',
  1104. 'fa-3': 'icon-list-ol',
  1105. octicons: 'octicon octicon-list-ordered'
  1106. },
  1107. callback: function(e) {
  1108. // Prepend/Give - surround the selection
  1109. var chunk, cursor, selected = e.getSelection(),
  1110. content = e.getContent();
  1111. // transform selection and set the cursor into chunked text
  1112. if (selected.length === 0) {
  1113. // Give extra word
  1114. chunk = e.__localize('list text here');
  1115. e.replaceSelection('1. ' + chunk);
  1116. // Set the cursor
  1117. cursor = selected.start + 3;
  1118. } else {
  1119. if (selected.text.indexOf('\n') < 0) {
  1120. chunk = selected.text;
  1121. e.replaceSelection('1. ' + chunk);
  1122. // Set the cursor
  1123. cursor = selected.start + 3;
  1124. } else {
  1125. var i = 1;
  1126. var list = [];
  1127. list = selected.text.split('\n');
  1128. chunk = list[0];
  1129. $.each(list, function(k, v) {
  1130. list[k] = i + '. ' + v;
  1131. i++;
  1132. });
  1133. e.replaceSelection('\n\n' + list.join('\n'));
  1134. // Set the cursor
  1135. cursor = selected.start + 5;
  1136. }
  1137. }
  1138. // Set the cursor
  1139. e.setSelection(cursor, cursor + chunk.length);
  1140. }
  1141. }, {
  1142. name: 'cmdCode',
  1143. hotkey: 'Ctrl+K',
  1144. title: 'Code',
  1145. icon: {
  1146. glyph: 'glyphicon glyphicon-console',
  1147. fa: 'fa fa-code',
  1148. 'fa-3': 'icon-code',
  1149. octicons: 'octicon octicon-code'
  1150. },
  1151. callback: function(e) {
  1152. // Give/remove ** surround the selection
  1153. var chunk, cursor, selected = e.getSelection(),
  1154. content = e.getContent();
  1155. if (selected.length === 0) {
  1156. // Give extra word
  1157. chunk = e.__localize('code text here');
  1158. } else {
  1159. chunk = selected.text;
  1160. }
  1161. // transform selection and set the cursor into chunked text
  1162. if (content.substr(selected.start - 4, 4) === '```\n' &&
  1163. content.substr(selected.end, 4) === '\n```') {
  1164. e.setSelection(selected.start - 4, selected.end + 4);
  1165. e.replaceSelection(chunk);
  1166. cursor = selected.start - 4;
  1167. } else if (content.substr(selected.start - 1, 1) === '`' &&
  1168. content.substr(selected.end, 1) === '`') {
  1169. e.setSelection(selected.start - 1, selected.end + 1);
  1170. e.replaceSelection(chunk);
  1171. cursor = selected.start - 1;
  1172. } else if (content.indexOf('\n') > -1) {
  1173. e.replaceSelection('```\n' + chunk + '\n```');
  1174. cursor = selected.start + 4;
  1175. } else {
  1176. e.replaceSelection('`' + chunk + '`');
  1177. cursor = selected.start + 1;
  1178. }
  1179. // Set the cursor
  1180. e.setSelection(cursor, cursor + chunk.length);
  1181. }
  1182. }, {
  1183. name: 'cmdQuote',
  1184. hotkey: 'Ctrl+Q',
  1185. title: 'Quote',
  1186. icon: {
  1187. glyph: 'glyphicon glyphicon-comment',
  1188. fa: 'fa fa-quote-left',
  1189. 'fa-3': 'icon-quote-left',
  1190. octicons: 'octicon octicon-quote'
  1191. },
  1192. callback: function(e) {
  1193. // Prepend/Give - surround the selection
  1194. var chunk, cursor, selected = e.getSelection(),
  1195. content = e.getContent();
  1196. // transform selection and set the cursor into chunked text
  1197. if (selected.length === 0) {
  1198. // Give extra word
  1199. chunk = e.__localize('quote here');
  1200. e.replaceSelection('> ' + chunk);
  1201. // Set the cursor
  1202. cursor = selected.start + 2;
  1203. } else {
  1204. if (selected.text.indexOf('\n') < 0) {
  1205. chunk = selected.text;
  1206. e.replaceSelection('> ' + chunk);
  1207. // Set the cursor
  1208. cursor = selected.start + 2;
  1209. } else {
  1210. var list = [];
  1211. list = selected.text.split('\n');
  1212. chunk = list[0];
  1213. $.each(list, function(k, v) {
  1214. list[k] = '> ' + v;
  1215. });
  1216. e.replaceSelection('\n\n' + list.join('\n'));
  1217. // Set the cursor
  1218. cursor = selected.start + 4;
  1219. }
  1220. }
  1221. // Set the cursor
  1222. e.setSelection(cursor, cursor + chunk.length);
  1223. }
  1224. }]
  1225. }, {
  1226. name: 'groupUtil',
  1227. data: [{
  1228. name: 'cmdPreview',
  1229. toggle: true,
  1230. hotkey: 'Ctrl+P',
  1231. title: 'Preview',
  1232. btnText: 'Preview',
  1233. btnClass: 'btn btn-primary btn-sm',
  1234. icon: {
  1235. glyph: 'glyphicon glyphicon-search',
  1236. fa: 'fa fa-search',
  1237. 'fa-3': 'icon-search',
  1238. octicons: 'octicon octicon-search'
  1239. },
  1240. callback: function(e) {
  1241. // Check the preview mode and toggle based on this flag
  1242. var isPreview = e.$isPreview,
  1243. content;
  1244. if (isPreview === false) {
  1245. // Give flag that tells the editor to enter preview mode
  1246. e.showPreview();
  1247. } else {
  1248. e.hidePreview();
  1249. }
  1250. }
  1251. }]
  1252. }]
  1253. ],
  1254. customIcons: {},
  1255. additionalButtons: [], // Place to hook more buttons by code
  1256. reorderButtonGroups: [],
  1257. hiddenButtons: [], // Default hidden buttons
  1258. disabledButtons: [], // Default disabled buttons
  1259. footer: '',
  1260. fullscreen: {
  1261. enable: true,
  1262. icons: {
  1263. fullscreenOn: {
  1264. name: "fullscreenOn",
  1265. icon: {
  1266. fa: 'fa fa-expand',
  1267. glyph: 'glyphicon glyphicon-fullscreen',
  1268. 'fa-3': 'icon-resize-full',
  1269. octicons: 'octicon octicon-link-external'
  1270. }
  1271. },
  1272. fullscreenOff: {
  1273. name: "fullscreenOff",
  1274. icon: {
  1275. fa: 'fa fa-compress',
  1276. glyph: 'glyphicon glyphicon-fullscreen',
  1277. 'fa-3': 'icon-resize-small',
  1278. octicons: 'octicon octicon-browser'
  1279. }
  1280. }
  1281. }
  1282. },
  1283. /* Events hook */
  1284. onShow: function(e) {},
  1285. onPreview: function(e) {},
  1286. onPreviewEnd: function(e) {},
  1287. onSave: function(e) {},
  1288. onBlur: function(e) {},
  1289. onFocus: function(e) {},
  1290. onChange: function(e) {},
  1291. onFullscreen: function(e) {},
  1292. onFullscreenExit: function(e) {},
  1293. onSelect: function(e) {}
  1294. };
  1295. $.fn.markdown.Constructor = Markdown;
  1296. /* MARKDOWN NO CONFLICT
  1297. * ==================== */
  1298. $.fn.markdown.noConflict = function() {
  1299. $.fn.markdown = old;
  1300. return this;
  1301. };
  1302. /* MARKDOWN GLOBAL FUNCTION & DATA-API
  1303. * ==================================== */
  1304. var initMarkdown = function(el) {
  1305. var $this = el;
  1306. if ($this.data('markdown')) {
  1307. $this.data('markdown').showEditor();
  1308. return;
  1309. }
  1310. $this.markdown();
  1311. };
  1312. var blurNonFocused = function(e) {
  1313. var $activeElement = $(document.activeElement);
  1314. // Blur event
  1315. $(document).find('.md-editor').each(function() {
  1316. var $this = $(this),
  1317. focused = $activeElement.closest('.md-editor')[0] === this,
  1318. attachedMarkdown = $this.find('textarea').data('markdown') ||
  1319. $this.find('div[data-provider="markdown-preview"]').data('markdown');
  1320. if (attachedMarkdown && !focused) {
  1321. attachedMarkdown.blur();
  1322. }
  1323. });
  1324. };
  1325. $(document)
  1326. .on('click.markdown.data-api', '[data-provide="markdown-editable"]', function(e) {
  1327. initMarkdown($(this));
  1328. e.preventDefault();
  1329. })
  1330. .on('click focusin', function(e) {
  1331. blurNonFocused(e);
  1332. })
  1333. .ready(function() {
  1334. $('textarea[data-provide="markdown"]').each(function() {
  1335. initMarkdown($(this));
  1336. });
  1337. });
  1338. }));