暫無描述

ZSSRichTextEditor.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*!
  2. *
  3. * ZSSRichTextEditor v0.5.2
  4. * http://www.zedsaid.com
  5. *
  6. * Copyright 2014 Zed Said Studio LLC
  7. *
  8. */
  9. var zss_editor = {};
  10. // If we are using iOS or desktop
  11. zss_editor.isUsingiOS = true;
  12. // If the user is draging
  13. zss_editor.isDragging = false;
  14. // The current selection
  15. zss_editor.currentSelection;
  16. // The current editing image
  17. zss_editor.currentEditingImage;
  18. // The current editing link
  19. zss_editor.currentEditingLink;
  20. // The objects that are enabled
  21. zss_editor.enabledItems = {};
  22. // Height of content window, will be set by viewController
  23. zss_editor.contentHeight = 244;
  24. // Sets to true when extra footer gap shows and requires to hide
  25. zss_editor.updateScrollOffset = false;
  26. /**
  27. * The initializer function that must be called onLoad
  28. */
  29. function setupTouchEndEnableEditing(editorId) {
  30. $(`#${editorId}`).on('touchend', function(e) {
  31. zss_editor.enabledEditingItems(e);
  32. var clicked = $(e.target);
  33. if (!clicked.hasClass('zs_active')) {
  34. $('img').removeClass('zs_active');
  35. }
  36. });
  37. }
  38. function setupSelectionChange(editorId) {
  39. $(document).on('selectionchange',function(e){
  40. zss_editor.calculateEditorHeightWithCaretPosition(editorId);
  41. zss_editor.setScrollPosition();
  42. zss_editor.enabledEditingItems(e);
  43. });
  44. }
  45. function setupTouchEndFocus(editorId) {
  46. $(window).on('touchend', function(e) {
  47. if (!zss_editor.isDragging && (e.target.id == "zss_editor_footer"||e.target.nodeName.toLowerCase() == "html")) {
  48. zss_editor.focusEditor(editorId);
  49. }
  50. });
  51. }
  52. function disableLineBreakIfNecessary(editorId) {
  53. var editor = $(`#${editorId}`);
  54. var disableLineBreaks = editor.attr("disableLineBreaks");
  55. if (disableLineBreaks) {
  56. editor.keypress(function(e){ return e.which != 13; });
  57. }
  58. }
  59. zss_editor.init = function() {
  60. disableLineBreakIfNecessary('zss_editor_title');
  61. disableLineBreakIfNecessary('zss_editor_content');
  62. setupTouchEndEnableEditing('zss_editor_title');
  63. setupTouchEndEnableEditing('zss_editor_content');
  64. setupSelectionChange('zss_editor_title');
  65. setupSelectionChange('zss_editor_content');
  66. $(window).on('scroll', function(e) {
  67. zss_editor.updateOffset();
  68. });
  69. // Make sure that when we tap anywhere in the document we focus on the editor
  70. $(window).on('touchmove', function(e) {
  71. zss_editor.isDragging = true;
  72. zss_editor.updateScrollOffset = true;
  73. zss_editor.setScrollPosition();
  74. zss_editor.enabledEditingItems(e);
  75. });
  76. $(window).on('touchstart', function(e) {
  77. zss_editor.isDragging = false;
  78. });
  79. setupTouchEndFocus('zss_editor_title');
  80. setupTouchEndFocus('zss_editor_content');
  81. setTimeout(function() {
  82. WebViewBridge.send(JSON.stringify({type: 'ZSS_INITIALIZED'}))
  83. }, 20);
  84. }//end
  85. zss_editor.updateOffset = function() {
  86. if (!zss_editor.updateScrollOffset)
  87. return;
  88. var offsetY = window.document.body.scrollTop;
  89. var footer = $('#zss_editor_footer');
  90. var maxOffsetY = footer.offset().top - zss_editor.contentHeight;
  91. if (maxOffsetY < 0)
  92. maxOffsetY = 0;
  93. if (offsetY > maxOffsetY)
  94. {
  95. window.scrollTo(0, maxOffsetY);
  96. }
  97. zss_editor.setScrollPosition();
  98. }
  99. // This will show up in the XCode console as we are able to push this into an NSLog.
  100. zss_editor.debug = function(msg) {
  101. WebViewBridge.send(JSON.stringify({type: 'LOG', data: msg}));
  102. }
  103. zss_editor.setScrollPosition = function() {
  104. var position = window.pageYOffset;
  105. WebViewBridge.send(JSON.stringify({type: 'SCROLL', data: position}));
  106. }
  107. function setPlaceholder(editorId, placeholder) {
  108. var editor = $(`#${editorId}`);
  109. //set placeHolder
  110. editor.attr("placeholder",placeholder);
  111. //set focus
  112. editor.focusout(function(){
  113. var element = $(this);
  114. if (!element.text().trim().length) {
  115. element.empty();
  116. }
  117. });
  118. }
  119. zss_editor.setTitlePlaceholder = function(placeholder) {
  120. setPlaceholder('zss_editor_title', placeholder);
  121. }
  122. zss_editor.setContentPlaceholder = function(placeholder) {
  123. setPlaceholder('zss_editor_content', placeholder);
  124. }
  125. zss_editor.setFooterHeight = function(footerHeight) {
  126. var footer = $('#zss_editor_footer');
  127. footer.height(footerHeight + 'px');
  128. }
  129. zss_editor.getCaretYPosition = function() {
  130. var sel = window.getSelection();
  131. // Next line is comented to prevent deselecting selection. It looks like work but if there are any issues will appear then uconmment it as well as code above.
  132. //sel.collapseToStart();
  133. var range = sel.getRangeAt(0);
  134. var span = document.createElement('span');// something happening here preventing selection of elements
  135. range.collapse(false);
  136. range.insertNode(span);
  137. var topPosition = span.offsetTop;
  138. span.parentNode.removeChild(span);
  139. return topPosition;
  140. }
  141. zss_editor.calculateEditorHeightWithCaretPosition = function(editorId) {
  142. var padding = 50;
  143. var c = zss_editor.getCaretYPosition();
  144. var e = document.getElementById(editorId);
  145. var editor = $(`#${editorId}`);
  146. var offsetY = window.document.body.scrollTop;
  147. var height = zss_editor.contentHeight;
  148. var newPos = window.pageYOffset;
  149. if (c < offsetY) {
  150. newPos = c;
  151. } else if (c > (offsetY + height - padding)) {
  152. var newPos = c - height + padding - 18;
  153. }
  154. window.scrollTo(0, newPos);
  155. }
  156. zss_editor.backuprange = function(){
  157. var selection = window.getSelection();
  158. var range = selection.getRangeAt(0);
  159. zss_editor.currentSelection = {"startContainer": range.startContainer, "startOffset":range.startOffset,"endContainer":range.endContainer, "endOffset":range.endOffset};
  160. }
  161. zss_editor.restorerange = function(){
  162. var selection = window.getSelection();
  163. selection.removeAllRanges();
  164. var range = document.createRange();
  165. range.setStart(zss_editor.currentSelection.startContainer, zss_editor.currentSelection.startOffset);
  166. range.setEnd(zss_editor.currentSelection.endContainer, zss_editor.currentSelection.endOffset);
  167. selection.addRange(range);
  168. }
  169. zss_editor.getSelectedNode = function() {
  170. var node,selection;
  171. if (window.getSelection) {
  172. selection = getSelection();
  173. node = selection.anchorNode;
  174. }
  175. if (!node && document.selection) {
  176. selection = document.selection
  177. var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange();
  178. node = range.commonAncestorContainer ? range.commonAncestorContainer :
  179. range.parentElement ? range.parentElement() : range.item(0);
  180. }
  181. if (node) {
  182. return (node.nodeName == "#text" ? node.parentNode : node);
  183. }
  184. };
  185. zss_editor.setBold = function() {
  186. document.execCommand('bold', false, null);
  187. zss_editor.enabledEditingItems();
  188. }
  189. zss_editor.setItalic = function() {
  190. document.execCommand('italic', false, null);
  191. zss_editor.enabledEditingItems();
  192. }
  193. zss_editor.setSubscript = function() {
  194. document.execCommand('subscript', false, null);
  195. zss_editor.enabledEditingItems();
  196. }
  197. zss_editor.setSuperscript = function() {
  198. document.execCommand('superscript', false, null);
  199. zss_editor.enabledEditingItems();
  200. }
  201. zss_editor.setStrikeThrough = function() {
  202. document.execCommand('strikeThrough', false, null);
  203. zss_editor.enabledEditingItems();
  204. }
  205. zss_editor.setUnderline = function() {
  206. document.execCommand('underline', false, null);
  207. zss_editor.enabledEditingItems();
  208. }
  209. zss_editor.setBlockquote = function() {
  210. document.execCommand('formatBlock', false, '<blockquote>');
  211. zss_editor.enabledEditingItems();
  212. }
  213. zss_editor.removeFormating = function() {
  214. document.execCommand('removeFormat', false, null);
  215. zss_editor.enabledEditingItems();
  216. }
  217. zss_editor.setHorizontalRule = function() {
  218. document.execCommand('insertHorizontalRule', false, null);
  219. zss_editor.enabledEditingItems();
  220. }
  221. zss_editor.setHeading = function(heading) {
  222. var current_selection = $(zss_editor.getSelectedNode());
  223. var t = current_selection.prop("tagName").toLowerCase();
  224. var is_heading = (t == 'h1' || t == 'h2' || t == 'h3' || t == 'h4' || t == 'h5' || t == 'h6');
  225. if (is_heading && heading == t) {
  226. //var c = current_selection.html();
  227. //current_selection.replaceWith(c);
  228. } else {
  229. document.execCommand('formatBlock', false, '<'+heading+'>');
  230. }
  231. zss_editor.enabledEditingItems();
  232. }
  233. zss_editor.setParagraph = function() {
  234. var current_selection = $(zss_editor.getSelectedNode());
  235. var t = current_selection.prop("tagName").toLowerCase();
  236. var is_paragraph = (t == 'p');
  237. if (is_paragraph) {
  238. var c = current_selection.html();
  239. current_selection.replaceWith(c);
  240. } else {
  241. document.execCommand('formatBlock', false, '<p>');
  242. }
  243. zss_editor.enabledEditingItems();
  244. }
  245. // Need way to remove formatBlock
  246. console.log('WARNING: We need a way to remove formatBlock items');
  247. zss_editor.undo = function() {
  248. document.execCommand('undo', false, null);
  249. zss_editor.enabledEditingItems();
  250. }
  251. zss_editor.redo = function() {
  252. document.execCommand('redo', false, null);
  253. zss_editor.enabledEditingItems();
  254. }
  255. zss_editor.setOrderedList = function() {
  256. document.execCommand('insertOrderedList', false, null);
  257. zss_editor.enabledEditingItems();
  258. }
  259. zss_editor.setUnorderedList = function() {
  260. document.execCommand('insertUnorderedList', false, null);
  261. zss_editor.enabledEditingItems();
  262. }
  263. zss_editor.setJustifyCenter = function() {
  264. document.execCommand('justifyCenter', false, null);
  265. zss_editor.enabledEditingItems();
  266. }
  267. zss_editor.setJustifyFull = function() {
  268. document.execCommand('justifyFull', false, null);
  269. zss_editor.enabledEditingItems();
  270. }
  271. zss_editor.setJustifyLeft = function() {
  272. document.execCommand('justifyLeft', false, null);
  273. zss_editor.enabledEditingItems();
  274. }
  275. zss_editor.setJustifyRight = function() {
  276. document.execCommand('justifyRight', false, null);
  277. zss_editor.enabledEditingItems();
  278. }
  279. zss_editor.setIndent = function() {
  280. document.execCommand('indent', false, null);
  281. zss_editor.enabledEditingItems();
  282. }
  283. zss_editor.setOutdent = function() {
  284. document.execCommand('outdent', false, null);
  285. zss_editor.enabledEditingItems();
  286. }
  287. zss_editor.setFontFamily = function(fontFamily) {
  288. zss_editor.restorerange();
  289. document.execCommand("styleWithCSS", null, true);
  290. document.execCommand("fontName", false, fontFamily);
  291. document.execCommand("styleWithCSS", null, false);
  292. zss_editor.enabledEditingItems();
  293. }
  294. zss_editor.setTextColor = function(color) {
  295. zss_editor.restorerange();
  296. document.execCommand("styleWithCSS", null, true);
  297. document.execCommand('foreColor', false, color);
  298. document.execCommand("styleWithCSS", null, false);
  299. zss_editor.enabledEditingItems();
  300. // document.execCommand("removeFormat", false, "foreColor"); // Removes just foreColor
  301. }
  302. zss_editor.setBackgroundColor = function(color) {
  303. zss_editor.restorerange();
  304. document.execCommand("styleWithCSS", null, true);
  305. document.execCommand('hiliteColor', false, color);
  306. document.execCommand("styleWithCSS", null, false);
  307. zss_editor.enabledEditingItems();
  308. }
  309. // Needs addClass method
  310. zss_editor.insertLink = function(url, title) {
  311. zss_editor.restorerange();
  312. var sel = document.getSelection();
  313. console.log(sel);
  314. if (sel.toString().length != 0) {
  315. if (sel.rangeCount) {
  316. var el = document.createElement("a");
  317. el.setAttribute("href", url);
  318. el.setAttribute("title", title);
  319. var range = sel.getRangeAt(0).cloneRange();
  320. range.surroundContents(el);
  321. sel.removeAllRanges();
  322. sel.addRange(range);
  323. }
  324. }
  325. else
  326. {
  327. document.execCommand("insertHTML",false,"<a href='"+url+"'>"+title+"</a>");
  328. }
  329. zss_editor.enabledEditingItems();
  330. }
  331. zss_editor.updateLink = function(url, title) {
  332. zss_editor.restorerange();
  333. if (zss_editor.currentEditingLink) {
  334. var c = zss_editor.currentEditingLink;
  335. c.attr('href', url);
  336. c.attr('title', title);
  337. }
  338. zss_editor.enabledEditingItems();
  339. }//end
  340. zss_editor.updateImage = function(url, alt) {
  341. zss_editor.restorerange();
  342. if (zss_editor.currentEditingImage) {
  343. var c = zss_editor.currentEditingImage;
  344. c.attr('src', url);
  345. c.attr('alt', alt);
  346. }
  347. zss_editor.enabledEditingItems();
  348. }//end
  349. zss_editor.updateImageBase64String = function(imageBase64String, alt) {
  350. zss_editor.restorerange();
  351. if (zss_editor.currentEditingImage) {
  352. var c = zss_editor.currentEditingImage;
  353. var src = 'data:image/jpeg;base64,' + imageBase64String;
  354. c.attr('src', src);
  355. c.attr('alt', alt);
  356. }
  357. zss_editor.enabledEditingItems();
  358. }//end
  359. zss_editor.unlink = function() {
  360. if (zss_editor.currentEditingLink) {
  361. var c = zss_editor.currentEditingLink;
  362. c.contents().unwrap();
  363. }
  364. zss_editor.enabledEditingItems();
  365. }
  366. zss_editor.quickLink = function() {
  367. var sel = document.getSelection();
  368. var link_url = "";
  369. var test = new String(sel);
  370. var mailregexp = new RegExp("^(.+)(\@)(.+)$", "gi");
  371. if (test.search(mailregexp) == -1) {
  372. checkhttplink = new RegExp("^http\:\/\/", "gi");
  373. if (test.search(checkhttplink) == -1) {
  374. checkanchorlink = new RegExp("^\#", "gi");
  375. if (test.search(checkanchorlink) == -1) {
  376. link_url = "http://" + sel;
  377. } else {
  378. link_url = sel;
  379. }
  380. } else {
  381. link_url = sel;
  382. }
  383. } else {
  384. checkmaillink = new RegExp("^mailto\:", "gi");
  385. if (test.search(checkmaillink) == -1) {
  386. link_url = "mailto:" + sel;
  387. } else {
  388. link_url = sel;
  389. }
  390. }
  391. var html_code = '<a href="' + link_url + '">' + sel + '</a>';
  392. zss_editor.insertHTML(html_code);
  393. }
  394. zss_editor.prepareInsert = function() {
  395. zss_editor.backuprange();
  396. }
  397. zss_editor.insertImage = function(url, alt) {
  398. zss_editor.restorerange();
  399. var html = '<img src="'+url+'" alt="'+alt+'" />';
  400. zss_editor.insertHTML(html);
  401. zss_editor.enabledEditingItems();
  402. }
  403. zss_editor.insertImageBase64String = function(imageBase64String, alt) {
  404. zss_editor.restorerange();
  405. var html = '<img src="data:image/jpeg;base64,'+imageBase64String+'" alt="'+alt+'" />';
  406. zss_editor.insertHTML(html);
  407. zss_editor.enabledEditingItems();
  408. }
  409. function setHTML(editorId, html) {
  410. var editor = $(`#${editorId}`);
  411. editor.html(html);
  412. }
  413. zss_editor.setTitleHTML = function(html) {
  414. setHTML('zss_editor_title', html);
  415. }
  416. zss_editor.setContentHTML = function(html) {
  417. setHTML('zss_editor_content', html);
  418. }
  419. zss_editor.insertHTML = function(html) {
  420. document.execCommand('insertHTML', false, html);
  421. zss_editor.enabledEditingItems();
  422. }
  423. function getHtml(editorId) {
  424. // Images
  425. var img = $('img');
  426. if (img.length != 0) {
  427. $('img').removeClass('zs_active');
  428. $('img').each(function(index, e) {
  429. var image = $(this);
  430. var zs_class = image.attr('class');
  431. if (typeof(zs_class) != "undefined") {
  432. if (zs_class == '') {
  433. image.removeAttr('class');
  434. }
  435. }
  436. });
  437. }
  438. // Blockquote
  439. var bq = $('blockquote');
  440. if (bq.length != 0) {
  441. bq.each(function() {
  442. var b = $(this);
  443. if (b.css('border').indexOf('none') != -1) {
  444. b.css({'border': ''});
  445. }
  446. if (b.css('padding').indexOf('0px') != -1) {
  447. b.css({'padding': ''});
  448. }
  449. });
  450. }
  451. // Get the contents
  452. var h = document.getElementById(editorId).innerHTML;
  453. return h;
  454. }
  455. zss_editor.getTitleHTML = function() {
  456. return getHtml("zss_editor_title");
  457. }
  458. zss_editor.getContentHTML = function() {
  459. return getHtml("zss_editor_content");
  460. }
  461. zss_editor.getTitleText = function() {
  462. return $('#zss_editor_title').text();
  463. }
  464. zss_editor.getContentText = function() {
  465. return $('#zss_editor_content').text();
  466. }
  467. zss_editor.isCommandEnabled = function(commandName) {
  468. return document.queryCommandState(commandName);
  469. }
  470. zss_editor.enabledEditingItems = function(e) {
  471. console.log('enabledEditingItems');
  472. var items = [];
  473. if (zss_editor.isCommandEnabled('bold')) {
  474. items.push('bold');
  475. }
  476. if (zss_editor.isCommandEnabled('italic')) {
  477. items.push('italic');
  478. }
  479. if (zss_editor.isCommandEnabled('subscript')) {
  480. items.push('subscript');
  481. }
  482. if (zss_editor.isCommandEnabled('superscript')) {
  483. items.push('superscript');
  484. }
  485. if (zss_editor.isCommandEnabled('strikeThrough')) {
  486. items.push('strikeThrough');
  487. }
  488. if (zss_editor.isCommandEnabled('underline')) {
  489. items.push('underline');
  490. }
  491. if (zss_editor.isCommandEnabled('insertOrderedList')) {
  492. items.push('orderedList');
  493. }
  494. if (zss_editor.isCommandEnabled('insertUnorderedList')) {
  495. items.push('unorderedList');
  496. }
  497. if (zss_editor.isCommandEnabled('justifyCenter')) {
  498. items.push('justifyCenter');
  499. }
  500. if (zss_editor.isCommandEnabled('justifyFull')) {
  501. items.push('justifyFull');
  502. }
  503. if (zss_editor.isCommandEnabled('justifyLeft')) {
  504. items.push('justifyLeft');
  505. }
  506. if (zss_editor.isCommandEnabled('justifyRight')) {
  507. items.push('justifyRight');
  508. }
  509. if (zss_editor.isCommandEnabled('insertHorizontalRule')) {
  510. items.push('horizontalRule');
  511. }
  512. var formatBlock = document.queryCommandValue('formatBlock');
  513. if (formatBlock.length > 0) {
  514. items.push(formatBlock);
  515. }
  516. // Images
  517. $('img').bind('touchstart', function(e) {
  518. $('img').removeClass('zs_active');
  519. $(this).addClass('zs_active');
  520. });
  521. // Use jQuery to figure out those that are not supported
  522. if (typeof(e) != "undefined") {
  523. // The target element
  524. var s = zss_editor.getSelectedNode();
  525. var t = $(s);
  526. var nodeName = e.target.nodeName.toLowerCase();
  527. // Background Color
  528. var bgColor = t.css('backgroundColor');
  529. if (bgColor.length != 0 && bgColor != 'rgba(0, 0, 0, 0)' && bgColor != 'rgb(0, 0, 0)' && bgColor != 'transparent') {
  530. items.push('backgroundColor');
  531. }
  532. // Text Color
  533. var textColor = t.css('color');
  534. if (textColor.length != 0 && textColor != 'rgba(0, 0, 0, 0)' && textColor != 'rgb(0, 0, 0)' && textColor != 'transparent') {
  535. items.push('textColor');
  536. }
  537. //Fonts
  538. var font = t.css('font-family');
  539. if (font.length != 0 && font != 'Arial, Helvetica, sans-serif') {
  540. items.push('fonts');
  541. }
  542. // Link
  543. if (nodeName == 'a') {
  544. zss_editor.currentEditingLink = t;
  545. var title = t.attr('title');
  546. items.push('link:'+t.attr('href'));
  547. if (t.attr('title') !== undefined) {
  548. items.push('link-title:'+t.attr('title'));
  549. }
  550. } else {
  551. zss_editor.currentEditingLink = null;
  552. }
  553. // Blockquote
  554. if (nodeName == 'blockquote') {
  555. items.push('indent');
  556. }
  557. // Image
  558. if (nodeName == 'img') {
  559. zss_editor.currentEditingImage = t;
  560. items.push('image:'+t.attr('src'));
  561. if (t.attr('alt') !== undefined) {
  562. items.push('image-alt:'+t.attr('alt'));
  563. }
  564. } else {
  565. zss_editor.currentEditingImage = null;
  566. }
  567. }
  568. WebViewBridge.send(JSON.stringify({type: 'SELECTION_CHANGE', data: {items}}))
  569. }
  570. zss_editor.focusContent = function() {
  571. $('#zss_editor_content').focus();
  572. }
  573. zss_editor.focusTitle = function() {
  574. $('#zss_editor_title').focus();
  575. }
  576. zss_editor.focusEditor = function(editorId) {
  577. // the following was taken from http://stackoverflow.com/questions/1125292/how-to-move-cursor-to-end-of-contenteditable-entity/3866442#3866442
  578. // and ensures we move the cursor to the end of the editor
  579. var editor = $(`#${editorId}`);
  580. var range = document.createRange();
  581. range.selectNodeContents(editor.get(0));
  582. range.collapse(false);
  583. var selection = window.getSelection();
  584. selection.removeAllRanges();
  585. selection.addRange(range);
  586. editor.focus();
  587. }
  588. zss_editor.blurTitleEditor = function() {
  589. $('#zss_editor_title').blur();
  590. }
  591. zss_editor.blurContentEditor = function() {
  592. $('#zss_editor_content').blur();
  593. }
  594. zss_editor.setCustomCSS = function(customCSS) {
  595. document.getElementsByTagName('style')[0].innerHTML=customCSS;
  596. //set focus
  597. /*editor.focusout(function(){
  598. var element = $(this);
  599. if (!element.text().trim().length) {
  600. element.empty();
  601. }
  602. });*/
  603. }
  604. function addFocusEvent(editorId, callbackHandler) {
  605. var editor = $(`#${editorId}`);
  606. editor.focus(callbackHandler);
  607. }
  608. zss_editor.setTitleFocusHandler = function() {
  609. addFocusEvent('zss_editor_title', function() {
  610. WebViewBridge.send(JSON.stringify({type: 'TITLE_FOCUSED'}))
  611. });
  612. }
  613. zss_editor.setContentFocusHandler = function() {
  614. addFocusEvent('zss_editor_content', function() {
  615. WebViewBridge.send(JSON.stringify({type: 'CONTENT_FOCUSED'}))
  616. });
  617. }
  618. //end